20 std::vector<KDTree> hit_kdtree(2*(
maxlayer+1));
22 std::vector<std::array<float,2> > minpos(2*(
maxlayer+1),{ {0.0f,0.0f} }),maxpos(2*(
maxlayer+1),{ {0.0f,0.0f} });
30 std::cout <<
"-------------------------------------------------------------" << std::endl;
31 std::cout <<
"HGC Imaging algorithm invoked for " << std::endl;
38 for (
unsigned int i=0;
i<hits.
size();++
i) {
49 points[layer].emplace_back(
Hexel(hgrh,detid,isHalf,&
rhtools_),position.
x(),position.
y());
50 if(points[layer].
size()==0){
51 minpos[layer][0] = position.
x(); minpos[layer][1] = position.
y();
52 maxpos[layer][0] = position.
x(); maxpos[layer][1] = position.
y();
54 minpos[layer][0] =
std::min((
float)position.
x(),minpos[layer][0]);
55 minpos[layer][1] =
std::min((
float)position.
y(),minpos[layer][1]);
56 maxpos[layer][0] =
std::max((
float)position.
x(),maxpos[layer][0]);
57 maxpos[layer][1] =
std::max((
float)position.
y(),maxpos[layer][1]);
65 minpos[i][1],maxpos[i][1]);
67 hit_kdtree[
i].build(points[i],bounds);
82 std::vector< std::pair<DetId, float> > thisCluster;
92 std::vector<std::vector<double> > fractions;
100 for(
unsigned isub = 0; isub < fractions.size(); ++isub ) {
101 double effective_hits = 0.0;
106 for(
unsigned ihit = 0; ihit < fractions[isub].size(); ++ihit ) {
107 const double fraction = fractions[isub][ihit];
108 if( fraction > 1
e-7 ) {
111 thisCluster.emplace_back(
current_v[i][ihit].
data.detid,fraction);
118 std::cout <<
"\t******** NEW CLUSTER (SHARING) ********" << std::endl;
119 std::cout <<
"\tEff. No. of cells = " << effective_hits << std::endl;
120 std::cout <<
"\t Energy = " << energy << std::endl;
121 std::cout <<
"\t Phi = " << position.phi() << std::endl;
122 std::cout <<
"\t Eta = " << position.eta() << std::endl;
123 std::cout <<
"\t*****************************" << std::endl;
131 std::vector< KDNode >::iterator it;
134 energy += (*it).data.isHalo ? 0. : (*it).data.weight;
135 thisCluster.emplace_back(std::pair<DetId, float>((*it).data.detid,((*it).data.isHalo?0.:1.)));
139 std::cout <<
"******** NEW CLUSTER (HGCIA) ********" << std::endl;
142 std::cout <<
" Energy = " << energy << std::endl;
143 std::cout <<
" Phi = " << position.phi() << std::endl;
144 std::cout <<
" Eta = " << position.eta() << std::endl;
145 std::cout <<
"*****************************" << std::endl;
156 float total_weight = 0.;
160 for (
unsigned int i = 0;
i < v.size();
i++){
161 if(!v[
i].
data.isHalo){
162 total_weight += v[
i].data.weight;
163 x += v[
i].data.x*v[
i].data.weight;
164 y += v[
i].data.y*v[
i].data.weight;
165 z += v[
i].data.z*v[
i].data.weight;
175 double maxdensity = 0.;
176 for(
unsigned int i = 0;
i < nd.size(); ++
i){
179 std::vector<KDNode>
found;
180 lp.
search(search_box,found);
181 for(
unsigned int j = 0; j < found.size(); j++){
183 nd[
i].data.rho += found[j].data.weight;
184 if(nd[
i].data.rho > maxdensity) maxdensity = nd[
i].data.rho;
197 double maxdensity = 0.0;
198 int nearestHigher = -1;
202 maxdensity = nd[rs[0]].
data.rho;
205 double dist2 = 2500.0;
209 for(
unsigned int j = 0; j < nd.size(); j++){
211 dist2 = tmp > dist2 ? tmp : dist2;
214 nd[rs[0]].data.nearestHigher = nearestHigher;
218 const double max_dist2 = dist2;
220 for(
unsigned int oi = 1; oi < nd.size(); ++oi){
222 unsigned int i = rs[oi];
227 for(
unsigned int oj = 0; oj < oi; oj++){
228 unsigned int j = rs[oj];
236 nd[
i].data.nearestHigher = nearestHigher;
247 unsigned int clusterIndex = 0;
253 for(
unsigned int i =0;
i < nd.size(); ++
i){
257 if(nd[ds[
i]].
data.rho < maxdensity/
kappa )
continue;
260 nd[ds[
i]].data.clusterIndex = clusterIndex;
264 std::cout <<
"Cluster center is hit " << ds[
i] << std::endl;
271 if(clusterIndex==0)
return clusterIndex;
275 for(
unsigned int oi =1; oi < nd.size(); ++oi){
276 unsigned int i = rs[oi];
277 int ci = nd[
i].data.clusterIndex;
279 nd[
i].data.clusterIndex = nd[nd[
i].data.nearestHigher].data.clusterIndex;
287 std::cout <<
"resizing cluster vector by "<< clusterIndex << std::endl;
293 std::vector<double> rho_b(clusterIndex,0.);
297 for(
unsigned int i = 0;
i < nd.size(); ++
i){
298 int ci = nd[
i].data.clusterIndex;
299 bool flag_isolated =
true;
303 std::vector<KDNode>
found;
304 lp.
search(search_box,found);
306 for(
unsigned int j = 1; j < found.size(); j++){
308 if(found[j].
data.clusterIndex!=-1){
310 if(dist <
delta_c && found[j].data.clusterIndex!=ci){
312 nd[
i].data.isBorder =
true;
317 if(dist <
delta_c && dist != 0. && found[j].data.clusterIndex==ci){
319 flag_isolated =
false;
323 if(flag_isolated) nd[
i].data.isBorder =
true;
326 if(nd[
i].
data.isBorder && rho_b[ci] < nd[
i].data.rho)
327 rho_b[ci] = nd[
i].data.rho;
331 for(
unsigned int i = 0;
i < nd.size(); ++
i){
332 int ci = nd[
i].data.clusterIndex;
333 if(ci!=-1 && nd[
i].
data.rho < rho_b[ci])
334 nd[
i].data.isHalo =
true;
335 if(nd[
i].
data.clusterIndex!=-1){
348 std::cout <<
"moving cluster offset by " << clusterIndex << std::endl;
356 std::vector<unsigned>
result;
357 std::vector<bool>
seed(cluster.size(),
true);
359 for(
unsigned i = 0;
i < cluster.size(); ++
i ) {
360 for(
unsigned j = 0; j < cluster.size(); ++j ) {
363 if( cluster[
i].data.weight < cluster[j].data.weight ) {
371 for(
unsigned i = 0 ;
i < cluster.size(); ++
i ) {
384 const std::vector<double>& fractions) {
385 double norm(0.0),
x(0.0),
y(0.0),
z(0.0);
386 for(
unsigned i = 0;
i < hits.size(); ++
i ) {
387 const double weight = fractions[
i]*hits[
i].data.weight;
389 x += weight*hits[
i].data.x;
390 y += weight*hits[
i].data.y;
391 z += weight*hits[
i].data.z;
394 double norm_inv = 1.0/norm;
400 const std::vector<double>& fractions) {
402 for(
unsigned i = 0 ;
i < hits.size(); ++
i ) {
403 result += fractions[
i]*hits[
i].data.weight;
409 const std::vector<unsigned>& seeds,
410 std::vector<std::vector<double> >& outclusters) {
411 std::vector<bool> isaseed(incluster.size(),
false);
413 outclusters.resize(seeds.size());
414 std::vector<Point> centroids(seeds.size());
415 std::vector<double> energies(seeds.size());
417 if( seeds.size() == 1 ) {
418 outclusters[0].clear();
419 outclusters[0].resize(incluster.size(),1.0);
426 for(
unsigned i = 0;
i < seeds.size(); ++
i ) {
427 isaseed[seeds[
i]] =
true;
436 for(
unsigned i = 0;
i < seeds.size(); ++
i ) {
437 outclusters[
i].resize(incluster.size(),0.0);
438 for(
unsigned j = 0; j < incluster.size(); ++j ) {
439 if( j == seeds[
i] ) {
440 outclusters[
i][j] = 1.0;
441 centroids[
i] =
math::XYZPoint(incluster[j].
data.x,incluster[j].data.y,incluster[j].data.z);
442 energies[
i] = incluster[j].data.weight;
454 const unsigned iterMax = 50;
457 const double toleranceScaling =
std::pow(
std::max(1.0,seeds.size()-1.0),2.0);
458 std::vector<Point> prevCentroids;
459 std::vector<double>
frac(seeds.size()), dist2(seeds.size());
460 while( iter++ < iterMax && diff > stoppingTolerance*toleranceScaling ) {
461 for(
unsigned i = 0;
i < incluster.size(); ++
i ) {
462 const Hexel& ihit = incluster[
i].data;
463 double fraction(0.0), fracTot(0.0), d2(0.0);
464 for(
unsigned j = 0; j < seeds.size(); ++j ) {
466 d2 = (
std::pow(ihit.
x - centroids[j].x(),2.0) +
467 std::pow(ihit.
y - centroids[j].y(),2.0) +
471 if(
i == seeds[j] ) {
473 }
else if( isaseed[
i] ) {
483 for(
unsigned j = 0; j < seeds.size(); ++j ) {
484 if( fracTot > minFracTot ||
485 (
i == seeds[j] && fracTot > 0.0 ) ) {
486 outclusters[j][
i] =
frac[j]/fracTot;
488 outclusters[j][
i] = 0.0;
496 centroids.resize(seeds.size());
498 for(
unsigned i = 0;
i < seeds.size(); ++
i ) {
502 const double delta2 = (prevCentroids[
i]-centroids[
i]).
perp2();
503 if( delta2 > diff2 ) diff2 = delta2;
double calculateDistanceToHigher(std::vector< KDNode > &, KDTree &)
double calculateEnergyWithFraction(const std::vector< KDNode > &, const std::vector< double > &)
hgcal::RecHitTools rhtools_
std::vector< std::vector< KDNode > > current_v
math::XYZPoint calculatePositionWithFraction(const std::vector< KDNode > &, const std::vector< double > &)
double distance(const Hexel &pt1, const Hexel &pt2)
reco::CaloCluster::AlgoId algoId
static const unsigned int maxlayer
const DetId & detid() const
double calculateLocalDensity(std::vector< KDNode > &, KDTree &)
void build(std::vector< KDTreeNodeInfoT< DATA, DIM > > &eltList, const KDTreeBoxT< DIM > ®ion)
math::XYZPoint Point
point in the space
void search(const KDTreeBoxT< DIM > &searchBox, std::vector< KDTreeNodeInfoT< DATA, DIM > > &resRecHitList)
void makeClusters(const HGCRecHitCollection &hits)
std::vector< size_t > sort_by_delta(const std::vector< KDNode > &v)
void shareEnergy(const std::vector< KDNode > &, const std::vector< unsigned > &, std::vector< std::vector< double > > &)
unsigned int cluster_offset
std::vector< std::vector< Hexel > > points
std::vector< size_t > sorted_indices(const std::vector< T > &v)
std::vector< reco::BasicCluster > getClusters(bool)
std::vector< unsigned > findLocalMaximaInCluster(const std::vector< KDNode > &)
XYZPointD XYZPoint
point in space with cartesian internal representation
T perp2() const
Squared magnitude of transverse component.
math::XYZPoint calculatePosition(std::vector< KDNode > &)
std::vector< std::vector< double > > tmp
char data[epos_bytes_allocation]
double distance2(const Hexel &pt1, const Hexel &pt2)
static int position[264][3]
int findAndAssignClusters(std::vector< KDNode > &, KDTree &, double, KDTreeBox &)
Power< A, B >::type pow(const A &a, const B &b)
std::vector< reco::BasicCluster > clusters_v