Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:59:18

0001 // -*- C++ -*-
0002 //
0003 // Package:    IsolatedParticles
0004 // Class:      StudyTriggerHLT
0005 //
0006 /**\class StudyTriggerHLT StudyTriggerHLT.cc Calibration/IsolatedParticles/plugins/StudyTriggerHLT.cc
0007 
0008  Description: Studies single particle response measurements in data/MC
0009 
0010  Implementation:
0011      <Notes on implementation>
0012 */
0013 //
0014 // Original Author:  Sunanda Banerjee
0015 //         Created:  Thu Mar  4 18:52:02 CST 2011
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 #include <string>
0022 
0023 // Root objects
0024 #include "TH1.h"
0025 #include "TH2.h"
0026 
0027 // user include files
0028 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0029 #include "CondFormats/DataRecord/interface/EcalChannelStatusRcd.h"
0030 
0031 #include "DataFormats/Common/interface/TriggerResults.h"
0032 #include "DataFormats/HLTReco/interface/TriggerEvent.h"
0033 #include "DataFormats/HLTReco/interface/TriggerObject.h"
0034 #include "DataFormats/L1GlobalTrigger/interface/L1GlobalTriggerReadoutRecord.h"
0035 
0036 #include "FWCore/Framework/interface/Frameworkfwd.h"
0037 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0038 #include "FWCore/Framework/interface/Event.h"
0039 #include "FWCore/Framework/interface/MakerMacros.h"
0040 #include "FWCore/ServiceRegistry/interface/Service.h"
0041 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0042 #include "FWCore/Common/interface/TriggerNames.h"
0043 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0044 #include "DataFormats/Math/interface/deltaR.h"
0045 #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
0046 #include "DataFormats/MuonReco/interface/Muon.h"
0047 #include "DataFormats/MuonReco/interface/MuonFwd.h"
0048 #include "DataFormats/MuonReco/interface/MuonSelectors.h"
0049 #include "DataFormats/TrackReco/interface/Track.h"
0050 #include "DataFormats/TrackReco/interface/TrackFwd.h"
0051 #include "DataFormats/TrackReco/interface/TrackBase.h"
0052 #include "DataFormats/TrackReco/interface/HitPattern.h"
0053 #include "Calibration/IsolatedParticles/interface/TrackSelection.h"
0054 
0055 class StudyTriggerHLT : public edm::one::EDAnalyzer<edm::one::WatchRuns, edm::one::SharedResources> {
0056 public:
0057   explicit StudyTriggerHLT(const edm::ParameterSet&);
0058   ~StudyTriggerHLT() override {}
0059 
0060   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0061 
0062 private:
0063   void analyze(edm::Event const&, edm::EventSetup const&) override;
0064   void beginJob() override;
0065   void beginRun(edm::Run const&, edm::EventSetup const&) override;
0066   void endRun(edm::Run const&, edm::EventSetup const&) override;
0067   virtual void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {}
0068   virtual void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) {}
0069 
0070   std::string truncate_str(const std::string&);
0071 
0072   // ----------member data ---------------------------
0073   HLTConfigProvider hltConfig_;
0074   edm::Service<TFileService> fs_;
0075   const int verbosity_;
0076   const std::vector<std::string> trigNames_, newNames_;
0077   const edm::InputTag triggerEvent_, theTriggerResultsLabel_;
0078   const edm::InputTag labelMuon_, labelGenTrack_;
0079   const std::string theTrackQuality_;
0080   std::vector<std::string> HLTNames_;
0081   bool changed_, firstEvent_;
0082   reco::TrackBase::TrackQuality trackQuality_;
0083 
0084   edm::EDGetTokenT<trigger::TriggerEvent> tok_trigEvt;
0085   edm::EDGetTokenT<edm::TriggerResults> tok_trigRes;
0086   edm::EDGetTokenT<reco::MuonCollection> tok_Muon_;
0087   edm::EDGetTokenT<reco::TrackCollection> tok_genTrack_;
0088   std::vector<bool> mediumMuon_;
0089   TH1I *h_nHLT, *h_HLTAccept, *h_HLTCorr;
0090   TH2I* h_nHLTvsRN;
0091   std::vector<TH1I*> h_HLTAccepts;
0092   TH1D *h_pt, *h_eta, *h_phi, *h_dr1, *h_dr2, *h_dr3;
0093   int nRun_;
0094 };
0095 
0096 StudyTriggerHLT::StudyTriggerHLT(const edm::ParameterSet& iConfig)
0097     : verbosity_(iConfig.getParameter<int>("verbosity")),
0098       triggerEvent_(edm::InputTag("hltTriggerSummaryAOD", "", "HLT")),
0099       theTriggerResultsLabel_(edm::InputTag("TriggerResults", "", "HLT")),
0100       labelMuon_(iConfig.getParameter<edm::InputTag>("labelMuon")),
0101       labelGenTrack_(iConfig.getParameter<edm::InputTag>("labelTrack")),
0102       theTrackQuality_(iConfig.getParameter<std::string>("trackQuality")),
0103       nRun_(0) {
0104   usesResource(TFileService::kSharedResource);
0105   trackQuality_ = reco::TrackBase::qualityByName(theTrackQuality_);
0106 
0107   // define tokens for access
0108   tok_trigEvt = consumes<trigger::TriggerEvent>(triggerEvent_);
0109   tok_trigRes = consumes<edm::TriggerResults>(theTriggerResultsLabel_);
0110   tok_Muon_ = consumes<reco::MuonCollection>(labelMuon_);
0111   tok_genTrack_ = consumes<reco::TrackCollection>(labelGenTrack_);
0112 
0113   edm::LogVerbatim("StudyHLT") << "Verbosity " << verbosity_ << " Trigger labels " << triggerEvent_ << " and "
0114                                << theTriggerResultsLabel_ << " Labels used: Track " << labelGenTrack_ << " Muon "
0115                                << labelMuon_ << " Track Quality " << theTrackQuality_;
0116 
0117   firstEvent_ = true;
0118   changed_ = false;
0119 }
0120 
0121 void StudyTriggerHLT::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0122   edm::ParameterSetDescription desc;
0123   desc.add<int>("verbosity", 0);
0124   desc.add<edm::InputTag>("labelMuon", edm::InputTag("muons", "", "RECO"));
0125   desc.add<edm::InputTag>("labelTrack", edm::InputTag("generalTracks", "", "RECO"));
0126   desc.add<std::string>("trackQuality", "highPurity");
0127   descriptions.add("studyTriggerHLT", desc);
0128 }
0129 
0130 void StudyTriggerHLT::analyze(edm::Event const& iEvent, edm::EventSetup const& iSetup) {
0131   int RunNo = iEvent.id().run();
0132   int EvtNo = iEvent.id().event();
0133 
0134   if (verbosity_ > 0)
0135     edm::LogVerbatim("StudyHLT") << "RunNo " << RunNo << " EvtNo " << EvtNo << " Lumi " << iEvent.luminosityBlock()
0136                                  << " Bunch " << iEvent.bunchCrossing();
0137 
0138   trigger::TriggerEvent triggerEvent;
0139   edm::Handle<trigger::TriggerEvent> triggerEventHandle;
0140   iEvent.getByToken(tok_trigEvt, triggerEventHandle);
0141 
0142   if (!triggerEventHandle.isValid()) {
0143     edm::LogWarning("StudyHLT") << "Error! Can't get the product " << triggerEvent_.label();
0144   } else {
0145     triggerEvent = *(triggerEventHandle.product());
0146 
0147     /////////////////////////////TriggerResults
0148     edm::Handle<edm::TriggerResults> triggerResults;
0149     iEvent.getByToken(tok_trigRes, triggerResults);
0150 
0151     if (triggerResults.isValid()) {
0152       h_nHLT->Fill(triggerResults->size());
0153       h_nHLTvsRN->Fill(RunNo, triggerResults->size());
0154 
0155       const edm::TriggerNames& triggerNames = iEvent.triggerNames(*triggerResults);
0156       const std::vector<std::string>& triggerNames_ = triggerNames.triggerNames();
0157       for (unsigned int iHLT = 0; iHLT < triggerResults->size(); iHLT++) {
0158         int ipos = -1;
0159         std::string newtriggerName = truncate_str(triggerNames_[iHLT]);
0160         for (unsigned int i = 0; i < HLTNames_.size(); ++i) {
0161           if (newtriggerName == HLTNames_[i]) {
0162             ipos = i + 1;
0163             break;
0164           }
0165         }
0166         if (ipos < 0) {
0167           HLTNames_.push_back(newtriggerName);
0168           ipos = (int)(HLTNames_.size());
0169           if (ipos <= h_HLTAccept->GetNbinsX())
0170             h_HLTAccept->GetXaxis()->SetBinLabel(ipos, newtriggerName.c_str());
0171         }
0172         if ((int)(iHLT + 1) > h_HLTAccepts[nRun_]->GetNbinsX()) {
0173           edm::LogVerbatim("StudyHLT") << "Wrong trigger " << RunNo << " Event " << EvtNo << " Hlt " << iHLT;
0174         } else {
0175           if (firstEvent_)
0176             h_HLTAccepts[nRun_]->GetXaxis()->SetBinLabel(iHLT + 1, newtriggerName.c_str());
0177         }
0178         int hlt = triggerResults->accept(iHLT);
0179         if (hlt) {
0180           h_HLTAccepts[nRun_]->Fill(iHLT + 1);
0181           h_HLTAccept->Fill(ipos);
0182         }
0183       }
0184     }
0185   }
0186   firstEvent_ = false;
0187 
0188   double globalMin = 1000;
0189   edm::Handle<reco::MuonCollection> muonEventHandle;
0190   iEvent.getByToken(tok_Muon_, muonEventHandle);
0191   edm::Handle<reco::TrackCollection> trackEventHandle;
0192   iEvent.getByToken(tok_genTrack_, trackEventHandle);
0193   edm::LogVerbatim("StudyHLT") << "Muon Handle " << muonEventHandle.isValid() << " Track Handle "
0194                                << trackEventHandle.isValid();
0195   for (reco::TrackCollection::const_iterator track1 = trackEventHandle->begin(); track1 != trackEventHandle->end();
0196        ++track1) {
0197     double localMin = 1000;
0198     if (muonEventHandle.isValid()) {
0199       for (reco::MuonCollection::const_iterator recMuon = muonEventHandle->begin(); recMuon != muonEventHandle->end();
0200            ++recMuon) {
0201         if (((recMuon->isPFMuon()) && (recMuon->isGlobalMuon() || recMuon->isTrackerMuon())) &&
0202             (recMuon->innerTrack()->validFraction() > 0.49)) {
0203           double chiGlobal = ((recMuon->globalTrack().isNonnull()) ? recMuon->globalTrack()->normalizedChi2() : 999);
0204           bool goodGlob =
0205               (recMuon->isGlobalMuon() && chiGlobal < 3 && recMuon->combinedQuality().chi2LocalPosition < 12 &&
0206                recMuon->combinedQuality().trkKink < 20);
0207           if (muon::segmentCompatibility(*recMuon) > (goodGlob ? 0.303 : 0.451)) {
0208             double dr = reco::deltaR(track1->eta(), track1->phi(), recMuon->eta(), recMuon->phi());
0209             if (dr < localMin) {
0210               localMin = dr;
0211               if (localMin < globalMin)
0212                 globalMin = localMin;
0213             }
0214           }
0215         }
0216       }
0217     }
0218     h_pt->Fill(track1->pt());
0219     h_eta->Fill(track1->eta());
0220     h_phi->Fill(track1->phi());
0221     h_dr1->Fill(localMin);
0222     if (track1->quality(trackQuality_))
0223       h_dr3->Fill(localMin);
0224     edm::LogVerbatim("StudyHLT") << "Track pT " << track1->pt() << " eta " << track1->eta() << " phi " << track1->phi()
0225                                  << " minimum distance " << localMin;
0226   }
0227   edm::LogVerbatim("StudyHLT") << "GlobalMinimum  = " << globalMin;
0228   h_dr2->Fill(globalMin);
0229 }
0230 
0231 void StudyTriggerHLT::beginJob() {
0232   // Book histograms
0233   h_nHLT = fs_->make<TH1I>("h_nHLT", "size of trigger Names", 1000, 0, 1000);
0234   h_HLTAccept = fs_->make<TH1I>("h_HLTAccept", "HLT Accepts for all runs", 500, 0, 500);
0235   for (int i = 1; i <= 500; ++i)
0236     h_HLTAccept->GetXaxis()->SetBinLabel(i, " ");
0237   h_nHLTvsRN = fs_->make<TH2I>("h_nHLTvsRN", "size of trigger Names vs RunNo", 300, 319200, 319500, 100, 400, 500);
0238   h_pt = fs_->make<TH1D>("h_pt", "p_{t}", 50, 0, 20);
0239   h_pt->Sumw2();
0240   h_eta = fs_->make<TH1D>("h_eta", "#eta", 50, -3, 3);
0241   h_eta->Sumw2();
0242   h_phi = fs_->make<TH1D>("h_phi", "#phi", 50, -10, 10);
0243   h_phi->Sumw2();
0244   h_dr1 = fs_->make<TH1D>("dR1", "#Delta R (Track)", 1000, 0, 0.1);
0245   h_dr1->Sumw2();
0246   h_dr2 = fs_->make<TH1D>("dR2", "#Delta R (Global)", 3000, 0, 0.000003);
0247   h_dr2->Sumw2();
0248   h_dr3 = fs_->make<TH1D>("dR3", "#Delta R (Good Track)", 1000, 0, 0.1);
0249   h_dr3->Sumw2();
0250 }
0251 
0252 // ------------ method called when starting to processes a run  ------------
0253 void StudyTriggerHLT::beginRun(edm::Run const& iRun, edm::EventSetup const& iSetup) {
0254   char hname[100], htit[400];
0255   edm::LogVerbatim("StudyHLT") << "Run[" << nRun_ << "] " << iRun.run() << " hltconfig.init "
0256                                << hltConfig_.init(iRun, iSetup, "HLT", changed_);
0257   sprintf(hname, "h_HLTAccepts_%i", iRun.run());
0258   sprintf(htit, "HLT Accepts for Run No %i", iRun.run());
0259   TH1I* hnew = fs_->make<TH1I>(hname, htit, 500, 0, 500);
0260   for (int i = 1; i <= 500; ++i)
0261     hnew->GetXaxis()->SetBinLabel(i, " ");
0262   h_HLTAccepts.push_back(hnew);
0263   edm::LogVerbatim("StudyHLT") << "beginRun " << iRun.run();
0264   firstEvent_ = true;
0265   changed_ = false;
0266 }
0267 
0268 // ------------ method called when ending the processing of a run  ------------
0269 void StudyTriggerHLT::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0270   ++nRun_;
0271   edm::LogVerbatim("StudyHLT") << "endRun[" << nRun_ << "] " << iRun.run();
0272 }
0273 
0274 std::string StudyTriggerHLT::truncate_str(const std::string& str) {
0275   std::string truncated_str(str);
0276   int length = str.length();
0277   for (int i = 0; i < length - 2; i++) {
0278     if (str[i] == '_' && str[i + 1] == 'v' && isdigit(str.at(i + 2))) {
0279       int z = i + 1;
0280       truncated_str = str.substr(0, z);
0281     }
0282   }
0283   return (truncated_str);
0284 }
0285 
0286 DEFINE_FWK_MODULE(StudyTriggerHLT);