Skip to content

Commit 37feb1c

Browse files
committed
Finished implementation for remaining geneators
They are all working now, a cleanup of the code will be done in next commits. The cmd spawning feature is now working also with the hybrid configuration
1 parent b2d67c7 commit 37feb1c

File tree

10 files changed

+127
-50
lines changed

10 files changed

+127
-50
lines changed

Generators/include/Generators/GeneratorExternalParam.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ struct GeneratorExternalParam : public o2::conf::ConfigurableParamHelper<Generat
3434
O2ParamDef(GeneratorExternalParam, "GeneratorExternal");
3535
};
3636

37+
struct ExternalGenConfig {
38+
std::string fileName = "";
39+
std::string funcName = "";
40+
};
41+
3742
} // end namespace eventgen
3843
} // end namespace o2
3944

Generators/include/Generators/GeneratorFileOrCmd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ struct GeneratorFileOrCmd {
3535
* function so as to better facilitate changes. */
3636
void setup(const GeneratorFileOrCmdParam& param,
3737
const conf::SimConfig& config);
38+
// Configure with local parameters
39+
void setup(const FileOrCmdGenConfig& param,
40+
const conf::SimConfig& config);
3841
/**
3942
* Set command to execute in bacground rather than reading from
4043
* existing file(s)

Generators/include/Generators/GeneratorFileOrCmdParam.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ struct GeneratorFileOrCmdParam : public o2::conf::ConfigurableParamHelper<Genera
3939
O2ParamDef(GeneratorFileOrCmdParam, "GeneratorFileOrCmd");
4040
};
4141

42+
struct FileOrCmdGenConfig {
43+
std::string fileNames = "";
44+
std::string cmd = ""; // Program command line to spawn
45+
};
46+
4247
} // end namespace eventgen
4348
} // end namespace o2
4449

Generators/include/Generators/GeneratorHepMC.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Generators/Generator.h"
1818
#include "Generators/GeneratorFileOrCmd.h"
1919
#include "Generators/GeneratorHepMCParam.h"
20+
#include "Generators/GeneratorFileOrCmdParam.h"
2021

2122
#ifdef GENERATORS_WITH_HEPMC3_DEPRECATED
2223
namespace HepMC
@@ -69,6 +70,10 @@ class GeneratorHepMC : public Generator, public GeneratorFileOrCmd
6970
void setup(const GeneratorFileOrCmdParam& param0,
7071
const GeneratorHepMCParam& param,
7172
const conf::SimConfig& config);
73+
// Generator configuration from external local parameters
74+
void setup(const FileOrCmdGenConfig& param0,
75+
const HepMCGenConfig& param,
76+
const conf::SimConfig& config);
7277
/**
7378
* Generate a single event. The event is read in from the current
7479
* input file. Returns false if a new event could not be read.

Generators/include/Generators/GeneratorHepMCParam.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ struct GeneratorHepMCParam : public o2::conf::ConfigurableParamHelper<GeneratorH
5454
O2ParamDef(GeneratorHepMCParam, "HepMC");
5555
};
5656

57+
struct HepMCGenConfig {
58+
// Same parameters as GeneratorHepMCParam
59+
int version = 0;
60+
uint64_t eventsToSkip = 0;
61+
std::string fileName = "";
62+
bool prune = false;
63+
};
64+
5765
} // end namespace eventgen
5866
} // end namespace o2
5967

Generators/include/Generators/GeneratorHybrid.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "Generators/GeneratorPythia8Param.h"
2828
#include "Generators/GeneratorFileOrCmdParam.h"
2929
#include "Generators/GeneratorFromO2KineParam.h"
30+
#include "Generators/GeneratorExternalParam.h"
3031
#include <TRandom3.h>
3132
#include "CommonUtils/ConfigurationMacroHelper.h"
3233
#include "FairGenerator.h"
@@ -72,6 +73,9 @@ class GeneratorHybrid : public Generator
7273
std::vector<std::unique_ptr<o2::eventgen::BoxGenConfig>> mBoxGenConfigs;
7374
std::vector<std::unique_ptr<o2::eventgen::Pythia8GenConfig>> mPythia8GenConfigs;
7475
std::vector<std::unique_ptr<o2::eventgen::O2KineGenConfig>> mO2KineGenConfigs;
76+
std::vector<std::unique_ptr<o2::eventgen::ExternalGenConfig>> mExternalGenConfigs;
77+
std::vector<std::unique_ptr<o2::eventgen::FileOrCmdGenConfig>> mFileOrCmdGenConfigs;
78+
std::vector<std::unique_ptr<o2::eventgen::HepMCGenConfig>> mHepMCGenConfigs;
7579

7680
bool mRandomize = false;
7781
std::vector<int> mFractions;

Generators/src/GeneratorFileOrCmd.cxx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,22 @@ void GeneratorFileOrCmd::setup(const GeneratorFileOrCmdParam& param,
7070
setBmax(config.getBMax());
7171
}
7272
// -----------------------------------------------------------------
73+
// Switches are permanently set to default values
74+
void GeneratorFileOrCmd::setup(const FileOrCmdGenConfig& param,
75+
const conf::SimConfig& config)
76+
{
77+
setFileNames(param.fileNames);
78+
setCmd(param.cmd);
79+
setOutputSwitch(">");
80+
setSeedSwitch("-s");
81+
setBmaxSwitch("-b");
82+
setNEventsSwitch("-n");
83+
setBackgroundSwitch("&");
84+
setSeed(config.getStartSeed());
85+
setNEvents(config.getNEvents());
86+
setBmax(config.getBMax());
87+
}
88+
// -----------------------------------------------------------------
7389
void GeneratorFileOrCmd::setFileNames(const std::string& filenames)
7490
{
7591
std::stringstream s(filenames);

Generators/src/GeneratorHepMC.cxx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,42 @@ void GeneratorHepMC::setup(const GeneratorFileOrCmdParam& param0,
9393
}
9494

9595
if (param.version != 0 and mCmd.empty()) {
96-
LOG(warn) << "The key \"HepMC.version\" is no longer used when "
96+
LOG(warn) << "The key \"HepMC.version\" is no longer needed when "
9797
<< "reading from files. The format version of the input files "
98-
<< "are automatically deduced.";
98+
<< "are automatically deduced. However, it is mandatory when reading "
99+
<< "from a pipe containing HepMC2 data.";
100+
}
101+
}
102+
103+
/*****************************************************************/
104+
void GeneratorHepMC::setup(const FileOrCmdGenConfig& param0,
105+
const HepMCGenConfig& param,
106+
const conf::SimConfig& config)
107+
{
108+
if (not param.fileName.empty()) {
109+
LOG(warn) << "The use of the key \"HepMC.fileName\" is "
110+
<< "deprecated, use \"GeneratorFileOrCmd.fileNames\" instead";
111+
}
112+
113+
GeneratorFileOrCmd::setup(param0, config);
114+
if (not param.fileName.empty()) {
115+
setFileNames(param.fileName);
116+
}
117+
118+
mVersion = param.version;
119+
mPrune = param.prune;
120+
setEventsToSkip(param.eventsToSkip);
121+
122+
// we are skipping ahead in the HepMC stream now
123+
for (int i = 0; i < mEventsToSkip; ++i) {
124+
generateEvent();
125+
}
126+
127+
if (param.version != 0 and mCmd.empty()) {
128+
LOG(warn) << "The key \"HepMC.version\" is no longer needed when "
129+
<< "reading from files. The format version of the input files "
130+
<< "are automatically deduced. However, it is mandatory when reading "
131+
<< "from a pipe containing HepMC2 data.";
99132
}
100133
}
101134

Generators/src/GeneratorHybrid.cxx

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -77,45 +77,26 @@ namespace o2
7777
gens.push_back(std::make_unique<o2::eventgen::GeneratorFromO2Kine>(*mO2KineGenConfigs[confO2KineIndex]));
7878
mGens.push_back(gen);
7979
} else if (gen.compare("external") == 0) {
80-
if (mConfigs[index].compare("") == 0) {
81-
LOG(fatal) << "No configuration provided";
80+
int confextIndex = std::stoi(mConfigs[index].substr(9));
81+
auto& extgen_filename = mExternalGenConfigs[confextIndex]->fileName;
82+
auto& extgen_func = mExternalGenConfigs[confextIndex]->funcName;
83+
auto extGen = std::unique_ptr<o2::eventgen::Generator>(o2::conf::GetFromMacro<o2::eventgen::Generator*>(extgen_filename, extgen_func, "FairGenerator*", "extgen"));
84+
if (!extGen) {
85+
LOG(fatal) << "Failed to load external generator from " << extgen_filename << " with function " << extgen_func;
8286
exit(1);
83-
} else {
84-
std::stringstream ss(mConfigs[index]);
85-
std::string pars;
86-
std::vector<std::string> params;
87-
while (std::getline(ss, pars, ',')) {
88-
params.push_back(pars);
89-
}
90-
auto& extgen_filename = params[0];
91-
auto& extgen_func = params[1];
92-
auto extGen = std::unique_ptr<o2::eventgen::Generator>(o2::conf::GetFromMacro<o2::eventgen::Generator*>(extgen_filename, extgen_func, "FairGenerator*", "extgen"));
93-
if (!extGen) {
94-
LOG(fatal) << "Failed to load external generator from " << extgen_filename << " with function " << extgen_func;
95-
exit(1);
96-
}
97-
gens.push_back(std::move(extGen));
98-
mGens.push_back(gen);
9987
}
88+
gens.push_back(std::move(extGen));
89+
mGens.push_back(gen);
10090
} else if (gen.compare("hepmc") == 0) {
101-
if (mConfigs[index].compare("") == 0) {
102-
LOG(fatal) << "No configuration provided";
103-
exit(1);
104-
} else {
105-
std::stringstream ss(mConfigs[index]);
106-
std::string pars;
107-
std::vector<std::string> params;
108-
while (std::getline(ss, pars, ',')) {
109-
params.push_back(pars);
110-
}
111-
gens.push_back(std::make_unique<o2::eventgen::GeneratorHepMC>());
112-
dynamic_cast<o2::eventgen::GeneratorHepMC*>(gens.back().get())->setFileNames(std::string(params[0]));
113-
dynamic_cast<o2::eventgen::GeneratorHepMC*>(gens.back().get())->setVersion(std::stoi(params[1]));
114-
mGens.push_back(gen);
115-
}
91+
int confHepMCIndex = std::stoi(mConfigs[index].substr(6));
92+
gens.push_back(std::make_unique<o2::eventgen::GeneratorHepMC>());
93+
auto& globalConfig = o2::conf::SimConfig::Instance();
94+
dynamic_cast<o2::eventgen::GeneratorHepMC*>(gens.back().get())->setup(*mFileOrCmdGenConfigs[confHepMCIndex], *mHepMCGenConfigs[confHepMCIndex], globalConfig);
95+
mGens.push_back(gen);
11696
} else {
117-
LOG(info) << "Generator " << gen << " not found in the list of available generators \n";
118-
}
97+
LOG(fatal) << "Generator " << gen << " not found in the list of available generators \n";
98+
exit(1);
99+
}
119100
}
120101
index++;
121102
}
@@ -165,7 +146,10 @@ namespace o2
165146
}
166147
mIndex = mCurrentFraction;
167148
}
168-
LOG(info) << "GeneratorHybrid: generating event with generator " << mGens[mIndex];
149+
if(mConfigs[mIndex].compare("") == 0)
150+
LOG(info) << "GeneratorHybrid: generating event with generator " << mGens[mIndex];
151+
else
152+
LOG(info) << "GeneratorHybrid: generating event with generator " << mConfigs[mIndex];
169153
gens[mIndex]->clearParticles(); // clear container of this class
170154
gens[mIndex]->generateEvent();
171155
// notify the sub event generator
@@ -214,7 +198,6 @@ namespace o2
214198
if (doc.HasParseError()) {
215199
LOG(error) << "Error parsing provided json file " << path;
216200
LOG(error) << " - Error -> " << rapidjson::GetParseError_En(doc.GetParseError());
217-
LOG(error) << " - Offset -> " << doc.GetErrorOffset();
218201
return false;
219202
}
220203

@@ -245,20 +228,33 @@ namespace o2
245228
mO2KineGenConfigs.push_back(std::move(o2kineConfig));
246229
mConfigs.push_back("extkinO2_" + std::to_string(mO2KineGenConfigs.size() - 1));
247230
continue;
248-
}
249-
if (gen["config"].IsArray()) {
250-
std::string config = "";
251-
for (const auto& conf : gen["config"].GetArray()) {
252-
config += conf.GetString();
253-
config += ",";
254-
}
255-
mConfigs.push_back(config);
231+
} else if (name == "external") {
232+
const auto& extconf = gen["config"];
233+
auto extConfig = TBufferJSON::FromJSON<o2::eventgen::ExternalGenConfig>(jsonValueToString(extconf).c_str());
234+
mExternalGenConfigs.push_back(std::move(extConfig));
235+
mConfigs.push_back("external_" + std::to_string(mExternalGenConfigs.size() - 1));
236+
continue;
237+
} else if (name == "hepmc") {
238+
const auto& genconf = gen["config"];
239+
const auto& cmdconf = genconf["configcmd"];
240+
const auto& hepmcconf = genconf["confighepmc"];
241+
auto cmdConfig = TBufferJSON::FromJSON<o2::eventgen::FileOrCmdGenConfig>(jsonValueToString(cmdconf).c_str());
242+
auto hepmcConfig = TBufferJSON::FromJSON<o2::eventgen::HepMCGenConfig>(jsonValueToString(hepmcconf).c_str());
243+
mFileOrCmdGenConfigs.push_back(std::move(cmdConfig));
244+
mHepMCGenConfigs.push_back(std::move(hepmcConfig));
245+
mConfigs.push_back("hepmc_" + std::to_string(mFileOrCmdGenConfigs.size() - 1));
246+
continue;
256247
} else {
257-
mConfigs.push_back(gen["config"].GetString());
248+
mConfigs.push_back("");
258249
}
259250
}
260251
else {
261-
mConfigs.push_back("");
252+
if(name == "boxgen" || name == "pythia8" || name == "extkinO2" || name == "external" || name == "hepmc"){
253+
LOG(fatal) << "No configuration provided for generator " << name;
254+
return false;
255+
}
256+
else
257+
mConfigs.push_back("");
262258
}
263259
}
264260
}
@@ -277,7 +273,6 @@ namespace o2
277273
mFractions.push_back(1);
278274
}
279275
}
280-
281276
return true;
282277
}
283278

Generators/src/GeneratorsLinkDef.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@
2929
#pragma link C++ class o2::eventgen::Generator + ;
3030
#pragma link C++ class o2::eventgen::GeneratorTGenerator + ;
3131
#pragma link C++ class o2::eventgen::GeneratorExternalParam + ;
32+
#pragma link C++ class o2::eventgen::ExternalGenConfig + ;
3233
#pragma link C++ class o2::eventgen::GeneratorGeantinos + ;
3334
#pragma link C++ class o2::conf::ConfigurableParamHelper < o2::eventgen::GeneratorExternalParam> + ;
3435
#ifdef GENERATORS_WITH_HEPMC3
3536
#pragma link C++ class o2::eventgen::GeneratorHepMC + ;
37+
#pragma link C++ class o2::eventgen::HepMCGenConfig + ;
3638
#pragma link C++ class o2::eventgen::GeneratorHepMCParam + ;
3739
#endif
3840
#ifdef GENERATORS_WITH_PYTHIA6
@@ -76,6 +78,7 @@
7678
#pragma link C++ class o2::eventgen::GeneratorTParticleParam + ;
7779
#pragma link C++ class o2::conf::ConfigurableParamHelper < o2::eventgen::GeneratorTParticleParam> + ;
7880
#pragma link C++ class o2::eventgen::GeneratorFileOrCmdParam + ;
81+
#pragma link C++ class o2::eventgen::FileOrCmdGenConfig + ;
7982
#pragma link C++ class o2::conf::ConfigurableParamHelper < o2::eventgen::GeneratorFileOrCmdParam> + ;
8083

8184
#pragma link C++ class o2::eventgen::BoxGenerator + ;

0 commit comments

Comments
 (0)