CMS 3D CMS Logo

SimHitsValidationHcal.cc
Go to the documentation of this file.
4 
5 //#define EDM_ML_DEBUG
6 
8  : g4Label_(ps.getParameter<std::string>("ModuleLabel")),
9  hcalHits_(ps.getParameter<std::string>("HitCollection")),
10  verbose_(ps.getParameter<bool>("Verbose")),
11  testNumber_(ps.getParameter<bool>("TestNumber")),
12  tok_hits_(consumes<edm::PCaloHitContainer>(edm::InputTag(g4Label_, hcalHits_))),
14  edm::LogVerbatim("HitsValidationHcal") << "Module Label: " << g4Label_ << " Hits: " << hcalHits_
15  << " TestNumbering " << testNumber_;
16 }
17 
19  const auto &pHRNDC = es.getData(tok_HRNDC_);
20  hcons = &pHRNDC;
25 
26  // Get Phi segmentation from geometry, use the max phi number so that all iphi
27  // values are included.
28 
29  int NphiMax = hcons->getNPhi(0);
30 
31  NphiMax = (hcons->getNPhi(1) > NphiMax ? hcons->getNPhi(1) : NphiMax);
32  NphiMax = (hcons->getNPhi(2) > NphiMax ? hcons->getNPhi(2) : NphiMax);
33  NphiMax = (hcons->getNPhi(3) > NphiMax ? hcons->getNPhi(3) : NphiMax);
34 
35  // Center the iphi bins on the integers
36  float iphi_min = 0.5;
37  float iphi_max = NphiMax + 0.5;
38  int iphi_bins = (int)(iphi_max - iphi_min);
39 
40  int iEtaHBMax = hcons->getEtaRange(0).second;
41  int iEtaHEMax = std::max(hcons->getEtaRange(1).second, 1);
42  int iEtaHFMax = hcons->getEtaRange(2).second;
43  int iEtaHOMax = hcons->getEtaRange(3).second;
44 
45  // Retain classic behavior, all plots have same ieta range.
46  // Comment out code to allow each subdetector to have its on range
47 
48  int iEtaMax = (iEtaHBMax > iEtaHEMax ? iEtaHBMax : iEtaHEMax);
49  iEtaMax = (iEtaMax > iEtaHFMax ? iEtaMax : iEtaHFMax);
50  iEtaMax = (iEtaMax > iEtaHOMax ? iEtaMax : iEtaHOMax);
51 
52  iEtaHBMax = iEtaMax;
53  iEtaHEMax = iEtaMax;
54  iEtaHFMax = iEtaMax;
55  iEtaHOMax = iEtaMax;
56 
57  // Give an empty bin around the subdet ieta range to make it clear that all
58  // ieta rings have been included float ieta_min_HB = -iEtaHBMax - 1.5; float
59  // ieta_max_HB = iEtaHBMax + 1.5; int ieta_bins_HB = (int) (ieta_max_HB -
60  // ieta_min_HB);
61 
62  // float ieta_min_HE = -iEtaHEMax - 1.5;
63  // float ieta_max_HE = iEtaHEMax + 1.5;
64  // int ieta_bins_HE = (int) (ieta_max_HE - ieta_min_HE);
65 
66  // float ieta_min_HF = -iEtaHFMax - 1.5;
67  // float ieta_max_HF = iEtaHFMax + 1.5;
68  // int ieta_bins_HF = (int) (ieta_max_HF - ieta_min_HF);
69 
70  // float ieta_min_HO = -iEtaHOMax - 1.5;
71  // float ieta_max_HO = iEtaHOMax + 1.5;
72  // int ieta_bins_HO = (int) (ieta_max_HO - ieta_min_HO);
73 
74 #ifdef EDM_ML_DEBUG
75  edm::LogVerbatim("HitsValidationHcal") << " Maximum Depths HB:" << maxDepthHB_ << " HE:" << maxDepthHE_
76  << " HO:" << maxDepthHO_ << " HF:" << maxDepthHF_;
77 #endif
78  std::vector<std::pair<std::string, std::string>> divisions = getHistogramTypes();
79 
80  edm::LogVerbatim("HitsValidationHcal") << "Booking the Histograms";
81  ib.setCurrentFolder("HcalHitsV/SimHitsValidationHcal");
82 
83  // Histograms for Hits
84 
86  for (unsigned int i = 0; i < types.size(); ++i) {
88  name = "HcalHitEta" + divisions[i].first;
89  title = "Hit energy as a function of eta tower index in " + divisions[i].second;
90  meHcalHitEta_.push_back(ib.book1D(name, title, limit.bins, limit.low, limit.high));
91 
92  name = "HcalHitTimeAEta" + divisions[i].first;
93  title = "Hit time as a function of eta tower index in" + divisions[i].second;
94  meHcalHitTimeEta_.push_back(ib.book1D(name, title, limit.bins, limit.low, limit.high));
95 
96  name = "HcalHitE25" + divisions[i].first;
97  title = "Energy in time window 0 to 25 for a tower in " + divisions[i].second;
98  meHcalEnergyl25_.push_back(
99  ib.book2D(name, title, limit.bins, limit.low, limit.high, iphi_bins, iphi_min, iphi_max));
100 
101  name = "HcalHitE50" + divisions[i].first;
102  title = "Energy in time window 0 to 50 for a tower in " + divisions[i].second;
103  meHcalEnergyl50_.push_back(
104  ib.book2D(name, title, limit.bins, limit.low, limit.high, iphi_bins, iphi_min, iphi_max));
105 
106  name = "HcalHitE100" + divisions[i].first;
107  title = "Energy in time window 0 to 100 for a tower in " + divisions[i].second;
108  meHcalEnergyl100_.push_back(
109  ib.book2D(name, title, limit.bins, limit.low, limit.high, iphi_bins, iphi_min, iphi_max));
110 
111  name = "HcalHitE250" + divisions[i].first;
112  title = "Energy in time window 0 to 250 for a tower in " + divisions[i].second;
113  meHcalEnergyl250_.push_back(
114  ib.book2D(name, title, limit.bins, limit.low, limit.high, iphi_bins, iphi_min, iphi_max));
115  }
116 
117  name = "Energy_HB";
118  meEnergy_HB = ib.book1D(name, name, 100, 0, 1);
119  name = "Energy_HE";
120  meEnergy_HE = ib.book1D(name, name, 100, 0, 1);
121  name = "Energy_HO";
122  meEnergy_HO = ib.book1D(name, name, 100, 0, 1);
123  name = "Energy_HF";
124  meEnergy_HF = ib.book1D(name, name, 100, 0, 50);
125 
126  name = "Time_HB";
127  metime_HB = ib.book1D(name, name, 300, -150, 150);
128  name = "Time_HE";
129  metime_HE = ib.book1D(name, name, 300, -150, 150);
130  name = "Time_HO";
131  metime_HO = ib.book1D(name, name, 300, -150, 150);
132  name = "Time_HF";
133  metime_HF = ib.book1D(name, name, 300, -150, 150);
134 
135  name = "Time_Enweighted_HB";
136  metime_enweighted_HB = ib.book1D(name, name, 300, -150, 150);
137  name = "Time_Enweighted_HE";
138  metime_enweighted_HE = ib.book1D(name, name, 300, -150, 150);
139  name = "Time_Enweighted_HO";
140  metime_enweighted_HO = ib.book1D(name, name, 300, -150, 150);
141  name = "Time_Enweighted_HF";
142  metime_enweighted_HF = ib.book1D(name, name, 300, -150, 150);
143 }
144 
146 #ifdef EDM_ML_DEBUG
147  edm::LogVerbatim("HitsValidationHcal") << "Run = " << e.id().run() << " Event = " << e.id().event();
148 #endif
149 
150  bool getHits = false;
151  const edm::Handle<edm::PCaloHitContainer> &hitsHcal = e.getHandle(tok_hits_);
152  if (hitsHcal.isValid())
153  getHits = true;
154 #ifdef EDM_ML_DEBUG
155  edm::LogVerbatim("HitsValidationHcal") << "HitsValidationHcal.: Input flags Hits " << getHits;
156 #endif
157  if (getHits) {
158  std::vector<PCaloHit> caloHits;
159  caloHits.insert(caloHits.end(), hitsHcal->begin(), hitsHcal->end());
160 #ifdef EDM_ML_DEBUG
161  edm::LogVerbatim("HitsValidationHcal") << "testNumber_:" << testNumber_;
162 #endif
163  if (testNumber_) {
164  for (unsigned int i = 0; i < caloHits.size(); ++i) {
165  unsigned int id_ = caloHits[i].id();
167  caloHits[i].setID(hid.rawId());
168 #ifdef EDM_ML_DEBUG
169  edm::LogVerbatim("HitsValidationHcal") << "Hit[" << i << "] " << hid;
170 #endif
171  }
172  }
173 #ifdef EDM_ML_DEBUG
174  edm::LogVerbatim("HitsValidationHcal") << "HitsValidationHcal: Hit buffer " << caloHits.size();
175 #endif
176  analyzeHits(caloHits);
177  }
178 }
179 
180 void SimHitsValidationHcal::analyzeHits(std::vector<PCaloHit> &hits) {
181  int nHit = hits.size();
182  double entotHB = 0, entotHE = 0, entotHF = 0, entotHO = 0;
183  double timetotHB = 0, timetotHE = 0, timetotHF = 0, timetotHO = 0;
184 
185  std::map<std::pair<HcalDetId, unsigned int>, energysum> map_try;
186  map_try.clear();
187  std::map<std::pair<HcalDetId, unsigned int>, energysum>::iterator itr;
188 
189  for (int i = 0; i < nHit; i++) {
190  double energy = hits[i].energy();
191  double time = hits[i].time();
192  HcalDetId id = HcalDetId(hits[i].id());
193  int itime = (int)(time);
194  int subdet = id.subdet();
195  int depth = id.depth();
196  int eta = id.ieta();
197  unsigned int dep = hits[i].depth();
198 
199  std::pair<int, int> types = histId(subdet, eta, depth, dep);
200  if (subdet == static_cast<int>(HcalBarrel)) {
201  entotHB += energy;
202  timetotHB += time;
203  } else if (subdet == static_cast<int>(HcalEndcap)) {
204  entotHE += energy;
205  timetotHE += time;
206  } else if (subdet == static_cast<int>(HcalOuter)) {
207  entotHO += energy;
208  timetotHO += time;
209  } else if (subdet == static_cast<int>(HcalForward)) {
210  entotHF += energy;
211  timetotHF += time;
212  }
213 
214  std::pair<HcalDetId, unsigned int> id0(id, dep);
215  energysum ensum;
216  if (map_try.count(id0) != 0)
217  ensum = map_try[id0];
218  if (itime < 250) {
219  ensum.e250 += energy;
220  if (itime < 100) {
221  ensum.e100 += energy;
222  if (itime < 50) {
223  ensum.e50 += energy;
224  if (itime < 25)
225  ensum.e25 += energy;
226  }
227  }
228  }
229  map_try[id0] = ensum;
230 
231 #ifdef EDM_ML_DEBUG
232  edm::LogVerbatim("HitsValidationHcal")
233  << "Hit[" << i << "] ID " << std::dec << " " << id << std::dec << " Det " << id.det() << " Sub " << subdet
234  << " depth " << depth << " depthX " << dep << " Eta " << eta << " Phi " << id.iphi() << " E " << energy
235  << " time " << time << " type " << types.first << " " << types.second;
236 #endif
237 
238  double etax = eta - 0.5;
239  if (eta < 0)
240  etax += 1;
241  if (types.first >= 0) {
242  meHcalHitEta_[types.first]->Fill(etax, energy);
243  meHcalHitTimeEta_[types.first]->Fill(etax, time);
244  }
245  if (types.second >= 0) {
246  meHcalHitEta_[types.second]->Fill(etax, energy);
247  meHcalHitTimeEta_[types.second]->Fill(etax, time);
248  }
249  }
250 
251  meEnergy_HB->Fill(entotHB);
252  meEnergy_HE->Fill(entotHE);
253  meEnergy_HF->Fill(entotHF);
254  meEnergy_HO->Fill(entotHO);
255 
256  metime_HB->Fill(timetotHB);
257  metime_HE->Fill(timetotHE);
258  metime_HF->Fill(timetotHF);
259  metime_HO->Fill(timetotHO);
260 
261  metime_enweighted_HB->Fill(timetotHB, entotHB);
262  metime_enweighted_HE->Fill(timetotHE, entotHE);
263  metime_enweighted_HF->Fill(timetotHF, entotHF);
264  metime_enweighted_HO->Fill(timetotHO, entotHO);
265 
266  for (itr = map_try.begin(); itr != map_try.end(); ++itr) {
267  HcalDetId id = (*itr).first.first;
268  energysum ensum = (*itr).second;
269  std::pair<int, int> types = histId((int)(id.subdet()), id.ieta(), id.depth(), (*itr).first.second);
270  int eta = id.ieta();
271  int phi = id.iphi();
272  double etax = eta - 0.5;
273  double phix = phi - 0.5;
274  if (types.first >= 0) {
275  meHcalEnergyl25_[types.first]->Fill(etax, phix, ensum.e25);
276  meHcalEnergyl50_[types.first]->Fill(etax, phix, ensum.e50);
277  meHcalEnergyl100_[types.first]->Fill(etax, phix, ensum.e100);
278  meHcalEnergyl250_[types.first]->Fill(etax, phix, ensum.e250);
279  }
280  if (types.second >= 0) {
281  meHcalEnergyl25_[types.second]->Fill(etax, phix, ensum.e25);
282  meHcalEnergyl50_[types.second]->Fill(etax, phix, ensum.e50);
283  meHcalEnergyl100_[types.second]->Fill(etax, phix, ensum.e100);
284  meHcalEnergyl250_[types.second]->Fill(etax, phix, ensum.e250);
285  }
286 
287 #ifdef EDM_ML_DEBUG
288  edm::LogVerbatim("HitsValidationHcal")
289  << " energy of tower =" << (*itr).first.first << " in time 25ns is == " << (*itr).second.e25
290  << " in time 25-50ns == " << (*itr).second.e50 << " in time 50-100ns == " << (*itr).second.e100
291  << " in time 100-250 ns == " << (*itr).second.e250;
292 #endif
293  }
294 }
295 
297  int bins;
298  std::pair<int, int> range;
299  double low, high;
300 
301  if (type.subdet == HcalBarrel) {
302  range = hcons->getEtaRange(0);
303  low = -range.second;
304  high = range.second;
305  bins = (high - low);
306  } else if (type.subdet == HcalEndcap) {
307  range = hcons->getEtaRange(1);
308  bins = range.second - range.first;
309  if (type.z == 1) {
310  low = range.first;
311  high = range.second;
312  } else {
313  low = -range.second;
314  high = -range.first;
315  }
316  } else if (type.subdet == HcalOuter) {
317  range = hcons->getEtaRange(3);
318  low = -range.second;
319  high = range.second;
320  bins = high - low;
321  } else if (type.subdet == HcalForward) {
322  range = hcons->getEtaRange(2);
323  bins = range.second - range.first;
324  if (type.z == 1) {
325  low = range.first;
326  high = range.second;
327  } else {
328  low = -range.second;
329  high = -range.first;
330  }
331  } else {
332  bins = 82;
333  low = -41;
334  high = 41;
335  }
336 #ifdef EDM_ML_DEBUG
337  edm::LogVerbatim("HitsValidationHcal") << "Subdetector:" << type.subdet << " z:" << type.z
338  << " range.first:" << range.first << " and second:" << range.second;
339  edm::LogVerbatim("HitsValidationHcal") << "bins: " << bins << " low:" << low << " high:" << high;
340 #endif
342 }
343 
344 std::pair<int, int> SimHitsValidationHcal::histId(int subdet, int eta, int depth, unsigned int dep) {
345  int id1(-1), id2(-1);
346  for (unsigned int k = 0; k < types.size(); ++k) {
347  if (subdet == HcalForward) {
348  if (subdet == (int)(types[k].subdet) && depth == types[k].depth1 && eta * types[k].z > 0 &&
349  dep == (unsigned int)(types[k].depth2)) {
350  id1 = k;
351  break;
352  }
353  } else if (subdet == HcalEndcap) {
354  if (subdet == (int)(types[k].subdet) && depth == types[k].depth1 && eta * types[k].z > 0) {
355  id1 = k;
356  break;
357  }
358  } else {
359  if (subdet == (int)(types[k].subdet) && depth == types[k].depth1) {
360  id1 = k;
361  break;
362  }
363  }
364  }
365  if (subdet == HcalForward)
366  depth += 2 * dep;
367  for (unsigned int k = 0; k < types.size(); ++k) {
368  if (types[k].subdet == HcalEmpty && types[k].depth1 == depth) {
369  id2 = k;
370  break;
371  }
372  }
373  return std::pair<int, int>(id1, id2);
374 }
375 
376 std::vector<std::pair<std::string, std::string>> SimHitsValidationHcal::getHistogramTypes() {
380 
381  std::vector<std::pair<std::string, std::string>> divisions;
382  // divisions and types need to be in sync
383  types.clear();
384  std::pair<std::string, std::string> names;
385  char name1[40], name2[40];
387  // first overall Hcal
388  for (int depth = 0; depth < maxDepth; ++depth) {
389  snprintf(name1, 40, "HC%d", depth);
390  snprintf(name2, 40, "HCAL depth%d", depth + 1);
391  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
393  divisions.push_back(names);
394  types.push_back(type);
395  }
396  // HB
397  for (int depth = 0; depth < maxDepthHB_; ++depth) {
398  snprintf(name1, 40, "HB%d", depth);
399  snprintf(name2, 40, "HB depth%d", depth + 1);
400  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
402  divisions.push_back(names);
403  types.push_back(type);
404  }
405  // HE
406  for (int depth = 0; depth < maxDepthHE_; ++depth) {
407  snprintf(name1, 40, "HE%d+z", depth);
408  snprintf(name2, 40, "HE +z depth%d", depth + 1);
409  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
411  divisions.push_back(names);
412  types.push_back(type);
413  snprintf(name1, 40, "HE%d-z", depth);
414  snprintf(name2, 40, "HE -z depth%d", depth + 1);
415  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
417  divisions.push_back(names);
418  types.push_back(type);
419  }
420  // HO
421  {
422  int depth = maxDepthHO_;
423  snprintf(name1, 40, "HO%d", depth);
424  snprintf(name2, 40, "HO depth%d", depth);
425  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
427  divisions.push_back(names);
428  types.push_back(type);
429  }
430  // HF (first absorber, then different types of abnormal hits)
431  std::string hfty1[4] = {"A", "W", "B", "J"};
432  std::string hfty2[4] = {"Absorber", "Window", "Bundle", "Jungle"};
433  int dept0[4] = {0, 1, 2, 3};
434  for (int k = 0; k < 4; ++k) {
435  for (int depth = 0; depth < maxDepthHF_; ++depth) {
436  snprintf(name1, 40, "HF%s%d+z", hfty1[k].c_str(), depth);
437  snprintf(name2, 40, "HF (%s) +z depth%d", hfty2[k].c_str(), depth + 1);
438  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
440  divisions.push_back(names);
441  types.push_back(type);
442  snprintf(name1, 40, "HF%s%d-z", hfty1[k].c_str(), depth);
443  snprintf(name2, 40, "HF (%s) -z depth%d", hfty2[k].c_str(), depth + 1);
444  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
446  divisions.push_back(names);
447  types.push_back(type);
448  }
449  }
450 
451  return divisions;
452 }
453 
456  desc.add<std::string>("ModuleLabel", "g4SimHits");
457  desc.add<std::string>("HitCollection", "HcalHits");
458  desc.add<bool>("Verbose", false);
459  desc.add<bool>("TestNumber", false);
460 
461  descriptions.addDefault(desc);
462 }
463 
SimHitsValidationHcal(const edm::ParameterSet &ps)
Log< level::Info, true > LogVerbatim
MonitorElement * meEnergy_HF
std::pair< int, int > getEtaRange(const int &i) const
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const HcalDDDRecConstants * hcons
std::vector< PCaloHit > PCaloHitContainer
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
int getNPhi(const int &type) const
MonitorElement * metime_enweighted_HO
void analyze(const edm::Event &e, const edm::EventSetup &c) override
const std::string names[nVars_]
MonitorElement * metime_enweighted_HB
void Fill(long long x)
std::vector< MonitorElement * > meHcalEnergyl25_
MonitorElement * meEnergy_HB
void addDefault(ParameterSetDescription const &psetDescription)
const std::string hcalHits_
std::vector< MonitorElement * > meHcalEnergyl100_
Transition
Definition: Transition.h:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< idType > types
MonitorElement * metime_enweighted_HF
DetId relabel(const uint32_t testId) const
int getMaxDepth(const int &type) const
MonitorElement * metime_enweighted_HE
std::vector< MonitorElement * > meHcalEnergyl250_
std::vector< MonitorElement * > meHcalHitEta_
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
MonitorElement * meEnergy_HE
std::vector< MonitorElement * > meHcalEnergyl50_
const edm::ESGetToken< HcalDDDRecConstants, HcalRecNumberingRecord > tok_HRNDC_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
std::vector< MonitorElement * > meHcalHitTimeEta_
std::pair< int, int > histId(int subdet, int eta, int depth, unsigned int dep)
const edm::EDGetTokenT< edm::PCaloHitContainer > tok_hits_
const std::string g4Label_
MonitorElement * meEnergy_HO
Definition: Run.h:45
ib
Definition: cuy.py:661
std::vector< std::pair< std::string, std::string > > getHistogramTypes()
void analyzeHits(std::vector< PCaloHit > &)