Skip to content

Commit

Permalink
Working version without smart pointers
Browse files Browse the repository at this point in the history
Cleanup function called manually in main simulation programs
  • Loading branch information
jackal1-66 committed Dec 12, 2024
1 parent 22bfb67 commit e81a76f
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 98 deletions.
41 changes: 38 additions & 3 deletions Generators/include/Generators/GeneratorFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@
#ifndef ALICEO2_GENERATORFACTORY_H_
#define ALICEO2_GENERATORFACTORY_H_

#include "FairGenerator.h"
#include "FairBoxGenerator.h"
#include <Generators/GeneratorFromFile.h>
#include <Generators/GeneratorTParticle.h>
#ifdef GENERATORS_WITH_HEPMC3
#include <Generators/GeneratorHepMC.h>
#endif
#if defined(GENERATORS_WITH_PYTHIA8) && defined(GENERATORS_WITH_HEPMC3)
#include <Generators/GeneratorHybrid.h>
#endif
#ifdef GENERATORS_WITH_PYTHIA8
#include <Generators/GeneratorPythia8.h>
#endif

class FairPrimaryGenerator;
namespace o2
{
Expand All @@ -32,11 +46,32 @@ namespace eventgen
// main purpose is to init a FairPrimGen given some (Sim)Config
struct GeneratorFactory {
static void setPrimaryGenerator(o2::conf::SimConfig const&, FairPrimaryGenerator*);
//Make destructor to delete all the pointers
~GeneratorFactory() {
cleanup();
}
static void cleanup() {
for (auto& gen : mBoxGenPtr) {
delete gen;
}
delete mPythia8GenPtr;
delete mHybridGenPtr;
delete mHepMCGenPtr;
delete mExtGenPtr;
delete mFileGenPtr;
delete mO2KineGenPtr;
delete mTParticleGenPtr;
}
static std::vector<FairBoxGenerator*> mBoxGenPtr;
static o2::eventgen::GeneratorPythia8* mPythia8GenPtr;
static o2::eventgen::GeneratorHybrid* mHybridGenPtr;
static o2::eventgen::GeneratorHepMC* mHepMCGenPtr;
static FairGenerator* mExtGenPtr;
static o2::eventgen::GeneratorFromFile* mFileGenPtr;
static o2::eventgen::GeneratorFromO2Kine* mO2KineGenPtr;
static o2::eventgen::GeneratorTParticle* mTParticleGenPtr;
};

template <typename T>
std::vector<std::unique_ptr<T>> genptr;

} // end namespace eventgen
} // end namespace o2

Expand Down
2 changes: 1 addition & 1 deletion Generators/include/Generators/GeneratorFileOrCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ struct GeneratorFileOrCmd {
* @return true if the temporary file name was generated
* successfully.
*/
virtual bool makeTemp();
virtual bool makeTemp(const bool&);
/**
* Remove the temporary file if it was set and it exists.
*
Expand Down
6 changes: 6 additions & 0 deletions Generators/include/Generators/GeneratorService.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <SimulationDataFormat/MCTrack.h>
#include <Generators/PrimaryGenerator.h> // could be forward declaration
#include <DetectorsBase/Stack.h>
#include "Generators/GeneratorFactory.h"

namespace o2
{
Expand Down Expand Up @@ -59,6 +60,11 @@ class GeneratorService
{

public:

~GeneratorService() {
o2::eventgen::GeneratorFactory::cleanup();
};

void initService(std::string const& generatorName,
std::string const& triggerName,
VertexOption const& vtxOption);
Expand Down
145 changes: 70 additions & 75 deletions Generators/src/GeneratorFactory.cxx

Large diffs are not rendered by default.

45 changes: 33 additions & 12 deletions Generators/src/GeneratorFileOrCmd.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// For fifo's and system call
#include <cstdlib>
#include <sys/types.h> // POSIX only
#include <sys/stat.h> // POISX only
#include <sys/stat.h> // POSIX only
#include <signal.h>
#include <sys/wait.h>
#include <cstdio>
Expand Down Expand Up @@ -166,19 +166,40 @@ bool GeneratorFileOrCmd::terminateCmd()
return true;
}
// -----------------------------------------------------------------
bool GeneratorFileOrCmd::makeTemp()
bool GeneratorFileOrCmd::makeTemp(const bool& fromName)
{
mFileNames.clear();
char buf[] = "generatorFifoXXXXXX";
auto fp = mkstemp(buf);
if (fp < 0) {
LOG(fatal) << "Failed to make temporary file: "
<< std::strerror(errno);
return false;
if (fromName) {
if (mFileNames.empty()) {
LOG(fatal) << "No file names to make temporary file from";
return false;
} else if (mFileNames.size() > 1) {
LOG(warning) << "More than one file name to make temporary file from";
LOG(warning) << "Using the first one: " << mFileNames.front();
LOG(warning) << "Removing all the others";
mFileNames.erase(++mFileNames.begin(), mFileNames.end());
} else {
LOG(debug) << "Making temporary file from: " << mFileNames.front();
}
std::ofstream ofs(mFileNames.front().c_str());
if (!ofs) {
LOG(fatal) << "Failed to create temporary file: " << mFileNames.front();
return false;
}
mTemporary = std::string(mFileNames.front());
ofs.close();
} else {
mFileNames.clear();
char buf[] = "generatorFifoXXXXXX";
auto fp = mkstemp(buf);
if (fp < 0) {
LOG(fatal) << "Failed to make temporary file: "
<< std::strerror(errno);
return false;
}
mTemporary = std::string(buf);
mFileNames.push_back(mTemporary);
close(fp);
}
mTemporary = std::string(buf);
mFileNames.push_back(mTemporary);
close(fp);
return true;
}
// -----------------------------------------------------------------
Expand Down
13 changes: 10 additions & 3 deletions Generators/src/GeneratorHepMC.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -581,9 +581,16 @@ Bool_t GeneratorHepMC::Init()
// All of this can conviniently be achieved via a wrapper script
// around the actual EG program.
if (not mCmd.empty()) {
// Set filename to be a temporary name
if (not makeTemp()) {
return false;
if (mFileNames.empty()) {
// Set filename to be a temporary name
if (not makeTemp(false)) {
return false;
}
} else {
// Use the first filename as output for cmd line
if (not makeTemp(true)) {
return false;
}
}

// Make a fifo
Expand Down
1 change: 0 additions & 1 deletion Generators/src/GeneratorService.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
// or submit itself to any jurisdiction.

#include "Generators/GeneratorService.h"
#include "Generators/GeneratorFactory.h"
#include "SimConfig/SimConfig.h"
#include "DataFormatsCalibration/MeanVertexObject.h"

Expand Down
13 changes: 10 additions & 3 deletions Generators/src/GeneratorTParticle.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,16 @@ Bool_t GeneratorTParticle::Init()
mChain->SetBranchAddress(mBranchName.c_str(), &mTParticles);

if (not mCmd.empty()) {
// Set filename to be a temporary name
if (not makeTemp()) {
return false;
if (mFileNames.empty()) {
// Set filename to be a temporary name
if (not makeTemp(false)) {
return false;
}
} else {
// Use the first filename as output for cmd line
if (not makeTemp(true)) {
return false;
}
}

// Build command line, Assumes command line parameter
Expand Down
1 change: 1 addition & 0 deletions macro/o2sim.C
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,5 @@ void o2sim(bool asservice = false, bool evalmat = false)
auto run = o2sim_init(asservice, evalmat);
o2sim_run(run, asservice);
delete run;
o2::eventgen::GeneratorFactory::cleanup();
}
1 change: 1 addition & 0 deletions run/O2PrimaryServerDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class O2PrimaryServerDevice final : public fair::mq::Device
if (mControlThread.joinable()) {
mControlThread.join();
}
o2::eventgen::GeneratorFactory::cleanup();
} catch (...) {
}
}
Expand Down

0 comments on commit e81a76f

Please sign in to comment.