CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
26 
27 // cond formats
29 
30 // data formats
34 
35 // RECO TRIGGER MATCHING:
41 #include "TString.h"
42 #include "TRegexp.h"
43 #include <utility>
44 
45 //muons
55 
56 //taus
60 
61 // ROOT output stuff
64 #include "TH1.h"
65 #include "TTree.h"
66 #include "TF1.h"
67 
68 //local data formats
71 
72 using namespace std;
73 
74 //
75 // class declaration
76 //
77 
79 public:
82 
83 
84 private:
85  virtual void beginJob(void) ;
86  virtual void analyze(const edm::Event&, const edm::EventSetup&);
87  virtual void beginRun(const edm::Run &, const edm::EventSetup &);
88  virtual void endJob();
89 
90 public:
92 
94 
95  double match_trigger(std::vector<int> &trigIndices, const trigger::TriggerObjectCollection &trigObjs, edm::Handle<trigger::TriggerEvent> &triggerEvent, const reco::Muon &mu);
96  void empty_hlt();
97 
98 
99 private:
100 
101  // output file
103 
104  // tree
105  TTree * tree_;
106 
107  // EDM input tags
111 
112  // bool triggerMatching_;
113  // edm::InputTag triggerSummaryLabel_;
114  // std::string triggerProcessLabel_;
115  // std::vector<std::string> isoTriggerNames_;
116  // std::vector<std::string> triggerNames_;
117 
118  // edm::Handle<edm::TriggerResults> isoTriggerToken_;
119  // edm::Handle<edm::TriggerResults> TriggerToken_;
120 
122 
123  // debug stuff
124  unsigned int maxMuon_;
128  std::vector<std::string> isoTriggerNames_;
129  std::vector<std::string> triggerNames_;
130  std::vector<int> isoTriggerIndices_;
131  std::vector<int> triggerIndices_;
132 
133 };
134 
135 
136 
138 {
139 
140  maxMuon_ = iConfig.getParameter<unsigned int>("maxMuon");
141  // isoTriggerToken_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
142  // TriggerToken_ = iConfig.getParameter<std::vector<std::string>>("triggerNames");
143  MuonToken_ = consumes<reco::MuonCollection>(iConfig.getUntrackedParameter("MuonToken",edm::InputTag("muons")));
144 
145  TriggerResultsToken_ = consumes<edm::TriggerResults>(iConfig.getUntrackedParameter("TriggerResultsToken",edm::InputTag("TriggerResults")));
146  triggerSummaryLabelToken_ = consumes<trigger::TriggerEvent>(iConfig.getUntrackedParameter("triggerSummaryLabelToken",edm::InputTag("hltTriggerSummaryAOD","","HLT")));
147  // TriggerResultsToken_ = consumes<edm::TriggerResults>(iConfig.getUntrackedParameter("TriggerResultsToken",edm::InputTag("triggerSummaryLabel")));
148  // triggerSummaryLabelToken_ = consumes<trigger::TriggerEvent>(iConfig.getUntrackedParameter("triggerSummaryLabelToken",edm::InputTag("triggerSummaryLabel")));
149  //iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
150 
152  muon_data = muon->getData();
153 
154  tree_=fs_->make<TTree>("Muon2RecoTree", "Muon2RecoTree");
155  tree_->Branch("Muon", "L1Analysis::L1AnalysisRecoMuon2DataFormat", &muon_data, 32000, 3);
156 
157  triggerMaxDeltaR_ = iConfig.getParameter<double>("triggerMaxDeltaR");
158  triggerMatching_ = iConfig.getUntrackedParameter<bool>("triggerMatching");
159  triggerProcessLabel_ = iConfig.getUntrackedParameter<std::string>("triggerProcessLabel");
160  isoTriggerNames_ = iConfig.getParameter<std::vector<std::string>>("isoTriggerNames");
161  triggerNames_ = iConfig.getParameter<std::vector<std::string>>("triggerNames");
162 
163  // triggerMatching_ = iConfig.getUntrackedParameter<bool>("triggerMatching");
164  // _ = iConfig.getParameter<edm::InputTag>("triggerSummaryLabel");
165  // triggerProcessLabel_ = iConfig.getUntrackedParameter<std::string>("triggerProcessLabel");
166  // triggerNames_ = iConfig.getParameter<std::vector<std::string> > ("triggerNames");
167  // isoTriggerNames_ = iConfig.getParameter<std::vector<std::string> > ("isoTriggerNames");
168  // triggerMaxDeltaR_ = iConfig.getParameter<double> ("triggerMaxDeltaR");
169 
170 }
171 
172 
174 {
175 
176  // do anything here that needs to be done at desctruction time
177  // (e.g. close files, deallocate resources etc.)
178 
179 }
180 
181 
182 //
183 // member functions
184 //
185 
186 // ------------ method called to for each event ------------
188 {
189 
190  muon->Reset();
192  iEvent.getByToken(MuonToken_, recoMuons);
193 
195  iEvent.getByToken(TriggerResultsToken_, triggerResults);
196 
197  edm::Handle<trigger::TriggerEvent> triggerSummaryLabel_;
198  iEvent.getByToken(triggerSummaryLabelToken_, triggerSummaryLabel_);
199 
200  if (recoMuons.isValid()) {
201  muon->SetMuon(iEvent, iSetup, recoMuons, maxMuon_);
202  }
203  else {
204 
205  }
206 
207  int counter_mu = 0;
208 
209  for(reco::MuonCollection::const_iterator imu = recoMuons->begin();
210  imu != recoMuons->end() && (unsigned) counter_mu < maxMuon_; imu++) {
211 
212  //---------------------------------------------------------------------
213  // TRIGGER MATCHING:
214  // if specified the reconstructed muons are matched to a trigger
215  //---------------------------------------------------------------------
216  if (triggerMatching_) {
217  double isoMatchDeltaR = 9999.;
218  double matchDeltaR = 9999.;
219  int hasIsoTriggered = 0;
220  int hasTriggered = 0;
221 
222 
223  // first check if the trigger results are valid:
224  if (triggerResults.isValid()) {
225 
226  if (triggerSummaryLabel_.isValid()) {
227  // get trigger objects:
228  const trigger::TriggerObjectCollection triggerObjects = triggerSummaryLabel_->getObjects();
229 
230  matchDeltaR = match_trigger(triggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
231  if (matchDeltaR < triggerMaxDeltaR_)
232  hasTriggered = 1;
233 
234  // cout<<"isoTriggerIndices_.size() = "<<isoTriggerIndices_.size()<<endl;
235  // cout<<"triggerObjects.size() = "<<triggerObjects.size()<<endl;
236  // cout<<"imu->eta() = "<<imu->eta()<<endl;
237 
238  isoMatchDeltaR = match_trigger(isoTriggerIndices_, triggerObjects, triggerSummaryLabel_, (*imu));
239  if (isoMatchDeltaR < triggerMaxDeltaR_)
240  hasIsoTriggered = 1;
241 
242  } // end if (triggerEvent.isValid())
243 
244  } // end if (triggerResults.isValid())
245 
246  // fill trigger matching variables:
247  muon_data->hlt_isomu.push_back(hasIsoTriggered);
248  muon_data->hlt_mu.push_back(hasTriggered);
249  muon_data->hlt_isoDeltaR.push_back(isoMatchDeltaR);
250  muon_data->hlt_deltaR.push_back(matchDeltaR);
251 
252  } else {
253  empty_hlt();
254  } // end if (triggerMatching_)
255  }
256 
257 
258  tree_->Fill();
259 
260 }
261 
262 // ------------ method called once each job just before starting event loop ------------
263 void
265 {
266 }
267 
268 // ------------ method called once each job just after ending the event loop ------------
269 void
271 }
272 
274 
275  muon_data->hlt_isomu.push_back(-9999);
276  muon_data->hlt_mu.push_back(-9999);
277  muon_data->hlt_isoDeltaR.push_back(-9999);
278  muon_data->hlt_deltaR.push_back(-9999);
279 
280 }
281 
283  std::vector<int> &trigIndices, const trigger::TriggerObjectCollection &trigObjs,
284  edm::Handle<trigger::TriggerEvent> &triggerEvent, const reco::Muon &mu
285  )
286 {
287  double matchDeltaR = 9999;
288 
289  for(size_t iTrigIndex = 0; iTrigIndex < trigIndices.size(); ++iTrigIndex) {
290  int triggerIndex = trigIndices[iTrigIndex];
291  const std::vector<std::string> moduleLabels(hltConfig_.moduleLabels(triggerIndex));
292  // find index of the last module:
293  const unsigned moduleIndex = hltConfig_.size(triggerIndex)-2;
294  // find index of HLT trigger name:
295  const unsigned hltFilterIndex = triggerEvent->filterIndex( edm::InputTag ( moduleLabels[moduleIndex], "", triggerProcessLabel_ ) );
296 
297  if (hltFilterIndex < triggerEvent->sizeFilters()) {
298  const trigger::Keys triggerKeys(triggerEvent->filterKeys(hltFilterIndex));
299  const trigger::Vids triggerVids(triggerEvent->filterIds(hltFilterIndex));
300 
301  const unsigned nTriggers = triggerVids.size();
302  for (size_t iTrig = 0; iTrig < nTriggers; ++iTrig) {
303  // loop over all trigger objects:
304  const trigger::TriggerObject trigObject = trigObjs[triggerKeys[iTrig]];
305 
306  double dRtmp = deltaR( mu, trigObject );
307 
308  if ( dRtmp < matchDeltaR ) {
309  matchDeltaR = dRtmp;
310  }
311 
312  } // loop over different trigger objects
313  } // if trigger is in event (should apply hltFilter with used trigger...)
314  } // loop over muon candidates
315 
316  return matchDeltaR;
317 }
318 
320  // Prepare for trigger matching for each new run:
321  // Look up triggetIndices in the HLT config for the different paths
322  if (triggerMatching_) {
323  bool changed = true;
324  if (!hltConfig_.init(run, eventSetup, triggerProcessLabel_, changed)) {
325  // if you can't initialize hlt configuration, crash!
326  std::cout << "Error: didn't find process" << triggerProcessLabel_ << std::endl;
327  assert(false);
328  }
329 
330  bool enableWildcard = true;
331  for (size_t iTrig = 0; iTrig < triggerNames_.size(); ++iTrig) {
332  // prepare for regular expression (with wildcards) functionality:
333  TString tNameTmp = TString(triggerNames_[iTrig]);
334  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
335  int tIndex = -1;
336  // find the trigger index:
337  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
338  // use TString since it provides reg exp functionality:
339  TString tmpName = TString(hltConfig_.triggerName(ipath));
340  if (tmpName.Contains(tNamePattern)) {
341  tIndex = int(ipath);
342  triggerIndices_.push_back(tIndex);
343  }
344  }
345  if (tIndex < 0) { // if can't find trigger path at all, give warning:
346  std::cout << "Warning: Could not find trigger" << triggerNames_[iTrig] << std::endl;
347  //assert(false);
348  }
349  } // end for triggerNames
350  for (size_t iTrig = 0; iTrig < isoTriggerNames_.size(); ++iTrig) {
351 
352  // prepare for regular expression functionality:
353  TString tNameTmp = TString(isoTriggerNames_[iTrig]);
354  TRegexp tNamePattern = TRegexp(tNameTmp, enableWildcard);
355  int tIndex = -1;
356  // find the trigger index:
357  for (unsigned ipath = 0; ipath < hltConfig_.size(); ++ipath) {
358  // use TString since it provides reg exp functionality:
359  TString tmpName = TString(hltConfig_.triggerName(ipath));
360  if (tmpName.Contains(tNamePattern)) {
361  tIndex = int(ipath);
362  isoTriggerIndices_.push_back(tIndex);
363  }
364  }
365  if (tIndex < 0) { // if can't find trigger path at all, give warning:
366  std::cout << "Warning: Could not find trigger" << isoTriggerNames_[iTrig] << std::endl;
367  //assert(false);
368  }
369  } // end for isoTriggerNames
370  } // end if (triggerMatching_)
371 }
372 
373 //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:462
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
assert(m_qm.get())
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)
edm::EDGetTokenT< reco::MuonCollection > MuonToken_
int iEvent
Definition: GenABIO.cc:230
edm::Service< TFileService > fs_
virtual void beginRun(const edm::Run &, const edm::EventSetup &)
const int mu
Definition: Constants.h:22
static std::string const triggerResults
Definition: EdmProvDump.cc:41
bool isValid() const
Definition: HandleBase.h:75
virtual void analyze(const edm::Event &, const edm::EventSetup &)
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
std::vector< size_type > Keys
L1Muon2RecoTreeProducer(const edm::ParameterSet &)
edm::EDGetTokenT< edm::TriggerResults > TriggerResultsToken_
L1Analysis::L1AnalysisRecoMuon2 * muon
tuple cout
Definition: gather_cfg.py:145
std::vector< int > isoTriggerIndices_
edm::EDGetTokenT< trigger::TriggerEvent > triggerSummaryLabelToken_
std::vector< std::string > triggerNames_
std::vector< int > Vids
Definition: Run.h:43
std::vector< std::string > isoTriggerNames_