CMS 3D CMS Logo

PATCleaner.cc

Go to the documentation of this file.
00001 //
00002 // $Id: PATCleaner.cc,v 1.1.2.1 2009/01/14 17:27:56 gpetrucc Exp $
00003 //
00004 
00016 #include "FWCore/Framework/interface/EDProducer.h"
00017 #include "FWCore/Framework/interface/Event.h"
00018 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00019 #include "FWCore/ParameterSet/interface/InputTag.h"
00020 
00021 #include "PhysicsTools/Utilities/interface/StringObjectFunction.h"
00022 #include "PhysicsTools/Utilities/interface/StringCutObjectSelector.h"
00023 
00024 #include "DataFormats/PatCandidates/interface/Electron.h"
00025 #include "DataFormats/PatCandidates/interface/Muon.h"
00026 #include "DataFormats/PatCandidates/interface/Tau.h"
00027 #include "DataFormats/PatCandidates/interface/Photon.h"
00028 #include "DataFormats/PatCandidates/interface/Jet.h"
00029 #include "DataFormats/PatCandidates/interface/MET.h"
00030 #include "DataFormats/PatCandidates/interface/GenericParticle.h"
00031 #include "DataFormats/PatCandidates/interface/PFParticle.h"
00032 
00033 #include "PhysicsTools/PatAlgos/interface/OverlapTest.h"
00034 #include <boost/ptr_container/ptr_vector.hpp>
00035 
00036 namespace pat {
00037 
00038   template<class PATObjType>
00039   class PATCleaner : public edm::EDProducer {
00040     public:
00041       explicit PATCleaner(const edm::ParameterSet & iConfig);
00042       virtual ~PATCleaner() {}
00043 
00044       virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup);
00045 
00046     private:
00047       typedef StringCutObjectSelector<PATObjType> Selector;
00048 
00049       edm::InputTag src_;
00050       bool doPreselection_, doFinalCut_;  
00051       Selector preselectionCut_;
00052       Selector finalCut_;
00053 
00054       typedef pat::helper::OverlapTest OverlapTest;
00055       // make a list of overlap tests (ptr_vector instead of std::vector because they're polymorphic)
00056       boost::ptr_vector<OverlapTest> overlapTests_;
00057   };
00058 
00059 } // namespace
00060 
00061 template <class PATObjType>
00062 pat::PATCleaner<PATObjType>::PATCleaner(const edm::ParameterSet & iConfig) :
00063     src_(iConfig.getParameter<edm::InputTag>("src")),
00064     preselectionCut_(iConfig.getParameter<std::string>("preselection")),
00065     finalCut_(iConfig.getParameter<std::string>("finalCut"))
00066 {
00067     // pick parameter set for overlaps
00068     edm::ParameterSet overlapPSet = iConfig.getParameter<edm::ParameterSet>("checkOverlaps");
00069     // get all the names of the tests (all nested PSets in this PSet)
00070     std::vector<std::string> overlapNames = overlapPSet.getParameterNamesForType<edm::ParameterSet>();
00071     // loop on them
00072     for (std::vector<std::string>::const_iterator itn = overlapNames.begin(); itn != overlapNames.end(); ++itn) {
00073         // retrieve configuration
00074         edm::ParameterSet cfg = overlapPSet.getParameter<edm::ParameterSet>(*itn);
00075         // skip empty parameter sets
00076         if (cfg.empty()) continue; 
00077         // get the name of the algorithm to use
00078         std::string algorithm = cfg.getParameter<std::string>("algorithm");
00079         // create the appropriate OverlapTest
00080         if (algorithm == "byDeltaR") {
00081             overlapTests_.push_back(new pat::helper::BasicOverlapTest(*itn, cfg));
00082         } else if (algorithm == "bySuperClusterSeed") {
00083             overlapTests_.push_back(new pat::helper::OverlapBySuperClusterSeed(*itn, cfg));
00084         } else {
00085             throw cms::Exception("Configuration") << "PATCleaner for " << src_ << ": unsupported algorithm '" << algorithm << "'\n";
00086         }
00087     }
00088         
00089 
00090     produces<std::vector<PATObjType> >();
00091 }
00092 
00093 template <class PATObjType>
00094 void 
00095 pat::PATCleaner<PATObjType>::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
00096   using namespace edm;
00097 
00098   // Read the input. We use View<> in case the input happes to be something different than a std::vector<>
00099   Handle<View<PATObjType> > candidates;
00100   iEvent.getByLabel(src_, candidates);
00101 
00102   // Prepare a collection for the output
00103   std::auto_ptr< std::vector<PATObjType> > output(new std::vector<PATObjType>());
00104 
00105   // initialize the overlap tests
00106   for (boost::ptr_vector<OverlapTest>::iterator itov = overlapTests_.begin(), edov = overlapTests_.end(); itov != edov; ++itov) {
00107     itov->readInput(iEvent,iSetup);
00108   }
00109 
00110   for (typename View<PATObjType>::const_iterator it = candidates->begin(), ed = candidates->end(); it != ed; ++it) {
00111       // Apply a preselection to the inputs and copy them in the output
00112       if (!preselectionCut_(*it)) continue; 
00113 
00114       // Add it to the list and take a reference to it, so it can be modified (e.g. to set the overlaps)
00115       // If at some point I'll decide to drop this item, I'll use pop_back to remove it
00116       output->push_back(*it);
00117       PATObjType &obj = output->back();
00118 
00119       // Look for overlaps
00120       bool badForOverlap = false;
00121       for (boost::ptr_vector<OverlapTest>::iterator itov = overlapTests_.begin(), edov = overlapTests_.end(); itov != edov; ++itov) {
00122         reco::CandidatePtrVector overlaps;
00123         bool hasOverlap = itov->fillOverlapsForItem(obj, overlaps);
00124         if (hasOverlap && itov->requireNoOvelaps()) { 
00125             badForOverlap = true; // mark for discarding
00126             break; // no point in checking the others, as this item will be discarded
00127         }
00128         obj.setOverlaps(itov->name(), overlaps);
00129       }
00130       if (badForOverlap) { output->pop_back(); continue; }
00131 
00132       // Apply one final selection cut
00133       if (!finalCut_(obj)) output->pop_back();
00134   }
00135 
00136   iEvent.put(output);
00137 }
00138 
00139 
00140 #include "FWCore/Framework/interface/MakerMacros.h"
00141 namespace pat {
00142     typedef pat::PATCleaner<pat::Electron>   PATElectronCleaner;
00143     typedef pat::PATCleaner<pat::Muon>       PATMuonCleaner;
00144     typedef pat::PATCleaner<pat::Tau>        PATTauCleaner;
00145     typedef pat::PATCleaner<pat::Photon>     PATPhotonCleaner;
00146     typedef pat::PATCleaner<pat::Jet>        PATJetCleaner;
00147     typedef pat::PATCleaner<pat::MET>        PATMETCleaner;
00148     typedef pat::PATCleaner<pat::GenericParticle> PATGenericParticleCleaner;
00149     //typedef pat::PATCleaner<pat::PFParticle> PATPFParticleCleaner; // I don't think the PF folks need/want this
00150                                                                      // but technically it can work
00151 }
00152 using namespace pat;
00153 DEFINE_FWK_MODULE(PATElectronCleaner);
00154 DEFINE_FWK_MODULE(PATMuonCleaner);
00155 DEFINE_FWK_MODULE(PATTauCleaner);
00156 DEFINE_FWK_MODULE(PATPhotonCleaner);
00157 DEFINE_FWK_MODULE(PATJetCleaner);
00158 DEFINE_FWK_MODULE(PATMETCleaner);
00159 DEFINE_FWK_MODULE(PATGenericParticleCleaner);
00160 //DEFINE_FWK_MODULE(PATPFParticleCleaner);

Generated on Tue Jun 9 17:41:38 2009 for CMSSW by  doxygen 1.5.4