CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ObjectViewCleaner.cc
Go to the documentation of this file.
1 
2 /*****************************************************************************
3  * Project: CMS detector at the CERN
4  *
5  * Package: PhysicsTools/TagAndProbe
6  *
7  *
8  * Authors:
9  *
10  * Kalanand Mishra, Fermilab - kalanand@fnal.gov
11  *
12  * Description:
13  * - Cleans a given object collection of other
14  * cross-object candidates using deltaR-matching.
15  * - For example: can clean a muon collection by
16  * removing all jets in the muon collection.
17  * - Saves collection of the reference vectors of cleaned objects.
18  * History:
19  * Generalized the existing CandViewCleaner
20  *
21  * Copyright (C) 2010 FNAL
22  *****************************************************************************/
24 // Includes
30 
33 
41 
42 
44 
45 #include <memory>
46 #include <vector>
47 #include <sstream>
48 
49 
51 // class definition
53 template<typename T>
55 {
56 public:
57  // construction/destruction
58  ObjectViewCleaner(const edm::ParameterSet& iConfig);
59  virtual ~ObjectViewCleaner();
60 
61  // member functions
62  void produce(edm::Event& iEvent,const edm::EventSetup& iSetup);
63  void endJob();
64 
65 private:
66  // member data
68  std::vector<edm::InputTag> srcObjects_;
69  double deltaRMin_;
70 
71  std::string moduleLabel_;
72  StringCutObjectSelector<T,true> objKeepCut_; // lazy parsing, to allow cutting on variables not in reco::Candidate class
73  StringCutObjectSelector<reco::Candidate,true> objRemoveCut_; // lazy parsing, to allow cutting on variables
74 
75  unsigned int nObjectsTot_;
76  unsigned int nObjectsClean_;
77 };
78 
79 
80 using namespace std;
81 
82 
84 // construction/destruction
86 
87 //______________________________________________________________________________
88 template<typename T>
90  : srcCands_ (iConfig.getParameter<edm::InputTag> ("srcObject"))
91  , srcObjects_ (iConfig.getParameter<vector<edm::InputTag> >("srcObjectsToRemove"))
92  , deltaRMin_ (iConfig.getParameter<double> ("deltaRMin"))
93  , moduleLabel_(iConfig.getParameter<string> ("@module_label"))
94  , objKeepCut_(iConfig.existsAs<std::string>("srcObjectSelection") ? iConfig.getParameter<std::string>("srcObjectSelection") : "", true)
95  ,objRemoveCut_(iConfig.existsAs<std::string>("srcObjectsToRemoveSelection") ? iConfig.getParameter<std::string>("srcObjectsToRemoveSelection") : "", true)
96  , nObjectsTot_(0)
97  , nObjectsClean_(0)
98 {
99  produces<edm::RefToBaseVector<T> >();
100 }
101 
102 
103 //______________________________________________________________________________
104 template<typename T>
106 {
107 
108 }
109 
110 
111 
113 // implementation of member functions
115 
116 //______________________________________________________________________________
117 template<typename T>
119 {
120  auto_ptr<edm::RefToBaseVector<T> >
121  cleanObjects(new edm::RefToBaseVector<T >());
122 
123  edm::Handle<edm::View<T> > candidates;
124  iEvent.getByLabel(srcCands_,candidates);
125 
126  bool* isClean = new bool[candidates->size()];
127  for (unsigned int iObject=0;iObject<candidates->size();iObject++) isClean[iObject] = true;
128 
129  for (unsigned int iSrc=0;iSrc<srcObjects_.size();iSrc++) {
131  iEvent.getByLabel(srcObjects_[iSrc],objects);
132 
133  for (unsigned int iObject=0;iObject<candidates->size();iObject++) {
134  const T& candidate = candidates->at(iObject);
135  if (!objKeepCut_(candidate)) isClean[iObject] = false;
136 
137  for (unsigned int iObj=0;iObj<objects->size();iObj++) {
138  const reco::Candidate& obj = objects->at(iObj);
139  if (!objRemoveCut_(obj)) continue;
140 
141  double deltaR = reco::deltaR(candidate,obj);
142  if (deltaR<deltaRMin_) isClean[iObject] = false;
143  }
144  }
145  }
146 
147  for (unsigned int iObject=0;iObject<candidates->size();iObject++)
148  if (isClean[iObject]) cleanObjects->push_back(candidates->refAt(iObject));
149 
150  nObjectsTot_ +=candidates->size();
151  nObjectsClean_+=cleanObjects->size();
152 
153  delete [] isClean;
154  iEvent.put(cleanObjects);
155 }
156 
157 
158 //______________________________________________________________________________
159 template<typename T>
161 {
162  stringstream ss;
163  ss<<"nObjectsTot="<<nObjectsTot_<<" nObjectsClean="<<nObjectsClean_
164  <<" fObjectsClean="<<100.*(nObjectsClean_/(double)nObjectsTot_)<<"%\n";
165  cout<<"++++++++++++++++++++++++++++++++++++++++++++++++++"
166  <<"\n"<<moduleLabel_<<"(ObjectViewCleaner) SUMMARY:\n"<<ss.str()
167  <<"++++++++++++++++++++++++++++++++++++++++++++++++++"
168  <<endl;
169 }
170 
171 
173 // plugin definition
175 
176 
183 
184 
void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
ObjectViewCleaner(const edm::ParameterSet &iConfig)
ObjectViewCleaner< reco::Electron > ElectronViewCleaner
unsigned int nObjectsTot_
ObjectViewCleaner< reco::Jet > JetViewCleaner
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
ObjectViewCleaner< reco::Photon > PhotonViewCleaner
ObjectViewCleaner< reco::GsfElectron > GsfElectronViewCleaner
ObjectViewCleaner< reco::Muon > MuonViewCleaner
unsigned int nObjectsClean_
double deltaR(double eta1, double phi1, double eta2, double phi2)
Definition: deltaR.h:19
edm::InputTag srcCands_
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
StringCutObjectSelector< reco::Candidate, true > objRemoveCut_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
std::string moduleLabel_
std::vector< edm::InputTag > srcObjects_
StringCutObjectSelector< T, true > objKeepCut_
ObjectViewCleaner< reco::Candidate > CandViewCleaner
tuple cout
Definition: gather_cfg.py:121
long double T