CMS 3D CMS Logo

HGCalSimHitValidation.cc
Go to the documentation of this file.
1 // system include files
2 #include <cmath>
3 #include <iostream>
4 #include <fstream>
5 #include <vector>
6 #include <map>
7 #include <string>
8 
12 
19 
22 
33 
40 
45 
46 #include "CLHEP/Geometry/Point3D.h"
47 #include "CLHEP/Geometry/Transform3D.h"
48 #include "CLHEP/Geometry/Vector3D.h"
49 #include "CLHEP/Units/GlobalSystemOfUnits.h"
50 #include "CLHEP/Units/GlobalPhysicalConstants.h"
51 
53 
54 public:
55 
56  struct energysum{
57  energysum() {etotal=0; for (int i=0; i<6; ++i) eTime[i] = 0.;}
58  double eTime[6], etotal;
59  };
60 
61  struct hitsinfo{
63  x=y=z=phi=eta=0.0;
64  cell=sector=layer=0;
65  }
66  double x, y, z, phi, eta;
67  int cell, sector, layer;
68  };
69 
70 
72  ~HGCalSimHitValidation() override {}
73 
74  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
75 
76 protected:
77 
78  void dqmBeginRun(const edm::Run&, const edm::EventSetup&) override;
79  void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override;
80  void analyze(const edm::Event&, const edm::EventSetup&) override;
81 
82 private:
83 
84  void analyzeHits (std::vector<PCaloHit>& hits);
85  void fillOccupancyMap(std::map<int, int>& OccupancyMap, int layer);
86  void fillHitsInfo(std::pair<hitsinfo,energysum> hit_, unsigned int itimeslice, double esum);
88 
89  // ----------member data ---------------------------
93  std::vector<double> times_;
96  unsigned int nTimes_;
99  unsigned int layers_;
100  std::map<uint32_t, HepGeom::Transform3D> transMap_;
101 
102  std::vector<MonitorElement*> HitOccupancy_Plus_, HitOccupancy_Minus_;
103  std::vector<MonitorElement*> EtaPhi_Plus_, EtaPhi_Minus_;
105  std::vector<MonitorElement*> energy_[6];
106 };
107 
109  nameDetector_(iConfig.getParameter<std::string>("DetectorName")),
110  caloHitSource_(iConfig.getParameter<std::string>("CaloHitSource")),
111  times_(iConfig.getParameter<std::vector<double> >("TimeSlices")),
112  verbosity_(iConfig.getUntrackedParameter<int>("Verbosity",0)),
113  testNumber_(iConfig.getUntrackedParameter<bool>("TestNumber",true)),
114  symmDet_(true) {
115 
116  heRebuild_ = (nameDetector_ == "HCal") ? true : false;
117  tok_hepMC_ = consumes<edm::HepMCProduct>(edm::InputTag("generatorSmeared"));
118  tok_hits_ = consumes<edm::PCaloHitContainer>(edm::InputTag("g4SimHits",caloHitSource_));
119  nTimes_ = (times_.size() > 6) ? 6 : times_.size();
120 }
121 
124  std::vector<double> times = {25.0,1000.0};
125  desc.add<std::string>("DetectorName","HGCalEESensitive");
126  desc.add<std::string>("CaloHitSource","HGCHitsEE");
127  desc.add<std::vector<double> >("TimeSlices",times);
128  desc.addUntracked<int>("Verbosity",0);
129  desc.addUntracked<bool>("TestNumber",true);
130  descriptions.add("hgcalSimHitValidationEE",desc);
131 }
132 
134  const edm::EventSetup& iSetup) {
135 
136  //Generator input
138  iEvent.getByToken(tok_hepMC_,evtMC);
139  if (!evtMC.isValid()) {
140  edm::LogWarning("HGCalValidation") << "no HepMCProduct found\n";
141  } else {
142  const HepMC::GenEvent * myGenEvent = evtMC->GetEvent();
143  unsigned int k(0);
144  for (HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin();
145  p != myGenEvent->particles_end(); ++p, ++k) {
146  edm::LogVerbatim("HGCalValidation") << "Particle[" << k << "] with pt "
147  << (*p)->momentum().perp() << " eta "
148  << (*p)->momentum().eta() << " phi "
149  << (*p)->momentum().phi();
150  }
151  }
152 
153  //Now the hits
154  edm::Handle<edm::PCaloHitContainer> theCaloHitContainers;
155  iEvent.getByToken(tok_hits_, theCaloHitContainers);
156  if (theCaloHitContainers.isValid()) {
157  if (verbosity_>0)
158  edm::LogVerbatim("HGCalValidation") << " PcalohitItr = "
159  << theCaloHitContainers->size();
160  std::vector<PCaloHit> caloHits;
161  caloHits.insert(caloHits.end(), theCaloHitContainers->begin(),
162  theCaloHitContainers->end());
163  if (heRebuild_ && testNumber_) {
164  for (unsigned int i=0; i<caloHits.size(); ++i) {
165  unsigned int id_ = caloHits[i].id();
167  if(hid.subdet()!=int(HcalEndcap)) hid = HcalDetId(HcalEmpty,hid.ieta(),hid.iphi(),hid.depth());
168  caloHits[i].setID(hid.rawId());
169  if (verbosity_>0)
170  edm::LogVerbatim("HGCalValidation") << "Hit[" << i << "] " << hid;
171  }
172  }
173  analyzeHits(caloHits);
174  } else if (verbosity_>0) {
175  edm::LogVerbatim("HGCalValidation") << "PCaloHitContainer does not exist!";
176  }
177 }
178 
179 void HGCalSimHitValidation::analyzeHits (std::vector<PCaloHit>& hits) {
180 
181  std::map<int, int> OccupancyMap_plus, OccupancyMap_minus;
182  OccupancyMap_plus.clear(); OccupancyMap_minus.clear();
183 
184  std::map<uint32_t,std::pair<hitsinfo,energysum> > map_hits;
185  map_hits.clear();
186 
187  if (verbosity_ > 0)
188  edm::LogVerbatim("HGCalValidation") << nameDetector_ << " with "
189  << hits.size() << " PcaloHit elements";
190  unsigned int nused(0);
191  for (unsigned int i=0; i<hits.size(); i++) {
192  double energy = hits[i].energy();
193  double time = hits[i].time();
194  uint32_t id_ = hits[i].id();
195  int cell, sector, subsector, layer, zside;
196  int subdet(0);
197  if (heRebuild_) {
198  HcalDetId detId = HcalDetId(id_);
199  subdet = detId.subdet();
200  if (subdet != static_cast<int>(HcalEndcap)) continue;
201  cell = detId.ietaAbs();
202  sector = detId.iphi();
203  subsector = 1;
204  layer = detId.depth();
205  zside = detId.zside();
206  } else {
208  HGCalTestNumbering::unpackSquareIndex(id_, zside, layer, sector, subsector, cell);
209  } else {
210  HGCalTestNumbering::unpackHexagonIndex(id_, subdet, zside, layer, sector, subsector, cell);
211  }
212  }
213  nused++;
214  if (verbosity_>1)
215  edm::LogVerbatim("HGCalValidation") << "Detector " << nameDetector_
216  << " zside = " << zside
217  << " sector|wafer = " << sector
218  << " subsector|type = " << subsector
219  << " layer = " << layer
220  << " cell = " << cell
221  << " energy = " << energy
222  << " energyem = " << hits[i].energyEM()
223  << " energyhad = " << hits[i].energyHad()
224  << " time = " << time;
225 
226  HepGeom::Point3D<float> gcoord;
227  if (heRebuild_) {
228  std::pair<double,double> etaphi = hcons_->getEtaPhi(subdet,zside*cell,sector);
229  double rz = hcons_->getRZ(subdet,zside*cell,layer);
230  if (verbosity_>2)
231  edm::LogVerbatim("HGCalValidation") << "i/p " << subdet << ":"
232  << zside << ":" << cell << ":"
233  << sector << ":" << layer <<" o/p "
234  << etaphi.first << ":"
235  << etaphi.second << ":" << rz;
236  gcoord = HepGeom::Point3D<float>(rz*cos(etaphi.second)/cosh(etaphi.first),
237  rz*sin(etaphi.second)/cosh(etaphi.first),
238  rz*tanh(etaphi.first));
239  } else {
241  std::pair<float,float> xy = hgcons_->locateCell(cell,layer,subsector,false);
242  const HepGeom::Point3D<float> lcoord(xy.first,xy.second,0);
243  int subs = (symmDet_ ? 0 : subsector);
244  id_ = HGCalTestNumbering::packSquareIndex(zside,layer,sector,subs,0);
245  gcoord = (transMap_[id_]*lcoord);
246  } else {
247  std::pair<float,float> xy = hgcons_->locateCell(cell,layer,sector,false);
248  double zp = hgcons_->waferZ(layer,false);
249  if (zside < 0) zp = -zp;
250  float xp = (zp < 0) ? -xy.first : xy.first;
251  gcoord = HepGeom::Point3D<float>(xp,xy.second,zp);
252  }
253  }
254  double tof = (gcoord.mag()*CLHEP::mm)/CLHEP::c_light;
255  if (verbosity_>1)
256  edm::LogVerbatim("HGCalValidation") << std::hex << id_ << std::dec
257  << " global coordinate " << gcoord
258  << " time " << time << ":" << tof;
259  time -= tof;
260 
261  energysum esum;
262  hitsinfo hinfo;
263  if (map_hits.count(id_) != 0) {
264  hinfo = map_hits[id_].first;
265  esum = map_hits[id_].second;
266  } else {
267  hinfo.x = gcoord.x();
268  hinfo.y = gcoord.y();
269  hinfo.z = gcoord.z();
270  hinfo.sector = sector;
271  hinfo.cell = cell;
272  hinfo.layer = layer;
273  hinfo.phi = gcoord.getPhi();
274  hinfo.eta = gcoord.getEta();
275  }
276  esum.etotal += energy;
277  for (unsigned int k=0; k<nTimes_; ++k) {
278  if (time > 0 && time < times_[k]) esum.eTime[k] += energy;
279  }
280  if (verbosity_>1)
281  edm::LogVerbatim("HGCalValidation") << " ----------------------- gx = "
282  << hinfo.x << " gy = " << hinfo.y
283  << " gz = " << hinfo.z << " phi = "
284  << hinfo.phi << " eta = " << hinfo.eta;
285  map_hits[id_] = std::pair<hitsinfo,energysum>(hinfo,esum);
286  }
287  if (verbosity_>0)
288  edm::LogVerbatim("HGCalValidation") << nameDetector_ << " with "
289  << map_hits.size()
290  << " detector elements being hit";
291 
292  std::map<uint32_t,std::pair<hitsinfo,energysum> >::iterator itr;
293  for (itr = map_hits.begin() ; itr != map_hits.end(); ++itr) {
294  hitsinfo hinfo = (*itr).second.first;
295  energysum esum = (*itr).second.second;
296  int layer = hinfo.layer;
297 
298  for (unsigned int itimeslice = 0; itimeslice < nTimes_; itimeslice++ ) {
299  fillHitsInfo((*itr).second, itimeslice, esum.eTime[itimeslice]);
300  }
301 
302  double eta = hinfo.eta;
303 
304  if (eta > 0.0) fillOccupancyMap(OccupancyMap_plus, layer-1);
305  else fillOccupancyMap(OccupancyMap_minus, layer-1);
306  }
307  edm::LogVerbatim("HGCalValidation") << "With map:used:total " << hits.size()
308  << "|" << nused << "|" << map_hits.size()
309  << " hits";
310 
311  for (auto itr = OccupancyMap_plus.begin() ; itr != OccupancyMap_plus.end(); ++itr) {
312  int layer = (*itr).first;
313  int occupancy = (*itr).second;
314  HitOccupancy_Plus_.at(layer)->Fill(occupancy);
315  }
316  for (auto itr = OccupancyMap_minus.begin() ; itr != OccupancyMap_minus.end(); ++itr) {
317  int layer = (*itr).first;
318  int occupancy = (*itr).second;
319  HitOccupancy_Minus_.at(layer)->Fill(occupancy);
320  }
321 }
322 
323 void HGCalSimHitValidation::fillOccupancyMap(std::map<int, int>& OccupancyMap, int layer){
324  if (OccupancyMap.find(layer) != OccupancyMap.end()) {
325  OccupancyMap[layer] ++;
326  } else {
327  OccupancyMap[layer] = 1;
328  }
329 }
330 
331 void HGCalSimHitValidation::fillHitsInfo(std::pair<hitsinfo,energysum> hits,
332  unsigned int itimeslice, double esum){
333 
334  unsigned int ilayer = hits.first.layer - 1;
335  if (ilayer < layers_) {
336  energy_[itimeslice].at(ilayer)->Fill(esum);
337  if (itimeslice==0) {
338  EtaPhi_Plus_.at(ilayer)->Fill(hits.first.eta , hits.first.phi);
339  EtaPhi_Minus_.at(ilayer)->Fill(hits.first.eta , hits.first.phi);
340  }
341  } else {
342  if (verbosity_>0)
343  edm::LogVerbatim("HGCalValidation") << "Problematic Hit for "
344  << nameDetector_ << " at sector "
345  << hits.first.sector << " layer "
346  << hits.first.layer << " cell "
347  << hits.first.cell << " energy "
348  << hits.second.etotal;
349  }
350 }
351 
353  if (verbosity_>0)
354  edm::LogVerbatim("HGCalValidation") << "Initialize HGCalDDDConstants for "
355  << nameDetector_ << " : " << hgcons_;
356 
357  if (hgcons_->geomMode() == HGCalGeometryMode::Square) {
358  const DDCompactView & cview = *ddViewH;
359  std::string attribute = "Volume";
361 
362  DDSpecificsMatchesValueFilter filter{DDValue(attribute, value, 0)};
363  DDFilteredView fv(cview,filter);
364  bool dodet = fv.firstChild();
365 
366  while (dodet) {
367  const DDSolid & sol = fv.logicalPart().solid();
368  std::string name = sol.name();
369  int isd = (name.find(nameDetector_) == std::string::npos) ? -1 : 1;
370  if (isd > 0) {
371  std::vector<int> copy = fv.copyNumbers();
372  int nsiz = (int)(copy.size());
373  int lay = (nsiz > 0) ? copy[nsiz-1] : -1;
374  int sec = (nsiz > 1) ? copy[nsiz-2] : -1;
375  int zp = (nsiz > 3) ? copy[nsiz-4] : -1;
376  if (zp !=1 ) zp = -1;
377  const DDTrap & trp = static_cast<DDTrap>(sol);
378  int subs = (trp.alpha1()>0 ? 1 : 0);
379  symmDet_ = (trp.alpha1()==0 ? true : false);
380  uint32_t id = HGCalTestNumbering::packSquareIndex(zp,lay,sec,subs,0);
381  DD3Vector x, y, z;
382  fv.rotation().GetComponents( x, y, z ) ;
383  const CLHEP::HepRep3x3 rotation ( x.X(), y.X(), z.X(),
384  x.Y(), y.Y(), z.Y(),
385  x.Z(), y.Z(), z.Z() );
386  const CLHEP::HepRotation hr ( rotation );
387  const CLHEP::Hep3Vector h3v ( fv.translation().X(),
388  fv.translation().Y(),
389  fv.translation().Z() ) ;
390  const HepGeom::Transform3D ht3d (hr, h3v);
391  transMap_.insert(std::make_pair(id,ht3d));
392  if (verbosity_>2)
393  edm::LogVerbatim("HGCalValidation") << HGCalDetId(id)
394  << " Transform using " << h3v
395  << " and " << hr;
396  }
397  dodet = fv.next();
398  }
399  if (verbosity_>0)
400  edm::LogVerbatim("HGCalValidation") << "Finds " << transMap_.size()
401  << " elements and SymmDet_ = "
402  << symmDet_;
403  }
404  return true;
405 }
406 
407 // ------------ method called when starting to processes a run ------------
409  const edm::EventSetup& iSetup) {
410  if (heRebuild_) {
412  iSetup.get<HcalRecNumberingRecord>().get( pHRNDC );
413  hcons_ = &(*pHRNDC);
414  layers_ = hcons_->getMaxDepth(1);
415  } else {
417  iSetup.get<IdealGeometryRecord>().get(nameDetector_, pHGDC);
418  hgcons_ = &(*pHGDC);
419  layers_ = hgcons_->layers(false);
421  iSetup.get<IdealGeometryRecord>().get( pDD );
422  defineGeometry(pDD);
423  }
424  if (verbosity_>0)
425  edm::LogVerbatim("HGCalValidation") << nameDetector_ << " defined with "
426  << layers_ << " Layers";
427 }
428 
430  edm::Run const&,
431  edm::EventSetup const&) {
432 
433  iB.setCurrentFolder("HGCalSimHitsV/"+nameDetector_);
434 
435  std::ostringstream histoname;
436  for (unsigned int ilayer = 0; ilayer < layers_; ilayer++ ) {
437  histoname.str(""); histoname << "HitOccupancy_Plus_layer_" << ilayer;
438  HitOccupancy_Plus_.push_back(iB.book1D(histoname.str().c_str(), "HitOccupancy_Plus", 501, -0.5, 500.5));
439  histoname.str(""); histoname << "HitOccupancy_Minus_layer_" << ilayer;
440  HitOccupancy_Minus_.push_back(iB.book1D(histoname.str().c_str(), "HitOccupancy_Minus", 501, -0.5, 500.5));
441 
442  histoname.str(""); histoname << "EtaPhi_Plus_" << "layer_" << ilayer;
443  EtaPhi_Plus_.push_back(iB.book2D(histoname.str().c_str(), "Occupancy", 31, 1.45, 3.0, 72, -CLHEP::pi, CLHEP::pi));
444  histoname.str(""); histoname << "EtaPhi_Minus_" << "layer_" << ilayer;
445  EtaPhi_Minus_.push_back(iB.book2D(histoname.str().c_str(), "Occupancy", 31, -3.0, -1.45, 72, -CLHEP::pi, CLHEP::pi));
446 
447  for (unsigned int itimeslice = 0; itimeslice < nTimes_ ; itimeslice++ ) {
448  histoname.str(""); histoname << "energy_time_"<< itimeslice << "_layer_" << ilayer;
449  energy_[itimeslice].push_back(iB.book1D(histoname.str().c_str(),"energy_",100,0,0.1));
450  }
451  }
452 
453  MeanHitOccupancy_Plus_ = iB.book1D("MeanHitOccupancy_Plus", "MeanHitOccupancy_Plus", layers_, 0.5, layers_ + 0.5);
454  MeanHitOccupancy_Minus_ = iB.book1D("MeanHitOccupancy_Minus", "MeanHitOccupancy_Minus", layers_, 0.5, layers_ + 0.5);
455 }
456 
458 //define this as a plug-in
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
MonitorElement * MeanHitOccupancy_Plus_
std::vector< MonitorElement * > energy_[6]
const DDLogicalPart & logicalPart() const
The logical-part of the current node in the filtered-view.
const N & name() const
Definition: DDBase.h:78
void fillOccupancyMap(std::map< int, int > &OccupancyMap, int layer)
std::pair< double, double > getEtaPhi(const int &subdet, const int &ieta, const int &iphi) const
HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:49
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
std::vector< MonitorElement * > HitOccupancy_Plus_
void analyze(const edm::Event &, const edm::EventSetup &) override
def copy(args, dbName)
ProductID id() const
Definition: HandleBase.cc:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
nav_type copyNumbers() const
return the stack of copy numbers
int zside() const
get the z-side of the cell (1/-1)
Definition: HcalDetId.cc:93
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< edm::HepMCProduct > tok_hepMC_
const DDRotationMatrix & rotation() const
The absolute rotation of the current node.
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
void analyzeHits(std::vector< PCaloHit > &hits)
int zside(DetId const &)
std::vector< MonitorElement * > HitOccupancy_Minus_
const DDSolid & solid(void) const
Returns a reference object of the solid being the shape of this LogicalPart.
std::pair< float, float > locateCell(int cell, int lay, int type, bool reco) const
type of data representation of DDCompactView
Definition: DDCompactView.h:90
A DDSolid represents the shape of a part.
Definition: DDSolid.h:38
MonitorElement * MeanHitOccupancy_Minus_
const Double_t pi
uint32_t rawId() const
get the raw id
Definition: DetId.h:44
bool defineGeometry(edm::ESTransientHandle< DDCompactView > &ddViewH)
int depth() const
get the tower depth
Definition: HcalDetId.cc:108
int iEvent
Definition: GenABIO.cc:230
unsigned int layers(bool reco) const
ROOT::Math::DisplacementVector3D< ROOT::Math::Cartesian3D< double > > DD3Vector
A DD Translation is currently implemented with Root Vector3D.
Definition: DDTranslation.h:6
bool next()
set current node to the next node in the filtered tree
const HcalDDDRecConstants * hcons_
susybsm::HSCParticleRef hr
Definition: classes.h:26
double getRZ(const int &subdet, const int &ieta, const int &depth) const
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
void dqmBeginRun(const edm::Run &, const edm::EventSetup &) override
int ieta() const
get the cell ieta
Definition: HcalDetId.h:56
MonitorElement * book1D(Args &&...args)
Definition: DQMStore.h:118
Interface to a Trapezoid.
Definition: DDSolid.h:79
std::pair< T, T > etaphi(T x, T y, T z)
Definition: FastMath.h:128
HGCalGeometryMode::GeometryMode geomMode() const
Definition: value.py:1
std::vector< MonitorElement * > EtaPhi_Plus_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
HGCalSimHitValidation(const edm::ParameterSet &)
double waferZ(int layer, bool reco) const
int k[5][pyjets_maxn]
int ietaAbs() const
get the absolute value of the cell ieta
Definition: HcalDetId.cc:98
int iphi() const
get the cell iphi
Definition: HcalDetId.cc:103
void setCurrentFolder(const std::string &fullpath)
Definition: DQMStore.cc:279
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:38
MonitorElement * book2D(Args &&...args)
Definition: DQMStore.h:136
int getMaxDepth(const int &type) const
static void unpackSquareIndex(const uint32_t &idx, int &z, int &lay, int &sec, int &subsec, int &cell)
const T & get() const
Definition: EventSetup.h:59
double alpha1(void) const
Angle with respect to the y axis from the centre of the side at y=-pDy1 to the centre at y=+pDy1 of t...
Definition: DDSolid.cc:183
edm::EDGetTokenT< edm::PCaloHitContainer > tok_hits_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
std::map< uint32_t, HepGeom::Transform3D > transMap_
bool firstChild()
set the current node to the first child ...
const HGCalDDDConstants * hgcons_
std::vector< MonitorElement * > EtaPhi_Minus_
const DDTranslation & translation() const
The absolute translation of the current node.
DetId relabel(const uint32_t testId) const
static uint32_t packSquareIndex(int z, int lay, int sec, int subsec, int cell)
void fillHitsInfo(std::pair< hitsinfo, energysum > hit_, unsigned int itimeslice, double esum)
std::vector< double > times_
static void unpackHexagonIndex(const uint32_t &idx, int &subdet, int &z, int &lay, int &wafer, int &celltyp, int &cell)
Definition: Run.h:43