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
33//
34// Created by Sandro Wenzel on 13.07.21.
35//
36
37// A utility to create/engineer (later modify/display) collision contexts
38
39// options struct filled from command line
40struct Options {
41 std::vector<std::string> interactionRates;
42 std::string qedInteraction; // specification for QED contribution
43 std::string outfilename; //
44 int orbits; // number of orbits to generate (can be a multiple of orbitsPerTF --> determine fraction or multiple of timeframes)
45 long seed; //
46 bool printContext = false;
47 std::string bcpatternfile;
48 int tfid = 0; // tfid -> used to calculate start orbit for collisions
49 double orbitsEarly = 0.; // how many orbits from a prev timeframe should still be kept in the current timeframe
50 double firstFractionalOrbit; // capture orbit and bunch crossing via decimal number
51 uint32_t firstOrbit = 0; // first orbit in run (orbit offset)
52 uint32_t firstBC = 0; // first bunch crossing (relative to firstOrbit) of the first interaction;
53 int orbitsPerTF = 256; // number of orbits per timeframe --> used to calculate start orbit for collisions
55 bool noEmptyTF = false; // prevent empty timeframes; the first interaction will be shifted backwards to fall within the range given by Options.orbits
56 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)
57 std::string configKeyValues = ""; // string to init config key values
58 long timestamp = -1; // timestamp for CCDB queries
59 std::string individualTFextraction = ""; // triggers extraction of individuel timeframe components when non-null
60 // format is path prefix
61 std::string vertexModeString{"kNoVertex"}; // Vertex Mode; vertices will be assigned to collisions of mode != kNoVertex
63 std::string external_path = ""; // optional external path where we can directly take the collision contexts
64 // This is useful when someone else is creating the contexts (MC-data embedding) and we
65 // merely want to pass these through. If this is given, we simply take the timeframe ID, number of orbits
66 // and copy the right amount of timeframes into the destination folder (implies individualTFextraction)
67};
68
70 NOLOCK,
71 EVERYN,
73};
74
76 std::string name; // name (prefix for transport simulation); may also serve as unique identifier
78 std::pair<int, float> synconto; // if this interaction locks on another interaction; takes precedence over interactionRate
80 char syncmodeop = 0; // syncmode operation ("@" --> embedd; "r" --> replace)
81 int mcnumberasked = -1; // number of MC events asked (but can be left -1) in which case it will be determined from timeframelength
82 int mcnumberavail = -1; // number of MC events avail (but can be left -1); if avail < asked there will be reuse of events
83 bool randomizeorder = false; // whether order of events will be randomized
84};
85
86InteractionSpec parseInteractionSpec(std::string const& specifier, std::vector<InteractionSpec> const& existingPatterns, bool adjustEventCount)
87{
88 // An interaction specification is a command-separated string
89 // of the following form:
90 // SPEC=NAMESTRING,INTERACTIONSTRING[,MCNUMBERSTRING]
91 //
92 // where
93 //
94 // NAMESTRING : a simple named specifier for the interaction; matching to a simulation prefix used by o2-sim
95 //
96 // INTERACTIONSTRING: irate | @ID:[ed]FLOATVALUE
97 // - either: a simple number irate specifying the interaction rate in kHz
98 // - or: a string such as @0:e5, saying that this interaction should match/sync
99 // with collisions of the 0-th interaction, but inject only every 5 collisions.
100 // Alternatively @0:d10000 means to inject but leaving a timedistance of at least 10000ns between signals
101 // - or: a string r0:e5, saying that this interaction should sync with collisions of the 0-th interaction but
102 // **overwrite** every 5-th interaction with a collision from this interaction name
103 // MCNUMBERSTRING: NUMBER1:r?NUMBER2 can specify how many collisions NUMBER1 to produce, taking from a sample of NUMBER2 available collisions
104 // - this option is only supported on the first interaction which is supposed to be the background interaction
105 // - if the 'r' character is present we randomize the order of the MC events
106
107 // tokens are separated by comma
108 std::vector<std::string> tokens = o2::RangeTokenizer::tokenize<std::string>(specifier);
109
110 float rate = -1.;
111 std::pair<int, float> synconto(-1, 1);
112
113 // extract (kinematics prefix) name
114 std::string name = tokens[0];
115
116 // extract the MC number spec if given
117 int collisionsasked = -1;
118 int collisionsavail = -1;
119 bool randomizeorder = false;
120 if (tokens.size() > 2) {
121 auto mctoken = tokens[2];
122 std::regex re("([0-9]*):(r?)([0-9]*)$", std::regex_constants::extended);
123
124 std::cmatch m;
125 if (std::regex_match(mctoken.c_str(), m, re)) {
126 collisionsasked = std::atoi(m[1].str().c_str());
127 if (m[2].str().compare("r") == 0) {
128 randomizeorder = true;
129 }
130 collisionsavail = std::atoi(m[3].str().c_str());
131 } else {
132 LOG(error) << "Could not parse " << mctoken << " as MCNUMBERSTRING";
133 exit(1);
134 }
135 }
136
137 if (adjustEventCount) {
138 // if the number of collisionsavail has not been specified, we should
139 // try to extract it from the kinematics directly
141 if (collisionsavail > 0) {
142 collisionsavail = std::min((size_t)collisionsavail, (size_t)mcreader.getNEvents(0));
143 } else {
144 collisionsavail = mcreader.getNEvents(0);
145 }
146 }
147 LOG(info) << "Collisions avail for " << name << " " << collisionsavail;
148
149 // extract interaction rate ... or locking
150 auto& interactionToken = tokens[1];
151 if (interactionToken[0] == '@' || interactionToken[0] == 'r') {
152 try {
153 // locking onto some other interaction
154 std::regex re("[@r]([0-9]*):([ed])([0-9]*[.]?[0-9]?)$", std::regex_constants::extended);
155
156 std::cmatch m;
157 if (std::regex_match(interactionToken.c_str(), m, re)) {
158 auto crossindex = std::atoi(m[1].str().c_str());
159 auto mode = m[2].str();
160 auto modevalue = std::atof(m[3].str().c_str());
161
162 if (crossindex > existingPatterns.size()) {
163 LOG(error) << "Reference to non-existent interaction spec";
164 exit(1);
165 }
166 synconto = std::pair<int, float>(crossindex, modevalue);
167
168 InteractionLockMode lockMode;
169 if (mode.compare("e") == 0) {
171 }
172 if (mode.compare("d") == 0) {
174 }
175 return InteractionSpec{name, rate, synconto, lockMode, interactionToken[0], collisionsasked, collisionsavail, randomizeorder};
176 } else {
177 LOG(error) << "Could not parse " << interactionToken << " as INTERACTIONSTRING";
178 exit(1);
179 }
180 } catch (std::regex_error e) {
181 LOG(error) << "Exception during regular expression match " << e.what();
182 exit(1);
183 }
184 } else {
185 rate = std::atof(interactionToken.c_str());
186 return InteractionSpec{name, rate, synconto, InteractionLockMode::NOLOCK, 0, collisionsasked, collisionsavail, randomizeorder};
187 }
188}
189
190bool parseOptions(int argc, char* argv[], Options& optvalues)
191{
192 namespace bpo = boost::program_options;
193 bpo::options_description options(
194 "A utility to create and manipulate digitization contexts (MC collision structure within a timeframe).\n\n"
195 "Allowed options");
196
197 options.add_options()(
198 "interactions,i", bpo::value<std::vector<std::string>>(&optvalues.interactionRates)->multitoken(), "name,IRate|LockSpecifier")(
199 "QEDinteraction", bpo::value<std::string>(&optvalues.qedInteraction)->default_value(""), "Interaction specifier for QED contribution (name,IRATE,maxeventnumber)")(
200 "outfile,o", bpo::value<std::string>(&optvalues.outfilename)->default_value("collisioncontext.root"), "Outfile of collision context")(
201 "orbits", bpo::value<int>(&optvalues.orbits)->default_value(-1),
202 "Number of orbits to generate maximally (if given, can be used to determine the number of timeframes). "
203 "Otherwise, the context will be generated by using collision numbers from the interaction specification.")(
204 "seed", bpo::value<long>(&optvalues.seed)->default_value(0L), "Seed for random number generator (for time sampling etc). Default 0: Random")(
205 "show-context", "Print generated collision context to terminal.")(
206 "bcPatternFile", bpo::value<std::string>(&optvalues.bcpatternfile)->default_value(""), "Interacting BC pattern file (e.g. from CreateBCPattern.C); Use \"ccdb\" when fetching from CCDB.")(
207 "orbitsPerTF", bpo::value<int>(&optvalues.orbitsPerTF)->default_value(256), "Orbits per timeframes")(
208 "orbitsEarly", bpo::value<double>(&optvalues.orbitsEarly)->default_value(0.), "Number of orbits with extra collisions prefixed to each timeframe")(
209 "use-existing-kine", "Read existing kinematics to adjust event counts")(
210 "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")(
211 "first-orbit", bpo::value<double>(&optvalues.firstFractionalOrbit)->default_value(0), "First (fractional) orbit in the run (HBFUtils.firstOrbit + BC from decimal)")(
212 "maxCollsPerTF", bpo::value<int>(&optvalues.maxCollsPerTF)->default_value(-1), "Maximal number of MC collisions to put into one timeframe. By default no constraint.")(
213 "noEmptyTF", bpo::bool_switch(&optvalues.noEmptyTF), "Enforce to have at least one collision")(
214 "configKeyValues", bpo::value<std::string>(&optvalues.configKeyValues)->default_value(""), "Semicolon separated key=value strings (e.g.: 'TPC.gasDensity=1;...')")(
215 "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")(
216 "timestamp", bpo::value<long>(&optvalues.timestamp)->default_value(-1L), "Timestamp for CCDB queries / anchoring")(
217 "extract-per-timeframe", bpo::value<std::string>(&optvalues.individualTFextraction)->default_value(""),
218 "Extract individual timeframe contexts. Format required: time_frame_prefix[:comma_separated_list_of_signals_to_offset]")(
219 "import-external", bpo::value<std::string>(&optvalues.external_path)->default_value(""),
220 "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.");
221
222 options.add_options()("help,h", "Produce help message.");
223
224 bpo::variables_map vm;
225 try {
226 bpo::store(bpo::command_line_parser(argc, argv).options(options).run(), vm);
227 bpo::notify(vm);
228
229 // help
230 if (vm.count("help")) {
231 std::cout << options << std::endl;
232 return false;
233 }
234 if (vm.count("show-context")) {
235 optvalues.printContext = true;
236 }
237 if (vm.count("use-existing-kine")) {
238 optvalues.useexistingkinematics = true;
239 }
240
242
243 // fix the first orbit and bunch crossing
244 // auto orbitbcpair = parseOrbitAndBC(optvalues.firstIRString);
245 optvalues.firstOrbit = (uint32_t)optvalues.firstFractionalOrbit;
246 optvalues.firstBC = (uint32_t)((optvalues.firstFractionalOrbit - 1. * optvalues.firstOrbit) * o2::constants::lhc::LHCMaxBunches);
247 LOG(info) << "First orbit " << optvalues.firstOrbit;
248 LOG(info) << "First BC " << optvalues.firstBC;
249
250 } catch (const bpo::error& e) {
251 std::cerr << e.what() << "\n\n";
252 std::cerr << "Error parsing options; Available options:\n";
253 std::cerr << options << std::endl;
254 return false;
255 }
256 return true;
257}
258
259bool copy_collision_context(const std::string& external_path, int this_tf_id, int target_tf_id)
260{
261 namespace fs = std::filesystem;
262 try {
263 // Construct source file path
264 fs::path filename = fs::path(external_path) / ("collission_context_" + std::to_string(this_tf_id) + ".root");
265
266 LOG(info) << "Checking existence of file: " << filename;
267
268 if (fs::exists(filename)) {
269 // Build destination path
270 std::string path_prefix = "tf"; // Can be made configurable
271 std::stringstream destination_path_stream;
272 destination_path_stream << path_prefix << (target_tf_id) << "/collisioncontext.root";
273 fs::path destination_path = destination_path_stream.str();
274
275 // Ensure parent directory exists
276 fs::path destination_dir = destination_path.parent_path();
277 if (!fs::exists(destination_dir)) {
278 fs::create_directories(destination_dir);
279 LOG(info) << "Created directory: " << destination_dir;
280 }
281
282 // Copy file
283 fs::copy_file(filename, destination_path, fs::copy_options::overwrite_existing);
284 LOG(info) << "Copied file to: " << destination_path;
285 return true;
286 } else {
287 LOG(warning) << "Source file does not exist: " << filename;
288 return false;
289 }
290 } catch (const fs::filesystem_error& e) {
291 LOG(error) << "Filesystem error: " << e.what();
292 return false;
293 } catch (const std::exception& e) {
294 LOG(error) << "Unexpected error: " << e.what();
295 return false;
296 }
297 return true;
298}
299
300int main(int argc, char* argv[])
301{
302 Options options;
303 if (!parseOptions(argc, argv, options)) {
304 exit(1);
305 }
306
307 // init params
309
310 // See if this is external mode, which simplifies things
311 if (options.external_path.size() > 0) {
312 // in this mode, we don't actually have to do much work.
313 // all we do is to
314 // - determine how many timeframes are asked
315 // - check if the right files are present in the external path (someone else needs to create/put them there)
316 // - check if the given contexts are consistent with options given (orbitsPerTF, ...)
317 // - copy the files into the MC destination folder (this implies timeframeextraction mode)
318 // - return
319
320 if (options.orbits < 0) {
321 LOG(error) << "External mode; orbits need to be given";
322 return 1;
323 }
324
325 if (options.orbitsPerTF == 0) {
326 LOG(error) << "External mode; need to have orbitsPerTF";
327 return 1;
328 }
329
330 if (options.individualTFextraction.size() == 0) {
331 LOG(error) << "External mode: This requires --extract-per-timeframe";
332 return 1;
333 }
334
335 // calculate number of timeframes
336 auto num_timeframes = options.orbits / options.orbitsPerTF;
337 LOG(info) << "External mode for " << num_timeframes << " consecutive timeframes; starting from " << options.tfid;
338
339 // loop over all timeframe ids - check if file is present - (check consistency) - copy to final destination
340 for (int i = 0; i < num_timeframes; ++i) {
341 auto this_tf_id = options.tfid + i;
342 if (!copy_collision_context(options.external_path, this_tf_id, i + 1)) {
343 return 1;
344 }
345 }
346 return 0;
347 }
348
349 // init random generator
350 gRandom->SetSeed(options.seed);
351
352 std::vector<InteractionSpec> ispecs;
353 // building the interaction spec
354 for (auto& i : options.interactionRates) {
355 // this is created as output from
356 ispecs.push_back(parseInteractionSpec(i, ispecs, options.useexistingkinematics));
357 }
358
359 std::vector<std::pair<o2::InteractionTimeRecord, std::vector<o2::steer::EventPart>>> collisions;
360 std::vector<o2::BunchFilling> bunchFillings; // vector of bunch filling objects; generated by interaction samplers
361
362 // now we generate the collision structure (interaction type by interaction type)
363 bool usetimeframelength = options.orbits > 0;
364
365 auto setBCFillingHelper = [&options](auto& sampler, auto& bcPatternString) {
366 if (bcPatternString == "ccdb") {
367 LOG(info) << "Fetch bcPattern information from CCDB";
368 // fetch the GRP Object
370 ccdb.setCaching(false);
371 ccdb.setLocalObjectValidityChecking(true);
372 auto grpLHC = ccdb.getForTimeStamp<o2::parameters::GRPLHCIFData>("GLO/Config/GRPLHCIF", options.timestamp);
373 LOG(info) << "Fetched injection scheme " << grpLHC->getInjectionScheme() << " from CCDB";
374 sampler.setBunchFilling(grpLHC->getBunchFilling());
375 } else {
376 sampler.setBunchFilling(bcPatternString);
377 }
378 };
379
380 // this is the starting orbit from which on we construct interactions (it is possibly shifted by one tf to the left
381 // in order to generate eventual "earlyOrbits"
382 auto orbitstart = options.firstOrbit + options.tfid * options.orbitsPerTF;
383 auto orbits_total = options.orbits;
384 if (options.orbitsEarly > 0.) {
385 orbitstart -= options.orbitsPerTF;
386 orbits_total += options.orbitsPerTF;
387 }
388
389 for (int id = 0; id < ispecs.size(); ++id) {
390 auto mode = ispecs[id].syncmode;
392 auto sampler = std::make_unique<o2::steer::InteractionSampler>();
393
394 // for debug purposes: allows to instantiate trivial sampler
395 if (const char* env = getenv("ALICEO2_ENFORCE_TRIVIAL_BC_SAMPLER")) {
396 std::string spec(env);
397 std::regex re(R"((\d+):(\d+))");
398 std::smatch match;
399 int every_n = 1, mult = 1;
400 if (std::regex_match(spec, match, re)) {
401 every_n = std::stoi(match[1]);
402 mult = std::stoi(match[2]);
403 } else {
404 LOG(error) << "ALICEO2_ENFORCE_TRIVIAL_BC_SAMPLER format invalid, expected NUMBER_1:NUMBER_2";
405 exit(1);
406 }
407 sampler.reset(new o2::steer::FixedSkipBC_InteractionSampler(every_n, mult));
408 }
409
410 sampler->setInteractionRate(ispecs[id].interactionRate);
411 if (!options.bcpatternfile.empty()) {
412 setBCFillingHelper(*sampler, options.bcpatternfile);
413 }
415 // this loop makes sure that the first collision is within the range of orbits asked (if noEmptyTF is enabled)
416 do {
417 sampler->setFirstIR(o2::InteractionRecord(options.firstBC, orbitstart));
418 sampler->init();
419 record = sampler->generateCollisionTime();
420 } while (options.noEmptyTF && usetimeframelength && record.orbit >= orbitstart + orbits_total);
421 int count = 0;
422 do {
423 if (usetimeframelength && record.orbit >= orbitstart + orbits_total) {
424 break;
425 }
426 std::vector<o2::steer::EventPart> parts;
427 parts.emplace_back(id, count);
428
429 std::pair<o2::InteractionTimeRecord, std::vector<o2::steer::EventPart>> insertvalue(record, parts);
430 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; });
431 collisions.insert(iter, insertvalue);
432 record = sampler->generateCollisionTime();
433 count++;
434 } 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
435
436 // we support randomization etc on non-injected/embedded interactions
437 // and we can apply them here
438 auto random_shuffle = [](auto first, auto last) {
439 auto n = last - first;
440 for (auto i = n - 1; i > 0; --i) {
441 using std::swap;
442 swap(first[i], first[(int)(gRandom->Rndm() * n)]);
443 }
444 };
445 std::vector<int> eventindices(count);
446 std::iota(eventindices.begin(), eventindices.end(), 0);
447 // apply randomization of order if any
448 if (ispecs[id].randomizeorder) {
449 random_shuffle(eventindices.begin(), eventindices.end());
450 }
451 if (ispecs[id].mcnumberavail > 0) {
452 // apply cutting to number of available entries
453 for (auto& e : eventindices) {
454 e = e % ispecs[id].mcnumberavail;
455 }
456 }
457 // make these transformations final:
458 for (auto& col : collisions) {
459 for (auto& part : col.second) {
460 if (part.sourceID == id) {
461 part.entryID = eventindices[part.entryID];
462 }
463 }
464 }
465
466 // keep bunch filling information produced by these samplers
467 bunchFillings.push_back(sampler->getBunchFilling());
468
469 } else {
470 // we are in some lock/sync mode and modify existing collisions
471 int lastcol = -1;
472 double lastcoltime = -1.;
473 auto distanceval = ispecs[id].synconto.second;
474 auto lockonto = ispecs[id].synconto.first;
475 int eventcount = 0;
476
477 for (int colid = 0; colid < collisions.size(); ++colid) {
478 auto& col = collisions[colid];
479 auto coltime = col.first.getTimeNS();
480
481 bool rightinteraction = false;
482 // we are locking only on collisions which have the referenced interaction present
483 // --> there must be an EventPart with the right sourceID
484 for (auto& eventPart : col.second) {
485 if (eventPart.sourceID == lockonto) {
486 rightinteraction = true;
487 break;
488 }
489 }
490 if (!rightinteraction) {
491 continue;
492 }
493
494 bool inject = false;
495 // we always start with first one
496 if (lastcol == -1) {
497 inject = true;
498 }
499 if (mode == InteractionLockMode::EVERYN && (colid - lastcol) >= distanceval) {
500 inject = true;
501 }
502 if (mode == InteractionLockMode::MINTIMEDISTANCE && (coltime - lastcoltime) >= distanceval) {
503 inject = true;
504 }
505
506 if (inject) {
507 if (ispecs[id].syncmodeop == 'r') {
508 LOG(debug) << "Replacing/overwriting another event ";
509 // Syncing is replacing; which means we need to take out the original
510 // event that we locked onto.
511 // We take out this event part immediately (and complain if there is a problem).
512 int index = 0;
513 auto iter = std::find_if(col.second.begin(), col.second.end(), [lockonto](auto val) { return lockonto == val.sourceID; });
514 if (iter != col.second.end()) {
515 col.second.erase(iter);
516 } else {
517 LOG(error) << "Expected to replace another event part but did not find one for source " << lockonto << " and collision " << colid;
518 }
519 }
520
521 if (ispecs[id].mcnumberavail >= 0) {
522 col.second.emplace_back(id, eventcount % ispecs[id].mcnumberavail);
523 } else {
524 col.second.emplace_back(id, eventcount);
525 }
526 eventcount++;
527 lastcol = colid;
528 lastcoltime = coltime;
529 }
530 }
531 }
532 }
533
534 // create DigitizationContext
536 // we can fill this container
537 auto& parts = digicontext.getEventParts();
538 // we can fill this container
539 auto& records = digicontext.getEventRecords();
540 // copy over information
541 size_t maxParts = 0;
542 for (auto& p : collisions) {
543 records.push_back(p.first);
544 parts.push_back(p.second);
545 maxParts = std::max(p.second.size(), maxParts);
546 }
547 digicontext.setNCollisions(collisions.size());
548 digicontext.setMaxNumberParts(maxParts);
549 // merge bunch filling info
550 for (int i = 1; i < bunchFillings.size(); ++i) {
551 bunchFillings[0].mergeWith(bunchFillings[i]);
552 }
553 digicontext.setBunchFilling(bunchFillings[0]);
554 std::vector<std::string> prefixes;
555 for (auto& p : ispecs) {
556 prefixes.push_back(p.name);
557 }
558 digicontext.setSimPrefixes(prefixes);
559
560 // <---- at this moment we have a dense collision context (not representing the final output we want)
561 LOG(info) << "<<------ DENSE CONTEXT ---------";
562 if (options.printContext) {
563 digicontext.printCollisionSummary();
564 }
565 LOG(info) << "-------- DENSE CONTEXT ------->>";
566
567 auto timeframeindices = digicontext.calcTimeframeIndices(orbitstart, options.orbitsPerTF, options.orbitsEarly);
568 LOG(info) << "Fixed " << timeframeindices.size() << " timeframes ";
569 for (auto p : timeframeindices) {
570 LOG(info) << std::get<0>(p) << " " << std::get<1>(p) << " " << std::get<2>(p);
571 }
572
573 // apply max collision per timeframe filters + reindexing of event id (linearisation and compactification)
574 digicontext.applyMaxCollisionFilter(timeframeindices, orbitstart, options.orbitsPerTF, options.maxCollsPerTF, options.orbitsEarly);
575
576 LOG(info) << "Timeframe indices after collision filter";
577 LOG(info) << "Fixed " << timeframeindices.size() << " timeframes ";
578 for (auto p : timeframeindices) {
579 LOG(info) << std::get<0>(p) << " " << std::get<1>(p) << " " << std::get<2>(p);
580 }
581
582 // <---- at this moment we have a dense collision context (not representing the final output we want)
583 LOG(info) << "<<------ FILTERED CONTEXT ---------";
584 if (options.printContext) {
585 digicontext.printCollisionSummary();
586 }
587 LOG(info) << "-------- FILTERED CONTEXT ------->>";
588
589 auto numTimeFrames = timeframeindices.size(); // digicontext.finalizeTimeframeStructure(orbitstart, options.orbitsPerTF, options.orbitsEarly);
590
592 switch (options.vertexMode) {
594 // fetch mean vertex from CCDB
596 if (meanv) {
597 LOG(info) << "Applying vertexing using CCDB mean vertex " << *meanv;
598 digicontext.sampleInteractionVertices(*meanv);
599 } else {
600 LOG(fatal) << "No vertex available";
601 }
602 break;
603 }
604
606 // init this vertex from CCDB or InteractionDiamond parameter
608 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);
609 LOG(info) << "Applying vertexing using DiamondParam mean vertex " << meanv;
610 digicontext.sampleInteractionVertices(meanv);
611 break;
612 }
613 default: {
614 LOG(error) << "Unknown vertex mode ... Not generating vertices";
615 }
616 }
617 }
618
619 // we fill QED contributions to the context
620 if (options.qedInteraction.size() > 0) {
621 // TODO: use bcFilling information
622 auto qedSpec = parseInteractionSpec(options.qedInteraction, ispecs, options.useexistingkinematics);
623 std::cout << "### IRATE " << qedSpec.interactionRate << "\n";
624 digicontext.fillQED(qedSpec.name, qedSpec.mcnumberasked, qedSpec.interactionRate);
625 }
626
627 if (options.printContext) {
628 digicontext.printCollisionSummary();
629 }
630 digicontext.saveToFile(options.outfilename);
631
632 // extract individual timeframes
633 if (options.individualTFextraction.size() > 0) {
634 // we are asked to extract individual timeframe components
635
636 LOG(info) << "Extracting individual timeframe collision contexts";
637 // extract prefix path to store these collision contexts
638 // Function to check the pattern and extract tokens from b
639 auto check_and_extract_tokens = [](const std::string& input, std::vector<std::string>& tokens) {
640 // the regular expression pattern for expected input format
641 const std::regex pattern(R"(^([a-zA-Z0-9]+)(:([a-zA-Z0-9]+(,[a-zA-Z0-9]+)*))?$)");
642 std::smatch matches;
643
644 // Check if the input matches the pattern
645 if (std::regex_match(input, matches, pattern)) {
646 // Clear any existing tokens in the vector
647 tokens.clear();
648
649 // matches[1] contains the part before the colon which we save first
650 tokens.push_back(matches[1].str());
651 // matches[2] contains the comma-separated list
652 std::string b = matches[2].str();
653 std::regex token_pattern(R"([a-zA-Z0-9]+)");
654 auto tokens_begin = std::sregex_iterator(b.begin(), b.end(), token_pattern);
655 auto tokens_end = std::sregex_iterator();
656
657 // Iterate over the tokens and add them to the vector
658 for (std::sregex_iterator i = tokens_begin; i != tokens_end; ++i) {
659 tokens.push_back((*i).str());
660 }
661 return true;
662 }
663 LOG(error) << "Argument for --extract-per-timeframe does not match specification";
664 return false;
665 };
666
667 std::vector<std::string> tokens;
668 if (check_and_extract_tokens(options.individualTFextraction, tokens)) {
669 auto path_prefix = tokens[0];
670 std::vector<int> sources_to_offset{};
671
672 LOG(info) << "PREFIX is " << path_prefix;
673
674 for (int i = 1; i < tokens.size(); ++i) {
675 LOG(info) << "Offsetting " << tokens[i];
676 sources_to_offset.push_back(digicontext.findSimPrefix(tokens[i]));
677 }
678
679 auto first_timeframe = options.orbitsEarly > 0. ? 1 : 0;
680 // now we are ready to loop over all timeframes
681 int tf_output_counter = 1;
682 for (int tf_id = first_timeframe; tf_id < numTimeFrames; ++tf_id) {
683 auto copy = digicontext.extractSingleTimeframe(tf_id, timeframeindices, sources_to_offset);
684
685 // each individual case gets QED interactions injected
686 // This should probably be done inside the extraction itself
687 if (digicontext.isQEDProvided()) {
688 auto qedSpec = parseInteractionSpec(options.qedInteraction, ispecs, options.useexistingkinematics);
689 copy.fillQED(qedSpec.name, qedSpec.mcnumberasked, qedSpec.interactionRate);
690 }
691
692 std::stringstream str;
693 str << path_prefix << tf_output_counter++ << "/collisioncontext.root";
694 copy.saveToFile(str.str());
695 LOG(info) << "---- CollisionContext for timeframe " << tf_id << " -----";
696 copy.printCollisionSummary();
697 }
698 }
699 }
700
701 return 0;
702}
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)
InteractionSpec parseInteractionSpec(std::string const &specifier, std::vector< InteractionSpec > const &existingPatterns, bool adjustEventCount)
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
std::ostringstream debug
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...
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
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()
InteractionLockMode syncmode
std::pair< int, float > synconto
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