Skip to content

Commit 0bb50c7

Browse files
preghenellasawenzel
authored andcommitted
Extend the external generator (extgen) concept to any FairGenerator
Modifies the behaviour of o2sim with the external generator option -g extgen. It now expects that the external configuration macro defined via --extGenFile returns a FairGenerator* instead of a TGenerator* as it was before. This allows for a more generic handling of customised external generators. The TGenerator interface functionality is preserved, as everything can be defined in the configuration macro. Please see for example share/external/tgenerator.C share/external/pythia6.C This change simplifies significantly the code and provides the user with more flexibility.
1 parent f461526 commit 0bb50c7

5 files changed

Lines changed: 67 additions & 72 deletions

File tree

Generators/share/external/extgen.C

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,30 @@
11
/**
2-
It is mandatory that the function returns a TGenerator*
2+
It is mandatory that the function returns a FairGenerator*
33
whereas there are no restrictions on the function name
44
and the arguments to the function prototype.
55
6-
TGenerator *extgen(double energy = 2760.);
6+
FairGenerator *extgen(Int_t aPDG = 211.);
77
8-
It is mandatory to define the units used by the
9-
concerned generator to allow for the proper
10-
conversion to the units used by the simulation.
11-
The above is done by defining the following
12-
variable mandatory global variables
13-
14-
double momentumUnit; // [GeV/c]
15-
double energyUnit; // [GeV/c]
16-
double positionUnit; // [cm]
17-
double timeUnit; // [s]
18-
19-
and assign the proper values, either initialising them
20-
or withing the function to be called
218
**/
229

23-
double momentumUnit = 1.;
24-
double energyUnit = 1.;
25-
double positionUnit = 1.;
26-
double timeUnit = 1.;
10+
class MyGenerator : public FairGenerator
11+
{
12+
public:
13+
MyGenerator(Int_t aPDG) : FairGenerator("MyGenerator"), mPDG(aPDG){};
14+
Bool_t ReadEvent(FairPrimaryGenerator* primGen) override
15+
{
16+
primGen->AddTrack(mPDG, 0.5, 0.5, 0., 0., 0., 0.);
17+
return kTRUE;
18+
};
19+
20+
private:
21+
Int_t mPDG;
22+
};
2723

28-
TGenerator*
29-
extgen()
24+
FairGenerator*
25+
extgen(Int_t aPDG = 211)
3026
{
31-
std::cout << "This is a template function for an external generator" << std::endl;
32-
auto gen = new TGenerator;
27+
std::cout << "This is a template function for an custom generator" << std::endl;
28+
auto gen = new MyGenerator(aPDG);
3329
return gen;
3430
}

Generators/share/external/hijing.C

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44

55
/// \author R+Preghenella - October 2018
66

7-
double momentumUnit = 1.; // [GeV/c]
8-
double energyUnit = 1.; // [GeV/c]
9-
double positionUnit = 0.1; // [cm]
10-
double timeUnit = 3.3356410e-12; // [s]
11-
127
R__LOAD_LIBRARY(libTHijing)
138

14-
TGenerator*
9+
FairGenerator*
1510
hijing(double energy = 5020., double bMin = 0., double bMax = 20.)
1611
{
12+
// instance and configure Hijing
1713
auto hij = new AliGenHijing(-1);
1814
hij->SetEnergyCMS(energy);
1915
hij->SetImpactParameterRange(bMin, bMax);
@@ -28,5 +24,13 @@ TGenerator*
2824
hij->SetSelectAll(0);
2925
hij->SetPtHardMin(2.3);
3026
hij->Init();
31-
return hij->GetMC();
27+
28+
// instance and configure TGenerator interface
29+
auto tgen = new o2::eventgen::GeneratorTGenerator();
30+
tgen->setMomentumUnit(1.); // [GeV/c]
31+
tgen->setEnergyUnit(1.); // [GeV/c]
32+
tgen->setPositionUnit(0.1); // [cm]
33+
tgen->setTimeUnit(3.3356410e-12); // [s]
34+
tgen->setTGenerator(hij);
35+
return tgen;
3236
}

Generators/share/external/pythia6.C

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,27 @@
44

55
/// \author R+Preghenella - October 2018
66

7-
double momentumUnit = 1.; // [GeV/c]
8-
double energyUnit = 1.; // [GeV/c]
9-
double positionUnit = 0.1; // [cm]
10-
double timeUnit = 3.3356410e-12; // [s]
11-
127
R__LOAD_LIBRARY(libpythia6)
138

149
void configure(TPythia6* py6, const char* params);
1510

16-
TGenerator*
11+
FairGenerator*
1712
pythia6(double energy = 14000., const char* params = nullptr)
1813
{
14+
// instance and configure Pythia6
1915
auto py6 = TPythia6::Instance();
2016
if (params)
2117
configure(py6, params);
2218
py6->Initialize("CMS", "p", "p", energy);
23-
return py6;
19+
20+
// instance and configure TGenerator interface
21+
auto tgen = new o2::eventgen::GeneratorTGenerator();
22+
tgen->setMomentumUnit(1.); // [GeV/c]
23+
tgen->setEnergyUnit(1.); // [GeV/c]
24+
tgen->setPositionUnit(0.1); // [cm]
25+
tgen->setTimeUnit(3.3356410e-12); // [s]
26+
tgen->setTGenerator(py6);
27+
return tgen;
2428
}
2529

2630
void configure(TPythia6* py6, const char* params)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// template macro to configure a generic TGenerator interface
2+
3+
/// \author R+Preghenella - March 2019
4+
5+
FairGenerator*
6+
tgenerator()
7+
{
8+
// instance and configure an external TGenerator
9+
auto gen = new TGenerator;
10+
11+
// instance and configure TGenerator interface
12+
auto tgen = new o2::eventgen::GeneratorTGenerator();
13+
tgen->setMomentumUnit(1.); // [GeV/c]
14+
tgen->setEnergyUnit(1.); // [GeV/c]
15+
tgen->setPositionUnit(0.1); // [cm]
16+
tgen->setTimeUnit(3.3356410e-12); // [s]
17+
tgen->setTGenerator(gen);
18+
return tgen;
19+
}

Generators/src/GeneratorFactory.cxx

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
#include <SimConfig/SimConfig.h>
1919
#include <Generators/GeneratorFromFile.h>
2020
#include <Generators/Pythia8Generator.h>
21-
#include <Generators/GeneratorTGenerator.h>
2221
#include "TROOT.h"
2322
#include "TSystem.h"
2423
#include "TGlobal.h"
@@ -138,8 +137,7 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
138137
py8Gen->SetParameters("ParticleDecays:limitTau0 on");
139138
primGen->AddGenerator(py8Gen);
140139
} else if (genconfig.compare("extgen") == 0) {
141-
// external generator via TGenerator interface
142-
auto tgen = new o2::eventgen::GeneratorTGenerator();
140+
// external generator via configuration macro
143141
auto extgen_filename = conf.getExtGeneratorFileName();
144142
auto extgen_func = conf.getExtGeneratorFuncName();
145143
if (extgen_func.empty()) {
@@ -154,47 +152,21 @@ void GeneratorFactory::setPrimaryGenerator(o2::conf::SimConfig const& conf, Fair
154152
LOG(FATAL) << "Cannot find " << extgen_filename << FairLogger::endl;
155153
return;
156154
}
157-
/** setup convertion units **/
158-
if (gROOT->GetGlobal("momentumUnit")) {
159-
auto ptr = (double*)gROOT->GetGlobal("momentumUnit")->GetAddress();
160-
tgen->setMomentumUnit(*ptr);
161-
} else {
162-
LOG(FATAL) << "Mandatory global variable 'momentumUnit' not defined";
163-
}
164-
if (gROOT->GetGlobal("energyUnit")) {
165-
auto ptr = (double*)gROOT->GetGlobal("energyUnit")->GetAddress();
166-
tgen->setEnergyUnit(*ptr);
167-
} else {
168-
LOG(FATAL) << "Mandatory global variable 'energyUnit' not defined";
169-
}
170-
if (gROOT->GetGlobal("positionUnit")) {
171-
auto ptr = (double*)gROOT->GetGlobal("positionUnit")->GetAddress();
172-
tgen->setPositionUnit(*ptr);
173-
} else {
174-
LOG(FATAL) << "Mandatory global variable 'positionUnit' not defined";
175-
}
176-
if (gROOT->GetGlobal("timeUnit")) {
177-
auto ptr = (double*)gROOT->GetGlobal("timeUnit")->GetAddress();
178-
tgen->setMomentumUnit(*ptr);
179-
} else {
180-
LOG(FATAL) << "Mandatory global variable 'timeUnit' not defined";
181-
}
182-
/** retrieve TGenerator **/
155+
/** retrieve FairGenerator **/
183156
auto extgen_gfunc = extgen_func.substr(0, extgen_func.find_first_of('('));
184157
if (!gROOT->GetGlobalFunction(extgen_gfunc.c_str())) {
185158
LOG(FATAL) << "Global function '"
186159
<< extgen_gfunc
187160
<< "' not defined";
188161
}
189-
if (strcmp(gROOT->GetGlobalFunction(extgen_gfunc.c_str())->GetReturnTypeName(), "TGenerator*")) {
162+
if (strcmp(gROOT->GetGlobalFunction(extgen_gfunc.c_str())->GetReturnTypeName(), "FairGenerator*")) {
190163
LOG(FATAL) << "Global function '"
191164
<< extgen_gfunc
192-
<< "' does not return a 'TGenerator*' type";
165+
<< "' does not return a 'FairGenerator*' type";
193166
}
194-
gROOT->ProcessLine(Form("TGenerator *__extgen = dynamic_cast<TGenerator *>(%s);", extgen_func.c_str()));
195-
auto extgen_ptr = (TGenerator**)gROOT->GetGlobal("__extgen")->GetAddress();
196-
tgen->setTGenerator(*extgen_ptr);
197-
primGen->AddGenerator(tgen);
167+
gROOT->ProcessLine(Form("FairGenerator *__extgen = dynamic_cast<FairGenerator *>(%s);", extgen_func.c_str()));
168+
auto extgen_ptr = (FairGenerator**)gROOT->GetGlobal("__extgen")->GetAddress();
169+
primGen->AddGenerator(*extgen_ptr);
198170
} else if (genconfig.compare("toftest") == 0) { // 1 muon per sector and per module
199171
LOG(INFO) << "Init tof test generator -> 1 muon per sector and per module";
200172
for (int i = 0; i < 18; i++) {

0 commit comments

Comments
 (0)