Project
Loading...
Searching...
No Matches
GeneratorHybrid.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
13#include <fairlogger/Logger.h>
14#include <algorithm>
15#include <tbb/concurrent_queue.h>
16#include <tbb/task_arena.h>
17#include <tbb/parallel_for.h>
19#include <filesystem>
20#include "TGrid.h"
21
22namespace o2
23{
24namespace eventgen
25{
26
27GeneratorHybrid& GeneratorHybrid::Instance(const std::string& inputgens)
28{
29 static GeneratorHybrid instance(inputgens);
30 return instance;
31}
32
33GeneratorHybrid::GeneratorHybrid(const std::string& inputgens)
34{
35 // This generator has trivial unit conversions
36 setTimeUnit(1.);
39 setEnergyUnit(1.);
40
41 // Pull file from alien for dynamic configuration if needed
42 bool isAlien = false;
43 if (inputgens.starts_with("alien://")) {
44 if (!gGrid) {
45 TGrid::Connect("alien://");
46 if (!gGrid) {
47 LOG(fatal) << "AliEn connection failed, check token.";
48 exit(1);
49 }
50 }
51 TString aliencp = Form("alien_cp %s file:./%s",
52 inputgens.c_str(), "hybridAlien.json");
53 if (gSystem->Exec(aliencp.Data()) != 0) {
54 LOG(fatal) << "Error: Issues in fetching file" << inputgens;
55 exit(1);
56 }
57 isAlien = true;
58 }
59
60 if (!parseJSON(isAlien ? "hybridAlien.json" : inputgens)) {
61 LOG(fatal) << "Failed to parse JSON configuration from input generators";
62 exit(1);
63 }
64 mRandomize = GeneratorHybridParam::Instance().randomize;
65 if (mConfigs.size() != mInputGens.size()) {
66 LOG(fatal) << "Number of configurations does not match the number of generators";
67 exit(1);
68 }
69 if (mConfigs.size() == 0) {
70 for (auto gen : mInputGens) {
71 mConfigs.push_back("");
72 }
73 }
74 int index = 0;
75 if (!(mRandomize || mGenerationMode == GenMode::kParallel)) {
76 if (mCocktailMode) {
77 if (mGroups.size() != mFractions.size()) {
78 LOG(fatal) << "Number of groups does not match the number of fractions";
79 return;
80 }
81 } else {
82 if (mFractions.size() != mInputGens.size()) {
83 LOG(fatal) << "Number of fractions does not match the number of generators";
84 return;
85 }
86 }
87 // Check if all elements of mFractions are 0
88 if (std::all_of(mFractions.begin(), mFractions.end(), [](int i) { return i == 0; })) {
89 LOG(fatal) << "All fractions provided are 0, no simulation will be performed";
90 return;
91 }
92 }
93 for (auto gen : mInputGens) {
94 // Search if the generator name is inside generatorNames (which is a vector of strings)
95 LOG(info) << "Checking if generator " << gen << " is in the list of available generators \n";
96 if (std::find(generatorNames.begin(), generatorNames.end(), gen) != generatorNames.end()) {
97 LOG(info) << "Found generator " << gen << " in the list of available generators \n";
98 if (gen.compare("boxgen") == 0) {
99 if (mConfigs[index].compare("") == 0) {
100 gens.push_back(std::make_shared<o2::eventgen::BoxGenerator>());
101 } else {
102 // Get the index of boxgen configuration
103 int confBoxIndex = std::stoi(mConfigs[index].substr(7));
104 gens.push_back(std::make_shared<o2::eventgen::BoxGenerator>(*mBoxGenConfigs[confBoxIndex]));
105 }
106 mGens.push_back(gen);
107 } else if (gen.compare(0, 7, "pythia8") == 0) {
108 // Check if mConfigs[index] contains pythia8_ and a number
109 if (mConfigs[index].compare("") == 0) {
110 auto pars = Pythia8GenConfig();
111 gens.push_back(std::make_shared<o2::eventgen::GeneratorPythia8>(pars));
112 } else {
113 // Get the index of pythia8 configuration
114 int confPythia8Index = std::stoi(mConfigs[index].substr(8));
115 gens.push_back(std::make_shared<o2::eventgen::GeneratorPythia8>(*mPythia8GenConfigs[confPythia8Index]));
116 }
117 mConfsPythia8.push_back(mConfigs[index]);
118 mGens.push_back(gen);
119 } else if (gen.compare("evtpool") == 0) {
120 int confEvtPoolIndex = std::stoi(mConfigs[index].substr(8));
121 gens.push_back(std::make_shared<o2::eventgen::GeneratorFromEventPool>(mEventPoolConfigs[confEvtPoolIndex]));
122 mGens.push_back(gen);
123 } else if (gen.compare("external") == 0) {
124 int confextIndex = std::stoi(mConfigs[index].substr(9));
125 // we need analyse the ini file to update the config key param
126 if (mExternalGenConfigs[confextIndex]->iniFile.size() > 0) {
127 LOG(info) << "Setting up external gen using the given INI file";
128
129 // this means that we go via the ConfigurableParam system ---> in order not to interfere with other
130 // generators we use an approach with backup and restore of the system
131
132 // we write the current state to a file
133 // create a tmp file name
134 std::string tmp_config_file = "configkey_tmp_backup_" + std::to_string(getpid()) + std::string(".ini");
136
137 auto expandedFileName = o2::utils::expandShellVarsInFileName(mExternalGenConfigs[confextIndex]->iniFile);
139 // toDo: check that this INI file makes sense
140
142 LOG(info) << "Setting up external generator with following parameters";
143 LOG(info) << params;
144 auto extgen_filename = params.fileName;
145 auto extgen_func = params.funcName;
146 auto extgen = std::shared_ptr<o2::eventgen::Generator>(o2::conf::GetFromMacro<o2::eventgen::Generator*>(extgen_filename, extgen_func, "FairGenerator*", "extgen"));
147 if (!extgen) {
148 LOG(fatal) << "Failed to retrieve \'extgen\': problem with configuration ";
149 }
150 // restore old state
152 // delete tmp file
153 std::filesystem::remove(tmp_config_file);
154
155 gens.push_back(std::move(extgen));
156 mGens.push_back(gen);
157 } else {
158 LOG(info) << "Setting up external gen using the given fileName and funcName";
159 // we need to restore the config key param system to what is was before
160 auto& extgen_filename = mExternalGenConfigs[confextIndex]->fileName;
161 auto& extgen_func = mExternalGenConfigs[confextIndex]->funcName;
162 auto extGen = std::shared_ptr<o2::eventgen::Generator>(o2::conf::GetFromMacro<o2::eventgen::Generator*>(extgen_filename, extgen_func, "FairGenerator*", "extgen"));
163 if (!extGen) {
164 LOG(fatal) << "Failed to load external generator from " << extgen_filename << " with function " << extgen_func;
165 }
166 gens.push_back(std::move(extGen));
167 mGens.push_back(gen);
168 }
169 } else if (gen.compare("hepmc") == 0) {
170 int confHepMCIndex = std::stoi(mConfigs[index].substr(6));
171 gens.push_back(std::make_shared<o2::eventgen::GeneratorHepMC>());
172 auto& globalConfig = o2::conf::SimConfig::Instance();
173 dynamic_cast<o2::eventgen::GeneratorHepMC*>(gens.back().get())->setup(*mFileOrCmdGenConfigs[confHepMCIndex], *mHepMCGenConfigs[confHepMCIndex], globalConfig);
174 mGens.push_back(gen);
175 }
176 } else {
177 LOG(fatal) << "Generator " << gen << " not found in the list of available generators \n";
178 exit(1);
179 }
180 index++;
181 }
182}
183
184GeneratorHybrid::~GeneratorHybrid()
185{
186 LOG(info) << "Destructor of generator hybrid called";
187 mStopFlag = true;
188}
189
191{
192 // init all sub-gens
193 int count = 0;
194 for (auto& gen : mGens) {
195 if (gen == "pythia8pp") {
196 auto config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_inel.cfg";
197 LOG(info) << "Setting \'Pythia8\' base configuration: " << config << std::endl;
198 dynamic_cast<o2::eventgen::GeneratorPythia8*>(gens[count].get())->setConfig(config);
199 } else if (gen == "pythia8hf") {
200 auto config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_hf.cfg";
201 LOG(info) << "Setting \'Pythia8\' base configuration: " << config << std::endl;
202 dynamic_cast<o2::eventgen::GeneratorPythia8*>(gens[count].get())->setConfig(config);
203 } else if (gen == "pythia8hi") {
204 auto config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_hi.cfg";
205 LOG(info) << "Setting \'Pythia8\' base configuration: " << config << std::endl;
206 dynamic_cast<o2::eventgen::GeneratorPythia8*>(gens[count].get())->setConfig(config);
207 } else if (gen == "pythia8powheg") {
208 auto config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_powheg.cfg";
209 LOG(info) << "Setting \'Pythia8\' base configuration: " << config << std::endl;
210 dynamic_cast<o2::eventgen::GeneratorPythia8*>(gens[count].get())->setConfig(config);
211 }
212 gens[count]->Init(); // TODO: move this to multi-threaded
214 if (mTriggerModes[count] != o2::eventgen::Generator::kTriggerOFF) {
215 gens[count]->setTriggerMode(mTriggerModes[count]);
216 LOG(info) << "Setting Trigger mode of generator " << gen << " to: " << mTriggerModes[count];
217 o2::eventgen::Trigger trigger = nullptr;
218 o2::eventgen::DeepTrigger deeptrigger = nullptr;
219 for (int trg = 0; trg < mTriggerMacros[count].size(); trg++) {
220 if (mTriggerMacros[count][trg].empty() || mTriggerFuncs[count][trg].empty()) {
221 continue;
222 }
223 std::string expandedMacro = o2::utils::expandShellVarsInFileName(mTriggerMacros[count][trg]);
224 LOG(info) << "Setting trigger " << trg << " of generator " << gen << " with following parameters";
225 LOG(info) << "Macro filename: " << expandedMacro;
226 LOG(info) << "Function name: " << mTriggerFuncs[count][trg];
227 trigger = o2::conf::GetFromMacro<o2::eventgen::Trigger>(expandedMacro, mTriggerFuncs[count][trg], "o2::eventgen::Trigger", "trigger");
228 if (!trigger) {
229 LOG(info) << "Trying to retrieve a \'o2::eventgen::DeepTrigger\' type";
230 deeptrigger = o2::conf::GetFromMacro<o2::eventgen::DeepTrigger>(expandedMacro, mTriggerFuncs[count][trg], "o2::eventgen::DeepTrigger", "deeptrigger");
231 }
232 if (!trigger && !deeptrigger) {
233 LOG(warn) << "Failed to retrieve \'external trigger\': problem with configuration";
234 LOG(warn) << "Trigger " << trg << " of generator " << gen << " will not be included";
235 continue;
236 } else {
237 LOG(info) << "Trigger " << trg << " of generator " << gen << " successfully set";
238 }
239 if (trigger) {
240 gens[count]->addTrigger(trigger);
241 } else {
242 gens[count]->addDeepTrigger(deeptrigger);
243 }
244 }
245 }
246 count++;
247 }
248 if (mRandomize) {
249 if (std::all_of(mFractions.begin(), mFractions.end(), [](int i) { return i == 1; })) {
250 LOG(info) << "Full randomisation of generators order";
251 } else {
252 LOG(info) << "Randomisation based on fractions";
253 int allfracs = 0;
254 for (auto& f : mFractions) {
255 allfracs += f;
256 }
257 // Assign new rng fractions
258 float sum = 0;
259 float chance = 0;
260 for (int k = 0; k < mFractions.size(); k++) {
261 if (mFractions[k] == 0) {
262 // Generator will not be used if fraction is 0
263 mRngFractions.push_back(-1);
264 LOG(info) << "Generator " << mGens[k] << " will not be used";
265 } else {
266 chance = static_cast<float>(mFractions[k]) / allfracs;
267 sum += chance;
268 mRngFractions.push_back(sum);
269 LOG(info) << "Generator " << (mConfigs[k] == "" ? mGens[k] : mConfigs[k]) << " has a " << chance * 100 << "% chance of being used";
270 }
271 }
272 }
273 } else {
274 LOG(info) << "Generators will be used in sequence, following provided fractions";
275 }
276
277 mGenIsInitialized.resize(gens.size(), false);
278 if (mGenerationMode == GenMode::kParallel) {
279 // in parallel mode we just use one queue --> collaboration
280 mResultQueue.resize(1);
281 } else {
282 // in sequential mode we have one queue per generator
283 mResultQueue.resize(gens.size());
284 }
285 // Create a task arena with a specified number of threads
286 mTaskArena.initialize(GeneratorHybridParam::Instance().num_workers);
287
288 // the process task function actually calls event generation
289 // when it is done it notifies the outside world by pushing it's index into an appropriate queue
290 // This should be a lambda, which can be given at TaskPool creation time
291 auto process_generator_task = [this](std::vector<std::shared_ptr<o2::eventgen::Generator>> const& generatorvec, int task) {
292 LOG(debug) << "Starting eventgen for task " << task;
293 auto& generator = generatorvec[task];
294 if (!mStopFlag) {
295 // TODO: activate this once we are use Init is threadsafe
296 // if (!mGenIsInitialized[task]) {
297 // if(!generator->Init()) {
298 // LOG(error) << "failed to init generator " << task;
299 // }
300 // mGenIsInitialized[task] = true;
301 // }
302 }
303 bool isTriggered = false;
304 while (!isTriggered) {
305 generator->clearParticles();
306 generator->generateEvent();
307 generator->importParticles();
308 isTriggered = generator->triggerEvent();
309 }
310 LOG(debug) << "eventgen finished for task " << task;
311 if (!mStopFlag) {
312 if (mGenerationMode == GenMode::kParallel) {
313 mResultQueue[0].push(task);
314 } else {
315 mResultQueue[task].push(task);
316 }
317 }
318 };
319
320 // fundamental tbb thread-worker function
321 auto worker_function = [this, process_generator_task]() {
322 // we increase the reference count in the generator pointers
323 // by making a copy of the vector. In this way we ensure that the lifetime
324 // of the generators is no shorter than the lifetime of the thread for this worker function
325 auto generators_copy = gens;
326
327 while (!mStopFlag) {
328 int task;
329 if (mInputTaskQueue.try_pop(task)) {
330 process_generator_task(generators_copy, task); // Process the task
331 } else {
332 std::this_thread::sleep_for(std::chrono::milliseconds(10)); // Wait if no task
333 }
334 }
335 };
336
337 // let the TBB task system run in it's own thread
338 mTBBTaskPoolRunner = std::thread([this, worker_function]() { mTaskArena.execute([&]() { tbb::parallel_for(0, mTaskArena.max_concurrency(), [&](int) { worker_function(); }); }); });
339 mTBBTaskPoolRunner.detach(); // detaching because we don't like to wait on the thread to finish
340 // some of the generators might still be generating when we are done
341
342 // let's also push initial generation tasks for each event generator
343 for (size_t genindex = 0; genindex < gens.size(); ++genindex) {
344 mInputTaskQueue.push(genindex);
345 mTasksStarted++;
346 }
347 mIsInitialized = true;
348 return Generator::Init();
349}
350
352{
353 if (!mIsInitialized) {
354 Init();
355 }
356 if (mGenerationMode == GenMode::kParallel) {
357 mIndex = -1; // this means any index is welcome
358 notifySubGenerator(0); // we shouldn't distinguish the sub-gen ids
359 } else {
360 // Order randomisation or sequence of generators
361 // following provided fractions, if not generators are used in proper sequence
362 // Order randomisation or sequence of generators
363 // following provided fractions. If not available generators will be used sequentially
364 if (mRandomize) {
365 if (mRngFractions.size() != 0) {
366 // Generate number between 0 and 1
367 float rnum = gRandom->Rndm();
368 // Find generator index
369 for (int k = 0; k < mRngFractions.size(); k++) {
370 if (rnum <= mRngFractions[k]) {
371 mIndex = k;
372 break;
373 }
374 }
375 } else {
376 mIndex = gRandom->Integer(mFractions.size());
377 }
378 } else {
379 while (mFractions[mCurrentFraction] == 0 || mseqCounter == mFractions[mCurrentFraction]) {
380 if (mFractions[mCurrentFraction] != 0) {
381 mseqCounter = 0;
382 }
383 mCurrentFraction = (mCurrentFraction + 1) % mFractions.size();
384 }
385 mIndex = mCurrentFraction;
386 }
387 notifySubGenerator(mIndex);
388 }
389 return true;
390}
391
393{
394 int genIndex = -1;
395 std::vector<int> subGenIndex = {};
396 if (mIndex == -1) {
397 // this means parallel mode ---> we have a common queue
398 mResultQueue[0].pop(genIndex);
399 } else {
400 // need to pop from a particular queue
401 if (!mCocktailMode) {
402 mResultQueue[mIndex].pop(genIndex);
403 } else {
404 // in cocktail mode we need to pop from the group queue
405 subGenIndex.resize(mGroups[mIndex].size());
406 for (size_t pos = 0; pos < mGroups[mIndex].size(); ++pos) {
407 int subIndex = mGroups[mIndex][pos];
408 LOG(info) << "Getting generator " << mGens[subIndex] << " from cocktail group " << mIndex;
409 mResultQueue[subIndex].pop(subGenIndex[pos]);
410 }
411 }
412 }
413
414 auto unit_transformer = [](auto& p, auto pos_unit, auto time_unit, auto en_unit, auto mom_unit) {
415 p.SetMomentum(p.Px() * mom_unit, p.Py() * mom_unit, p.Pz() * mom_unit, p.Energy() * en_unit);
416 p.SetProductionVertex(p.Vx() * pos_unit, p.Vy() * pos_unit, p.Vz() * pos_unit, p.T() * time_unit);
417 };
418
419 auto index_transformer = [](auto& p, int offset) {
420 for (int i = 0; i < 2; ++i) {
421 if (p.GetMother(i) != -1) {
422 const auto newindex = p.GetMother(i) + offset;
423 p.SetMother(i, newindex);
424 }
425 }
426 if (p.GetNDaughters() > 0) {
427 for (int i = 0; i < 2; ++i) {
428 const auto newindex = p.GetDaughter(i) + offset;
429 p.SetDaughter(i, newindex);
430 }
431 }
432 };
433
434 // Clear particles and event header
435 mParticles.clear();
436 // event header of underlying generator must be fully reset
437 // this is important when using event pools where the full header information is forwarded from the generator
438 // otherwise some events might have mixed header information from different generators
439 mMCEventHeader.Reset();
440 if (mCocktailMode) {
441 // in cocktail mode we need to merge the particles from the different generators
442 for (auto subIndex : subGenIndex) {
443 LOG(info) << "Importing particles for task " << subIndex;
444 auto subParticles = gens[subIndex]->getParticles();
445
446 auto time_unit = gens[subIndex]->getTimeUnit();
447 auto pos_unit = gens[subIndex]->getPositionUnit();
448 auto mom_unit = gens[subIndex]->getMomentumUnit();
449 auto energy_unit = gens[subIndex]->getEnergyUnit();
450
451 // The particles carry mother and daughter indices, which are relative
452 // to the sub-generator. We need to adjust these indices to reflect that particles
453 // are now embedded into a cocktail.
454 auto offset = mParticles.size();
455 for (auto& p : subParticles) {
456 // apply the mother-daugher index transformation
457 index_transformer(p, offset);
458 // apply unit transformation of sub-generator
459 unit_transformer(p, pos_unit, time_unit, energy_unit, mom_unit);
460 }
461
462 mParticles.insert(mParticles.end(), subParticles.begin(), subParticles.end());
463 // first generator of the cocktail is used as reference to update the event header information
464 if (mHeaderGeneratorIndex == -1) {
465 gens[subIndex]->updateHeader(&mMCEventHeader);
466 mHeaderGeneratorIndex = subIndex; // store index of generator updating the header
467 }
468 mInputTaskQueue.push(subIndex);
469 mTasksStarted++;
470 }
471 } else {
472 LOG(info) << "Importing particles for task " << genIndex;
473 // at this moment the mIndex-th generator is ready to be used
474 mParticles = gens[genIndex]->getParticles();
475
476 auto time_unit = gens[genIndex]->getTimeUnit();
477 auto pos_unit = gens[genIndex]->getPositionUnit();
478 auto mom_unit = gens[genIndex]->getMomentumUnit();
479 auto energy_unit = gens[genIndex]->getEnergyUnit();
480
481 // transform units to units of the hybrid generator
482 for (auto& p : mParticles) {
483 // apply unit transformation
484 unit_transformer(p, pos_unit, time_unit, energy_unit, mom_unit);
485 }
486
487 // fetch the event Header information from the underlying generator
488 gens[genIndex]->updateHeader(&mMCEventHeader);
489 mHeaderGeneratorIndex = genIndex; // store index of generator updating the header
490 mInputTaskQueue.push(genIndex);
491 mTasksStarted++;
492 }
493
494 mseqCounter++;
495 mEventCounter++;
496 if (mEventCounter == getTotalNEvents()) {
497 LOG(info) << "HybridGen: Stopping TBB task pool";
498 mStopFlag = true;
499 }
500
501 return true;
502}
503
505{
506 if (eventHeader) {
507 // Overwrite current vertex information to the underlying generator header,
508 // otherwise the info will be dropped when copying the FairMCEventHeader part of the header
509 mMCEventHeader.SetVertex(eventHeader->GetX(), eventHeader->GetY(), eventHeader->GetZ());
510 mHeaderGeneratorIndex = -1; // reset header generator index for next event
511 // Forward the base class fields from FairMCEventHeader
512 static_cast<FairMCEventHeader&>(*eventHeader) = static_cast<FairMCEventHeader&>(mMCEventHeader);
513 // Copy the key-value store info
514 eventHeader->copyInfoFrom(mMCEventHeader);
515
516 // put additional information about
517 eventHeader->putInfo<std::string>("forwarding-generator", "HybridGen");
518 }
519}
520
521template <typename T>
523{
524 rapidjson::StringBuffer buffer;
525 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
526 value.Accept(writer);
527 return buffer.GetString();
528}
529
531{
532 std::string name = gen["name"].GetString();
533 mInputGens.push_back(name);
534 if (gen.HasMember("config")) {
535 if (name == "boxgen") {
536 const auto& boxconf = gen["config"];
537 auto boxConfig = TBufferJSON::FromJSON<o2::eventgen::BoxGenConfig>(jsonValueToString(boxconf).c_str());
538 mBoxGenConfigs.push_back(std::move(boxConfig));
539 mConfigs.push_back("boxgen_" + std::to_string(mBoxGenConfigs.size() - 1));
540 } else if (name == "pythia8") {
541 const auto& pythia8conf = gen["config"];
542 auto pythia8Config = TBufferJSON::FromJSON<o2::eventgen::Pythia8GenConfig>(jsonValueToString(pythia8conf).c_str());
543 mPythia8GenConfigs.push_back(std::move(pythia8Config));
544 mConfigs.push_back("pythia8_" + std::to_string(mPythia8GenConfigs.size() - 1));
545 } else if (name == "evtpool") {
546 const auto& o2kineconf = gen["config"];
547 auto poolConfig = TBufferJSON::FromJSON<o2::eventgen::EventPoolGenConfig>(jsonValueToString(o2kineconf).c_str());
548 mEventPoolConfigs.push_back(*poolConfig);
549 mConfigs.push_back("evtpool_" + std::to_string(mEventPoolConfigs.size() - 1));
550 } else if (name == "external") {
551 const auto& extconf = gen["config"];
552 auto extConfig = TBufferJSON::FromJSON<o2::eventgen::ExternalGenConfig>(jsonValueToString(extconf).c_str());
553 mExternalGenConfigs.push_back(std::move(extConfig));
554 mConfigs.push_back("external_" + std::to_string(mExternalGenConfigs.size() - 1));
555 } else if (name == "hepmc") {
556 const auto& genconf = gen["config"];
557 const auto& cmdconf = genconf["configcmd"];
558 const auto& hepmcconf = genconf["confighepmc"];
559 auto cmdConfig = TBufferJSON::FromJSON<o2::eventgen::FileOrCmdGenConfig>(jsonValueToString(cmdconf).c_str());
560 auto hepmcConfig = TBufferJSON::FromJSON<o2::eventgen::HepMCGenConfig>(jsonValueToString(hepmcconf).c_str());
561 mFileOrCmdGenConfigs.push_back(std::move(cmdConfig));
562 mHepMCGenConfigs.push_back(std::move(hepmcConfig));
563 mConfigs.push_back("hepmc_" + std::to_string(mFileOrCmdGenConfigs.size() - 1));
564 } else {
565 mConfigs.push_back("");
566 }
567 } else {
568 if (name == "boxgen" || name == "pythia8" || name == "external" || name == "hepmc") {
569 LOG(fatal) << "No configuration provided for generator " << name;
570 return false;
571 } else {
572 mConfigs.push_back("");
573 }
574 }
575 if (gen.HasMember("triggers")) {
576 const auto& trigger = gen["triggers"];
577 auto trigger_specs = [this, &trigger]() {
578 mTriggerMacros.push_back({});
579 mTriggerFuncs.push_back({});
580 if (trigger.HasMember("specs")) {
581 for (auto& spec : trigger["specs"].GetArray()) {
582 if (spec.HasMember("macro")) {
583 const auto& macro = spec["macro"].GetString();
584 if (!(strcmp(macro, "") == 0)) {
585 mTriggerMacros.back().push_back(macro);
586 } else {
587 mTriggerMacros.back().push_back("");
588 }
589 } else {
590 mTriggerMacros.back().push_back("");
591 }
592 if (spec.HasMember("function")) {
593 const auto& function = spec["function"].GetString();
594 if (!(strcmp(function, "") == 0)) {
595 mTriggerFuncs.back().push_back(function);
596 } else {
597 mTriggerFuncs.back().push_back("");
598 }
599 } else {
600 mTriggerFuncs.back().push_back("");
601 }
602 }
603 } else {
604 mTriggerMacros.back().push_back("");
605 mTriggerFuncs.back().push_back("");
606 }
607 };
608 if (trigger.HasMember("mode")) {
609 const auto& trmode = trigger["mode"].GetString();
610 if (strcmp(trmode, "or") == 0) {
611 mTriggerModes.push_back(o2::eventgen::Generator::kTriggerOR);
612 trigger_specs();
613 } else if (strcmp(trmode, "and") == 0) {
614 mTriggerModes.push_back(o2::eventgen::Generator::kTriggerAND);
615 trigger_specs();
616 } else if (strcmp(trmode, "off") == 0) {
617 mTriggerModes.push_back(o2::eventgen::Generator::kTriggerOFF);
618 mTriggerMacros.push_back({""});
619 mTriggerFuncs.push_back({""});
620 } else {
621 LOG(warn) << "Wrong trigger mode provided for generator " << name << ", keeping trigger OFF";
622 mTriggerModes.push_back(o2::eventgen::Generator::kTriggerOFF);
623 mTriggerMacros.push_back({""});
624 mTriggerFuncs.push_back({""});
625 }
626 } else {
627 LOG(warn) << "No trigger mode provided for generator " << name << ", turning trigger OFF";
628 mTriggerModes.push_back(o2::eventgen::Generator::kTriggerOFF);
629 mTriggerMacros.push_back({""});
630 mTriggerFuncs.push_back({""});
631 }
632 } else {
633 mTriggerModes.push_back(o2::eventgen::Generator::kTriggerOFF);
634 mTriggerMacros.push_back({""});
635 mTriggerFuncs.push_back({""});
636 }
637 return true;
638}
639
640Bool_t GeneratorHybrid::parseJSON(const std::string& path)
641{
642 auto expandedPath = o2::utils::expandShellVarsInFileName(path);
643 // Check if configuration file exists
644 if (gSystem->AccessPathName(expandedPath.c_str())) {
645 LOG(fatal) << "Configuration file " << expandedPath << " for hybrid generator does not exist";
646 return false;
647 }
648 // Parse JSON file to build map
649 std::ifstream fileStream(expandedPath, std::ios::in);
650 if (!fileStream.is_open()) {
651 LOG(error) << "Cannot open " << expandedPath;
652 return false;
653 }
654 rapidjson::IStreamWrapper isw(fileStream);
655 rapidjson::Document doc;
656 doc.ParseStream(isw);
657 if (doc.HasParseError()) {
658 LOG(error) << "Error parsing provided json file " << expandedPath;
659 LOG(error) << " - Error -> " << rapidjson::GetParseError_En(doc.GetParseError());
660 return false;
661 }
662
663 // check if there is a mode field
664 if (doc.HasMember("mode")) {
665 const auto& mode = doc["mode"].GetString();
666 if (strcmp(mode, "sequential") == 0) {
667 // events are generated in the order given by fractions or random weight
668 mGenerationMode = GenMode::kSeq;
669 }
670 if (strcmp(mode, "parallel") == 0) {
671 // events are generated fully in parallel and the order will be random
672 // this is mainly for event pool generation or mono-type generators
673 mGenerationMode = GenMode::kParallel;
674 LOG(info) << "Setting mode to parallel";
675 }
676 }
677
678 // Put the generator names in mInputGens
679 if (doc.HasMember("generators")) {
680 const auto& gens = doc["generators"];
681 for (const auto& gen : gens.GetArray()) {
682 mGroups.push_back({});
683 // Check if gen is an array (cocktail mode)
684 if (gen.HasMember("cocktail")) {
685 mCocktailMode = true;
686 for (const auto& subgen : gen["cocktail"].GetArray()) {
687 if (confSetter(subgen)) {
688 mGroups.back().push_back(mInputGens.size() - 1);
689 } else {
690 return false;
691 }
692 }
693 } else {
694 if (!confSetter(gen)) {
695 return false;
696 }
697 // Groups are created in case cocktail mode is activated, this way
698 // cocktails can be declared anywhere in the JSON file, without the need
699 // of grouping single generators. If no cocktail is defined
700 // groups will be ignored nonetheless.
701 mGroups.back().push_back(mInputGens.size() - 1);
702 }
703 }
704 }
705
706 // Get fractions and put them in mFractions
707 if (doc.HasMember("fractions")) {
708 const auto& fractions = doc["fractions"];
709 for (const auto& frac : fractions.GetArray()) {
710 mFractions.push_back(frac.GetInt());
711 }
712 } else {
713 // Set fractions to unity for all generators in case they are not provided
714 const auto& gens = doc["generators"];
715 for (const auto& gen : gens.GetArray()) {
716 mFractions.push_back(1);
717 }
718 }
719 return true;
720}
721
722} // namespace eventgen
723} // namespace o2
724
default_random_engine gen(dev())
std::ostringstream debug
int32_t i
ClassImp(o2::eventgen::GeneratorHybrid)
uint16_t pos
Definition RawData.h:3
static void writeINI(std::string const &filename, std::string const &keyOnly="")
static void updateFromFile(std::string const &, std::string const &paramsList="", bool unchangedOnly=false)
static SimConfig & Instance()
Definition SimConfig.h:111
void copyInfoFrom(MCEventHeader const &other)
inits info fields from another Event header
void putInfo(std::string const &key, T const &value)
GeneratorHybrid(const GeneratorHybrid &)=delete
Bool_t parseJSON(const std::string &path)
std::string jsonValueToString(const T &value)
void updateHeader(o2::dataformats::MCEventHeader *eventHeader) override
static GeneratorHybrid & Instance(const std::string &inputgens="")
Bool_t confSetter(const auto &gen)
void setPositionUnit(double val)
Definition Generator.h:87
void setEnergyUnit(double val)
Definition Generator.h:85
void notifySubGenerator(int subGeneratorId)
Definition Generator.h:123
static unsigned int getTotalNEvents()
Definition Generator.h:100
void setTimeUnit(double val)
Definition Generator.h:89
void addSubGenerator(int subGeneratorId, std::string const &subGeneratorDescription)
std::vector< TParticle > mParticles
Definition Generator.h:147
void setMomentumUnit(double val)
Definition Generator.h:83
Bool_t Init() override
float sum(float s, o2::dcs::DataPointValue v)
Definition dcs-ccdb.cxx:39
GLenum mode
Definition glcorearb.h:266
GLint GLsizei count
Definition glcorearb.h:399
GLuint buffer
Definition glcorearb.h:655
GLsizeiptr size
Definition glcorearb.h:659
GLuint index
Definition glcorearb.h:781
GLuint const GLchar * name
Definition glcorearb.h:781
GLdouble f
Definition glcorearb.h:310
GLsizei const GLfloat * value
Definition glcorearb.h:819
GLenum const GLfloat * params
Definition glcorearb.h:272
GLintptr offset
Definition glcorearb.h:660
GLsizei const GLchar *const * path
Definition glcorearb.h:3591
std::function< bool(void *, std::string)> DeepTrigger
Definition Trigger.h:26
std::function< bool(const std::vector< TParticle > &)> Trigger
Definition Trigger.h:25
std::string expandShellVarsInFileName(std::string const &input)
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
void empty(int)
void compare(std::string_view s1, std::string_view s2)
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"