Project
Loading...
Searching...
No Matches
CollisionContextTool.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
12#include <boost/program_options.hpp>
13#include <string>
14#include <iostream>
16#include <regex>
22#include <cmath>
23#include <TRandom.h>
24#include <numeric>
25#include <fairlogger/Logger.h>
30#include "SimConfig/SimConfig.h"
31#include <filesystem>
32#include <sstream>
33#include <vector>
34#include <numeric>
35
36//
37// Created by Sandro Wenzel on 13.07.21.
38//
39
40// A utility to create/engineer (later modify/display) collision contexts
41
42// options struct filled from command line
43struct Options {
44 std::vector<std::string> interactionRates;
45 std::string qedInteraction; // specification for QED contribution
46 std::string outfilename; //
47 int orbits; // number of orbits to generate (can be a multiple of orbitsPerTF --> determine fraction or multiple of timeframes)
48 long seed; //
49 bool printContext = false;
50 std::string bcpatternfile;
51 int tfid = 0; // tfid -> used to calculate start orbit for collisions
52 double orbitsEarly = 0.; // how many orbits from a prev timeframe should still be kept in the current timeframe
53 double firstFractionalOrbit; // capture orbit and bunch crossing via decimal number
54 uint32_t firstOrbit = 0; // first orbit in run (orbit offset)
55 uint32_t firstBC = 0; // first bunch crossing (relative to firstOrbit) of the first interaction;
56 int orbitsPerTF = 256; // number of orbits per timeframe --> used to calculate start orbit for collisions
58 bool noEmptyTF = false; // prevent empty timeframes; the first interaction will be shifted backwards to fall within the range given by Options.orbits
59 int maxCollsPerTF = -1; // the maximal number of hadronic collisions per TF (can be used to constrain number of collisions per timeframe to some maximal value)
60 std::string configKeyValues = ""; // string to init config key values
61 long timestamp = -1; // timestamp for CCDB queries
62 std::string individualTFextraction = ""; // triggers extraction of individuel timeframe components when non-null
63 // format is path prefix
64 std::string vertexModeString{"kNoVertex"}; // Vertex Mode; vertices will be assigned to collisions of mode != kNoVertex
66 std::string external_path = ""; // optional external path where we can directly take the collision contexts
67 // This is useful when someone else is creating the contexts (MC-data embedding) and we
68 // merely want to pass these through. If this is given, we simply take the timeframe ID, number of orbits
69 // and copy the right amount of timeframes into the destination folder (implies individualTFextraction)
70 std::string nontrivial_mu_distribution = ""; // path to fetch a non-uniform MC(BC) distribution for the interaction sampler
71 // can be: (a) ccdb, (b) a ROOT file with the histogram included
72};
73
75 NOLOCK,
76 EVERYN,
78};
79
80struct CcdbUrl {
81 std::string server; // may include http:// or https://
82 std::string port; // empty if none
83 std::string fullPath; // everything after server[:port]/
84};
85
86std::optional<CcdbUrl> parseCcdbRegex(const std::string& url)
87{
88 static const std::regex re(
89 R"(^(?:ccdb://)(https?://[^/:]+|[^/:]+)(?::(\d+))?/(.+)$)");
90 std::smatch m;
91 if (!std::regex_match(url, m, re)) {
92 return std::nullopt;
93 }
94
95 CcdbUrl out;
96 out.server = m[1].str(); // server (may include http:// or https://)
97 out.port = m[2].str(); // optional port
98 out.fullPath = m[3].str(); // remainder
99 return out;
100}
101
103 std::string name; // name (prefix for transport simulation); may also serve as unique identifier
105 std::pair<int, float> synconto; // if this interaction locks on another interaction; takes precedence over interactionRate
107 char syncmodeop = 0; // syncmode operation ("@" --> embedd; "r" --> replace)
108 int mcnumberasked = -1; // number of MC events asked (but can be left -1) in which case it will be determined from timeframelength
109 int mcnumberavail = -1; // number of MC events avail (but can be left -1); if avail < asked there will be reuse of events
110 bool randomizeorder = false; // whether order of events will be randomized
111};
112
113InteractionSpec parseInteractionSpec(std::string const& specifier, std::vector<InteractionSpec> const& existingPatterns, bool adjustEventCount)
114{
115 // An interaction specification is a command-separated string
116 // of the following form:
117 // SPEC=NAMESTRING,INTERACTIONSTRING[,MCNUMBERSTRING]
118 //
119 // where
120 //
121 // NAMESTRING : a simple named specifier for the interaction; matching to a simulation prefix used by o2-sim
122 //
123 // INTERACTIONSTRING: irate | @ID:[ed]FLOATVALUE
124 // - either: a simple number irate specifying the interaction rate in kHz
125 // - or: a string such as @0:e5, saying that this interaction should match/sync
126 // with collisions of the 0-th interaction, but inject only every 5 collisions.
127 // Alternatively @0:d10000 means to inject but leaving a timedistance of at least 10000ns between signals
128 // - or: a string r0:e5, saying that this interaction should sync with collisions of the 0-th interaction but
129 // **overwrite** every 5-th interaction with a collision from this interaction name
130 // MCNUMBERSTRING: NUMBER1:r?NUMBER2 can specify how many collisions NUMBER1 to produce, taking from a sample of NUMBER2 available collisions
131 // - this option is only supported on the first interaction which is supposed to be the background interaction
132 // - if the 'r' character is present we randomize the order of the MC events
133
134 // tokens are separated by comma
135 std::vector<std::string> tokens = o2::RangeTokenizer::tokenize<std::string>(specifier);
136
137 float rate = -1.;
138 std::pair<int, float> synconto(-1, 1);
139
140 // extract (kinematics prefix) name
141 std::string name = tokens[0];
142
143 // extract the MC number spec if given
144 int collisionsasked = -1;
145 int collisionsavail = -1;
146 bool randomizeorder = false;
147 if (tokens.size() > 2) {
148 auto mctoken = tokens[2];
149 std::regex re("([0-9]*):(r?)([0-9]*)$", std::regex_constants::extended);
150
151 std::cmatch m;
152 if (std::regex_match(mctoken.c_str(), m, re)) {
153 collisionsasked = std::atoi(m[1].str().c_str());
154 if (m[2].str().compare("r") == 0) {
155 randomizeorder = true;
156 }
157 collisionsavail = std::atoi(m[3].str().c_str());
158 } else {
159 LOG(error) << "Could not parse " << mctoken << " as MCNUMBERSTRING";
160 exit(1);
161 }
162 }
163
164 if (adjustEventCount) {
165 // if the number of collisionsavail has not been specified, we should
166 // try to extract it from the kinematics directly
168 if (collisionsavail > 0) {
169 collisionsavail = std::min((size_t)collisionsavail, (size_t)mcreader.getNEvents(0));
170 } else {
171 collisionsavail = mcreader.getNEvents(0);
172 }
173 }
174 LOG(info) << "Collisions avail for " << name << " " << collisionsavail;
175
176 // extract interaction rate ... or locking
177 auto& interactionToken = tokens[1];
178 if (interactionToken[0] == '@' || interactionToken[0] == 'r') {
179 try {
180 // locking onto some other interaction
181 std::regex re("[@r]([0-9]*):([ed])([0-9]*[.]?[0-9]?)$", std::regex_constants::extended);
182
183 std::cmatch m;
184 if (std::regex_match(interactionToken.c_str(), m, re)) {
185 auto crossindex = std::atoi(m[1].str().c_str());
186 auto mode = m[2].str();
187 auto modevalue = std::atof(m[3].str().c_str());
188
189 if (crossindex > existingPatterns.size()) {
190 LOG(error) << "Reference to non-existent interaction spec";
191 exit(1);
192 }
193 synconto = std::pair<int, float>(crossindex, modevalue);
194
195 InteractionLockMode lockMode;
196 if (mode.compare("e") == 0) {
198 }
199 if (mode.compare("d") == 0) {
201 }
202 return InteractionSpec{name, rate, synconto, lockMode, interactionToken[0], collisionsasked, collisionsavail, randomizeorder};
203 } else {
204 LOG(error) << "Could not parse " << interactionToken << " as INTERACTIONSTRING";
205 exit(1);
206 }
207 } catch (std::regex_error e) {
208 LOG(error) << "Exception during regular expression match " << e.what();
209 exit(1);
210 }
211 } else {
212 rate = std::atof(interactionToken.c_str());
213 return InteractionSpec{name, rate, synconto, InteractionLockMode::NOLOCK, 0, collisionsasked, collisionsavail, randomizeorder};
214 }
215}
216
217bool parseOptions(int argc, char* argv[], Options& optvalues)
218{
219 namespace bpo = boost::program_options;
220 bpo::options_description options(
221 "A utility to create and manipulate digitization contexts (MC collision structure within a timeframe).\n\n"
222 "Allowed options");
223
224 options.add_options()(
225 "interactions,i", bpo::value<std::vector<std::string>>(&optvalues.interactionRates)->multitoken(), "name,IRate|LockSpecifier")(
226 "QEDinteraction", bpo::value<std::string>(&optvalues.qedInteraction)->default_value(""), "Interaction specifier for QED contribution (name,IRATE,maxeventnumber)")(
227 "outfile,o", bpo::value<std::string>(&optvalues.outfilename)->default_value("collisioncontext.root"), "Outfile of collision context")(
228 "orbits", bpo::value<int>(&optvalues.orbits)->default_value(-1),
229 "Number of orbits to generate maximally (if given, can be used to determine the number of timeframes). "
230 "Otherwise, the context will be generated by using collision numbers from the interaction specification.")(
231 "seed", bpo::value<long>(&optvalues.seed)->default_value(0L), "Seed for random number generator (for time sampling etc). Default 0: Random")(
232 "show-context", "Print generated collision context to terminal.")(
233 "bcPatternFile", bpo::value<std::string>(&optvalues.bcpatternfile)->default_value(""), "Interacting BC pattern file (e.g. from CreateBCPattern.C); Use \"ccdb\" when fetching from CCDB.")(
234 "orbitsPerTF", bpo::value<int>(&optvalues.orbitsPerTF)->default_value(256), "Orbits per timeframes")(
235 "orbitsEarly", bpo::value<double>(&optvalues.orbitsEarly)->default_value(0.), "Number of orbits with extra collisions prefixed to each timeframe")(
236 "use-existing-kine", "Read existing kinematics to adjust event counts")(
237 "timeframeID", bpo::value<int>(&optvalues.tfid)->default_value(0), "Timeframe id of the first timeframe int this context. Allows to generate contexts for different start orbits")(
238 "first-orbit", bpo::value<double>(&optvalues.firstFractionalOrbit)->default_value(0), "First (fractional) orbit in the run (HBFUtils.firstOrbit + BC from decimal)")(
239 "maxCollsPerTF", bpo::value<int>(&optvalues.maxCollsPerTF)->default_value(-1), "Maximal number of MC collisions to put into one timeframe. By default no constraint.")(
240 "noEmptyTF", bpo::bool_switch(&optvalues.noEmptyTF), "Enforce to have at least one collision")(
241 "configKeyValues", bpo::value<std::string>(&optvalues.configKeyValues)->default_value(""), "Semicolon separated key=value strings (e.g.: 'TPC.gasDensity=1;...')")(
242 "with-vertices", bpo::value<std::string>(&optvalues.vertexModeString)->default_value("kNoVertex"), "Assign vertices to collisions. Argument is the vertex mode. Defaults to no vertexing applied")(
243 "timestamp", bpo::value<long>(&optvalues.timestamp)->default_value(-1L), "Timestamp for CCDB queries / anchoring")(
244 "extract-per-timeframe", bpo::value<std::string>(&optvalues.individualTFextraction)->default_value(""),
245 "Extract individual timeframe contexts. Format required: time_frame_prefix[:comma_separated_list_of_signals_to_offset]")(
246 "import-external", bpo::value<std::string>(&optvalues.external_path)->default_value(""), "Take collision contexts (per timeframe) from external files for instance for data-anchoring use-case. Needs timeframeID and number of orbits to be given as well.")(
247 "nontrivial-mu-distribution", bpo::value<std::string>(&optvalues.nontrivial_mu_distribution)->default_value(""), "Distribution for MU(BC)");
248
249 options.add_options()("help,h", "Produce help message.");
250
251 bpo::variables_map vm;
252 try {
253 bpo::store(bpo::command_line_parser(argc, argv).options(options).run(), vm);
254 bpo::notify(vm);
255
256 // help
257 if (vm.count("help")) {
258 std::cout << options << std::endl;
259 return false;
260 }
261 if (vm.count("show-context")) {
262 optvalues.printContext = true;
263 }
264 if (vm.count("use-existing-kine")) {
265 optvalues.useexistingkinematics = true;
266 }
267
269
270 // fix the first orbit and bunch crossing
271 // auto orbitbcpair = parseOrbitAndBC(optvalues.firstIRString);
272 optvalues.firstOrbit = (uint32_t)optvalues.firstFractionalOrbit;
273 optvalues.firstBC = (uint32_t)((optvalues.firstFractionalOrbit - 1. * optvalues.firstOrbit) * o2::constants::lhc::LHCMaxBunches);
274 LOG(info) << "First orbit " << optvalues.firstOrbit;
275 LOG(info) << "First BC " << optvalues.firstBC;
276
277 } catch (const bpo::error& e) {
278 std::cerr << e.what() << "\n\n";
279 std::cerr << "Error parsing options; Available options:\n";
280 std::cerr << options << std::endl;
281 return false;
282 }
283 return true;
284}
285
286bool copy_collision_context(const std::string& external_path, int this_tf_id, int target_tf_id)
287{
288 namespace fs = std::filesystem;
289 try {
290 fs::path filename;
291 if (fs::exists(external_path) && fs::is_regular_file(external_path)) {
292 std::cout << "external_path is an existing file: " << external_path << "\n";
293 // use it directly
294 filename = fs::path(external_path);
295 } else {
296 // Construct source file path
297 filename = fs::path(external_path) / ("collission_context_" + std::to_string(this_tf_id) + ".root");
298 }
299
300 LOG(info) << "Checking existence of file: " << filename;
301
302 if (fs::exists(filename)) {
303 // Build destination path
304 std::string path_prefix = "tf"; // Can be made configurable
305 std::stringstream destination_path_stream;
306 destination_path_stream << path_prefix << (target_tf_id) << "/collisioncontext.root";
307 fs::path destination_path = destination_path_stream.str();
308
309 // Ensure parent directory exists
310 fs::path destination_dir = destination_path.parent_path();
311 if (!fs::exists(destination_dir)) {
312 fs::create_directories(destination_dir);
313 LOG(info) << "Created directory: " << destination_dir;
314 }
315
316 // Copy file
317 fs::copy_file(filename, destination_path, fs::copy_options::overwrite_existing);
318 LOG(info) << "Copied file to: " << destination_path;
319 return true;
320 } else {
321 LOG(warning) << "Source file does not exist: " << filename;
322 return false;
323 }
324 } catch (const fs::filesystem_error& e) {
325 LOG(error) << "Filesystem error: " << e.what();
326 return false;
327 } catch (const std::exception& e) {
328 LOG(error) << "Unexpected error: " << e.what();
329 return false;
330 }
331 return true;
332}
333
334int main(int argc, char* argv[])
335{
336 Options options;
337 if (!parseOptions(argc, argv, options)) {
338 exit(1);
339 }
340
341 // init params
343
344 // See if this is external mode, which simplifies things
345 if (options.external_path.size() > 0) {
346 // in this mode, we don't actually have to do much work.
347 // all we do is to
348 // - determine how many timeframes are asked
349 // - check if the right files are present in the external path (someone else needs to create/put them there)
350 // - check if the given contexts are consistent with options given (orbitsPerTF, ...)
351 // - copy the files into the MC destination folder (this implies timeframeextraction mode)
352 // - return
353
354 if (options.orbits < 0) {
355 LOG(error) << "External mode; orbits need to be given";
356 return 1;
357 }
358
359 if (options.orbitsPerTF == 0) {
360 LOG(error) << "External mode; need to have orbitsPerTF";
361 return 1;
362 }
363
364 if (options.individualTFextraction.size() == 0) {
365 LOG(error) << "External mode: This requires --extract-per-timeframe";
366 return 1;
367 }
368
369 // calculate number of timeframes
370 auto num_timeframes = options.orbits / options.orbitsPerTF;
371 LOG(info) << "External mode for " << num_timeframes << " consecutive timeframes; starting from " << options.tfid;
372
373 // loop over all timeframe ids - check if file is present - (check consistency) - copy to final destination
374 for (int i = 0; i < num_timeframes; ++i) {
375 auto this_tf_id = options.tfid + i;
376 if (!copy_collision_context(options.external_path, this_tf_id, i + 1)) {
377 return 1;
378 }
379 }
380 return 0;
381 }
382
383 // init random generator
384 gRandom->SetSeed(options.seed);
385
386 std::vector<InteractionSpec> ispecs;
387 // building the interaction spec
388 for (auto& i : options.interactionRates) {
389 // this is created as output from
390 ispecs.push_back(parseInteractionSpec(i, ispecs, options.useexistingkinematics));
391 }
392
393 std::vector<std::pair<o2::InteractionTimeRecord, std::vector<o2::steer::EventPart>>> collisions;
394 std::vector<o2::BunchFilling> bunchFillings; // vector of bunch filling objects; generated by interaction samplers
395
396 // now we generate the collision structure (interaction type by interaction type)
397 bool usetimeframelength = options.orbits > 0;
398
399 auto setBCFillingHelper = [&options](auto& sampler, auto& bcPatternString) {
400 if (bcPatternString == "ccdb") {
401 LOG(info) << "Fetch bcPattern information from CCDB";
402 // fetch the GRP Object
404 ccdb.setCaching(false);
405 ccdb.setLocalObjectValidityChecking(true);
406 auto grpLHC = ccdb.getForTimeStamp<o2::parameters::GRPLHCIFData>("GLO/Config/GRPLHCIF", options.timestamp);
407 LOG(info) << "Fetched injection scheme " << grpLHC->getInjectionScheme() << " from CCDB";
408 sampler.setBunchFilling(grpLHC->getBunchFilling());
409 } else {
410 sampler.setBunchFilling(bcPatternString);
411 }
412 };
413
414 // this is the starting orbit from which on we construct interactions (it is possibly shifted by one tf to the left
415 // in order to generate eventual "earlyOrbits"
416 auto orbitstart = options.firstOrbit + options.tfid * options.orbitsPerTF;
417 auto orbits_total = options.orbits;
418 if (options.orbitsEarly > 0.) {
419 orbitstart -= options.orbitsPerTF;
420 orbits_total += options.orbitsPerTF;
421 }
422
423 for (int id = 0; id < ispecs.size(); ++id) {
424 auto mode = ispecs[id].syncmode;
426 auto sampler = std::make_unique<o2::steer::InteractionSampler>();
427 TH1F* mu_hist = nullptr;
428
429 // we check if there is a realistic bunch crossing distribution available
430 const auto& mu_distr_source = options.nontrivial_mu_distribution;
431 if (mu_distr_source.size() > 0) {
432 if (mu_distr_source.find("ccdb") == 0) {
433 auto ccdb_info_wrapper = parseCcdbRegex(mu_distr_source);
434 if (!ccdb_info_wrapper.has_value()) {
435 LOG(error) << "Could not parse CCDB path for mu(bc) distribution";
436 } else {
437 auto& ccdb_info = ccdb_info_wrapper.value();
438
439 // for now construct a specific CCDBManager for this query
440 o2::ccdb::CCDBManagerInstance ccdb_inst(ccdb_info.server + std::string(":") + ccdb_info.port);
441 ccdb_inst.setFatalWhenNull(false);
442 auto local_hist = ccdb_inst.getForTimeStamp<TH1F>(ccdb_info.fullPath, options.timestamp);
443 if (local_hist) {
444 mu_hist = (TH1F*)(local_hist->Clone("h2")); // we need to clone since ownership of local_hist is with TFile
445 } else {
446 LOG(warn) << "No mu(bc) distribution found on CCDB. Using uniform one";
447 }
448 }
449 } else {
450 // we interpret the file as a ROOT file and open it to extract the wanted histogram
451 auto mudistr_file = TFile::Open(mu_distr_source.c_str(), "OPEN");
452 if (mudistr_file && !mudistr_file->IsZombie()) {
453 auto local_hist = mudistr_file->Get<TH1F>("hBcTVX");
454 mu_hist = (TH1F*)(local_hist->Clone("h2")); // we need to clone since ownership of local_hist is with TFile
455 mudistr_file->Close();
456 }
457 }
458 if (mu_hist) {
459 LOG(info) << "Found an external mu distribution with mean BC value " << mu_hist->GetMean();
460
461 // do some checks
462
463 // reset to correct interaction Sampler type
465 }
466 }
467
468 // for debug purposes: allows to instantiate trivial sampler
469 if (const char* env = getenv("ALICEO2_ENFORCE_TRIVIAL_BC_SAMPLER")) {
470 std::string spec(env);
471 std::regex re(R"((\d+):(\d+))");
472 std::smatch match;
473 int every_n = 1, mult = 1;
474 if (std::regex_match(spec, match, re)) {
475 every_n = std::stoi(match[1]);
476 mult = std::stoi(match[2]);
477 } else {
478 LOG(error) << "ALICEO2_ENFORCE_TRIVIAL_BC_SAMPLER format invalid, expected NUMBER_1:NUMBER_2";
479 exit(1);
480 }
481 sampler.reset(new o2::steer::FixedSkipBC_InteractionSampler(every_n, mult));
482 }
483
484 sampler->setInteractionRate(ispecs[id].interactionRate);
485 if (!options.bcpatternfile.empty()) {
486 setBCFillingHelper(*sampler, options.bcpatternfile);
487 }
488 sampler->init();
489 if (auto sampler_cast = dynamic_cast<o2::steer::NonUniformMuInteractionSampler*>(sampler.get())) {
490 if (mu_hist) {
491 sampler_cast->setBCIntensityScales(*mu_hist);
492 }
493 }
494
496 // this loop makes sure that the first collision is within the range of orbits asked (if noEmptyTF is enabled)
497 do {
498 sampler->setFirstIR(o2::InteractionRecord(options.firstBC, orbitstart));
499 record = sampler->generateCollisionTime();
500 } while (options.noEmptyTF && usetimeframelength && record.orbit >= orbitstart + orbits_total);
501 int count = 0;
502 do {
503 if (usetimeframelength && record.orbit >= orbitstart + orbits_total) {
504 break;
505 }
506 std::vector<o2::steer::EventPart> parts;
507 parts.emplace_back(id, count);
508
509 std::pair<o2::InteractionTimeRecord, std::vector<o2::steer::EventPart>> insertvalue(record, parts);
510 auto iter = std::lower_bound(collisions.begin(), collisions.end(), insertvalue, [](std::pair<o2::InteractionTimeRecord, std::vector<o2::steer::EventPart>> const& a, std::pair<o2::InteractionTimeRecord, std::vector<o2::steer::EventPart>> const& b) { return a.first < b.first; });
511 collisions.insert(iter, insertvalue);
512 record = sampler->generateCollisionTime();
513 count++;
514 } while ((ispecs[id].mcnumberasked > 0 && count < ispecs[id].mcnumberasked)); // TODO: this loop should probably be replaced by a condition with usetimeframelength and number of orbits
515
516 // we support randomization etc on non-injected/embedded interactions
517 // and we can apply them here
518 auto random_shuffle = [](auto first, auto last) {
519 auto n = last - first;
520 for (auto i = n - 1; i > 0; --i) {
521 using std::swap;
522 swap(first[i], first[(int)(gRandom->Rndm() * n)]);
523 }
524 };
525 std::vector<int> eventindices(count);
526 std::iota(eventindices.begin(), eventindices.end(), 0);
527 // apply randomization of order if any
528 if (ispecs[id].randomizeorder) {
529 random_shuffle(eventindices.begin(), eventindices.end());
530 }
531 if (ispecs[id].mcnumberavail > 0) {
532 // apply cutting to number of available entries
533 for (auto& e : eventindices) {
534 e = e % ispecs[id].mcnumberavail;
535 }
536 }
537 // make these transformations final:
538 for (auto& col : collisions) {
539 for (auto& part : col.second) {
540 if (part.sourceID == id) {
541 part.entryID = eventindices[part.entryID];
542 }
543 }
544 }
545
546 // keep bunch filling information produced by these samplers
547 bunchFillings.push_back(sampler->getBunchFilling());
548
549 } else {
550 // we are in some lock/sync mode and modify existing collisions
551 int lastcol = -1;
552 double lastcoltime = -1.;
553 auto distanceval = ispecs[id].synconto.second;
554 auto lockonto = ispecs[id].synconto.first;
555 int eventcount = 0;
556
557 for (int colid = 0; colid < collisions.size(); ++colid) {
558 auto& col = collisions[colid];
559 auto coltime = col.first.getTimeNS();
560
561 bool rightinteraction = false;
562 // we are locking only on collisions which have the referenced interaction present
563 // --> there must be an EventPart with the right sourceID
564 for (auto& eventPart : col.second) {
565 if (eventPart.sourceID == lockonto) {
566 rightinteraction = true;
567 break;
568 }
569 }
570 if (!rightinteraction) {
571 continue;
572 }
573
574 bool inject = false;
575 // we always start with first one
576 if (lastcol == -1) {
577 inject = true;
578 }
579 if (mode == InteractionLockMode::EVERYN && (colid - lastcol) >= distanceval) {
580 inject = true;
581 }
582 if (mode == InteractionLockMode::MINTIMEDISTANCE && (coltime - lastcoltime) >= distanceval) {
583 inject = true;
584 }
585
586 if (inject) {
587 if (ispecs[id].syncmodeop == 'r') {
588 LOG(debug) << "Replacing/overwriting another event ";
589 // Syncing is replacing; which means we need to take out the original
590 // event that we locked onto.
591 // We take out this event part immediately (and complain if there is a problem).
592 int index = 0;
593 auto iter = std::find_if(col.second.begin(), col.second.end(), [lockonto](auto val) { return lockonto == val.sourceID; });
594 if (iter != col.second.end()) {
595 col.second.erase(iter);
596 } else {
597 LOG(error) << "Expected to replace another event part but did not find one for source " << lockonto << " and collision " << colid;
598 }
599 }
600
601 if (ispecs[id].mcnumberavail >= 0) {
602 col.second.emplace_back(id, eventcount % ispecs[id].mcnumberavail);
603 } else {
604 col.second.emplace_back(id, eventcount);
605 }
606 eventcount++;
607 lastcol = colid;
608 lastcoltime = coltime;
609 }
610 }
611 }
612 }
613
614 // create DigitizationContext
616 // we can fill this container
617 auto& parts = digicontext.getEventParts();
618 // we can fill this container
619 auto& records = digicontext.getEventRecords();
620 // copy over information
621 size_t maxParts = 0;
622 for (auto& p : collisions) {
623 records.push_back(p.first);
624 parts.push_back(p.second);
625 maxParts = std::max(p.second.size(), maxParts);
626 }
627 digicontext.setNCollisions(collisions.size());
628 digicontext.setMaxNumberParts(maxParts);
629 // merge bunch filling info
630 for (int i = 1; i < bunchFillings.size(); ++i) {
631 bunchFillings[0].mergeWith(bunchFillings[i]);
632 }
633 digicontext.setBunchFilling(bunchFillings[0]);
634 std::vector<std::string> prefixes;
635 // Signal interaction rate
636 float sgnIRate = -1.;
637 for (auto& p : ispecs) {
638 prefixes.push_back(p.name);
639 // Set the interaction rate from the first pattern with a valid value.
640 // This handles both simple signal-only productions (where "sgn" has the rate)
641 // and embedding productions (where "bkg" has the rate and "sgn" syncs to it)
642 if (sgnIRate < 0 && p.interactionRate > 0) {
643 LOG(debug) << "Setting signal interaction rate to " << p.interactionRate << " Hz in the digitization context.";
644 sgnIRate = p.interactionRate;
645 digicontext.setDigitizerInteractionRate(p.interactionRate);
646 }
647 }
648 digicontext.setSimPrefixes(prefixes);
649
650 // <---- at this moment we have a dense collision context (not representing the final output we want)
651 LOG(info) << "<<------ DENSE CONTEXT ---------";
652 if (options.printContext) {
653 digicontext.printCollisionSummary();
654 }
655 LOG(info) << "-------- DENSE CONTEXT ------->>";
656
657 auto timeframeindices = digicontext.calcTimeframeIndices(orbitstart, options.orbitsPerTF, options.orbitsEarly);
658 LOG(info) << "Fixed " << timeframeindices.size() << " timeframes ";
659 for (auto p : timeframeindices) {
660 LOG(info) << std::get<0>(p) << " " << std::get<1>(p) << " " << std::get<2>(p);
661 }
662
663 // apply max collision per timeframe filters + reindexing of event id (linearisation and compactification)
664 digicontext.applyMaxCollisionFilter(timeframeindices, orbitstart, options.orbitsPerTF, options.maxCollsPerTF, options.orbitsEarly);
665
666 LOG(info) << "Timeframe indices after collision filter";
667 LOG(info) << "Fixed " << timeframeindices.size() << " timeframes ";
668 for (auto p : timeframeindices) {
669 LOG(info) << std::get<0>(p) << " " << std::get<1>(p) << " " << std::get<2>(p);
670 }
671
672 // <---- at this moment we have a dense collision context (not representing the final output we want)
673 LOG(info) << "<<------ FILTERED CONTEXT ---------";
674 if (options.printContext) {
675 digicontext.printCollisionSummary();
676 }
677 LOG(info) << "-------- FILTERED CONTEXT ------->>";
678
679 auto numTimeFrames = timeframeindices.size(); // digicontext.finalizeTimeframeStructure(orbitstart, options.orbitsPerTF, options.orbitsEarly);
680
682 switch (options.vertexMode) {
684 // fetch mean vertex from CCDB
686 if (meanv) {
687 LOG(info) << "Applying vertexing using CCDB mean vertex " << *meanv;
688 digicontext.sampleInteractionVertices(*meanv);
689 } else {
690 LOG(fatal) << "No vertex available";
691 }
692 break;
693 }
694
696 // init this vertex from CCDB or InteractionDiamond parameter
698 o2::dataformats::MeanVertexObject meanv(dparam.position[0], dparam.position[1], dparam.position[2], dparam.width[0], dparam.width[1], dparam.width[2], dparam.slopeX, dparam.slopeY);
699 LOG(info) << "Applying vertexing using DiamondParam mean vertex " << meanv;
700 digicontext.sampleInteractionVertices(meanv);
701 break;
702 }
703 default: {
704 LOG(error) << "Unknown vertex mode ... Not generating vertices";
705 }
706 }
707 }
708
709 // we fill QED contributions to the context
710 if (options.qedInteraction.size() > 0) {
711 // TODO: use bcFilling information
712 auto qedSpec = parseInteractionSpec(options.qedInteraction, ispecs, options.useexistingkinematics);
713 std::cout << "### IRATE " << qedSpec.interactionRate << "\n";
714 digicontext.fillQED(qedSpec.name, qedSpec.mcnumberasked, qedSpec.interactionRate);
715 }
716
717 if (options.printContext) {
718 digicontext.printCollisionSummary();
719 }
720 digicontext.saveToFile(options.outfilename);
721
722 // extract individual timeframes
723 if (options.individualTFextraction.size() > 0) {
724 // we are asked to extract individual timeframe components
725
726 LOG(info) << "Extracting individual timeframe collision contexts";
727 // extract prefix path to store these collision contexts
728 // Function to check the pattern and extract tokens from b
729 auto check_and_extract_tokens = [](const std::string& input, std::vector<std::string>& tokens) {
730 // the regular expression pattern for expected input format
731 const std::regex pattern(R"(^([a-zA-Z0-9]+)(:([a-zA-Z0-9]+(,[a-zA-Z0-9]+)*))?$)");
732 std::smatch matches;
733
734 // Check if the input matches the pattern
735 if (std::regex_match(input, matches, pattern)) {
736 // Clear any existing tokens in the vector
737 tokens.clear();
738
739 // matches[1] contains the part before the colon which we save first
740 tokens.push_back(matches[1].str());
741 // matches[2] contains the comma-separated list
742 std::string b = matches[2].str();
743 std::regex token_pattern(R"([a-zA-Z0-9]+)");
744 auto tokens_begin = std::sregex_iterator(b.begin(), b.end(), token_pattern);
745 auto tokens_end = std::sregex_iterator();
746
747 // Iterate over the tokens and add them to the vector
748 for (std::sregex_iterator i = tokens_begin; i != tokens_end; ++i) {
749 tokens.push_back((*i).str());
750 }
751 return true;
752 }
753 LOG(error) << "Argument for --extract-per-timeframe does not match specification";
754 return false;
755 };
756
757 std::vector<std::string> tokens;
758 if (check_and_extract_tokens(options.individualTFextraction, tokens)) {
759 auto path_prefix = tokens[0];
760 std::vector<int> sources_to_offset{};
761
762 LOG(info) << "PREFIX is " << path_prefix;
763
764 for (int i = 1; i < tokens.size(); ++i) {
765 LOG(info) << "Offsetting " << tokens[i];
766 sources_to_offset.push_back(digicontext.findSimPrefix(tokens[i]));
767 }
768
769 auto first_timeframe = options.orbitsEarly > 0. ? 1 : 0;
770 // now we are ready to loop over all timeframes
771 int tf_output_counter = 1;
772 for (int tf_id = first_timeframe; tf_id < numTimeFrames; ++tf_id) {
773 auto copy = digicontext.extractSingleTimeframe(tf_id, timeframeindices, sources_to_offset);
774
775 // each individual case gets QED interactions injected
776 // This should probably be done inside the extraction itself
777 if (digicontext.isQEDProvided()) {
778 auto qedSpec = parseInteractionSpec(options.qedInteraction, ispecs, options.useexistingkinematics);
779 copy.fillQED(qedSpec.name, qedSpec.mcnumberasked, qedSpec.interactionRate);
780 }
781
782 std::stringstream str;
783 str << path_prefix << tf_output_counter++ << "/collisioncontext.root";
784 copy.saveToFile(str.str());
785 LOG(info) << "---- CollisionContext for timeframe " << tf_id << " -----";
786 copy.printCollisionSummary();
787 }
788 }
789 }
790
791 return 0;
792}
std::vector< o2::soa::IndexRecord > records
std::string url
bool copy_collision_context(const std::string &external_path, int this_tf_id, int target_tf_id)
bool parseOptions(int argc, char *argv[], Options &optvalues)
std::optional< CcdbUrl > parseCcdbRegex(const std::string &url)
InteractionSpec parseInteractionSpec(std::string const &specifier, std::vector< InteractionSpec > const &existingPatterns, bool adjustEventCount)
std::ostringstream debug
int32_t i
container for the LHC InterFace data
Helper function to tokenize sequences and ranges of integral numbers.
uint32_t eventcount
Definition RawData.h:1
uint32_t col
Definition RawData.h:4
static BasicCCDBManager & instance()
T * getForTimeStamp(std::string const &path, long timestamp, std::map< std::string, std::string > *headers=nullptr)
retrieve an object of type T from CCDB as stored under path and timestamp. Optional to get the header...
void setFatalWhenNull(bool b)
set the fatal property (when false; nullptr object responses will not abort)
static void updateFromString(std::string const &)
static bool parseVertexModeString(std::string const &vertexstring, o2::conf::VertexMode &mode)
const std::string & getInjectionScheme() const
DigitizationContext extractSingleTimeframe(int timeframeid, std::vector< std::tuple< int, int, int > > const &timeframeindices, std::vector< int > const &sources_to_offset)
void fillQED(std::string_view QEDprefix, int max_events, double qedrate)
add QED contributions to context, giving prefix; maximal event number and qed interaction rate
void printCollisionSummary(bool withQED=false, int truncateOutputTo=-1) const
int findSimPrefix(std::string const &prefix) const
void applyMaxCollisionFilter(std::vector< std::tuple< int, int, int > > &timeframeindices, long startOrbit, long orbitsPerTF, int maxColl, double orbitsEarly=0.)
void setSimPrefixes(std::vector< std::string > const &p)
std::vector< o2::InteractionTimeRecord > & getEventRecords(bool withQED=false)
void sampleInteractionVertices(o2::dataformats::MeanVertexObject const &v)
std::vector< std::vector< o2::steer::EventPart > > & getEventParts(bool withQED=false)
void saveToFile(std::string_view filename) const
void setBunchFilling(o2::BunchFilling const &bf)
std::vector< std::tuple< int, int, int > > calcTimeframeIndices(long startOrbit, long orbitsPerTF, double orbitsEarly=0.) const
get timeframe structure --> index markers where timeframe starts/ends/is_influenced_by
void setDigitizerInteractionRate(float intRate)
size_t getNEvents(int source) const
Get number of events.
bool match(const std::vector< std::string > &queries, const char *pattern)
Definition dcs-ccdb.cxx:229
GLdouble n
Definition glcorearb.h:1982
const GLfloat * m
Definition glcorearb.h:4066
GLenum mode
Definition glcorearb.h:266
GLint GLsizei count
Definition glcorearb.h:399
GLuint GLenum * rate
Definition glcorearb.h:5735
GLuint sampler
Definition glcorearb.h:1630
GLuint index
Definition glcorearb.h:781
GLuint const GLchar * name
Definition glcorearb.h:781
GLint first
Definition glcorearb.h:399
GLboolean GLboolean GLboolean b
Definition glcorearb.h:1233
GLuint GLfloat * val
Definition glcorearb.h:1582
GLboolean GLboolean GLboolean GLboolean a
Definition glcorearb.h:1233
GLuint id
Definition glcorearb.h:650
constexpr int LHCMaxBunches
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
std::string filename()
std::string port
std::string server
std::string fullPath
InteractionLockMode syncmode
std::pair< int, float > synconto
std::string nontrivial_mu_distribution
std::string vertexModeString
std::string configKeyValues
Definition GRPTool.cxx:66
o2::conf::VertexMode vertexMode
std::string individualTFextraction
std::string external_path
std::string bcpatternfile
std::vector< std::string > interactionRates
uint64_t timestamp
Definition GRPTool.cxx:67
std::string outfilename
double firstFractionalOrbit
int orbitsPerTF
Definition GRPTool.cxx:51
std::string qedInteraction
uint32_t orbit
LHC orbit.
void compare(std::string_view s1, std::string_view s2)
#define main
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"
std::array< uint16_t, 5 > pattern
const std::string str