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) Royal Holloway,
3University of London 2001 - 2023.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "BDSOutputROOTEventSamplerC.hh"
20#include "BDSOutputROOTParticleData.hh"
21
22#include "TTree.h"
23
25
26#ifndef __ROOTBUILD__
27#include "BDSHitSamplerCylinder.hh"
28#include "BDSParticleCoordsCylindrical.hh"
29#include "BDSPhysicalConstants.hh"
30#include "BDSPrimaryVertexInformationV.hh"
31
32#include "globals.hh"
33#include "CLHEP/Units/SystemOfUnits.h"
34#include <cmath>
35#endif
36
37BDSOutputROOTParticleData* BDSOutputROOTEventSamplerC::particleTable = nullptr;
38
40
41BDSOutputROOTEventSamplerC::BDSOutputROOTEventSamplerC():
42 samplerName("samplercylinder")
43{
44 Flush();
45}
46
47BDSOutputROOTEventSamplerC::BDSOutputROOTEventSamplerC(const std::string& samplerNameIn):
48 samplerName(samplerNameIn)
49{
50 Flush();
51}
52
53BDSOutputROOTEventSamplerC::~BDSOutputROOTEventSamplerC()
54{;}
55
56#ifndef __ROOTBUILD__
57void BDSOutputROOTEventSamplerC::Fill(const BDSHitSamplerCylinder* hit,
58 G4bool storeMass,
59 G4bool storeCharge,
60 G4bool storeElectrons,
61 G4bool storeRigidity,
62 G4bool storeKineticEnergy)
63{
64 n++;
65
66 totalEnergy.push_back(static_cast<float>(hit->totalEnergy / CLHEP::GeV));
67 z.push_back(static_cast<float>(hit->coords.z / CLHEP::m));
68 phi.push_back(static_cast<float>(hit->coords.phi / CLHEP::radian));
69 rp.push_back(static_cast<float>(hit->coords.rp));
70 zp.push_back(static_cast<float>(hit->coords.zp));
71 phip.push_back(static_cast<float>(hit->coords.phip));
72 p.push_back(static_cast<float>(hit->momentum / CLHEP::GeV));
73 T.push_back(static_cast<float>(hit->coords.T / CLHEP::ns));
74
75 weight.push_back(static_cast<float>(hit->weight));
76 partID.push_back(hit->pdgID);
77 parentID.push_back(hit->parentID);
78 trackID.push_back(hit->trackID);
79 modelID = hit->beamlineIndex;
80 turnNumber.push_back(hit->turnsTaken);
81
82 S = static_cast<float> (hit->S / CLHEP::m);
83
84 if (storeMass)
85 {mass.push_back(static_cast<float>(hit->mass / CLHEP::GeV));}
86
87 if (storeCharge)
88 {charge.push_back(static_cast<int>(hit->charge / (G4double)CLHEP::eplus));}
89
90 if (storeKineticEnergy)
91 {kineticEnergy.push_back(static_cast<float>((hit->totalEnergy - hit->mass) / CLHEP::GeV));}
92
93 if (storeRigidity)
94 {rigidity.push_back(static_cast<float>(hit->rigidity/(CLHEP::tesla*CLHEP::m)));}
95
96 if (storeElectrons)
97 {nElectrons.push_back(static_cast<int>(hit->nElectrons));}
98}
99
100#endif
101
102void BDSOutputROOTEventSamplerC::Fill(const BDSOutputROOTEventSamplerC* other)
103{
104 if (!other)
105 {return;}
106 n = other->n;
107 totalEnergy = other->totalEnergy;
108 z = other->z;
109 phi = other->phi;
110 rp = other->rp;
111 zp = other->zp;
112 phip = other->phip;
113 p = other->p;
114 T = other->T;
115
116 weight = other->weight;
117 partID = other->partID;
118 parentID = other->parentID;
119 trackID = other->trackID;
120 modelID = other->modelID;
121 turnNumber = other->turnNumber;
122 S = other->S;
123
124 charge = other->charge;
126 mass = other->mass;
127 rigidity = other->rigidity;
128
129 isIon = other->isIon;
130 ionA = other->ionA;
131 ionZ = other->ionZ;
132 nElectrons = other->nElectrons;
133}
134
135void BDSOutputROOTEventSamplerC::SetBranchAddress(TTree*)
136{;}
137
139{
140 n = 0;
141 totalEnergy.clear();
142 z.clear();
143 phi.clear();
144 rp.clear();
145 zp.clear();
146 phip.clear();
147 p.clear();
148 T.clear();
149 weight.clear();
150 partID.clear();
151 parentID.clear();
152 trackID.clear();
153 modelID = -1;
154 turnNumber.clear();
155
156 S = 0.0;
157
158 charge.clear();
159 kineticEnergy.clear();
160 mass.clear();
161 rigidity.clear();
162 isIon.clear();
163 ionA.clear();
164 ionZ.clear();
165 nElectrons.clear();
166}
167
169{
170 std::vector<float> result((unsigned long)n);
171 if (!particleTable)
172 {return result;}
173 for (int i = 0; i < n; ++i)
174 {result[i] = static_cast<float>(particleTable->KineticEnergy(partID[i], totalEnergy[i]));}
175 return result;
176}
177
179{
180 std::vector<float> result((unsigned long)n);
181 if (!particleTable)
182 {return result;}
183 for (int i = 0; i < n; ++i)
184 {result[i] = static_cast<float>(particleTable->Mass(partID[i]));}
185 return result;
186}
187
189{
190 std::vector<float> result((unsigned long)n);
191 if (!particleTable)
192 {return result;}
193 for (int i = 0; i < n; ++i)
194 {result[i] = static_cast<float>(particleTable->Rigidity(partID[i], totalEnergy[i]));}
195 return result;
196}
197
199{
200 bool useNElectrons = !nElectrons.empty();
201 std::vector<bool> result((unsigned long)n);
202 if (!particleTable)
203 {return result;}
204 for (int i = 0; i < n; ++i)
205 {
206 result[i] = particleTable->IsIon(partID[i]);
207 if (useNElectrons)
208 {result[i] = result[i] || nElectrons[i] > 0;}
209 }
210 return result;
211}
212
214{
215 std::vector<int> result((unsigned long)n);
216 if (!particleTable)
217 {return result;}
218 for (int i = 0; i < n; ++i)
219 {result[i] = static_cast<int>(particleTable->IonA(partID[i]));}
220 return result;
221}
222
224{
225 std::vector<int> result((unsigned long)n);
226 if (!particleTable)
227 {return result;}
228 for (int i = 0; i < n; ++i)
229 {result[i] = static_cast<int>(particleTable->IonZ(partID[i]));}
230 return result;
231}
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.