For Pythia with tune don't switch off MI in ConfigHeavyFlavor
[u/mrichter/AliRoot.git] / ITS / AliITSOnlineSDDInjectors.cxx
index 06a5a10..36ba405 100644 (file)
  * about the suitability of this software for any purpose. It is          *
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
+#include <TFile.h>
 #include "AliITSOnlineSDDInjectors.h"
+#include "AliLog.h"
 #include <TH2F.h>
+#include <TF1.h>
 #include <TGraphErrors.h>
 #include <TMath.h>
 
@@ -34,23 +37,26 @@ const Float_t AliITSOnlineSDDInjectors::fgkDefaultHThreshold = 25.;
 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMinSpeed = 5.5;
 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxSpeed = 9.0;
 const Float_t AliITSOnlineSDDInjectors::fgkDefaultMaxErr = 1.5;
-const Int_t   AliITSOnlineSDDInjectors::fgkDefaultPolOrder = 3;
-const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMin[kInjLines] = {20,90,170};
-const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMax[kInjLines] = {50,160,240};
+const Int_t   AliITSOnlineSDDInjectors::fgkDefaultPolDegree = 3;
+const Float_t AliITSOnlineSDDInjectors::fgkDefaultTimeStep = 50.;
+const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMin[kInjLines] = {10,50,100};
+const UShort_t AliITSOnlineSDDInjectors::fgkDefaultTbMax[kInjLines] = {20,70,120};
 
 //______________________________________________________________________
-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)
+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)
 {
   // default constructor
   SetPositions();
   SetDefaults();
+  SetTimeStep(fgkDefaultTimeStep);
 }
 //______________________________________________________________________
-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)
+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)
 { 
 // standard constructor
   SetPositions();
   SetDefaults();
+  SetTimeStep(fgkDefaultTimeStep);
 }
 //______________________________________________________________________
 AliITSOnlineSDDInjectors::~AliITSOnlineSDDInjectors(){
@@ -62,10 +68,12 @@ AliITSOnlineSDDInjectors::~AliITSOnlineSDDInjectors(){
 }
 //______________________________________________________________________
 void AliITSOnlineSDDInjectors::SetDefaults(){
-  for(Int_t i=0;i<kInjLines;i++) 
+  for(Int_t i=0;i<kInjLines;i++) {
     SetInjLineRange(i,fgkDefaultTbMin[i],fgkDefaultTbMax[i]);
+    SetUseLine(i,kTRUE);
+  }
   SetThresholds(fgkDefaultLThreshold,fgkDefaultHThreshold);
-  SetPolOrder(fgkDefaultPolOrder);
+  SetPolDegree(fgkDefaultPolDegree);
   SetMinDriftSpeed(fgkDefaultMinSpeed);
   SetMaxDriftSpeed(fgkDefaultMaxSpeed);
   SetMaxDriftSpeedErr(fgkDefaultMaxErr);
@@ -75,8 +83,8 @@ void AliITSOnlineSDDInjectors::SetDefaults(){
 //______________________________________________________________________
 void AliITSOnlineSDDInjectors::SetPositions(){
   // 
-  Float_t xLinFromCenterUm[kInjLines]={31860.,17460.,660.};
-  Float_t xAnodeFromCenterUm=35085;
+  Double_t xLinFromCenterUm[kInjLines]={31860.,17460.,660.};
+  Double_t xAnodeFromCenterUm=35085;
   for(Int_t i=0;i<kInjLines;i++){
     fPosition[i]=xAnodeFromCenterUm-xLinFromCenterUm[i];
     fPosition[i]/=10000.; // from microns to cm
@@ -100,12 +108,51 @@ void AliITSOnlineSDDInjectors::Reset(){
 //______________________________________________________________________
 void AliITSOnlineSDDInjectors::AnalyzeEvent(TH2F* his){
   //
+  AddEvent(his);
+  FitDriftSpeedVsAnode();
+}
+//______________________________________________________________________
+void AliITSOnlineSDDInjectors::AddEvent(TH2F* his){
+  // Add the drift speed from current event to the average value
+  if(fNEvents==0){
+    for(Int_t i=0;i<kInjPads;i++){ 
+      fSumDriftSpeed[i]=0.;
+      fSumSqDriftSpeed[i]=0.;
+    }
+  }
   Reset();
   fHisto=his;
   FindGoodInjectors();
   FindCentroids();
   CalcTimeBinZero();
-  for(Int_t j=0;j<kInjPads;j++) CalcDriftSpeed(j);
+  for(Int_t j=0;j<kInjPads;j++){ 
+    CalcDriftSpeed(j);
+    fSumDriftSpeed[j]+=fDriftSpeed[j];
+    fSumSqDriftSpeed[j]+=fDriftSpeed[j]*fDriftSpeed[j];
+  }
+  ++fNEvents;
+}
+//______________________________________________________________________
+Double_t AliITSOnlineSDDInjectors::GetRMSDriftSpeed(Int_t ipad) const {
+  // 
+  if(fNEvents<=1) return 0.;
+  Double_t mean=fSumDriftSpeed[ipad]/(Double_t)fNEvents;
+  Double_t diff=fSumSqDriftSpeed[ipad]/(Double_t)fNEvents-mean*mean;
+  if(diff<0.) diff=0.;
+  return TMath::Sqrt(diff);
+}
+
+//______________________________________________________________________
+void AliITSOnlineSDDInjectors::FitMeanDriftSpeedVsAnode(){
+  // Calculates
+  if(fNEvents==0) return;
+  for(Int_t i=0;i<kInjPads;i++){ 
+    fDriftSpeed[i]=GetMeanDriftSpeed(i);
+    if(fNEvents>1){
+      Double_t rms=GetRMSDriftSpeed(i);
+      if(rms>0.) fDriftSpeedErr[i]=rms/TMath::Sqrt(fNEvents);
+    }
+  }
   FitDriftSpeedVsAnode();
 }
 //______________________________________________________________________
@@ -158,26 +205,93 @@ TGraphErrors* AliITSOnlineSDDInjectors::GetSelectedDriftSpeedGraph(Int_t minAcce
 }
 //______________________________________________________________________
 void AliITSOnlineSDDInjectors::CalcTimeBinZero(){
-  //
-  Float_t tzero=0.,intCont=0.;
+  // Get time zero from trigger signal
+  Double_t tzero=0.,intCont=0.,rmsPeak=0.;
+  Bool_t isTbUsed[256];
+  Int_t nTbUsed=0;
+  for(Int_t i=0;i<256;i++) isTbUsed[i]=0;
   for(Int_t ian=0;ian<fgkNAnodes;ian++){
     for(Int_t itb=1;itb<fTbMin[0];itb++){
-      Float_t cont=fHisto->GetBinContent(itb,ian+1);
+      Double_t cont=fHisto->GetBinContent(itb,ian+1);
+      Double_t contm1=fHisto->GetBinContent(itb+1,ian+1);
+      Double_t contp1=fHisto->GetBinContent(itb-1,ian+1);
       if(cont>fLowThreshold){
-       tzero+=cont*float(itb);
-       intCont+=cont;
+       if(contm1>fHighThreshold || cont>fHighThreshold || contp1>fHighThreshold){
+         tzero+=cont*float(itb);
+         rmsPeak+=cont*float(itb)*float(itb);
+         intCont+=cont;
+         if(!isTbUsed[itb]){
+           isTbUsed[itb]=1;
+           ++nTbUsed;
+         }
+       }
       }
     }
   }
-  if(intCont>0) fTbZero=tzero/intCont;
+  if(intCont>0){ 
+    fTbZero=tzero/intCont;
+    fRMSTbZero=TMath::Sqrt(rmsPeak/intCont-fTbZero*fTbZero);
+  }
+  if(nTbUsed==1) fRMSTbZero=0.5; 
 }
+
 //______________________________________________________________________
 void AliITSOnlineSDDInjectors::FitDriftSpeedVsAnode(){
+  // fits the anode dependence of drift speed
+
+  Float_t rangeForMax[2]={78.,178.};
+  PolyFit(fPolDegree);
+  fActualPolDegree=fPolDegree;
+  if(fPolDegree==3){
+    Double_t deltasq=fParam[2]*fParam[2]-3*fParam[1]*fParam[3];
+    Double_t zero1=-999.;
+    Double_t zero2=-999.;
+    if(deltasq>=0. && TMath::Abs(fParam[3])>0.){
+      Double_t delta=TMath::Sqrt(deltasq);
+      zero1=(-fParam[2]+delta)/3./fParam[3];
+      zero2=(-fParam[2]-delta)/3./fParam[3];
+    }
+    Bool_t twoZeroes=kFALSE;
+    Bool_t oneZero=kFALSE;
+    if(zero1>0. && zero1<256. && zero2>0. && zero2<256.) twoZeroes=kTRUE;
+    if(zero1>rangeForMax[0] && zero1<rangeForMax[1]) oneZero=kTRUE;
+    if(zero2>rangeForMax[0] && zero2<rangeForMax[1]) oneZero=kTRUE;
+    if(!oneZero || twoZeroes){
+      PolyFit(2);
+      Double_t xmax=-999.;
+      if(fParam[2]<0.) xmax=-fParam[1]/2./fParam[2];
+      if(xmax>rangeForMax[0] && xmax<rangeForMax[1]){
+       fActualPolDegree=2;
+      }else{
+       Double_t averSpeed=0.;
+       Double_t sumWei=0.;
+       Int_t nUsedPts=0;
+       for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
+         if(fDriftSpeed[jpad]>0 && GetInjPadStatus(jpad)>fPadStatusCutForFit){
+           Double_t wei=1./fDriftSpeedErr[jpad]/fDriftSpeedErr[jpad];
+           averSpeed+=wei*fDriftSpeed[jpad];
+           sumWei+=wei;
+           nUsedPts++;
+         }
+       }
+       if(sumWei>0.) averSpeed/=sumWei;
+       if(nUsedPts<fPolDegree+1) averSpeed=0;
+       fParam[0]=averSpeed;
+       for(Int_t i=1; i < fPolDegree+1; i++) fParam[i]=0.;
+       fActualPolDegree=0;
+      }
+    }
+  }
+}
+//______________________________________________________________________
+void AliITSOnlineSDDInjectors::PolyFit(Int_t degree){
   // fits the anode dependence of drift speed with a polynomial function
-  const Int_t kNn=fPolOrder+1;
-  Float_t **mat = new Float_t*[kNn];
-  for(Int_t i=0; i < kNn; i++) mat[i] = new Float_t[kNn];
-  Float_t *vect = new Float_t[kNn];
+  const Int_t kNn=degree+1;
+  const Int_t kDimens=fPolDegree+1;
+
+  Double_t **mat = new Double_t*[kNn];
+  for(Int_t i=0; i < kNn; i++) mat[i] = new Double_t[kNn];
+  Double_t *vect = new Double_t[kNn];
 
   for(Int_t k1=0;k1<kNn;k1++){
     vect[k1]=0;
@@ -185,73 +299,82 @@ void AliITSOnlineSDDInjectors::FitDriftSpeedVsAnode(){
       mat[k1][k2]=0;
     }
   }
+  Int_t npts = 0;
   for(Int_t k1=0;k1<kNn;k1++){
     for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
-      Float_t x=(Float_t)GetAnodeNumber(jpad);
+      Double_t x=(Double_t)GetAnodeNumber(jpad);
       if(fDriftSpeed[jpad]>0 && GetInjPadStatus(jpad)>fPadStatusCutForFit){
          vect[k1]+=fDriftSpeed[jpad]*TMath::Power(x,k1)/TMath::Power(fDriftSpeedErr[jpad],2);  
+         if(k1==0) npts++;
          for(Int_t k2=0;k2<kNn;k2++){
-
            mat[k1][k2]+=TMath::Power(x,k1+k2)/TMath::Power(fDriftSpeedErr[jpad],2);
          }
       }
     }
   }
-  Int_t *iPivot = new Int_t[kNn];
-  Int_t *indxR = new Int_t[kNn];
-  Int_t *indxC = new Int_t[kNn];
-  for(Int_t i=0;i<kNn;i++) iPivot[i]=0;
-  Int_t iCol=-1,iRow=-1;
-  for(Int_t i=0;i<kNn;i++){
-    Float_t big=0.;
-    for(Int_t j=0;j<kNn;j++){
-      if(iPivot[j]!=1){
-       for(Int_t k=0;k<kNn;k++){
-          if(iPivot[k]==0){
-            if(TMath::Abs(mat[j][k])>=big){
-              big=TMath::Abs(mat[j][k]);
-              iRow=j;
-              iCol=k;
-            }
+  if(npts<fPolDegree+1){ 
+    if(fParam) delete [] fParam;
+    fParam=new Double_t[kDimens];
+    for(Int_t i=0; i<kDimens;i++)fParam[i]=0;
+  }else{
+    Int_t *iPivot = new Int_t[kNn];
+    Int_t *indxR = new Int_t[kNn];
+    Int_t *indxC = new Int_t[kNn];
+    for(Int_t i=0;i<kNn;i++) iPivot[i]=0;
+    Int_t iCol=-1,iRow=-1;
+    for(Int_t i=0;i<kNn;i++){
+      Double_t big=0.;
+      for(Int_t j=0;j<kNn;j++){
+       if(iPivot[j]!=1){
+         for(Int_t k=0;k<kNn;k++){
+           if(iPivot[k]==0){
+             if(TMath::Abs(mat[j][k])>=big){
+               big=TMath::Abs(mat[j][k]);
+               iRow=j;
+               iCol=k;
+             }
+           }
          }
        }
       }
-    }
-    iPivot[iCol]++;
-    Float_t aux;
-    if(iRow!=iCol){
-      for(Int_t l=0;l<kNn;l++){
-       aux=mat[iRow][l];
-       mat[iRow][l]=mat[iCol][l];
-       mat[iCol][l]=aux;
+      iPivot[iCol]++;
+      Double_t aux;
+      if(iRow!=iCol){
+       for(Int_t l=0;l<kNn;l++){
+         aux=mat[iRow][l];
+         mat[iRow][l]=mat[iCol][l];
+         mat[iCol][l]=aux;
+       }
+       aux=vect[iRow];
+       vect[iRow]=vect[iCol];
+       vect[iCol]=aux;
       }
-      aux=vect[iRow];
-      vect[iRow]=vect[iCol];
-      vect[iCol]=aux;
+      indxR[i]=iRow;
+      indxC[i]=iCol;
+      if(mat[iCol][iCol]==0) break;
+      Double_t pivinv=1./mat[iCol][iCol];
+      mat[iCol][iCol]=1;
+      for(Int_t l=0;l<kNn;l++) mat[iCol][l]*=pivinv;
+      vect[iCol]*=pivinv;
+      for(Int_t m=0;m<kNn;m++){
+       if(m!=iCol){
+         aux=mat[m][iCol];
+         mat[m][iCol]=0;
+         for(Int_t n=0;n<kNn;n++) mat[m][n]-=mat[iCol][n]*aux;
+         vect[m]-=vect[iCol]*aux;
+       }
+      }    
     }
-    indxR[i]=iRow;
-    indxC[i]=iCol;
-    if(mat[iCol][iCol]==0) break;
-    Float_t pivinv=1./mat[iCol][iCol];
-    mat[iCol][iCol]=1;
-    for(Int_t l=0;l<kNn;l++) mat[iCol][l]*=pivinv;
-    vect[iCol]*=pivinv;
-    for(Int_t m=0;m<kNn;m++){
-      if(m!=iCol){
-       aux=mat[m][iCol];
-       mat[m][iCol]=0;
-       for(Int_t n=0;n<kNn;n++) mat[m][n]-=mat[iCol][n]*aux;
-       vect[m]-=vect[iCol]*aux;
-      }
-    }    
+    delete [] iPivot;
+    delete [] indxR;
+    delete [] indxC;
+    
+  
+    if(fParam) delete [] fParam;
+    fParam=new Double_t[kDimens];
+    for(Int_t i=0; i<kNn;i++)fParam[i]=vect[i];
+    if(degree<fPolDegree) for(Int_t i=kNn; i<kDimens;i++)fParam[i]=0.;
   }
-  delete [] iPivot;
-  delete [] indxR;
-  delete [] indxC;
-
-  if(fParam) delete [] fParam;
-  fParam=new Float_t[kNn];
-  for(Int_t i=0; i<kNn;i++)fParam[i]=vect[i];
 
   for(Int_t i=0; i < kNn; i++) delete [] mat[i];
   delete [] mat;
@@ -260,15 +383,16 @@ void AliITSOnlineSDDInjectors::FitDriftSpeedVsAnode(){
 //______________________________________________________________________
 void AliITSOnlineSDDInjectors::CalcDriftSpeed(Int_t jpad){
   // 
-  Float_t sumY=0,sumX=0,sumXX=0,sumYY=0.,sumXY=0,sumWEI=0.;
+  Double_t sumY=0,sumX=0,sumXX=0,sumYY=0.,sumXY=0,sumWEI=0.;
   Int_t npt=0;
-  Float_t y[kInjLines],ey[kInjLines];
-  Float_t tzero=0,erry=0;
+  Double_t y[kInjLines],ey[kInjLines];
+  Double_t tzero=0,erry=0;
   for(Int_t i=0;i<kInjLines;i++){ 
     y[i]=fCentroid[jpad][i];
     ey[i]=fRMSCentroid[jpad][i];
   }
   for(Int_t i=0;i<kInjLines;i++){
+    if(!fUseLine[i]) continue;
     if(fGoodInj[jpad][i] && ey[i]!=0){
       sumY+=y[i]/ey[i]/ey[i];
       sumX+=fPosition[i]/ey[i]/ey[i];
@@ -277,26 +401,32 @@ void AliITSOnlineSDDInjectors::CalcDriftSpeed(Int_t jpad){
       sumXY+=fPosition[i]*y[i]/ey[i]/ey[i];
       sumWEI+=1./ey[i]/ey[i];
       tzero=fTbZero/ey[i]/ey[i];
-      erry=ey[i];
+      erry=ey[i]/ey[i]/ey[i];
       npt++;
     }
   }
-  Float_t vel=0,evel=0;
+  Double_t slope=0.,eslope=0.;
+  if(npt==1){
+    slope=(sumY-tzero)/sumX;
+    eslope=erry/sumX;
+  }
   if(npt>1){ 
-    Float_t slope=(sumWEI*sumXY-sumY*sumX)/(sumWEI*sumXX-sumX*sumX);
-    Float_t eslope=TMath::Sqrt(sumWEI/(sumWEI*sumXX-sumX*sumX));
-    if(slope!=0){
-      vel=1./slope*10000./25.;// micron/ns
-      evel=eslope/slope/slope*10000./25.;// micron/ns
+    if(fUseTimeZeroSignal){
+      sumY+=fTbZero/fRMSTbZero/fRMSTbZero;
+      sumX+=0.;
+      sumXX+=0.;
+      sumYY+=fTbZero*fTbZero/fRMSTbZero/fRMSTbZero;
+      sumXY+=0.;
+      sumWEI+=1./fRMSTbZero/fRMSTbZero;
     }
+    slope=(sumWEI*sumXY-sumY*sumX)/(sumWEI*sumXX-sumX*sumX);
+    eslope=TMath::Sqrt(sumWEI/(sumWEI*sumXX-sumX*sumX));
   }
-  if(npt==1){
-    Float_t slope=(sumY-tzero)/sumX;
-    Float_t eslope=erry/sumX;
-    if(slope!=0){
-      vel=1./slope*10000./25.;// micron/ns    
-      evel=eslope/slope/slope*10000./25.;// micron/ns
-    }
+
+  Double_t vel=0,evel=0;
+  if(slope!=0. && fTimeStep>0.){
+    vel=1./slope*10000./fTimeStep;// micron/ns
+    evel=eslope/slope/slope*10000./fTimeStep;// micron/ns
   }
   if(vel>fMaxDriftSpeed||vel<fMinDriftSpeed || evel>fMaxDriftSpeedErr){ 
     vel=0.;
@@ -307,13 +437,15 @@ void AliITSOnlineSDDInjectors::CalcDriftSpeed(Int_t jpad){
 }
 //______________________________________________________________________
 Int_t AliITSOnlineSDDInjectors::GetAnodeNumber(Int_t iInjPad) const{
-  //
+  // Injectors location along anodes:
+  // Side left  (UP)   - channel 0: injectors on anodes 0,7,15,...,247,255 
+  // Side right (DOWN) - channel 1: injectors on anodes 0,8,16,...,248,255 
   Int_t ian=-1;
   if(iInjPad>=kInjPads) return ian;
-  if(!fSide){
+  if(fSide==1){  // right side
     ian=iInjPad*8;
     if(iInjPad==32) ian--;
-  }else{
+  }else{         // left side
     ian=iInjPad*8-1;
     if(iInjPad==0) ian=0;
   }
@@ -323,10 +455,10 @@ Int_t AliITSOnlineSDDInjectors::GetAnodeNumber(Int_t iInjPad) const{
 Int_t AliITSOnlineSDDInjectors::GetInjPadNumberFromAnode(Int_t nAnode) const{
   //
   Int_t iInjPad=-1;
-  if(!fSide){
+  if(fSide==1){  // right side
     if(nAnode%8==0) iInjPad=nAnode/8;
     if(nAnode==255) iInjPad=32;
-  }else{
+  }else{         // left side
     if(nAnode%8==7) iInjPad=1+nAnode/8;
     if(nAnode==0) iInjPad=0;
   }
@@ -356,10 +488,12 @@ void AliITSOnlineSDDInjectors::FindGoodInjectors(){
       for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
        Float_t c1=fHisto->GetBinContent(jjj,ian+1);
        Float_t c2=fHisto->GetBinContent(jjj+1,ian+1);
-       Float_t c3=fHisto->GetBinContent(jjj+2,ian+1);
-       if(c1>fLowThreshold && c2>fHighThreshold && c3>fLowThreshold){ 
-         fGoodInj[jpad][jlin]=1;
-         break;
+       //      Float_t c3=fHisto->GetBinContent(jjj+2,ian+1);
+       if(c1>fLowThreshold && c2>fLowThreshold){ 
+         if(c1>fHighThreshold || c2>fHighThreshold){
+           fGoodInj[jpad][jlin]=1;
+           break;
+         }
        }
       }
     }
@@ -372,41 +506,41 @@ void AliITSOnlineSDDInjectors::FindCentroids(){
     Int_t ian=GetAnodeNumber(jpad);
     for(Int_t jlin=0;jlin<kInjLines;jlin++){
       if(!fGoodInj[jpad][jlin]) continue;
-      Float_t maxcont=0;
+      Double_t maxcont=0;
       Int_t ilmax=-1;
       for(Int_t jjj=fTbMin[jlin];jjj<fTbMax[jlin];jjj++){
-       Float_t cont=fHisto->GetBinContent(jjj,ian+1);
+       Double_t cont=fHisto->GetBinContent(jjj,ian+1);
        if(cont>maxcont){
          maxcont=cont;
          ilmax=jjj;
        }
       }
-      Float_t intCont=0;
+      Double_t intCont=0;
       Int_t jjj=ilmax;
       while(1){
-       Float_t cont=fHisto->GetBinContent(jjj,ian+1);
+       Double_t cont=fHisto->GetBinContent(jjj,ian+1);
        if(cont<fLowThreshold) break;
        if(cont<fgkSaturation){
-         fCentroid[jpad][jlin]+=cont*(Float_t)jjj;
-         fRMSCentroid[jpad][jlin]+=cont*TMath::Power((Float_t)jjj,2);
+         fCentroid[jpad][jlin]+=cont*(Double_t)jjj;
+         fRMSCentroid[jpad][jlin]+=cont*(Double_t)jjj*(Double_t)jjj;
          intCont+=cont;
        }
        jjj--;
       }
       jjj=ilmax+1;
       while(1){
-       Float_t cont=fHisto->GetBinContent(jjj,ian+1);
+       Double_t cont=fHisto->GetBinContent(jjj,ian+1);
        if(cont<fLowThreshold) break;
        if(cont<fgkSaturation){
          fCentroid[jpad][jlin]+=cont*float(jjj);
-         fRMSCentroid[jpad][jlin]+=cont*TMath::Power((Float_t)jjj,2);
+         fRMSCentroid[jpad][jlin]+=cont*(Double_t)jjj*(Double_t)jjj;
          intCont+=cont;
        }
        jjj++;
       }
       if(intCont>0){ 
        fCentroid[jpad][jlin]/=intCont;
-       fRMSCentroid[jpad][jlin]=TMath::Sqrt(fRMSCentroid[jpad][jlin]/intCont-fCentroid[jpad][jlin]*fCentroid[jpad][jlin]);
+       fRMSCentroid[jpad][jlin]=TMath::Sqrt(fRMSCentroid[jpad][jlin]/intCont-fCentroid[jpad][jlin]*fCentroid[jpad][jlin])/TMath::Sqrt(intCont);
       }
       else{ 
        fCentroid[jpad][jlin]=0.;
@@ -439,13 +573,80 @@ void AliITSOnlineSDDInjectors::WriteToASCII(Int_t evNumb, UInt_t timeStamp, Int_
   FILE* outf;
   if(optAppend==0){ 
     outf=fopen(outfilnam,"w");
-    fprintf(outf,"%d\n",fPolOrder);
+    fprintf(outf,"%d\n",fActualPolDegree);
   }
   else outf=fopen(outfilnam,"a");
   fprintf(outf,"%d   %d   ",evNumb,timeStamp);
-  for(Int_t ic=0;ic<fPolOrder+1;ic++){
+  for(Int_t ic=0;ic<fPolDegree+1;ic++){
     fprintf(outf,"%G ",fParam[ic]);
   }
   fprintf(outf,"\n");
   fclose(outf);  
 }
+//______________________________________________________________________
+TH1F* AliITSOnlineSDDInjectors::GetMeanDriftSpeedVsPadHisto() const{
+  Char_t hisnam[20];
+  sprintf(hisnam,"hdrsp%02dc%02ds%d",fDDL,fCarlos,fSide);
+  TH1F* h=new TH1F(hisnam,"",kInjPads,-0.5,kInjPads-0.5);
+  if(fNEvents>0){
+    for(Int_t i=0;i<kInjPads;i++){ 
+      h->SetBinContent(i+1,GetMeanDriftSpeed(i));    
+      Double_t rms=GetRMSDriftSpeed(i);
+      Double_t err=0.;
+      if(rms>0.) err=rms/TMath::Sqrt(fNEvents);
+      h->SetBinError(i+1,err);
+    }
+  }
+  return h;
+}
+//______________________________________________________________________
+Bool_t AliITSOnlineSDDInjectors::WriteToROOT(TFile *fil) const {
+  //
+  if(fil==0){ 
+    AliWarning("Invalid pointer to ROOT file");
+    return kFALSE;    
+  }  
+  Char_t hisnam[20];
+  fil->cd();
+  sprintf(hisnam,"hdrsp%02dc%02ds%d",fDDL,fCarlos,fSide);
+  TH1F hdsp(hisnam,"",kInjPads,-0.5,kInjPads-0.5);
+  if(fNEvents==0){
+    AliWarning("Zero analyzed events");
+    return kFALSE;    
+  }  
+    
+  for(Int_t i=0;i<kInjPads;i++){ 
+    hdsp.SetBinContent(i+1,GetMeanDriftSpeed(i));    
+    Double_t rms=GetRMSDriftSpeed(i);
+    Double_t err=0.;
+    if(rms>0.) err=rms/TMath::Sqrt(fNEvents);
+    hdsp.SetBinError(i+1,err);
+  }
+  hdsp.Write();
+  return kTRUE;    
+}
+//______________________________________________________________________
+void AliITSOnlineSDDInjectors::WriteInjectorStatusToASCII(){
+  // dump status of injectors encoded into UInt_t
+  // 5 bits (value 0-31) to store number of pads with given status
+  Char_t outfilnam[100];
+  sprintf(outfilnam,"SDDinj_ddl%02dc%02d_sid%d.data",fDDL,fCarlos,fSide);  
+  FILE* outf=fopen(outfilnam,"a");
+  Int_t n[8]={0,0,0,0,0,0,0,0};
+  for(Int_t jpad=fFirstPadForFit; jpad<=fLastPadForFit; jpad++){
+    Int_t statusPad=GetInjPadStatus(jpad);
+    ++n[statusPad];
+  }
+  UInt_t statusInj=0;
+  statusInj+=(n[7]&0x1F)<<25; // bits 25-29: n. of pads with status 7
+  statusInj+=(n[6]&0x1F)<<20; // bits 20-24: n. of pads with status 6
+  statusInj+=(n[5]&0x1F)<<15; // bits 15-19: n. of pads with status 5
+  statusInj+=(n[4]&0x1F)<<10; // bits 10-14: n. of pads with status 4
+  statusInj+=(n[3]&0x1F)<<5;  // bits  5- 9: n. of pads with status 3
+  statusInj+=(n[2]&0x1F);     // bits  0- 4: n. of pads with status 2
+
+  fprintf(outf,"-99 %u\n",statusInj); // -99 used in preprocessor to find line
+                                      // with injector status info
+  fclose(outf);  
+  
+}