ATLAS Offline Software
LArG4ShowerLibSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 // shower lib classes
9 
11 
12 #include "AthenaKernel/Units.h"
13 
14 #include <sstream>
15 #include "TFile.h"
16 #include "TTree.h"
17 #include "Randomize.hh"
18 
19 namespace Units = Athena::Units;
20 
21 
22 LArG4ShowerLibSvc::LArG4ShowerLibSvc(const std::string& name,ISvcLocator* svc)
23  : base_class(name,svc)
24  , m_fileNameList()
25 {
26  declareProperty( "FileNameList", m_fileNameList, "List of filenames for direct reading" );
27 
28  /* BE SURE THIS ONE IS THE SAME AS IN LArG4FastSimSvc!!! */
29  enum DETECTOR {EMB=100000,EMEC=200000,FCAL1=300000,FCAL2=400000,FCAL3=500000,HECLOC=600000,HEC=700000};
30 
31  m_detmap["EMB"]=EMB;
32  m_detmap["EMEC"]=EMEC;
33  m_detmap["FCAL1"]=FCAL1;
34  m_detmap["FCAL2"]=FCAL2;
35  m_detmap["FCAL3"]=FCAL3;
36  m_detmap["HECLOC"]=HECLOC;
37  m_detmap["HEC"]=HEC;
38 }
39 
41 {
42 }
43 
45 {
46  ATH_MSG_INFO("Initializing");
47 
48  // iterate through filenames in list
49  for (const std::string& fileName : m_fileNameList) {
50  std::string resolvedFilename = PathResolverFindCalibFile(fileName);
51  if (resolvedFilename.empty()) {
52  ATH_MSG_WARNING("Could not resolve input filename " << (fileName) << ". Ignoring!");
53  continue;
54  } else {
55  ATH_MSG_INFO("Resolving input filename to " << resolvedFilename);
56  }
57 
58  TFile rootfile(resolvedFilename.c_str(),"READ");
59 
60  if (rootfile.IsZombie()) {
61  ATH_MSG_WARNING("File " << resolvedFilename << " is not a valid ROOT file");
62  continue;
63  }
64 
65  const ShowerLib::IShowerLib* library = nullptr;
66 
67  // trying to create a library out of provided file
69 
70  // if no library can be created based on the file nullptr is returned
71  if (library == nullptr) {
72  ATH_MSG_WARNING("File " << resolvedFilename << " is not a valid library file");
73  continue;
74  }
75 
76  if (m_detmap.find(library->detector()) == m_detmap.end()) {
77  ATH_MSG_WARNING("Library " << resolvedFilename << " is produced for unknown detector: " << library->detector());
78  delete library;
79  continue;
80  }
81 
82  std::stringstream location;
83  location << library->detector() << "/" << library->particle_id();
84  int key = m_detmap.find(library->detector())->second + library->particle_id();
85 
86  // put the library into map
87  m_libraryMap[key] = library;
88  m_locations[key] = location.str();
89 
90  ATH_MSG_DEBUG("Filename: " << resolvedFilename.c_str());
91  ATH_MSG_DEBUG("Location: " << location.str());
92  ATH_MSG_DEBUG("Release: " << library->release().c_str());
93  ATH_MSG_DEBUG("Geant ver: " << library->geantVersion().c_str());
94  ATH_MSG_DEBUG("Phys list: " << library->physicsList().c_str());
95  }
96 
97  // no point in the service with no libraries
98  if (m_libraryMap.empty()) {
99  ATH_MSG_WARNING("No library files found");
100  } else {
101  ATH_MSG_INFO("List of loaded libraries:");
102  for (const auto& m : m_libraryMap) {
103  ATH_MSG_INFO(" " << m_locations[m.first] << ": " << m.second->comment());
104 #ifdef DEBUG_FrozenShowers
105  m_statisticsMap[m.second] = m.second->createStatistics();
106 #endif
107  }
108  }
109 
110  ATH_MSG_INFO("Shower library successfully initialized.");
111 
112  return StatusCode::SUCCESS;
113 }
114 
116 {
117 
118  ATH_MSG_INFO("Finalizing shower library service.");
119 
120  libmap::const_iterator iter;
121 
122  for (iter = m_libraryMap.begin(); iter != m_libraryMap.end(); ++iter) {
123  ATH_MSG_INFO("Found ShowerLib at location " << m_locations[(*iter).first]);
124  ATH_MSG_INFO(std::endl << (*iter).second->statistics());
125  if (m_statisticsMap.find((*iter).second) != m_statisticsMap.end())
126  ATH_MSG_INFO(m_statisticsMap.find((*iter).second)->second->statistics());
127  else
128  ATH_MSG_INFO("No statistics available for this kind of library");
129  // delete the library:
130  delete (*iter).second ;
131  }
132 
133  return StatusCode::SUCCESS;
134 }
135 
136 /*
137  * Returns library from internal map based on the particle. Returns nullptr if there
138  * is no library for needed particle/detector.
139  */
140 const ShowerLib::IShowerLib* LArG4ShowerLibSvc::getShowerLib(G4int particleCode, int detectorTag) const
141 {
142  int location;
143 
144  location = detectorTag + particleCode;
145 
146  libmap::const_iterator iter = m_libraryMap.find(location);
147  if (iter != m_libraryMap.end()) {
148  return (*iter).second;
149  }
150 
151  return nullptr;
152 }
153 
154 bool
155 LArG4ShowerLibSvc::checkLibrary(G4int particleCode, int detectorTag)
156 {
157  const ShowerLib::IShowerLib* library = getShowerLib(particleCode, detectorTag);
158  if (library != nullptr) return true;
159 
160  return false;
161 }
162 
163 /*
164  * Returns a shower based on the particle. Return empty shower if there is no
165  * appropriate showers in the libraries.
166  */
167 
168 #ifdef DEBUG_FrozenShowers
169 std::vector<EnergySpot>
170 LArG4ShowerLibSvc::getShower(const G4FastTrack& track, int detectorTag)
171 #else
172 std::vector<EnergySpot>
173 LArG4ShowerLibSvc::getShower(const G4FastTrack& track, int detectorTag) const
174 #endif
175 {
176  // get shower lib from the map
177  const ShowerLib::IShowerLib* library = getShowerLib(track.GetPrimaryTrack()->GetDefinition()->GetPDGEncoding(), detectorTag);
178 
179  if (library == nullptr) {
180  // no library in map
181  ATH_MSG_ERROR("No library for location: " << detectorTag << "/" << track.GetPrimaryTrack()->GetDefinition()->GetPDGEncoding());
182  return std::vector<EnergySpot>();
183  }
184 
185  // starting point of the shower:
186  G4ThreeVector PositionShower = track.GetPrimaryTrack()->GetPosition();
187 
188  // get a shower from the library
189  int randomShift = 0;
190  randomShift = (int)(CLHEP::RandGaussZiggurat::shoot(G4Random::getTheEngine(), 0., 2.5)+0.5);
191 
192 #ifdef DEBUG_FrozenShowers
193  std::vector<EnergySpot>* shower = library->getShower(track.GetPrimaryTrack(), m_statisticsMap[library], randomShift);
194 #else
195  std::vector<EnergySpot>* shower = library->getShower(track.GetPrimaryTrack(), nullptr, randomShift);
196 #endif
197 
198 
199  if (shower == nullptr) {
200  return std::vector<EnergySpot>();
201  }
202 
203  // axis of the shower, in global reference frame:
204  G4ThreeVector DirectionShower = track.GetPrimaryTrack()->GetMomentumDirection();
205 
206  // time of the track (as in LArBarrelCalculator.cc)
207  G4double tof = track.GetPrimaryTrack()->GetGlobalTime() / Units::ns;
208  G4double time = tof - ( track.GetPrimaryTrack()->GetPosition().mag()/Units::c_light ) / Units::ns;
209 
211 
212  // Create energy spots
213  for (hit = shower->begin(); hit != shower->end(); ++hit) {
214  G4ThreeVector hitpos = (*hit).GetPosition();
215  hitpos.rotateUz(DirectionShower); // rotate the hit to the needed direction
216  (*hit).SetPosition(hitpos+PositionShower);
217  (*hit).SetTime((*hit).GetTime() + time);
218  }
219 
220  if (msgSvc()->outputLevel(name()) <= MSG::VERBOSE) {
221  ATH_MSG_VERBOSE("Prepared " << shower->size() << " EnergySpot");
222  for (std::vector<EnergySpot>::const_iterator iter = shower->begin(); iter != shower->end(); ++iter) {
223  ATH_MSG_VERBOSE("EnergySpot: " << iter->GetPosition().x() << " " << iter->GetPosition().y() << " " << iter->GetPosition().z()
224  << " " << iter->GetEnergy() << " " << iter->GetTime());
225  }
226  }
227 
228  return *std::unique_ptr< std::vector<EnergySpot> >(shower);
229 }
230 
231 double
232 LArG4ShowerLibSvc::getContainmentZ(const G4FastTrack& track, int detectorTag)
233 {
234  // get shower lib from the map
235  const ShowerLib::IShowerLib* library = getShowerLib(track.GetPrimaryTrack()->GetDefinition()->GetPDGEncoding(), detectorTag);
236 
237  if (library == nullptr) {
238  return 0.0;
239  }
240 
241  return library->getContainmentZ(track.GetPrimaryTrack());
242 }
243 
244 double
245 LArG4ShowerLibSvc::getContainmentR(const G4FastTrack& track, int detectorTag)
246 {
247  // get shower lib from the map
248  const ShowerLib::IShowerLib* library = getShowerLib(track.GetPrimaryTrack()->GetDefinition()->GetPDGEncoding(), detectorTag);
249 
250  if (library == nullptr) {
251  return 0.0;
252  }
253 
254  return library->getContainmentR(track.GetPrimaryTrack());
255 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ShowerLib::IShowerLib::getContainmentZ
virtual double getContainmentZ(const G4Track *track) const =0
get average length of showers for the given energy
ShowerLib::IShowerLib::release
virtual const std::string release() const
get Release tag
Definition: IShowerLib.h:127
LArSamples::HEC
@ HEC
Definition: CaloId.h:26
python.SystemOfUnits.m
int m
Definition: SystemOfUnits.py:91
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ShowerLib::IShowerLib::getContainmentR
virtual double getContainmentR(const G4Track *track) const =0
get average lateral spread of the showers for the given energy
LArG4ShowerLibSvc.h
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
ShowerLib::IShowerLib::detector
virtual const std::string detector() const
get detector tag
Definition: IShowerLib.h:121
LArG4ShowerLibSvc::LArG4ShowerLibSvc
LArG4ShowerLibSvc(const std::string &name, ISvcLocator *svc)
Definition: LArG4ShowerLibSvc.cxx:22
LArG4ShowerLibSvc::checkLibrary
virtual bool checkLibrary(G4int particleCode, int detectorTag)
Definition: LArG4ShowerLibSvc.cxx:155
LArG4ShowerLibSvc::getShowerLib
const ShowerLib::IShowerLib * getShowerLib(G4int particleCode, int detectorTag) const
get shower library from StoreGate by track (using current volume name)
Definition: LArG4ShowerLibSvc.cxx:140
LArG4ShowerLibSvc::~LArG4ShowerLibSvc
virtual ~LArG4ShowerLibSvc()
Definition: LArG4ShowerLibSvc.cxx:40
ShowerLib::iterateTTree
IShowerLib * iterateTTree(TFile *fname)
Definition: ShowerLibList.cxx:55
CaloCell_ID_FCS::FCAL1
@ FCAL1
Definition: FastCaloSim_CaloCell_ID.h:41
LArG4ShowerLibSvc::m_libraryMap
libmap m_libraryMap
mapping StoreGate key to handle in StoreGate
Definition: LArG4ShowerLibSvc.h:69
LArG4ShowerLibSvc::getContainmentR
virtual double getContainmentR(const G4FastTrack &track, int detectorTag)
Definition: LArG4ShowerLibSvc.cxx:245
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
LArCalib_HVScale2NtupleConfig.rootfile
string rootfile
Definition: LArCalib_HVScale2NtupleConfig.py:74
ShowerLib::IShowerLib::particle_id
virtual int particle_id() const
get particle tag
Definition: IShowerLib.h:124
LArSamples::EMEC
@ EMEC
Definition: CaloId.h:25
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
LArSamples::EMB
@ EMB
Definition: CaloId.h:25
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:36
LArG4ShowerLibSvc::m_detmap
std::map< std::string, int > m_detmap
Definition: LArG4ShowerLibSvc.h:73
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
python.sizes.location
string location
Definition: sizes.py:11
LArG4ShowerLibSvc::m_locations
std::map< int, std::string > m_locations
Definition: LArG4ShowerLibSvc.h:72
LArG4ShowerLibSvc::m_statisticsMap
statmap m_statisticsMap
Definition: LArG4ShowerLibSvc.h:71
ShowerLib::IShowerLib
Class for shower library shower lib interface.
Definition: IShowerLib.h:40
ShowerLib::IShowerLib::getShower
virtual std::vector< EnergySpot > * getShower(const G4Track *track, ShowerLibStatistics *stats, int randomShift) const =0
get shower for given G4 track
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
LArG4ShowerLibSvc::getContainmentZ
virtual double getContainmentZ(const G4FastTrack &track, int detectorTag)
Definition: LArG4ShowerLibSvc.cxx:232
Athena::Units
Definition: Units.h:45
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
LArG4ShowerLibSvc::m_fileNameList
StringArrayProperty m_fileNameList
property, list of library files
Definition: LArG4ShowerLibSvc.h:75
ShowerLibList.h
python.PhysicalConstants.c_light
float c_light
Definition: PhysicalConstants.py:63
Units.h
Wrapper to avoid constant divisions when using units.
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
LArG4ShowerLibSvc::finalize
virtual StatusCode finalize()
Definition: LArG4ShowerLibSvc.cxx:115
CaloSwCorrections.time
def time(flags, cells_name, *args, **kw)
Definition: CaloSwCorrections.py:242
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ShowerLib::IShowerLib::geantVersion
virtual const std::string geantVersion() const
get geant version tag
Definition: IShowerLib.h:133
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
python.SystemOfUnits.ns
int ns
Definition: SystemOfUnits.py:130
CaloCell_ID_FCS::FCAL2
@ FCAL2
Definition: FastCaloSim_CaloCell_ID.h:42
xAOD::track
@ track
Definition: TrackingPrimitives.h:512
ShowerLib::IShowerLib::physicsList
virtual const std::string physicsList() const
get geant 4 physics list name
Definition: IShowerLib.h:136
python.Constants.VERBOSE
int VERBOSE
Definition: Control/AthenaCommon/python/Constants.py:14
LArG4ShowerLibSvc::initialize
virtual StatusCode initialize()
Definition: LArG4ShowerLibSvc.cxx:44
LArG4ShowerLibSvc::getShower
virtual std::vector< EnergySpot > getShower(const G4FastTrack &track, int detectorTag) const
return list of energy depositions for given track (interface implementation)
Definition: LArG4ShowerLibSvc.cxx:173
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37