CMS 3D CMS Logo

RecAnalyzerHF.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
31 
33 
34 #include "TH1D.h"
35 #include "TTree.h"
36 
37 //#define EDM_ML_DEBUG
38 
39 // class declaration
40 class RecAnalyzerHF : public edm::one::EDAnalyzer<edm::one::SharedResources> {
41 public:
42  explicit RecAnalyzerHF(const edm::ParameterSet&);
43  ~RecAnalyzerHF() override = default;
44 
45  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
46 
47 private:
48  void analyze(edm::Event const&, edm::EventSetup const&) override;
49  void beginJob() override;
50  void endJob() override;
51 
52 private:
53  void analyzeHcal(const HFPreRecHitCollection&, int, bool);
54 
55  // ----------member data ---------------------------
57  double eLowHF_, eHighHF_;
58  std::vector<int> trigbit_;
59  std::vector<unsigned int> hcalID_;
60  TTree* myTree_;
61  TH1D* hist_[2];
62  std::vector<std::pair<TH1D*, TH1D*>> histo_;
63  double rnnum_;
64  struct myInfo {
65  double kount, f11, f12, f13, f14, f21, f22, f23, f24, runcheck;
66  myInfo() { kount = f11 = f12 = f13 = f14 = f21 = f22 = f23 = f24 = runcheck = 0; }
67  };
68  // Root tree members
69  double rnnumber;
73  std::map<std::pair<int, HcalDetId>, myInfo> myMap_;
76 };
77 
78 // constructors and destructor
80  : nzs_(iConfig.getParameter<bool>("RunNZS")),
81  noise_(iConfig.getParameter<bool>("Noise")),
82  ratio_(iConfig.getParameter<bool>("Ratio")),
83  ignoreL1_(iConfig.getUntrackedParameter<bool>("IgnoreL1", false)),
84  fillTree_(iConfig.getUntrackedParameter<bool>("FillTree", true)),
85  eLowHF_(iConfig.getParameter<double>("ELowHF")),
86  eHighHF_(iConfig.getParameter<double>("EHighHF")),
87  trigbit_(iConfig.getUntrackedParameter<std::vector<int>>("TriggerBits")),
88  tok_hfreco_(consumes<HFPreRecHitCollection>(iConfig.getParameter<edm::InputTag>("hfInput"))),
89  tok_hltL1GtMap_(consumes<L1GlobalTriggerObjectMapRecord>(edm::InputTag("hltL1GtObjectMap"))) {
90  usesResource("TFileService");
91  // get constants for the ID
92  std::vector<int> ieta = iConfig.getUntrackedParameter<std::vector<int>>("HcalIeta");
93  std::vector<int> iphi = iConfig.getUntrackedParameter<std::vector<int>>("HcalIphi");
94  std::vector<int> depth = iConfig.getUntrackedParameter<std::vector<int>>("HcalDepth");
95 
96  edm::LogVerbatim("RecAnalyzerHF") << " Flags (IgnoreL1): " << ignoreL1_ << " (NZS) " << nzs_ << " (Noise) " << noise_
97  << " (Ratio) " << ratio_;
98  edm::LogVerbatim("RecAnalyzerHF") << "Thresholds for HF " << eLowHF_ << ":" << eHighHF_;
99  for (unsigned int k = 0; k < ieta.size(); ++k) {
100  if (std::abs(ieta[k]) >= 29) {
101  unsigned int id = (HcalDetId(HcalForward, ieta[k], iphi[k], depth[k])).rawId();
102  hcalID_.push_back(id);
103  edm::LogVerbatim("RecAnalyzerHF") << "DetId[" << k << "] " << HcalDetId(id);
104  }
105  }
106  edm::LogVerbatim("RecAnalyzerHF") << "Select on " << trigbit_.size() << " L1 Trigger selection";
107  unsigned int k(0);
108  for (auto trig : trigbit_) {
109  edm::LogVerbatim("RecAnalyzerHF") << "Bit[" << k << "] " << trig;
110  ++k;
111  }
112 }
113 
114 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
117  desc.add<bool>("RunNZS", true);
118  desc.add<bool>("Noise", false);
119  desc.add<bool>("Ratio", false);
120  desc.add<double>("ELowHF", 10);
121  desc.add<double>("EHighHF", 150);
122  std::vector<int> idummy;
123  desc.addUntracked<std::vector<int>>("TriggerBits", idummy);
124  desc.addUntracked<bool>("IgnoreL1", false);
125  desc.addUntracked<bool>("FillHisto", false);
126  desc.addUntracked<std::vector<int>>("HcalIeta", idummy);
127  desc.addUntracked<std::vector<int>>("HcalIphi", idummy);
128  desc.addUntracked<std::vector<int>>("HcalDepth", idummy);
129  desc.add<edm::InputTag>("hfInput", edm::InputTag("hfprereco"));
130  descriptions.add("recAnalyzerHF", desc);
131 }
132 
135  char name[700], title[700];
136  double xmin(-10.0), xmax(90.0);
137  if (ratio_) {
138  xmin = -5.0;
139  xmax = 5.0;
140  }
141  for (int i = 0; i < 2; ++i) {
142  sprintf(name, "HF%d", i);
143  sprintf(title, "The metric F%d for HF", i + 1);
144  hist_[i] = fs->make<TH1D>(name, title, 50, xmin, xmax);
145  }
146 
147  for (const auto& id : hcalID_) {
148  HcalDetId hid = HcalDetId(id);
149  TH1D *h1(nullptr), *h2(nullptr);
150  for (int i = 0; i < 2; ++i) {
151  sprintf(name, "HF%d%d_%d_%d", i, hid.ieta(), hid.iphi(), hid.depth());
152  sprintf(title, "The metric F%d for HF i#eta %d i#phi %d depth %d", i + 1, hid.ieta(), hid.iphi(), hid.depth());
153  if (i == 0)
154  h1 = fs->make<TH1D>(name, title, 50, xmin, xmax);
155  else
156  h2 = fs->make<TH1D>(name, title, 50, xmin, xmax);
157  }
158  histo_.push_back(std::pair<TH1D*, TH1D*>(h1, h2));
159  };
160 
161  if (fillTree_) {
162  myTree_ = fs->make<TTree>("RecJet", "RecJet Tree");
163  myTree_->Branch("cells", &cells, "cells/I");
164  myTree_->Branch("mysubd", &mysubd, "mysubd/I");
165  myTree_->Branch("depth", &depth, "depth/I");
166  myTree_->Branch("ieta", &ieta, "ieta/I");
167  myTree_->Branch("iphi", &iphi, "iphi/I");
168  myTree_->Branch("mom0_F1", &mom0_F1, "mom0_F1/F");
169  myTree_->Branch("mom1_F1", &mom1_F1, "mom1_F1/F");
170  myTree_->Branch("mom2_F1", &mom2_F1, "mom2_F1/F");
171  myTree_->Branch("mom3_F1", &mom3_F1, "mom3_F1/F");
172  myTree_->Branch("mom4_F1", &mom4_F1, "mom4_F1/F");
173  myTree_->Branch("mom0_F2", &mom0_F2, "mom0_F2/F");
174  myTree_->Branch("mom1_F2", &mom1_F2, "mom1_F2/F");
175  myTree_->Branch("mom2_F2", &mom2_F2, "mom2_F2/F");
176  myTree_->Branch("mom3_F2", &mom3_F2, "mom3_F2/F");
177  myTree_->Branch("mom4_F2", &mom4_F2, "mom4_F2/F");
178  myTree_->Branch("trigbit", &trigbit, "trigbit/I");
179  myTree_->Branch("rnnumber", &rnnumber, "rnnumber/D");
180  }
181 
182  myMap_.clear();
183 }
184 
186  if (fillTree_) {
187  cells = 0;
188  for (const auto& itr : myMap_) {
189  edm::LogVerbatim("RecAnalyzerHF") << "Fired trigger bit number " << itr.first.first;
190  myInfo info = itr.second;
191  if (info.kount > 0) {
192  mom0_F1 = info.kount;
193  mom1_F1 = info.f11;
194  mom2_F1 = info.f12;
195  mom3_F1 = info.f13;
196  mom4_F1 = info.f14;
197  mom0_F2 = info.kount;
198  mom1_F2 = info.f21;
199  mom2_F2 = info.f22;
200  mom3_F2 = info.f23;
201  mom4_F2 = info.f24;
202  rnnumber = info.runcheck;
203  trigbit = itr.first.first;
204  mysubd = itr.first.second.subdet();
205  depth = itr.first.second.depth();
206  iphi = itr.first.second.iphi();
207  ieta = itr.first.second.ieta();
208  edm::LogVerbatim("RecAnalyzerHF")
209  << " Result= " << trigbit << " " << mysubd << " " << ieta << " " << iphi << " F1:mom0 " << mom0_F1
210  << " mom1 " << mom1_F1 << " mom2 " << mom2_F1 << " mom3 " << mom3_F1 << " mom4 " << mom4_F1 << " F2:mom0 "
211  << mom0_F2 << " mom1 " << mom1_F2 << " mom2 " << mom2_F2 << " mom3 " << mom3_F2 << " mom4 " << mom4_F2;
212  myTree_->Fill();
213  cells++;
214  }
215  }
216  edm::LogVerbatim("RecAnalyzerHF") << "cells"
217  << " " << cells;
218  }
219 #ifdef EDM_ML_DEBUG
220  edm::LogVerbatim("RecAnalyzerHF") << "Exiting from RecAnalyzerHF::endjob";
221 #endif
222 }
223 
224 //
225 // member functions
226 //
227 // ------------ method called to produce the data ------------
228 
230  rnnum_ = (double)iEvent.run();
231 
233  if (!hf.isValid()) {
234  edm::LogWarning("RecAnalyzerHF") << "HcalCalibAlgos: Error! can't get hf product!";
235  return;
236  }
237  const HFPreRecHitCollection Hithf = *(hf.product());
238  edm::LogVerbatim("RecAnalyzerHF") << "HF MB size of collection " << Hithf.size();
239  if (Hithf.size() < 1700 && nzs_) {
240  edm::LogWarning("RecAnalyzerHF") << "HF problem " << rnnum_ << " size " << Hithf.size();
241  }
242 
243  bool select(false);
244  if (!trigbit_.empty()) {
245  const edm::Handle<L1GlobalTriggerObjectMapRecord>& gtObjectMapRecord = iEvent.getHandle(tok_hltL1GtMap_);
246  if (gtObjectMapRecord.isValid()) {
247  const std::vector<L1GlobalTriggerObjectMap>& objMapVec = gtObjectMapRecord->gtObjectMap();
248  for (std::vector<L1GlobalTriggerObjectMap>::const_iterator itMap = objMapVec.begin(); itMap != objMapVec.end();
249  ++itMap) {
250  bool resultGt = (*itMap).algoGtlResult();
251  if (resultGt) {
252  int algoBit = (*itMap).algoBitNumber();
253  if (std::find(trigbit_.begin(), trigbit_.end(), algoBit) != trigbit_.end()) {
254  select = true;
255  break;
256  }
257  }
258  }
259  }
260  }
261 
262  //event weight for FLAT sample and PU information
263 
264  if (ignoreL1_ || (!trigbit_.empty() && select)) {
265  analyzeHcal(Hithf, 1, true);
266  } else if ((!ignoreL1_) && (trigbit_.empty())) {
267  const edm::Handle<L1GlobalTriggerObjectMapRecord>& gtObjectMapRecord = iEvent.getHandle(tok_hltL1GtMap_);
268  if (gtObjectMapRecord.isValid()) {
269  const std::vector<L1GlobalTriggerObjectMap>& objMapVec = gtObjectMapRecord->gtObjectMap();
270  bool ok(false);
271  for (std::vector<L1GlobalTriggerObjectMap>::const_iterator itMap = objMapVec.begin(); itMap != objMapVec.end();
272  ++itMap) {
273  bool resultGt = (*itMap).algoGtlResult();
274  if (resultGt) {
275  int algoBit = (*itMap).algoBitNumber();
276  analyzeHcal(Hithf, algoBit, (!ok));
277  ok = true;
278  }
279  }
280  if (!ok) {
281  edm::LogVerbatim("RecAnalyzerHF") << "No passed L1 Trigger found";
282  }
283  }
284  }
285 }
286 
287 void RecAnalyzerHF::analyzeHcal(const HFPreRecHitCollection& Hithf, int algoBit, bool fill) {
288 #ifdef EDM_ML_DEBUG
289  edm::LogVerbatim("RecAnalyzerHF") << "Enter analyzeHcal for bit " << algoBit << " Fill " << fill
290  << " Collection size " << Hithf.size();
291 #endif
292  // Signal part for HF
293  for (const auto& hfItr : Hithf) {
294  HcalDetId hid = hfItr.id();
295  double e0 = (hfItr.getHFQIE10Info(0) == nullptr) ? 0 : hfItr.getHFQIE10Info(0)->energy();
296  double e1 = (hfItr.getHFQIE10Info(1) == nullptr) ? 0 : hfItr.getHFQIE10Info(1)->energy();
297  double energy = e0 + e1;
298  if (std::abs(energy) < 1e-6)
299  energy = (energy > 0) ? 1e-6 : -1e-6;
300  double f1(e0), f2(e1);
301  if (ratio_) {
302  f1 /= energy;
303  f2 /= energy;
304  }
305 #ifdef EDM_ML_DEBUG
306  edm::LogVerbatim("RecAnalyzerHF") << hid << " E " << e0 << ":" << e1 << " F " << f1 << ":" << f2;
307 #endif
308  if (fill) {
309  for (unsigned int i = 0; i < hcalID_.size(); i++) {
310  if (hcalID_[i] == hid.rawId()) {
311  histo_[i].first->Fill(f1);
312  histo_[i].second->Fill(f2);
313  break;
314  }
315  }
316  hist_[0]->Fill(f1);
317  hist_[1]->Fill(f2);
318  }
319 
320  //
321  // Remove PMT hits
322  //
323  if (((noise_ || nzs_) && fabs(energy) <= 40.) || (energy >= eLowHF_ && energy <= eHighHF_)) {
324  std::map<std::pair<int, HcalDetId>, myInfo>::iterator itr1 = myMap_.find(std::pair<int, HcalDetId>(algoBit, hid));
325  if (itr1 == myMap_.end()) {
326  myInfo info;
327  myMap_[std::pair<int, HcalDetId>(algoBit, hid)] = info;
328  itr1 = myMap_.find(std::pair<int, HcalDetId>(algoBit, hid));
329  }
330  itr1->second.kount += 1.0;
331  itr1->second.f11 += (f1);
332  itr1->second.f12 += (f1 * f1);
333  itr1->second.f13 += (f1 * f1 * f1);
334  itr1->second.f14 += (f1 * f1 * f1 * f1);
335  itr1->second.f21 += (f2);
336  itr1->second.f22 += (f2 * f2);
337  itr1->second.f23 += (f2 * f2 * f2);
338  itr1->second.f24 += (f2 * f2 * f2 * f2);
339  itr1->second.runcheck = rnnum_;
340  }
341  }
342 }
343 
344 //define this as a plug-in
static const char noise_[]
Log< level::Info, true > LogVerbatim
TH1D * hist_[2]
static const TGPicture * info(bool iBackgroundIsBlack)
std::map< std::pair< int, HcalDetId >, myInfo > myMap_
void endJob() override
size_type size() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< unsigned int > hcalID_
std::vector< int > trigbit_
~RecAnalyzerHF() override=default
void analyze(edm::Event const &, edm::EventSetup const &) override
void beginJob() override
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
const std::vector< L1GlobalTriggerObjectMap > & gtObjectMap() const
get / set the vector of object maps
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< std::pair< TH1D *, TH1D * > > histo_
constexpr int ieta() const
get the cell ieta
Definition: HcalDetId.h:155
RecAnalyzerHF(const edm::ParameterSet &)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::EDGetTokenT< HFPreRecHitCollection > tok_hfreco_
constexpr uint32_t rawId() const
get the raw id
Definition: DetId.h:57
void add(std::string const &label, ParameterSetDescription const &psetDescription)
bool isValid() const
Definition: HandleBase.h:70
HLT enums.
edm::EDGetTokenT< L1GlobalTriggerObjectMapRecord > tok_hltL1GtMap_
Log< level::Warning, false > LogWarning
void analyzeHcal(const HFPreRecHitCollection &, int, bool)
constexpr int iphi() const
get the cell iphi
Definition: HcalDetId.h:157
constexpr int depth() const
get the tower depth
Definition: HcalDetId.h:164