Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:48

0001 /**
0002   \class    pat::PATCleaner PATCleaner.h "PhysicsTools/PatAlgos/interface/PATCleaner.h"
0003   \brief    PAT Cleaner module for PAT Objects
0004 
0005             The same module is used for all collections.
0006 
0007   \author   Giovanni Petrucciani
0008   \version  $Id: PATCleaner.h,v 1.3 2010/10/20 23:08:30 wmtan Exp $
0009 */
0010 
0011 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
0012 #include "CommonTools/Utils/interface/StringObjectFunction.h"
0013 #include "DataFormats/PatCandidates/interface/Electron.h"
0014 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
0015 #include "DataFormats/PatCandidates/interface/Jet.h"
0016 #include "DataFormats/PatCandidates/interface/MET.h"
0017 #include "DataFormats/PatCandidates/interface/Muon.h"
0018 #include "DataFormats/PatCandidates/interface/PFParticle.h"
0019 #include "DataFormats/PatCandidates/interface/Photon.h"
0020 #include "DataFormats/PatCandidates/interface/Tau.h"
0021 #include "FWCore/Framework/interface/Event.h"
0022 #include "FWCore/Framework/interface/stream/EDProducer.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 #include "FWCore/Utilities/interface/InputTag.h"
0025 #include "PhysicsTools/PatAlgos/interface/OverlapTest.h"
0026 
0027 #include <memory>
0028 #include <vector>
0029 
0030 namespace pat {
0031 
0032   template <class PATObjType>
0033   class PATCleaner : public edm::stream::EDProducer<> {
0034   public:
0035     explicit PATCleaner(const edm::ParameterSet& iConfig);
0036     ~PATCleaner() override {}
0037 
0038     void produce(edm::Event& iEvent, const edm::EventSetup& iSetup) final;
0039 
0040   private:
0041     typedef StringCutObjectSelector<PATObjType> Selector;
0042 
0043     const edm::InputTag src_;
0044     const edm::EDGetTokenT<edm::View<PATObjType>> srcToken_;
0045     const Selector preselectionCut_;
0046     const Selector finalCut_;
0047 
0048     typedef pat::helper::OverlapTest OverlapTest;
0049     std::vector<std::unique_ptr<OverlapTest>> overlapTests_;
0050   };
0051 
0052 }  // namespace pat
0053 
0054 template <class PATObjType>
0055 pat::PATCleaner<PATObjType>::PATCleaner(const edm::ParameterSet& iConfig)
0056     : src_(iConfig.getParameter<edm::InputTag>("src")),
0057       srcToken_(consumes<edm::View<PATObjType>>(src_)),
0058       preselectionCut_(iConfig.getParameter<std::string>("preselection")),
0059       finalCut_(iConfig.getParameter<std::string>("finalCut")) {
0060   // pick parameter set for overlaps
0061   edm::ParameterSet overlapPSet = iConfig.getParameter<edm::ParameterSet>("checkOverlaps");
0062   // get all the names of the tests (all nested PSets in this PSet)
0063   std::vector<std::string> overlapNames = overlapPSet.getParameterNamesForType<edm::ParameterSet>();
0064   // loop on them
0065   for (std::vector<std::string>::const_iterator itn = overlapNames.begin(); itn != overlapNames.end(); ++itn) {
0066     // retrieve configuration
0067     edm::ParameterSet cfg = overlapPSet.getParameter<edm::ParameterSet>(*itn);
0068     // skip empty parameter sets
0069     if (cfg.empty())
0070       continue;
0071     // get the name of the algorithm to use
0072     std::string algorithm = cfg.getParameter<std::string>("algorithm");
0073     // create the appropriate OverlapTest
0074     if (algorithm == "byDeltaR") {
0075       overlapTests_.emplace_back(new pat::helper::BasicOverlapTest(*itn, cfg, consumesCollector()));
0076     } else if (algorithm == "bySuperClusterSeed") {
0077       overlapTests_.emplace_back(new pat::helper::OverlapBySuperClusterSeed(*itn, cfg, consumesCollector()));
0078     } else {
0079       throw cms::Exception("Configuration")
0080           << "PATCleaner for " << src_ << ": unsupported algorithm '" << algorithm << "'\n";
0081     }
0082   }
0083 
0084   produces<std::vector<PATObjType>>();
0085 }
0086 
0087 template <class PATObjType>
0088 void pat::PATCleaner<PATObjType>::produce(edm::Event& iEvent, const edm::EventSetup& iSetup) {
0089   // Read the input. We use edm::View<> in case the input happes to be something different than a std::vector<>
0090   edm::Handle<edm::View<PATObjType>> candidates;
0091   iEvent.getByToken(srcToken_, candidates);
0092 
0093   // Prepare a collection for the output
0094   auto output = std::make_unique<std::vector<PATObjType>>();
0095 
0096   // initialize the overlap tests
0097   for (auto& itov : overlapTests_) {
0098     itov->readInput(iEvent, iSetup);
0099   }
0100 
0101   for (typename edm::View<PATObjType>::const_iterator it = candidates->begin(), ed = candidates->end(); it != ed;
0102        ++it) {
0103     // Apply a preselection to the inputs and copy them in the output
0104     if (!preselectionCut_(*it))
0105       continue;
0106 
0107     // Add it to the list and take a reference to it, so it can be modified (e.g. to set the overlaps)
0108     // If at some point I'll decide to drop this item, I'll use pop_back to remove it
0109     output->push_back(*it);
0110     PATObjType& obj = output->back();
0111 
0112     // Look for overlaps
0113     bool badForOverlap = false;
0114     for (auto& itov : overlapTests_) {
0115       reco::CandidatePtrVector overlaps;
0116       bool hasOverlap = itov->fillOverlapsForItem(obj, overlaps);
0117       if (hasOverlap && itov->requireNoOverlaps()) {
0118         badForOverlap = true;  // mark for discarding
0119         break;                 // no point in checking the others, as this item will be discarded
0120       }
0121       obj.setOverlaps(itov->name(), overlaps);
0122     }
0123     if (badForOverlap) {
0124       output->pop_back();
0125       continue;
0126     }
0127 
0128     // Apply one final selection cut
0129     if (!finalCut_(obj))
0130       output->pop_back();
0131   }
0132 
0133   iEvent.put(std::move(output));
0134 }
0135 
0136 #include "FWCore/Framework/interface/MakerMacros.h"
0137 namespace pat {
0138   typedef pat::PATCleaner<pat::Electron> PATElectronCleaner;
0139   typedef pat::PATCleaner<pat::Muon> PATMuonCleaner;
0140   typedef pat::PATCleaner<pat::Tau> PATTauCleaner;
0141   typedef pat::PATCleaner<pat::Photon> PATPhotonCleaner;
0142   typedef pat::PATCleaner<pat::Jet> PATJetCleaner;
0143   typedef pat::PATCleaner<pat::MET> PATMETCleaner;
0144   typedef pat::PATCleaner<pat::GenericParticle> PATGenericParticleCleaner;
0145   typedef pat::PATCleaner<pat::PFParticle> PATPFParticleCleaner;
0146 }  // namespace pat
0147 using namespace pat;
0148 DEFINE_FWK_MODULE(PATElectronCleaner);
0149 DEFINE_FWK_MODULE(PATMuonCleaner);
0150 DEFINE_FWK_MODULE(PATTauCleaner);
0151 DEFINE_FWK_MODULE(PATPhotonCleaner);
0152 DEFINE_FWK_MODULE(PATJetCleaner);
0153 DEFINE_FWK_MODULE(PATMETCleaner);
0154 DEFINE_FWK_MODULE(PATGenericParticleCleaner);
0155 DEFINE_FWK_MODULE(PATPFParticleCleaner);