1#include "Trigger/IdealClusterBuilder.h"
5void ClusterGeometry::AddTP(
int tid,
int cell_id,
int module_id,
float x,
7 id_map[tid] = std::make_pair(cell_id, module_id);
8 reverse_id_map[std::make_pair(cell_id, module_id)] = tid;
9 positions[tid] = std::make_pair(x, y);
11void ClusterGeometry::AddNeighbor(
int id1,
int id2) {
12 if (neighbors.count(id1))
13 neighbors[id1].push_back(id2);
15 neighbors[id1] = {id2};
16 if (neighbors.count(id2))
17 neighbors[id2].push_back(id1);
19 neighbors[id2] = {id1};
22bool ClusterGeometry::CheckNeighbor(
int id1,
int id2) {
24 auto &ns = neighbors[id1];
25 return std::find(ns.begin(), ns.end(), id2) != ns.end();
27void ClusterGeometry::Initialize() {
30 for (
auto pair1 = id_map.begin(); pair1 != id_map.end(); pair1++) {
31 for (
auto pair2 = pair1; pair2 != id_map.end(); pair2++) {
32 if (pair1 == pair2)
continue;
33 auto &id1 = pair1->first;
34 auto &id2 = pair2->first;
35 auto &xy1 = positions[id1];
36 auto &xy2 = positions[id2];
38 sqrt(pow(xy1.first - xy2.first, 2) + pow(xy1.second - xy2.second, 2));
39 distances[std::make_pair(id1, id2)] = d;
40 distances[std::make_pair(id2, id1)] = d;
44 float n_dist = 1.8 * GetDist(GetID(0, 0), GetID(1, 0));
46 for (
auto pair1 = id_map.begin(); pair1 != id_map.end(); pair1++) {
47 for (
auto pair2 = pair1; pair2 != id_map.end(); pair2++) {
48 if (pair1 == pair2)
continue;
49 if (GetDist(pair1->first, pair2->first) < n_dist)
50 AddNeighbor(pair1->first, pair2->first);
53 is_initialized =
true;
58std::vector<Cluster> IdealClusterBuilder::Build2dClustersLayer(
59 std::vector<Hit> hits) {
61 std::map<int, Hit> hits_by_id;
62 for (
auto &hit : hits) hits_by_id[hit.id] = hit;
65 cout <<
"--------\nBuild2dClustersLayer Input Hits" << endl;
66 for (
auto &hitpair : hits_by_id) hitpair.second.Print();
70 std::vector<Cluster> clusters;
71 for (
auto &hitpair : hits_by_id) {
72 auto &hit = hitpair.second;
73 bool isLocalMax =
true;
74 for (
auto n : g->neighbors[hit.id]) {
76 if (hits_by_id.count(n) && hits_by_id[n].e > hit.e) isLocalMax =
false;
80 if (isLocalMax && (hit.e > seed_thresh)) {
83 c.hits.push_back(hit);
89 c.module = g->id_map[hit.id].second;
91 clusters.push_back(c);
96 cout <<
"--------\nAfter seed-finding" << endl;
97 for (
auto &hitpair : hits_by_id) hitpair.second.Print();
98 for (
auto &c : clusters) c.Print();
103 while (i_neighbor < n_neighbors) {
105 std::map<int, std::vector<int> > assoc_clus2hitIDs;
106 for (
int iclus = 0; iclus < clusters.size(); iclus++) {
107 auto &clus = clusters[iclus];
108 std::vector<int> neighbors;
109 for (
const auto &hit : clus.hits) {
110 for (
auto n : g->neighbors[hit.id]) {
111 if (hits_by_id.count(n) && !hits_by_id[n].used &&
112 hits_by_id[n].e > neighb_thresh) {
113 neighbors.push_back(n);
117 assoc_clus2hitIDs[iclus] = neighbors;
121 std::map<int, std::vector<int> > assoc_hitID2clusters;
122 for (
auto clus2hitID : assoc_clus2hitIDs) {
123 auto iclus = clus2hitID.first;
124 auto &hitIDs = clus2hitID.second;
125 for (
const auto &hitID : hitIDs) {
126 if (assoc_hitID2clusters.count(hitID))
127 assoc_hitID2clusters[hitID].push_back(iclus);
129 assoc_hitID2clusters[hitID] = {iclus};
135 for (
auto hitID2clusters : assoc_hitID2clusters) {
136 auto hitID = hitID2clusters.first;
137 auto iclusters = hitID2clusters.second;
138 if (iclusters.size() == 1) {
140 auto &hit = hits_by_id[hitID];
141 auto iclus = iclusters[0];
143 clusters[iclus].hits.push_back(hit);
144 clusters[iclus].e += hit.e;
146 auto &hit = hits_by_id[hitID];
149 for (
auto iclus : iclusters) {
150 esum += clusters[iclus].e;
152 for (
auto iclus : iclusters) {
154 if (split_energy) newHit.e = hit.e * clusters[iclus].e / esum;
155 clusters[iclus].hits.push_back(newHit);
156 clusters[iclus].e += newHit.e;
162 for (
auto &c : clusters) {
171 for (
auto hit : c.hits) {
175 float w = std::max(0., log(hit.e / MIN_TP_ENERGY));
179 c.xx += hit.x * hit.x * w;
180 c.yy += hit.y * hit.y * w;
181 c.zz += hit.z * hit.z * w;
190 c.xx = sqrt(c.xx - c.x * c.x);
191 c.yy = sqrt(c.yy - c.y * c.y);
192 c.zz = sqrt(c.zz - c.z * c.z);
198 cout <<
"--------\nAfter " << i_neighbor <<
" neighbors" << endl;
199 for (
auto &hitpair : hits_by_id) hitpair.second.Print();
200 for (
auto &c : clusters) c.Print();
207void IdealClusterBuilder::Build2dClusters() {
209 std::map<int, std::vector<Hit> > layer_hits;
210 for (
const auto hit : all_hits) {
212 if (layer_hits.count(l)) {
213 layer_hits[l].push_back(hit);
215 layer_hits[l] = {hit};
220 for (
auto &pair : layer_hits) {
222 cout <<
"Found " << pair.second.size() <<
" hits in layer " << pair.first
225 auto clus = Build2dClustersLayer(pair.second);
226 all_clusters.insert(all_clusters.end(), clus.begin(), clus.end());
230void IdealClusterBuilder::Build3dClusters() {
232 cout <<
"--------\nBuilding 3d clusters" << endl;
236 std::vector<std::vector<Cluster> > layer_clusters;
237 layer_clusters.resize(LAYER_MAX);
238 for (
auto &clus : all_clusters) {
239 layer_clusters[clus.layer].push_back(clus);
243 for (
auto &clusters : layer_clusters) ESort(clusters);
246 cout <<
"--------\n3d: sorted 2d inputs" << endl;
247 for (
auto &clusters : layer_clusters)
248 for (auto &c : clusters) c.Print(g);
253 bool building =
true;
254 std::vector<Cluster> clusters3d;
258 cluster3d.is2D =
false;
259 cluster3d.first_layer = LAYER_SHOWERMAX;
260 cluster3d.last_layer = LAYER_SHOWERMAX;
262 for (
int ilayer = 0; ilayer < LAYER_MAX; ilayer++) {
263 if (LAYER_SHOWERMAX + ilayer < LAYER_MAX) {
265 test_layer = LAYER_SHOWERMAX + ilayer;
268 test_layer = LAYER_MAX - ilayer - 1;
271 auto &clusters2d = layer_clusters[test_layer];
274 if (cluster3d.depth == 0) {
276 if (test_layer > LAYER_SEEDMAX || test_layer < LAYER_SEEDMIN)
continue;
277 if (clusters2d.size()) {
279 cout <<
" 3d seed: ";
280 clusters2d[0].Print(g);
284 cluster3d.clusters2d = {clusters2d[0]};
285 cluster3d.first_layer = test_layer;
286 cluster3d.last_layer = test_layer;
289 clusters2d.erase(clusters2d.begin());
295 auto &last_seed2d = cluster3d.clusters2d.back().seed;
297 if (test_layer == LAYER_SHOWERMAX - 1)
298 last_seed2d = cluster3d.clusters2d.front().seed;
302 cout <<
" check 3d w/ seed id " << last_seed2d << endl;
305 for (
int iclus2d = 0; iclus2d < clusters2d.size(); iclus2d++) {
307 cout <<
" -- " << iclus2d << endl;
311 auto &seed2d = clusters2d[iclus2d].seed;
312 if (last_seed2d == seed2d || g->CheckNeighbor(last_seed2d, seed2d)) {
318 cluster3d.clusters2d.push_back(clusters2d[iclus2d]);
320 if (test_layer < cluster3d.first_layer)
321 cluster3d.first_layer = test_layer;
322 if (test_layer > cluster3d.last_layer)
323 cluster3d.last_layer = test_layer;
325 clusters2d.erase(clusters2d.begin() + iclus2d);
333 if (cluster3d.depth == 0) {
337 if (cluster3d.depth >= DEPTH_GOOD) clusters3d.push_back(cluster3d);
342 for (
auto &c : clusters3d) {
351 for (
auto &c2 : c.clusters2d) {
354 float w = std::max(0., log(c2.e / MIN_TP_ENERGY));
358 c.xx += c2.x * c2.x * w;
359 c.yy += c2.y * c2.y * w;
360 c.zz += c2.z * c2.z * w;
372 c.xx = sqrt(c.xx - c.x * c.x);
373 c.yy = sqrt(c.yy - c.y * c.y);
374 c.zz = sqrt(c.zz - c.z * c.z);
379 cout <<
"--------\nFound 3d clusters" << endl;
380 for (
auto &c : clusters3d) c.Print3d();
407 all_clusters.clear();
408 all_clusters.insert(all_clusters.begin(), clusters3d.begin(),
412void IdealClusterBuilder::BuildClusters() {
414 cout <<
"--------\nAll hits" << endl;
415 for (
auto &hit : all_hits) hit.Print();
420 std::map<int, Hit> towers;
421 for (
const auto hit : all_hits) {
422 if (towers.count(hit.id)) {
423 towers[hit.id].e += hit.e;
424 towers[hit.id].nSubHit++;
426 towers[hit.id] = hit;
427 towers[hit.id].layer = 0;
428 towers[hit.id].z = 0;
429 towers[hit.id].nSubHit = 1;
433 for (
const auto t : towers) all_hits.push_back(t.second);
436 cout <<
"--------\nHits after towers" << endl;
437 for (
auto &hit : all_hits) hit.Print();
450void IdealClusterBuilder::Fit(
Cluster &c3) {
455 if (c3.clusters2d.size() < 4)
return;
458 std::vector<float> x;
459 std::vector<float> y;
460 std::vector<float> z;
461 for (
const auto &c2 : c3.clusters2d) {
467 TGraph gxz(z.size(), z.data(), x.data());
468 auto r_xz = gxz.Fit(
"pol1",
"SQ");
469 c3.dxdz = r_xz->Value(1);
470 c3.dxdze = r_xz->ParError(1);
472 TGraph gyz(z.size(), z.data(), y.data());
473 auto r_yz = gyz.Fit(
"pol1",
"SQ");
474 c3.dydz = r_yz->Value(1);
475 c3.dydze = r_yz->ParError(1);