1 |
// -*- C++ -*-
|
2 |
//
|
3 |
// Package: ObjectCounter
|
4 |
// Class: ObjectCounter
|
5 |
//
|
6 |
/**\class ObjectCounter ObjectCounter.cc CommonTools/ObjectCounter/src/ObjectCounter.cc
|
7 |
|
8 |
Description: [one line class summary]
|
9 |
|
10 |
Implementation:
|
11 |
[Notes on implementation]
|
12 |
*/
|
13 |
//
|
14 |
// Original Author: Yutaro Iiyama,512 1-005,+41227670489,
|
15 |
// Created: Fri Jun 1 11:20:59 CEST 2012
|
16 |
// $Id: ObjectCounter.cc,v 1.1 2012/06/01 11:10:17 yiiyama Exp $
|
17 |
//
|
18 |
//
|
19 |
|
20 |
|
21 |
// system include files
|
22 |
#include <memory>
|
23 |
|
24 |
// user include files
|
25 |
#include "FWCore/Framework/interface/Frameworkfwd.h"
|
26 |
#include "FWCore/Framework/interface/EDFilter.h"
|
27 |
|
28 |
#include "FWCore/Framework/interface/Event.h"
|
29 |
#include "FWCore/Framework/interface/MakerMacros.h"
|
30 |
|
31 |
#include "FWCore/ParameterSet/interface/ParameterSet.h"
|
32 |
|
33 |
#include "FWCore/Utilities/interface/Exception.h"
|
34 |
#include "FWCore/Utilities/interface/InputTag.h"
|
35 |
|
36 |
#include "DataFormats/Common/interface/Handle.h"
|
37 |
#include "DataFormats/Common/interface/RefToBaseVector.h"
|
38 |
|
39 |
#include "DataFormats/Candidate/interface/Candidate.h"
|
40 |
#include "DataFormats/Candidate/interface/CandidateFwd.h"
|
41 |
#include "DataFormats/EgammaCandidates/interface/Photon.h"
|
42 |
#include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
|
43 |
#include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
|
44 |
#include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
|
45 |
#include "DataFormats/MuonReco/interface/Muon.h"
|
46 |
#include "DataFormats/MuonReco/interface/MuonFwd.h"
|
47 |
|
48 |
//
|
49 |
// class declaration
|
50 |
//
|
51 |
|
52 |
class ObjectCounter : public edm::EDFilter {
|
53 |
public:
|
54 |
explicit ObjectCounter(const edm::ParameterSet&);
|
55 |
~ObjectCounter();
|
56 |
|
57 |
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
|
58 |
|
59 |
private:
|
60 |
virtual void beginJob() ;
|
61 |
virtual bool filter(edm::Event&, const edm::EventSetup&);
|
62 |
virtual void endJob() ;
|
63 |
|
64 |
virtual bool beginRun(edm::Run&, edm::EventSetup const&);
|
65 |
virtual bool endRun(edm::Run&, edm::EventSetup const&);
|
66 |
virtual bool beginLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&);
|
67 |
virtual bool endLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&);
|
68 |
|
69 |
// ----------member data ---------------------------
|
70 |
|
71 |
edm::InputTag srcTag_;
|
72 |
edm::InputTag matchTag_;
|
73 |
unsigned threshold_;
|
74 |
std::string object_;
|
75 |
};
|
76 |
|
77 |
//
|
78 |
// constants, enums and typedefs
|
79 |
//
|
80 |
|
81 |
//
|
82 |
// static data member definitions
|
83 |
//
|
84 |
|
85 |
//
|
86 |
// constructors and destructor
|
87 |
//
|
88 |
ObjectCounter::ObjectCounter(const edm::ParameterSet& iConfig) :
|
89 |
srcTag_(iConfig.getParameter<edm::InputTag>("src")),
|
90 |
threshold_(iConfig.getUntrackedParameter<int>("threshold", 1)),
|
91 |
object_(iConfig.getParameter<std::string>("object"))
|
92 |
{
|
93 |
if(iConfig.existsAs<edm::InputTag>("matchTag"))
|
94 |
matchTag_ = iConfig.getParameter<edm::InputTag>("matchTag");
|
95 |
|
96 |
}
|
97 |
|
98 |
|
99 |
ObjectCounter::~ObjectCounter()
|
100 |
{
|
101 |
|
102 |
// do anything here that needs to be done at desctruction time
|
103 |
// (e.g. close files, deallocate resources etc.)
|
104 |
|
105 |
}
|
106 |
|
107 |
|
108 |
//
|
109 |
// member functions
|
110 |
//
|
111 |
|
112 |
// ------------ method called on each new Event ------------
|
113 |
bool
|
114 |
ObjectCounter::filter(edm::Event& iEvent, const edm::EventSetup&)
|
115 |
{
|
116 |
using namespace edm;
|
117 |
|
118 |
if(object_ == "Photon"){
|
119 |
Handle<reco::PhotonCollection> collection;
|
120 |
if(!iEvent.getByLabel(srcTag_, collection))
|
121 |
throw cms::Exception("ProductNotFound") << srcTag_;
|
122 |
|
123 |
return collection->size() >= threshold_;
|
124 |
}
|
125 |
else if(object_ == "Electron"){
|
126 |
Handle<reco::GsfElectronCollection> collection;
|
127 |
if(!iEvent.getByLabel(srcTag_, collection))
|
128 |
throw cms::Exception("ProductNotFound") << srcTag_;
|
129 |
|
130 |
return collection->size() >= threshold_;
|
131 |
}
|
132 |
else if(object_ == "Muon"){
|
133 |
Handle<reco::MuonCollection> collection;
|
134 |
if(!iEvent.getByLabel(srcTag_, collection))
|
135 |
throw cms::Exception("ProductNotFound") << srcTag_;
|
136 |
|
137 |
return collection->size() >= threshold_;
|
138 |
}
|
139 |
else if(object_ == "PhotonRef"){
|
140 |
Handle<RefToBaseVector<reco::Photon> > collection;
|
141 |
if(!iEvent.getByLabel(srcTag_, collection))
|
142 |
throw cms::Exception("ProductNotFound") << srcTag_;
|
143 |
|
144 |
return collection->size() >= threshold_;
|
145 |
}
|
146 |
else if(object_ == "ElectronRef"){
|
147 |
Handle<reco::GsfElectronRefVector> collection;
|
148 |
if(iEvent.getByLabel(srcTag_, collection))
|
149 |
return collection->size() >= threshold_;
|
150 |
else{
|
151 |
Handle<RefToBaseVector<reco::GsfElectron> > collection2;
|
152 |
if(iEvent.getByLabel(srcTag_, collection2))
|
153 |
return collection2->size() >= threshold_;
|
154 |
else
|
155 |
throw cms::Exception("ProductNotFound") << srcTag_;
|
156 |
}
|
157 |
}
|
158 |
else if(object_ == "MuonRef"){
|
159 |
Handle<reco::MuonRefVector> collection;
|
160 |
if(iEvent.getByLabel(srcTag_, collection))
|
161 |
return collection->size() >= threshold_;
|
162 |
else{
|
163 |
Handle<RefToBaseVector<reco::Muon> > collection2;
|
164 |
if(iEvent.getByLabel(srcTag_, collection2))
|
165 |
return collection2->size() >= threshold_;
|
166 |
else
|
167 |
throw cms::Exception("ProductNotFound") << srcTag_;
|
168 |
}
|
169 |
}
|
170 |
else if(object_ == "Candidate"){
|
171 |
Handle<reco::CandidateView> collection;
|
172 |
if(!iEvent.getByLabel(srcTag_, collection)){
|
173 |
throw cms::Exception("ProductNotFound") << srcTag_;
|
174 |
}
|
175 |
|
176 |
return collection->size() >= threshold_;
|
177 |
}
|
178 |
else
|
179 |
return false;
|
180 |
}
|
181 |
|
182 |
// ------------ method called once each job just before starting event loop ------------
|
183 |
void
|
184 |
ObjectCounter::beginJob()
|
185 |
{
|
186 |
}
|
187 |
|
188 |
// ------------ method called once each job just after ending the event loop ------------
|
189 |
void
|
190 |
ObjectCounter::endJob() {
|
191 |
}
|
192 |
|
193 |
// ------------ method called when starting to processes a run ------------
|
194 |
bool
|
195 |
ObjectCounter::beginRun(edm::Run&, edm::EventSetup const&)
|
196 |
{
|
197 |
return true;
|
198 |
}
|
199 |
|
200 |
// ------------ method called when ending the processing of a run ------------
|
201 |
bool
|
202 |
ObjectCounter::endRun(edm::Run&, edm::EventSetup const&)
|
203 |
{
|
204 |
return true;
|
205 |
}
|
206 |
|
207 |
// ------------ method called when starting to processes a luminosity block ------------
|
208 |
bool
|
209 |
ObjectCounter::beginLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&)
|
210 |
{
|
211 |
return true;
|
212 |
}
|
213 |
|
214 |
// ------------ method called when ending the processing of a luminosity block ------------
|
215 |
bool
|
216 |
ObjectCounter::endLuminosityBlock(edm::LuminosityBlock&, edm::EventSetup const&)
|
217 |
{
|
218 |
return true;
|
219 |
}
|
220 |
|
221 |
// ------------ method fills 'descriptions' with the allowed parameters for the module ------------
|
222 |
void
|
223 |
ObjectCounter::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
|
224 |
//The following says we do not know what parameters are allowed so do no validation
|
225 |
// Please change this to state exactly what you do use, even if it is no parameters
|
226 |
edm::ParameterSetDescription desc;
|
227 |
desc.setUnknown();
|
228 |
descriptions.addDefault(desc);
|
229 |
}
|
230 |
//define this as a plug-in
|
231 |
DEFINE_FWK_MODULE(ObjectCounter);
|