Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-12-10 02:51:18

0001 #include <map>
0002 #include <string>
0003 
0004 #include "TH1D.h"
0005 #include "TH2D.h"
0006 
0007 #include "FWCore/Framework/interface/Event.h"
0008 #include "FWCore/Utilities/interface/InputTag.h"
0009 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0010 #include "FWCore/Framework/interface/Frameworkfwd.h"
0011 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0012 #include "DataFormats/PatCandidates/interface/Muon.h"
0013 #include "DataFormats/PatCandidates/interface/TriggerEvent.h"
0014 
0015 class PatTriggerAnalyzer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0016 public:
0017   /// default constructor
0018   explicit PatTriggerAnalyzer(const edm::ParameterSet& iConfig);
0019   /// default destructor
0020   ~PatTriggerAnalyzer() override;
0021 
0022 private:
0023   /// everythin that needs to be done before the event loop
0024   void beginJob() override;
0025   /// everythin that needs to be done during the event loop
0026   void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0027   /// everythin that needs to be done after the event loop
0028   void endJob() override;
0029 
0030   /// input for patTrigger
0031   edm::InputTag trigger_;
0032   /// input for patTriggerEvent
0033   edm::EDGetTokenT<pat::TriggerEvent> triggerEventToken_;
0034   /// input for muons
0035   edm::EDGetTokenT<pat::MuonCollection> muonsToken_;
0036   /// input for trigger match objects
0037   std::string muonMatch_;
0038   /// minimal id for meanPt plot
0039   unsigned minID_;
0040   /// maximal id for meanPt plot
0041   unsigned maxID_;
0042 
0043   /// histogram management
0044   std::map<std::string, TH1D*> histos1D_;
0045   std::map<std::string, TH2D*> histos2D_;
0046 
0047   /// internals for meanPt histogram calculation
0048   std::map<unsigned, unsigned> sumN_;
0049   std::map<unsigned, double> sumPt_;
0050 };
0051 
0052 #include "TMath.h"
0053 
0054 #include "FWCore/ServiceRegistry/interface/Service.h"
0055 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0056 #include "PhysicsTools/PatUtils/interface/TriggerHelper.h"
0057 
0058 using namespace pat;
0059 
0060 PatTriggerAnalyzer::PatTriggerAnalyzer(const edm::ParameterSet& iConfig)
0061     :  // pat::Trigger
0062       trigger_(iConfig.getParameter<edm::InputTag>("trigger")),
0063       // pat::TriggerEvent
0064       triggerEventToken_(consumes<TriggerEvent>(iConfig.getParameter<edm::InputTag>("triggerEvent"))),
0065       // muon input collection
0066       muonsToken_(consumes<MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0067       // muon match objects
0068       muonMatch_(iConfig.getParameter<std::string>("muonMatch")),
0069       // minimal id for of all trigger objects
0070       minID_(iConfig.getParameter<unsigned>("minID")),
0071       // maximal id for of all trigger objects
0072       maxID_(iConfig.getParameter<unsigned>("maxID")),
0073       histos1D_(),
0074       histos2D_() {
0075   usesResource(TFileService::kSharedResource);
0076 }
0077 
0078 PatTriggerAnalyzer::~PatTriggerAnalyzer() {}
0079 
0080 void PatTriggerAnalyzer::beginJob() {
0081   edm::Service<TFileService> fileService;
0082 
0083   // pt correlation plot
0084   histos2D_["ptTrigCand"] =
0085       fileService->make<TH2D>("ptTrigCand", "Object vs. candidate p_{T} (GeV)", 60, 0., 300., 60, 0., 300.);
0086   histos2D_["ptTrigCand"]->SetXTitle("candidate p_{T} (GeV)");
0087   histos2D_["ptTrigCand"]->SetYTitle("object p_{T} (GeV)");
0088   // eta correlation plot
0089   histos2D_["etaTrigCand"] =
0090       fileService->make<TH2D>("etaTrigCand", "Object vs. candidate #eta", 50, -2.5, 2.5, 50, -2.5, 2.5);
0091   histos2D_["etaTrigCand"]->SetXTitle("candidate #eta");
0092   histos2D_["etaTrigCand"]->SetYTitle("object #eta");
0093   // phi correlation plot
0094   histos2D_["phiTrigCand"] = fileService->make<TH2D>(
0095       "phiTrigCand", "Object vs. candidate #phi", 60, -TMath::Pi(), TMath::Pi(), 60, -TMath::Pi(), TMath::Pi());
0096   histos2D_["phiTrigCand"]->SetXTitle("candidate #phi");
0097   histos2D_["phiTrigCand"]->SetYTitle("object #phi");
0098   // turn-on curves
0099   histos1D_["turnOn"] = fileService->make<TH1D>("turnOn", "p_{T} (GeV) of matched candidate", 10, 0., 50.);
0100   histos1D_["turnOn"]->SetXTitle("candidate p_{T} (GeV)");
0101   histos1D_["turnOn"]->SetYTitle("# of objects");
0102   // mean pt for all trigger objects
0103   histos1D_["ptMean"] = fileService->make<TH1D>(
0104       "ptMean", "Mean p_{T} (GeV) per trigger object type", maxID_ - minID_ + 1, minID_ - 0.5, maxID_ + 0.5);
0105   histos1D_["ptMean"]->SetXTitle("trigger object type");
0106   histos1D_["ptMean"]->SetYTitle("mean p_{T} (GeV)");
0107 
0108   // initialize counters for mean pt calculation
0109   for (unsigned id = minID_; id <= maxID_; ++id) {
0110     sumN_[id] = 0;
0111     sumPt_[id] = 0.;
0112   }
0113 }
0114 
0115 void PatTriggerAnalyzer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0116   // PAT trigger event
0117   edm::Handle<TriggerEvent> triggerEvent;
0118   iEvent.getByToken(triggerEventToken_, triggerEvent);
0119 
0120   // PAT object collection
0121   edm::Handle<MuonCollection> muons;
0122   iEvent.getByToken(muonsToken_, muons);
0123 
0124   // PAT trigger helper for trigger matching information
0125   const helper::TriggerMatchHelper matchHelper;
0126 
0127   /*
0128     kinematics comparison
0129   */
0130 
0131   // loop over muon references (PAT muons have been used in the matcher in task 3)
0132   for (size_t iMuon = 0; iMuon < muons->size(); ++iMuon) {
0133     // we need all these ingedients to recieve matched trigger objects from the matchHelper
0134     const TriggerObjectRef trigRef(matchHelper.triggerMatchObject(muons, iMuon, muonMatch_, iEvent, *triggerEvent));
0135     // finally we can fill the histograms
0136     if (trigRef.isAvailable() && trigRef.isNonnull()) {  // check references (necessary!)
0137       histos2D_["ptTrigCand"]->Fill(muons->at(iMuon).pt(), trigRef->pt());
0138       histos2D_["etaTrigCand"]->Fill(muons->at(iMuon).eta(), trigRef->eta());
0139       histos2D_["phiTrigCand"]->Fill(muons->at(iMuon).phi(), trigRef->phi());
0140     }
0141   }
0142 
0143   /*
0144     turn-on curve
0145   */
0146 
0147   // get the trigger objects corresponding to the used matching (HLT muons)
0148   const TriggerObjectRefVector trigRefs(triggerEvent->objects(trigger::TriggerMuon));
0149   // loop over selected trigger objects
0150   for (TriggerObjectRefVector::const_iterator iTrig = trigRefs.begin(); iTrig != trigRefs.end(); ++iTrig) {
0151     // get all matched candidates for the trigger object
0152     const reco::CandidateBaseRefVector candRefs(
0153         matchHelper.triggerMatchCandidates((*iTrig), muonMatch_, iEvent, *triggerEvent));
0154     if (candRefs.empty())
0155       continue;
0156     // fill the histogram...
0157     // (only for the first match, since we resolved ambiguities in the matching configuration,
0158     // so that we have one at maximum per trigger object)
0159     reco::CandidateBaseRef muonRef(candRefs.at(0));
0160     if (muonRef.isAvailable() && muonRef.isNonnull()) {
0161       histos1D_["turnOn"]->Fill(muonRef->pt());
0162     }
0163   }
0164 
0165   /*
0166     mean pt
0167   */
0168 
0169   // loop over all trigger match objects from minID to maxID; have
0170   // a look to DataFormats/HLTReco/interface/TriggerTypeDefs.h to
0171   // know more about the available trrigger object id's
0172   for (unsigned id = minID_; id <= maxID_; ++id) {
0173     // vector of all objects for a given object id
0174     const TriggerObjectRefVector objRefs(triggerEvent->objects(id));
0175     // buffer the number of objects
0176     sumN_[id] += objRefs.size();
0177     // iterate the objects and buffer the pt of the objects
0178     for (TriggerObjectRefVector::const_iterator iRef = objRefs.begin(); iRef != objRefs.end(); ++iRef) {
0179       sumPt_[id] += (*iRef)->pt();
0180     }
0181   }
0182 }
0183 
0184 void PatTriggerAnalyzer::endJob() {
0185   // normalize the entries for the mean pt plot
0186   for (unsigned id = minID_; id <= maxID_; ++id) {
0187     if (sumN_[id] != 0)
0188       histos1D_["ptMean"]->Fill(id, sumPt_[id] / sumN_[id]);
0189   }
0190 }
0191 
0192 #include "FWCore/Framework/interface/MakerMacros.h"
0193 DEFINE_FWK_MODULE(PatTriggerAnalyzer);