Skip to content

Commit 79bfee1

Browse files
authored
Example user generator (#64)
1 parent eada5c2 commit 79bfee1

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/// \author R+Preghenella - June 2021
2+
3+
// Example of an implementation of a simple user generator
4+
// that injects particles at wish according to predefined setting
5+
// which are defined by configuration strings
6+
//
7+
//
8+
// usage: o2sim -g external --configKeyValues 'GeneratorExternal.fileName=user_generator.C;GeneratorExternal.funcName=user_generator("one_proton_and_one_photon")'
9+
10+
#include <string>
11+
12+
using namespace o2::eventgen;
13+
14+
class user_generator_class : public Generator
15+
{
16+
public:
17+
user_generator_class() { };
18+
~user_generator_class() = default;
19+
void selectConfiguration(std::string val) { mSelectedConfiguration = val; };
20+
21+
// at init we check that the selected configuration is known
22+
bool Init() override {
23+
Generator::Init();
24+
if (std::find(mKnownConfigurations.begin(), mKnownConfigurations.end(), mSelectedConfiguration) != mKnownConfigurations.end()) {
25+
std::cout << " --- user_generator initialised with configuration: " << mSelectedConfiguration << std::endl;
26+
return true;
27+
}
28+
std::cout << " --- [ERROR] user_generator has unknown selected configuration: " << mSelectedConfiguration << std::endl;
29+
return false;
30+
};
31+
32+
// it generatrEvent we do nothing
33+
bool generateEvent() override { return true; };
34+
35+
// at importParticles we add particles to the output particle vector
36+
// according to the selected configuration
37+
bool importParticles() override {
38+
TLorentzVector lv;
39+
TParticle particle;
40+
particle.SetFirstMother(-1);
41+
particle.SetLastMother(-1);
42+
particle.SetFirstDaughter(-1);
43+
particle.SetLastDaughter(-1);
44+
particle.SetStatusCode(1);
45+
particle.SetProductionVertex(0., 0., 0., 0.);
46+
if (mSelectedConfiguration.compare("one_proton_and_one_photon") == 0) {
47+
// one proton
48+
lv.SetPtEtaPhiM(10., 0.5, M_PI, 0.93827200);
49+
particle.SetPdgCode(2212);
50+
particle.SetMomentum(lv);
51+
mParticles.push_back(particle);
52+
// one photon
53+
lv.SetPtEtaPhiM(10., -0.5, M_PI, 0.);
54+
particle.SetPdgCode(22);
55+
particle.SetMomentum(lv);
56+
mParticles.push_back(particle);
57+
return true;
58+
}
59+
if (mSelectedConfiguration.compare("two_protons_and_two_photons") == 0) {
60+
// one proton
61+
lv.SetPtEtaPhiM(10., 0.5, M_PI, 0.93827200);
62+
particle.SetPdgCode(2212);
63+
particle.SetMomentum(lv);
64+
mParticles.push_back(particle);
65+
// another proton
66+
lv.SetPtEtaPhiM(10., 0.5, -M_PI, 0.93827200);
67+
particle.SetPdgCode(2212);
68+
particle.SetMomentum(lv);
69+
mParticles.push_back(particle);
70+
// one photon
71+
lv.SetPtEtaPhiM(10., -0.5, M_PI, 0.);
72+
particle.SetPdgCode(22);
73+
particle.SetMomentum(lv);
74+
mParticles.push_back(particle);
75+
// another photon
76+
lv.SetPtEtaPhiM(10., -0.5, -M_PI, 0.);
77+
particle.SetPdgCode(22);
78+
particle.SetMomentum(lv);
79+
mParticles.push_back(particle);
80+
return true;
81+
}
82+
83+
// failure
84+
return false;
85+
};
86+
87+
private:
88+
89+
const std::vector<std::string> mKnownConfigurations = {"one_proton_and_one_photon", "two_protons_and_two_photons"};
90+
std::string mSelectedConfiguration = "";
91+
92+
};
93+
94+
FairGenerator*
95+
user_generator(std::string configuration = "empty")
96+
{
97+
auto gen = new user_generator_class;
98+
gen->selectConfiguration(configuration);
99+
return gen;
100+
}

0 commit comments

Comments
 (0)