ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtTPCFissionGeneratorV3.cxx
Go to the documentation of this file.
2 
3 #include "AtCSVReader.h"
4 #include "AtVertexPropagator.h"
5 
6 #include <FairIon.h>
7 #include <FairLogger.h>
8 #include <FairPrimaryGenerator.h>
9 #include <FairRunSim.h>
10 
11 #include <TDatabasePDG.h>
12 #include <TFile.h>
13 #include <TObject.h> // for TObject
14 #include <TParticlePDG.h>
15 #include <TTree.h>
16 #include <TVirtualMC.h> //For gMC
17 #include <TVirtualMCStack.h>
18 
19 #include <iostream>
20 
21 void AtTPCFissionGeneratorV3::loadIonList(TString ionList)
22 {
23  std::ifstream fileIn(ionList.Data());
24 
25  if (!fileIn.is_open())
26  LOG(fatal) << "Failed to open file: " << ionList;
27 
28  // Loop through each row in the csv and cast each entry to an integer.
29  for (auto &row : CSVRange<int>(fileIn)) {
30  if (row.size() != 2)
31  continue;
32 
33  int A = row[0];
34  int Z = row[1];
35  auto *ion = new FairIon(TString::Format("Ion_%d_%d", Z, A), Z, A, Z);
36  FairRunSim::Instance()->AddNewIon(ion);
37  }
38 }
39 
40 void AtTPCFissionGeneratorV3::loadFissionFragmentTree(TString fissionDistro)
41 {
42 
43  fEventFile = new TFile(fissionDistro, "READ"); // NOLINT (belongs to ROOT)
44 
45  if (fEventFile->IsZombie())
46  LOG(fatal) << "Could not open file with decay fragments: " << fissionDistro;
47 
48  fEventTree = dynamic_cast<TTree *>(fEventFile->Get("fragments"));
49  if (!fEventTree)
50  LOG(fatal) << "Failed to find the tree fragments";
51 
52  fEventTree->SetBranchAddress("decayFragments", &fDecayFrags); // NOLINT
53  fEventTree->SetBranchAddress("A", &fA);
54  fEventTree->SetBranchAddress("Z", &fZ);
55 
56  fNumEvents = fEventTree->GetEntries();
57 }
58 // Default constructor
60 
61 // Generator that takes in a file that specifies the expected distribution of
62 // fission particles.
63 AtTPCFissionGeneratorV3::AtTPCFissionGeneratorV3(const char *name, TString ionList, TString fissionDistro)
64  : fDecayFrags(new std::vector<VecPE>()), fA(new std::vector<Int_t>()), fZ(new std::vector<Int_t>())
65 {
66  loadIonList(ionList);
67  loadFissionFragmentTree(fissionDistro);
68 }
69 
70 // Deep copy constructor
72 
74 
75 Bool_t AtTPCFissionGeneratorV3::ReadEvent(FairPrimaryGenerator *primeGen)
76 {
77  fPrimeGen = primeGen;
78 
79  // If this is a beam-like event don't do anything
80  if (AtVertexPropagator::Instance()->GetDecayEvtCnt() % 2 == 0) {
81  LOG(debug) << "AtTPCFissionGeneratorV3: Skipping beam-like event";
82  } else {
83  LOG(debug) << "AtTPCFissionGeneratorV3: Runing reaction-like event";
84  generateEvent();
85  }
86 
88  return true;
89 }
90 
91 void AtTPCFissionGeneratorV3::generateEvent()
92 {
93  setBeamParameters();
94  fEventTree->GetEntry(fCurrEvent);
95 
96  for (int i = 0; i < fDecayFrags->size(); ++i)
97  generateFragment(fDecayFrags->at(i), fA->at(i), fZ->at(i));
98 
99  LOG(debug) << "Wrote tracks for fission root event: " << fCurrEvent;
100  LOG(debug) << "Wrote tracks for MC event: " << AtVertexPropagator::Instance()->GetDecayEvtCnt();
101  fCurrEvent++;
102 }
103 
104 VecPE AtTPCFissionGeneratorV3::getBeam4Vec()
105 {
106  Double_t fVEn =
108  Double_t fVPx = AtVertexPropagator::Instance()->GetPx() * 1000;
109  Double_t fVPy = AtVertexPropagator::Instance()->GetPy() * 1000;
110  Double_t fVPz = AtVertexPropagator::Instance()->GetPz() * 1000;
111  VecPE beam;
112  beam.SetPxPyPzE(fVPx, fVPy, fVPz, fVEn);
113  return beam;
114 }
115 
116 Cartesian3D AtTPCFissionGeneratorV3::getVertex()
117 {
120 }
121 
122 void AtTPCFissionGeneratorV3::setBeamParameters()
123 {
124  fVertex = getVertex();
125  fBeamBoost = ROOT::Math::Boost(getBeam4Vec().BoostToCM());
126  fBeamBoost.Invert();
127 }
128 
129 void AtTPCFissionGeneratorV3::generateFragment(VecPE &P, Int_t A, Int_t Z)
130 {
131  TString particleName = TString::Format("Ion_%d_%d", Z, A);
132  auto particle = TDatabasePDG::Instance()->GetParticle(particleName);
133  if (!particle)
134  LOG(fatal) << "Couldn't find particle " << particleName << " in database!";
135 
136  auto labP = fBeamBoost(P);
137  auto kinE = labP.E() - labP.mass();
138  auto angle = labP.Theta();
139 
140  std::cout << std::endl;
141  LOG(debug) << TString::Format(
142  "AtTPCFissionGeneratorV3: Generating ion of type %s with CoM momentum (%f, %f, %f) MeV/c", particleName.Data(),
143  P.Px(), P.Py(), P.Pz());
144  LOG(debug) << TString::Format("Lab momentum (%f, %f, %f) MeV/c at (%f, %f, %f) cm with E = %f and angle = %f",
145  labP.Px(), labP.Py(), labP.Pz(), fVertex.X(), fVertex.Y(), fVertex.Z(), kinE, angle);
146 
147  auto nextTrackID = gMC->GetStack()->GetNtrack();
148  AtVertexPropagator::Instance()->SetTrackEnergy(nextTrackID, kinE);
149  AtVertexPropagator::Instance()->SetTrackAngle(nextTrackID, angle);
150 
151  // Requires GeV
152  // NOLINTNEXTLINE
153  fPrimeGen->AddTrack(particle->PdgCode(), labP.Px() / 1000, labP.Py() / 1000, labP.Pz() / 1000, fVertex.X(),
154  fVertex.Y(), fVertex.Z());
155 }
156 
CSVRange
Range class for CSVIterator.
Definition: AtCSVReader.h:102
AtVertexPropagator::GetVx
Double_t GetVx()
Definition: AtVertexPropagator.cxx:196
AtVertexPropagator::GetVz
Double_t GetVz()
Definition: AtVertexPropagator.cxx:204
ClassImp
ClassImp(AtTPCFissionGeneratorV3)
AtVertexPropagator::GetPx
Double_t GetPx()
Definition: AtVertexPropagator.cxx:220
AtTPCFissionGeneratorV3.h
VecPE
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D<> > VecPE
Definition: AtTPCFissionGeneratorV3.h:21
AtVertexPropagator::GetPz
Double_t GetPz()
Definition: AtVertexPropagator.cxx:228
AtVertexPropagator::GetPy
Double_t GetPy()
Definition: AtVertexPropagator.cxx:224
Cartesian3D
ROOT::Math::Cartesian3D<> Cartesian3D
Definition: AtTPCFissionGeneratorV3.h:22
AtVertexPropagator::GetDecayEvtCnt
Int_t GetDecayEvtCnt()
Definition: AtVertexPropagator.cxx:192
AtVertexPropagator::GetVy
Double_t GetVy()
Definition: AtVertexPropagator.cxx:200
AtTPCFissionGeneratorV3::ReadEvent
Bool_t ReadEvent(FairPrimaryGenerator *primGen) override
Definition: AtTPCFissionGeneratorV3.cxx:75
AtTPCFissionGeneratorV3::AtTPCFissionGeneratorV3
AtTPCFissionGeneratorV3()
AtVertexPropagator::GetEnergy
Double_t GetEnergy()
Definition: AtVertexPropagator.cxx:232
AtVertexPropagator.h
AtTPCFissionGeneratorV3::~AtTPCFissionGeneratorV3
virtual ~AtTPCFissionGeneratorV3()
AtVertexPropagator::SetTrackEnergy
void SetTrackEnergy(int trackID, double energy)
Definition: AtVertexPropagator.cxx:92
AtVertexPropagator::GetBeamMass
Double_t GetBeamMass()
Definition: AtVertexPropagator.cxx:236
AtVertexPropagator::SetTrackAngle
void SetTrackAngle(int trackID, double angle)
Definition: AtVertexPropagator.cxx:96
AtTPCFissionGeneratorV3
Definition: AtTPCFissionGeneratorV3.h:24
AtVertexPropagator::Instance
static AtVertexPropagator * Instance()
Definition: AtVertexPropagator.cxx:18
AtCSVReader.h
AtVertexPropagator::IncDecayEvtCnt
void IncDecayEvtCnt()
Definition: AtVertexPropagator.cxx:321