Skip to content

Commit 74db66f

Browse files
committed
Introduced TBufferJSON functions + local params for Pythia8, BoxGen and O2Kine
Pythia8 and BoxGen work well, but O2Kine has an issue since it returns "GeneratorFromO2Kine: Ran out of events" when running. This is ONLY the first implementation, multiple tests and fixes must still be implemented, apart from adding the other generators.
1 parent 1ae6842 commit 74db66f

File tree

11 files changed

+170
-27
lines changed

11 files changed

+170
-27
lines changed

Generators/include/Generators/BoxGenerator.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Generators/Generator.h"
1818
#include "TParticle.h"
1919
#include <vector>
20+
#include <Generators/BoxGunParam.h>
2021

2122
namespace o2::eventgen
2223
{
@@ -45,6 +46,13 @@ class BoxGenerator : public Generator
4546
SetPhiRange(phimin, phimax);
4647
}
4748

49+
BoxGenerator(BoxGenConfig const& config) : mPDG{config.pdg}, mMult{config.number}
50+
{
51+
SetEtaRange(config.eta[0], config.eta[1]);
52+
SetPRange(config.prange[0], config.prange[1]);
53+
SetPhiRange(config.phirange[0], config.phirange[1]);
54+
}
55+
4856
void SetPRange(Double32_t pmin = 0, Double32_t pmax = 10)
4957
{
5058
mPMin = pmin;

Generators/include/Generators/BoxGunParam.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,14 @@ struct BoxGunParam : public o2::conf::ConfigurableParamHelper<BoxGunParam> {
3737
O2ParamDef(BoxGunParam, "BoxGun");
3838
};
3939

40+
struct BoxGenConfig {
41+
int pdg = 211; // which particle (default pion); could make this an enum
42+
int number = 10; // how many particles
43+
double eta[2] = {-1, 1}; // eta range
44+
double prange[2] = {0.1, 5}; // energy range min, max in GeV
45+
double phirange[2] = {0., 360.}; // phi range
46+
};
47+
4048
} // end namespace eventgen
4149
} // end namespace o2
4250

Generators/include/Generators/GeneratorFromFile.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "FairGenerator.h"
1818
#include "Generators/Generator.h"
19+
#include "Generators/GeneratorFromO2KineParam.h"
1920
#include <TRandom3.h>
2021
#include <TGrid.h>
2122

@@ -69,6 +70,7 @@ class GeneratorFromO2Kine : public o2::eventgen::Generator
6970
public:
7071
GeneratorFromO2Kine() = default;
7172
GeneratorFromO2Kine(const char* name);
73+
GeneratorFromO2Kine(O2KineGenConfig const& pars);
7274

7375
bool Init() override;
7476

@@ -100,6 +102,8 @@ class GeneratorFromO2Kine : public o2::eventgen::Generator
100102
unsigned int mRngSeed = 0; //! randomizer seed, 0 for random value
101103
bool mRandomPhi = false; //! whether we want to randomize the phi angle of the particles
102104
TGrid* mAlienInstance = nullptr; // a cached connection to TGrid (needed for Alien locations)
105+
bool mGlobal = true; // whether to use the global configuration
106+
std::unique_ptr<O2KineGenConfig> mConfig; //! Local configuration of the generator
103107

104108
std::unique_ptr<o2::dataformats::MCEventHeader> mOrigMCEventHeader; //! the MC event header of the original file
105109

Generators/include/Generators/GeneratorFromO2KineParam.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ struct GeneratorFromO2KineParam : public o2::conf::ConfigurableParamHelper<Gener
3939
O2ParamDef(GeneratorFromO2KineParam, "GeneratorFromO2Kine");
4040
};
4141

42+
struct O2KineGenConfig {
43+
bool skipNonTrackable = true;
44+
bool continueMode = false;
45+
bool roundRobin = false; // read events with period boundary conditions
46+
bool randomize = false; // randomize the order of events
47+
unsigned int rngseed = 0; // randomizer seed, 0 for random value
48+
bool randomphi = false; // randomize phi angle
49+
std::string fileName = ""; // filename to read from - takes precedence over SimConfig if given
50+
};
51+
4252
} // end namespace eventgen
4353
} // end namespace o2
4454

Generators/include/Generators/GeneratorHybrid.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "SimulationDataFormat/ParticleStatus.h"
2525
#include "Generators/GeneratorHybridParam.h"
2626
#include "Generators/GeneratorHepMCParam.h"
27+
#include "Generators/GeneratorPythia8Param.h"
2728
#include "Generators/GeneratorFileOrCmdParam.h"
2829
#include "Generators/GeneratorFromO2KineParam.h"
2930
#include <TRandom3.h>
@@ -34,6 +35,8 @@
3435
#include <rapidjson/document.h>
3536
#include <rapidjson/error/en.h>
3637
#include <rapidjson/istreamwrapper.h>
38+
#include <rapidjson/writer.h>
39+
#include "TBufferJSON.h"
3740

3841
namespace o2
3942
{
@@ -53,6 +56,8 @@ class GeneratorHybrid : public Generator
5356
Bool_t importParticles() override;
5457

5558
Bool_t parseJSON(const std::string& path);
59+
template <typename T>
60+
std::string jsonValueToString(const T& value);
5661

5762
private:
5863
o2::eventgen::Generator* currentgen = nullptr;
@@ -62,6 +67,12 @@ class GeneratorHybrid : public Generator
6267
std::vector<std::string> mGens;
6368
std::vector<std::string> mConfigs;
6469
std::vector<std::string> mConfsPythia8;
70+
71+
// Parameters configurations
72+
std::vector<std::unique_ptr<o2::eventgen::BoxGenConfig>> mBoxGenConfigs;
73+
std::vector<std::unique_ptr<o2::eventgen::Pythia8GenConfig>> mPythia8GenConfigs;
74+
std::vector<std::unique_ptr<o2::eventgen::O2KineGenConfig>> mO2KineGenConfigs;
75+
6576
bool mRandomize = false;
6677
std::vector<int> mFractions;
6778
int mseqCounter = 0;

Generators/include/Generators/GeneratorPythia8.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 "Pythia8/Pythia.h"
1919
#include <functional>
20+
#include "Generators/GeneratorPythia8Param.h"
2021

2122
namespace o2
2223
{
@@ -89,6 +90,8 @@ class GeneratorPythia8 : public Generator
8990
/** default constructor **/
9091
GeneratorPythia8();
9192
/** constructor **/
93+
GeneratorPythia8(Pythia8GenConfig const& pars);
94+
/** constructor **/
9295
GeneratorPythia8(const Char_t* name, const Char_t* title = "ALICEo2 Pythia8 Generator");
9396
/** destructor **/
9497
~GeneratorPythia8() override = default;
@@ -282,6 +285,8 @@ class GeneratorPythia8 : public Generator
282285
long mInitialRNGSeed = -1; // initial seed for Pythia random number state;
283286
// will be transported to Pythia in the Init function through the Pythia::readString("Random:seed") mechanism.
284287
// Value of -1 means unitialized; 0 will be time-dependent and values >1 <= MAX_SEED concrete reproducible seeding
288+
bool mGlobalParam = true; // if true the use of the global parameters is foreseen, otherwise it means the parametric constructor has been called
289+
std::unique_ptr<Pythia8GenConfig> mGenConfig; // local configuration
285290

286291
constexpr static long MAX_SEED = 900000000;
287292

Generators/include/Generators/GeneratorPythia8Param.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ struct GeneratorPythia8Param : public o2::conf::ConfigurableParamHelper<Generato
3939
O2ParamDef(GeneratorPythia8Param, "GeneratorPythia8");
4040
};
4141

42+
struct Pythia8GenConfig {
43+
std::string config = "";
44+
std::string hooksFileName = "";
45+
std::string hooksFuncName = "";
46+
bool includePartonEvent = false; // whether to keep the event before hadronization
47+
std::string particleFilter = ""; // user particle filter
48+
int verbose = 0; // verbose control (if > 0 may show more info messages about what is going on)
49+
};
50+
4251
} // end namespace eventgen
4352
} // end namespace o2
4453

Generators/src/GeneratorFromFile.cxx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,37 @@ GeneratorFromO2Kine::GeneratorFromO2Kine(const char* name)
207207
LOG(error) << "Problem reading events from file " << name;
208208
}
209209

210+
GeneratorFromO2Kine::GeneratorFromO2Kine(O2KineGenConfig const& pars)
211+
{
212+
mGlobal = false;
213+
mConfig = std::make_unique<O2KineGenConfig>(pars);
214+
GeneratorFromO2Kine(mConfig->fileName.c_str());
215+
}
216+
210217
bool GeneratorFromO2Kine::Init()
211218
{
212219

213220
// read and set params
214-
auto& param = GeneratorFromO2KineParam::Instance();
215-
LOG(info) << "Init \'FromO2Kine\' generator with following parameters";
216-
LOG(info) << param;
217-
mSkipNonTrackable = param.skipNonTrackable;
218-
mContinueMode = param.continueMode;
219-
mRoundRobin = param.roundRobin;
220-
mRandomize = param.randomize;
221-
mRngSeed = param.rngseed;
222-
mRandomPhi = param.randomphi;
221+
222+
if(mGlobal){
223+
auto& param = GeneratorFromO2KineParam::Instance();
224+
LOG(info) << "Init \'FromO2Kine\' generator with following parameters";
225+
LOG(info) << param;
226+
mSkipNonTrackable = param.skipNonTrackable;
227+
mContinueMode = param.continueMode;
228+
mRoundRobin = param.roundRobin;
229+
mRandomize = param.randomize;
230+
mRngSeed = param.rngseed;
231+
mRandomPhi = param.randomphi;
232+
}
233+
else{
234+
mSkipNonTrackable = mConfig->skipNonTrackable;
235+
mContinueMode = mConfig->continueMode;
236+
mRoundRobin = mConfig->roundRobin;
237+
mRandomize = mConfig->randomize;
238+
mRngSeed = mConfig->rngseed;
239+
mRandomPhi = mConfig->randomphi;
240+
}
223241
if (mRandomize) {
224242
gRandom->SetSeed(mRngSeed);
225243
}

Generators/src/GeneratorHybrid.cxx

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,26 @@ namespace o2
5555
if (mConfigs[index].compare("") == 0) {
5656
gens.push_back(std::make_unique<o2::eventgen::BoxGenerator>());
5757
} else {
58-
std::stringstream ss(mConfigs[index]);
59-
std::string pars;
60-
std::vector<double> params;
61-
while (std::getline(ss, pars, ',')) {
62-
params.push_back(std::stod(pars));
63-
}
64-
gens.push_back(std::make_unique<o2::eventgen::BoxGenerator>(int(params[0]), int(params[1]), params[2], params[3], params[4], params[5], params[6], params[7]));
58+
// Get the index of boxgen configuration
59+
int confBoxIndex = std::stoi(mConfigs[index].substr(7));
60+
gens.push_back(std::make_unique<o2::eventgen::BoxGenerator>(*mBoxGenConfigs[confBoxIndex]));
6561
}
6662
mGens.push_back(gen);
6763
} else if (gen.compare(0, 7, "pythia8") == 0) {
68-
gens.push_back(std::make_unique<o2::eventgen::GeneratorPythia8>());
64+
// Check if mConfigs[index] contains pythia8_ and a number
65+
if (mConfigs[index].compare("") == 0) {
66+
gens.push_back(std::make_unique<o2::eventgen::GeneratorPythia8>());
67+
}
68+
else {
69+
// Get the index of pythia8 configuration
70+
int confPythia8Index = std::stoi(mConfigs[index].substr(8));
71+
gens.push_back(std::make_unique<o2::eventgen::GeneratorPythia8>(*mPythia8GenConfigs[confPythia8Index]));
72+
}
6973
mConfsPythia8.push_back(mConfigs[index]);
7074
mGens.push_back(gen);
7175
} else if (gen.compare("extkinO2") == 0){
72-
gens.push_back(std::make_unique<o2::eventgen::GeneratorFromO2Kine>(mConfigs[index].c_str()));
76+
int confO2KineIndex = std::stoi(mConfigs[index].substr(9));
77+
gens.push_back(std::make_unique<o2::eventgen::GeneratorFromO2Kine>(*mO2KineGenConfigs[confO2KineIndex]));
7378
mGens.push_back(gen);
7479
} else if (gen.compare("external") == 0) {
7580
if (mConfigs[index].compare("") == 0) {
@@ -122,11 +127,7 @@ namespace o2
122127
int count = 0;
123128
for (auto& gen : mGens)
124129
{
125-
if (gen == "pythia8"){
126-
auto config = std::string(std::getenv("O2_ROOT")) + mConfsPythia8[count];
127-
LOG(info) << "Setting \'Pythia8\' base configuration: " << config << std::endl;
128-
dynamic_cast<o2::eventgen::GeneratorPythia8*>(gens[count].get())->setConfig(config);
129-
} else if (gen == "pythia8pp") {
130+
if (gen == "pythia8pp") {
130131
auto config = std::string(std::getenv("O2_ROOT")) + "/share/Generators/egconfig/pythia8_inel.cfg";
131132
LOG(info) << "Setting \'Pythia8\' base configuration: " << config << std::endl;
132133
dynamic_cast<o2::eventgen::GeneratorPythia8*>(gens[count].get())->setConfig(config);
@@ -190,6 +191,15 @@ namespace o2
190191
return true;
191192
}
192193

194+
template <typename T>
195+
std::string GeneratorHybrid::jsonValueToString(const T& value)
196+
{
197+
rapidjson::StringBuffer buffer;
198+
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
199+
value.Accept(writer);
200+
return buffer.GetString();
201+
}
202+
193203
Bool_t GeneratorHybrid::parseJSON(const std::string& path)
194204
{
195205
// Parse JSON file to build map
@@ -213,9 +223,29 @@ namespace o2
213223
const auto& gens = doc["generators"];
214224
for (const auto& gen : gens.GetArray()) {
215225
// push in mInputGens the "name" of the generator
216-
mInputGens.push_back(gen["name"].GetString());
226+
std::string name = gen["name"].GetString();
227+
mInputGens.push_back(name);
217228
if (gen.HasMember("config")) {
218229
//Check if config is an array
230+
if (name == "boxgen") {
231+
const auto& boxconf = gen["config"];
232+
auto boxConfig = TBufferJSON::FromJSON<o2::eventgen::BoxGenConfig>(jsonValueToString(boxconf).c_str());
233+
mBoxGenConfigs.push_back(std::move(boxConfig));
234+
mConfigs.push_back("boxgen_" + std::to_string(mBoxGenConfigs.size() - 1));
235+
continue;
236+
} else if (name == "pythia8") {
237+
const auto& pythia8conf = gen["config"];
238+
auto pythia8Config = TBufferJSON::FromJSON<o2::eventgen::Pythia8GenConfig>(jsonValueToString(pythia8conf).c_str());
239+
mPythia8GenConfigs.push_back(std::move(pythia8Config));
240+
mConfigs.push_back("pythia8_" + std::to_string(mPythia8GenConfigs.size() - 1));
241+
continue;
242+
} else if (name == "extkinO2") {
243+
const auto& o2kineconf = gen["config"];
244+
auto o2kineConfig = TBufferJSON::FromJSON<o2::eventgen::O2KineGenConfig>(jsonValueToString(o2kineconf).c_str());
245+
mO2KineGenConfigs.push_back(std::move(o2kineConfig));
246+
mConfigs.push_back("extkinO2_" + std::to_string(mO2KineGenConfigs.size() - 1));
247+
continue;
248+
}
219249
if (gen["config"].IsArray()) {
220250
std::string config = "";
221251
for (const auto& conf : gen["config"].GetArray()) {

Generators/src/GeneratorPythia8.cxx

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ GeneratorPythia8::GeneratorPythia8() : Generator("ALICEo2", "ALICEo2 Pythia8 Gen
6363

6464
/*****************************************************************/
6565

66+
GeneratorPythia8::GeneratorPythia8(Pythia8GenConfig const& pars) : Generator("ALICEo2", "ALICEo2 Pythia8 Generator")
67+
{
68+
/** constructor **/
69+
70+
mInterface = reinterpret_cast<void*>(&mPythia);
71+
mInterfaceName = "pythia8";
72+
73+
//LOG(info) << "Instance \'Pythia8\' generator with following parameters";
74+
//LOG(info) << pars;
75+
LOG(info) << pars.config;
76+
LOG(info) << pars.hooksFileName;
77+
LOG(info) << pars.hooksFuncName;
78+
79+
mGenConfig = std::make_unique<Pythia8GenConfig>(pars);
80+
81+
setConfig(pars.config);
82+
setHooksFileName(pars.hooksFileName);
83+
setHooksFuncName(pars.hooksFuncName);
84+
mGlobalParam = false;
85+
}
86+
87+
/*****************************************************************/
88+
6689
GeneratorPythia8::GeneratorPythia8(const Char_t* name, const Char_t* title) : Generator(name, title)
6790
{
6891
/** constructor **/
@@ -557,7 +580,12 @@ void GeneratorPythia8::pruneEvent(Pythia8::Event& event, Select select)
557580
}
558581
}
559582
}
560-
if (GeneratorPythia8Param::Instance().verbose) {
583+
int verbose;
584+
if(mGlobalParam)
585+
verbose = GeneratorPythia8Param::Instance().verbose;
586+
else
587+
verbose = mGenConfig->verbose;
588+
if (verbose) {
561589
LOG(info) << "Pythia event was pruned from " << event.size()
562590
<< " to " << pruned.size() << " particles";
563591
}
@@ -570,7 +598,11 @@ void GeneratorPythia8::initUserFilterCallback()
570598
{
571599
mUserFilterFcn = [](Pythia8::Particle const&) -> bool { return true; };
572600

573-
auto& filter = GeneratorPythia8Param::Instance().particleFilter;
601+
std::string filter;
602+
if (mGlobalParam)
603+
filter = GeneratorPythia8Param::Instance().particleFilter;
604+
else
605+
filter = mGenConfig->particleFilter;
574606
if (filter.size() > 0) {
575607
LOG(info) << "Initializing the callback for user-based particle pruning " << filter;
576608
auto expandedFileName = o2::utils::expandShellVarsInFileName(filter);
@@ -599,7 +631,12 @@ Bool_t
599631
// event record in the AOD.
600632

601633
std::function<bool(const Pythia8::Particle&)> partonSelect = [](const Pythia8::Particle&) { return true; };
602-
if (not GeneratorPythia8Param::Instance().includePartonEvent) {
634+
bool includeParton;
635+
if (mGlobalParam)
636+
includeParton = GeneratorPythia8Param::Instance().includePartonEvent;
637+
else
638+
includeParton = mGenConfig->includePartonEvent;
639+
if (not includeParton) {
603640

604641
// Select pythia particles
605642
partonSelect = [](const Pythia8::Particle& particle) {

0 commit comments

Comments
 (0)