FairShip
Loading...
Searching...
No Matches
FixedTargetGenerator.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 <TGeoManager.h>
8
9#include <algorithm>
10#include <array>
11#include <cmath>
12#include <cstdlib>
13#include <string>
14#include <vector>
15
16#include "BeamSmearingUtils.h"
17#include "FairMCEventHeader.h"
18#include "FairPrimaryGenerator.h"
19#include "HNLPythia8Generator.h"
20#include "MeanMaterialBudget.h"
21#include "Pythia8Plugins/EvtGen.h"
22#include "PythiaUnits.h"
23#include "ShipUnit.h"
24#include "TGeoBBox.h"
25#include "TGeoNode.h"
26#include "TGeoVolume.h"
27#include "TH1.h"
28#include "TMCProcess.h"
29#include "TMath.h"
30#include "TROOT.h"
31
32namespace {
33// B(D_s+ -> tau+ nu_tau), world average (PDG 2024)
34constexpr double kDsToTauNuBR = 0.0536;
35
36// Set the D_s -> tau nu branching fraction and rescale the other D_s decay
37// channels so that the branching fractions still add up to one.
38void SetDsToTauNuBR(Pythia8::Pythia* pythia, double br) {
39 auto entry = pythia->particleData.particleDataEntryPtr(431);
40 if (!entry) {
41 return;
42 }
43 int iTauNu = -1;
44 for (int i = 0; i < entry->sizeChannels(); ++i) {
45 const auto& ch = entry->channel(i);
46 if (ch.multiplicity() == 2 && std::abs(ch.product(0)) == 15 &&
47 std::abs(ch.product(1)) == 16) {
48 iTauNu = i;
49 }
50 }
51 if (iTauNu < 0) {
52 return;
53 }
54 const double scale = (1. - br) / (1. - entry->channel(iTauNu).bRatio());
55 for (int i = 0; i < entry->sizeChannels(); ++i) {
56 auto& ch = entry->channel(i);
57 ch.bRatio(i == iTauNu ? br : ch.bRatio() * scale);
58 }
59}
60} // namespace
61
63using ShipUnit::cm;
64using ShipUnit::mm;
65
66// ----- Default constructor -------------------------------------------
68 fUseRandom1 = kFALSE;
69 fUseRandom3 = kTRUE;
70 fSeed = 0;
71 fMom = 400; // proton
72 fLogger = FairLogger::GetLogger();
73 targetName = "cave_1/target_vacuum_box_1/TargetArea_1/HeVolume_1";
74 xOff = 0;
75 yOff = 0;
76 zOff = 0;
77 fsmearBeam = 8 * mm; // default value for smearing beam (8 mm)
78 fPaintBeam = 5 * cm; // default value for painting beam (5 cm)
79 tauOnly = false;
80 JpsiMainly = false;
81 DrellYan = false;
82 PhotonCollision = false;
83 G4only = false;
84 OnlyMuons = false;
85 maxCrossSection = 0.;
86 EMax = 0;
87 fBoost = 1.;
88 withEvtGen = kFALSE;
89 withNtuple = kFALSE;
90 chicc = 1.7e-3; // prob to produce primary ccbar pair/pot on Mo
91 // (cf. python/heavyFlavourScaling.py)
92 chibb = 1.6e-7; // prob to produce primary bbbar pair/pot on Mo
93 nrpotspill = 5.E13; // nr of protons / spill
94 setByHand = kFALSE;
95 Debug = kFALSE;
96 targetFromGeometry = kFALSE;
97 Option = "Primary";
98 wspill = 1.; // event weight == 1 for primary events
99 heartbeat = 1000;
100}
101Bool_t FixedTargetGenerator::InitForCharmOrBeauty(const TString& fInName,
102 Int_t nev, Double_t npot,
103 Int_t nStart) {
104 Option = "charm";
105 nEntry = nStart;
106 // open input file with charm or beauty
107 if (nStart < 0) {
108 LOG(error) << "FixedTargetGenerator: startEvent must be >= 0, got "
109 << nStart;
110 return kFALSE;
111 }
112 fin = TFile::Open(fInName, "READ");
113 if (!fin) {
114 LOG(error) << "FixedTargetGenerator: could not open input file "
115 << fInName.Data();
116 delete fin;
117 fin = nullptr;
118 return kFALSE;
119 }
120 nTree = dynamic_cast<TNtuple*>(
121 fin->FindObjectAny("pythia6")); // old format, simple ntuple
122 if (!nTree) {
123 LOG(error) << "FixedTargetGenerator: cannot find tree pythia6 in file "
124 << fInName.Data();
125 fin->Close();
126 delete fin;
127 fin = nullptr;
128 return kFALSE;
129 }
130 nEvents = nTree->GetEntries();
131 if (nStart >= nEvents) {
132 LOG(error) << "FixedTargetGenerator: startEvent " << nStart
133 << " is out of range for " << nEvents << " entries";
134 fin->Close();
135 delete fin;
136 fin = nullptr;
137 nTree = nullptr;
138 return kFALSE;
139 }
140 bool ok = true;
141 ok &= (nTree->SetBranchAddress("id", &n_id) >= 0);
142 ok &= (nTree->SetBranchAddress("px", &n_px) >= 0);
143 ok &= (nTree->SetBranchAddress("py", &n_py) >= 0);
144 ok &= (nTree->SetBranchAddress("pz", &n_pz) >= 0);
145 ok &= (nTree->SetBranchAddress("M", &n_M) >= 0);
146 ok &= (nTree->SetBranchAddress("E", &n_E) >= 0);
147 ok &= (nTree->SetBranchAddress("mid", &n_mid) >= 0);
148 ok &= (nTree->SetBranchAddress("mpx", &n_mpx) >= 0);
149 ok &= (nTree->SetBranchAddress("mpy", &n_mpy) >= 0);
150 ok &= (nTree->SetBranchAddress("mpz", &n_mpz) >= 0);
151 ok &= (nTree->SetBranchAddress("mE", &n_mE) >= 0);
152 if (!ok) {
153 LOG(error)
154 << "FixedTargetGenerator: failed to bind one or more required branches";
155 fin->Close();
156 delete fin;
157 fin = nullptr;
158 nTree = nullptr;
159 return kFALSE;
160 }
161 if (nTree->GetBranch("k")) {
162 LOG(info) << "+++has branch+++";
163 nTree->SetBranchAddress("k", &ck);
164 }
165 hasHadronCount = nTree->GetBranch("n_hadrons") && nTree->GetBranch("k");
166 if (hasHadronCount) {
167 nTree->SetBranchAddress("n_hadrons", &n_hadrons);
168 }
169 // check if we deal with charm or beauty:
170 nTree->GetEvent(0);
171 if (!setByHand && n_M > 5) {
172 chicc = chibb;
173 LOG(info) << "automatic detection of beauty, configured for beauty";
174 LOG(info) << "bb cross section / mbias " << chicc;
175 } else {
176 LOG(info) << "cc cross section / mbias " << chicc;
177 }
178 // convert pot to weight corresponding to one spill of 5e13 pot
179 // get histogram with number of pot to normalise
180 // pot are counted double, i.e. for each signal, i.e. pot/2.
181 // not TH1F: rootUtils, used by makeCascade.py, books a TH1D
182 auto* potHist = dynamic_cast<TH1*>(fin->Get("2"));
183 if (!potHist) {
184 LOG(error) << "FixedTargetGenerator: histogram '2' not found in input file";
185 fin->Close();
186 delete fin;
187 fin = nullptr;
188 nTree = nullptr;
189 return kFALSE;
190 }
191 // number of primary interactions and of events on the file. Without the
192 // hadron count every event is a pair of entries.
193 Double_t nrcpot = std::floor(potHist->GetBinContent(1) / 2.);
194 Double_t nInputEvents = nEvents / 2.;
195 if (hasHadronCount) {
196 // count the events and the primary (depth 1) ones, and move the start to
197 // the first entry of an event
198 nTree->SetBranchStatus("*", 0);
199 nTree->SetBranchStatus("n_hadrons", 1);
200 nTree->SetBranchStatus("k", 1);
201 nrcpot = 0.;
202 nInputEvents = 0.;
203 Int_t firstEntry = nEvents;
204 for (Int_t i = 0; i < nEvents;) {
205 nTree->GetEntry(i);
206 if (firstEntry == nEvents && i >= nStart) {
207 firstEntry = i;
208 }
209 nInputEvents += 1.;
210 if (ck < 1.5) {
211 nrcpot += 1.;
212 }
213 i += std::max(1, static_cast<Int_t>(TMath::Nint(n_hadrons)));
214 }
215 nTree->SetBranchStatus("*", 1);
216 if (firstEntry == nEvents) {
217 LOG(error) << "FixedTargetGenerator: no event starts at or after entry "
218 << nStart;
219 fin->Close();
220 delete fin;
221 fin = nullptr;
222 nTree = nullptr;
223 return kFALSE;
224 }
225 if (firstEntry != nStart) {
226 LOG(info) << "start entry " << nStart
227 << " is inside an event, starting at entry " << firstEntry;
228 }
229 nEntry = firstEntry;
230 } else if (nStart % 2 != 0) {
231 // events are pairs of entries, so an odd start entry is the second hadron
232 // of an event: start at the next event instead of pairing hadrons from
233 // two different events
234 if (nStart + 1 >= nEvents) {
235 LOG(error) << "FixedTargetGenerator: no event starts at or after entry "
236 << nStart;
237 fin->Close();
238 delete fin;
239 fin = nullptr;
240 nTree = nullptr;
241 return kFALSE;
242 }
243 LOG(info) << "start entry " << nStart
244 << " is inside an event, starting at entry " << nStart + 1;
245 nEntry = nStart + 1;
246 }
247 wspill = nrpotspill * chicc / nrcpot * nInputEvents / nev;
248 LOG(info) << "Input file: " << fInName.Data() << " with " << nEvents
249 << " entries, corresponding to nr-pot=" << (nrcpot / chicc);
250 LOG(info) << "weight " << wspill << " corresponding to " << nrpotspill
251 << " p.o.t. per spill for " << nev << " events to process";
252
253 pot = 0.;
254 // Determine fDs on this file for primaries
255 nDsprim = 0;
256 ntotprim = 0;
257
258 return kTRUE;
259}
260// ----- Default Init -------------------------------------------
262 fPythiaP =
263 new Pythia8::Pythia(); // pythia needed also for G4only, to copy 2mu BRs
264 if (Option == "Primary" && !G4only) {
265 fPythiaN = new Pythia8::Pythia();
266 } else if (Option != "charm" && Option != "beauty" && !G4only) {
267 LOG(error) << "Option not known " << Option.Data() << ", abort";
268 return kFALSE;
269 }
270 if (fUseRandom1) fRandomEngine = std::make_shared<PyTr1Rng>();
271 if (fUseRandom3) fRandomEngine = std::make_shared<PyTr3Rng>();
272 std::vector<int> r = {221, 221, 223, 223, 113, 331, 333};
273 std::vector<int> c = {6, 7, 5, 7, 5, 6, 9}; // decay channel mumu mumuX
274
275 // FTFT tune (arXiv:2608.29076), shared with makeCascadePythia8.py
276 TString ftftTuneFile = getenv("FAIRSHIP");
277 ftftTuneFile += "/gconfig/FTFT.cmnd";
278 if (!fTune.IsNull() && fTune != "FTFT") {
279 LOG(fatal)
280 << "FixedTargetGenerator: unknown Pythia8 tune " << fTune.Data()
281 << ", choices are FTFT, default or empty for the Pythia8 default";
282 }
283 if (fTune == "FTFT" && (Option != "Primary" || G4only)) {
284 LOG(warning) << "FixedTargetGenerator: Pythia8 tune " << fTune.Data()
285 << " has no effect without Pythia8 primary interactions";
286 }
287
288 if (Option == "Primary" && !G4only) {
289 fPythiaP->settings.mode("Beams:idB", 2212);
290 fPythiaN->settings.mode("Beams:idB", 2112);
291 } else {
292 fPythiaP->readString("ProcessLevel:all = off");
293 }
294 std::array<Pythia8::Pythia*, 2> plist = {fPythiaP, fPythiaN};
295 Int_t pcount = 0;
296 for (const auto& fPythia : plist) {
297 if (pcount > 0 && (Option != "Primary" || G4only)) {
298 continue;
299 }
300 pcount += 1;
301 fPythia->setRndmEnginePtr(fRandomEngine);
302 fPythia->settings.mode("Random:seed", fSeed);
303 fPythia->settings.mode("Next:numberCount", heartbeat);
304 if (Option == "Primary") {
305 fPythia->settings.mode("Beams:idA", 2212);
306 fPythia->settings.mode("Beams:frameType", 2);
307 fPythia->settings.parm("Beams:eA", fMom); // codespell:ignore parm
308 fPythia->settings.parm("Beams:eB", 0.); // codespell:ignore parm
309 // apply the tune before the process settings below, which Tune:pp
310 // would otherwise reset
311 if (fTune == "FTFT" && !G4only) {
312 if (!fPythia->readFile(ftftTuneFile.Data())) {
313 LOG(fatal) << "FixedTargetGenerator: Pythia8 could not read tune "
314 << "settings from " << ftftTuneFile.Data();
315 }
316 LOG(info) << "FixedTargetGenerator: using Pythia8 tune "
317 << fTune.Data();
318 }
319 }
320 if (JpsiMainly) {
321 // use this for all onia productions
322 fPythia->readString("Onia:all(3S1) = on");
323 fPythia->readString(
324 "443:new J/psi J/psi 3 0 0 3.09692 0.00009 3.09602 "
325 " 3.09782 0. 1 1 0 1 0");
326 fPythia->readString("443:addChannel = 1 1. 0 -13 13");
327 fPythia->readString(
328 "553:new Upsilon Upsilon 3 0 0 9.46030 0.00005 "
329 "9.45980 9.46080 0.00000e+00 0 1 0 1 0");
330 fPythia->readString("553:addChannel = 1 1. 0 -13 13");
331 }
332 if (DrellYan) {
333 fPythia->readString("WeakSingleBoson:ffbar2gmZ = on");
334 // Z0/gamma -> mu+ mu-
335 fPythia->readString("23:onMode = off");
336 fPythia->readString("23:onIfAll = 13 13");
337 fPythia->readString("23:mMin = 0.5");
338 fPythia->readString("PhaseSpace:mHatMin = 0.5");
339 fPythia->readString("PartonLevel:FSR = on");
340 fPythia->readString("PDF:pSet = 4");
341 }
342 if (PhotonCollision) {
343 fPythia->readString("PhotonCollision:gmgm2mumu = on");
344 }
345 if (!DrellYan && !PhotonCollision) {
346 fPythia->readString("SoftQCD:inelastic = on");
347 fPythia->readString("PhotonCollision:gmgm2mumu = on");
348 fPythia->readString("PromptPhoton:all = on");
349 fPythia->readString("WeakBosonExchange:all = on");
350 fPythia->readString("WeakSingleBoson:all = on");
351 }
352 if (tauOnly) {
353 fPythia->readString(
354 "431:new D_s+ D_s- 1 3 0 1.96849 0.00000 0.00000 "
355 "0.00000 1.49900e-01 0 1 0 1 0");
356 fPythia->readString(
357 "431:addChannel = 1 0.0640000 0 -15 16");
358 }
359
360 // D_s -> tau nu_tau: Pythia8 has 6.4%, set the world average and rescale
361 // the other channels, since it sets the tau neutrino yield
362 SetDsToTauNuBR(fPythia, kDsToTauNuBR);
363 // find all long lived particles in pythia
364 Int_t n = 1;
365 while (n != 0) {
366 n = fPythia->particleData.nextId(n);
367 std::shared_ptr<Pythia8::ParticleDataEntry> p =
368 fPythia->particleData.particleDataEntryPtr(n);
369 if (p->tau0() > 1) {
370 std::string particle = std::to_string(n) + ":mayDecay = false";
371 fPythia->readString(particle);
372 LOG(info) << "Made " << p->name().c_str()
373 << " stable for Pythia, should decay in Geant4";
374 }
375 }
376 // boost branching fraction of rare di-muon decays
377 // eta omega rho0 eta' phi
378 if (fBoost != 1.) {
379 LOG(info) << "Rescale BRs of dimuon decays in Pythia: " << fBoost;
380 for (unsigned int i = 0; i < r.size(); ++i) {
381 std::shared_ptr<Pythia8::ParticleDataEntry> V =
382 fPythia->particleData.particleDataEntryPtr(r[i]);
383 Pythia8::DecayChannel ch = V->channel(c[i]);
384 if (TMath::Abs(ch.product(0)) != 13 ||
385 TMath::Abs(ch.product(1)) != 13) {
386 LOG(info) << "this is not the right decay channel: " << r[i] << " "
387 << c[i];
388 } else {
389 TString tmp = "";
390 tmp += r[i];
391 tmp += ":";
392 tmp += c[i];
393 tmp += ":bRatio =";
394 tmp += fBoost * ch.bRatio();
395 fPythia->readString(tmp.Data());
396 }
397 }
398 }
399 fPythia->init();
400 }
401 // Initialize EvtGen.
402 if (withEvtGen) {
403 // Use EVTGENDATA environment variable to find EvtGen data files
404 const char* evtgendata = getenv("EVTGENDATA");
405 if (!evtgendata) {
406 LOG(fatal) << "EVTGENDATA environment variable not set";
407 }
408 TString DecayFile = TString(evtgendata) + "/DECAY.DEC";
409
410 TString ParticleFile = TString(evtgendata) + "/evt.pdl";
411 std::cout << "Using $EVTGENDATA " << evtgendata << std::endl;
412 EvtExternalGenList* extPtr = new EvtExternalGenList();
413 std::list<EvtDecayBase*> models = extPtr->getListOfModels();
414 TString UdecayFile = getenv("FAIRSHIP");
415 UdecayFile += "/gconfig/USERDECAY.DEC";
416 evtgenP = new Pythia8::EvtGenDecays(fPythiaP, DecayFile.Data(),
417 ParticleFile.Data(), extPtr);
418 evtgenP->readDecayFile(
419 UdecayFile.Data()); // will make update of EvtGen with user decay file
420 // use one instance of EvtGen, requires patch to Pythia8Plugins/EvtGen.h
421 if (Option == "Primary") {
422 evtgenN =
423 new Pythia8::EvtGenDecays(fPythiaN, DecayFile.Data(), "", extPtr);
424 }
425 }
426 if (targetFromGeometry) {
427 // Use geometry-based coordinates passed from run_simScript.py
428 start[0] = xOff;
429 start[1] = yOff;
430 start[2] = startZ + zOff;
431 end[0] = xOff;
432 end[1] = yOff;
433 end[2] = endZ;
434 LOG(info) << "FixedTargetGenerator: Using geometry-based target "
435 "coordinates startZ="
436 << startZ << " endZ=" << endZ;
437 // find maximum interaction length
440 } else if (targetName != "") {
441 // Fallback to fragile TGeo navigation for backward compatibility
442 TGeoNavigator* nav = gGeoManager->GetCurrentNavigator();
443 if (nav->CheckPath(targetName)) {
444 nav->cd(targetName);
445 } else {
446 LOG(fatal) << "Invalid target volume specified";
447 }
448 TGeoNode* target = nav->GetCurrentNode();
449 TObjArray* nodes = target->GetVolume()->GetNodes();
450 // Get the first and last node of the target to calculate the material seen
451 TGeoNode* first = static_cast<TGeoNode*>(nodes->At(0));
452 TGeoNode* last = static_cast<TGeoNode*>(nodes->At(nodes->GetLast()));
453 nav->cd(targetName + "/" + first->GetName());
454 TGeoBBox* sha = static_cast<TGeoBBox*>(first->GetVolume()->GetShape());
455 Double_t dz = sha->GetDZ();
456 Double_t origin[3] = {0, 0, -dz};
457 Double_t master[3] = {0, 0, 0};
458 nav->LocalToMaster(origin, master);
459 startZ = master[2];
460 nav->cd(targetName + "/" + last->GetName());
461 sha = static_cast<TGeoBBox*>(last->GetVolume()->GetShape());
462 dz = sha->GetDZ();
463 origin[2] = +dz;
464 nav->LocalToMaster(origin, master);
465 endZ = master[2];
466 start[0] = xOff;
467 start[1] = yOff;
468 start[2] = startZ + zOff;
469 end[0] = xOff;
470 end[1] = yOff;
471 end[2] = endZ;
472 // find maximum interaction length
475 } else {
476 LOG(fatal) << "No target set.";
477 }
478
479 return kTRUE;
480}
481// -------------------------------------------------------------------------
482
483// ----- Destructor ----------------------------------------------------
485// -------------------------------------------------------------------------
486
487// ----- Passing the event ---------------------------------------------
488Bool_t FixedTargetGenerator::ReadEvent(FairPrimaryGenerator* cpg) {
489 // Calculate beam smearing and painting
490 auto [dx, dy] = CalculateBeamOffset(fsmearBeam, fPaintBeam);
491
492 if (G4only) {
493 // Geant4 determines the primary interaction point, so skip the expensive
494 // interaction-point sampling below (it would only be discarded).
495 IncrementCounter("generated_events");
496 IncrementCounter("g4only_events");
497 IncrementCounter("stored_tracks");
498 cpg->AddTrack(2212, 0., 0., fMom, (xOff + dx) / cm, (yOff + dy) / cm,
499 start[2], -1, kTRUE, -1., 0., 1.);
500 return kTRUE;
501 }
502
503 // Cascade input: read the first hadron of the event before placing it,
504 // since its cascade depth sets how far into the target it is produced.
505 Int_t nInteractions = 1;
506 if (Option != "Primary") {
507 if (nEntry == nEvents) {
508 LOG(info) << "Rewind input file: " << nEntry;
509 nEntry = 0;
510 }
511 nTree->GetEvent(nEntry);
512 nEntry += 1;
513 if (nTree->GetBranch("k")) {
514 nInteractions = std::max(1, static_cast<Int_t>(TMath::Nint(ck)));
515 }
516 }
517
518 Double_t zinter = 0;
519 Double_t ZoverA = 1.;
520 if (!targetName.IsNull()) {
521 // Interaction point, sampled along the beam line from the interaction
522 // probability in the material (copied from GenieGenerator and adapted to
523 // hadrons). A hadron from cascade depth k is produced in the k-th
524 // interaction: each further interaction point is sampled from the material
525 // after the previous one.
526 constexpr int kMaxTries = 100000;
527 Double_t sigma;
528 Double_t zinterStart = start[2];
529 for (Int_t iInter = 0; iInter < nInteractions; ++iInter) {
530 const Double_t from[3] = {start[0], start[1], zinterStart};
531 Double_t prob2int = -1.;
532 Double_t rndm = 0.;
533 Double_t zTry = zinterStart;
534 int tries = 0;
535 while (prob2int < rndm && tries < kMaxTries) {
536 ++tries;
537 // place x,y,z uniform along path
538 zTry = gRandom->Uniform(zinterStart, end[2]);
539 Double_t point[3] = {xOff, yOff, zTry};
541 Double_t interLength = mparam[8];
542 TGeoNode* node = gGeoManager->FindNode(point[0], point[1], point[2]);
543 TGeoMaterial* mat = nullptr;
544 if (node && !gGeoManager->IsOutside()) {
545 mat = node->GetVolume()->GetMaterial();
546 ZoverA = mat->GetZ() / mat->GetA();
548 prob2int = TMath::Exp(-interLength) * sigma / maxCrossSection;
549 } else {
550 prob2int = 0.;
551 }
552 rndm = gRandom->Uniform(0., 1.);
553 }
554 if (prob2int < rndm) {
555 // no material left downstream: keep the previous interaction point
556 break;
557 }
558 zinter = zTry;
559 zinterStart = zinter;
560 }
561 zinter = zinter * cm;
562 }
563 Pythia8::Pythia* fPythia;
564 if (Option == "Primary") {
565 constexpr int kMaxPythiaRetries = 100;
566 if (gRandom->Uniform(0., 1.) < ZoverA) {
567 int retries = 0;
568 while (!fPythiaP->next()) {
569 if (++retries >= kMaxPythiaRetries) {
570 LOG(error) << "PythiaP failed to generate an event after "
571 << kMaxPythiaRetries << " retries";
572 return kFALSE;
573 }
574 LOG(warning) << "PythiaP failed to generate event, retry " << retries;
575 }
576 if (withEvtGen) {
577 evtgenP->decay();
578 }
579 fPythia = fPythiaP;
580 } else {
581 int retries = 0;
582 while (!fPythiaN->next()) {
583 if (++retries >= kMaxPythiaRetries) {
584 LOG(error) << "PythiaN failed to generate an event after "
585 << kMaxPythiaRetries << " retries";
586 return kFALSE;
587 }
588 LOG(warning) << "PythiaN failed to generate event, retry " << retries;
589 }
590 if (withEvtGen) {
591 evtgenN->decay();
592 }
593 fPythia = fPythiaN;
594 }
595 } else {
596 // the first hadron of the event was read above; reject an event that the
597 // input file cuts short before using any of it
598 const Int_t nHadrons =
599 hasHadronCount ? static_cast<Int_t>(TMath::Nint(n_hadrons)) : 2;
600 if (nEntry + nHadrons - 1 > nEvents) {
601 LOG(error) << "FixedTargetGenerator: input ends inside an event";
602 return kFALSE;
603 }
604 IncrementCounter("charm_input_pairs");
605 // primary: produced by the beam proton, cascade depth 1. Files without
606 // the depth branch tag the beam proton by its zero transverse momentum,
607 // which misses beam protons that scattered elastically before.
608 const Bool_t isPrimary =
609 nTree->GetBranch("k")
610 ? ck < 1.5
611 : n_mid == 2212 && (n_mpx * n_mpx + n_mpy * n_mpy) < 1E-5;
612 // sanity check, count number of p.o.t. on input file.
613 // every event appears twice, i.e.
614 if (isPrimary) {
615 pot += 0.5;
616 ntotprim += 1;
617 }
618 Int_t idabs = static_cast<int>(TMath::Abs(n_id));
619 if (idabs == 431) {
620 nDsprim += 1;
621 }
622 fPythiaP->event.reset();
623 Int_t nID1 = n_id;
624 fPythiaP->event.append(static_cast<int>(n_id), 1, 0, 0, n_px, n_py, n_pz,
625 n_E, n_M, 0., 9.);
626 TMCProcess procID = kPTransportation;
627 if (isPrimary) {
628 procID = kPPrimary;
629 }
630 cpg->AddTrack(static_cast<int>(n_mid), n_mpx, n_mpy, n_mpz,
631 (xOff + dx) * cm, (yOff + dy) * cm, zinter * cm, -1, kFALSE,
632 n_mE, 0., wspill, procID);
633 IncrementCounter("stored_tracks");
634 // the other heavy-flavour hadrons of the event
635 for (Int_t iHadron = 1; iHadron < nHadrons; ++iHadron) {
636 nTree->GetEvent(nEntry);
637 if (nHadrons == 2 && nID1 * n_id > 0) {
638 LOG(info) << "same sign charm: " << nEntry << ", " << nID1 << ", "
639 << n_id;
640 }
641 nEntry += 1;
642 fPythiaP->event.append(static_cast<int>(n_id), 1, 0, 0, n_px, n_py, n_pz,
643 n_E, n_M, 0., 9.);
644 }
645 fPythiaP->next();
646 fPythia = fPythiaP;
647 }
648 if (withEvtGen) {
649 fPythia->moreDecays();
650 } // let the very short lived resonances decay via Pythia8
651 if (Debug) {
652 fPythia->event.list();
653 }
654 TMCProcess procID;
655 for (Int_t ii = 1; ii < fPythia->event.size(); ii++) {
656 Double_t e = fPythia->event[ii].e();
657 Double_t m = fPythia->event[ii].m();
658 Double_t pz = fPythia->event[ii].pz();
659 Int_t id = fPythia->event[ii].id();
660 Bool_t wanttracking = kTRUE;
661 if (e - m < EMax || !fPythia->event[ii].isFinal() || pz < 0) {
662 wanttracking = kFALSE;
663 }
665 // don't track underlying event
666 if (fabs(id) != 13) {
667 wanttracking = kFALSE;
668 }
669 }
670 Double_t z = fPythia->event[ii].zProd() * mm + zinter * cm;
671 Double_t x = fPythia->event[ii].xProd() * mm + xOff * cm + dx * cm;
672 Double_t y = fPythia->event[ii].yProd() * mm + yOff * cm + dy * cm;
673 Double_t tof = PythiaTimeToS(fPythia->event[ii].tProd());
674 Double_t px = fPythia->event[ii].px();
675 Double_t py = fPythia->event[ii].py();
676 Int_t im = fPythia->event[ii].mother1() - 1;
677 procID = kPPrimary;
678 if (Option != "Primary") {
679 procID = kPDecay;
680 im += 1;
681 if (ii == 1) {
682 procID = kPHadronic;
683 }
684 } else {
685 if (ii < 3) {
686 im = -1;
687 }
688 }
689 IncrementCounter("stored_tracks");
690 if (wanttracking) {
691 IncrementCounter("tracked_final_state_particles");
692 } else {
693 IncrementCounter("skipped_final_state_particles");
694 }
695 cpg->AddTrack(id, px, py, pz, x * cm, y * cm, z * cm, im, wanttracking, e,
696 tof, wspill, procID);
697 if (withNtuple) {
698 int idabs = TMath::Abs(id);
699 if (idabs < 10) {
700 continue;
701 }
702 if (idabs < 18 || idabs == 22 || idabs == 111 || idabs == 221 ||
703 idabs == 223 || idabs == 331 || idabs == 211 || idabs == 113 ||
704 idabs == 333 || idabs == 321 || idabs == 2212) {
705 Int_t moID = fPythia->event[im + 1].id();
706 fNtuple->Fill(0, id, px, py, pz, e, x * cm, y * cm, z * cm, moID);
707 }
708 }
709 }
710 IncrementCounter("generated_events");
711 return kTRUE;
712}
713// -------------------------------------------------------------------------
std::pair< Double_t, Double_t > CalculateBeamOffset(Double_t smearBeam, Double_t paintBeam)
constexpr double PythiaTimeToS(double t)
A Pythia8 time (mm/c) in VMC units (s).
Definition PythiaUnits.h:20
constexpr Double_t cm
constexpr Double_t m
Bool_t InitForCharmOrBeauty(const TString &fInName, Int_t nev, Double_t npots=5E13, Int_t nStart=0)
~FixedTargetGenerator() override
Bool_t ReadEvent(FairPrimaryGenerator *) override
TNtuple * fNtuple
special option for Dark Photon physics studies
Pythia8::EvtGenDecays * evtgenN
Pythia8::Pythia * fPythiaN
don't make it persistent, magic ROOT command
Pythia8::EvtGenDecays * evtgenP
std::shared_ptr< Pythia8::RndmEngine > fRandomEngine
void IncrementCounter(const std::string &name, Long64_t delta=1)
Definition Generator.h:65
constexpr double mm
Definition ShipUnit.h:25
int i
Definition ShipAna.py:108
constexpr double cm
Definition ShipUnit.h:23
constexpr double mm
Definition ShipUnit.h:25
constexpr double PythiaTimeToS(double t)
A Pythia8 time (mm/c) in VMC units (s).
Definition PythiaUnits.h:20
double NuclearCrossSection_mbarn(const TGeoMaterial &material)
double MeanMaterialBudget(std::span< const double, 3 > start, std::span< const double, 3 > end, std::span< double, 10 > mparam)