CMS 3D CMS Logo

Basic2DGenericPFlowPositionCalc.cc
Go to the documentation of this file.
9 
10 #include "vdt/vdtMath.h"
11 
12 #include <boost/iterator/function_output_iterator.hpp>
13 
14 #include <cmath>
15 #include <iterator>
16 #include <memory>
17 #include <tuple>
18 
20 public:
23  _posCalcNCrystals(conf.getParameter<int>("posCalcNCrystals")),
24  _minAllowedNorm(conf.getParameter<double>("minAllowedNormalization")) {
25  std::vector<int> detectorEnum;
26  std::vector<int> depths;
27  std::vector<double> logWeightDenom;
28  std::vector<float> logWeightDenomInv;
29 
30  if (conf.exists("logWeightDenominatorByDetector")) {
31  const std::vector<edm::ParameterSet>& logWeightDenominatorByDetectorPSet =
32  conf.getParameterSetVector("logWeightDenominatorByDetector");
33 
34  for (const auto& pset : logWeightDenominatorByDetectorPSet) {
35  if (!pset.exists("detector")) {
36  throw cms::Exception("logWeightDenominatorByDetectorPSet") << "logWeightDenominator : detector not specified";
37  }
38 
39  const std::string& det = pset.getParameter<std::string>("detector");
40 
41  if (det == std::string("HCAL_BARREL1") || det == std::string("HCAL_ENDCAP")) {
42  std::vector<int> depthsT = pset.getParameter<std::vector<int> >("depths");
43  std::vector<double> logWeightDenomT = pset.getParameter<std::vector<double> >("logWeightDenominator");
44  if (logWeightDenomT.size() != depthsT.size()) {
45  throw cms::Exception("logWeightDenominator") << "logWeightDenominator mismatch with the numbers of depths";
46  }
47  for (unsigned int i = 0; i < depthsT.size(); ++i) {
48  if (det == std::string("HCAL_BARREL1"))
49  detectorEnum.push_back(1);
50  if (det == std::string("HCAL_ENDCAP"))
51  detectorEnum.push_back(2);
52  depths.push_back(depthsT[i]);
53  logWeightDenom.push_back(logWeightDenomT[i]);
54  }
55  }
56  }
57  } else {
58  detectorEnum.push_back(0);
59  depths.push_back(0);
60  logWeightDenom.push_back(conf.getParameter<double>("logWeightDenominator"));
61  }
62 
63  for (unsigned int i = 0; i < depths.size(); ++i) {
64  logWeightDenomInv.push_back(1. / logWeightDenom[i]);
65  }
66 
67  // _logWeightDenom = std::make_pair(depths,logWeightDenomInv);
68  _logWeightDenom = std::make_tuple(detectorEnum, depths, logWeightDenomInv);
69 
70  _timeResolutionCalcBarrel.reset(nullptr);
71  if (conf.exists("timeResolutionCalcBarrel")) {
72  const edm::ParameterSet& timeResConf = conf.getParameterSet("timeResolutionCalcBarrel");
73  _timeResolutionCalcBarrel = std::make_unique<CaloRecHitResolutionProvider>(timeResConf);
74  }
75  _timeResolutionCalcEndcap.reset(nullptr);
76  if (conf.exists("timeResolutionCalcEndcap")) {
77  const edm::ParameterSet& timeResConf = conf.getParameterSet("timeResolutionCalcEndcap");
78  _timeResolutionCalcEndcap = std::make_unique<CaloRecHitResolutionProvider>(timeResConf);
79  }
80 
81  switch (_posCalcNCrystals) {
82  case 5:
83  case 9:
84  case -1:
85  break;
86  default:
87  edm::LogError("Basic2DGenericPFlowPositionCalc") << "posCalcNCrystals not valid";
88  assert(0); // bug
89  }
90  }
91 
94 
97 
98 private:
99  const int _posCalcNCrystals;
100  std::tuple<std::vector<int>, std::vector<int>, std::vector<float> > _logWeightDenom;
101  const float _minAllowedNorm;
102 
103  std::unique_ptr<CaloRecHitResolutionProvider> _timeResolutionCalcBarrel;
104  std::unique_ptr<CaloRecHitResolutionProvider> _timeResolutionCalcEndcap;
105 
107 };
108 
110 
111 namespace {
112  inline bool isBarrel(int cell_layer) {
113  return (cell_layer == PFLayer::HCAL_BARREL1 || cell_layer == PFLayer::HCAL_BARREL2 ||
114  cell_layer == PFLayer::ECAL_BARREL);
115  }
116 } // namespace
117 
120 }
121 
123  for (reco::PFCluster& cluster : clusters) {
125  }
126 }
127 
129  if (!cluster.seed()) {
130  throw cms::Exception("ClusterWithNoSeed") << " Found a cluster with no seed: " << cluster;
131  }
132  double cl_energy = 0;
133  double cl_time = 0;
134  double cl_timeweight = 0.0;
135  double max_e = 0.0;
136  PFLayer::Layer max_e_layer = PFLayer::NONE;
137  // find the seed and max layer and also calculate time
138  //Michalis : Even if we dont use timing in clustering here we should fill
139  //the time information for the cluster. This should use the timing resolution(1/E)
140  //so the weight should be fraction*E^2
141  //calculate a simplistic depth now. The log weighted will be done
142  //in different stage
143 
144  auto const recHitCollection =
145  &(*cluster.recHitFractions()[0].recHitRef()) - cluster.recHitFractions()[0].recHitRef().key();
146  auto nhits = cluster.recHitFractions().size();
147  struct LHit {
148  reco::PFRecHit const* hit;
149  float energy;
150  float fraction;
151  };
152  declareDynArray(LHit, nhits, hits);
153  for (auto i = 0U; i < nhits; ++i) {
154  auto const& hf = cluster.recHitFractions()[i];
155  auto k = hf.recHitRef().key();
156  auto p = recHitCollection + k;
157  hits[i] = {p, (*p).energy(), float(hf.fraction())};
158  }
159 
161  LHit mySeed = {};
162  for (auto const& rhf : hits) {
163  const reco::PFRecHit& refhit = *rhf.hit;
164  if (refhit.detId() == cluster.seed())
165  mySeed = rhf;
166  const auto rh_fraction = rhf.fraction;
167  const auto rh_rawenergy = rhf.energy;
168  const auto rh_energy = rh_rawenergy * rh_fraction;
169 #ifdef PF_DEBUG
170  if UNLIKELY (edm::isNotFinite(rh_energy)) {
171  throw cms::Exception("PFClusterAlgo") << "rechit " << refhit.detId() << " has a NaN energy... "
172  << "The input of the particle flow clustering seems to be corrupted.";
173  }
174 #endif
175  cl_energy += rh_energy;
176  // If time resolution is given, calculated weighted average
177  if (resGiven) {
178  double res2 = 1.e-4;
179  int cell_layer = (int)refhit.layer();
180  res2 = isBarrel(cell_layer) ? 1. / _timeResolutionCalcBarrel->timeResolution2(rh_rawenergy)
181  : 1. / _timeResolutionCalcEndcap->timeResolution2(rh_rawenergy);
182  cl_time += rh_fraction * refhit.time() * res2;
183  cl_timeweight += rh_fraction * res2;
184  } else { // assume resolution = 1/E**2
185  const double rh_rawenergy2 = rh_rawenergy * rh_rawenergy;
186  cl_timeweight += rh_rawenergy2 * rh_fraction;
187  cl_time += rh_rawenergy2 * rh_fraction * refhit.time();
188  }
189 
190  if (rh_energy > max_e) {
191  max_e = rh_energy;
192  max_e_layer = refhit.layer();
193  }
194  }
195 
196  cluster.setEnergy(cl_energy);
197  cluster.setTime(cl_time / cl_timeweight);
198  if (resGiven) {
199  cluster.setTimeError(std::sqrt(1.0f / float(cl_timeweight)));
200  }
201  cluster.setLayer(max_e_layer);
202 
203  // calculate the position
204  double depth = 0.0;
205  double position_norm = 0.0;
206  double x(0.0), y(0.0), z(0.0);
207  if (nullptr != mySeed.hit) {
208  auto seedNeighbours = mySeed.hit->neighbours();
209  switch (_posCalcNCrystals) {
210  case 5:
211  seedNeighbours = mySeed.hit->neighbours4();
212  break;
213  case 9:
214  seedNeighbours = mySeed.hit->neighbours8();
215  break;
216  default:
217  break;
218  }
219 
220  auto compute = [&](LHit const& rhf) {
221  const reco::PFRecHit& refhit = *rhf.hit;
222 
223  int cell_layer = (int)refhit.layer();
224  float threshold = 0;
225 
226  for (unsigned int j = 0; j < (std::get<2>(_logWeightDenom)).size(); ++j) {
227  // barrel is detecor type1
228  int detectorEnum = std::get<0>(_logWeightDenom)[j];
229  int depth = std::get<1>(_logWeightDenom)[j];
230 
231  if ((cell_layer == PFLayer::HCAL_BARREL1 && detectorEnum == 1 && refhit.depth() == depth) ||
232  (cell_layer == PFLayer::HCAL_ENDCAP && detectorEnum == 2 && refhit.depth() == depth) || detectorEnum == 0)
233  threshold = std::get<2>(_logWeightDenom)[j];
234  }
235 
236  const auto rh_energy = rhf.energy * rhf.fraction;
237  const auto norm =
238  (rhf.fraction < _minFractionInCalc ? 0.0f : std::max(0.0f, vdt::fast_logf(rh_energy * threshold)));
239  const auto rhpos_xyz = refhit.position() * norm;
240  x += rhpos_xyz.x();
241  y += rhpos_xyz.y();
242  z += rhpos_xyz.z();
243  depth += refhit.depth() * norm;
244  position_norm += norm;
245  };
246 
247  if (_posCalcNCrystals != -1) // sorted to make neighbour search faster (maybe)
248  std::sort(hits.begin(), hits.end(), [](LHit const& a, LHit const& b) { return a.hit < b.hit; });
249 
250  if (_posCalcNCrystals == -1)
251  for (auto const& rhf : hits)
252  compute(rhf);
253  else { // only seed and its neighbours
254  compute(mySeed);
255  // search seedNeighbours to find energy fraction in cluster (sic)
256  unInitDynArray(reco::PFRecHit const*, seedNeighbours.size(), nei);
257  for (auto k : seedNeighbours) {
258  nei.push_back(recHitCollection + k);
259  }
260  std::sort(nei.begin(), nei.end());
261  struct LHitLess {
262  auto operator()(LHit const& a, reco::PFRecHit const* b) const { return a.hit < b; }
263  auto operator()(reco::PFRecHit const* b, LHit const& a) const { return b < a.hit; }
264  };
265  std::set_intersection(
266  hits.begin(), hits.end(), nei.begin(), nei.end(), boost::make_function_output_iterator(compute), LHitLess());
267  }
268  } else {
269  throw cms::Exception("Basic2DGenerticPFlowPositionCalc")
270  << "Cluster seed hit is null, something is wrong with PFlow RecHit!";
271  }
272 
273  if (position_norm < _minAllowedNorm) {
274  edm::LogError("WeirdClusterNormalization") << "PFCluster too far from seeding cell: set position to (0,0,0).";
275  cluster.setPosition(math::XYZPoint(0, 0, 0));
276  cluster.calculatePositionREP();
277  } else {
278  const double norm_inverse = 1.0 / position_norm;
279  x *= norm_inverse;
280  y *= norm_inverse;
281  z *= norm_inverse;
282  depth *= norm_inverse;
283  cluster.setPosition(math::XYZPoint(x, y, z));
284  cluster.setDepth(depth);
285  cluster.calculatePositionREP();
286  }
287 }
reco::PFClusterCollection
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
PFCPositionCalculatorBase::_minFractionInCalc
const float _minFractionInCalc
Definition: PFCPositionCalculatorBase.h:37
Basic2DGenericPFlowPositionCalc::_timeResolutionCalcBarrel
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalcBarrel
Definition: Basic2DGenericPFlowPositionCalc.cc:103
DDAxes::y
Basic2DGenericPFlowPositionCalc::calculateAndSetPositionActual
void calculateAndSetPositionActual(reco::PFCluster &) const
Definition: Basic2DGenericPFlowPositionCalc.cc:128
PFRecHitFwd.h
electrons_cff.bool
bool
Definition: electrons_cff.py:366
mps_fire.i
i
Definition: mps_fire.py:428
reco::PFCluster::setTime
void setTime(float time, float timeError=0)
Definition: PFCluster.h:84
Basic2DGenericPFlowPositionCalc::_posCalcNCrystals
const int _posCalcNCrystals
Definition: Basic2DGenericPFlowPositionCalc.cc:99
MessageLogger.h
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
Basic2DGenericPFlowPositionCalc::Basic2DGenericPFlowPositionCalc
Basic2DGenericPFlowPositionCalc(const edm::ParameterSet &conf, edm::ConsumesCollector &cc)
Definition: Basic2DGenericPFlowPositionCalc.cc:21
HLT_FULL_cff.detectorEnum
detectorEnum
Definition: HLT_FULL_cff.py:13160
hfClusterShapes_cfi.hits
hits
Definition: hfClusterShapes_cfi.py:5
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
edm::isNotFinite
constexpr bool isNotFinite(T x)
Definition: isFinite.h:9
reco::PFCluster::setLayer
void setLayer(PFLayer::Layer layer)
set layer
Definition: PFCluster.cc:49
PFLayer::HCAL_ENDCAP
Definition: PFLayer.h:37
reco::PFCluster::recHitFractions
const std::vector< reco::PFRecHitFraction > & recHitFractions() const
vector of rechit fractions
Definition: PFCluster.h:65
Basic2DGenericPFlowPositionCalc
Definition: Basic2DGenericPFlowPositionCalc.cc:19
cms::cuda::assert
assert(be >=bs)
Basic2DGenericPFlowPositionCalc::calculateAndSetPosition
void calculateAndSetPosition(reco::PFCluster &) override
Definition: Basic2DGenericPFlowPositionCalc.cc:118
bookConverter.compute
def compute(min, max)
Definition: bookConverter.py:106
DDAxes::x
Basic2DGenericPFlowPositionCalc::_logWeightDenom
std::tuple< std::vector< int >, std::vector< int >, std::vector< float > > _logWeightDenom
Definition: Basic2DGenericPFlowPositionCalc.cc:100
Basic2DGenericPFlowPositionCalc::_timeResolutionCalcEndcap
std::unique_ptr< CaloRecHitResolutionProvider > _timeResolutionCalcEndcap
Definition: Basic2DGenericPFlowPositionCalc.cc:104
ecalTB2006H4_GenSimDigiReco_cfg.mySeed
mySeed
Definition: ecalTB2006H4_GenSimDigiReco_cfg.py:8
PFRecHit.h
PFLayer::ECAL_BARREL
Definition: PFLayer.h:33
reco::PFRecHit::depth
int depth() const
depth for segemntation
Definition: PFRecHit.h:105
UNLIKELY
#define UNLIKELY(x)
Definition: Likely.h:21
photonIsolationHIProducer_cfi.hf
hf
Definition: photonIsolationHIProducer_cfi.py:9
PFLayer::HCAL_BARREL2
Definition: PFLayer.h:36
HLT_FULL_cff.fraction
fraction
Definition: HLT_FULL_cff.py:52823
myMath::fast_logf
float fast_logf(float x)
Definition: EcalUncalibRecHitRatioMethodAlgo.h:28
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
PFLayer::HCAL_BARREL1
Definition: PFLayer.h:35
DDAxes::z
declareDynArray
#define declareDynArray(T, n, x)
Definition: DynArray.h:91
HCALHighEnergyHPDFilter_cfi.energy
energy
Definition: HCALHighEnergyHPDFilter_cfi.py:5
PFLayer::NONE
Definition: PFLayer.h:34
dqmdumpme.k
k
Definition: dqmdumpme.py:60
Basic2DGenericPFlowPositionCalc::_minAllowedNorm
const float _minAllowedNorm
Definition: Basic2DGenericPFlowPositionCalc.cc:101
PFCPositionCalculatorBase.h
PFCPositionCalculatorBase
Definition: PFCPositionCalculatorBase.h:15
nhits
Definition: HIMultiTrackSelector.h:42
DEFINE_EDM_PLUGIN
#define DEFINE_EDM_PLUGIN(factory, type, name)
Definition: PluginFactory.h:124
b
double b
Definition: hdecay.h:118
reco::CaloCluster::setPosition
void setPosition(const math::XYZPoint &p)
Definition: CaloCluster.h:140
LEDCalibrationChannels.depth
depth
Definition: LEDCalibrationChannels.py:65
mitigatedMETSequence_cff.U
U
Definition: mitigatedMETSequence_cff.py:36
PFLayer::Layer
Layer
layer definition
Definition: PFLayer.h:29
bsc_activity_cfg.clusters
clusters
Definition: bsc_activity_cfg.py:36
PixelPluginsPhase0_cfi.isBarrel
isBarrel
Definition: PixelPluginsPhase0_cfi.py:17
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
edm::ParameterSet
Definition: ParameterSet.h:47
math::XYZPoint
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
a
double a
Definition: hdecay.h:119
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
Basic2DGenericPFlowPositionCalc::calculateAndSetPositions
void calculateAndSetPositions(reco::PFClusterCollection &) override
Definition: Basic2DGenericPFlowPositionCalc.cc:122
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
jetUpdater_cfi.sort
sort
Definition: jetUpdater_cfi.py:29
edmplugin::PluginFactory
Definition: PluginFactory.h:34
reco::PFCluster::calculatePositionREP
void calculatePositionREP()
computes posrep_ once and for all
Definition: PFCluster.h:95
createfilelist.int
int
Definition: createfilelist.py:10
Basic2DGenericPFlowPositionCalc::operator=
Basic2DGenericPFlowPositionCalc & operator=(const Basic2DGenericPFlowPositionCalc &)=delete
edm::LogError
Log< level::Error, false > LogError
Definition: MessageLogger.h:123
cc
reco::CaloCluster::seed
DetId seed() const
return DetId of seed
Definition: CaloCluster.h:219
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
DynArray.h
reco::PFRecHit::time
float time() const
timing for cleaned hits
Definition: PFRecHit.h:102
HLT_FULL_cff.depths
depths
Definition: HLT_FULL_cff.py:13426
reco::PFRecHit::detId
unsigned detId() const
rechit detId
Definition: PFRecHit.h:93
reco::CaloCluster::setEnergy
void setEnergy(double energy)
Definition: CaloCluster.h:136
isFinite.h
CaloRecHitResolutionProvider.h
Exception
Definition: hltDiff.cc:245
reco::PFCluster
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:42
reco::PFCluster::setTimeError
void setTimeError(float timeError)
Definition: PFCluster.h:88
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
reco::PFRecHit::position
PositionType const & position() const
rechit cell centre x, y, z
Definition: PFRecHit.h:117
reco::PFRecHit
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:31
edm::ParameterSet::getParameterSetVector
VParameterSet const & getParameterSetVector(std::string const &name) const
Definition: ParameterSet.cc:2160
reco::PFRecHit::layer
PFLayer::Layer layer() const
rechit layer
Definition: PFRecHit.h:96
reco::PFCluster::setDepth
void setDepth(double depth)
Definition: PFCluster.h:89
ConsumesCollector.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
remoteMonitoring_LED_IterMethod_cfg.threshold
threshold
Definition: remoteMonitoring_LED_IterMethod_cfg.py:430
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
edm::ParameterSet::getParameterSet
ParameterSet const & getParameterSet(std::string const &) const
Definition: ParameterSet.cc:2128
unInitDynArray
#define unInitDynArray(T, n, x)
Definition: DynArray.h:88
hit
Definition: SiStripHitEffFromCalibTree.cc:88
muonDTDigis_cfi.pset
pset
Definition: muonDTDigis_cfi.py:27
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443