Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:16:30

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 PatTriggerTagAndProbe : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0016 public:
0017   /// default constructor
0018   explicit PatTriggerTagAndProbe(const edm::ParameterSet& iConfig);
0019   /// default destructor
0020   ~PatTriggerTagAndProbe() 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   /// helper function to set proper bin errors
0031   void setErrors(TH1D& h, const TH1D& ref);
0032 
0033   /// input for patTriggerEvent
0034   edm::EDGetTokenT<pat::TriggerEvent> triggerEventToken_;
0035   /// input for muons
0036   edm::EDGetTokenT<pat::MuonCollection> muonsToken_;
0037   /// input for trigger match objects
0038   std::string muonMatch_;
0039   /// management of 1d histograms
0040   std::map<std::string, TH1D*> histos1D_;
0041 };
0042 
0043 #include "TMath.h"
0044 
0045 #include "FWCore/ServiceRegistry/interface/Service.h"
0046 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0047 #include "PhysicsTools/PatUtils/interface/TriggerHelper.h"
0048 
0049 PatTriggerTagAndProbe::PatTriggerTagAndProbe(const edm::ParameterSet& iConfig)
0050     :  // pat::TriggerEvent
0051       triggerEventToken_(consumes<pat::TriggerEvent>(iConfig.getParameter<edm::InputTag>("triggerEvent"))),
0052       // muon input collection
0053       muonsToken_(consumes<pat::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0054       // muon match objects
0055       muonMatch_(iConfig.getParameter<std::string>("muonMatch")),
0056       // histogram management
0057       histos1D_() {
0058   usesResource(TFileService::kSharedResource);
0059 }
0060 
0061 PatTriggerTagAndProbe::~PatTriggerTagAndProbe() {}
0062 
0063 void PatTriggerTagAndProbe::beginJob() {
0064   edm::Service<TFileService> fileService;
0065 
0066   // mass plot around Z peak
0067   histos1D_["mass"] = fileService->make<TH1D>("mass", "Mass_{Z} (GeV)", 90, 30., 120.);
0068   // pt for test candidate
0069   histos1D_["testPt"] = fileService->make<TH1D>("testPt", "p_{T} (GeV)", 100, 0., 100.);
0070   // pt for probe candidate
0071   histos1D_["probePt"] = fileService->make<TH1D>("probePt", "p_{T} (GeV)", 100, 0., 100.);
0072   // eta for test candidate
0073   histos1D_["testEta"] = fileService->make<TH1D>("testEta", "#eta", 48, -2.4, 2.4);
0074   // eta for probe candidate
0075   histos1D_["probeEta"] = fileService->make<TH1D>("probeEta", "#eta", 48, -2.4, 2.4);
0076 }
0077 
0078 void PatTriggerTagAndProbe::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0079   // trigger event
0080   edm::Handle<pat::TriggerEvent> triggerEvent;
0081   iEvent.getByToken(triggerEventToken_, triggerEvent);
0082   // pat candidate collection
0083   edm::Handle<pat::MuonCollection> muons;
0084   iEvent.getByToken(muonsToken_, muons);
0085 
0086   // pat trigger helper to recieve for trigger
0087   // matching information
0088   const pat::helper::TriggerMatchHelper matchHelper;
0089 
0090   // ask for trigger accept of HLT_Mu9; otherwise we don't even start
0091   if (!(triggerEvent->path("HLT_IsoMu17_v5")->wasRun() && triggerEvent->path("HLT_IsoMu17_v5")->wasAccept())) {
0092     return;
0093   }
0094 
0095   // loop over muon references for the tag muon
0096   for (size_t idxTag = 0; idxTag < muons->size(); ++idxTag) {
0097     const pat::TriggerObjectRef trigRefTag(
0098         matchHelper.triggerMatchObject(muons, idxTag, muonMatch_, iEvent, *triggerEvent));
0099     if (trigRefTag.isAvailable()) {
0100       // loop over muon references for the probe/test muon
0101       for (size_t idxProbe = 0; idxProbe < muons->size() && idxProbe != idxTag; ++idxProbe) {
0102         histos1D_["mass"]->Fill((muons->at(idxTag).p4() + muons->at(idxProbe).p4()).mass());
0103         if (fabs((muons->at(idxTag).p4() + muons->at(idxProbe).p4()).mass() - 90) < 5) {
0104           const pat::TriggerObjectRef trigRefProbe(
0105               matchHelper.triggerMatchObject(muons, idxProbe, muonMatch_, iEvent, *triggerEvent));
0106           histos1D_["probePt"]->Fill(muons->at(idxProbe).pt());
0107           histos1D_["probeEta"]->Fill(muons->at(idxProbe).eta());
0108           if (trigRefProbe.isAvailable()) {
0109             histos1D_["testPt"]->Fill(muons->at(idxProbe).pt());
0110             histos1D_["testEta"]->Fill(muons->at(idxProbe).eta());
0111           }
0112         }
0113       }
0114     }
0115   }
0116 }
0117 
0118 void PatTriggerTagAndProbe::endJob() {
0119   // normalize the entries of the histograms
0120   histos1D_["testPt"]->Divide(histos1D_["probePt"]);
0121   setErrors(*histos1D_["testPt"], *histos1D_["probePt"]);
0122   histos1D_["testEta"]->Divide(histos1D_["probeEta"]);
0123   setErrors(*histos1D_["testEta"], *histos1D_["probeEta"]);
0124 }
0125 
0126 void PatTriggerTagAndProbe::setErrors(TH1D& h, const TH1D& ref) {
0127   for (int bin = 0; bin < h.GetNbinsX(); ++bin) {
0128     if (ref.GetBinContent(bin + 1) > 0) {
0129       h.SetBinError(bin + 1,
0130                     sqrt((h.GetBinContent(bin + 1) * (1. - h.GetBinContent(bin + 1))) / ref.GetBinContent(bin + 1)));
0131     } else {
0132       h.SetBinError(bin + 1, 0.);
0133     }
0134   }
0135 }
0136 
0137 #include "FWCore/Framework/interface/MakerMacros.h"
0138 DEFINE_FWK_MODULE(PatTriggerTagAndProbe);