From e6a57a65e42e1c9e4d4bddd7c41efbc63f95bbdd Mon Sep 17 00:00:00 2001 From: swenzel Date: Fri, 13 Dec 2024 15:10:08 +0100 Subject: [PATCH] HybridGen/EventPool: small fixes, Add missing EventHeader propagation The HybridGen needs to forward event headers from the underlying generators. In order to access these, we need to make a protected function public. Also adding eventHeader treatment to BoxGenerator. More fixes: - clear particle container in EventPool - do not modify particle statuses in HybridGen (lead to wrong Geant4 simulations) - fix possible segfault in chosing EventPool file --- Generators/include/Generators/BoxGenerator.h | 9 +++++++ Generators/include/Generators/Generator.h | 4 +-- .../include/Generators/GeneratorFromFile.h | 2 ++ .../include/Generators/GeneratorHybrid.h | 2 ++ Generators/src/GeneratorFromFile.cxx | 8 +++--- Generators/src/GeneratorHybrid.cxx | 25 +++++++++++++------ 6 files changed, 36 insertions(+), 14 deletions(-) diff --git a/Generators/include/Generators/BoxGenerator.h b/Generators/include/Generators/BoxGenerator.h index 623f39e48b7c1..e24694296a70f 100644 --- a/Generators/include/Generators/BoxGenerator.h +++ b/Generators/include/Generators/BoxGenerator.h @@ -18,6 +18,7 @@ #include "TParticle.h" #include #include +#include "SimulationDataFormat/MCEventHeader.h" namespace o2::eventgen { @@ -92,6 +93,14 @@ class BoxGenerator : public Generator return true; } + void updateHeader(o2::dataformats::MCEventHeader* eventHeader) override + { + using Key = o2::dataformats::MCInfoKeys; + if (eventHeader) { + eventHeader->putInfo(Key::generator, "o2::eventgen::BoxGenerator"); + } + } + private: double mPtMin{0.}, mPtMax{0.}; // Transverse momentum range [GeV] double mPhiMin{0.}, mPhiMax{360.}; // Azimuth angle range [degree] diff --git a/Generators/include/Generators/Generator.h b/Generators/include/Generators/Generator.h index 72d287f8fd404..6da1a0e837cc8 100644 --- a/Generators/include/Generators/Generator.h +++ b/Generators/include/Generators/Generator.h @@ -73,6 +73,7 @@ class Generator : public FairGenerator /** methods to override **/ virtual Bool_t generateEvent() = 0; // generates event (in structure internal to generator) virtual Bool_t importParticles() = 0; // fills the mParticles vector (transfer from generator state) + virtual void updateHeader(o2::dataformats::MCEventHeader* eventHeader) {}; /** setters **/ void setMomentumUnit(double val) { mMomentumUnit = val; }; @@ -102,9 +103,6 @@ class Generator : public FairGenerator /** operator= **/ Generator& operator=(const Generator&); - /** methods that can be overridded **/ - virtual void updateHeader(o2::dataformats::MCEventHeader* eventHeader){}; - /** internal methods **/ Bool_t addTracks(FairPrimaryGenerator* primGen); Bool_t boostEvent(); diff --git a/Generators/include/Generators/GeneratorFromFile.h b/Generators/include/Generators/GeneratorFromFile.h index 423cb5d3e0a72..3b469751a4d47 100644 --- a/Generators/include/Generators/GeneratorFromFile.h +++ b/Generators/include/Generators/GeneratorFromFile.h @@ -135,9 +135,11 @@ class GeneratorFromEventPool : public o2::eventgen::Generator } bool importParticles() override { + mO2KineGenerator->clearParticles(); // clear old container before filling with new ones auto import_good = mO2KineGenerator->importParticles(); // transfer the particles (could be avoided) mParticles = mO2KineGenerator->getParticles(); + return import_good; } diff --git a/Generators/include/Generators/GeneratorHybrid.h b/Generators/include/Generators/GeneratorHybrid.h index 2ed0bd6089cd6..abce56f762f2a 100644 --- a/Generators/include/Generators/GeneratorHybrid.h +++ b/Generators/include/Generators/GeneratorHybrid.h @@ -61,6 +61,7 @@ class GeneratorHybrid : public Generator Bool_t Init() override; Bool_t generateEvent() override; Bool_t importParticles() override; + void updateHeader(o2::dataformats::MCEventHeader* eventHeader) override; void setNEvents(int n) { mNEvents = n; } @@ -106,6 +107,7 @@ class GeneratorHybrid : public Generator bool mIsInitialized = false; int mNEvents = -1; // the number of events to be done, if known (helps initiating cleanup) + o2::dataformats::MCEventHeader mMCEventHeader; // to capture event headers enum class GenMode { kSeq, diff --git a/Generators/src/GeneratorFromFile.cxx b/Generators/src/GeneratorFromFile.cxx index 935a03fe7c5b1..c885cbc0adb42 100644 --- a/Generators/src/GeneratorFromFile.cxx +++ b/Generators/src/GeneratorFromFile.cxx @@ -398,12 +398,14 @@ bool GeneratorFromEventPool::Init() LOG(error) << "No file found that can be used with EventPool generator"; return false; } + LOG(info) << "Found " << mPoolFilesAvailable.size() << " available event pool files"; // now choose the actual file - std::uniform_int_distribution distribution(0, mPoolFilesAvailable.size()); - mFileChosen = mPoolFilesAvailable[distribution(mRandomEngine)]; + std::uniform_int_distribution distribution(0, mPoolFilesAvailable.size() - 1); + auto chosenIndex = distribution(mRandomEngine); + mFileChosen = mPoolFilesAvailable[chosenIndex]; LOG(info) << "EventPool is using file " << mFileChosen; - + // we bring up the internal mO2KineGenerator auto kine_config = O2KineGenConfig{ .skipNonTrackable = mConfig.skipNonTrackable, diff --git a/Generators/src/GeneratorHybrid.cxx b/Generators/src/GeneratorHybrid.cxx index a47a6c9fdd5b0..932be0586ce4d 100644 --- a/Generators/src/GeneratorHybrid.cxx +++ b/Generators/src/GeneratorHybrid.cxx @@ -332,18 +332,16 @@ bool GeneratorHybrid::importParticles() LOG(info) << "Importing particles for task " << genIndex; // at this moment the mIndex-th generator is ready to be used - std::copy(gens[genIndex]->getParticles().begin(), gens[genIndex]->getParticles().end(), std::back_insert_iterator(mParticles)); + mParticles.clear(); + mParticles = gens[genIndex]->getParticles(); + + // fetch the event Header information from the underlying generator + mMCEventHeader.clearInfo(); + gens[genIndex]->updateHeader(&mMCEventHeader); mInputTaskQueue.push(genIndex); mTasksStarted++; - // we need to fix particles statuses --> need to enforce this on the importParticles level of individual generators - for (auto& p : mParticles) { - auto st = o2::mcgenstatus::MCGenStatusEncoding(p.GetStatusCode(), p.GetStatusCode()).fullEncoding; - p.SetStatusCode(st); - p.SetBit(ParticleStatus::kToBeDone, true); - } - mseqCounter++; mEventCounter++; if (mEventCounter == mNEvents) { @@ -353,6 +351,17 @@ bool GeneratorHybrid::importParticles() return true; } +void GeneratorHybrid::updateHeader(o2::dataformats::MCEventHeader* eventHeader) +{ + if (eventHeader) { + // we forward the original header information if any + eventHeader->copyInfoFrom(mMCEventHeader); + + // put additional information about + eventHeader->putInfo("forwarding-generator", "HybridGen"); + } +} + template std::string GeneratorHybrid::jsonValueToString(const T& value) {