CMS 3D CMS Logo

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