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, const 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 22 of file DBScanClusterBuilder.h.

Constructor & Destructor Documentation

◆ DBScanClusterBuilder() [1/2]

recon::DBScanClusterBuilder::DBScanClusterBuilder ( )

Definition at line 12 of file DBScanClusterBuilder.cxx.

12 {
13 min_hit_energy_ = 0;
14 cluster_hit_dist_ = 100;
15 cluster_z_bias_ = 1; // defaults to 1
16 min_cluster_hit_mult_ = 2;
17}

◆ DBScanClusterBuilder() [2/2]

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

Definition at line 19 of file DBScanClusterBuilder.cxx.

22 {
23 min_hit_energy_ = minHitEnergy;
24 cluster_hit_dist_ = clusterHitDist;
25 cluster_z_bias_ = clusterZBias; // clustering bias in the z_ direction
26 min_cluster_hit_mult_ = minClusterHitMult;
27}

Member Function Documentation

◆ dist()

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

Definition at line 58 of file DBScanClusterBuilder.h.

58 {
59 return sqrt(pow(a->getXPos() - b->getXPos(), 2) // distance
60 + pow(a->getYPos() - b->getYPos(), 2) +
61 pow((a->getZPos() - b->getZPos()) / cluster_z_bias_,
62 2)); // divide by the z_ bias
63 }
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 84 of file DBScanClusterBuilder.cxx.

86 {
87 float e(0), x(0), y(0), z(0), xx(0), yy(0), zz(0), n(0);
88 float w = 1; // weight
89 float sumw = 0;
90 std::vector<float> raw_xvals{};
91 std::vector<float> raw_yvals{};
92 std::vector<float> raw_zvals{};
93 std::vector<float> raw_evals{};
94 std::vector<const ldmx::CalorimeterHit*> constituent_hits;
95
96 for (const ldmx::CalorimeterHit* h : hits_) {
97 if (h->getEnergy() < min_hit_energy_) continue;
98 if (logEnergyWeight) w = log(h->getEnergy()) - log(min_hit_energy_);
99 e += h->getEnergy();
100 x += w * h->getXPos();
101 y += w * h->getYPos();
102 z += w * h->getZPos();
103 xx += w * h->getXPos() * h->getXPos();
104 yy += w * h->getYPos() * h->getYPos();
105 zz += w * h->getZPos() * h->getZPos();
106 n += 1;
107 sumw += w;
108 if (saveHitContribs) {
109 raw_xvals.push_back(h->getXPos());
110 raw_yvals.push_back(h->getYPos());
111 raw_zvals.push_back(h->getZPos());
112 raw_evals.push_back(h->getEnergy());
113 constituent_hits.emplace_back(h);
114 }
115 } // over hits_
116 x /= sumw; // now is <x_>
117 y /= sumw;
118 z /= sumw;
119 xx /= sumw; // now is <x_^2>
120 yy /= sumw;
121 zz /= sumw;
122 xx = sqrt(xx - x * x); // now is sqrt(<x_^2>-<x_>^2)
123 yy = sqrt(yy - y * y);
124 zz = sqrt(zz - z * z);
125 cl->setEnergy(e);
126 cl->setNHits(n);
127 cl->setCentroidXYZ(x, y, z);
128 cl->setRMSXYZ(xx, yy, zz);
129 if (saveHitContribs) {
130 cl->setHitValsX(raw_xvals);
131 cl->setHitValsY(raw_yvals);
132 cl->setHitValsZ(raw_zvals);
133 cl->setHitValsE(raw_evals);
134 cl->addHits(constituent_hits); // associate used hits_ to cluster
135 }
136
137 if (raw_xvals.size() > 2) {
138 // skip fits for 'vertical' clusters
139 std::vector<float> sorted_z = raw_zvals;
140 std::sort(sorted_z.begin(), sorted_z.end());
141 if ((sorted_z.size() > 2) and (sorted_z.back() - sorted_z.front() > 1e3)) {
142 for (int i = 0; i < raw_xvals.size(); i++) { // mean subtract
143 raw_xvals[i] = raw_xvals[i] - x;
144 raw_yvals[i] = raw_yvals[i] - y;
145 raw_zvals[i] = raw_zvals[i] - z;
146 }
147
148 TGraph gxz(raw_zvals.size(), raw_zvals.data(), raw_xvals.data());
149 auto r_xz = gxz.Fit("pol1", "SQ"); // p0 + x_*p1
150 cl->setDXDZ(r_xz->Value(1));
151 cl->setEDXDZ(r_xz->ParError(1));
152
153 TGraph gyz(raw_zvals.size(), raw_zvals.data(), raw_yvals.data());
154 auto r_yz = gyz.Fit("pol1", "SQ"); // p0 + x_*p1
155 cl->setDYDZ(r_yz->Value(1));
156 cl->setEDYDZ(r_yz->ParError(1));
157 }
158 }
159 return;
160}
void setNHits(int nHits)
Sets total number of hits in the cluster.
Definition CaloCluster.h:64
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:84
void setEnergy(double energy)
Sets total energy for the cluster.
Definition CaloCluster.h:58
Represents a reconstructed hit in a calorimeter cell within the detector.

◆ getMinHitEnergy()

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

Definition at line 48 of file DBScanClusterBuilder.h.

48{ return min_hit_energy_; };

◆ isIn()

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

Definition at line 55 of file DBScanClusterBuilder.h.

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

◆ runDBSCAN()

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

Definition at line 30 of file DBScanClusterBuilder.cxx.

31 {
32 const int n = hits_.size();
33 std::vector<std::vector<const ldmx::CalorimeterHit*> > idx_clusters;
34 std::vector<unsigned int> tried;
35 tried.reserve(n);
36 std::vector<unsigned int> used;
37 used.reserve(n);
38 for (unsigned int i = 0; i < n; i++) {
39 if (isIn(i, tried)) continue;
40 tried.push_back(i);
41 ldmx_log(debug) << "trying " << i;
42 if (hits_[i]->getEnergy() < min_hit_energy_) continue;
43 std::set<unsigned int> neighbors;
44 unsigned int n_nearby = 1;
45 // find neighbors
46 for (unsigned int j = 0; j < n; j++) {
47 if (i != j &&
48 dist(hits_[i], hits_[j]) < cluster_hit_dist_) { // pair-wise distance
49 neighbors.insert(j);
50 if (hits_[j]->getEnergy() >= min_hit_energy_) n_nearby++;
51 }
52 }
53 if (n_nearby >= min_cluster_hit_mult_) {
54 std::vector<const ldmx::CalorimeterHit*> idx_cluster{
55 hits_[i]}; // start a cluster
56 used.push_back(i);
57 ldmx_log(debug) << "- starting a cluster from " << i;
58 for (unsigned int j : neighbors) {
59 if (!isIn(j, tried)) {
60 tried.push_back(j);
61 ldmx_log(debug) << "== tried " << j;
62 std::vector<unsigned int> neighbors2;
63 for (unsigned int k = 0; k < n; k++) {
64 if (dist(hits_[k], hits_[j]) < cluster_hit_dist_) {
65 neighbors2.push_back(k);
66 }
67 }
68 for (unsigned int k : neighbors2) neighbors.insert(k);
69 }
70 if (!isIn(j, used)) {
71 ldmx_log(debug) << "== used " << j;
72 used.push_back(j);
73 idx_cluster.push_back(hits_[j]);
74 }
75 }
76 idx_clusters.push_back(idx_cluster);
77 }
78 }
79 ldmx_log(debug) << "done. writing this many clusters out: "
80 << idx_clusters.size();
81 return idx_clusters;
82}

◆ setMinHitDistance() [1/2]

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

Definition at line 50 of file DBScanClusterBuilder.h.

50{ return cluster_hit_dist_; }

◆ setMinHitDistance() [2/2]

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

Definition at line 40 of file DBScanClusterBuilder.h.

40{ cluster_hit_dist_ = x_; }

◆ setMinHitEnergy()

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

Definition at line 38 of file DBScanClusterBuilder.h.

38{ min_hit_energy_ = x_; }

◆ setMinHitMultiplicity() [1/2]

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

Definition at line 52 of file DBScanClusterBuilder.h.

52{ return min_cluster_hit_mult_; }

◆ setMinHitMultiplicity() [2/2]

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

Definition at line 46 of file DBScanClusterBuilder.h.

46{ min_cluster_hit_mult_ = x_; }

◆ setZBias()

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

Definition at line 42 of file DBScanClusterBuilder.h.

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

Member Data Documentation

◆ cluster_hit_dist_

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

Definition at line 66 of file DBScanClusterBuilder.h.

66{100.};

◆ cluster_z_bias_

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

Definition at line 67 of file DBScanClusterBuilder.h.

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

◆ min_cluster_hit_mult_

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

Definition at line 68 of file DBScanClusterBuilder.h.

68{2};

◆ min_hit_energy_

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

Definition at line 65 of file DBScanClusterBuilder.h.

65{0};

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