Skip to content

Commit

Permalink
HybridGen/EventPool: small fixes, Add missing EventHeader propagation
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sawenzel committed Dec 15, 2024
1 parent 3318e86 commit e6a57a6
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 14 deletions.
9 changes: 9 additions & 0 deletions Generators/include/Generators/BoxGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "TParticle.h"
#include <vector>
#include <Generators/BoxGunParam.h>
#include "SimulationDataFormat/MCEventHeader.h"

namespace o2::eventgen
{
Expand Down Expand Up @@ -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<std::string>(Key::generator, "o2::eventgen::BoxGenerator");
}
}

private:
double mPtMin{0.}, mPtMax{0.}; // Transverse momentum range [GeV]
double mPhiMin{0.}, mPhiMax{360.}; // Azimuth angle range [degree]
Expand Down
4 changes: 1 addition & 3 deletions Generators/include/Generators/Generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; };
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 2 additions & 0 deletions Generators/include/Generators/GeneratorFromFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 2 additions & 0 deletions Generators/include/Generators/GeneratorHybrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions Generators/src/GeneratorFromFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<int> distribution(0, mPoolFilesAvailable.size());
mFileChosen = mPoolFilesAvailable[distribution(mRandomEngine)];
std::uniform_int_distribution<int> distribution(0, mPoolFilesAvailable.size() - 1);
auto chosenIndex = distribution(mRandomEngine);
mFileChosen = mPoolFilesAvailable[chosenIndex];
LOG(info) << "EventPool is using file " << mFileChosen;

Check failure on line 408 in Generators/src/GeneratorFromFile.cxx

View workflow job for this annotation

GitHub Actions / PR formatting / whitespace

Trailing spaces

Remove the trailing spaces at the end of the line.
// we bring up the internal mO2KineGenerator
auto kine_config = O2KineGenConfig{
.skipNonTrackable = mConfig.skipNonTrackable,
Expand Down
25 changes: 17 additions & 8 deletions Generators/src/GeneratorHybrid.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<std::string>("forwarding-generator", "HybridGen");
}
}

template <typename T>
std::string GeneratorHybrid::jsonValueToString(const T& value)
{
Expand Down

0 comments on commit e6a57a6

Please sign in to comment.