Skip to content

Commit e81a76f

Browse files
committed
Working version without smart pointers
Cleanup function called manually in main simulation programs
1 parent 22bfb67 commit e81a76f

File tree

10 files changed

+170
-98
lines changed

10 files changed

+170
-98
lines changed

Generators/include/Generators/GeneratorFactory.h

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
#ifndef ALICEO2_GENERATORFACTORY_H_
1515
#define ALICEO2_GENERATORFACTORY_H_
1616

17+
#include "FairGenerator.h"
18+
#include "FairBoxGenerator.h"
19+
#include <Generators/GeneratorFromFile.h>
20+
#include <Generators/GeneratorTParticle.h>
21+
#ifdef GENERATORS_WITH_HEPMC3
22+
#include <Generators/GeneratorHepMC.h>
23+
#endif
24+
#if defined(GENERATORS_WITH_PYTHIA8) && defined(GENERATORS_WITH_HEPMC3)
25+
#include <Generators/GeneratorHybrid.h>
26+
#endif
27+
#ifdef GENERATORS_WITH_PYTHIA8
28+
#include <Generators/GeneratorPythia8.h>
29+
#endif
30+
1731
class FairPrimaryGenerator;
1832
namespace o2
1933
{
@@ -32,11 +46,32 @@ namespace eventgen
3246
// main purpose is to init a FairPrimGen given some (Sim)Config
3347
struct GeneratorFactory {
3448
static void setPrimaryGenerator(o2::conf::SimConfig const&, FairPrimaryGenerator*);
49+
//Make destructor to delete all the pointers
50+
~GeneratorFactory() {
51+
cleanup();
52+
}
53+
static void cleanup() {
54+
for (auto& gen : mBoxGenPtr) {
55+
delete gen;
56+
}
57+
delete mPythia8GenPtr;
58+
delete mHybridGenPtr;
59+
delete mHepMCGenPtr;
60+
delete mExtGenPtr;
61+
delete mFileGenPtr;
62+
delete mO2KineGenPtr;
63+
delete mTParticleGenPtr;
64+
}
65+
static std::vector<FairBoxGenerator*> mBoxGenPtr;
66+
static o2::eventgen::GeneratorPythia8* mPythia8GenPtr;
67+
static o2::eventgen::GeneratorHybrid* mHybridGenPtr;
68+
static o2::eventgen::GeneratorHepMC* mHepMCGenPtr;
69+
static FairGenerator* mExtGenPtr;
70+
static o2::eventgen::GeneratorFromFile* mFileGenPtr;
71+
static o2::eventgen::GeneratorFromO2Kine* mO2KineGenPtr;
72+
static o2::eventgen::GeneratorTParticle* mTParticleGenPtr;
3573
};
3674

37-
template <typename T>
38-
std::vector<std::unique_ptr<T>> genptr;
39-
4075
} // end namespace eventgen
4176
} // end namespace o2
4277

Generators/include/Generators/GeneratorFileOrCmd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ struct GeneratorFileOrCmd {
154154
* @return true if the temporary file name was generated
155155
* successfully.
156156
*/
157-
virtual bool makeTemp();
157+
virtual bool makeTemp(const bool&);
158158
/**
159159
* Remove the temporary file if it was set and it exists.
160160
*

Generators/include/Generators/GeneratorService.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <SimulationDataFormat/MCTrack.h>
2121
#include <Generators/PrimaryGenerator.h> // could be forward declaration
2222
#include <DetectorsBase/Stack.h>
23+
#include "Generators/GeneratorFactory.h"
2324

2425
namespace o2
2526
{
@@ -59,6 +60,11 @@ class GeneratorService
5960
{
6061

6162
public:
63+
64+
~GeneratorService() {
65+
o2::eventgen::GeneratorFactory::cleanup();
66+
};
67+
6268
void initService(std::string const& generatorName,
6369
std::string const& triggerName,
6470
VertexOption const& vtxOption);

Generators/src/GeneratorFactory.cxx

Lines changed: 70 additions & 75 deletions
Large diffs are not rendered by default.

Generators/src/GeneratorFileOrCmd.cxx

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// For fifo's and system call
1717
#include <cstdlib>
1818
#include <sys/types.h> // POSIX only
19-
#include <sys/stat.h> // POISX only
19+
#include <sys/stat.h> // POSIX only
2020
#include <signal.h>
2121
#include <sys/wait.h>
2222
#include <cstdio>
@@ -166,19 +166,40 @@ bool GeneratorFileOrCmd::terminateCmd()
166166
return true;
167167
}
168168
// -----------------------------------------------------------------
169-
bool GeneratorFileOrCmd::makeTemp()
169+
bool GeneratorFileOrCmd::makeTemp(const bool& fromName)
170170
{
171-
mFileNames.clear();
172-
char buf[] = "generatorFifoXXXXXX";
173-
auto fp = mkstemp(buf);
174-
if (fp < 0) {
175-
LOG(fatal) << "Failed to make temporary file: "
176-
<< std::strerror(errno);
177-
return false;
171+
if (fromName) {
172+
if (mFileNames.empty()) {
173+
LOG(fatal) << "No file names to make temporary file from";
174+
return false;
175+
} else if (mFileNames.size() > 1) {
176+
LOG(warning) << "More than one file name to make temporary file from";
177+
LOG(warning) << "Using the first one: " << mFileNames.front();
178+
LOG(warning) << "Removing all the others";
179+
mFileNames.erase(++mFileNames.begin(), mFileNames.end());
180+
} else {
181+
LOG(debug) << "Making temporary file from: " << mFileNames.front();
182+
}
183+
std::ofstream ofs(mFileNames.front().c_str());
184+
if (!ofs) {
185+
LOG(fatal) << "Failed to create temporary file: " << mFileNames.front();
186+
return false;
187+
}
188+
mTemporary = std::string(mFileNames.front());
189+
ofs.close();
190+
} else {
191+
mFileNames.clear();
192+
char buf[] = "generatorFifoXXXXXX";
193+
auto fp = mkstemp(buf);
194+
if (fp < 0) {
195+
LOG(fatal) << "Failed to make temporary file: "
196+
<< std::strerror(errno);
197+
return false;
198+
}
199+
mTemporary = std::string(buf);
200+
mFileNames.push_back(mTemporary);
201+
close(fp);
178202
}
179-
mTemporary = std::string(buf);
180-
mFileNames.push_back(mTemporary);
181-
close(fp);
182203
return true;
183204
}
184205
// -----------------------------------------------------------------

Generators/src/GeneratorHepMC.cxx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,16 @@ Bool_t GeneratorHepMC::Init()
581581
// All of this can conviniently be achieved via a wrapper script
582582
// around the actual EG program.
583583
if (not mCmd.empty()) {
584-
// Set filename to be a temporary name
585-
if (not makeTemp()) {
586-
return false;
584+
if (mFileNames.empty()) {
585+
// Set filename to be a temporary name
586+
if (not makeTemp(false)) {
587+
return false;
588+
}
589+
} else {
590+
// Use the first filename as output for cmd line
591+
if (not makeTemp(true)) {
592+
return false;
593+
}
587594
}
588595

589596
// Make a fifo

Generators/src/GeneratorService.cxx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
// or submit itself to any jurisdiction.
1111

1212
#include "Generators/GeneratorService.h"
13-
#include "Generators/GeneratorFactory.h"
1413
#include "SimConfig/SimConfig.h"
1514
#include "DataFormatsCalibration/MeanVertexObject.h"
1615

Generators/src/GeneratorTParticle.cxx

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,16 @@ Bool_t GeneratorTParticle::Init()
5454
mChain->SetBranchAddress(mBranchName.c_str(), &mTParticles);
5555

5656
if (not mCmd.empty()) {
57-
// Set filename to be a temporary name
58-
if (not makeTemp()) {
59-
return false;
57+
if (mFileNames.empty()) {
58+
// Set filename to be a temporary name
59+
if (not makeTemp(false)) {
60+
return false;
61+
}
62+
} else {
63+
// Use the first filename as output for cmd line
64+
if (not makeTemp(true)) {
65+
return false;
66+
}
6067
}
6168

6269
// Build command line, Assumes command line parameter

macro/o2sim.C

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,4 +325,5 @@ void o2sim(bool asservice = false, bool evalmat = false)
325325
auto run = o2sim_init(asservice, evalmat);
326326
o2sim_run(run, asservice);
327327
delete run;
328+
o2::eventgen::GeneratorFactory::cleanup();
328329
}

run/O2PrimaryServerDevice.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class O2PrimaryServerDevice final : public fair::mq::Device
7373
if (mControlThread.joinable()) {
7474
mControlThread.join();
7575
}
76+
o2::eventgen::GeneratorFactory::cleanup();
7677
} catch (...) {
7778
}
7879
}

0 commit comments

Comments
 (0)