Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:58:43

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 alCaGammaJetSelector {
0042   struct Counters {
0043     Counters() : nProcessed_(0), nSelected_(0) {}
0044     mutable std::atomic<unsigned int> nProcessed_, nSelected_;
0045   };
0046 }  // namespace alCaGammaJetSelector
0047 
0048 class AlCaGammaJetSelector : public edm::stream::EDFilter<edm::GlobalCache<alCaGammaJetSelector::Counters> > {
0049 public:
0050   explicit AlCaGammaJetSelector(const edm::ParameterSet&, const alCaGammaJetSelector::Counters* count);
0051   ~AlCaGammaJetSelector() override = default;
0052 
0053   static std::unique_ptr<alCaGammaJetSelector::Counters> initializeGlobalCache(edm::ParameterSet const&) {
0054     return std::make_unique<alCaGammaJetSelector::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 alCaGammaJetSelector::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,
0091                                            const alCaGammaJetSelector::Counters* counters) {
0092   nProcessed_ = 0;
0093   nSelected_ = 0;
0094 
0095   // get input
0096   labelPhoton_ = iConfig.getParameter<edm::InputTag>("PhoInput");
0097   labelPFJet_ = iConfig.getParameter<edm::InputTag>("PFjetInput");
0098   minPtJet_ = iConfig.getParameter<double>("MinPtJet");
0099   minPtPhoton_ = iConfig.getParameter<double>("MinPtPhoton");
0100 
0101   // Register consumption
0102   tok_Photon_ = consumes<reco::PhotonCollection>(labelPhoton_);
0103   tok_PFJet_ = consumes<reco::PFJetCollection>(labelPFJet_);
0104 }
0105 
0106 //
0107 // member functions
0108 //
0109 
0110 // ------------ method fills 'descriptions' with the allowed parameters for the module  ------------
0111 void AlCaGammaJetSelector::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0112   //The following says we do not know what parameters are allowed so do no validation
0113   // Please change this to state exactly what you do use, even if it is no parameters
0114   edm::ParameterSetDescription desc;
0115   desc.add<edm::InputTag>("PhoInput", edm::InputTag("gedPhotons"));
0116   desc.add<edm::InputTag>("PFjetInput", edm::InputTag("ak4PFJetsCHS"));
0117   desc.add<double>("MinPtJet", 10.0);
0118   desc.add<double>("MinPtPhoton", 10.0);
0119   descriptions.add("alcaGammaJetSelector", desc);
0120 }
0121 
0122 // ------------ method called on each new Event  ------------
0123 bool AlCaGammaJetSelector::filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0124   nProcessed_++;
0125 
0126   // Access the collections from iEvent
0127   auto const& phoHandle = iEvent.getHandle(tok_Photon_);
0128   if (!phoHandle.isValid()) {
0129     edm::LogWarning("AlCaGammaJet") << "AlCaGammaJetProducer: Error! can't get the product " << labelPhoton_;
0130     return false;  // do not filter
0131   }
0132   const reco::PhotonCollection photons = *(phoHandle.product());
0133 
0134   auto const& pfjetHandle = iEvent.getHandle(tok_PFJet_);
0135   if (!pfjetHandle.isValid()) {
0136     edm::LogWarning("AlCaGammaJet") << "AlCaGammaJetProducer: Error! can't get product " << labelPFJet_;
0137     return false;  // do not filter
0138   }
0139   const reco::PFJetCollection pfjets = *(pfjetHandle.product());
0140 
0141   // Check the conditions for a good event
0142   if (!(select(photons, pfjets)))
0143     return false;
0144 
0145   edm::LogVerbatim("AlCaGammaJet") << "good event\n";
0146   nSelected_++;
0147   return true;
0148 }
0149 
0150 void AlCaGammaJetSelector::endStream() {
0151   globalCache()->nProcessed_ += nProcessed_;
0152   globalCache()->nSelected_ += nSelected_;
0153 }
0154 
0155 // ------------ method called once each job just after ending the event loop  ------------
0156 
0157 void AlCaGammaJetSelector::globalEndJob(const alCaGammaJetSelector::Counters* count) {
0158   edm::LogWarning("AlCaGammaJet") << "Finds " << count->nSelected_ << " good events out of " << count->nProcessed_;
0159 }
0160 
0161 bool AlCaGammaJetSelector::select(const reco::PhotonCollection& photons, const reco::PFJetCollection& jets) {
0162   // Check the requirement for minimum pT
0163   if (photons.empty())
0164     return false;
0165   bool ok(false);
0166   for (reco::PFJetCollection::const_iterator itr = jets.begin(); itr != jets.end(); ++itr) {
0167     if (itr->pt() >= minPtJet_) {
0168       ok = true;
0169       break;
0170     }
0171   }
0172   if (!ok)
0173     return ok;
0174   for (reco::PhotonCollection::const_iterator itr = photons.begin(); itr != photons.end(); ++itr) {
0175     if (itr->pt() >= minPtPhoton_)
0176       return ok;
0177   }
0178   return false;
0179 }
0180 
0181 //define this as a plug-in
0182 DEFINE_FWK_MODULE(AlCaGammaJetSelector);