Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:55:41

0001 // -*- C++ -*-
0002 //
0003 // Package:    L1Trigger/L1TNtuples
0004 // Class:      L1MetFilterRecoTreeProducer
0005 //
0006 /**\class L1MetFilterRecoTreeProducer L1MetFilterRecoTreeProducer.cc L1Trigger/L1TNtuples/src/L1MetFilterRecoTreeProducer.cc
0007 
0008  Description: Produces tree containing reco quantities
0009 
0010 
0011 */
0012 
0013 // system include files
0014 #include <memory>
0015 
0016 // framework
0017 #include "FWCore/Framework/interface/Frameworkfwd.h"
0018 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0019 #include "FWCore/Framework/interface/Event.h"
0020 #include "FWCore/Framework/interface/EventSetup.h"
0021 #include "FWCore/Framework/interface/MakerMacros.h"
0022 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0023 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0024 #include "FWCore/Framework/interface/ESHandle.h"
0025 #include "FWCore/Common/interface/TriggerNames.h"
0026 
0027 // cond formats
0028 
0029 // data formats
0030 #include "DataFormats/Common/interface/TriggerResults.h"
0031 #include "DataFormats/Provenance/interface/ParameterSetID.h"
0032 
0033 // ROOT output stuff
0034 #include "FWCore/ServiceRegistry/interface/Service.h"
0035 #include "CommonTools/UtilAlgos/interface/TFileService.h"
0036 #include "TH1.h"
0037 #include "TTree.h"
0038 #include "TF1.h"
0039 #include <TVector2.h>
0040 
0041 //local  data formats
0042 #include "L1Trigger/L1TNtuples/interface/L1AnalysisRecoMetFilterDataFormat.h"
0043 
0044 //
0045 // class declaration
0046 //
0047 
0048 class L1MetFilterRecoTreeProducer : public edm::one::EDAnalyzer<edm::one::SharedResources> {
0049 public:
0050   explicit L1MetFilterRecoTreeProducer(const edm::ParameterSet&);
0051   ~L1MetFilterRecoTreeProducer() override;
0052 
0053 private:
0054   void beginJob(void) override;
0055   void analyze(const edm::Event&, const edm::EventSetup&) override;
0056   void endJob() override;
0057 
0058   void doMetFilters(edm::Handle<edm::TriggerResults> trigRes, edm::TriggerNames trigNames);
0059 
0060 public:
0061   L1Analysis::L1AnalysisRecoMetFilterDataFormat* metFilter_data;
0062 
0063 private:
0064   // output file
0065   edm::Service<TFileService> fs_;
0066 
0067   // tree
0068   TTree* tree_;
0069 
0070   // EDM input tags
0071   edm::EDGetTokenT<edm::TriggerResults> triggerResultsToken_;
0072   //edm::EDGetTokenT<bool>                    hbheNoiseFilterResultToken_;
0073   edm::EDGetTokenT<bool> badChCandFilterToken_;
0074   edm::EDGetTokenT<bool> badPFMuonFilterToken_;
0075 
0076   // debug stuff
0077   bool triggerResultsMissing_;
0078   //bool hbheNoiseFilterResultMissing_;
0079   bool badPFMuonFilterResultMissing_;
0080   bool badChCandFilterResultMissing_;
0081   //bool hbheNFRes = false;
0082   bool badPFMuonRes = false;
0083   bool badChCandRes = false;
0084 };
0085 
0086 L1MetFilterRecoTreeProducer::L1MetFilterRecoTreeProducer(const edm::ParameterSet& iConfig)
0087     : triggerResultsMissing_(false)
0088 //hbheNoiseFilterResultMissing_(false)
0089 {
0090   usesResource(TFileService::kSharedResource);
0091 
0092   triggerResultsToken_ = consumes<edm::TriggerResults>(
0093       iConfig.getUntrackedParameter("triggerResultsToken", edm::InputTag("TriggerResults")));
0094 
0095   //hbheNoiseFilterResultToken_ = consumes<bool>(iConfig.getUntrackedParameter("hbheNoiseFilterResultToken",edm::InputTag("HBHENoiseFilterResultProducer:HBHENoiseFilterResult")));
0096 
0097   badChCandFilterToken_ =
0098       consumes<bool>(iConfig.getUntrackedParameter<edm::InputTag>("badChargedCandidateFilterToken"));
0099 
0100   badPFMuonFilterToken_ = consumes<bool>(iConfig.getUntrackedParameter<edm::InputTag>("badPFMuonFilterToken"));
0101 
0102   metFilter_data = new L1Analysis::L1AnalysisRecoMetFilterDataFormat();
0103 
0104   // set up output
0105   tree_ = fs_->make<TTree>("MetFilterRecoTree", "MetFilterRecoTree");
0106   tree_->Branch("MetFilters", "L1Analysis::L1AnalysisRecoMetFilterDataFormat", &metFilter_data, 32000, 3);
0107 }
0108 
0109 L1MetFilterRecoTreeProducer::~L1MetFilterRecoTreeProducer() {
0110   // do anything here that needs to be done at desctruction time
0111   // (e.g. close files, deallocate resources etc.)
0112 }
0113 
0114 //
0115 // member functions
0116 //
0117 
0118 // ------------ method called to for each event  ------------
0119 void L1MetFilterRecoTreeProducer::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0120   metFilter_data->Reset();
0121 
0122   //hbheNFRes = false;
0123   badPFMuonRes = false;
0124   badChCandRes = false;
0125 
0126   // get trigger results
0127   edm::Handle<edm::TriggerResults> trigRes;
0128   iEvent.getByToken(triggerResultsToken_, trigRes);
0129 
0130   if (trigRes.isValid()) {
0131     // get trigger names
0132     const edm::TriggerNames& trigNames = iEvent.triggerNames(*trigRes);
0133     doMetFilters(trigRes, trigNames);
0134   } else {
0135     if (!triggerResultsMissing_) {
0136       edm::LogWarning("MissingProduct") << "Trigger results not found.  Some MET filters will not be filled"
0137                                         << std::endl;
0138     }
0139     triggerResultsMissing_ = true;
0140   }
0141 
0142   // // get hbhe noise filter result
0143   // edm::Handle<bool> hbheNoiseFilterResult;
0144   // iEvent.getByToken(hbheNoiseFilterResultToken_, hbheNoiseFilterResult);
0145 
0146   // if(hbheNoiseFilterResult.isValid()){
0147   //   hbheNFRes = *hbheNoiseFilterResult;
0148   // }
0149   // else {
0150   //   if(!hbheNoiseFilterResultMissing_){edm::LogWarning("MissingProduct") << "HBHE Noise Filter Result not found.  Branch will not be filled" << std::endl;}
0151   //   hbheNoiseFilterResultMissing_=true;
0152   // }
0153 
0154   edm::Handle<bool> badChCandFilterResult;
0155   iEvent.getByToken(badChCandFilterToken_, badChCandFilterResult);
0156 
0157   if (badChCandFilterResult.isValid()) {
0158     badChCandRes = *badChCandFilterResult;
0159   } else {
0160     if (!badChCandFilterResultMissing_) {
0161       edm::LogWarning("MissingProduct")
0162           << "Bad Charged Hadron Candidate Filter Result not found.  Branch will not be filled" << std::endl;
0163     }
0164     badChCandFilterResultMissing_ = true;
0165   }
0166 
0167   edm::Handle<bool> badPFMuonFilterResult;
0168   iEvent.getByToken(badPFMuonFilterToken_, badPFMuonFilterResult);
0169 
0170   if (badPFMuonFilterResult.isValid()) {
0171     badPFMuonRes = *badPFMuonFilterResult;
0172   } else {
0173     if (!badPFMuonFilterResultMissing_) {
0174       edm::LogWarning("MissingProduct") << "Bad PF Muon Filter Result not found.  Branch will not be filled"
0175                                         << std::endl;
0176     }
0177     badPFMuonFilterResultMissing_ = true;
0178   }
0179 
0180   //metFilter_data->hbheNoiseFilter                 = hbheNFRes;
0181   metFilter_data->badChCandFilter = badChCandRes;
0182   metFilter_data->badPFMuonFilter = badPFMuonRes;
0183 
0184   tree_->Fill();
0185 }
0186 
0187 void L1MetFilterRecoTreeProducer::doMetFilters(edm::Handle<edm::TriggerResults> trigRes, edm::TriggerNames trigNames) {
0188   //get array size
0189   unsigned int numTrigs = trigNames.triggerNames().size();
0190 
0191   //get indices of flags from event parameter set
0192   unsigned int hbheNoiseFilterIndex = trigNames.triggerIndex("Flag_HBHENoiseFilter");
0193   unsigned int hbheNoiseIsoFilterIndex = trigNames.triggerIndex("Flag_HBHENoiseIsoFilter");
0194   unsigned int cscTightHalo2015FilterIndex = trigNames.triggerIndex("Flag_CSCTightHalo2015Filter");
0195   unsigned int globalSuperTightHalo2016FilterIndex = trigNames.triggerIndex("Flag_globalSuperTightHalo2016Filter");
0196   unsigned int ecalDeadCellTPFilterIndex = trigNames.triggerIndex("Flag_EcalDeadCellTriggerPrimitiveFilter");
0197   unsigned int goodVerticesFilterIndex = trigNames.triggerIndex("Flag_goodVertices");
0198   unsigned int eeBadScFilterIndex = trigNames.triggerIndex("Flag_eeBadScFilter");
0199   unsigned int chHadTrackResFilterIndex = trigNames.triggerIndex("Flag_chargedHadronTrackResolutionFilter");
0200   unsigned int muonBadTrackFilterIndex = trigNames.triggerIndex("Flag_muonBadTrackFilter");
0201 
0202   //set flag
0203   metFilter_data->hbheNoiseFilter = hbheNoiseFilterIndex < numTrigs ? trigRes->accept(hbheNoiseFilterIndex) : false;
0204   metFilter_data->hbheNoiseIsoFilter =
0205       hbheNoiseIsoFilterIndex < numTrigs ? trigRes->accept(hbheNoiseIsoFilterIndex) : false;
0206   metFilter_data->cscTightHalo2015Filter =
0207       cscTightHalo2015FilterIndex < numTrigs ? trigRes->accept(cscTightHalo2015FilterIndex) : false;
0208   metFilter_data->globalSuperTightHalo2016Filter =
0209       globalSuperTightHalo2016FilterIndex < numTrigs ? trigRes->accept(globalSuperTightHalo2016FilterIndex) : false;
0210   metFilter_data->ecalDeadCellTPFilter =
0211       ecalDeadCellTPFilterIndex < numTrigs ? trigRes->accept(ecalDeadCellTPFilterIndex) : false;
0212   metFilter_data->goodVerticesFilter =
0213       goodVerticesFilterIndex < numTrigs ? trigRes->accept(goodVerticesFilterIndex) : false;
0214   metFilter_data->eeBadScFilter = eeBadScFilterIndex < numTrigs ? trigRes->accept(eeBadScFilterIndex) : false;
0215   metFilter_data->chHadTrackResFilter =
0216       chHadTrackResFilterIndex < numTrigs ? trigRes->accept(chHadTrackResFilterIndex) : false;
0217   metFilter_data->muonBadTrackFilter =
0218       muonBadTrackFilterIndex < numTrigs ? trigRes->accept(muonBadTrackFilterIndex) : false;
0219 }
0220 
0221 // ------------ method called once each job just before starting event loop  ------------
0222 void L1MetFilterRecoTreeProducer::beginJob(void) {}
0223 
0224 // ------------ method called once each job just after ending the event loop  ------------
0225 void L1MetFilterRecoTreeProducer::endJob() {}
0226 
0227 //define this as a plug-in
0228 DEFINE_FWK_MODULE(L1MetFilterRecoTreeProducer);