CMS 3D CMS Logo

HcalIsoTrackAnalyzer.cc
Go to the documentation of this file.
1 // system include files
2 #include <string>
3 #include <vector>
4 
5 // Root objects
6 #include "TTree.h"
7 
11 
14 
17 
20 
26 
29 
30 //#define EDM_ML_DEBUG
31 
32 class HcalIsoTrackAnalyzer : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
33 public:
34  explicit HcalIsoTrackAnalyzer(edm::ParameterSet const&);
35  ~HcalIsoTrackAnalyzer() override;
36 
37  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
38 
39 private:
40  void analyze(edm::Event const&, edm::EventSetup const&) override;
41  void beginJob() override;
42  void beginRun(edm::Run const&, edm::EventSetup const&) override;
43  void endRun(edm::Run const&, edm::EventSetup const&) override;
44  double respCorr(const DetId& id);
45  double gainFactor(const HcalDbService* dbserv, const HcalDetId& id);
46 
47  const double pTrackLow_, pTrackHigh_;
49  const bool fillInRange_;
51  const std::vector<int> debEvents_;
59 
60  unsigned int nRun_, nRange_, nLow_, nHigh_;
61 
62  TTree *tree, *tree2;
68  double t_mindR1, t_mindR2;
74  std::vector<unsigned int> t_DetIds, t_DetIds1, t_DetIds3;
76  std::vector<bool> t_trgbits;
77 
78  unsigned int t_RunNo, t_EventNo;
82  std::vector<int> t_ietaAll, t_ietaGood, t_trackType;
83  std::vector<bool> t_hltbits;
84 };
85 
87  : pTrackLow_(iConfig.getParameter<double>("momentumLow")),
88  pTrackHigh_(iConfig.getParameter<double>("momentumHigh")),
89  useRaw_(iConfig.getUntrackedParameter<int>("useRaw", 0)),
90  dataType_(iConfig.getUntrackedParameter<int>("dataType", 0)),
91  unCorrect_(iConfig.getUntrackedParameter<int>("unCorrect", 0)),
92  fillInRange_(iConfig.getUntrackedParameter<bool>("fillInRange", false)),
93  labelIsoTkVar_(iConfig.getParameter<edm::InputTag>("isoTrackVarLabel")),
94  labelIsoTkEvt_(iConfig.getParameter<edm::InputTag>("isoTrackEvtLabel")),
95  debEvents_(iConfig.getParameter<std::vector<int>>("debugEvents")),
96  tok_htopo_(esConsumes<HcalTopology, HcalRecNumberingRecord, edm::Transition::BeginRun>()),
97  tok_respcorr_(esConsumes<HcalRespCorrs, HcalRespCorrsRcd, edm::Transition::BeginRun>()),
98  tok_dbservice_(esConsumes<HcalDbService, HcalDbRecord>()),
99  tokIsoTrkVar_(consumes<HcalIsoTrkCalibVariablesCollection>(labelIsoTkVar_)),
100  tokIsoTrkEvt_(consumes<HcalIsoTrkEventVariablesCollection>(labelIsoTkEvt_)),
101  theHBHETopology_(nullptr),
102  respCorrs_(nullptr),
103  nRun_(0),
104  nRange_(0),
105  nLow_(0),
106  nHigh_(0) {
107  usesResource(TFileService::kSharedResource);
108 
109  //now do whatever initialization is needed
110  edm::LogVerbatim("HcalIsoTrack") << "Labels used " << labelIsoTkVar_ << " " << labelIsoTkEvt_;
111 
112  edm::LogVerbatim("HcalIsoTrack") << "Parameters read from config file \n\t momentumLow_ " << pTrackLow_
113  << "\t momentumHigh_ " << pTrackHigh_ << "\t useRaw_ " << useRaw_
114  << "\t dataType_ " << dataType_ << "\t unCorrect " << unCorrect_
115  << "\t fillInRange " << fillInRange_ << "\t and " << debEvents_.size()
116  << " events to be debugged";
117 }
118 
120  if (respCorrs_)
121  delete respCorrs_;
122 }
123 
126  t_Run = iEvent.id().run();
127  t_Event = iEvent.id().event();
129 #ifdef EDM_ML_DEBUG
130  bool debug = (debEvents_.empty())
131  ? true
132  : (std::find(debEvents_.begin(), debEvents_.end(), iEvent.id().event()) != debEvents_.end());
133  if (debug)
134  edm::LogVerbatim("HcalIsoTrack") << "Run " << t_Run << " Event " << t_Event << " type " << t_DataType
135  << " Luminosity " << iEvent.luminosityBlock() << " Bunch "
136  << iEvent.bunchCrossing();
137 #endif
138 
139  // Fill from IsoTrkCalibVariables collection
140  auto const& isotrkCalibColl = iEvent.getHandle(tokIsoTrkVar_);
141  if (isotrkCalibColl.isValid()) {
142  auto isotrkCalib = *isotrkCalibColl.product();
143 #ifdef EDM_ML_DEBUG
144  if (debug)
145  edm::LogVerbatim("HcalIsoTrack") << "Finds HcalIsoTrkCalibVariablesCollection with " << isotrkCalib.size()
146  << " entries";
147  int k(0);
148 #endif
149  for (const auto& itr : isotrkCalib) {
150  t_ieta = itr.ieta_;
151  t_iphi = itr.iphi_;
152  t_goodPV = itr.goodPV_;
153  t_nVtx = itr.nVtx_;
154  t_nTrk = itr.nTrk_;
155  t_EventWeight = itr.eventWeight_;
156  t_p = itr.p_;
157  t_pt = itr.pt_;
158  t_phi = itr.phi_;
159 #ifdef EDM_ML_DEBUG
160  ++k;
161  if (debug)
162  edm::LogVerbatim("HcalIsoTrack") << "Track " << k << " p:pt:phi " << t_p << ":" << t_pt << ":" << t_phi
163  << " nvtx:ntrk:goodPV:wt " << t_nVtx << ":" << t_nTrk << ":" << t_goodPV << ":"
164  << t_EventWeight << " ieta:iphi " << t_ieta << ":" << t_iphi;
165 #endif
166  t_l1pt = itr.l1pt_;
167  t_l1eta = itr.l1eta_;
168  t_l1phi = itr.l1phi_;
169  t_l3pt = itr.l3pt_;
170  t_l3eta = itr.l3eta_;
171  t_l3phi = itr.l3phi_;
172  t_mindR1 = itr.mindR1_;
173  t_mindR2 = itr.mindR2_;
174 #ifdef EDM_ML_DEBUG
175  if (debug)
176  edm::LogVerbatim("HcalIsoTrack") << "L1 pt:eta:phi " << t_l1pt << ":" << t_l1eta << ":" << t_l1phi
177  << " L3 pt:eta:phi " << t_l3pt << ":" << t_l3eta << ":" << t_l3phi << " R1:R2 "
178  << t_mindR1 << ":" << t_mindR2;
179 #endif
180  t_eMipDR = itr.eMipDR_[0];
181  t_eMipDR2 = itr.eMipDR_[1];
182  t_eMipDR3 = itr.eMipDR_[2];
183  t_eMipDR4 = itr.eMipDR_[3];
184  t_eMipDR5 = itr.eMipDR_[4];
185 #ifdef EDM_ML_DEBUG
186  if (debug)
187  edm::LogVerbatim("HcalIsoTrack") << "eMIPDR 1:2:3:4:5 " << t_eMipDR << ":" << t_eMipDR2 << ":" << t_eMipDR3
188  << ":" << t_eMipDR4 << ":" << t_eMipDR5;
189 #endif
190  t_hmaxNearP = itr.hmaxNearP_;
191  t_emaxNearP = itr.emaxNearP_;
192  t_eAnnular = itr.eAnnular_;
193  t_hAnnular = itr.hAnnular_;
194 #ifdef EDM_ML_DEBUG
195  if (debug)
196  edm::LogVerbatim("HcalIsoTrack") << "emaxNearP:hmaxNearP " << t_emaxNearP << ":" << t_hmaxNearP
197  << " eAnnlar:hAnnular" << t_eAnnular << ":" << t_hAnnular;
198 #endif
199  t_gentrackP = itr.gentrackP_;
200  t_rhoh = itr.rhoh_;
201  t_selectTk = itr.selectTk_;
202  t_qltyFlag = itr.qltyFlag_;
203  t_qltyMissFlag = itr.qltyMissFlag_;
204  t_qltyPVFlag = itr.qltyPVFlag_;
205 #ifdef EDM_ML_DEBUG
206  if (debug)
207  edm::LogVerbatim("HcalIsoTrack") << "gentrackP " << t_gentrackP << " rhoh " << t_rhoh
208  << " qltyFlag:qltyMissFlag:qltyPVFlag:selectTk " << t_qltyFlag << ":"
209  << t_qltyMissFlag << ":" << t_qltyPVFlag << ":" << t_selectTk;
210 #endif
211  t_trgbits = itr.trgbits_;
212  t_DetIds = itr.detIds_;
213  t_DetIds1 = itr.detIds1_;
214  t_DetIds3 = itr.detIds3_;
215  if (useRaw_ == 1) {
216  t_eHcal = itr.eHcalAux_;
217  t_eHcal10 = itr.eHcal10Aux_;
218  t_eHcal30 = itr.eHcal30Aux_;
219  t_HitEnergies = itr.hitEnergiesAux_;
220  t_HitEnergies1 = itr.hitEnergies1Aux_;
221  t_HitEnergies3 = itr.hitEnergies3Aux_;
222  } else if (useRaw_ == 2) {
223  t_eHcal = itr.eHcalRaw_;
224  t_eHcal10 = itr.eHcal10Raw_;
225  t_eHcal30 = itr.eHcal30Raw_;
226  t_HitEnergies = itr.hitEnergiesRaw_;
227  t_HitEnergies1 = itr.hitEnergies1Raw_;
228  t_HitEnergies3 = itr.hitEnergies3Raw_;
229  } else {
230  t_eHcal = itr.eHcal_;
231  t_eHcal10 = itr.eHcal10_;
232  t_eHcal30 = itr.eHcal30_;
233  t_HitEnergies = itr.hitEnergies_;
234  t_HitEnergies1 = itr.hitEnergies1_;
235  t_HitEnergies3 = itr.hitEnergies3_;
236  if (unCorrect_ > 0) {
237  t_eHcal = t_eHcal10 = t_eHcal30 = 0;
238  for (unsigned int k = 0; k < t_DetIds.size(); ++k) {
240  if (corr != 0)
241  t_HitEnergies[k] /= corr;
242  t_eHcal += t_HitEnergies[k];
243  }
244  for (unsigned int k = 0; k < t_DetIds1.size(); ++k) {
246  if (corr != 0)
247  t_HitEnergies1[k] /= corr;
249  }
250  for (unsigned int k = 0; k < t_DetIds3.size(); ++k) {
252  if (corr != 0)
253  t_HitEnergies3[k] /= corr;
255  }
256  }
257  }
258 #ifdef EDM_ML_DEBUG
259  if (debug)
260  edm::LogVerbatim("HcalIsoTrack") << "eHcal:eHcal10:eHCal30 " << t_eHcal << ":" << t_eHcal10 << t_eHcal30;
261 #endif
262  bool select(true);
263  if (fillInRange_) {
264  if ((t_p < pTrackLow_) || (t_p > pTrackHigh_))
265  select = false;
266  }
267  if (select) {
268  tree->Fill();
269  edm::LogVerbatim("HcalIsoTrackX") << "Run " << t_Run << " Event " << t_Event << " p " << t_p;
270  }
271  if (t_p < pTrackLow_) {
272  ++nLow_;
273  } else if (t_p > pTrackHigh_) {
274  ++nHigh_;
275  } else {
276  ++nRange_;
277  }
278  }
279  } else {
280  edm::LogVerbatim("HcalIsoTrack") << "Cannot find HcalIsoTrkCalibVariablesCollection";
281  }
282 
283  // Fill from IsoTrkEventVariables collection
284  auto const& isotrkEventColl = iEvent.getHandle(tokIsoTrkEvt_);
285  if (isotrkEventColl.isValid()) {
286  auto isotrkEvent = isotrkEventColl.product();
287 #ifdef EDM_ML_DEBUG
288  if (debug)
289  edm::LogVerbatim("HcalIsoTrack") << "Finds HcalIsoTrkEventVariablesCollection with " << isotrkEvent->size()
290  << " entries";
291 #endif
292  auto itr = isotrkEvent->begin();
293  if (itr != isotrkEvent->end()) {
294  t_RunNo = iEvent.id().run();
295  t_EventNo = iEvent.id().event();
296  t_TrigPass = itr->trigPass_;
297  t_TrigPassSel = itr->trigPassSel_;
298  t_L1Bit = itr->l1Bit_;
299  t_Tracks = itr->tracks_;
300  t_TracksProp = itr->tracksProp_;
301  t_TracksSaved = itr->tracksSaved_;
302  t_TracksLoose = itr->tracksLoose_;
303  t_TracksTight = itr->tracksTight_;
304  t_allvertex = itr->allvertex_;
305  t_ietaAll = itr->ietaAll_;
306  t_ietaGood = itr->ietaGood_;
307  t_trackType = itr->trackType_;
308  t_hltbits = itr->hltbits_;
309  tree2->Fill();
310  }
311  } else {
312  edm::LogVerbatim("HcalIsoTrack") << "Cannot find HcalIsoTrkEventVariablesCollections";
313  }
314 }
315 
318  tree = fs->make<TTree>("CalibTree", "CalibTree");
319 
320  tree->Branch("t_Run", &t_Run, "t_Run/I");
321  tree->Branch("t_Event", &t_Event, "t_Event/I");
322  tree->Branch("t_DataType", &t_DataType, "t_DataType/I");
323  tree->Branch("t_ieta", &t_ieta, "t_ieta/I");
324  tree->Branch("t_iphi", &t_iphi, "t_iphi/I");
325  tree->Branch("t_EventWeight", &t_EventWeight, "t_EventWeight/D");
326  tree->Branch("t_nVtx", &t_nVtx, "t_nVtx/I");
327  tree->Branch("t_nTrk", &t_nTrk, "t_nTrk/I");
328  tree->Branch("t_goodPV", &t_goodPV, "t_goodPV/I");
329  tree->Branch("t_l1pt", &t_l1pt, "t_l1pt/D");
330  tree->Branch("t_l1eta", &t_l1eta, "t_l1eta/D");
331  tree->Branch("t_l1phi", &t_l1phi, "t_l1phi/D");
332  tree->Branch("t_l3pt", &t_l3pt, "t_l3pt/D");
333  tree->Branch("t_l3eta", &t_l3eta, "t_l3eta/D");
334  tree->Branch("t_l3phi", &t_l3phi, "t_l3phi/D");
335  tree->Branch("t_p", &t_p, "t_p/D");
336  tree->Branch("t_pt", &t_pt, "t_pt/D");
337  tree->Branch("t_phi", &t_phi, "t_phi/D");
338  tree->Branch("t_mindR1", &t_mindR1, "t_mindR1/D");
339  tree->Branch("t_mindR2", &t_mindR2, "t_mindR2/D");
340  tree->Branch("t_eMipDR", &t_eMipDR, "t_eMipDR/D");
341  tree->Branch("t_eMipDR2", &t_eMipDR2, "t_eMipDR2/D");
342  tree->Branch("t_eMipDR3", &t_eMipDR3, "t_eMipDR3/D");
343  tree->Branch("t_eMipDR4", &t_eMipDR4, "t_eMipDR4/D");
344  tree->Branch("t_eMipDR5", &t_eMipDR5, "t_eMipDR5/D");
345  tree->Branch("t_eHcal", &t_eHcal, "t_eHcal/D");
346  tree->Branch("t_eHcal10", &t_eHcal10, "t_eHcal10/D");
347  tree->Branch("t_eHcal30", &t_eHcal30, "t_eHcal30/D");
348  tree->Branch("t_hmaxNearP", &t_hmaxNearP, "t_hmaxNearP/D");
349  tree->Branch("t_emaxNearP", &t_emaxNearP, "t_emaxNearP/D");
350  tree->Branch("t_eAnnular", &t_eAnnular, "t_eAnnular/D");
351  tree->Branch("t_hAnnular", &t_hAnnular, "t_hAnnular/D");
352  tree->Branch("t_rhoh", &t_rhoh, "t_rhoh/D");
353  tree->Branch("t_selectTk", &t_selectTk, "t_selectTk/O");
354  tree->Branch("t_qltyFlag", &t_qltyFlag, "t_qltyFlag/O");
355  tree->Branch("t_qltyMissFlag", &t_qltyMissFlag, "t_qltyMissFlag/O");
356  tree->Branch("t_qltyPVFlag", &t_qltyPVFlag, "t_qltyPVFlag/O");
357  tree->Branch("t_gentrackP", &t_gentrackP, "t_gentrackP/D");
358 
359  tree->Branch("t_DetIds", &t_DetIds);
360  tree->Branch("t_HitEnergies", &t_HitEnergies);
361  tree->Branch("t_trgbits", &t_trgbits);
362  tree->Branch("t_DetIds1", &t_DetIds1);
363  tree->Branch("t_DetIds3", &t_DetIds3);
364  tree->Branch("t_HitEnergies1", &t_HitEnergies1);
365  tree->Branch("t_HitEnergies3", &t_HitEnergies3);
366 
367  tree2 = fs->make<TTree>("EventInfo", "Event Information");
368 
369  tree2->Branch("t_RunNo", &t_RunNo, "t_RunNo/i");
370  tree2->Branch("t_EventNo", &t_EventNo, "t_EventNo/i");
371  tree2->Branch("t_Tracks", &t_Tracks, "t_Tracks/I");
372  tree2->Branch("t_TracksProp", &t_TracksProp, "t_TracksProp/I");
373  tree2->Branch("t_TracksSaved", &t_TracksSaved, "t_TracksSaved/I");
374  tree2->Branch("t_TracksLoose", &t_TracksLoose, "t_TracksLoose/I");
375  tree2->Branch("t_TracksTight", &t_TracksTight, "t_TracksTight/I");
376  tree2->Branch("t_TrigPass", &t_TrigPass, "t_TrigPass/O");
377  tree2->Branch("t_TrigPassSel", &t_TrigPassSel, "t_TrigPassSel/O");
378  tree2->Branch("t_L1Bit", &t_L1Bit, "t_L1Bit/O");
379  tree2->Branch("t_allvertex", &t_allvertex, "t_allvertex/I");
380  tree2->Branch("t_ietaAll", &t_ietaAll);
381  tree2->Branch("t_ietaGood", &t_ietaGood);
382  tree2->Branch("t_trackType", &t_trackType);
383  tree2->Branch("t_hltbits", &t_hltbits);
384 }
385 
386 // ------------ method called when starting to processes a run ------------
387 void HcalIsoTrackAnalyzer::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
389  const HcalRespCorrs* resp = &iSetup.getData(tok_respcorr_);
390  respCorrs_ = new HcalRespCorrs(*resp);
392  edm::LogVerbatim("HcalIsoTrack") << "beginRun " << iRun.run() << " get responseCoorection " << respCorrs_;
393 }
394 
395 // ------------ method called when ending the processing of a run ------------
397  nRun_++;
398  edm::LogVerbatim("HcalIsoTrack") << "endRun[" << nRun_ << "] " << iRun.run() << " with " << nLow_
399  << " events with p < " << pTrackLow_ << ", " << nHigh_ << " events with p > "
400  << pTrackHigh_ << ", and " << nRange_ << " events in the right momentum range";
401  if (respCorrs_) {
402  delete respCorrs_;
403  respCorrs_ = nullptr;
404  }
405 }
406 
409  desc.add<double>("momentumLow", 40.0);
410  desc.add<double>("momentumHigh", 60.0);
411  desc.addUntracked<int>("useRaw", 0);
412  desc.addUntracked<int>("dataType", 0);
413  desc.addUntracked<int>("unCorrect", 0);
414  desc.addUntracked<bool>("fillInRange", false);
415  desc.add<edm::InputTag>("isoTrackVarLabel", edm::InputTag("alcaHcalIsotrkProducer", "HcalIsoTrack"));
416  desc.add<edm::InputTag>("isoTrackEvtLabel", edm::InputTag("alcaHcalIsotrkProducer", "HcalIsoTrackEvent"));
417  std::vector<int> events;
418  desc.add<std::vector<int>>("debugEvents", events);
419  descriptions.add("hcalIsoTrackAnalyzer", desc);
420 }
421 
423  double cfac(1.0);
424  if (respCorrs_ != nullptr)
425  cfac = (respCorrs_->getValues(id))->getValue();
426  return cfac;
427 }
428 
430  double gain(0.0);
431  const HcalCalibrations& calibs = conditions->getHcalCalibrations(id);
432  for (int capid = 0; capid < 4; ++capid)
433  gain += (0.25 * calibs.respcorrgain(capid));
434  return gain;
435 }
436 
437 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
Log< level::Info, true > LogVerbatim
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
std::vector< double > t_HitEnergies1
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
const HcalTopology * theHBHETopology_
const std::vector< int > debEvents_
void beginRun(edm::Run const &, edm::EventSetup const &) override
const edm::ESGetToken< HcalDbService, HcalDbRecord > tok_dbservice_
std::vector< double > t_HitEnergies3
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
edm::EDGetTokenT< HcalIsoTrkEventVariablesCollection > tokIsoTrkEvt_
std::vector< int > t_ietaGood
void endRun(edm::Run const &, edm::EventSetup const &) override
std::vector< unsigned int > t_DetIds3
std::vector< unsigned int > t_DetIds
std::vector< double > t_HitEnergies
const edm::InputTag labelIsoTkEvt_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::vector< HcalIsoTrkEventVariables > HcalIsoTrkEventVariablesCollection
const edm::ESGetToken< HcalRespCorrs, HcalRespCorrsRcd > tok_respcorr_
const Item * getValues(DetId fId, bool throwOnFail=true) const
HcalIsoTrackAnalyzer(edm::ParameterSet const &)
int iEvent
Definition: GenABIO.cc:224
RunNumber_t run() const
Definition: RunBase.h:40
dictionary corr
double respCorr(const DetId &id)
Transition
Definition: Transition.h:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
std::vector< bool > t_trgbits
edm::EDGetTokenT< HcalIsoTrkCalibVariablesCollection > tokIsoTrkVar_
std::vector< int > t_ietaAll
constexpr double respcorrgain(int fCapId) const
get response corrected gain for capid=0..3
Definition: DetId.h:17
#define debug
Definition: HDRShower.cc:19
const edm::ESGetToken< HcalTopology, HcalRecNumberingRecord > tok_htopo_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const edm::InputTag labelIsoTkVar_
std::vector< HcalIsoTrkCalibVariables > HcalIsoTrkCalibVariablesCollection
double gainFactor(const HcalDbService *dbserv, const HcalDetId &id)
void analyze(edm::Event const &, edm::EventSetup const &) override
HLT enums.
std::vector< int > t_trackType
const JetExtendedData & getValue(const Container &, const reco::JetBaseRef &)
get value for the association. Throw exception if no association found
std::vector< bool > t_hltbits
Definition: tree.py:1
std::vector< unsigned int > t_DetIds1
int events
void setTopo(const HcalTopology *topo)
Definition: Run.h:45