ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MitProd/TreeFiller/src/FillerPFCandidates.cc
Revision: 1.17
Committed: Wed Jul 25 03:08:42 2012 UTC (12 years, 9 months ago) by paus
Content type: text/plain
Branch: MAIN
CVS Tags: Mit_029, Mit_029_pre1
Changes since 1.16: +73 -22 lines
Log Message:
Preparing for version 029.

File Contents

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