FairShip
Loading...
Searching...
No Matches
EvtCalcGenerator.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 "EvtCalcGenerator.h"
6
7#include <cmath>
8
9#include "FairLogger.h"
10#include "FairPrimaryGenerator.h"
11#include "ShipUnit.h"
12#include "TDatabasePDG.h"
13#include "TFile.h"
14#include "TMath.h"
15
16// ----- Default constructor -------------------------------------------
18// -------------------------------------------------------------------------
19// ----- Default constructor -------------------------------------------
20Bool_t EvtCalcGenerator::Init(const char* fileName) {
21 return Init(fileName, 0);
22}
23// ----- Default constructor -------------------------------------------
24Bool_t EvtCalcGenerator::Init(const char* fileName, const int startEvent) {
25 if (startEvent < 0) {
26 LOG(error) << "EvtCalcGenerator: startEvent must be >= 0, got "
27 << startEvent;
28 return kFALSE;
29 }
30 fInputFile = std::unique_ptr<TFile>(TFile::Open(fileName, "read"));
31 LOGF(info, "Info EvtCalcGenerator: Opening input file %s", fileName);
32 if (!fInputFile) {
33 LOG(error) << "EvtCalcGenerator: error opening input file " << fileName;
34 fInputFile.reset();
35 return kFALSE;
36 }
37
38 fTree =
39 std::unique_ptr<TTree>(dynamic_cast<TTree*>(fInputFile->Get("LLP_tree")));
40 if (!fTree) {
41 LOG(error) << "EvtCalcGenerator: cannot find tree LLP_tree in file "
42 << fileName;
43 fInputFile.reset();
44 return kFALSE;
45 }
46 fNevents = fTree->GetEntries();
47 if (startEvent >= fNevents) {
48 LOG(error) << "EvtCalcGenerator: startEvent " << startEvent
49 << " is out of range for " << fNevents << " entries";
50 fTree.reset();
51 fInputFile.reset();
52 return kFALSE;
53 }
54 fn = startEvent;
55
56 auto* branches = fTree->GetListOfBranches();
57 if (!branches) {
58 LOG(error) << "EvtCalcGenerator: failed to access tree branches";
59 fTree.reset();
60 fInputFile.reset();
61 return kFALSE;
62 }
63 nBranches = branches->GetEntries();
64 if (nBranches <= 0) {
65 LOG(error) << "EvtCalcGenerator: tree LLP_tree has no branches";
66 fTree.reset();
67 fInputFile.reset();
68 return kFALSE;
69 }
70 branchVars.resize(nBranches);
71
72 for (int i = 0; i < nBranches; ++i) {
73 auto* branch = dynamic_cast<TBranch*>(branches->At(i));
74 if (!branch) {
75 LOG(error) << "EvtCalcGenerator: encountered an invalid branch entry";
76 fTree.reset();
77 fInputFile.reset();
78 return kFALSE;
79 }
80 if (fTree->FindBranch(branch->GetName())) {
81 if (fTree->SetBranchAddress(branch->GetName(), &branchVars[i]) < 0) {
82 LOG(error) << "EvtCalcGenerator: failed to bind branch "
83 << branch->GetName();
84 fTree.reset();
85 fInputFile.reset();
86 return kFALSE;
87 }
88 }
89 }
90
91 return kTRUE;
92}
93// ----- Destructor ----------------------------------------------------
95
96// -- Generalized branch access --------------------------------------------
97Double_t EvtCalcGenerator::GetBranchValue(const std::unique_ptr<TTree>& tree,
98 unsigned index) const {
99 if (index < branchVars.size()) {
100 return branchVars[index];
101 } else {
102 throw std::out_of_range("Branch index out of range");
103 }
104}
105// -- Generalized daughter variable access ---------------------------------
106Double_t EvtCalcGenerator::GetDaughterValue(const std::unique_ptr<TTree>& tree,
107 int dauID, int offset) const {
108 int baseIndex = 10 + (dauID * 6);
109 return GetBranchValue(tree, baseIndex + offset);
110}
111
112// -- Wrapper functions ----------------------------------------------------
113// -------------------------------------------------------------------------
115 const std::unique_ptr<TTree>& tree) const {
116 return GetBranchValue(tree, nBranches - 1);
117}
118
119// -- LLP properties ------------------------------------------------------
121 const std::unique_ptr<TTree>& tree) const {
122 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherPx));
123}
125 const std::unique_ptr<TTree>& tree) const {
126 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherPy));
127}
129 const std::unique_ptr<TTree>& tree) const {
130 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherPz));
131}
133 const std::unique_ptr<TTree>& tree) const {
134 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherE));
135}
136
137// -- Vertex properties ---------------------------------------------------
138Double_t EvtCalcGenerator::GetVx(const std::unique_ptr<TTree>& tree) const {
139 return GetBranchValue(tree, static_cast<int>(BranchIndices::Vx));
140}
141Double_t EvtCalcGenerator::GetVy(const std::unique_ptr<TTree>& tree) const {
142 return GetBranchValue(tree, static_cast<int>(BranchIndices::Vy));
143}
144Double_t EvtCalcGenerator::GetVz(const std::unique_ptr<TTree>& tree) const {
145 return GetBranchValue(tree, static_cast<int>(BranchIndices::Vz));
146}
147
148// -- Decay probability ---------------------------------------------------
150 const std::unique_ptr<TTree>& tree) const {
151 return GetBranchValue(tree, static_cast<int>(BranchIndices::DecayProb));
152}
153
154// -- Daughter properties ------------------------------------------------
155Double_t EvtCalcGenerator::GetDauPx(const std::unique_ptr<TTree>& tree,
156 int dauID) const {
157 return GetDaughterValue(tree, dauID, 0);
158}
159Double_t EvtCalcGenerator::GetDauPy(const std::unique_ptr<TTree>& tree,
160 int dauID) const {
161 return GetDaughterValue(tree, dauID, 1);
162}
163Double_t EvtCalcGenerator::GetDauPz(const std::unique_ptr<TTree>& tree,
164 int dauID) const {
165 return GetDaughterValue(tree, dauID, 2);
166}
167Double_t EvtCalcGenerator::GetDauE(const std::unique_ptr<TTree>& tree,
168 int dauID) const {
169 return GetDaughterValue(tree, dauID, 3);
170}
171Double_t EvtCalcGenerator::GetDauPDG(const std::unique_ptr<TTree>& tree,
172 int dauID) const {
173 return GetDaughterValue(tree, dauID, 5);
174}
175
176// ----- Passing the event -------------------------------------------
177Bool_t EvtCalcGenerator::ReadEvent(FairPrimaryGenerator* cpg) {
178 if (fn == fNevents) {
179 LOG(warning) << "End of input file. Rewind.";
180 fn = 0;
181 }
182
183 fTree->GetEntry(fn);
184 fn++;
185 if (fn % 100 == 0) LOGF(info, "Info EvtCalcGenerator: event nr %d", fn);
186
188 // Vertex coordinates in the SHiP reference frame, expressed in [cm]
189 Double_t space_unit_conv = 100.; // m to cm
190 Double_t vx_transf = GetVx(fTree) * space_unit_conv; // units cm
191 Double_t vy_transf = GetVy(fTree) * space_unit_conv; // units cm
192 Double_t vz_transf = GetVz(fTree) * space_unit_conv + ztarget; // units cm
193
194 Double_t tof = TMath::Sqrt(vx_transf * vx_transf + vy_transf * vy_transf +
195 vz_transf * vz_transf) /
197 Double_t decay_prob = GetDecayProb(fTree);
198 Double_t pdg_dau = 0;
199
200 // Mother LLP
201 Bool_t wanttracking = false;
202 Double_t pdg_llp = 999; // Geantino, placeholder
203
204 cpg->AddTrack(pdg_llp, GetMotherPx(fTree), GetMotherPy(fTree),
205 GetMotherPz(fTree), vx_transf, vy_transf, vz_transf, -1.,
206 wanttracking, GetMotherE(fTree), tof, decay_prob);
207
208 wanttracking = true;
209
210 // Secondaries
211 for (int iPart = 0; iPart < Ndau; ++iPart) {
212 pdg_dau = GetDauPDG(fTree, iPart);
213 if (pdg_dau != -999) {
214 cpg->AddTrack(pdg_dau, GetDauPx(fTree, iPart), GetDauPy(fTree, iPart),
215 GetDauPz(fTree, iPart), vx_transf, vy_transf, vz_transf, 0.,
216 wanttracking, GetDauE(fTree, iPart), tof, decay_prob);
217 }
218 }
219
220 return kTRUE;
221}
std::unique_ptr< TFile > fInputFile
Bool_t Init(const char *, int) override
Double_t GetDauPDG(const std::unique_ptr< TTree > &, int) const
Double_t GetNdaughters(const std::unique_ptr< TTree > &) const
Double_t GetDauE(const std::unique_ptr< TTree > &, int) const
Double_t GetMotherE(const std::unique_ptr< TTree > &) const
Double_t GetBranchValue(const std::unique_ptr< TTree > &, unsigned) const
Double_t GetDauPx(const std::unique_ptr< TTree > &, int) const
std::unique_ptr< TTree > fTree
Double_t GetDecayProb(const std::unique_ptr< TTree > &) const
Bool_t ReadEvent(FairPrimaryGenerator *) override
std::vector< double > branchVars
Double_t GetDauPz(const std::unique_ptr< TTree > &, int) const
Double_t GetMotherPy(const std::unique_ptr< TTree > &) const
Double_t GetMotherPz(const std::unique_ptr< TTree > &) const
Double_t GetMotherPx(const std::unique_ptr< TTree > &) const
Double_t GetVz(const std::unique_ptr< TTree > &) const
Double_t GetDauPy(const std::unique_ptr< TTree > &, int) const
Double_t GetVx(const std::unique_ptr< TTree > &) const
Double_t GetVy(const std::unique_ptr< TTree > &) const
~EvtCalcGenerator() override
Double_t GetDaughterValue(const std::unique_ptr< TTree > &tree, int, int) const
constexpr double c_light
Definition ShipUnit.h:44
constexpr double s
Definition ShipUnit.h:31