CMS 3D CMS Logo

PFMultiDepthClusterizer.cc
Go to the documentation of this file.
7 #include "Math/GenVector/VectorUtil.h"
9 
10 #include "vdt/vdtMath.h"
11 
12 #include <iterator>
13 #include <unordered_map>
14 
17 
18 public:
20 
21  ~PFMultiDepthClusterizer() override = default;
22  PFMultiDepthClusterizer(const B2DGPF&) = delete;
23  B2DGPF& operator=(const B2DGPF&) = delete;
24 
25  void update(const edm::EventSetup& es) override { _allCellsPosCalc->update(es); }
26 
28  const std::vector<bool>&,
29  reco::PFClusterCollection& outclus) override;
30 
31 private:
32  std::unique_ptr<PFCPositionCalculatorBase> _allCellsPosCalc;
33  double nSigmaEta_;
34  double nSigmaPhi_;
35 
36  class ClusterLink {
37  public:
38  ClusterLink(unsigned int i, unsigned int j, double DR, double DZ, double energy) {
39  from_ = i;
40  to_ = j;
41  linkDR_ = DR;
42  linkDZ_ = DZ;
43  linkE_ = energy;
44  }
45 
46  ~ClusterLink() = default;
47 
48  unsigned int from() const { return from_; }
49  unsigned int to() const { return to_; }
50  double dR() const { return linkDR_; }
51  double dZ() const { return linkDZ_; }
52  double energy() const { return linkE_; }
53 
54  private:
55  unsigned int from_;
56  unsigned int to_;
57  double linkDR_;
58  double linkDZ_;
59  double linkE_;
60  };
61 
62  void calculateShowerShapes(const reco::PFClusterCollection&, std::vector<double>&, std::vector<double>&);
63  std::vector<ClusterLink> link(const reco::PFClusterCollection&,
64  const std::vector<double>&,
65  const std::vector<double>&);
66  std::vector<ClusterLink> prune(std::vector<ClusterLink>&, std::vector<bool>& linkedClusters);
67 
69  unsigned int point,
70  std::vector<bool>& mask,
72  const std::vector<ClusterLink>& links);
73 
75 };
76 
78 
80  : PFClusterBuilderBase(conf, cc) {
81  if (conf.exists("allCellsPositionCalc")) {
82  const edm::ParameterSet& acConf = conf.getParameterSet("allCellsPositionCalc");
83  const std::string& algoac = acConf.getParameter<std::string>("algoName");
84  _allCellsPosCalc = PFCPositionCalculatorFactory::get()->create(algoac, acConf, cc);
85  }
86 
87  nSigmaEta_ = pow(conf.getParameter<double>("nSigmaEta"), 2);
88  nSigmaPhi_ = pow(conf.getParameter<double>("nSigmaPhi"), 2);
89 }
90 
92  const std::vector<bool>& seedable,
94  std::vector<double> etaRMS2(input.size(), 0.0);
95  std::vector<double> phiRMS2(input.size(), 0.0);
96 
97  //We need to sort the clusters for smaller to larger depth
98  // for (unsigned int i=0;i<input.size();++i)
99  // printf(" cluster%f %f \n",input[i].depth(),input[i].energy());
100 
101  //calculate cluster shapes
102  calculateShowerShapes(input, etaRMS2, phiRMS2);
103 
104  //link
105  auto&& links = link(input, etaRMS2, phiRMS2);
106  // for (const auto& link: links)
107  // printf("link %d %d %f %f\n",link.from(),link.to(),link.dR(),link.dZ());
108 
109  std::vector<bool> mask(input.size(), false);
110  std::vector<bool> linked(input.size(), false);
111 
112  //prune
113  auto&& prunedLinks = prune(links, linked);
114 
115  //printf("Pruned links\n")
116  // for (const auto& link: prunedLinks)
117  // printf("link %d %d %f %f\n",link.from(),link.to(),link.dR(),link.dZ());
118 
119  //now we need to build clusters
120  for (unsigned int i = 0; i < input.size(); ++i) {
121  //if not masked
122  if (mask[i])
123  continue;
124  //if not linked just spit it out
125  if (!linked[i]) {
126  output.push_back(input[i]);
127  // printf("Added single cluster with energy =%f \n",input[i].energy());
128  mask[i] = true;
129  continue;
130  }
131 
132  //now business: if linked and not masked gather clusters
133  reco::PFCluster cluster = input[i];
134  mask[i] = true;
135  expandCluster(cluster, i, mask, input, prunedLinks);
136  _allCellsPosCalc->calculateAndSetPosition(cluster);
137  output.push_back(cluster);
138  // printf("Added linked cluster with energy =%f\n",cluster.energy());
139  }
140 }
141 
143  std::vector<double>& etaRMS2,
144  std::vector<double>& phiRMS2) {
145  //shower shapes. here do not use the fractions
146 
147  for (unsigned int i = 0; i < clusters.size(); ++i) {
148  const reco::PFCluster& cluster = clusters[i];
149  double etaSum = 0.0;
150  double phiSum = 0.0;
151  auto const& crep = cluster.positionREP();
152  for (const auto& frac : cluster.recHitFractions()) {
153  auto const& h = *frac.recHitRef();
154  auto const& rep = h.positionREP();
155  etaSum += (frac.fraction() * h.energy()) * std::abs(rep.eta() - crep.eta());
156  phiSum += (frac.fraction() * h.energy()) * std::abs(deltaPhi(rep.phi(), crep.phi()));
157  }
158  //protection for single line : assign ~ tower
159  etaRMS2[i] = std::max(etaSum / cluster.energy(), 0.1);
160  etaRMS2[i] *= etaRMS2[i];
161  phiRMS2[i] = std::max(phiSum / cluster.energy(), 0.1);
162  phiRMS2[i] *= phiRMS2[i];
163  }
164 }
165 
166 std::vector<PFMultiDepthClusterizer::ClusterLink> PFMultiDepthClusterizer::link(
167  const reco::PFClusterCollection& clusters, const std::vector<double>& etaRMS2, const std::vector<double>& phiRMS2) {
168  std::vector<ClusterLink> links;
169  //loop on all pairs
170  for (unsigned int i = 0; i < clusters.size(); ++i)
171  for (unsigned int j = 0; j < clusters.size(); ++j) {
172  if (i == j)
173  continue;
174 
175  const reco::PFCluster& cluster1 = clusters[i];
176  const reco::PFCluster& cluster2 = clusters[j];
177 
178  auto dz = (cluster2.depth() - cluster1.depth());
179 
180  //Do not link at the same layer and only link inside out!
181  if (dz < 0.0f || std::abs(dz) < 0.2f)
182  continue;
183 
184  auto const& crep1 = cluster1.positionREP();
185  auto const& crep2 = cluster2.positionREP();
186 
187  auto deta = crep1.eta() - crep2.eta();
188  deta = deta * deta / (etaRMS2[i] + etaRMS2[j]);
189  auto dphi = deltaPhi(crep1.phi(), crep2.phi());
190  dphi = dphi * dphi / (phiRMS2[i] + phiRMS2[j]);
191 
192  // printf("Testing Link %d -> %d (%f %f %f %f ) \n",i,j,deta,dphi,cluster1.position().Eta()-cluster2.position().Eta(),deltaPhi(cluster1.position().Phi(),cluster2.position().Phi()));
193 
194  if ((deta < nSigmaEta_) & (dphi < nSigmaPhi_))
195  links.push_back(ClusterLink(i, j, deta + dphi, std::abs(dz), cluster1.energy() + cluster2.energy()));
196  }
197 
198  return links;
199 }
200 
201 std::vector<PFMultiDepthClusterizer::ClusterLink> PFMultiDepthClusterizer::prune(std::vector<ClusterLink>& links,
202  std::vector<bool>& linkedClusters) {
203  std::vector<ClusterLink> goodLinks;
204  std::vector<bool> mask(links.size(), false);
205  if (links.empty())
206  return goodLinks;
207 
208  for (unsigned int i = 0; i < links.size() - 1; ++i) {
209  if (mask[i])
210  continue;
211  for (unsigned int j = i + 1; j < links.size(); ++j) {
212  if (mask[j])
213  continue;
214 
215  const ClusterLink& link1 = links[i];
216  const ClusterLink& link2 = links[j];
217 
218  if (link1.to() == link2.to()) { //found two links going to the same spot,kill one
219  //first prefer nearby layers
220  if (link1.dZ() < link2.dZ()) {
221  mask[j] = true;
222  } else if (link1.dZ() > link2.dZ()) {
223  mask[i] = true;
224  } else if (fabs(link1.dZ() - link2.dZ()) < 0.2) { //if same layer-pick based on transverse compatibility
225  if (link1.dR() < link2.dR()) {
226  mask[j] = true;
227  } else if (link1.dR() > link2.dR()) {
228  mask[i] = true;
229  } else {
230  //same distance as well -> can happen !!!!! Pick the highest SUME
231  if (link1.energy() < link2.energy())
232  mask[i] = true;
233  else
234  mask[j] = true;
235  }
236  }
237  }
238  }
239  }
240 
241  for (unsigned int i = 0; i < links.size(); ++i) {
242  if (mask[i])
243  continue;
244  goodLinks.push_back(links[i]);
245  linkedClusters[links[i].from()] = true;
246  linkedClusters[links[i].to()] = true;
247  }
248 
249  return goodLinks;
250 }
251 
253  double e1 = 0.0;
254  double e2 = 0.0;
255 
256  //find seeds
257  for (const auto& fraction : main.recHitFractions())
258  if (fraction.recHitRef()->detId() == main.seed()) {
259  e1 = fraction.recHitRef()->energy();
260  }
261 
262  for (const auto& fraction : added.recHitFractions()) {
263  main.addRecHitFraction(fraction);
264  if (fraction.recHitRef()->detId() == added.seed()) {
265  e2 = fraction.recHitRef()->energy();
266  }
267  }
268  if (e2 > e1)
269  main.setSeed(added.seed());
270 }
271 
273  unsigned int point,
274  std::vector<bool>& mask,
276  const std::vector<ClusterLink>& links) {
277  for (const auto& link : links) {
278  if (link.from() == point) {
279  //found link that starts from this guy if not masked absorb
280  if (!mask[link.from()]) {
281  absorbCluster(cluster, clusters[link.from()]);
282  mask[link.from()] = true;
283  }
284 
285  if (!mask[link.to()]) {
286  absorbCluster(cluster, clusters[link.to()]);
287  mask[link.to()] = true;
288  expandCluster(cluster, link.to(), mask, clusters, links);
289  }
290  }
291  if (link.to() == point) {
292  //found link that starts from this guy if not masked absorb
293  if (!mask[link.to()]) {
294  absorbCluster(cluster, clusters[link.to()]);
295  mask[link.to()] = true;
296  }
297 
298  if (!mask[link.from()]) {
299  absorbCluster(cluster, clusters[link.from()]);
300  mask[link.from()] = true;
301  expandCluster(cluster, link.from(), mask, clusters, links);
302  }
303  }
304  }
305 }
reco::PFClusterCollection
std::vector< PFCluster > PFClusterCollection
collection of PFCluster objects
Definition: PFClusterFwd.h:9
mps_fire.i
i
Definition: mps_fire.py:428
input
static const std::string input
Definition: EdmProvDump.cc:48
MessageLogger.h
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:72
deltaPhi.h
PFMultiDepthClusterizer::calculateShowerShapes
void calculateShowerShapes(const reco::PFClusterCollection &, std::vector< double > &, std::vector< double > &)
Definition: PFMultiDepthClusterizer.cc:142
PFMultiDepthClusterizer::expandCluster
void expandCluster(reco::PFCluster &, unsigned int point, std::vector< bool > &mask, const reco::PFClusterCollection &, const std::vector< ClusterLink > &links)
Definition: PFMultiDepthClusterizer.cc:272
PFMultiDepthClusterizer::nSigmaEta_
double nSigmaEta_
Definition: PFMultiDepthClusterizer.cc:33
reco::PFCluster::recHitFractions
const std::vector< reco::PFRecHitFraction > & recHitFractions() const
vector of rechit fractions
Definition: PFCluster.h:65
PFMultiDepthClusterizer::operator=
B2DGPF & operator=(const B2DGPF &)=delete
PFRecHit.h
PFMultiDepthClusterizer::_allCellsPosCalc
std::unique_ptr< PFCPositionCalculatorBase > _allCellsPosCalc
Definition: PFMultiDepthClusterizer.cc:32
reco::PFCluster::energy
double energy() const
cluster energy
Definition: PFCluster.h:74
PFMultiDepthClusterizer::prune
std::vector< ClusterLink > prune(std::vector< ClusterLink > &, std::vector< bool > &linkedClusters)
Definition: PFMultiDepthClusterizer.cc:201
DivergingColor.frac
float frac
Definition: DivergingColor.py:175
SiPixelRawToDigiRegional_cfi.deltaPhi
deltaPhi
Definition: SiPixelRawToDigiRegional_cfi.py:9
HLT_FULL_cff.fraction
fraction
Definition: HLT_FULL_cff.py:52823
PFClusterBuilderBase.h
reco::PFCluster::depth
double depth() const
cluster depth
Definition: PFCluster.h:82
PFMultiDepthClusterizer::PFMultiDepthClusterizer
PFMultiDepthClusterizer(const edm::ParameterSet &conf, edm::ConsumesCollector &cc)
Definition: PFMultiDepthClusterizer.cc:79
h
reco::PFCluster::positionREP
const REPPoint & positionREP() const
cluster position: rho, eta, phi
Definition: PFCluster.h:92
DEFINE_EDM_PLUGIN
#define DEFINE_EDM_PLUGIN(factory, type, name)
Definition: PluginFactory.h:124
bsc_activity_cfg.clusters
clusters
Definition: bsc_activity_cfg.py:36
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
PFMultiDepthClusterizer::absorbCluster
void absorbCluster(reco::PFCluster &, const reco::PFCluster &)
Definition: PFMultiDepthClusterizer.cc:252
edm::ParameterSet
Definition: ParameterSet.h:47
deltaR.h
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
PFMultiDepthClusterizer::update
void update(const edm::EventSetup &es) override
Definition: PFMultiDepthClusterizer.cc:25
edmplugin::PluginFactory
Definition: PluginFactory.h:34
PFMultiDepthClusterizer::nSigmaPhi_
double nSigmaPhi_
Definition: PFMultiDepthClusterizer.cc:34
PFMultiDepthClusterizer::link
std::vector< ClusterLink > link(const reco::PFClusterCollection &, const std::vector< double > &, const std::vector< double > &)
Definition: PFMultiDepthClusterizer.cc:166
StorageManager_cfg.e1
e1
Definition: StorageManager_cfg.py:16
cuy.rep
rep
Definition: cuy.py:1189
edm::EventSetup
Definition: EventSetup.h:58
get
#define get
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
main
Definition: main.py:1
PFRecHitFraction.h
PVValHelper::dz
Definition: PVValidationHelpers.h:51
PFMultiDepthClusterizer::~PFMultiDepthClusterizer
~PFMultiDepthClusterizer() override=default
electronStore.links
links
Definition: electronStore.py:149
reco::PFCluster
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:42
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
PFMultiDepthClusterizer::buildClusters
void buildClusters(const reco::PFClusterCollection &, const std::vector< bool > &, reco::PFClusterCollection &outclus) override
Definition: PFMultiDepthClusterizer.cc:91
PFClusterBuilderBase
Definition: PFClusterBuilderBase.h:19
funct::pow
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
ConsumesCollector.h
funct::abs
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
PFMultiDepthClusterizer
Definition: PFMultiDepthClusterizer.cc:15
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
point
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
PFMultiDepthClusterizer::B2DGPF
PFMultiDepthClusterizer B2DGPF
Definition: PFMultiDepthClusterizer.cc:16
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
edm::ParameterSet::getParameterSet
ParameterSet const & getParameterSet(std::string const &) const
Definition: ParameterSet.cc:2128