FairShip
Loading...
Searching...
No Matches
TEvtGenDecayer.cxx
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-3.0-or-later
2// SPDX-FileCopyrightText: Copyright CERN for the benefit of the SHiP
3// Collaboration
4
5#include "TEvtGenDecayer.h"
6
7#include "FairLogger.h"
8#include "TClonesArray.h"
9#include "TLorentzVector.h"
10#include "TMCProcess.h"
11#include "TParticle.h"
12#include "TPythia8.h"
13#include "TRandom.h"
14#include "TSystem.h"
15
16// EvtGen includes
17#include "EvtGen/EvtGen.hh"
18#include "EvtGenBase/EvtAbsRadCorr.hh"
19#include "EvtGenBase/EvtId.hh"
20#include "EvtGenBase/EvtMTRandomEngine.hh"
21#include "EvtGenBase/EvtPDL.hh"
22#include "EvtGenBase/EvtParticle.hh"
23#include "EvtGenBase/EvtParticleFactory.hh"
24#include "EvtGenBase/EvtRandom.hh"
25#include "EvtGenBase/EvtVector4R.hh"
26#include "EvtGenExternal/EvtExternalGenList.hh"
27
28//_____________________________________________________________________________
30 : TVirtualMCDecayer(),
31 fPythia8Decayer(nullptr),
32 fEvtGen(nullptr),
33 fDecayFile(""),
34 fParticleFile(""),
35 fEvtGenProducts(nullptr),
36 fHasEvtGenDecay(kFALSE) {
37 // Default constructor
38 fEvtGenParticles.clear();
39 fEvtGenProducts = new TClonesArray("TParticle");
40}
41
42//_____________________________________________________________________________
44 // Destructor
46 if (fEvtGen) delete fEvtGen;
48}
49
50//_____________________________________________________________________________
52 // Initialize both EvtGen and Pythia8 decayers
53
54 // Initialize fallback Pythia8 decayer
55 fPythia8Decayer = new TPythia8Decayer();
56 fPythia8Decayer->Init();
57
58 // Initialize EvtGen if decay files are specified
59 if (fDecayFile != "" && fParticleFile != "") {
60 // Set up EvtGen random engine with ROOT's random number generator seed
61 UInt_t seed = gRandom->GetSeed();
62
63 // Also seed Pythia8 with the same seed for consistency
64 TPythia8* pythia8 = TPythia8::Instance();
65 if (pythia8) {
66 pythia8->Pythia8()->readString(Form("Random:seed = %u", seed));
67 pythia8->Pythia8()->readString("Random:setSeed = on");
68 pythia8->Pythia8()->init(); // Re-initialize with new seed
69 LOG(debug) << "TEvtGenDecayer: Pythia8 RNG seeded with " << seed;
70 }
71
72 EvtRandomEngine* randomEngine = new EvtMTRandomEngine(seed);
73 EvtRandom::setRandomEngine(randomEngine);
74
75 LOG(info) << "TEvtGenDecayer: Random engine initialized with seed " << seed;
76
77 // Set up external generators with PHOTOS radiative corrections
78 std::string photonType("gamma");
79 bool useEvtGenRandom(true);
80 bool convertPythiaCodes(false);
81 std::string pythiaDir("");
82
83 EvtExternalGenList* externalGenList = new EvtExternalGenList(
84 convertPythiaCodes, pythiaDir, photonType, useEvtGenRandom);
85
86 // Get the interface to the radiative correction engine (PHOTOS)
87 EvtAbsRadCorr* radCorrEngine = externalGenList->getPhotosModel();
88 if (radCorrEngine) {
89 LOG(info) << "TEvtGenDecayer: PHOTOS radiative corrections enabled";
90 } else {
91 LOG(warning) << "TEvtGenDecayer: PHOTOS not available, proceeding "
92 "without radiative corrections";
93 }
94
95 // Get the interface to other external generators
96 std::list<EvtDecayBase*> extraModels = externalGenList->getListOfModels();
97
98 // Create EvtGen instance
99 fEvtGen = new EvtGen(fDecayFile.Data(), fParticleFile.Data(), randomEngine,
100 radCorrEngine, &extraModels, 1, false);
101
102 LOG(debug) << "TEvtGenDecayer: EvtGen created with " << EvtPDL::entries()
103 << " particle entries";
104
105 LOG(info) << "TEvtGenDecayer: EvtGen initialized with decay file: "
106 << fDecayFile.Data()
107 << ", particle file: " << fParticleFile.Data();
108 } else {
109 LOG(debug)
110 << "TEvtGenDecayer: No EvtGen files specified, using Pythia8 only";
111 }
112
113 // Log summary at end of Init
114 LOG(info) << "TEvtGenDecayer initialized - EvtGen: "
115 << (fEvtGen ? "YES" : "NO")
116 << ", Pythia8 fallback: " << (fPythia8Decayer ? "YES" : "NO")
117 << ", Particles configured for EvtGen: " << fEvtGenParticles.size();
118
119 for (size_t i = 0; i < fEvtGenParticles.size(); i++) {
120 LOG(debug) << " EvtGen particle: PDG " << fEvtGenParticles[i];
121 }
122}
123
124//_____________________________________________________________________________
125void TEvtGenDecayer::Decay(Int_t pdg, TLorentzVector* p) {
126 // Main decay method - route to EvtGen or Pythia8 based on particle type
127
128 // Debug output for decay calls (highest verbosity)
129 LOG(debug2) << "TEvtGenDecayer::Decay called for PDG " << pdg
130 << " (E=" << p->E() << ", p=" << p->P() << ")";
131
132 // Check if this particle should use EvtGen
133 Bool_t useEvtGen = UseEvtGenForParticle(pdg);
134 Bool_t evtGenAvailable = (fEvtGen != nullptr);
135
136 LOG(debug1) << "UseEvtGenForParticle(" << pdg << ") = " << useEvtGen
137 << ", EvtGen available = " << evtGenAvailable;
138
139 if (useEvtGen && evtGenAvailable) {
140 LOG(debug) << "Routing PDG " << pdg << " to EvtGen";
141 DecayWithEvtGen(pdg, p);
142 } else {
143 LOG(debug) << "Routing PDG " << pdg << " to Pythia8 fallback";
144 DecayWithPythia8(pdg, p);
145 }
146}
147
148//_____________________________________________________________________________
150 // Check if this particle should be handled by EvtGen
151 LOG(debug2) << "UseEvtGenForParticle: Checking PDG " << pdg
152 << " against EvtGen particle list";
153
154 for (Int_t evtGenPdg : fEvtGenParticles) {
155 if (pdg == evtGenPdg) {
156 LOG(debug2) << "UseEvtGenForParticle: Match found - PDG " << pdg
157 << " should use EvtGen";
158 return kTRUE;
159 }
160 }
161
162 LOG(debug) << "UseEvtGenForParticle: PDG " << pdg
163 << " not in EvtGen particle list, falling back to Pythia8";
164 return kFALSE;
165}
166
167//_____________________________________________________________________________
168void TEvtGenDecayer::DecayWithEvtGen(Int_t pdg, TLorentzVector* p) {
169 // Decay particle using EvtGen
170
171 if (!fEvtGen) {
172 // Fall back to Pythia8 if EvtGen not available
173 DecayWithPythia8(pdg, p);
174 return;
175 }
176
177 LOG(debug1) << "DecayWithEvtGen: Decaying PDG " << pdg << " with EvtGen";
178
179 try {
180 // Convert TLorentzVector to EvtGen format
181 EvtVector4R p4(p->E(), p->Px(), p->Py(), p->Pz());
182
183 // Get EvtGen particle ID from PDG code
184 EvtId evtId = EvtPDL::evtIdFromStdHep(pdg);
185
186 // Check if EvtId is valid before creating particle
187 if (evtId.getId() < 0 ||
188 static_cast<size_t>(evtId.getId()) >= EvtPDL::entries()) {
189 LOG(warning) << "TEvtGenDecayer: Invalid EvtId for PDG " << pdg
190 << " (EvtId: " << evtId.getId()
191 << "), falling back to Pythia8";
192 DecayWithPythia8(pdg, p);
193 return;
194 }
195
196 // Create EvtGen particle
197 EvtParticle* parent = EvtParticleFactory::particleFactory(evtId, p4);
198
199 if (parent) {
200 // Generate decay
201 fEvtGen->generateDecay(parent);
202
203 // Convert EvtGen decay products to ROOT format
204 ConvertEvtGenDecay(parent);
205
206 LOG(debug1)
207 << "TEvtGenDecayer: EvtGen decay products converted and stored";
208
209 // Clean up
210 parent->deleteTree();
211 } else {
212 LOG(warning) << "TEvtGenDecayer: Could not create EvtParticle for PDG "
213 << pdg;
214 // Fall back to Pythia8
215 DecayWithPythia8(pdg, p);
216 }
217 } catch (const std::exception& e) {
218 LOG(error) << "TEvtGenDecayer: EvtGen exception for PDG " << pdg << ": "
219 << e.what();
220 // Fall back to Pythia8 on any exception
221 DecayWithPythia8(pdg, p);
222 }
223}
224
225//_____________________________________________________________________________
226void TEvtGenDecayer::ConvertEvtGenDecay(EvtParticle* parent) {
227 // Convert EvtGen decay products to ROOT/GEANT4 format
228 // Clear previous decay products
229 fEvtGenProducts->Clear();
230 fHasEvtGenDecay = kTRUE;
231
232 int nDaughters = parent->getNDaug();
233 LOG(debug2) << "ConvertEvtGenDecay: Converting " << nDaughters
234 << " daughter particles";
235
236 // Convert each daughter particle to TParticle format
237 for (int i = 0; i < nDaughters; i++) {
238 EvtParticle* daughter = parent->getDaug(i);
239 EvtVector4R p4 = daughter->getP4();
240 int pdgId = EvtPDL::getStdHep(daughter->getId());
241
242 // Create TParticle (ROOT format)
243 TParticle* particle = new ((*fEvtGenProducts)[i]) TParticle();
244 particle->SetPdgCode(pdgId);
245 particle->SetMomentum(p4.get(1), p4.get(2), p4.get(3),
246 p4.get(0)); // px, py, pz, E
247 particle->SetProductionVertex(0, 0, 0, 0); // Produced at origin
248 particle->SetStatusCode(1); // Stable particle
249
250 LOG(debug2) << " Daughter " << i << ": PDG=" << pdgId
251 << ", E=" << p4.get(0) << ", px=" << p4.get(1)
252 << ", py=" << p4.get(2) << ", pz=" << p4.get(3);
253 }
254
255 LOG(debug1) << "ConvertEvtGenDecay: Stored "
256 << fEvtGenProducts->GetEntriesFast() << " particles";
257}
258
259//_____________________________________________________________________________
260void TEvtGenDecayer::DecayWithPythia8(Int_t pdg, TLorentzVector* p) {
261 // Decay particle using Pythia8 decayer
262 LOG(debug1) << "DecayWithPythia8: Handling PDG " << pdg << " with Pythia8";
263
264 // Mark that this decay will use Pythia8 products
265 fHasEvtGenDecay = kFALSE;
266
267 if (fPythia8Decayer) {
268 fPythia8Decayer->Decay(pdg, p);
269 }
270}
271
272//_____________________________________________________________________________
273Int_t TEvtGenDecayer::ImportParticles(TClonesArray* particles) {
274 // Import particles - return EvtGen products if available, otherwise delegate
275 // to Pythia8
277 LOG(debug1) << "ImportParticles: Returning "
278 << fEvtGenProducts->GetEntriesFast() << " EvtGen particles";
279
280 // Copy EvtGen particles to the provided array
281 particles->Clear();
282 int nParticles = fEvtGenProducts->GetEntriesFast();
283 for (int i = 0; i < nParticles; i++) {
284 TParticle* srcParticle = static_cast<TParticle*>(fEvtGenProducts->At(i));
285 new ((*particles)[i]) TParticle(*srcParticle);
286 }
287
288 // Reset the flag for next decay
289 fHasEvtGenDecay = kFALSE;
290 return nParticles;
291 } else {
292 // Fall back to Pythia8
293 LOG(debug1) << "ImportParticles: Delegating to Pythia8";
294 if (fPythia8Decayer) {
295 return fPythia8Decayer->ImportParticles(particles);
296 }
297 return 0;
298 }
299}
300
301//_____________________________________________________________________________
302void TEvtGenDecayer::SetEvtGenDecayFile(const char* decayFile) {
303 fDecayFile = decayFile;
304}
305
306//_____________________________________________________________________________
307void TEvtGenDecayer::SetEvtGenParticleFile(const char* particleFile) {
308 fParticleFile = particleFile;
309}
310
311//_____________________________________________________________________________
313 fEvtGenParticles.push_back(pdg);
314 LOG(info) << "TEvtGenDecayer: Added PDG " << pdg << " to EvtGen particle list"
315 << " (Total: " << fEvtGenParticles.size() << ")";
316}
317
318//_____________________________________________________________________________
320 if (fPythia8Decayer) fPythia8Decayer->SetForceDecay(type);
321}
322
323//_____________________________________________________________________________
325 if (fPythia8Decayer) fPythia8Decayer->ForceDecay();
326}
327
328//_____________________________________________________________________________
330 if (fPythia8Decayer) return fPythia8Decayer->GetPartialBranchingRatio(ipart);
331 return 0.0;
332}
333
334//_____________________________________________________________________________
336 if (fPythia8Decayer) return fPythia8Decayer->GetLifetime(kf);
337 return 0.0;
338}
339
340//_____________________________________________________________________________
342 if (fPythia8Decayer) fPythia8Decayer->ReadDecayTable();
343}
const Int_t debug
std::vector< Int_t > fEvtGenParticles
void ReadDecayTable() override
Int_t ImportParticles(TClonesArray *particles) override
void SetForceDecay(Int_t type) override
void DecayWithPythia8(Int_t pdg, TLorentzVector *p)
TString fParticleFile
void AddEvtGenParticle(Int_t pdg)
void SetEvtGenParticleFile(const char *particleFile)
Float_t GetLifetime(Int_t kf) override
TClonesArray * fEvtGenProducts
void Decay(Int_t pdg, TLorentzVector *p) override
void Init() override
void ForceDecay() override
Float_t GetPartialBranchingRatio(Int_t ipart) override
void ConvertEvtGenDecay(EvtParticle *parent)
void SetEvtGenDecayFile(const char *decayFile)
~TEvtGenDecayer() override
void DecayWithEvtGen(Int_t pdg, TLorentzVector *p)
TPythia8Decayer * fPythia8Decayer
Bool_t UseEvtGenForParticle(Int_t pdg)