ATLAS Offline Software
Loading...
Searching...
No Matches
EtaEnergyShowerLib.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
11
12#include <sstream>
13#include <fstream>
14
15#include <iostream>
16#include <iomanip>
17
18// G4 includes
19#include "G4Track.hh"
20
23
24#include "TTree.h"
25#include "TFile.h"
26#include "TParameter.h"
27
28#define LIB_VERSION 1
29
30namespace ShowerLib {
31
32 inline int calcKey(float eta) {
33 return (int)(eta*1000);
34 }
35
37 {
38 TParameter<int>* ver;
39 ver = (TParameter<int>*)source->Get("version");
40
41 if ((ver == nullptr) || (ver->GetVal() != LIB_VERSION)) return nullptr; //Eta Energy library header = 1
42
43 TTree* TTreeMeta = (TTree*)source->Get("meta");
44 TTree* TTreeLib = (TTree*)source->Get("library");
45
46 if ((TTreeMeta == nullptr) || (TTreeLib == nullptr)) return nullptr;
47
48 std::cout << "EtaEnergyShowerLib header found." << std::endl;
49
51
52 if (!(newlib->readMeta(TTreeMeta)) || !(newlib->read(TTreeLib))) {
53 delete newlib;
54 std::cout << "EtaEnergyShowerLib read unsuccessful." << std::endl;
55 return nullptr;
56 }
57
58 return newlib;
59
60 }
61
62 IShowerLib* EtaEnergyShowerLib::createEmptyLib(const std::string& inputFile)
63 {
64 /*
65 * Eta Energy Structure format:
66 *
67 * VER PART DET ETA1 ETA2 ...
68 * COMMENT
69 *
70 * where
71 *
72 * VER == 1
73 * ETA(N+1) > ETA(N)
74 * First ETA is the beginning of the lib
75 * Last ETA is the end of the lib
76 */
77 std::ifstream filestr(inputFile.c_str(),std::ios::in);
78
79
80 if (!filestr.is_open()) {
81 std::cout << "EtaEnergyShowerLib " << inputFile << ": bad file!" << std::endl;
82 return nullptr;
83 }
84
85 std::string instr;
86 std::getline(filestr,instr);
87 std::stringstream ss(instr);
88
89 int ver;
90
91 ss >> ver;
92
93 if (ver != LIB_VERSION) {
94 return nullptr;
95 }
96
97
98 int part;
99 std::string det;
100
101 ss >> part >> det;
102
103 std::vector<float> etalist;
104
105 float eta;
106 float etaold = -9999999.9; //hopefully will be enough
107
108 ss >> eta;
109
110 if (ss.fail()) {
111 std::cout << "file reading failed! (not enough data)" << std::endl;
112 return nullptr;
113 }
114
115 float mineta = eta;
116 float maxeta = eta;
117
118 while (!ss.eof()) {
119 if ((ss.fail()) || (eta <= etaold)) {
120 std::cout << "screwed etas! (" << eta << "<=" << etaold << ")" << std::endl;
121 return nullptr;
122 }
123 etalist.push_back(eta);
124 etaold = eta;
125 ss >> eta;
126 maxeta = eta;
127 }
128
130 if (!newlib->readStructure(etalist)) {
131 std::cout << "this structure is not valid" << std::endl;
132 delete newlib;
133 return nullptr;
134 }
135
136 newlib->m_detector = std::move(det);
137 newlib->m_particle = part;
138 newlib->m_mineta = mineta;
139 newlib->m_maxeta = maxeta;
140
141 newlib->m_filled = false;
142 if (mineta < 0) newlib->m_onlyPositive = false;
143 else newlib->m_onlyPositive = true;
144
145 std::getline(filestr,instr);
146 newlib->m_comment = std::move(instr);
147
148 return newlib;
149 }
150
151 std::vector<EnergySpot>* EtaEnergyShowerLib::getShower(const G4Track* track, ShowerLibStatistics* stats, int randomShift) const
152 {
153 if (!checkEtaAndStuff(track)) return nullptr;
154
155 double eta = track->GetPosition().eta();
156
157 if ((m_onlyPositive) && (eta < 0.0)) eta = -eta;
158
159 library::const_iterator libit = m_libData.upper_bound(eta);
160
161 if (libit == m_libData.begin()) {
162 //this is really weird
163 std::cout << "Something is wrong with eta: mineta=" << m_mineta << " eta=" << eta << std::endl;
164 } else {
165 --libit;
166 }
167
168 //std::cout << "Found eta bin" << std::endl;
169
170 if ((*libit).second.empty()) {
171 std::cout << "The etabin corresponding to the eta is empty" << std::endl;
172 return nullptr;
173 }
174 double trenergy = track->GetKineticEnergy();
175 //std::cout << "Energy: " << trenergy << std::endl;
176 etabin::const_iterator etait = (*libit).second.lower_bound(trenergy);
177 if (etait == (*libit).second.end()) --etait; //if it points to the end, repoint it to the last shower in bin
178 else if (etait != (*libit).second.begin()) { //find the closest energy. it's either found item or previous (if there is a previous)
179 etabin::const_iterator etaitch = etait;
180 --etaitch;
181 if (((*etait).first - trenergy) > (trenergy - (*etaitch).first)) { // if the closest is the previous
182 --etait;
183 }
184 }
185 //std::cout << "Found closest energy:" << (*etait).first << std::endl;
186 if (randomShift > 0) {
187 double upperEnergy = (*etait).first * 1.01; //we allow 1% off
188 for (int i = 0; i < randomShift; i++) {
189 ++etait;
190 if (etait == (*libit).second.end()) {
191 --etait; // oops! we reached the end of the bin, going back
192 break;
193 }
194 if ((*etait).first > upperEnergy) break; //energy diff too high
195 }
196 }
197 if ((randomShift < 0)&&(etait != (*libit).second.begin())) {
198 double lowerEnergy = (*etait).first * 0.99; //we allow 1% off
199 for (int i = 0; i > randomShift; i--) {
200 --etait;
201 if (etait == (*libit).second.begin()) { // oops! we reached the beginning of the bin
202 //etait++; iterator.begin() is a perfectly normal shower, in contrary to the iterator.end(), which is not
203 break;
204 }
205 if (lowerEnergy > (*etait).first) break; //energy diff too high
206 }
207 }
208 //std::cout << "Found out energy" << std::endl;
209 //std::cout << "Shower with num hits:" << (*etait).second.size() << std::endl;
210 std::vector<EnergySpot>* outshower = new std::vector<EnergySpot>();//((*etait).second);
211 Shower::const_iterator iter;
212 //std::cout << "Created out shower" << std::endl;
213
214 float energyScale = track->GetKineticEnergy() / (*etait).first;
215 //std::cout << "Scale: " << energyScale << std::endl;
216
217 for (iter = (*etait).second.begin() /*outshower->begin()*/; iter != (*etait).second.end() /*outshower->end()*/; ++iter) {
218 EnergySpot tmp( (*iter)->GetPosition(), (*iter)->GetEnergy(), (*iter)->GetTime() );
219 tmp.SetEnergy(tmp.GetEnergy() * energyScale);
220 outshower->push_back(tmp);
221 //(*iter).SetEnergy((*iter).GetEnergy() * energyScale);
222 }
223 //std::cout << "Scaled" << std::endl;
224 if (stats != nullptr) {
225 stats->recordShowerLibUse(calcKey((*libit).first));
226 }
227 //std::cout << "Done" << std::endl;
228 return outshower;
229 }
230
231 double EtaEnergyShowerLib::getContainmentZ(const G4Track* track) const
232 {
233 if (!checkEtaAndStuff(track)) return 0.0;
234
235 double eta = track->GetPosition().eta();
236
237 if ((m_onlyPositive) && (eta < 0.0)) eta = -eta;
238
239 library::const_iterator libit = m_libData.upper_bound(eta);
240
241 if (libit == m_libData.begin()) {
242 //this is really weird
243 std::cout << "Something is wrong with eta: mineta=" << m_mineta << " eta=" << eta << std::endl;
244 } else {
245 --libit;
246 }
247
248 if ((*libit).second.empty()) {
249 std::cout << "The etabin corresponding to the eta is empty" << std::endl;
250 return 0.0;
251 }
252 double trenergy = track->GetKineticEnergy();
253 etabin::const_iterator etait = (*libit).second.lower_bound(trenergy);
254 if (etait == (*libit).second.end()) --etait; //if it points to the end, repoint it to the last shower in bin
255 else if (etait != (*libit).second.begin()) { //find the closest energy. it's either found item or previous (if there is a previous)
256 etabin::const_iterator etaitch = etait;
257 --etaitch;
258 if (((*etait).first - trenergy) > (trenergy - (*etaitch).first)) { // the closest is the previous
259 --etait;
260 }
261 }
262 //std::cout << "Found closest energy:" << (*etait).first << std::endl;
263 double rezZ = (*etait).second.getZSize();
264 etabin::const_iterator etaiter = etait;
265 int actualNumFS = 1;
266 int spread = 2; //will calculate average Z for 5 showers max ( -2 .. 0 .. +2 )
267 double upperEnergy = (*etait).first * 1.01; //we allow 1% off
268 for (int i = 0; i < spread; i++) {
269 ++etaiter;
270 if (etaiter == (*libit).second.end()) {
271 break;
272 }
273 if (upperEnergy < (*etaiter).first) break; //energy diff too high
274 //the shower is OK, including it to the average
275 rezZ += (*etaiter).second.getZSize();
276 actualNumFS++;
277 }
278 etaiter = etait;
279 if (etaiter != (*libit).second.begin()) {
280 double lowerEnergy = (*etait).first * 0.99; //we allow 1% off
281 for (int i = 0; i < spread; i++) {
282 --etaiter;
283 if (lowerEnergy > (*etaiter).first) break; //energy diff too high
284 //the shower is OK, including it to the average
285 rezZ += (*etaiter).second.getZSize();
286 actualNumFS++;
287 if (etaiter == (*libit).second.begin()) {
288 break;
289 }
290 }
291 }
292 return rezZ/actualNumFS; //average Z size
293 }
294
295 double EtaEnergyShowerLib::getContainmentR(const G4Track* track) const
296 {
297 if (!checkEtaAndStuff(track)) return 0.0;
298
299 double eta = track->GetPosition().eta();
300
301 if ((m_onlyPositive) && (eta < 0.0)) eta = -eta;
302
303 library::const_iterator libit = m_libData.upper_bound(eta);
304
305 if (libit == m_libData.begin()) {
306 //this is really weird
307 std::cout << "Something is wrong with eta: mineta=" << m_mineta << " eta=" << eta << std::endl;
308 } else {
309 --libit;
310 }
311
312 if ((*libit).second.empty()) {
313 std::cout << "The etabin corresponding to the eta is empty" << std::endl;
314 return 0.0;
315 }
316 double trenergy = track->GetKineticEnergy();
317 etabin::const_iterator etait = (*libit).second.lower_bound(trenergy);
318 if (etait == (*libit).second.end()) --etait; //if it points to the end, repoint it to the last shower in bin
319 else if (etait != (*libit).second.begin()) { //find the closest energy. it's either found item or previous (if there is a previous)
320 etabin::const_iterator etaitch = etait;
321 --etaitch;
322 if (((*etait).first - trenergy) > (trenergy - (*etaitch).first)) { // the closest is the previous
323 --etait;
324 }
325 }
326 //std::cout << "Found closest energy:" << (*etait).first << std::endl;
327 double rezR = (*etait).second.getRSize();
328 etabin::const_iterator etaiter = etait;
329 int actualNumFS = 1;
330 int spread = 2; //will calculate average Z for 5 showers max ( -2 .. 0 .. +2 )
331 double upperEnergy = (*etait).first * 1.01; //we allow 1% off
332 for (int i = 0; i < spread; i++) {
333 ++etaiter;
334 if (etaiter == (*libit).second.end()) {
335 break;
336 }
337 if (upperEnergy < (*etaiter).first) break; //energy diff too high
338 //the shower is OK, including it to the average
339 rezR += (*etaiter).second.getRSize();
340 actualNumFS++;
341 }
342 etaiter = etait;
343 if (etaiter != (*libit).second.begin()) {
344 double lowerEnergy = (*etait).first * 0.99; //we allow 1% off
345 for (int i = 0; i < spread; i++) {
346 --etaiter;
347 if (lowerEnergy > (*etaiter).first) break; //energy diff too high
348 //the shower is OK, including it to the average
349 rezR += (*etaiter).second.getRSize();
350 actualNumFS++;
351 if (etaiter == (*libit).second.begin()) {
352 break;
353 }
354 }
355 }
356 return rezR/actualNumFS; //average Z size
357 }
358
360 {
361 if (m_filled) {
362 std::cout << "ERROR: filled" << std::endl;
363 return false;
364 }
365
366 double eta = genParticle->production_vertex()->position().eta();//momentum().eta();
367
368 if ((m_onlyPositive) && (eta < 0.0)) eta = -eta;
369
370 if ( (eta < m_mineta) || (eta > m_maxeta) ) {
371 std::cout << "ERROR: eta is outside: " << m_mineta << " << " << m_maxeta << " : " << eta << std::endl;
372 return false;
373 }
374 if ( genParticle->pdg_id() != m_particle ) {
375 std::cout << "ERROR: wrong pdgcode: " << m_particle << " != " << genParticle->pdg_id() << std::endl;
376 return false;
377 }
378
379 library::iterator libit = m_libData.upper_bound(eta);
380 if (libit == m_libData.begin()) {
381 //this is really weird
382 std::cout << "Something is wrong with eta: mineta=" << m_mineta << " eta=" << eta << std::endl;
383 } else {
384 --libit;
385 }
386 (*libit).second.insert(etabin::value_type(genParticle->momentum().e(),(*shower)));
387 return true;
388 }
389
391 {
392 if (m_libData.empty()) return false;
393 TParameter<int> ver("version",LIB_VERSION);
394
395 dest->WriteObject(&ver,"version");
396
397 TTree TTreeMeta;
398 TTree TTreeLib;
399
400 write(&TTreeLib);
401 writeMeta(&TTreeMeta);
402
403 dest->WriteObject(&TTreeLib,"library");
404 dest->WriteObject(&TTreeMeta,"meta");
405
406 return true;
407 }
408
409
410 bool EtaEnergyShowerLib::read(TTree* source)
411 {
412 /*
413 * Eta Energy library format:
414 * | x | y | z | e | time | - name of branch in TTree
415 * ------------------------------------------------------------------
416 * |num of showers| min eta for | global | global | not | - eta bin header
417 * | in eta bin | cur eta bin | eta min | eta max | used |
418 * ------------------------------------------------------------------
419 * | num of hits |shower r-size |shower z-size | truth | not | - shower header
420 * | in shower |for cont.check|for cont.check| energy | used |
421 * ------------------------------------------------------------------
422 * |x-coord of hit|y-coord of hit|z-coord of hit|dep.energy|hit time| - hit
423 */
424 int nentr = source->GetEntriesFast();
425 if (nentr < 3) return false;
426 Float_t x,y,z,e,time;
427 source->SetBranchAddress("x",&x);
428 source->SetBranchAddress("y",&y);
429 source->SetBranchAddress("z",&z);
430 source->SetBranchAddress("e",&e);
431 source->SetBranchAddress("time",&time);
432 int entr = 0;
433
434 do {
435 //read eta bin header
436 source->GetEntry(entr++); //x - nshowers, y - min eta in the current eta bin
437 int nsh = (int)(x+0.1); // +0.1 just in case - c++ has low round
438 float curEta = y;
439 etabin * curbin = &(m_libData[curEta]); //creating a new eta bin
440 m_mineta = z;
441 m_maxeta = e;
442 for(int i = 0; i < nsh; i++) {
443 //read shower header
444 source->GetEntry(entr++); //x - nhits, y - r size, z - z size, e - gen energy
445 int nhits = (int)(x+0.1);
446 float curEnergy = e;
447 Shower * shower = &((*curbin)[curEnergy]);
448 shower->setRSize(y);
449 shower->setZSize(z);
450 for(int j = 0; j < nhits; j++) {
451 source->GetEntry(entr++); //variables mean what the name suggests
452 shower->push_back(new ShowerEnergySpot(G4ThreeVector(x,y,z),e,time));
453 }
454 }
455 } while (entr < nentr);
456
457 if (entr != nentr) {
458 return false;
459 }
460
461 m_filled = true;
462 if (m_mineta < 0) m_onlyPositive = false;
463 else m_onlyPositive = true;
464 return true;
465 }
466
467 bool EtaEnergyShowerLib::write(TTree* dest) const
468 {
469 /*
470 * Eta Energy library format:
471 * | x | y | z | e | time | - name of branch in TTree
472 * ------------------------------------------------------------------
473 * |num of showers| min eta for | global | global | not | - eta bin header
474 * | in eta bin | cur eta bin | eta min | eta max | used |
475 * ------------------------------------------------------------------
476 * | num of hits |shower r-size |shower z-size | truth | not | - shower header
477 * | in shower |for cont.check|for cont.check| energy | used |
478 * ------------------------------------------------------------------
479 * |x-coord of hit|y-coord of hit|z-coord of hit|dep.energy|hit time| - hit
480 */
481 Float_t x,y,z,e,time;
482 dest->Branch("x",&x);
483 dest->Branch("y",&y);
484 dest->Branch("z",&z);
485 dest->Branch("e",&e);
486 dest->Branch("time",&time);
487 library::const_iterator libit;
488 for (libit = m_libData.begin(); libit != m_libData.end(); ++libit) {
489 x = (*libit).second.size();
490 y = (*libit).first;
491 z = m_mineta;
492 e = m_maxeta;
493 dest->Fill(); //eta bin header
494 etabin::const_iterator etait;
495 for (etait = (*libit).second.begin(); etait != (*libit).second.end(); ++etait) {
496 x = (*etait).second.size();
497 y = (*etait).second.getRSize();
498 z = (*etait).second.getZSize();
499 e = (*etait).first;
500 dest->Fill(); //shower header
501 Shower::const_iterator iter;
502 for (iter = (*etait).second.begin(); iter != (*etait).second.end(); ++iter) {
503 x = (*iter)->GetPosition().x();
504 y = (*iter)->GetPosition().y();
505 z = (*iter)->GetPosition().z();
506 e = (*iter)->GetEnergy();
507 time = (*iter)->GetTime();
508 dest->Fill();
509 }
510 }
511 }
512 //dest->Write();
513 return true;
514 }
515
516 bool EtaEnergyShowerLib::readStructure(std::vector<float>& structure)
517 {
518 std::vector<float>::const_iterator iter;
519
520 for (iter = structure.begin(); iter != structure.end(); ++iter) {
521 m_libData[(*iter)];
522 }
523
524 return true;
525 }
526
528 {
529 std::map<int, std::string> names;
530 std::map<int, int> sizes;
531 for(library::const_iterator it = m_libData.begin(); it != m_libData.end(); ++it) {
532 sizes[calcKey(it->first)]=it->second.size();
533 float etalow = it->first;
534 float etahigh;
535 library::const_iterator it_copy = it;
536 ++it_copy;
537 if (it_copy == m_libData.end()) {
538 etahigh = m_maxeta;
539 } else {
540 etahigh = it_copy->first;
541 }
542 std::stringstream ss;
543 ss << std::showpos << std::fixed << std::setprecision(2);
544 ss << "ETA: " << etalow << " .. " << etahigh;
545 names[calcKey(it->first)] = ss.str();
546 }
547 return new ShowerLibStatistics(names, sizes);
548 }
549
550 bool EtaEnergyShowerLib::checkEtaAndStuff(const G4Track* track) const
551 {
552 if (!m_filled) {
553 std::cout << "Library is not created for production use" << std::endl;
554 return false;
555 }
556
557 double eta = track->GetPosition().eta();
558
559 if ((m_onlyPositive) && (eta < 0.0)) eta = -eta;
560
561 if ( (eta < m_mineta) || (eta > m_maxeta)) {
562 std::cout << "eta is outside library eta range: mineta=" << m_mineta << " maxeta: " << m_maxeta <<" eta=" << eta << std::endl;
563 return false;
564 }
565
566 G4int particleCode = track->GetDefinition()->GetPDGEncoding();
567 if ( particleCode < 0 ) particleCode = -particleCode; // hack for positrons.
568
569 if ( particleCode != m_particle ) {
570 std::cout << "wrong particle: " << particleCode << "!=" << m_particle << std::endl;
571 return false;
572 }
573 return true;
574 }
575
576} // namespace ShowerLib
Scalar eta() const
pseudorapidity method
#define LIB_VERSION
static Double_t ss
#define y
#define x
#define z
std::map< float, Shower > etabin
static IShowerLib * createEmptyLib(const std::string &inputFile)
factory method. create empty library with the given structure. returns NULL if file is invalid.
static IShowerLib * readFromROOTFile(TFile *source)
factory method. create a library from root file. returns NULL if file is invalid.
bool write(TTree *dest) const
write library to given TTree
bool read(TTree *source)
read library from given TTree
virtual double getContainmentZ(const G4Track *track) const
get average length of showers for the given energy
virtual bool storeShower(HepMC::ConstGenParticlePtr genParticle, const Shower *shower)
store shower in the library
virtual ShowerLibStatistics * createStatistics() const
virtual bool writeToROOT(TFile *dest)
write library to ROOT file
bool readStructure(std::vector< float > &structure)
virtual std::vector< EnergySpot > * getShower(const G4Track *track, ShowerLibStatistics *stats, int randomShift) const
get shower for given G4 track
bool checkEtaAndStuff(const G4Track *track) const
virtual double getContainmentR(const G4Track *track) const
get average lateral spread of the showers for the given energy
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
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
const GenParticle * ConstGenParticlePtr
Definition GenParticle.h:38
Namespace for the ShowerLib related classes.
Definition StepInfo.h:17
int calcKey(float eta)