1 |
//
|
2 |
// $Id: PATElectronProducer.cc,v 1.20.2.9 2009/04/30 09:11:45 gpetrucc Exp $
|
3 |
//
|
4 |
|
5 |
#include "PhysicsTools/PatAlgos/plugins/PATElectronProducer.h"
|
6 |
|
7 |
#include "FWCore/MessageLogger/interface/MessageLogger.h"
|
8 |
#include "FWCore/ParameterSet/interface/FileInPath.h"
|
9 |
|
10 |
#include "DataFormats/Common/interface/Association.h"
|
11 |
#include "DataFormats/Common/interface/ValueMap.h"
|
12 |
#include "DataFormats/HepMCCandidate/interface/GenParticleFwd.h"
|
13 |
#include "DataFormats/HepMCCandidate/interface/GenParticle.h"
|
14 |
|
15 |
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
|
16 |
#include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
|
17 |
|
18 |
#include "PhysicsTools/PatUtils/interface/TrackerIsolationPt.h"
|
19 |
#include "PhysicsTools/PatUtils/interface/CaloIsolationEnergy.h"
|
20 |
|
21 |
#include <vector>
|
22 |
#include <memory>
|
23 |
|
24 |
|
25 |
using namespace pat;
|
26 |
|
27 |
|
28 |
PATElectronProducer::PATElectronProducer(const edm::ParameterSet & iConfig) :
|
29 |
isolator_(iConfig.exists("isolation") ? iConfig.getParameter<edm::ParameterSet>("isolation") : edm::ParameterSet(), false) ,
|
30 |
userDataHelper_ ( iConfig.getParameter<edm::ParameterSet>("userData") )
|
31 |
{
|
32 |
|
33 |
// general configurables
|
34 |
electronSrc_ = iConfig.getParameter<edm::InputTag>( "electronSource" );
|
35 |
embedGsfTrack_ = iConfig.getParameter<bool> ( "embedGsfTrack" );
|
36 |
embedSuperCluster_= iConfig.getParameter<bool> ( "embedSuperCluster" );
|
37 |
embedTrack_ = iConfig.getParameter<bool> ( "embedTrack" );
|
38 |
|
39 |
// pflow specific
|
40 |
pfElecSrc_ = iConfig.getParameter<edm::InputTag>( "pfElectronSource" );
|
41 |
useParticleFlow_ = iConfig.getParameter<bool>( "useParticleFlow" );
|
42 |
embedPFCandidate_ = iConfig.getParameter<bool>( "embedPFCandidate" );
|
43 |
|
44 |
|
45 |
// MC matching configurables
|
46 |
addGenMatch_ = iConfig.getParameter<bool> ( "addGenMatch" );
|
47 |
if (addGenMatch_) {
|
48 |
embedGenMatch_ = iConfig.getParameter<bool> ( "embedGenMatch" );
|
49 |
if (iConfig.existsAs<edm::InputTag>("genParticleMatch")) {
|
50 |
genMatchSrc_.push_back(iConfig.getParameter<edm::InputTag>( "genParticleMatch" ));
|
51 |
} else {
|
52 |
genMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "genParticleMatch" );
|
53 |
}
|
54 |
}
|
55 |
|
56 |
// trigger matching configurables
|
57 |
addTrigMatch_ = iConfig.getParameter<bool> ( "addTrigMatch" );
|
58 |
trigMatchSrc_ = iConfig.getParameter<std::vector<edm::InputTag> >( "trigPrimMatch" );
|
59 |
|
60 |
// resolution configurables
|
61 |
|
62 |
// electron ID configurables
|
63 |
addElecID_ = iConfig.getParameter<bool> ( "addElectronID" );
|
64 |
if (addElecID_) {
|
65 |
// it might be a single electron ID
|
66 |
if (iConfig.existsAs<edm::InputTag>("electronIDSource")) {
|
67 |
elecIDSrcs_.push_back(NameTag("", iConfig.getParameter<edm::InputTag>("electronIDSource")));
|
68 |
}
|
69 |
// or there might be many of them
|
70 |
if (iConfig.existsAs<edm::ParameterSet>("electronIDSources")) {
|
71 |
// please don't configure me twice
|
72 |
if (!elecIDSrcs_.empty()) throw cms::Exception("Configuration") <<
|
73 |
"PATElectronProducer: you can't specify both 'electronIDSource' and 'electronIDSources'\n";
|
74 |
// read the different electron ID names
|
75 |
edm::ParameterSet idps = iConfig.getParameter<edm::ParameterSet>("electronIDSources");
|
76 |
std::vector<std::string> names = idps.getParameterNamesForType<edm::InputTag>();
|
77 |
for (std::vector<std::string>::const_iterator it = names.begin(), ed = names.end(); it != ed; ++it) {
|
78 |
elecIDSrcs_.push_back(NameTag(*it, idps.getParameter<edm::InputTag>(*it)));
|
79 |
}
|
80 |
}
|
81 |
// but in any case at least once
|
82 |
if (elecIDSrcs_.empty()) throw cms::Exception("Configuration") <<
|
83 |
"PATElectronProducer: id addElectronID is true, you must specify either:\n" <<
|
84 |
"\tInputTag electronIDSource = <someTag>\n" << "or\n" <<
|
85 |
"\tPSet electronIDSources = { \n" <<
|
86 |
"\t\tInputTag <someName> = <someTag> // as many as you want \n " <<
|
87 |
"\t}\n";
|
88 |
}
|
89 |
|
90 |
// construct resolution calculator
|
91 |
|
92 |
// IsoDeposit configurables
|
93 |
if (iConfig.exists("isoDeposits")) {
|
94 |
edm::ParameterSet depconf = iConfig.getParameter<edm::ParameterSet>("isoDeposits");
|
95 |
if (depconf.exists("tracker")) isoDepositLabels_.push_back(std::make_pair(TrackerIso, depconf.getParameter<edm::InputTag>("tracker")));
|
96 |
if (depconf.exists("ecal")) isoDepositLabels_.push_back(std::make_pair(ECalIso, depconf.getParameter<edm::InputTag>("ecal")));
|
97 |
if (depconf.exists("hcal")) isoDepositLabels_.push_back(std::make_pair(HCalIso, depconf.getParameter<edm::InputTag>("hcal")));
|
98 |
if (depconf.exists("particle")) isoDepositLabels_.push_back(std::make_pair(ParticleIso, depconf.getParameter<edm::InputTag>("particle")));
|
99 |
if (depconf.exists("chargedparticle")) isoDepositLabels_.push_back(std::make_pair(ChargedParticleIso, depconf.getParameter<edm::InputTag>("chargedparticle")));
|
100 |
if (depconf.exists("neutralparticle")) isoDepositLabels_.push_back(std::make_pair(NeutralParticleIso,depconf.getParameter<edm::InputTag>("neutralparticle")));
|
101 |
if (depconf.exists("gammaparticle")) isoDepositLabels_.push_back(std::make_pair(GammaParticleIso, depconf.getParameter<edm::InputTag>("gammaparticle")));
|
102 |
|
103 |
|
104 |
if (depconf.exists("user")) {
|
105 |
std::vector<edm::InputTag> userdeps = depconf.getParameter<std::vector<edm::InputTag> >("user");
|
106 |
std::vector<edm::InputTag>::const_iterator it = userdeps.begin(), ed = userdeps.end();
|
107 |
int key = UserBaseIso;
|
108 |
for ( ; it != ed; ++it, ++key) {
|
109 |
isoDepositLabels_.push_back(std::make_pair(IsolationKeys(key), *it));
|
110 |
}
|
111 |
}
|
112 |
}
|
113 |
|
114 |
// Efficiency configurables
|
115 |
addEfficiencies_ = iConfig.getParameter<bool>("addEfficiencies");
|
116 |
if (addEfficiencies_) {
|
117 |
efficiencyLoader_ = pat::helper::EfficiencyLoader(iConfig.getParameter<edm::ParameterSet>("efficiencies"));
|
118 |
}
|
119 |
|
120 |
// Resolution configurables
|
121 |
addResolutions_ = iConfig.getParameter<bool>("addResolutions");
|
122 |
if (addResolutions_) {
|
123 |
resolutionLoader_ = pat::helper::KinResolutionsLoader(iConfig.getParameter<edm::ParameterSet>("resolutions"));
|
124 |
}
|
125 |
|
126 |
// Check to see if the user wants to add user data
|
127 |
useUserData_ = false;
|
128 |
if ( iConfig.exists("userData") ) {
|
129 |
useUserData_ = true;
|
130 |
}
|
131 |
|
132 |
// electron ID configurables
|
133 |
addElecShapes_ = iConfig.getParameter<bool>("addElectronShapes" );
|
134 |
reducedBarrelRecHitCollection_ = iConfig.getParameter<edm::InputTag>("reducedBarrelRecHitCollection") ;
|
135 |
reducedEndcapRecHitCollection_ = iConfig.getParameter<edm::InputTag>("reducedEndcapRecHitCollection") ;
|
136 |
|
137 |
// produces vector of muons
|
138 |
produces<std::vector<Electron> >();
|
139 |
|
140 |
}
|
141 |
|
142 |
|
143 |
PATElectronProducer::~PATElectronProducer() {
|
144 |
}
|
145 |
|
146 |
|
147 |
void PATElectronProducer::produce(edm::Event & iEvent, const edm::EventSetup & iSetup) {
|
148 |
|
149 |
// Get the collection of electrons from the event
|
150 |
edm::Handle<edm::View<ElectronType> > electrons;
|
151 |
iEvent.getByLabel(electronSrc_, electrons);
|
152 |
|
153 |
if (isolator_.enabled()) isolator_.beginEvent(iEvent,iSetup);
|
154 |
|
155 |
if (efficiencyLoader_.enabled()) efficiencyLoader_.newEvent(iEvent);
|
156 |
if (resolutionLoader_.enabled()) resolutionLoader_.newEvent(iEvent, iSetup);
|
157 |
|
158 |
std::vector<edm::Handle<edm::ValueMap<IsoDeposit> > > deposits(isoDepositLabels_.size());
|
159 |
for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
|
160 |
iEvent.getByLabel(isoDepositLabels_[j].second, deposits[j]);
|
161 |
}
|
162 |
|
163 |
// prepare the MC matching
|
164 |
GenAssociations genMatches(genMatchSrc_.size());
|
165 |
if (addGenMatch_) {
|
166 |
for (size_t j = 0, nd = genMatchSrc_.size(); j < nd; ++j) {
|
167 |
iEvent.getByLabel(genMatchSrc_[j], genMatches[j]);
|
168 |
}
|
169 |
}
|
170 |
|
171 |
|
172 |
// prepare the trigger matching
|
173 |
TrigAssociations trigMatches(trigMatchSrc_.size());
|
174 |
if ( addTrigMatch_ ) {
|
175 |
for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
|
176 |
iEvent.getByLabel(trigMatchSrc_[i], trigMatches[i]);
|
177 |
}
|
178 |
}
|
179 |
|
180 |
// prepare ID extraction
|
181 |
std::vector<edm::Handle<edm::ValueMap<float> > > idhandles;
|
182 |
std::vector<pat::Electron::IdPair> ids;
|
183 |
if (addElecID_) {
|
184 |
idhandles.resize(elecIDSrcs_.size());
|
185 |
ids.resize(elecIDSrcs_.size());
|
186 |
for (size_t i = 0; i < elecIDSrcs_.size(); ++i) {
|
187 |
iEvent.getByLabel(elecIDSrcs_[i].second, idhandles[i]);
|
188 |
ids[i].first = elecIDSrcs_[i].first;
|
189 |
}
|
190 |
}
|
191 |
|
192 |
|
193 |
if (addElecShapes_) {
|
194 |
lazyTools_ .reset(new EcalClusterLazyTools( iEvent , iSetup , reducedBarrelRecHitCollection_ , reducedEndcapRecHitCollection_ ));
|
195 |
}
|
196 |
|
197 |
std::vector<Electron> * patElectrons = new std::vector<Electron>();
|
198 |
|
199 |
|
200 |
if( useParticleFlow_ ) {
|
201 |
edm::Handle< reco::PFCandidateCollection > pfElectrons;
|
202 |
iEvent.getByLabel(pfElecSrc_, pfElectrons);
|
203 |
unsigned index=0;
|
204 |
|
205 |
for( reco::PFCandidateConstIterator i = pfElectrons->begin();
|
206 |
i != pfElectrons->end(); ++i, ++index) {
|
207 |
|
208 |
reco::PFCandidateRef pfRef(pfElectrons,index);
|
209 |
reco::PFCandidatePtr ptrToMother(pfElectrons,index);
|
210 |
reco::CandidateBaseRef pfBaseRef( pfRef );
|
211 |
|
212 |
reco::GsfTrackRef PfTk= i->gsfTrackRef();
|
213 |
|
214 |
bool Matched=false;
|
215 |
for (edm::View<ElectronType>::const_iterator itElectron = electrons->begin(); itElectron != electrons->end(); ++itElectron) {
|
216 |
unsigned int idx = itElectron - electrons->begin();
|
217 |
if (Matched) continue;
|
218 |
reco::GsfTrackRef EgTk= itElectron->gsfTrack();
|
219 |
if (itElectron->gsfTrack()==i->gsfTrackRef()){
|
220 |
const ElectronBaseRef elecsRef = electrons->refAt(idx);
|
221 |
Electron anElectron(elecsRef);
|
222 |
FillElectron(anElectron,elecsRef,pfBaseRef, genMatches, trigMatches);
|
223 |
Matched=true;
|
224 |
anElectron.setPFCandidateRef( pfRef );
|
225 |
if( embedPFCandidate_ ) anElectron.embedPFCandidate();
|
226 |
|
227 |
if (isolator_.enabled()){
|
228 |
reco::CandidatePtr mother = ptrToMother->sourceCandidatePtr(0);
|
229 |
isolator_.fill(mother, isolatorTmpStorage_);
|
230 |
typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
|
231 |
for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(),
|
232 |
ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
|
233 |
anElectron.setIsolation(it->first, it->second);
|
234 |
}
|
235 |
for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
|
236 |
anElectron.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[mother]);
|
237 |
}
|
238 |
}
|
239 |
//Electron Id
|
240 |
if (addElecID_) {
|
241 |
//STANDARD EL ID
|
242 |
for (size_t i = 0; i < elecIDSrcs_.size(); ++i) {
|
243 |
ids[i].second = (*idhandles[i])[elecsRef];
|
244 |
}
|
245 |
//SPECIFIC PF ID
|
246 |
ids.push_back(std::make_pair("pf_evspi",pfRef->mva_e_pi()));
|
247 |
ids.push_back(std::make_pair("pf_evsmu",pfRef->mva_e_mu()));
|
248 |
anElectron.setElectronIDs(ids);
|
249 |
}
|
250 |
|
251 |
patElectrons->push_back(anElectron);
|
252 |
|
253 |
}
|
254 |
|
255 |
|
256 |
}
|
257 |
|
258 |
|
259 |
}
|
260 |
}
|
261 |
|
262 |
else{
|
263 |
for (edm::View<ElectronType>::const_iterator itElectron = electrons->begin(); itElectron != electrons->end(); ++itElectron) {
|
264 |
// construct the Electron from the ref -> save ref to original object
|
265 |
unsigned int idx = itElectron - electrons->begin();
|
266 |
|
267 |
const ElectronBaseRef elecsRef = electrons->refAt(idx);
|
268 |
reco::CandidateBaseRef elecBaseRef(elecsRef);
|
269 |
|
270 |
// const edm::Ptr<ElectronType> electronPtr = electrons->ptrAt(idx);
|
271 |
Electron anElectron(elecsRef);
|
272 |
|
273 |
FillElectron(anElectron,elecsRef,elecBaseRef, genMatches, trigMatches);
|
274 |
|
275 |
|
276 |
// add resolution info
|
277 |
|
278 |
// Isolation
|
279 |
if (isolator_.enabled()) {
|
280 |
isolator_.fill(*electrons, idx, isolatorTmpStorage_);
|
281 |
typedef pat::helper::MultiIsolator::IsolationValuePairs IsolationValuePairs;
|
282 |
// better to loop backwards, so the vector is resized less times
|
283 |
for (IsolationValuePairs::const_reverse_iterator it = isolatorTmpStorage_.rbegin(), ed = isolatorTmpStorage_.rend(); it != ed; ++it) {
|
284 |
anElectron.setIsolation(it->first, it->second);
|
285 |
}
|
286 |
}
|
287 |
|
288 |
for (size_t j = 0, nd = deposits.size(); j < nd; ++j) {
|
289 |
anElectron.setIsoDeposit(isoDepositLabels_[j].first, (*deposits[j])[elecsRef]);
|
290 |
}
|
291 |
|
292 |
// add electron ID info
|
293 |
if (addElecID_) {
|
294 |
for (size_t i = 0; i < elecIDSrcs_.size(); ++i) {
|
295 |
ids[i].second = (*idhandles[i])[elecsRef];
|
296 |
}
|
297 |
anElectron.setElectronIDs(ids);
|
298 |
}
|
299 |
|
300 |
|
301 |
if ( useUserData_ ) {
|
302 |
userDataHelper_.add( anElectron, iEvent, iSetup );
|
303 |
}
|
304 |
|
305 |
|
306 |
// add sel to selected
|
307 |
patElectrons->push_back(anElectron);
|
308 |
}
|
309 |
}
|
310 |
|
311 |
// sort electrons in pt
|
312 |
std::sort(patElectrons->begin(), patElectrons->end(), pTComparator_);
|
313 |
|
314 |
// add the electrons to the event output
|
315 |
std::auto_ptr<std::vector<Electron> > ptr(patElectrons);
|
316 |
iEvent.put(ptr);
|
317 |
|
318 |
// clean up
|
319 |
if (isolator_.enabled()) isolator_.endEvent();
|
320 |
|
321 |
}
|
322 |
|
323 |
void PATElectronProducer::FillElectron(Electron& anElectron,
|
324 |
const ElectronBaseRef& elecsRef,
|
325 |
const reco::CandidateBaseRef& baseRef,
|
326 |
const GenAssociations& genMatches,
|
327 |
const TrigAssociations& trigMatches)const {
|
328 |
if (embedGsfTrack_) anElectron.embedGsfTrack();
|
329 |
if (embedSuperCluster_) anElectron.embedSuperCluster();
|
330 |
if (embedTrack_) anElectron.embedTrack();
|
331 |
|
332 |
// store the match to the generated final state muons
|
333 |
if (addGenMatch_) {
|
334 |
for(size_t i = 0, n = genMatches.size(); i < n; ++i) {
|
335 |
reco::GenParticleRef genElectron = (*genMatches[i])[elecsRef];
|
336 |
anElectron.addGenParticleRef(genElectron);
|
337 |
}
|
338 |
if (embedGenMatch_) anElectron.embedGenParticle();
|
339 |
}
|
340 |
// matches to trigger primitives
|
341 |
if ( addTrigMatch_ ) {
|
342 |
for ( size_t i = 0; i < trigMatchSrc_.size(); ++i ) {
|
343 |
TriggerPrimitiveRef trigPrim = (*trigMatches[i])[elecsRef];
|
344 |
if ( trigPrim.isNonnull() && trigPrim.isAvailable() ) {
|
345 |
anElectron.addTriggerMatch(*trigPrim);
|
346 |
}
|
347 |
}
|
348 |
}
|
349 |
|
350 |
if (efficiencyLoader_.enabled()) {
|
351 |
efficiencyLoader_.setEfficiencies( anElectron, elecsRef );
|
352 |
}
|
353 |
|
354 |
if (resolutionLoader_.enabled()) {
|
355 |
resolutionLoader_.setResolutions(anElectron);
|
356 |
}
|
357 |
|
358 |
// add electron shapes info
|
359 |
if (addElecShapes_) {
|
360 |
std::vector<float> covariances = lazyTools_->covariances(*(elecsRef->superCluster()->seed())) ;
|
361 |
std::vector<float> localCovariances = lazyTools_->localCovariances(*(elecsRef->superCluster()->seed())) ;
|
362 |
float scSigmaEtaEta = sqrt(covariances[0]) ;
|
363 |
float scSigmaIEtaIEta = sqrt(localCovariances[0]) ;
|
364 |
float scE1x5 = lazyTools_->e1x5(*(elecsRef->superCluster()->seed())) ;
|
365 |
float scE2x5Max = lazyTools_->e2x5Max(*(elecsRef->superCluster()->seed())) ;
|
366 |
float scE5x5 = lazyTools_->e5x5(*(elecsRef->superCluster()->seed())) ;
|
367 |
anElectron.setClusterShapes(scSigmaEtaEta,scSigmaIEtaIEta,scE1x5,scE2x5Max,scE5x5) ;
|
368 |
}
|
369 |
}
|
370 |
|
371 |
|
372 |
#include "FWCore/Framework/interface/MakerMacros.h"
|
373 |
|
374 |
DEFINE_FWK_MODULE(PATElectronProducer);
|