File indexing completed on 2024-04-06 12:24:01
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
0018 explicit PatTriggerTagAndProbe(const edm::ParameterSet& iConfig);
0019
0020 ~PatTriggerTagAndProbe() override;
0021
0022 private:
0023
0024 void beginJob() override;
0025
0026 void analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) override;
0027
0028 void endJob() override;
0029
0030
0031 void setErrors(TH1D& h, const TH1D& ref);
0032
0033
0034 edm::EDGetTokenT<pat::TriggerEvent> triggerEventToken_;
0035
0036 edm::EDGetTokenT<pat::MuonCollection> muonsToken_;
0037
0038 std::string muonMatch_;
0039
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 :
0051 triggerEventToken_(consumes<pat::TriggerEvent>(iConfig.getParameter<edm::InputTag>("triggerEvent"))),
0052
0053 muonsToken_(consumes<pat::MuonCollection>(iConfig.getParameter<edm::InputTag>("muons"))),
0054
0055 muonMatch_(iConfig.getParameter<std::string>("muonMatch")),
0056
0057 histos1D_() {
0058 usesResource(TFileService::kSharedResource);
0059 }
0060
0061 PatTriggerTagAndProbe::~PatTriggerTagAndProbe() {}
0062
0063 void PatTriggerTagAndProbe::beginJob() {
0064 edm::Service<TFileService> fileService;
0065
0066
0067 histos1D_["mass"] = fileService->make<TH1D>("mass", "Mass_{Z} (GeV)", 90, 30., 120.);
0068
0069 histos1D_["testPt"] = fileService->make<TH1D>("testPt", "p_{T} (GeV)", 100, 0., 100.);
0070
0071 histos1D_["probePt"] = fileService->make<TH1D>("probePt", "p_{T} (GeV)", 100, 0., 100.);
0072
0073 histos1D_["testEta"] = fileService->make<TH1D>("testEta", "#eta", 48, -2.4, 2.4);
0074
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
0080 edm::Handle<pat::TriggerEvent> triggerEvent;
0081 iEvent.getByToken(triggerEventToken_, triggerEvent);
0082
0083 edm::Handle<pat::MuonCollection> muons;
0084 iEvent.getByToken(muonsToken_, muons);
0085
0086
0087
0088 const pat::helper::TriggerMatchHelper matchHelper;
0089
0090
0091 if (!(triggerEvent->path("HLT_IsoMu17_v5")->wasRun() && triggerEvent->path("HLT_IsoMu17_v5")->wasAccept())) {
0092 return;
0093 }
0094
0095
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
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
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);