CMS 3D CMS Logo

HGCalHitCalibration.cc
Go to the documentation of this file.
1 // user include files
4 
8 
10 
15 
25 
31 
34 
37 
38 #include "TH1F.h"
39 #include <string>
40 #include <map>
41 
42 //#define EDM_ML_DEBUG
43 
44 class HGCalHitCalibration : public edm::one::EDAnalyzer<edm::one::SharedResources> {
45 public:
46  explicit HGCalHitCalibration(const edm::ParameterSet&);
48 
49  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
50 
51 private:
52  virtual void beginJob() override {}
53  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
54  virtual void endJob() override {}
55 
60 
62  double cutValue_;
63  int algo_;
65 
70  TH1F* h_MissingHit_[3];
71 
72  std::vector<float> Energy_layer_calib;
73  std::vector<float> Energy_layer_calib_fraction;
74 };
75 
77  rawRecHits_(iConfig.getParameter<bool>("rawRecHits")),
78  cutOnPt_(iConfig.getParameter<bool>("cutOnPt")),
79  cutValue_(iConfig.getParameter<double>("cutValue")) {
80 
81  usesResource(TFileService::kSharedResource);
82  std::string detector = iConfig.getParameter<std::string >("detector");
83  _recHitsEE = consumes<HGCRecHitCollection>(edm::InputTag("HGCalRecHit","HGCEERecHits"));
84  _recHitsFH = consumes<HGCRecHitCollection>(edm::InputTag("HGCalRecHit","HGCHEFRecHits"));
85  _recHitsBH = consumes<HGCRecHitCollection>(edm::InputTag("HGCalRecHit","HGCHEBRecHits"));
86  if (detector=="all") {
87  algo_ = 1;
88  } else if (detector=="EM") {
89  algo_ = 2;
90  } else if (detector=="HAD") {
91  algo_ = 3;
92  }
93  _caloParticles = consumes<std::vector<CaloParticle> >(edm::InputTag("mix","MergedCaloTruth"));
94 
96  std::string particle[6] = {"All", "Electron", "Muon", "Photon", "ChgHad", "NeutHad"};
97  char name[100];
98  for (int k=0; k<6; ++k) {
99  sprintf(name,"h_EoP_CPene_100_calib_fraction_%s",particle[k].c_str());
100  h_EoP_CPene_100_calib_fraction_[k] = fs->make<TH1F>(name,"",1000,-0.5,2.5);
101  sprintf(name,"h_EoP_CPene_200_calib_fraction_%s",particle[k].c_str());
102  h_EoP_CPene_200_calib_fraction_[k] = fs->make<TH1F>(name,"",1000,-0.5,2.5);
103  sprintf(name,"h_EoP_CPene_300_calib_fraction_%s",particle[k].c_str());
104  h_EoP_CPene_300_calib_fraction_[k] = fs->make<TH1F>(name,"",1000,-0.5,2.5);
105  sprintf(name,"h_LayerOccupancy_%s",particle[k].c_str());
106  h_LayerOccupancy_[k] = fs->make<TH1F>(name, "", 60, 0., 60.);
107  }
108  std::string dets[3] = {"EE", "FH", "BH"};
109  for (int k=0; k<3; ++k) {
110  sprintf(name,"h_missHit_%s",dets[k].c_str());
111  h_MissingHit_[k] = fs->make<TH1F>(name, "", 200, 0., 200.);
112  }
113 }
114 
116 
118 
119  recHitTools.getEventSetup(iSetup);
120 
121  edm::Handle<HGCRecHitCollection> recHitHandleEE;
122  edm::Handle<HGCRecHitCollection> recHitHandleFH;
123  edm::Handle<HGCRecHitCollection> recHitHandleBH;
124  iEvent.getByToken(_recHitsEE,recHitHandleEE);
125  iEvent.getByToken(_recHitsFH,recHitHandleFH);
126  iEvent.getByToken(_recHitsBH,recHitHandleBH);
127  const HGCRecHitCollection& rechitsEE = *recHitHandleEE;
128  const HGCRecHitCollection& rechitsFH = *recHitHandleFH;
129  const HGCRecHitCollection& rechitsBH = *recHitHandleBH;
130 
131  edm::Handle<std::vector<CaloParticle> > caloParticleHandle;
132  iEvent.getByToken(_caloParticles, caloParticleHandle);
133  const std::vector<CaloParticle>& caloParticles = *caloParticleHandle;
134  const int pdgId[3] = {11, 13, 22};
135 
136  // loop over caloParticles
137  int mhit[3] = {0,0,0};
138  for (auto it_caloPart = caloParticles.begin();
139  it_caloPart != caloParticles.end(); ++it_caloPart) {
140  double cut = (cutOnPt_) ? it_caloPart->pt() : it_caloPart->energy();
141  if (cut > cutValue_) {
142  int type(5);
143  if (std::abs(it_caloPart->pdgId()) == pdgId[0]) type = 1;
144  else if (std::abs(it_caloPart->pdgId()) == pdgId[1]) type = 2;
145  else if (it_caloPart->pdgId() == pdgId[2]) type = 3;
146  else if (it_caloPart->threeCharge() != 0) type = 4;
147  const SimClusterRefVector simClusterRefVector = it_caloPart->simClusters();
148 
149  //size should be HGC layers 52 is enough
150  Energy_layer_calib.assign(60,0.);
151  Energy_layer_calib_fraction.assign(60,0.);
152 
153  int seedDet = 0;
154  float seedEnergy = 0.;
155  int simClusterCount = 0;
156 
157  for ( const auto & simCluster : simClusterRefVector) {
158  ++simClusterCount;
159 #ifdef EDM_ML_DEBUG
160  std::cout << ">>> simCluster.energy() = " << simCluster->energy() << std::endl;
161 #endif
162  const std::vector<std::pair<uint32_t,float> > hits_and_fractions = simCluster->hits_and_fractions();
163 
164  //loop over hits
165  for (auto it_haf = hits_and_fractions.begin();
166  it_haf != hits_and_fractions.end(); ++it_haf) {
167  unsigned int hitlayer = recHitTools.getLayerWithOffset(it_haf->first);
168  DetId hitid = (it_haf->first);
169 
170  // dump raw RecHits and match
171  if (rawRecHits_) {
172  if (hitid.det() == DetId::Forward && hitid.subdetId() == HGCEE &&
173  (algo_ == 1 || algo_ == 2)) {
174  // loop over EE RecHits
175  bool found(false);
176  for (auto it_hit = rechitsEE.begin();
177  it_hit < rechitsEE.end(); ++it_hit) {
178  const HGCalDetId detid = it_hit->detid();
179  unsigned int layer = recHitTools.getLayerWithOffset(detid);
180  if (detid == hitid) {
181  found = true;
182  if (hitlayer != layer) {
183 #ifdef EDM_ML_DEBUG
184  std::cout << " recHit ID problem EE " << std::endl;
185 #endif
186  return;
187  }
188  Energy_layer_calib_fraction[layer] += it_hit->energy()*it_haf->second;
189  h_LayerOccupancy_[0]->Fill(layer);
190  h_LayerOccupancy_[type]->Fill(layer);
191  if(seedEnergy < it_hit->energy()){
192  seedEnergy = it_hit->energy();
193  seedDet = recHitTools.getSiThickness(detid);
194  }
195  break;
196  }
197  }
198  if (!found) ++mhit[0];
199  }
200  if (hitid.det() == DetId::Forward && hitid.subdetId() == HGCHEF &&
201  (algo_ == 1 || algo_ == 3)) {
202  // loop over HEF RecHits
203  bool found(false);
204  for (auto it_hit = rechitsFH.begin();
205  it_hit < rechitsFH.end(); ++it_hit) {
206  const HGCalDetId detid = it_hit->detid();
207  unsigned int layer = recHitTools.getLayerWithOffset(detid);
208  if (detid == hitid) {
209  found = true;
210  if (hitlayer != layer) {
211 #ifdef EDM_ML_DEBUG
212  std::cout << " recHit ID problem FH " << std::endl;
213 #endif
214  return;
215  }
216  Energy_layer_calib_fraction[layer] += it_hit->energy()*it_haf->second;
217  h_LayerOccupancy_[0]->Fill(layer);
218  h_LayerOccupancy_[type]->Fill(layer);
219  if(seedEnergy < it_hit->energy()){
220  seedEnergy = it_hit->energy();
221  seedDet = recHitTools.getSiThickness(detid);
222  }
223  break;
224  }
225  }
226  if (!found) ++mhit[1];
227  }
228  if (hitid.det() == DetId::Hcal && hitid.subdetId() == HcalEndcap &&
229  (algo_ == 1 || algo_ == 3)) {
230  // loop over BH RecHits
231  bool found(false);
232  for (auto it_hit = rechitsBH.begin();
233  it_hit < rechitsBH.end(); ++it_hit) {
234  const HcalDetId detid = it_hit->detid();
235  unsigned int layer = recHitTools.getLayerWithOffset(detid);
236  if (detid == hitid) {
237  found = true;
238  if (hitlayer != layer) {
239 #ifdef EDM_ML_DEBUG
240  std::cout << " recHit ID problem BH " << std::endl;
241 #endif
242  return;
243  }
244  Energy_layer_calib_fraction[layer] += it_hit->energy()*it_haf->second;
245  h_LayerOccupancy_[0]->Fill(layer);
246  h_LayerOccupancy_[type]->Fill(layer);
247  if (seedEnergy < it_hit->energy()){
248  seedEnergy = it_hit->energy();
249  seedDet = 300; // recHitTools.getSiThickness(detid);
250  }
251  break;
252  }
253  }
254  if (!found) ++mhit[2];
255  }
256 
257  }//end recHits
258  }// end simHit
259  }//end simCluster
260 
261  float sumCalibRecHitCalib_fraction = 0;
262  for(unsigned int iL=0; iL<Energy_layer_calib_fraction.size(); ++iL){
263  sumCalibRecHitCalib_fraction += Energy_layer_calib_fraction[iL];
264  }
265 
266  double ebyp = sumCalibRecHitCalib_fraction / it_caloPart->energy();
267  if(seedDet == 100) {
268  h_EoP_CPene_100_calib_fraction_[0]->Fill(ebyp);
270  } else if (seedDet == 200) {
271  h_EoP_CPene_200_calib_fraction_[0]->Fill(ebyp);
273  } else if (seedDet == 300){
274  h_EoP_CPene_300_calib_fraction_[0]->Fill(ebyp);
276  }
277  }
278  }//end caloparticle
279 
280  for (int k=0; k<3; ++k) h_MissingHit_[k]->Fill(mhit[k]);
281 }
282 
283 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
284 void
286  //The following says we do not know what parameters are allowed so do no validation
287  // Please change this to state exactly what you do use, even if it is no parameters
289  desc.add<std::string>("detector","all");
290  desc.add<bool>("rawRecHits",true);
291  desc.add<bool>("cutOnPt",true);
292  desc.add<double>("cutValue",10.0);
293  descriptions.add("hgcalHitCalibration",desc);
294 }
295 
296 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
type
Definition: HCALResponse.h:21
T getParameter(std::string const &) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::EDGetTokenT< HGCRecHitCollection > _recHitsBH
HGCalHitCalibration(const edm::ParameterSet &)
void getEventSetup(const edm::EventSetup &)
Definition: RecHitTools.cc:66
edm::EDGetTokenT< std::vector< CaloParticle > > _caloParticles
edm::EDGetTokenT< HGCRecHitCollection > _recHitsFH
int iEvent
Definition: GenABIO.cc:230
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
ParameterDescriptionBase * add(U const &iLabel, T const &value)
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:37
virtual void beginJob() override
TH1F * h_EoP_CPene_300_calib_fraction_[6]
int k[5][pyjets_maxn]
const_iterator end() const
std::float_t getSiThickness(const DetId &) const
Definition: RecHitTools.cc:99
Definition: DetId.h:18
virtual void endJob() override
edm::EDGetTokenT< HGCRecHitCollection > _recHitsEE
std::vector< float > Energy_layer_calib_fraction
unsigned int getLayerWithOffset(const DetId &) const
Definition: RecHitTools.cc:139
void add(std::string const &label, ParameterSetDescription const &psetDescription)
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
hgcal::RecHitTools recHitTools
TH1F * h_EoP_CPene_200_calib_fraction_[6]
std::vector< float > Energy_layer_calib
Detector det() const
get the detector field from this detid
Definition: DetId.h:35
TH1F * h_EoP_CPene_100_calib_fraction_[6]
const_iterator begin() const