CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ECAL2DPositionCalcWithDepthCorr.cc
Go to the documentation of this file.
2 
5 
6 #include <cmath>
7 #include <unordered_map>
8 
12 
13 #include "vdt/vdtMath.h"
14 
15 // faithful reimplementation of RecoEcal/EgammaCoreTools PositionCalc
16 // sorry Stefano
17 
19 update(const edm::EventSetup& es) {
20 
21  const CaloGeometryRecord& caloGeom = es.get<CaloGeometryRecord>();
23  caloGeom.get(geohandle);
24  _ebGeom = geohandle->getSubdetectorGeometry(DetId::Ecal,EcalBarrel);
25  _eeGeom = geohandle->getSubdetectorGeometry(DetId::Ecal,EcalEndcap);
26  _esGeom = geohandle->getSubdetectorGeometry(DetId::Ecal,EcalPreshower);
27  // ripped from RecoEcal/EgammaCoreTools
28  for( uint32_t ic = 0;
29  ic < _esGeom->getValidDetIds().size() &&
30  ( !_esPlus || !_esMinus ); ++ic ) {
31  const double z = _esGeom->getGeometry( _esGeom->getValidDetIds()[ic] )->getPosition().z();
32  _esPlus = _esPlus || ( 0 < z ) ;
33  _esMinus = _esMinus || ( 0 > z ) ;
34  }
35 
36 }
37 
41 }
42 
45  for( reco::PFCluster& cluster : clusters ) {
47  }
48 }
49 
52  constexpr double preshowerStartEta = 1.653;
53  constexpr double preshowerEndEta = 2.6;
54  if( !cluster.seed() ) {
55  throw cms::Exception("ClusterWithNoSeed")
56  << " Found a cluster with no seed: " << cluster;
57  }
58  double cl_energy = 0;
59  double cl_energy_float = 0;
60  double cl_time = 0;
61  double cl_timeweight=0.0;
62  double max_e = 0.0;
63  double clusterT0 = 0.0;
64  PFLayer::Layer max_e_layer = PFLayer::NONE;
65  reco::PFRecHitRef refmax;
66  // find the seed and max layer
67  for( const reco::PFRecHitFraction& rhf : cluster.recHitFractions() ) {
68  const reco::PFRecHitRef& refhit = rhf.recHitRef();
69  const double rh_fraction = rhf.fraction();
70  const double rh_rawenergy = refhit->energy();
71  const double rh_energy = rh_rawenergy * rh_fraction;
72  const double rh_energyf = ((float)rh_rawenergy) * ((float) rh_fraction);
73  if( !edm::isFinite(rh_energy) ) {
74  throw cms::Exception("PFClusterAlgo")
75  <<"rechit " << refhit->detId() << " has non-finite energy... "
76  << "The input of the particle flow clustering seems to be corrupted.";
77  }
78  cl_energy += rh_energy;
79  cl_energy_float += rh_energyf;
80  // If time resolution is given, calculate weighted average
81  if (_timeResolutionCalc) {
82  const double res2 = _timeResolutionCalc->timeResolution2(rh_rawenergy);
83  cl_time += rh_fraction*refhit->time()/res2;
84  cl_timeweight += rh_fraction/res2;
85  }
86  else { // assume resolution ~ 1/E**2
87  const double rh_rawenergy2 = rh_rawenergy*rh_rawenergy;
88  cl_timeweight+=rh_rawenergy2*rh_fraction;
89  cl_time += rh_rawenergy2*rh_fraction*refhit->time();
90  }
91  if( rh_energy > max_e ) {
92  max_e = rh_energy;
93  max_e_layer = rhf.recHitRef()->layer();
94  refmax = refhit;
95  }
96  }
97  cluster.setEnergy(cl_energy);
98  cluster.setTime(cl_time/cl_timeweight);
99  cluster.setLayer(max_e_layer);
100  const CaloSubdetectorGeometry* ecal_geom = NULL;
101  // get seed geometry information
102  switch(max_e_layer){
104  ecal_geom = _ebGeom;
105  clusterT0 = _param_T0_EB;
106  break;
108  ecal_geom = _eeGeom;
109  clusterT0 = _param_T0_EE;
110  break;
111  default:
112  throw cms::Exception("InvalidLayer")
113  << "ECAL Position Calc only accepts ECAL_BARREL or ECAL_ENDCAP";
114  }
115 
116  const CaloCellGeometry* center_cell =
117  ecal_geom->getGeometry(refmax->detId());
118  const double ctreta = center_cell->getPosition().eta();
119  const double actreta = std::abs(ctreta);
120  // need to change T0 if in ES
121  if( actreta > preshowerStartEta && actreta < preshowerEndEta ) {
122  if(ctreta > 0 && _esPlus ) clusterT0 = _param_T0_ES;
123  if(ctreta < 0 && _esMinus) clusterT0 = _param_T0_ES;
124  }
125  // floats to reproduce exactly the EGM code
126  const float maxDepth = _param_X0*(clusterT0 + vdt::fast_log(cl_energy_float));
127  const float maxToFront = center_cell->getPosition().mag();
128  // calculate the position
129  const double logETot_inv = -vdt::fast_log(cl_energy_float);
130  double position_norm = 0.0;
131  double x(0.0),y(0.0),z(0.0);
132  for( const reco::PFRecHitFraction& rhf : cluster.recHitFractions() ) {
133  double weight = 0.0;
134  const reco::PFRecHitRef& refhit = rhf.recHitRef();
135  const double rh_energy = ((float)refhit->energy()) * ((float)rhf.fraction());
136  if( rh_energy > 0.0 ) weight = std::max(0.0,( _param_W0 +
137  vdt::fast_log(rh_energy) +
138  logETot_inv ));
139  const CaloCellGeometry* cell = ecal_geom->getGeometry(refhit->detId());
140  const float depth = maxDepth + maxToFront - cell->getPosition().mag();
141  const GlobalPoint pos =
142  static_cast<const TruncatedPyramid*>(cell)->getPosition(depth);
143 
144  x += weight*pos.x() ;
145  y += weight*pos.y() ;
146  z += weight*pos.z() ;
147 
148  position_norm += weight ;
149  }
150 
151  // FALL BACK to LINEAR WEIGHTS
152  if (position_norm == 0.) {
153  for( const reco::PFRecHitFraction& rhf : cluster.recHitFractions() ) {
154  double weight = 0.0;
155  const reco::PFRecHitRef& refhit = rhf.recHitRef();
156  const double rh_energy = ((float)refhit->energy()) * ((float)rhf.fraction());
157  if( rh_energy > 0.0 )
158  weight = rh_energy/cluster.energy();
159 
160  const CaloCellGeometry* cell = ecal_geom->getGeometry(refhit->detId());
161  const float depth = maxDepth + maxToFront - cell->getPosition().mag();
162  const GlobalPoint pos = static_cast<const TruncatedPyramid*>(cell)->getPosition(depth);
163 
164  x += weight*pos.x() ;
165  y += weight*pos.y() ;
166  z += weight*pos.z() ;
167 
168  position_norm += weight ;
169  }
170  }
171 
172  if( position_norm < _minAllowedNorm ) {
173  edm::LogError("WeirdClusterNormalization")
174  << "PFCluster too far from seeding cell: set position to (0,0,0).";
175  cluster.setPosition(math::XYZPoint(0,0,0));
176  } else {
177  const double norm_inverse = 1.0/position_norm;
178  x *= norm_inverse;
179  y *= norm_inverse;
180  z *= norm_inverse;
181 
182  cluster.setPosition(math::XYZPoint(x,y,z));
183  cluster.calculatePositionREP();
184  }
185 }
void setLayer(PFLayer::Layer layer)
set layer
Definition: PFCluster.cc:78
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:47
void setPosition(const math::XYZPoint &p)
Definition: CaloCluster.h:112
T y() const
Definition: PV3DBase.h:63
Fraction of a PFRecHit (rechits can be shared between several PFCluster&#39;s)
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalc
void setEnergy(double energy)
Definition: CaloCluster.h:108
#define NULL
Definition: scimark2.h:8
#define constexpr
void calculateAndSetPositionActual(reco::PFCluster &) const
virtual const std::vector< DetId > & getValidDetIds(DetId::Detector det=DetId::Detector(0), int subdet=0) const
Get a list of valid detector ids (for the given subdetector)
virtual const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
bool isFinite(T x)
T mag() const
Definition: PV3DBase.h:67
void calculateAndSetPositions(reco::PFClusterCollection &)
void setTime(double time)
Definition: PFCluster.h:89
void calculatePositionREP()
computes posrep_ once and for all
Definition: PFCluster.h:96
T z() const
Definition: PV3DBase.h:64
void get(HolderT &iHolder) const
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double energy() const
cluster energy
Definition: PFCluster.h:82
Layer
layer definition
Definition: PFLayer.h:31
DetId seed() const
return DetId of seed
Definition: CaloCluster.h:202
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
const T & get() const
Definition: EventSetup.h:56
const CaloSubdetectorGeometry * _ebGeom
const CaloSubdetectorGeometry * _esGeom
A base class to handle the particular shape of Ecal Xtals. Taken from ORCA Calorimetry Code...
T eta() const
Definition: PV3DBase.h:76
const CaloSubdetectorGeometry * _eeGeom
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
const std::vector< reco::PFRecHitFraction > & recHitFractions() const
vector of rechit fractions
Definition: PFCluster.h:72
const GlobalPoint & getPosition() const
Returns the position of reference for this cell.
T x() const
Definition: PV3DBase.h:62