gcc 4.3 warnings fixed
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibraFillHisto.cxx
index d2dbc51..8c52989 100644 (file)
@@ -62,8 +62,9 @@
 #include "AliTRDtrigParam.h"
 #include "AliTRDpadPlane.h"
 #include "AliTRDcluster.h"
+#include "AliTRDtrack.h"
 #include "AliTRDtrackV1.h"
-#include "AliTRDrawStreamTB.h"
+#include "AliTRDrawStreamBase.h"
 #include "AliRawReader.h"
 #include "AliRawReaderDate.h"
 #include "AliTRDgeometry.h"
@@ -130,13 +131,15 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   ,fLinearFitterDebugOn(kFALSE)
   ,fRelativeScale(0)
   ,fThresholdClusterPRF2(15.0)
+  ,fLimitChargeIntegration(kFALSE)
+  ,fFillWithZero(kFALSE)
   ,fCalibraMode(new AliTRDCalibraMode())
   ,fDebugStreamer(0)
   ,fDebugLevel(0)
   ,fDetectorPreviousTrack(-1)
   ,fMCMPrevious(-1)
   ,fROBPrevious(-1)
-  ,fNumberClusters(18)
+  ,fNumberClusters(0)
   ,fNumberClustersf(30)
   ,fProcent(6.0)
   ,fDifference(17)
@@ -150,6 +153,7 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   ,fPHPlace(0x0)
   ,fPHValue(0x0)
   ,fGoodTracklet(kTRUE)
+  ,fLinearFitterTracklet(0x0)
   ,fEntriesCH(0x0)
   ,fEntriesLinearFitter(0x0)
   ,fCalibraVector(0x0)
@@ -192,6 +196,8 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fLinearFitterDebugOn(c.fLinearFitterDebugOn)
   ,fRelativeScale(c.fRelativeScale)
   ,fThresholdClusterPRF2(c.fThresholdClusterPRF2)
+  ,fLimitChargeIntegration(c.fLimitChargeIntegration)
+  ,fFillWithZero(c.fFillWithZero)
   ,fCalibraMode(0x0)
   ,fDebugStreamer(0)
   ,fDebugLevel(c.fDebugLevel)
@@ -212,6 +218,7 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fPHPlace(0x0)
   ,fPHValue(0x0)
   ,fGoodTracklet(c.fGoodTracklet)
+  ,fLinearFitterTracklet(0x0)
   ,fEntriesCH(0x0)
   ,fEntriesLinearFitter(0x0)
   ,fCalibraVector(0x0)
@@ -266,6 +273,18 @@ AliTRDCalibraFillHisto::~AliTRDCalibraFillHisto()
   if ( fCalDetGain )  delete fCalDetGain;
   if ( fCalROCGain )  delete fCalROCGain;
 
+  if( fLinearFitterTracklet ) { delete fLinearFitterTracklet; }
+  
+  delete [] fPHPlace;
+  delete [] fPHValue;
+  delete [] fEntriesCH;
+  delete [] fEntriesLinearFitter;
+  delete [] fAmpTotal;
+  
+  for(Int_t idet=0; idet<AliTRDgeometry::kNdet; idet++){ 
+    TLinearFitter *f = (TLinearFitter*)fLinearFitterArray.At(idet);
+    if(f) { delete f;}
+  }
   if (fGeo) {
     delete fGeo;
   }
@@ -338,14 +357,24 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
   }
 
   // Some parameters
-  fTimeMax = cal->GetNumberOfTimeBins();
-  fSf      = parCom->GetSamplingFrequency();
-  fRelativeScale = 20;
+  fTimeMax            = cal->GetNumberOfTimeBins();
+  fSf                 = parCom->GetSamplingFrequency();
+  fRelativeScale      = 20;
+  fNumberClustersf    = fTimeMax;
+  fNumberClusters     = (Int_t)(0.5*fTimeMax);
  
-  //calib object from database used for reconstruction
-  if(fCalDetGain) delete fCalDetGain;
-  fCalDetGain  = new AliTRDCalDet(*(cal->GetGainFactorDet()));
+  // Init linear fitter
+  if(!fLinearFitterTracklet) {
+    fLinearFitterTracklet = new TLinearFitter(2,"pol1");
+    fLinearFitterTracklet->StoreData(kFALSE);
+  }
 
+  //calib object from database used for reconstruction
+  if( fCalDetGain ){ 
+    fCalDetGain->~AliTRDCalDet();
+    new(fCalDetGain) AliTRDCalDet(*(cal->GetGainFactorDet()));
+  }else fCalDetGain = new AliTRDCalDet(*(cal->GetGainFactorDet()));
+  
   // Calcul Xbins Chambd0, Chamb2
   Int_t ntotal0 = CalculateTotalNumberOfBins(0);
   Int_t ntotal1 = CalculateTotalNumberOfBins(1);
@@ -482,7 +511,7 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistograms(AliTRDtrack *t)
          
       //If the same track, then look if the previous detector is in
       //the same plane, if yes: not a good track
-      if ((GetPlane(detector) == GetPlane(fDetectorPreviousTrack))) {
+      if ((GetLayer(detector) == GetLayer(fDetectorPreviousTrack))) {
        return kFALSE;
       }
       
@@ -530,9 +559,11 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistograms(AliTRDtrack *t)
       }
       
       // Get calib objects
-      if( fCalROCGain ) delete fCalROCGain;
-      fCalROCGain = new AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
-       
+      if( fCalROCGain ){ 
+        fCalROCGain->~AliTRDCalROC();
+        new(fCalROCGain) AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
+      }else fCalROCGain = new AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
+      
     }
     
     // Reset the detectbjobsor
@@ -543,7 +574,7 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistograms(AliTRDtrack *t)
     ///////////////////////////////////////
     Int_t row = cl->GetPadRow();
     Int_t col = cl->GetPadCol();
-    CheckGoodTracklet(detector,row,col);
+    CheckGoodTrackletV1(cl);
     Int_t     group[2] = {0,0};
     if(fCH2dOn)  group[0]  = CalculateCalibrationGroup(0,row,col);
     if(fPH2dOn)  group[1]  = CalculateCalibrationGroup(1,row,col);
@@ -596,8 +627,15 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
 
   
   const AliTRDseedV1 *tracklet = 0x0;          // tracklet per plane
-  AliTRDcluster *cl      = 0x0;          // cluster attached now to the tracklet
+  AliTRDcluster *cl      = 0x0;                // cluster attached now to the tracklet
+  Bool_t         newtr   = kTRUE;              // new track
   
+  // Get cal
+  AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
+  if (!cal) {
+    AliInfo("Could not get calibDB");
+    return kFALSE;
+  }
   
   ///////////////////////////
   // loop over the tracklet
@@ -612,47 +650,48 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
     //////////////////////////////////////////
     // localisation of the tracklet and dqdl
     //////////////////////////////////////////
-    Int_t plane    = tracklet->GetPlane();
+    Int_t layer    = tracklet->GetPlane();
     Int_t ic = 0;
     while(!(cl = tracklet->GetClusters(ic++))) continue;
     Int_t detector = cl->GetDetector();
     if (detector != fDetectorPreviousTrack) {
-      // don't use the rest of this track if in the same plane
-      if ((plane == GetPlane(fDetectorPreviousTrack))) {
-       break;
+      // if not a new track
+      if(!newtr){
+       // don't use the rest of this track if in the same plane
+       if (layer == GetLayer(fDetectorPreviousTrack)) {
+         //printf("bad tracklet, same layer for detector %d\n",detector);
+         break;
+       }
       }
       //Localise the detector bin
       LocalisationDetectorXbins(detector);
-      // Get cal
-      AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
-      if (!cal) {
-       AliInfo("Could not get calibDB");
-       return kFALSE;
-      }
       // Get calib objects
-      if( fCalROCGain ) delete fCalROCGain;
-      fCalROCGain = new AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
+      if( fCalROCGain ){ 
+        fCalROCGain->~AliTRDCalROC();
+        new(fCalROCGain) AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
+      }else fCalROCGain = new AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
+      
       // reset
       fDetectorPreviousTrack = detector;
     }
-
+    newtr = kFALSE;
 
     ////////////////////////////
     // loop over the clusters
     ////////////////////////////
     Int_t nbclusters = 0;
-    for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
-      if(!(cl = tracklet->GetClusters(ic))) continue;
+    for(int jc=0; jc<AliTRDseed::knTimebins; jc++){
+      if(!(cl = tracklet->GetClusters(jc))) continue;
       nbclusters++;
       
       // Store the info bis of the tracklet
       Int_t row = cl->GetPadRow();
       Int_t col = cl->GetPadCol();
-      CheckGoodTracklet(detector,row,col);
+      CheckGoodTrackletV1(cl);
       Int_t     group[2] = {0,0};
       if(fCH2dOn)  group[0]  = CalculateCalibrationGroup(0,row,col);
       if(fPH2dOn)  group[1]  = CalculateCalibrationGroup(1,row,col);
-      StoreInfoCHPHtrack(cl, tracklet->GetdQdl(ic),group,row,col);
+      StoreInfoCHPHtrack(cl, tracklet->GetdQdl(jc),group,row,col);
     }
     
     ////////////////////////////////////////
@@ -676,10 +715,8 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
       if(pass && fPRF2dOn) HandlePRFtrackletV1(tracklet,nbclusters);
                
     } // if a good tracklet
   }
-
-
+  
   return kTRUE;
   
 }
@@ -704,8 +741,8 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
   ////////////////
   Int_t    detector                   = ((AliTRDcluster *) t->GetCluster(index0))->GetDetector(); //detector
   // parameters of the track
-  Double_t snp                        = t->GetSnpPlane(GetPlane(detector)); // sin angle in the plan yx track
-  Double_t tgl                        = t->GetTglPlane(GetPlane(detector)); // dz/dl and not dz/dx!  
+  Double_t snp                        = t->GetSnpPlane(GetLayer(detector)); // sin angle in the plan yx track
+  Double_t tgl                        = t->GetTglPlane(GetLayer(detector)); // dz/dl and not dz/dx!  
   Double_t tnp                        = 0.0;                                // tan angle in the plan xy track
   if( TMath::Abs(snp) <  1.){
     tnp = snp / (TMath::Sqrt(1-(snp*snp)));
@@ -714,17 +751,15 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
   // tilting pad and cross row
   Int_t    crossrow                   = 0;                                  // if it crosses a pad row
   Int_t    rowp                       = -1;                                 // if it crosses a pad row
-  AliTRDpadPlane *padplane            = fGeo->GetPadPlane(GetPlane(detector),GetChamber(detector));
+  AliTRDpadPlane *padplane            = fGeo->GetPadPlane(GetLayer(detector),GetStack(detector));
   Double_t tiltingangle               = padplane->GetTiltingAngle();        // tiltingangle of the pad      
   Float_t  tnt                        = TMath::Tan(tiltingangle/180.*TMath::Pi()); // tan tiltingangle
   // linear fit
-  TLinearFitter linearFitterTracklet  = TLinearFitter(2,"pol1");            // TLinearFitter per tracklet
+  fLinearFitterTracklet->ClearPoints();  
   Double_t dydt                       = 0.0;                                // dydt tracklet after straight line fit
   Double_t errorpar                   = 0.0;                                // error after straight line fit on dy/dt
   Double_t pointError                 = 0.0;                                // error after straight line fit 
   Int_t     nbli                      = 0;                                  // number linear fitter points
-  linearFitterTracklet.StoreData(kFALSE);
-  linearFitterTracklet.ClearPoints();
   
   //////////////////////////////
   // loop over clusters
@@ -732,7 +767,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
   for(Int_t k = 0; k < npoints; k++){
     
     AliTRDcluster *cl                 = (AliTRDcluster *) t->GetCluster(k+index0);
-    if(!cl->IsInChamber()) continue;
+    if((fLimitChargeIntegration) && (!cl->IsInChamber())) continue;
     Double_t ycluster                 = cl->GetY();
     Int_t time                        = cl->GetPadTime();
     Double_t timeis                   = time/fSf;
@@ -742,7 +777,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
     if(k==0) rowp                     = row;
     if(row != rowp) crossrow          = 1;
 
-    linearFitterTracklet.AddPoint(&timeis,ycluster,1);
+    fLinearFitterTracklet->AddPoint(&timeis,ycluster,1);
     nbli++;
 
   }
@@ -750,14 +785,18 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
   //////////////////////////////
   // linear fit
   //////////////////////////////
-  if(nbli <= 2) return kFALSE; 
+  if(nbli <= 2){ 
+    fLinearFitterTracklet->ClearPoints();  
+    return kFALSE; 
+  }
   TVectorD pars;
-  linearFitterTracklet.Eval();
-  linearFitterTracklet.GetParameters(pars);
-  pointError  =  TMath::Sqrt(linearFitterTracklet.GetChisquare()/nbli);
-  errorpar    =  linearFitterTracklet.GetParError(1)*pointError;
+  fLinearFitterTracklet->Eval();
+  fLinearFitterTracklet->GetParameters(pars);
+  pointError  =  TMath::Sqrt(fLinearFitterTracklet->GetChisquare()/(nbli-2));
+  errorpar    =  fLinearFitterTracklet->GetParError(1)*pointError;
   dydt        = pars[1]; 
-
+  fLinearFitterTracklet->ClearPoints();  
+    
   /////////////////////////////
   // debug
   ////////////////////////////
@@ -788,13 +827,13 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
 
 
     Int_t nbclusters = index1-index0;
-    Int_t plane      = GetPlane(fDetectorPreviousTrack);
+    Int_t layer      = GetLayer(fDetectorPreviousTrack);
 
     (* fDebugStreamer) << "FindP1TrackPHtracklet1"<<
       //"snpright="<<snpright<<
       "nbclusters="<<nbclusters<<
       "detector="<<fDetectorPreviousTrack<<
-      "plane="<<plane<<
+      "layer="<<layer<<
       "\n";     
 
   }
@@ -841,7 +880,6 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   ////////////
   //Variables
   ////////////
-  TLinearFitter linearFitterTracklet  = TLinearFitter(2,"pol1");            // TLinearFitter per tracklet
   // results of the linear fit
   Double_t dydt                       = 0.0;                                // dydt tracklet after straight line fit
   Double_t errorpar                   = 0.0;                                // error after straight line fit on dy/dt
@@ -850,9 +888,8 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   Int_t    crossrow                   = 0;                                  // if it crosses a pad row
   Int_t    rowp                       = -1;                                 // if it crosses a pad row
   Float_t  tnt                        = tracklet->GetTilt();                // tan tiltingangle
-
-  linearFitterTracklet.StoreData(kFALSE);
-  linearFitterTracklet.ClearPoints();
+  fLinearFitterTracklet->ClearPoints();  
   
   ///////////////////////////////////////////
   // Take the parameters of the track
@@ -881,7 +918,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   AliTRDcluster *cl                   = 0x0;
   for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
     if(!(cl = tracklet->GetClusters(ic))) continue;
-    if(!cl->IsInChamber()) continue;
+    if((fLimitChargeIntegration) && (!cl->IsInChamber())) continue;
     
     Double_t ycluster                 = cl->GetY();
     Int_t time                        = cl->GetPadTime();
@@ -891,7 +928,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
     if(rowp==-1) rowp                 = row;
     if(row != rowp) crossrow          = 1;
 
-    linearFitterTracklet.AddPoint(&timeis,ycluster,1);
+    fLinearFitterTracklet->AddPoint(&timeis,ycluster,1);
     nbli++;  
 
   }
@@ -899,12 +936,17 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   ////////////////////////////////////
   // Do the straight line fit now
   ///////////////////////////////////
+  if(nbli <= 2){ 
+    fLinearFitterTracklet->ClearPoints();  
+    return kFALSE; 
+  }
   TVectorD pars;
-  linearFitterTracklet.Eval();
-  linearFitterTracklet.GetParameters(pars);
-  pointError  =  TMath::Sqrt(linearFitterTracklet.GetChisquare()/nbli);
-  errorpar    =  linearFitterTracklet.GetParError(1)*pointError;
+  fLinearFitterTracklet->Eval();
+  fLinearFitterTracklet->GetParameters(pars);
+  pointError  =  TMath::Sqrt(fLinearFitterTracklet->GetChisquare()/(nbli-2));
+  errorpar    =  fLinearFitterTracklet->GetParError(1)*pointError;
   dydt        = pars[1]; 
+  fLinearFitterTracklet->ClearPoints();  
  
   ////////////////////////////////
   // Debug stuff
@@ -920,13 +962,13 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
     } 
     
 
-    Int_t plane = GetPlane(fDetectorPreviousTrack);
+    Int_t layer = GetLayer(fDetectorPreviousTrack);
         
     (* fDebugStreamer) << "FindP1TrackPHtrackletV1"<<
       //"snpright="<<snpright<<
       "nbclusters="<<nbclusters<<
       "detector="<<fDetectorPreviousTrack<<
-      "plane="<<plane<<
+      "layer="<<layer<<
       "snp="<<snp<<
       "tnp="<<tnp<<
       "tgl="<<tgl<<
@@ -993,8 +1035,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
     padPositions[k] = 0.0;
   } 
   // Take the tgl and snp with the track t now
-  Double_t  tgl = t->GetTglPlane(GetPlane(detector)); //dz/dl and not dz/dx
-  Double_t  snp = t->GetSnpPlane(GetPlane(detector)); // sin angle in xy plan
+  Double_t  tgl = t->GetTglPlane(GetLayer(detector)); //dz/dl and not dz/dx
+  Double_t  snp = t->GetSnpPlane(GetLayer(detector)); // sin angle in xy plan
   Float_t  dzdx = 0.0;                                // dzdx
   Float_t  tnp  = 0.0;
   if(TMath::Abs(snp) < 1.0){
@@ -1002,10 +1044,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
     dzdx = tgl*TMath::Sqrt(1+tnp*tnp);
   }
   // linear fitter
-  TLinearFitter fitter(2,"pol1");
-  fitter.StoreData(kFALSE);
-  fitter.ClearPoints();
-
+  fLinearFitterTracklet->ClearPoints();
 
   ///////////////////////////
   // calcul the tnp group
@@ -1024,8 +1063,10 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
       }
     }
   }
-  if(echec) return kFALSE;
-  
+  if(echec) {
+    delete [] padPositions;
+    return kFALSE;
+  }
 
   //////////////////////
   // loop clusters
@@ -1034,16 +1075,16 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
     //Take the cluster
     AliTRDcluster *cl  = (AliTRDcluster *) t->GetCluster(k+index0);
     Short_t  *signals  = cl->GetSignals();
-    Double_t     time  = cl->GetPadTime();
+    Double_t     time  = cl->GetLocalTimeBin();
     //Calculate x if possible 
     Float_t xcenter    = 0.0;    
-    Bool_t  echec      = kTRUE;   
+    Bool_t  echec1      = kTRUE;   
     if((time<=7) || (time>=21)) continue; 
     // Center 3 balanced: position with the center of the pad
     if ((((Float_t) signals[3]) > 0.0) && 
        (((Float_t) signals[2]) > 0.0) && 
        (((Float_t) signals[4]) > 0.0)) {
-      echec = kFALSE;
+      echec1 = kFALSE;
       // Security if the denomiateur is 0 
       if ((((Float_t) (((Float_t) signals[3]) * ((Float_t) signals[3]))) / 
           ((Float_t) (((Float_t) signals[2]) * ((Float_t) signals[4])))) != 1.0) {
@@ -1052,7 +1093,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
                        / ((Float_t) (((Float_t) signals[2]) * ((Float_t) signals[4])))));
       }
       else {
-       echec = kTRUE;
+       echec1 = kTRUE;
       }
     }
     if(TMath::Abs(xcenter) > 0.5) echec = kTRUE;
@@ -1062,19 +1103,26 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
     Double_t       padPosition = xcenter +  cl->GetPadCol();
     padPositions[k]            = padPosition;
     nb3pc++;
-    fitter.AddPoint(&time, padPosition,1);
+    fLinearFitterTracklet->AddPoint(&time, padPosition,1);
   }//clusters loop
 
 
   /////////////////////////////
   // fit
   ////////////////////////////
-  if(nb3pc < 3) return kFALSE;
-  fitter.Eval();
+  if(nb3pc < 3) {
+    delete [] padPositions;
+    fLinearFitterTracklet->ClearPoints();  
+    return kFALSE;
+  }
+  fLinearFitterTracklet->Eval();
   TVectorD line(2);
-  fitter.GetParameters(line);
+  fLinearFitterTracklet->GetParameters(line);
   Float_t  pointError  = -1.0;
-  pointError  =  TMath::Sqrt(fitter.GetChisquare()/nb3pc);
+  if( fLinearFitterTracklet->GetChisquare()>=0.0) {
+    pointError  =  TMath::Sqrt( fLinearFitterTracklet->GetChisquare()/(nb3pc-2));
+  }
+  fLinearFitterTracklet->ClearPoints();  
   
   /////////////////////////////////////////////////////
   // Now fill the PRF: second loop over clusters
@@ -1083,7 +1131,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
     //Take the cluster
     AliTRDcluster *cl      = (AliTRDcluster *) t->GetCluster(k+index0);
     Short_t  *signals      = cl->GetSignals();              // signal
-    Double_t     time      = cl->GetPadTime();              // time bin
+    Double_t     time      = cl->GetLocalTimeBin();              // time bin
     Float_t padPosTracklet = line[0]+line[1]*time;          // reconstruct position from fit
     Float_t padPos         = cl->GetPadCol();               // middle pad
     Double_t dpad          = padPosTracklet - padPos;       // reconstruct position relative to middle pad from fit 
@@ -1107,8 +1155,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
     Int_t     caligroup    = fCalibraMode->GetXbins(2)+ grouplocal;     // calcul the corresponding group
     Float_t   xcl          = cl->GetY();                                // y cluster
     Float_t   qcl          = cl->GetQ();                                // charge cluster 
-    Int_t     plane        = GetPlane(detector);                        // plane 
-    Int_t     chamber      = GetChamber(detector);                      // chamber  
+    Int_t     layer        = GetLayer(detector);                        // layer 
+    Int_t     stack        = GetStack(detector);                        // stack
     Double_t  xdiff        = dpad;                                      // reconstructed position constant
     Double_t  x            = dpad;                                      // reconstructed position moved
     Float_t   ep           = pointError;                                // error of fit
@@ -1136,8 +1184,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
       (* fDebugStreamer) << "HandlePRFtracklet"<<
        "caligroup="<<caligroup<<
        "detector="<<detector<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
+       "layer="<<layer<<
+       "stack="<< stack <<
        "npoints="<<npoints<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
@@ -1166,8 +1214,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
       (* fDebugStreamer) << "HandlePRFtracklet"<<
        "caligroup="<<caligroup<<
        "detector="<<detector<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
+       "layer="<<layer<<
+       "stack="<<stack<<
        "npoints="<<npoints<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
@@ -1196,8 +1244,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
       (* fDebugStreamer) << "HandlePRFtracklet"<<
        "caligroup="<<caligroup<<
        "detector="<<detector<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
+       "layer="<<layer<<
+       "stack="<<stack<<
        "npoints="<<npoints<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
@@ -1265,6 +1313,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
       }
     }
   }
+  delete [] padPositions;
   return kTRUE;
   
 }
@@ -1280,6 +1329,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   //
   //
 
+  //printf("begin\n");
   ///////////////////////////////////////////
   // Take the parameters of the track
   //////////////////////////////////////////
@@ -1317,6 +1367,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
     }
   }
   // do nothing if out of tnp range
+  //printf("echec %d\n",(Int_t)echec);
   if(echec) return kFALSE;
 
   ///////////////////////
@@ -1329,10 +1380,9 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   for(Int_t k = 0; k < AliTRDseed::knTimebins; k++){
     padPositions[k] = 0.0;
   } 
-  TLinearFitter fitter(2,"pol1");  // TLinearFitter for the linear fit in the drift region
-  fitter.StoreData(kFALSE);
-  fitter.ClearPoints();
-
+  fLinearFitterTracklet->ClearPoints();  
+  
+  //printf("loop clusters \n");
   ////////////////////////////
   // loop over the clusters
   ////////////////////////////
@@ -1340,11 +1390,11 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
     if(!(cl = tracklet->GetClusters(ic))) continue;
     
-    Double_t     time  = cl->GetPadTime();
+    Double_t     time  = cl->GetLocalTimeBin();
     if((time<=7) || (time>=21)) continue;
     Short_t  *signals  = cl->GetSignals(); 
     Float_t xcenter    = 0.0;    
-    Bool_t  echec      = kTRUE;   
+    Bool_t  echec1      = kTRUE;   
 
     /////////////////////////////////////////////////////////////
     // Center 3 balanced: position with the center of the pad
@@ -1352,7 +1402,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
     if ((((Float_t) signals[3]) > 0.0) && 
        (((Float_t) signals[2]) > 0.0) && 
        (((Float_t) signals[4]) > 0.0)) {
-      echec = kFALSE;
+      echec1 = kFALSE;
       // Security if the denomiateur is 0 
       if ((((Float_t) (((Float_t) signals[3]) * ((Float_t) signals[3]))) / 
           ((Float_t) (((Float_t) signals[2]) * ((Float_t) signals[4])))) != 1.0) {
@@ -1361,36 +1411,45 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
                        / ((Float_t) (((Float_t) signals[2]) * ((Float_t) signals[4])))));
       }
       else {
-       echec = kTRUE;
+       echec1 = kTRUE;
       }
     }
-    if(TMath::Abs(xcenter) > 0.5) echec = kTRUE;
-    if(echec) continue;
+    if(TMath::Abs(xcenter) > 0.5) echec1 = kTRUE;
+    if(echec1) continue;
 
     ////////////////////////////////////////////////////////
-    //if no echec: calculate with the position of the pad
+    //if no echec1: calculate with the position of the pad
     // Position of the cluster
     // fill the linear fitter
     ///////////////////////////////////////////////////////
     Double_t       padPosition = xcenter +  cl->GetPadCol();
     padPositions[ic]            = padPosition;
     nb3pc++;
-    fitter.AddPoint(&time, padPosition,1);
+    fLinearFitterTracklet->AddPoint(&time, padPosition,1);
 
 
   }//clusters loop
 
-
+  //printf("Fin loop clusters \n");
   //////////////////////////////
   // fit with a straight line
   /////////////////////////////
-  if(nb3pc < 3) return kFALSE;
-  fitter.Eval();
+  if(nb3pc < 3){ 
+    delete [] padPositions;
+    fLinearFitterTracklet->ClearPoints();  
+    delete [] padPositions;
+    return kFALSE;
+  }
+  fLinearFitterTracklet->Eval();
   TVectorD line(2);
-  fitter.GetParameters(line);
+  fLinearFitterTracklet->GetParameters(line);
   Float_t  pointError  = -1.0;
-  pointError  =  TMath::Sqrt(fitter.GetChisquare()/nb3pc);
-  
+  if( fLinearFitterTracklet->GetChisquare()>=0.0) {
+  pointError  =  TMath::Sqrt( fLinearFitterTracklet->GetChisquare()/(nb3pc-2));
+  }
+  fLinearFitterTracklet->ClearPoints();  
+  //printf("PRF second loop \n");
   ////////////////////////////////////////////////
   // Fill the PRF: Second loop over clusters
   //////////////////////////////////////////////
@@ -1398,7 +1457,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
     if(!(cl = tracklet->GetClusters(ic))) continue;
 
     Short_t  *signals      = cl->GetSignals();              // signal
-    Double_t     time      = cl->GetPadTime();              // time bin
+    Double_t     time      = cl->GetLocalTimeBin();         // time bin
     Float_t padPosTracklet = line[0]+line[1]*time;          // reconstruct position from fit
     Float_t padPos         = cl->GetPadCol();               // middle pad
     Double_t dpad          = padPosTracklet - padPos;       // reconstruct position relative to middle pad from fit 
@@ -1426,8 +1485,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
     Int_t     caligroup    = fCalibraMode->GetXbins(2)+ grouplocal;     // calcul the corresponding group
     Float_t   xcl          = cl->GetY();                                // y cluster
     Float_t   qcl          = cl->GetQ();                                // charge cluster 
-    Int_t     plane        = GetPlane(fDetectorPreviousTrack);          // plane 
-    Int_t     chamber      = GetChamber(fDetectorPreviousTrack);        // chamber  
+    Int_t     layer        = GetLayer(fDetectorPreviousTrack);          // layer 
+    Int_t     stack        = GetStack(fDetectorPreviousTrack);          // stack  
     Double_t  xdiff        = dpad;                                      // reconstructed position constant
     Double_t  x            = dpad;                                      // reconstructed position moved
     Float_t   ep           = pointError;                                // error of fit
@@ -1437,6 +1496,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
     Float_t   signal4      = (Float_t)signals[4];                       // signal
     Float_t   signal5      = (Float_t)signals[5];                       // signal at the border
    
+
+
     /////////////////////
     // Debug stuff
     ////////////////////
@@ -1455,8 +1516,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
       (* fDebugStreamer) << "HandlePRFtrackletV1"<<
        "caligroup="<<caligroup<<
        "detector="<<fDetectorPreviousTrack<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
+       "layer="<<layer<<
+       "stack="<<stack<<
        "npoints="<<nbclusters<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
@@ -1485,8 +1546,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
       (* fDebugStreamer) << "HandlePRFtrackletV1"<<
        "caligroup="<<caligroup<<
        "detector="<<fDetectorPreviousTrack<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
+       "layer="<<layer<<
+       "stack="<<stack<<
        "npoints="<<nbclusters<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
@@ -1515,8 +1576,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
       (* fDebugStreamer) << "HandlePRFtrackletV1"<<
        "caligroup="<<caligroup<<
        "detector="<<fDetectorPreviousTrack<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
+       "layer="<<layer<<
+       "stack="<<stack<<
        "npoints="<<nbclusters<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
@@ -1589,6 +1650,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   } // second loop over clusters
 
 
+  delete [] padPositions;
   return kTRUE;
   
 }
@@ -1596,7 +1658,18 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
 // Pad row col stuff: see if masked or not
 ///////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________________
-void AliTRDCalibraFillHisto::CheckGoodTracklet(Int_t detector, Int_t row, Int_t col)
+void AliTRDCalibraFillHisto::CheckGoodTrackletV1(AliTRDcluster *cl)
+{
+  //
+  // See if we are not near a masked pad
+  //
+
+  if(cl->IsMasked()) fGoodTracklet = kFALSE;
+
+  
+}
+//_____________________________________________________________________________
+void AliTRDCalibraFillHisto::CheckGoodTrackletV0(Int_t detector, Int_t row, Int_t col)
 {
   //
   // See if we are not near a masked pad
@@ -1744,9 +1817,9 @@ Bool_t AliTRDCalibraFillHisto::LocalisationDetectorXbins(Int_t detector)
 
   // fragmentation of idect
   for (Int_t i = 0; i < 3; i++) {
-    fCalibraMode->ModePadCalibration((Int_t) GetChamber(detector),i);
-    fCalibraMode->ModePadFragmentation((Int_t) GetPlane(detector)
-                       , (Int_t) GetChamber(detector)
+    fCalibraMode->ModePadCalibration((Int_t) GetStack(detector),i);
+    fCalibraMode->ModePadFragmentation((Int_t) GetLayer(detector)
+                       , (Int_t) GetStack(detector)
                        , (Int_t) GetSector(detector),i);
   }
   
@@ -1795,8 +1868,8 @@ void AliTRDCalibraFillHisto::StoreInfoCHPHtrack(AliTRDcluster *cl, Double_t dqdl
   
 
   // Fill the fAmpTotal with the charge
-  if (fCH2dOn && cl->IsInChamber()) {
-    fAmpTotal[(Int_t) group[0]] += correction;
+  if (fCH2dOn) {
+    if((!fLimitChargeIntegration) || (cl->IsInChamber())) fAmpTotal[(Int_t) group[0]] += correction;
   }
 
   // Fill the fPHPlace and value
@@ -1868,7 +1941,7 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
       fEntriesCH[fCalibraMode->GetXbins(0)+fd]++;
       if (fHisto2d) {
        FillCH2d(fCalibraMode->GetXbins(0)+fd,fAmpTotal[fd]/fRelativeScale);
-       //fCH2d->Fill(fAmpTotal[fd]/fRelativeScale,fCalibraMode->GetXbins(0)+fd+0.5);
+       //fCH2d->Fill(fAmpTotal[fd]/fRelativeScale,fCalibraMode->GetXbins(0)+fd+0.5);
       }
       if (fVector2d) {
        fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd,fAmpTotal[fd]/fRelativeScale);
@@ -1975,6 +2048,8 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
     }
   }
 
+  //printf("nbclusters %d, low limit %d, high limit %d\n",nbclusters,fNumberClusters,fNumberClustersf);
+
   if(nbclusters < fNumberClusters) return;
   if(nbclusters > fNumberClustersf) return;
   
@@ -1984,11 +2059,17 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
       fNumberUsedPh[0]++;
       for (Int_t i = 0; i < fTimeMax; i++) {
        if (fHisto2d) {
-         fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+         if(fFillWithZero) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+         else {
+           if(((Float_t) fPHValue[i] > 0.0)) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+             }
          //printf("Fill the time bin %d with %f\n",i,fPHValue[i]);
        }
        if (fVector2d) {
-         fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+         if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+         else {
+           if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);  
+         }
        }
       }
       break;
@@ -2001,10 +2082,16 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
          fNumberUsedPh[1]++;
          for (Int_t i = 0; i < fTimeMax; i++) {
            if (fHisto2d) {
-             fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+             if(fFillWithZero) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+             else {
+               if(((Float_t) fPHValue[i] > 0.0)) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+                 }
            }
            if (fVector2d) {
-             fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+             if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+             else {
+               if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 }
            }
          }
        }
@@ -2013,10 +2100,16 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
          fNumberUsedPh[1]++;
          for (Int_t i = 0; i < fTimeMax; i++) {
            if (fHisto2d) {
-             fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+             if(fFillWithZero) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+             else {
+               if(((Float_t) fPHValue[i] > 0.0)) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+             }
            }
          if (fVector2d) {
-           fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+           if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+           else {
+             if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+           }
          }
          }
        }
@@ -2032,10 +2125,16 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
              fNumberUsedPh[1]++;
              for (Int_t i = 0; i < fTimeMax; i++) {
                if (fHisto2d) {
-                 fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+                 if(fFillWithZero) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+                 }
                }
                if (fVector2d) {
-                 fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 }
                }
              }
            }
@@ -2044,10 +2143,16 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
              fNumberUsedPh[1]++;
              for (Int_t i = 0; i < fTimeMax; i++) {
                if (fHisto2d) {
-                 fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+                 if(fFillWithZero) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+                 }
                }
                if (fVector2d) {
-                 fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 }
                }
              }
            }
@@ -2063,10 +2168,16 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
              fNumberUsedPh[1]++;
              for (Int_t i = 0; i < fTimeMax; i++) {
                if (fHisto2d) {
-                 fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+                 if(fFillWithZero) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd1)+0.5,(Float_t) fPHValue[i]);
+                 }
                }
                if (fVector2d) {
-                 fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 }
                }
              }
            }
@@ -2075,10 +2186,16 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
              fNumberUsedPh[1]++;
              for (Int_t i = 0; i < fTimeMax; i++) {
                if (fHisto2d) {
-                 fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+                 if(fFillWithZero) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fPH2d->Fill((Float_t) i/fSf,(fCalibraMode->GetXbins(1)+fd2)+0.5,(Float_t) fPHValue[i]);
+                 }
                }
                if (fVector2d) {
-                 fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 }
                }
              }
            }
@@ -2093,10 +2210,10 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
 // DAQ process functions
 /////////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool_t nocheck)
+Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamBase *rawStream, Bool_t nocheck)
 {
   //
-  // Event Processing loop - AliTRDrawStreamTB
+  // Event Processing loop - AliTRDrawStreamBase
   // TestBeam 2007 version
   // 0 timebin problem
   // 1 no input
@@ -2120,7 +2237,7 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool
   fMCMPrevious           = -1;
   fROBPrevious           = -1;
   Int_t nbtimebin = 0;                                        
-  Int_t baseline  = 0;  
+  Int_t baseline  = 10;  
 
 
   if(!nocheck){
@@ -2132,7 +2249,9 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool
       Int_t idetector = rawStream->GetDet();                            //  current detector
       Int_t imcm      = rawStream->GetMCM();                            //  current MCM
       Int_t irob      = rawStream->GetROB();                            //  current ROB
-      
+
+      //printf("Detector %d\n",idetector);
+
       if((fDetectorPreviousTrack != idetector) && (fDetectorPreviousTrack != -1)){
        
        // Fill
@@ -2158,9 +2277,11 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool
       if((fTimeMax != 0) && (nbtimebin != fTimeMax)) return 0;
       fTimeMax          = nbtimebin;
 
-      baseline          = rawStream->GetCommonAdditive();                // common additive baseline
-     
-      Int_t iTimeBin    = rawStream->GetTimeBin();                       //  current time bin
+      //baseline          = rawStream->GetCommonAdditive();                // common additive baseline
+      fNumberClustersf    = fTimeMax;
+      fNumberClusters     = (Int_t)(0.6*fTimeMax);
+
+
       Int_t *signal     = rawStream->GetSignals();                       //  current ADC signal
       Int_t  col        = rawStream->GetCol();
       Int_t row         = rawStream->GetRow();    
@@ -2169,11 +2290,8 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool
       //printf("detector %d, signal[0] %d, signal[1] %d, signal[2] %d, baseline %d\n",idetector,signal[0],signal[1],signal[2], baseline);
      
                 
-      Int_t fin     = TMath::Min(fTimeMax,(iTimeBin+3));
-      Int_t n       = 0;
-      for(Int_t itime = iTimeBin; itime < fin; itime++){
-       phvalue[row][col][itime] = signal[n]-baseline;
-       n++;
+      for(Int_t itime = 0; itime < nbtimebin; itime++){
+       phvalue[row][col][itime] = signal[itime]-baseline;
       }
     }
     
@@ -2202,6 +2320,8 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool
       Int_t imcm      = rawStream->GetMCM();                            //  current MCM
       Int_t irob      = rawStream->GetROB();                            //  current ROB
 
+      //printf("Detector %d\n",idetector);
+
       if((fDetectorPreviousTrack != idetector) && (fDetectorPreviousTrack != -1)){
 
        // Fill
@@ -2221,22 +2341,20 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool
       fMCMPrevious           = imcm;
       fROBPrevious           = irob;
 
-      baseline          = rawStream->GetCommonAdditive();                //  common baseline
+      //baseline          = rawStream->GetCommonAdditive();                //  common baseline
       
       fTimeMax          = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
-      Int_t iTimeBin    = rawStream->GetTimeBin();                       //  current time bin
+      fNumberClustersf    = fTimeMax;
+      fNumberClusters     = (Int_t)(0.6*fTimeMax);
       Int_t *signal     = rawStream->GetSignals();                       //  current ADC signal
       Int_t col         = rawStream->GetCol();
       Int_t row         = rawStream->GetRow();   
 
-      Int_t fin     = TMath::Min(fTimeMax,(iTimeBin+3));
-      Int_t n       = 0;
-    
+       
       //printf("detector %d, signal[0] %d, signal[1] %d, signal[2] %d, baseline %d\n",idetector,signal[0],signal[1],signal[2], baseline);
       
-      for(Int_t itime = iTimeBin; itime < fin; itime++){
-       phvalue[row][col][itime] = signal[n]-baseline;
-       n++;
+      for(Int_t itime = 0; itime < fTimeMax; itime++){
+       phvalue[row][col][itime] = signal[itime]-baseline;
       }
     }
     
@@ -2261,10 +2379,10 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool
   
 }
 //_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliTRDrawStreamTB *rawStream, Bool_t nocheck)
+Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliTRDrawStreamBase *rawStream, Bool_t nocheck)
 {
   //
-  // Event Processing loop - AliTRDrawStreamTB
+  // Event Processing loop - AliTRDrawStreamBase
   // Use old AliTRDmcmtracklet code
   // 0 timebin problem
   // 1 no input
@@ -2284,7 +2402,7 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliTRDrawStreamTB *rawStream, Bo
   fROBPrevious           = -1;
   Int_t row              = -1;
   Int_t nbtimebin = 0;                                        
-  Int_t baseline  = 0;  
+  Int_t baseline  = 10;  
 
 
   if(!nocheck){
@@ -2330,23 +2448,20 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliTRDrawStreamTB *rawStream, Bo
       if(nbtimebin == 0) return 0;
       if((fTimeMax != 0) && (nbtimebin != fTimeMax)) return 0;
       fTimeMax          = nbtimebin;
+      fNumberClustersf  = fTimeMax;
+      fNumberClusters   = (Int_t)(0.6*fTimeMax);
       param->SetTimeRange(0,fTimeMax);
 
-      baseline          = rawStream->GetCommonAdditive();                // common additive baseline
+      //baseline          = rawStream->GetCommonAdditive();                // common additive baseline
      
-      Int_t iTimeBin    = rawStream->GetTimeBin();                       //  current time bin
       Int_t *signal     = rawStream->GetSignals();                       //  current ADC signal
       Int_t  adc        = rawStream->GetADC();
     
       
       //printf("detector %d, signal[0] %d, signal[1] %d, signal[2] %d, baseline %d\n",idetector,signal[0],signal[1],signal[2], baseline);
      
-                
-      Int_t fin     = TMath::Min(fTimeMax,(iTimeBin+3));
-      Int_t n       = 0;
-      for(Int_t itime = iTimeBin; itime < fin; itime++){
-       mcm.SetADC(adc,itime,(signal[n]-baseline));
-       n++;
+      for(Int_t itime = 0; itime < nbtimebin; itime++){
+       mcm.SetADC(adc,itime,(signal[itime]-baseline));
       }
     }
     
@@ -2395,23 +2510,20 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliTRDrawStreamTB *rawStream, Bo
       fMCMPrevious           = imcm;
       fROBPrevious           = irob;
 
-      baseline          = rawStream->GetCommonAdditive();                //  common baseline
+      //baseline          = rawStream->GetCommonAdditive();                //  common baseline
       
       fTimeMax          = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
+      fNumberClustersf  = fTimeMax;
+      fNumberClusters   = (Int_t)(0.6*fTimeMax);
       param->SetTimeRange(0,fTimeMax);
-      Int_t iTimeBin    = rawStream->GetTimeBin();                       //  current time bin
       Int_t *signal     = rawStream->GetSignals();                       //  current ADC signal
       Int_t adc         = rawStream->GetADC();
        
-
-      Int_t fin     = TMath::Min(fTimeMax,(iTimeBin+3));
-      Int_t n       = 0;
-    
+      
       //printf("detector %d, signal[0] %d, signal[1] %d, signal[2] %d, baseline %d\n",idetector,signal[0],signal[1],signal[2], baseline);
       
-      for(Int_t itime = iTimeBin; itime < fin; itime++){
-       mcm.SetADC(adc,itime,(signal[n]-baseline));
-       n++;
+      for(Int_t itime = 0; itime < fTimeMax; itime++){
+       mcm.SetADC(adc,itime,(signal[itime]-baseline));
       }
     }
     
@@ -2442,7 +2554,7 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliRawReader *rawReader, Bool_t no
   //  Testbeam 2007 version
   //
 
-  AliTRDrawStreamTB rawStream(rawReader);
+  AliTRDrawStreamBase rawStream(rawReader);
 
   rawReader->Select("TRD");
 
@@ -2484,7 +2596,7 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliRawReader *rawReader, Bool_t
   //  use the old mcm traklet code
   //
 
-  AliTRDrawStreamTB rawStream(rawReader);
+  AliTRDrawStreamBase rawStream(rawReader);
 
   rawReader->Select("TRD");
 
@@ -2610,12 +2722,12 @@ Int_t AliTRDCalibraFillHisto::UpdateHistogramcm(AliTRDmcmTracklet *trk)
   
   Int_t used = 1;
 
-  fGoodTracklet = kTRUE;
+  //fGoodTracklet = kTRUE;
 
   // Localisation of the Xbins involved
   Int_t idect = trk->GetDetector();
   Int_t idectrue = trk->GetDetector();
-  idect = 0;
+  //idect = 0;
 
   Int_t nbclusters = trk->GetNclusters();
   
@@ -2644,7 +2756,7 @@ Int_t AliTRDCalibraFillHisto::UpdateHistogramcm(AliTRDmcmTracklet *trk)
     Int_t   time   = trk->GetClusterTime(icl);
     Int_t   col    = trk->GetClusterCol(icl);
     
-    CheckGoodTracklet(idect,row,col);
+    //CheckGoodTrackletV0(idect,row,col);
     
     amp[0] = trk->GetClusterADC(icl)[0] * correction;
     amp[1] = trk->GetClusterADC(icl)[1] * correction;
@@ -2727,13 +2839,14 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
 
 
   Int_t idect = fDetectorPreviousTrack;      
+  //printf("Enter Detector %d\n",fDetectorPreviousTrack);
   Double_t sum[36];
   for(Int_t tb = 0; tb < 36; tb++){
     sum[tb] = 0.0;
   }
 
-  fGoodTracklet = kTRUE;
-  fDetectorPreviousTrack = 0;
+  //fGoodTracklet = kTRUE;
+  //fDetectorPreviousTrack = 0;  
 
 
   ///////////////////////////
@@ -2775,9 +2888,12 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
 
   //printf("imaxRow %d, imaxCol %d, fTimeMax %d, integralMax %f\n",imaxRow,imaxCol,fTimeMax, integralMax);
 
-  if((imaxRow == 0) || (imaxCol == 0)) used=1;
-  CheckGoodTracklet(fDetectorPreviousTrack,imaxRow,imaxCol);
-  if(!fGoodTracklet) used = 1;;
+  if((imaxRow == 0) || (imaxCol == 0)) {
+    used=1;
+    return used;
+  }
+  //CheckGoodTrackletV0(fDetectorPreviousTrack,imaxRow,imaxCol);
+  //if(!fGoodTracklet) used = 1;;
   
   //  /////////////////////////////////////////////////////
   // sum ober 2 row and 4 pad cols for each time bins
@@ -2848,12 +2964,17 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
   //if((TMath::RMS(fTimeMax,sum) <= 10.0) && (sum[0] > 200.0)) return 1;
   if(used == 0){
     for(Int_t it = 0; it < fTimeMax; it++){
-      UpdateDAQ(fDetectorPreviousTrack,0,0,it,sum[it],fTimeMax);  
+      if(fFillWithZero) UpdateDAQ(fDetectorPreviousTrack,0,0,it,sum[it],fTimeMax); 
+      else{
+       if(sum[it] > 0.0) UpdateDAQ(fDetectorPreviousTrack,0,0,it,sum[it],fTimeMax); 
+      } 
     }
     
    
     //((TH2I *)GetCH2d()->Fill(sumcharge/30.0,fDetectorPreviousTrack));
     used = 2;
+    //printf("Pass Detector %d\n",fDetectorPreviousTrack);
+
   }
  
   return used;
@@ -3272,10 +3393,10 @@ void  AliTRDCalibraFillHisto::FillCH2d(Int_t x, Float_t y)
 // Geometrical functions
 ///////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________________
-Int_t AliTRDCalibraFillHisto::GetPlane(Int_t d) const
+Int_t AliTRDCalibraFillHisto::GetLayer(Int_t d) const
 {
   //
-  // Reconstruct the plane number from the detector number
+  // Reconstruct the layer number from the detector number
   //
 
   return ((Int_t) (d % 6));
@@ -3283,14 +3404,14 @@ Int_t AliTRDCalibraFillHisto::GetPlane(Int_t d) const
 }
 
 //_____________________________________________________________________________
-Int_t AliTRDCalibraFillHisto::GetChamber(Int_t d) const
+Int_t AliTRDCalibraFillHisto::GetStack(Int_t d) const
 {
   //
-  // Reconstruct the chamber number from the detector number
+  // Reconstruct the stack number from the detector number
   //
-  Int_t fgkNplan = 6;
+  const Int_t kNlayer = 6;
 
-  return ((Int_t) (d % 30) / fgkNplan);
+  return ((Int_t) (d % 30) / kNlayer);
 
 }