]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDCalibraFillHisto.cxx
minor coverity defect: added protection for self-assignment
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibraFillHisto.cxx
index d2dbc513d3203f1216aeda3d9e3096e25c7b19a9..42603d89cb45dd776df3752296364fe91a864afc 100644 (file)
@@ -1,17 +1,17 @@
-/**************************************************************************
- * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
- *                                                                        *
- * Author: The ALICE Off-line Project.                                    *
- * Contributors are mentioned in the code where appropriate.              *
- *                                                                        *
- * Permission to use, copy, modify and distribute this software and its   *
- * documentation strictly for non-commercial purposes is hereby granted   *
- * without fee, provided that the above copyright notice appears in all   *
- * copies and that both the copyright notice and this permission notice   *
- * appear in the supporting documentation. The authors make no claims     *
- * about the suitability of this software for any purpose. It is          *
- * provided "as is" without express or implied warranty.                  *
- **************************************************************************/
+//**************************************************************************
+// * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
+// *                                                                        *
+// * Author: The ALICE Off-line Project.                                    *
+// * Contributors are mentioned in the code where appropriate.              *
+//   *                                                                        *
+//   * Permission to use, copy, modify and distribute this software and its   *
+//  * documentation strictly for non-commercial purposes is hereby granted   *
+//    * without fee, provided that the above copyright notice appears in all   *
+//    * copies and that both the copyright notice and this permission notice   *
+//* appear in the supporting documentation. The authors make no claims     *
+//* about the suitability of this software for any purpose. It is          *
+//* provided "as is" without express or implied warranty.                  *
+//**************************************************************************/
 
 /* $Id$ */
 
@@ -28,8 +28,9 @@
 // in the function "FollowBackProlongation" (AliTRDtracker)
 // Per default the functions to fill are off.                                   
 //                        
-// Author:
-//   R. Bailhache (R.Bailhache@gsi.de)
+// Authors:
+//   R. Bailhache (R.Bailhache@gsi.de, rbailhache@ikf.uni-frankfurt.de)
+//   J. Book (jbook@ikf.uni-frankfurt.de)
 //                            
 //////////////////////////////////////////////////////////////////////////////////////
 
@@ -48,6 +49,7 @@
 #include <TDirectory.h>
 #include <TTreeStream.h>
 #include <TVectorD.h>
+#include <TLinearFitter.h>
 
 #include "AliLog.h"
 
 #include "AliTRDCalibraMode.h"
 #include "AliTRDCalibraVector.h"
 #include "AliTRDCalibraVdriftLinearFit.h"
+#include "AliTRDCalibraExbAltFit.h"
 #include "AliTRDcalibDB.h"
 #include "AliTRDCommonParam.h"
-#include "AliTRDmcmTracklet.h"
-#include "AliTRDmcm.h"
-#include "AliTRDtrigParam.h"
 #include "AliTRDpadPlane.h"
 #include "AliTRDcluster.h"
 #include "AliTRDtrackV1.h"
-#include "AliTRDrawStreamTB.h"
 #include "AliRawReader.h"
 #include "AliRawReaderDate.h"
 #include "AliTRDgeometry.h"
+#include "AliTRDfeeParam.h"
 #include "./Cal/AliTRDCalROC.h"
+#include "./Cal/AliTRDCalPad.h"
 #include "./Cal/AliTRDCalDet.h"
 
+#include "AliTRDdigitsManager.h"
+#include "AliTRDdigitsParam.h"
+#include "AliTRDSignalIndex.h"
+#include "AliTRDarrayADC.h"
+
+#include "AliTRDrawStream.h"
+
+#include "AliCDBEntry.h"
+#include "AliCDBManager.h"
+
 #ifdef ALI_DATE
 #include "event.h"
 #endif
@@ -120,7 +131,8 @@ void AliTRDCalibraFillHisto::Terminate()
 AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   :TObject()
   ,fGeo(0)
-  ,fMcmCorrectAngle(kFALSE)
+  ,fCalibDB(0)
+  ,fIsHLT(kFALSE)
   ,fCH2dOn(kFALSE)
   ,fPH2dOn(kFALSE)
   ,fPRF2dOn(kFALSE)
@@ -128,28 +140,51 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   ,fVector2d(kFALSE)
   ,fLinearFitterOn(kFALSE)
   ,fLinearFitterDebugOn(kFALSE)
+  ,fExbAltFitOn(kFALSE)
   ,fRelativeScale(0)
   ,fThresholdClusterPRF2(15.0)
+  ,fLimitChargeIntegration(kFALSE)
+  ,fFillWithZero(kFALSE)
+  ,fNormalizeNbOfCluster(kFALSE)
+  ,fMaxCluster(0)
+  ,fNbMaxCluster(0)
+  ,fFirstRunGain(0)
+  ,fVersionGainUsed(0)
+  ,fSubVersionGainUsed(0)
+  ,fFirstRunGainLocal(0)
+  ,fVersionGainLocalUsed(0)
+  ,fSubVersionGainLocalUsed(0)
+  ,fFirstRunVdrift(0)
+  ,fVersionVdriftUsed(0) 
+  ,fSubVersionVdriftUsed(0)
+  ,fFirstRunExB(0)
+  ,fVersionExBUsed(0) 
+  ,fSubVersionExBUsed(0)
   ,fCalibraMode(new AliTRDCalibraMode())
   ,fDebugStreamer(0)
   ,fDebugLevel(0)
   ,fDetectorPreviousTrack(-1)
   ,fMCMPrevious(-1)
   ,fROBPrevious(-1)
-  ,fNumberClusters(18)
+  ,fNumberClusters(1)
   ,fNumberClustersf(30)
+  ,fNumberClustersProcent(0.5)
+  ,fThresholdClustersDAQ(120.0)
+  ,fNumberRowDAQ(2)
+  ,fNumberColDAQ(4)
   ,fProcent(6.0)
   ,fDifference(17)
   ,fNumberTrack(0)
   ,fTimeMax(0)
   ,fSf(10.0)
-  ,fNumberBinCharge(100)
-  ,fNumberBinPRF(40)
-  ,fNgroupprf(0)
+  ,fNumberBinCharge(50)
+  ,fNumberBinPRF(10)
+  ,fNgroupprf(3)
   ,fAmpTotal(0x0)
   ,fPHPlace(0x0)
   ,fPHValue(0x0)
   ,fGoodTracklet(kTRUE)
+  ,fLinearFitterTracklet(0x0)
   ,fEntriesCH(0x0)
   ,fEntriesLinearFitter(0x0)
   ,fCalibraVector(0x0)
@@ -158,6 +193,7 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   ,fCH2d(0x0)
   ,fLinearFitterArray(540)
   ,fLinearVdriftFit(0x0)
+  ,fExbAltFit(0x0)
   ,fCalDetGain(0x0)
   ,fCalROCGain(0x0)
 {
@@ -175,14 +211,15 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   fNumberUsedPh[1]       = 0;
   
   fGeo = new AliTRDgeometry();
-
+  fCalibDB = AliTRDcalibDB::Instance();
 }
 
 //______________________________________________________________________________________
 AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   :TObject(c)
   ,fGeo(0)
-  ,fMcmCorrectAngle(c.fMcmCorrectAngle)
+  ,fCalibDB(0)
+  ,fIsHLT(c.fIsHLT)
   ,fCH2dOn(c.fCH2dOn)
   ,fPH2dOn(c.fPH2dOn)
   ,fPRF2dOn(c.fPRF2dOn)
@@ -190,8 +227,26 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fVector2d(c.fVector2d)
   ,fLinearFitterOn(c.fLinearFitterOn)
   ,fLinearFitterDebugOn(c.fLinearFitterDebugOn)
+  ,fExbAltFitOn(c.fExbAltFitOn)
   ,fRelativeScale(c.fRelativeScale)
   ,fThresholdClusterPRF2(c.fThresholdClusterPRF2)
+  ,fLimitChargeIntegration(c.fLimitChargeIntegration)
+  ,fFillWithZero(c.fFillWithZero)
+  ,fNormalizeNbOfCluster(c.fNormalizeNbOfCluster)
+  ,fMaxCluster(c.fMaxCluster)
+  ,fNbMaxCluster(c.fNbMaxCluster)
+  ,fFirstRunGain(c.fFirstRunGain)
+  ,fVersionGainUsed(c.fVersionGainUsed)
+  ,fSubVersionGainUsed(c.fSubVersionGainUsed)
+  ,fFirstRunGainLocal(c.fFirstRunGainLocal)
+  ,fVersionGainLocalUsed(c.fVersionGainLocalUsed)
+  ,fSubVersionGainLocalUsed(c.fSubVersionGainLocalUsed)
+  ,fFirstRunVdrift(c.fFirstRunVdrift)
+  ,fVersionVdriftUsed(c.fVersionVdriftUsed) 
+  ,fSubVersionVdriftUsed(c.fSubVersionVdriftUsed)
+  ,fFirstRunExB(c.fFirstRunExB)
+  ,fVersionExBUsed(c.fVersionExBUsed) 
+  ,fSubVersionExBUsed(c.fSubVersionExBUsed)
   ,fCalibraMode(0x0)
   ,fDebugStreamer(0)
   ,fDebugLevel(c.fDebugLevel)
@@ -200,6 +255,10 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fROBPrevious(c.fROBPrevious)
   ,fNumberClusters(c.fNumberClusters)
   ,fNumberClustersf(c.fNumberClustersf)
+  ,fNumberClustersProcent(c.fNumberClustersProcent)
+  ,fThresholdClustersDAQ(c.fThresholdClustersDAQ)
+  ,fNumberRowDAQ(c.fNumberRowDAQ)
+  ,fNumberColDAQ(c.fNumberColDAQ)
   ,fProcent(c.fProcent)
   ,fDifference(c.fDifference)
   ,fNumberTrack(c.fNumberTrack)
@@ -212,6 +271,7 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fPHPlace(0x0)
   ,fPHValue(0x0)
   ,fGoodTracklet(c.fGoodTracklet)
+  ,fLinearFitterTracklet(0x0)
   ,fEntriesCH(0x0)
   ,fEntriesLinearFitter(0x0)
   ,fCalibraVector(0x0)
@@ -220,6 +280,7 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fCH2d(0x0)
   ,fLinearFitterArray(540)
   ,fLinearVdriftFit(0x0)
+  ,fExbAltFit(0x0)
   ,fCalDetGain(0x0)
   ,fCalROCGain(0x0)
 {
@@ -243,6 +304,9 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   if(c.fLinearVdriftFit){
     fLinearVdriftFit = new AliTRDCalibraVdriftLinearFit(*c.fLinearVdriftFit);
   }
+  if(c.fExbAltFit){
+    fExbAltFit = new AliTRDCalibraExbAltFit(*c.fExbAltFit);
+  }
 
   if(c.fCalDetGain)  fCalDetGain   = new AliTRDCalDet(*c.fCalDetGain);
   if(c.fCalROCGain)  fCalROCGain   = new AliTRDCalROC(*c.fCalROCGain);
@@ -251,6 +315,13 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
     delete fGeo;
   }
   fGeo = new AliTRDgeometry();
+  fCalibDB = AliTRDcalibDB::Instance();
+
+  fNumberUsedCh[0]       = 0;
+  fNumberUsedCh[1]       = 0;
+  fNumberUsedPh[0]       = 0;
+  fNumberUsedPh[1]       = 0;
+
 }
 
 //____________________________________________________________________________________
@@ -266,6 +337,20 @@ 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(fLinearVdriftFit) delete fLinearVdriftFit;
+  if(fExbAltFit) delete fExbAltFit;
   if (fGeo) {
     delete fGeo;
   }
@@ -291,6 +376,7 @@ void AliTRDCalibraFillHisto::DestroyDebugStreamer()
   //
 
   if ( fDebugStreamer ) delete fDebugStreamer;
+  fDebugStreamer = 0x0;
  
 }
 //_____________________________________________________________________________
@@ -318,7 +404,7 @@ void AliTRDCalibraFillHisto::ClearHistos()
 // calibration with AliTRDtrackV1: Init, Update 
 //////////////////////////////////////////////////////////////////////////////////
 //____________Functions for initialising the AliTRDCalibraFillHisto in the code_________
-Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
+Bool_t AliTRDCalibraFillHisto::Init2Dhistos(Int_t nboftimebin)
 {
   //
   // Init the histograms and stuff to be filled 
@@ -338,13 +424,23 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
   }
 
   // Some parameters
-  fTimeMax = cal->GetNumberOfTimeBins();
-  fSf      = parCom->GetSamplingFrequency();
-  fRelativeScale = 20;
+  if(nboftimebin > 0) fTimeMax = nboftimebin;
+  else fTimeMax = cal->GetNumberOfTimeBinsDCS();
+  if(fTimeMax <= 0) fTimeMax = 30;
+  printf("////////////////////////////////////////////\n");
+  printf("Number of time bins in calibration component %d\n",fTimeMax);
+  printf("////////////////////////////////////////////\n");
+  fSf                 = parCom->GetSamplingFrequency();
+  if(!fNormalizeNbOfCluster) fRelativeScale = 20.0;
+  else fRelativeScale = 1.18;
+  fNumberClustersf    = fTimeMax;
+  fNumberClusters     = (Int_t)(fNumberClustersProcent*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(kTRUE);
+  }
 
   // Calcul Xbins Chambd0, Chamb2
   Int_t ntotal0 = CalculateTotalNumberOfBins(0);
@@ -368,23 +464,10 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
       fCalibraVector->SetDetCha0(k,fCalibraMode->GetDetChamb0(k));
       fCalibraVector->SetDetCha2(k,fCalibraMode->GetDetChamb2(k));
     }
-    TString namech("Nz");
-    namech += fCalibraMode->GetNz(0);
-    namech += "Nrphi";
-    namech += fCalibraMode->GetNrphi(0);
-    fCalibraVector->SetNameCH((const char* ) namech);
-    TString nameph("Nz");
-    nameph += fCalibraMode->GetNz(1);
-    nameph += "Nrphi";
-    nameph += fCalibraMode->GetNrphi(1);
-    fCalibraVector->SetNamePH((const char* ) nameph);
-    TString nameprf("Nz");
-    nameprf += fCalibraMode->GetNz(2);
-    nameprf += "Nrphi";
-    nameprf += fCalibraMode->GetNrphi(2);
-    nameprf += "Ngp";
-    nameprf += fNgroupprf;
-    fCalibraVector->SetNamePRF((const char* ) nameprf);
+    fCalibraVector->SetNzNrphi(0,fCalibraMode->GetNz(0),fCalibraMode->GetNrphi(0));
+    fCalibraVector->SetNzNrphi(1,fCalibraMode->GetNz(1),fCalibraMode->GetNrphi(1));
+    fCalibraVector->SetNzNrphi(2,fCalibraMode->GetNz(2),fCalibraMode->GetNrphi(2));
+    fCalibraVector->SetNbGroupPRF(fNgroupprf);
   }
  
   // Create the 2D histos corresponding to the pad groupCalibration mode
@@ -431,13 +514,25 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
     }
   }
   if (fLinearFitterOn) {
-    //fLinearFitterArray.Expand(540);
-    fLinearFitterArray.SetName("ArrayLinearFitters");
-    fEntriesLinearFitter = new Int_t[540];
-    for(Int_t k = 0; k < 540; k++){
-      fEntriesLinearFitter[k] = 0;
+    if(fLinearFitterDebugOn) {
+      fLinearFitterArray.SetName("ArrayLinearFitters");
+      fEntriesLinearFitter = new Int_t[540];
+      for(Int_t k = 0; k < 540; k++){
+       fEntriesLinearFitter[k] = 0;
+      }
     }
     fLinearVdriftFit = new AliTRDCalibraVdriftLinearFit();
+    TString nameee("Ver");
+    nameee += fVersionExBUsed;
+    nameee += "Subver";
+    nameee += fSubVersionExBUsed;
+    nameee += "FirstRun";
+    nameee += fFirstRunExB;
+    nameee += "Nz";
+    fLinearVdriftFit->SetNameCalibUsed(nameee); 
+  }
+  if(fExbAltFitOn){
+    fExbAltFit = new AliTRDCalibraExbAltFit();
   }
 
   if (fPRF2dOn) {
@@ -454,141 +549,117 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
   return kTRUE;
 
 }
-//____________Offline tracking in the AliTRDtracker____________________________
-Bool_t AliTRDCalibraFillHisto::UpdateHistograms(AliTRDtrack *t)
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////
+Bool_t AliTRDCalibraFillHisto::InitCalDet()
 {
   //
-  // Use AliTRDtrack for the calibration
+  // Init the Gain Cal Det 
   //
 
+  // DB Setting
+  // Get cal
+  AliCDBEntry *entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberGainFactor",fFirstRunGain,fVersionGainUsed,fSubVersionGainUsed);
+  if(!entry) {
+    AliError("No gain det calibration entry found");
+    return kFALSE;
+  }
+  AliTRDCalDet *calDet = (AliTRDCalDet *)entry->GetObject();
+  if(!calDet) {
+    AliError("No calDet gain found");
+    return kFALSE;
+  }
+   
+
+  if( fCalDetGain ){ 
+    fCalDetGain->~AliTRDCalDet();
+    new(fCalDetGain) AliTRDCalDet(*(calDet));
+  }else fCalDetGain = new AliTRDCalDet(*(calDet));
+  
   
-  AliTRDcluster *cl = 0x0;       // pointeur to cluster
-  Int_t index0 = 0;              // index of the first cluster in the current chamber
-  Int_t index1 = 0;              // index of the current cluster in the current chamber
+  // title CH2d
+  TString name("Ver");
+  name += fVersionGainUsed;
+  name += "Subver";
+  name += fSubVersionGainUsed;
+  name += "FirstRun";
+  name += fFirstRunGain;
+  name += "Nz";
+  name += fCalibraMode->GetNz(0);
+  name += "Nrphi";
+  name += fCalibraMode->GetNrphi(0);
 
-  //////////////////////////////  
-  // loop over the clusters
-  ///////////////////////////////
-  while((cl = t->GetCluster(index1))){
+  fCH2d->SetTitle(name);  
+  
+  // title PH2d
+  TString namee("Ver");
+  namee += fVersionVdriftUsed;
+  namee += "Subver";
+  namee += fSubVersionVdriftUsed;
+  namee += "FirstRun";
+  namee += fFirstRunVdrift;
+  namee += "Nz";
+  namee += fCalibraMode->GetNz(1);
+  namee += "Nrphi";
+  namee += fCalibraMode->GetNrphi(1);
+  
+  fPH2d->SetTitle(namee);  
 
-    /////////////////////////////////////////////////////////////////////////
-    // Fill the infos for the previous clusters if not the same detector
-    ////////////////////////////////////////////////////////////////////////
-    Int_t detector = cl->GetDetector();
-    if ((detector != fDetectorPreviousTrack) && 
-       (index0 != index1)) {
-      
-      fNumberTrack++;   
-         
-      //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))) {
-       return kFALSE;
-      }
-      
-      // Fill only if the track doesn't touch a masked pad or doesn't
-      if (fGoodTracklet) {
-       
-       // drift velocity unables to cut bad tracklets 
-       Bool_t  pass = FindP1TrackPHtracklet(t,index0,index1);
-       
-       // Gain calibration
-       if (fCH2dOn) {
-         FillTheInfoOfTheTrackCH(index1-index0);
-       }
-       
-       // PH calibration
-       if (fPH2dOn) {
-         FillTheInfoOfTheTrackPH();    
-       }
-       
-       if(pass && fPRF2dOn) HandlePRFtracklet(t,index0,index1);
-       
-       
-      } // if a good tracklet
-      // reset stuff     
-      ResetfVariablestracklet();
-      index0 = index1;
-   
-    } // Fill at the end the charge
+  // title AliTRDCalibraVdriftLinearFit
+  TString nameee("Ver");
+  nameee += fVersionExBUsed;
+  nameee += "Subver";
+  nameee += fSubVersionExBUsed;
+  nameee += "FirstRun";
+  nameee += fFirstRunExB;
+  nameee += "Nz";
 
+  
+  fLinearVdriftFit->SetNameCalibUsed(nameee);  
 
-    /////////////////////////////////////////////////////////////
-    // Localise and take the calib gain object for the detector
-    ////////////////////////////////////////////////////////////
-    if (detector != fDetectorPreviousTrack) {
-      
-      //Localise the detector
-      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)));
-       
-    }
-    
-    // Reset the detectbjobsor
-    fDetectorPreviousTrack = detector;
 
-    ///////////////////////////////////////
-    // Store the info of the cluster
-    ///////////////////////////////////////
-    Int_t row = cl->GetPadRow();
-    Int_t col = cl->GetPadCol();
-    CheckGoodTracklet(detector,row,col);
-    Int_t     group[2] = {0,0};
-    if(fCH2dOn)  group[0]  = CalculateCalibrationGroup(0,row,col);
-    if(fPH2dOn)  group[1]  = CalculateCalibrationGroup(1,row,col);
-    StoreInfoCHPHtrack(cl,t->GetClusterdQdl(index1),group,row,col);
-         
-    index1++;
 
-  } // while on clusters
+  return kTRUE;
 
-  ///////////////////////////
-  // Fill the last plane
-  //////////////////////////
-  if( index0 != index1 ){
-    
-    fNumberTrack++; 
-    
-    if (fGoodTracklet) {
-      
-      // drift velocity unables to cut bad tracklets 
-      Bool_t  pass = FindP1TrackPHtracklet(t,index0,index1);
-      
-      // Gain calibration
-      if (fCH2dOn) {
-       FillTheInfoOfTheTrackCH(index1-index0);
-      }
-      
-      // PH calibration
-      if (fPH2dOn) {
-       FillTheInfoOfTheTrackPH();    
-      }
-      
-      if(pass && fPRF2dOn) HandlePRFtracklet(t,index0,index1);
-          
-    } // if a good tracklet
-    
+}
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////
+Bool_t AliTRDCalibraFillHisto::InitCalPad(Int_t detector)
+{
+  //
+  // Init the Gain Cal Pad 
+  //
+
+  // DB Setting
+  // Get cal
+  AliCDBEntry *entry = AliCDBManager::Instance()->Get("TRD/Calib/LocalGainFactor",AliCDBManager::Instance()->GetRun(),fVersionGainLocalUsed,fSubVersionGainLocalUsed);
+  if(!entry) {
+    AliError("No gain pad calibration entry found");
+    return kFALSE;
   }
+  AliTRDCalPad *calPad = (AliTRDCalPad *)entry->GetObject();
+  if(!calPad) {
+    AliError("No calPad gain found");
+    return kFALSE;
+  }
+  AliTRDCalROC *calRoc = (AliTRDCalROC *)calPad->GetCalROC(detector);
+  if(!calRoc) {
+    AliError("No calRoc gain found");
+    return kFALSE;
+  }
+  
+  if( fCalROCGain ){ 
+    fCalROCGain->~AliTRDCalROC();
+    new(fCalROCGain) AliTRDCalROC(*(calRoc));
+  }else fCalROCGain = new AliTRDCalROC(*(calRoc));
+  
 
-  // reset stuff     
-  ResetfVariablestracklet();
-   
-  return kTRUE;
   
+  
+  return kTRUE;
+
 }
 //____________Offline tracking in the AliTRDtracker____________________________
-Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
+Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(const AliTRDtrackV1 *t)
 {
   //
   // Use AliTRDtrackV1 for the calibration
@@ -596,8 +667,23 @@ 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
+  AliTRDcluster *cls     = 0x0;                // shared 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;
+  }
+*/
+  if (!fCalibDB) {
+    AliInfo("Could not get calibDB");
+    return kFALSE;
+  }
+
   
   ///////////////////////////
   // loop over the tracklet
@@ -612,57 +698,60 @@ 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(!fIsHLT) InitCalPad(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<AliTRDseedV1::kNtb; 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);
+      // Add the charge if shared cluster
+      cls = tracklet->GetClusters(jc+AliTRDseedV1::kNtb);
+      //
+      StoreInfoCHPHtrack(cl, tracklet->GetQperTB(jc),group,row,col,cls); //tracklet->GetdQdl(jc)
     }
     
     ////////////////////////////////////////
     // Fill the stuffs if a good tracklet
     ////////////////////////////////////////
     if (fGoodTracklet) {
-      
+
       // drift velocity unables to cut bad tracklets 
       Bool_t  pass = FindP1TrackPHtrackletV1(tracklet, nbclusters);
        
+      //printf("pass %d and nbclusters %d\n",pass,nbclusters);
+
       // Gain calibration
       if (fCH2dOn) {
        FillTheInfoOfTheTrackCH(nbclusters);
@@ -676,10 +765,8 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
       if(pass && fPRF2dOn) HandlePRFtrackletV1(tracklet,nbclusters);
                
     } // if a good tracklet
   }
-
-
+  
   return kTRUE;
   
 }
@@ -687,144 +774,6 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
 // Routine inside the update with AliTRDtrack
 ///////////////////////////////////////////////////////////////////////////////////
 //____________Offine tracking in the AliTRDtracker_____________________________
-Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index0, Int_t index1)
-{
-  //
-  // Drift velocity calibration:
-  // Fit the clusters with a straight line
-  // From the slope find the drift velocity
-  //
-
-  //Number of points: if less than 3 return kFALSE
-  Int_t npoints = index1-index0;
-  if(npoints <= 2) return kFALSE;
-
-  /////////////////
-  //Variables
-  ////////////////
-  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 tnp                        = 0.0;                                // tan angle in the plan xy track
-  if( TMath::Abs(snp) <  1.){
-    tnp = snp / (TMath::Sqrt(1-(snp*snp)));
-  } 
-  Float_t dzdx                        = tgl*TMath::Sqrt(1+tnp*tnp);         // dz/dx now from dz/dl
-  // 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));
-  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
-  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
-  ////////////////////////////
-  for(Int_t k = 0; k < npoints; k++){
-    
-    AliTRDcluster *cl                 = (AliTRDcluster *) t->GetCluster(k+index0);
-    if(!cl->IsInChamber()) continue;
-    Double_t ycluster                 = cl->GetY();
-    Int_t time                        = cl->GetPadTime();
-    Double_t timeis                   = time/fSf;
-    //Double_t q                        = cl->GetQ();
-    //See if cross two pad rows
-    Int_t    row                      = cl->GetPadRow();
-    if(k==0) rowp                     = row;
-    if(row != rowp) crossrow          = 1;
-
-    linearFitterTracklet.AddPoint(&timeis,ycluster,1);
-    nbli++;
-
-  }
-
-  //////////////////////////////
-  // linear fit
-  //////////////////////////////
-  if(nbli <= 2) return kFALSE; 
-  TVectorD pars;
-  linearFitterTracklet.Eval();
-  linearFitterTracklet.GetParameters(pars);
-  pointError  =  TMath::Sqrt(linearFitterTracklet.GetChisquare()/nbli);
-  errorpar    =  linearFitterTracklet.GetParError(1)*pointError;
-  dydt        = pars[1]; 
-
-  /////////////////////////////
-  // debug
-  ////////////////////////////
-  if(fDebugLevel > 0){
-    if ( !fDebugStreamer ) {
-      //debug stream
-      TDirectory *backup = gDirectory;
-      fDebugStreamer = new TTreeSRedirector("TRDdebugCalibraFill.root");
-      if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
-    } 
-    
-        
-    (* fDebugStreamer) << "FindP1TrackPHtracklet0"<<
-      //"snpright="<<snpright<<
-      "npoints="<<npoints<<
-      "nbli="<<nbli<<
-      "detector="<<detector<<
-      "snp="<<snp<<
-      "tnp="<<tnp<<
-      "tgl="<<tgl<<
-      "tnt="<<tnt<<
-      "dydt="<<dydt<<
-      "dzdx="<<dzdx<<
-      "crossrow="<<crossrow<<
-      "errorpar="<<errorpar<<
-      "pointError="<<pointError<<
-      "\n";
-
-
-    Int_t nbclusters = index1-index0;
-    Int_t plane      = GetPlane(fDetectorPreviousTrack);
-
-    (* fDebugStreamer) << "FindP1TrackPHtracklet1"<<
-      //"snpright="<<snpright<<
-      "nbclusters="<<nbclusters<<
-      "detector="<<fDetectorPreviousTrack<<
-      "plane="<<plane<<
-      "\n";     
-
-  }
-  
-  ///////////////////////////
-  // quality cuts
-  ///////////////////////////
-  if(npoints < fNumberClusters) return kFALSE;
-  if(npoints > fNumberClustersf) return kFALSE;
-  if(pointError >= 0.1) return kFALSE;
-  if(crossrow == 1) return kFALSE;
-
-  ////////////////////////////
-  // fill
-  ////////////////////////////
-  if(fLinearFitterOn){
-    //Add to the linear fitter of the detector
-    if( TMath::Abs(snp) <  1.){
-      Double_t x = tnp-dzdx*tnt; 
-      (GetLinearFitter(detector,kTRUE))->AddPoint(&x,dydt);
-      if(fLinearFitterDebugOn) {
-       fLinearVdriftFit->Update(detector,x,pars[1]);
-      }
-      fEntriesLinearFitter[detector]++;
-    }
-  }
-  
-  return kTRUE;
-}
-//____________Offine tracking in the AliTRDtracker_____________________________
 Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *tracklet, Int_t nbclusters)
 {
   //
@@ -841,7 +790,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 +798,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
@@ -861,7 +808,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   Double_t snp = tracklet->GetSnp();             // sin dy/dx at the end of the chamber
   Double_t tnp = 0.0;                            // dy/dx at the end of the chamber 
   if( TMath::Abs(snp) <  1.){
-    tnp = snp / (TMath::Sqrt(1-(snp*snp)));
+    tnp = snp / TMath::Sqrt((1.-snp)*(1.+snp));
   } 
   Double_t tgl  = tracklet->GetTgl();           // dz/dl
   Double_t dzdx = tgl*TMath::Sqrt(1+tnp*tnp);   // dz/dx calculated from dz/dl
@@ -879,394 +826,162 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   ////////////////////////////
   Int_t  nbli = 0;
   AliTRDcluster *cl                   = 0x0;
-  for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
-    if(!(cl = tracklet->GetClusters(ic))) continue;
-    if(!cl->IsInChamber()) continue;
-    
-    Double_t ycluster                 = cl->GetY();
-    Int_t time                        = cl->GetPadTime();
-    Double_t timeis                   = time/fSf;
-    //See if cross two pad rows
-    Int_t    row                      = cl->GetPadRow();
-    if(rowp==-1) rowp                 = row;
-    if(row != rowp) crossrow          = 1;
-
-    linearFitterTracklet.AddPoint(&timeis,ycluster,1);
-    nbli++;  
-
-  }
-  
-  ////////////////////////////////////
-  // Do the straight line fit now
-  ///////////////////////////////////
-  TVectorD pars;
-  linearFitterTracklet.Eval();
-  linearFitterTracklet.GetParameters(pars);
-  pointError  =  TMath::Sqrt(linearFitterTracklet.GetChisquare()/nbli);
-  errorpar    =  linearFitterTracklet.GetParError(1)*pointError;
-  dydt        = pars[1]; 
-  ////////////////////////////////
-  // Debug stuff
-  /////////////////////////////// 
-
-
-  if(fDebugLevel > 0){
-    if ( !fDebugStreamer ) {
-      //debug stream
-      TDirectory *backup = gDirectory;
-      fDebugStreamer = new TTreeSRedirector("TRDdebugCalibraFill.root");
-      if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
-    } 
-    
-
-    Int_t plane = GetPlane(fDetectorPreviousTrack);
-        
-    (* fDebugStreamer) << "FindP1TrackPHtrackletV1"<<
-      //"snpright="<<snpright<<
-      "nbclusters="<<nbclusters<<
-      "detector="<<fDetectorPreviousTrack<<
-      "plane="<<plane<<
-      "snp="<<snp<<
-      "tnp="<<tnp<<
-      "tgl="<<tgl<<
-      "tnt="<<tnt<<
-      "dydt="<<dydt<<
-      "dzdx="<<dzdx<<
-      "crossrow="<<crossrow<<
-      "errorpar="<<errorpar<<
-      "pointError="<<pointError<<
-      "\n";
-
-  }
-  
-  /////////////////////////
-  // Cuts quality
-  ////////////////////////
-
-  if(nbclusters < fNumberClusters) return kFALSE;
-  if(nbclusters > fNumberClustersf) return kFALSE;
-  if(pointError >= 0.1) return kFALSE;
-  if(crossrow == 1) return kFALSE;
-  
-  ///////////////////////
-  // Fill
-  //////////////////////
-
-  if(fLinearFitterOn){
-    //Add to the linear fitter of the detector
-    if( TMath::Abs(snp) <  1.){
-      Double_t x = tnp-dzdx*tnt; 
-      (GetLinearFitter(fDetectorPreviousTrack,kTRUE))->AddPoint(&x,dydt);
-      if(fLinearFitterDebugOn) {
-       fLinearVdriftFit->Update(fDetectorPreviousTrack,x,pars[1]);
-      }
-      fEntriesLinearFitter[fDetectorPreviousTrack]++;
-    }
-  }
-  
-  return kTRUE;
-}
-//____________Offine tracking in the AliTRDtracker_____________________________
-Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, Int_t index1)
-{
-  //
-  // PRF width calibration
-  // Assume a Gaussian shape: determinate the position of the three pad clusters
-  // Fit with a straight line
-  // Take the fitted values for all the clusters (3 or 2 pad clusters)
-  // Fill the PRF as function of angle of the track
-  //
-  //
-
-
-  //////////////////////////
-  // variables
-  /////////////////////////
-  Int_t npoints  = index1-index0;                                           // number of total points
-  Int_t nb3pc    = 0;                                                       // number of three pads clusters used for fit 
-  Int_t detector = ((AliTRDcluster *) t->GetCluster(index0))->GetDetector(); // detector
-  // To see the difference due to the fit
-  Double_t *padPositions;
-  padPositions = new Double_t[npoints];
-  for(Int_t k = 0; k < npoints; k++){
-    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
-  Float_t  dzdx = 0.0;                                // dzdx
-  Float_t  tnp  = 0.0;
-  if(TMath::Abs(snp) < 1.0){
-    tnp = snp / (TMath::Sqrt(1-snp*snp));
-    dzdx = tgl*TMath::Sqrt(1+tnp*tnp);
-  }
-  // linear fitter
-  TLinearFitter fitter(2,"pol1");
-  fitter.StoreData(kFALSE);
-  fitter.ClearPoints();
-
-
-  ///////////////////////////
-  // calcul the tnp group
-  ///////////////////////////
-  Bool_t echec   = kFALSE;
-  Double_t shift = 0.0;
-  //Calculate the shift in x coresponding to this tnp
-  if(fNgroupprf != 0.0){
-    shift      = -3.0*(fNgroupprf-1)-1.5;
-    Double_t limithigh  = -0.2*(fNgroupprf-1);
-    if((tnp < (-0.2*fNgroupprf)) || (tnp > (0.2*fNgroupprf))) echec = kTRUE;
-    else{
-      while(tnp > limithigh){
-       limithigh += 0.2;
-       shift += 3.0;
-      }
-    }
+  //////////////////////////////
+  // Check no shared clusters
+  //////////////////////////////
+  for(int icc=AliTRDseedV1::kNtb; icc<AliTRDseedV1::kNclusters; icc++){
+    cl = tracklet->GetClusters(icc);
+    if(cl)  crossrow = 1;
   }
-  if(echec) return kFALSE;
-  
-
-  //////////////////////
-  // loop clusters
-  /////////////////////
-  for(Int_t k = 0;  k < npoints; k++){
-    //Take the cluster
-    AliTRDcluster *cl  = (AliTRDcluster *) t->GetCluster(k+index0);
-    Short_t  *signals  = cl->GetSignals();
-    Double_t     time  = cl->GetPadTime();
-    //Calculate x if possible 
-    Float_t xcenter    = 0.0;    
-    Bool_t  echec      = 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;
-      // 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) {
-       xcenter = 0.5 * (TMath::Log((Float_t) (((Float_t) signals[4]) / ((Float_t) signals[2]))))
-         / (TMath::Log(((Float_t) (((Float_t) signals[3]) * ((Float_t) signals[3]))) 
-                       / ((Float_t) (((Float_t) signals[2]) * ((Float_t) signals[4])))));
-      }
-      else {
-       echec = kTRUE;
-      }
-    }
-    if(TMath::Abs(xcenter) > 0.5) echec = kTRUE;
-    if(echec) continue;
-    //if no echec: calculate with the position of the pad
-    // Position of the cluster
-    Double_t       padPosition = xcenter +  cl->GetPadCol();
-    padPositions[k]            = padPosition;
-    nb3pc++;
-    fitter.AddPoint(&time, padPosition,1);
-  }//clusters loop
+  //////////////////////////////////
+  // Loop clusters
+  //////////////////////////////////
 
+  Float_t sigArr[AliTRDfeeParam::GetNcol()];
+  memset(sigArr, 0, AliTRDfeeParam::GetNcol()*sizeof(sigArr[0]));
+  Int_t ncl=0, tbf=0, tbl=0;
 
-  /////////////////////////////
-  // fit
-  ////////////////////////////
-  if(nb3pc < 3) return kFALSE;
-  fitter.Eval();
-  TVectorD line(2);
-  fitter.GetParameters(line);
-  Float_t  pointError  = -1.0;
-  pointError  =  TMath::Sqrt(fitter.GetChisquare()/nb3pc);
-  
-  /////////////////////////////////////////////////////
-  // Now fill the PRF: second loop over clusters
-  /////////////////////////////////////////////////////
-  for(Int_t k = 0;  k < npoints; k++){
-    //Take the cluster
-    AliTRDcluster *cl      = (AliTRDcluster *) t->GetCluster(k+index0);
-    Short_t  *signals      = cl->GetSignals();              // signal
-    Double_t     time      = cl->GetPadTime();              // 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 
-    Float_t ycenter        = 0.0;                           // relative center charge
-    Float_t ymin           = 0.0;                           // relative left charge
-    Float_t ymax           = 0.0;                           // relative right charge
-  
-    //Requiere simply two pads clusters at least
-    if(((((Float_t) signals[3]) > 0.0) && (((Float_t) signals[2]) > 0.0)) ||
-       ((((Float_t) signals[3]) > 0.0) && (((Float_t) signals[4]) > 0.0))){
-      Float_t sum     = ((Float_t) signals[2]) + ((Float_t) signals[3]) + ((Float_t) signals[4]);
-      if(sum > 0.0) ycenter = ((Float_t) signals[3])/ sum;
-      if(sum > 0.0) ymin    = ((Float_t) signals[2])/ sum;
-      if(sum > 0.0) ymax    = ((Float_t) signals[4])/ sum; 
-    }
-    
-    //calibration group
-    Int_t     rowcl        = cl->GetPadRow();                           // row of cluster
-    Int_t     colcl        = cl->GetPadCol();                           // col of cluster 
-    Int_t     grouplocal   = CalculateCalibrationGroup(2,rowcl,colcl);  // calcul the corresponding group
-    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  
-    Double_t  xdiff        = dpad;                                      // reconstructed position constant
-    Double_t  x            = dpad;                                      // reconstructed position moved
-    Float_t   ep           = pointError;                                // error of fit
-    Float_t   signal1      = (Float_t)signals[1];                       // signal at the border
-    Float_t   signal3      = (Float_t)signals[3];                       // signal
-    Float_t   signal2      = (Float_t)signals[2];                       // signal
-    Float_t   signal4      = (Float_t)signals[4];                       // signal
-    Float_t   signal5      = (Float_t)signals[5];                       // signal at the border
-   
-    //////////////////////////////
-    // debug
-    /////////////////////////////  
-    if(fDebugLevel > 0){
-      if ( !fDebugStreamer ) {
-       //debug stream
-       TDirectory *backup = gDirectory;
-       fDebugStreamer = new TTreeSRedirector("TRDdebugCalibraFill.root");
-       if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
-      }     
-         
-      
-      x = xdiff;
-      Int_t type=0;
-      Float_t y = ycenter;
-      (* fDebugStreamer) << "HandlePRFtracklet"<<
-       "caligroup="<<caligroup<<
-       "detector="<<detector<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
-       "npoints="<<npoints<<
-       "Np="<<nb3pc<<
-       "ep="<<ep<<
-       "type="<<type<<
-       "snp="<<snp<<
-       "tnp="<<tnp<<
-       "tgl="<<tgl<<  
-       "dzdx="<<dzdx<< 
-       "padPos="<<padPos<<
-       "padPosition="<<padPositions[k]<<
-       "padPosTracklet="<<padPosTracklet<<
-       "x="<<x<<
-       "y="<<y<<           
-       "xcl="<<xcl<<
-       "qcl="<<qcl<<
-       "signal1="<<signal1<<
-       "signal2="<<signal2<<
-       "signal3="<<signal3<<
-       "signal4="<<signal4<<
-       "signal5="<<signal5<<
-       "time="<<time<<
-       "\n";
-      x=-(xdiff+1);
-      y = ymin;
-      type=-1;
-      (* fDebugStreamer) << "HandlePRFtracklet"<<
-       "caligroup="<<caligroup<<
-       "detector="<<detector<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
-       "npoints="<<npoints<<
-       "Np="<<nb3pc<<
-       "ep="<<ep<<
-       "type="<<type<<
-       "snp="<<snp<<
-       "tnp="<<tnp<<
-       "tgl="<<tgl<<  
-       "dzdx="<<dzdx<< 
-       "padPos="<<padPos<<
-       "padPosition="<<padPositions[k]<<
-       "padPosTracklet="<<padPosTracklet<<
-       "x="<<x<<
-       "y="<<y<<
-       "xcl="<<xcl<<
-       "qcl="<<qcl<<
-       "signal1="<<signal1<<
-       "signal2="<<signal2<<
-       "signal3="<<signal3<<
-       "signal4="<<signal4<<
-       "signal5="<<signal5<<
-       "time="<<time<<
-       "\n";
-      x=1-xdiff;
-      y = ymax;
-      type=1;
-      (* fDebugStreamer) << "HandlePRFtracklet"<<
-       "caligroup="<<caligroup<<
-       "detector="<<detector<<
-       "plane="<<plane<<
-       "chamber="<<chamber<<
-       "npoints="<<npoints<<
-       "Np="<<nb3pc<<
-       "ep="<<ep<<
-       "type="<<type<<
-       "snp="<<snp<<
-       "tnp="<<tnp<<   
-       "tgl="<<tgl<<  
-       "dzdx="<<dzdx<< 
-       "padPos="<<padPos<<
-       "padPosition="<<padPositions[k]<<
-       "padPosTracklet="<<padPosTracklet<<
-       "x="<<x<<
-       "y="<<y<<
-       "xcl="<<xcl<<
-       "qcl="<<qcl<<
-       "signal1="<<signal1<<
-       "signal2="<<signal2<<
-       "signal3="<<signal3<<
-       "signal4="<<signal4<<
-       "signal5="<<signal5<<
-       "time="<<time<<
-       "\n";
-      
-    }
+  for(int ic=0; ic<AliTRDseedV1::kNtb; ic++){
+    if(!(cl = tracklet->GetClusters(ic))) continue;
 
-    ////////////////////////////
-    // quality cuts
-    ///////////////////////////
-    if(npoints < fNumberClusters) continue;
-    if(npoints > fNumberClustersf) continue;
-    if(nb3pc <= 5) continue;
-    if((time >= 21) || (time < 7)) continue;
-    if(TMath::Abs(snp) >= 1.0) continue;
-    if(TMath::Abs(qcl) < 80) continue; 
-   
-    ////////////////////////////
-    // Fill
-    ///////////////////////////
-    if (fHisto2d) {
-      if(TMath::Abs(dpad) < 1.5) {
-       fPRF2d->Fill(shift+dpad,(caligroup+0.5),ycenter);
-       fPRF2d->Fill(shift-dpad,(caligroup+0.5),ycenter);
-      }
-      if((ymin > 0.0) && (TMath::Abs(dpad+1.0) < 1.5)) {
-       fPRF2d->Fill(shift-(dpad+1.0),(caligroup+0.5),ymin);
-       fPRF2d->Fill(shift+(dpad+1.0),(caligroup+0.5),ymin);
-      }
-      if((ymax > 0.0) && (TMath::Abs(dpad-1.0) < 1.5)) {
-       fPRF2d->Fill(shift+1.0-dpad,(caligroup+0.5),ymax);
-       fPRF2d->Fill(shift-1.0+dpad,(caligroup+0.5),ymax);
-      }
+    if(!tbf) tbf=ic;
+    tbl=ic;
+    ncl++;
+    Int_t col = cl->GetPadCol();
+    for(int ip=-1, jp=2; jp<5; ip++, jp++){
+      Int_t idx=col+ip;
+      if(idx<0 || idx>=AliTRDfeeParam::GetNcol()) continue;
+      sigArr[idx]+=((Float_t)cl->GetSignals()[jp]);
     }
-    if (fVector2d) {
-      if(TMath::Abs(dpad) < 1.5) {
-       fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift+dpad,ycenter);
-       fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift-dpad,ycenter);
-      }
-      if((ymin > 0.0) && (TMath::Abs(dpad+1.0) < 1.5)) {
-       fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift-(dpad+1.0),ymin);
-       fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift+(dpad+1.0),ymin);
-      }
-      if((ymax > 0.0)  && (TMath::Abs(dpad-1.0) < 1.5)) {
-       fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift+1.0-dpad,ymax);
-       fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift-1.0+dpad,ymax);
+
+    if((fLimitChargeIntegration) && (!cl->IsInChamber())) continue;
+    
+    Double_t ycluster                 = cl->GetY();
+    Int_t time                        = cl->GetPadTime();
+    Double_t timeis                   = time/fSf;
+    //See if cross two pad rows
+    Int_t    row                      = cl->GetPadRow();
+    if(rowp==-1) rowp                 = row;
+    if(row != rowp) crossrow          = 1;
+
+    fLinearFitterTracklet->AddPoint(&timeis,ycluster,1);
+    nbli++;  
+
+    
+  }
+  
+  ////////////////////////////////////
+  // Do the straight line fit now
+  ///////////////////////////////////
+  if(nbli <= 2){ 
+    fLinearFitterTracklet->ClearPoints();  
+    return kFALSE; 
+  }
+  TVectorD pars;
+  fLinearFitterTracklet->Eval();
+  fLinearFitterTracklet->GetParameters(pars);
+  pointError  =  TMath::Sqrt(TMath::Abs(fLinearFitterTracklet->GetChisquare()/(nbli-2)));
+  errorpar    =  fLinearFitterTracklet->GetParError(1)*pointError;
+  dydt        = pars[1]; 
+  //printf("chis %f, nbli %d, pointError %f, parError %f, errorpar %f\n",fLinearFitterTracklet->GetChisquare(),nbli,pointError,fLinearFitterTracklet->GetParError(1),errorpar);
+  fLinearFitterTracklet->ClearPoints();  
+
+  ////////////////////////////////////
+  // Calc the projection of the clusters on the y direction
+  ///////////////////////////////////
+
+  Float_t signalSum(0.);
+  Float_t mean = 0.0, rms = 0.0;
+  Float_t dydx = tracklet->GetYref(1), tilt = tracklet->GetTilt(); // ,dzdx = tracklet->GetZref(1); (identical to the previous definition!)
+  Float_t dz = dzdx*(tbl-tbf)/10;
+  if(ncl>10){
+    for(Int_t ip(0); ip<AliTRDfeeParam::GetNcol(); ip++){
+      signalSum+=sigArr[ip]; 
+      mean+=ip*sigArr[ip];
+    } 
+    if(signalSum > 0.0) mean/=signalSum;
+  
+    for(Int_t ip = 0; ip<AliTRDfeeParam::GetNcol(); ip++) 
+      rms+=sigArr[ip]*(ip-mean)*(ip-mean);
+    
+    if(signalSum > 0.0) rms = TMath::Sqrt(TMath::Abs(rms/signalSum));
+    
+    rms -= TMath::Abs(dz*tilt);
+    dydx -= dzdx*tilt;
+  }
+  ////////////////////////////////
+  // Debug stuff
+  /////////////////////////////// 
+
+
+  //if(fDebugLevel > 0){
+    if ( !fDebugStreamer ) {
+      //debug stream
+      TDirectory *backup = gDirectory;
+      fDebugStreamer = new TTreeSRedirector("TRDdebugCalibraFill.root");
+      if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
+    } 
+    
+    float xcoord = tnp-dzdx*tnt;
+    float pt = tracklet->GetPt();
+    Int_t layer = GetLayer(fDetectorPreviousTrack);
+           
+    (* fDebugStreamer) << "FindP1TrackPHtrackletV1"<<
+      //"snpright="<<snpright<<
+      "nbli="<<nbli<<
+      "nbclusters="<<nbclusters<<
+      "detector="<<fDetectorPreviousTrack<<
+      "layer="<<layer<<
+      "snp="<<snp<<
+      "tnp="<<tnp<<
+      "tgl="<<tgl<<
+      "tnt="<<tnt<<
+      "dydt="<<dydt<<
+      "dzdx="<<dzdx<<
+      "crossrow="<<crossrow<<
+      "errorpar="<<errorpar<<
+      "pointError="<<pointError<<
+      "xcoord="<<xcoord<<
+      "pt="<<pt<<
+      "rms="<<rms<<
+      "dydx="<<dydx<<
+      "dz="<<dz<<
+      "tilt="<<tilt<<
+      "ncl="<<ncl<<
+      "\n";
+
+    //}
+  
+  /////////////////////////
+  // Cuts quality
+  ////////////////////////
+  
+  if(nbclusters < fNumberClusters) return kFALSE;
+  if(nbclusters > fNumberClustersf) return kFALSE;
+  if(pointError >= 0.3) return kFALSE;
+  if(crossrow == 1) return kTRUE;
+  
+  ///////////////////////
+  // Fill
+  //////////////////////
+
+  if(fLinearFitterOn){
+    //Add to the linear fitter of the detector
+    if( TMath::Abs(snp) <  1.){
+      Double_t x = tnp-dzdx*tnt; 
+      if(fLinearFitterDebugOn) {
+       (GetLinearFitter(fDetectorPreviousTrack,kTRUE))->AddPoint(&x,dydt);
+       fEntriesLinearFitter[fDetectorPreviousTrack]++;
       }
+      fLinearVdriftFit->Update(fDetectorPreviousTrack,x,pars[1]);
     }
   }
-  return kTRUE;
+  if(fExbAltFitOn){
+    fExbAltFit->Update(fDetectorPreviousTrack,dydx,rms);
+  }
   
+  return kTRUE;
 }
 //____________Offine tracking in the AliTRDtracker_____________________________
 Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet, Int_t nbclusters)
@@ -1280,6 +995,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   //
   //
 
+  //printf("begin\n");
   ///////////////////////////////////////////
   // Take the parameters of the track
   //////////////////////////////////////////
@@ -1287,7 +1003,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   Double_t snp = tracklet->GetSnp();             // sin dy/dx at the end of the chamber
   Double_t tnp = 0.0;                            // dy/dx at the end of the chamber 
   if( TMath::Abs(snp) <  1.){
-    tnp = snp / (TMath::Sqrt(1-(snp*snp)));
+    tnp = snp / TMath::Sqrt((1.-snp)*(1.+snp));
   } 
   Double_t tgl  = tracklet->GetTgl();           // dz/dl
   Double_t dzdx = tgl*TMath::Sqrt(1+tnp*tnp);   // dz/dx calculated from dz/dl
@@ -1317,6 +1033,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;
 
   ///////////////////////
@@ -1324,27 +1041,29 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   //////////////////////
 
   Int_t nb3pc    = 0;              // number of three pads clusters used for fit 
-  Double_t *padPositions;          // to see the difference between the fit and the 3 pad clusters position
-  padPositions = new Double_t[AliTRDseed::knTimebins];
-  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();
-
+  // to see the difference between the fit and the 3 pad clusters position
+  Double_t padPositions[AliTRDseedV1::kNtb];
+  memset(padPositions, 0, AliTRDseedV1::kNtb*sizeof(Double_t)); 
+  fLinearFitterTracklet->ClearPoints();  
+  
+  //printf("loop clusters \n");
   ////////////////////////////
   // loop over the clusters
   ////////////////////////////
   AliTRDcluster *cl                   = 0x0;
-  for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
-    if(!(cl = tracklet->GetClusters(ic))) continue;
-    
+  for(int ic=0; ic<AliTRDseedV1::kNtb; ic++){
+    // reject shared clusters on pad row
+    if((ic+AliTRDseedV1::kNtb) < AliTRDseedV1::kNclusters) {
+      cl = tracklet->GetClusters(ic+AliTRDseedV1::kNtb);
+      if(cl) continue;
+    }
+    cl = tracklet->GetClusters(ic);
+    if(!cl) continue;
     Double_t     time  = cl->GetPadTime();
     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 +1071,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,44 +1080,56 @@ 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){ 
+    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();  
+  //printf("PRF second loop \n");
   ////////////////////////////////////////////////
   // Fill the PRF: Second loop over clusters
   //////////////////////////////////////////////
-  for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
-    if(!(cl = tracklet->GetClusters(ic))) continue;
+  for(int ic=0; ic<AliTRDseedV1::kNtb; ic++){
+    // reject shared clusters on pad row
+    cl = tracklet->GetClusters(ic+AliTRDseedV1::kNtb);
+    if(((ic+AliTRDseedV1::kNtb) < AliTRDseedV1::kNclusters) && (cl)) continue;
+    //
+    cl = tracklet->GetClusters(ic);
+    if(!cl) continue;
 
     Short_t  *signals      = cl->GetSignals();              // signal
-    Double_t     time      = cl->GetPadTime();              // time bin
+    Double_t     time      = cl->GetPadTime();         // 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 +1157,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 +1168,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 +1188,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 +1218,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 +1248,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<<
@@ -1590,13 +1323,23 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
 
 
   return kTRUE;
-  
 }
 ///////////////////////////////////////////////////////////////////////////////////////
 // Pad row col stuff: see if masked or not
 ///////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________________
-void AliTRDCalibraFillHisto::CheckGoodTracklet(Int_t detector, Int_t row, Int_t col)
+void AliTRDCalibraFillHisto::CheckGoodTrackletV1(const AliTRDcluster *cl)
+{
+  //
+  // See if we are not near a masked pad
+  //
+
+  if(cl->IsMasked()) fGoodTracklet = kFALSE;
+
+  
+}
+//_____________________________________________________________________________
+void AliTRDCalibraFillHisto::CheckGoodTrackletV0(const Int_t detector,const Int_t row,const Int_t col)
 {
   //
   // See if we are not near a masked pad
@@ -1634,8 +1377,8 @@ Bool_t AliTRDCalibraFillHisto::IsPadOn(Int_t detector, Int_t row, Int_t col) con
     return kFALSE;
   }
   
-  if (!cal->IsChamberInstalled(detector)     || 
-       cal->IsChamberMasked(detector)        ||
+  if (!cal->IsChamberGood(detector)     || 
+       cal->IsChamberNoData(detector)        ||
        cal->IsPadMasked(detector,col,row)) {
     return kFALSE;
   }
@@ -1678,6 +1421,22 @@ Int_t AliTRDCalibraFillHisto::CalculateTotalNumberOfBins(Int_t i)
   //
   
   Int_t ntotal = 0;
+
+  // All together
+  if((fCalibraMode->GetNz(i)==100) && (fCalibraMode->GetNrphi(i)==100)){
+    ntotal = 1;
+    AliInfo(Form("Total number of Xbins: %d for i %d",ntotal,i));
+    return ntotal;
+  }
+
+  // Per Supermodule
+  if((fCalibraMode->GetNz(i)==10) && (fCalibraMode->GetNrphi(i)==10)){
+    ntotal = 18;
+    AliInfo(Form("Total number of Xbins: %d for i %d",ntotal,i));
+    return ntotal;
+  }
+
+  // More
   fCalibraMode->ModePadCalibration(2,i);
   fCalibraMode->ModePadFragmentation(0,2,0,i);
   fCalibraMode->SetDetChamb2(i);
@@ -1722,6 +1481,35 @@ void AliTRDCalibraFillHisto::SetNrphi(Int_t i, Short_t Nrphi)
   }
 
 }
+
+//_____________________________________________________________________________
+void AliTRDCalibraFillHisto::SetAllTogether(Int_t i)
+{
+  //
+  // Set the mode of calibration group all together
+  //
+  if(fVector2d == kTRUE) {
+    AliInfo("Can not work with the vector method");
+    return;
+  }
+  fCalibraMode->SetAllTogether(i);
+  
+}
+
+//_____________________________________________________________________________
+void AliTRDCalibraFillHisto::SetPerSuperModule(Int_t i)
+{
+  //
+  // Set the mode of calibration group per supermodule
+  //
+  if(fVector2d == kTRUE) {
+    AliInfo("Can not work with the vector method");
+    return;
+  }
+  fCalibraMode->SetPerSuperModule(i);
+  
+}
+
 //____________Set the pad calibration variables for the detector_______________
 Bool_t AliTRDCalibraFillHisto::LocalisationDetectorXbins(Int_t detector)
 {
@@ -1744,9 +1532,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);
   }
   
@@ -1772,20 +1560,32 @@ void AliTRDCalibraFillHisto::SetNumberGroupsPRF(Short_t numberGroupsPRF)
 // Per tracklet: store or reset the info, fill the histos with the info
 //////////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________________
-void AliTRDCalibraFillHisto::StoreInfoCHPHtrack(AliTRDcluster *cl, Double_t dqdl, Int_t *group, Int_t row, Int_t col)
+void AliTRDCalibraFillHisto::StoreInfoCHPHtrack(const AliTRDcluster *cl,const Double_t dqdl,const Int_t *group,const Int_t row,const Int_t col,const AliTRDcluster *cls)
 {
   //
   // Store the infos in fAmpTotal, fPHPlace and fPHValue
   // Correct from the gain correction before
+  // cls is shared cluster if any
   //
   
+  //printf("StoreInfoCHPHtrack\n");
+
   // time bin of the cluster not corrected
   Int_t    time     = cl->GetPadTime();
-   
+  Float_t  charge   = TMath::Abs(cl->GetQ());  
+  if(cls) {
+    charge += TMath::Abs(cls->GetQ());
+    //printf("AliTRDCalibraFillHisto::Add the cluster charge");
+  }
+
+  //printf("Store::time %d, amplitude %f\n",time,dqdl);
+  
   //Correct for the gain coefficient used in the database for reconstruction
-  Float_t correctthegain = fCalDetGain->GetValue(fDetectorPreviousTrack)*fCalROCGain->GetValue(col,row);
+  Float_t correctthegain = 1.0;
+  if(fIsHLT) correctthegain = fCalDetGain->GetValue(fDetectorPreviousTrack);
+  else correctthegain = fCalDetGain->GetValue(fDetectorPreviousTrack)*fCalROCGain->GetValue(col,row);
   Float_t correction    = 1.0;
-  Float_t normalisation = 6.67;
+  Float_t normalisation = 1.13; //org: 6.67; 1st: 1.056; 2nd: 1.13;
   // we divide with gain in AliTRDclusterizer::Transform...
   if( correctthegain > 0 ) normalisation /= correctthegain;
 
@@ -1795,14 +1595,17 @@ 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())) {
+      //printf("Store::group %d, amplitude %f\n",group[0],correction);
+      fAmpTotal[(Int_t) group[0]] += correction;
+    }
   }
 
   // Fill the fPHPlace and value
   if (fPH2dOn) {
     fPHPlace[time] = group[1];
-    fPHValue[time] = correction;
+    fPHValue[time] = charge;
   }
   
 }
@@ -1845,12 +1648,21 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
   Int_t fd            = -1;   // Premiere zone non nulle
   Float_t totalcharge = 0.0;  // Total charge for the supermodule histo
 
+  //printf("CH2d nbclusters %d, fNumberClusters %d, fNumberClustersf %d\n",nbclusters,fNumberClusters,fNumberClustersf);
+
   if(nbclusters < fNumberClusters) return;
   if(nbclusters > fNumberClustersf) return;
-  
+
+
+  // Normalize with the number of clusters
+  Double_t normalizeCst = fRelativeScale;
+  if(fNormalizeNbOfCluster) normalizeCst = normalizeCst*nbclusters;
+
+  //printf("Number of groups in one detector %d\n",fCalibraMode->GetNfragZ(0)*fCalibraMode->GetNfragRphi(0));
   
   // See if the track goes through different zones
   for (Int_t k = 0; k < fCalibraMode->GetNfragZ(0)*fCalibraMode->GetNfragRphi(0); k++) {
+    //printf("fAmpTotal %f for %d\n",fAmpTotal[k],k);
     if (fAmpTotal[k] > 0.0) {
       totalcharge += fAmpTotal[k];
       nb++;
@@ -1860,6 +1672,7 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
     }
   }
 
+  //printf("CH2d: nb %d, fd %d, calibration group %d, amplitude %f, detector %d\n",nb,fd,fCalibraMode->GetXbins(0),fAmpTotal[fd]/normalizeCst,fDetectorPreviousTrack);
     
   switch (nb)
     { 
@@ -1867,11 +1680,11 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
       fNumberUsedCh[0]++;
       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);
+       FillCH2d(fCalibraMode->GetXbins(0)+fd,fAmpTotal[fd]/normalizeCst);
+       //fCH2d->Fill(fAmpTotal[fd]/normalizeCst,fCalibraMode->GetXbins(0)+fd+0.5);
       }
       if (fVector2d) {
-       fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd,fAmpTotal[fd]/fRelativeScale);
+       fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd,fAmpTotal[fd]/normalizeCst);
       }
       break;
     case 2:
@@ -1880,22 +1693,22 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
        // One of the two very big
        if (fAmpTotal[fd] > fProcent*fAmpTotal[fd+1]) {
          if (fHisto2d) {
-           FillCH2d(fCalibraMode->GetXbins(0)+fd,fAmpTotal[fd]/fRelativeScale);
-           //fCH2d->Fill(fAmpTotal[fd]/fRelativeScale,fCalibraMode->GetXbins(0)+fd+0.5);
+           FillCH2d(fCalibraMode->GetXbins(0)+fd,fAmpTotal[fd]/normalizeCst);
+           //fCH2d->Fill(fAmpTotal[fd]/normalizeCst,fCalibraMode->GetXbins(0)+fd+0.5);
          }
          if (fVector2d) {
-           fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd,fAmpTotal[fd]/fRelativeScale);
+           fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd,fAmpTotal[fd]/normalizeCst);
          }
          fNumberUsedCh[1]++;
          fEntriesCH[fCalibraMode->GetXbins(0)+fd]++;
        }
        if (fAmpTotal[fd+1] > fProcent*fAmpTotal[fd]) {
          if (fHisto2d) {
-           FillCH2d(fCalibraMode->GetXbins(0)+fd+1,fAmpTotal[fd+1]/fRelativeScale);
-           //fCH2d->Fill(fAmpTotal[fd+1]/fRelativeScale,fCalibraMode->GetXbins(0)+fd+1.5);
+           FillCH2d(fCalibraMode->GetXbins(0)+fd+1,fAmpTotal[fd+1]/normalizeCst);
+           //fCH2d->Fill(fAmpTotal[fd+1]/normalizeCst,fCalibraMode->GetXbins(0)+fd+1.5);
          }
          if (fVector2d) {
-           fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd+1,fAmpTotal[fd+1]/fRelativeScale);
+           fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd+1,fAmpTotal[fd+1]/normalizeCst);
          }
          fNumberUsedCh[1]++;
          fEntriesCH[fCalibraMode->GetXbins(0)+fd+1]++;
@@ -1908,24 +1721,24 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
              // One of the two very big
              if (fAmpTotal[fd] > fProcent*fAmpTotal[fd+fCalibraMode->GetNfragZ(0)]) {
                if (fHisto2d) {
-                 FillCH2d(fCalibraMode->GetXbins(0)+fd,fAmpTotal[fd]/fRelativeScale);
-                 //fCH2d->Fill(fAmpTotal[fd]/fRelativeScale,fCalibraMode->GetXbins(0)+fd+0.5);
+                 FillCH2d(fCalibraMode->GetXbins(0)+fd,fAmpTotal[fd]/normalizeCst);
+                 //fCH2d->Fill(fAmpTotal[fd]/normalizeCst,fCalibraMode->GetXbins(0)+fd+0.5);
                }
                if (fVector2d) {
-                 fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd,fAmpTotal[fd]/fRelativeScale);
+                 fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd,fAmpTotal[fd]/normalizeCst);
                }
                fNumberUsedCh[1]++;
                fEntriesCH[fCalibraMode->GetXbins(0)+fd]++;
              }
              if (fAmpTotal[fd+fCalibraMode->GetNfragZ(0)] > fProcent*fAmpTotal[fd]) {
                if (fHisto2d) {
-                 FillCH2d(fCalibraMode->GetXbins(0)+fd+fCalibraMode->GetNfragZ(0),fAmpTotal[fd+fCalibraMode->GetNfragZ(0)]/fRelativeScale);
-                 //fCH2d->Fill(fAmpTotal[fd+fCalibraMode->GetNfragZ(0)]/fRelativeScale,fCalibraMode->GetXbins(0)+fd+fCalibraMode->GetNfragZ(0)+0.5);
+                 FillCH2d(fCalibraMode->GetXbins(0)+fd+fCalibraMode->GetNfragZ(0),fAmpTotal[fd+fCalibraMode->GetNfragZ(0)]/normalizeCst);
+                 //fCH2d->Fill(fAmpTotal[fd+fCalibraMode->GetNfragZ(0)]/normalizeCst,fCalibraMode->GetXbins(0)+fd+fCalibraMode->GetNfragZ(0)+0.5);
                }
                fNumberUsedCh[1]++;
                fEntriesCH[fCalibraMode->GetXbins(0)+fd+fCalibraMode->GetNfragZ(0)]++;
                if (fVector2d) {
-                 fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd+fCalibraMode->GetNfragZ(0),fAmpTotal[fd+fCalibraMode->GetNfragZ(0)]/fRelativeScale);
+                 fCalibraVector->UpdateVectorCH(fDetectorPreviousTrack,fd+fCalibraMode->GetNfragZ(0),fAmpTotal[fd+fCalibraMode->GetNfragZ(0)]/normalizeCst);
                }
              }
            }
@@ -1934,7 +1747,7 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
       }
       break;
     default: break;
-    }
+  }
 }
 //____________Offine tracking in the AliTRDtracker_____________________________
 void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
@@ -1975,6 +1788,18 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
     }
   }
 
+  // See if noise before and after
+  if(fMaxCluster > 0) {
+    if(fPHValue[0] > fMaxCluster) return;
+    if(fTimeMax > fNbMaxCluster) {
+      for(Int_t k = (fTimeMax-fNbMaxCluster); k < fTimeMax; k++){
+       if(fPHValue[k] > fMaxCluster) return;
+      }
+    }
+  }
+
+  //printf("nbclusters %d, low limit %d, high limit %d\n",nbclusters,fNumberClusters,fNumberClustersf);
+
   if(nbclusters < fNumberClusters) return;
   if(nbclusters > fNumberClustersf) return;
   
@@ -1984,11 +1809,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 +1832,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 +1850,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 +1875,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 +1893,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 +1918,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 +1936,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]);
+                 }
                }
              }
            }
@@ -2086,636 +1953,153 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
        }
       }
       break;
-    default: break;
-    } 
-}
-//////////////////////////////////////////////////////////////////////////////////////////
-// DAQ process functions
-/////////////////////////////////////////////////////////////////////////////////////////
-//_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamTB *rawStream, Bool_t nocheck)
-{
-  //
-  // Event Processing loop - AliTRDrawStreamTB
-  // TestBeam 2007 version
-  // 0 timebin problem
-  // 1 no input
-  // 2 input
-  //
-  // Same algorithm as TestBeam but different reader
-  //
-  
-  Int_t withInput = 1;
-  
-  Double_t phvalue[16][144][36];
-  for(Int_t k = 0; k < 36; k++){
-    for(Int_t j = 0; j < 16; j++){
-      for(Int_t c = 0; c < 144; c++){
-       phvalue[j][c][k] = 0.0;
-      }
-    }
-  }
-  
-  fDetectorPreviousTrack = -1;
-  fMCMPrevious           = -1;
-  fROBPrevious           = -1;
-  Int_t nbtimebin = 0;                                        
-  Int_t baseline  = 0;  
-
-
-  if(!nocheck){
-  
-    fTimeMax = 0;
-       
-    while (rawStream->Next()) {
-      
-      Int_t idetector = rawStream->GetDet();                            //  current detector
-      Int_t imcm      = rawStream->GetMCM();                            //  current MCM
-      Int_t irob      = rawStream->GetROB();                            //  current ROB
-      
-      if((fDetectorPreviousTrack != idetector) && (fDetectorPreviousTrack != -1)){
-       
-       // Fill
-       withInput = TMath::Max(FillDAQ(phvalue),withInput);
-
-
-       // reset
-       for(Int_t k = 0; k < 36; k++){
-         for(Int_t j = 0; j < 16; j++){
-           for(Int_t c = 0; c < 144; c++){
-             phvalue[j][c][k] = 0.0;
-           }
-         }
-       }
-      }
-
-      fDetectorPreviousTrack = idetector;
-      fMCMPrevious           = imcm;
-      fROBPrevious           = irob;
-
-      nbtimebin              = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
-      if(nbtimebin == 0) return 0;
-      if((fTimeMax != 0) && (nbtimebin != fTimeMax)) return 0;
-      fTimeMax          = nbtimebin;
-
-      baseline          = rawStream->GetCommonAdditive();                // common additive baseline
-     
-      Int_t iTimeBin    = rawStream->GetTimeBin();                       //  current time bin
-      Int_t *signal     = rawStream->GetSignals();                       //  current ADC signal
-      Int_t  col        = rawStream->GetCol();
-      Int_t row         = rawStream->GetRow();    
-
-      
-      //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++;
-      }
-    }
-    
-    // fill the last one
-    if(fDetectorPreviousTrack != -1){
-
-      // Fill
-      withInput = TMath::Max(FillDAQ(phvalue),withInput);
-      
-      // reset
-      for(Int_t k = 0; k < 36; k++){
-       for(Int_t j = 0; j < 16; j++){
-         for(Int_t c = 0; c < 144; c++){
-           phvalue[j][c][k] = 0.0;
-         }
-       }
-      }
-    }
-    
-  }
-  else{
-
-    while (rawStream->Next()) {
-
-      Int_t idetector = rawStream->GetDet();                            //  current detector
-      Int_t imcm      = rawStream->GetMCM();                            //  current MCM
-      Int_t irob      = rawStream->GetROB();                            //  current ROB
-
-      if((fDetectorPreviousTrack != idetector) && (fDetectorPreviousTrack != -1)){
-
-       // Fill
-       withInput = TMath::Max(FillDAQ(phvalue),withInput);
-       
-       // reset
-       for(Int_t k = 0; k < 36; k++){
-         for(Int_t j = 0; j < 16; j++){
-           for(Int_t c = 0; c < 144; c++){
-             phvalue[j][c][k] = 0.0;
-           }
-         }
-       }
-      }
-      
-      fDetectorPreviousTrack = idetector;
-      fMCMPrevious           = imcm;
-      fROBPrevious           = irob;
-
-      baseline          = rawStream->GetCommonAdditive();                //  common baseline
-      
-      fTimeMax          = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
-      Int_t iTimeBin    = rawStream->GetTimeBin();                       //  current time bin
-      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++;
-      }
-    }
-    
-    // fill the last one
-    if(fDetectorPreviousTrack != -1){
-      
-      // Fill
-      withInput = TMath::Max(FillDAQ(phvalue),withInput);
-
-      // reset
-      for(Int_t k = 0; k < 36; k++){
-       for(Int_t j = 0; j < 16; j++){
-         for(Int_t c = 0; c < 144; c++){
-           phvalue[j][c][k] = 0.0;
-         }
-       }
-      }
-    }
-  }
-  
-  return withInput;
-  
+    default: break;
+    } 
 }
+//////////////////////////////////////////////////////////////////////////////////////////
+// DAQ process functions
+/////////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliTRDrawStreamTB *rawStream, Bool_t nocheck)
-{
+Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliRawReader *rawReader)
+ { //main
   //
-  // Event Processing loop - AliTRDrawStreamTB
-  // Use old AliTRDmcmtracklet code
+  // Event Processing loop - AliTRDrawStream
+  // 
   // 0 timebin problem
   // 1 no input
   // 2 input
+  // Same algorithm as TestBeam but different reader
   //
-  // Algorithm with mcm tracklet
-  //
-  
+
+  AliTRDrawStream *rawStream = new AliTRDrawStream(rawReader);
+
+  AliTRDdigitsManager *digitsManager = new AliTRDdigitsManager(kTRUE);
+  digitsManager->CreateArrays();
+    
   Int_t withInput = 1;
   
-  AliTRDmcm mcm = AliTRDmcm(0);
-  AliTRDtrigParam *param = AliTRDtrigParam::Instance();
-  rawStream->SetSharedPadReadout(kTRUE);
+  Double_t phvalue[16][144][36];
+  for(Int_t k = 0; k < 36; k++){
+    for(Int_t j = 0; j < 16; j++){
+      for(Int_t c = 0; c < 144; c++){
+       phvalue[j][c][k] = 0.0;
+      }
+    }
+  }
   
   fDetectorPreviousTrack = -1;
   fMCMPrevious           = -1;
   fROBPrevious           = -1;
-  Int_t row              = -1;
+  
   Int_t nbtimebin = 0;                                        
-  Int_t baseline  = 0;  
+  Int_t baseline  = 10;  
 
-
-  if(!nocheck){
   
     fTimeMax = 0;
        
-    while (rawStream->Next()) {
-      
-      Int_t idetector = rawStream->GetDet();                            //  current detector
-      Int_t imcm      = rawStream->GetMCM();                            //  current MCM
-      Int_t irob      = rawStream->GetROB();                            //  current ROB
-      row       = rawStream->GetRow();    
-  
-      
-      if(((fDetectorPreviousTrack != idetector) || (fMCMPrevious != imcm) || (fROBPrevious != irob)) && (fDetectorPreviousTrack != -1)){
-       
-       // Fill
-       withInput = TMath::Max(FillDAQ(&mcm),withInput);
-       
-       
-       // reset
-       mcm.Reset();
-       mcm.SetRobId(irob);
-       mcm.SetChaId(idetector);
-       mcm.SetRow(row);
-       mcm.SetColRange(0,21);
-  
-      }
-      if(fDetectorPreviousTrack == -1){
-       
-       mcm.SetRobId(irob);
-       mcm.SetChaId(idetector);
-       mcm.SetRow(row);
-       mcm.SetColRange(0,21);
-
-      }
-
-      fDetectorPreviousTrack = idetector;
-      fMCMPrevious           = imcm;
-      fROBPrevious           = irob;
-
-      nbtimebin              = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
-      if(nbtimebin == 0) return 0;
-      if((fTimeMax != 0) && (nbtimebin != fTimeMax)) return 0;
-      fTimeMax          = nbtimebin;
-      param->SetTimeRange(0,fTimeMax);
-
-      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++;
-      }
-    }
-    
-    // fill the last one
-    if(fDetectorPreviousTrack != -1){
-
-      // Fill
-      withInput = TMath::Max(FillDAQ(&mcm),withInput);
-
-
-      // reset
-      mcm.Reset();
-      mcm.SetRobId(fROBPrevious);
-      mcm.SetChaId(fDetectorPreviousTrack);
-      mcm.SetRow(row);
-      mcm.SetColRange(0,21);
-      
-    }
-    
-  }
-  else{
-
-    while (rawStream->Next()) {
-
-      Int_t idetector = rawStream->GetDet();                            //  current detector
-      Int_t imcm      = rawStream->GetMCM();                            //  current MCM
-      Int_t irob      = rawStream->GetROB();                            //  current ROB
-      row       = rawStream->GetRow();    
-
-      if(((fDetectorPreviousTrack != idetector) || (fMCMPrevious != imcm) || (fROBPrevious != irob)) && (fDetectorPreviousTrack != -1)){
-
-       // Fill
-       withInput = TMath::Max(FillDAQ(&mcm),withInput);
-
-
-       // reset
-       mcm.Reset();
-       mcm.SetRobId(irob);
-       mcm.SetChaId(idetector);
-       mcm.SetRow(row);
-       mcm.SetColRange(0,21);
-
-      }
-      
-      fDetectorPreviousTrack = idetector;
-      fMCMPrevious           = imcm;
-      fROBPrevious           = irob;
-
-      baseline          = rawStream->GetCommonAdditive();                //  common baseline
-      
-      fTimeMax          = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
-      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++;
-      }
-    }
-    
-    // fill the last one
-    if(fDetectorPreviousTrack != -1){
-      
-      // Fill
-      withInput = TMath::Max(FillDAQ(&mcm),withInput);
-      
-      // reset
-      mcm.Reset();
-      mcm.SetRobId(fROBPrevious);
-      mcm.SetChaId(fDetectorPreviousTrack);
-      mcm.SetRow(row);
-      mcm.SetColRange(0,21);
-
-    }
-  }
-  
-  return withInput;
-  
-}
-//_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliRawReader *rawReader, Bool_t nocheck)
-{
-  //
-  //  Event processing loop - AliRawReader
-  //  Testbeam 2007 version
-  //
+    Int_t det    = 0;
+    while ((det = rawStream->NextChamber(digitsManager, NULL, NULL)) >= 0) { //idetector
 
-  AliTRDrawStreamTB rawStream(rawReader);
+      if (digitsManager->GetIndexes(det)->HasEntry()) {//QA
+       //      printf("there is ADC data on this chamber!\n");
 
-  rawReader->Select("TRD");
-
-  return ProcessEventDAQ(&rawStream, nocheck);
-}
-
-//_________________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(
-#ifdef ALI_DATE
-                                              eventHeaderStruct *event,
-                                              Bool_t nocheck
-#else
-                                              eventHeaderStruct* /*event*/,
-                                              Bool_t /*nocheck*/
+       AliTRDarrayADC *digits = (AliTRDarrayADC *) digitsManager->GetDigits(det); //mod
+       if (digits->HasData()) { //array
+         
+         AliTRDSignalIndex   *indexes = digitsManager->GetIndexes(det);
+         if (indexes->IsAllocated() == kFALSE) {
+           AliError("Indexes do not exist!");
+           break;
+         }
+         Int_t iRow  = 0;
+         Int_t iCol  = 0;
+         indexes->ResetCounters();
+         
+         while (indexes->NextRCIndex(iRow, iCol)) { //column,row
+           //printf(" det %d \t row %d \t col %d \t digit\n",det,iRow,iCol);
+           //while (rawStream->Next()) {
            
-#endif 
-                                  )
-{
-  //
-  //  process date event
-  //  Testbeam 2007 version
-  //
-#ifdef ALI_DATE
-    AliRawReader *rawReader = new AliRawReaderDate((void*)event);
-    Int_t result=ProcessEventDAQ(rawReader, nocheck);
-    delete rawReader;
-    return result;
-#else
-    Fatal("AliTRDCalibraFillHisto", "this class was compiled without DATE");
-    return 0;
-#endif
-
-}
-//_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(AliRawReader *rawReader, Bool_t nocheck)
-{
-  //
-  //  Event processing loop - AliRawReader
-  //  use the old mcm traklet code
-  //
-
-  AliTRDrawStreamTB rawStream(rawReader);
-
-  rawReader->Select("TRD");
-
-  return ProcessEventDAQV1(&rawStream, nocheck);
-}
-//_________________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQV1(
-#ifdef ALI_DATE
-                                              eventHeaderStruct *event,
-                                              Bool_t nocheck
-#else
-                                              eventHeaderStruct* /*event*/,
-                                              Bool_t /*nocheck*/
+           Int_t idetector = det;                                             //  current detector
+           //Int_t imcm      = rawStream->GetMCM();                            //  current MCM
+           //Int_t irob      = rawStream->GetROB();                            //  current ROB
            
-#endif 
-                                  )
-{
-  //
-  //  process date event
-  //  use the old mcm tracklet code
-  //
-#ifdef ALI_DATE
-    AliRawReader *rawReader = new AliRawReaderDate((void*)event);
-    Int_t result=ProcessEventDAQV1(rawReader, nocheck);
-    delete rawReader;
-    return result;
-#else
-    Fatal("AliTRDCalibraFillHisto", "this class was compiled without DATE");
-    return 0;
-#endif
-
-}
+         
+           if((fDetectorPreviousTrack != idetector) && (fDetectorPreviousTrack != -1)) {
+             // Fill
+             withInput = TMath::Max(FillDAQ(phvalue),withInput);
+             
+             // reset
+             for(Int_t k = 0; k < 36; k++){
+               for(Int_t j = 0; j < 16; j++){
+                 for(Int_t c = 0; c < 144; c++){
+                   phvalue[j][c][k] = 0.0;
+                 }
+               }
+             }
+           }
+           
+           fDetectorPreviousTrack = idetector;
+           //fMCMPrevious           = imcm;
+           //fROBPrevious           = irob;
+           
+           //    nbtimebin              = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
+           AliTRDdigitsParam *digitParam = (AliTRDdigitsParam *)digitsManager->GetDigitsParam();
+           nbtimebin              = digitParam->GetNTimeBins(det);              //  number of time bins read from data
+           baseline               = digitParam->GetADCbaseline(det);            //  baseline
+           
+           if(nbtimebin == 0) return 0;
+           if((fTimeMax != 0) && (nbtimebin != fTimeMax)) return 0;
+           fTimeMax          = nbtimebin;
+           
+           fNumberClustersf    = fTimeMax;
+           fNumberClusters     = (Int_t)(fNumberClustersProcent*fTimeMax);
+                 
+           
+           for(Int_t itime = 0; itime < nbtimebin; itime++) {
+             //            phvalue[row][col][itime] = signal[itime]-baseline;
+             phvalue[iRow][iCol][itime] = (Short_t)(digits->GetData(iRow,iCol,itime) - baseline);
+             /*if(phvalue[iRow][iCol][itime] >= 20) {
+                printf("----------> phvalue[%d][%d][%d] %d  baseline %d \n",
+                      iRow,
+                      iCol,
+                      itime,
+                      (Short_t)(digits->GetData(iRow,iCol,itime)),
+                      baseline);
+                      }*/
+           }
+           
+         }//column,row
+         
+         // fill the last one
+         if(fDetectorPreviousTrack != -1){
+           
+           // Fill
+           withInput = TMath::Max(FillDAQ(phvalue),withInput);
+           //      printf("\n ---> withinput %d\n\n",withInput);
+           // reset
+           for(Int_t k = 0; k < 36; k++){
+             for(Int_t j = 0; j < 16; j++){
+               for(Int_t c = 0; c < 144; c++){
+                 phvalue[j][c][k] = 0.0;
+               }
+             }
+           }
+         }
+         
+       }//array
+      }//QA
+      digitsManager->ClearArrays(det);
+    }//idetector
+    delete digitsManager;
+
+    delete rawStream;
+    return withInput;
+ }//main
+//_____________________________________________________________________
 //////////////////////////////////////////////////////////////////////////////
 // Routine inside the DAQ process
 /////////////////////////////////////////////////////////////////////////////
 //_______________________________________________________________________
-Int_t AliTRDCalibraFillHisto::FillDAQ(AliTRDmcm *mcm){
-  
-  //
-  // Return 2 if some tracklets are found and used, 1 if nothing
-  //
-
-  Int_t nbev = 0;
-
-  if(mcm->Run()){
-
-    for (Int_t iSeed = 0; iSeed < 4; iSeed++) {
-      
-      if (mcm->GetSeedCol()[iSeed] < 0) {
-       continue;
-      }
-      nbev += TestTracklet(mcm->GetChaId(),mcm->GetRow(),iSeed,mcm);
-    }
-    
-  }
-
-  if(nbev > 0) nbev = 2;
-  else nbev = 1;
-
-  return nbev;
-  
-}
-//__________________________________________________________________________
-Int_t AliTRDCalibraFillHisto::TestTracklet( Int_t idet, Int_t row, Int_t iSeed, AliTRDmcm *mcm){
-  
-  //
-  // Build the tracklet and return if the tracklet if finally used or not (1/0)
-  //
-
-  Int_t nbev = 0;
-
-  AliTRDmcmTracklet mcmtracklet = AliTRDmcmTracklet();  
-  //mcmtracklet.Reset();
-  mcmtracklet.SetDetector(idet);
-  mcmtracklet.SetRow(row);
-  mcmtracklet.SetN(0);
-
-  Int_t iCol, iCol1, iCol2, track[3];
-  iCol = mcm->GetSeedCol()[iSeed];  // 0....20 (MCM)
-  mcm->GetColRange(iCol1,iCol2);   // range in the pad plane
-           
-  Float_t amp[3];
-  for (Int_t iTime = 0; iTime < fTimeMax; iTime++) {
-
-    amp[0] = mcm->GetADC(iCol-1,iTime);
-    amp[1] = mcm->GetADC(iCol  ,iTime);
-    amp[2] = mcm->GetADC(iCol+1,iTime);
-
-    if(mcm->IsCluster(iCol,iTime)) {
-      
-      mcmtracklet.AddCluster(iCol+iCol1,iTime,amp,track);
-      
-    } 
-    else if ((iCol+1+1) < 21) {
-
-      amp[0] = mcm->GetADC(iCol-1+1,iTime);
-      amp[1] = mcm->GetADC(iCol  +1,iTime);
-      amp[2] = mcm->GetADC(iCol+1+1,iTime);
-
-      if(mcm->IsCluster(iCol+1,iTime)) {
-       
-       mcmtracklet.AddCluster(iCol+1+iCol1,iTime,amp,track);
-       
-      }
-      
-    } 
-    
-  } 
-
-
-  nbev = UpdateHistogramcm(&mcmtracklet); 
-
-  return nbev;
-
-}
-//____________Online trackling in AliTRDtrigger________________________________
-Int_t AliTRDCalibraFillHisto::UpdateHistogramcm(AliTRDmcmTracklet *trk)
-{
-  //
-  // Return if the tracklet is finally used or not (1/0) for calibration
-  //
-  
-  Int_t used = 1;
-
-  fGoodTracklet = kTRUE;
-
-  // Localisation of the Xbins involved
-  Int_t idect = trk->GetDetector();
-  Int_t idectrue = trk->GetDetector();
-  idect = 0;
-
-  Int_t nbclusters = trk->GetNclusters();
-  
-  // Eventuelle correction due to track angle in z direction
-  Float_t correction = 1.0;
-  if (fMcmCorrectAngle) {
-    Float_t z = trk->GetRowz();
-    Float_t r = trk->GetTime0();
-    correction = r / TMath::Sqrt((r*r+z*z));
-  }
-
-  //row
-  Int_t row = trk->GetRow();
-
-
-  // Boucle sur les clusters
-  // Condition on number of cluster: don't come from the middle of the detector
-  
-  Double_t amph[36];
-  for(Int_t k =0; k < 36; k++) amph[k]=0.0;
-  Double_t ampTotal = 0.0;
-  
-  for (Int_t icl = 0; icl < trk->GetNclusters(); icl++) {
-    
-    Float_t amp[3] = { 0.0, 0.0, 0.0 };
-    Int_t   time   = trk->GetClusterTime(icl);
-    Int_t   col    = trk->GetClusterCol(icl);
-    
-    CheckGoodTracklet(idect,row,col);
-    
-    amp[0] = trk->GetClusterADC(icl)[0] * correction;
-    amp[1] = trk->GetClusterADC(icl)[1] * correction;
-    amp[2] = trk->GetClusterADC(icl)[2] * correction;
-    
-    ampTotal += (Float_t) (amp[0]+amp[1]+amp[2]);
-    amph[time]=amp[0]+amp[1]+amp[2];
-   
-    if(fDebugLevel > 0){
-      if ( !fDebugStreamer ) {
-       //debug stream
-       TDirectory *backup = gDirectory;
-       fDebugStreamer = new TTreeSRedirector("TRDdebugCalibraFill.root");
-       if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
-      }     
-      
-      Double_t amp0 = amp[0];
-      Double_t amp1 = amp[1];
-      Double_t amp2 = amp[2];
-
-      (* fDebugStreamer) << "UpdateHistogramcm0"<<
-       "nbclusters="<<nbclusters<<
-       "amp0="<<amp0<<
-       "amp1="<<amp1<<
-       "amp2="<<amp2<<
-       "time="<<time<<
-       "col="<<col<<
-       "row="<<row<<
-       "detector="<<idectrue<<
-       "\n"; 
-    }
-  } // Boucle clusters
-
-  if((amph[0] > 100.0) || (!fGoodTracklet) || (trk->GetNclusters() < fNumberClusters) || (trk->GetNclusters() > fNumberClustersf)) used = 0;
-  
-  if (used == 1) {
-    for(Int_t k = 0; k < fTimeMax; k++)  UpdateDAQ(idect,0,0,k,amph[k],fTimeMax); 
-    //((TH2I *)GetCH2d()->Fill(ampTotal/30.0,idect));    
-  } // Condition cut
-
-
-  if(fDebugLevel > 0){
-    if ( !fDebugStreamer ) {
-      //debug stream
-      TDirectory *backup = gDirectory;
-      fDebugStreamer = new TTreeSRedirector("TRDdebugCalibraFill.root");
-      if ( backup ) backup->cd();  //we don't want to be cd'd to the debug streamer
-    }     
-
-    Double_t amph0 = amph[0];
-    Double_t amphlast = amph[fTimeMax-1];
-    Double_t rms      = TMath::RMS(fTimeMax,amph);
-    Int_t    goodtracklet = (Int_t) fGoodTracklet; 
-
-    (* fDebugStreamer) << "UpdateHistogramcm1"<<
-      "nbclusters="<<nbclusters<<
-      "ampTotal="<<ampTotal<<
-      "row="<<row<<
-      "detector="<<idectrue<<
-      "amph0="<<amph0<<
-      "amphlast="<<amphlast<<
-      "goodtracklet="<<goodtracklet<<
-      "rms="<<rms<<
-      "\n"; 
-  }
-
-  return used;
-
-}
-//_______________________________________________________________________
 Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
 
   //
@@ -2727,13 +2111,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;  
 
 
   ///////////////////////////
@@ -2749,9 +2134,9 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
       for (Int_t ic = 2; ic <= 142; ic++)
        {
          Double_t integral = 0;                  
-         for (Int_t ishiftR = 0; ishiftR < 2; ishiftR++)
+         for (Int_t ishiftR = 0; ishiftR < fNumberRowDAQ; ishiftR++)
            {
-             for (Int_t ishiftC = -2; ishiftC < 2; ishiftC++)
+             for (Int_t ishiftC = -fNumberColDAQ; ishiftC < fNumberColDAQ; ishiftC++)
                {
                  if (ir + ishiftR >= 1 && ir + ishiftR <= 16 &&
                      ic + ishiftC >= 1 && ic + ishiftC <= 144)
@@ -2763,42 +2148,54 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
                    } //addsignal
                } //shiftC
            } // shiftR
-         
          if (integralMax < integral)
            {
              imaxRow = ir;
              imaxCol = ic;
              integralMax = integral;
+             
            } // check max integral
        } //ic
     } // ir
 
-  //printf("imaxRow %d, imaxCol %d, fTimeMax %d, integralMax %f\n",imaxRow,imaxCol,fTimeMax, integralMax);
+  //  printf("imaxRow %d, imaxCol %d, fTimeMax %d, integralMax %f\n",imaxRow,imaxCol,fTimeMax, integralMax);
+  //if((imaxRow == 0) || (imaxRow >= 15) || (imaxCol <= 3) || (imaxCol >= 140)) {
+  //  used=1;
+  //  return used;
+  // }
 
-  if((imaxRow == 0) || (imaxCol == 0)) used=1;
-  CheckGoodTracklet(fDetectorPreviousTrack,imaxRow,imaxCol);
-  if(!fGoodTracklet) used = 1;;
+  if(((imaxRow + fNumberRowDAQ) > 16) || (imaxRow == 0) || ((imaxCol - fNumberColDAQ) <= 1) || ((imaxCol + fNumberColDAQ) >= 144)) {
+    used=1;
+    return used;
+  }
+  //CheckGoodTrackletV0(fDetectorPreviousTrack,imaxRow,imaxCol);
+  //if(!fGoodTracklet) used = 1;;
   
   //  /////////////////////////////////////////////////////
   // sum ober 2 row and 4 pad cols for each time bins
   //  ////////////////////////////////////////////////////       
   
   
-  for (Int_t ir = imaxRow - 1; ir < imaxRow + 1; ir++)
+  
+  for (Int_t ishiftR = 0; ishiftR < fNumberRowDAQ; ishiftR++)
     {
-      for (Int_t ic = imaxCol - 2; ic < imaxCol + 2; ic++)
+      for (Int_t ishiftC = -fNumberColDAQ; ishiftC < fNumberColDAQ; ishiftC++)
        {
-         for(Int_t it = 0; it < fTimeMax; it++){
-           sum[it] += phvalue[ir][ic][it];
-         }
-       }//ic
-    }//ir  
+         if (imaxRow + ishiftR >= 1 && imaxRow + ishiftR <= 16 &&
+             imaxCol + ishiftC >= 1 && imaxCol + ishiftC <= 144)
+           { 
+             for(Int_t it = 0; it < fTimeMax; it++){
+               sum[it] += phvalue[imaxRow + ishiftR-1][imaxCol + ishiftC-1][it];
+             } 
+           }
+       } // col shift
+    }// row shift
 
   Int_t nbcl = 0;
   Double_t sumcharge = 0.0;
   for(Int_t it = 0; it < fTimeMax; it++){
     sumcharge += sum[it];
-    if(sum[it] > 20.0) nbcl++;
+    if(sum[it] > fThresholdClustersDAQ)  nbcl++;
   }
 
 
@@ -2831,6 +2228,7 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
       "clustera="<<clustera<<
       "it="<<it<<
       "rms="<<rms<<
+      "nbcl="<<nbcl<<
       "\n"; 
     }
   }
@@ -2838,6 +2236,7 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
   ////////////////////////////////////////////////////////
   // fill
   ///////////////////////////////////////////////////////
+  //printf("fNumberClusters %d, fNumberClustersf %d\n",fNumberClusters,fNumberClustersf);
   if(sum[0] > 100.0) used = 1; 
   if(nbcl < fNumberClusters) used = 1;
   if(nbcl > fNumberClustersf) used = 1;
@@ -2848,12 +2247,21 @@ 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); 
+      } 
+      //if(fFillWithZero) UpdateDAQ(0,0,0,it,sum[it],fTimeMax);
+      //else{
+      // if(sum[it] > 0.0) UpdateDAQ(0,0,0,it,sum[it],fTimeMax);
+      //}
     }
     
    
     //((TH2I *)GetCH2d()->Fill(sumcharge/30.0,fDetectorPreviousTrack));
     used = 2;
+    //printf("Pass Detector %d\n",fDetectorPreviousTrack);
+
   }
  
   return used;
@@ -2927,7 +2335,7 @@ void AliTRDCalibraFillHisto::Write2d(const Char_t *filename, Bool_t append)
     }
   }
   if(fLinearFitterOn){
-    AnalyseLinearFitter();
+    if(fLinearFitterDebugOn) AnalyseLinearFitter();
     f.WriteTObject(fLinearVdriftFit);
   }
    
@@ -3004,20 +2412,15 @@ Double_t *AliTRDCalibraFillHisto::StatH(TH2 *h, Int_t i)
 
     //Debug
     if(i > 1){
-      if((!((Bool_t)nbEntries)) && (nentries > 0)){
-       nbEntries = new TH1F("Number of entries","Number of entries"
-                               ,100,(Int_t)nentries/2,nentries*2);
+      if(nentries > 0){
+       if(!((Bool_t)nbEntries)) nbEntries = new TH1F("Number of entries","Number of entries",100,(Int_t)nentries/2,nentries*2);
        nbEntries->SetDirectory(0);
-       nbEntriesPerGroup = new TH1F("Number of entries per group","Number of entries per group"
-                               ,nbins,0,nbins);
+       nbEntries->Fill(nentries);
+       if(!((Bool_t)nbEntriesPerGroup)) nbEntriesPerGroup = new TH1F("Number of entries per group","Number of entries per group",nbins,0,nbins);
        nbEntriesPerGroup->SetDirectory(0);
-       nbEntriesPerSp = new TProfile("Number of entries per supermodule","Number of entries per supermodule"
-                               ,(Int_t)(nbins/18),0,(Int_t)(nbins/18));
-       nbEntriesPerSp->SetDirectory(0);
-      }
-      if(nbEntries){
-       if(nentries > 0) nbEntries->Fill(nentries);
        nbEntriesPerGroup->Fill(idect+0.5,nentries);
+       if(!((Bool_t)nbEntriesPerSp)) nbEntriesPerSp = new TProfile("Number of entries per supermodule","Number of entries per supermodule",(Int_t)(nbins/18),0,(Int_t)(nbins/18));
+       nbEntriesPerSp->SetDirectory(0);
        nbEntriesPerSp->Fill((idect%((Int_t)(nbins/18)))+0.5,nentries);
       }
     }
@@ -3058,7 +2461,7 @@ Double_t *AliTRDCalibraFillHisto::StatH(TH2 *h, Int_t i)
   info[5] = meanstats;
   info[6] = meanrelativerror;
 
-  if(i > 1){
+  if(nbEntries && nbEntriesPerSp && nbEntriesPerGroup){
     gStyle->SetPalette(1);
     gStyle->SetOptStat(1111);
     gStyle->SetPadBorderMode(0);
@@ -3092,12 +2495,13 @@ Double_t *AliTRDCalibraFillHisto::GetMeanMedianRMSNumberCH()
   // 3 number of group with entries
   //
 
-  Double_t *stat      = new Double_t[4]; 
+  Double_t *stat = new Double_t[4];
   stat[3]             = 0.0;
 
   Int_t    nbofgroups = CalculateTotalNumberOfBins(0);
-  Double_t *weight    = new Double_t[nbofgroups];
-  Int_t    *nonul     = new Int_t[nbofgroups];
+  
+  Double_t *weight = new Double_t[nbofgroups];
+  Double_t *nonul = new Double_t[nbofgroups];
  
   for(Int_t k = 0; k < nbofgroups; k++){
     if(fEntriesCH[k] > 0) {
@@ -3111,6 +2515,9 @@ Double_t *AliTRDCalibraFillHisto::GetMeanMedianRMSNumberCH()
   stat[1]          = TMath::Median(nbofgroups,fEntriesCH,weight); 
   stat[2]          = TMath::RMS((Int_t)stat[3],nonul); 
 
+  delete [] weight;
+  delete [] nonul;
+
   return stat;
 
 }
@@ -3144,6 +2551,9 @@ Double_t *AliTRDCalibraFillHisto::GetMeanMedianRMSNumberLinearFitter() const
   stat[1]          = TMath::Median(nbofgroups,fEntriesLinearFitter,weight); 
   stat[2]          = TMath::RMS((Int_t)stat[3],nonul); 
 
+  delete [] weight;
+  delete [] nonul;
+
   return stat;
 
 }
@@ -3192,7 +2602,13 @@ void AliTRDCalibraFillHisto::CreatePH2d(Int_t nn)
   // Create the 2D histos
   //
 
-  TString name("Nz");
+  TString name("Ver");
+  name += fVersionVdriftUsed;
+  name += "Subver";
+  name += fSubVersionVdriftUsed;
+  name += "FirstRun";
+  name += fFirstRunVdrift;
+  name += "Nz";
   name += fCalibraMode->GetNz(1);
   name += "Nrphi";
   name += fCalibraMode->GetNrphi(1);
@@ -3213,11 +2629,17 @@ void AliTRDCalibraFillHisto::CreateCH2d(Int_t nn)
   // Create the 2D histos
   //
 
-  TString name("Nz");
+  TString name("Ver");
+  name += fVersionGainUsed;
+  name += "Subver";
+  name += fSubVersionGainUsed;
+  name += "FirstRun";
+  name += fFirstRunGain;
+  name += "Nz";
   name += fCalibraMode->GetNz(0);
   name += "Nrphi";
   name += fCalibraMode->GetNrphi(0);
-
+  
   fCH2d = new TH2I("CH2d",(const Char_t *) name
                   ,fNumberBinCharge,0,300,nn,0,nn);
   fCH2d->SetYTitle("Det/pad groups");
@@ -3272,10 +2694,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 +2705,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);
 
 }
 
@@ -3377,16 +2799,23 @@ void AliTRDCalibraFillHisto::AnalyseLinearFitter()
       TVectorD  *par  = new TVectorD(2);
       TVectorD   pare = TVectorD(2);
       TVectorD  *parE = new TVectorD(3);
-      linearfitter->Eval();
-      linearfitter->GetParameters(*par);
-      linearfitter->GetErrors(pare);
-      Float_t  ppointError =  TMath::Sqrt(TMath::Abs(linearfitter->GetChisquare())/fEntriesLinearFitter[k]);
-      (*parE)[0] = pare[0]*ppointError;
-      (*parE)[1] = pare[1]*ppointError;
-      (*parE)[2] = (Double_t) fEntriesLinearFitter[k];
-      ((TObjArray *)fLinearVdriftFit->GetPArray())->AddAt(par,k);
-      ((TObjArray *)fLinearVdriftFit->GetEArray())->AddAt(parE,k);
+      if((linearfitter->EvalRobust(0.8)==0)) {
+       //linearfitter->Eval();
+       linearfitter->GetParameters(*par);
+       //linearfitter->GetErrors(pare);
+       //Float_t  ppointError =  TMath::Sqrt(TMath::Abs(linearfitter->GetChisquare())/fEntriesLinearFitter[k]);
+       //(*parE)[0] = pare[0]*ppointError;
+       //(*parE)[1] = pare[1]*ppointError;
+
+       (*parE)[0] = 0.0;
+       (*parE)[1] = 0.0;
+       (*parE)[2] = (Double_t) fEntriesLinearFitter[k];
+       ((TObjArray *)fLinearVdriftFit->GetPArray())->AddAt(par,k);
+       ((TObjArray *)fLinearVdriftFit->GetEArray())->AddAt(parE,k);
+      }
     }
   }
 }
 
+
+