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 namespace {
14  inline bool isBarrel(int cell_layer) {
15  return (cell_layer == PFLayer::HCAL_BARREL1 || cell_layer == PFLayer::HCAL_BARREL2 ||
16  cell_layer == PFLayer::ECAL_BARREL);
17  }
18 } // namespace
19 
22 }
23 
25  for (reco::PFCluster& cluster : clusters) {
27  }
28 }
29 
31  if (!cluster.seed()) {
32  throw cms::Exception("ClusterWithNoSeed") << " Found a cluster with no seed: " << cluster;
33  }
34  double cl_energy = 0;
35  double cl_time = 0;
36  double cl_timeweight = 0.0;
37  double max_e = 0.0;
38  PFLayer::Layer max_e_layer = PFLayer::NONE;
39  // find the seed and max layer and also calculate time
40  //Michalis : Even if we dont use timing in clustering here we should fill
41  //the time information for the cluster. This should use the timing resolution(1/E)
42  //so the weight should be fraction*E^2
43  //calculate a simplistic depth now. The log weighted will be done
44  //in different stage
45 
46  auto const recHitCollection =
47  &(*cluster.recHitFractions()[0].recHitRef()) - cluster.recHitFractions()[0].recHitRef().key();
48  auto nhits = cluster.recHitFractions().size();
49  struct LHit {
50  reco::PFRecHit const* hit;
51  float energy;
52  float fraction;
53  };
54  declareDynArray(LHit, nhits, hits);
55  for (auto i = 0U; i < nhits; ++i) {
56  auto const& hf = cluster.recHitFractions()[i];
57  auto k = hf.recHitRef().key();
58  auto p = recHitCollection + k;
59  hits[i] = {p, (*p).energy(), float(hf.fraction())};
60  }
61 
63  LHit mySeed = {};
64  for (auto const& rhf : hits) {
65  const reco::PFRecHit& refhit = *rhf.hit;
66  if (refhit.detId() == cluster.seed())
67  mySeed = rhf;
68  const auto rh_fraction = rhf.fraction;
69  const auto rh_rawenergy = rhf.energy;
70  const auto rh_energy = rh_rawenergy * rh_fraction;
71 #ifdef PF_DEBUG
72  if
73  UNLIKELY(edm::isNotFinite(rh_energy)) {
74  throw cms::Exception("PFClusterAlgo") << "rechit " << refhit.detId() << " has a NaN energy... "
75  << "The input of the particle flow clustering seems to be corrupted.";
76  }
77 #endif
78  cl_energy += rh_energy;
79  // If time resolution is given, calculated weighted average
80  if (resGiven) {
81  double res2 = 1.e-4;
82  int cell_layer = (int)refhit.layer();
83  res2 = isBarrel(cell_layer) ? 1. / _timeResolutionCalcBarrel->timeResolution2(rh_rawenergy)
84  : 1. / _timeResolutionCalcEndcap->timeResolution2(rh_rawenergy);
85  cl_time += rh_fraction * refhit.time() * res2;
86  cl_timeweight += rh_fraction * res2;
87  } else { // assume resolution = 1/E**2
88  const double rh_rawenergy2 = rh_rawenergy * rh_rawenergy;
89  cl_timeweight += rh_rawenergy2 * rh_fraction;
90  cl_time += rh_rawenergy2 * rh_fraction * refhit.time();
91  }
92 
93  if (rh_energy > max_e) {
94  max_e = rh_energy;
95  max_e_layer = refhit.layer();
96  }
97  }
98 
99  cluster.setEnergy(cl_energy);
100  cluster.setTime(cl_time / cl_timeweight);
101  if (resGiven) {
102  cluster.setTimeError(std::sqrt(1.0f / float(cl_timeweight)));
103  }
104  cluster.setLayer(max_e_layer);
105 
106  // calculate the position
107  double depth = 0.0;
108  double position_norm = 0.0;
109  double x(0.0), y(0.0), z(0.0);
110  if (nullptr != mySeed.hit) {
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 
126  int cell_layer = (int)refhit.layer();
127  float threshold = 0;
128 
129  for (unsigned int j = 0; j < (std::get<2>(_logWeightDenom)).size(); ++j) {
130  // barrel is detecor type1
131  int detectorEnum = std::get<0>(_logWeightDenom)[j];
132  int depth = std::get<1>(_logWeightDenom)[j];
133 
134  if ((cell_layer == PFLayer::HCAL_BARREL1 && detectorEnum == 1 && refhit.depth() == depth) ||
135  (cell_layer == PFLayer::HCAL_ENDCAP && detectorEnum == 2 && refhit.depth() == depth) || detectorEnum == 0)
136  threshold = std::get<2>(_logWeightDenom)[j];
137  }
138 
139  const auto rh_energy = rhf.energy * rhf.fraction;
140  const auto norm =
141  (rhf.fraction < _minFractionInCalc ? 0.0f : std::max(0.0f, vdt::fast_logf(rh_energy * threshold)));
142  const auto rhpos_xyz = refhit.position() * norm;
143  x += rhpos_xyz.x();
144  y += rhpos_xyz.y();
145  z += rhpos_xyz.z();
146  depth += refhit.depth() * norm;
147  position_norm += norm;
148  };
149 
150  if (_posCalcNCrystals != -1) // sorted to make neighbour search faster (maybe)
151  std::sort(hits.begin(), hits.end(), [](LHit const& a, LHit const& b) { return a.hit < b.hit; });
152 
153  if (_posCalcNCrystals == -1)
154  for (auto const& rhf : hits)
155  compute(rhf);
156  else { // only seed and its neighbours
157  compute(mySeed);
158  // search seedNeighbours to find energy fraction in cluster (sic)
159  unInitDynArray(reco::PFRecHit const*, seedNeighbours.size(), nei);
160  for (auto k : seedNeighbours) {
161  nei.push_back(recHitCollection + k);
162  }
163  std::sort(nei.begin(), nei.end());
164  struct LHitLess {
165  auto operator()(LHit const& a, reco::PFRecHit const* b) const { return a.hit < b; }
166  auto operator()(reco::PFRecHit const* b, LHit const& a) const { return b < a.hit; }
167  };
168  std::set_intersection(
169  hits.begin(), hits.end(), nei.begin(), nei.end(), boost::make_function_output_iterator(compute), LHitLess());
170  }
171  } else {
172  throw cms::Exception("Basic2DGenerticPFlowPositionCalc")
173  << "Cluster seed hit is null, something is wrong with PFlow RecHit!";
174  }
175 
176  if (position_norm < _minAllowedNorm) {
177  edm::LogError("WeirdClusterNormalization") << "PFCluster too far from seeding cell: set position to (0,0,0).";
178  cluster.setPosition(math::XYZPoint(0, 0, 0));
179  cluster.calculatePositionREP();
180  } else {
181  const double norm_inverse = 1.0 / position_norm;
182  x *= norm_inverse;
183  y *= norm_inverse;
184  z *= norm_inverse;
185  depth *= norm_inverse;
186  cluster.setPosition(math::XYZPoint(x, y, z));
187  cluster.setDepth(depth);
188  cluster.calculatePositionREP();
189  }
190 }
#define declareDynArray(T, n, x)
Definition: DynArray.h:91
void setLayer(PFLayer::Layer layer)
set layer
Definition: PFCluster.cc:93
void calculateAndSetPositionActual(reco::PFCluster &) const
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:46
float time() const
timing for cleaned hits
Definition: PFRecHit.h:98
unsigned detId() const
rechit detId
Definition: PFRecHit.h:89
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
void setPosition(const math::XYZPoint &p)
Definition: CaloCluster.h:139
void setTime(float time, float timeError=0)
Definition: PFCluster.h:88
void setEnergy(double energy)
Definition: CaloCluster.h:135
PositionType const & position() const
rechit cell centre x, y, z
Definition: PFRecHit.h:107
int depth() const
depth for segemntation
Definition: PFRecHit.h:101
PFLayer::Layer layer() const
rechit layer
Definition: PFRecHit.h:92
#define unInitDynArray(T, n, x)
Definition: DynArray.h:88
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:99
T sqrt(T t)
Definition: SSEVec.h:19
double f[11][100]
Layer
layer definition
Definition: PFLayer.h:29
void setTimeError(float timeError)
Definition: PFCluster.h:92
DetId seed() const
return DetId of seed
Definition: CaloCluster.h:218
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:118
void calculateAndSetPosition(reco::PFCluster &) override
const std::vector< reco::PFRecHitFraction > & recHitFractions() const
vector of rechit fractions
Definition: PFCluster.h:69
std::tuple< std::vector< int >, std::vector< int >, std::vector< float > > _logWeightDenom
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalcEndcap
double a
Definition: hdecay.h:119
void setDepth(double depth)
Definition: PFCluster.h:93
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
#define UNLIKELY(x)
Definition: Likely.h:21
float fast_logf(float x)