ATLAS Offline Software
SiHitAnalysis.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "SiHitAnalysis.h"
6 
7 #include "GeoAdaptors/GeoSiHit.h"
9 #include "AtlasHepMC/GenVertex.h"
10 #include "AtlasHepMC/GenParticle.h"
11 
12 #include "TH1.h"
13 #include "TH2.h"
14 #include "TTree.h"
15 
16 SiHitAnalysis::SiHitAnalysis(const std::string& name, ISvcLocator* pSvcLocator)
17  : AthAlgorithm(name, pSvcLocator)
18 {
19 }
20 
22 {
23  ATH_MSG_DEBUG("Initializing SiHitAnalysis");
24 
25  std::string detName("Pixel");
26  std::string ntupName("SiPixel");
27 
28  if (m_hitsContainerKey.key()=="PixelHits") {
29  detName = "Pixel";
30  ntupName = "SiPixel";
31  }
32  else if (m_hitsContainerKey.key()=="ITkPixelHits") {
33  detName = "ITkPixel";
34  ntupName = "SiITkPixel";
35  }
36  else if (m_hitsContainerKey.key()=="PLR_Hits") {
37  detName = "PLR";
38  ntupName = "SiPLR";
39  }
40  else if (m_hitsContainerKey.key()=="SCT_Hits") {
41  detName = "SCT";
42  ntupName = "SiSCT";
43  }
44  else if (m_hitsContainerKey.key()=="ITkStripHits") {
45  detName = "ITkStrip";
46  ntupName = "SiITkStrip";
47  }
48  else if (m_hitsContainerKey.key()=="BCMHits") {
49  detName = "BCM";
50  ntupName = "SiBCM";
51  }
52  else if (m_hitsContainerKey.key()=="BLMHits") {
53  detName = "BLM";
54  ntupName = "SiBLM";
55  }
56  else if (m_hitsContainerKey.key()=="HGTD_Hits") {
57  detName = "HGTD";
58  ntupName = "SiHGTD";
59  }
60  else {
61  ATH_MSG_ERROR("SiHitsAnalysis for " << m_hitsContainerKey.key() << " not supported!!!");
62  return StatusCode::FAILURE;
63  }
64 
65  // Grab the Ntuple and histogramming service for the tree
66  ATH_CHECK(m_thistSvc.retrieve());
67 
69  float bin_down = -600;
70  float bin_up = 600;
71  float radius_up = 600;
72  float radius_down = 200;
73  float z_max = 1200;
74  if (detName == "Pixel") {
75  bin_down = -170;
76  bin_up = 170;
77  radius_up = 170;
78  radius_down = 0;
79  } else if (detName == "ITkPixel") {
80  bin_down = -350;
81  bin_up = 350;
82  radius_up = 350;
83  radius_down = 0;
84  z_max = 3400;
85  } else if (detName == "ITkStrip") {
86  bin_down = -1100;
87  bin_up = 1100;
88  radius_up = 1100;
89  radius_down = 0;
90  z_max = 3400;
91  } else if (detName == "HGTD") {
92  bin_down = -1000;
93  bin_up = 1000;
94  radius_up = 700;
95  radius_down = 0;
96  z_max = 4000;
97  } else if (detName == "PLR") {
98  bin_down = -125;
99  bin_up = 125;
100  radius_up = 125;
101  radius_down = 0;
102  z_max = 3000;
103  }
104  m_h_hits_x = new TH1D(("h_"+detName+"_x").c_str(),("h_"+detName+"_x").c_str(), 100,bin_down, bin_up);
105  m_h_hits_x->StatOverflows();
106 
107  m_h_hits_y = new TH1D(("h_"+detName+"_y").c_str(), ("h_"+detName+"_y").c_str(), 100,bin_down,bin_up);
108  m_h_hits_y->StatOverflows();
109 
110  m_h_hits_z = new TH1D(("h_"+detName+"_z").c_str(), ("h_"+detName+"_z").c_str(), 200,-z_max,z_max);
111  m_h_hits_z->StatOverflows();
112 
113  m_h_hits_r = new TH1D(("h_"+detName+"_r").c_str(), ("h_"+detName+"_r").c_str(), 100,radius_down,radius_up);
114  m_h_hits_r->StatOverflows();
115 
116  m_h_xy = new TH2D(("h_"+detName+"_xy").c_str(), ("h_"+detName+"_xy").c_str(), 100,bin_down,bin_up,100, bin_down, bin_up);
117  m_h_xy->StatOverflows();
118 
119  m_h_zr = new TH2D(("h_"+detName+"_zr").c_str(), ("h_"+detName+"_zr").c_str(), 100,-z_max, z_max, 100, radius_down, radius_up);
120  m_h_zr->StatOverflows();
121 
122  m_h_hits_time = new TH1D(("h_"+detName+"_time").c_str(), ("h_"+detName+"_time").c_str(), 100,0,500);
123  m_h_hits_time->StatOverflows();
124 
125  m_h_hits_eloss = new TH1D(("h_"+detName+"_eloss").c_str(), ("h_"+detName+"_eloss").c_str(), 100,0,50);
126  m_h_hits_eloss->StatOverflows();
127 
128  m_h_hits_step = new TH1D(("h_"+detName+"_step").c_str(), ("h_"+detName+"_step").c_str(), 100,0,50);
129  m_h_hits_step->StatOverflows();
130 
131  m_h_hits_barcode = new TH1D(("h_"+detName+"_barcode").c_str(), ("h_"+detName+"_barcode").c_str(), 200,0,250000);
132  m_h_hits_barcode->StatOverflows();
133 
134  m_h_time_eloss = new TH2D(("h_"+detName+"_time_eloss").c_str(), ("h_"+detName+" Eloss vs. time").c_str(),100, 0,500,100,0,50);
135  m_h_time_eloss->StatOverflows();
136 
137  m_h_z_eloss = new TH2D(("h_"+detName+"_z_eloss").c_str(), ("h_"+detName+" Eloss vs. z").c_str(),100, -z_max,z_max,100,0,50);
138  m_h_z_eloss->StatOverflows();
139 
140  m_h_r_eloss = new TH2D(("h_"+detName+"_r_eloss").c_str(), ("h_"+detName+ " Eloss vs. r").c_str(),100, radius_down,radius_down,100,0,50);
141  m_h_r_eloss->StatOverflows();
142 
143  m_h_barrel_endcap = new TH1D(("h_"+detName+"_barrel_endcap").c_str(), ("h_"+detName+ " barrel/endcap").c_str(), 10, -5, 5);
144  m_h_barrel_endcap->StatOverflows();
145 
146  m_h_layer_disk = new TH1D(("h_"+detName+"_layer_disk").c_str(), ("h_"+detName+ " layer/disk").c_str(), 10, 0, 10);
147  m_h_layer_disk->StatOverflows();
148 
149  m_h_module_eta = new TH1D(("h_"+detName+"_module_eta").c_str(), ("h_"+detName+ " module in #eta").c_str(), 100, 0, 100);
150  m_h_module_eta->StatOverflows();
151 
152  m_h_module_phi = new TH1D(("h_"+detName+"_module_phi").c_str(), ("h_"+detName+ " module in #phi").c_str(), 100, 0, 100);
153  m_h_module_phi->StatOverflows();
154 
155  ATH_CHECK(m_thistSvc.retrieve());
156 
157  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_hits_x->GetName(), m_h_hits_x));
158  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_hits_y->GetName(), m_h_hits_y));
159  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_hits_z->GetName(), m_h_hits_z));
160  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_hits_r->GetName(), m_h_hits_r));
161  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_xy->GetName(), m_h_xy));
162  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_zr->GetName(), m_h_zr));
163  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_hits_time->GetName(), m_h_hits_time));
165  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_hits_step->GetName(), m_h_hits_step));
167 
168  //To be filled only when the expert mode is on.
169  if (m_expert.value()) {
171  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_z_eloss->GetName(), m_h_z_eloss));
172  ATH_CHECK(m_thistSvc->regHist(m_histPath + m_h_r_eloss->GetName(), m_h_r_eloss));
173  }
174 
179 
180  // Special shared ITk histograms
181  if (detName.find("ITk") != std::string::npos) {
182  std::string xy_name = "h_ITk_xy";
183  auto xy = std::make_unique<TH2D>(xy_name.c_str(), xy_name.c_str(), 2200, -1100, 1100, 2200, -1100, 1100);
184  xy->StatOverflows();
185  ATH_CHECK(m_thistSvc->regShared(m_histPath + xy_name, std::move(xy), m_h_xy_shared));
186 
187  std::string zr_name = "h_ITk_zr";
188  auto zr = std::make_unique<TH2D>(zr_name.c_str(), zr_name.c_str(), 6800, -3400, 3400, 1100, 0, 1100);
189  zr->StatOverflows();
190  ATH_CHECK(m_thistSvc->regShared(m_histPath + zr_name, std::move(zr), m_h_zr_shared));
191  }
192 
194  m_tree = new TTree(ntupName.c_str(), ntupName.c_str());
195  std::string fullNtupleName = "/" + m_ntuplePath + "/" + detName;
196  ATH_CHECK(m_thistSvc->regTree(fullNtupleName,m_tree));
197 
198  if (m_tree){
199  m_tree->Branch((detName+"_x").c_str(), &m_hits_x);
200  m_tree->Branch((detName+"_y").c_str(), &m_hits_y);
201  m_tree->Branch((detName+"_z").c_str(), &m_hits_z);
202  m_tree->Branch((detName+"_r").c_str(), &m_hits_r);
203  m_tree->Branch((detName+"_time").c_str(), &m_hits_time);
204  m_tree->Branch((detName+"_eloss").c_str(), &m_hits_eloss);
205  m_tree->Branch((detName+"_step").c_str(), &m_hits_step);
206  m_tree->Branch((detName+"_barcode").c_str(), &m_hits_barcode);
207  if (m_extraTruthBranches) {
208  m_tree->Branch((detName+"_pdgId").c_str(), &m_hits_pdgId);
209  m_tree->Branch((detName+"_pT").c_str(), &m_hits_pT);
210  m_tree->Branch((detName+"_eta").c_str(), &m_hits_eta);
211  m_tree->Branch((detName+"_phi").c_str(), &m_hits_phi);
212  m_tree->Branch((detName+"_prodVtx_x").c_str(), &m_hits_prodVtx_x);
213  m_tree->Branch((detName+"_prodVtx_y").c_str(), &m_hits_prodVtx_y);
214  m_tree->Branch((detName+"_prodVtx_z").c_str(), &m_hits_prodVtx_z);
215  }
216 
217  m_tree->Branch((detName+"_barrel_endcap").c_str(), &m_barrel_endcap);
218  m_tree->Branch((detName+"_layer_disk").c_str(), &m_layer_disk);
219  m_tree->Branch((detName+"_module_eta").c_str(), &m_module_eta);
220  m_tree->Branch((detName+"_module_phi").c_str(), &m_module_phi);
221  }
222  else {
223  ATH_MSG_ERROR("No tree found!");
224  }
225 
226  // Initialize keys
227  ATH_CHECK(m_hitsContainerKey.initialize());
228 
229  return StatusCode::SUCCESS;
230 }
231 
232 
234 {
235  ATH_MSG_DEBUG("In SiHitAnalysis::execute()");
236 
237  m_hits_x->clear();
238  m_hits_y->clear();
239  m_hits_z->clear();
240  m_hits_r->clear();
241  m_hits_time->clear();
242  m_hits_eloss->clear();
243  m_hits_step->clear();
244  m_hits_barcode->clear();
245  if (m_extraTruthBranches) {
246  m_hits_pdgId->clear();
247  m_hits_pT->clear();
248  m_hits_eta->clear();
249  m_hits_phi->clear();
250  m_hits_prodVtx_x->clear();
251  m_hits_prodVtx_y->clear();
252  m_hits_prodVtx_z->clear();
253  }
254 
255  m_barrel_endcap->clear();
256  m_layer_disk->clear();
257  m_module_eta->clear();
258  m_module_phi->clear();
259 
261  if (hitCollection.isValid()) {
262  ATH_MSG_INFO("Event contains " << hitCollection->size() << " entries in " << m_hitsContainerKey.key());
263  for (const SiHit &hit : *hitCollection) {
264  GeoSiHit ghit(hit);
265  HepGeom::Point3D<double> p = ghit.getGlobalPosition();
266  m_h_hits_x->Fill(p.x());
267  m_h_hits_y->Fill(p.y());
268  m_h_hits_z->Fill(p.z());
269  m_h_hits_r->Fill(p.perp());
270  m_h_xy->Fill(p.x(), p.y());
271  m_h_zr->Fill(p.z(), p.perp());
272  if (m_h_xy_shared.get() != nullptr) {
273  m_h_xy_shared->Fill(p.x(), p.y());
274  }
275  if (m_h_zr_shared.get() != nullptr) {
276  m_h_zr_shared->Fill(p.z(), p.perp());
277  }
278  m_h_hits_eloss->Fill(hit.energyLoss());
279  m_h_hits_time->Fill(hit.meanTime());
280  double step_length = (hit.localStartPosition() - hit.localEndPosition()).mag();
281  m_h_hits_step->Fill(step_length);
282  m_h_hits_barcode->Fill(HepMC::barcode(hit.particleLink()));
283 
284  if (m_expert.value()) {
285  m_h_time_eloss->Fill(hit.meanTime(), hit.energyLoss());
286  if (hit.getBarrelEndcap() == 0) {
287  m_h_z_eloss->Fill(p.z(), hit.energyLoss());
288  }
289  else {
290  m_h_r_eloss->Fill(p.perp(), hit.energyLoss());
291  }
292  }
293 
294  m_h_barrel_endcap->Fill(hit.getBarrelEndcap());
295  m_h_layer_disk->Fill(hit.getLayerDisk());
296  m_h_module_eta->Fill(hit.getEtaModule());
297  m_h_module_phi->Fill(hit.getPhiModule());
298 
299  m_hits_x->push_back(p.x());
300  m_hits_y->push_back(p.y());
301  m_hits_z->push_back(p.z());
302  m_hits_r->push_back(p.perp());
303  m_hits_eloss->push_back(hit.energyLoss());
304  m_hits_time->push_back(hit.meanTime());
305  m_hits_step->push_back(step_length);
306  m_hits_barcode->push_back(HepMC::barcode(hit.particleLink()));
307  if (m_extraTruthBranches) {
308  const auto& HMPL = hit.particleLink();
309  if (HMPL.isValid()) {
310  auto part = HMPL.cptr();
311  m_hits_pdgId->push_back(part->pdg_id());
312  m_hits_pT->push_back(part->momentum().perp());
313  m_hits_eta->push_back(part->momentum().eta());
314  m_hits_phi->push_back(part->momentum().phi());
315  m_hits_prodVtx_x->push_back(part->production_vertex()->position().x());
316  m_hits_prodVtx_y->push_back(part->production_vertex()->position().y());
317  m_hits_prodVtx_z->push_back(part->production_vertex()->position().z());
318  }
319  else {
320  m_hits_pdgId->push_back(-9999);
321  m_hits_pT->push_back(-9999);
322  m_hits_eta->push_back(-9999);
323  m_hits_phi->push_back(-9999);
324  m_hits_prodVtx_x->push_back(-9999);
325  m_hits_prodVtx_y->push_back(-9999);
326  m_hits_prodVtx_z->push_back(-9999);
327  }
328  }
329 
330  m_barrel_endcap->push_back(hit.getBarrelEndcap());
331  m_layer_disk->push_back(hit.getLayerDisk());
332  m_module_eta->push_back(hit.getEtaModule());
333  m_module_phi->push_back(hit.getPhiModule());
334  } // End while hits
335  } else { // End statuscode success upon retrieval of hits
336  ATH_MSG_ERROR("Invalid collection");
337  return StatusCode::FAILURE;
338  }
339 
340  if (m_tree != nullptr) m_tree->Fill();
341 
342  return StatusCode::SUCCESS;
343 }
LArG4FSStartPointFilter.part
part
Definition: LArG4FSStartPointFilter.py:21
SiHitAnalysis::m_expert
Gaudi::Property< bool > m_expert
Definition: SiHitAnalysis.h:82
SiHitAnalysis::m_h_hits_y
TH1 * m_h_hits_y
Definition: SiHitAnalysis.h:38
SiHitAnalysis::m_hits_x
std::vector< float > * m_hits_x
Definition: SiHitAnalysis.h:58
SiHitAnalysis::m_module_phi
std::vector< int > * m_module_phi
Definition: SiHitAnalysis.h:75
SiHitAnalysis::m_hits_y
std::vector< float > * m_hits_y
Definition: SiHitAnalysis.h:59
SiHitAnalysis::m_hits_eta
std::vector< float > * m_hits_eta
Definition: SiHitAnalysis.h:68
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SiHitAnalysis::m_hits_pT
std::vector< float > * m_hits_pT
Definition: SiHitAnalysis.h:67
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
SiHitAnalysis::m_barrel_endcap
std::vector< int > * m_barrel_endcap
Definition: SiHitAnalysis.h:73
GenVertex.h
SiHitAnalysis::m_hits_prodVtx_y
std::vector< float > * m_hits_prodVtx_y
Definition: SiHitAnalysis.h:71
SiHitAnalysis::m_thistSvc
ServiceHandle< ITHistSvc > m_thistSvc
Definition: SiHitAnalysis.h:85
SiHitAnalysis::m_h_hits_eloss
TH1 * m_h_hits_eloss
Definition: SiHitAnalysis.h:46
SiHitAnalysis::m_module_eta
std::vector< int > * m_module_eta
Definition: SiHitAnalysis.h:76
SiHitAnalysis::m_hits_z
std::vector< float > * m_hits_z
Definition: SiHitAnalysis.h:60
SiHitAnalysis::m_extraTruthBranches
Gaudi::Property< bool > m_extraTruthBranches
Definition: SiHitAnalysis.h:83
SiHitAnalysis::m_hits_barcode
std::vector< float > * m_hits_barcode
Definition: SiHitAnalysis.h:65
GenParticle.h
GeoSiHit
Definition: GeoSiHit.h:30
SiHit
Definition: SiHit.h:19
SiHitAnalysis::m_hits_pdgId
std::vector< float > * m_hits_pdgId
Definition: SiHitAnalysis.h:66
SiHitAnalysis::m_h_barrel_endcap
TH1 * m_h_barrel_endcap
Definition: SiHitAnalysis.h:52
SiHitAnalysis::m_hitsContainerKey
SG::ReadHandleKey< SiHitCollection > m_hitsContainerKey
Definition: SiHitAnalysis.h:78
SiHitAnalysis::SiHitAnalysis
SiHitAnalysis(const std::string &name, ISvcLocator *pSvcLocator)
Definition: SiHitAnalysis.cxx:16
SiHitAnalysis::m_h_r_eloss
TH2 * m_h_r_eloss
Definition: SiHitAnalysis.h:51
SiHitAnalysis::m_hits_phi
std::vector< float > * m_hits_phi
Definition: SiHitAnalysis.h:69
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
GeoSiHit.h
SiHitAnalysis::m_h_hits_x
TH1 * m_h_hits_x
Some variables.
Definition: SiHitAnalysis.h:37
SiHitAnalysis.h
SiHitAnalysis::m_hits_r
std::vector< float > * m_hits_r
Definition: SiHitAnalysis.h:61
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
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
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
SiHitAnalysis::execute
virtual StatusCode execute() override
Definition: SiHitAnalysis.cxx:233
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SiHitAnalysis::m_h_hits_time
TH1 * m_h_hits_time
Definition: SiHitAnalysis.h:45
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
SiHitAnalysis::m_h_module_phi
TH1 * m_h_module_phi
Definition: SiHitAnalysis.h:54
SiHitAnalysis::m_h_hits_z
TH1 * m_h_hits_z
Definition: SiHitAnalysis.h:39
SiHitAnalysis::initialize
virtual StatusCode initialize() override
Definition: SiHitAnalysis.cxx:21
SiHitAnalysis::m_hits_prodVtx_z
std::vector< float > * m_hits_prodVtx_z
Definition: SiHitAnalysis.h:72
SiHitAnalysis::m_hits_eloss
std::vector< float > * m_hits_eloss
Definition: SiHitAnalysis.h:63
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
SiHitAnalysis::m_tree
TTree * m_tree
Definition: SiHitAnalysis.h:57
SiHitAnalysis::m_h_hits_barcode
TH1 * m_h_hits_barcode
Definition: SiHitAnalysis.h:48
SiHitAnalysis::m_h_hits_r
TH1 * m_h_hits_r
Definition: SiHitAnalysis.h:40
SiHitAnalysis::m_hits_time
std::vector< float > * m_hits_time
Definition: SiHitAnalysis.h:62
SiHitAnalysis::m_layer_disk
std::vector< int > * m_layer_disk
Definition: SiHitAnalysis.h:74
SiHitAnalysis::m_h_zr_shared
LockedHandle< TH2 > m_h_zr_shared
Definition: SiHitAnalysis.h:44
SiHitAnalysis::m_h_xy_shared
LockedHandle< TH2 > m_h_xy_shared
Definition: SiHitAnalysis.h:43
SiHitAnalysis::m_h_zr
TH2 * m_h_zr
Definition: SiHitAnalysis.h:42
GeoSiHit::getGlobalPosition
HepGeom::Point3D< double > getGlobalPosition() const
SiHitAnalysis::m_hits_prodVtx_x
std::vector< float > * m_hits_prodVtx_x
Definition: SiHitAnalysis.h:70
AtlasHitsVector::size
size_type size() const
Definition: AtlasHitsVector.h:143
SiHitAnalysis::m_h_xy
TH2 * m_h_xy
Definition: SiHitAnalysis.h:41
python.LArCondContChannels.detName
detName
Definition: LArCondContChannels.py:665
SiHitAnalysis::m_histPath
Gaudi::Property< std::string > m_histPath
Definition: SiHitAnalysis.h:80
SiHitAnalysis::m_hits_step
std::vector< float > * m_hits_step
Definition: SiHitAnalysis.h:64
mag
Scalar mag() const
mag method
Definition: AmgMatrixBasePlugin.h:26
SiHitAnalysis::m_h_time_eloss
TH2 * m_h_time_eloss
Definition: SiHitAnalysis.h:49
SiHitAnalysis::m_h_layer_disk
TH1 * m_h_layer_disk
Definition: SiHitAnalysis.h:53
SiHitAnalysis::m_ntuplePath
Gaudi::Property< std::string > m_ntuplePath
Definition: SiHitAnalysis.h:81
SiHitAnalysis::m_h_z_eloss
TH2 * m_h_z_eloss
Definition: SiHitAnalysis.h:50
SiHitAnalysis::m_h_hits_step
TH1 * m_h_hits_step
Definition: SiHitAnalysis.h:47
SiHitAnalysis::m_h_module_eta
TH1 * m_h_module_eta
Definition: SiHitAnalysis.h:55