CMS 3D CMS Logo

HGCalHitCalibration.cc
Go to the documentation of this file.
1 // user include files
2 
6 
10 
12 
14 
17 
24 
29 
35 
37 
38 #include <map>
39 #include <array>
40 #include <string>
41 #include <numeric>
42 
44 public:
45  explicit HGCalHitCalibration(const edm::ParameterSet&);
46  ~HGCalHitCalibration() override;
47 
48  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
49 
50 private:
51  void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&) override;
52  void analyze(const edm::Event&, const edm::EventSetup&) override;
53 
54  void fillWithRecHits(std::map<DetId, const HGCRecHit*>&, DetId, unsigned int, float, int&, float&);
55 
63 
66  int debug_;
68  static constexpr int depletion1_ = 200;
69  static constexpr int depletion2_ = 300;
70 
71  std::map<int, MonitorElement*> h_EoP_CPene_calib_fraction_;
72  std::map<int, MonitorElement*> hgcal_EoP_CPene_calib_fraction_;
73  std::map<int, MonitorElement*> hgcal_ele_EoP_CPene_calib_fraction_;
74  std::map<int, MonitorElement*> hgcal_photon_EoP_CPene_calib_fraction_;
76 
77  static constexpr int layers_ = 60;
78  std::array<float, layers_> Energy_layer_calib_;
79  std::array<float, layers_> Energy_layer_calib_fraction_;
80 };
81 
83  : rawRecHits_(iConfig.getParameter<bool>("rawRecHits")), debug_(iConfig.getParameter<int>("debug")) {
84  auto detector = iConfig.getParameter<std::string>("detector");
85  auto recHitsEE = iConfig.getParameter<edm::InputTag>("recHitsEE");
86  auto recHitsFH = iConfig.getParameter<edm::InputTag>("recHitsFH");
87  auto recHitsBH = iConfig.getParameter<edm::InputTag>("recHitsBH");
88  auto caloParticles = iConfig.getParameter<edm::InputTag>("caloParticles");
89  auto hgcalMultiClusters = iConfig.getParameter<edm::InputTag>("hgcalMultiClusters");
90  auto electrons = iConfig.getParameter<edm::InputTag>("electrons");
91  auto photons = iConfig.getParameter<edm::InputTag>("photons");
92  if (detector == "all") {
93  recHitsEE_ = consumes<HGCRecHitCollection>(recHitsEE);
94  recHitsFH_ = consumes<HGCRecHitCollection>(recHitsFH);
95  recHitsBH_ = consumes<HGCRecHitCollection>(recHitsBH);
96  algo_ = 1;
97  } else if (detector == "EM") {
98  recHitsEE_ = consumes<HGCRecHitCollection>(recHitsEE);
99  algo_ = 2;
100  } else if (detector == "HAD") {
101  recHitsFH_ = consumes<HGCRecHitCollection>(recHitsFH);
102  recHitsBH_ = consumes<HGCRecHitCollection>(recHitsBH);
103  algo_ = 3;
104  }
105  depletion0_ = iConfig.getParameter<int>("depletionFine");
106  caloParticles_ = consumes<std::vector<CaloParticle> >(caloParticles);
107  hgcalMultiClusters_ = consumes<std::vector<reco::PFCluster> >(hgcalMultiClusters);
108  electrons_ = consumes<std::vector<reco::GsfElectron> >(electrons);
109  photons_ = consumes<std::vector<reco::Photon> >(photons);
110 
111  // size should be HGC layers 52 is enough
112  Energy_layer_calib_.fill(0.);
114 }
115 
117  // do anything here that needs to be done at desctruction time
118  // (e.g. close files, deallocate resources etc.)
119 }
120 
122  edm::Run const& iRun,
123  edm::EventSetup const& /* iSetup */) {
124  ibooker.cd();
125  ibooker.setCurrentFolder("HGCalHitCalibration");
126  h_EoP_CPene_calib_fraction_[depletion0_] = ibooker.book1D("h_EoP_CPene_100_calib_fraction", "", 1000, -0.5, 2.5);
127  h_EoP_CPene_calib_fraction_[depletion1_] = ibooker.book1D("h_EoP_CPene_200_calib_fraction", "", 1000, -0.5, 2.5);
128  h_EoP_CPene_calib_fraction_[depletion2_] = ibooker.book1D("h_EoP_CPene_300_calib_fraction", "", 1000, -0.5, 2.5);
130  ibooker.book1D("hgcal_EoP_CPene_100_calib_fraction", "", 1000, -0.5, 2.5);
132  ibooker.book1D("hgcal_EoP_CPene_200_calib_fraction", "", 1000, -0.5, 2.5);
134  ibooker.book1D("hgcal_EoP_CPene_300_calib_fraction", "", 1000, -0.5, 2.5);
136  ibooker.book1D("hgcal_ele_EoP_CPene_100_calib_fraction", "", 1000, -0.5, 2.5);
138  ibooker.book1D("hgcal_ele_EoP_CPene_200_calib_fraction", "", 1000, -0.5, 2.5);
140  ibooker.book1D("hgcal_ele_EoP_CPene_300_calib_fraction", "", 1000, -0.5, 2.5);
142  ibooker.book1D("hgcal_photon_EoP_CPene_100_calib_fraction", "", 1000, -0.5, 2.5);
144  ibooker.book1D("hgcal_photon_EoP_CPene_200_calib_fraction", "", 1000, -0.5, 2.5);
146  ibooker.book1D("hgcal_photon_EoP_CPene_300_calib_fraction", "", 1000, -0.5, 2.5);
147  LayerOccupancy_ = ibooker.book1D("LayerOccupancy", "", layers_, 0., (float)layers_);
148 }
149 
150 void HGCalHitCalibration::fillWithRecHits(std::map<DetId, const HGCRecHit*>& hitmap,
151  const DetId hitid,
152  const unsigned int hitlayer,
153  const float fraction,
154  int& seedDet,
155  float& seedEnergy) {
156  if (hitmap.find(hitid) == hitmap.end()) {
157  // Hit was not reconstructed
158  IfLogTrace(debug_ > 0, "HGCalHitCalibration")
159  << ">>> Failed to find detid " << std::hex << hitid.rawId() << std::dec << " Det " << hitid.det() << " Subdet "
160  << hitid.subdetId() << std::endl;
161  return;
162  }
163  if ((hitid.det() != DetId::Forward) && (hitid.det() != DetId::HGCalEE) && (hitid.det() != DetId::HGCalHSi) &&
164  (hitid.det() != DetId::HGCalHSc)) {
165  IfLogTrace(debug_ > 0, "HGCalHitCalibration")
166  << ">>> Wrong type of detid " << std::hex << hitid.rawId() << std::dec << " Det " << hitid.det() << " Subdet "
167  << hitid.subdetId() << std::endl;
168  return;
169  }
170  unsigned int layer = recHitTools_.getLayerWithOffset(hitid);
171  assert(hitlayer == layer);
172  Energy_layer_calib_fraction_[layer] += hitmap[hitid]->energy() * fraction;
173  LayerOccupancy_->Fill(layer);
174  if (seedEnergy < hitmap[hitid]->energy()) {
175  seedEnergy = hitmap[hitid]->energy();
176  seedDet = recHitTools_.getSiThickness(hitid);
177  }
178 }
179 
181  using namespace edm;
182 
183  recHitTools_.getEventSetup(iSetup);
184 
185  Handle<HGCRecHitCollection> recHitHandleEE;
186  Handle<HGCRecHitCollection> recHitHandleFH;
187  Handle<HGCRecHitCollection> recHitHandleBH;
188 
189  Handle<std::vector<CaloParticle> > caloParticleHandle;
190  iEvent.getByToken(caloParticles_, caloParticleHandle);
191  const std::vector<CaloParticle>& caloParticles = *caloParticleHandle;
192 
193  Handle<std::vector<reco::PFCluster> > hgcalMultiClustersHandle;
194  iEvent.getByToken(hgcalMultiClusters_, hgcalMultiClustersHandle);
195 
196  Handle<std::vector<reco::GsfElectron> > PFElectronHandle;
197  iEvent.getByToken(electrons_, PFElectronHandle);
198 
199  Handle<std::vector<reco::Photon> > PFPhotonHandle;
200  iEvent.getByToken(photons_, PFPhotonHandle);
201 
202  // make a map detid-rechit
203  std::map<DetId, const HGCRecHit*> hitmap;
204  switch (algo_) {
205  case 1: {
206  iEvent.getByToken(recHitsEE_, recHitHandleEE);
207  iEvent.getByToken(recHitsFH_, recHitHandleFH);
208  iEvent.getByToken(recHitsBH_, recHitHandleBH);
209  const auto& rechitsEE = *recHitHandleEE;
210  const auto& rechitsFH = *recHitHandleFH;
211  const auto& rechitsBH = *recHitHandleBH;
212  for (unsigned int i = 0; i < rechitsEE.size(); ++i) {
213  hitmap[rechitsEE[i].detid()] = &rechitsEE[i];
214  }
215  for (unsigned int i = 0; i < rechitsFH.size(); ++i) {
216  hitmap[rechitsFH[i].detid()] = &rechitsFH[i];
217  }
218  for (unsigned int i = 0; i < rechitsBH.size(); ++i) {
219  hitmap[rechitsBH[i].detid()] = &rechitsBH[i];
220  }
221  break;
222  }
223  case 2: {
224  iEvent.getByToken(recHitsEE_, recHitHandleEE);
225  const HGCRecHitCollection& rechitsEE = *recHitHandleEE;
226  for (unsigned int i = 0; i < rechitsEE.size(); i++) {
227  hitmap[rechitsEE[i].detid()] = &rechitsEE[i];
228  }
229  break;
230  }
231  case 3: {
232  iEvent.getByToken(recHitsFH_, recHitHandleFH);
233  iEvent.getByToken(recHitsBH_, recHitHandleBH);
234  const auto& rechitsFH = *recHitHandleFH;
235  const auto& rechitsBH = *recHitHandleBH;
236  for (unsigned int i = 0; i < rechitsFH.size(); i++) {
237  hitmap[rechitsFH[i].detid()] = &rechitsFH[i];
238  }
239  for (unsigned int i = 0; i < rechitsBH.size(); i++) {
240  hitmap[rechitsBH[i].detid()] = &rechitsBH[i];
241  }
242  break;
243  }
244  default:
245  assert(false);
246  break;
247  }
248 
249  // loop over caloParticles
250  int seedDet = 0;
251  float seedEnergy = 0.;
252  IfLogTrace(debug_ > 0, "HGCalHitCalibration") << "Number of caloParticles: " << caloParticles.size() << std::endl;
253  for (const auto& it_caloPart : caloParticles) {
254  if (it_caloPart.g4Tracks()[0].eventId().event() != 0 or it_caloPart.g4Tracks()[0].eventId().bunchCrossing() != 0) {
255  LogDebug("HGCalHitCalibration") << "Excluding CaloParticles from event: "
256  << it_caloPart.g4Tracks()[0].eventId().event()
257  << " with BX: " << it_caloPart.g4Tracks()[0].eventId().bunchCrossing()
258  << std::endl;
259  continue;
260  }
261 
262  const SimClusterRefVector& simClusterRefVector = it_caloPart.simClusters();
263  Energy_layer_calib_.fill(0.);
265 
266  seedDet = 0;
267  seedEnergy = 0.;
268  for (const auto& it_sc : simClusterRefVector) {
269  const SimCluster& simCluster = (*(it_sc));
270  IfLogTrace(debug_ > 1, "HGCalHitCalibration")
271  << ">>> SC.energy(): " << simCluster.energy() << " SC.simEnergy(): " << simCluster.simEnergy() << std::endl;
272  const std::vector<std::pair<uint32_t, float> >& hits_and_fractions = simCluster.hits_and_fractions();
273 
274  // loop over hits
275  for (const auto& it_haf : hits_and_fractions) {
276  unsigned int hitlayer = recHitTools_.getLayerWithOffset(it_haf.first);
277  DetId hitid = (it_haf.first);
278  // dump raw RecHits and match
279  if (rawRecHits_) {
280  if ((hitid.det() == DetId::Forward &&
281  (hitid.subdetId() == HGCEE || hitid.subdetId() == HGCHEF || hitid.subdetId() == HGCHEB)) ||
282  (hitid.det() == DetId::HGCalEE) || (hitid.det() == DetId::HGCalHSi) || (hitid.det() == DetId::HGCalHSc) ||
283  (hitid.det() == DetId::Hcal && hitid.subdetId() == HcalEndcap))
284  fillWithRecHits(hitmap, hitid, hitlayer, it_haf.second, seedDet, seedEnergy);
285  }
286  } // end simHit
287  } // end simCluster
288 
289  auto sumCalibRecHitCalib_fraction =
290  std::accumulate(Energy_layer_calib_fraction_.begin(), Energy_layer_calib_fraction_.end(), 0.);
291  IfLogTrace(debug_ > 0, "HGCalHitCalibration")
292  << ">>> MC Energy: " << it_caloPart.energy() << " reco energy: " << sumCalibRecHitCalib_fraction << std::endl;
293  if (h_EoP_CPene_calib_fraction_.count(seedDet))
294  h_EoP_CPene_calib_fraction_[seedDet]->Fill(sumCalibRecHitCalib_fraction / it_caloPart.energy());
295 
296  // Loop on reconstructed SC.
297  const auto& clusters = *hgcalMultiClustersHandle;
298  float total_energy = 0.;
299  float max_dR2 = 0.0025;
300  auto closest =
301  std::min_element(clusters.begin(), clusters.end(), [&](const reco::PFCluster& a, const reco::PFCluster& b) {
302  auto dR2_a = reco::deltaR2(it_caloPart, a);
303  auto dR2_b = reco::deltaR2(it_caloPart, b);
304  auto ERatio_a = a.correctedEnergy() / it_caloPart.energy();
305  auto ERatio_b = b.correctedEnergy() / it_caloPart.energy();
306  // If both clusters are within 0.0025, mark as first (min) the
307  // element with the highest ratio against the SimCluster
308  if (dR2_a < max_dR2 && dR2_b < max_dR2)
309  return ERatio_a > ERatio_b;
310  return dR2_a < dR2_b;
311  });
312  if (closest != clusters.end() && reco::deltaR2(*closest, it_caloPart) < 0.01) {
313  total_energy = closest->correctedEnergy();
314  seedDet = recHitTools_.getSiThickness(closest->seed());
315  if (hgcal_EoP_CPene_calib_fraction_.count(seedDet)) {
316  hgcal_EoP_CPene_calib_fraction_[seedDet]->Fill(total_energy / it_caloPart.energy());
317  }
318  }
319 
320  auto closest_fcn = [&](auto const& a, auto const& b) {
321  auto dR2_a = reco::deltaR2(it_caloPart, a);
322  auto dR2_b = reco::deltaR2(it_caloPart, b);
323  auto ERatio_a = a.energy() / it_caloPart.energy();
324  auto ERatio_b = b.energy() / it_caloPart.energy();
325  // If both clusters are within 0.0025, mark as first (min) the
326  // element with the highest ratio against the SimCluster
327  if (dR2_a < max_dR2 && dR2_b < max_dR2)
328  return ERatio_a > ERatio_b;
329  return dR2_a < dR2_b;
330  };
331  // ELECTRONS in HGCAL
332  if (PFElectronHandle.isValid()) {
333  auto const& ele = (*PFElectronHandle);
334  auto closest = std::min_element(ele.begin(), ele.end(), closest_fcn);
335  if (closest != ele.end() &&
336  (closest->superCluster()->seed()->seed().det() == DetId::Forward ||
337  closest->superCluster()->seed()->seed().det() == DetId::HGCalEE) &&
338  reco::deltaR2(*closest, it_caloPart) < 0.01) {
339  seedDet = recHitTools_.getSiThickness(closest->superCluster()->seed()->seed());
340  if (hgcal_ele_EoP_CPene_calib_fraction_.count(seedDet)) {
341  hgcal_ele_EoP_CPene_calib_fraction_[seedDet]->Fill(closest->energy() / it_caloPart.energy());
342  }
343  }
344  }
345 
346  // PHOTONS in HGCAL
347  if (PFPhotonHandle.isValid()) {
348  auto const& photon = (*PFPhotonHandle);
349  auto closest = std::min_element(photon.begin(), photon.end(), closest_fcn);
350  if (closest != photon.end() &&
351  (closest->superCluster()->seed()->seed().det() == DetId::Forward ||
352  closest->superCluster()->seed()->seed().det() == DetId::HGCalEE) &&
353  reco::deltaR2(*closest, it_caloPart) < 0.01) {
354  seedDet = recHitTools_.getSiThickness(closest->superCluster()->seed()->seed());
355  if (hgcal_photon_EoP_CPene_calib_fraction_.count(seedDet)) {
356  hgcal_photon_EoP_CPene_calib_fraction_[seedDet]->Fill(closest->energy() / it_caloPart.energy());
357  }
358  }
359  }
360  } // end caloparticle
361 }
362 
363 // ------------ method fills 'descriptions' with the allowed parameters for the
364 // module ------------
367  desc.add<int>("debug", 0);
368  desc.add<bool>("rawRecHits", true);
369  desc.add<std::string>("detector", "all");
370  desc.add<int>("depletionFine", 100);
371  desc.add<edm::InputTag>("caloParticles", edm::InputTag("mix", "MergedCaloTruth"));
372  desc.add<edm::InputTag>("recHitsEE", edm::InputTag("HGCalRecHit", "HGCEERecHits"));
373  desc.add<edm::InputTag>("recHitsFH", edm::InputTag("HGCalRecHit", "HGCHEFRecHits"));
374  desc.add<edm::InputTag>("recHitsBH", edm::InputTag("HGCalRecHit", "HGCHEBRecHits"));
375  desc.add<edm::InputTag>("hgcalMultiClusters", edm::InputTag("particleFlowClusterHGCalFromMultiCl"));
376  desc.add<edm::InputTag>("electrons", edm::InputTag("ecalDrivenGsfElectronsFromMultiCl"));
377  desc.add<edm::InputTag>("photons", edm::InputTag("photonsFromMultiCl"));
378  descriptions.add("hgcalHitCalibrationDefault", desc);
379 }
380 
381 // define this as a plug-in
#define LogDebug(id)
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX)
Definition: DQMStore.cc:239
T getParameter(std::string const &) const
edm::EDGetTokenT< std::vector< CaloParticle > > caloParticles_
edm::EDGetTokenT< std::vector< reco::Photon > > photons_
std::array< float, layers_ > Energy_layer_calib_fraction_
edm::EDGetTokenT< std::vector< reco::PFCluster > > hgcalMultiClusters_
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::map< int, MonitorElement * > hgcal_EoP_CPene_calib_fraction_
double correctedEnergy() const
Definition: CaloCluster.h:149
edm::EDGetTokenT< HGCRecHitCollection > recHitsFH_
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:46
edm::EDGetTokenT< HGCRecHitCollection > recHitsEE_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:525
std::map< int, MonitorElement * > hgcal_ele_EoP_CPene_calib_fraction_
void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:418
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
#define IfLogTrace(cond, cat)
HGCalHitCalibration(const edm::ParameterSet &)
std::map< int, MonitorElement * > hgcal_photon_EoP_CPene_calib_fraction_
void getEventSetup(const edm::EventSetup &)
Definition: RecHitTools.cc:70
void Fill(long long x)
MonitorElement * LayerOccupancy_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< HGCRecHitCollection > recHitsBH_
Monte Carlo truth information used for tracking validation.
Definition: SimCluster.h:29
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e< void, edm::EventID const &, edm::Timestamp const & > We also list in braces which AR_WATCH_USING_METHOD_ is used for those or
Definition: Activities.doc:12
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:48
float energy() const
Energy. Note this is taken from the first SimTrack only.
Definition: SimCluster.h:104
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:70
double energy() const
cluster energy
Definition: PFCluster.h:78
hgcal::RecHitTools recHitTools_
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &) override
float simEnergy() const
returns the accumulated sim energy in the cluster
Definition: SimCluster.h:196
std::float_t getSiThickness(const DetId &) const
Definition: RecHitTools.cc:179
Definition: DetId.h:17
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
unsigned int getLayerWithOffset(const DetId &) const
Definition: RecHitTools.cc:355
edm::EDGetTokenT< std::vector< reco::GsfElectron > > electrons_
std::map< int, MonitorElement * > h_EoP_CPene_calib_fraction_
double b
Definition: hdecay.h:118
void add(std::string const &label, ParameterSetDescription const &psetDescription)
void analyze(const edm::Event &, const edm::EventSetup &) override
HLT enums.
void fillWithRecHits(std::map< DetId, const HGCRecHit * > &, DetId, unsigned int, float, int &, float &)
size_type size() const
double a
Definition: hdecay.h:119
std::array< float, layers_ > Energy_layer_calib_
#define constexpr
Definition: Run.h:45
std::vector< std::pair< uint32_t, float > > hits_and_fractions() const
Returns list of rechit IDs and fractions for this SimCluster.
Definition: SimCluster.h:181
constexpr Detector det() const
get the detector field from this detid
Definition: DetId.h:46