CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFClusterWidthAlgo.cc
Go to the documentation of this file.
6 #include "TMath.h"
7 #include <algorithm>
8 using namespace std;
9 using namespace reco;
10 
11 
12 
13 PFClusterWidthAlgo::PFClusterWidthAlgo(const std::vector<const reco::PFCluster *>& pfclust,
14  const EBRecHitCollection * ebRecHits,
15  const EERecHitCollection * eeRecHits){
16 
17 
18  double numeratorEtaWidth = 0.;
19  double numeratorPhiWidth = 0.;
20  double sclusterE = 0.;
21  double posX = 0.;
22  double posY = 0.;
23  double posZ = 0.;
24  sigmaEtaEta_ = 0.;
25 
26  unsigned int nclust= pfclust.size();
27  if(nclust == 0 ) {
28  etaWidth_ = 0.;
29  phiWidth_ = 0.;
30  sigmaEtaEta_ = 0.;
31  }
32  else {
33 
34  for(unsigned int icl=0;icl<nclust;++icl) {
35  double e = pfclust[icl]->energy();
36  sclusterE += e;
37  posX += e * pfclust[icl]->position().X();
38  posY += e * pfclust[icl]->position().Y();
39  posZ += e * pfclust[icl]->position().Z();
40  }
41 
42  posX /=sclusterE;
43  posY /=sclusterE;
44  posZ /=sclusterE;
45 
46  double denominator = sclusterE;
47 
48  math::XYZPoint pflowSCPos(posX,posY,posZ);
49 
50  double scEta = pflowSCPos.eta();
51  double scPhi = pflowSCPos.phi();
52 
53  double SeedClusEnergy = -1.;
54  unsigned int SeedDetID = 0;
55  double SeedEta = -1.;
56  double SeedPhi = -1.;
57 
58  for(unsigned int icl=0; icl<nclust; ++icl) {
59  const std::vector< reco::PFRecHitFraction >& PFRecHits = pfclust[icl]->recHitFractions();
60 
61 
62  for ( std::vector< reco::PFRecHitFraction >::const_iterator it = PFRecHits.begin();
63  it != PFRecHits.end(); ++it) {
64  const PFRecHitRef& RefPFRecHit = it->recHitRef();
65  double energyHit=0;
66  // if the PFRecHit is not available, try to get it from the collections
67  // if(!RefPFRecHit.isAvailable() && ebRecHits && eeRecHits )
68  if(ebRecHits && eeRecHits )
69  {
70  // std::cout << " Recomputing " << std::endl;
71  unsigned index=it-PFRecHits.begin();
72  DetId id=pfclust[icl]->hitsAndFractions()[index].first;
73  // look for the hit; do not forget to multiply by the fraction
74  if(id.det()==DetId::Ecal && id.subdetId()==EcalBarrel)
75  {
76  EBRecHitCollection::const_iterator itcheck=ebRecHits->find(id);
77  if(itcheck!=ebRecHits->end())
78  energyHit= itcheck->energy();
79  // The cluster shapes do not take into account the RecHit fraction !
80  // * pfclust[icl]->hitsAndFractions()[index].second;
81  }
82  if(id.det()==DetId::Ecal && id.subdetId()==EcalEndcap)
83  {
84  EERecHitCollection::const_iterator itcheck=eeRecHits->find(id);
85  if(itcheck!=eeRecHits->end())
86  energyHit= itcheck->energy();
87  // The cluster shapes do not take into account the RecHit fraction !
88  // * pfclust[icl]->hitsAndFractions()[index].second;
89  }
90  }
91  else
92  energyHit = RefPFRecHit->energy();
93 
94  //only for the first cluster (from GSF) find the seed
95  if(icl==0) {
96  if (energyHit > SeedClusEnergy) {
97  SeedClusEnergy = energyHit;
98  SeedEta = RefPFRecHit->position().eta();
99  SeedPhi = RefPFRecHit->position().phi();
100  SeedDetID = RefPFRecHit->detId();
101  }
102  }
103 
104 
105  double dPhi = RefPFRecHit->position().phi() - scPhi;
106  if (dPhi > + TMath::Pi()) { dPhi = TMath::TwoPi() - dPhi; }
107  if (dPhi < - TMath::Pi()) { dPhi = TMath::TwoPi() + dPhi; }
108  double dEta = RefPFRecHit->position().eta() - scEta;
109  if ( energyHit > 0 ) {
110  numeratorEtaWidth += energyHit * dEta * dEta;
111  numeratorPhiWidth += energyHit * dPhi * dPhi;
112  }
113  }
114  } // end for ncluster
115 
116  //for the first cluster (from GSF) computed sigmaEtaEta
117  const std::vector< reco::PFRecHitFraction >& PFRecHits = pfclust[0]->recHitFractions();
118  for ( std::vector< reco::PFRecHitFraction >::const_iterator it = PFRecHits.begin();
119  it != PFRecHits.end(); ++it) {
120  const PFRecHitRef& RefPFRecHit = it->recHitRef();
121  if(!RefPFRecHit.isAvailable())
122  return;
123  double energyHit = RefPFRecHit->energy();
124  if (RefPFRecHit->detId() != SeedDetID) {
125  float diffEta = RefPFRecHit->position().eta() - SeedEta;
126  sigmaEtaEta_ += (diffEta*diffEta) * (energyHit/SeedClusEnergy);
127  }
128  }
129  if (sigmaEtaEta_ == 0.) sigmaEtaEta_ = 0.00000001;
130 
131  etaWidth_ = sqrt(numeratorEtaWidth / denominator);
132  phiWidth_ = sqrt(numeratorPhiWidth / denominator);
133 
134 
135  } // endif ncluster > 0
136 }
138 {
139 }
const double TwoPi
const double Pi
std::vector< T >::const_iterator const_iterator
bool isAvailable() const
Definition: Ref.h:275
double dPhi(double phi1, double phi2)
Definition: JetUtil.h:30
T sqrt(T t)
Definition: SSEVec.h:28
PFClusterWidthAlgo(const std::vector< const reco::PFCluster * > &pfclust, const EBRecHitCollection *ebRecHits=0, const EERecHitCollection *eeRecHits=0)
const_iterator end() const
Definition: DetId.h:20
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:13
iterator find(key_type k)