ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/TreeFiller/src/FillerPFCandidates.cc
Revision: 1.18
Committed: Fri Dec 28 17:27:21 2012 UTC (12 years, 4 months ago) by pharris
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_032, Mit_031, Mit_030, Mit_029c, Mit_029b, Mit_030_pre1, Mit_029a, HEAD
Changes since 1.17: +10 -3 lines
Error occurred while calculating annotation data.
Log Message:
Added Embedded and PFAOD functionality

File Contents

# Content
1 // $Id: FillerPFCandidates.cc,v 1.17 2012/07/25 03:08:42 paus Exp $
2
3 #include "MitProd/TreeFiller/interface/FillerPFCandidates.h"
4 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
5 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
6 #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
7 #include "DataFormats/MuonReco/interface/Muon.h"
8 #include "DataFormats/MuonReco/interface/MuonFwd.h"
9 #include "DataFormats/EgammaCandidates/interface/Photon.h"
10 #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
11 #include "DataFormats/Common/interface/RefToPtr.h"
12 #include "MitAna/DataTree/interface/Muon.h"
13 #include "MitAna/DataTree/interface/Names.h"
14 #include "MitAna/DataTree/interface/PFCandidateCol.h"
15 #include "MitAna/DataTree/interface/Track.h"
16 #include "MitProd/ObjectService/interface/ObjectService.h"
17
18 using namespace std;
19 using namespace edm;
20 using namespace mithep;
21
22 //--------------------------------------------------------------------------------------------------
23 FillerPFCandidates::FillerPFCandidates(const edm::ParameterSet &cfg,
24 const char *name, bool active) :
25 BaseFiller (cfg,name,active),
26 edmName_ (Conf().getUntrackedParameter<string>("edmName","particleFlow")),
27 edmPfNoPileupName_ (Conf().getUntrackedParameter<string>("edmName","pfNoElectrons")),
28 mitName_ (Conf().getUntrackedParameter<string>("mitName",Names::gkPFCandidatesBrn)),
29 trackerTrackMapNames_ (Conf().exists("trackerTrackMapNames") ?
30 Conf().getUntrackedParameter<vector<string> >("trackerTrackMapNames") :
31 vector<string>()),
32 gsfTrackMapName_ (Conf().getUntrackedParameter<string>("gsfTrackMapName","")),
33 muonMapName_ (Conf().getUntrackedParameter<string>("muonMapName","")),
34 electronMapName_ (Conf().getUntrackedParameter<string>("electronMapName","")),
35 photonMapName_ (Conf().getUntrackedParameter<string>("photonMapName","")),
36 barrelSuperClusterMapName_ (Conf().getUntrackedParameter<string>("barrelSuperClusterMapName","")),
37 endcapSuperClusterMapName_ (Conf().getUntrackedParameter<string>("endcapSuperClusterMapName","")),
38 pfElectronSuperClusterMapName_(Conf().getUntrackedParameter<string>("pfElectronSuperClusterMapName","")),
39 conversionMapName_ (Conf().getUntrackedParameter<string>("conversionMapName","")),
40 pfCandMapName_ (Conf().getUntrackedParameter<string>("pfCandMapName","")),
41 pfNoPileupCandMapName_ (Conf().getUntrackedParameter<string>("pfNoPileupMapName","")),
42 allowMissingTrackRef_ (Conf().getUntrackedParameter<bool>("allowMissingTrackRef",false)),
43 allowMissingPhotonRef_ (Conf().getUntrackedParameter<bool>("allowMissingPhotonRef",false)),
44 fillPfNoPileup_ (Conf().getUntrackedParameter<bool>("fillPfNoPileup",true)),
45 gsfTrackMap_ (0),
46 muonMap_ (0),
47 electronMap_ (0),
48 photonMap_ (0),
49 barrelSuperClusterMap_ (0),
50 endcapSuperClusterMap_ (0),
51 pfElectronSuperClusterMap_ (0),
52 conversionMap_ (0),
53 pfCandMap_ (new mithep::PFCandidateMap),
54 pfNoPileupCandMap_ (new mithep::PFCandidateMap),
55 pfCands_ (new mithep::PFCandidateArr(16))
56 {
57 // Constructor.
58 }
59
60 //--------------------------------------------------------------------------------------------------
61 FillerPFCandidates::~FillerPFCandidates()
62 {
63 // Destructor.
64
65 delete pfCands_;
66 delete pfCandMap_;
67 delete pfNoPileupCandMap_;
68 }
69
70 //--------------------------------------------------------------------------------------------------
71 void FillerPFCandidates::BookDataBlock(TreeWriter &tws)
72 {
73 // Add particle-flow candidate branch to tree and get pointers to maps.
74
75 tws.AddBranch(mitName_,&pfCands_);
76 OS()->add<mithep::PFCandidateArr>(pfCands_,mitName_);
77
78 // if (!trackerTrackMapName_.empty()) {
79 // trackerTrackMap_ = OS()->get<TrackMap>(trackerTrackMapName_);
80 // if (trackerTrackMap_)
81 // AddBranchDep(mitName_,trackerTrackMap_->GetBrName());
82 // }
83
84 for (std::vector<std::string>::const_iterator bmapName = trackerTrackMapNames_.begin();
85 bmapName!=trackerTrackMapNames_.end(); ++bmapName) {
86 if (!bmapName->empty()) {
87 const TrackMap *map = OS()->get<TrackMap>(*bmapName);
88 if (map) {
89 trackerTrackMaps_.push_back(map);
90 AddBranchDep(mitName_,map->GetBrName());
91 }
92 }
93 }
94
95 if (!gsfTrackMapName_.empty()) {
96 gsfTrackMap_ = OS()->get<TrackMap>(gsfTrackMapName_);
97 if (gsfTrackMap_)
98 AddBranchDep(mitName_,gsfTrackMap_->GetBrName());
99 }
100 if (!muonMapName_.empty()) {
101 muonMap_ = OS()->get<MuonMap>(muonMapName_);
102 if (muonMap_)
103 AddBranchDep(mitName_,muonMap_->GetBrName());
104 }
105 if (!electronMapName_.empty()) {
106 electronMap_ = OS()->get<ElectronMap>(electronMapName_);
107 if (electronMap_)
108 AddBranchDep(mitName_,electronMap_->GetBrName());
109 }
110 if (!photonMapName_.empty()) {
111 photonMap_ = OS()->get<PhotonMap>(photonMapName_);
112 if (photonMap_)
113 AddBranchDep(mitName_,photonMap_->GetBrName());
114 }
115 if (!barrelSuperClusterMapName_.empty()) {
116 barrelSuperClusterMap_ = OS()->get<SuperClusterMap>(barrelSuperClusterMapName_);
117 if (barrelSuperClusterMap_)
118 AddBranchDep(mitName_,barrelSuperClusterMap_->GetBrName());
119 }
120 if (!endcapSuperClusterMapName_.empty()) {
121 endcapSuperClusterMap_ = OS()->get<SuperClusterMap>(endcapSuperClusterMapName_);
122 if (endcapSuperClusterMap_)
123 AddBranchDep(mitName_,endcapSuperClusterMap_->GetBrName());
124 }
125 if (!pfElectronSuperClusterMapName_.empty()) {
126 pfElectronSuperClusterMap_ = OS()->get<SuperClusterMap>(pfElectronSuperClusterMapName_);
127 if (pfElectronSuperClusterMap_)
128 AddBranchDep(mitName_,pfElectronSuperClusterMap_->GetBrName());
129 }
130 if (!conversionMapName_.empty()) {
131 conversionMap_ = OS()->get<ConversionMap>(conversionMapName_);
132 if (conversionMap_)
133 AddBranchDep(mitName_,conversionMap_->GetBrName());
134 }
135 if (!pfCandMapName_.empty()) {
136 pfCandMap_->SetBrName(mitName_);
137 OS()->add<PFCandidateMap>(pfCandMap_,pfCandMapName_);
138 }
139 if (!pfNoPileupCandMapName_.empty()) {
140 pfNoPileupCandMap_->SetBrName(mitName_);
141 OS()->add<PFCandidateMap>(pfNoPileupCandMap_,pfNoPileupCandMapName_);
142 }
143 }
144
145 //--------------------------------------------------------------------------------------------------
146 void FillerPFCandidates::FillDataBlock(const edm::Event &event,
147 const edm::EventSetup &setup)
148 {
149 // Fill muon information.
150
151 pfCands_->Delete();
152 pfCandMap_->Reset();
153 pfNoPileupCandMap_->Reset();
154
155 Handle<reco::PFCandidateCollection> hPfCandProduct;
156 GetProduct(edmName_, hPfCandProduct, event);
157 const reco::PFCandidateCollection &inPfCands = *(hPfCandProduct.product());
158
159 Handle<reco::PFCandidateCollection> hPfNoPileupCandProduct;
160 GetProduct(edmPfNoPileupName_, hPfNoPileupCandProduct, event);
161 const reco::PFCandidateCollection &inPfNoPileupCands = *(hPfNoPileupCandProduct.product());
162 for (reco::PFCandidateCollection::const_iterator iP = inPfCands.begin();
163 iP != inPfCands.end(); ++iP) {
164 mithep::PFCandidate *outPfCand = pfCands_->Allocate();
165 new (outPfCand) mithep::PFCandidate(iP->px(),iP->py(),iP->pz(),iP->energy());
166
167 // fill variables
168 outPfCand->SetCharge(iP->charge());
169 outPfCand->SetEECal(iP->ecalEnergy());
170 outPfCand->SetEHCal(iP->hcalEnergy());
171 outPfCand->SetEECalRaw(iP->rawEcalEnergy());
172 outPfCand->SetEHCalRaw(iP->rawHcalEnergy());
173 outPfCand->SetEPS1(iP->pS1Energy());
174 outPfCand->SetEPS2(iP->pS2Energy());
175 outPfCand->SetPError(iP->deltaP());
176 outPfCand->SetMvaEPi(iP->mva_e_pi());
177 outPfCand->SetMvaEMu(iP->mva_e_mu());
178 outPfCand->SetMvaPiMu(iP->mva_pi_mu());
179 outPfCand->SetMvaGamma(iP->mva_nothing_gamma());
180 outPfCand->SetMvaNeutralH(iP->mva_nothing_nh());
181 outPfCand->SetMvaGammaNeutralH(iP->mva_gamma_nh());
182 outPfCand->SetEtaECal(iP->positionAtECALEntrance().eta());
183 outPfCand->SetPhiECal(iP->positionAtECALEntrance().phi());
184
185 // fill source vertex
186 outPfCand->SetVertex(iP->vertex().x(),iP->vertex().y(),iP->vertex().z());
187
188 // fill pf type enum
189 if (iP->particleId()==reco::PFCandidate::X)
190 outPfCand->SetPFType(mithep::PFCandidate::eX);
191 else if (iP->particleId()==reco::PFCandidate::h)
192 outPfCand->SetPFType(mithep::PFCandidate::eHadron);
193 else if (iP->particleId()==reco::PFCandidate::e)
194 outPfCand->SetPFType(mithep::PFCandidate::eElectron);
195 else if (iP->particleId()==reco::PFCandidate::mu)
196 outPfCand->SetPFType(mithep::PFCandidate::eMuon);
197 else if (iP->particleId()==reco::PFCandidate::gamma)
198 outPfCand->SetPFType(mithep::PFCandidate::eGamma);
199 else if (iP->particleId()==reco::PFCandidate::h0)
200 outPfCand->SetPFType(mithep::PFCandidate::eNeutralHadron);
201 else if (iP->particleId()==reco::PFCandidate::h_HF)
202 outPfCand->SetPFType(mithep::PFCandidate::eHadronHF);
203 else if (iP->particleId()==reco::PFCandidate::egamma_HF)
204 outPfCand->SetPFType(mithep::PFCandidate::eEGammaHF);
205
206 // fill pf flags bitmask
207 outPfCand->SetFlag(mithep::PFCandidate::eNormal,
208 iP->flag(reco::PFCandidate::NORMAL));
209 outPfCand->SetFlag(mithep::PFCandidate::eEMPhiSModules,
210 iP->flag(reco::PFCandidate::E_PHI_SMODULES));
211 outPfCand->SetFlag(mithep::PFCandidate::eEMEta0,
212 iP->flag(reco::PFCandidate::E_ETA_0));
213 outPfCand->SetFlag(mithep::PFCandidate::eEMEtaModules,
214 iP->flag(reco::PFCandidate::E_ETA_MODULES));
215 outPfCand->SetFlag(mithep::PFCandidate::eEMBarrelEndcap,
216 iP->flag(reco::PFCandidate::E_BARREL_ENDCAP));
217 outPfCand->SetFlag(mithep::PFCandidate::eEMPreshowerEdge,
218 iP->flag(reco::PFCandidate::E_PRESHOWER_EDGE));
219 outPfCand->SetFlag(mithep::PFCandidate::eEMPreshower,
220 iP->flag(reco::PFCandidate::E_PRESHOWER));
221 outPfCand->SetFlag(mithep::PFCandidate::eEMEndCapEdge,
222 iP->flag(reco::PFCandidate::E_ENDCAP_EDGE));
223 outPfCand->SetFlag(mithep::PFCandidate::eHEta0,
224 iP->flag(reco::PFCandidate::H_ETA_0));
225 outPfCand->SetFlag(mithep::PFCandidate::eHBarrelEndcap,
226 iP->flag(reco::PFCandidate::H_BARREL_ENDCAP));
227 outPfCand->SetFlag(mithep::PFCandidate::eHEndcapVFCal,
228 iP->flag(reco::PFCandidate::H_ENDCAP_VFCAL));
229 outPfCand->SetFlag(mithep::PFCandidate::eHVFCalEdge,
230 iP->flag(reco::PFCandidate::H_VFCAL_EDGE));
231 outPfCand->SetFlag(mithep::PFCandidate::eToDispVtx,
232 iP->flag(reco::PFCandidate::T_TO_DISP));
233 outPfCand->SetFlag(mithep::PFCandidate::eFromDispVtx,
234 iP->flag(reco::PFCandidate::T_FROM_DISP));
235 outPfCand->SetFlag(mithep::PFCandidate::eFromV0,
236 iP->flag(reco::PFCandidate::T_FROM_V0));
237 outPfCand->SetFlag(mithep::PFCandidate::eFromGammaConv,
238 iP->flag(reco::PFCandidate::T_FROM_GAMMACONV));
239 outPfCand->SetFlag(mithep::PFCandidate::eToConversion,
240 iP->flag(reco::PFCandidate::GAMMA_TO_GAMMACONV));
241
242 //printf("pf type = %i\n",iP->particleId());
243
244 // fill references to other branches
245 if (iP->trackRef().isNonnull()) {
246 //printf("track: process = %i, product = %i, algo = %i, highPurity = %i\n",
247 // iP->trackRef().id().processIndex(),iP->trackRef().id().productIndex(),
248 // iP->trackRef()->algo(),iP->trackRef()->quality(reco::TrackBase::highPurity));
249 const mithep::Track *thetrack = getMitTrack(refToPtr(iP->trackRef()),allowMissingTrackRef_);
250 outPfCand->SetTrackerTrk(thetrack);
251 }
252 if (gsfTrackMap_ && iP->gsfTrackRef().isNonnull())
253 outPfCand->SetGsfTrk(gsfTrackMap_->GetMit(refToPtr(iP->gsfTrackRef())));
254 if (muonMap_ && iP->muonRef().isNonnull())
255 outPfCand->SetMuon(muonMap_->GetMit(refToPtr(iP->muonRef())));
256 if (electronMap_ && iP->gsfElectronRef().isNonnull())
257 outPfCand->SetElectron(electronMap_->GetMit(refToPtr(iP->gsfElectronRef())));
258 if (photonMap_ && iP->photonRef().isNonnull())
259 try{outPfCand->SetPhoton(photonMap_->GetMit(refToPtr(iP->photonRef())));}
260 catch (...) {
261 if(!allowMissingPhotonRef_) {
262 throw edm::Exception(edm::errors::Configuration, "FillerPFCandidates:FillDataBlock()\n")
263 << "Error! Photon unmapped collection " << edmName_ << std::endl;
264 }
265 }
266 if (barrelSuperClusterMap_ && endcapSuperClusterMap_ && pfElectronSuperClusterMap_ && iP->superClusterRef().isNonnull()) {
267 if (barrelSuperClusterMap_->HasMit(iP->superClusterRef()))
268 outPfCand->SetSCluster(barrelSuperClusterMap_->GetMit(iP->superClusterRef()));
269 else if (endcapSuperClusterMap_->HasMit(iP->superClusterRef()))
270 outPfCand->SetSCluster(endcapSuperClusterMap_->GetMit(iP->superClusterRef()));
271 else if ( pfElectronSuperClusterMap_->HasMit(iP->superClusterRef()))
272 outPfCand->SetSCluster(pfElectronSuperClusterMap_->GetMit(iP->superClusterRef()));
273 }
274 if (conversionMap_ && iP->conversionRef().isNonnull())
275 outPfCand->SetConversion(conversionMap_->GetMit(iP->conversionRef()));
276
277 // add to exported pf candidate map
278 reco::PFCandidatePtr thePtr(hPfCandProduct, iP - inPfCands.begin());
279 pfCandMap_->Add(thePtr, outPfCand);
280
281 //add pf No Pileup map
282 //===> Do I do a loop?
283 bool found = false;
284 if(fillPfNoPileup_) {
285 outPfCand->SetFlag(mithep::PFCandidate::ePFNoPileup,false);
286 for (reco::PFCandidateCollection::const_iterator iNoPileupP = inPfNoPileupCands.begin();
287 iNoPileupP != inPfNoPileupCands.end(); ++iNoPileupP) {
288 if(iP->px() == iNoPileupP->px() && iP->py() == iNoPileupP->py() && iNoPileupP->pz() == iP->pz()) {
289 reco::PFCandidatePtr theNoPileupPtr(hPfNoPileupCandProduct, iNoPileupP - inPfNoPileupCands.begin());
290 pfNoPileupCandMap_->Add(theNoPileupPtr, outPfCand);
291 outPfCand->SetFlag(mithep::PFCandidate::ePFNoPileup,true);
292 if(found) edm::Exception(edm::errors::Configuration, "FillerPFCandidates:FillDataBlock()\n")
293 << "Error! PF No Pileup double linked " << endl;
294 found = true;
295 }
296 }
297 }
298 }
299 pfCands_->Trim();
300 }
301
302 //--------------------------------------------------------------------------------------------------
303 void FillerPFCandidates::ResolveLinks(const edm::Event &event,
304 const edm::EventSetup &setup)
305 {
306 // Resolve and fill mother-daughter links.
307
308 Handle<reco::PFCandidateCollection> hPfCandProduct;
309 GetProduct(edmName_, hPfCandProduct, event);
310 const reco::PFCandidateCollection &inPfCands = *(hPfCandProduct.product());
311
312 // loop through pf candidates and resolve mother-daughter links
313 for (reco::PFCandidateCollection::const_iterator iP = inPfCands.begin();
314 iP != inPfCands.end(); ++iP) {
315
316 reco::PFCandidatePtr thePtr(hPfCandProduct, iP - inPfCands.begin());
317
318 //mithep::PFCandidate *outPfCand = pfCandMap_->GetMit(thePtr);
319 // fill mother-daughter links
320 // const reco::CandidatePtr motherCandPtr = iP->sourceCandidatePtr(0);
321 // const reco::PFCandidatePtr motherPtr(motherCandPtr);
322 // if (motherCandPtr.isNonnull()) {
323 // mithep::PFCandidate *mother = pfCandMap_->GetMit(motherPtr);
324 // outPfCand->SetMother(mother);
325 // mother->AddDaughter(outPfCand);
326 // }
327 }
328 }
329
330 //--------------------------------------------------------------------------------------------------
331 const mithep::Track *FillerPFCandidates::getMitTrack(mitedm::TrackPtr ptr, bool allowmissing) const
332 {
333 // Return our particle referenced by the edm pointer.
334
335 mithep::Track *mitPart = 0;
336 for (std::vector<const mithep::TrackMap*>::const_iterator bmap = trackerTrackMaps_.begin();
337 bmap!=trackerTrackMaps_.end(); ++bmap) {
338 const mithep::TrackMap *theMap = *bmap;
339 if (theMap->HasMit(ptr)) {
340 mitPart = theMap->GetMit(ptr);
341 return mitPart;
342 }
343 }
344
345 if (!mitPart && !allowmissing)
346 throw edm::Exception(edm::errors::Configuration, "FillerPFCandidates::FillDataBlock()\n")
347 << "Error! MITHEP Object "
348 << "not found in AssociationMaps (" << typeid(*this).name() << ")." << std::endl;
349
350 return mitPart;
351 }