CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PFMultiDepthClusterizer.cc
Go to the documentation of this file.
6 #include "Math/GenVector/VectorUtil.h"
7 
8 #include "vdt/vdtMath.h"
9 
10 #include <iterator>
11 
12 
13 
17 {
18 
19  _allCellsPosCalc.reset(NULL);
20  if( conf.exists("allCellsPositionCalc") ) {
21  const edm::ParameterSet& acConf =
22  conf.getParameterSet("allCellsPositionCalc");
23  const std::string& algoac =
24  acConf.getParameter<std::string>("algoName");
25  PosCalc* accalc =
26  PFCPositionCalculatorFactory::get()->create(algoac, acConf);
27  _allCellsPosCalc.reset(accalc);
28  }
29 
30  nSigmaEta_ = pow(conf.getParameter<double>("nSigmaEta"),2);
31  nSigmaPhi_ = pow(conf.getParameter<double>("nSigmaPhi"),2);
32 
33 }
34 
37  const std::vector<bool>& seedable,
39 
40  std::vector<double> etaRMS(input.size(),0.0);
41  std::vector<double> phiRMS(input.size(),0.0);
42 
43  //We need to sort the clusters for smaller to larger depth
44  // for (unsigned int i=0;i<input.size();++i)
45  // printf(" cluster%f %f \n",input[i].depth(),input[i].energy());
46 
47 
48 
49  //calculate cluster shapes
50  calculateShowerShapes(input,etaRMS,phiRMS);
51 
52  //link
53  std::vector<ClusterLink> links = link(input,etaRMS,phiRMS);
54  // for (const auto& link: links)
55  // printf("link %d %d %f %f\n",link.from(),link.to(),link.dR(),link.dZ());
56 
57 
58 
59  std::vector<bool> mask(input.size(),false);
60  std::vector<bool> linked(input.size(),false);
61 
62  //prune
63  std::vector<ClusterLink> prunedLinks;
64  if (links.size())
65  prunedLinks = prune(links,linked);
66 
67  //printf("Pruned links\n")
68  // for (const auto& link: prunedLinks)
69  // printf("link %d %d %f %f\n",link.from(),link.to(),link.dR(),link.dZ());
70 
71 
72 
73  //now we need to build clusters
74  for (unsigned int i=0;i<input.size();++i) {
75  //if not masked
76  if( mask[i])
77  continue;
78  //if not linked just spit it out
79  if (!linked[i]) {
80  output.push_back(input[i]);
81  // printf("Added single cluster with energy =%f \n",input[i].energy());
82  mask[i] = true;
83  continue;
84  }
85 
86 
87  //now business: if linked and not masked gather clusters
88  reco::PFCluster cluster = input[i];
89  mask[i] = true;
90  expandCluster(cluster,i, mask,input,prunedLinks);
91  _allCellsPosCalc->calculateAndSetPosition(cluster);
92  output.push_back(cluster);
93  // printf("Added linked cluster with energy =%f\n",cluster.energy());
94 
95  }
96 }
97 
98 
100 calculateShowerShapes(const reco::PFClusterCollection& clusters,std::vector<double>& etaRMS,std::vector<double>& phiRMS) {
101  float etaSum;
102  float phiSum;
103 
104  //shower shapes. here do not use the fractions
105 
106  for( unsigned int i=0;i<clusters.size();++i ) {
107  const reco::PFCluster& cluster = clusters[i];
108  etaSum=0.0;phiSum=0.0;
109  for (const auto& frac : cluster.recHitFractions()) {
110  etaSum +=frac.fraction()*frac.recHitRef()->energy()*fabs(frac.recHitRef()->position().Eta() -cluster.position().Eta() );
111  phiSum +=frac.fraction()*frac.recHitRef()->energy()*fabs(deltaPhi(frac.recHitRef()->position().Phi(),cluster.position().Phi()));
112  }
113  //protection for single line : assign ~ tower
114  etaRMS[i] = std::max(etaSum/cluster.energy(),0.1);
115  phiRMS[i] = std::max(phiSum/cluster.energy(),0.1);
116 
117  }
118 
119 }
120 
121 
122 std::vector<PFMultiDepthClusterizer::ClusterLink> PFMultiDepthClusterizer::
123 link(const reco::PFClusterCollection& clusters ,const std::vector<double>& etaRMS,const std::vector<double>& phiRMS) {
124 
125  std::vector<ClusterLink> links;
126  //loop on all pairs
127  for (unsigned int i=0;i<clusters.size();++i)
128  for (unsigned int j=0;j<clusters.size();++j) {
129  if (i==j )
130  continue;
131 
132  const reco::PFCluster& cluster1 = clusters[i];
133  const reco::PFCluster& cluster2 = clusters[j];
134 
135  float dz = (cluster2.depth() - cluster1.depth());
136 
137  //Do not link at the same layer and only link inside out!
138  if (dz<0.0 || fabs(dz)<0.2)
139  continue;
140 
141  float deta =(cluster1.position().Eta()-cluster2.position().Eta())*(cluster1.position().Eta()-cluster2.position().Eta())/(etaRMS[i]*etaRMS[i]+etaRMS[j]*etaRMS[j]);
142  float dphi = deltaPhi(cluster1.position().Phi(),cluster2.position().Phi())*deltaPhi(cluster1.position().Phi(),cluster2.position().Phi())/(phiRMS[i]*phiRMS[i]+phiRMS[j]*phiRMS[j]);
143 
144  // 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()));
145 
146  if (deta<nSigmaEta_ && dphi<nSigmaPhi_ )
147  links.push_back(ClusterLink(i,j,deta+dphi,fabs(dz),cluster1.energy()+cluster2.energy()));
148  }
149 
150  return links;
151 }
152 
153 std::vector<PFMultiDepthClusterizer::ClusterLink> PFMultiDepthClusterizer::
154 prune(std::vector<ClusterLink>& links,std::vector<bool>& linkedClusters) {
155  std::vector<ClusterLink> goodLinks ;
156  std::vector<bool> mask(links.size(),false);
157 
158  for (unsigned int i=0;i<links.size()-1;++i) {
159  if (mask[i])
160  continue;
161  for (unsigned int j=i+1;j<links.size();++j) {
162 
163  if (mask[j])
164  continue;
165 
166  const ClusterLink& link1 = links[i];
167  const ClusterLink& link2 = links[j];
168 
169  if (link1.to() == link2.to()) {//found two links going to the same spot,kill one
170  //first prefer nearby layers
171  if (link1.dZ() < link2.dZ()) {
172  mask[j]=true;
173  }
174  else if (link1.dZ() > link2.dZ()) {
175  mask[i] = true;
176  }
177  else if (fabs(link1.dZ()-link2.dZ())<0.2) { //if same layer-pick based on transverse compatibility
178  if (link1.dR()<link2.dR()) {
179  mask[j]=true;
180  }
181  else if (link1.dR()>link2.dR()) {
182  mask[i] = true;
183  }
184  else {
185  //same distance as well -> can happen !!!!! Pick the highest SUME
186  if (link1.energy()<link2.energy())
187  mask[i] = true;
188  else
189  mask[j] = true;
190 
191  }
192  }
193  }
194  }
195  }
196 
197  for (unsigned int i=0;i<links.size();++i) {
198  if (mask[i])
199  continue;
200  goodLinks.push_back(links[i]);
201  linkedClusters[links[i].from()]=true;
202  linkedClusters[links[i].to()]=true;
203  }
204 
205 
206  return goodLinks;
207 }
208 
209 
210 void
213 
214  double e1=0.0;
215  double e2=0.0;
216 
217  //find seeds
218  for ( const auto& fraction : main.recHitFractions())
219  if (fraction.recHitRef()->detId() == main.seed()) {
220  e1=fraction.recHitRef()->energy();
221  }
222 
223  for ( const auto& fraction : added.recHitFractions()) {
224  main.addRecHitFraction(fraction);
225  if (fraction.recHitRef()->detId() == added.seed()) {
226  e2=fraction.recHitRef()->energy();
227  }
228  }
229  if (e2>e1)
230  main.setSeed(added.seed());
231 
232 }
233 
234 
235 void
237 expandCluster(reco::PFCluster& cluster,unsigned int point,std::vector<bool>& mask,const reco::PFClusterCollection& clusters, const std::vector<ClusterLink>& links) {
238  for (const auto& link : links) {
239  if (link.from() == point) {
240  //found link that starts from this guy if not masked absorb
241  if (!mask[link.from()]) {
242  absorbCluster(cluster,clusters[link.from()]);
243  mask[link.from()] = true;
244  }
245 
246  if (!mask[link.to()]) {
247  absorbCluster(cluster,clusters[link.to()]);
248  mask[link.to()]=true;
249  expandCluster(cluster,link.to(),mask,clusters,links);
250  }
251  }
252  if (link.to() == point) {
253  //found link that starts from this guy if not masked absorb
254  if (!mask[link.to()]) {
255  absorbCluster(cluster,clusters[link.to()]);
256  mask[link.to()] = true;
257  }
258 
259  if (!mask[link.from()]) {
260  absorbCluster(cluster,clusters[link.from()]);
261  mask[link.from()]=true;
262  expandCluster(cluster,link.from(),mask,clusters,links);
263  }
264  }
265 
266 
267 
268  }
269 
270 }
T getParameter(std::string const &) const
void calculateShowerShapes(const reco::PFClusterCollection &, std::vector< double > &, std::vector< double > &)
const math::XYZPoint & position() const
cluster centroid position
Definition: CaloCluster.h:126
int i
Definition: DBlmapReader.cc:9
Particle flow cluster, see clustering algorithm in PFClusterAlgo.
Definition: PFCluster.h:47
bool exists(std::string const &parameterName) const
checks if a parameter exists
#define NULL
Definition: scimark2.h:8
void buildClusters(const reco::PFClusterCollection &, const std::vector< bool > &, reco::PFClusterCollection &outclus)
std::vector< ClusterLink > prune(std::vector< ClusterLink > &, std::vector< bool > &linkedClusters)
static std::string const input
Definition: EdmProvDump.cc:44
void setSeed(const DetId &id)
Definition: CaloCluster.h:118
int main(int argc, char **argv)
std::unique_ptr< PFCPositionCalculatorBase > _allCellsPosCalc
void absorbCluster(reco::PFCluster &, const reco::PFCluster &)
PFMultiDepthClusterizer(const edm::ParameterSet &conf)
int j
Definition: DBlmapReader.cc:9
std::vector< ClusterLink > link(const reco::PFClusterCollection &, const std::vector< double > &, const std::vector< double > &)
double energy() const
cluster energy
Definition: PFCluster.h:79
tuple conf
Definition: dbtoconf.py:185
DetId seed() const
return DetId of seed
Definition: CaloCluster.h:202
ParameterSet const & getParameterSet(std::string const &) const
void addRecHitFraction(const reco::PFRecHitFraction &frac)
add a given fraction of the rechit
Definition: PFCluster.cc:57
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:69
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
T get(const Candidate &c)
Definition: component.h:55
*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
void expandCluster(reco::PFCluster &, unsigned int point, std::vector< bool > &mask, const reco::PFClusterCollection &, const std::vector< ClusterLink > &links)
double depth() const
cluster depth
Definition: PFCluster.h:84