Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00024
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
00053 template<typename T>
00054 class ObjectViewCleaner : public edm::EDProducer
00055 {
00056 public:
00057
00058 ObjectViewCleaner(const edm::ParameterSet& iConfig);
00059 virtual ~ObjectViewCleaner();
00060
00061
00062 void produce(edm::Event& iEvent,const edm::EventSetup& iSetup);
00063 void endJob();
00064
00065 private:
00066
00067 edm::InputTag srcCands_;
00068 std::vector<edm::InputTag> srcObjects_;
00069 double deltaRMin_;
00070
00071 std::string moduleLabel_;
00072 StringCutObjectSelector<T,true> objKeepCut_;
00073 StringCutObjectSelector<reco::Candidate,true> objRemoveCut_;
00074
00075 unsigned int nObjectsTot_;
00076 unsigned int nObjectsClean_;
00077 };
00078
00079
00080 using namespace std;
00081
00082
00084
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
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
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);