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
27 
28 #include "TH1F.h"
29 #include "TFile.h"
30 #include "TTree.h"
31 
32 // class declaration
34 
35 public:
36  explicit RecAnalyzerMinbias(const edm::ParameterSet&);
38  virtual void analyze(const edm::Event&, const edm::EventSetup&);
39  virtual void beginJob() ;
40  virtual void endJob() ;
41 
42 private:
43  void analyzeHcal(const HBHERecHitCollection&, const HFRecHitCollection&, int);
44 
45  // ----------member data ---------------------------
46  char name[700], title[700];
50  double eLowHF_, eHighHF_;
51  std::map<DetId,double> corrFactor_;
52  TFile *hOutputFile ;
53  TTree *myTree;
54  std::vector<TH1D *> histo;
55  std::vector<unsigned int> hcalID;
56 
57  // Root tree members
58  double rnnum, rnnumber;
61  struct myInfo{
63  void MyInfo() {
64  theMB0 = theMB1 = theMB2 = theMB3 = theMB4 = runcheck = 0;
65  }
66  };
67  std::map<std::pair<int,HcalDetId>,myInfo> myMap;
71 };
72 
73 // constructors and destructor
75 
76  // get name of output file with histogramms
77  fOutputFileName = iConfig.getUntrackedParameter<std::string>("HistOutFile");
78  ignoreL1 = iConfig.getUntrackedParameter<bool>("IgnoreL1", false);
79  std::string cfile= iConfig.getUntrackedParameter<std::string>("CorrFile");
80  std::vector<int> ieta = iConfig.getUntrackedParameter<std::vector<int>>("HcalIeta");
81  std::vector<int> iphi = iConfig.getUntrackedParameter<std::vector<int>>("HcalIphi");
82  std::vector<int> depth= iConfig.getUntrackedParameter<std::vector<int>>("HcalDepth");
83  runNZS_ = iConfig.getParameter<bool>("RunNZS");
84  eLowHB_ = iConfig.getParameter<double>("ELowHB");
85  eHighHB_ = iConfig.getParameter<double>("EHighHB");
86  eLowHE_ = iConfig.getParameter<double>("ELowHE");
87  eHighHE_ = iConfig.getParameter<double>("EHighHE");
88  eLowHF_ = iConfig.getParameter<double>("ELowHF");
89  eHighHF_ = iConfig.getParameter<double>("EHighHF");
90 
91  // get token names of modules, producing object collections
92  tok_hbherecoMB_ = consumes<HBHERecHitCollection>(iConfig.getParameter<edm::InputTag>("hbheInputMB"));
93  tok_hfrecoMB_ = consumes<HFRecHitCollection>(iConfig.getParameter<edm::InputTag>("hfInputMB"));
94  tok_hltL1GtMap_ = consumes<L1GlobalTriggerObjectMapRecord>(edm::InputTag("hltL1GtObjectMap"));
95 
96  // Read correction factors
97  std::ifstream infile(cfile);
98  if (!infile.is_open()) {
99  theRecalib = false;
100  edm::LogInfo("AnalyzerMB") << "Cannot open '" << cfile
101  << "' for the correction file";
102  } else {
103  unsigned int ndets(0), nrec(0);
104  while(1) {
105  unsigned int id;
106  double cfac;
107  infile >> id >> cfac;
108  if (!infile.good()) break;
109  HcalDetId detId(id);
110  nrec++;
111  std::map<DetId,double>::iterator itr = corrFactor_.find(detId);
112  if (itr == corrFactor_.end()) {
113  corrFactor_[detId] = cfac;
114  ndets++;
115  }
116  }
117  infile.close();
118  edm::LogInfo("AnalyzerMB") << "Reads " << nrec << " correction factors for "
119  << ndets << " detIds";
120  theRecalib = (ndets>0);
121  }
122 
123  edm::LogInfo("AnalyzerMB") << "Output File: " << fOutputFileName
124  << " Flags (ReCalib): " << theRecalib
125  << " (IgnoreL1): " << ignoreL1 << " (NZS) "
126  << runNZS_ << " and with " << ieta.size()
127  << " detId for full histogram";
128  edm::LogInfo("AnalyzerMB") << "Thresholds for HB " << eLowHB_ << ":"
129  << eHighHB_ << " for HE " << eLowHE_ << ":"
130  << eHighHE_ << " for HF " << eLowHF_ << ":"
131  << eHighHF_;
132  for (unsigned int k=0; k<ieta.size(); ++k) {
133  HcalSubdetector subd = ((std::abs(ieta[k]) > 29) ? HcalForward :
134  (std::abs(ieta[k]) > 16) ? HcalEndcap :
135  ((std::abs(ieta[k]) == 16) && (depth[k] == 3)) ? HcalEndcap :
136  (depth[k] == 4) ? HcalOuter : HcalBarrel);
137  unsigned int id = (HcalDetId(subd,ieta[k],iphi[k],depth[k])).rawId();
138  hcalID.push_back(id);
139  edm::LogInfo("AnalyzerMB") << "DetId[" << k << "] " << HcalDetId(id);
140  }
141 }
142 
144 
146  std::string hc[5] = {"Empty", "HB", "HE", "HO", "HF"};
147  for (unsigned int i=0; i<hcalID.size(); i++) {
148  HcalDetId id = HcalDetId(hcalID[i]);
149  int subdet = id.subdetId();
150  sprintf (name, "%s%d_%d_%d", hc[subdet].c_str(), id.ieta(), id.iphi(), id.depth());
151  sprintf (title, "Energy Distribution for %s ieta %d iphi %d depth %d", hc[subdet].c_str(), id.ieta(), id.iphi(), id.depth());
152  double xmin = (subdet == 4) ? -10 : -1;
153  double xmax = (subdet == 4) ? 90 : 9;
154  TH1D* hh = new TH1D(name, title, 50, xmin, xmax);
155  histo.push_back(hh);
156  };
157 
158  hOutputFile = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
159  myTree = new TTree("RecJet","RecJet Tree");
160  myTree->Branch("cells", &cells, "cells/I");
161  myTree->Branch("mysubd", &mysubd, "mysubd/I");
162  myTree->Branch("depth", &depth, "depth/I");
163  myTree->Branch("ieta", &ieta, "ieta/I");
164  myTree->Branch("iphi", &iphi, "iphi/I");
165  myTree->Branch("mom0_MB", &mom0_MB, "mom0_MB/F");
166  myTree->Branch("mom1_MB", &mom1_MB, "mom1_MB/F");
167  myTree->Branch("mom2_MB", &mom2_MB, "mom2_MB/F");
168  myTree->Branch("mom3_MB", &mom2_MB, "mom3_MB/F");
169  myTree->Branch("mom4_MB", &mom4_MB, "mom4_MB/F");
170  myTree->Branch("trigbit", &trigbit, "trigbit/I");
171  myTree->Branch("rnnumber", &rnnumber, "rnnumber/D");
172  myMap.clear();
173  return ;
174 }
175 
176 // EndJob
177 //
178 
180  cells = 0;
181  for (std::map<std::pair<int,HcalDetId>,myInfo>::const_iterator itr=myMap.begin(); itr != myMap.end(); ++itr) {
182  edm::LogInfo("AnalyzerMB") << "Fired trigger bit number "<<itr->first.first;
183  myInfo info = itr->second;
184  if (info.theMB0 > 0) {
185  mom0_MB = info.theMB0;
186  mom1_MB = info.theMB1;
187  mom2_MB = info.theMB2;
188  mom3_MB = info.theMB3;
189  mom4_MB = info.theMB4;
190  rnnumber = info.runcheck;
191  trigbit = itr->first.first;
192  mysubd = itr->first.second.subdet();
193  depth = itr->first.second.depth();
194  iphi = itr->first.second.iphi();
195  ieta = itr->first.second.ieta();
196  edm::LogInfo("AnalyzerMB") << " Result= " << trigbit << " " << mysubd
197  << " " << ieta << " " << iphi << " mom0 "
198  << mom0_MB << " mom1 " << mom1_MB << " mom2 "
199  << mom2_MB << " mom3 " << mom3_MB << " mom4 "
200  << mom4_MB;
201  myTree->Fill();
202  cells++;
203  }
204  }
205  edm::LogInfo("AnalyzerMB") << "cells" << " " << cells;
206 
207  hOutputFile->Write();
208  hOutputFile->cd();
209  for(unsigned int i = 0; i<histo.size(); i++){
210  histo[i]->Write();
211  }
212  myTree->Write();
213  hOutputFile->Close() ;
214 }
215 
216 //
217 // member functions
218 //
219 // ------------ method called to produce the data ------------
220 
222 
223  rnnum = (float)iEvent.run();
224 
226  iEvent.getByToken(tok_hbherecoMB_, hbheMB);
227  if (!hbheMB.isValid()) {
228  edm::LogWarning("AnalyzerMB") << "HcalCalibAlgos: Error! can't get hbhe product!";
229  return ;
230  }
231  const HBHERecHitCollection HithbheMB = *(hbheMB.product());
232  edm::LogInfo("AnalyzerMB") << "HBHE MB size of collection "<<HithbheMB.size();
233  if (HithbheMB.size() < 5100 && runNZS_) {
234  edm::LogWarning("AnalyzerMB") << "HBHE problem " << rnnum << " size "
235  << HithbheMB.size();
236  return;
237  }
238 
240  iEvent.getByToken(tok_hfrecoMB_, hfMB);
241  if (!hfMB.isValid()) {
242  edm::LogWarning("AnalyzerMB") << "HcalCalibAlgos: Error! can't get hbhe product!";
243  return ;
244  }
245  const HFRecHitCollection HithfMB = *(hfMB.product());
246  edm::LogInfo("AnalyzerMB") << "HF MB size of collection " << HithfMB.size();
247  if (HithfMB.size() < 1700 && runNZS_) {
248  edm::LogWarning("AnalyzerMB") << "HF problem " << rnnum << " size "
249  << HithfMB.size();
250  return;
251  }
252 
253  if (ignoreL1) {
254  analyzeHcal(HithbheMB, HithfMB, 1);
255  } else {
257  iEvent.getByToken(tok_hltL1GtMap_, gtObjectMapRecord);
258  if (gtObjectMapRecord.isValid()) {
259  const std::vector<L1GlobalTriggerObjectMap>& objMapVec = gtObjectMapRecord->gtObjectMap();
260  bool ok(false);
261  for (std::vector<L1GlobalTriggerObjectMap>::const_iterator itMap = objMapVec.begin();
262  itMap != objMapVec.end(); ++itMap) {
263  bool resultGt = (*itMap).algoGtlResult();
264  if (resultGt) {
265  int algoBit = (*itMap).algoBitNumber();
266  analyzeHcal(HithbheMB, HithfMB, algoBit);
267  ok = true;
268  }
269  }
270  if (!ok) {
271  edm::LogInfo("AnalyzerMB") << "No passed L1 Trigger found";
272  }
273  }
274  }
275 }
276 
278  const HFRecHitCollection & HithfMB,
279  int algoBit) {
280  // Signal part for HB HE
281  for (HBHERecHitCollection::const_iterator hbheItr=HithbheMB.begin();
282  hbheItr!=HithbheMB.end(); hbheItr++) {
283  // Recalibration of energy
284  DetId mydetid = hbheItr->id().rawId();
285  double icalconst(1.);
286  if (theRecalib) {
287  std::map<DetId,double>::iterator itr = corrFactor_.find(mydetid);
288  if (itr != corrFactor_.end()) icalconst = itr->second;
289  }
290  HBHERecHit aHit(hbheItr->id(),hbheItr->energy()*icalconst,hbheItr->time());
291  double energyhit = aHit.energy();
292  DetId id = (*hbheItr).detid();
293  HcalDetId hid = HcalDetId(id);
294  double eLow = (hid.subdet() == HcalEndcap) ? eLowHE_ : eLowHB_;
295  double eHigh = (hid.subdet() == HcalEndcap) ? eHighHE_ : eHighHB_;
296  for (unsigned int i = 0; i < hcalID.size(); i++) {
297  if (hcalID[i] == id.rawId()) {
298  histo[i]->Fill(energyhit);
299  break;
300  }
301  }
302  if (runNZS_ || (energyhit >= eLow && energyhit <= eHigh)) {
303  std::map<std::pair<int,HcalDetId>,myInfo>::iterator itr1 = myMap.find(std::pair<int,HcalDetId>(algoBit,hid));
304  if (itr1 == myMap.end()) {
305  myInfo info;
306  myMap[std::pair<int,HcalDetId>(algoBit,hid)] = info;
307  itr1 = myMap.find(std::pair<int,HcalDetId>(algoBit,hid));
308  }
309  itr1->second.theMB0++;
310  itr1->second.theMB1 += energyhit;
311  itr1->second.theMB2 += (energyhit*energyhit);
312  itr1->second.theMB3 += (energyhit*energyhit*energyhit);
313  itr1->second.theMB4 += (energyhit*energyhit*energyhit*energyhit);
314  itr1->second.runcheck = rnnum;
315  }
316  } // HBHE_MB
317 
318  // Signal part for HF
319  for (HFRecHitCollection::const_iterator hfItr=HithfMB.begin();
320  hfItr!=HithfMB.end(); hfItr++) {
321  // Recalibration of energy
322  DetId mydetid = hfItr->id().rawId();
323  double icalconst(1.);
324  if (theRecalib) {
325  std::map<DetId,double>::iterator itr = corrFactor_.find(mydetid);
326  if (itr != corrFactor_.end()) icalconst = itr->second;
327  }
328  HFRecHit aHit(hfItr->id(),hfItr->energy()*icalconst,hfItr->time());
329 
330  double energyhit = aHit.energy();
331  DetId id = (*hfItr).detid();
332  HcalDetId hid = HcalDetId(id);
333  for (unsigned int i = 0; i < hcalID.size(); i++) {
334  if (hcalID[i] == id.rawId()) {
335  histo[i]->Fill(energyhit);
336  break;
337  }
338  }
339  //
340  // Remove PMT hits
341  //
342  if ((runNZS_ && fabs(energyhit) <= 40.) ||
343  (energyhit >= eLowHF_ && energyhit <= eHighHF_)) {
344  std::map<std::pair<int,HcalDetId>,myInfo>::iterator itr1 = myMap.find(std::pair<int,HcalDetId>(algoBit,hid));
345  if (itr1 == myMap.end()) {
346  myInfo info;
347  myMap[std::pair<int,HcalDetId>(algoBit,hid)] = info;
348  itr1 = myMap.find(std::pair<int,HcalDetId>(algoBit,hid));
349  }
350  itr1->second.theMB0++;
351  itr1->second.theMB1 += energyhit;
352  itr1->second.theMB2 += (energyhit*energyhit);
353  itr1->second.theMB3 += (energyhit*energyhit*energyhit);
354  itr1->second.theMB4 += (energyhit*energyhit*energyhit*energyhit);
355  itr1->second.runcheck = rnnum;
356  }
357  }
358 }
359 
360 //define this as a plug-in
std::vector< unsigned int > hcalID
T getParameter(std::string const &) const
RecAnalyzerMinbias(const edm::ParameterSet &)
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
static const TGPicture * info(bool iBackgroundIsBlack)
HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:45
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
std::vector< HBHERecHit >::const_iterator const_iterator
std::map< DetId, double > corrFactor_
virtual void analyze(const edm::Event &, const edm::EventSetup &)
return((rh^lh)&mask)
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
void analyzeHcal(const HBHERecHitCollection &, const HFRecHitCollection &, int)
int iEvent
Definition: GenABIO.cc:230
float energy() const
Definition: CaloRecHit.h:17
std::map< std::pair< int, HcalDetId >, myInfo > myMap
edm::EDGetTokenT< HFRecHitCollection > tok_hfrecoMB_
RunNumber_t run() const
Definition: Event.h:87
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
T const * product() const
Definition: Handle.h:81
edm::EDGetTokenT< HBHERecHitCollection > tok_hbherecoMB_
size_type size() const
list infile
Definition: EdgesToViz.py:90
susybsm::HSCParticleCollection hc
Definition: classes.h:25
edm::EDGetTokenT< L1GlobalTriggerObjectMapRecord > tok_hltL1GtMap_
std::vector< TH1D * > histo
const_iterator begin() const