EICrecon
JANA based reconstruction for the EPIC detector
Loading...
Searching...
No Matches
clusterizer_MA.h
Go to the documentation of this file.
1// Copyright 2023, Friederike Bock
2// Subject to the terms in the LICENSE file found in the top-level directory.
3//
4// Sections Copyright (C) 2023 Friederike Bock
5// under SPDX-License-Identifier: LGPL-3.0-or-later
6
7#pragma once
8
9#include <vector>
10#include <TVector3.h>
11
14 : energy(0)
15 , time(0)
16 , posx(0)
17 , posy(0)
18 , posz(0)
19 , cellID(0)
20 , cellIDx(-1)
21 , cellIDy(-1)
22 , cellIDz(-1)
23 , tower_trueID(-10000)
25 , tower_clusterIDB(-1) {}
26 float energy;
27 float time;
28 float posx;
29 float posy;
30 float posz;
31 int cellID;
38};
39
40bool acompare(towersStrct lhs, towersStrct rhs) { return lhs.energy > rhs.energy; }
41
44 : cluster_E(0.)
45 , cluster_seed(0.)
46 , cluster_Eta(-10.)
47 , cluster_Phi(-10.)
48 , cluster_X(0.)
49 , cluster_Y(0.)
50 , cluster_Z(0.)
51 , cluster_M02(0.)
52 , cluster_M20(0.)
54 , cluster_trueID(-10000)
55 , cluster_NtrueID(0) {}
56 float cluster_E;
60 float cluster_X;
61 float cluster_Y;
62 float cluster_Z;
68 std::vector<towersStrct> cluster_towers;
69};
70
71bool acompareCl(clustersStrct lhs, clustersStrct rhs) { return lhs.cluster_E > rhs.cluster_E; }
72
73//**************************************************************************************************************//
74//**************************************************************************************************************//
75// find clusters with common edges or corners, separate if energy increases in neighboring cell
76//**************************************************************************************************************//
77//**************************************************************************************************************//
79 float seed, // minimum seed energy
80 float /* agg */, // minimum aggregation energy
81 std::vector<towersStrct>& input_towers_temp, // temporary full tower array
82 std::vector<towersStrct>& cluster_towers_temp, // towers associated to cluster
83 // std::vector<int> clslabels_temp // MC labels in cluster
84 float aggMargin = 1.0 // aggregation margin
85) {
86 clustersStrct tempstructC;
87 if (input_towers_temp.at(0).energy > seed) {
88 // std::cout << "new cluster" << std::endl;
89 // fill seed cell information into current cluster
90 tempstructC.cluster_E = input_towers_temp.at(0).energy;
91 tempstructC.cluster_seed = input_towers_temp.at(0).energy;
92 tempstructC.cluster_NTowers = 1;
93 tempstructC.cluster_NtrueID = 1;
94 tempstructC.cluster_trueID = input_towers_temp.at(0).tower_trueID; // TODO save all MC labels?
95 cluster_towers_temp.push_back(input_towers_temp.at(0));
96 // clslabels_temp.push_back(input_towers_temp.at(0).tower_trueID);
97 // std::cout << "seed: "<< input_towers_temp.at(0).cellIDx << "\t" << input_towers_temp.at(0).cellIDy
98 // << "\t" << input_towers_temp.at(0).cellIDz << "\t E:"<< tempstructC.cluster_E << std::endl;
99
100 // remove seed tower from sample
101 input_towers_temp.erase(input_towers_temp.begin());
102 for (int tit = 0; tit < (int)cluster_towers_temp.size(); tit++) {
103 // Now go recursively to all neighbours and add them to the cluster if they fulfill the conditions
104 int iEtaTwr = cluster_towers_temp.at(tit).cellIDx;
105 int iPhiTwr = cluster_towers_temp.at(tit).cellIDy;
106 int iLTwr = cluster_towers_temp.at(tit).cellIDz;
107 for (int ait = 0; ait < (int)input_towers_temp.size(); ait++) {
108 int iEtaTwrAgg = input_towers_temp.at(ait).cellIDx;
109 int iPhiTwrAgg = input_towers_temp.at(ait).cellIDy;
110 int iLTwrAgg = input_towers_temp.at(ait).cellIDz;
111
112 int deltaL = TMath::Abs(iLTwrAgg - iLTwr);
113 int deltaPhi = TMath::Abs(iPhiTwrAgg - iPhiTwr);
114 int deltaEta = TMath::Abs(iEtaTwrAgg - iEtaTwr);
115 bool neighbor = (deltaL + deltaPhi + deltaEta == 1);
116 bool corner2D = (deltaL == 0 && deltaPhi == 1 && deltaEta == 1) ||
117 (deltaL == 1 && deltaPhi == 0 && deltaEta == 1) ||
118 (deltaL == 1 && deltaPhi == 1 && deltaEta == 0);
119 // first condition asks for V3-like neighbors, while second condition also checks diagonally attached towers
120 if (neighbor || corner2D) {
121
122 // only aggregate towers with lower energy than current tower
123
124 if (input_towers_temp.at(ait).energy >= (cluster_towers_temp.at(tit).energy + aggMargin))
125 continue;
126 tempstructC.cluster_E += input_towers_temp.at(ait).energy;
127 tempstructC.cluster_NTowers++;
128 cluster_towers_temp.push_back(input_towers_temp.at(ait));
129 // if(!(std::find(clslabels_temp.begin(), clslabels_temp.end(), input_towers_temp.at(ait).tower_trueID) != clslabels_temp.end())){
130 // tempstructC.cluster_NtrueID++;
131 // clslabels_temp.push_back(input_towers_temp.at(ait).tower_trueID);
132 // }
133 // std::cout << "aggregated: "<< iEtaTwrAgg << "\t" << iPhiTwrAgg << "\t" << iLTwrAgg << "\t E:" << input_towers_temp.at(ait).energy << "\t reference: "<< refC << "\t"<< iEtaTwr << "\t" << iPhiTwr << "\t" << iLTwr << "\t cond.: \t"<< neighbor << "\t" << corner2D << "\t diffs: " << deltaEta << "\t" << deltaPhi << "\t" << deltaL<< std::endl;
134
135 input_towers_temp.erase(input_towers_temp.begin() + ait);
136 ait--;
137 }
138 }
139 }
140 }
141 return tempstructC;
142}
143
144// ANCHOR function to determine shower shape
145float* CalculateM02andWeightedPosition(std::vector<towersStrct> cluster_towers,
146 float cluster_E_calc, float weight0) {
147 static float returnVariables[8]; //0:M02, 1:M20, 2:eta, 3: phi
148 float w_tot = 0;
149 std::vector<float> w_i;
150 TVector3 vecTwr;
151 TVector3 vecTwrTmp;
152 float w_0 = weight0;
153
154 vecTwr = {0., 0., 0.};
155 //calculation of weights and weighted position vector
156 for (int cellI = 0; cellI < (int)cluster_towers.size(); cellI++) {
157 w_i.push_back(TMath::Max(
158 (float)0, (float)(w_0 + TMath::Log(cluster_towers.at(cellI).energy / cluster_E_calc))));
159 w_tot += w_i.at(cellI);
160 if (w_i.at(cellI) > 0) {
161 vecTwrTmp = TVector3(cluster_towers.at(cellI).posx, cluster_towers.at(cellI).posy,
162 cluster_towers.at(cellI).posz);
163 vecTwr += w_i.at(cellI) * vecTwrTmp;
164 }
165 }
166 // correct Eta position for average shift in calo
167 returnVariables[2] = vecTwr.Eta();
168 returnVariables[3] =
169 vecTwr.Phi(); //(vecTwr.Phi()<0 ? vecTwr.Phi()+TMath::Pi() : vecTwr.Phi()-TMath::Pi());
170 vecTwr *= 1. / w_tot;
171 // std::cout << "Cluster: X: "<< vecTwr.X() << "\t" << " Y: "<< vecTwr.Y() << "\t" << " Z: "<< vecTwr.Z() << std::endl;
172 returnVariables[4] = vecTwr.X();
173 returnVariables[5] = vecTwr.Y();
174 returnVariables[6] = vecTwr.Z();
175
176 //calculation of M02
177 float delta_phi_phi[4] = {0};
178 float delta_eta_eta[4] = {0};
179 float delta_eta_phi[4] = {0};
180 float dispersion = 0;
181
182 for (int cellI = 0; cellI < (int)cluster_towers.size(); cellI++) {
183 int iphi = cluster_towers.at(cellI).cellIDy;
184 int ieta = cluster_towers.at(cellI).cellIDx;
185 delta_phi_phi[1] += (w_i.at(cellI) * iphi * iphi) / w_tot;
186 delta_phi_phi[2] += (w_i.at(cellI) * iphi) / w_tot;
187 delta_phi_phi[3] += (w_i.at(cellI) * iphi) / w_tot;
188
189 delta_eta_eta[1] += (w_i.at(cellI) * ieta * ieta) / w_tot;
190 delta_eta_eta[2] += (w_i.at(cellI) * ieta) / w_tot;
191 delta_eta_eta[3] += (w_i.at(cellI) * ieta) / w_tot;
192
193 delta_eta_phi[1] += (w_i.at(cellI) * ieta * iphi) / w_tot;
194 delta_eta_phi[2] += (w_i.at(cellI) * iphi) / w_tot;
195 delta_eta_phi[3] += (w_i.at(cellI) * ieta) / w_tot;
196
197 vecTwrTmp = TVector3(cluster_towers.at(cellI).posx, cluster_towers.at(cellI).posy,
198 cluster_towers.at(cellI).posz);
199 // scale cluster position to z-plane
200 vecTwr *= std::abs(vecTwrTmp.Z() / vecTwr.Z());
201 float dx2 = pow(vecTwrTmp.X() - vecTwr.X(), 2);
202 float dy2 = pow(vecTwrTmp.Y() - vecTwr.Y(), 2);
203 float dz2 = pow(vecTwrTmp.Z() - vecTwr.Z(), 2);
204 dispersion += (w_i.at(cellI) * (dx2 + dy2 + dz2)) / w_tot;
205 }
206 returnVariables[7] = dispersion;
207 delta_phi_phi[0] = delta_phi_phi[1] - (delta_phi_phi[2] * delta_phi_phi[3]);
208 delta_eta_eta[0] = delta_eta_eta[1] - (delta_eta_eta[2] * delta_eta_eta[3]);
209 delta_eta_phi[0] = delta_eta_phi[1] - (delta_eta_phi[2] * delta_eta_phi[3]);
210
211 float calcM02 = 0.5 * (delta_phi_phi[0] + delta_eta_eta[0]) +
212 TMath::Sqrt(0.25 * TMath::Power((delta_phi_phi[0] - delta_eta_eta[0]), 2) +
213 TMath::Power(delta_eta_phi[0], 2));
214 float calcM20 = 0.5 * (delta_phi_phi[0] + delta_eta_eta[0]) -
215 TMath::Sqrt(0.25 * TMath::Power((delta_phi_phi[0] - delta_eta_eta[0]), 2) +
216 TMath::Power(delta_eta_phi[0], 2));
217 // std::cout << "M02_calc: " << calcM02 << "\t\t = 0.5 * ( " << delta_phi_phi[0] <<" + "<<delta_eta_eta[0]<<" ) + TMath::Sqrt( 0.25 * TMath::Power( ( "<<delta_phi_phi[0]<<" - "<<delta_eta_eta[0]<<" ), 2 ) + TMath::Power( "<<delta_eta_phi[0]<<", 2 ) ) "<< std::endl;
218 returnVariables[0] = calcM02;
219 returnVariables[1] = calcM20;
220 return returnVariables;
221}
bool acompare(towersStrct lhs, towersStrct rhs)
Definition clusterizer_MA.h:40
clustersStrct findMACluster(float seed, float, std::vector< towersStrct > &input_towers_temp, std::vector< towersStrct > &cluster_towers_temp, float aggMargin=1.0)
Definition clusterizer_MA.h:78
float * CalculateM02andWeightedPosition(std::vector< towersStrct > cluster_towers, float cluster_E_calc, float weight0)
Definition clusterizer_MA.h:145
bool acompareCl(clustersStrct lhs, clustersStrct rhs)
Definition clusterizer_MA.h:71
Definition clusterizer_MA.h:42
float cluster_Phi
Definition clusterizer_MA.h:59
float cluster_M02
Definition clusterizer_MA.h:63
int cluster_NTowers
Definition clusterizer_MA.h:65
float cluster_Y
Definition clusterizer_MA.h:61
clustersStrct()
Definition clusterizer_MA.h:43
std::vector< towersStrct > cluster_towers
Definition clusterizer_MA.h:68
float cluster_Eta
Definition clusterizer_MA.h:58
float cluster_seed
Definition clusterizer_MA.h:57
int cluster_NtrueID
Definition clusterizer_MA.h:67
int cluster_trueID
Definition clusterizer_MA.h:66
float cluster_M20
Definition clusterizer_MA.h:64
float cluster_X
Definition clusterizer_MA.h:60
float cluster_Z
Definition clusterizer_MA.h:62
float cluster_E
Definition clusterizer_MA.h:56
Definition clusterizer_MA.h:12
int cellIDy
Definition clusterizer_MA.h:33
float energy
Definition clusterizer_MA.h:26
int tower_clusterIDB
Definition clusterizer_MA.h:37
int cellIDz
Definition clusterizer_MA.h:34
int cellIDx
Definition clusterizer_MA.h:32
int cellID
Definition clusterizer_MA.h:31
float posz
Definition clusterizer_MA.h:30
towersStrct()
Definition clusterizer_MA.h:13
int tower_clusterIDA
Definition clusterizer_MA.h:36
float posy
Definition clusterizer_MA.h:29
int tower_trueID
Definition clusterizer_MA.h:35
float posx
Definition clusterizer_MA.h:28
float time
Definition clusterizer_MA.h:27