BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
BDSOutputROOTEventSamplerC.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "BDSOutputROOTEventSamplerC.hh"
19#include "BDSOutputROOTParticleData.hh"
20
21#include "TTree.h"
22
24
25#ifndef __ROOTBUILD__
26#include "BDSHitSamplerCylinder.hh"
27#include "BDSParticleCoordsCylindrical.hh"
28#include "BDSPhysicalConstants.hh"
29#include "BDSPrimaryVertexInformationV.hh"
30
31#include "globals.hh"
32#include "CLHEP/Units/SystemOfUnits.h"
33#include <cmath>
34#endif
35
36BDSOutputROOTParticleData* BDSOutputROOTEventSamplerC::particleTable = nullptr;
37
39
40BDSOutputROOTEventSamplerC::BDSOutputROOTEventSamplerC():
41 samplerName("samplercylinder")
42{
43 Flush();
44}
45
46BDSOutputROOTEventSamplerC::BDSOutputROOTEventSamplerC(const std::string& samplerNameIn):
47 samplerName(samplerNameIn)
48{
49 Flush();
50}
51
52BDSOutputROOTEventSamplerC::~BDSOutputROOTEventSamplerC()
53{;}
54
55#ifndef __ROOTBUILD__
56void BDSOutputROOTEventSamplerC::Fill(const BDSHitSamplerCylinder* hit,
57 G4bool storeMass,
58 G4bool storeCharge,
59 G4bool storeElectrons,
60 G4bool storeRigidity,
61 G4bool storeKineticEnergy)
62{
63 n++;
64
65 totalEnergy.push_back(static_cast<float>(hit->totalEnergy / CLHEP::GeV));
66 z.push_back(static_cast<float>(hit->coords.z / CLHEP::m));
67 phi.push_back(static_cast<float>(hit->coords.phi / CLHEP::radian));
68 rp.push_back(static_cast<float>(hit->coords.rp));
69 zp.push_back(static_cast<float>(hit->coords.zp));
70 phip.push_back(static_cast<float>(hit->coords.phip));
71 p.push_back(static_cast<float>(hit->momentum / CLHEP::GeV));
72 T.push_back(static_cast<float>(hit->coords.T / CLHEP::ns));
73
74 weight.push_back(static_cast<float>(hit->weight));
75 partID.push_back(hit->pdgID);
76 parentID.push_back(hit->parentID);
77 trackID.push_back(hit->trackID);
78 modelID = hit->beamlineIndex;
79 turnNumber.push_back(hit->turnsTaken);
80
81 S = static_cast<float> (hit->S / CLHEP::m);
82
83 if (storeMass)
84 {mass.push_back(static_cast<float>(hit->mass / CLHEP::GeV));}
85
86 if (storeCharge)
87 {charge.push_back(static_cast<int>(hit->charge / (G4double)CLHEP::eplus));}
88
89 if (storeKineticEnergy)
90 {kineticEnergy.push_back(static_cast<float>((hit->totalEnergy - hit->mass) / CLHEP::GeV));}
91
92 if (storeRigidity)
93 {rigidity.push_back(static_cast<float>(hit->rigidity/(CLHEP::tesla*CLHEP::m)));}
94
95 if (storeElectrons)
96 {nElectrons.push_back(static_cast<int>(hit->nElectrons));}
97}
98
99#endif
100
101void BDSOutputROOTEventSamplerC::Fill(const BDSOutputROOTEventSamplerC* other)
102{
103 if (!other)
104 {return;}
105 n = other->n;
106 totalEnergy = other->totalEnergy;
107 z = other->z;
108 phi = other->phi;
109 rp = other->rp;
110 zp = other->zp;
111 phip = other->phip;
112 p = other->p;
113 T = other->T;
114
115 weight = other->weight;
116 partID = other->partID;
117 parentID = other->parentID;
118 trackID = other->trackID;
119 modelID = other->modelID;
120 turnNumber = other->turnNumber;
121 S = other->S;
122
123 charge = other->charge;
125 mass = other->mass;
126 rigidity = other->rigidity;
127
128 isIon = other->isIon;
129 ionA = other->ionA;
130 ionZ = other->ionZ;
131 nElectrons = other->nElectrons;
132}
133
134void BDSOutputROOTEventSamplerC::SetBranchAddress(TTree*)
135{;}
136
138{
139 n = 0;
140 totalEnergy.clear();
141 z.clear();
142 phi.clear();
143 rp.clear();
144 zp.clear();
145 phip.clear();
146 p.clear();
147 T.clear();
148 weight.clear();
149 partID.clear();
150 parentID.clear();
151 trackID.clear();
152 modelID = -1;
153 turnNumber.clear();
154
155 S = 0.0;
156
157 charge.clear();
158 kineticEnergy.clear();
159 mass.clear();
160 rigidity.clear();
161 isIon.clear();
162 ionA.clear();
163 ionZ.clear();
164 nElectrons.clear();
165}
166
168{
169 std::vector<float> result((unsigned long)n);
170 if (!particleTable)
171 {return result;}
172 for (int i = 0; i < n; ++i)
173 {result[i] = static_cast<float>(particleTable->KineticEnergy(partID[i], totalEnergy[i]));}
174 return result;
175}
176
178{
179 std::vector<float> result((unsigned long)n);
180 if (!particleTable)
181 {return result;}
182 for (int i = 0; i < n; ++i)
183 {result[i] = static_cast<float>(particleTable->Mass(partID[i]));}
184 return result;
185}
186
188{
189 std::vector<float> result((unsigned long)n);
190 if (!particleTable)
191 {return result;}
192 for (int i = 0; i < n; ++i)
193 {result[i] = static_cast<float>(particleTable->Rigidity(partID[i], totalEnergy[i]));}
194 return result;
195}
196
198{
199 bool useNElectrons = !nElectrons.empty();
200 std::vector<bool> result((unsigned long)n);
201 if (!particleTable)
202 {return result;}
203 for (int i = 0; i < n; ++i)
204 {
205 result[i] = particleTable->IsIon(partID[i]);
206 if (useNElectrons)
207 {result[i] = result[i] || nElectrons[i] > 0;}
208 }
209 return result;
210}
211
213{
214 std::vector<int> result((unsigned long)n);
215 if (!particleTable)
216 {return result;}
217 for (int i = 0; i < n; ++i)
218 {result[i] = static_cast<int>(particleTable->IonA(partID[i]));}
219 return result;
220}
221
223{
224 std::vector<int> result((unsigned long)n);
225 if (!particleTable)
226 {return result;}
227 for (int i = 0; i < n; ++i)
228 {result[i] = static_cast<int>(particleTable->IonZ(partID[i]));}
229 return result;
230}
The information recorded from a particle impacting a sampler.
G4int nElectrons
Can only get this at inspection time so include here.
G4double charge
Double as g4 uses charge as a double.
Information stored per cylindrical sampler per event.
std::vector< int > getIonA()
Function to calculate on the fly the parameters.
std::vector< int > ionZ
These are not filled by default.
std::vector< bool > isIon
These are not filled by default.
std::vector< float > rigidity
These are not filled by default.
std::vector< bool > getIsIon()
Function to calculate on the fly the parameters.
std::vector< float > mass
These are not filled by default.
std::vector< float > getMass()
Function to calculate on the fly the parameters.
std::vector< float > getRigidity()
Function to calculate on the fly the parameters.
std::vector< int > getIonZ()
Function to calculate on the fly the parameters.
std::vector< float > kineticEnergy
These are not filled by default.
std::vector< int > charge
These are not filled by default.
std::vector< float > getKineticEnergy()
Function to calculate on the fly the parameters.
std::vector< int > ionA
These are not filled by default.
virtual void Flush()
Clean Sampler.
std::vector< int > nElectrons
These are not filled by default.
Geant4 particle data for particles used in simulation.
bool IsIon(const int &pdgID) const
This doesn't count a proton (even with electrons) as an ion.
double KineticEnergy(const int &pdgID, const double &totalEnergy) const
Calculate kinetic energy of particle given PDG ID and total energy in GeV.
virtual void Flush()
Clear maps.
double Rigidity(const int &pdgID, const double &totalEnergy) const
Calculate rigidity of particle given PDG ID and total energy in GeV.