ATLAS Offline Software
Loading...
Searching...
No Matches
TestShowerLib.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5
6// this header file
8
9// CLHEP incldues
12
13//#include <algorithm>
14//#include <functional>
15#include <sstream>
16#include <fstream>
17
18#include <iostream>
19
20// local includes
21//#include "LArG4ShowerLib/PositionBin.h"
22
23// G4 includes
24#include "G4Track.hh"
25
28
29#include "TTree.h"
30#include "TFile.h"
31#include "TParameter.h"
32
33#define LIB_VERSION 10
34
35namespace ShowerLib {
36
40
42 {
43 TParameter<int>* ver;
44 ver = (TParameter<int>*)source->Get("version");
45
46 if ((ver == nullptr) || (ver->GetVal() != LIB_VERSION)) return nullptr; //Test library header = 10
47
48 TTree* TTreeMeta = (TTree*)source->Get("meta");
49 TTree* TTreeLib = (TTree*)source->Get("library");
50
51 if ((TTreeMeta == nullptr) || (TTreeLib == nullptr)) return nullptr;
52
53 std::cout << "TestShowerLib header found." << std::endl;
54
55 TestShowerLib* newlib = new TestShowerLib();
56
57 if (!(newlib->readMeta(TTreeMeta)) || !(newlib->read(TTreeLib))) {
58 delete newlib;
59 std::cout << "TestShowerLib read unsuccessful." << std::endl;
60 return nullptr;
61 }
62
63 return newlib;
64
65 }
66
67 IShowerLib* TestShowerLib::createEmptyLib(const std::string& inputFile)
68 {
69 /*
70 * Test library Structure format:
71 *
72 * VER PART DET
73 * COMMENT
74 *
75 * where
76 *
77 * VER == 10
78 */
79 std::ifstream filestr(inputFile.c_str(),std::ios::in);
80
81 if (!filestr.is_open()) {
82 std::cout << "TestShowerLib " << inputFile << ": bad file!" << std::endl;
83 return nullptr;
84 }
85
86 std::string instr;
87 std::getline(filestr,instr);
88 std::stringstream ss(instr);
89
90 int ver;
91
92 ss >> ver;
93
94 if (ver != LIB_VERSION) {
95 return nullptr;
96 }
97
98
99 int part;
100 std::string det;
101
102 ss >> part >> det;
103
104
105 TestShowerLib* newlib = new TestShowerLib();
106
107 newlib->m_detector = std::move(det);
108 newlib->m_particle = part;
109 newlib->m_filled = false;
110
111 std::getline(filestr,instr);
112 newlib->m_comment = std::move(instr);
113
114 return newlib;
115 }
116
117
118 std::vector<EnergySpot>* TestShowerLib::getShower(const G4Track* , ShowerLibStatistics* , int ) const
119 {
120 if (!m_filled) {
121 std::cout << "Library is not created for production use" << std::endl;
122 return nullptr;
123 }
124
125 std::cout << "Library is only for testing, not for production use" << std::endl;
126 return nullptr;
127 }
128
129 double TestShowerLib::getContainmentZ(const G4Track* ) const
130 {
131 if (!m_filled) {
132 std::cout << "Library is not created for production use" << std::endl;
133 return 0.0;
134 }
135
136 std::cout << "Library is only for testing, not for production use" << std::endl;
137 return 0.0;
138 }
139
140 double TestShowerLib::getContainmentR(const G4Track* ) const
141 {
142 if (!m_filled) {
143 std::cout << "Library is not created for production use" << std::endl;
144 return 0.0;
145 }
146
147 std::cout << "Library is only for testing, not for production use" << std::endl;
148 return 0.0;
149 }
150
152 {
153 if (m_filled) {
154 std::cout << "ERROR: filled" << std::endl;
155 return false;
156 }
157
158 genInfo theinfo{};
159 theinfo.vertex = std::make_unique<HepMC::FourVector>(genParticle->production_vertex()->position());
160 theinfo.momentum = std::make_unique<HepMC::FourVector>(genParticle->momentum());
161
162 m_libData.emplace_back(std::move(theinfo), *shower);
163
164 return true;
165 }
166
168 {
169 if (m_libData.empty()) return false;
170 TParameter<int> ver("version",LIB_VERSION);
171
172 dest->WriteObject(&ver,"version");
173
174 TTree TTreeMeta;
175 TTree TTreeLib;
176
177 write(&TTreeLib);
178 writeMeta(&TTreeMeta);
179
180 dest->WriteObject(&TTreeLib,"library");
181 dest->WriteObject(&TTreeMeta,"meta");
182
183 return true;
184 }
185
186
187 bool TestShowerLib::read(TTree* source)
188 {
189 /*
190 * Eta Energy library format:
191 * | x | y | z | e | time | - name of branch in TTree
192 * ------------------------------------------------------------------
193 * | vertex | vertex | vertex | num of | cont | - shower header
194 * | X | Y | Z | hits | Z |
195 * ------------------------------------------------------------------
196 * | momentum | momentum | momentum | truth | cont | - shower header
197 * | X | Y | Z | energy | R |
198 * ------------------------------------------------------------------
199 * |x-coord of hit|y-coord of hit|z-coord of hit|dep.energy|hit time| - hit
200 */
201 int nentr = source->GetEntriesFast();
202 if (nentr < 3) return false;
203 Float_t x,y,z,e,time;
204 source->SetBranchAddress("x",&x);
205 source->SetBranchAddress("y",&y);
206 source->SetBranchAddress("z",&z);
207 source->SetBranchAddress("e",&e);
208 source->SetBranchAddress("time",&time);
209 int entr = 0;
210
211 do {
212 //read eta bin header
213 source->GetEntry(entr++);
214 int nhits = (int)(e+0.1); // +0.1 just in case - c++ has low round
215 Shower shower;
216 shower.setZSize(time);
217 genInfo theinfo{};
218 theinfo.vertex = std::make_unique<HepMC::FourVector>(x,y,z,0);
219 source->GetEntry(entr++);
220 shower.setRSize(time);
221 theinfo.momentum = std::make_unique<HepMC::FourVector>(x,y,z,e);
222 for(int i = 0; i < nhits; i++) {
223 source->GetEntry(entr++); //variables mean what the name suggests
224 shower.push_back(new ShowerEnergySpot(G4ThreeVector(x,y,z),e,time));
225 }
226 m_libData.emplace_back(std::move(theinfo),shower);
227 } while (entr < nentr);
228
229 m_filled = true;
230 return true;
231 }
232
233 bool TestShowerLib::write(TTree* dest) const
234 {
235 /*
236 * Eta Energy library format:
237 * | x | y | z | e | time | - name of branch in TTree
238 * ------------------------------------------------------------------
239 * | vertex | vertex | vertex | num of | cont | - shower header
240 * | X | Y | Z | hits | Z |
241 * ------------------------------------------------------------------
242 * | momentum | momentum | momentum | truth | cont | - shower header
243 * | X | Y | Z | energy | R |
244 * ------------------------------------------------------------------
245 * |x-coord of hit|y-coord of hit|z-coord of hit|dep.energy|hit time| - hit
246 */
247 Float_t x,y,z,e,time;
248 dest->Branch("x",&x);
249 dest->Branch("y",&y);
250 dest->Branch("z",&z);
251 dest->Branch("e",&e);
252 dest->Branch("time",&time);
253 for (const storedShower& lib : m_libData) {
254 HepMC::FourVector vertex = *lib.first.vertex;
255 HepMC::FourVector momentum = *lib.first.momentum;
256 x = vertex.x();
257 y = vertex.y();
258 z = vertex.z();
259 e = lib.second.size();
260 time = lib.second.getZSize();
261 dest->Fill(); //eta bin header
262 x = momentum.px();
263 y = momentum.py();
264 z = momentum.pz();
265 e = momentum.e();
266 time = lib.second.getRSize();
267 dest->Fill(); //eta bin header
268 for (const ShowerEnergySpot* spot : lib.second) {
269 x = spot->GetPosition().x();
270 y = spot->GetPosition().y();
271 z = spot->GetPosition().z();
272 e = spot->GetEnergy();
273 time = spot->GetTime();
274 dest->Fill();
275 }
276 }
277 //dest->Write();
278 return true;
279 }
280
282 {
283 return nullptr;
284 }
285
286} // namespace ShowerLib
#define LIB_VERSION
static Double_t ss
#define y
#define x
#define z
bool readMeta(TTree *source)
read metadata from the given TTree
IShowerLib()
default constructor
Definition IShowerLib.h:98
int m_particle
ID of the generated particles.
Definition IShowerLib.h:107
bool writeMeta(TTree *dest) const
write metadata to the given TTree
std::string m_detector
name of the detector
Definition IShowerLib.h:106
std::string m_comment
comment
Definition IShowerLib.h:112
bool m_filled
is the library read from ROOT or from structure file
Definition IShowerLib.h:114
G4ThreeVector GetPosition() const
Class for shower library shower.
Definition Shower.h:36
void setZSize(const float zsize)
Definition Shower.h:64
void setRSize(const float rsize)
Definition Shower.h:65
virtual std::vector< EnergySpot > * getShower(const G4Track *track, ShowerLibStatistics *stats, int randomShift) const
get shower for given G4 track
virtual bool storeShower(HepMC::ConstGenParticlePtr genParticle, const Shower *shower)
store shower in the library
std::pair< genInfo, Shower > storedShower
static IShowerLib * createEmptyLib(const std::string &inputFile)
factory method. create empty library with the given structure. returns NULL if file is invalid.
virtual bool writeToROOT(TFile *dest)
write library to ROOT file
virtual double getContainmentR(const G4Track *track) const
get average lateral spread of the showers for the given energy
virtual double getContainmentZ(const G4Track *track) const
get average length of showers for the given energy
bool write(TTree *dest) const
write library to given TTree
static IShowerLib * readFromROOTFile(TFile *source)
factory method. create a library from root file. returns NULL if file is invalid.
virtual ~TestShowerLib()
default destructor
bool read(TTree *source)
read library from given TTree
virtual ShowerLibStatistics * createStatistics() const
const GenParticle * ConstGenParticlePtr
Definition GenParticle.h:38
Namespace for the ShowerLib related classes.
Definition StepInfo.h:17
std::unique_ptr< HepMC::FourVector > momentum
std::unique_ptr< HepMC::FourVector > vertex