CMS 3D CMS Logo

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;};
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 
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  if (resGiven) {
106  cluster.setTimeError(std::sqrt(1.0f/float(cl_timeweight)));
107  }
108  cluster.setLayer(max_e_layer);
109 
110  // calculate the position
111  double depth = 0.0;
112  double position_norm = 0.0;
113  double x(0.0),y(0.0),z(0.0);
114  if( nullptr != mySeed.hit ) {
115  auto seedNeighbours = mySeed.hit->neighbours();
116  switch( _posCalcNCrystals ) {
117  case 5:
118  seedNeighbours = mySeed.hit->neighbours4();
119  break;
120  case 9:
121  seedNeighbours = mySeed.hit->neighbours8();
122  break;
123  default:
124  break;
125  }
126 
127  auto compute = [&](LHit const& rhf) {
128  const reco::PFRecHit & refhit = *rhf.hit;
129 
130  int cell_layer = (int)refhit.layer();
131  float threshold=0;
132 
133  for (unsigned int j=0; j<(std::get<2>(_logWeightDenom)).size(); ++j) {
134  // barrel is detecor type1
135  int detectorEnum=std::get<0>(_logWeightDenom)[j];
136  int depth=std::get<1>(_logWeightDenom)[j];
137 
138  if( ( cell_layer == PFLayer::HCAL_BARREL1 && detectorEnum==1 && refhit.depth()== depth)
139  || ( cell_layer == PFLayer::HCAL_ENDCAP && detectorEnum==2 && refhit.depth()== depth)
140  || detectorEnum==0
141  ) threshold=std::get<2>(_logWeightDenom)[j];
142 
143  }
144 
145  const auto rh_energy = rhf.energy * rhf.fraction;
146  const auto norm = ( rhf.fraction < _minFractionInCalc ?
147  0.0f :
148  std::max(0.0f,vdt::fast_logf(rh_energy*threshold)) );
149  const auto rhpos_xyz = refhit.position()*norm;
150  x += rhpos_xyz.x();
151  y += rhpos_xyz.y();
152  z += rhpos_xyz.z();
153  depth += refhit.depth()*norm;
154  position_norm += norm;
155 
156  };
157 
158  if(_posCalcNCrystals != -1) // sorted to make neighbour search faster (maybe)
159  std::sort(hits.begin(),hits.end(),[](LHit const& a, LHit const& b) { return a.hit<b.hit;});
160 
161 
162  if(_posCalcNCrystals == -1)
163  for( auto const & rhf : hits ) compute(rhf);
164  else { // only seed and its neighbours
165  compute(mySeed);
166  // search seedNeighbours to find energy fraction in cluster (sic)
167  unInitDynArray(reco::PFRecHit const *,seedNeighbours.size(),nei);
168  for(auto k : seedNeighbours){
169  nei.push_back(recHitCollection+k);
170  }
171  std::sort(nei.begin(),nei.end());
172  struct LHitLess {
173  auto operator()(LHit const &a, reco::PFRecHit const * b) const {return a.hit<b;}
174  auto operator()(reco::PFRecHit const * b, LHit const &a) const {return b<a.hit;}
175  };
176  std::set_intersection(hits.begin(),hits.end(),nei.begin(),nei.end(),
177  boost::make_function_output_iterator(compute),
178  LHitLess()
179  );
180  }
181  } else {
182  throw cms::Exception("Basic2DGenerticPFlowPositionCalc")
183  << "Cluster seed hit is null, something is wrong with PFlow RecHit!";
184  }
185 
186  if( position_norm < _minAllowedNorm ) {
187  edm::LogError("WeirdClusterNormalization")
188  << "PFCluster too far from seeding cell: set position to (0,0,0).";
189  cluster.setPosition(math::XYZPoint(0,0,0));
190  cluster.calculatePositionREP();
191  } else {
192  const double norm_inverse = 1.0/position_norm;
193  x *= norm_inverse;
194  y *= norm_inverse;
195  z *= norm_inverse;
196  depth *= norm_inverse;
197  cluster.setPosition(math::XYZPoint(x,y,z));
198  cluster.setDepth(depth);
199  cluster.calculatePositionREP();
200  }
201 }
void setLayer(PFLayer::Layer layer)
set layer
Definition: PFCluster.cc:112
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:102
unsigned detId() const
rechit detId
Definition: PFRecHit.h:93
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
void setPosition(const math::XYZPoint &p)
Definition: CaloCluster.h:117
bool isBarrel(GeomDetEnumerators::SubDetector m)
void setTime(float time, float timeError=0)
Definition: PFCluster.h:92
void setEnergy(double energy)
Definition: CaloCluster.h:113
PositionType const & position() const
rechit cell centre x, y, z
Definition: PFRecHit.h:117
int depth() const
depth for segemntation
Definition: PFRecHit.h:105
PFLayer::Layer layer() const
rechit layer
Definition: PFRecHit.h:96
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:31
void calculatePositionREP()
computes posrep_ once and for all
Definition: PFCluster.h:100
T sqrt(T t)
Definition: SSEVec.h:18
std::tuple< std::vector< int >,std::vector< int >, std::vector< float > > _logWeightDenom
#define unInitDynArray(T, n, x)
Definition: DynArray.h:58
double f[11][100]
int k[5][pyjets_maxn]
Layer
layer definition
Definition: PFLayer.h:31
void setTimeError(float timeError)
Definition: PFCluster.h:93
DetId seed() const
return DetId of seed
Definition: CaloCluster.h:207
void calculateAndSetPositions(reco::PFClusterCollection &) override
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
def compute(min, max)
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalcBarrel
double b
Definition: hdecay.h:120
void calculateAndSetPosition(reco::PFCluster &) override
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalcEndcap
double a
Definition: hdecay.h:121
void setDepth(double depth)
Definition: PFCluster.h:94
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:59
#define UNLIKELY(x)
Definition: Likely.h:21
float fast_logf(float x)