LDMX Software
DBScanClusterBuilder.cxx
1// #include "DetDescr/EcalGeometry.h"
2// #include "Recon/Event/HgcrocDigiCollection.h"
4
5#include <iostream>
6#include <set>
7
8namespace recon {
9
10DBScanClusterBuilder::DBScanClusterBuilder() {
11 min_hit_energy_ = 0;
12 cluster_hit_dist_ = 100;
13 cluster_z_bias_ = 1; // defaults to 1
14 min_cluster_hit_mult_ = 2;
15}
16
17DBScanClusterBuilder::DBScanClusterBuilder(float minHitEnergy,
18 float clusterHitDist,
19 float clusterZBias,
20 float minClusterHitMult) {
21 min_hit_energy_ = minHitEnergy;
22 cluster_hit_dist_ = clusterHitDist;
23 cluster_z_bias_ = clusterZBias; // clustering bias in the z_ direction
24 min_cluster_hit_mult_ = minClusterHitMult;
25}
26
27std::vector<std::vector<const ldmx::CalorimeterHit *> >
28DBScanClusterBuilder::runDBSCAN(
29 const std::vector<const ldmx::CalorimeterHit *> &hits_) {
30 const int n = hits_.size();
31 std::vector<std::vector<const ldmx::CalorimeterHit *> > idx_clusters;
32 std::vector<unsigned int> tried;
33 tried.reserve(n);
34 std::vector<unsigned int> used;
35 used.reserve(n);
36 for (unsigned int i = 0; i < n; i++) {
37 if (isIn(i, tried)) continue;
38 tried.push_back(i);
39 ldmx_log(debug) << "trying " << i;
40 if (hits_[i]->getEnergy() < min_hit_energy_) continue;
41 std::set<unsigned int> neighbors;
42 unsigned int n_nearby = 1;
43 // find neighbors
44 for (unsigned int j = 0; j < n; j++) {
45 if (i != j &&
46 dist(hits_[i], hits_[j]) < cluster_hit_dist_) { // pair-wise distance
47 neighbors.insert(j);
48 if (hits_[j]->getEnergy() >= min_hit_energy_) n_nearby++;
49 }
50 }
51 if (n_nearby >= min_cluster_hit_mult_) {
52 std::vector<const ldmx::CalorimeterHit *> idx_cluster{
53 hits_[i]}; // start a cluster
54 used.push_back(i);
55 ldmx_log(debug) << "- starting a cluster from " << i;
56 for (unsigned int j : neighbors) {
57 if (!isIn(j, tried)) {
58 tried.push_back(j);
59 ldmx_log(debug) << "== tried " << j;
60 std::vector<unsigned int> neighbors2;
61 for (unsigned int k = 0; k < n; k++) {
62 if (dist(hits_[k], hits_[j]) < cluster_hit_dist_) {
63 neighbors2.push_back(k);
64 }
65 }
66 for (unsigned int k : neighbors2) neighbors.insert(k);
67 }
68 if (!isIn(j, used)) {
69 ldmx_log(debug) << "== used " << j;
70 used.push_back(j);
71 idx_cluster.push_back(hits_[j]);
72 }
73 }
74 idx_clusters.push_back(idx_cluster);
75 }
76 }
77 ldmx_log(debug) << "done. writing this many clusters out: "
78 << idx_clusters.size();
79 return idx_clusters;
80}
81
82void DBScanClusterBuilder::fillClusterInfoFromHits(
83 ldmx::CaloCluster *cl, std::vector<const ldmx::CalorimeterHit *> hits_,
84 bool logEnergyWeight) {
85 float e(0), x(0), y(0), z(0), xx(0), yy(0), zz(0), n(0);
86 float w = 1; // weight
87 float sumw = 0;
88 std::vector<float> raw_xvals{};
89 std::vector<float> raw_yvals{};
90 std::vector<float> raw_zvals{};
91 std::vector<float> raw_evals{};
92 std::vector<const ldmx::CalorimeterHit *> constituent_hits;
93
94 for (const ldmx::CalorimeterHit *h : hits_) {
95 if (h->getEnergy() < min_hit_energy_) continue;
96 if (logEnergyWeight) w = log(h->getEnergy()) - log(min_hit_energy_);
97 e += h->getEnergy();
98 x += w * h->getXPos();
99 y += w * h->getYPos();
100 z += w * h->getZPos();
101 xx += w * h->getXPos() * h->getXPos();
102 yy += w * h->getYPos() * h->getYPos();
103 zz += w * h->getZPos() * h->getZPos();
104 n += 1;
105 sumw += w;
106 raw_xvals.push_back(h->getXPos());
107 raw_yvals.push_back(h->getYPos());
108 raw_zvals.push_back(h->getZPos());
109 raw_evals.push_back(h->getEnergy());
110 constituent_hits.emplace_back(h);
111 } // over hits_
112 x /= sumw; // now is <x_>
113 y /= sumw;
114 z /= sumw;
115 xx /= sumw; // now is <x_^2>
116 yy /= sumw;
117 zz /= sumw;
118 xx = sqrt(xx - x * x); // now is sqrt(<x_^2>-<x_>^2)
119 yy = sqrt(yy - y * y);
120 zz = sqrt(zz - z * z);
121 cl->setEnergy(e);
122 cl->setNHits(n);
123 cl->setCentroidXYZ(x, y, z);
124 cl->setRMSXYZ(xx, yy, zz);
125 cl->setHitValsX(raw_xvals);
126 cl->setHitValsY(raw_yvals);
127 cl->setHitValsZ(raw_zvals);
128 cl->setHitValsE(raw_evals);
129 cl->addHits(constituent_hits); // associate used hits_ to cluster
130
131 if (raw_xvals.size() > 2) {
132 // skip fits for 'vertical' clusters
133 std::vector<float> sorted_z = raw_zvals;
134 std::sort(sorted_z.begin(), sorted_z.end());
135 if ((sorted_z.size() > 2) and (sorted_z.back() - sorted_z.front() > 1e3)) {
136 for (int i = 0; i < raw_xvals.size(); i++) { // mean subtract
137 raw_xvals[i] = raw_xvals[i] - x;
138 raw_yvals[i] = raw_yvals[i] - y;
139 raw_zvals[i] = raw_zvals[i] - z;
140 }
141
142 TGraph gxz(raw_zvals.size(), raw_zvals.data(), raw_xvals.data());
143 auto r_xz = gxz.Fit("pol1", "SQ"); // p0 + x_*p1
144 cl->setDXDZ(r_xz->Value(1));
145 cl->setEDXDZ(r_xz->ParError(1));
146
147 TGraph gyz(raw_zvals.size(), raw_zvals.data(), raw_yvals.data());
148 auto r_yz = gyz.Fit("pol1", "SQ"); // p0 + x_*p1
149 cl->setDYDZ(r_yz->Value(1));
150 cl->setEDYDZ(r_yz->ParError(1));
151 }
152 }
153 return;
154}
155
156} // namespace recon
Implementation of DBSCAN clustering algo.
Stores cluster information from the ECal.
Definition CaloCluster.h:26
void setNHits(int nHits)
Sets total number of hits in the cluster.
Definition CaloCluster.h:65
void addHits(const std::vector< const ldmx::CalorimeterHit * > hitsVec)
Take in the hits that make up the cluster.
void setCentroidXYZ(double centroid_x, double centroid_y, double centroid_z)
Sets the three coordinates of the cluster centroid.
Definition CaloCluster.h:85
void setEnergy(double energy)
Sets total energy for the cluster.
Definition CaloCluster.h:59
Represents a reconstructed hit in a calorimeter cell within the detector.