From bb7d6d7c70c46d64329ccc64b1f294c2831bd96a Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Mon, 16 Nov 2026 11:43:18 -0500 Subject: [PATCH 01/17] added parameters for EIC bunch structure --- include/MergerConfig.h | 5 ++++- src/CommandLineParser.cc | 27 ++++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/MergerConfig.h b/include/MergerConfig.h index d390af7..3534c34 100644 --- a/include/MergerConfig.h +++ b/include/MergerConfig.h @@ -36,8 +36,11 @@ struct SourceConfig { struct MergerConfig { bool introduce_offsets{true}; float timeframe_duration{2000.0f}; - float bunch_crossing_period{10.0f}; + float bunch_crossing_period{10.152f}; float global_phase{0.5f}; + size_t crossings_per_rev{1260}; + size_t abort_gap{100}; + size_t filled_crossings{1}; unsigned int random_seed{0}; // 0 means use random_device // Config per source diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index 393dad3..a9ebe45 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -14,8 +14,11 @@ void CommandLineParser::printUsage(const char* program_name) { << " -o, --output FILE Output file name (default: merged_timeframes.edm4hep.root)\n" << " -n, --nevents N Maximum number of timeframes to generate (default: 100)\n" << " -d, --duration TIME Timeframe duration in ns (default: 20.0)\n" - << " -p, --bunch-period PERIOD Bunch crossing period in ns (default: 10.0)\n" - << " -g, --global-phase Global phase adjustment in fraction of a crossing period (default: 0.0)\n" + << " -p, --bunch-period PERIOD Bunch crossing period in ns (default: 10.152)\n" + << " -g, --global-phase Global phase adjustment in fraction of a crossing period (default: 0.5)\n" + << " -r, --crossings_per_rev N Number of bunch crossings per revolution (default: 1260)" + << " -a, --abort_gap N Abort gap size in bunch crossings (default: 100)" + << " -l, --filled_crossings N Every N bunches filled (default: N=1 every crossing)" << " --random-seed SEED Random number generator seed (default: 0, use random_device)\n" << " --writer BACKEND Output writer: 'root' (TTree, default)\n" << " -h, --help Show this help message\n" @@ -196,6 +199,9 @@ void CommandLineParser::loadYAMLConfig(const std::string& config_file, MergerCon if (yaml["timeframe_duration"]) config.timeframe_duration = yaml["timeframe_duration"].as(); if (yaml["bunch_crossing_period"]) config.bunch_crossing_period = yaml["bunch_crossing_period"].as(); if (yaml["global_phase"]) config.global_phase = yaml["global_phase"].as(); + if (yaml["crossings_per_rev"]) config.crossings_per_rev = yaml["crossings_per_rev"].as(); + if (yaml["abort_gap"]) config.abort_gap = yaml["abort_gap"].as(); + if (yaml["filled_crossings"]) config.filled_crossings = yaml["filled_crossings"].as(); if (yaml["random_seed"]) config.random_seed = yaml["random_seed"].as(); if (yaml["introduce_offsets"]) config.introduce_offsets = yaml["introduce_offsets"].as(); @@ -344,6 +350,9 @@ void CommandLineParser::printConfiguration(const MergerConfig& config) { std::cout << "Timeframe duration: " << config.timeframe_duration << " ns" << std::endl; std::cout << "Bunch crossing period: " << config.bunch_crossing_period << " ns" << std::endl; std::cout << "Crossing global phase: " << config.global_phase << std::endl; + std::cout << "Crossings per revolution: " << config.crossings_per_rev << std::endl; + std::cout << "Abort gap: " << config.abort_gap << " crossings" << std::endl; + std::cout << "Filled Crossings: " << config.filled_crossings << std::endl; std::cout << "Random seed: " << config.random_seed << (config.random_seed == 0 ? " (using random_device)" : "") << std::endl; std::cout << "Introduce offsets: " << (config.introduce_offsets ? "true" : "false") << std::endl; std::cout << "Writer backend: " << config.writer << std::endl; @@ -394,7 +403,10 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { {"duration", required_argument, 0, 'd'}, {"frequency", required_argument, 0, 'f'}, {"bunch-period", required_argument, 0, 'p'}, - {"global-phase", required_argument, 0, 'p'}, + {"global-phase", required_argument, 0, 'g'}, + {"crossings-per-rev", required_argument, 0, 'r'}, + {"abort-gap", required_argument, 0, 'a'}, + {"filled_crossings", required_argument, 0, 'l'}, {"random-seed", required_argument, 0, 1005}, {"use-bunch-crossing", no_argument, 0, 'b'}, {"static-events", no_argument, 0, 's'}, @@ -433,6 +445,15 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { case 'p': config.bunch_crossing_period = std::stof(optarg); break; + case 'r': + config.crossings_per_rev = std::stoul(optarg); + break; + case 'a': + config.abort_gap = std::stoul(optarg); + break; + case 'l': + config.filled_crossings = std::stoul(optarg); + break; case 'b': default_source.use_bunch_crossing = true; break; From 6ae4bd2269adbe219c520f72d5c22a18ba4fd558 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Mon, 16 Nov 2026 16:01:22 -0500 Subject: [PATCH 02/17] fill bunch pattern, generate starting crossing --- include/DataHandler.h | 1 + include/MergerConfig.h | 4 +++- src/CommandLineParser.cc | 17 +++++++++++++++++ src/DataHandler.cc | 5 +++++ src/TimeframeBuilder.cc | 2 +- 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/include/DataHandler.h b/include/DataHandler.h index b53cca7..f0a1b21 100644 --- a/include/DataHandler.h +++ b/include/DataHandler.h @@ -55,6 +55,7 @@ class DataHandler { float timeframe_duration, float bunch_crossing_period, float global_phase, + size_t crossings_per_rev, std::mt19937& gen) final; /** diff --git a/include/MergerConfig.h b/include/MergerConfig.h index 3534c34..a491c6e 100644 --- a/include/MergerConfig.h +++ b/include/MergerConfig.h @@ -3,7 +3,6 @@ #include #include - struct SourceConfig { // Input/output configuration std::vector input_files; @@ -43,6 +42,9 @@ struct MergerConfig { size_t filled_crossings{1}; unsigned int random_seed{0}; // 0 means use random_device + // Bunch Pattern + std::vector bunch_pattern; + // Config per source std::vector sources; diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index a9ebe45..f5d9822 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -522,6 +522,23 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { // Print configuration printConfiguration(config); + + // Generate bunch pattern + size_t scaledn = 0; + for(size_t i=0; i=(config.crossings_per_rev-config.abort_gap) ){ + config.bunch_pattern[i] = false; // abort gap always empty + } + else{ + scaledn++; + if(scaledn==config.filled_crossings){ + scaledn=0; + config.bunch_pattern[i] = true; + } + else + config.bunch_pattern[i] = false; + } + } return config; } diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 85ab476..74f9a53 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -10,8 +10,13 @@ void DataHandler::mergeEvents(std::vector>& sources, float timeframe_duration, float bunch_crossing_period, float global_phase, + size_t crossings_per_rev, std::mt19937& gen) { current_timeframe_number_ = timeframe_number; + + // randomly select the starting crossing position for the timeframe + std::uniform_int_distribution uniform(0,(int)crossings_per_rev-1); + int start_crossing = uniform(gen); size_t total_events_consumed = 0; // Iterate over all sources diff --git a/src/TimeframeBuilder.cc b/src/TimeframeBuilder.cc index c34af76..441d05e 100644 --- a/src/TimeframeBuilder.cc +++ b/src/TimeframeBuilder.cc @@ -47,7 +47,7 @@ void TimeframeBuilder::run() { // Merge events from all sources data_handler_->mergeEvents( data_sources_, events_generated, m_config.timeframe_duration, - m_config.bunch_crossing_period, m_config.global_phase, gen); + m_config.bunch_crossing_period, m_config.global_phase, m_config.crossings_per_rev, gen); // Write the timeframe data_handler_->writeTimeframe(); From c4f20ff5bbcccb6111b372830ed1326b855e8429 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Mon, 16 Nov 2026 18:27:25 -0500 Subject: [PATCH 03/17] pass bunch_pattern down to generateTimeOffset() --- include/DataHandler.h | 1 + include/DataSource.h | 7 +++++-- src/DataHandler.cc | 3 ++- src/DataSource.cc | 9 ++++++--- src/TimeframeBuilder.cc | 3 ++- 5 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/DataHandler.h b/include/DataHandler.h index f0a1b21..bb58230 100644 --- a/include/DataHandler.h +++ b/include/DataHandler.h @@ -56,6 +56,7 @@ class DataHandler { float bunch_crossing_period, float global_phase, size_t crossings_per_rev, + const std::vector& bunch_pattern, std::mt19937& gen) final; /** diff --git a/include/DataSource.h b/include/DataSource.h index 01f10bc..28fb66b 100644 --- a/include/DataSource.h +++ b/include/DataSource.h @@ -40,7 +40,8 @@ class DataSource { // Event loading and time offset update virtual void loadEvent(size_t event_index) = 0; void UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, - float global_phase, std::mt19937& rng); + float global_phase, int start_crossing, + const std::vector& bunch_pattern, std::mt19937& rng); // Configuration access const SourceConfig& getConfig() const { return *config_; } @@ -82,5 +83,7 @@ class DataSource { // Shared time offset generation logic float generateTimeOffset(float distance, float timeframe_duration, - float bunch_crossing_period, float global_phase, std::mt19937& rng) const; + float bunch_crossing_period, float global_phase, + int start_crossing, const std::vector& bunch_pattern, + std::mt19937& rng) const; }; diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 74f9a53..3eb74f9 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -11,6 +11,7 @@ void DataHandler::mergeEvents(std::vector>& sources, float bunch_crossing_period, float global_phase, size_t crossings_per_rev, + const std::vector& bunch_pattern, std::mt19937& gen) { current_timeframe_number_ = timeframe_number; @@ -29,7 +30,7 @@ void DataHandler::mergeEvents(std::vector>& sources, for (size_t entry = 0; entry < entries_needed; ++entry) { // Load and prepare the event source->loadEvent(source->getCurrentEntryIndex()); - source->UpdateTimeOffset(timeframe_duration, bunch_crossing_period, global_phase, gen); + source->UpdateTimeOffset(timeframe_duration, bunch_crossing_period, global_phase, start_crossing, bunch_pattern, gen); // Call format-specific processing processEvent(*source); diff --git a/src/DataSource.cc b/src/DataSource.cc index 26c3246..4239dc9 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -5,6 +5,8 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, float global_phase, + int start_crossing, + const std::vector& bunch_pattern, std::mt19937& rng) { float distance = 0.0f; @@ -13,11 +15,12 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, distance = calculateBeamDistance(); } - current_time_offset_ = generateTimeOffset(distance, timeframe_duration, bunch_crossing_period, global_phase, rng); + current_time_offset_ = generateTimeOffset(distance, timeframe_duration, bunch_crossing_period, global_phase, start_crossing, bunch_pattern, rng); } float DataSource::generateTimeOffset(float distance, float timeframe_duration, - float bunch_crossing_period, float global_phase, std::mt19937& rng) const { + float bunch_crossing_period, float global_phase, int start_crossing, + const std::vector& bunch_pattern, std::mt19937& rng) const { const auto& config = getConfig(); std::uniform_real_distribution uniform(0.0f, timeframe_duration); @@ -26,7 +29,7 @@ float DataSource::generateTimeOffset(float distance, float timeframe_duration, if (!config.already_merged) { // Apply bunch crossing if enabled if (config.use_bunch_crossing) { - time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; + time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; } // Apply beam effects if enabled diff --git a/src/TimeframeBuilder.cc b/src/TimeframeBuilder.cc index 441d05e..54e6d91 100644 --- a/src/TimeframeBuilder.cc +++ b/src/TimeframeBuilder.cc @@ -47,7 +47,8 @@ void TimeframeBuilder::run() { // Merge events from all sources data_handler_->mergeEvents( data_sources_, events_generated, m_config.timeframe_duration, - m_config.bunch_crossing_period, m_config.global_phase, m_config.crossings_per_rev, gen); + m_config.bunch_crossing_period, m_config.global_phase, m_config.crossings_per_rev, + m_config.bunch_pattern, gen); // Write the timeframe data_handler_->writeTimeframe(); From f03b855766306f06f4eb51e7d6dc16699c9c8221 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Mon, 16 Nov 2026 18:46:56 -0500 Subject: [PATCH 04/17] add plumbing for using pattern with use_bunch_crossing --- include/DataSource.h | 5 +++-- src/DataHandler.cc | 3 ++- src/DataSource.cc | 22 ++++++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/include/DataSource.h b/include/DataSource.h index 28fb66b..e6f3c8c 100644 --- a/include/DataSource.h +++ b/include/DataSource.h @@ -40,7 +40,7 @@ class DataSource { // Event loading and time offset update virtual void loadEvent(size_t event_index) = 0; void UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, - float global_phase, int start_crossing, + float global_phase, int start_crossing, size_t crossings_per_rev, const std::vector& bunch_pattern, std::mt19937& rng); // Configuration access @@ -84,6 +84,7 @@ class DataSource { // Shared time offset generation logic float generateTimeOffset(float distance, float timeframe_duration, float bunch_crossing_period, float global_phase, - int start_crossing, const std::vector& bunch_pattern, + int start_crossing, size_t crossings_per_rev, + const std::vector& bunch_pattern, std::mt19937& rng) const; }; diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 3eb74f9..87cfca4 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -30,7 +30,8 @@ void DataHandler::mergeEvents(std::vector>& sources, for (size_t entry = 0; entry < entries_needed; ++entry) { // Load and prepare the event source->loadEvent(source->getCurrentEntryIndex()); - source->UpdateTimeOffset(timeframe_duration, bunch_crossing_period, global_phase, start_crossing, bunch_pattern, gen); + source->UpdateTimeOffset(timeframe_duration, bunch_crossing_period, global_phase, + start_crossing, crossings_per_rev, bunch_pattern, gen); // Call format-specific processing processEvent(*source); diff --git a/src/DataSource.cc b/src/DataSource.cc index 4239dc9..067a97e 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -6,6 +6,7 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, float global_phase, int start_crossing, + size_t crossings_per_rev, const std::vector& bunch_pattern, std::mt19937& rng) { float distance = 0.0f; @@ -15,12 +16,13 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, distance = calculateBeamDistance(); } - current_time_offset_ = generateTimeOffset(distance, timeframe_duration, bunch_crossing_period, global_phase, start_crossing, bunch_pattern, rng); + current_time_offset_ = generateTimeOffset(distance, timeframe_duration, bunch_crossing_period, global_phase, + start_crossing, crossings_per_rev, bunch_pattern, rng); } float DataSource::generateTimeOffset(float distance, float timeframe_duration, - float bunch_crossing_period, float global_phase, int start_crossing, - const std::vector& bunch_pattern, std::mt19937& rng) const { + float bunch_crossing_period, float global_phase, int start_crossing, \ + size_t crossings_per_rev, const std::vector& bunch_pattern, std::mt19937& rng) const { const auto& config = getConfig(); std::uniform_real_distribution uniform(0.0f, timeframe_duration); @@ -29,7 +31,19 @@ float DataSource::generateTimeOffset(float distance, float timeframe_duration, if (!config.already_merged) { // Apply bunch crossing if enabled if (config.use_bunch_crossing) { - time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; + + // Make sure this is a valid bunch crossing + bool found = false; + do{ + int cross_in_timeframe = std::floor(time_offset / bunch_crossing_period); + std::div_t cross_in_pattern = std::div(std::div(cross_in_timeframe, crossings_per_rev).rem + start_crossing, crossings_per_rev); + if(!bunch_pattern[cross_in_pattern.rem]) + time_offset = uniform(rng); + else + found = true; + }while(!found); + + time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; } // Apply beam effects if enabled From 94dd8551eb565b07d0d84ed6e11f2c4059935a59 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Mon, 16 Nov 2026 18:53:59 -0500 Subject: [PATCH 05/17] fix naming bug for new command line arguments --- src/CommandLineParser.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index f5d9822..7bef8b4 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -16,9 +16,9 @@ void CommandLineParser::printUsage(const char* program_name) { << " -d, --duration TIME Timeframe duration in ns (default: 20.0)\n" << " -p, --bunch-period PERIOD Bunch crossing period in ns (default: 10.152)\n" << " -g, --global-phase Global phase adjustment in fraction of a crossing period (default: 0.5)\n" - << " -r, --crossings_per_rev N Number of bunch crossings per revolution (default: 1260)" - << " -a, --abort_gap N Abort gap size in bunch crossings (default: 100)" - << " -l, --filled_crossings N Every N bunches filled (default: N=1 every crossing)" + << " -r, --crossings-per-rev N Number of bunch crossings per revolution (default: 1260)" + << " -a, --abort-gap N Abort gap size in bunch crossings (default: 100)" + << " -l, --filled-crossings N Every N bunches filled (default: N=1 every crossing)" << " --random-seed SEED Random number generator seed (default: 0, use random_device)\n" << " --writer BACKEND Output writer: 'root' (TTree, default)\n" << " -h, --help Show this help message\n" @@ -406,7 +406,7 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { {"global-phase", required_argument, 0, 'g'}, {"crossings-per-rev", required_argument, 0, 'r'}, {"abort-gap", required_argument, 0, 'a'}, - {"filled_crossings", required_argument, 0, 'l'}, + {"filled-crossings", required_argument, 0, 'l'}, {"random-seed", required_argument, 0, 1005}, {"use-bunch-crossing", no_argument, 0, 'b'}, {"static-events", no_argument, 0, 's'}, From e9d31b643df1073dfef6b2130290ce9d567a27a8 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Mon, 16 Nov 2026 19:09:51 -0500 Subject: [PATCH 06/17] multiple bug fixes --- src/CommandLineParser.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index 7bef8b4..d2b05fe 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -16,9 +16,9 @@ void CommandLineParser::printUsage(const char* program_name) { << " -d, --duration TIME Timeframe duration in ns (default: 20.0)\n" << " -p, --bunch-period PERIOD Bunch crossing period in ns (default: 10.152)\n" << " -g, --global-phase Global phase adjustment in fraction of a crossing period (default: 0.5)\n" - << " -r, --crossings-per-rev N Number of bunch crossings per revolution (default: 1260)" - << " -a, --abort-gap N Abort gap size in bunch crossings (default: 100)" - << " -l, --filled-crossings N Every N bunches filled (default: N=1 every crossing)" + << " -r, --crossings-per-rev N Number of bunch crossings per revolution (default: 1260)\n" + << " -a, --abort-gap N Abort gap size in bunch crossings (default: 100)\n" + << " -l, --filled-crossings N Every N bunches filled (default: N=1 every crossing)\n" << " --random-seed SEED Random number generator seed (default: 0, use random_device)\n" << " --writer BACKEND Output writer: 'root' (TTree, default)\n" << " -h, --help Show this help message\n" @@ -425,7 +425,7 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { int opt; int option_index = 0; - while ((opt = getopt_long(new_argc, new_argv, "o:n:d:f:p:bse:hv", long_options, &option_index)) != -1) { + while ((opt = getopt_long(new_argc, new_argv, "o:n:d:f:p:g:r:a:l:bse:hv", long_options, &option_index)) != -1) { switch (opt) { case 1004: config_file = optarg; @@ -445,6 +445,9 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { case 'p': config.bunch_crossing_period = std::stof(optarg); break; + case 'g': + config.global_phase = std::stof(optarg); + break; case 'r': config.crossings_per_rev = std::stoul(optarg); break; @@ -524,6 +527,7 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { printConfiguration(config); // Generate bunch pattern + config.bunch_pattern.assign(config.crossings_per_rev, false); size_t scaledn = 0; for(size_t i=0; i=(config.crossings_per_rev-config.abort_gap) ){ @@ -535,8 +539,6 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { scaledn=0; config.bunch_pattern[i] = true; } - else - config.bunch_pattern[i] = false; } } From 805e4a387d5c42c96ab2c3d70ef2d27f0d44c962 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Mon, 16 Nov 2026 19:13:45 -0500 Subject: [PATCH 07/17] fix usage output --- src/CommandLineParser.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index d2b05fe..9627fcc 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -15,7 +15,7 @@ void CommandLineParser::printUsage(const char* program_name) { << " -n, --nevents N Maximum number of timeframes to generate (default: 100)\n" << " -d, --duration TIME Timeframe duration in ns (default: 20.0)\n" << " -p, --bunch-period PERIOD Bunch crossing period in ns (default: 10.152)\n" - << " -g, --global-phase Global phase adjustment in fraction of a crossing period (default: 0.5)\n" + << " -g, --global-phase PHASE Global phase adjustment in fraction of a crossing period (default: 0.5)\n" << " -r, --crossings-per-rev N Number of bunch crossings per revolution (default: 1260)\n" << " -a, --abort-gap N Abort gap size in bunch crossings (default: 100)\n" << " -l, --filled-crossings N Every N bunches filled (default: N=1 every crossing)\n" From 6005474fd3d732750c9d2e905e9908801323a545 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Tue, 17 Nov 2026 03:20:21 -0500 Subject: [PATCH 08/17] constrain to crossing for use_bunch_crossing and attach_to_beam --- src/DataSource.cc | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/DataSource.cc b/src/DataSource.cc index 067a97e..578255b 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -29,11 +29,10 @@ float DataSource::generateTimeOffset(float distance, float timeframe_duration, float time_offset = uniform(rng); if (!config.already_merged) { - // Apply bunch crossing if enabled - if (config.use_bunch_crossing) { - - // Make sure this is a valid bunch crossing - bool found = false; + + // Constrain to a valid bunch crossing + if(config.use_bunch_crossing || config.attach_to_beam){ + bool found = false; do{ int cross_in_timeframe = std::floor(time_offset / bunch_crossing_period); std::div_t cross_in_pattern = std::div(std::div(cross_in_timeframe, crossings_per_rev).rem + start_crossing, crossings_per_rev); @@ -41,8 +40,11 @@ float DataSource::generateTimeOffset(float distance, float timeframe_duration, time_offset = uniform(rng); else found = true; - }while(!found); - + }while(!found); + } + + // Apply bunch crossing if enabled + if (config.use_bunch_crossing) { time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; } From 49c37521f57c2adc144407faba45debe79d68a94 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Tue, 17 Nov 2026 03:25:43 -0500 Subject: [PATCH 09/17] add pattern validity check --- src/CommandLineParser.cc | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index 9627fcc..a42995e 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -320,6 +320,20 @@ void CommandLineParser::validateConfiguration(MergerConfig& config) { if (config.sources.empty()) { throw std::runtime_error("Error: No valid sources with input files specified"); } + + // Check the bunch pattern - it must have at least one valid crossing + bool pattern_valid = false; + for(size_t i=0; i Date: Tue, 17 Nov 2026 13:44:20 -0500 Subject: [PATCH 10/17] scale events required from source by fraction of live crossings in timeframe --- src/DataHandler.cc | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 87cfca4..4930a09 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -17,13 +17,29 @@ void DataHandler::mergeEvents(std::vector>& sources, // randomly select the starting crossing position for the timeframe std::uniform_int_distribution uniform(0,(int)crossings_per_rev-1); - int start_crossing = uniform(gen); + int start_crossing = uniform(gen); + + // Calculate the populated vs. empty crossings in the timeframe + size_t populated_cross = 0; + size_t empty_cross = 0; + int cnt = start_crossing; + float cnt_time = 0.0; + do{ + if(bunch_pattern[cnt]) + populated_cross++; + else + empty_cross++; + cnt++; + if(cnt>=crossings_per_rev) cnt = 0; + cnt_time += bunch_crossing_period; + }while(cnt_timegetConfig(); - size_t entries_needed = source->getEntriesNeeded(); + size_t entries_needed = static_cast(source->getEntriesNeeded()* + (static_cast(populated_cross)/(populated_cross + empty_cross))); int events_consumed = 0; // Process each event from this source From 661a0b9a3089a62f6242e963b9cab61d620741ad Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Tue, 17 Nov 2026 14:04:24 -0500 Subject: [PATCH 11/17] added new paramenets to metadata --- src/HepMC3DataHandler.cc | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/HepMC3DataHandler.cc b/src/HepMC3DataHandler.cc index 5e7ccc2..81bf774 100644 --- a/src/HepMC3DataHandler.cc +++ b/src/HepMC3DataHandler.cc @@ -74,6 +74,9 @@ std::shared_ptr HepMC3DataHandler::configureMetadata(const M runInfo->add_attribute(metadata_prefix + "timeframe_duration_ns", std::make_shared(std::to_string(config.timeframe_duration))); runInfo->add_attribute(metadata_prefix + "bunch_crossing_period_ns", std::make_shared(std::to_string(config.bunch_crossing_period))); runInfo->add_attribute(metadata_prefix + "global_phase", std::make_shared(std::to_string(config.global_phase))); + runInfo->add_attribute(metadata_prefix + "crossings_per_rev", std::make_shared(std::to_string(config.crossings_per_rev))); + runInfo->add_attribute(metadata_prefix + "abort_gap", std::make_shared(std::to_string(config.abort_gap))); + runInfo->add_attribute(metadata_prefix + "filled_crossings", std::make_shared(std::to_string(config.filled_crossings))); runInfo->add_attribute(metadata_prefix + "max_events", std::make_shared(std::to_string(config.max_events))); runInfo->add_attribute(metadata_prefix + "random_seed", std::make_shared(std::to_string(config.random_seed))); From 9eed93ecdc6df819a60814c50cc1dd52ffee32d3 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Tue, 17 Nov 2026 18:08:25 -0500 Subject: [PATCH 12/17] cleanups --- src/CommandLineParser.cc | 17 +++++++---------- src/DataSource.cc | 5 +++-- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/CommandLineParser.cc b/src/CommandLineParser.cc index a42995e..31d42e1 100644 --- a/src/CommandLineParser.cc +++ b/src/CommandLineParser.cc @@ -536,17 +536,14 @@ MergerConfig CommandLineParser::parse(int argc, char* argv[]) { // Generate bunch pattern config.bunch_pattern.assign(config.crossings_per_rev, false); - size_t scaledn = 0; + size_t scaledn = 0; for(size_t i=0; i=(config.crossings_per_rev-config.abort_gap) ){ - config.bunch_pattern[i] = false; // abort gap always empty - } - else{ - scaledn++; - if(scaledn==config.filled_crossings){ - scaledn=0; - config.bunch_pattern[i] = true; - } + if( i<(config.crossings_per_rev-config.abort_gap) ){ // abort gap always empty + scaledn++; + if(scaledn==config.filled_crossings){ + scaledn=0; + config.bunch_pattern[i] = true; + } } } diff --git a/src/DataSource.cc b/src/DataSource.cc index 578255b..0068c54 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -1,6 +1,7 @@ // DataSource.cc - Base class implementation for shared functionality #include "DataSource.h" #include +#include void DataSource::UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, @@ -35,8 +36,8 @@ float DataSource::generateTimeOffset(float distance, float timeframe_duration, bool found = false; do{ int cross_in_timeframe = std::floor(time_offset / bunch_crossing_period); - std::div_t cross_in_pattern = std::div(std::div(cross_in_timeframe, crossings_per_rev).rem + start_crossing, crossings_per_rev); - if(!bunch_pattern[cross_in_pattern.rem]) + int cross_in_pattern = std::div(std::div(cross_in_timeframe, crossings_per_rev).rem + start_crossing, crossings_per_rev).rem; + if(!bunch_pattern[cross_in_pattern]) time_offset = uniform(rng); else found = true; From 5bb829fd9bbedefe72e57b2c0861c0be8b7519d3 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Tue, 17 Nov 2026 18:23:32 -0500 Subject: [PATCH 13/17] only scale if #events not forced --- src/DataHandler.cc | 6 ++++-- src/DataSource.cc | 5 ++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 4930a09..f7ad54f 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -38,8 +38,10 @@ void DataHandler::mergeEvents(std::vector>& sources, // Iterate over all sources for (auto& source : sources) { const auto& config = source->getConfig(); - size_t entries_needed = static_cast(source->getEntriesNeeded()* - (static_cast(populated_cross)/(populated_cross + empty_cross))); + size_t entries_needed = source->getEntriesNeeded(); + // scale entries if not forced + if(!config.static_number_of_events) entries_needed = static_cast(entries_needed*(static_cast(populated_cross)/(populated_cross + empty_cross))); + int events_consumed = 0; // Process each event from this source diff --git a/src/DataSource.cc b/src/DataSource.cc index 0068c54..73028a2 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -1,7 +1,6 @@ // DataSource.cc - Base class implementation for shared functionality #include "DataSource.h" #include -#include void DataSource::UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, @@ -45,8 +44,8 @@ float DataSource::generateTimeOffset(float distance, float timeframe_duration, } // Apply bunch crossing if enabled - if (config.use_bunch_crossing) { - time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; + if (config.use_bunch_crossing) { + time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; } // Apply beam effects if enabled From 7919ac2aa4d733f3ea8a9ce2963097137358f398 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Wed, 18 Nov 2026 05:23:58 -0500 Subject: [PATCH 14/17] protect against a timeframe with no valid crossings --- src/DataHandler.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/DataHandler.cc b/src/DataHandler.cc index f7ad54f..4581864 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -39,8 +39,8 @@ void DataHandler::mergeEvents(std::vector>& sources, for (auto& source : sources) { const auto& config = source->getConfig(); size_t entries_needed = source->getEntriesNeeded(); - // scale entries if not forced - if(!config.static_number_of_events) entries_needed = static_cast(entries_needed*(static_cast(populated_cross)/(populated_cross + empty_cross))); + // scale entries if not forced or timeframe has no valid crossings + if((!config.static_number_of_events) || (populated_cross==0)) entries_needed = static_cast(entries_needed*(static_cast(populated_cross)/(populated_cross + empty_cross))); int events_consumed = 0; From a4c412fef48e31a05c69134c2e46cdd21f1f6603 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Wed, 18 Nov 2026 11:19:02 -0500 Subject: [PATCH 15/17] simplify code, generate filled crossings pattern once per event --- include/DataSource.h | 4 ++-- src/DataHandler.cc | 25 ++++++++++++------------- src/DataSource.cc | 39 +++++++++++++++++++-------------------- 3 files changed, 33 insertions(+), 35 deletions(-) diff --git a/include/DataSource.h b/include/DataSource.h index e6f3c8c..38cd4e1 100644 --- a/include/DataSource.h +++ b/include/DataSource.h @@ -41,7 +41,7 @@ class DataSource { virtual void loadEvent(size_t event_index) = 0; void UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, float global_phase, int start_crossing, size_t crossings_per_rev, - const std::vector& bunch_pattern, std::mt19937& rng); + const std::vector& populated_crossing_times, std::mt19937& rng); // Configuration access const SourceConfig& getConfig() const { return *config_; } @@ -85,6 +85,6 @@ class DataSource { float generateTimeOffset(float distance, float timeframe_duration, float bunch_crossing_period, float global_phase, int start_crossing, size_t crossings_per_rev, - const std::vector& bunch_pattern, + const std::vector& populated_crossing_times, std::mt19937& rng) const; }; diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 4581864..2aec472 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -19,28 +19,27 @@ void DataHandler::mergeEvents(std::vector>& sources, std::uniform_int_distribution uniform(0,(int)crossings_per_rev-1); int start_crossing = uniform(gen); - // Calculate the populated vs. empty crossings in the timeframe - size_t populated_cross = 0; - size_t empty_cross = 0; + // Create a map of populated crossings in the timeframe int cnt = start_crossing; - float cnt_time = 0.0; + int cnt_total = 0; + int crossings_in_timeframe = std::floor(timeframe_duration/bunch_crossing_period); + std::vector populated_crossing_times; // start time of populated crossings do{ - if(bunch_pattern[cnt]) - populated_cross++; - else - empty_cross++; + if(bunch_pattern[cnt]) populated_crossing_times.push_back(cnt_total*bunch_crossing_period); cnt++; if(cnt>=crossings_per_rev) cnt = 0; - cnt_time += bunch_crossing_period; - }while(cnt_time(populated_crossing_times.size())/crossings_in_timeframe; + size_t total_events_consumed = 0; // Iterate over all sources for (auto& source : sources) { const auto& config = source->getConfig(); size_t entries_needed = source->getEntriesNeeded(); // scale entries if not forced or timeframe has no valid crossings - if((!config.static_number_of_events) || (populated_cross==0)) entries_needed = static_cast(entries_needed*(static_cast(populated_cross)/(populated_cross + empty_cross))); + if((!config.static_number_of_events) || (populated_crossing_times.size()==0)) entries_needed *= live_fraction; int events_consumed = 0; @@ -49,7 +48,7 @@ void DataHandler::mergeEvents(std::vector>& sources, // Load and prepare the event source->loadEvent(source->getCurrentEntryIndex()); source->UpdateTimeOffset(timeframe_duration, bunch_crossing_period, global_phase, - start_crossing, crossings_per_rev, bunch_pattern, gen); + start_crossing, crossings_per_rev, populated_crossing_times, gen); // Call format-specific processing processEvent(*source); diff --git a/src/DataSource.cc b/src/DataSource.cc index 73028a2..6a77bc2 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -7,7 +7,7 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, float global_phase, int start_crossing, size_t crossings_per_rev, - const std::vector& bunch_pattern, + const std::vector& populated_crossing_times, std::mt19937& rng) { float distance = 0.0f; @@ -17,41 +17,33 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, } current_time_offset_ = generateTimeOffset(distance, timeframe_duration, bunch_crossing_period, global_phase, - start_crossing, crossings_per_rev, bunch_pattern, rng); + start_crossing, crossings_per_rev, populated_crossing_times, rng); } float DataSource::generateTimeOffset(float distance, float timeframe_duration, float bunch_crossing_period, float global_phase, int start_crossing, \ - size_t crossings_per_rev, const std::vector& bunch_pattern, std::mt19937& rng) const { + size_t crossings_per_rev, const std::vector& populated_crossing_times, std::mt19937& rng) const { const auto& config = getConfig(); - std::uniform_real_distribution uniform(0.0f, timeframe_duration); - float time_offset = uniform(rng); + std::uniform_int_distribution uniform_int(0, populated_crossing_times.size()); + float time_offset = populated_crossing_times[uniform_int(rng)]; if (!config.already_merged) { - // Constrain to a valid bunch crossing - if(config.use_bunch_crossing || config.attach_to_beam){ - bool found = false; - do{ - int cross_in_timeframe = std::floor(time_offset / bunch_crossing_period); - int cross_in_pattern = std::div(std::div(cross_in_timeframe, crossings_per_rev).rem + start_crossing, crossings_per_rev).rem; - if(!bunch_pattern[cross_in_pattern]) - time_offset = uniform(rng); - else - found = true; - }while(!found); - } - // Apply bunch crossing if enabled if (config.use_bunch_crossing) { - time_offset = std::floor(time_offset / bunch_crossing_period) * bunch_crossing_period + global_phase * bunch_crossing_period; + time_offset += (global_phase * bunch_crossing_period); } // Apply beam effects if enabled if (config.attach_to_beam) { + + // randomize within the bunch_crossing_period + std::uniform_real_distribution uniform(0, bunch_crossing_period); + time_offset += uniform(rng); + // Add time offset based on distance along beam - time_offset += distance / config.beam_speed + global_phase * bunch_crossing_period; + time_offset += distance / config.beam_speed + (global_phase * bunch_crossing_period); // Add Gaussian spread if specified if (config.beam_spread > 0.0f) { @@ -60,6 +52,13 @@ float DataSource::generateTimeOffset(float distance, float timeframe_duration, } } } + else{ + + // randomize within the bunch_crossing_period + std::uniform_real_distribution uniform(0, bunch_crossing_period); + time_offset += uniform(rng); + + } return time_offset; } From 81e509525c37252fbaac979d5117d696751335b8 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Fri, 20 Nov 2026 14:18:07 -0500 Subject: [PATCH 16/17] bug fix, proper calculation for poisson sources --- include/DataSource.h | 3 +-- src/DataHandler.cc | 11 +++++++---- src/DataSource.cc | 13 ++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/include/DataSource.h b/include/DataSource.h index 38cd4e1..55c506b 100644 --- a/include/DataSource.h +++ b/include/DataSource.h @@ -82,9 +82,8 @@ class DataSource { float calculateBeamDistance() const; // Shared time offset generation logic - float generateTimeOffset(float distance, float timeframe_duration, + float generateTimeOffset(float distance, float bunch_crossing_period, float global_phase, - int start_crossing, size_t crossings_per_rev, const std::vector& populated_crossing_times, std::mt19937& rng) const; }; diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 2aec472..326228d 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -31,15 +31,18 @@ void DataHandler::mergeEvents(std::vector>& sources, cnt_total++; }while(cnt_total(populated_crossing_times.size())/crossings_in_timeframe; + double live_fraction = double(populated_crossing_times.size())/crossings_in_timeframe; size_t total_events_consumed = 0; // Iterate over all sources for (auto& source : sources) { const auto& config = source->getConfig(); - size_t entries_needed = source->getEntriesNeeded(); - // scale entries if not forced or timeframe has no valid crossings - if((!config.static_number_of_events) || (populated_crossing_times.size()==0)) entries_needed *= live_fraction; + + size_t entries_needed; + if (config.already_merged || config.static_number_of_events) + entries_needed = source->getEntriesNeeded(); + else + entries_needed = std::poisson_distribution<>(timeframe_duration * live_fraction * config.mean_event_frequency)(gen); int events_consumed = 0; diff --git a/src/DataSource.cc b/src/DataSource.cc index 6a77bc2..40a7777 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -5,7 +5,7 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, float global_phase, - int start_crossing, + int start_crossing, size_t crossings_per_rev, const std::vector& populated_crossing_times, std::mt19937& rng) { @@ -16,16 +16,15 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, distance = calculateBeamDistance(); } - current_time_offset_ = generateTimeOffset(distance, timeframe_duration, bunch_crossing_period, global_phase, - start_crossing, crossings_per_rev, populated_crossing_times, rng); + current_time_offset_ = generateTimeOffset(distance, bunch_crossing_period, global_phase, + populated_crossing_times, rng); } -float DataSource::generateTimeOffset(float distance, float timeframe_duration, - float bunch_crossing_period, float global_phase, int start_crossing, \ - size_t crossings_per_rev, const std::vector& populated_crossing_times, std::mt19937& rng) const { +float DataSource::generateTimeOffset(float distance, float bunch_crossing_period, float global_phase, + const std::vector& populated_crossing_times, std::mt19937& rng) const { const auto& config = getConfig(); - std::uniform_int_distribution uniform_int(0, populated_crossing_times.size()); + std::uniform_int_distribution uniform_int(0, populated_crossing_times.size()-1); float time_offset = populated_crossing_times[uniform_int(rng)]; if (!config.already_merged) { From ef7bdf812ac05258ffbad1ae9d02ae7adb6475b1 Mon Sep 17 00:00:00 2001 From: johnlajoie Date: Fri, 20 Nov 2026 16:09:42 -0500 Subject: [PATCH 17/17] proper conditioning on number of events selection, removed unused parameters passed --- include/DataSource.h | 5 ++--- src/DataHandler.cc | 13 ++++++++----- src/DataSource.cc | 30 ++++++++++++++++-------------- 3 files changed, 26 insertions(+), 22 deletions(-) diff --git a/include/DataSource.h b/include/DataSource.h index 55c506b..602bedf 100644 --- a/include/DataSource.h +++ b/include/DataSource.h @@ -40,8 +40,7 @@ class DataSource { // Event loading and time offset update virtual void loadEvent(size_t event_index) = 0; void UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, - float global_phase, int start_crossing, size_t crossings_per_rev, - const std::vector& populated_crossing_times, std::mt19937& rng); + float global_phase, const std::vector& populated_crossing_times, std::mt19937& rng); // Configuration access const SourceConfig& getConfig() const { return *config_; } @@ -82,7 +81,7 @@ class DataSource { float calculateBeamDistance() const; // Shared time offset generation logic - float generateTimeOffset(float distance, + float generateTimeOffset(float distance, float timeframe_duration, float bunch_crossing_period, float global_phase, const std::vector& populated_crossing_times, std::mt19937& rng) const; diff --git a/src/DataHandler.cc b/src/DataHandler.cc index 326228d..26cb12c 100644 --- a/src/DataHandler.cc +++ b/src/DataHandler.cc @@ -39,11 +39,14 @@ void DataHandler::mergeEvents(std::vector>& sources, const auto& config = source->getConfig(); size_t entries_needed; - if (config.already_merged || config.static_number_of_events) + if (config.already_merged || config.static_number_of_events) entries_needed = source->getEntriesNeeded(); - else - entries_needed = std::poisson_distribution<>(timeframe_duration * live_fraction * config.mean_event_frequency)(gen); - + else{ + if(config.attach_to_beam || config.use_bunch_crossing) + entries_needed = std::poisson_distribution<>(timeframe_duration * live_fraction * config.mean_event_frequency)(gen); + else + entries_needed = source->getEntriesNeeded(); + } int events_consumed = 0; // Process each event from this source @@ -51,7 +54,7 @@ void DataHandler::mergeEvents(std::vector>& sources, // Load and prepare the event source->loadEvent(source->getCurrentEntryIndex()); source->UpdateTimeOffset(timeframe_duration, bunch_crossing_period, global_phase, - start_crossing, crossings_per_rev, populated_crossing_times, gen); + populated_crossing_times, gen); // Call format-specific processing processEvent(*source); diff --git a/src/DataSource.cc b/src/DataSource.cc index 40a7777..609ecf9 100644 --- a/src/DataSource.cc +++ b/src/DataSource.cc @@ -5,8 +5,6 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, float bunch_crossing_period, float global_phase, - int start_crossing, - size_t crossings_per_rev, const std::vector& populated_crossing_times, std::mt19937& rng) { float distance = 0.0f; @@ -16,16 +14,26 @@ void DataSource::UpdateTimeOffset(float timeframe_duration, distance = calculateBeamDistance(); } - current_time_offset_ = generateTimeOffset(distance, bunch_crossing_period, global_phase, + current_time_offset_ = generateTimeOffset(distance, timeframe_duration, bunch_crossing_period, global_phase, populated_crossing_times, rng); } -float DataSource::generateTimeOffset(float distance, float bunch_crossing_period, float global_phase, +float DataSource::generateTimeOffset(float distance, float timeframe_duration, float bunch_crossing_period, float global_phase, const std::vector& populated_crossing_times, std::mt19937& rng) const { const auto& config = getConfig(); - - std::uniform_int_distribution uniform_int(0, populated_crossing_times.size()-1); - float time_offset = populated_crossing_times[uniform_int(rng)]; + + float time_offset = 0.0; + + if((populated_crossing_times.size()>0)&&(!config.already_merged && (config.use_bunch_crossing||config.attach_to_beam))){ + // constrain times to populated crossings + std::uniform_int_distribution uniform_int(0, populated_crossing_times.size()-1); + time_offset = populated_crossing_times[uniform_int(rng)]; + } + else{ + // randomize within the timeframe window + std::uniform_real_distribution uniform(0, timeframe_duration); + time_offset = uniform(rng); + } if (!config.already_merged) { @@ -33,7 +41,7 @@ float DataSource::generateTimeOffset(float distance, float bunch_crossing_period if (config.use_bunch_crossing) { time_offset += (global_phase * bunch_crossing_period); } - + // Apply beam effects if enabled if (config.attach_to_beam) { @@ -50,13 +58,7 @@ float DataSource::generateTimeOffset(float distance, float bunch_crossing_period time_offset += spread_dist(rng); } } - } - else{ - // randomize within the bunch_crossing_period - std::uniform_real_distribution uniform(0, bunch_crossing_period); - time_offset += uniform(rng); - } return time_offset;