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 fInputFile = std::unique_ptr<TFile>(TFile::Open(fileName, "read"));
26 LOGF(info, "Info EvtCalcGenerator: Opening input file %s", fileName);
27
28 fTree =
29 std::unique_ptr<TTree>(dynamic_cast<TTree*>(fInputFile->Get("LLP_tree")));
30 fNevents = fTree->GetEntries();
31 fn = startEvent;
32
33 auto* branches = fTree->GetListOfBranches();
34 nBranches = branches->GetEntries();
35 branchVars.resize(nBranches);
36
37 for (int i = 0; i < nBranches; ++i) {
38 auto* branch = dynamic_cast<TBranch*>(branches->At(i));
39 if (fTree->FindBranch(branch->GetName())) {
40 fTree->SetBranchAddress(branch->GetName(), &branchVars[i]);
41 }
42 }
43
44 return kTRUE;
45}
46// ----- Destructor ----------------------------------------------------
48
49// -- Generalized branch access --------------------------------------------
50Double_t EvtCalcGenerator::GetBranchValue(const std::unique_ptr<TTree>& tree,
51 unsigned index) const {
52 if (index < branchVars.size()) {
53 return branchVars[index];
54 } else {
55 throw std::out_of_range("Branch index out of range");
56 }
57}
58// -- Generalized daughter variable access ---------------------------------
59Double_t EvtCalcGenerator::GetDaughterValue(const std::unique_ptr<TTree>& tree,
60 int dauID, int offset) const {
61 int baseIndex = 10 + (dauID * 6);
62 return GetBranchValue(tree, baseIndex + offset);
63}
64
65// -- Wrapper functions ----------------------------------------------------
66// -------------------------------------------------------------------------
68 const std::unique_ptr<TTree>& tree) const {
69 return GetBranchValue(tree, nBranches - 1);
70}
71
72// -- LLP properties ------------------------------------------------------
74 const std::unique_ptr<TTree>& tree) const {
75 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherPx));
76}
78 const std::unique_ptr<TTree>& tree) const {
79 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherPy));
80}
82 const std::unique_ptr<TTree>& tree) const {
83 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherPz));
84}
86 const std::unique_ptr<TTree>& tree) const {
87 return GetBranchValue(tree, static_cast<int>(BranchIndices::MotherE));
88}
89
90// -- Vertex properties ---------------------------------------------------
91Double_t EvtCalcGenerator::GetVx(const std::unique_ptr<TTree>& tree) const {
92 return GetBranchValue(tree, static_cast<int>(BranchIndices::Vx));
93}
94Double_t EvtCalcGenerator::GetVy(const std::unique_ptr<TTree>& tree) const {
95 return GetBranchValue(tree, static_cast<int>(BranchIndices::Vy));
96}
97Double_t EvtCalcGenerator::GetVz(const std::unique_ptr<TTree>& tree) const {
98 return GetBranchValue(tree, static_cast<int>(BranchIndices::Vz));
99}
100
101// -- Decay probability ---------------------------------------------------
103 const std::unique_ptr<TTree>& tree) const {
104 return GetBranchValue(tree, static_cast<int>(BranchIndices::DecayProb));
105}
106
107// -- Daughter properties ------------------------------------------------
108Double_t EvtCalcGenerator::GetDauPx(const std::unique_ptr<TTree>& tree,
109 int dauID) const {
110 return GetDaughterValue(tree, dauID, 0);
111}
112Double_t EvtCalcGenerator::GetDauPy(const std::unique_ptr<TTree>& tree,
113 int dauID) const {
114 return GetDaughterValue(tree, dauID, 1);
115}
116Double_t EvtCalcGenerator::GetDauPz(const std::unique_ptr<TTree>& tree,
117 int dauID) const {
118 return GetDaughterValue(tree, dauID, 2);
119}
120Double_t EvtCalcGenerator::GetDauE(const std::unique_ptr<TTree>& tree,
121 int dauID) const {
122 return GetDaughterValue(tree, dauID, 3);
123}
124Double_t EvtCalcGenerator::GetDauPDG(const std::unique_ptr<TTree>& tree,
125 int dauID) const {
126 return GetDaughterValue(tree, dauID, 5);
127}
128
129// ----- Passing the event -------------------------------------------
130Bool_t EvtCalcGenerator::ReadEvent(FairPrimaryGenerator* cpg) {
131 if (fn == fNevents) {
132 LOG(warning) << "End of input file. Rewind.";
133 fn = 0;
134 }
135
136 fTree->GetEntry(fn);
137 fn++;
138 if (fn % 100 == 0) LOGF(info, "Info EvtCalcGenerator: event nr %d", fn);
139
141 // Vertex coordinates in the SHiP reference frame, expressed in [cm]
142 Double_t space_unit_conv = 100.; // m to cm
143 Double_t coord_shift = (zDecayVolume - ztarget) / space_unit_conv; // units m
144 Double_t vx_transf = GetVx(fTree) * space_unit_conv; // units cm
145 Double_t vy_transf = GetVy(fTree) * space_unit_conv; // units cm
146 Double_t vz_transf =
147 (GetVz(fTree) - coord_shift) * space_unit_conv; // units cm
148
149 Double_t c = 2.99792458e+10; // speed of light [cm/s]
150 Double_t tof = TMath::Sqrt(vx_transf * vx_transf + vy_transf * vy_transf +
151 vz_transf * vz_transf) /
152 c;
153 Double_t decay_prob = GetDecayProb(fTree);
154 Double_t pdg_dau = 0;
155
156 // Mother LLP
157 Bool_t wanttracking = false;
158 Double_t pdg_llp = 999; // Geantino, placeholder
159
160 cpg->AddTrack(pdg_llp, GetMotherPx(fTree), GetMotherPy(fTree),
161 GetMotherPz(fTree), vx_transf, vy_transf, vz_transf, -1.,
162 wanttracking, GetMotherE(fTree), tof, decay_prob);
163
164 wanttracking = true;
165
166 // Secondaries
167 for (int iPart = 0; iPart < Ndau; ++iPart) {
168 pdg_dau = GetDauPDG(fTree, iPart);
169 if (pdg_dau != -999) {
170 cpg->AddTrack(pdg_dau, GetDauPx(fTree, iPart), GetDauPy(fTree, iPart),
171 GetDauPz(fTree, iPart), vx_transf, vy_transf, vz_transf, 0.,
172 wanttracking, GetDauE(fTree, iPart), tof, decay_prob);
173 }
174 }
175
176 return kTRUE;
177}
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