SND@LHC Software
Loading...
Searching...
No Matches
splitcalCluster.cxx
Go to the documentation of this file.
1#include "splitcalCluster.h"
2#include "TMath.h"
3
4#include <iostream>
5#include <math.h>
6#include <functional>
7#include <numeric>
8#include <map>
9
10
11// ----- constructor from list/vector of splitcalHit ------------------------------------------
12// splitcalCluster::splitcalCluster(boost::python::list& l)
13// {
14// std::vector<splitcalHit > v;
15// for(int i=0; i<boost::python::len(l); i++) {
16// v.push_back(boost::python::extract<splitcalHit >(l[i]));
17// }
18// SetVectorOfHits(v);
19// }
20
21// ----- Default constructor -------------------------------------------
25// ----- constructor from splitcalHit ------------------------------------------
27{
28 _vectorOfHits.push_back(h);
29
30 if ( _vectorOfHits.size() == 1){ //first element added to the cluster
32 SetEndPoint(h);
33 } else {
34 if (_start.Z() > h->GetZ()) SetStartPoint(h); // start point is the hit with the smalllest z
35 if (_end.Z() < h->GetZ()) SetEndPoint(h); // end point is the hit with the largest z
36 }
37
38}
39
40
42{
43
44
45 // Compute energy weighted average for hits in the same layer
46 // This is in preparation of the linear fit to get the cluster eta and phi
47
48 // maps to copute the weighted average of all the hits in the same layer
49 std::map<int, double> mapLayerWeigthedX;
50 std::map<int, double> mapLayerWeigthedY;
51 std::map<int, double> mapLayerZ1;
52 std::map<int, double> mapLayerZ2;
53 std::map<int, double> mapLayerSumWeigthsX;
54 std::map<int, double> mapLayerSumWeigthsY;
55
56 // loop over hits to compute cluster energy sum and to compute the coordinates weighted average per layer
57 double energy = 0.;
58 for (auto hit : _vectorOfHits){
59 energy += hit->GetEnergyForCluster(_index);
60 int layer = hit->GetLayerNumber();
61 // hits from high precision layers give both x and y coordinates --> use if-if instead of if-else
62 if (hit->IsX()){
63 if (mapLayerWeigthedX.count(layer)==0) { //if key is not yet in map, initialise element to 0
64 mapLayerWeigthedX[layer] = 0.;
65 mapLayerSumWeigthsX[layer] = 0.;
66 }
67 mapLayerWeigthedX[layer] += hit->GetX()*hit->GetEnergyForCluster(_index);
68 mapLayerSumWeigthsX[layer] += hit->GetEnergyForCluster(_index);
69 mapLayerZ1[layer] = hit->GetZ();
70 }
71 if (hit->IsY()){
72 if (mapLayerWeigthedY.count(layer)==0) { //if key is not yet in map, initialise element to 0
73 mapLayerWeigthedY[layer] = 0.;
74 mapLayerSumWeigthsY[layer] = 0.;
75 }
76 mapLayerWeigthedY[layer] += hit->GetY()*hit->GetEnergyForCluster(_index);
77 mapLayerSumWeigthsY[layer] += hit->GetEnergyForCluster(_index);
78 mapLayerZ2[layer] = hit->GetZ();
79 }
80 }//end loop on hit
81
82
83 // FIXME: regression fit seems instable --> for the moment commented it out in favour of simple direction from initial and end point
84
85 auto const& firstElementX = mapLayerWeigthedX.begin();
86 int minLayerX = firstElementX->first;
87 double minX = firstElementX->second/mapLayerSumWeigthsX[minLayerX];
88 double minZ1 = mapLayerZ1[minLayerX];
89
90 auto const& firstElementY = mapLayerWeigthedY.begin();
91 int minLayerY = firstElementY->first;
92 double minY = firstElementY->second/mapLayerSumWeigthsY[minLayerY];
93 double minZ2 = mapLayerZ1[minLayerY];
94
95 double minZ = (minZ1+minZ2)/2.;
96
97 SetStartPoint(minX, minY, minZ);
98
99 auto const& lastElementX = mapLayerWeigthedX.rbegin();
100 int maxLayerX = lastElementX->first;
101 double maxX = lastElementX->second/mapLayerSumWeigthsX[maxLayerX];
102 double maxZ1 = mapLayerZ1[maxLayerX];
103
104 auto const& lastElementY = mapLayerWeigthedY.rbegin();
105 int maxLayerY = lastElementY->first;
106 double maxY = lastElementY->second/mapLayerSumWeigthsY[maxLayerY];
107 double maxZ2 = mapLayerZ1[maxLayerY];
108
109 double maxZ = (maxZ1+maxZ2)/2.;
110
111 SetEndPoint(maxX, maxY, maxZ);
112
113 // get direction vector from end-strat vector difference
114 TVector3 direction;
115 direction = _end - _start;
116 double eta = direction.Eta();
117 double phi = direction.Phi();
118 SetEtaPhiE(eta, phi, energy);
119
120
121 // // vectors holding the weighted coordinates per layer: x and z for the eta fit, and y and z for the phi fit
122 // std::vector<double > x;
123 // std::vector<double > z1;
124 // std::vector<double > y;
125 // std::vector<double > z2;
126
127 // for (auto const& element : mapLayerWeigthedX){
128 // int key = element.first;
129 // x.push_back(element.second/mapLayerSumWeigthsX[key]);
130 // z1.push_back(mapLayerZ1[key]);
131 // }
132
133 // regression resultZX = LinearRegression(z1,x);
134 // double alpha = acos(resultZX.slope);
135
136 // for (auto const& element : mapLayerWeigthedY){
137 // int key = element.first;
138 // y.push_back(element.second/mapLayerSumWeigthsY[key]);
139 // z2.push_back(mapLayerZ2[key]);
140 // }
141
142 // regression resultZY = LinearRegression(z2,y);
143 // double eta_check = acos(resultZY.slope);
144
145 // // regression resultXY = LinearRegression(x,y);
146 // // double phi = acos(resultXY.slope);
147
148 // std::cout<<" ---- before fit " << std::endl;
149 // _start.Print();
150 // _end.Print();
151
152 // // replace the x and y of start and end points with the re-evaluated values from the corresponding fit
153 // _start.SetX( resultZX.slope * _start.Z() + resultZX.intercept );
154 // _start.SetY( resultZY.slope * _start.Z() + resultZY.intercept );
155
156 // _end.SetX( resultZX.slope * _end.Z() + resultZX.intercept );
157 // _end.SetY( resultZY.slope * _end.Z() + resultZY.intercept );
158
159 // // get direction vector from end-strat vector difference
160 // TVector3 direction;
161 // direction = _end - _start;
162 // double eta = direction.Eta();
163 // double phi = direction.Phi();
164
165 // std::cout<<" ---- after fit " << std::endl;
166 // _start.Print();
167 // _end.Print();
168
169 // std::cout<<" -- eta = "<< eta << std::endl;
170 // std::cout<<" -- eta_check = "<< eta_check << std::endl;
171 // std::cout<<" -- phi = "<< phi << std::endl;
172 // std::cout<<" -- energy = "<< energy << std::endl;
173
174 // SetEtaPhiE(eta, phi, energy);
175
176 // // temporary for test
177 // _mZX = resultZX.slope;
178 // _qZX = resultZX.intercept;
179 // _mZY = resultZY.slope;
180 // _qZY = resultZY.intercept;
181
182 return;
183}
184
185
186
187regression splitcalCluster::LinearRegression(std::vector<double >& x, std::vector<double >& y) {
188
189 const auto n = x.size();
190 const auto s_x = std::accumulate(x.begin(), x.end(), 0.);
191 const auto s_y = std::accumulate(y.begin(), y.end(), 0.);
192 const auto s_xx = std::inner_product(x.begin(), x.end(), x.begin(), 0.);
193 const auto s_xy = std::inner_product(x.begin(), x.end(), y.begin(), 0.);
194
195 regression result;
196
197 result.slope = (n * s_xy - s_x * s_y) / (n * s_xx - s_x * s_x);
198 result.intercept = (s_x * s_x * s_y - s_xy * s_x) / (n * s_xx - s_x * s_x);
199
200 std::cout<< "--- LinearRegression ---" <<std::endl;
201 std::cout<< "--------- slope = " << result.slope <<std::endl;
202 std::cout<< "--------- intercept = " << result.intercept <<std::endl;
203
204 return result;
205
206}
207
208
210 SetStartPoint(h->GetX(),h->GetY(),h->GetZ());
211}
212
214 SetEndPoint(h->GetX(),h->GetY(),h->GetZ());
215}
216
217
218// -------------------------------------------------------------------------
219
220// ----- Destructor ----------------------------------------------------
222// -------------------------------------------------------------------------
223
224// ----- Public method Print -------------------------------------------
226{
227
228 std::cout<< "-I- splitcalCluster: " <<std::endl;
229 std::cout<< " (eta,phi,energy) = "
230 << _eta << " , "
231 << _phi << " , "
232 << _energy << std::endl;
233 std::cout<< " start(x,y,z) = "
234 << _start.X() << " , "
235 << _start.Y() << " , "
236 << _start.Z() << std::endl;
237 std::cout<< " end(x,y,z) = "
238 << _end.X() << " , "
239 << _end.Y() << " , "
240 << _end.Z() << std::endl;
241 std::cout<< "------- " <<std::endl;
242}
243// -------------------------------------------------------------------------
244
246
void SetStartPoint(const double &x, const double &y, const double &z)
void SetEndPoint(const double &x, const double &y, const double &z)
virtual void Print() const
void SetEtaPhiE(double &eta, double &phi, double &e)
std::vector< splitcalHit * > _vectorOfHits
regression LinearRegression(std::vector< double > &x, std::vector< double > &y)
ClassImp(ecalContFact) ecalContFact