]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ITS/AliITSOnlineSDDInjectors.cxx
Removing warnings (Andrea)
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSDDInjectors.cxx
1 /**************************************************************************
2  * Copyright(c) 2007-2009, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15 #include "AliITSOnlineSDDInjectors.h"
16 #include <TH2F.h>
17 #include <TGraphErrors.h>
18 #include <TMath.h>
19
20 /* $Id$ */
21
22 ///////////////////////////////////////////////////////////////////
23 //                                                               //
24 // Implementation of the class used for SDD injector analysis    //
25 // Origin: F.Prino, Torino, prino@to.infn.it                     //
26 //                                                               //
27 ///////////////////////////////////////////////////////////////////
28
29 ClassImp(AliITSOnlineSDDInjectors)
30
31 const Float_t AliITSOnlineSDDInjectors::fgkSaturation = 1008.;
32 const Float_t AliITSOnlineSDDInjectors::fgkDefaultLThreshold = 5.;
33 const Float_t AliITSOnlineSDDInjectors::fgkDefaultHThreshold = 25.;
34 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMinSpeed = 5.5;
35 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxSpeed = 9.0;
36 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxErr = 1.5;
37 const Int_t   AliITSOnlineSDDInjectors::fgkDefaultPolOrder = 3;
38 const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMin[kInjLines] = {20,90,170};
39 const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMax[kInjLines] = {50,160,240};
40
41 //______________________________________________________________________
42 AliITSOnlineSDDInjectors::AliITSOnlineSDDInjectors():AliITSOnlineSDD(),fHisto(),fTbZero(0.),fParam(),fPolOrder(0),fMinDriftSpeed(0.),fMaxDriftSpeed(0.),fMaxDriftSpeedErr(0.),fLowThreshold(0.),fHighThreshold(0.),fFirstPadForFit(0),fLastPadForFit(0),fPadStatusCutForFit(0)
43 {
44   // default constructor
45   SetPositions();
46   SetDefaults();
47 }
48 //______________________________________________________________________
49 AliITSOnlineSDDInjectors::AliITSOnlineSDDInjectors(Int_t nddl, Int_t ncarlos, Int_t sid):AliITSOnlineSDD(nddl,ncarlos,sid),fHisto(),fTbZero(0.),fParam(),fPolOrder(0),fMinDriftSpeed(0.),fMaxDriftSpeed(0.),fMaxDriftSpeedErr(0.),fLowThreshold(0.),fHighThreshold(0.),fFirstPadForFit(0),fLastPadForFit(0),fPadStatusCutForFit(0)
50
51 // standard constructor
52   SetPositions();
53   SetDefaults();
54 }
55 //______________________________________________________________________
56 AliITSOnlineSDDInjectors::~AliITSOnlineSDDInjectors(){
57   // Destructor
58   // fHisto should not be deleted here because it points to an histo created 
59   // by the external code which calls the method AnalyzeEvent
60   // if(fHisto) delete fHisto;  
61   if(fParam) delete [] fParam;
62 }
63 //______________________________________________________________________
64 void AliITSOnlineSDDInjectors::SetDefaults(){
65   for(Int_t i=0;i<kInjLines;i++) 
66     SetInjLineRange(i,fgkDefaultTbMin[i],fgkDefaultTbMax[i]);
67   SetThresholds(fgkDefaultLThreshold,fgkDefaultHThreshold);
68   SetPolOrder(fgkDefaultPolOrder);
69   SetMinDriftSpeed(fgkDefaultMinSpeed);
70   SetMaxDriftSpeed(fgkDefaultMaxSpeed);
71   SetMaxDriftSpeedErr(fgkDefaultMaxErr);
72   SetFitLimits(1,kInjPads-2); // exclude first and last pad
73   SetPadStatusCutForFit();
74 }
75 //______________________________________________________________________
76 void AliITSOnlineSDDInjectors::SetPositions(){
77   // 
78   Float_t xLinFromCenterUm[kInjLines]={31860.,17460.,660.};
79   Float_t xAnodeFromCenterUm=35085;
80   for(Int_t i=0;i<kInjLines;i++){
81     fPosition[i]=xAnodeFromCenterUm-xLinFromCenterUm[i];
82     fPosition[i]/=10000.; // from microns to cm
83   }
84 }
85 //______________________________________________________________________
86 void AliITSOnlineSDDInjectors::Reset(){
87   //
88   for(Int_t i=0;i<kInjPads;i++){ 
89     fDriftSpeed[i]=0.;
90     fDriftSpeedErr[i]=0.;
91   }
92   for(Int_t i=0;i<kInjPads;i++){
93     for(Int_t j=0;j<kInjLines;j++){
94       fGoodInj[i][j]=0;
95       fCentroid[i][j]=0.;
96       fRMSCentroid[i][j]=0.;
97     }
98   }
99 }
100 //______________________________________________________________________
101 void AliITSOnlineSDDInjectors::AnalyzeEvent(TH2F* his){
102   //
103   Reset();
104   fHisto=his;
105   FindGoodInjectors();
106   FindCentroids();
107   CalcTimeBinZero();
108   for(Int_t j=0;j<kInjPads;j++) CalcDriftSpeed(j);
109   FitDriftSpeedVsAnode();
110 }
111 //______________________________________________________________________
112 TGraphErrors* AliITSOnlineSDDInjectors::GetTimeVsDistGraph(Int_t jpad) const{
113   // 
114   const Int_t kPts=kInjLines+1;
115   Float_t x[kPts],y[kPts],ex[kPts],ey[kPts];
116   x[0]=0.;
117   ex[0]=0.;
118   y[0]=fTbZero;
119   ey[0]=0.;
120   for(Int_t i=0;i<kInjLines;i++){
121     x[i+1]=fPosition[i];
122     ex[i+1]=0.;
123     y[i+1]=fCentroid[jpad][i];
124     ey[i+1]=fRMSCentroid[jpad][i];
125   }
126   TGraphErrors *g=new TGraphErrors(4,x,y,ex,ey);
127   return g;
128 }
129
130 //______________________________________________________________________
131 TGraphErrors* AliITSOnlineSDDInjectors::GetDriftSpeedGraph() const{
132   // 
133   Int_t ipt=0;
134   TGraphErrors *g=new TGraphErrors(0);
135   for(Int_t i=0;i<kInjPads;i++){
136     if(fDriftSpeed[i]>0){ 
137       g->SetPoint(ipt,GetAnodeNumber(i),fDriftSpeed[i]);
138       g->SetPointError(ipt,0,fDriftSpeedErr[i]);
139       ipt++;
140     }
141   }
142   return g;
143 }
144 //______________________________________________________________________
145 TGraphErrors* AliITSOnlineSDDInjectors::GetSelectedDriftSpeedGraph(Int_t minAcceptStatus) const{
146   // TGraphErrors with only pads with status of injector >= minAcceptStatus
147   Int_t ipt=0;
148   TGraphErrors *g=new TGraphErrors(0);
149   for(Int_t i=0;i<kInjPads;i++){
150     Int_t padStatus = GetInjPadStatus(i);
151     if(fDriftSpeed[i]>0 && padStatus >= minAcceptStatus ){
152       g->SetPoint(ipt,GetAnodeNumber(i),fDriftSpeed[i]);
153       g->SetPointError(ipt,0,fDriftSpeedErr[i]);
154       ipt++;
155     }
156   }
157   return g;
158 }
159 //______________________________________________________________________
160 void AliITSOnlineSDDInjectors::CalcTimeBinZero(){
161   //
162   Float_t tzero=0.,intCont=0.;
163   for(Int_t ian=0;ian<fgkNAnodes;ian++){
164     for(Int_t itb=1;itb<fTbMin[0];itb++){
165       Float_t cont=fHisto->GetBinContent(itb,ian+1);
166       if(cont>fLowThreshold){
167         tzero+=cont*float(itb);
168         intCont+=cont;
169       }
170     }
171   }
172   if(intCont>0) fTbZero=tzero/intCont;
173 }
174 //______________________________________________________________________
175 void AliITSOnlineSDDInjectors::FitDriftSpeedVsAnode(){
176   // fits the anode dependence of drift speed with a polynomial function
177   const Int_t kNn=fPolOrder+1;
178   Float_t **mat = new Float_t*[kNn];
179   for(Int_t i=0; i < kNn; i++) mat[i] = new Float_t[kNn];
180   Float_t *vect = new Float_t[kNn];
181
182   for(Int_t k1=0;k1<kNn;k1++){
183     vect[k1]=0;
184     for(Int_t k2=0;k2<kNn;k2++){
185       mat[k1][k2]=0;
186     }
187   }
188   for(Int_t k1=0;k1<kNn;k1++){
189     for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
190       Float_t x=(Float_t)GetAnodeNumber(jpad);
191       if(fDriftSpeed[jpad]>0 && GetInjPadStatus(jpad)>fPadStatusCutForFit){
192           vect[k1]+=fDriftSpeed[jpad]*TMath::Power(x,k1)/TMath::Power(fDriftSpeedErr[jpad],2);  
193           for(Int_t k2=0;k2<kNn;k2++){
194
195             mat[k1][k2]+=TMath::Power(x,k1+k2)/TMath::Power(fDriftSpeedErr[jpad],2);
196           }
197       }
198     }
199   }
200   Int_t *iPivot = new Int_t[kNn];
201   Int_t *indxR = new Int_t[kNn];
202   Int_t *indxC = new Int_t[kNn];
203   for(Int_t i=0;i<kNn;i++) iPivot[i]=0;
204   Int_t iCol=-1,iRow=-1;
205   for(Int_t i=0;i<kNn;i++){
206     Float_t big=0.;
207     for(Int_t j=0;j<kNn;j++){
208       if(iPivot[j]!=1){
209         for(Int_t k=0;k<kNn;k++){
210            if(iPivot[k]==0){
211              if(TMath::Abs(mat[j][k])>=big){
212                big=TMath::Abs(mat[j][k]);
213                iRow=j;
214                iCol=k;
215              }
216           }
217         }
218       }
219     }
220     iPivot[iCol]++;
221     Float_t aux;
222     if(iRow!=iCol){
223       for(Int_t l=0;l<kNn;l++){
224         aux=mat[iRow][l];
225         mat[iRow][l]=mat[iCol][l];
226         mat[iCol][l]=aux;
227       }
228       aux=vect[iRow];
229       vect[iRow]=vect[iCol];
230       vect[iCol]=aux;
231     }
232     indxR[i]=iRow;
233     indxC[i]=iCol;
234     if(mat[iCol][iCol]==0) break;
235     Float_t pivinv=1./mat[iCol][iCol];
236     mat[iCol][iCol]=1;
237     for(Int_t l=0;l<kNn;l++) mat[iCol][l]*=pivinv;
238     vect[iCol]*=pivinv;
239     for(Int_t m=0;m<kNn;m++){
240       if(m!=iCol){
241         aux=mat[m][iCol];
242         mat[m][iCol]=0;
243         for(Int_t n=0;n<kNn;n++) mat[m][n]-=mat[iCol][n]*aux;
244         vect[m]-=vect[iCol]*aux;
245       }
246     }    
247   }
248   delete [] iPivot;
249   delete [] indxR;
250   delete [] indxC;
251
252   if(fParam) delete [] fParam;
253   fParam=new Float_t[kNn];
254   for(Int_t i=0; i<kNn;i++)fParam[i]=vect[i];
255
256   for(Int_t i=0; i < kNn; i++) delete [] mat[i];
257   delete [] mat;
258   delete [] vect;
259 }
260 //______________________________________________________________________
261 void AliITSOnlineSDDInjectors::CalcDriftSpeed(Int_t jpad){
262   // 
263   Float_t sumY=0,sumX=0,sumXX=0,sumYY=0.,sumXY=0,sumWEI=0.;
264   Int_t npt=0;
265   Float_t y[kInjLines],ey[kInjLines];
266   Float_t tzero=0,erry=0;
267   for(Int_t i=0;i<kInjLines;i++){ 
268     y[i]=fCentroid[jpad][i];
269     ey[i]=fRMSCentroid[jpad][i];
270   }
271   for(Int_t i=0;i<kInjLines;i++){
272     if(fGoodInj[jpad][i] && ey[i]!=0){
273       sumY+=y[i]/ey[i]/ey[i];
274       sumX+=fPosition[i]/ey[i]/ey[i];
275       sumXX+=fPosition[i]*fPosition[i]/ey[i]/ey[i];
276       sumYY+=y[i]*y[i]/ey[i]/ey[i];
277       sumXY+=fPosition[i]*y[i]/ey[i]/ey[i];
278       sumWEI+=1./ey[i]/ey[i];
279       tzero=fTbZero/ey[i]/ey[i];
280       erry=ey[i];
281       npt++;
282     }
283   }
284   Float_t vel=0,evel=0;
285   if(npt>1){ 
286     Float_t slope=(sumWEI*sumXY-sumY*sumX)/(sumWEI*sumXX-sumX*sumX);
287     Float_t eslope=TMath::Sqrt(sumWEI/(sumWEI*sumXX-sumX*sumX));
288     if(slope!=0){
289       vel=1./slope*10000./25.;// micron/ns
290       evel=eslope/slope/slope*10000./25.;// micron/ns
291     }
292   }
293   if(npt==1){
294     Float_t slope=(sumY-tzero)/sumX;
295     Float_t eslope=erry/sumX;
296     if(slope!=0){
297       vel=1./slope*10000./25.;// micron/ns    
298       evel=eslope/slope/slope*10000./25.;// micron/ns
299     }
300   }
301   if(vel>fMaxDriftSpeed||vel<fMinDriftSpeed || evel>fMaxDriftSpeedErr){ 
302     vel=0.;
303     evel=0.;
304   }
305   fDriftSpeed[jpad]=vel;
306   fDriftSpeedErr[jpad]=evel;
307 }
308 //______________________________________________________________________
309 Int_t AliITSOnlineSDDInjectors::GetAnodeNumber(Int_t iInjPad) const{
310   // Injectors location along anodes:
311   // Side left  (UP)   - channel 0: injectors on anodes 0,7,15,...,247,255 
312   // Side right (DOWN) - channel 1: injectors on anodes 0,8,16,...,248,255 
313   Int_t ian=-1;
314   if(iInjPad>=kInjPads) return ian;
315   if(fSide==1){  // right side
316     ian=iInjPad*8;
317     if(iInjPad==32) ian--;
318   }else{         // left side
319     ian=iInjPad*8-1;
320     if(iInjPad==0) ian=0;
321   }
322   return ian;
323 }
324 //______________________________________________________________________
325 Int_t AliITSOnlineSDDInjectors::GetInjPadNumberFromAnode(Int_t nAnode) const{
326   //
327   Int_t iInjPad=-1;
328   if(fSide==1){  // right side
329     if(nAnode%8==0) iInjPad=nAnode/8;
330     if(nAnode==255) iInjPad=32;
331   }else{         // left side
332     if(nAnode%8==7) iInjPad=1+nAnode/8;
333     if(nAnode==0) iInjPad=0;
334   }
335   if(nAnode>=256) iInjPad=-1;
336   return iInjPad;
337 }
338 //______________________________________________________________________
339 Int_t AliITSOnlineSDDInjectors::GetInjPadStatus(Int_t jpad) const{
340   // returns an integer value with status of injector lines for given pad/anode
341   // status=7  -->  111  all injector are good
342   // status=6  -->  110  1st line (close to anodes) is bad, other two are good
343   // ....
344   // status=1  -->  001  only 1st line (close to anodes) good
345   // status=0  -->  000  all lines are bad
346   Int_t istatus=0;
347   if(jpad>=0 && jpad<kInjPads){
348     for(Int_t jlin=0;jlin<kInjLines;jlin++) istatus+=fGoodInj[jpad][jlin]<<jlin;
349   }
350   return istatus;
351 }
352 //______________________________________________________________________
353 void AliITSOnlineSDDInjectors::FindGoodInjectors(){
354   // 
355   for(Int_t jpad=0;jpad<kInjPads;jpad++){
356     Int_t ian=GetAnodeNumber(jpad);
357     for(Int_t jlin=0;jlin<kInjLines;jlin++){
358       for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
359         Float_t c1=fHisto->GetBinContent(jjj,ian+1);
360         Float_t c2=fHisto->GetBinContent(jjj+1,ian+1);
361         Float_t c3=fHisto->GetBinContent(jjj+2,ian+1);
362         if(c1>fLowThreshold && c2>fHighThreshold && c3>fLowThreshold){ 
363           fGoodInj[jpad][jlin]=1;
364           break;
365         }
366       }
367     }
368   }
369 }
370 //______________________________________________________________________
371 void AliITSOnlineSDDInjectors::FindCentroids(){
372   // 
373   for(Int_t jpad=0;jpad<kInjPads;jpad++){
374     Int_t ian=GetAnodeNumber(jpad);
375     for(Int_t jlin=0;jlin<kInjLines;jlin++){
376       if(!fGoodInj[jpad][jlin]) continue;
377       Float_t maxcont=0;
378       Int_t ilmax=-1;
379       for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
380         Float_t cont=fHisto->GetBinContent(jjj,ian+1);
381         if(cont>maxcont){
382           maxcont=cont;
383           ilmax=jjj;
384         }
385       }
386       Float_t intCont=0;
387       Int_t jjj=ilmax;
388       while(1){
389         Float_t cont=fHisto->GetBinContent(jjj,ian+1);
390         if(cont<fLowThreshold) break;
391         if(cont<fgkSaturation){
392           fCentroid[jpad][jlin]+=cont*(Float_t)jjj;
393           fRMSCentroid[jpad][jlin]+=cont*TMath::Power((Float_t)jjj,2);
394           intCont+=cont;
395         }
396         jjj--;
397       }
398       jjj=ilmax+1;
399       while(1){
400         Float_t cont=fHisto->GetBinContent(jjj,ian+1);
401         if(cont<fLowThreshold) break;
402         if(cont<fgkSaturation){
403           fCentroid[jpad][jlin]+=cont*float(jjj);
404           fRMSCentroid[jpad][jlin]+=cont*TMath::Power((Float_t)jjj,2);
405           intCont+=cont;
406         }
407         jjj++;
408       }
409       if(intCont>0){ 
410         fCentroid[jpad][jlin]/=intCont;
411         fRMSCentroid[jpad][jlin]=TMath::Sqrt(fRMSCentroid[jpad][jlin]/intCont-fCentroid[jpad][jlin]*fCentroid[jpad][jlin]);
412       }
413       else{ 
414         fCentroid[jpad][jlin]=0.;
415         fRMSCentroid[jpad][jlin]=0.;
416         fGoodInj[jpad][jlin]=0;
417       }
418       if(fRMSCentroid[jpad][jlin]==0) fGoodInj[jpad][jlin]=0;
419     }
420   }
421 }
422 //______________________________________________________________________
423 void AliITSOnlineSDDInjectors::PrintInjectorStatus(){
424   //
425   for(Int_t jpad=0;jpad<kInjPads;jpad++){
426     printf("Line%d-Anode%d: %d %d %d\n",jpad,GetAnodeNumber(jpad),fGoodInj[jpad][0],fGoodInj[jpad][1],fGoodInj[jpad][2]);
427   }
428 }
429 //______________________________________________________________________
430 void AliITSOnlineSDDInjectors::PrintCentroids(){
431   //
432   for(Int_t jpad=0;jpad<kInjPads;jpad++){
433     printf("Line%d-Anode%d: %f+-%f %f+-%f %f+-%f\n",jpad,GetAnodeNumber(jpad),fCentroid[jpad][0],fRMSCentroid[jpad][0],fCentroid[jpad][1],fRMSCentroid[jpad][1],fCentroid[jpad][2],fRMSCentroid[jpad][2]);
434   }
435 }
436 //______________________________________________________________________
437 void AliITSOnlineSDDInjectors::WriteToASCII(Int_t evNumb, UInt_t timeStamp, Int_t optAppend){
438   //
439   Char_t outfilnam[100];
440   sprintf(outfilnam,"SDDinj_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);  
441   FILE* outf;
442   if(optAppend==0){ 
443     outf=fopen(outfilnam,"w");
444     fprintf(outf,"%d\n",fPolOrder);
445   }
446   else outf=fopen(outfilnam,"a");
447   fprintf(outf,"%d   %d   ",evNumb,timeStamp);
448   for(Int_t ic=0;ic<fPolOrder+1;ic++){
449     fprintf(outf,"%G ",fParam[ic]);
450   }
451   fprintf(outf,"\n");
452   fclose(outf);  
453 }