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
L1Muon2RecoTreeProducer::muon
L1Analysis::L1AnalysisRecoMuon2 * muon
Definition: L1Muon2RecoTreeProducer.cc:97
L1Muon2RecoTreeProducer::endJob
void endJob() override
Definition: L1Muon2RecoTreeProducer.cc:309
CaloJetCollection.h
L1Analysis::L1AnalysisRecoMuon2DataFormat
Definition: L1AnalysisRecoMuon2DataFormat.h:14
bk::beginJob
void beginJob()
Definition: Breakpoints.cc:14
L1Muon2RecoTreeProducer::metToken_
edm::EDGetTokenT< reco::PFMETCollection > metToken_
Definition: L1Muon2RecoTreeProducer.cc:119
L1Muon2RecoTreeProducer::isoTriggerNames_
std::vector< std::string > isoTriggerNames_
Definition: L1Muon2RecoTreeProducer.cc:138
PFTauFwd.h
Muon.h
MessageLogger.h
Cylinder.h
L1Muon2RecoTreeProducer::triggerMaxDeltaR_
double triggerMaxDeltaR_
Definition: L1Muon2RecoTreeProducer.cc:135
JetID.h
trigNames
static const char *const trigNames[]
Definition: EcalDumpRaw.cc:57
triggerMatchMonitor_cfi.triggerObjects
triggerObjects
Definition: triggerMatchMonitor_cfi.py:15
trigger::Vids
std::vector< int > Vids
Definition: TriggerTypeDefs.h:21
ESHandle.h
TriggerResults.h
PFTauDiscriminator.h
muon
Definition: MuonCocktails.h:17
amptDefaultParameters_cff.mu
mu
Definition: amptDefaultParameters_cff.py:16
edm::Run
Definition: Run.h:45
TriggerAnalyzer.trigObjs
trigObjs
Definition: TriggerAnalyzer.py:89
TriggerEvent.h
edm::EDGetTokenT< reco::MuonCollection >
gather_cfg.cout
cout
Definition: gather_cfg.py:144
L1Muon2RecoTreeProducer::triggerMatching_
bool triggerMatching_
Definition: L1Muon2RecoTreeProducer.cc:136
PFJetCollection.h
triggerResults
static const std::string triggerResults
Definition: EdmProvDump.cc:45
cms::cuda::assert
assert(be >=bs)
L1Analysis::L1AnalysisRecoMuon2
Definition: L1AnalysisRecoMuon2.h:40
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
L1AnalysisRecoVertexDataFormat.h
EDAnalyzer.h
PFMETCollection.h
L1Muon2RecoTreeProducer::match_trigger
double match_trigger(std::vector< int > &trigIndices, const trigger::TriggerObjectCollection &trigObjs, edm::Handle< trigger::TriggerEvent > &triggerEvent, const reco::Muon &mu)
Definition: L1Muon2RecoTreeProducer.cc:318
L1Muon2RecoTreeProducer::MuonToken_
edm::EDGetTokenT< reco::MuonCollection > MuonToken_
Definition: L1Muon2RecoTreeProducer.cc:115
edm::Handle< trigger::TriggerEvent >
L1Muon2RecoTreeProducer::fs_
edm::Service< TFileService > fs_
Definition: L1Muon2RecoTreeProducer.cc:109
L1Muon2RecoTreeProducer::~L1Muon2RecoTreeProducer
~L1Muon2RecoTreeProducer() override
Definition: L1Muon2RecoTreeProducer.cc:182
TrackerSurfaceDeformationRcd.h
reco::Muon
Definition: Muon.h:27
edm::EDAnalyzer
Definition: EDAnalyzer.h:28
Plane.h
MakerMacros.h
Track.h
TrackFwd.h
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
L1Muon2RecoTreeProducer::beginRun
void beginRun(const edm::Run &, const edm::EventSetup &) override
Definition: L1Muon2RecoTreeProducer.cc:355
hltMonBTagIPClient_cfi.pathName
pathName
Definition: hltMonBTagIPClient_cfi.py:5
Service.h
MuonFwd.h
L1Muon2RecoTreeProducer::hltConfig_
HLTConfigProvider hltConfig_
Definition: L1Muon2RecoTreeProducer.cc:131
L1Muon2RecoTreeProducer::maxMuon_
unsigned int maxMuon_
Definition: L1Muon2RecoTreeProducer.cc:134
JetCorrector.h
trigger::TriggerObject
Single trigger physics object (e.g., an isolated muon)
Definition: TriggerObject.h:21
PbPb_ZMuSkimMuonDPG_cff.deltaR
deltaR
Definition: PbPb_ZMuSkimMuonDPG_cff.py:63
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
Vertex.h
TFileService.h
L1AnalysisRecoMet.h
edm::ParameterSet
Definition: ParameterSet.h:47
Event.h
deltaR.h
L1Muon2RecoTreeProducer::L1Muon2RecoTreeProducer
L1Muon2RecoTreeProducer(const edm::ParameterSet &)
Definition: L1Muon2RecoTreeProducer.cc:144
MuonTime.h
trigger::TriggerObjectCollection
std::vector< TriggerObject > TriggerObjectCollection
collection of trigger physics objects (e.g., all isolated muons)
Definition: TriggerObject.h:75
PFMET.h
edm::Service< TFileService >
createfilelist.int
int
Definition: createfilelist.py:10
TriggerNames.h
iEvent
int iEvent
Definition: GenABIO.cc:224
trigger::Keys
std::vector< size_type > Keys
Definition: TriggerTypeDefs.h:19
L1Muon2RecoTreeProducer::tree_
TTree * tree_
Definition: L1Muon2RecoTreeProducer.cc:112
analyze
example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
L1Muon2RecoTreeProducer::TriggerResultsToken_
edm::EDGetTokenT< edm::TriggerResults > TriggerResultsToken_
Definition: L1Muon2RecoTreeProducer.cc:116
L1Muon2RecoTreeProducer::beginJob
void beginJob(void) override
Definition: L1Muon2RecoTreeProducer.cc:306
edm::EventSetup
Definition: EventSetup.h:58
L1Muon2RecoTreeProducer::isoTriggerToken_
edm::Handle< edm::TriggerResults > isoTriggerToken_
Definition: L1Muon2RecoTreeProducer.cc:127
L1Muon2RecoTreeProducer::empty_hlt
void empty_hlt()
Definition: L1Muon2RecoTreeProducer.cc:311
HLTConfigProvider.h
PDWG_DiPhoton_SD_cff.triggerEvent
triggerEvent
Definition: PDWG_DiPhoton_SD_cff.py:39
trigger::TriggerEvent::getObjects
const TriggerObjectCollection & getObjects() const
Definition: TriggerEvent.h:101
TriggerObject.h
VertexFwd.h
L1Muon2RecoTreeProducer::isoTriggerIndices_
std::vector< int > isoTriggerIndices_
Definition: L1Muon2RecoTreeProducer.cc:140
hltdqm::passTrig
bool passTrig(const float objEta, float objPhi, const trigger::TriggerEvent &trigEvt, const std::string &filterName, const std::string &processName)
Definition: UtilFuncs.h:14
HLTConfigProvider
Definition: HLTConfigProvider.h:29
std
Definition: JetResolutionObject.h:76
writedatasetfile.run
run
Definition: writedatasetfile.py:27
Frameworkfwd.h
L1Muon2RecoTreeProducer::triggerProcessLabel_
std::string triggerProcessLabel_
Definition: L1Muon2RecoTreeProducer.cc:137
edm::TriggerNames
Definition: TriggerNames.h:55
PFTau.h
L1Muon2RecoTreeProducer::triggerIndices_
std::vector< int > triggerIndices_
Definition: L1Muon2RecoTreeProducer.cc:141
EventSetup.h
L1Muon2RecoTreeProducer::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: L1Muon2RecoTreeProducer.cc:192
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
L1Muon2RecoTreeProducer::triggerSummaryLabelToken_
edm::EDGetTokenT< trigger::TriggerEvent > triggerSummaryLabelToken_
Definition: L1Muon2RecoTreeProducer.cc:117
L1Muon2RecoTreeProducer::triggerNames_
std::vector< std::string > triggerNames_
Definition: L1Muon2RecoTreeProducer.cc:139
L1Muon2RecoTreeProducer::muon_data
L1Analysis::L1AnalysisRecoMuon2DataFormat * muon_data
Definition: L1Muon2RecoTreeProducer.cc:99
L1Muon2RecoTreeProducer
Definition: L1Muon2RecoTreeProducer.cc:85
L1Muon2RecoTreeProducer::isoTriggerNamesToken_
edm::Handle< std::vector< std::string > > isoTriggerNamesToken_
Definition: L1Muon2RecoTreeProducer.cc:128
ParameterSet.h
L1AnalysisRecoMuon2.h
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
edm::Event
Definition: Event.h:73
MuonEnergy.h
edm::InputTag
Definition: InputTag.h:15
L1Muon2RecoTreeProducer::VtxToken_
edm::EDGetTokenT< reco::VertexCollection > VtxToken_
Definition: L1Muon2RecoTreeProducer.cc:118
pwdgSkimBPark_cfi.vertices
vertices
Definition: pwdgSkimBPark_cfi.py:7