Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 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,6 +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;
};

} // end namespace eventgen
Expand Down
21 changes: 19 additions & 2 deletions Generators/include/Generators/GeneratorFileOrCmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ struct GeneratorFileOrCmd {
* execute, but should not include pipes.
*/
void setCmd(const std::string& cmd) { mCmd = cmd; }
/**
* Set command child process PID
*
* @param cmdPid child process PID.
*/
void setCmdPid(const pid_t cmdPid) { mCmdPid = cmdPid; }
/**
* Set the number of events that a background command should
* generate. This should come from @c SimConfig::getNEents.
Expand Down Expand Up @@ -132,7 +138,14 @@ struct GeneratorFileOrCmd {
* @return true if the background command line was executed, false
* otherwise.
*/
virtual bool executeCmdLine(const std::string& cmd) const;
virtual bool executeCmdLine(const std::string& cmd);
/**
* Terminates the background command using PID of the child
* process generated by fork.
*
* @return true if the process was terminated successfully
*/
virtual bool terminateCmd();
/**
* Create a temporary file (and close it immediately). On success,
* the list of file names is cleared and the name of the temporary
Expand All @@ -141,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 Expand Up @@ -236,6 +249,10 @@ struct GeneratorFileOrCmd {
* Time in miliseconds between each wait for data
*/
int mWait = 500;
/**
* PID of the background command
*/
int mCmdPid = -1;
};

} // namespace eventgen
Expand Down
1 change: 1 addition & 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
143 changes: 69 additions & 74 deletions Generators/src/GeneratorFactory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,19 @@

#include <SimulationDataFormat/O2DatabasePDG.h>
#include <Generators/GeneratorFactory.h>
#include "FairGenerator.h"
#include "FairBoxGenerator.h"
#include <fairlogger/Logger.h>
#include <SimConfig/SimConfig.h>
#include <Generators/GeneratorFromFile.h>
#include <Generators/GeneratorTParticle.h>
#include <Generators/GeneratorTParticleParam.h>
#ifdef GENERATORS_WITH_PYTHIA8
#include <Generators/GeneratorPythia8.h>
#include <Generators/GeneratorPythia8Param.h>
#endif
#include <Generators/GeneratorTGenerator.h>
#include <Generators/GeneratorExternalParam.h>
#include "Generators/GeneratorFromO2KineParam.h"
#ifdef GENERATORS_WITH_HEPMC3
#include <Generators/GeneratorHepMC.h>
#include <Generators/GeneratorHepMCParam.h>
#endif
#if defined(GENERATORS_WITH_PYTHIA8) && defined(GENERATORS_WITH_HEPMC3)
#include <Generators/GeneratorHybrid.h>
#include <Generators/GeneratorHybridParam.h>
#endif
#include <Generators/PrimaryGenerator.h>
Expand All @@ -49,8 +42,17 @@ namespace o2
namespace eventgen
{

std::vector<FairBoxGenerator*> GeneratorFactory::mBoxGenPtr;
o2::eventgen::GeneratorPythia8* GeneratorFactory::mPythia8GenPtr;
o2::eventgen::GeneratorHybrid* GeneratorFactory::mHybridGenPtr;
o2::eventgen::GeneratorHepMC* GeneratorFactory::mHepMCGenPtr;
FairGenerator* GeneratorFactory::mExtGenPtr;
o2::eventgen::GeneratorFromFile* GeneratorFactory::mFileGenPtr;
o2::eventgen::GeneratorFromO2Kine* GeneratorFactory::mO2KineGenPtr;
o2::eventgen::GeneratorTParticle* GeneratorFactory::mTParticleGenPtr;
// reusable helper class
// main purpose is to init a FairPrimGen given some (Sim)Config

void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, FairPrimaryGenerator* primGen)
{
if (!primGen) {
Expand Down Expand Up @@ -99,58 +101,58 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
auto& boxparam = BoxGunParam::Instance();
LOG(info) << "Init generic box generator with following parameters";
LOG(info) << boxparam;
auto boxGen = makeBoxGen(boxparam.pdg, boxparam.number, boxparam.eta[0], boxparam.eta[1], boxparam.prange[0], boxparam.prange[1], boxparam.phirange[0], boxparam.phirange[1], boxparam.debug);
primGen->AddGenerator(boxGen);
mBoxGenPtr.push_back(makeBoxGen(boxparam.pdg, boxparam.number, boxparam.eta[0], boxparam.eta[1], boxparam.prange[0], boxparam.prange[1], boxparam.phirange[0], boxparam.phirange[1], boxparam.debug));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("fwmugen") == 0) {
// a simple "box" generator for forward muons
LOG(info) << "Init box forward muons generator";
auto boxGen = makeBoxGen(13, 1, -4, -2.5, 50., 50., 0., 360);
primGen->AddGenerator(boxGen);
mBoxGenPtr.push_back(makeBoxGen(13, 1, -4, -2.5, 50., 50., 0., 360));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("hmpidgun") == 0) {
// a simple "box" generator for forward muons
LOG(info) << "Init hmpid gun generator";
auto boxGen = makeBoxGen(-211, 100, -0.5, -0.5, 2, 5, -5, 60);
primGen->AddGenerator(boxGen);
mBoxGenPtr.push_back(makeBoxGen(-211, 100, -0.5, -0.5, 2, 5, -5, 60));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("fwpigen") == 0) {
// a simple "box" generator for forward pions
LOG(info) << "Init box forward pions generator";
auto boxGen = makeBoxGen(-211, 10, -4, -2.5, 7, 7, 0, 360);
primGen->AddGenerator(boxGen);
mBoxGenPtr.push_back(makeBoxGen(-211, 10, -4, -2.5, 7, 7, 0, 360));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("fwrootino") == 0) {
// a simple "box" generator for forward rootinos
LOG(info) << "Init box forward rootinos generator";
auto boxGen = makeBoxGen(0, 1, -4, -2.5, 1, 5, 0, 360);
primGen->AddGenerator(boxGen);
mBoxGenPtr.push_back(makeBoxGen(0, 1, -4, -2.5, 1, 5, 0, 360));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("zdcgen") == 0) {
// a simple "box" generator for forward neutrons
LOG(info) << "Init box forward/backward zdc generator";
auto boxGenC = makeBoxGen(2112 /*neutrons*/, 1, -8, -9999, 500, 1000, 0., 360.);
auto boxGenA = makeBoxGen(2112 /*neutrons*/, 1, 8, 9999, 500, 1000, 0., 360.);
primGen->AddGenerator(boxGenC);
primGen->AddGenerator(boxGenA);
mBoxGenPtr.push_back(makeBoxGen(2112 /*neutrons*/, 1, -8, -9999, 500, 1000, 0., 360.));
primGen->AddGenerator(mBoxGenPtr.back());
mBoxGenPtr.push_back(makeBoxGen(2112 /*neutrons*/, 1, 8, 9999, 500, 1000, 0., 360.));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("emcgenele") == 0) {
// box generator with one electron per event
LOG(info) << "Init box generator for electrons in EMCAL";
// using phi range of emcal
auto elecgen = makeBoxGen(11, 1, -0.67, 0.67, 15, 15, 80, 187);
primGen->AddGenerator(elecgen);
mBoxGenPtr.push_back(makeBoxGen(11, 1, -0.67, 0.67, 15, 15, 80, 187));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("emcgenphoton") == 0) {
LOG(info) << "Init box generator for photons in EMCAL";
auto photongen = makeBoxGen(22, 1, -0.67, 0.67, 15, 15, 80, 187);
primGen->AddGenerator(photongen);
mBoxGenPtr.push_back(makeBoxGen(22, 1, -0.67, 0.67, 15, 15, 80, 187));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("fddgen") == 0) {
LOG(info) << "Init box FDD generator";
auto boxGenFDC = makeBoxGen(13, 1000, -7, -4.8, 10, 500, 0, 360.);
auto boxGenFDA = makeBoxGen(13, 1000, 4.9, 6.3, 10, 500, 0., 360);
primGen->AddGenerator(boxGenFDA);
primGen->AddGenerator(boxGenFDC);
mBoxGenPtr.push_back(makeBoxGen(13, 1000, -7, -4.8, 10, 500, 0, 360.));
primGen->AddGenerator(mBoxGenPtr.back());
mBoxGenPtr.push_back(makeBoxGen(13, 1000, 4.9, 6.3, 10, 500, 0., 360));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("extkin") == 0) {
// external kinematics
// needs precense of a kinematics file "Kinematics.root"
// TODO: make this configurable and check for presence
auto extGen = new o2::eventgen::GeneratorFromFile(conf.getExtKinematicsFileName().c_str());
extGen->SetStartEvent(conf.getStartEvent());
primGen->AddGenerator(extGen);
mFileGenPtr = new o2::eventgen::GeneratorFromFile(conf.getExtKinematicsFileName().c_str());
mFileGenPtr->SetStartEvent(conf.getStartEvent());
primGen->AddGenerator(mFileGenPtr);
LOG(info) << "using external kinematics";
} else if (genconfig.compare("extkinO2") == 0) {
// external kinematics from previous O2 output
Expand All @@ -165,23 +167,16 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
.rngseed = singleton.rngseed,
.randomphi = singleton.randomphi,
.fileName = name1.size() > 0 ? name1.c_str() : name2.c_str()};
auto extGen = new o2::eventgen::GeneratorFromO2Kine(pars);
extGen->SetStartEvent(conf.getStartEvent());
primGen->AddGenerator(extGen);
mO2KineGenPtr = new o2::eventgen::GeneratorFromO2Kine(pars);
mO2KineGenPtr->SetStartEvent(conf.getStartEvent());
primGen->AddGenerator(mO2KineGenPtr);
if (pars.continueMode) {
auto o2PrimGen = dynamic_cast<o2::eventgen::PrimaryGenerator*>(primGen);
if (o2PrimGen) {
o2PrimGen->setApplyVertex(false);
}
}
LOG(info) << "using external O2 kinematics";
} else if (genconfig.compare("evtpool") == 0) {
// case of an "event-pool" which is a specialization of extkinO2
// with some additional logic in file management and less configurability
// and not features such as "continue transport"
auto extGen = new o2::eventgen::GeneratorFromEventPool(o2::eventgen::GeneratorEventPoolParam::Instance().detach());
primGen->AddGenerator(extGen);
LOG(info) << "using the eventpool generator";
} else if (genconfig.compare("tparticle") == 0) {
// External ROOT file(s) with tree of TParticle in clones array,
// or external program generating such a file
Expand All @@ -190,9 +185,9 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
LOG(info) << "Init 'GeneratorTParticle' with the following parameters";
LOG(info) << param0;
LOG(info) << param;
auto tgen = new o2::eventgen::GeneratorTParticle();
tgen->setup(param0, param, conf);
primGen->AddGenerator(tgen);
mTParticleGenPtr = new o2::eventgen::GeneratorTParticle();
mTParticleGenPtr->setup(param0, param, conf);
primGen->AddGenerator(mTParticleGenPtr);
#ifdef GENERATORS_WITH_HEPMC3
} else if (genconfig.compare("hepmc") == 0) {
// external HepMC file, or external program writing HepMC event
Expand All @@ -202,9 +197,9 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
LOG(info) << "Init \'GeneratorHepMC\' with following parameters";
LOG(info) << param0;
LOG(info) << param;
auto hepmcGen = new o2::eventgen::GeneratorHepMC();
hepmcGen->setup(param0, param, conf);
primGen->AddGenerator(hepmcGen);
mHepMCGenPtr = new o2::eventgen::GeneratorHepMC();
mHepMCGenPtr->setup(param0, param, conf);
primGen->AddGenerator(mHepMCGenPtr);
#endif
#ifdef GENERATORS_WITH_PYTHIA8
} else if (genconfig.compare("alldets") == 0) {
Expand All @@ -213,37 +208,37 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
// I compose it of:
// 1) pythia8
auto py8config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_inel.cfg";
auto py8 = makePythia8Gen(py8config);
primGen->AddGenerator(py8);
mPythia8GenPtr = makePythia8Gen(py8config);
primGen->AddGenerator(mPythia8GenPtr);
// 2) forward muons
auto muon = makeBoxGen(13, 100, -2.5, -4.0, 100, 100, 0., 360);
primGen->AddGenerator(muon);
mBoxGenPtr.push_back(makeBoxGen(13, 100, -2.5, -4.0, 100, 100, 0., 360));
primGen->AddGenerator(mBoxGenPtr.back());
} else if (genconfig.compare("pythia8") == 0) {
auto py8config = std::string();
auto py8 = makePythia8Gen(py8config);
primGen->AddGenerator(py8);
mPythia8GenPtr = makePythia8Gen(py8config);
primGen->AddGenerator(mPythia8GenPtr);
} else if (genconfig.compare("pythia8pp") == 0) {
auto py8config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_inel.cfg";
auto py8 = makePythia8Gen(py8config);
primGen->AddGenerator(py8);
mPythia8GenPtr = makePythia8Gen(py8config);
primGen->AddGenerator(mPythia8GenPtr);
} else if (genconfig.compare("pythia8hf") == 0) {
// pythia8 pp (HF production)
// configures pythia for HF production in pp collisions at 14 TeV
auto py8config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_hf.cfg";
auto py8 = makePythia8Gen(py8config);
primGen->AddGenerator(py8);
mPythia8GenPtr = makePythia8Gen(py8config);
primGen->AddGenerator(mPythia8GenPtr);
} else if (genconfig.compare("pythia8hi") == 0) {
// pythia8 heavy-ion
// exploits pythia8 heavy-ion machinery (available from v8.230)
// configures pythia for min.bias Pb-Pb collisions at 5.52 TeV
auto py8config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_hi.cfg";
auto py8 = makePythia8Gen(py8config);
primGen->AddGenerator(py8);
mPythia8GenPtr = makePythia8Gen(py8config);
primGen->AddGenerator(mPythia8GenPtr);
} else if (genconfig.compare("pythia8powheg") == 0) {
// pythia8 with powheg
auto py8config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_powheg.cfg";
auto py8 = makePythia8Gen(py8config);
primGen->AddGenerator(py8);
mPythia8GenPtr = makePythia8Gen(py8config);
primGen->AddGenerator(mPythia8GenPtr);
#endif
} else if (genconfig.compare("external") == 0 || genconfig.compare("extgen") == 0) {
// external generator via configuration macro
Expand All @@ -252,21 +247,21 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
LOG(info) << params;
auto extgen_filename = params.fileName;
auto extgen_func = params.funcName;
auto extgen = o2::conf::GetFromMacro<FairGenerator*>(extgen_filename, extgen_func, "FairGenerator*", "extgen");
if (!extgen) {
mExtGenPtr = o2::conf::GetFromMacro<FairGenerator*>(extgen_filename, extgen_func, "FairGenerator*", "extgen");
if (!mExtGenPtr) {
LOG(fatal) << "Failed to retrieve \'extgen\': problem with configuration ";
}
primGen->AddGenerator(extgen);
primGen->AddGenerator(mExtGenPtr);
} else if (genconfig.compare("toftest") == 0) { // 1 muon per sector and per module
LOG(info) << "Init tof test generator -> 1 muon per sector and per module";
for (int i = 0; i < 18; i++) {
for (int j = 0; j < 5; j++) {
auto boxGen = new FairBoxGenerator(13, 1); /*protons*/
boxGen->SetEtaRange(-0.8 + 0.32 * j + 0.15, -0.8 + 0.32 * j + 0.17);
boxGen->SetPRange(9, 10);
boxGen->SetPhiRange(10 + 20. * i - 1, 10 + 20. * i + 1);
boxGen->SetDebug(kTRUE);
primGen->AddGenerator(boxGen);
mBoxGenPtr.push_back(new FairBoxGenerator(13, 1)); /*protons*/
mBoxGenPtr.back()->SetEtaRange(-0.8 + 0.32 * j + 0.15, -0.8 + 0.32 * j + 0.17);
mBoxGenPtr.back()->SetPRange(9, 10);
mBoxGenPtr.back()->SetPhiRange(10 + 20. * i - 1, 10 + 20. * i + 1);
mBoxGenPtr.back()->SetDebug(kTRUE);
primGen->AddGenerator(mBoxGenPtr.back());
}
}
#if defined(GENERATORS_WITH_PYTHIA8) && defined(GENERATORS_WITH_HEPMC3)
Expand All @@ -284,9 +279,9 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
LOG(fatal) << "Configuration file for hybrid generator does not exist";
return;
}
auto hybrid = new o2::eventgen::GeneratorHybrid(config);
hybrid->setNEvents(conf.getNEvents());
primGen->AddGenerator(hybrid);
mHybridGenPtr = new o2::eventgen::GeneratorHybrid(config);
mHybridGenPtr->setNEvents(conf.getNEvents());
primGen->AddGenerator(mHybridGenPtr);
#endif
} else {
LOG(fatal) << "Invalid generator";
Expand Down
Loading
Loading