For Pythia with tune don't switch off MI in ConfigHeavyFlavor
[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 <TFile.h>
16 #include "AliITSOnlineSDDInjectors.h"
17 #include "AliLog.h"
18 #include <TH2F.h>
19 #include <TF1.h>
20 #include <TGraphErrors.h>
21 #include <TMath.h>
22
23 /* $Id$ */
24
25 ///////////////////////////////////////////////////////////////////
26 //                                                               //
27 // Implementation of the class used for SDD injector analysis    //
28 // Origin: F.Prino, Torino, prino@to.infn.it                     //
29 //                                                               //
30 ///////////////////////////////////////////////////////////////////
31
32 ClassImp(AliITSOnlineSDDInjectors)
33
34 const Float_t AliITSOnlineSDDInjectors::fgkSaturation = 1008.;
35 const Float_t AliITSOnlineSDDInjectors::fgkDefaultLThreshold = 5.;
36 const Float_t AliITSOnlineSDDInjectors::fgkDefaultHThreshold = 25.;
37 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMinSpeed = 5.5;
38 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxSpeed = 9.0;
39 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxErr = 1.5;
40 const Int_t   AliITSOnlineSDDInjectors::fgkDefaultPolDegree = 3;
41 const Float_t AliITSOnlineSDDInjectors::fgkDefaultTimeStep = 50.;
42 const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMin[kInjLines] = {10,50,100};
43 const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMax[kInjLines] = {20,70,120};
44
45 //______________________________________________________________________
46 AliITSOnlineSDDInjectors::AliITSOnlineSDDInjectors():AliITSOnlineSDD(),fHisto(),fTbZero(0.),fRMSTbZero(0.),fNEvents(0),fParam(),fPolDegree(0),fActualPolDegree(0),fMinDriftSpeed(0.),fMaxDriftSpeed(0.),fMaxDriftSpeedErr(0.),fLowThreshold(0.),fHighThreshold(0.),fFirstPadForFit(0),fLastPadForFit(0),fPadStatusCutForFit(0),fTimeStep(0.),fUseTimeZeroSignal(kFALSE)
47 {
48   // default constructor
49   SetPositions();
50   SetDefaults();
51   SetTimeStep(fgkDefaultTimeStep);
52 }
53 //______________________________________________________________________
54 AliITSOnlineSDDInjectors::AliITSOnlineSDDInjectors(Int_t nddl, Int_t ncarlos, Int_t sid):AliITSOnlineSDD(nddl,ncarlos,sid),fHisto(),fTbZero(0.),fRMSTbZero(0.),fNEvents(0),fParam(),fPolDegree(0),fActualPolDegree(0),fMinDriftSpeed(0.),fMaxDriftSpeed(0.),fMaxDriftSpeedErr(0.),fLowThreshold(0.),fHighThreshold(0.),fFirstPadForFit(0),fLastPadForFit(0),fPadStatusCutForFit(0),fTimeStep(0.),fUseTimeZeroSignal(kFALSE)
55
56 // standard constructor
57   SetPositions();
58   SetDefaults();
59   SetTimeStep(fgkDefaultTimeStep);
60 }
61 //______________________________________________________________________
62 AliITSOnlineSDDInjectors::~AliITSOnlineSDDInjectors(){
63   // Destructor
64   // fHisto should not be deleted here because it points to an histo created 
65   // by the external code which calls the method AnalyzeEvent
66   // if(fHisto) delete fHisto;  
67   if(fParam) delete [] fParam;
68 }
69 //______________________________________________________________________
70 void AliITSOnlineSDDInjectors::SetDefaults(){
71   for(Int_t i=0;i<kInjLines;i++) {
72     SetInjLineRange(i,fgkDefaultTbMin[i],fgkDefaultTbMax[i]);
73     SetUseLine(i,kTRUE);
74   }
75   SetThresholds(fgkDefaultLThreshold,fgkDefaultHThreshold);
76   SetPolDegree(fgkDefaultPolDegree);
77   SetMinDriftSpeed(fgkDefaultMinSpeed);
78   SetMaxDriftSpeed(fgkDefaultMaxSpeed);
79   SetMaxDriftSpeedErr(fgkDefaultMaxErr);
80   SetFitLimits(1,kInjPads-2); // exclude first and last pad
81   SetPadStatusCutForFit();
82 }
83 //______________________________________________________________________
84 void AliITSOnlineSDDInjectors::SetPositions(){
85   // 
86   Double_t xLinFromCenterUm[kInjLines]={31860.,17460.,660.};
87   Double_t xAnodeFromCenterUm=35085;
88   for(Int_t i=0;i<kInjLines;i++){
89     fPosition[i]=xAnodeFromCenterUm-xLinFromCenterUm[i];
90     fPosition[i]/=10000.; // from microns to cm
91   }
92 }
93 //______________________________________________________________________
94 void AliITSOnlineSDDInjectors::Reset(){
95   //
96   for(Int_t i=0;i<kInjPads;i++){ 
97     fDriftSpeed[i]=0.;
98     fDriftSpeedErr[i]=0.;
99   }
100   for(Int_t i=0;i<kInjPads;i++){
101     for(Int_t j=0;j<kInjLines;j++){
102       fGoodInj[i][j]=0;
103       fCentroid[i][j]=0.;
104       fRMSCentroid[i][j]=0.;
105     }
106   }
107 }
108 //______________________________________________________________________
109 void AliITSOnlineSDDInjectors::AnalyzeEvent(TH2F* his){
110   //
111   AddEvent(his);
112   FitDriftSpeedVsAnode();
113 }
114 //______________________________________________________________________
115 void AliITSOnlineSDDInjectors::AddEvent(TH2F* his){
116   // Add the drift speed from current event to the average value
117   if(fNEvents==0){
118     for(Int_t i=0;i<kInjPads;i++){ 
119       fSumDriftSpeed[i]=0.;
120       fSumSqDriftSpeed[i]=0.;
121     }
122   }
123   Reset();
124   fHisto=his;
125   FindGoodInjectors();
126   FindCentroids();
127   CalcTimeBinZero();
128   for(Int_t j=0;j<kInjPads;j++){ 
129     CalcDriftSpeed(j);
130     fSumDriftSpeed[j]+=fDriftSpeed[j];
131     fSumSqDriftSpeed[j]+=fDriftSpeed[j]*fDriftSpeed[j];
132   }
133   ++fNEvents;
134 }
135 //______________________________________________________________________
136 Double_t AliITSOnlineSDDInjectors::GetRMSDriftSpeed(Int_t ipad) const {
137   // 
138   if(fNEvents<=1) return 0.;
139   Double_t mean=fSumDriftSpeed[ipad]/(Double_t)fNEvents;
140   Double_t diff=fSumSqDriftSpeed[ipad]/(Double_t)fNEvents-mean*mean;
141   if(diff<0.) diff=0.;
142   return TMath::Sqrt(diff);
143 }
144
145 //______________________________________________________________________
146 void AliITSOnlineSDDInjectors::FitMeanDriftSpeedVsAnode(){
147   // Calculates
148   if(fNEvents==0) return;
149   for(Int_t i=0;i<kInjPads;i++){ 
150     fDriftSpeed[i]=GetMeanDriftSpeed(i);
151     if(fNEvents>1){
152       Double_t rms=GetRMSDriftSpeed(i);
153       if(rms>0.) fDriftSpeedErr[i]=rms/TMath::Sqrt(fNEvents);
154     }
155   }
156   FitDriftSpeedVsAnode();
157 }
158 //______________________________________________________________________
159 TGraphErrors* AliITSOnlineSDDInjectors::GetTimeVsDistGraph(Int_t jpad) const{
160   // 
161   const Int_t kPts=kInjLines+1;
162   Float_t x[kPts],y[kPts],ex[kPts],ey[kPts];
163   x[0]=0.;
164   ex[0]=0.;
165   y[0]=fTbZero;
166   ey[0]=0.;
167   for(Int_t i=0;i<kInjLines;i++){
168     x[i+1]=fPosition[i];
169     ex[i+1]=0.;
170     y[i+1]=fCentroid[jpad][i];
171     ey[i+1]=fRMSCentroid[jpad][i];
172   }
173   TGraphErrors *g=new TGraphErrors(4,x,y,ex,ey);
174   return g;
175 }
176
177 //______________________________________________________________________
178 TGraphErrors* AliITSOnlineSDDInjectors::GetDriftSpeedGraph() const{
179   // 
180   Int_t ipt=0;
181   TGraphErrors *g=new TGraphErrors(0);
182   for(Int_t i=0;i<kInjPads;i++){
183     if(fDriftSpeed[i]>0){ 
184       g->SetPoint(ipt,GetAnodeNumber(i),fDriftSpeed[i]);
185       g->SetPointError(ipt,0,fDriftSpeedErr[i]);
186       ipt++;
187     }
188   }
189   return g;
190 }
191 //______________________________________________________________________
192 TGraphErrors* AliITSOnlineSDDInjectors::GetSelectedDriftSpeedGraph(Int_t minAcceptStatus) const{
193   // TGraphErrors with only pads with status of injector >= minAcceptStatus
194   Int_t ipt=0;
195   TGraphErrors *g=new TGraphErrors(0);
196   for(Int_t i=0;i<kInjPads;i++){
197     Int_t padStatus = GetInjPadStatus(i);
198     if(fDriftSpeed[i]>0 && padStatus >= minAcceptStatus ){
199       g->SetPoint(ipt,GetAnodeNumber(i),fDriftSpeed[i]);
200       g->SetPointError(ipt,0,fDriftSpeedErr[i]);
201       ipt++;
202     }
203   }
204   return g;
205 }
206 //______________________________________________________________________
207 void AliITSOnlineSDDInjectors::CalcTimeBinZero(){
208   // Get time zero from trigger signal
209   Double_t tzero=0.,intCont=0.,rmsPeak=0.;
210   Bool_t isTbUsed[256];
211   Int_t nTbUsed=0;
212   for(Int_t i=0;i<256;i++) isTbUsed[i]=0;
213   for(Int_t ian=0;ian<fgkNAnodes;ian++){
214     for(Int_t itb=1;itb<fTbMin[0];itb++){
215       Double_t cont=fHisto->GetBinContent(itb,ian+1);
216       Double_t contm1=fHisto->GetBinContent(itb+1,ian+1);
217       Double_t contp1=fHisto->GetBinContent(itb-1,ian+1);
218       if(cont>fLowThreshold){
219         if(contm1>fHighThreshold || cont>fHighThreshold || contp1>fHighThreshold){
220           tzero+=cont*float(itb);
221           rmsPeak+=cont*float(itb)*float(itb);
222           intCont+=cont;
223           if(!isTbUsed[itb]){
224             isTbUsed[itb]=1;
225             ++nTbUsed;
226           }
227         }
228       }
229     }
230   }
231   if(intCont>0){ 
232     fTbZero=tzero/intCont;
233     fRMSTbZero=TMath::Sqrt(rmsPeak/intCont-fTbZero*fTbZero);
234   }
235   if(nTbUsed==1) fRMSTbZero=0.5; 
236 }
237
238 //______________________________________________________________________
239 void AliITSOnlineSDDInjectors::FitDriftSpeedVsAnode(){
240   // fits the anode dependence of drift speed
241
242   Float_t rangeForMax[2]={78.,178.};
243   PolyFit(fPolDegree);
244   fActualPolDegree=fPolDegree;
245   if(fPolDegree==3){
246     Double_t deltasq=fParam[2]*fParam[2]-3*fParam[1]*fParam[3];
247     Double_t zero1=-999.;
248     Double_t zero2=-999.;
249     if(deltasq>=0. && TMath::Abs(fParam[3])>0.){
250       Double_t delta=TMath::Sqrt(deltasq);
251       zero1=(-fParam[2]+delta)/3./fParam[3];
252       zero2=(-fParam[2]-delta)/3./fParam[3];
253     }
254     Bool_t twoZeroes=kFALSE;
255     Bool_t oneZero=kFALSE;
256     if(zero1>0. && zero1<256. && zero2>0. && zero2<256.) twoZeroes=kTRUE;
257     if(zero1>rangeForMax[0] && zero1<rangeForMax[1]) oneZero=kTRUE;
258     if(zero2>rangeForMax[0] && zero2<rangeForMax[1]) oneZero=kTRUE;
259     if(!oneZero || twoZeroes){
260       PolyFit(2);
261       Double_t xmax=-999.;
262       if(fParam[2]<0.) xmax=-fParam[1]/2./fParam[2];
263       if(xmax>rangeForMax[0] && xmax<rangeForMax[1]){
264         fActualPolDegree=2;
265       }else{
266         Double_t averSpeed=0.;
267         Double_t sumWei=0.;
268         Int_t nUsedPts=0;
269         for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
270           if(fDriftSpeed[jpad]>0 && GetInjPadStatus(jpad)>fPadStatusCutForFit){
271             Double_t wei=1./fDriftSpeedErr[jpad]/fDriftSpeedErr[jpad];
272             averSpeed+=wei*fDriftSpeed[jpad];
273             sumWei+=wei;
274             nUsedPts++;
275           }
276         }
277         if(sumWei>0.) averSpeed/=sumWei;
278         if(nUsedPts<fPolDegree+1) averSpeed=0;
279         fParam[0]=averSpeed;
280         for(Int_t i=1; i < fPolDegree+1; i++) fParam[i]=0.;
281         fActualPolDegree=0;
282       }
283     }
284   }
285 }
286 //______________________________________________________________________
287 void AliITSOnlineSDDInjectors::PolyFit(Int_t degree){
288   // fits the anode dependence of drift speed with a polynomial function
289   const Int_t kNn=degree+1;
290   const Int_t kDimens=fPolDegree+1;
291
292   Double_t **mat = new Double_t*[kNn];
293   for(Int_t i=0; i < kNn; i++) mat[i] = new Double_t[kNn];
294   Double_t *vect = new Double_t[kNn];
295
296   for(Int_t k1=0;k1<kNn;k1++){
297     vect[k1]=0;
298     for(Int_t k2=0;k2<kNn;k2++){
299       mat[k1][k2]=0;
300     }
301   }
302   Int_t npts = 0;
303   for(Int_t k1=0;k1<kNn;k1++){
304     for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
305       Double_t x=(Double_t)GetAnodeNumber(jpad);
306       if(fDriftSpeed[jpad]>0 && GetInjPadStatus(jpad)>fPadStatusCutForFit){
307           vect[k1]+=fDriftSpeed[jpad]*TMath::Power(x,k1)/TMath::Power(fDriftSpeedErr[jpad],2);  
308           if(k1==0) npts++;
309           for(Int_t k2=0;k2<kNn;k2++){
310             mat[k1][k2]+=TMath::Power(x,k1+k2)/TMath::Power(fDriftSpeedErr[jpad],2);
311           }
312       }
313     }
314   }
315   if(npts<fPolDegree+1){ 
316     if(fParam) delete [] fParam;
317     fParam=new Double_t[kDimens];
318     for(Int_t i=0; i<kDimens;i++)fParam[i]=0;
319   }else{
320     Int_t *iPivot = new Int_t[kNn];
321     Int_t *indxR = new Int_t[kNn];
322     Int_t *indxC = new Int_t[kNn];
323     for(Int_t i=0;i<kNn;i++) iPivot[i]=0;
324     Int_t iCol=-1,iRow=-1;
325     for(Int_t i=0;i<kNn;i++){
326       Double_t big=0.;
327       for(Int_t j=0;j<kNn;j++){
328         if(iPivot[j]!=1){
329           for(Int_t k=0;k<kNn;k++){
330             if(iPivot[k]==0){
331               if(TMath::Abs(mat[j][k])>=big){
332                 big=TMath::Abs(mat[j][k]);
333                 iRow=j;
334                 iCol=k;
335               }
336             }
337           }
338         }
339       }
340       iPivot[iCol]++;
341       Double_t aux;
342       if(iRow!=iCol){
343         for(Int_t l=0;l<kNn;l++){
344           aux=mat[iRow][l];
345           mat[iRow][l]=mat[iCol][l];
346           mat[iCol][l]=aux;
347         }
348         aux=vect[iRow];
349         vect[iRow]=vect[iCol];
350         vect[iCol]=aux;
351       }
352       indxR[i]=iRow;
353       indxC[i]=iCol;
354       if(mat[iCol][iCol]==0) break;
355       Double_t pivinv=1./mat[iCol][iCol];
356       mat[iCol][iCol]=1;
357       for(Int_t l=0;l<kNn;l++) mat[iCol][l]*=pivinv;
358       vect[iCol]*=pivinv;
359       for(Int_t m=0;m<kNn;m++){
360         if(m!=iCol){
361           aux=mat[m][iCol];
362           mat[m][iCol]=0;
363           for(Int_t n=0;n<kNn;n++) mat[m][n]-=mat[iCol][n]*aux;
364           vect[m]-=vect[iCol]*aux;
365         }
366       }    
367     }
368     delete [] iPivot;
369     delete [] indxR;
370     delete [] indxC;
371     
372   
373     if(fParam) delete [] fParam;
374     fParam=new Double_t[kDimens];
375     for(Int_t i=0; i<kNn;i++)fParam[i]=vect[i];
376     if(degree<fPolDegree) for(Int_t i=kNn; i<kDimens;i++)fParam[i]=0.;
377   }
378
379   for(Int_t i=0; i < kNn; i++) delete [] mat[i];
380   delete [] mat;
381   delete [] vect;
382 }
383 //______________________________________________________________________
384 void AliITSOnlineSDDInjectors::CalcDriftSpeed(Int_t jpad){
385   // 
386   Double_t sumY=0,sumX=0,sumXX=0,sumYY=0.,sumXY=0,sumWEI=0.;
387   Int_t npt=0;
388   Double_t y[kInjLines],ey[kInjLines];
389   Double_t tzero=0,erry=0;
390   for(Int_t i=0;i<kInjLines;i++){ 
391     y[i]=fCentroid[jpad][i];
392     ey[i]=fRMSCentroid[jpad][i];
393   }
394   for(Int_t i=0;i<kInjLines;i++){
395     if(!fUseLine[i]) continue;
396     if(fGoodInj[jpad][i] && ey[i]!=0){
397       sumY+=y[i]/ey[i]/ey[i];
398       sumX+=fPosition[i]/ey[i]/ey[i];
399       sumXX+=fPosition[i]*fPosition[i]/ey[i]/ey[i];
400       sumYY+=y[i]*y[i]/ey[i]/ey[i];
401       sumXY+=fPosition[i]*y[i]/ey[i]/ey[i];
402       sumWEI+=1./ey[i]/ey[i];
403       tzero=fTbZero/ey[i]/ey[i];
404       erry=ey[i]/ey[i]/ey[i];
405       npt++;
406     }
407   }
408   Double_t slope=0.,eslope=0.;
409   if(npt==1){
410     slope=(sumY-tzero)/sumX;
411     eslope=erry/sumX;
412   }
413   if(npt>1){ 
414     if(fUseTimeZeroSignal){
415       sumY+=fTbZero/fRMSTbZero/fRMSTbZero;
416       sumX+=0.;
417       sumXX+=0.;
418       sumYY+=fTbZero*fTbZero/fRMSTbZero/fRMSTbZero;
419       sumXY+=0.;
420       sumWEI+=1./fRMSTbZero/fRMSTbZero;
421     }
422     slope=(sumWEI*sumXY-sumY*sumX)/(sumWEI*sumXX-sumX*sumX);
423     eslope=TMath::Sqrt(sumWEI/(sumWEI*sumXX-sumX*sumX));
424   }
425
426   Double_t vel=0,evel=0;
427   if(slope!=0. && fTimeStep>0.){
428     vel=1./slope*10000./fTimeStep;// micron/ns
429     evel=eslope/slope/slope*10000./fTimeStep;// micron/ns
430   }
431   if(vel>fMaxDriftSpeed||vel<fMinDriftSpeed || evel>fMaxDriftSpeedErr){ 
432     vel=0.;
433     evel=0.;
434   }
435   fDriftSpeed[jpad]=vel;
436   fDriftSpeedErr[jpad]=evel;
437 }
438 //______________________________________________________________________
439 Int_t AliITSOnlineSDDInjectors::GetAnodeNumber(Int_t iInjPad) const{
440   // Injectors location along anodes:
441   // Side left  (UP)   - channel 0: injectors on anodes 0,7,15,...,247,255 
442   // Side right (DOWN) - channel 1: injectors on anodes 0,8,16,...,248,255 
443   Int_t ian=-1;
444   if(iInjPad>=kInjPads) return ian;
445   if(fSide==1){  // right side
446     ian=iInjPad*8;
447     if(iInjPad==32) ian--;
448   }else{         // left side
449     ian=iInjPad*8-1;
450     if(iInjPad==0) ian=0;
451   }
452   return ian;
453 }
454 //______________________________________________________________________
455 Int_t AliITSOnlineSDDInjectors::GetInjPadNumberFromAnode(Int_t nAnode) const{
456   //
457   Int_t iInjPad=-1;
458   if(fSide==1){  // right side
459     if(nAnode%8==0) iInjPad=nAnode/8;
460     if(nAnode==255) iInjPad=32;
461   }else{         // left side
462     if(nAnode%8==7) iInjPad=1+nAnode/8;
463     if(nAnode==0) iInjPad=0;
464   }
465   if(nAnode>=256) iInjPad=-1;
466   return iInjPad;
467 }
468 //______________________________________________________________________
469 Int_t AliITSOnlineSDDInjectors::GetInjPadStatus(Int_t jpad) const{
470   // returns an integer value with status of injector lines for given pad/anode
471   // status=7  -->  111  all injector are good
472   // status=6  -->  110  1st line (close to anodes) is bad, other two are good
473   // ....
474   // status=1  -->  001  only 1st line (close to anodes) good
475   // status=0  -->  000  all lines are bad
476   Int_t istatus=0;
477   if(jpad>=0 && jpad<kInjPads){
478     for(Int_t jlin=0;jlin<kInjLines;jlin++) istatus+=fGoodInj[jpad][jlin]<<jlin;
479   }
480   return istatus;
481 }
482 //______________________________________________________________________
483 void AliITSOnlineSDDInjectors::FindGoodInjectors(){
484   // 
485   for(Int_t jpad=0;jpad<kInjPads;jpad++){
486     Int_t ian=GetAnodeNumber(jpad);
487     for(Int_t jlin=0;jlin<kInjLines;jlin++){
488       for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
489         Float_t c1=fHisto->GetBinContent(jjj,ian+1);
490         Float_t c2=fHisto->GetBinContent(jjj+1,ian+1);
491         //      Float_t c3=fHisto->GetBinContent(jjj+2,ian+1);
492         if(c1>fLowThreshold && c2>fLowThreshold){ 
493           if(c1>fHighThreshold || c2>fHighThreshold){
494             fGoodInj[jpad][jlin]=1;
495             break;
496           }
497         }
498       }
499     }
500   }
501 }
502 //______________________________________________________________________
503 void AliITSOnlineSDDInjectors::FindCentroids(){
504   // 
505   for(Int_t jpad=0;jpad<kInjPads;jpad++){
506     Int_t ian=GetAnodeNumber(jpad);
507     for(Int_t jlin=0;jlin<kInjLines;jlin++){
508       if(!fGoodInj[jpad][jlin]) continue;
509       Double_t maxcont=0;
510       Int_t ilmax=-1;
511       for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
512         Double_t cont=fHisto->GetBinContent(jjj,ian+1);
513         if(cont>maxcont){
514           maxcont=cont;
515           ilmax=jjj;
516         }
517       }
518       Double_t intCont=0;
519       Int_t jjj=ilmax;
520       while(1){
521         Double_t cont=fHisto->GetBinContent(jjj,ian+1);
522         if(cont<fLowThreshold) break;
523         if(cont<fgkSaturation){
524           fCentroid[jpad][jlin]+=cont*(Double_t)jjj;
525           fRMSCentroid[jpad][jlin]+=cont*(Double_t)jjj*(Double_t)jjj;
526           intCont+=cont;
527         }
528         jjj--;
529       }
530       jjj=ilmax+1;
531       while(1){
532         Double_t cont=fHisto->GetBinContent(jjj,ian+1);
533         if(cont<fLowThreshold) break;
534         if(cont<fgkSaturation){
535           fCentroid[jpad][jlin]+=cont*float(jjj);
536           fRMSCentroid[jpad][jlin]+=cont*(Double_t)jjj*(Double_t)jjj;
537           intCont+=cont;
538         }
539         jjj++;
540       }
541       if(intCont>0){ 
542         fCentroid[jpad][jlin]/=intCont;
543         fRMSCentroid[jpad][jlin]=TMath::Sqrt(fRMSCentroid[jpad][jlin]/intCont-fCentroid[jpad][jlin]*fCentroid[jpad][jlin])/TMath::Sqrt(intCont);
544       }
545       else{ 
546         fCentroid[jpad][jlin]=0.;
547         fRMSCentroid[jpad][jlin]=0.;
548         fGoodInj[jpad][jlin]=0;
549       }
550       if(fRMSCentroid[jpad][jlin]==0) fGoodInj[jpad][jlin]=0;
551     }
552   }
553 }
554 //______________________________________________________________________
555 void AliITSOnlineSDDInjectors::PrintInjectorStatus(){
556   //
557   for(Int_t jpad=0;jpad<kInjPads;jpad++){
558     printf("Line%d-Anode%d: %d %d %d\n",jpad,GetAnodeNumber(jpad),fGoodInj[jpad][0],fGoodInj[jpad][1],fGoodInj[jpad][2]);
559   }
560 }
561 //______________________________________________________________________
562 void AliITSOnlineSDDInjectors::PrintCentroids(){
563   //
564   for(Int_t jpad=0;jpad<kInjPads;jpad++){
565     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]);
566   }
567 }
568 //______________________________________________________________________
569 void AliITSOnlineSDDInjectors::WriteToASCII(Int_t evNumb, UInt_t timeStamp, Int_t optAppend){
570   //
571   Char_t outfilnam[100];
572   sprintf(outfilnam,"SDDinj_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);  
573   FILE* outf;
574   if(optAppend==0){ 
575     outf=fopen(outfilnam,"w");
576     fprintf(outf,"%d\n",fActualPolDegree);
577   }
578   else outf=fopen(outfilnam,"a");
579   fprintf(outf,"%d   %d   ",evNumb,timeStamp);
580   for(Int_t ic=0;ic<fPolDegree+1;ic++){
581     fprintf(outf,"%G ",fParam[ic]);
582   }
583   fprintf(outf,"\n");
584   fclose(outf);  
585 }
586 //______________________________________________________________________
587 TH1F* AliITSOnlineSDDInjectors::GetMeanDriftSpeedVsPadHisto() const{
588   Char_t hisnam[20];
589   sprintf(hisnam,"hdrsp%02dc%02ds%d",fDDL,fCarlos,fSide);
590   TH1F* h=new TH1F(hisnam,"",kInjPads,-0.5,kInjPads-0.5);
591   if(fNEvents>0){
592     for(Int_t i=0;i<kInjPads;i++){ 
593       h->SetBinContent(i+1,GetMeanDriftSpeed(i));    
594       Double_t rms=GetRMSDriftSpeed(i);
595       Double_t err=0.;
596       if(rms>0.) err=rms/TMath::Sqrt(fNEvents);
597       h->SetBinError(i+1,err);
598     }
599   }
600   return h;
601 }
602 //______________________________________________________________________
603 Bool_t AliITSOnlineSDDInjectors::WriteToROOT(TFile *fil) const {
604   //
605   if(fil==0){ 
606     AliWarning("Invalid pointer to ROOT file");
607     return kFALSE;    
608   }  
609   Char_t hisnam[20];
610   fil->cd();
611   sprintf(hisnam,"hdrsp%02dc%02ds%d",fDDL,fCarlos,fSide);
612   TH1F hdsp(hisnam,"",kInjPads,-0.5,kInjPads-0.5);
613   if(fNEvents==0){
614     AliWarning("Zero analyzed events");
615     return kFALSE;    
616   }  
617     
618   for(Int_t i=0;i<kInjPads;i++){ 
619     hdsp.SetBinContent(i+1,GetMeanDriftSpeed(i));    
620     Double_t rms=GetRMSDriftSpeed(i);
621     Double_t err=0.;
622     if(rms>0.) err=rms/TMath::Sqrt(fNEvents);
623     hdsp.SetBinError(i+1,err);
624   }
625   hdsp.Write();
626   return kTRUE;    
627 }
628 //______________________________________________________________________
629 void AliITSOnlineSDDInjectors::WriteInjectorStatusToASCII(){
630   // dump status of injectors encoded into UInt_t
631   // 5 bits (value 0-31) to store number of pads with given status
632   Char_t outfilnam[100];
633   sprintf(outfilnam,"SDDinj_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);  
634   FILE* outf=fopen(outfilnam,"a");
635   Int_t n[8]={0,0,0,0,0,0,0,0};
636   for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
637     Int_t statusPad=GetInjPadStatus(jpad);
638     ++n[statusPad];
639   }
640   UInt_t statusInj=0;
641   statusInj+=(n[7]&0x1F)<<25; // bits 25-29: n. of pads with status 7
642   statusInj+=(n[6]&0x1F)<<20; // bits 20-24: n. of pads with status 6
643   statusInj+=(n[5]&0x1F)<<15; // bits 15-19: n. of pads with status 5
644   statusInj+=(n[4]&0x1F)<<10; // bits 10-14: n. of pads with status 4
645   statusInj+=(n[3]&0x1F)<<5;  // bits  5- 9: n. of pads with status 3
646   statusInj+=(n[2]&0x1F);     // bits  0- 4: n. of pads with status 2
647
648   fprintf(outf,"-99 %u\n",statusInj); // -99 used in preprocessor to find line
649                                       // with injector status info
650   fclose(outf);  
651   
652 }