Skip to content

Commit 45ffc57

Browse files
committed
Fix incorrect argument order in PYTHIA8 coalescence generator
1 parent fc9fea6 commit 45ffc57

1 file changed

Lines changed: 123 additions & 46 deletions

File tree

‎MC/config/PWGLF/pythia8/generator_pythia8_coalescence.C‎

Lines changed: 123 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,63 @@
1+
12
#if !defined(__CLING__) || defined(__ROOTCLING__)
23
#include "Pythia8/Pythia.h"
34
#include "FairGenerator.h"
45
#include "FairPrimaryGenerator.h"
56
#include "Generators/GeneratorPythia8.h"
67
#include "fairlogger/Logger.h"
78
#include "TRandom3.h"
8-
#include "TParticlePDG.h"
9-
#include "TDatabasePDG.h"
109
#include "TSystem.h"
11-
#include "TMath.h"
10+
#include <algorithm>
1211
#include <cmath>
12+
#include <cstdlib>
13+
#include <cstdint>
1314
#include <vector>
14-
#include <fstream>
1515
#include <string>
16-
using namespace Pythia8;
1716
#endif
1817

1918
R__ADD_INCLUDE_PATH($O2DPG_MC_CONFIG_ROOT)
2019
#include "MC/config/common/external/generator/CoalescencePythia8.h"
21-
/// First version of the simple coalescence generator based PYTHIA8
2220

23-
class GeneratorPythia8Coalescence : public o2::eventgen::GeneratorPythia8
21+
class GeneratorPythia8Coalescence
22+
: public o2::eventgen::GeneratorPythia8
2423
{
2524
public:
26-
/// Constructor
27-
GeneratorPythia8Coalescence(std::vector<unsigned int> pdgList, int input_trigger_ratio = 1, double coal_momentum = 0.4)
28-
: o2::eventgen::GeneratorPythia8()
25+
GeneratorPythia8Coalescence(
26+
std::vector<int> pdgList,
27+
int input_trigger_ratio = 1,
28+
double coal_momentum = 0.4,
29+
bool trivialCoal = false)
30+
: o2::eventgen::GeneratorPythia8(),
31+
mRequestedPdgList(std::move(pdgList)),
32+
mInverseTriggerRatio(input_trigger_ratio),
33+
mCoalMomentum(coal_momentum),
34+
mTrivialCoal(trivialCoal)
2935
{
30-
fmt::printf(">> Coalescence generator %d\n", input_trigger_ratio);
31-
mInverseTriggerRatio = input_trigger_ratio;
32-
mCoalMomentum = coal_momentum;
33-
mPdgList = pdgList;
36+
if (mRequestedPdgList.empty() ||
37+
mInverseTriggerRatio < 1) {
38+
LOG(fatal) << "Invalid coalescence configuration";
39+
}
40+
41+
// The afterburner expects positive PDG codes.
42+
// Preserve the signed list for event acceptance.
43+
for (auto pdg : mRequestedPdgList) {
44+
unsigned int absPdg =
45+
static_cast<unsigned int>(std::abs(pdg));
46+
47+
if (std::find(mPdgList.begin(),
48+
mPdgList.end(),
49+
absPdg) == mPdgList.end()) {
50+
mPdgList.push_back(absPdg);
51+
}
52+
}
53+
54+
LOG(info) << "Coalescence generator: p0 = "
55+
<< mCoalMomentum
56+
<< ", trivialCoal = " << mTrivialCoal
57+
<< ", trigger ratio = " << mInverseTriggerRatio;
3458
}
35-
/// Destructor
36-
~GeneratorPythia8Coalescence() = default;
59+
60+
~GeneratorPythia8Coalescence() override = default;
3761

3862
bool Init() override
3963
{
@@ -45,55 +69,108 @@ public:
4569
protected:
4670
bool generateEvent() override
4771
{
48-
fmt::printf(">> Generating event %d\n", mGeneratedEvents);
49-
// Simple straightforward check to alternate generators
50-
if (mGeneratedEvents % mInverseTriggerRatio == 0)
51-
{
52-
fmt::printf(">> Generating coalescence event %d\n", mGeneratedEvents);
72+
if (mGeneratedEvents % mInverseTriggerRatio == 0) {
73+
5374
bool genOk = false;
54-
int localCounter{0};
55-
while (!genOk)
56-
{
57-
if (GeneratorPythia8::generateEvent())
58-
{
59-
genOk = CoalescencePythia8(mPythia.event, mPdgList, mCoalMomentum);
75+
uint64_t localCounter = 0;
76+
77+
while (!genOk) {
78+
79+
if (!GeneratorPythia8::generateEvent()) {
80+
continue;
81+
}
82+
83+
++localCounter;
84+
85+
bool coalHappened = CoalescencePythia8(
86+
mPythia.event,
87+
mPdgList,
88+
mTrivialCoal,
89+
mCoalMomentum
90+
);
91+
92+
if (!coalHappened) {
93+
continue;
6094
}
61-
localCounter++;
95+
96+
// Accept only events containing a requested
97+
// nucleus or antinucleus.
98+
genOk = containsRequestedNucleus(mPythia.event);
6299
}
63-
fmt::printf(">> Coalescence successful after %i generations\n", localCounter);
64-
std::cout << std::endl << std::endl;
100+
101+
LOG(info) << "Coalescence successful after "
102+
<< localCounter << " generations";
103+
65104
notifySubGenerator(1);
66-
}
67-
else
68-
{
69-
fmt::printf(">> Generating minimum-bias event %d\n", mGeneratedEvents);
70-
// Generate minimum-bias event
105+
106+
} else {
107+
71108
bool genOk = false;
72-
while (!genOk)
73-
{
109+
110+
while (!genOk) {
74111
genOk = GeneratorPythia8::generateEvent();
75112
}
113+
76114
notifySubGenerator(0);
77115
}
78-
mGeneratedEvents++;
116+
117+
++mGeneratedEvents;
79118
return true;
80119
}
81120

121+
bool containsRequestedNucleus(
122+
const Pythia8::Event& event) const
123+
{
124+
for (int i = 0; i < event.size(); ++i) {
125+
126+
if (!event[i].isFinal()) {
127+
continue;
128+
}
129+
130+
const int pdg = event[i].id();
131+
132+
if (std::find(mRequestedPdgList.begin(),
133+
mRequestedPdgList.end(),
134+
pdg) != mRequestedPdgList.end()) {
135+
return true;
136+
}
137+
}
138+
139+
return false;
140+
}
82141

83142
private:
84-
std::vector<unsigned int> mPdgList; /// list of pdg codes to be generated
85-
float mCoalMomentum = 0.4; /// coalescence momentum
86-
uint64_t mGeneratedEvents = 0; /// number of events generated so far
87-
int mInverseTriggerRatio = 1; /// injection gap
143+
// Signed PDG codes requested by the user
144+
std::vector<int> mRequestedPdgList;
145+
146+
// Absolute PDG codes used by the afterburner
147+
std::vector<unsigned int> mPdgList;
148+
149+
int mInverseTriggerRatio = 1;
150+
double mCoalMomentum = 0.4;
151+
bool mTrivialCoal = false;
152+
153+
uint64_t mGeneratedEvents = 0;
88154
};
89155

90-
///___________________________________________________________
91-
FairGenerator *generateCoalescence(std::vector<unsigned int> pdgList, int input_trigger_ratio, double coal_momentum = 0.4)
156+
/// Generator factory
157+
FairGenerator* generateCoalescence(
158+
std::vector<int> pdgList,
159+
int input_trigger_ratio = 1,
160+
double coal_momentum = 0.4,
161+
bool trivialCoal = false)
92162
{
93-
auto myGen = new GeneratorPythia8Coalescence(pdgList, input_trigger_ratio, coal_momentum);
163+
auto myGen = new GeneratorPythia8Coalescence(
164+
pdgList,
165+
input_trigger_ratio,
166+
coal_momentum,
167+
trivialCoal
168+
);
169+
94170
auto seed = (gRandom->TRandom::GetSeed() % 900000000);
171+
95172
myGen->readString("Random:setSeed on");
96173
myGen->readString("Random:seed " + std::to_string(seed));
174+
97175
return myGen;
98176
}
99-
///___________________________________________________________

0 commit comments

Comments
 (0)