ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Morgan/src/PhotonAnalyzer.cc
Revision: 1.10
Committed: Fri Mar 13 15:49:38 2009 UTC (16 years, 1 month ago) by lethuill
Content type: text/plain
Branch: MAIN
CVS Tags: pat_2_2_5_03, pat_2_2_5_02
Changes since 1.9: +198 -43 lines
Log Message:
Migrating Photons to PAT... 1st iteration

File Contents

# User Rev Content
1 lethuill 1.5 #include "../interface/PhotonAnalyzer.h"
2 mlethuil 1.1
3     using namespace std;
4     using namespace reco;
5     using namespace edm;
6    
7 lethuill 1.9 PhotonAnalyzer::PhotonAnalyzer(const edm::ParameterSet& producersNames):verbosity_(0), iconvtrack_(0), doPhotonConversion_(true), doVertexCorrection_(true), useMC_(false)
8 mlethuil 1.1 {
9 lethuill 1.10 dataType_ = producersNames.getUntrackedParameter<string>("dataType","unknown");
10     photonProducer_ = producersNames.getParameter<edm::InputTag>("photonProducer");
11 lethuill 1.2 photonIDProducer_ = producersNames.getParameter<edm::InputTag>("photonIDProducer");
12 mlethuil 1.1 }
13 lethuill 1.2
14 lethuill 1.10 PhotonAnalyzer::PhotonAnalyzer(const edm::ParameterSet& producersNames, const edm::ParameterSet& myConfig, int verbosity):verbosity_(verbosity), iconvtrack_(0)
15 lethuill 1.2 {
16 lethuill 1.6 dataType_ = producersNames.getUntrackedParameter<string>("dataType","unknown");
17 lethuill 1.10 photonProducer_ = producersNames.getParameter<edm::InputTag>("photonProducer");
18 lethuill 1.2 photonIDProducer_ = producersNames.getParameter<edm::InputTag>("photonIDProducer");
19 lethuill 1.9 doPhotonConversion_ = myConfig.getUntrackedParameter<bool>("doPhotonConversion");
20     doVertexCorrection_ = myConfig.getUntrackedParameter<bool>("doPhotonVertexCorrection");
21     useMC_ = myConfig.getUntrackedParameter<bool>("doMuonMC");
22 lethuill 1.2 }
23    
24 mlethuil 1.1 PhotonAnalyzer::~PhotonAnalyzer()
25     {
26     }
27    
28 lethuill 1.10 void PhotonAnalyzer::Process(const edm::Event& iEvent, const edm::EventSetup& iSetup, TRootEvent* rootEvent, TClonesArray* rootPhotons, TClonesArray* conversionTracks, ConversionLikelihoodCalculator* convLikelihoodCalculator, EcalClusterLazyTools& lazyTools)
29 mlethuil 1.1 {
30    
31 lethuill 1.10 unsigned int nPhotons=0;
32    
33 mlethuil 1.1 edm::Handle< reco::PhotonCollection > recoPhotons;
34 lethuill 1.10 const reco::PhotonIDAssociationCollection *photonIDMap = 0;
35     if( dataType_=="RECO" || dataType_=="AOD" )
36     {
37     iEvent.getByLabel(photonProducer_, recoPhotons);
38     nPhotons = recoPhotons->size();
39     // Photon identification
40     edm::Handle<reco::PhotonIDAssociationCollection> photonIDMapColl;
41     iEvent.getByLabel(photonIDProducer_, photonIDMapColl);
42     photonIDMap = photonIDMapColl.product();
43     }
44    
45     edm::Handle < std::vector <pat::Photon> > patPhotons;
46     if( dataType_=="PAT" || dataType_=="PATAOD" )
47     {
48     iEvent.getByLabel(photonProducer_, patPhotons);
49     nPhotons = patPhotons->size();
50     }
51 lethuill 1.2
52 lethuill 1.10 if(verbosity_>1) std::cout << " Number of photons = " << nPhotons << " Label: " << photonProducer_.label() << " Instance: " << photonProducer_.instance() << std::endl;
53 lethuill 1.2
54    
55 lethuill 1.10 // TODO - add Pi0Disc... not yet implemented in 2.X.X
56 lethuill 1.2 //cout << "Pi0Disc..."<<endl;
57     //edm::Handle<reco::PhotonPi0DiscriminatorAssociationMap> pi0map;
58     //iEvent.getByLabel("piZeroDiscriminators","PhotonPi0DiscriminatorAssociationMap", pi0map);
59     //reco::PhotonPi0DiscriminatorAssociationMap::const_iterator pi0mapIter;
60     //Double_t pi0nn;
61    
62     // Index in TRootPhoton TCloneArray
63 lethuill 1.10 //unsigned int iPhoton = 0; // FIXME - utile ?
64    
65     for (unsigned int j=0; j<nPhotons; j++)
66     {
67     const reco::Photon* photon = 0;
68     if( dataType_=="RECO" || dataType_=="AOD" ) photon = &((*recoPhotons)[j]);
69     if( dataType_=="PAT" || dataType_=="PATAOD" ) photon = (const reco::Photon*) ( & ((*patPhotons)[j]) );
70    
71     TRootPhoton localPhoton(
72     photon->px()
73     ,photon->py()
74     ,photon->pz()
75     ,photon->energy()
76     ,photon->vx()
77     ,photon->vy()
78     ,photon->vz()
79     ,photon->pdgId()
80     ,photon->charge()
81     );
82    
83     // Variables from reco::Photon
84     localPhoton.setCaloPosition( photon->caloPosition().X(), photon->caloPosition().Y(), photon->caloPosition().Z() );
85     localPhoton.setHoE(photon->hadronicOverEm());
86     localPhoton.setHasPixelSeed( photon->hasPixelSeed() );
87    
88     // Variables from reco::SuperCluster
89     reco::SuperClusterRef superCluster = photon->superCluster();
90     //const reco::SuperCluster* sc = superCluster.get();
91     if ( superCluster.isNonnull() )
92     {
93     localPhoton.setSCRawEnergy( superCluster->rawEnergy() );
94     //localPhoton.setNbClusters(electron->numberOfClusters());
95     //localPhoton.setPreshowerEnergy(superCluster->preshowerEnergy());
96     }
97    
98     // Cluster Shape variables
99     // need reco::SuperCluster and reco::BasicCluster
100     if ( superCluster.isNonnull() )
101     {
102     reco::BasicClusterRef seedBasicCluster = superCluster->seed();
103     //if ( seedBasicCluster.isNonnull() ) localPhoton.setClusterAlgo(seedBasicCluster->algo());
104    
105     /*
106     // dR of the cone centered on the reco::GsfElectron and containing all its basic clusters constituents
107     Float_t caloConeSize = 0;
108     for (reco::basicCluster_iterator basicCluster = (*superCluster).clustersBegin(); basicCluster != (*superCluster).clustersEnd(); ++basicCluster )
109     {
110     Float_t dR = localElectron.DeltaR(TLorentzVector( (*basicCluster)->position().x(), (*basicCluster)->position().y(), (*basicCluster)->position().z(), 0. ) );
111     if (dR > caloConeSize) caloConeSize = dR;
112     }
113     // FIXME - if no BasicCluster collection, init to -999.
114     localElectron.setCaloConeSize(caloConeSize);
115     */
116    
117     // need reduced Ecal RecHits Collections for EcalClusterLazyTools
118     // FIXME - check lazyTools is initialized ==> lazyTools has to be a pointer
119     if ( seedBasicCluster.isNonnull() )
120     {
121     //localPhoton.setE2x2(lazyTools.e2x2(*seedBasicCluster));
122     localPhoton.setE3x3( lazyTools.e3x3(*seedBasicCluster) );
123     localPhoton.setE5x5( lazyTools.e5x5(*seedBasicCluster) );
124     localPhoton.setEmax( lazyTools.eMax(*seedBasicCluster) );
125     }
126     }
127    
128     if( dataType_=="RECO" || dataType_=="AOD" )
129     {
130     // Some specific methods requiring RECO / AOD format
131     // Do association to genParticle ?
132    
133     // Photon ID
134     edm::Ref<reco::PhotonCollection> photonRef(recoPhotons, j);
135     reco::PhotonIDAssociationCollection::const_iterator photonIter = photonIDMap->find(photonRef);
136     const reco::PhotonIDRef &photonID = photonIter->val;
137     //const reco::PhotonRef &photon = photonIter->key;
138     localPhoton.setBitsID(
139     photonID->isLooseEM()
140     ,photonID->isLoosePhoton()
141     ,photonID->isTightPhoton()
142     ,photonID->isEBPho()
143     ,photonID->isEEPho()
144     ,photonID->isEBGap()
145     ,photonID->isEEGap()
146     ,photonID->isEBEEGap()
147     ,photonID->isAlsoElectron()
148     );
149    
150     /*
151     if(verbosity_>4) std::cout << "seed E5x5=" << lazyTools.e5x5( *seedBasicCluster )
152     //<< " pi0NN=" << pi0nn
153     << " photonID->isolationEcalRecHit()=" << photonID->isolationEcalRecHit()
154     << " photonID->r9()=" << photonID->r9()
155     << " e3x3 / SC->energy()=" << ( lazyTools.e3x3( *seedBasicCluster ) / photon->superCluster()->energy() )
156     << " e3x3 / SC->rawEnergy()=" << ( lazyTools.e3x3( *seedBasicCluster ) / photon->superCluster()->rawEnergy() )
157     << " sc->pos=" << photon->superCluster()->position().X() << " , " << photon->superCluster()->position().Y() << " , " << photon->superCluster()->position().Z()
158     << " photon->caloPos=" << photon->caloPosition().X() << " , " << photon->caloPosition().Y() << " , " << photon->caloPosition().Z() << std::endl;
159     */
160     }
161    
162    
163     if( dataType_=="PATAOD" || dataType_=="PAT" )
164     {
165     // Some specific methods to pat::Photon
166     const pat::Photon *patPhoton = dynamic_cast<const pat::Photon*>(&*photon);
167    
168     // Photon ID
169     localPhoton.setBitsID(
170     patPhoton->isLooseEM()
171     ,patPhoton->isLoosePhoton()
172     ,patPhoton->isTightPhoton()
173     ,patPhoton->isEBPho()
174     ,patPhoton->isEEPho()
175     ,patPhoton->isEBGap()
176     ,patPhoton->isEEGap()
177     ,patPhoton->isEBEEGap()
178     ,patPhoton->isAlsoElectron()
179     );
180    
181     // Isolation ?
182     /*
183     pair < Float_t, Int_t > trackerIso;
184     trackerIso = patElectron->trackerIsoDeposit()->depositAndCountWithin(0.1);
185     localElectron.setIsoR01_sumPt(trackerIso.first);
186     localElectron.setIsoR01_nTracks(trackerIso.second);
187     */
188    
189    
190     if(useMC_)
191     {
192     // MC truth associator index
193     if ((patPhoton->genParticleRef()).isNonnull()) {
194     localPhoton.setGenParticleIndex((patPhoton->genParticleRef()).index());
195     } else {
196     localPhoton.setGenParticleIndex(-1);
197     }
198     }
199     }
200    
201     // Conversions
202    
203     // FIXME - associator supercluster <-> photon
204     //localElectron.setSCRef(superCluster->toto());
205    
206    
207    
208     // Vertex correction to photon - Assume photon is coming from primary vertex
209     if( doVertexCorrection_)
210     {
211     if(verbosity_>3) cout << " Before vertex correction ["<< setw(3) << j << "] " << localPhoton << endl;
212     // FIXME - Choose best vertex... Ht max for instance...
213     if( rootEvent->nPrimaryVertices()>0 )
214     {
215     TVector3 vertex( rootEvent->primaryVertex_x(), rootEvent->primaryVertex_y(), rootEvent->primaryVertex_z() );
216     localPhoton.setVertex(vertex);
217     }
218     else
219     {
220     cout << " PhotonAnalyzer - NO PRIMARY VERTEX FOUND !!!" << endl;
221     }
222     }
223    
224    
225     // Stock new TRootPhoton in TCloneArray
226     new( (*rootPhotons)[j] ) TRootPhoton(localPhoton);
227     //if(verbosity_>2) cout << " ["<< setw(3) << j << "] " << localPhoton << endl;
228     }
229     }
230    
231    
232     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233     /*
234 lethuill 1.2
235     for (unsigned int iphot=0; iphot<recoPhotons->size(); iphot++)
236 mlethuil 1.1 {
237 lethuill 1.9
238 lethuill 1.2 // TODO - add Pi0Disc... not yet implemented in 2.1.X
239     //pi0mapIter = pi0map->find(edm::Ref<reco::PhotonCollection>(recoPhotons,iPhoton));
240     //pi0nn = ( pi0mapIter == pi0map->end() ? -1 : pi0mapIter->val );
241    
242    
243     // TODO - add Pi0Disc... not yet implemented in 2.1.X
244     //localPhoton.setPi0nn( pi0nn );
245    
246 lethuill 1.9
247 lethuill 1.2
248 lethuill 1.9
249 lethuill 1.2 if( doPhotonConversion_ )
250 mlethuil 1.1 {
251 lethuill 1.2 // If more than one associated convertedPhotons is found, then
252     // if nTracks=2, select the convertedPhoton with greatest Likelihood value using Ted's weights file
253     // if nTracks=1, select the convertedPhoton with E/p closest to 1
254     double eoverp;
255     double best_eoverp = 999999.;
256     double likely;
257     double best_likely = -1.;
258     int best_iconv = -1;
259     int best_iconv_likely = -1;
260     int best_iconv_eoverp = -1;
261    
262     std::vector<reco::ConversionRef> conversions = photon->conversions();
263     for (unsigned int iconv=0; iconv<conversions.size(); iconv++)
264     {
265     if(verbosity_>4) cout << " ["<< setw(3) << iconv << "] Conversion - "
266     << " conv_vertex (x,y,z)=(" << conversions[iconv]->conversionVertex().x()
267     << "," << conversions[iconv]->conversionVertex().y()
268     << "," << conversions[iconv]->conversionVertex().z()
269     << ") isConverted=" << conversions[iconv]->isConverted()
270     << " nTracks=" << conversions[iconv]->nTracks()
271     << " primary_vtx_z=" << conversions[iconv]->zOfPrimaryVertexFromTracks()
272     << " inv_mass=" << conversions[iconv]->pairInvariantMass()
273     << " E/p=" << conversions[iconv]->EoverP()
274     << " cotanTheta=" << conversions[iconv]->pairCotThetaSeparation()
275     << " likely=" << convLikelihoodCalculator->calculateLikelihood(conversions[iconv])
276     << endl;
277    
278     likely = convLikelihoodCalculator->calculateLikelihood(conversions[iconv]);
279     if ( likely>best_likely )
280     {
281     best_iconv_likely = iconv;
282     best_likely = likely;
283     }
284 lethuill 1.9
285 lethuill 1.2 eoverp = conversions[iconv]->EoverP();
286     if ( abs(eoverp-1)<abs(best_eoverp-1) )
287     {
288     best_iconv_eoverp = iconv;
289     best_eoverp = eoverp;
290     }
291     }
292 lethuill 1.9
293 lethuill 1.2 best_iconv = (best_iconv_likely==-1 ? best_iconv_eoverp : best_iconv_likely);
294    
295     // Update Photon object with conversion infos
296     if ( best_iconv != -1 )
297     {
298     if(verbosity_>4) cout
299     << "Photon[" << iphot << "] associated to Conversion[" << best_iconv
300     << "] with E/p=" << conversions[best_iconv]->EoverP()
301     << " likely=" << convLikelihoodCalculator->calculateLikelihood(conversions[best_iconv])
302     << endl;
303    
304     localPhoton.setConvNTracks( conversions[best_iconv]->nTracks() );
305     localPhoton.setConvEoverP( conversions[best_iconv]->EoverP() );
306     localPhoton.setConvMass( conversions[best_iconv]->pairInvariantMass() );
307     localPhoton.setConvCotanTheta( conversions[best_iconv]->pairCotThetaSeparation() );
308     localPhoton.setConvLikely( convLikelihoodCalculator->calculateLikelihood(conversions[best_iconv]) );
309     localPhoton.setConvVertex( conversions[best_iconv]->conversionVertex().x(), conversions[best_iconv]->conversionVertex().y(), conversions[best_iconv]->conversionVertex().z() );
310     std::vector<math::XYZPoint> impactVector = conversions[best_iconv]->ecalImpactPosition();
311     if ( impactVector.size()>0 ) localPhoton.setConvEcalImpactPosition1( impactVector.at(0).x(), impactVector.at(0).y(), impactVector.at(0).z() );
312     if ( impactVector.size()>1 ) localPhoton.setConvEcalImpactPosition2( impactVector.at(1).x(), impactVector.at(1).y(), impactVector.at(1).z() );
313    
314     if ( conversions[best_iconv]->nTracks()>0 )
315     {
316     std::vector<reco::TrackRef> tracks = conversions[best_iconv]->tracks();
317     reco::TrackRef tk1 = tracks.at(0);
318     const reco::HitPattern& hit1 = tk1->hitPattern();
319     new( (*conversionTracks)[iconvtrack_] ) TRootTrack( tk1->px(), tk1->py(), tk1->pz(), tk1->p(), tk1->vx(), tk1->vy(), tk1->vz(), 0, tk1->charge()
320     ,hit1.numberOfValidPixelHits(), hit1.numberOfValidTrackerHits(), tk1->chi2(), tk1->d0(), tk1->d0Error(), tk1->dz(), tk1->dzError() );
321     localPhoton.setConvIndexTrack1(iconvtrack_);
322 lethuill 1.3 localPhoton.setConvTrack1((*conversionTracks)[iconvtrack_]);
323 lethuill 1.2 iconvtrack_++;
324    
325     if ( conversions[best_iconv]->nTracks()>1 )
326     {
327     reco::TrackRef tk2 = tracks.at(1);
328     const reco::HitPattern& hit2 = tk2->hitPattern();
329     new( (*conversionTracks)[iconvtrack_] ) TRootTrack( tk2->px(), tk2->py(), tk2->pz(), tk2->p(), tk2->vx(), tk2->vy(), tk2->vz(), 0, tk2->charge()
330     ,hit2.numberOfValidPixelHits(), hit2.numberOfValidTrackerHits(), tk2->chi2(), tk2->d0(), tk2->d0Error(), tk2->dz(), tk2->dzError() );
331     localPhoton.setConvIndexTrack2(iconvtrack_);
332 lethuill 1.3 localPhoton.setConvTrack2((*conversionTracks)[iconvtrack_]);
333 lethuill 1.2 iconvtrack_++;
334     }
335     }
336     }
337 mlethuil 1.1 }
338 lethuill 1.2
339     if(verbosity_>3) cout << " ["<< setw(3) << iPhoton << "] " << localPhoton << endl;
340    
341     // Vertex correction to photon - Assume photon is coming from primary vertex
342     if( doVertexCorrection_)
343 mlethuil 1.1 {
344 lethuill 1.2 if( rootEvent->nPrimaryVertices()>0 )
345     {
346     TVector3 vertex( rootEvent->primaryVertex_x(), rootEvent->primaryVertex_y(), rootEvent->primaryVertex_z() );
347     localPhoton.setVertex(vertex);
348     if(verbosity_>3) cout << " after vtx correction - ["<< setw(3) << iPhoton << "] " << localPhoton << endl;
349     }
350     else
351     {
352     cout << " PhotonAnalyzer - NO PRIMARY VERTEX FOUND !!!" << endl;
353     }
354 mlethuil 1.1 }
355 lethuill 1.6
356 lethuill 1.9
357 lethuill 1.2 // Stock new TRootPhoton in TCloneArray
358     new( (*rootPhotons)[iPhoton] ) TRootPhoton(localPhoton);
359     iPhoton++;
360 mlethuil 1.1 }
361     }
362 lethuill 1.10 */