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  int nHB = 0, nHE = 0, nHO = 0, nHF = 0;
185 
186  std::map<std::pair<HcalDetId, unsigned int>, energysum> map_try;
187  map_try.clear();
188  std::map<std::pair<HcalDetId, unsigned int>, energysum>::iterator itr;
189 
190  for (int i = 0; i < nHit; i++) {
191  double energy = hits[i].energy();
192  double time = hits[i].time();
193  HcalDetId id = HcalDetId(hits[i].id());
194  int itime = (int)(time);
195  int subdet = id.subdet();
196  int depth = id.depth();
197  int eta = id.ieta();
198  unsigned int dep = hits[i].depth();
199 
200  std::pair<int, int> types = histId(subdet, eta, depth, dep);
201  if (subdet == static_cast<int>(HcalBarrel)) {
202  entotHB += energy;
203  timetotHB += time;
204  nHB++;
205  } else if (subdet == static_cast<int>(HcalEndcap)) {
206  entotHE += energy;
207  timetotHE += time;
208  nHE++;
209  } else if (subdet == static_cast<int>(HcalOuter)) {
210  entotHO += energy;
211  timetotHO += time;
212  nHO++;
213  } else if (subdet == static_cast<int>(HcalForward)) {
214  entotHF += energy;
215  timetotHF += time;
216  nHF++;
217  }
218 
219  std::pair<HcalDetId, unsigned int> id0(id, dep);
220  energysum ensum;
221  if (map_try.count(id0) != 0)
222  ensum = map_try[id0];
223  if (itime < 250) {
224  ensum.e250 += energy;
225  if (itime < 100) {
226  ensum.e100 += energy;
227  if (itime < 50) {
228  ensum.e50 += energy;
229  if (itime < 25)
230  ensum.e25 += energy;
231  }
232  }
233  }
234  map_try[id0] = ensum;
235 
236 #ifdef EDM_ML_DEBUG
237  edm::LogVerbatim("HitsValidationHcal")
238  << "Hit[" << i << "] ID " << std::dec << " " << id << std::dec << " Det " << id.det() << " Sub " << subdet
239  << " depth " << depth << " depthX " << dep << " Eta " << eta << " Phi " << id.iphi() << " E " << energy
240  << " time " << time << " type " << types.first << " " << types.second;
241 #endif
242 
243  double etax = eta - 0.5;
244  if (eta < 0)
245  etax += 1;
246  if (types.first >= 0) {
247  meHcalHitEta_[types.first]->Fill(etax, energy);
248  meHcalHitTimeEta_[types.first]->Fill(etax, time);
249  }
250  if (types.second >= 0) {
251  meHcalHitEta_[types.second]->Fill(etax, energy);
252  meHcalHitTimeEta_[types.second]->Fill(etax, time);
253  }
254  }
255 
256  meEnergy_HB->Fill(entotHB);
257  meEnergy_HE->Fill(entotHE);
258  meEnergy_HF->Fill(entotHF);
259  meEnergy_HO->Fill(entotHO);
260 
261  metime_HB->Fill(timetotHB);
262  metime_HE->Fill(timetotHE);
263  metime_HF->Fill(timetotHF);
264  metime_HO->Fill(timetotHO);
265 
266  metime_enweighted_HB->Fill(timetotHB, entotHB);
267  metime_enweighted_HE->Fill(timetotHE, entotHE);
268  metime_enweighted_HF->Fill(timetotHF, entotHF);
269  metime_enweighted_HO->Fill(timetotHO, entotHO);
270 
271  for (itr = map_try.begin(); itr != map_try.end(); ++itr) {
272  HcalDetId id = (*itr).first.first;
273  energysum ensum = (*itr).second;
274  std::pair<int, int> types = histId((int)(id.subdet()), id.ieta(), id.depth(), (*itr).first.second);
275  int eta = id.ieta();
276  int phi = id.iphi();
277  double etax = eta - 0.5;
278  double phix = phi - 0.5;
279  if (types.first >= 0) {
280  meHcalEnergyl25_[types.first]->Fill(etax, phix, ensum.e25);
281  meHcalEnergyl50_[types.first]->Fill(etax, phix, ensum.e50);
282  meHcalEnergyl100_[types.first]->Fill(etax, phix, ensum.e100);
283  meHcalEnergyl250_[types.first]->Fill(etax, phix, ensum.e250);
284  }
285  if (types.second >= 0) {
286  meHcalEnergyl25_[types.second]->Fill(etax, phix, ensum.e25);
287  meHcalEnergyl50_[types.second]->Fill(etax, phix, ensum.e50);
288  meHcalEnergyl100_[types.second]->Fill(etax, phix, ensum.e100);
289  meHcalEnergyl250_[types.second]->Fill(etax, phix, ensum.e250);
290  }
291 
292 #ifdef EDM_ML_DEBUG
293  edm::LogVerbatim("HitsValidationHcal")
294  << " energy of tower =" << (*itr).first.first << " in time 25ns is == " << (*itr).second.e25
295  << " in time 25-50ns == " << (*itr).second.e50 << " in time 50-100ns == " << (*itr).second.e100
296  << " in time 100-250 ns == " << (*itr).second.e250;
297 #endif
298  }
299 }
300 
302  int bins;
303  std::pair<int, int> range;
304  double low, high;
305 
306  if (type.subdet == HcalBarrel) {
307  range = hcons->getEtaRange(0);
308  low = -range.second;
309  high = range.second;
310  bins = (high - low);
311  } else if (type.subdet == HcalEndcap) {
312  range = hcons->getEtaRange(1);
313  bins = range.second - range.first;
314  if (type.z == 1) {
315  low = range.first;
316  high = range.second;
317  } else {
318  low = -range.second;
319  high = -range.first;
320  }
321  } else if (type.subdet == HcalOuter) {
322  range = hcons->getEtaRange(3);
323  low = -range.second;
324  high = range.second;
325  bins = high - low;
326  } else if (type.subdet == HcalForward) {
327  range = hcons->getEtaRange(2);
328  bins = range.second - range.first;
329  if (type.z == 1) {
330  low = range.first;
331  high = range.second;
332  } else {
333  low = -range.second;
334  high = -range.first;
335  }
336  } else {
337  bins = 82;
338  low = -41;
339  high = 41;
340  }
341 #ifdef EDM_ML_DEBUG
342  edm::LogVerbatim("HitsValidationHcal") << "Subdetector:" << type.subdet << " z:" << type.z
343  << " range.first:" << range.first << " and second:" << range.second;
344  edm::LogVerbatim("HitsValidationHcal") << "bins: " << bins << " low:" << low << " high:" << high;
345 #endif
347 }
348 
349 std::pair<int, int> SimHitsValidationHcal::histId(int subdet, int eta, int depth, unsigned int dep) {
350  int id1(-1), id2(-1);
351  for (unsigned int k = 0; k < types.size(); ++k) {
352  if (subdet == HcalForward) {
353  if (subdet == (int)(types[k].subdet) && depth == types[k].depth1 && eta * types[k].z > 0 &&
354  dep == (unsigned int)(types[k].depth2)) {
355  id1 = k;
356  break;
357  }
358  } else if (subdet == HcalEndcap) {
359  if (subdet == (int)(types[k].subdet) && depth == types[k].depth1 && eta * types[k].z > 0) {
360  id1 = k;
361  break;
362  }
363  } else {
364  if (subdet == (int)(types[k].subdet) && depth == types[k].depth1) {
365  id1 = k;
366  break;
367  }
368  }
369  }
370  if (subdet == HcalForward)
371  depth += 2 * dep;
372  for (unsigned int k = 0; k < types.size(); ++k) {
373  if (types[k].subdet == HcalEmpty && types[k].depth1 == depth) {
374  id2 = k;
375  break;
376  }
377  }
378  return std::pair<int, int>(id1, id2);
379 }
380 
381 std::vector<std::pair<std::string, std::string>> SimHitsValidationHcal::getHistogramTypes() {
385 
386  std::vector<std::pair<std::string, std::string>> divisions;
387  // divisions and types need to be in sync
388  types.clear();
389  std::pair<std::string, std::string> names;
390  char name1[40], name2[40];
392  // first overall Hcal
393  for (int depth = 0; depth < maxDepth; ++depth) {
394  snprintf(name1, 40, "HC%d", depth);
395  snprintf(name2, 40, "HCAL depth%d", depth + 1);
396  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
398  divisions.push_back(names);
399  types.push_back(type);
400  }
401  // HB
402  for (int depth = 0; depth < maxDepthHB_; ++depth) {
403  snprintf(name1, 40, "HB%d", depth);
404  snprintf(name2, 40, "HB depth%d", depth + 1);
405  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
407  divisions.push_back(names);
408  types.push_back(type);
409  }
410  // HE
411  for (int depth = 0; depth < maxDepthHE_; ++depth) {
412  snprintf(name1, 40, "HE%d+z", depth);
413  snprintf(name2, 40, "HE +z depth%d", depth + 1);
414  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
416  divisions.push_back(names);
417  types.push_back(type);
418  snprintf(name1, 40, "HE%d-z", depth);
419  snprintf(name2, 40, "HE -z depth%d", depth + 1);
420  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
422  divisions.push_back(names);
423  types.push_back(type);
424  }
425  // HO
426  {
427  int depth = maxDepthHO_;
428  snprintf(name1, 40, "HO%d", depth);
429  snprintf(name2, 40, "HO depth%d", depth);
430  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
432  divisions.push_back(names);
433  types.push_back(type);
434  }
435  // HF (first absorber, then different types of abnormal hits)
436  std::string hfty1[4] = {"A", "W", "B", "J"};
437  std::string hfty2[4] = {"Absorber", "Window", "Bundle", "Jungle"};
438  int dept0[4] = {0, 1, 2, 3};
439  for (int k = 0; k < 4; ++k) {
440  for (int depth = 0; depth < maxDepthHF_; ++depth) {
441  snprintf(name1, 40, "HF%s%d+z", hfty1[k].c_str(), depth);
442  snprintf(name2, 40, "HF (%s) +z depth%d", hfty2[k].c_str(), depth + 1);
443  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
445  divisions.push_back(names);
446  types.push_back(type);
447  snprintf(name1, 40, "HF%s%d-z", hfty1[k].c_str(), depth);
448  snprintf(name2, 40, "HF (%s) -z depth%d", hfty2[k].c_str(), depth + 1);
449  names = std::pair<std::string, std::string>(std::string(name1), std::string(name2));
451  divisions.push_back(names);
452  types.push_back(type);
453  }
454  }
455 
456  return divisions;
457 }
458 
461  desc.add<std::string>("ModuleLabel", "g4SimHits");
462  desc.add<std::string>("HitCollection", "HcalHits");
463  desc.add<bool>("Verbose", false);
464  desc.add<bool>("TestNumber", false);
465 
466  descriptions.addDefault(desc);
467 }
468 
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
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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
bool getData(T &iHolder) const
Definition: EventSetup.h:122
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 > &)