/[PAMELA software]/calo/flight/CaloFranzini/src/Calib.cpp
ViewVC logotype

Contents of /calo/flight/CaloFranzini/src/Calib.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Thu Dec 13 17:08:09 2007 UTC (16 years, 11 months ago) by mocchiut
Branch: MAIN
Some changes implemented

1 #include <stdlib.h>
2 #include <iostream>
3 #include <iomanip>
4 //
5 #include <TString.h>
6 #include <TH1F.h>
7 #include <TH2F.h>
8 #include <TMatrixD.h>
9 #include <TArrayF.h>
10 #include <TArrayI.h>
11 //
12 #include <PamLevel2.h>
13 #include <CaloFranzini.h>
14 //
15 using namespace std;
16 //
17 CaloFranzini *cf;
18 Int_t nbin;
19 Float_t rig[18];
20 Float_t rmean[18];
21 Float_t qqplane[18][43];
22 Int_t nnqplane[18][43];
23
24 TArrayF *qplane[18];
25 TArrayI *nqplane[18];
26 TMatrixD *matrix[18];
27 TMatrixD *nmat[18];
28
29 //===============================================================================
30 bool Select( PamLevel2* event ){
31
32 //---------------------------------------------------------
33 // single track
34 //---------------------------------------------------------
35 if( event->GetTrkLevel2()->GetNTracks()!=1 ) return false;
36 PamTrack *track = event->GetTrack(0);
37 if(!track)return false;
38
39 //------------------------------------------------------------------
40 // tracker pre-selection
41 //------------------------------------------------------------------
42 TrkTrack *trk = track->GetTrkTrack();
43
44 bool TRACK__OK = false;
45 if(
46 trk->chi2 >0 &&
47 trk->GetNX()>=4 &&
48 trk->GetNY()>=3 &&
49 trk->GetLeverArmX()>=5 &&
50 true ) TRACK__OK = true;
51
52 if( !TRACK__OK )return false;
53
54 //------------------------------------------------------------------
55 // TOF pre-selection
56 //------------------------------------------------------------------
57 bool TOF__OK = false;
58 if(
59 event->GetToFLevel2()->GetNHitPaddles(0) == 1 &&
60 event->GetToFLevel2()->GetNHitPaddles(1) == 1 &&
61 event->GetToFLevel2()->GetNHitPaddles(2) == 1 &&
62 event->GetToFLevel2()->GetNHitPaddles(3) == 1 &&
63 event->GetToFLevel2()->GetNHitPaddles(4) >= 1 &&
64 event->GetToFLevel2()->GetNHitPaddles(5) >= 1 &&
65 event->GetToFLevel2()->npmt() <= 18 &&
66 !event->GetAcLevel2()->CARDhit() &&
67 !event->GetAcLevel2()->CAThit() &&
68 true ) TOF__OK = true;
69 if( !TOF__OK )return false;
70 //------------------------------------------------------
71 // no albedo
72 //------------------------------------------------------
73 if( track->GetToFTrack()->beta[12]<=0.2 ||
74 track->GetToFTrack()->beta[12] >= 1.5 ) return false;
75
76 //------------------------------------------------------
77 bool CUT1 = false;
78 if(
79 trk->nstep<100 &&
80 trk->GetRigidity()<400. &&
81 trk->GetRigidity()>0.1 &&
82 trk->GetDeflection()<0. &&
83 trk->resx[0]<0.001 &&
84 trk->resx[5]<0.001 &&
85 track->IsSolved() &&
86 trk->IsInsideCavity() &&
87 true ) CUT1 = true;
88 //------------------------------------------------------
89 if( !CUT1 )return false;
90 Float_t defl = trk->GetDeflection();
91 float p0 = 1.111588e+00;
92 float p1 = 1.707656e+00;
93 float p2 = 1.489693e-01;
94 float chi2m025 = p0 + fabs(defl)*p1 + defl*defl*p2;
95
96 float def_0 = 0.07;
97 float chi2m025_0 = p0 + fabs(def_0)*p1 + def_0*def_0*p2;
98
99 // int nchi2cut=5;
100 float chi2cut=3.;
101 float chi2m = pow( chi2m025-chi2m025_0+pow(chi2cut,0.25), 4.);
102 bool CUT2 = false;
103 if(
104 track->GetTrkTrack()->chi2 < chi2m &&
105 true ) CUT2 = true;
106 if ( !CUT2 ) return false;
107 //------------------------------------------------------
108 //
109 // energy momentum match
110 //
111 Float_t qtotimp = event->GetCaloLevel2()->qtot / trk->GetRigidity();
112 Float_t qcut2 = (-0.5 * trk->GetRigidity() + 150.) * 1.1;
113 if ( qcut2 < 55. ) qcut2 = 55.;
114 if ( qtotimp <= qcut2 ) return false;
115 //
116 for (Int_t i=0; i < 22; i++){
117 if ( track->GetCaloTrack()->tibar[i][1] < 0 || track->GetCaloTrack()->tibar[i][0] < 0 ){
118 return false;
119 };
120 };
121 //
122 if ( event->GetCaloLevel1()->qtotpl(0) > 7. ) return false;
123 //
124 return true;
125 }
126 //===============================================================
127 // Create histograms
128 //
129 //
130 //
131 //
132 //
133 //===============================================================
134 void CreateHistos( PamLevel2* event , TString file){
135
136 cf = new CaloFranzini(event);
137 cf->CreateMatrixFile(file.Data());
138 //
139 nbin = 18;
140 rig[0] = 0.1;
141 rig[1] = 0.5;
142 rig[2] = 1.;
143 rig[3] = 1.5;
144 rig[4] = 2.2;
145 rig[5] = 3.;
146 rig[6] = 4.;
147 rig[7] = 5.;
148 rig[8] = 6.;
149 rig[9] = 8.;
150 rig[10] = 10.;
151 rig[11] = 15.;
152 rig[12] = 25.;
153 rig[13] = 35.;
154 rig[14] = 50.;
155 rig[15] = 100.;
156 rig[16] = 200.;
157 rig[17] = 400.;
158 //
159 // memset(qplane, 0, 44*18*sizeof(Float_t));
160 memset(rmean, 0, 18*sizeof(Float_t));
161 memset(qqplane, 0, 43*18*sizeof(Float_t));
162 memset(nnqplane, 0, 43*18*sizeof(Float_t));
163 //
164 for (Int_t i=0; i < 18 ; i++){
165 matrix[i] = new TMatrixD(43,43);
166 qplane[i] = new TArrayF(43);
167 nqplane[i] = new TArrayI(43);
168 nmat[i] = new TMatrixD(43,43);
169 };
170 // for (Int_t i=0; i < 18 ; i++){
171 // for (Int_t j = 0; j < 43;i++){
172 // (*qplane[i])[j] = 0.;
173 // qplane[i]->Reset();
174 // nqplane[i]->Reset();
175 // for (Int_t k = 0; k < 43;k++){
176 // (*matrix[i])[j][k] = 0.;
177 // (*nmat[i])[j][k] = 0.;
178 // };
179 // };
180 // };
181 //
182 }
183
184 //===============================================================
185 void FindAverage( PamLevel2* L2, int iev ){
186 //
187 // printf("SELEZIONATO \n");
188 Float_t erig = L2->GetTrack(0)->GetTrkTrack()->GetRigidity();
189 //
190 Int_t rbi = 0;
191 for (Int_t i = 0; i<nbin-1; i++){ // < 17
192 if ( erig>=rig[i] && erig < rig[i+1] ){
193 rbi = i;
194 break;
195 };
196 };
197 //
198 // printf("SELEZIONATO rbi %i erig %f rig[0] %f rig[nbin] %f \n",rbi, erig, rig[0],rig[nbin-1]);
199 //
200 if ( erig < rig[0] ) return;
201 if ( erig >= rig[nbin-1] ) return;
202 //
203 rmean[rbi] += erig;
204 //
205 Int_t dgf = 43;
206 //
207 // for (Int_t i=0; i < 22; i++){
208 // if ( L2->GetTrack(0)->GetCaloTrack()->tibar[i][1] < 0 ){
209 // dgf = 2 * i;
210 // break;
211 // };
212 // if ( L2->GetTrack(0)->GetCaloTrack()->tibar[i][0] < 0 ){
213 // dgf = 1 + 2 * i;
214 // break;
215 // };
216 // };
217 //
218 // if ( dgf < 43 && dgf > 37 ) dgf--;
219 //
220 for (Int_t i=0; i<dgf; i++){
221 (*nqplane[rbi])[i]++;
222 nnqplane[rbi][i]++;
223 };
224 //
225 // printf(" SELEZIONATO rig %f dgf %i \n",erig,dgf);
226 //
227 //
228 // Fill the estrip matrix
229 //
230 Int_t nplane = 0;
231 Int_t view = 0;
232 Int_t plane = 0;
233 Int_t strip = 0;
234 Float_t mip = 0.;
235 //
236 for ( Int_t i=0; i<L2->GetCaloLevel1()->istrip; i++ ){
237 //
238 mip = L2->GetCaloLevel1()->DecodeEstrip(i,view,plane,strip);
239 //
240 nplane = 1 - view + 2 * plane;
241 if ( nplane > 37 ) nplane--;
242 // printf(" mip %f view %i plane %i => nplane %i \n",mip,view,plane,nplane);
243 if ( nplane < dgf ){
244 // printf(" PRIMA: qpl %f nqpl %i \n",(*qplane[rbi])[nplane],(*nqplane[rbi])[nplane]);
245 (*qplane[rbi])[nplane] += mip;
246 qqplane[rbi][nplane] += mip;
247 // printf(" DOPO: qpl %f nqpl %i \n",(*qplane[rbi])[nplane],(*nqplane[rbi])[nplane]);
248 };
249 //
250 };
251 }
252
253 //===============================================================
254 void FindMatrix( PamLevel2* L2, int iev ){
255 //
256 // printf("SELEZIONATO \n");
257 Float_t erig = L2->GetTrack(0)->GetTrkTrack()->GetRigidity();
258 //
259 Int_t rbi = 0;
260 for (Int_t i = 0; i<nbin-1; i++){
261 if ( erig>=rig[i] && erig < rig[i+1] ){
262 rbi = i;
263 break;
264 };
265 };
266 //
267 // printf("SELEZIONATO rbi %i erig %f rig[0] %f rig[nbin] %f \n",rbi, erig, rig[0],rig[nbin-1]);
268 //
269 if ( erig < rig[0] ) return;
270 if ( erig >= rig[nbin-1] ) return;
271 //
272 Int_t dgf = 43;
273 //
274 // for (Int_t i=0; i < 22; i++){
275 // if ( L2->GetTrack(0)->GetCaloTrack()->tibar[i][1] < 0 ){
276 // dgf = 2 * i;
277 // break;
278 // };
279 // if ( L2->GetTrack(0)->GetCaloTrack()->tibar[i][0] < 0 ){
280 // dgf = 1 + 2 * i;
281 // break;
282 // };
283 // };
284 //
285 // if ( dgf < 43 && dgf > 37 ) dgf--;
286 //
287 for (Int_t i=0; i<dgf; i++){
288 for (Int_t j=0; j<dgf; j++){
289 (*nmat[rbi])[i][j] += 1.;
290 };
291 };
292 //
293 // printf(" SELEZIONATO rig %f dgf %i \n",erig,dgf);
294 //
295 //
296 // Fill the estrip matrix
297 //
298 Int_t nplane = 0;
299 Int_t view = 0;
300 Int_t plane = 0;
301 Int_t strip = 0;
302 Float_t mip = 0.;
303 Float_t hpl[43];
304 memset(hpl,0,43*sizeof(Float_t));
305 for ( Int_t i=0; i<L2->GetCaloLevel1()->istrip; i++ ){
306 //
307 mip = L2->GetCaloLevel1()->DecodeEstrip(i,view,plane,strip);
308 //
309 nplane = 1 - view + 2 * plane;
310 if ( nplane > 37 ) nplane--;
311 if ( nplane < dgf ){
312 hpl[nplane] += mip;
313 };
314 //
315 };
316 //
317 for (Int_t i=0; i<dgf; i++){
318 for (Int_t j=0; j<dgf; j++){
319 // if ( rbi == 1 ) printf(" PRIMA: matrix %f qpli %f qplj %f hpli %f hplj %f \n",(*matrix[rbi])[i][j],(*qplane[rbi])[i],(*qplane[rbi])[j],hpl[i],hpl[j]);
320 (*matrix[rbi])[i][j] += (hpl[i] - (*qplane[rbi])[i]) * (hpl[j] - (*qplane[rbi])[j]);
321 // if ( rbi == 1 ) printf(" DOPO: matrix %f qpli %f qplj %f hpli %f hplj %f \n",(*matrix[rbi])[i][j],(*qplane[rbi])[i],(*qplane[rbi])[j],hpl[i],hpl[j]);
322 };
323 };
324 }
325
326 void CalculateAverage(){
327 for (Int_t i=0; i<nbin; i++){
328 if ( (*nqplane[i])[0] > 0 ) rmean[i] /= (Float_t)(*nqplane[i])[0];
329 for (Int_t j=0; j<43 ; j++){
330 if ( (*nqplane[i])[j] > 0 ){
331 (*qplane[i])[j] /= (Float_t)(*nqplane[i])[j];
332 } else {
333 (*qplane[i])[j] = 0.;
334 };
335 printf(" BIN %i plane %i average energy %f qplane %f nqplane %i \n",i,j,rmean[i],(*qplane[i])[j],(*nqplane[i])[j]);
336 // if ( nnqplane[i][j] > 0 ){
337 // qqplane[i][j] /= (Float_t)nnqplane[i][j];
338 // } else {
339 // qqplane[i][j] = 0.;
340 // };
341 // printf(" BBIN %i plane %i average energy %f qplane %f nqplane %i \n",i,j,rmean[i],qqplane[i][j],nnqplane[i][j]);
342 };
343 };
344
345 cf->WriteNumBin(nbin);
346
347 TArrayF *rigbin = new TArrayF(18, rig);
348 cf->WriteRigBin(rigbin);
349
350 TArrayF *rmeanbin = new TArrayF(18, rmean);
351 TFile *file = cf->GetFile();
352 file->cd();
353 // rigbin->Write("binrig");
354 file->WriteObject(&(*rmeanbin), "binrigmean");
355
356 // cf->WriteRigBin(rigbin);
357
358 for (Int_t i=0; i<nbin; i++){
359
360 cf->WriteLongMean(qplane[i], i);
361
362 };
363 }
364 //===============================================================
365 // Save histograms
366 //
367 //
368 //
369 //
370 //
371 //===============================================================
372 void SaveHistos(){
373
374 printf("Finished, calculating average and inverting matrices\n");
375
376
377
378 // cf->WriteNumBin(nbin);
379
380 // TArrayF *rigbin = new TArrayF(18, rig);
381 // cf->WriteRigBin(rigbin);
382
383 for (Int_t i=0; i<nbin; i++){
384
385 // cf->WriteLongMean(qplane[i], i);
386
387 //
388 // determine the average matrix
389 //
390 for (Int_t ii=0; ii<43; ii++){
391 for (Int_t j=0; j<43; j++){
392 // if ( i == 1 ) printf(" num %f \n",(*nmat[i])[ii][j]);
393 if ( (*nmat[i])[ii][j] > 0. ){
394 // if ( i == 1 ) printf(" value %f \n",(*matrix[i])[ii][j]);
395 (*matrix[i])[ii][j] /= (*nmat[i])[ii][j];
396 } else {
397 (*matrix[i])[ii][j] = 0.;
398 };
399 };
400 };
401
402 // if ( i == 2 ){
403 // printf("\n");
404 // for (Int_t ii=0; ii<43; ii++){
405 // for (Int_t j=0; j<43; j++){
406 // printf(" %.f",(*matrix[i])[ii][j]);
407 // };
408 // printf("\n");
409 // };
410 // printf("\n");
411 // };
412
413
414 cf->WriteLongMatrix(matrix[i],i);
415
416 if ( matrix[i]->Determinant() == 0. ){
417 printf("\n");
418 for (Int_t ii=0; ii<43; ii++){
419 for (Int_t j=0; j<43; j++){
420 printf(" %.f",(*matrix[i])[ii][j]);
421 };
422 printf("\n");
423 };
424 printf("\n");
425 printf(" ERROR: the matrix at bin %i is singular, determinant = 0., it cannot be inverted! \n",i);
426 } else {
427 Double_t det = 0.;
428 TMatrixD invmatrix = (TMatrixD)(matrix[i]->Invert(&det));
429 printf(" Bin %i determinant is %f \n",i,det);
430 cf->WriteInvertedLongMatrix((TMatrixD)invmatrix,i);
431 };
432 };
433
434 printf(" done, closing file and exiting\n");
435
436 cf->CloseMatrixFile();
437
438 cf->Delete();
439 }

  ViewVC Help
Powered by ViewVC 1.1.23