aegir
Phlex-based simulation framework for the SHiP experiment.
Loading...
Searching...
No Matches
mc_particle_source.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 CERN for the benefit of the SHiP Collaboration
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5// mc_particle_source.hpp — shared helper for event-generator sources
6//
7// Phlex 0.3.0 lets a source register its data-product providers implicitly:
8// a phlex::source implements create_providers(selector), returning the
9// provider bundles that satisfy the requested product. Every aegir event
10// generator publishes the same products — the "mc_particles" collection
11// (std::vector<SHiP::MCParticle>) and the "event_header" record
12// (SHiP::EventHeader), both on the "event" layer — so the bundle
13// construction is factored out here.
14//
15// The header carries the per-event weight. Sources that sample uniformly
16// leave the generator empty and get the unweighted default (weight 1.0,
17// no originating id), which keeps the output schema identical across all
18// workflows; sources reading a weighted record (eventcalc_source) pass a
19// generator.
20
21#pragma once
22
23#include <SHiP/EventHeader.hpp>
24#include <SHiP/MCParticle.hpp>
25#include <functional>
26#include <string>
27#include <utility>
28#include <vector>
29
30#include "phlex/concurrency.hpp"
31#include "phlex/core/product_selector.hpp"
32#include "phlex/model/data_cell_index.hpp"
33#include "phlex/model/products.hpp"
34#include "phlex/source.hpp"
35
36namespace aegir {
37
38// Generator signature: produce the particles for a single data cell (event).
40 std::function<std::vector<SHiP::MCParticle>(phlex::data_cell_index const&)>;
41
42// Generator signature: produce the metadata for a single data cell. May be
43// empty, in which case the unweighted default is published.
45 std::function<SHiP::EventHeader(phlex::data_cell_index const&)>;
46
47// Build the implicit-provider bundle(s) for a source emitting the
48// "mc_particles" and "event_header" products, honouring the framework's
49// product selector. The generators are invoked once per data cell and their
50// results type-erased into Phlex products.
51inline phlex::detail::provider_bundles mc_particle_provider_bundles(
52 phlex::product_selector const& selector, mc_particle_generator generate,
53 phlex::concurrency max_concurrency,
54 event_header_generator generate_header = {}) {
55 using namespace phlex::experimental;
56 using namespace phlex::detail;
57
58 provider_bundles bundles;
59 std::string const layer = "event";
60 std::string const stage = "CURRENT";
61
62 product_specification particles_spec{
63 algorithm_name::create("mc_particles"), identifier{"particles"},
64 make_type_id<std::vector<SHiP::MCParticle>>()};
65
66 if (selector.match(particles_spec, identifier{layer}, identifier{stage})) {
67 bundles.push_back(provider_bundle{
68 .provider_function =
69 [generate = std::move(generate)](phlex::data_cell_index const& id)
70 -> product_ptr { return product_for(generate(id)); },
71 .max_concurrency = max_concurrency,
72 .spec = std::move(particles_spec),
73 .layer = layer,
74 .stage = stage});
75 }
76
77 product_specification header_spec{algorithm_name::create("event_header"),
78 identifier{"header"},
79 make_type_id<SHiP::EventHeader>()};
80
81 if (selector.match(header_spec, identifier{layer}, identifier{stage})) {
82 bundles.push_back(provider_bundle{
83 .provider_function =
84 [generate_header = std::move(generate_header)](
85 phlex::data_cell_index const& id) -> product_ptr {
86 // Unweighted default: every event counts once, no provenance.
87 if (!generate_header) return product_for(SHiP::EventHeader{});
88 return product_for(generate_header(id));
89 },
90 .max_concurrency = max_concurrency,
91 .spec = std::move(header_spec),
92 .layer = layer,
93 .stage = stage});
94 }
95
96 return bundles;
97}
98
99} // namespace aegir
Definition math_utils.hpp:12
std::function< std::vector< SHiP::MCParticle >(phlex::data_cell_index const &)> mc_particle_generator
Definition mc_particle_source.hpp:40
std::function< SHiP::EventHeader(phlex::data_cell_index const &)> event_header_generator
Definition mc_particle_source.hpp:45
phlex::detail::provider_bundles mc_particle_provider_bundles(phlex::product_selector const &selector, mc_particle_generator generate, phlex::concurrency max_concurrency, event_header_generator generate_header={})
Definition mc_particle_source.hpp:51