1 |
#if !defined(__CINT__) || defined(__MAKECINT__) |
2 |
|
3 |
|
4 |
#include <TString.h> |
5 |
#include <TH1F.h> |
6 |
#include <TH2F.h> |
7 |
#include <TMath.h> |
8 |
#include <TLine.h> |
9 |
#include <TPolyMarker.h> |
10 |
#include <TSelector.h> |
11 |
#include <TFile.h> |
12 |
|
13 |
#include <stdlib.h> |
14 |
#include <iostream> |
15 |
using namespace std; |
16 |
|
17 |
#include <PamLevel2.h> |
18 |
|
19 |
#endif |
20 |
//=============================================================================== |
21 |
// |
22 |
// |
23 |
// |
24 |
// |
25 |
//=============================================================================== |
26 |
class CaloAxis { |
27 |
|
28 |
public: |
29 |
vector<float> x;// independent coordinates (z) |
30 |
vector<float> y;// dependente coordinate (x/y) |
31 |
vector<float> w;// fit weight |
32 |
vector<float> q;// signal |
33 |
|
34 |
Float_t par[2]; |
35 |
Float_t covpar[2][2]; |
36 |
|
37 |
TPolyMarker *sel; |
38 |
TLine *axis; |
39 |
|
40 |
CaloAxis(){ |
41 |
for(Int_t i=0; i<2;i++){ |
42 |
par[i]=0; |
43 |
for(Int_t j=0; j<2;j++)covpar[i][j]=0; |
44 |
}; |
45 |
sel = 0; |
46 |
axis = 0; |
47 |
}; |
48 |
|
49 |
CaloAxis(Int_t nin, float* xin, float *yin){ |
50 |
for(Int_t i=0; i<nin; i++){ |
51 |
x.push_back(*xin++); |
52 |
y.push_back(*yin++); |
53 |
w.push_back(1.); |
54 |
q.push_back(0.); |
55 |
}; |
56 |
if(nin>2)Fit(); |
57 |
sel = 0; |
58 |
axis = 0; |
59 |
}; |
60 |
CaloAxis(Int_t nin, float* xin, float *yin, float* qin){ |
61 |
for(Int_t i=0; i<nin; i++){ |
62 |
x.push_back(*xin++); |
63 |
y.push_back(*yin++); |
64 |
q.push_back(*qin++); |
65 |
w.push_back(1.); |
66 |
}; |
67 |
if(nin>2)Fit(); |
68 |
sel = 0; |
69 |
axis = 0; |
70 |
}; |
71 |
CaloAxis(Int_t nin, float* xin, float *yin, float* qin, float* win){ |
72 |
for(Int_t i=0; i<nin; i++){ |
73 |
x.push_back(*xin++); |
74 |
y.push_back(*yin++); |
75 |
w.push_back(*win++); |
76 |
q.push_back(*qin++); |
77 |
}; |
78 |
if(nin>2)Fit(); |
79 |
sel = 0; |
80 |
axis = 0; |
81 |
}; |
82 |
|
83 |
void Add(float xin, float yin){ |
84 |
x.push_back(xin); |
85 |
y.push_back(yin); |
86 |
w.push_back(1.); |
87 |
q.push_back(0.); |
88 |
Int_t nin = x.size(); |
89 |
if(nin>2)Fit(); |
90 |
}; |
91 |
void Add(float xin, float yin, float qin){ |
92 |
x.push_back(xin); |
93 |
y.push_back(yin); |
94 |
q.push_back(qin); |
95 |
w.push_back(1.); |
96 |
Int_t nin = x.size(); |
97 |
|
98 |
if(nin>2)Fit(); |
99 |
}; |
100 |
void Add(float xin, float yin, float qin, float win){ |
101 |
x.push_back(xin); |
102 |
y.push_back(yin); |
103 |
w.push_back(win); |
104 |
q.push_back(qin); |
105 |
Int_t nin = x.size(); |
106 |
if(nin>2)Fit(); |
107 |
}; |
108 |
|
109 |
Int_t Fit(){ |
110 |
|
111 |
Float_t SSS = 0; |
112 |
Float_t SSX = 0; |
113 |
Float_t SXX = 0; |
114 |
|
115 |
Float_t SSY = 0; |
116 |
Float_t SXY = 0; |
117 |
|
118 |
Int_t np=0; |
119 |
for(Int_t i=0; i<(int)(x.size()); i++){ |
120 |
SSS += w[i]*w[i]; |
121 |
SSX += x[i]*w[i]*w[i]; |
122 |
SXX += x[i]*x[i]*w[i]*w[i]; |
123 |
SSY += y[i]*w[i]*w[i]; |
124 |
SXY += x[i]*y[i]*w[i]*w[i]; |
125 |
if(w[i])np++; |
126 |
} |
127 |
Float_t det = SSS*SXX-SSX*SSX; |
128 |
if(det==0)return 0; |
129 |
if(np<3)return 0; |
130 |
// cout << np << "points fitted -- ok "<<endl; |
131 |
par[0] = (SSY*SXX-SXY*SSX)/det; |
132 |
par[1] = (SXY*SSS-SSY*SSX)/det; |
133 |
return 1; |
134 |
}; |
135 |
|
136 |
void Reset(){ |
137 |
for(Int_t i=0; i<2;i++){ |
138 |
par[i]=0; |
139 |
for(Int_t j=0; j<2;j++)covpar[i][j]=0; |
140 |
}; |
141 |
x.clear(); |
142 |
y.clear(); |
143 |
w.clear(); |
144 |
q.clear(); |
145 |
if(sel)sel->Delete(); |
146 |
if(axis)axis->Delete(); |
147 |
|
148 |
} |
149 |
|
150 |
void Delete(){ |
151 |
Reset(); |
152 |
if(sel)delete sel; |
153 |
if(axis)delete axis; |
154 |
|
155 |
} |
156 |
|
157 |
void Print(){ |
158 |
for(Int_t i=0; i<(int)(x.size()); i++)if(w[i])cout << x[i] << " - "<<y[i]<<endl; |
159 |
} |
160 |
|
161 |
float GetPar(Int_t i){return par[i];}; |
162 |
|
163 |
int GetN(){return (int)(x.size());}; |
164 |
|
165 |
float* GetX(){ |
166 |
float *xout = new float[(int)(x.size())]; |
167 |
for(Int_t i=0; i<(int)(x.size()); i++)xout[i]=x[i]; |
168 |
return xout; |
169 |
} |
170 |
|
171 |
float* GetY(){ |
172 |
float *yout = new float[(int)(y.size())]; |
173 |
for(Int_t i=0; i<(int)(y.size()); i++) yout[i]=y[i]; |
174 |
return yout; |
175 |
} |
176 |
|
177 |
float* GetQ(){ |
178 |
float *qout = new float[(int)(q.size())]; |
179 |
for(int i=0; i<(int)(q.size()); i++) qout[i]=q[i]; |
180 |
return qout; |
181 |
} |
182 |
|
183 |
float GetChi2(){ |
184 |
float chi2=0; |
185 |
int nchi=0; |
186 |
for(Int_t i=0; i<(int)(y.size()); i++){ |
187 |
chi2 += w[i]*w[i]*(y[i]-par[0]-par[1]*x[i])*(y[i]-par[0]-par[1]*x[i]); |
188 |
nchi += (int)w[i]; |
189 |
}; |
190 |
if(nchi<3)return -999; |
191 |
chi2=chi2/(nchi-2); |
192 |
return chi2; |
193 |
} |
194 |
|
195 |
float GetQaxis(){ |
196 |
float qaxis=0; |
197 |
for(Int_t i=0; i<(int)(q.size()); i++) qaxis +=q[i]; |
198 |
// for(Int_t i=0; i<(int)(q.size()); i++) cout<<q[i]<<endl; |
199 |
return qaxis; |
200 |
} |
201 |
|
202 |
float GetQstrip(){ |
203 |
|
204 |
float qaxis=0; |
205 |
for(Int_t i=0; i<(int)(q.size()); i++) qaxis +=q[i]; |
206 |
if(q.size()==0) return 0.; |
207 |
return qaxis/q.size(); |
208 |
} |
209 |
|
210 |
float GetXmin(){ |
211 |
float zmin=9999; |
212 |
for(Int_t i=0; i<(int)(x.size()); i++) if(x[i]<zmin) zmin = x[i]; |
213 |
return zmin; |
214 |
} |
215 |
float GetXmax(){ |
216 |
float zmax=-9999; |
217 |
for(Int_t i=0; i<(int)(x.size()); i++) if(x[i]>zmax) zmax = x[i]; |
218 |
return zmax; |
219 |
|
220 |
} |
221 |
|
222 |
|
223 |
void Draw(){ |
224 |
|
225 |
if(GetN()<3)return; |
226 |
|
227 |
sel = new TPolyMarker(GetN(),GetY(),GetX()); |
228 |
sel->SetMarkerSize(0.3); |
229 |
sel->SetMarkerColor(5); |
230 |
|
231 |
axis = new TLine( par[0]+GetXmin()*par[1], GetXmin(), par[0]+GetXmax()*par[1], GetXmax() ); |
232 |
axis->SetLineWidth(1); |
233 |
axis->SetLineColor(5); |
234 |
|
235 |
sel->Draw(); |
236 |
axis->Draw(); |
237 |
} |
238 |
|
239 |
Int_t FitAxis( CaloLevel1* calo , Int_t view , Float_t toll ){ |
240 |
|
241 |
CaloStrip* cstrip = new CaloStrip(calo); |
242 |
|
243 |
// --------------------------- |
244 |
// first : all hits included |
245 |
// --------------------------- |
246 |
// CaloAxis *axis = new CaloAxis(); |
247 |
for(int ih=0;ih<calo->istrip;ih++){ |
248 |
int iv=-1; |
249 |
int ip=-1; |
250 |
int is=-1; |
251 |
calo->DecodeEstrip(ih,iv,ip,is); |
252 |
if(iv==view){ |
253 |
// cout<<ip<<" "<<is<<" "<< calo->DecodeEstrip(ih,iv,ip,is)<<" "<<endl; |
254 |
// float x = GetCoordXY(view,ip,is) ; |
255 |
// float z = GetCoordZ(view,ip) ; |
256 |
// float q = calo->GetEstrip(view,ip,is); |
257 |
cstrip->Set(view,ip,is); |
258 |
float x = 0; |
259 |
if( !view ) x = cstrip->GetX(); |
260 |
else x = cstrip->GetY(); |
261 |
float z = cstrip->GetZ(); |
262 |
float q = cstrip->GetE(); |
263 |
// if(view==0 && ip==21)q=0; |
264 |
if(q>0)this->Add(z,x,q); |
265 |
} |
266 |
|
267 |
} |
268 |
// cout << "(1) hits "<<this->GetN()<<endl; |
269 |
if(this->GetN()<3){ |
270 |
delete cstrip; |
271 |
return 0; |
272 |
} |
273 |
float P0; |
274 |
float P1; |
275 |
int dof; |
276 |
bool missing = false; |
277 |
int nmissing = 0; |
278 |
|
279 |
// --------------------------- |
280 |
// second : iteration |
281 |
// --------------------------- |
282 |
int niter=0; // number of iterations |
283 |
do{ |
284 |
niter++; |
285 |
float ttoll = toll; //tolerance (cm) |
286 |
if(niter==1)ttoll = 10.*toll; |
287 |
dof = this->GetN(); //previous fit |
288 |
P0 = this->par[0]; |
289 |
P1 = this->par[1]; |
290 |
this->Reset(); //fit reset |
291 |
// cout << "previous fit "<<dof<<" "<<P0<<" "<<P1<<endl; |
292 |
// cout << "(reset) hits "<<this->GetN()<<endl; |
293 |
missing = false; |
294 |
nmissing = 0; |
295 |
for(Int_t ip=0; ip<22; ip++){ |
296 |
// break the track if more than 2 planes are missing |
297 |
if(missing)nmissing++; |
298 |
if(nmissing==3)break; |
299 |
missing=true; |
300 |
// float z = GetCoordZ(view,ip) ; |
301 |
for(Int_t is=0; is<96; is++){ |
302 |
|
303 |
cstrip->Set(view,ip,is); |
304 |
float x = 0; |
305 |
if( !view ) x = cstrip->GetX(); |
306 |
else x = cstrip->GetY(); |
307 |
float z = cstrip->GetZ(); |
308 |
float q = cstrip->GetE(); |
309 |
|
310 |
if( q ){ |
311 |
// float x = GetCoordXY(view,ip,is) ; |
312 |
// float q = calo->GetEstrip(view,ip,is); |
313 |
// if(view==0 && ip==21)q=0; |
314 |
float d = fabs(x-P0-P1*z)/sqrt(P1*P1+1); |
315 |
// cout << d << " "<<ttoll<<endl; |
316 |
// if( q>0 && d < ttoll ){ |
317 |
if( d < ttoll ){ |
318 |
this->Add(z,x,q); |
319 |
// cout << "(-) hits "<<this->GetN()<<endl; |
320 |
missing=false; |
321 |
nmissing=0; |
322 |
} |
323 |
} |
324 |
} |
325 |
} |
326 |
// cout << "(2) hits "<<this->GetN()<<endl; |
327 |
if(niter==100)break; |
328 |
if(this->GetN()==0)break; |
329 |
}while(niter==1 || this->GetN() != dof); |
330 |
if(this->GetN()<3){ |
331 |
delete cstrip; |
332 |
return 0; |
333 |
} |
334 |
// --------------------------------------------- |
335 |
// third : selecting only closest strip-clusters |
336 |
// --------------------------------------------- |
337 |
P0 = this->par[0]; |
338 |
P1 = this->par[1]; |
339 |
this->Reset(); |
340 |
missing = false; |
341 |
nmissing = 0; |
342 |
for(Int_t ip=0; ip<22; ip++){ |
343 |
if(missing)nmissing++; |
344 |
if(nmissing==3)break; |
345 |
missing=true; |
346 |
int isclose = -1; |
347 |
float dmin = 999; |
348 |
// float z = GetCoordZ(view,ip) ; |
349 |
//search for closest hit strip |
350 |
for(Int_t is=0; is<96; is++){ |
351 |
cstrip->Set(view,ip,is); |
352 |
float x = 0; |
353 |
if( !view ) x = cstrip->GetX(); |
354 |
else x = cstrip->GetY(); |
355 |
float z = cstrip->GetZ(); |
356 |
float q = cstrip->GetE(); |
357 |
if( q ){ |
358 |
// float x = GetCoordXY(view,ip,is) ; |
359 |
float d = fabs(x-P0-P1*z)/sqrt(P1*P1+1); |
360 |
// cout << x <<" "<<z<<" "<<P0+P1*z<<" "<<d << endl; |
361 |
if(d<dmin && d<toll){ |
362 |
isclose =is; |
363 |
dmin =d; |
364 |
} |
365 |
} |
366 |
} |
367 |
if( isclose != -1 ){ |
368 |
Int_t isfrom = isclose-1; |
369 |
if(isfrom<0) isfrom=0; |
370 |
Int_t isto = isclose+2; |
371 |
if(isto>96) isto=96; |
372 |
for(Int_t is=isfrom; is< isto; is++){ |
373 |
cstrip->Set(view,ip,is); |
374 |
float x = 0; |
375 |
if( !view ) x = cstrip->GetX(); |
376 |
else x = cstrip->GetY(); |
377 |
float z = cstrip->GetZ(); |
378 |
float q = cstrip->GetE(); |
379 |
// float x = GetCoordXY(view,ip,is) ; |
380 |
// float q = calo->GetEstrip(view,ip,is); |
381 |
// if(view==0 && ip==21)q = 0; //mask last plane |
382 |
if(q){ |
383 |
this->Add(z,x,q); |
384 |
missing = false; |
385 |
nmissing = 0; |
386 |
} |
387 |
} |
388 |
} |
389 |
} |
390 |
|
391 |
// cout << " n. hits :"<<this->GetN()<<endl; |
392 |
// cout << " P0 P1 :"<<this->par[0]<< " "<<this->par[1]<<endl; |
393 |
// cout << " Chi2 :"<<this->GetChi2()<<endl; |
394 |
// cout << " Q axis :"<<this->GetQaxis()<<endl; |
395 |
// cout << " Q strip :"<<this->GetQstrip()<<endl; |
396 |
delete cstrip; |
397 |
return 1; |
398 |
} |
399 |
}; |
400 |
//=============================================================================== |
401 |
// |
402 |
// |
403 |
// |
404 |
// |
405 |
//=============================================================================== |