CMS 3D CMS Logo

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 // ------------ method called to for each event ------------
195  muon->init(iSetup);
196 
197  muon->Reset();
199  iEvent.getByToken(MuonToken_, recoMuons);
200 
202  iEvent.getByToken(VtxToken_, vertices);
203 
205  iEvent.getByToken(TriggerResultsToken_, triggerResults);
206 
207  edm::Handle<trigger::TriggerEvent> triggerSummaryLabel_;
208  iEvent.getByToken(triggerSummaryLabelToken_, triggerSummaryLabel_);
209 
211  iEvent.getByToken(metToken_, metLabel_);
212 
213  int counter_met = 0;
214 
215  double METx = 0.;
216  double METy = 0.;
217 
218  for (reco::PFMETCollection::const_iterator imet = metLabel_->begin();
219  imet != metLabel_->end() && (unsigned)counter_met < 1;
220  imet++) {
221  METx = imet->px();
222  METy = imet->py();
223  }
224 
225  if (recoMuons.isValid()) {
226  muon->SetMuon(iEvent, iSetup, recoMuons, vertices, METx, METy, maxMuon_);
227  } else {
228  }
229 
230  int counter_mu = 0;
231  for (reco::MuonCollection::const_iterator imu = recoMuons->begin();
232  imu != recoMuons->end() && (unsigned)counter_mu < maxMuon_;
233  imu++) {
234  //---------------------------------------------------------------------
235  // TRIGGER MATCHING:
236  // if specified the reconstructed muons are matched to a trigger
237  //---------------------------------------------------------------------
238  if (triggerMatching_) {
239  double isoMatchDeltaR = 9999.;
240  double matchDeltaR = 9999.;
241  int hasIsoTriggered = 0;
242  int hasTriggered = 0;
243 
244  int passesSingleMuonFlag = 0;
245 
246  // first check if the trigger results are valid:
247  if (triggerResults.isValid()) {
248  if (triggerSummaryLabel_.isValid()) {
249  const edm::TriggerNames &trigNames = iEvent.triggerNames(*triggerResults);
250  // for(UInt_t iPath = 0 ; iPath < trigNames.size() ; ++iPath)
251  // {
252  // cout<<iPath<<": "<<trigNames.triggerName(iPath)<<endl;
253  // }
254 
255  for (UInt_t iPath = 0; iPath < isoTriggerNames_.size(); ++iPath) {
256  if (passesSingleMuonFlag == 1)
257  continue;
258  std::string pathName = isoTriggerNames_.at(iPath);
259 
260  bool passTrig = false;
261  //cout<<"testing pathName = "<<pathName<<endl;
262  //cout<<"trigNames.triggerIndex(pathName) = "<<trigNames.triggerIndex(pathName)<<endl;
263  // cout<<"trigNames.triggerIndex(pathName)<trigNames.size()= "<<(trigNames.triggerIndex(pathName)<trigNames.size())<<endl;
264 
265  if (trigNames.triggerIndex(pathName) < trigNames.size())
266  passTrig = triggerResults->accept(trigNames.triggerIndex(pathName));
267  // cout<<"pass = "<<passTrig<<endl;
268  if (passTrig)
269  passesSingleMuonFlag = 1;
270  }
271 
272  if (triggerSummaryLabel_.isValid()) {
273  // get trigger objects:
274  const trigger::TriggerObjectCollection triggerObjects = triggerSummaryLabel_->getObjects();
275 
276  matchDeltaR = match_trigger(triggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
277  if (matchDeltaR < triggerMaxDeltaR_)
278  hasTriggered = 1;
279 
280  // cout<<"isoTriggerIndices_.size() = "<<isoTriggerIndices_.size()<<endl;
281  // cout<<"triggerObjects.size() = "<<triggerObjects.size()<<endl;
282  // cout<<"imu->eta() = "<<imu->eta()<<endl;
283 
284  isoMatchDeltaR = match_trigger(isoTriggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
285  if (isoMatchDeltaR < triggerMaxDeltaR_)
286  hasIsoTriggered = 1;
287 
288  } // end if (triggerEvent.isValid())
289 
290  } // end if (triggerResults.isValid())
291 
292  } // end if (triggerResults.isValid())
293 
294  // fill trigger matching variables:
295  muon_data->hlt_isomu.push_back(hasIsoTriggered);
296  muon_data->hlt_mu.push_back(hasTriggered);
297  muon_data->hlt_isoDeltaR.push_back(isoMatchDeltaR);
298  muon_data->hlt_deltaR.push_back(matchDeltaR);
299  muon_data->passesSingleMuon.push_back(passesSingleMuonFlag);
300 
301  } else {
302  empty_hlt();
303  } // end if (triggerMatching_)
304  }
305 
306  tree_->Fill();
307 }
308 
309 // ------------ method called once each job just before starting event loop ------------
311 
312 // ------------ method called once each job just after ending the event loop ------------
314 
316  muon_data->hlt_isomu.push_back(-9999);
317  muon_data->hlt_mu.push_back(-9999);
318  muon_data->hlt_isoDeltaR.push_back(-9999);
319  muon_data->hlt_deltaR.push_back(-9999);
320 }
321 
322 double L1Muon2RecoTreeProducer::match_trigger(std::vector<int> &trigIndices,
325  const reco::Muon &mu) {
326  double matchDeltaR = 9999;
327 
328  for (size_t iTrigIndex = 0; iTrigIndex < trigIndices.size(); ++iTrigIndex) {
329  int triggerIndex = trigIndices[iTrigIndex];
330  const std::vector<std::string> moduleLabels(hltConfig_.moduleLabels(triggerIndex));
331  // find index of the last module:
332  const unsigned moduleIndex = hltConfig_.size(triggerIndex) - 2;
333  // find index of HLT trigger name:
334  const unsigned hltFilterIndex =
335  triggerEvent->filterIndex(edm::InputTag(moduleLabels[moduleIndex], "", triggerProcessLabel_));
336 
337  if (hltFilterIndex < triggerEvent->sizeFilters()) {
338  const trigger::Keys triggerKeys(triggerEvent->filterKeys(hltFilterIndex));
339  const trigger::Vids triggerVids(triggerEvent->filterIds(hltFilterIndex));
340 
341  const unsigned nTriggers = triggerVids.size();
342  for (size_t iTrig = 0; iTrig < nTriggers; ++iTrig) {
343  // loop over all trigger objects:
344  const trigger::TriggerObject trigObject = trigObjs[triggerKeys[iTrig]];
345 
346  double dRtmp = deltaR(mu, trigObject);
347 
348  if (dRtmp < matchDeltaR) {
349  matchDeltaR = dRtmp;
350  }
351 
352  } // loop over different trigger objects
353  } // if trigger is in event (should apply hltFilter with used trigger...)
354  } // loop over muon candidates
355 
356  return matchDeltaR;
357 }
358 
360  // Prepare for trigger matching for each new run:
361  // Look up triggetIndices in the HLT config for the different paths
362  if (triggerMatching_) {
363  bool changed = true;
364  if (!hltConfig_.init(run, eventSetup, triggerProcessLabel_, changed)) {
365  // if you can't initialize hlt configuration, crash!
366  std::cout << "Error: didn't find process" << triggerProcessLabel_ << std::endl;
367  assert(false);
368  }
369 
370  bool enableWildcard = true;
371  for (size_t iTrig = 0; iTrig < triggerNames_.size(); ++iTrig) {
372  // prepare for regular expression (with wildcards) functionality:
373  TString tNameTmp = TString(triggerNames_[iTrig]);
374  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
375  int tIndex = -1;
376  // find the trigger index:
377  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
378  // use TString since it provides reg exp functionality:
379  TString tmpName = TString(hltConfig_.triggerName(ipath));
380  if (tmpName.Contains(tNamePattern)) {
381  tIndex = int(ipath);
382  triggerIndices_.push_back(tIndex);
383  }
384  }
385  if (tIndex < 0) { // if can't find trigger path at all, give warning:
386  std::cout << "Warning: Could not find trigger" << triggerNames_[iTrig] << std::endl;
387  //assert(false);
388  }
389  } // end for triggerNames
390  for (size_t iTrig = 0; iTrig < isoTriggerNames_.size(); ++iTrig) {
391  // prepare for regular expression functionality:
392  TString tNameTmp = TString(isoTriggerNames_[iTrig]);
393  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
394  int tIndex = -1;
395  // find the trigger index:
396  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
397  // use TString since it provides reg exp functionality:
398  TString tmpName = TString(hltConfig_.triggerName(ipath));
399  if (tmpName.Contains(tNamePattern)) {
400  tIndex = int(ipath);
401  isoTriggerIndices_.push_back(tIndex);
402  }
403  }
404  if (tIndex < 0) { // if can't find trigger path at all, give warning:
405  std::cout << "Warning: Could not find trigger" << isoTriggerNames_[iTrig] << std::endl;
406  //assert(false);
407  }
408  } // end for isoTriggerNames
409  } // end if (triggerMatching_)
410 }
411 
412 //define this as a plug-in
static const std::string kSharedResource
Definition: TFileService.h:76
L1Analysis::L1AnalysisRecoMuon2DataFormat * muon_data
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
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
T getUntrackedParameter(std::string const &, T const &) const
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 TriggerObjectCollection & getObjects() const
Definition: TriggerEvent.h:102
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static std::string const triggerResults
Definition: EdmProvDump.cc:47
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
L1Muon2RecoTreeProducer(const edm::ParameterSet &)
bool isValid() const
Definition: HandleBase.h:70
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
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_