ATLAS Offline Software
Loading...
Searching...
No Matches
LarEMSamplingFraction.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
12
13#include "GaudiKernel/ServiceHandle.h"
14#include "GaudiKernel/ITHistSvc.h"
15#include "Gaudi/Property.h"
16
18
19#include "TString.h"
20#include <iterator>
21#include <cmath>
22#include <map>
23
24using namespace std;
25
26//###############################################################################
28 , ISvcLocator* pSvcLocator)
29 : AthAlgorithm(name, pSvcLocator)
30{
31 // Name of ClusterContainer to use
32 declareProperty("DoCells",m_docells);
33 declareProperty("CalibrationHitContainerNames",m_CalibrationHitContainerNames);
34}
35
36//###############################################################################
37
40
41//###############################################################################
42
44{
45 //---- initialize the StoreGateSvc ptr ----------------
46
47 ServiceHandle<ITHistSvc> histSvc("THistSvc",name());
48 ATH_CHECK( histSvc.retrieve() );
49
50 m_mytree = new TTree("mytree","mytree");
51 m_mytree->Branch("energy_reco", &m_energy_reco);
52 m_mytree->Branch("energy_hit", &m_energy_hit);
53 m_mytree->Branch("energy_inactive_total",&m_energy_inactive_total);
54 m_mytree->Branch("energy_inactive_em", &m_energy_inactive_em);
55 m_mytree->Branch("energy_inactive_nonem",&m_energy_inactive_nonem);
56 m_mytree->Branch("energy_inactive_inv", &m_energy_inactive_inv);
57 m_mytree->Branch("energy_inactive_esc", &m_energy_inactive_esc);
58 m_mytree->Branch("energy_active_total_corrected",&m_energy_active_total_corrected);
59 m_mytree->Branch("energy_active_total",&m_energy_active_total);
60 m_mytree->Branch("energy_active_em", &m_energy_active_em);
61 m_mytree->Branch("energy_active_nonem",&m_energy_active_nonem);
62 m_mytree->Branch("energy_active_inv", &m_energy_active_inv);
63 m_mytree->Branch("energy_active_esc", &m_energy_active_esc);
64 m_mytree->Branch("mc_pdg", &m_mc_pdg);
65 m_mytree->Branch("mc_eta", &m_mc_eta);
66 m_mytree->Branch("mc_phi", &m_mc_phi);
67 m_mytree->Branch("mc_e", &m_mc_e);
68 m_mytree->Branch("mc_pt", &m_mc_pt);
69
70 if(m_docells) {
71 m_mytree->Branch("cell_identifier",&m_cell_identifier);
72 m_mytree->Branch("cell_energy_reco",&m_cell_energy_reco);
73 m_mytree->Branch("cell_energy_inactive_total",&m_cell_energy_inactive_total);
74 m_mytree->Branch("cell_energy_active_total_corrected",&m_cell_energy_active_total_corrected);
75 m_mytree->Branch("cell_energy_active_total",&m_cell_energy_active_total);
76 m_mytree->Branch("cell_sampling",&m_cell_sampling);
77 m_mytree->Branch("cell_eta",&m_cell_eta);
78 m_mytree->Branch("cell_phi",&m_cell_phi);
79 }
80
81 histSvc->regTree("/MYSTREAM/myTree",m_mytree).ignore();
82
83 // pointer to detector manager:
84 ATH_CHECK(m_caloMgrKey.initialize());
85 ATH_CHECK(detStore()->retrieve(m_calo_id, "CaloCell_ID"));
86
87 const CaloIdManager* caloIdManager;
88 ATH_CHECK(detStore()->retrieve(caloIdManager));
89
90 m_tileID=caloIdManager->getTileID();
91 if(m_tileID==0) throw std::runtime_error("ISF_HitAnalysis: Invalid Tile ID helper");
92
93 ATH_CHECK(detStore()->retrieve(m_tileHWID));
94
95 ATH_CHECK(m_fSamplKey.initialize());
97
98 ATH_CHECK( m_tileCablingSvc.retrieve() );
99 m_tileCabling = m_tileCablingSvc->cablingService();
100
101 return StatusCode::SUCCESS;
102}
103
104//###############################################################################
105
107{
108
109 return StatusCode::SUCCESS;
110}
111
112//###############################################################################
113
115{
117 const ILArfSampl* fSampl=*fSamplHdl;
118
120 ATH_CHECK( tileSamplingFraction.isValid() );
121
122 const CaloCalibrationHitContainer* cchc;
123 std::vector<const CaloCalibrationHitContainer *> v_cchc;
124 for (const std::string& containerName : m_CalibrationHitContainerNames) {
125 if ( !evtStore()->contains<CaloCalibrationHitContainer>(containerName))
126 {
127 ATH_MSG_WARNING("SG does not contain calibration hit container " << containerName);
128 }
129 else
130 {
131 StatusCode sc = evtStore()->retrieve(cchc,containerName);
132 if (sc.isFailure() )
133 {
134 ATH_MSG_ERROR("Cannot retrieve calibration hit container " << containerName);
135 return sc;
136 }
137 else
138 {
139 v_cchc.push_back(cchc);
140 }
141 }
142 }
143
144 const McEventCollection* truthEvent{};
145 StatusCode sc = evtStore()->retrieve(truthEvent, "TruthEvent");
146 if (sc.isFailure()||!truthEvent)
147 {
148 ATH_MSG_ERROR("No McEventCollection found");
149 return StatusCode::FAILURE;
150 }
151 auto gen = *HepMC::begin(*truthEvent->at(0));
152 m_mc_pdg = gen->pdg_id();
153 m_mc_eta = gen->momentum().pseudoRapidity();
154 m_mc_phi = gen->momentum().phi();
155 m_mc_e = gen->momentum().e();
156 m_mc_pt = sqrt(pow(gen->momentum().px(),2)+pow(gen->momentum().py(),2));
157
158 //inspiration:
159 //see https://gitlab.cern.ch/atlas/athena/blob/master/Calorimeter/CaloCalibHitRec/src/CalibHitToCaloCell.cxx
160 //and https://gitlab.cern.ch/atlas/athena/blob/master/Calorimeter/CaloCalibHitRec/src/CaloDmEnergy.cxx
161
163 ATH_CHECK(caloMgrHandle.isValid());
164 const CaloDetDescrManager* caloMgr = *caloMgrHandle;
165
166 m_energy_reco =new vector<float>;
167 m_energy_hit =new vector<float>;
168
169 m_energy_inactive_total=new vector<float>;
170 m_energy_inactive_em =new vector<float>;
171 m_energy_inactive_nonem=new vector<float>;
172 m_energy_inactive_inv =new vector<float>;
173 m_energy_inactive_esc =new vector<float>;
174
175 m_energy_active_total_corrected=new vector<float>;
176 m_energy_active_total=new vector<float>;
177 m_energy_active_em =new vector<float>;
178 m_energy_active_nonem=new vector<float>;
179 m_energy_active_inv =new vector<float>;
180 m_energy_active_esc =new vector<float>;
181
182 if(m_docells) {
183 m_cell_identifier =new std::vector<Long64_t>;
184 m_cell_energy_reco =new std::vector<float>;
185 m_cell_energy_active_total_corrected=new std::vector<float>;
186 m_cell_energy_active_total =new std::vector<float>;
187 m_cell_energy_inactive_total =new std::vector<float>;
188 m_cell_sampling =new std::vector<int>;
189 m_cell_eta =new std::vector<float>;
190 m_cell_phi =new std::vector<float>;
191 }
192
193 struct cell_info {
194 Long64_t cell_identifier=0;
195 float cell_energy_reco=0;
196 float cell_energy_active_total_corrected=0;
197 float cell_energy_active_total=0;
198 float cell_energy_inactive_total=0;
199 int cell_sampling=0;
200 float cell_eta=0;
201 float cell_phi=0;
202 };
203
204 std::map< Long64_t , cell_info > cell_info_map;
205
206 for(int s=0;s<24;s++)
207 {
208 m_energy_reco->push_back(0.0);
209 m_energy_hit->push_back(0.0);
210
211 m_energy_inactive_total->push_back(0.0);
212 m_energy_inactive_em ->push_back(0.0);
213 m_energy_inactive_nonem->push_back(0.0);
214 m_energy_inactive_inv ->push_back(0.0);
215 m_energy_inactive_esc ->push_back(0.0);
216
217 m_energy_active_total_corrected->push_back(0.0);
218 m_energy_active_total->push_back(0.0);
219 m_energy_active_em ->push_back(0.0);
220 m_energy_active_nonem->push_back(0.0);
221 m_energy_active_inv ->push_back(0.0);
222 m_energy_active_esc ->push_back(0.0);
223 }
224
225 int count=0;
226 for (const CaloCalibrationHitContainer* calibHitContainer: v_cchc)
227 {
229 for(const CaloCalibrationHit* calibHit : *calibHitContainer)
230 {
231 Identifier id=calibHit->cellID();
232
233 double Etot = calibHit->energyTotal();
234 double Eem = calibHit->energyEM();
235 double Enonem = calibHit->energyNonEM();
236 double Einv = calibHit->energyInvisible();
237 double Eesc = calibHit->energyEscaped();
238
239 double Efactor=1.0;
240
241 const CaloDetDescrElement* caloDDE = caloMgr->get_element(id);
242 int sampling=-1;
243 if(caloDDE) {
244 sampling = caloDDE->getSampling();
245
246 if((sampling>=0 && sampling<=11) || (sampling>=21 && sampling<=23)) Efactor=1/fSampl->FSAMPL(id);
247 if((sampling>=12 && sampling<=20)) {
248 HWIdentifier channel_id = m_tileCabling->s2h_channel_id(id);
249 int channel = m_tileHWID->channel(channel_id);
250 int drawerIdx = m_tileHWID->drawerIdx(channel_id);
251 Efactor = tileSamplingFraction->getSamplingFraction(drawerIdx, channel);
252 Identifier cell_id = m_tileID->cell_id(id);
253 if(caloMgr->get_element(cell_id)) {
254 id=cell_id;
255 }
256 }
257 }
258
259 ATH_MSG_VERBOSE( "cellID "<<id<<" layer "<<sampling<<" energyTotal "<<Etot<<" Eem "<<Eem<<" Enonem "<<Enonem<<" Einv "<<Einv<<" Eesc "<<Eesc<<" Efactor="<<Efactor);
260
261 if(sampling>=0 && sampling<=23)
262 {
263 if(m_docells) {
264 cell_info_map[id.get_compact()].cell_identifier=id.get_compact();
265 cell_info_map[id.get_compact()].cell_sampling=sampling;
266 cell_info_map[id.get_compact()].cell_eta=caloDDE->eta_raw();
267 cell_info_map[id.get_compact()].cell_phi=caloDDE->phi_raw();
268 }
269
270 if(m_CalibrationHitContainerNames[count]=="LArCalibrationHitInactive" || m_CalibrationHitContainerNames[count]=="TileCalibHitInactiveCell")
271 {
272 m_energy_inactive_total->at(sampling)+=Etot;
273 m_energy_inactive_em ->at(sampling)+=Eem;
274 m_energy_inactive_nonem->at(sampling)+=Enonem;
275 m_energy_inactive_inv ->at(sampling)+=Einv;
276 m_energy_inactive_esc ->at(sampling)+=Eesc;
277
278 if(m_docells) cell_info_map[id.get_compact()].cell_energy_inactive_total+=Etot;
279 }
280
281 if(m_CalibrationHitContainerNames[count]=="LArCalibrationHitActive" || m_CalibrationHitContainerNames[count]=="TileCalibHitActiveCell")
282 {
283 m_energy_active_total_corrected->at(sampling)+=Etot*Efactor;
284 m_energy_active_total->at(sampling)+=Etot;
285 m_energy_active_em ->at(sampling)+=Eem;
286 m_energy_active_nonem->at(sampling)+=Enonem;
287 m_energy_active_inv ->at(sampling)+=Einv;
288 m_energy_active_esc ->at(sampling)+=Eesc;
289
290 if(m_docells) {
291 cell_info_map[id.get_compact()].cell_energy_active_total_corrected+=Etot*Efactor;
292 cell_info_map[id.get_compact()].cell_energy_active_total+=Etot;
293 }
294 }
295
296 }
297
298 }
299
300 count++;
301 }
302
303 //Get reco cells if available
304 const CaloCellContainer *cellColl{};
305 sc = evtStore()->retrieve(cellColl, "AllCalo");
306
307 if (sc.isFailure()) {
308 ATH_MSG_WARNING( "Couldn't read AllCalo cells from StoreGate");
309 //return NULL;
310 } else {
311 ATH_MSG_DEBUG( "Found: "<<cellColl->size()<<" calorimeter cells");
312 for (const CaloCell* cell : *cellColl) {
313 Identifier id=cell->ID();
314 const CaloDetDescrElement* caloDDE = caloMgr->get_element(id);
315 int sampling=-1;
316 if(caloDDE) {
317 sampling = caloDDE->getSampling();
318 m_energy_reco->at(sampling)+=cell->energy();
319 if((sampling>=12 && sampling<=20)) {
320 Identifier cell_id = m_tileID->cell_id(id);
321 if(caloMgr->get_element(cell_id)) {
322 id=cell_id;
323 }
324 }
325 }
326 if(m_docells) {
327 cell_info_map[id.get_compact()].cell_identifier=id.get_compact();
328 cell_info_map[id.get_compact()].cell_sampling=sampling;
329 cell_info_map[id.get_compact()].cell_eta=caloDDE->eta_raw();
330 cell_info_map[id.get_compact()].cell_phi=caloDDE->phi_raw();
331 cell_info_map[id.get_compact()].cell_energy_reco+=cell->energy();
332 }
333 }
334 } //calorimeter cells
335
336
337 //Get all G4Hits (from CaloHitAnalysis)
338 const std::vector<std::string> lArKeys = {"LArHitEMB", "LArHitEMEC", "LArHitFCAL", "LArHitHEC"};
339 for (const std::string& containerName: lArKeys) {
340 const LArHitContainer* larContainer{};
341 ATH_MSG_DEBUG( "Checking G4Hits: "<<containerName);
342 if(evtStore()->retrieve(larContainer,containerName)==StatusCode::SUCCESS) {
343 int hitnumber = 0;
344 for (const LArHit* larHit : *larContainer) {
345 hitnumber++;
346 const CaloDetDescrElement *hitElement = caloMgr->get_element(larHit->cellID());
347 if(!hitElement) continue;
348 Identifier larhitid = hitElement->identify();
349 if(caloMgr->get_element(larhitid)) {
350 CaloCell_ID::CaloSample larlayer = caloMgr->get_element(larhitid)->getSampling();
351 m_energy_hit->at(larlayer)+=larHit->energy();
352 }
353 } // End while LAr hits
354 ATH_MSG_DEBUG( "Read "<<hitnumber<<" G4Hits from "<<containerName);
355 }
356 else {
357 ATH_MSG_INFO( "Can't retrieve LAr hits");
358 }// End statuscode success upon retrieval of hits
359 }// End detector type loop
360
361 const TileHitVector * hitVec{};
362 if (evtStore()->retrieve(hitVec,"TileHitVec")==StatusCode::SUCCESS && m_tileID ) {
363 int hitnumber = 0;
364 for(const TileHit& hit : *hitVec) {
365 ++hitnumber;
366 Identifier pmt_id = hit.identify();
367 Identifier cell_id = m_tileID->cell_id(pmt_id);
368
369 if (caloMgr->get_element(cell_id)) {
370 CaloCell_ID::CaloSample layer = caloMgr->get_element(cell_id)->getSampling();
371
372 //could there be more subhits??
373 for (int tilesubhit_i = 0; tilesubhit_i<hit.size(); tilesubhit_i++) {
375 ATH_MSG_DEBUG( "Tile subhit: "<<tilesubhit_i<<"/"<<hit.size()<< " E: "<<hit.energy(tilesubhit_i) );
376 m_energy_hit->at(layer) += hit.energy(tilesubhit_i);
377 }
378 }
379 }
380 ATH_MSG_DEBUG( "Read "<<hitnumber<<" G4Hits from TileHitVec");
381 }
382
383 for(auto& cell:cell_info_map) {
384 m_cell_identifier ->push_back(cell.second.cell_identifier);
385 m_cell_sampling ->push_back(cell.second.cell_sampling);
386 m_cell_eta ->push_back(cell.second.cell_eta);
387 m_cell_phi ->push_back(cell.second.cell_phi);
388 m_cell_energy_reco ->push_back(cell.second.cell_energy_reco);
389 m_cell_energy_active_total_corrected->push_back(cell.second.cell_energy_active_total_corrected);
390 m_cell_energy_active_total ->push_back(cell.second.cell_energy_active_total);
391 m_cell_energy_inactive_total ->push_back(cell.second.cell_energy_inactive_total);
392 }
393
394 m_mytree->Fill();
395
396 delete m_energy_reco;
397 delete m_energy_hit;
403
406 delete m_energy_active_em;
408 delete m_energy_active_inv;
409 delete m_energy_active_esc;
410
411 if(m_docells) {
412 delete m_cell_identifier;
413 delete m_cell_energy_reco;
417 delete m_cell_sampling;
418 delete m_cell_eta;
419 delete m_cell_phi;
420 }
421
422 return StatusCode::SUCCESS;
423}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static Double_t sc
AtlasHitsVector< TileHit > TileHitVector
constexpr int pow(int base, int exp) noexcept
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
const ServiceHandle< StoreGateSvc > & detStore() const
Class to store calorimeter calibration hit.
Container class for CaloCell.
CaloSampling::CaloSample CaloSample
Definition CaloCell_ID.h:53
Data object for each calorimeter readout cell.
Definition CaloCell.h:57
This class groups all DetDescr information related to a CaloCell.
CaloCell_ID::CaloSample getSampling() const
cell sampling
Identifier identify() const override final
cell identifier
const CaloDetDescrElement * get_element(const Identifier &cellId) const
get element by its identifier
This class provides the client interface for accessing the detector description information common to...
This class initializes the Calo (LAr and Tile) offline identifiers.
const TileID * getTileID(void) const
const T * at(size_type n) const
Access an element, as an rvalue.
size_type size() const noexcept
Returns the number of elements in the collection.
virtual const float & FSAMPL(const HWIdentifier &id) const =0
Hit collection.
Class to store hit energy and time in LAr cell from G4 simulation.
Definition LArHit.h:25
SG::ReadCondHandleKey< CaloDetDescrManager > m_caloMgrKey
std::vector< float > * m_energy_inactive_nonem
std::vector< float > * m_cell_eta
const TileCablingService * m_tileCabling
std::vector< float > * m_energy_active_total
std::vector< float > * m_energy_active_em
std::vector< float > * m_energy_active_inv
ServiceHandle< TileCablingSvc > m_tileCablingSvc
Name of Tile cabling service.
std::vector< float > * m_energy_active_esc
std::vector< int > * m_cell_sampling
SG::ReadCondHandleKey< ILArfSampl > m_fSamplKey
std::vector< float > * m_cell_energy_active_total
std::vector< float > * m_energy_hit
std::vector< float > * m_energy_inactive_em
LarEMSamplingFraction()
Default constructor:
std::vector< std::string > m_CalibrationHitContainerNames
std::vector< float > * m_energy_inactive_inv
std::vector< float > * m_cell_energy_reco
virtual StatusCode finalize() override
SG::ReadCondHandleKey< TileSamplingFraction > m_tileSamplingFractionKey
Name of TileSamplingFraction in condition store.
virtual StatusCode initialize() override
std::vector< float > * m_cell_phi
std::vector< float > * m_energy_active_nonem
std::vector< float > * m_cell_energy_active_total_corrected
virtual StatusCode execute() override
std::vector< Long64_t > * m_cell_identifier
std::vector< float > * m_energy_inactive_total
const CaloCell_ID * m_calo_id
std::vector< float > * m_energy_reco
std::vector< float > * m_cell_energy_inactive_total
std::vector< float > * m_energy_active_total_corrected
std::vector< float > * m_energy_inactive_esc
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146
GenEvent::particle_iterator begin(HepMC::GenEvent &e)
Definition GenEvent.h:621
STL namespace.