CMS 3D CMS Logo

Basic2DGenericPFlowClusterizer.cc
Go to the documentation of this file.
7 
8 #include "Math/GenVector/VectorUtil.h"
9 #include "vdt/vdtMath.h"
10 
11 #include <iterator>
12 #include <unordered_map>
13 
16 
17 public:
19 
20  ~Basic2DGenericPFlowClusterizer() override = default;
21  Basic2DGenericPFlowClusterizer(const B2DGPF&) = delete;
22  B2DGPF& operator=(const B2DGPF&) = delete;
23 
24  void update(const edm::EventSetup& es) override {
25  _positionCalc->update(es);
26  if (_allCellsPosCalc)
27  _allCellsPosCalc->update(es);
29  _convergencePosCalc->update(es);
30  }
31 
33  const std::vector<bool>&,
34  reco::PFClusterCollection& outclus) override;
35 
36 private:
37  const unsigned _maxIterations;
38  const double _stoppingTolerance;
39  const double _showerSigma2;
40  const bool _excludeOtherSeeds;
41  const double _minFracTot;
42  const std::unordered_map<std::string, int> _layerMap;
43 
44  std::unordered_map<int, std::pair<std::vector<int>, std::vector<double> > > _recHitEnergyNorms;
45  std::unique_ptr<PFCPositionCalculatorBase> _allCellsPosCalc;
46  std::unique_ptr<PFCPositionCalculatorBase> _convergencePosCalc;
47 
48  void seedPFClustersFromTopo(const reco::PFCluster&, const std::vector<bool>&, reco::PFClusterCollection&) const;
49 
50  void growPFClusters(const reco::PFCluster&,
51  const std::vector<bool>&,
52  const unsigned toleranceScaling,
53  const unsigned iter,
54  double dist,
56 
58 };
59 
61 
62 #ifdef PFLOW_DEBUG
63 #define LOGVERB(x) edm::LogVerbatim(x)
64 #define LOGWARN(x) edm::LogWarning(x)
65 #define LOGERR(x) edm::LogError(x)
66 #define LOGDRESSED(x) edm::LogInfo(x)
67 #else
68 #define LOGVERB(x) LogTrace(x)
69 #define LOGWARN(x) edm::LogWarning(x)
70 #define LOGERR(x) edm::LogError(x)
71 #define LOGDRESSED(x) LogDebug(x)
72 #endif
73 
76  : PFClusterBuilderBase(conf, cc),
77  _maxIterations(conf.getParameter<unsigned>("maxIterations")),
78  _stoppingTolerance(conf.getParameter<double>("stoppingTolerance")),
79  _showerSigma2(std::pow(conf.getParameter<double>("showerSigma"), 2.0)),
80  _excludeOtherSeeds(conf.getParameter<bool>("excludeOtherSeeds")),
81  _minFracTot(conf.getParameter<double>("minFracTot")),
82  _layerMap({{"PS2", (int)PFLayer::PS2},
83  {"PS1", (int)PFLayer::PS1},
84  {"ECAL_ENDCAP", (int)PFLayer::ECAL_ENDCAP},
85  {"ECAL_BARREL", (int)PFLayer::ECAL_BARREL},
86  {"NONE", (int)PFLayer::NONE},
87  {"HCAL_BARREL1", (int)PFLayer::HCAL_BARREL1},
88  {"HCAL_BARREL2_RING0", (int)PFLayer::HCAL_BARREL2},
89  {"HCAL_BARREL2_RING1", 100 * (int)PFLayer::HCAL_BARREL2},
90  {"HCAL_ENDCAP", (int)PFLayer::HCAL_ENDCAP},
91  {"HF_EM", (int)PFLayer::HF_EM},
92  {"HF_HAD", (int)PFLayer::HF_HAD}}) {
93  const std::vector<edm::ParameterSet>& thresholds = conf.getParameterSetVector("recHitEnergyNorms");
94  for (const auto& pset : thresholds) {
95  const std::string& det = pset.getParameter<std::string>("detector");
96 
97  std::vector<int> depths;
98  std::vector<double> rhE_norm;
99 
100  if (det == std::string("HCAL_BARREL1") || det == std::string("HCAL_ENDCAP")) {
101  depths = pset.getParameter<std::vector<int> >("depths");
102  rhE_norm = pset.getParameter<std::vector<double> >("recHitEnergyNorm");
103  } else {
104  depths.push_back(0);
105  rhE_norm.push_back(pset.getParameter<double>("recHitEnergyNorm"));
106  }
107 
108  if (rhE_norm.size() != depths.size()) {
109  throw cms::Exception("InvalidPFRecHitThreshold")
110  << "PFlowClusterizerThreshold mismatch with the numbers of depths";
111  }
112 
113  auto entry = _layerMap.find(det);
114  if (entry == _layerMap.end()) {
115  throw cms::Exception("InvalidDetectorLayer") << "Detector layer : " << det << " is not in the list of recognized"
116  << " detector layers!";
117  }
118  _recHitEnergyNorms.emplace(_layerMap.find(det)->second, std::make_pair(depths, rhE_norm));
119  }
120 
121  if (conf.exists("allCellsPositionCalc")) {
122  const edm::ParameterSet& acConf = conf.getParameterSet("allCellsPositionCalc");
123  const std::string& algoac = acConf.getParameter<std::string>("algoName");
124  _allCellsPosCalc = PFCPositionCalculatorFactory::get()->create(algoac, acConf, cc);
125  }
126  // if necessary a third pos calc for convergence testing
127  if (conf.exists("positionCalcForConvergence")) {
128  const edm::ParameterSet& convConf = conf.getParameterSet("positionCalcForConvergence");
129  const std::string& algoconv = convConf.getParameter<std::string>("algoName");
130  _convergencePosCalc = PFCPositionCalculatorFactory::get()->create(algoconv, convConf, cc);
131  }
132 }
133 
135  const std::vector<bool>& seedable,
137  reco::PFClusterCollection clustersInTopo;
138  for (const auto& topocluster : input) {
139  clustersInTopo.clear();
140  seedPFClustersFromTopo(topocluster, seedable, clustersInTopo);
141  const unsigned tolScal = std::pow(std::max(1.0, clustersInTopo.size() - 1.0), 2.0);
142  growPFClusters(topocluster, seedable, tolScal, 0, tolScal, clustersInTopo);
143  // step added by Josh Bendavid, removes low-fraction clusters
144  // did not impact position resolution with fraction cut of 1e-7
145  // decreases the size of each pf cluster considerably
146  prunePFClusters(clustersInTopo);
147  // recalculate the positions of the pruned clusters
148  if (_convergencePosCalc) {
149  // if defined, use the special position calculation for convergence tests
150  _convergencePosCalc->calculateAndSetPositions(clustersInTopo);
151  } else {
152  if (clustersInTopo.size() == 1 && _allCellsPosCalc) {
153  _allCellsPosCalc->calculateAndSetPosition(clustersInTopo.back());
154  } else {
155  _positionCalc->calculateAndSetPositions(clustersInTopo);
156  }
157  }
158  for (auto& clusterout : clustersInTopo) {
159  output.insert(output.end(), std::move(clusterout));
160  }
161  }
162 }
163 
165  const std::vector<bool>& seedable,
166  reco::PFClusterCollection& initialPFClusters) const {
167  const auto& recHitFractions = topo.recHitFractions();
168  for (const auto& rhf : recHitFractions) {
169  if (!seedable[rhf.recHitRef().key()])
170  continue;
171  initialPFClusters.push_back(reco::PFCluster());
172  reco::PFCluster& current = initialPFClusters.back();
173  current.addRecHitFraction(rhf);
174  current.setSeed(rhf.recHitRef()->detId());
175  if (_convergencePosCalc) {
176  _convergencePosCalc->calculateAndSetPosition(current);
177  } else {
178  _positionCalc->calculateAndSetPosition(current);
179  }
180  }
181 }
182 
184  const std::vector<bool>& seedable,
185  const unsigned toleranceScaling,
186  const unsigned iter,
187  double diff,
189  if (iter >= _maxIterations) {
190  LOGDRESSED("Basic2DGenericPFlowClusterizer:growAndStabilizePFClusters")
191  << "reached " << _maxIterations << " iterations, terminated position "
192  << "fit with diff = " << diff;
193  }
194  if (iter >= _maxIterations || diff <= _stoppingTolerance * toleranceScaling)
195  return;
196  // reset the rechits in this cluster, keeping the previous position
197  std::vector<reco::PFCluster::REPPoint> clus_prev_pos;
198  for (auto& cluster : clusters) {
199  const reco::PFCluster::REPPoint& repp = cluster.positionREP();
200  clus_prev_pos.emplace_back(repp.rho(), repp.eta(), repp.phi());
201  if (_convergencePosCalc) {
202  if (clusters.size() == 1 && _allCellsPosCalc) {
203  _allCellsPosCalc->calculateAndSetPosition(cluster);
204  } else {
205  _positionCalc->calculateAndSetPosition(cluster);
206  }
207  }
208  cluster.resetHitsAndFractions();
209  }
210  // loop over topo cluster and grow current PFCluster hypothesis
211  std::vector<double> dist2, frac;
212  double fractot = 0;
213  for (const reco::PFRecHitFraction& rhf : topo.recHitFractions()) {
214  const reco::PFRecHitRef& refhit = rhf.recHitRef();
215  int cell_layer = (int)refhit->layer();
216  if (cell_layer == PFLayer::HCAL_BARREL2 && std::abs(refhit->positionREP().eta()) > 0.34) {
217  cell_layer *= 100;
218  }
219 
220  math::XYZPoint topocellpos_xyz(refhit->position());
221  dist2.clear();
222  frac.clear();
223  fractot = 0;
224 
225  double recHitEnergyNorm = 0.;
226  auto const& recHitEnergyNormDepthPair = _recHitEnergyNorms.find(cell_layer)->second;
227 
228  for (unsigned int j = 0; j < recHitEnergyNormDepthPair.second.size(); ++j) {
229  int depth = recHitEnergyNormDepthPair.first[j];
230 
231  if ((cell_layer == PFLayer::HCAL_BARREL1 && refhit->depth() == depth) ||
232  (cell_layer == PFLayer::HCAL_ENDCAP && refhit->depth() == depth) ||
233  (cell_layer != PFLayer::HCAL_ENDCAP && cell_layer != PFLayer::HCAL_BARREL1))
234  recHitEnergyNorm = recHitEnergyNormDepthPair.second[j];
235  }
236 
237  // add rechits to clusters, calculating fraction based on distance
238  for (auto& cluster : clusters) {
239  const math::XYZPoint& clusterpos_xyz = cluster.position();
240  const math::XYZVector deltav = clusterpos_xyz - topocellpos_xyz;
241  const double d2 = deltav.Mag2() / _showerSigma2;
242  dist2.emplace_back(d2);
243  if (d2 > 100) {
244  LOGDRESSED("Basic2DGenericPFlowClusterizer:growAndStabilizePFClusters")
245  << "Warning! :: pfcluster-topocell distance is too large! d= " << d2;
246  }
247 
248  // fraction assignment logic
249  double fraction;
250  if (refhit->detId() == cluster.seed() && _excludeOtherSeeds) {
251  fraction = 1.0;
252  } else if (seedable[refhit.key()] && _excludeOtherSeeds) {
253  fraction = 0.0;
254  } else {
255  fraction = cluster.energy() / recHitEnergyNorm * vdt::fast_expf(-0.5 * d2);
256  }
257  fractot += fraction;
258  frac.emplace_back(fraction);
259  }
260  for (unsigned i = 0; i < clusters.size(); ++i) {
261  if (fractot > _minFracTot || (refhit->detId() == clusters[i].seed() && fractot > 0.0)) {
262  frac[i] /= fractot;
263  } else {
264  continue;
265  }
266  // if the fraction has been set to 0, the cell
267  // is now added to the cluster - careful ! (PJ, 19/07/08)
268  // BUT KEEP ONLY CLOSE CELLS OTHERWISE MEMORY JUST EXPLOSES
269  // (PJ, 15/09/08 <- similar to what existed before the
270  // previous bug fix, but keeps the close seeds inside,
271  // even if their fraction was set to zero.)
272  // Also add a protection to keep the seed in the cluster
273  // when the latter gets far from the former. These cases
274  // (about 1% of the clusters) need to be studied, as
275  // they create fake photons, in general.
276  // (PJ, 16/09/08)
277  if (dist2[i] < 100.0 || frac[i] > 0.9999) {
278  clusters[i].addRecHitFraction(reco::PFRecHitFraction(refhit, frac[i]));
279  }
280  }
281  }
282  // recalculate positions and calculate convergence parameter
283  double diff2 = 0.0;
284  for (unsigned i = 0; i < clusters.size(); ++i) {
285  if (_convergencePosCalc) {
286  _convergencePosCalc->calculateAndSetPosition(clusters[i]);
287  } else {
288  if (clusters.size() == 1 && _allCellsPosCalc) {
289  _allCellsPosCalc->calculateAndSetPosition(clusters[i]);
290  } else {
291  _positionCalc->calculateAndSetPosition(clusters[i]);
292  }
293  }
294  const double delta2 = reco::deltaR2(clusters[i].positionREP(), clus_prev_pos[i]);
295  if (delta2 > diff2)
296  diff2 = delta2;
297  }
298  diff = std::sqrt(diff2);
299  dist2.clear();
300  frac.clear();
301  clus_prev_pos.clear(); // avoid badness
302  growPFClusters(topo, seedable, toleranceScaling, iter + 1, diff, clusters);
303 }
304 
306  for (auto& cluster : clusters) {
307  cluster.pruneUsing([&](const reco::PFRecHitFraction& rhf) { return rhf.fraction() > _minFractionToKeep; });
308  }
309 }
void growPFClusters(const reco::PFCluster &, const std::vector< bool > &, const unsigned toleranceScaling, const unsigned iter, double dist, reco::PFClusterCollection &) const
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void buildClusters(const reco::PFClusterCollection &, const std::vector< bool > &, reco::PFClusterCollection &outclus) override
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:42
std::unique_ptr< PosCalc > _positionCalc
ParameterSet const & getParameterSet(std::string const &) const
const std::unordered_map< std::string, int > _layerMap
Fraction of a PFRecHit (rechits can be shared between several PFCluster&#39;s)
const std::vector< reco::PFRecHitFraction > & recHitFractions() const
vector of rechit fractions
Definition: PFCluster.h:65
std::unique_ptr< PFCPositionCalculatorBase > _convergencePosCalc
#define LOGDRESSED(x)
key_type key() const
Accessor for product key.
Definition: Ref.h:250
static std::string const input
Definition: EdmProvDump.cc:50
void setSeed(const DetId &id)
Definition: CaloCluster.h:146
Basic2DGenericPFlowClusterizer(const edm::ParameterSet &conf, edm::ConsumesCollector &cc)
std::unique_ptr< PFCPositionCalculatorBase > _allCellsPosCalc
void prunePFClusters(reco::PFClusterCollection &) const
T sqrt(T t)
Definition: SSEVec.h:19
~Basic2DGenericPFlowClusterizer() override=default
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
void seedPFClustersFromTopo(const reco::PFCluster &, const std::vector< bool > &, reco::PFClusterCollection &) const
B2DGPF & operator=(const B2DGPF &)=delete
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:31
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
std::unordered_map< int, std::pair< std::vector< int >, std::vector< double > > > _recHitEnergyNorms
void addRecHitFraction(const reco::PFRecHitFraction &frac)
add a given fraction of the rechit
Definition: PFCluster.cc:33
float fast_expf(float x)
ROOT::Math::PositionVector3D< ROOT::Math::CylindricalEta3D< double > > REPPoint
Definition: PFCluster.h:48
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
#define DEFINE_EDM_PLUGIN(factory, type, name)
#define get
void update(const edm::EventSetup &es) override
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
def move(src, dest)
Definition: eostools.py:511