ATLAS Offline Software
TestShowerLib.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
6 // this header file
8 
9 // CLHEP incldues
10 #include "AtlasHepMC/GenParticle.h"
11 #include "AtlasHepMC/GenVertex.h"
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 
26 #include "LArG4Code/EnergySpot.h"
28 
29 #include "TTree.h"
30 #include "TFile.h"
31 #include "TParameter.h"
32 
33 #define LIB_VERSION 10
34 
35 namespace ShowerLib {
36 
38  {
39  }
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 
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 = det;
108  newlib->m_particle = part;
109  newlib->m_filled = false;
110 
111  std::getline(filestr,instr);
112  newlib->m_comment = 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
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
ShowerLib::Shower::setZSize
void setZSize(const float zsize)
Definition: Shower.h:64
ShowerLib::IShowerLib::m_detector
std::string m_detector
name of the detector
Definition: IShowerLib.h:106
LIB_VERSION
#define LIB_VERSION
Definition: TestShowerLib.cxx:33
ShowerLib::Shower
Class for shower library shower.
Definition: Shower.h:36
ShowerLib::TestShowerLib::readFromROOTFile
static IShowerLib * readFromROOTFile(TFile *source)
factory method. create a library from root file. returns NULL if file is invalid.
Definition: TestShowerLib.cxx:41
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
ShowerLib::ShowerLibStatistics
Definition: ShowerLibStatistics.h:20
ShowerLib::TestShowerLib::~TestShowerLib
virtual ~TestShowerLib()
default destructor
Definition: TestShowerLib.cxx:37
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ShowerLib::TestShowerLib::read
bool read(TTree *source)
read library from given TTree
Definition: TestShowerLib.cxx:187
ShowerLib::TestShowerLib::m_libData
library m_libData
Definition: TestShowerLib.h:84
TestShowerLib.h
GenVertex.h
ShowerLib::TestShowerLib::getShower
virtual std::vector< EnergySpot > * getShower(const G4Track *track, ShowerLibStatistics *stats, int randomShift) const
get shower for given G4 track
Definition: TestShowerLib.cxx:118
ShowerLib::ShowerEnergySpot
Definition: ShowerEnergySpot.h:13
ShowerLib::IShowerLib::m_comment
std::string m_comment
comment
Definition: IShowerLib.h:112
ShowerLib::ShowerEnergySpot::GetPosition
G4ThreeVector GetPosition() const
Definition: ShowerEnergySpot.h:48
ShowerLib::TestShowerLib::TestShowerLib
TestShowerLib()
Definition: TestShowerLib.h:69
ShowerLib::TestShowerLib::createEmptyLib
static IShowerLib * createEmptyLib(const std::string &inputFile)
factory method. create empty library with the given structure. returns NULL if file is invalid.
Definition: TestShowerLib.cxx:67
perfmonmt-printer.dest
dest
Definition: perfmonmt-printer.py:189
ShowerLib::IShowerLib::m_filled
bool m_filled
is the library read from ROOT or from structure file
Definition: IShowerLib.h:114
x
#define x
ShowerLib::TestShowerLib::write
bool write(TTree *dest) const
write library to given TTree
Definition: TestShowerLib.cxx:233
GenParticle.h
ShowerLib::TestShowerLib::getContainmentR
virtual double getContainmentR(const G4Track *track) const
get average lateral spread of the showers for the given energy
Definition: TestShowerLib.cxx:140
ShowerLib::IShowerLib::writeMeta
bool writeMeta(TTree *dest) const
write metadata to the given TTree
Definition: IShowerLib.cxx:40
ShowerLib::TestShowerLib::getContainmentZ
virtual double getContainmentZ(const G4Track *track) const
get average length of showers for the given energy
Definition: TestShowerLib.cxx:129
Pythia8_A14_NNPDF23LO_Var1Down_Common.ver
ver
Definition: Pythia8_A14_NNPDF23LO_Var1Down_Common.py:26
ShowerEnergySpot.h
ParticleGun_EoverP_Config.momentum
momentum
Definition: ParticleGun_EoverP_Config.py:63
CaloCondBlobAlgs_fillNoiseFromASCII.inputFile
string inputFile
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:17
lumiFormat.i
int i
Definition: lumiFormat.py:92
z
#define z
ShowerLib::IShowerLib::m_particle
int m_particle
ID of the generated particles.
Definition: IShowerLib.h:107
WritePulseShapeToCool.det
det
Definition: WritePulseShapeToCool.py:204
ShowerLib::ShowerEnergySpot::GetTime
G4double GetTime() const
Definition: ShowerEnergySpot.h:54
ShowerLib::IShowerLib
Class for shower library shower lib interface.
Definition: IShowerLib.h:40
ShowerLib::IShowerLib::readMeta
bool readMeta(TTree *source)
read metadata from the given TTree
Definition: IShowerLib.cxx:16
ShowerLib::TestShowerLib::createStatistics
virtual ShowerLibStatistics * createStatistics() const
Definition: TestShowerLib.cxx:281
HepMC::ConstGenParticlePtr
const GenParticle * ConstGenParticlePtr
Definition: GenParticle.h:38
ShowerLib::TestShowerLib::genInfo::vertex
std::unique_ptr< HepMC::FourVector > vertex
Definition: TestShowerLib.h:72
ShowerLib::TestShowerLib::writeToROOT
virtual bool writeToROOT(TFile *dest)
write library to ROOT file
Definition: TestShowerLib.cxx:167
EnergySpot.h
ShowerLib::TestShowerLib::genInfo
Definition: TestShowerLib.h:71
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
ShowerLib::TestShowerLib::storeShower
virtual bool storeShower(HepMC::ConstGenParticlePtr genParticle, const Shower *shower)
store shower in the library
Definition: TestShowerLib.cxx:151
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
y
#define y
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
ShowerLib::Shower::setRSize
void setRSize(const float rsize)
Definition: Shower.h:65
ShowerLib::ShowerEnergySpot::GetEnergy
G4double GetEnergy() const
Definition: ShowerEnergySpot.h:38
ShowerLib
Namespace for the ShowerLib related classes.
Definition: LArG4GenShowerLib.h:19
ShowerLib::TestShowerLib::storedShower
std::pair< genInfo, Shower > storedShower
Definition: TestShowerLib.h:76
ShowerLib::TestShowerLib
Class for shower library shower lib.
Definition: TestShowerLib.h:35