CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RecAnalyzerMinbias.cc
Go to the documentation of this file.
1 // system include files
2 #include <memory>
3 #include <string>
4 #include <iostream>
5 #include <fstream>
6 #include <sstream>
7 #include <vector>
8 #include <map>
9 
10 // user include files
30 
31 #include "TH1D.h"
32 #include "TFile.h"
33 #include "TTree.h"
34 #include "TMath.h"
35 #include "TF1.h"
36 
37 //#define DebugLog
38 
39 // class declaration
41 
42 public:
43  explicit RecAnalyzerMinbias(const edm::ParameterSet&);
45 
46  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
47  virtual void beginJob();
48  virtual void beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup);
49  virtual void endJob();
50 
51 private:
52  void analyzeHcal(const HBHERecHitCollection&, const HFRecHitCollection&, int, bool);
53  // ----------member data ---------------------------
57  double eLowHF_, eHighHF_;
58  std::map<DetId,double> corrFactor_;
59  std::vector<unsigned int> hcalID_;
60  TTree *myTree_;
61  TH1D *h_[4];
62  std::vector<TH1D*> histo_;
63  std::map<HcalDetId,TH1D*> histHC_;
64  std::vector<int> trigbit_;
65  double rnnum_;
66  struct myInfo{
68  myInfo() {
69  theMB0 = theMB1 = theMB2 = theMB3 = theMB4 = runcheck = 0;
70  }
71  };
72  // Root tree members
73  double rnnumber;
76  std::map<std::pair<int,HcalDetId>,myInfo> myMap_;
80 };
81 
82 // constructors and destructor
84  init_(false) {
85 
86  // get name of output file with histogramms
87  runNZS_ = iConfig.getParameter<bool>("RunNZS");
88  Noise_ = iConfig.getParameter<bool>("Noise");
89  eLowHB_ = iConfig.getParameter<double>("ELowHB");
90  eHighHB_ = iConfig.getParameter<double>("EHighHB");
91  eLowHE_ = iConfig.getParameter<double>("ELowHE");
92  eHighHE_ = iConfig.getParameter<double>("EHighHE");
93  eLowHF_ = iConfig.getParameter<double>("ELowHF");
94  eHighHF_ = iConfig.getParameter<double>("EHighHF");
95  trigbit_ = iConfig.getUntrackedParameter<std::vector<int>>("TriggerBits");
96  ignoreL1_ = iConfig.getUntrackedParameter<bool>("IgnoreL1",false);
97  std::string cfile= iConfig.getUntrackedParameter<std::string>("CorrFile");
98  fillHist_ = iConfig.getUntrackedParameter<bool>("FillHisto",false);
99  std::vector<int> ieta = iConfig.getUntrackedParameter<std::vector<int>>("HcalIeta");
100  std::vector<int> iphi = iConfig.getUntrackedParameter<std::vector<int>>("HcalIphi");
101  std::vector<int> depth= iConfig.getUntrackedParameter<std::vector<int>>("HcalDepth");
102 
103  // get token names of modules, producing object collections
104  tok_hbherecoMB_ = consumes<HBHERecHitCollection>(iConfig.getParameter<edm::InputTag>("hbheInputMB"));
105  tok_hfrecoMB_ = consumes<HFRecHitCollection>(iConfig.getParameter<edm::InputTag>("hfInputMB"));
106  tok_hltL1GtMap_ = consumes<L1GlobalTriggerObjectMapRecord>(edm::InputTag("hltL1GtObjectMap"));
107 
108  // Read correction factors
109  std::ifstream infile(cfile.c_str());
110  if (!infile.is_open()) {
111  theRecalib_ = false;
112  edm::LogInfo("AnalyzerMB") << "Cannot open '" << cfile
113  << "' for the correction file";
114  } else {
115  unsigned int ndets(0), nrec(0);
116  while(1) {
117  unsigned int id;
118  double cfac;
119  infile >> id >> cfac;
120  if (!infile.good()) break;
121  HcalDetId detId(id);
122  nrec++;
123  std::map<DetId,double>::iterator itr = corrFactor_.find(detId);
124  if (itr == corrFactor_.end()) {
125  corrFactor_[detId] = cfac;
126  ndets++;
127  }
128  }
129  infile.close();
130  edm::LogInfo("AnalyzerMB") << "Reads " << nrec << " correction factors for "
131  << ndets << " detIds";
132  theRecalib_ = (ndets>0);
133  }
134 
135  edm::LogInfo("AnalyzerMB") << " Flags (ReCalib): " << theRecalib_
136  << " (IgnoreL1): " << ignoreL1_ << " (NZS) "
137  << runNZS_ << " and with " << ieta.size()
138  << " detId for full histogram";
139  edm::LogInfo("AnalyzerMB") << "Thresholds for HB " << eLowHB_ << ":"
140  << eHighHB_ << " for HE " << eLowHE_ << ":"
141  << eHighHE_ << " for HF " << eLowHF_ << ":"
142  << eHighHF_;
143  for (unsigned int k=0; k<ieta.size(); ++k) {
144  HcalSubdetector subd = ((std::abs(ieta[k]) > 29) ? HcalForward :
145  (std::abs(ieta[k]) > 16) ? HcalEndcap :
146  ((std::abs(ieta[k]) == 16) && (depth[k] == 3)) ? HcalEndcap :
147  (depth[k] == 4) ? HcalOuter : HcalBarrel);
148  unsigned int id = (HcalDetId(subd,ieta[k],iphi[k],depth[k])).rawId();
149  hcalID_.push_back(id);
150  edm::LogInfo("AnalyzerMB") << "DetId[" << k << "] " << HcalDetId(id);
151  }
152  edm::LogInfo("AnalyzerMB") << "Select on " << trigbit_.size()
153  << " L1 Trigger selection";
154  for (unsigned int k=0; k<trigbit_.size(); ++k)
155  edm::LogInfo("AnalyzerMB") << "Bit[" << k << "] " << trigbit_[k];
156 }
157 
159 
161 
162  std::string hc[5] = {"Empty", "HB", "HE", "HO", "HF"};
163  char name[700], title[700];
164  for(int idet=1; idet<=4; idet++){
165  sprintf(name, "%s", hc[idet].c_str());
166  sprintf (title, "Noise distribution for %s", hc[idet].c_str());
167  h_[idet-1] = fs_->make<TH1D>(name,title,48,-6., 6.);
168  }
169 
170  for (unsigned int i=0; i<hcalID_.size(); i++) {
171  HcalDetId id = HcalDetId(hcalID_[i]);
172  int subdet = id.subdetId();
173  sprintf (name, "%s%d_%d_%d", hc[subdet].c_str(), id.ieta(), id.iphi(), id.depth());
174  sprintf (title, "Energy Distribution for %s ieta %d iphi %d depth %d", hc[subdet].c_str(), id.ieta(), id.iphi(), id.depth());
175  double xmin = (subdet == 4) ? -10 : -1;
176  double xmax = (subdet == 4) ? 90 : 9;
177  TH1D* hh = fs_->make<TH1D>(name, title, 50, xmin, xmax);
178  histo_.push_back(hh);
179  };
180 
181  if (!fillHist_) {
182  myTree_ = fs_->make<TTree>("RecJet","RecJet Tree");
183  myTree_->Branch("cells", &cells, "cells/I");
184  myTree_->Branch("mysubd", &mysubd, "mysubd/I");
185  myTree_->Branch("depth", &depth, "depth/I");
186  myTree_->Branch("ieta", &ieta, "ieta/I");
187  myTree_->Branch("iphi", &iphi, "iphi/I");
188  myTree_->Branch("mom0_MB", &mom0_MB, "mom0_MB/F");
189  myTree_->Branch("mom1_MB", &mom1_MB, "mom1_MB/F");
190  myTree_->Branch("mom2_MB", &mom2_MB, "mom2_MB/F");
191  myTree_->Branch("mom3_MB", &mom2_MB, "mom3_MB/F");
192  myTree_->Branch("mom4_MB", &mom4_MB, "mom4_MB/F");
193  myTree_->Branch("trigbit", &trigbit, "trigbit/I");
194  myTree_->Branch("rnnumber", &rnnumber, "rnnumber/D");
195  }
196  myMap_.clear();
197 }
198 
199 
201 
202  if (!init_) {
203  init_ = true;
204  if (fillHist_) {
206  iS.get<IdealGeometryRecord>().get(htopo);
207  if (htopo.isValid()) {
208  const HcalTopology* hcaltopology = htopo.product();
209 
210  char name[700], title[700];
211  // For HB
212  int maxDepthHB = hcaltopology->maxDepthHB();
213  int nbinHB = (Noise_) ? 18 : int(2000*eHighHB_);
214  double x_min = (Noise_) ? -3. : 0.;
215  double x_max = (Noise_) ? 3. : 2.*eHighHB_;
216  for (int eta = -50; eta < 50; eta++) {
217  for (int phi = 0; phi < 100; phi++) {
218  for (int depth = 1; depth <= maxDepthHB; depth++) {
219  HcalDetId cell (HcalBarrel, eta, phi, depth);
220  if (hcaltopology->valid(cell)) {
221  sprintf (name, "HBeta%dphi%ddep%d", eta, phi, depth);
222  sprintf (title,"HB #eta %d #phi %d depth %d", eta, phi, depth);
223  TH1D* h = fs_->make<TH1D>(name, title, nbinHB, x_min, x_max);
224  histHC_[cell] = h;
225  }
226  }
227  }
228  }
229  // For HE
230  int maxDepthHE = hcaltopology->maxDepthHE();
231  int nbinHE = (Noise_) ? 18 : int(2000*eHighHE_);
232  x_min = (Noise_) ? -3. : 0.;
233  x_max = (Noise_) ? 3. : 2.*eHighHE_;
234  for (int eta = -50; eta < 50; eta++) {
235  for (int phi = 0; phi < 100; phi++) {
236  for (int depth = 1; depth <= maxDepthHE; depth++) {
237  HcalDetId cell (HcalEndcap, eta, phi, depth);
238  if (hcaltopology->valid(cell)) {
239  sprintf (name, "HEeta%dphi%ddep%d", eta, phi, depth);
240  sprintf (title,"HE #eta %d #phi %d depth %d", eta, phi, depth);
241  TH1D* h = fs_->make<TH1D>(name, title, nbinHE, x_min, x_max);
242  histHC_[cell] = h;
243  }
244  }
245  }
246  }
247  // For HF
248  int maxDepthHF = 4;
249  int nbinHF = (Noise_) ? 200 : int(2000*eHighHF_);
250  x_min = (Noise_) ? -10. : 0.;
251  x_max = (Noise_) ? 10. : 2.*eHighHF_;
252  for (int eta = -50; eta < 50; eta++) {
253  for (int phi = 0; phi < 100; phi++) {
254  for (int depth = 1; depth <= maxDepthHF; depth++) {
255  HcalDetId cell (HcalForward, eta, phi, depth);
256  if (hcaltopology->valid(cell)) {
257  sprintf (name, "HFeta%dphi%ddep%d", eta, phi, depth);
258  sprintf (title,"Energy (HF #eta %d #phi %d depth %d)", eta, phi, depth);
259  TH1D* h = fs_->make<TH1D>(name, title, nbinHF, x_min, x_max);
260  histHC_[cell] = h;
261  }
262  }
263  }
264  }
265  }
266  }
267  }
268 }
269 
270 // EndJob
271 //
273 
274  if (!fillHist_) {
275  cells = 0;
276  for (std::map<std::pair<int,HcalDetId>,myInfo>::const_iterator itr=myMap_.begin(); itr != myMap_.end(); ++itr) {
277  edm::LogInfo("AnalyzerMB") << "Fired trigger bit number "<<itr->first.first;
278 #ifdef DebugLog
279  std::cout << "Fired trigger bit number "<<itr->first.first << std::endl;
280 #endif
281  myInfo info = itr->second;
282  if (info.theMB0 > 0) {
283  mom0_MB = info.theMB0;
284  mom1_MB = info.theMB1;
285  mom2_MB = info.theMB2;
286  mom3_MB = info.theMB3;
287  mom4_MB = info.theMB4;
288  rnnumber = info.runcheck;
289  trigbit = itr->first.first;
290  mysubd = itr->first.second.subdet();
291  depth = itr->first.second.depth();
292  iphi = itr->first.second.iphi();
293  ieta = itr->first.second.ieta();
294  edm::LogInfo("AnalyzerMB") << " Result= " << trigbit << " " << mysubd
295  << " " << ieta << " " << iphi << " mom0 "
296  << mom0_MB << " mom1 " << mom1_MB << " mom2 "
297  << mom2_MB << " mom3 " << mom3_MB << " mom4 "
298  << mom4_MB;
299 #ifdef DebugLog
300  std::cout << " Result= " << trigbit << " " << mysubd
301  << " " << ieta << " " << iphi << " mom0 "
302  << mom0_MB << " mom1 " << mom1_MB << " mom2 "
303  << mom2_MB << " mom3 " << mom3_MB << " mom4 "
304  << mom4_MB << std::endl;
305 #endif
306  myTree_->Fill();
307  cells++;
308  }
309  }
310  edm::LogInfo("AnalyzerMB") << "cells" << " " << cells;
311 #ifdef DebugLog
312  std::cout << "cells" << " " << cells << std::endl;
313 #endif
314  }
315 #ifdef DebugLog
316  std::cout << "Exiting from RecAnalyzerMinbias::endjob" << std::endl;
317 #endif
318 }
319 
320 //
321 // member functions
322 //
323 // ------------ method called to produce the data ------------
324 
326 
327  rnnum_ = (float)iEvent.run();
329  iEvent.getByToken(tok_hbherecoMB_, hbheMB);
330  if (!hbheMB.isValid()) {
331  edm::LogWarning("AnalyzerMB") << "HcalCalibAlgos: Error! can't get hbhe product!";
332  return ;
333  }
334  const HBHERecHitCollection HithbheMB = *(hbheMB.product());
335  edm::LogInfo("AnalyzerMB") << "HBHE MB size of collection "<<HithbheMB.size();
336  if (HithbheMB.size() < 5100 && runNZS_) {
337  edm::LogWarning("AnalyzerMB") << "HBHE problem " << rnnum_ << " size "
338  << HithbheMB.size();
339  }
340 
342  iEvent.getByToken(tok_hfrecoMB_, hfMB);
343  if (!hfMB.isValid()) {
344  edm::LogWarning("AnalyzerMB") << "HcalCalibAlgos: Error! can't get hf product!";
345  return;
346  }
347  const HFRecHitCollection HithfMB = *(hfMB.product());
348  edm::LogInfo("AnalyzerMB") << "HF MB size of collection " << HithfMB.size();
349  if (HithfMB.size() < 1700 && runNZS_) {
350  edm::LogWarning("AnalyzerMB") << "HF problem " << rnnum_ << " size "
351  << HithfMB.size();
352  }
353 
354  bool select(false);
355  if (trigbit_.size() > 0) {
357  iEvent.getByToken(tok_hltL1GtMap_, gtObjectMapRecord);
358  if (gtObjectMapRecord.isValid()) {
359  const std::vector<L1GlobalTriggerObjectMap>& objMapVec = gtObjectMapRecord->gtObjectMap();
360  for (std::vector<L1GlobalTriggerObjectMap>::const_iterator itMap = objMapVec.begin();
361  itMap != objMapVec.end(); ++itMap) {
362  bool resultGt = (*itMap).algoGtlResult();
363  if (resultGt) {
364  int algoBit = (*itMap).algoBitNumber();
365  if (std::find(trigbit_.begin(),trigbit_.end(),algoBit) !=
366  trigbit_.end()) {
367  select = true;
368  break;
369  }
370  }
371  }
372  }
373  }
374 
375  if (ignoreL1_ || ((trigbit_.size() > 0) && select)) {
376  analyzeHcal(HithbheMB, HithfMB, 1, true);
377  } else if ((!ignoreL1_) && (trigbit_.size() == 0)) {
379  iEvent.getByToken(tok_hltL1GtMap_, gtObjectMapRecord);
380  if (gtObjectMapRecord.isValid()) {
381  const std::vector<L1GlobalTriggerObjectMap>& objMapVec = gtObjectMapRecord->gtObjectMap();
382  bool ok(false);
383  for (std::vector<L1GlobalTriggerObjectMap>::const_iterator itMap = objMapVec.begin();
384  itMap != objMapVec.end(); ++itMap) {
385  bool resultGt = (*itMap).algoGtlResult();
386  if (resultGt) {
387  int algoBit = (*itMap).algoBitNumber();
388  analyzeHcal(HithbheMB, HithfMB, algoBit, (!ok));
389  ok = true;
390  }
391  }
392  if (!ok) {
393  edm::LogInfo("AnalyzerMB") << "No passed L1 Trigger found";
394  }
395  }
396  }
397 }
398 
400  const HFRecHitCollection & HithfMB,
401  int algoBit, bool fill) {
402  // Signal part for HB HE
403  for (HBHERecHitCollection::const_iterator hbheItr=HithbheMB.begin();
404  hbheItr!=HithbheMB.end(); hbheItr++) {
405  // Recalibration of energy
406  DetId mydetid = hbheItr->id().rawId();
407  double icalconst(1.);
408  if (theRecalib_) {
409  std::map<DetId,double>::iterator itr = corrFactor_.find(mydetid);
410  if (itr != corrFactor_.end()) icalconst = itr->second;
411  }
412  HBHERecHit aHit(hbheItr->id(),hbheItr->energy()*icalconst,hbheItr->time());
413  double energyhit = aHit.energy();
414  DetId id = (*hbheItr).detid();
415  HcalDetId hid = HcalDetId(id);
416  double eLow = (hid.subdet() == HcalEndcap) ? eLowHE_ : eLowHB_;
417  double eHigh = (hid.subdet() == HcalEndcap) ? eHighHE_ : eHighHB_;
418  if (fill) {
419  for (unsigned int i = 0; i < hcalID_.size(); i++) {
420  if (hcalID_[i] == id.rawId()) {
421  histo_[i]->Fill(energyhit);
422  break;
423  }
424  }
425  if (fillHist_) {
426  std::map<HcalDetId,TH1D*>::iterator itr1 = histHC_.find(hid);
427  if (itr1 != histHC_.end()) itr1->second->Fill(energyhit);
428  }
429  h_[hid.subdet()-1]->Fill(energyhit);
430  }
431  if (!fillHist_) {
432  if (Noise_ || runNZS_ || (energyhit >= eLow && energyhit <= eHigh)) {
433  std::map<std::pair<int,HcalDetId>,myInfo>::iterator itr1 = myMap_.find(std::pair<int,HcalDetId>(algoBit,hid));
434  if (itr1 == myMap_.end()) {
435  myInfo info;
436  myMap_[std::pair<int,HcalDetId>(algoBit,hid)] = info;
437  itr1 = myMap_.find(std::pair<int,HcalDetId>(algoBit,hid));
438  }
439  itr1->second.theMB0++;
440  itr1->second.theMB1 += energyhit;
441  itr1->second.theMB2 += (energyhit*energyhit);
442  itr1->second.theMB3 += (energyhit*energyhit*energyhit);
443  itr1->second.theMB4 += (energyhit*energyhit*energyhit*energyhit);
444  itr1->second.runcheck = rnnum_;
445  }
446  }
447  } // HBHE_MB
448 
449  // Signal part for HF
450  for (HFRecHitCollection::const_iterator hfItr=HithfMB.begin();
451  hfItr!=HithfMB.end(); hfItr++) {
452  // Recalibration of energy
453  DetId mydetid = hfItr->id().rawId();
454  double icalconst(1.);
455  if (theRecalib_) {
456  std::map<DetId,double>::iterator itr = corrFactor_.find(mydetid);
457  if (itr != corrFactor_.end()) icalconst = itr->second;
458  }
459  HFRecHit aHit(hfItr->id(),hfItr->energy()*icalconst,hfItr->time());
460 
461  double energyhit = aHit.energy();
462  DetId id = (*hfItr).detid();
463  HcalDetId hid = HcalDetId(id);
464  if (fill) {
465  for (unsigned int i = 0; i < hcalID_.size(); i++) {
466  if (hcalID_[i] == id.rawId()) {
467  histo_[i]->Fill(energyhit);
468  break;
469  }
470  }
471  if (fillHist_) {
472  std::map<HcalDetId,TH1D*>::iterator itr1 = histHC_.find(hid);
473  if (itr1 != histHC_.end()) itr1->second->Fill(energyhit);
474  }
475  h_[hid.subdet()-1]->Fill(energyhit);
476  }
477 
478  //
479  // Remove PMT hits
480  //
481  if (!fillHist_) {
482  if (((Noise_ || runNZS_) && fabs(energyhit) <= 40.) ||
483  (energyhit >= eLowHF_ && energyhit <= eHighHF_)) {
484  std::map<std::pair<int,HcalDetId>,myInfo>::iterator itr1 = myMap_.find(std::pair<int,HcalDetId>(algoBit,hid));
485  if (itr1 == myMap_.end()) {
486  myInfo info;
487  myMap_[std::pair<int,HcalDetId>(algoBit,hid)] = info;
488  itr1 = myMap_.find(std::pair<int,HcalDetId>(algoBit,hid));
489  }
490  itr1->second.theMB0++;
491  itr1->second.theMB1 += energyhit;
492  itr1->second.theMB2 += (energyhit*energyhit);
493  itr1->second.theMB3 += (energyhit*energyhit*energyhit);
494  itr1->second.theMB4 += (energyhit*energyhit*energyhit*energyhit);
495  itr1->second.runcheck = rnnum_;
496  }
497  }
498  }
499 }
500 
501 //define this as a plug-in
T getParameter(std::string const &) const
RecAnalyzerMinbias(const edm::ParameterSet &)
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
std::vector< int > trigbit_
static const TGPicture * info(bool iBackgroundIsBlack)
string fill
Definition: lumiContext.py:319
HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:49
FWCore Framework interface EventSetupRecordImplementation h
Helper function to determine trigger accepts.
std::map< HcalDetId, TH1D * > histHC_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:462
int maxDepthHE() const
Definition: HcalTopology.h:129
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
std::vector< HBHERecHit >::const_iterator const_iterator
std::map< DetId, double > corrFactor_
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
void analyzeHcal(const HBHERecHitCollection &, const HFRecHitCollection &, int, bool)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
std::vector< TH1D * > histo_
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
int iEvent
Definition: GenABIO.cc:230
float energy() const
Definition: CaloRecHit.h:17
edm::EDGetTokenT< HFRecHitCollection > tok_hfrecoMB_
RunNumber_t run() const
Definition: Event.h:93
HcalSubdetector
Definition: HcalAssistant.h:31
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
bool isValid() const
Definition: HandleBase.h:75
const_iterator end() const
Definition: DetId.h:18
edm::Service< TFileService > fs_
T const * product() const
Definition: Handle.h:81
const T & get() const
Definition: EventSetup.h:56
std::vector< unsigned int > hcalID_
T const * product() const
Definition: ESHandle.h:86
edm::EDGetTokenT< HBHERecHitCollection > tok_hbherecoMB_
virtual bool valid(const DetId &id) const
int maxDepthHB() const
Definition: HcalTopology.h:128
return(e1-e2)*(e1-e2)+dp *dp
size_type size() const
susybsm::HSCParticleCollection hc
Definition: classes.h:25
virtual void beginRun(edm::Run const &iRun, edm::EventSetup const &iSetup)
tuple cout
Definition: gather_cfg.py:145
std::map< std::pair< int, HcalDetId >, myInfo > myMap_
volatile std::atomic< bool > shutdown_flag false
bool isValid() const
Definition: ESHandle.h:47
edm::EDGetTokenT< L1GlobalTriggerObjectMapRecord > tok_hltL1GtMap_
const_iterator begin() const
Definition: Run.h:43