Skip to content

Commit

Permalink
Make sure PrimaryGenerator is destructed
Browse files Browse the repository at this point in the history
Makes sure that PrimaryGenerators are destructed in o2-sim.

Note that all FairGenerator pointers registered in the PrimaryGenerator
are automatically destructed as well.
  • Loading branch information
sawenzel committed Dec 19, 2024
1 parent 948d271 commit 9424b41
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Generators/src/PrimaryGenerator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ namespace eventgen
PrimaryGenerator::~PrimaryGenerator()
{
/** destructor **/

LOG(info) << "Destructing PrimaryGenerator";
if (mEmbedFile && mEmbedFile->IsOpen()) {
mEmbedFile->Close();
delete mEmbedFile;
Expand Down
10 changes: 6 additions & 4 deletions run/O2PrimaryServerDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class O2PrimaryServerDevice final : public fair::mq::Device
if (conf.getGenerator().compare("extkin") != 0 || conf.getGenerator().compare("extkinO2") != 0) {
auto iter = mPrimGeneratorCache.find(conf.getGenerator());
if (iter != mPrimGeneratorCache.end()) {
mPrimGen = iter->second;
mPrimGen = iter->second.get();
LOG(info) << "Found cached generator for " << conf.getGenerator();
}
}
Expand Down Expand Up @@ -133,7 +133,9 @@ class O2PrimaryServerDevice final : public fair::mq::Device

mPrimGen->Init();

mPrimGeneratorCache[conf.getGenerator()] = mPrimGen;
std::unique_ptr<o2::eventgen::PrimaryGenerator> ptr_wrapper;
ptr_wrapper.reset(mPrimGen);
mPrimGeneratorCache[conf.getGenerator()] = std::move(ptr_wrapper);
}
mPrimGen->SetEvent(&mEventHeader);

Expand Down Expand Up @@ -668,11 +670,11 @@ class O2PrimaryServerDevice final : public fair::mq::Device

// Keeps various generators instantiated in memory
// useful when running simulation as a service (when generators
// change between batches)
// change between batches). Also takes care of resource management of Primary generators via unique ptr
// TODO: some care needs to be taken (or the user warned) that the caching is based on generator name
// and that parameter-based reconfiguration is not yet implemented (for which we would need to hash all
// configuration parameters as well)
std::map<std::string, o2::eventgen::PrimaryGenerator*> mPrimGeneratorCache;
std::map<std::string, std::unique_ptr<o2::eventgen::PrimaryGenerator>> mPrimGeneratorCache;

std::atomic<O2PrimaryServerState> mState{O2PrimaryServerState::Initializing};
std::atomic<int> mWaitingControlInput{0};
Expand Down

0 comments on commit 9424b41

Please sign in to comment.