CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Basic2DGenericPFlowPositionCalc.cc
Go to the documentation of this file.
2 
5 
6 #include <cmath>
8 #include<iterator>
9 #include <boost/function_output_iterator.hpp>
10 
11 #include "vdt/vdtMath.h"
12 
13 
14 namespace {
15  inline
16  bool isBarrel(int cell_layer){
17  return (cell_layer == PFLayer::HCAL_BARREL1 ||
18  cell_layer == PFLayer::HCAL_BARREL2 ||
19  cell_layer == PFLayer::ECAL_BARREL);
20  }
21 }
22 
26 }
27 
30  for( reco::PFCluster& cluster : clusters ) {
32  }
33 }
34 
37  if( !cluster.seed() ) {
38  throw cms::Exception("ClusterWithNoSeed")
39  << " Found a cluster with no seed: " << cluster;
40  }
41  double cl_energy = 0;
42  double cl_time = 0;
43  double cl_timeweight=0.0;
44  double max_e = 0.0;
45  PFLayer::Layer max_e_layer = PFLayer::NONE;
46  // find the seed and max layer and also calculate time
47  //Michalis : Even if we dont use timing in clustering here we should fill
48  //the time information for the cluster. This should use the timing resolution(1/E)
49  //so the weight should be fraction*E^2
50  //calculate a simplistic depth now. The log weighted will be done
51  //in different stage
52 
53 
54  auto const recHitCollection = &(*cluster.recHitFractions()[0].recHitRef()) - cluster.recHitFractions()[0].recHitRef().key();
55  auto nhits = cluster.recHitFractions().size();
56  struct LHit{ reco::PFRecHit const * hit; float energy; float fraction;};
57  declareDynArray(LHit,nhits,hits);
58  for(auto i=0U; i<nhits; ++i) {
59  auto const & hf = cluster.recHitFractions()[i];
60  auto k = hf.recHitRef().key();
61  auto p = recHitCollection+k;
62  hits[i]= {p,(*p).energy(), float(hf.fraction())};
63  }
64 
65 
66  bool resGiven = bool(_timeResolutionCalcBarrel) & bool(_timeResolutionCalcEndcap);
67  LHit mySeed={nullptr};
68  for( auto const & rhf : hits ) {
69  const reco::PFRecHit & refhit = *rhf.hit;
70  if( refhit.detId() == cluster.seed() ) mySeed = rhf;
71  const auto rh_fraction = rhf.fraction;
72  const auto rh_rawenergy = rhf.energy;
73  const auto rh_energy = rh_rawenergy * rh_fraction;
74 #ifdef PF_DEBUG
75  if unlikely( edm::isNotFinite(rh_energy) ) {
76  throw cms::Exception("PFClusterAlgo")
77  <<"rechit " << refhit.detId() << " has a NaN energy... "
78  << "The input of the particle flow clustering seems to be corrupted.";
79  }
80 #endif
81  cl_energy += rh_energy;
82  // If time resolution is given, calculated weighted average
83  if ( resGiven ) {
84  double res2 = 1.e-4;
85  int cell_layer = (int)refhit.layer();
86  res2 = isBarrel(cell_layer) ? 1./_timeResolutionCalcBarrel->timeResolution2(rh_rawenergy) :
87  1./_timeResolutionCalcEndcap->timeResolution2(rh_rawenergy);
88  cl_time += rh_fraction*refhit.time()*res2;
89  cl_timeweight += rh_fraction*res2;
90  }
91  else { // assume resolution = 1/E**2
92  const double rh_rawenergy2 = rh_rawenergy*rh_rawenergy;
93  cl_timeweight+=rh_rawenergy2*rh_fraction;
94  cl_time += rh_rawenergy2*rh_fraction*refhit.time();
95  }
96 
97  if( rh_energy > max_e ) {
98  max_e = rh_energy;
99  max_e_layer = refhit.layer();
100  }
101  }
102 
103  cluster.setEnergy(cl_energy);
104  cluster.setTime(cl_time/cl_timeweight);
105  cluster.setLayer(max_e_layer);
106 
107  // calculate the position
108  double depth = 0.0;
109  double position_norm = 0.0;
110  double x(0.0),y(0.0),z(0.0);
111  auto seedNeighbours = mySeed.hit->neighbours();
112  switch( _posCalcNCrystals ) {
113  case 5:
114  seedNeighbours = mySeed.hit->neighbours4();
115  break;
116  case 9:
117  seedNeighbours = mySeed.hit->neighbours8();
118  break;
119  default:
120  break;
121  }
122 
123  auto compute = [&](LHit const& rhf) {
124  const reco::PFRecHit & refhit = *rhf.hit;
125  const auto rh_energy = rhf.energy * rhf.fraction;
126  const auto norm = ( rhf.fraction < _minFractionInCalc ?
127  0.0f :
128  std::max(0.0f,vdt::fast_logf(rh_energy*_logWeightDenom)) );
129  const auto rhpos_xyz = refhit.position()*norm;
130  x += rhpos_xyz.x();
131  y += rhpos_xyz.y();
132  z += rhpos_xyz.z();
133  depth += refhit.depth()*norm;
134  position_norm += norm;
135  };
136 
137  if(_posCalcNCrystals != -1) // sorted to make neighbour search faster (maybe)
138  std::sort(hits.begin(),hits.end(),[](LHit const& a, LHit const& b) { return a.hit<b.hit;});
139 
140 
141  if(_posCalcNCrystals == -1)
142  for( auto const & rhf : hits ) compute(rhf);
143  else { // only seed and its neighbours
144  compute(mySeed);
145  // search seedNeighbours to find energy fraction in cluster (sic)
146  unInitDynArray(reco::PFRecHit const *,seedNeighbours.size(),nei);
147  for(auto k : seedNeighbours){
148  nei.push_back(recHitCollection+k);
149  }
150  std::sort(nei.begin(),nei.end());
151  struct LHitLess {
152  auto operator()(LHit const &a, reco::PFRecHit const * b) const {return a.hit<b;}
153  auto operator()(reco::PFRecHit const * b, LHit const &a) const {return b<a.hit;}
154  };
155  std::set_intersection(hits.begin(),hits.end(),nei.begin(),nei.end(),
156  boost::make_function_output_iterator(compute),
157  LHitLess()
158  );
159  }
160 
161 
162  if( position_norm < _minAllowedNorm ) {
163  edm::LogError("WeirdClusterNormalization")
164  << "PFCluster too far from seeding cell: set position to (0,0,0).";
165  cluster.setPosition(math::XYZPoint(0,0,0));
166  cluster.calculatePositionREP();
167  } else {
168  const double norm_inverse = 1.0/position_norm;
169  x *= norm_inverse;
170  y *= norm_inverse;
171  z *= norm_inverse;
172  depth *= norm_inverse;
173  cluster.setPosition(math::XYZPoint(x,y,z));
174  cluster.setDepth(depth);
175  cluster.calculatePositionREP();
176  }
177 }
void setLayer(PFLayer::Layer layer)
set layer
Definition: PFCluster.cc:120
int i
Definition: DBlmapReader.cc:9
void calculateAndSetPositionActual(reco::PFCluster &) const
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:47
float time() const
timing for cleaned hits
Definition: PFRecHit.h:118
unsigned detId() const
rechit detId
Definition: PFRecHit.h:108
void setPosition(const math::XYZPoint &p)
Definition: CaloCluster.h:112
bool isBarrel(GeomDetEnumerators::SubDetector m)
void setEnergy(double energy)
Definition: CaloCluster.h:108
PositionType const & position() const
rechit cell centre x, y, z
Definition: PFRecHit.h:129
int depth() const
depth for segemntation
Definition: PFRecHit.h:121
void calculateAndSetPositions(reco::PFClusterCollection &)
PFLayer::Layer layer() const
rechit layer
Definition: PFRecHit.h:111
#define unlikely(x)
bool isNotFinite(T x)
Definition: isFinite.h:10
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:31
void setTime(double time)
Definition: PFCluster.h:89
void calculatePositionREP()
computes posrep_ once and for all
Definition: PFCluster.h:96
#define unInitDynArray(T, n, x)
Definition: DynArray.h:57
double f[11][100]
float energy() const
rechit energy
Definition: PFRecHit.h:114
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
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalcBarrel
double b
Definition: hdecay.h:120
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalcEndcap
double a
Definition: hdecay.h:121
void setDepth(double depth)
Definition: PFCluster.h:90
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
#define declareDynArray(T, n, x)
Definition: DynArray.h:58
float fast_logf(float x)