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 
86 public:
88  ~L1Muon2RecoTreeProducer() override;
89 
90 private:
91  void beginJob(void) override;
92  void analyze(const edm::Event &, const edm::EventSetup &) override;
93  void beginRun(const edm::Run &, const edm::EventSetup &) override;
94  void endJob() override;
95 
96 public:
98 
100 
101  double match_trigger(std::vector<int> &trigIndices,
104  const reco::Muon &mu);
105  void empty_hlt();
106 
107 private:
108  // output file
110 
111  // tree
112  TTree *tree_;
113 
114  // EDM input tags
120 
121  // bool triggerMatching_;
122  // edm::InputTag triggerSummaryLabel_;
123  // std::string triggerProcessLabel_;
124  // std::vector<std::string> isoTriggerNames_;
125  // std::vector<std::string> triggerNames_;
126 
129  // edm::Handle<edm::TriggerResults> TriggerToken_;
130 
132 
133  // debug stuff
134  unsigned int maxMuon_;
138  std::vector<std::string> isoTriggerNames_;
139  std::vector<std::string> triggerNames_;
140  std::vector<int> isoTriggerIndices_;
141  std::vector<int> triggerIndices_;
142 };
143 
145  maxMuon_ = iConfig.getParameter<unsigned int>("maxMuon");
146  isoTriggerNames_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
147  // isoTriggerToken_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
148  // TriggerToken_ = iConfig.getParameter<std::vector<std::string>>("triggerNames");
149  MuonToken_ = consumes<reco::MuonCollection>(iConfig.getUntrackedParameter("MuonToken", edm::InputTag("muons")));
150  VtxToken_ = consumes<reco::VertexCollection>(
151  iConfig.getUntrackedParameter("VertexToken", edm::InputTag("offlinePrimaryVertices")));
152 
153  TriggerResultsToken_ = consumes<edm::TriggerResults>(
154  iConfig.getUntrackedParameter("TriggerResultsToken", edm::InputTag("TriggerResults", "", "HLT")));
155  triggerSummaryLabelToken_ = consumes<trigger::TriggerEvent>(
156  iConfig.getUntrackedParameter("triggerSummaryLabelToken", edm::InputTag("hltTriggerSummaryAOD", "", "HLT")));
157  // TriggerResultsToken_ = consumes<edm::TriggerResults>(iConfig.getUntrackedParameter("TriggerResultsToken",edm::InputTag("triggerSummaryLabel")));
158  // triggerSummaryLabelToken_ = consumes<trigger::TriggerEvent>(iConfig.getUntrackedParameter("triggerSummaryLabelToken",edm::InputTag("triggerSummaryLabel")));
159  //iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
160  metToken_ = consumes<reco::PFMETCollection>(iConfig.getUntrackedParameter("metToken", edm::InputTag("pfMet")));
161 
162  muon = new L1Analysis::L1AnalysisRecoMuon2(iConfig);
163  muon_data = muon->getData();
164 
165  tree_ = fs_->make<TTree>("Muon2RecoTree", "Muon2RecoTree");
166  tree_->Branch("Muon", "L1Analysis::L1AnalysisRecoMuon2DataFormat", &muon_data, 32000, 3);
167 
168  triggerMaxDeltaR_ = iConfig.getParameter<double>("triggerMaxDeltaR");
169  triggerMatching_ = iConfig.getUntrackedParameter<bool>("triggerMatching");
170  triggerProcessLabel_ = iConfig.getUntrackedParameter<std::string>("triggerProcessLabel");
171  isoTriggerNames_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
172  triggerNames_ = iConfig.getParameter<std::vector<std::string>>("triggerNames");
173 
174  // triggerMatching_ = iConfig.getUntrackedParameter<bool>("triggerMatching");
175  // _ = iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
176  // triggerProcessLabel_ = iConfig.getUntrackedParameter<std::string>("triggerProcessLabel");
177  // triggerNames_ = iConfig.getParameter<std::vector<std::string> > ("triggerNames");
178  // isoTriggerNames_ = iConfig.getParameter<std::vector<std::string> > ("isoTriggerNames");
179  // triggerMaxDeltaR_ = iConfig.getParameter<double> ("triggerMaxDeltaR");
180 }
181 
183  // do anything here that needs to be done at desctruction time
184  // (e.g. close files, deallocate resources etc.)
185 }
186 
187 //
188 // member functions
189 //
190 
191 // ------------ method called to for each event ------------
193  muon->Reset();
195  iEvent.getByToken(MuonToken_, recoMuons);
196 
198  iEvent.getByToken(VtxToken_, vertices);
199 
201  iEvent.getByToken(TriggerResultsToken_, triggerResults);
202 
203  edm::Handle<trigger::TriggerEvent> triggerSummaryLabel_;
204  iEvent.getByToken(triggerSummaryLabelToken_, triggerSummaryLabel_);
205 
207  iEvent.getByToken(metToken_, metLabel_);
208 
209  int counter_met = 0;
210 
211  double METx = 0.;
212  double METy = 0.;
213 
214  for (reco::PFMETCollection::const_iterator imet = metLabel_->begin();
215  imet != metLabel_->end() && (unsigned)counter_met < 1;
216  imet++) {
217  METx = imet->px();
218  METy = imet->py();
219  }
220 
221  if (recoMuons.isValid()) {
222  muon->SetMuon(iEvent, iSetup, recoMuons, vertices, METx, METy, maxMuon_);
223  } else {
224  }
225 
226  int counter_mu = 0;
227  for (reco::MuonCollection::const_iterator imu = recoMuons->begin();
228  imu != recoMuons->end() && (unsigned)counter_mu < maxMuon_;
229  imu++) {
230  //---------------------------------------------------------------------
231  // TRIGGER MATCHING:
232  // if specified the reconstructed muons are matched to a trigger
233  //---------------------------------------------------------------------
234  if (triggerMatching_) {
235  double isoMatchDeltaR = 9999.;
236  double matchDeltaR = 9999.;
237  int hasIsoTriggered = 0;
238  int hasTriggered = 0;
239 
240  int passesSingleMuonFlag = 0;
241 
242  // first check if the trigger results are valid:
243  if (triggerResults.isValid()) {
244  if (triggerSummaryLabel_.isValid()) {
245  const edm::TriggerNames &trigNames = iEvent.triggerNames(*triggerResults);
246  // for(UInt_t iPath = 0 ; iPath < trigNames.size() ; ++iPath)
247  // {
248  // cout<<iPath<<": "<<trigNames.triggerName(iPath)<<endl;
249  // }
250 
251  for (UInt_t iPath = 0; iPath < isoTriggerNames_.size(); ++iPath) {
252  if (passesSingleMuonFlag == 1)
253  continue;
254  std::string pathName = isoTriggerNames_.at(iPath);
255 
256  bool passTrig = false;
257  //cout<<"testing pathName = "<<pathName<<endl;
258  //cout<<"trigNames.triggerIndex(pathName) = "<<trigNames.triggerIndex(pathName)<<endl;
259  // cout<<"trigNames.triggerIndex(pathName)<trigNames.size()= "<<(trigNames.triggerIndex(pathName)<trigNames.size())<<endl;
260 
261  if (trigNames.triggerIndex(pathName) < trigNames.size())
262  passTrig = triggerResults->accept(trigNames.triggerIndex(pathName));
263  // cout<<"pass = "<<passTrig<<endl;
264  if (passTrig)
265  passesSingleMuonFlag = 1;
266  }
267 
268  if (triggerSummaryLabel_.isValid()) {
269  // get trigger objects:
270  const trigger::TriggerObjectCollection triggerObjects = triggerSummaryLabel_->getObjects();
271 
272  matchDeltaR = match_trigger(triggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
273  if (matchDeltaR < triggerMaxDeltaR_)
274  hasTriggered = 1;
275 
276  // cout<<"isoTriggerIndices_.size() = "<<isoTriggerIndices_.size()<<endl;
277  // cout<<"triggerObjects.size() = "<<triggerObjects.size()<<endl;
278  // cout<<"imu->eta() = "<<imu->eta()<<endl;
279 
280  isoMatchDeltaR = match_trigger(isoTriggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
281  if (isoMatchDeltaR < triggerMaxDeltaR_)
282  hasIsoTriggered = 1;
283 
284  } // end if (triggerEvent.isValid())
285 
286  } // end if (triggerResults.isValid())
287 
288  } // end if (triggerResults.isValid())
289 
290  // fill trigger matching variables:
291  muon_data->hlt_isomu.push_back(hasIsoTriggered);
292  muon_data->hlt_mu.push_back(hasTriggered);
293  muon_data->hlt_isoDeltaR.push_back(isoMatchDeltaR);
294  muon_data->hlt_deltaR.push_back(matchDeltaR);
295  muon_data->passesSingleMuon.push_back(passesSingleMuonFlag);
296 
297  } else {
298  empty_hlt();
299  } // end if (triggerMatching_)
300  }
301 
302  tree_->Fill();
303 }
304 
305 // ------------ method called once each job just before starting event loop ------------
307 
308 // ------------ method called once each job just after ending the event loop ------------
310 
312  muon_data->hlt_isomu.push_back(-9999);
313  muon_data->hlt_mu.push_back(-9999);
314  muon_data->hlt_isoDeltaR.push_back(-9999);
315  muon_data->hlt_deltaR.push_back(-9999);
316 }
317 
318 double L1Muon2RecoTreeProducer::match_trigger(std::vector<int> &trigIndices,
321  const reco::Muon &mu) {
322  double matchDeltaR = 9999;
323 
324  for (size_t iTrigIndex = 0; iTrigIndex < trigIndices.size(); ++iTrigIndex) {
325  int triggerIndex = trigIndices[iTrigIndex];
326  const std::vector<std::string> moduleLabels(hltConfig_.moduleLabels(triggerIndex));
327  // find index of the last module:
328  const unsigned moduleIndex = hltConfig_.size(triggerIndex) - 2;
329  // find index of HLT trigger name:
330  const unsigned hltFilterIndex =
331  triggerEvent->filterIndex(edm::InputTag(moduleLabels[moduleIndex], "", triggerProcessLabel_));
332 
333  if (hltFilterIndex < triggerEvent->sizeFilters()) {
334  const trigger::Keys triggerKeys(triggerEvent->filterKeys(hltFilterIndex));
335  const trigger::Vids triggerVids(triggerEvent->filterIds(hltFilterIndex));
336 
337  const unsigned nTriggers = triggerVids.size();
338  for (size_t iTrig = 0; iTrig < nTriggers; ++iTrig) {
339  // loop over all trigger objects:
340  const trigger::TriggerObject trigObject = trigObjs[triggerKeys[iTrig]];
341 
342  double dRtmp = deltaR(mu, trigObject);
343 
344  if (dRtmp < matchDeltaR) {
345  matchDeltaR = dRtmp;
346  }
347 
348  } // loop over different trigger objects
349  } // if trigger is in event (should apply hltFilter with used trigger...)
350  } // loop over muon candidates
351 
352  return matchDeltaR;
353 }
354 
356  // Prepare for trigger matching for each new run:
357  // Look up triggetIndices in the HLT config for the different paths
358  if (triggerMatching_) {
359  bool changed = true;
360  if (!hltConfig_.init(run, eventSetup, triggerProcessLabel_, changed)) {
361  // if you can't initialize hlt configuration, crash!
362  std::cout << "Error: didn't find process" << triggerProcessLabel_ << std::endl;
363  assert(false);
364  }
365 
366  bool enableWildcard = true;
367  for (size_t iTrig = 0; iTrig < triggerNames_.size(); ++iTrig) {
368  // prepare for regular expression (with wildcards) functionality:
369  TString tNameTmp = TString(triggerNames_[iTrig]);
370  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
371  int tIndex = -1;
372  // find the trigger index:
373  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
374  // use TString since it provides reg exp functionality:
375  TString tmpName = TString(hltConfig_.triggerName(ipath));
376  if (tmpName.Contains(tNamePattern)) {
377  tIndex = int(ipath);
378  triggerIndices_.push_back(tIndex);
379  }
380  }
381  if (tIndex < 0) { // if can't find trigger path at all, give warning:
382  std::cout << "Warning: Could not find trigger" << triggerNames_[iTrig] << std::endl;
383  //assert(false);
384  }
385  } // end for triggerNames
386  for (size_t iTrig = 0; iTrig < isoTriggerNames_.size(); ++iTrig) {
387  // prepare for regular expression functionality:
388  TString tNameTmp = TString(isoTriggerNames_[iTrig]);
389  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
390  int tIndex = -1;
391  // find the trigger index:
392  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
393  // use TString since it provides reg exp functionality:
394  TString tmpName = TString(hltConfig_.triggerName(ipath));
395  if (tmpName.Contains(tNamePattern)) {
396  tIndex = int(ipath);
397  isoTriggerIndices_.push_back(tIndex);
398  }
399  }
400  if (tIndex < 0) { // if can't find trigger path at all, give warning:
401  std::cout << "Warning: Could not find trigger" << isoTriggerNames_[iTrig] << std::endl;
402  //assert(false);
403  }
404  } // end for isoTriggerNames
405  } // end if (triggerMatching_)
406 
407  muon->init(eventSetup);
408 }
409 
410 //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:525
bool accept() const
Has at least one path accepted the event?
const Keys & filterKeys(trigger::size_type index) const
Definition: TriggerEvent.h:118
trigger::size_type filterIndex(const edm::InputTag &filterTag) const
find index of filter in data-member vector from filter tag
Definition: TriggerEvent.h:132
Strings::size_type size() const
Definition: TriggerNames.cc:31
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
void beginRun(const edm::Run &, const edm::EventSetup &) override
void beginJob()
Definition: Breakpoints.cc:14
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)
const Vids & filterIds(trigger::size_type index) const
Definition: TriggerEvent.h:117
edm::EDGetTokenT< reco::MuonCollection > MuonToken_
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::Service< TFileService > fs_
unsigned int triggerIndex(std::string const &name) const
Definition: TriggerNames.cc:24
const TriggerObjectCollection & getObjects() const
Definition: TriggerEvent.h:101
static std::string const triggerResults
Definition: EdmProvDump.cc:45
bool isValid() const
Definition: HandleBase.h:70
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 &)
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_
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:265
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_