LDMX Software
TemplatedClusterFinder.h
1/*
2 TemplatedClusterFinder
3 */
4
5#ifndef ECAL_TEMPLATEDCLUSTERFINDER_H_
6#define ECAL_TEMPLATEDCLUSTERFINDER_H_
7
8#include <math.h>
9
10#include <map>
11
12#include "Ecal/WorkingCluster.h"
13#include "TH2F.h"
14
15namespace ecal {
16
17template <class WeightClass>
18
20 public:
21 void add(const ldmx::EcalHit* eh, const ldmx::EcalGeometry& hex) {
22 clusters_.push_back(WorkingCluster(eh, hex));
23 }
24
25 static bool compClusters(const WorkingCluster& a, const WorkingCluster& b) {
26 return a.centroid().E() > b.centroid().E();
27 }
28
29 void cluster(double seed_threshold, double cutoff) {
30 int ncluster = clusters_.size();
31 double minwgt = cutoff;
32
33 std::sort(clusters_.begin(), clusters_.end(), compClusters);
34 do {
35 bool any = false;
36 size_t mi(0), mj(0);
37
38 int nseeds = 0;
39
40 for (size_t i = 0; i < clusters_.size(); i++) {
41 if (clusters_[i].empty()) continue;
42
43 bool iseed = (clusters_[i].centroid().E() >= seed_threshold);
44 if (iseed) {
45 nseeds++;
46 } else {
47 // Since we sorted initially if we find a hit below seed threshold
48 // it is guaranteed that there will be no seeds after.
49 break;
50 }
51
52 for (size_t j = i + 1; j < clusters_.size(); j++) {
53 if (clusters_[j].empty() ||
54 (!iseed && clusters_[j].centroid().E() < seed_threshold))
55 continue;
56 double wgt = wgt_(clusters_[i], clusters_[j]);
57 if (!any || wgt < minwgt) {
58 any = true;
59 minwgt = wgt;
60 mi = i;
61 mj = j;
62 }
63 }
64 }
65
66 nseeds_ = nseeds;
67 transitionWeights_.insert(std::pair<int, double>(ncluster, minwgt));
68
69 if (any && minwgt < cutoff) {
70 // put the bigger one in mi
71 if (clusters_[mi].centroid().E() < clusters_[mj].centroid().E()) {
72 std::swap(mi, mj);
73 }
74 // now we have the smallest, merge
75 clusters_[mi].add(clusters_[mj]);
76 clusters_[mj].clear();
77 // decrement cluster count
78 ncluster--;
79 }
80
81 } while (minwgt < cutoff && ncluster > 1);
82 finalwgt_ = minwgt;
83 }
84
85 double getYMax() const { return finalwgt_; }
86
87 int getNSeeds() const { return nseeds_; }
88
89 std::map<int, double> getWeights() const { return transitionWeights_; }
90
91 std::vector<WorkingCluster> getClusters() const { return clusters_; }
92
93 private:
94 WeightClass wgt_;
95 double finalwgt_;
96 int nseeds_;
97 std::map<int, double> transitionWeights_;
98 std::vector<WorkingCluster> clusters_;
99};
100} // namespace ecal
101
102#endif
Translation between real-space positions and cell IDs within the ECal.
Stores reconstructed hit information from the ECAL.
Definition EcalHit.h:19
A very simple wrapper enabling us to more easily tell the output stream to style the input word in he...
Definition Hex.h:18