LDMX Software
recon::DBScanClusterBuilder Class Reference

Public Member Functions

 DBScanClusterBuilder (float minHitEnergy, float clusterHitDist, float clusterZBias, float minClusterHitMult)
 
std::vector< std::vector< const ldmx::CalorimeterHit * > > runDBSCAN (const std::vector< const ldmx::CalorimeterHit * > &hits_)
 
void fillClusterInfoFromHits (ldmx::CaloCluster *cl, std::vector< const ldmx::CalorimeterHit * > hits_, bool logEnergyWeight, bool saveHitContribs=true)
 
void setMinHitEnergy (float x_)
 
void setMinHitDistance (float x_)
 
void setZBias (float x_)
 
void setMinHitMultiplicity (int x_)
 
float getMinHitEnergy () const
 
float setMinHitDistance () const
 
int setMinHitMultiplicity () const
 

Private Member Functions

bool isIn (unsigned int i, std::vector< unsigned int > l)
 
float dist (const ldmx::CalorimeterHit *a, const ldmx::CalorimeterHit *b)
 

Private Attributes

float min_hit_energy_ {0}
 
float cluster_hit_dist_ {100.}
 
float cluster_z_bias_ {1.}
 
int min_cluster_hit_mult_ {2}
 

Detailed Description

Definition at line 21 of file DBScanClusterBuilder.h.

Constructor & Destructor Documentation

◆ DBScanClusterBuilder() [1/2]

recon::DBScanClusterBuilder::DBScanClusterBuilder ( )

Definition at line 10 of file DBScanClusterBuilder.cxx.

10 {
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}

◆ DBScanClusterBuilder() [2/2]

recon::DBScanClusterBuilder::DBScanClusterBuilder ( float minHitEnergy,
float clusterHitDist,
float clusterZBias,
float minClusterHitMult )

Definition at line 17 of file DBScanClusterBuilder.cxx.

20 {
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}

Member Function Documentation

◆ dist()

float recon::DBScanClusterBuilder::dist ( const ldmx::CalorimeterHit * a,
const ldmx::CalorimeterHit * b )
inlineprivate

Definition at line 57 of file DBScanClusterBuilder.h.

57 {
58 return sqrt(pow(a->getXPos() - b->getXPos(), 2) // distance
59 + pow(a->getYPos() - b->getYPos(), 2) +
60 pow((a->getZPos() - b->getZPos()) / cluster_z_bias_,
61 2)); // divide by the z_ bias
62 }
float getYPos() const
Get the Y position of the hit [mm].
float getZPos() const
Get the Z position of the hit [mm].
float getXPos() const
Get the X position of the hit [mm].

◆ fillClusterInfoFromHits()

void recon::DBScanClusterBuilder::fillClusterInfoFromHits ( ldmx::CaloCluster * cl,
std::vector< const ldmx::CalorimeterHit * > hits_,
bool logEnergyWeight,
bool saveHitContribs = true )

Definition at line 82 of file DBScanClusterBuilder.cxx.

84 {
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 if (saveHitContribs) {
107 raw_xvals.push_back(h->getXPos());
108 raw_yvals.push_back(h->getYPos());
109 raw_zvals.push_back(h->getZPos());
110 raw_evals.push_back(h->getEnergy());
111 constituent_hits.emplace_back(h);
112 }
113 } // over hits_
114 x /= sumw; // now is <x_>
115 y /= sumw;
116 z /= sumw;
117 xx /= sumw; // now is <x_^2>
118 yy /= sumw;
119 zz /= sumw;
120 xx = sqrt(xx - x * x); // now is sqrt(<x_^2>-<x_>^2)
121 yy = sqrt(yy - y * y);
122 zz = sqrt(zz - z * z);
123 cl->setEnergy(e);
124 cl->setNHits(n);
125 cl->setCentroidXYZ(x, y, z);
126 cl->setRMSXYZ(xx, yy, zz);
127 if (saveHitContribs) {
128 cl->setHitValsX(raw_xvals);
129 cl->setHitValsY(raw_yvals);
130 cl->setHitValsZ(raw_zvals);
131 cl->setHitValsE(raw_evals);
132 cl->addHits(constituent_hits); // associate used hits_ to cluster
133 }
134
135 if (raw_xvals.size() > 2) {
136 // skip fits for 'vertical' clusters
137 std::vector<float> sorted_z = raw_zvals;
138 std::sort(sorted_z.begin(), sorted_z.end());
139 if ((sorted_z.size() > 2) and (sorted_z.back() - sorted_z.front() > 1e3)) {
140 for (int i = 0; i < raw_xvals.size(); i++) { // mean subtract
141 raw_xvals[i] = raw_xvals[i] - x;
142 raw_yvals[i] = raw_yvals[i] - y;
143 raw_zvals[i] = raw_zvals[i] - z;
144 }
145
146 TGraph gxz(raw_zvals.size(), raw_zvals.data(), raw_xvals.data());
147 auto r_xz = gxz.Fit("pol1", "SQ"); // p0 + x_*p1
148 cl->setDXDZ(r_xz->Value(1));
149 cl->setEDXDZ(r_xz->ParError(1));
150
151 TGraph gyz(raw_zvals.size(), raw_zvals.data(), raw_yvals.data());
152 auto r_yz = gyz.Fit("pol1", "SQ"); // p0 + x_*p1
153 cl->setDYDZ(r_yz->Value(1));
154 cl->setEDYDZ(r_yz->ParError(1));
155 }
156 }
157 return;
158}
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.

◆ getMinHitEnergy()

float recon::DBScanClusterBuilder::getMinHitEnergy ( ) const
inline

Definition at line 47 of file DBScanClusterBuilder.h.

47{ return min_hit_energy_; };

◆ isIn()

bool recon::DBScanClusterBuilder::isIn ( unsigned int i,
std::vector< unsigned int > l )
inlineprivate

Definition at line 54 of file DBScanClusterBuilder.h.

54 {
55 return std::find(l.begin(), l.end(), i) != l.end();
56 }

◆ runDBSCAN()

std::vector< std::vector< const ldmx::CalorimeterHit * > > recon::DBScanClusterBuilder::runDBSCAN ( const std::vector< const ldmx::CalorimeterHit * > & hits_)

Definition at line 28 of file DBScanClusterBuilder.cxx.

29 {
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}

◆ setMinHitDistance() [1/2]

float recon::DBScanClusterBuilder::setMinHitDistance ( ) const
inline

Definition at line 49 of file DBScanClusterBuilder.h.

49{ return cluster_hit_dist_; }

◆ setMinHitDistance() [2/2]

void recon::DBScanClusterBuilder::setMinHitDistance ( float x_)
inline

Definition at line 39 of file DBScanClusterBuilder.h.

39{ cluster_hit_dist_ = x_; }

◆ setMinHitEnergy()

void recon::DBScanClusterBuilder::setMinHitEnergy ( float x_)
inline

Definition at line 37 of file DBScanClusterBuilder.h.

37{ min_hit_energy_ = x_; }

◆ setMinHitMultiplicity() [1/2]

int recon::DBScanClusterBuilder::setMinHitMultiplicity ( ) const
inline

Definition at line 51 of file DBScanClusterBuilder.h.

51{ return min_cluster_hit_mult_; }

◆ setMinHitMultiplicity() [2/2]

void recon::DBScanClusterBuilder::setMinHitMultiplicity ( int x_)
inline

Definition at line 45 of file DBScanClusterBuilder.h.

45{ min_cluster_hit_mult_ = x_; }

◆ setZBias()

void recon::DBScanClusterBuilder::setZBias ( float x_)
inline

Definition at line 41 of file DBScanClusterBuilder.h.

41 {
42 cluster_z_bias_ = x_;
43 } // set the z_ bias of the cluster

Member Data Documentation

◆ cluster_hit_dist_

float recon::DBScanClusterBuilder::cluster_hit_dist_ {100.}
private

Definition at line 65 of file DBScanClusterBuilder.h.

65{100.};

◆ cluster_z_bias_

float recon::DBScanClusterBuilder::cluster_z_bias_ {1.}
private

Definition at line 66 of file DBScanClusterBuilder.h.

66{1.}; // private parameter for z_ bias

◆ min_cluster_hit_mult_

int recon::DBScanClusterBuilder::min_cluster_hit_mult_ {2}
private

Definition at line 67 of file DBScanClusterBuilder.h.

67{2};

◆ min_hit_energy_

float recon::DBScanClusterBuilder::min_hit_energy_ {0}
private

Definition at line 64 of file DBScanClusterBuilder.h.

64{0};

The documentation for this class was generated from the following files: