ATLAS Offline Software
Loading...
Searching...
No Matches
TruthHitAnalysis.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#include "TruthHitAnalysis.h"
6
7// Section of includes for Truth tests
10
11
12
14 ATH_MSG_DEBUG( "Initializing TruthHitAnalysis" );
15
16 // Grab the Ntuple and histogramming service for the tree
17 ATH_CHECK(m_readKey.initialize());
18
20 m_h_n_vert = new TH1D("h_n_vert","n_vert", 100,200, 1500);
21 m_h_n_vert->StatOverflows();
22 ATH_CHECK(histSvc()->regHist(m_path + m_h_n_vert->GetName(), m_h_n_vert));
23
24 m_h_n_part = new TH1D("h_n_part","n_part", 100,1000, 10000);
25 m_h_n_part->StatOverflows();
26 ATH_CHECK(histSvc()->regHist(m_path + m_h_n_part->GetName(), m_h_n_part));
27
28 m_h_n_vert_prim = new TH1D("h_n_vert_prim","n_vert prim", 100,0, 1000);
29 m_h_n_vert_prim->StatOverflows();
30 ATH_CHECK(histSvc()->regHist(m_path + m_h_n_vert_prim->GetName(), m_h_n_vert_prim));
31
32 m_h_n_part_prim = new TH1D("h_n_part_prim","n_part prim", 100,200, 1500);
33 m_h_n_part_prim->StatOverflows();
34 ATH_CHECK(histSvc()->regHist(m_path + m_h_n_part_prim->GetName(), m_h_n_part_prim));
35
36 m_h_n_vert_sec = new TH1D("h_n_vert_sec","n_vert sec", 100,0, 1000);
37 m_h_n_vert_sec->StatOverflows();
38 ATH_CHECK(histSvc()->regHist(m_path + m_h_n_vert_sec->GetName(), m_h_n_vert_sec));
39
40 m_h_n_part_sec = new TH1D("h_n_part_sec","n_part sec", 100,0, 5000);
41 m_h_n_part_sec->StatOverflows();
42 ATH_CHECK(histSvc()->regHist(m_path + m_h_n_part_sec->GetName(), m_h_n_part_sec));
43
44 m_h_vtx_x = new TH1D("h_vtx_x","vtx_x", 100,-1300, 1300);
45 m_h_vtx_x->StatOverflows();
46 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_x->GetName(), m_h_vtx_x));
47
48 m_h_vtx_y = new TH1D("h_vtx_y","vtx_y", 100,-1200, 1200);
49 m_h_vtx_y->StatOverflows();
50 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_y->GetName(), m_h_vtx_y));
51
52 m_h_vtx_z = new TH1D("h_vtx_z","vtx_z", 100,-5000, 5000);
53 m_h_vtx_z->StatOverflows();
54 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_z->GetName(), m_h_vtx_z));
55
56 m_h_vtx_r = new TH1D("h_vtx_r","vtx_r", 100,0, 1160);
57 m_h_vtx_r->StatOverflows();
58 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_r->GetName(), m_h_vtx_r));
59
60 m_h_vtx_prim_xy = new TH2D("h_vtx_prim_xy","vtx_prim_xy", 100,-100, 100, 100,-100, 100);
61 m_h_vtx_prim_xy->StatOverflows();
62 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_prim_xy->GetName(), m_h_vtx_prim_xy));
63
64 m_h_vtx_prim_zr = new TH2D("h_vtx_prim_zr","vtx_prim_zr", 100,-1500, 1500, 100,0, 150);
65 m_h_vtx_prim_zr->StatOverflows();
66 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_prim_zr->GetName(), m_h_vtx_prim_zr));
67
68 m_h_vtx_sec_xy = new TH2D("h_vtx_sec_xy","vtx_sec_xy", 100,-1200, 1200, 100,-1200, 1200);
69 m_h_vtx_sec_xy->StatOverflows();
70 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_sec_xy->GetName(), m_h_vtx_sec_xy));
71
72 m_h_vtx_sec_zr = new TH2D("h_vtx_sec_zr","vtx_sec_zr", 100,-6000, 6000, 100,0, 1160);
73 m_h_vtx_sec_zr->StatOverflows();
74 ATH_CHECK(histSvc()->regHist(m_path + m_h_vtx_sec_zr->GetName(), m_h_vtx_sec_zr));
75
76 m_h_n_generations = new TH1D("h_n_generations","h_generations", 100,0, 25);
77 m_h_n_generations->StatOverflows();
79
80 m_h_truth_px = new TH1D("h_truth_px","truth_px", 100,0, 4000);
81 m_h_truth_px->StatOverflows();
82 ATH_CHECK(histSvc()->regHist(m_path + m_h_truth_px->GetName(), m_h_truth_px));
83
84 m_h_truth_py = new TH1D("h_truth_py","truth_py", 100,0, 4000);
85 m_h_truth_py->StatOverflows();
86 ATH_CHECK(histSvc()->regHist(m_path + m_h_truth_py->GetName(), m_h_truth_py));
87
88 m_h_truth_pz = new TH1D("h_truth_pz","truth_pz", 100,0, 4000);
89 m_h_truth_pz->StatOverflows();
90 ATH_CHECK(histSvc()->regHist(m_path + m_h_truth_pz->GetName(), m_h_truth_pz));
91
92 m_h_truth_pt = new TH1D("h_truth_pt","truth_pt", 100,0, 4000);
93 m_h_truth_pt->StatOverflows();
94 ATH_CHECK(histSvc()->regHist(m_path + m_h_truth_pt->GetName(), m_h_truth_pt));
95
96 m_h_truth_eta = new TH1D("h_truth_eta","truth_eta", 50,-10, 10);
97 m_h_truth_eta->StatOverflows();
98 ATH_CHECK(histSvc()->regHist(m_path + m_h_truth_eta->GetName(), m_h_truth_eta));
99
100 m_h_truth_phi = new TH1D("h_truth_phi","truth_phi", 25,-3.1416, 3.1416);
101 m_h_truth_phi->StatOverflows();
102 ATH_CHECK(histSvc()->regHist(m_path + m_h_truth_phi->GetName(), m_h_truth_phi));
103
104 m_h_barcode = new TH1D("h_truth_barcode","truth_barcode", 100,0, 300000);
105 m_h_barcode->StatOverflows();
106 ATH_CHECK(histSvc()->regHist(m_path + m_h_barcode->GetName(), m_h_barcode));
107
108 m_h_part_status = new TH1D("h_part_status","part status", 100,0,50);
109 m_h_part_status->StatOverflows();
110 ATH_CHECK(histSvc()->regHist(m_path + m_h_part_status->GetName(), m_h_part_status));
111
112 m_h_part_pdgid = new TH1D("h_part_pdgid","part pdgid", 100,-5000, 5000);
113 m_h_part_pdgid->StatOverflows();
114 ATH_CHECK(histSvc()->regHist(m_path + m_h_part_pdgid->GetName(), m_h_part_pdgid));
115
116 m_h_part_pdgid_sec = new TH1D("h_part_pdgid_sec","part pdgid sec", 100,-5000, 5000);
117 m_h_part_pdgid_sec->StatOverflows();
119
120 m_h_part_eta = new TH1D("h_part_eta","part eta", 100,-10, 10);
121 m_h_part_eta->StatOverflows();
122 ATH_CHECK(histSvc()->regHist(m_path + m_h_part_eta->GetName(), m_h_part_eta));
123
124 m_h_part_phi = new TH1D("h_part_phi","part phi", 100,-3.2, 3.2);
125 m_h_part_phi->StatOverflows();
126 ATH_CHECK(histSvc()->regHist(m_path + m_h_part_phi->GetName(), m_h_part_phi));
127
128 m_h_part_p = new TH1D("h_part_p","part p", 100,0, 5000);
129 m_h_part_p->StatOverflows();
130 ATH_CHECK(histSvc()->regHist(m_path + m_h_part_p->GetName(), m_h_part_p));
131
133 m_tree = new TTree("Truth", "Truth");
134 std::string fullNtupleName = "/" + m_ntupleFileName + "/";
135 ATH_CHECK(histSvc()->regTree(fullNtupleName,m_tree));
136
137 m_tree->Branch("vtx_x", &m_vtx_x);
138 m_tree->Branch("vtx_y", &m_vtx_y);
139 m_tree->Branch("vtx_z", &m_vtx_z);
140 m_tree->Branch("vtx_r", &m_vtx_r);
141 m_tree->Branch("vtx_barcode", &m_vtx_barcode);
142 m_tree->Branch("truth_px", &m_truth_px);
143 m_tree->Branch("truth_py", &m_truth_py);
144 m_tree->Branch("truth_pz", &m_truth_pz);
145 m_tree->Branch("truth_pt", &m_truth_pt);
146 m_tree->Branch("truth_eta", &m_truth_eta);
147 m_tree->Branch("truth_phi", &m_truth_phi);
148 m_tree->Branch("barcode", &m_barcode);
149 m_tree->Branch("status", &m_status);
150 m_tree->Branch("pdg_id", &m_pdgid);
151
152 return StatusCode::SUCCESS;
153}
154
155
157 ATH_MSG_DEBUG( "In TruthHitAnalysis::execute()" );
158
159 m_vtx_x->clear();
160 m_vtx_y->clear();
161 m_vtx_z->clear();
162 m_vtx_r->clear();
163 m_vtx_barcode->clear();
164 m_truth_px->clear();
165 m_truth_py->clear();
166 m_truth_pz->clear();
167 m_truth_pt->clear();
168 m_truth_eta->clear();
169 m_truth_phi->clear();
170 m_barcode->clear();
171 m_status->clear();
172 m_pdgid->clear();
173
174 const EventContext& ctx{Gaudi::Hive::currentContext()};
175 const McEventCollection* mcCollection{nullptr};
176 ATH_CHECK(SG::get(mcCollection, m_readKey, ctx));
177
178
179 McEventCollection::const_iterator currentGenEventIter = mcCollection->begin();
180 if (currentGenEventIter != mcCollection->end()) {
181 int nvtx = 0;
182 int nvtx_sec=0;
183#ifdef HEPMC3
184 const auto &barcodes = (*currentGenEventIter)->attribute<HepMC::GenEventBarcodes> ("barcodes");
185 std::map<int,int> id_to_barcode_map;
186 if (barcodes) id_to_barcode_map = barcodes->id_to_barcode_map();
187 for (const auto& vtx: (*currentGenEventIter)->vertices()) {
188 int bcode = id_to_barcode_map[vtx->id()];
189#else
190 for (HepMC::GenEvent::vertex_const_iterator vtxit=(*currentGenEventIter)->vertices_begin(); vtxit!=(*currentGenEventIter)->vertices_end(); ++vtxit) {
191 auto vtx=*vtxit;
192 int bcode = HepMC::barcode(*vtx);
193#endif
194 double x = vtx->position().x();
195 double y = vtx->position().y();
196 double z = vtx->position().z();
197 double r = std::sqrt(x*x+y*y);
198 m_h_vtx_x->Fill(x);
199 m_h_vtx_y->Fill(y);
200 m_h_vtx_r->Fill(r);
201 m_h_vtx_z->Fill(z);
202
203 m_vtx_x->push_back(x);
204 m_vtx_y->push_back(y);
205 m_vtx_r->push_back(r);
206 m_vtx_z->push_back(z);
207 m_vtx_barcode->push_back(bcode);
208
209 if (!HepMC::is_simulation_vertex(vtx)) {
210 m_h_vtx_prim_xy->Fill(x,y);
211 m_h_vtx_prim_zr->Fill(z,r);
212 ++nvtx;
213 }
214 else {
215 m_h_vtx_sec_xy->Fill(x,y);
216 m_h_vtx_sec_zr->Fill(z,r);
217 ++nvtx_sec;
218 }
219 } //End iteration over vertices
220
221 m_h_n_vert->Fill(nvtx+nvtx_sec);
222 m_h_n_vert_prim->Fill(nvtx);
223 m_h_n_vert_sec->Fill(nvtx_sec);
224
225 int npart_prim=0;
226 int npart_sec=0;
227
228 for (auto currentGenParticle: *(*currentGenEventIter)) {
229 const HepMC::FourVector mom = currentGenParticle->momentum();
230#ifdef HEPMC3
231 int currentGenParticlebarcode = id_to_barcode_map[currentGenParticle->id()];
232#else
233 int currentGenParticlebarcode=HepMC::barcode(currentGenParticle);
234#endif
235 m_h_truth_px->Fill(mom.x());
236 m_h_truth_py->Fill(mom.y());
237 m_h_truth_pz->Fill(mom.z());
238 m_h_truth_pt->Fill(mom.perp());
239 m_h_truth_eta->Fill(mom.eta());
240 m_h_truth_phi->Fill(mom.phi());
241 m_h_barcode->Fill(currentGenParticlebarcode);
242 m_h_part_status->Fill(currentGenParticle->status());
243 m_truth_px->push_back(mom.x());
244 m_truth_py->push_back(mom.y());
245 m_truth_pz->push_back(mom.z());
246 m_truth_pt->push_back(mom.perp());
247 m_truth_eta->push_back(mom.eta());
248 m_truth_phi->push_back(mom.phi());
249 m_barcode->push_back(currentGenParticlebarcode);
250 m_status->push_back(currentGenParticle->status());
251
252 int pdg = currentGenParticle->pdg_id();
253 m_pdgid->push_back(pdg);
254
255 if (!HepMC::is_simulation_particle(currentGenParticle)) {
256 m_h_part_pdgid->Fill(pdg);
257 m_h_part_p->Fill(std::sqrt(mom.x()*mom.x()+mom.y()*mom.y()+mom.z()*mom.z()));
258 m_h_part_eta->Fill(mom.eta());
259 m_h_part_phi->Fill(mom.phi());
260 ++npart_prim;
261 m_h_n_generations->Fill(1);
262/* The conditions don't make any sense.
263 if (currentGenParticlebarcode < HepMC::PHOTOSMIN) {
264 m_h_n_generations->Fill(0);
265 }
266 else {
267 m_h_n_generations->Fill(1);
268 }
269*/
270 } //End simulation particle
271 else {
272 m_h_part_pdgid_sec->Fill(pdg);
273 ++npart_sec;
274 const int gen = HepMC::generations(currentGenParticle) + 2;
275 m_h_n_generations->Fill(gen);
276 }
277 } // End iteration over particles
278
279 m_h_n_part_prim->Fill(npart_prim);
280 m_h_n_part_sec->Fill(npart_sec);
281 m_h_n_part->Fill(npart_prim+npart_sec);
282 } // End mcCollection
283
284 m_tree->Fill();
285
286 return StatusCode::SUCCESS;
287}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_DEBUG(x)
#define y
#define x
#define z
const ServiceHandle< ITHistSvc > & histSvc() const
The standard THistSvc (for writing histograms and TTrees and more to a root file) Returns (kind of) a...
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
This defines the McEventCollection, which is really just an ObjectVector of McEvent objectsFile: Gene...
std::vector< float > * m_vtx_z
std::vector< float > * m_truth_pt
virtual StatusCode initialize() override final
std::vector< float > * m_truth_py
std::vector< float > * m_pdgid
std::vector< float > * m_vtx_y
Gaudi::Property< std::string > m_path
TH1 * m_h_n_vert
Some variables.
virtual StatusCode execute() override final
std::vector< float > * m_vtx_x
SG::ReadHandleKey< McEventCollection > m_readKey
std::vector< float > * m_vtx_barcode
std::vector< float > * m_truth_pz
Gaudi::Property< std::string > m_ntupleFileName
std::vector< float > * m_truth_eta
std::vector< float > * m_barcode
std::vector< float > * m_truth_phi
std::vector< float > * m_status
std::vector< float > * m_vtx_r
std::vector< float > * m_truth_px
int r
Definition globals.cxx:22
bool is_simulation_vertex(const T &v)
Method to establish if the vertex was created during simulation (TODO migrate to be based on status).
int barcode(const T *p)
Definition Barcode.h:16
bool is_simulation_particle(const T &p)
Method to establish if a particle (or barcode) was created during the simulation (TODO update to be s...
int generations(const T &p)
Method to return how many interactions a particle has undergone during simulation (TODO migrate to be...
const T * get(const ReadCondHandleKey< T > &key, const EventContext &ctx)
Convenience function to retrieve an object given a ReadCondHandleKey.