Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-10-04 02:55:05

0001 // -*- C++ -*-
0002 //
0003 // Package:    Calibration/HcalAlCaRecoProducers/AlCaGammaJetSelector
0004 // Class:      AlCaGammaJetSelector
0005 //
0006 /**\class AlCaGammaJetSelector AlCaGammaJetSelector.cc Calibration/HcalAlCaRecoProducers/AlCaGammaJetSelector/src/AlCaGammaJetSelector.cc
0007 
0008  Description: Enable filtering of good events based on the AlCaGammaJetProducer info
0009 
0010  Implementation:
0011      [Notes on implementation]
0012 */
0013 //
0014 // Original Author:  Andrius Juodagalvis
0015 //         Created:  Fri, 15 Aug 2015 12:09:55 GMT
0016 //
0017 //
0018 
0019 // system include files
0020 #include <memory>
0021 #include <iostream>
0022 
0023 // user include files
0024 #include "FWCore/Framework/interface/Frameworkfwd.h"
0025 #include "FWCore/Framework/interface/stream/EDFilter.h"
0026 #include "FWCore/Framework/interface/Event.h"
0027 #include "FWCore/Framework/interface/MakerMacros.h"
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0030 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0031 
0032 #include "DataFormats/EgammaCandidates/interface/Photon.h"
0033 #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
0034 #include "DataFormats/JetReco/interface/PFJetCollection.h"
0035 //#include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
0036 
0037 //
0038 // class declaration
0039 //
0040 
0041 namespace AlCaGammaJet {
0042   struct Counters {
0043     Counters() : nProcessed_(0), nSelected_(0) {}
0044     mutable std::atomic<unsigned int> nProcessed_, nSelected_;
0045   };
0046 }  // namespace AlCaGammaJet
0047 
0048 class AlCaGammaJetSelector : public edm::stream::EDFilter<edm::GlobalCache<AlCaGammaJet::Counters> > {
0049 public:
0050   explicit AlCaGammaJetSelector(const edm::ParameterSet&, const AlCaGammaJet::Counters* count);
0051   ~AlCaGammaJetSelector() override = default;
0052 
0053   static std::unique_ptr<AlCaGammaJet::Counters> initializeGlobalCache(edm::ParameterSet const&) {
0054     return std::make_unique<AlCaGammaJet::Counters>();
0055   }
0056 
0057   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0058   bool filter(edm::Event&, const edm::EventSetup&) override;
0059   void endStream() override;
0060   static void globalEndJob(const AlCaGammaJet::Counters* counters);
0061 
0062 private:
0063   void beginRun(edm::Run const&, edm::EventSetup const&) override {}
0064   void endRun(edm::Run const&, edm::EventSetup const&) override {}
0065   void beginLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override {}
0066   void endLuminosityBlock(edm::LuminosityBlock const&, edm::EventSetup const&) override {}
0067   bool select(const reco::PhotonCollection&, const reco::PFJetCollection&);
0068 
0069   // ----------member data ---------------------------
0070 
0071   unsigned int nProcessed_, nSelected_;
0072 
0073   edm::InputTag labelPhoton_, labelPFJet_;
0074   double minPtJet_, minPtPhoton_;
0075   edm::EDGetTokenT<reco::PhotonCollection> tok_Photon_;
0076   edm::EDGetTokenT<reco::PFJetCollection> tok_PFJet_;
0077 };
0078 
0079 //
0080 // constants, enums and typedefs
0081 //
0082 
0083 //
0084 // static data member definitions
0085 //
0086 
0087 //
0088 // constructors and destructor
0089 //
0090 AlCaGammaJetSelector::AlCaGammaJetSelector(const edm::ParameterSet& iConfig, const AlCaGammaJet::Counters* counters) {
0091   nProcessed_ = 0;
0092   nSelected_ = 0;
0093 
0094   // get input
0095   labelPhoton_ = iConfig.getParameter<edm::InputTag>("PhoInput");
0096   labelPFJet_ = iConfig.getParameter<edm::InputTag>("PFjetInput");
0097   minPtJet_ = iConfig.getParameter<double>("MinPtJet");
0098   minPtPhoton_ = iConfig.getParameter<double>("MinPtPhoton");
0099 
0100   // Register consumption
0101   tok_Photon_ = consumes<reco::PhotonCollection>(labelPhoton_);
0102   tok_PFJet_ = consumes<reco::PFJetCollection>(labelPFJet_);
0103 }
0104 
0105 //
0106 // member functions
0107 //
0108 
0109 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0110 void AlCaGammaJetSelector::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0111   //The following says we do not know what parameters are allowed so do no validation
0112   // Please change this to state exactly what you do use, even if it is no parameters
0113   edm::ParameterSetDescription desc;
0114   desc.add<edm::InputTag>("PhoInput", edm::InputTag("gedPhotons"));
0115   desc.add<edm::InputTag>("PFjetInput", edm::InputTag("ak4PFJetsCHS"));
0116   desc.add<double>("MinPtJet", 10.0);
0117   desc.add<double>("MinPtPhoton", 10.0);
0118   descriptions.add("alcaGammaJetSelector", desc);
0119 }
0120 
0121 // ------------ method called on each new Event  ------------
0122 bool AlCaGammaJetSelector::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0123   nProcessed_++;
0124 
0125   // Access the collections from iEvent
0126   auto const& phoHandle = iEvent.getHandle(tok_Photon_);
0127   if (!phoHandle.isValid()) {
0128     edm::LogWarning("AlCaGammaJet") << "AlCaGammaJetProducer: Error! can't get the product " << labelPhoton_;
0129     return false;  // do not filter
0130   }
0131   const reco::PhotonCollection photons = *(phoHandle.product());
0132 
0133   auto const& pfjetHandle = iEvent.getHandle(tok_PFJet_);
0134   if (!pfjetHandle.isValid()) {
0135     edm::LogWarning("AlCaGammaJet") << "AlCaGammaJetProducer: Error! can't get product " << labelPFJet_;
0136     return false;  // do not filter
0137   }
0138   const reco::PFJetCollection pfjets = *(pfjetHandle.product());
0139 
0140   // Check the conditions for a good event
0141   if (!(select(photons, pfjets)))
0142     return false;
0143 
0144   edm::LogVerbatim("AlCaGammaJet") << "good event\n";
0145   nSelected_++;
0146   return true;
0147 }
0148 
0149 void AlCaGammaJetSelector::endStream() {
0150   globalCache()->nProcessed_ += nProcessed_;
0151   globalCache()->nSelected_ += nSelected_;
0152 }
0153 
0154 // ------------ method called once each job just after ending the event loop  ------------
0155 
0156 void AlCaGammaJetSelector::globalEndJob(const AlCaGammaJet::Counters* count) {
0157   edm::LogWarning("AlCaGammaJet") << "Finds " << count->nSelected_ << " good events out of " << count->nProcessed_;
0158 }
0159 
0160 bool AlCaGammaJetSelector::select(const reco::PhotonCollection& photons, const reco::PFJetCollection& jets) {
0161   // Check the requirement for minimum pT
0162   if (photons.empty())
0163     return false;
0164   bool ok(false);
0165   for (reco::PFJetCollection::const_iterator itr = jets.begin(); itr != jets.end(); ++itr) {
0166     if (itr->pt() >= minPtJet_) {
0167       ok = true;
0168       break;
0169     }
0170   }
0171   if (!ok)
0172     return ok;
0173   for (reco::PhotonCollection::const_iterator itr = photons.begin(); itr != photons.end(); ++itr) {
0174     if (itr->pt() >= minPtPhoton_)
0175       return ok;
0176   }
0177   return false;
0178 }
0179 
0180 //define this as a plug-in
0181 DEFINE_FWK_MODULE(AlCaGammaJetSelector);