FairShip
Loading...
Searching...
No Matches
DPPythia8Generator.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
6
7#include <cmath>
8#include <vector>
9
10#include "BeamSmearingUtils.h"
11#include "FairPrimaryGenerator.h"
12#include "Pythia8/Pythia.h"
13#include "PythiaUnits.h"
14#include "ShipUnit.h"
15#include "TDatabasePDG.h" // for TDatabasePDG
16#include "TMath.h"
17#include "TROOT.h"
18#include "TSystem.h"
19
21using ShipUnit::cm;
22using ShipUnit::mm;
23const Int_t debug = 1;
24
25// ----- Default constructor -------------------------------------------
27 // fHadDecay = false;
28 fId = 2212; // proton
29 fMom = 400; // proton
30 fDP = 9900015; // DP pdg code
31 fLmin = 5000. * cm; // mm minimum decay position z ROOT units !
32 fLmax = 12000. * cm; // mm maximum decay position z
33 fpbrem = kFALSE;
34 fpbremPDF = nullptr;
35 fsmearBeam = 8 * mm; // default value for smearing beam (8 mm)
36 fPaintBeam = 5 * cm; // default value for painting beam (5 cm)
37 fdy = kFALSE;
38 fDPminM = 0.5;
39 fnRetries = 0;
40 fnDPtot = 0;
41 fShipEventNr = 0;
42 fPythia = new Pythia8::Pythia();
43 // fPythiaHadDecay = new Pythia8::Pythia();
44}
45// -------------------------------------------------------------------------
46
47void DPPythia8Generator::Print() { fPythia->settings.listAll(); };
48
49// ----- Init ----------------------------------------------------------
51 if (fUseRandom1) fRandomEngine = std::make_shared<PyTr1Rng>();
52 if (fUseRandom3) fRandomEngine = std::make_shared<PyTr3Rng>();
53 fPythia->setRndmEnginePtr(fRandomEngine);
54 // fPythiaHadDecay->setRndmEnginePtr(fRandomEngine);
55 fn = 0;
56
57 if (!fpbrem) {
58 if (debug) {
59 std::cout << "Beam Momentum " << fMom << std::endl;
60 }
61 fPythia->settings.mode("Beams:idA", fId);
62 fPythia->settings.mode("Beams:idB", 2212);
63 fPythia->settings.mode("Beams:frameType", 2);
64 fPythia->settings.parm("Beams:eA", fMom); // codespell:ignore parm
65 fPythia->settings.parm("Beams:eB", 0.); // codespell:ignore parm
66
67 if (fdy)
68 fPythia->settings.parm("PhaseSpace:mHatMin", // codespell:ignore parm
69 fDPminM);
70
71 } else {
72 if (!fpbremPDF) {
73 LOG(fatal) << "Failed in retrieving dark photon PDF for production by "
74 "proton bremstrahlung!";
75 return kFALSE;
76 }
77 }
78
79 TDatabasePDG* pdgBase = TDatabasePDG::Instance();
80 Double_t root_ctau = pdgBase->GetParticle(fDP)->Lifetime();
81
82 if (debug) {
83 std::cout << "Final particle parameters for PDGID " << fDP << ":"
84 << std::endl;
85 List(fDP);
86 }
87 if (debug) {
88 std::cout << "tau root PDG database " << root_ctau << "[s] ctau root = "
89 << root_ctau * ShipUnit::s * ShipUnit::c_light << "[cm]"
90 << std::endl;
91 }
92 fctau = fPythia->particleData.tau0(fDP); //* 3.3333e-12
93 if (debug) {
94 std::cout << "ctau pythia " << fctau << "[mm]" << std::endl;
95 }
96 int initPass = fPythia->init();
97 if (debug) {
98 std::cout << "Pythia initialisation bool: " << initPass << std::endl;
99 }
100
101 if (!initPass) {
102 LOG(fatal) << "Pythia initialisation failed";
103 return kFALSE;
104 }
105
106 return kTRUE;
107}
108// -------------------------------------------------------------------------
109
110// ----- Destructor ----------------------------------------------------
112 delete fPythia;
113 fPythia = nullptr;
114}
115// -------------------------------------------------------------------------
116
117// ----- Passing the event ---------------------------------------------
118Bool_t DPPythia8Generator::ReadEvent(FairPrimaryGenerator* cpg) {
119 auto [dx, dy] = CalculateBeamOffset(fsmearBeam, fPaintBeam);
120 Double_t tp, tS, zp, xp, yp, zS, xS, yS, pz, px, py, e, w;
121 Double_t tm, zm, xm, ym, pmz, pmx, pmy, em;
122 Int_t im;
123 Int_t imout;
124 Double_t tN, zN, xN, yN, pzN, pxN, pyN, eN;
125
126 int iDP =
127 0; // index of the chosen DP, also ensures that at least 1 DP is produced
128 // Pythia indices of the particles stored on the FairShip MCTrack stack.
129 // In this implementation the exported chain is:
130 // [Pythia event[1] beam particle, mother meson, dark photon, daughters...]
131 // Pythia event[0] is the internal system entry and is not exported.
132 std::vector<int>
133 dec_chain; // pythia indices of the particles to be stored on the stack
134 std::vector<int> dpvec; // pythia indices of DP particles
135
136 do {
137 // clear containers at every retry to avoid mixing different events
138 dec_chain.clear();
139 dpvec.clear();
140
141 // bit for proton brem
142 if (fpbrem) {
143 fPythia->event.reset();
144 double dpmom = 0;
145 double thetain = 0;
146 fpbremPDF->GetRandom2(dpmom, thetain);
147 double dpm = fPythia->particleData.m0(fDP);
148 double dpe = sqrt(dpmom * dpmom + dpm * dpm);
149 double phiin = 2. * M_PI * gRandom->Rndm();
150
151 if (debug > 1) {
152 std::cout << " Adding DP gun with p " << dpmom << " m " << dpm << " e "
153 << dpe << " theta,phi " << thetain << "," << phiin
154 << std::endl
155 << std::flush;
156 }
157 fPythia->event.append(fDP, 1, 0, 0, dpmom * sin(thetain) * cos(phiin),
158 dpmom * sin(thetain) * sin(phiin),
159 dpmom * cos(thetain), dpe, dpm);
160 }
161
162 if (!fPythia->next()) LOG(fatal) << "fPythia->next() failed";
163
164 // fPythia->event.list();
165 for (int i = 0; i < fPythia->event.size(); i++) {
166 // find all DP
167 if (abs(fPythia->event[i].id()) == fDP) {
168 dpvec.push_back(i);
169 }
170 }
171 iDP = dpvec.size();
172 fnDPtot += iDP;
173 if (iDP == 0) {
174 fnRetries += 1;
175 } else {
176 // filter DPs by vessel acceptance
177 std::vector<int> accepted;
178 for (int idx : dpvec) {
179 if (IsInVesselAcceptance(fPythia->event[idx].px(),
180 fPythia->event[idx].py(),
181 fPythia->event[idx].pz())) {
182 accepted.push_back(idx);
183 }
184 }
185 if (accepted.empty()) {
186 iDP = 0;
187 dpvec.clear();
188 fnGeoRejects += 1;
189 continue;
190 }
191 int r = static_cast<int>(gRandom->Uniform(0, accepted.size()));
192 int i = accepted[r];
193 // production vertex
194 zp = fPythia->event[i].zProd();
195 xp = fPythia->event[i].xProd();
196 yp = fPythia->event[i].yProd();
197 tp = fPythia->event[i].tProd();
198 // momentum
199 pz = fPythia->event[i].pz();
200 px = fPythia->event[i].px();
201 py = fPythia->event[i].py();
202 e = fPythia->event[i].e();
203 // old decay vertex
204 if (debug > 1) {
205 int id1 = fPythia->event[i].daughter1();
206 int id2 = fPythia->event[i].daughter2();
207 std::cout << " Debug: decay product of A: ";
208 if (id1 > 0)
209 std::cout << fPythia->event[id1].id() << " ";
210 else
211 std::cout << "none ";
212 if (id2 > 0)
213 std::cout << fPythia->event[id2].id();
214 else
215 std::cout << "none";
216 std::cout << std::endl;
217 }
218
219 // new decay vertex
220 Double_t LS = gRandom->Uniform(fLmin / mm, fLmax / mm); // in mm
221 Double_t p = TMath::Sqrt(px * px + py * py + pz * pz);
222 Double_t lam = LS / p;
223 xS = xp + lam * px;
224 yS = yp + lam * py;
225 zS = zp + lam * pz;
226 Double_t gam = e / TMath::Sqrt(e * e - p * p);
227 Double_t beta = p / e;
228 tS = tp + LS / beta; // all in Pythia units
229 w = TMath::Exp(-LS / (beta * gam * fctau)) *
230 ((fLmax / mm - fLmin / mm) / (beta * gam * fctau));
231
232 // direct mother of the DP
233 im = (Int_t)fPythia->event[i].mother1();
234
235 if (!fpbrem && im > 0) {
236 // Meson-production chain: store event[1] (beam nucleon), mother, DP
237 if (fPythia->event.size() > 1) {
238 zN = fPythia->event[1].zProd();
239 xN = fPythia->event[1].xProd();
240 yN = fPythia->event[1].yProd();
241 pzN = fPythia->event[1].pz();
242 pxN = fPythia->event[1].px();
243 pyN = fPythia->event[1].py();
244 eN = fPythia->event[1].e();
245 tN = fPythia->event[1].tProd();
246
247 cpg->AddTrack((Int_t)fPythia->event[1].id(), pxN, pyN, pzN,
248 xN * mm + dx, yN * mm + dy, zN * mm, -1, false, eN,
249 PythiaTimeToS(tN), w);
250
251 dec_chain.push_back(1);
252
253 if (debug > 1)
254 std::cout << std::endl
255 << " insert nucleon ancestor id 0"
256 << " pdg=" << fPythia->event[1].id() << " pz = " << pzN
257 << " [GeV], z = " << zN << " [mm] t = " << tN << " [mm/c]"
258 << std::endl;
259 }
260
261 zm = fPythia->event[im].zProd();
262 xm = fPythia->event[im].xProd();
263 ym = fPythia->event[im].yProd();
264 pmz = fPythia->event[im].pz();
265 pmx = fPythia->event[im].px();
266 pmy = fPythia->event[im].py();
267 em = fPythia->event[im].e();
268 tm = fPythia->event[im].tProd();
269
270 // mother of the meson is track 0 (the beam nucleon)
271 imout = 0;
272
273 cpg->AddTrack(
274 (Int_t)fPythia->event[im].id(), pmx, pmy, pmz, xm * mm + dx,
275 ym * mm + dy, zm * mm, imout, false, em, PythiaTimeToS(tm),
276 w); // convert pythia's (x,y,z[mm], t[mm/c]) to ([cm], [s])
277
278 // mother of the DP is track 1 (the meson)
279 cpg->AddTrack(fDP, px, py, pz, xp * mm + dx, yp * mm + dy, zp * mm, 1,
280 false, e, PythiaTimeToS(tp), w);
281
282 // bookkeep the indices of stored particles
283 dec_chain.push_back(im);
284 dec_chain.push_back(i);
285
286 if (debug > 1)
287 std::cout << std::endl
288 << " insert mother id " << im
289 << " pdg=" << fPythia->event[im].id() << " pmz = " << pmz
290 << " [GeV], zm = " << zm << " [mm] tm = " << tm
291 << " [mm/c]" << std::endl;
292 if (debug > 1)
293 std::cout << " ----> insert DP id " << i << " pdg=" << fDP
294 << " pz = " << pz << " [GeV] zp = " << zp
295 << " [mm] tp = " << tp << " [mm/c]" << std::endl;
296 } else {
297 // Proton bremsstrahlung or no valid mother: export only the DP as root
298 cpg->AddTrack(fDP, px, py, pz, xp * mm + dx, yp * mm + dy, zp * mm, -1,
299 false, e, PythiaTimeToS(tp), w);
300 dec_chain.push_back(i);
301
302 if (debug > 1)
303 std::cout << " ----> insert DP (root) id " << i << " pdg=" << fDP
304 << " pz = " << pz << " [GeV] zp = " << zp
305 << " [mm] tp = " << tp << " [mm/c]" << std::endl;
306 }
307 iDP = i;
308 }
309 } while (iDP ==
310 0); // ----------- avoid rare empty events w/o any DP's produced
311
312 if (fShipEventNr % 100 == 0) {
313 LOGF(info, "ship event %i / pythia event-nr %i", fShipEventNr, fn);
314 }
315 fShipEventNr += 1;
316 // number of prefix entries (nucleon + mother + DP, or just DP for fpbrem)
317 size_t prefix_len = dec_chain.size();
318 // fill a container with pythia indices of the DP decay chain
319 if (debug > 1) std::cout << "Filling daughter particles" << std::endl;
320 // if (!hadDecay){
321 for (int k = 0; k < fPythia->event.size(); k++) {
322 // if daughter of DP, copy
323 if (debug > 1)
324 std::cout << k << " pdg =" << fPythia->event[k].id() << " mum "
325 << fPythia->event[k].mother1() << std::endl;
326 im = fPythia->event[k].mother1();
327 while (im > 0) {
328 if (im == iDP) {
329 break;
330 } // pick the decay products of only 1 chosen DP
331 // if ( abs(fPythia->event[im].id())==fDP && im == iDP ){break;}
332 else {
333 im = fPythia->event[im].mother1();
334 }
335 }
336 if (im < 1) {
337 if (debug > 1) std::cout << "reject" << std::endl;
338 continue;
339 }
340 if (debug > 1) std::cout << "accept" << std::endl;
341 dec_chain.push_back(k);
342 }
343
344 // go over daughters and store them on the stack
345 // dec_chain now contains prefix entries followed by daughters
346 for (std::vector<int>::iterator it = dec_chain.begin() + prefix_len;
347 it != dec_chain.end(); ++it) {
348 // pythia index of the particle to store
349 int k = *it;
350 // find mother position on the output stack: impy -> im
351 int impy = fPythia->event[k].mother1();
352 std::vector<int>::iterator itm =
353 std::find(dec_chain.begin(), dec_chain.end(), impy);
354 im = -1; // safety
355 if (itm != dec_chain.end()) {
356 im = itm - dec_chain.begin(); // convert iterator into sequence number
357 }
358
359 Bool_t wanttracking = false;
360 if (fPythia->event[k].isFinal()) {
361 wanttracking = true;
362 }
363 pz = fPythia->event[k].pz();
364 px = fPythia->event[k].px();
365 py = fPythia->event[k].py();
366 e = fPythia->event[k].e();
367
368 cpg->AddTrack((Int_t)fPythia->event[k].id(), px, py, pz, xS * mm, yS * mm,
369 zS * mm, im, wanttracking, e, PythiaTimeToS(tS), w);
370 }
371 return kTRUE;
372}
373// -------------------------------------------------------------------------
375 // Set Parameters
376 fPythia->readString(par);
377 if (debug) {
378 std::cout << "fPythia->readString(\"" << par << "\")" << std::endl;
379 }
380}
381
382// -------------------------------------------------------------------------
std::pair< Double_t, Double_t > CalculateBeamOffset(Double_t smearBeam, Double_t paintBeam)
const Int_t debug
constexpr double PythiaTimeToS(double t)
A Pythia8 time (mm/c) in VMC units (s).
Definition PythiaUnits.h:20
constexpr Double_t cm
Pythia8::Pythia * fPythia
Bool_t ReadEvent(FairPrimaryGenerator *) override
std::shared_ptr< Pythia8::RndmEngine > fRandomEngine
Bool_t Init() override
bool IsInVesselAcceptance(Double_t px, Double_t py, Double_t pz) const
Definition Generator.h:70
Int_t fnGeoRejects
Definition Generator.h:81
constexpr double mm
Definition ShipUnit.h:25
constexpr double cm
Definition ShipUnit.h:23
constexpr double mm
Definition ShipUnit.h:25
constexpr double c_light
Definition ShipUnit.h:44
constexpr double s
Definition ShipUnit.h:31
constexpr double PythiaTimeToS(double t)
A Pythia8 time (mm/c) in VMC units (s).
Definition PythiaUnits.h:20