CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/PhysicsTools/TagAndProbe/plugins/ObjectViewCleaner.cc

Go to the documentation of this file.
00001 
00002 /*****************************************************************************
00003  * Project: CMS detector at the CERN
00004  *
00005  * Package: PhysicsTools/TagAndProbe
00006  *
00007  *
00008  * Authors:
00009  *
00010  *   Kalanand Mishra, Fermilab - kalanand@fnal.gov
00011  *
00012  * Description:
00013  *   - Cleans a given object collection of other 
00014  *     cross-object candidates using deltaR-matching.
00015  *   - For example: can clean a muon collection by 
00016  *      removing all jets in the muon collection.
00017  *   - Saves collection of the reference vectors of cleaned objects.
00018  * History:
00019  *   Generalized the existing CandViewCleaner
00020  *
00021  * Copyright (C) 2010 FNAL 
00022  *****************************************************************************/
00024 // Includes
00026 #include "FWCore/Framework/interface/EDProducer.h"
00027 #include "FWCore/Framework/interface/Event.h"
00028 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00029 #include "FWCore/Utilities/interface/InputTag.h"
00030 
00031 #include "DataFormats/Common/interface/View.h"
00032 #include "DataFormats/Math/interface/deltaR.h"
00033 
00034 #include "DataFormats/Candidate/interface/Candidate.h"
00035 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00036 #include "DataFormats/MuonReco/interface/Muon.h"
00037 #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
00038 #include "DataFormats/EgammaCandidates/interface/Electron.h"
00039 #include "DataFormats/JetReco/interface/Jet.h"
00040 #include "DataFormats/EgammaCandidates/interface/Photon.h"
00041 
00042 
00043 #include "CommonTools/Utils/interface/StringCutObjectSelector.h"
00044 
00045 #include <memory>
00046 #include <vector>
00047 #include <sstream>
00048 
00049 
00051 // class definition
00053 template<typename T>
00054 class ObjectViewCleaner : public edm::EDProducer
00055 {
00056 public:
00057   // construction/destruction
00058   ObjectViewCleaner(const edm::ParameterSet& iConfig);
00059   virtual ~ObjectViewCleaner();
00060   
00061   // member functions
00062   void produce(edm::Event& iEvent,const edm::EventSetup& iSetup);
00063   void endJob();
00064 
00065 private:  
00066   // member data
00067   edm::InputTag              srcCands_;
00068   std::vector<edm::InputTag> srcObjects_;
00069   double                     deltaRMin_;
00070   
00071   std::string  moduleLabel_;
00072   StringCutObjectSelector<T,true> objKeepCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
00073   StringCutObjectSelector<reco::Candidate,true> objRemoveCut_; // lazy parsing, to allow cutting on variables 
00074 
00075   unsigned int nObjectsTot_;
00076   unsigned int nObjectsClean_;
00077 };
00078 
00079 
00080 using namespace std;
00081 
00082 
00084 // construction/destruction
00086 
00087 //______________________________________________________________________________
00088 template<typename T>
00089 ObjectViewCleaner<T>::ObjectViewCleaner(const edm::ParameterSet& iConfig)
00090   : srcCands_    (iConfig.getParameter<edm::InputTag>         ("srcObject"))
00091   , srcObjects_ (iConfig.getParameter<vector<edm::InputTag> >("srcObjectsToRemove"))
00092   , deltaRMin_  (iConfig.getParameter<double>                ("deltaRMin"))
00093   , moduleLabel_(iConfig.getParameter<string>                ("@module_label"))
00094   , objKeepCut_(iConfig.existsAs<std::string>("srcObjectSelection") ? iConfig.getParameter<std::string>("srcObjectSelection") : "", true)
00095   ,objRemoveCut_(iConfig.existsAs<std::string>("srcObjectsToRemoveSelection") ? iConfig.getParameter<std::string>("srcObjectsToRemoveSelection") : "", true)
00096   , nObjectsTot_(0)
00097   , nObjectsClean_(0)
00098 {
00099   produces<edm::RefToBaseVector<T> >();
00100 }
00101 
00102 
00103 //______________________________________________________________________________
00104 template<typename T>
00105 ObjectViewCleaner<T>::~ObjectViewCleaner()
00106 {
00107   
00108 }
00109 
00110 
00111 
00113 // implementation of member functions
00115 
00116 //______________________________________________________________________________
00117 template<typename T>
00118 void ObjectViewCleaner<T>::produce(edm::Event& iEvent,const edm::EventSetup& iSetup)
00119 {
00120   auto_ptr<edm::RefToBaseVector<T> >
00121     cleanObjects(new edm::RefToBaseVector<T >());
00122 
00123   edm::Handle<edm::View<T> > candidates;
00124   iEvent.getByLabel(srcCands_,candidates);
00125   
00126   bool* isClean = new bool[candidates->size()];
00127   for (unsigned int iObject=0;iObject<candidates->size();iObject++) isClean[iObject] = true;
00128   
00129   for (unsigned int iSrc=0;iSrc<srcObjects_.size();iSrc++) {
00130     edm::Handle<edm::View<reco::Candidate> > objects;
00131     iEvent.getByLabel(srcObjects_[iSrc],objects);
00132     
00133     for (unsigned int iObject=0;iObject<candidates->size();iObject++) {
00134       const T& candidate = candidates->at(iObject);
00135       if (!objKeepCut_(candidate)) isClean[iObject] = false;
00136 
00137       for (unsigned int iObj=0;iObj<objects->size();iObj++) {
00138         const reco::Candidate& obj = objects->at(iObj);
00139         if (!objRemoveCut_(obj)) continue;
00140 
00141         double deltaR = reco::deltaR(candidate,obj);
00142         if (deltaR<deltaRMin_)  isClean[iObject] = false;
00143       }
00144     }
00145   }
00146   
00147   for (unsigned int iObject=0;iObject<candidates->size();iObject++)
00148     if (isClean[iObject]) cleanObjects->push_back(candidates->refAt(iObject));
00149   
00150   nObjectsTot_  +=candidates->size();
00151   nObjectsClean_+=cleanObjects->size();
00152 
00153   delete [] isClean;  
00154   iEvent.put(cleanObjects);
00155 }
00156 
00157 
00158 //______________________________________________________________________________
00159 template<typename T>
00160 void ObjectViewCleaner<T>::endJob()
00161 {
00162   stringstream ss;
00163   ss<<"nObjectsTot="<<nObjectsTot_<<" nObjectsClean="<<nObjectsClean_
00164     <<" fObjectsClean="<<100.*(nObjectsClean_/(double)nObjectsTot_)<<"%\n";
00165   cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00166       <<"\n"<<moduleLabel_<<"(ObjectViewCleaner) SUMMARY:\n"<<ss.str()
00167       <<"++++++++++++++++++++++++++++++++++++++++++++++++++"
00168       <<endl;
00169 }
00170 
00171 
00173 // plugin definition
00175 
00176 
00177 typedef ObjectViewCleaner<reco::Candidate>   CandViewCleaner;
00178 typedef ObjectViewCleaner<reco::Jet>         JetViewCleaner;
00179 typedef ObjectViewCleaner<reco::Muon>        MuonViewCleaner;
00180 typedef ObjectViewCleaner<reco::GsfElectron> GsfElectronViewCleaner;
00181 typedef ObjectViewCleaner<reco::Electron>    ElectronViewCleaner;
00182 typedef ObjectViewCleaner<reco::Photon>      PhotonViewCleaner;
00183 
00184 
00185 #include "FWCore/Framework/interface/MakerMacros.h"
00186 DEFINE_FWK_MODULE(CandViewCleaner);
00187 DEFINE_FWK_MODULE(JetViewCleaner);
00188 DEFINE_FWK_MODULE(MuonViewCleaner);
00189 DEFINE_FWK_MODULE(GsfElectronViewCleaner);
00190 DEFINE_FWK_MODULE(ElectronViewCleaner);
00191 DEFINE_FWK_MODULE(PhotonViewCleaner);