CMS 3D CMS Logo

L1Muon2RecoTreeProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: L1Trigger/L1TNtuples
4 // Class: L1JetRecoTreeProducer
5 //
14 // system include files
15 #include <memory>
16 
17 // framework
27 
28 // cond formats
30 
31 // data formats
37 
38 // RECO TRIGGER MATCHING:
44 #include "TString.h"
45 #include "TRegexp.h"
46 #include <utility>
47 
48 //muons
58 
59 //taus
63 
64 // ROOT output stuff
67 #include "TH1.h"
68 #include "TTree.h"
69 #include "TF1.h"
70 
71 //local data formats
74 
75 
76 //vertices
80 
81 using namespace std;
82 
83 //
84 // class declaration
85 //
86 
88 public:
91 
92 
93 private:
94  virtual void beginJob(void) ;
95  virtual void analyze(const edm::Event&, const edm::EventSetup&);
96  virtual void beginRun(const edm::Run &, const edm::EventSetup &);
97  virtual void endJob();
98 
99 public:
101 
103 
104  double match_trigger(std::vector<int> &trigIndices, const trigger::TriggerObjectCollection &trigObjs, edm::Handle<trigger::TriggerEvent> &triggerEvent, const reco::Muon &mu);
105  void empty_hlt();
106 
107 
108 private:
109 
110  // output file
112 
113  // tree
114  TTree * tree_;
115 
116  // EDM input tags
122 
123  // bool triggerMatching_;
124  // edm::InputTag triggerSummaryLabel_;
125  // std::string triggerProcessLabel_;
126  // std::vector<std::string> isoTriggerNames_;
127  // std::vector<std::string> triggerNames_;
128 
131  // edm::Handle<edm::TriggerResults> TriggerToken_;
132 
134 
135  // debug stuff
136  unsigned int maxMuon_;
140  std::vector<std::string> isoTriggerNames_;
141  std::vector<std::string> triggerNames_;
142  std::vector<int> isoTriggerIndices_;
143  std::vector<int> triggerIndices_;
144 
145 };
146 
147 
148 
150 {
151 
152  maxMuon_ = iConfig.getParameter<unsigned int>("maxMuon");
153  isoTriggerNames_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
154  // isoTriggerToken_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
155  // TriggerToken_ = iConfig.getParameter<std::vector<std::string>>("triggerNames");
156  MuonToken_ = consumes<reco::MuonCollection>(iConfig.getUntrackedParameter("MuonToken",edm::InputTag("muons")));
157  VtxToken_ = consumes<reco::VertexCollection>(iConfig.getUntrackedParameter("VertexToken",edm::InputTag("offlinePrimaryVertices")));
158 
159  TriggerResultsToken_ = consumes<edm::TriggerResults>(iConfig.getUntrackedParameter("TriggerResultsToken",edm::InputTag("TriggerResults","","HLT")));
160  triggerSummaryLabelToken_ = consumes<trigger::TriggerEvent>(iConfig.getUntrackedParameter("triggerSummaryLabelToken",edm::InputTag("hltTriggerSummaryAOD","","HLT")));
161  // TriggerResultsToken_ = consumes<edm::TriggerResults>(iConfig.getUntrackedParameter("TriggerResultsToken",edm::InputTag("triggerSummaryLabel")));
162  // triggerSummaryLabelToken_ = consumes<trigger::TriggerEvent>(iConfig.getUntrackedParameter("triggerSummaryLabelToken",edm::InputTag("triggerSummaryLabel")));
163  //iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
164  metToken_ = consumes<reco::PFMETCollection>(iConfig.getUntrackedParameter("metToken",edm::InputTag("pfMet")));
165 
166  muon = new L1Analysis::L1AnalysisRecoMuon2(iConfig);
167  muon_data = muon->getData();
168 
169  tree_=fs_->make<TTree>("Muon2RecoTree", "Muon2RecoTree");
170  tree_->Branch("Muon", "L1Analysis::L1AnalysisRecoMuon2DataFormat", &muon_data, 32000, 3);
171 
172  triggerMaxDeltaR_ = iConfig.getParameter<double>("triggerMaxDeltaR");
173  triggerMatching_ = iConfig.getUntrackedParameter<bool>("triggerMatching");
174  triggerProcessLabel_ = iConfig.getUntrackedParameter<std::string>("triggerProcessLabel");
175  isoTriggerNames_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
176  triggerNames_ = iConfig.getParameter<std::vector<std::string>>("triggerNames");
177 
178  // triggerMatching_ = iConfig.getUntrackedParameter<bool>("triggerMatching");
179  // _ = iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
180  // triggerProcessLabel_ = iConfig.getUntrackedParameter<std::string>("triggerProcessLabel");
181  // triggerNames_ = iConfig.getParameter<std::vector<std::string> > ("triggerNames");
182  // isoTriggerNames_ = iConfig.getParameter<std::vector<std::string> > ("isoTriggerNames");
183  // triggerMaxDeltaR_ = iConfig.getParameter<double> ("triggerMaxDeltaR");
184 
185 }
186 
187 
189 {
190 
191  // do anything here that needs to be done at desctruction time
192  // (e.g. close files, deallocate resources etc.)
193 
194 }
195 
196 
197 //
198 // member functions
199 //
200 
201 // ------------ method called to for each event ------------
203 {
204 
205  muon->Reset();
207  iEvent.getByToken(MuonToken_, recoMuons);
208 
210  iEvent.getByToken(VtxToken_, vertices);
211 
213  iEvent.getByToken(TriggerResultsToken_, triggerResults);
214 
215  edm::Handle<trigger::TriggerEvent> triggerSummaryLabel_;
216  iEvent.getByToken(triggerSummaryLabelToken_, triggerSummaryLabel_);
217 
219  iEvent.getByToken(metToken_, metLabel_);
220 
221  int counter_met = 0;
222 
223  double METx = 0.;
224  double METy = 0.;
225 
226  for(reco::PFMETCollection::const_iterator imet = metLabel_->begin();
227  imet != metLabel_->end() && (unsigned) counter_met < 1; imet++) {
228 
229  METx = imet->px();
230  METy = imet->py();
231 
232  }
233 
234  if (recoMuons.isValid()) {
235  muon->SetMuon(iEvent, iSetup, recoMuons, vertices, METx, METy, maxMuon_);
236  }
237  else {
238 
239  }
240 
241 
242  int counter_mu = 0;
243  for(reco::MuonCollection::const_iterator imu = recoMuons->begin();
244  imu != recoMuons->end() && (unsigned) counter_mu < maxMuon_; imu++) {
245 
246  //---------------------------------------------------------------------
247  // TRIGGER MATCHING:
248  // if specified the reconstructed muons are matched to a trigger
249  //---------------------------------------------------------------------
250  if (triggerMatching_) {
251  double isoMatchDeltaR = 9999.;
252  double matchDeltaR = 9999.;
253  int hasIsoTriggered = 0;
254  int hasTriggered = 0;
255 
256  int passesSingleMuonFlag = 0;
257 
258  // first check if the trigger results are valid:
259  if (triggerResults.isValid()) {
260 
261  if (triggerSummaryLabel_.isValid()) {
262 
263  const edm::TriggerNames& trigNames = iEvent.triggerNames(*triggerResults);
264  // for(UInt_t iPath = 0 ; iPath < trigNames.size() ; ++iPath)
265  // {
266  // cout<<iPath<<": "<<trigNames.triggerName(iPath)<<endl;
267  // }
268 
269  for(UInt_t iPath = 0 ; iPath < isoTriggerNames_.size() ; ++iPath)
270  {
271  if(passesSingleMuonFlag==1) continue;
272  std::string pathName=isoTriggerNames_.at(iPath);
273 
274  bool passTrig=false;
275  //cout<<"testing pathName = "<<pathName<<endl;
276  //cout<<"trigNames.triggerIndex(pathName) = "<<trigNames.triggerIndex(pathName)<<endl;
277  // cout<<"trigNames.triggerIndex(pathName)<trigNames.size()= "<<(trigNames.triggerIndex(pathName)<trigNames.size())<<endl;
278 
279  if(trigNames.triggerIndex(pathName)<trigNames.size()) passTrig=triggerResults->accept(trigNames.triggerIndex(pathName));
280  // cout<<"pass = "<<passTrig<<endl;
281  if(passTrig) passesSingleMuonFlag=1;
282  }
283 
284  if (triggerSummaryLabel_.isValid()) {
285  // get trigger objects:
286  const trigger::TriggerObjectCollection triggerObjects = triggerSummaryLabel_->getObjects();
287 
288  matchDeltaR = match_trigger(triggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
289  if (matchDeltaR < triggerMaxDeltaR_)
290  hasTriggered = 1;
291 
292  // cout<<"isoTriggerIndices_.size() = "<<isoTriggerIndices_.size()<<endl;
293  // cout<<"triggerObjects.size() = "<<triggerObjects.size()<<endl;
294  // cout<<"imu->eta() = "<<imu->eta()<<endl;
295 
296  isoMatchDeltaR = match_trigger(isoTriggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
297  if (isoMatchDeltaR < triggerMaxDeltaR_)
298  hasIsoTriggered = 1;
299 
300  } // end if (triggerEvent.isValid())
301 
302  } // end if (triggerResults.isValid())
303 
304  } // end if (triggerResults.isValid())
305 
306  // fill trigger matching variables:
307  muon_data->hlt_isomu.push_back(hasIsoTriggered);
308  muon_data->hlt_mu.push_back(hasTriggered);
309  muon_data->hlt_isoDeltaR.push_back(isoMatchDeltaR);
310  muon_data->hlt_deltaR.push_back(matchDeltaR);
311  muon_data->passesSingleMuon.push_back(passesSingleMuonFlag);
312 
313  } else {
314  empty_hlt();
315  } // end if (triggerMatching_)
316  }
317 
318 
319  tree_->Fill();
320 
321 }
322 
323 // ------------ method called once each job just before starting event loop ------------
324 void
326 {
327 }
328 
329 // ------------ method called once each job just after ending the event loop ------------
330 void
332 }
333 
335 
336  muon_data->hlt_isomu.push_back(-9999);
337  muon_data->hlt_mu.push_back(-9999);
338  muon_data->hlt_isoDeltaR.push_back(-9999);
339  muon_data->hlt_deltaR.push_back(-9999);
340 
341 }
342 
344  std::vector<int> &trigIndices, const trigger::TriggerObjectCollection &trigObjs,
345  edm::Handle<trigger::TriggerEvent> &triggerEvent, const reco::Muon &mu
346  )
347 {
348  double matchDeltaR = 9999;
349 
350  for(size_t iTrigIndex = 0; iTrigIndex < trigIndices.size(); ++iTrigIndex) {
351  int triggerIndex = trigIndices[iTrigIndex];
352  const std::vector<std::string> moduleLabels(hltConfig_.moduleLabels(triggerIndex));
353  // find index of the last module:
354  const unsigned moduleIndex = hltConfig_.size(triggerIndex)-2;
355  // find index of HLT trigger name:
356  const unsigned hltFilterIndex = triggerEvent->filterIndex( edm::InputTag ( moduleLabels[moduleIndex], "", triggerProcessLabel_ ) );
357 
358  if (hltFilterIndex < triggerEvent->sizeFilters()) {
359  const trigger::Keys triggerKeys(triggerEvent->filterKeys(hltFilterIndex));
360  const trigger::Vids triggerVids(triggerEvent->filterIds(hltFilterIndex));
361 
362  const unsigned nTriggers = triggerVids.size();
363  for (size_t iTrig = 0; iTrig < nTriggers; ++iTrig) {
364  // loop over all trigger objects:
365  const trigger::TriggerObject trigObject = trigObjs[triggerKeys[iTrig]];
366 
367  double dRtmp = deltaR( mu, trigObject );
368 
369  if ( dRtmp < matchDeltaR ) {
370  matchDeltaR = dRtmp;
371  }
372 
373  } // loop over different trigger objects
374  } // if trigger is in event (should apply hltFilter with used trigger...)
375  } // loop over muon candidates
376 
377  return matchDeltaR;
378 }
379 
381  // Prepare for trigger matching for each new run:
382  // Look up triggetIndices in the HLT config for the different paths
383  if (triggerMatching_) {
384  bool changed = true;
385  if (!hltConfig_.init(run, eventSetup, triggerProcessLabel_, changed)) {
386  // if you can't initialize hlt configuration, crash!
387  std::cout << "Error: didn't find process" << triggerProcessLabel_ << std::endl;
388  assert(false);
389  }
390 
391  bool enableWildcard = true;
392  for (size_t iTrig = 0; iTrig < triggerNames_.size(); ++iTrig) {
393  // prepare for regular expression (with wildcards) functionality:
394  TString tNameTmp = TString(triggerNames_[iTrig]);
395  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
396  int tIndex = -1;
397  // find the trigger index:
398  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
399  // use TString since it provides reg exp functionality:
400  TString tmpName = TString(hltConfig_.triggerName(ipath));
401  if (tmpName.Contains(tNamePattern)) {
402  tIndex = int(ipath);
403  triggerIndices_.push_back(tIndex);
404  }
405  }
406  if (tIndex < 0) { // if can't find trigger path at all, give warning:
407  std::cout << "Warning: Could not find trigger" << triggerNames_[iTrig] << std::endl;
408  //assert(false);
409  }
410  } // end for triggerNames
411  for (size_t iTrig = 0; iTrig < isoTriggerNames_.size(); ++iTrig) {
412 
413  // prepare for regular expression functionality:
414  TString tNameTmp = TString(isoTriggerNames_[iTrig]);
415  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
416  int tIndex = -1;
417  // find the trigger index:
418  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
419  // use TString since it provides reg exp functionality:
420  TString tmpName = TString(hltConfig_.triggerName(ipath));
421  if (tmpName.Contains(tNamePattern)) {
422  tIndex = int(ipath);
423  isoTriggerIndices_.push_back(tIndex);
424  }
425  }
426  if (tIndex < 0) { // if can't find trigger path at all, give warning:
427  std::cout << "Warning: Could not find trigger" << isoTriggerNames_[iTrig] << std::endl;
428  //assert(false);
429  }
430  } // end for isoTriggerNames
431  } // end if (triggerMatching_)
432 
433  muon->init(eventSetup);
434 }
435 
436 //define this as a plug-in
L1Analysis::L1AnalysisRecoMuon2DataFormat * muon_data
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
def analyze(function, filename, filter=None)
Definition: Profiling.py:11
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
bool accept() const
Has at least one path accepted the event?
const Keys & filterKeys(trigger::size_type index) const
Definition: TriggerEvent.h:111
trigger::size_type filterIndex(const edm::InputTag &filterTag) const
find index of filter in data-member vector from filter tag
Definition: TriggerEvent.h:123
Strings::size_type size() const
Definition: TriggerNames.cc:39
void beginJob()
Definition: Breakpoints.cc:15
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:22
double match_trigger(std::vector< int > &trigIndices, const trigger::TriggerObjectCollection &trigObjs, edm::Handle< trigger::TriggerEvent > &triggerEvent, const reco::Muon &mu)
const Vids & filterIds(trigger::size_type index) const
Definition: TriggerEvent.h:110
edm::EDGetTokenT< reco::MuonCollection > MuonToken_
int iEvent
Definition: GenABIO.cc:230
edm::Service< TFileService > fs_
unsigned int triggerIndex(std::string const &name) const
Definition: TriggerNames.cc:32
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
const TriggerObjectCollection & getObjects() const
Definition: TriggerEvent.h:98
const int mu
Definition: Constants.h:22
static std::string const triggerResults
Definition: EdmProvDump.cc:41
bool isValid() const
Definition: HandleBase.h:74
virtual void analyze(const edm::Event &, const edm::EventSetup &)
edm::Handle< edm::TriggerResults > isoTriggerToken_
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:81
static const char *const trigNames[]
Definition: EcalDumpRaw.cc:74
std::vector< size_type > Keys
L1Muon2RecoTreeProducer(const edm::ParameterSet &)
edm::EDGetTokenT< edm::TriggerResults > TriggerResultsToken_
edm::EDGetTokenT< reco::VertexCollection > VtxToken_
edm::Handle< std::vector< std::string > > isoTriggerNamesToken_
L1Analysis::L1AnalysisRecoMuon2 * muon
std::vector< int > isoTriggerIndices_
edm::EDGetTokenT< trigger::TriggerEvent > triggerSummaryLabelToken_
std::vector< std::string > triggerNames_
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:272
std::vector< int > Vids
Definition: Run.h:43
edm::EDGetTokenT< reco::PFMETCollection > metToken_
bool passTrig(const float objEta, float objPhi, const trigger::TriggerEvent &trigEvt, const std::string &filterName, const std::string &processName)
Definition: UtilFuncs.h:14
std::vector< std::string > isoTriggerNames_