]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDCalibraFit.cxx
Fix coverity defect
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibraFit.cxx
index c472d7f361b499c98488c55ba877ff3767d8860c..a6e67efbbd01242f4f5711c6d2b557da17bee29b 100644 (file)
 #include <TAxis.h>
 #include <TMath.h>
 #include <TDirectory.h>
-#include <TROOT.h>
 #include <TTreeStream.h>
 #include <TLinearFitter.h>
 #include <TVectorD.h>
-#include <TArrayF.h>
+#include <TROOT.h>
+#include <TString.h>
 
 #include "AliLog.h"
 #include "AliMathBase.h"
@@ -362,7 +362,8 @@ Bool_t AliTRDCalibraFit::AnalyseCH(const TH2I *ch)
   //
 
   // Set the calibration mode
-  const char *name = ch->GetTitle();
+  //const char *name = ch->GetTitle();
+  TString name = ch->GetTitle();
   if(!SetModeCalibration(name,0)) return kFALSE;
 
   // Number of Ybins (detectors or groups of pads)
@@ -460,7 +461,8 @@ Bool_t AliTRDCalibraFit::AnalyseCH(AliTRDCalibraVector *calvect)
   //
 
   // Set the calibraMode
-  const char *name = calvect->GetNameCH();
+  //const char *name = calvect->GetNameCH();
+  TString name = calvect->GetNameCH();
   if(!SetModeCalibration(name,0)) return kFALSE;  
 
   // Number of Xbins (detectors or groups of pads)
@@ -553,26 +555,38 @@ Bool_t AliTRDCalibraFit::AnalysePH(const TProfile2D *ph)
   //
 
   // Set the calibration mode
-  const char *name = ph->GetTitle();
+  //const char *name = ph->GetTitle();
+  TString name = ph->GetTitle();
   if(!SetModeCalibration(name,1)) return kFALSE;
   
+  //printf("Mode calibration set\n");
+
   // Number of Xbins (detectors or groups of pads)
   Int_t    nbins   = ph->GetNbinsX();// time
   Int_t    nybins  = ph->GetNbinsY();// calibration group
   if (!InitFit(nybins,1)) {
     return kFALSE;
   }
+
+  //printf("Init fit\n");
+
   if (!InitFitPH()) {
     return kFALSE;
   }
+
+  //printf("Init fit PH\n");
+
   fStatisticMean        = 0.0;
   fNumberFit            = 0;
   fNumberFitSuccess     = 0;
   fNumberEnt            = 0;
   // Init fCountDet and fCount
   InitfCountDetAndfCount(1);
+  //printf("Init Count Det and fCount %d, %d\n",fDect1,fDect2);
+
   // Beginning of the loop
   for (Int_t idect = fDect1; idect < fDect2; idect++) {
+    //printf("idect = %d\n",idect);
     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi.......
     UpdatefCountDetAndfCount(idect,1);
     ReconstructFitRowMinRowMax(idect,1);
@@ -605,6 +619,7 @@ Bool_t AliTRDCalibraFit::AnalysePH(const TProfile2D *ph)
     CalculVdriftCoefMean();
     CalculT0CoefMean();
     //Method choosen
+    //printf("Method\n");
     switch(fMethod)
       {
       case 0: FitLagrangePoly((TH1 *) projph); break;
@@ -642,7 +657,8 @@ Bool_t AliTRDCalibraFit::AnalysePH(AliTRDCalibraVector *calvect)
   //
 
   // Set the calibration mode
-  const char *name = calvect->GetNamePH();
+  //const char *name = calvect->GetNamePH();
+  TString name = calvect->GetNamePH();
   if(!SetModeCalibration(name,1)) return kFALSE;
 
   // Number of Xbins (detectors or groups of pads)
@@ -721,7 +737,8 @@ Bool_t AliTRDCalibraFit::AnalysePRF(const TProfile2D *prf)
   //
 
   // Set the calibration mode
-  const char *name = prf->GetTitle();
+  //const char *name = prf->GetTitle();
+  TString name = prf->GetTitle();
   if(!SetModeCalibration(name,2)) return kFALSE;
 
   // Number of Ybins (detectors or groups of pads)
@@ -808,7 +825,8 @@ Bool_t AliTRDCalibraFit::AnalysePRFMarianFit(const TProfile2D *prf)
   //
 
   // Set the calibration mode
-  const char *name = prf->GetTitle();
+  //const char *name = prf->GetTitle();
+  TString name = prf->GetTitle();
   if(!SetModeCalibration(name,2)) return kFALSE;
 
   // Number of Ybins (detectors or groups of pads)
@@ -903,7 +921,8 @@ Bool_t AliTRDCalibraFit::AnalysePRF(AliTRDCalibraVector *calvect)
   //
 
   // Set the calibra mode
-  const char *name = calvect->GetNamePRF();
+  //const char *name = calvect->GetNamePRF();
+  TString name = calvect->GetNamePRF();
   if(!SetModeCalibration(name,2)) return kFALSE;
   //printf("test0 %s\n",name);
 
@@ -979,7 +998,8 @@ Bool_t AliTRDCalibraFit::AnalysePRFMarianFit(AliTRDCalibraVector *calvect)
   //
 
   // Set the calibra mode
-  const char *name = calvect->GetNamePRF();
+  //const char *name = calvect->GetNamePRF();
+  TString name = calvect->GetNamePRF();
   if(!SetModeCalibration(name,2)) return kFALSE;
   //printf("test0 %s\n",name);
   Int_t    nbg     = GetNumberOfGroupsPRF((const char *)name);
@@ -1132,7 +1152,7 @@ Bool_t AliTRDCalibraFit::AnalyseLinearFitters(AliTRDCalibraVdriftLinearFit *cali
     fCurrentCoef2[0] = (param[0]+fCurrentCoef[1]*fCurrentCoef2[1])/fCurrentCoef[0];
     fCurrentCoefE    = error[1];
     fCurrentCoefE2   = error[0];
-    if((fCurrentCoef2[0] != 0.0) && (param[0] != 0.0)){
+    if((TMath::Abs(fCurrentCoef2[0]) > 0.0000001) && (TMath::Abs(param[0]) > 0.0000001)){
       fCurrentCoefE2 = (fCurrentCoefE2/param[0]+fCurrentCoefE/fCurrentCoef[0])*fCurrentCoef2[0];
     }    
 
@@ -1155,7 +1175,7 @@ Bool_t AliTRDCalibraFit::AnalyseLinearFitters(AliTRDCalibraVdriftLinearFit *cali
 }
 //____________Functions for seeing if the pad is really okey___________________
 //_____________________________________________________________________________
-Int_t AliTRDCalibraFit::GetNumberOfGroupsPRF(const char* nametitle)
+Int_t AliTRDCalibraFit::GetNumberOfGroupsPRF(TString nametitle)
 {
   //
   // Get numberofgroupsprf
@@ -1171,25 +1191,25 @@ Int_t AliTRDCalibraFit::GetNumberOfGroupsPRF(const char* nametitle)
   const Char_t *pattern6 = "Ngp6";
 
   // Nrphi mode
-  if (strstr(nametitle,pattern0)) {
+  if (strstr(nametitle.Data(),pattern0)) {
     return 0;
   }
-  if (strstr(nametitle,pattern1)) {
+  if (strstr(nametitle.Data(),pattern1)) {
     return 1;
   }
-  if (strstr(nametitle,pattern2)) {
+  if (strstr(nametitle.Data(),pattern2)) {
     return 2;
   }
-  if (strstr(nametitle,pattern3)) {
+  if (strstr(nametitle.Data(),pattern3)) {
     return 3;
   }
-  if (strstr(nametitle,pattern4)) {
+  if (strstr(nametitle.Data(),pattern4)) {
     return 4;
   }
-  if (strstr(nametitle,pattern5)) {
+  if (strstr(nametitle.Data(),pattern5)) {
     return 5;
   }
-  if (strstr(nametitle,pattern6)){
+  if (strstr(nametitle.Data(),pattern6)){
     return 6;
   }
   else return -1;
@@ -1197,7 +1217,7 @@ Int_t AliTRDCalibraFit::GetNumberOfGroupsPRF(const char* nametitle)
 
 }
 //_____________________________________________________________________________
-Bool_t AliTRDCalibraFit::SetModeCalibration(const char *name, Int_t i)
+Bool_t AliTRDCalibraFit::SetModeCalibration(TString name, Int_t i)
 {
   //
   // Set fNz[i] and fNrphi[i] of the AliTRDCalibraFit::Instance()
@@ -1211,7 +1231,7 @@ Bool_t AliTRDCalibraFit::SetModeCalibration(const char *name, Int_t i)
 
 }
 //_____________________________________________________________________________
-Bool_t AliTRDCalibraFit::SetNrphiFromTObject(const char *name, Int_t i)
+Bool_t AliTRDCalibraFit::SetNrphiFromTObject(TString name, Int_t i)
 {
   //
   // Set fNrphi[i] of the AliTRDCalibraFit::Instance()
@@ -1234,7 +1254,7 @@ Bool_t AliTRDCalibraFit::SetNrphiFromTObject(const char *name, Int_t i)
   const Char_t *patternz100 = "Nz100";
 
   // Nrphi mode
-  if ((strstr(name,patternrphi100)) && (strstr(name,patternz100))) {
+  if ((strstr(name.Data(),patternrphi100)) && (strstr(name.Data(),patternz100))) {
     fCalibraMode->SetAllTogether(i);
     fNbDet = 540;
     if (fDebugLevel > 1) {
@@ -1242,7 +1262,7 @@ Bool_t AliTRDCalibraFit::SetNrphiFromTObject(const char *name, Int_t i)
     }
     return kTRUE;
   }
-  if ((strstr(name,patternrphi10)) && (strstr(name,patternz10))) {
+  if ((strstr(name.Data(),patternrphi10)) && (strstr(name.Data(),patternz10))) {
     fCalibraMode->SetPerSuperModule(i);
     fNbDet = 30;
     if (fDebugLevel > 1) {
@@ -1251,49 +1271,49 @@ Bool_t AliTRDCalibraFit::SetNrphiFromTObject(const char *name, Int_t i)
     return kTRUE;
   }
   
-  if (strstr(name,patternrphi0)) {
+  if (strstr(name.Data(),patternrphi0)) {
     fCalibraMode->SetNrphi(i ,0);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 0",fNbDet));
     }
     return kTRUE;
   }
-  if (strstr(name,patternrphi1)) {
+  if (strstr(name.Data(),patternrphi1)) {
     fCalibraMode->SetNrphi(i, 1);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 1",fNbDet));
     }
     return kTRUE;
   }
-  if (strstr(name,patternrphi2)) {
+  if (strstr(name.Data(),patternrphi2)) {
     fCalibraMode->SetNrphi(i, 2);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 2",fNbDet));
     }    
     return kTRUE;
   }
-  if (strstr(name,patternrphi3)) {
+  if (strstr(name.Data(),patternrphi3)) {
     fCalibraMode->SetNrphi(i, 3);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 3",fNbDet));
     }   
     return kTRUE;
   }
-  if (strstr(name,patternrphi4)) {
+  if (strstr(name.Data(),patternrphi4)) {
     fCalibraMode->SetNrphi(i, 4);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 4",fNbDet));
     }   
     return kTRUE;
   }
-  if (strstr(name,patternrphi5)) {
+  if (strstr(name.Data(),patternrphi5)) {
     fCalibraMode->SetNrphi(i, 5);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 5",fNbDet));
     }
     return kTRUE;
   }
-  if (strstr(name,patternrphi6)) {
+  if (strstr(name.Data(),patternrphi6)) {
     fCalibraMode->SetNrphi(i, 6);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 6",fNbDet));
@@ -1309,7 +1329,7 @@ Bool_t AliTRDCalibraFit::SetNrphiFromTObject(const char *name, Int_t i)
   
 }
 //_____________________________________________________________________________
-Bool_t AliTRDCalibraFit::SetNzFromTObject(const char *name, Int_t i)
+Bool_t AliTRDCalibraFit::SetNzFromTObject(TString name, Int_t i)
 {
   //
   // Set fNz[i] of the AliTRDCalibraFit::Instance()
@@ -1328,7 +1348,7 @@ Bool_t AliTRDCalibraFit::SetNzFromTObject(const char *name, Int_t i)
   const Char_t *patternz10 = "Nz10";
   const Char_t *patternz100 = "Nz100";
 
-  if ((strstr(name,patternrphi100)) && (strstr(name,patternz100))) {
+  if ((strstr(name.Data(),patternrphi100)) && (strstr(name.Data(),patternz100))) {
     fCalibraMode->SetAllTogether(i);
     fNbDet = 540;
     if (fDebugLevel > 1) {
@@ -1336,7 +1356,7 @@ Bool_t AliTRDCalibraFit::SetNzFromTObject(const char *name, Int_t i)
     }
     return kTRUE;
   }
-  if ((strstr(name,patternrphi10)) && (strstr(name,patternz10))) {
+  if ((strstr(name.Data(),patternrphi10)) && (strstr(name.Data(),patternz10))) {
     fCalibraMode->SetPerSuperModule(i);
     fNbDet = 30;
     if (fDebugLevel > 1) {
@@ -1344,35 +1364,35 @@ Bool_t AliTRDCalibraFit::SetNzFromTObject(const char *name, Int_t i)
     }
     return kTRUE;
   }
-  if (strstr(name,patternz0)) {
+  if (strstr(name.Data(),patternz0)) {
     fCalibraMode->SetNz(i, 0);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 0",fNbDet));
     }
     return kTRUE;
   }
-  if (strstr(name,patternz1)) {
+  if (strstr(name.Data(),patternz1)) {
     fCalibraMode->SetNz(i ,1);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 1",fNbDet));
     }
     return kTRUE;
   }
-  if (strstr(name,patternz2)) {
+  if (strstr(name.Data(),patternz2)) {
     fCalibraMode->SetNz(i ,2);
     if (fDebugLevel > 1) {    
       AliInfo(Form("fNbDet %d and 2",fNbDet));
     }
     return kTRUE;
   }
-  if (strstr(name,patternz3)) {
+  if (strstr(name.Data(),patternz3)) {
     fCalibraMode->SetNz(i ,3);
     if (fDebugLevel > 1) {
       AliInfo(Form("fNbDet %d and 3",fNbDet));
     }
     return kTRUE;  
   }
-  if (strstr(name,patternz4)) {
+  if (strstr(name.Data(),patternz4)) {
     fCalibraMode->SetNz(i ,4);
     if (fDebugLevel > 1) {    
       AliInfo(Form("fNbDet %d and 4",fNbDet));
@@ -1387,6 +1407,166 @@ Bool_t AliTRDCalibraFit::SetNzFromTObject(const char *name, Int_t i)
   return kFALSE;
 }
 //______________________________________________________________________
+void AliTRDCalibraFit::RemoveOutliers(Int_t type, Bool_t perdetector){
+  //
+  // Remove the results too far from the mean value and rms
+  // type: 0 gain, 1 vdrift
+  // perdetector
+  //
+
+  Int_t loop = (Int_t) fVectorFit.GetEntriesFast();
+  if(loop != 540) {
+    AliInfo("The Vector Fit is not complete!");
+    return;
+  }
+  Int_t detector = -1;
+  Int_t sector = -1;
+  Float_t value  = 0.0;
+
+  /////////////////////////////////
+  // Calculate the mean values
+  ////////////////////////////////
+  // Initialisation
+  ////////////////////////
+  Double_t meanAll = 0.0;
+   Double_t rmsAll = 0.0;
+   Int_t countAll = 0;
+   ////////////
+  // compute
+  ////////////
+  for (Int_t k = 0; k < loop; k++) {
+    detector  = ((AliTRDFitInfo *) fVectorFit.At(k))->GetDetector();
+    sector = GetSector(detector);
+    if(perdetector){
+      value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[0];
+      if(value > 0.0) {
+       rmsAll += value*value;
+       meanAll += value;
+       countAll++;
+      }
+    }
+    else {
+      Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
+      Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
+      for (Int_t row = 0; row < rowMax; row++) {
+       for (Int_t col = 0; col < colMax; col++) {
+         value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
+         if(value > 0.0) {
+           rmsAll += value*value;
+           meanAll += value;
+           countAll++;
+         }
+         
+       } // Col
+      } // Row
+    }
+  }  
+  if(countAll > 0) {
+    meanAll = meanAll/countAll;
+    rmsAll = TMath::Sqrt(TMath::Abs(rmsAll/countAll - (meanAll*meanAll)));
+  }
+  //printf("RemoveOutliers: meanAll %f and rmsAll %f\n",meanAll,rmsAll);
+  /////////////////////////////////////////////////
+  // Remove outliers
+  ////////////////////////////////////////////////
+  Double_t defaultvalue = -1.0;
+  if(type==1) defaultvalue = -1.5;
+  for (Int_t k = 0; k < loop; k++) {
+    detector  = ((AliTRDFitInfo *) fVectorFit.At(k))->GetDetector();
+    sector = GetSector(detector);
+    Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
+    Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
+    Float_t *coef = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef();
+  
+    // remove the results too far away  
+    for (Int_t row = 0; row < rowMax; row++) {
+      for (Int_t col = 0; col < colMax; col++) {
+       value = coef[(Int_t)(col*rowMax+row)];
+       if((value > 0.0) && (rmsAll > 0.0) && (TMath::Abs(value-meanAll) > (2*rmsAll))) {
+         coef[(Int_t)(col*rowMax+row)] = defaultvalue;
+       }
+      } // Col
+    } // Row
+  }
+}
+//______________________________________________________________________
+void AliTRDCalibraFit::RemoveOutliers2(Bool_t perdetector){
+  //
+  // Remove the results too far from the mean and rms
+  // perdetector
+  //
+
+  Int_t loop = (Int_t) fVectorFit2.GetEntriesFast();
+  if(loop != 540) {
+    AliInfo("The Vector Fit is not complete!");
+    return;
+  }
+  Int_t detector = -1;
+  Int_t sector = -1;
+  Float_t value  = 0.0;
+
+  /////////////////////////////////
+  // Calculate the mean values
+  ////////////////////////////////
+  // Initialisation
+  ////////////////////////
+  Double_t meanAll = 0.0;
+  Double_t rmsAll = 0.0;
+  Int_t countAll = 0;
+  /////////////
+  // compute
+  ////////////
+  for (Int_t k = 0; k < loop; k++) {
+    detector  = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetDetector();
+    sector = GetSector(detector);
+    if(perdetector){
+      value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[0];
+      if(value < 70.0) {
+       meanAll += value;
+       rmsAll += value*value;
+       countAll++;
+      }
+    }
+    else {
+      Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
+      Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
+      for (Int_t row = 0; row < rowMax; row++) {
+       for (Int_t col = 0; col < colMax; col++) {
+         value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
+         if(value < 70.0) {
+           rmsAll += value*value;
+           meanAll += value;
+           countAll++;
+         }       
+       } // Col
+      } // Row
+    }
+  }  
+  if(countAll > 0) {
+    meanAll = meanAll/countAll;
+    rmsAll = TMath::Sqrt(TMath::Abs(rmsAll/countAll - (meanAll*meanAll)));
+  }
+  //printf("Remove outliers 2: meanAll %f, rmsAll %f\n",meanAll,rmsAll);
+  /////////////////////////////////////////////////
+  // Remove outliers
+  ////////////////////////////////////////////////
+  for (Int_t k = 0; k < loop; k++) {
+    detector  = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetDetector();
+    sector = GetSector(detector);
+    Int_t rowMax    = fGeo->GetRowMax(GetLayer(detector),GetStack(detector),GetSector(detector));
+    Int_t colMax    = fGeo->GetColMax(GetLayer(detector));
+    Float_t *coef = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef();
+  
+    // remove the results too far away  
+    for (Int_t row = 0; row < rowMax; row++) {
+      for (Int_t col = 0; col < colMax; col++) {
+       value = coef[(Int_t)(col*rowMax+row)];
+       if((value < 70.0) && (rmsAll > 0.0) && (TMath::Abs(value-meanAll) > (2.5*rmsAll))) coef[(Int_t)(col*rowMax+row)] = 100.0;
+      } // Col
+    } // Row
+  }
+}
+//______________________________________________________________________
 void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetector){
   //
   // ofwhat is equaled to 0: mean value of all passing detectors
@@ -1410,17 +1590,23 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetect
   Double_t meanAll = 0.0;
   Double_t meanSupermodule[18];
   Double_t meanDetector[540];
+  Double_t rmsAll = 0.0;
+  Double_t rmsSupermodule[18];
+  Double_t rmsDetector[540];
   Int_t countAll = 0;
   Int_t countSupermodule[18];
   Int_t countDetector[540];
   for(Int_t sm = 0; sm < 18; sm++){
+    rmsSupermodule[sm] = 0.0;
     meanSupermodule[sm] = 0.0;
     countSupermodule[sm] = 0;
   }
   for(Int_t det = 0; det < 540; det++){
+    rmsDetector[det] = 0.0;
     meanDetector[det] = 0.0;
     countDetector[det] = 0;
   }
+  ////////////
   // compute
   ////////////
   for (Int_t k = 0; k < loop; k++) {
@@ -1429,10 +1615,13 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetect
     if(perdetector){
       value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[0];
       if(value > 0.0) {
+       rmsDetector[detector] += value*value;
        meanDetector[detector] += value;
        countDetector[detector]++;
+       rmsSupermodule[sector] += value*value;
        meanSupermodule[sector] += value;
        countSupermodule[sector]++;
+       rmsAll += value*value;
        meanAll += value;
        countAll++;
       }
@@ -1444,10 +1633,13 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetect
        for (Int_t col = 0; col < colMax; col++) {
          value = ((AliTRDFitInfo *) fVectorFit.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
          if(value > 0.0) {
+           rmsDetector[detector] += value*value;
            meanDetector[detector] += value;
            countDetector[detector]++;
+           rmsSupermodule[sector] += value*value;
            meanSupermodule[sector] += value;
            countSupermodule[sector]++;
+           rmsAll += value*value;
            meanAll += value;
            countAll++;
          }
@@ -1456,13 +1648,24 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetect
       } // Row
     }
   }  
-  if(countAll > 0) meanAll = meanAll/countAll;
+  if(countAll > 0) {
+    meanAll = meanAll/countAll;
+    rmsAll = TMath::Abs(rmsAll/countAll - (meanAll*meanAll));
+  }
   for(Int_t sm = 0; sm < 18; sm++){
-    if(countSupermodule[sm] > 0) meanSupermodule[sm] = meanSupermodule[sm]/countSupermodule[sm];
+    if(countSupermodule[sm] > 0) {
+      meanSupermodule[sm] = meanSupermodule[sm]/countSupermodule[sm];
+      rmsSupermodule[sm] = TMath::Abs(rmsSupermodule[sm]/countSupermodule[sm] - (meanSupermodule[sm]*meanSupermodule[sm]));
+    }
   }
   for(Int_t det = 0; det < 540; det++){
-    if(countDetector[det] > 0) meanDetector[det] = meanDetector[det]/countDetector[det];
+    if(countDetector[det] > 0) {
+      meanDetector[det] = meanDetector[det]/countDetector[det];
+      rmsDetector[det] = TMath::Abs(rmsDetector[det]/countDetector[det] - (meanDetector[det]*meanDetector[det]));
+    }
   }
+  //printf("Put mean value, meanAll %f, rmsAll %f\n",meanAll,rmsAll);
+  ///////////////////////////////////////////////
   // Put the mean value for the no-fitted
   /////////////////////////////////////////////  
   for (Int_t k = 0; k < loop; k++) {
@@ -1476,11 +1679,11 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetect
       for (Int_t col = 0; col < colMax; col++) {
        value = coef[(Int_t)(col*rowMax+row)];
        if(value < 0.0) {
-         if((ofwhat == 0) && (meanAll > 0.0)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanAll);
+         if((ofwhat == 0) && (meanAll > 0.0) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanAll);
          if(ofwhat == 1){
-           if(meanDetector[detector] > 0.0) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanDetector[detector]);
-           else if(meanSupermodule[sector] > 0.0) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanSupermodule[sector]);
-           else if(meanAll > 0.0) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanAll);
+           if((meanDetector[detector] > 0.0) && (countDetector[detector] > 20)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanDetector[detector]);
+           else if((meanSupermodule[sector] > 0.0) && (countSupermodule[sector] > 15)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanSupermodule[sector]);
+           else if((meanAll > 0.0) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = -TMath::Abs(meanAll);
          }  
        }
        // Debug
@@ -1507,7 +1710,6 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit(Int_t ofwhat, Bool_t perdetect
       } // Col
     } // Row
   }
-  
 }
 //______________________________________________________________________
 void AliTRDCalibraFit::PutMeanValueOtherVectorFit2(Int_t ofwhat, Bool_t perdetector){
@@ -1531,16 +1733,21 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit2(Int_t ofwhat, Bool_t perdetec
   // Initialisation
   ////////////////////////
   Double_t meanAll = 0.0;
+  Double_t rmsAll = 0.0;
   Double_t meanSupermodule[18];
+  Double_t rmsSupermodule[18];
   Double_t meanDetector[540];
+  Double_t rmsDetector[540];
   Int_t countAll = 0;
   Int_t countSupermodule[18];
   Int_t countDetector[540];
   for(Int_t sm = 0; sm < 18; sm++){
+    rmsSupermodule[sm] = 0.0;
     meanSupermodule[sm] = 0.0;
     countSupermodule[sm] = 0;
   }
   for(Int_t det = 0; det < 540; det++){
+    rmsDetector[det] = 0.0;
     meanDetector[det] = 0.0;
     countDetector[det] = 0;
   }
@@ -1552,11 +1759,14 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit2(Int_t ofwhat, Bool_t perdetec
     if(perdetector){
       value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[0];
       if(value < 70.0) {
+       rmsDetector[detector] += value*value;
        meanDetector[detector] += value;
        countDetector[detector]++;
+       rmsSupermodule[sector] += value*value;
        meanSupermodule[sector] += value;
        countSupermodule[sector]++;
        meanAll += value;
+       rmsAll += value*value;
        countAll++;
       }
     }
@@ -1567,10 +1777,13 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit2(Int_t ofwhat, Bool_t perdetec
        for (Int_t col = 0; col < colMax; col++) {
          value = ((AliTRDFitInfo *) fVectorFit2.At(k))->GetCoef()[(Int_t)(col*rowMax+row)];
          if(value < 70.0) {
+           rmsDetector[detector] += value*value;
            meanDetector[detector] += value;
            countDetector[detector]++;
+           rmsSupermodule[sector] += value*value;
            meanSupermodule[sector] += value;
            countSupermodule[sector]++;
+           rmsAll += value*value;
            meanAll += value;
            countAll++;
          }
@@ -1579,13 +1792,24 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit2(Int_t ofwhat, Bool_t perdetec
       } // Row
     }
   }  
-  if(countAll > 0) meanAll = meanAll/countAll;
+  if(countAll > 0) {
+    meanAll = meanAll/countAll;
+    rmsAll = TMath::Abs(rmsAll/countAll - (meanAll*meanAll));
+  }
   for(Int_t sm = 0; sm < 18; sm++){
-    if(countSupermodule[sm] > 0) meanSupermodule[sm] = meanSupermodule[sm]/countSupermodule[sm];
+    if(countSupermodule[sm] > 0) {
+      meanSupermodule[sm] = meanSupermodule[sm]/countSupermodule[sm];
+      rmsSupermodule[sm] = TMath::Abs(rmsSupermodule[sm]/countSupermodule[sm] - (meanSupermodule[sm]*meanSupermodule[sm]));
+    }
   }
   for(Int_t det = 0; det < 540; det++){
-    if(countDetector[det] > 0) meanDetector[det] = meanDetector[det]/countDetector[det];
+    if(countDetector[det] > 0) {
+      meanDetector[det] = meanDetector[det]/countDetector[det];
+      rmsDetector[det] = TMath::Abs(rmsDetector[det]/countDetector[det] - (meanDetector[det]*meanDetector[det]));
+    }
   }
+  //printf("Put mean value 2: meanAll %f, rmsAll %f\n",meanAll,rmsAll);
+  ////////////////////////////////////////////
   // Put the mean value for the no-fitted
   /////////////////////////////////////////////  
   for (Int_t k = 0; k < loop; k++) {
@@ -1599,11 +1823,11 @@ void AliTRDCalibraFit::PutMeanValueOtherVectorFit2(Int_t ofwhat, Bool_t perdetec
       for (Int_t col = 0; col < colMax; col++) {
        value = coef[(Int_t)(col*rowMax+row)];
        if(value > 70.0) {
-         if((ofwhat == 0) && (meanAll > 0.0)) coef[(Int_t)(col*rowMax+row)] = meanAll+100.0;
+         if((ofwhat == 0) && (meanAll > -1.5) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = meanAll+100.0;
          if(ofwhat == 1){
-           if(meanDetector[detector] > 0.0) coef[(Int_t)(col*rowMax+row)] = meanDetector[detector]+100.0;
-           else if(meanSupermodule[sector] > 0.0) coef[(Int_t)(col*rowMax+row)] = meanSupermodule[sector]+100.0;
-           else if(meanAll > 0.0) coef[(Int_t)(col*rowMax+row)] = meanAll+100.0;
+           if((meanDetector[detector] > -1.5) && (countDetector[detector] > 20)) coef[(Int_t)(col*rowMax+row)] = meanDetector[detector]+100.0;
+           else if((meanSupermodule[sector] > -1.5) && (countSupermodule[sector] > 15)) coef[(Int_t)(col*rowMax+row)] = meanSupermodule[sector]+100.0;
+           else if((meanAll > -1.5) && (countAll > 15)) coef[(Int_t)(col*rowMax+row)] = meanAll+100.0;
          }  
        }
        // Debug
@@ -1844,7 +2068,7 @@ TObject *AliTRDCalibraFit::CreatePadObjectGain(const TObjArray *vectorFit, Doubl
       detector  = ((AliTRDFitInfo *) vectorFit->At(k))->GetDetector();
       AliTRDCalROC *calROC = object->GetCalROC(detector);
       Float_t mean         = detobject->GetValue(detector);
-      if(mean == 0) continue;
+      if(TMath::Abs(mean) <= 0.0000000001) continue;
       Int_t rowMax    = calROC->GetNrows();
       Int_t colMax    = calROC->GetNcols();
       for (Int_t row = 0; row < rowMax; row++) {
@@ -2488,10 +2712,16 @@ void AliTRDCalibraFit::InitfCountDetAndfCount(Int_t i)
   if(fDebugLevel == 1) {
     fCountDet = 0;
     fCalibraMode->CalculXBins(fCountDet,i);
-    while(fCalibraMode->GetXbins(i) <=fFitVoir){
+    if((fCalibraMode->GetNz(i)!=100) && (fCalibraMode->GetNrphi(i)!=100)){
+      while(fCalibraMode->GetXbins(i) <=fFitVoir){
+       fCountDet++;
+       fCalibraMode->CalculXBins(fCountDet,i);
+       //printf("GetXBins %d\n",fCalibraMode->GetXbins(i));
+      }      
+    }
+    else {
       fCountDet++;
-      fCalibraMode->CalculXBins(fCountDet,i);
-    }      
+    }
     fCount    = fCalibraMode->GetXbins(i);
     fCountDet--;
     // Determination of fNnZ, fNnRphi, fNfragZ and fNfragRphi
@@ -3944,11 +4174,11 @@ void AliTRDCalibraFit::FitPente(TH1* projPH)
   Float_t l3P2am = pentea->GetFunction("pol2")->GetParameter(2);
   Float_t l3P1amE = pentea->GetFunction("pol2")->GetParError(1);
   Float_t l3P2amE = pentea->GetFunction("pol2")->GetParError(2);
-  if (l3P2am != 0) {
+  if (TMath::Abs(l3P2am) > 0.00000001) {
     fPhd[0] = -(l3P1am / (2 * l3P2am));
   }
   if(!fTakeTheMaxPH){
-    if((l3P1am != 0.0) && (l3P2am != 0.0)){
+    if((TMath::Abs(l3P1am) > 0.0000000001) && (TMath::Abs(l3P2am) > 0.00000000001)){
       fCurrentCoefE2 = (l3P1amE/l3P1am + l3P2amE/l3P2am)*fPhd[0];
     }
   }
@@ -3975,10 +4205,10 @@ void AliTRDCalibraFit::FitPente(TH1* projPH)
   Float_t l3P2amf = projPH->GetFunction("pol2")->GetParameter(2);
   Float_t l3P1amfE = projPH->GetFunction("pol2")->GetParError(1);
   Float_t l3P2amfE = projPH->GetFunction("pol2")->GetParError(2);
-  if (l3P2amf != 0) {
+  if (TMath::Abs(l3P2amf) > 0.00000000001) {
     fPhd[1] = -(l3P1amf / (2 * l3P2amf));
   }
-  if((l3P1amf != 0.0) && (l3P2amf != 0.0)){
+  if((TMath::Abs(l3P1amf) > 0.0000000001) && (TMath::Abs(l3P2amf) > 0.000000001)){
     fCurrentCoefE = (l3P1amfE/l3P1amf + l3P2amfE/l3P2amf)*fPhd[1];
   }
   if(fTakeTheMaxPH){
@@ -4009,10 +4239,10 @@ void AliTRDCalibraFit::FitPente(TH1* projPH)
   Float_t l3P2dr = pente->GetFunction("pol2")->GetParameter(2);
   Float_t l3P1drE = pente->GetFunction("pol2")->GetParError(1);
   Float_t l3P2drE = pente->GetFunction("pol2")->GetParError(2);
-  if (l3P2dr != 0) {
+  if (TMath::Abs(l3P2dr) > 0.00000001) {
     fPhd[2] = -(l3P1dr / (2 * l3P2dr));
   }
-  if((l3P1dr != 0.0) && (l3P2dr != 0.0)){
+  if((TMath::Abs(l3P1dr) > 0.0000000001) && (TMath::Abs(l3P2dr) > 0.00000000001)){
     fCurrentCoefE += (l3P1drE/l3P1dr + l3P2drE/l3P2dr)*fPhd[2]; 
   }
   Float_t fPhdt0  = 0.0;
@@ -4079,7 +4309,7 @@ void AliTRDCalibraFit::FitLagrangePoly(TH1* projPH)
   //
   // Slope methode but with polynomes de Lagrange
   //
-  
+
   // Constants
   const Float_t kDrWidth = AliTRDgeometry::DrThick();
   Int_t binmax      = 0;
@@ -4379,7 +4609,7 @@ void AliTRDCalibraFit::FitLagrangePoly(TH1* projPH)
     AliInfo("Too many fluctuations at the end!");
     put = kFALSE;
   }
-  if(pente->GetBinContent(binmin+1)==0){
+  if(TMath::Abs(pente->GetBinContent(binmin+1)) <= 0.0000000000001){
     AliInfo("No entries for the next bin!");
     pente->SetBinContent(binmin,0);
     if(pente->GetEntries() > 0) binmin = (Int_t) pente->GetMinimumBin();
@@ -4600,7 +4830,8 @@ void AliTRDCalibraFit::FitLagrangePoly(TH1* projPH)
       (fPhd[1] > fPhd[0]) &&
       (put)) {
     fCurrentCoef[0] = (kDrWidth) / (fPhd[2]-fPhd[1]);
-    fNumberFitSuccess++;
+    if(fCurrentCoef[0] > 2.5) fCurrentCoef[0] =  -TMath::Abs(fCurrentCoef[1]);
+    else fNumberFitSuccess++;
     if (fPhdt0 >= 0.0) {
       fCurrentCoef2[0] = (fPhdt0 - t0Shift) / widbins;
       if (fCurrentCoef2[0] < -1.0) {
@@ -4612,6 +4843,9 @@ void AliTRDCalibraFit::FitLagrangePoly(TH1* projPH)
     }
   }
   else {
+    //printf("Put default %f\n",-TMath::Abs(fCurrentCoef[1]));
+    fCurrentCoef[0]      = -TMath::Abs(fCurrentCoef[1]);
+    
     if((fPhd[1] > fPhd[0]) &&
        (put)) {
       if (fPhdt0 >= 0.0) {
@@ -4625,7 +4859,6 @@ void AliTRDCalibraFit::FitLagrangePoly(TH1* projPH)
       }
     }
     else{
-      fCurrentCoef[0]      = -TMath::Abs(fCurrentCoef[1]);
       fCurrentCoef2[0]     = fCurrentCoef2[1] + 100.0;
       //printf("Fit failed!\n");
     }
@@ -4772,7 +5005,7 @@ Bool_t AliTRDCalibraFit::FitPRFGausMI(Double_t *arraye, Double_t *arraym, Double
 
   Double_t ret = FitGausMI(arraye, arraym, arrayme, nBins, xMin, xMax,&param); 
 
-  if(ret == -4){
+  if(TMath::Abs(ret+4) <= 0.000000001){
     fCurrentCoef[0] = -fCurrentCoef[1];
     return kFALSE;
   }
@@ -4835,7 +5068,7 @@ Double_t AliTRDCalibraFit::FitGausMI(Double_t *arraye, Double_t *arraym, Double_
          if((valueI - arrayme[ibin]) > 0.0) errorn = TMath::Log((valueI - arrayme[ibin])/valueI);
          error = TMath::Max(TMath::Abs(errorm),TMath::Abs(errorn));
        }
-       if(error == 0.0) continue;
+       if(TMath::Abs(error) < 0.000000001) continue;
        val      = TMath::Log(Float_t(valueI));
        fitter.AddPoint(&xcenter,val,error);
        npoints++;
@@ -4882,7 +5115,7 @@ Double_t AliTRDCalibraFit::FitGausMI(Double_t *arraye, Double_t *arraym, Double_
     
         
     if (!param)  param  = new TVectorD(3);
-    if(par[2] == 0.0) return -4.0;
+    if(TMath::Abs(par[2]) <= 0.000000001) return -4.0;
     Double_t  x      = TMath::Sqrt(TMath::Abs(-2*par[2])); 
     Double_t deltax = (fitter.GetParError(2))/x;
     Double_t errorparam2 = TMath::Abs(deltax)/(x*x);
@@ -5082,7 +5315,7 @@ void AliTRDCalibraFit::FitTnpRange(Double_t *arraye, Double_t *arraym, Double_t
     Double_t ermin0       = 0.0;
     //Double_t prfe0      = 0.0;
     Double_t prf0         = 0.0;
-    if((pars0[2] > 0.0) && (pars0[1] != 0.0)) {
+    if((pars0[2] > 0.000000000001) && (TMath::Abs(pars0[1]) >= 0.000000000001)) {
       min0 = -pars0[1]/(2*pars0[2]);
       ermin0 = TMath::Abs(min0*(errorsx0/pars0[2]+linearfitter.GetParError(1)*pointError0/pars0[1]));
       prf0 = pars0[0]+pars0[1]*min0+pars0[2]*min0*min0;
@@ -5746,9 +5979,9 @@ Double_t AliTRDCalibraFit::AsymmGauss(const Double_t *x, const Double_t *par)
   Double_t  sigma2  = par2save*par2save;
   Double_t  sqrt2   = TMath::Sqrt(2.0);
   Double_t  exp1    = par3save * TMath::Exp(-par3save * (dx - 0.5 * par3save * sigma2))
-                               * (1.0 - TMath::Erf((par3save * sigma2 - dx) / (sqrt2 * par2save)));
+                               * (1.0 - AliMathBase::ErfFast((par3save * sigma2 - dx) / (sqrt2 * par2save)));
   Double_t  exp2    = par5save * TMath::Exp(-par5save * (dx - 0.5 * par5save * sigma2))
-                               * (1.0 - TMath::Erf((par5save * sigma2 - dx) / (sqrt2 * par2save)));
+                               * (1.0 - AliMathBase::ErfFast((par5save * sigma2 - dx) / (sqrt2 * par2save)));
 
   //return par[0]*(exp1+par[4]*exp2);
   return par[0] * (exp1 + 1.00124 * exp2);
@@ -5979,3 +6212,4 @@ Double_t AliTRDCalibraFit::GausConstant(const Double_t *x, const Double_t *par)
   return gauss;
 
 }
+