CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
StudyTriggerHLT.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: IsolatedParticles
4 // Class: StudyTriggerHLT
5 //
13 //
14 // Original Author: Sunanda Banerjee
15 // Created: Thu Mar 4 18:52:02 CST 2011
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 #include <string>
22 
23 // Root objects
24 #include "TH1.h"
25 #include "TH2.h"
26 
27 // user include files
30 
35 
54 
55 class StudyTriggerHLT : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
56 public:
57  explicit StudyTriggerHLT(const edm::ParameterSet&);
58  ~StudyTriggerHLT() override {}
59 
60  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
61 
62 private:
63  void analyze(edm::Event const&, edm::EventSetup const&) override;
64  void beginJob() override;
65  void beginRun(edm::Run const&, edm::EventSetup const&) override;
66  void endRun(edm::Run const&, edm::EventSetup const&) override;
69 
71 
72  // ----------member data ---------------------------
75  const int verbosity_;
76  const std::vector<std::string> trigNames_, newNames_;
80  std::vector<std::string> HLTNames_;
83 
88  std::vector<bool> mediumMuon_;
90  TH2I* h_nHLTvsRN;
91  std::vector<TH1I*> h_HLTAccepts;
92  TH1D *h_pt, *h_eta, *h_phi, *h_dr1, *h_dr2, *h_dr3;
93  int nRun_;
94 };
95 
97  : verbosity_(iConfig.getParameter<int>("verbosity")),
98  triggerEvent_(edm::InputTag("hltTriggerSummaryAOD", "", "HLT")),
99  theTriggerResultsLabel_(edm::InputTag("TriggerResults", "", "HLT")),
100  labelMuon_(iConfig.getParameter<edm::InputTag>("labelMuon")),
101  labelGenTrack_(iConfig.getParameter<edm::InputTag>("labelTrack")),
102  theTrackQuality_(iConfig.getParameter<std::string>("trackQuality")),
103  nRun_(0) {
104  usesResource(TFileService::kSharedResource);
106 
107  // define tokens for access
108  tok_trigEvt = consumes<trigger::TriggerEvent>(triggerEvent_);
109  tok_trigRes = consumes<edm::TriggerResults>(theTriggerResultsLabel_);
110  tok_Muon_ = consumes<reco::MuonCollection>(labelMuon_);
111  tok_genTrack_ = consumes<reco::TrackCollection>(labelGenTrack_);
112 
113  edm::LogVerbatim("StudyHLT") << "Verbosity " << verbosity_ << " Trigger labels " << triggerEvent_ << " and "
114  << theTriggerResultsLabel_ << " Labels used: Track " << labelGenTrack_ << " Muon "
115  << labelMuon_ << " Track Quality " << theTrackQuality_;
116 
117  firstEvent_ = true;
118  changed_ = false;
119 }
120 
123  desc.add<int>("verbosity", 0);
124  desc.add<edm::InputTag>("labelMuon", edm::InputTag("muons", "", "RECO"));
125  desc.add<edm::InputTag>("labelTrack", edm::InputTag("generalTracks", "", "RECO"));
126  desc.add<std::string>("trackQuality", "highPurity");
127  descriptions.add("studyTriggerHLT", desc);
128 }
129 
131  int RunNo = iEvent.id().run();
132  int EvtNo = iEvent.id().event();
133 
134  if (verbosity_ > 0)
135  edm::LogVerbatim("StudyHLT") << "RunNo " << RunNo << " EvtNo " << EvtNo << " Lumi " << iEvent.luminosityBlock()
136  << " Bunch " << iEvent.bunchCrossing();
137 
138  trigger::TriggerEvent triggerEvent;
139  edm::Handle<trigger::TriggerEvent> triggerEventHandle;
140  iEvent.getByToken(tok_trigEvt, triggerEventHandle);
141 
142  if (!triggerEventHandle.isValid()) {
143  edm::LogWarning("StudyHLT") << "Error! Can't get the product " << triggerEvent_.label();
144  } else {
145  triggerEvent = *(triggerEventHandle.product());
146 
149  iEvent.getByToken(tok_trigRes, triggerResults);
150 
151  if (triggerResults.isValid()) {
152  h_nHLT->Fill(triggerResults->size());
153  h_nHLTvsRN->Fill(RunNo, triggerResults->size());
154 
155  const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
156  const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
157  for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
158  int ipos = -1;
159  std::string newtriggerName = truncate_str(triggerNames_[iHLT]);
160  for (unsigned int i = 0; i < HLTNames_.size(); ++i) {
161  if (newtriggerName == HLTNames_[i]) {
162  ipos = i + 1;
163  break;
164  }
165  }
166  if (ipos < 0) {
167  HLTNames_.push_back(newtriggerName);
168  ipos = (int)(HLTNames_.size());
169  if (ipos <= h_HLTAccept->GetNbinsX())
170  h_HLTAccept->GetXaxis()->SetBinLabel(ipos, newtriggerName.c_str());
171  }
172  if ((int)(iHLT + 1) > h_HLTAccepts[nRun_]->GetNbinsX()) {
173  edm::LogVerbatim("StudyHLT") << "Wrong trigger " << RunNo << " Event " << EvtNo << " Hlt " << iHLT;
174  } else {
175  if (firstEvent_)
176  h_HLTAccepts[nRun_]->GetXaxis()->SetBinLabel(iHLT + 1, newtriggerName.c_str());
177  }
178  int hlt = triggerResults->accept(iHLT);
179  if (hlt) {
180  h_HLTAccepts[nRun_]->Fill(iHLT + 1);
181  h_HLTAccept->Fill(ipos);
182  }
183  }
184  }
185  }
186  firstEvent_ = false;
187 
188  double globalMin = 1000;
189  edm::Handle<reco::MuonCollection> muonEventHandle;
190  iEvent.getByToken(tok_Muon_, muonEventHandle);
191  edm::Handle<reco::TrackCollection> trackEventHandle;
192  iEvent.getByToken(tok_genTrack_, trackEventHandle);
193  edm::LogVerbatim("StudyHLT") << "Muon Handle " << muonEventHandle.isValid() << " Track Handle "
194  << trackEventHandle.isValid();
195  for (reco::TrackCollection::const_iterator track1 = trackEventHandle->begin(); track1 != trackEventHandle->end();
196  ++track1) {
197  double localMin = 1000;
198  if (muonEventHandle.isValid()) {
199  for (reco::MuonCollection::const_iterator recMuon = muonEventHandle->begin(); recMuon != muonEventHandle->end();
200  ++recMuon) {
201  if (((recMuon->isPFMuon()) && (recMuon->isGlobalMuon() || recMuon->isTrackerMuon())) &&
202  (recMuon->innerTrack()->validFraction() > 0.49)) {
203  double chiGlobal = ((recMuon->globalTrack().isNonnull()) ? recMuon->globalTrack()->normalizedChi2() : 999);
204  bool goodGlob =
205  (recMuon->isGlobalMuon() && chiGlobal < 3 && recMuon->combinedQuality().chi2LocalPosition < 12 &&
206  recMuon->combinedQuality().trkKink < 20);
207  if (muon::segmentCompatibility(*recMuon) > (goodGlob ? 0.303 : 0.451)) {
208  double dr = reco::deltaR(track1->eta(), track1->phi(), recMuon->eta(), recMuon->phi());
209  if (dr < localMin) {
210  localMin = dr;
211  if (localMin < globalMin)
212  globalMin = localMin;
213  }
214  }
215  }
216  }
217  }
218  h_pt->Fill(track1->pt());
219  h_eta->Fill(track1->eta());
220  h_phi->Fill(track1->phi());
221  h_dr1->Fill(localMin);
222  if (track1->quality(trackQuality_))
223  h_dr3->Fill(localMin);
224  edm::LogVerbatim("StudyHLT") << "Track pT " << track1->pt() << " eta " << track1->eta() << " phi " << track1->phi()
225  << " minimum distance " << localMin;
226  }
227  edm::LogVerbatim("StudyHLT") << "GlobalMinimum = " << globalMin;
228  h_dr2->Fill(globalMin);
229 }
230 
232  // Book histograms
233  h_nHLT = fs_->make<TH1I>("h_nHLT", "size of trigger Names", 1000, 0, 1000);
234  h_HLTAccept = fs_->make<TH1I>("h_HLTAccept", "HLT Accepts for all runs", 500, 0, 500);
235  for (int i = 1; i <= 500; ++i)
236  h_HLTAccept->GetXaxis()->SetBinLabel(i, " ");
237  h_nHLTvsRN = fs_->make<TH2I>("h_nHLTvsRN", "size of trigger Names vs RunNo", 300, 319200, 319500, 100, 400, 500);
238  h_pt = fs_->make<TH1D>("h_pt", "p_{t}", 50, 0, 20);
239  h_pt->Sumw2();
240  h_eta = fs_->make<TH1D>("h_eta", "#eta", 50, -3, 3);
241  h_eta->Sumw2();
242  h_phi = fs_->make<TH1D>("h_phi", "#phi", 50, -10, 10);
243  h_phi->Sumw2();
244  h_dr1 = fs_->make<TH1D>("dR1", "#Delta R (Track)", 1000, 0, 0.1);
245  h_dr1->Sumw2();
246  h_dr2 = fs_->make<TH1D>("dR2", "#Delta R (Global)", 3000, 0, 0.000003);
247  h_dr2->Sumw2();
248  h_dr3 = fs_->make<TH1D>("dR3", "#Delta R (Good Track)", 1000, 0, 0.1);
249  h_dr3->Sumw2();
250 }
251 
252 // ------------ method called when starting to processes a run ------------
253 void StudyTriggerHLT::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
254  char hname[100], htit[400];
255  edm::LogVerbatim("StudyHLT") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init "
256  << hltConfig_.init(iRun, iSetup, "HLT", changed_);
257  sprintf(hname, "h_HLTAccepts_%i", iRun.run());
258  sprintf(htit, "HLT Accepts for Run No %i", iRun.run());
259  TH1I* hnew = fs_->make<TH1I>(hname, htit, 500, 0, 500);
260  for (int i = 1; i <= 500; ++i)
261  hnew->GetXaxis()->SetBinLabel(i, " ");
262  h_HLTAccepts.push_back(hnew);
263  edm::LogVerbatim("StudyHLT") << "beginRun " << iRun.run();
264  firstEvent_ = true;
265  changed_ = false;
266 }
267 
268 // ------------ method called when ending the processing of a run ------------
270  ++nRun_;
271  edm::LogVerbatim("StudyHLT") << "endRun[" << nRun_ << "] " << iRun.run();
272 }
273 
275  std::string truncated_str(str);
276  int length = str.length();
277  for (int i = 0; i < length - 2; i++) {
278  if (str[i] == '_' && str[i + 1] == 'v' && isdigit(str.at(i + 2))) {
279  int z = i + 1;
280  truncated_str = str.substr(0, z);
281  }
282  }
283  return (truncated_str);
284 }
285 
RunNumber_t run() const
Definition: EventID.h:38
static const std::string kSharedResource
Definition: TFileService.h:76
Log< level::Info, true > LogVerbatim
EventNumber_t event() const
Definition: EventID.h:40
const int verbosity_
const edm::InputTag theTriggerResultsLabel_
edm::EDGetTokenT< trigger::TriggerEvent > tok_trigEvt
RunNumber_t run() const
Definition: RunBase.h:40
The single EDProduct to be saved for each event (AOD case)
Definition: TriggerEvent.h:25
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
TrackQuality
track quality
Definition: TrackBase.h:150
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
int bunchCrossing() const
Definition: EventBase.h:64
std::vector< TH1I * > h_HLTAccepts
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
T * make(const Args &...args) const
make new ROOT object
Definition: TFileService.h:64
Strings const & triggerNames() const
Definition: TriggerNames.cc:48
void beginJob() override
edm::TriggerNames const & triggerNames(edm::TriggerResults const &triggerResults) const override
Definition: Event.cc:275
const edm::InputTag labelGenTrack_
int iEvent
Definition: GenABIO.cc:224
void beginRun(edm::Run const &, edm::EventSetup const &) override
edm::EDGetTokenT< reco::MuonCollection > tok_Muon_
edm::Service< TFileService > fs_
float segmentCompatibility(const reco::Muon &muon, reco::Muon::ArbitrationType arbitrationType=reco::Muon::SegmentAndTrackArbitration)
reco::TrackBase::TrackQuality trackQuality_
static std::string const triggerResults
Definition: EdmProvDump.cc:44
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:70
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
std::string truncate_str(const std::string &)
const std::vector< std::string > trigNames_
const edm::InputTag labelMuon_
static TrackQuality qualityByName(const std::string &name)
Definition: TrackBase.cc:126
const edm::InputTag triggerEvent_
edm::EDGetTokenT< reco::TrackCollection > tok_genTrack_
T const * product() const
Definition: Handle.h:70
edm::EDGetTokenT< edm::TriggerResults > tok_trigRes
bool init(const edm::Run &iRun, const edm::EventSetup &iSetup, const std::string &processName, bool &changed)
d&#39;tor
~StudyTriggerHLT() override
const std::vector< std::string > newNames_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::vector< bool > mediumMuon_
virtual void endLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &)
StudyTriggerHLT(const edm::ParameterSet &)
std::string const & label() const
Definition: InputTag.h:36
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EventID id() const
Definition: EventBase.h:59
void endRun(edm::Run const &, edm::EventSetup const &) override
void analyze(edm::Event const &, edm::EventSetup const &) override
Log< level::Warning, false > LogWarning
HLTConfigProvider hltConfig_
#define str(s)
const std::string theTrackQuality_
std::vector< std::string > HLTNames_
Definition: Run.h:45
virtual void beginLuminosityBlock(edm::LuminosityBlock const &, edm::EventSetup const &)