]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDCalibraFillHisto.cxx
Remove old tracking code
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibraFillHisto.cxx
index 10190f0acc7f618061a2f9a7a0cc907d23a4d26e..1de42df1794894423da446eaaf9bbe59a9b3cf71 100644 (file)
@@ -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 "AliTRDcluster.h"
 #include "AliTRDtrack.h"
 #include "AliTRDtrackV1.h"
-#include "AliTRDrawStreamBase.h"
 #include "AliRawReader.h"
 #include "AliRawReaderDate.h"
 #include "AliTRDgeometry.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
@@ -118,8 +130,8 @@ void AliTRDCalibraFillHisto::Terminate()
 AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   :TObject()
   ,fGeo(0)
+  ,fCalibDB(0)
   ,fIsHLT(kFALSE)
-  ,fMcmCorrectAngle(kFALSE)
   ,fCH2dOn(kFALSE)
   ,fPH2dOn(kFALSE)
   ,fPRF2dOn(kFALSE)
@@ -134,6 +146,12 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   ,fNormalizeNbOfCluster(kFALSE)
   ,fMaxCluster(0)
   ,fNbMaxCluster(0)
+  ,fVersionGainUsed(0)
+  ,fSubVersionGainUsed(0)
+  ,fVersionGainLocalUsed(0)
+  ,fSubVersionGainLocalUsed(0)
+  ,fVersionVdriftUsed(0) 
+  ,fSubVersionVdriftUsed(0)
   ,fCalibraMode(new AliTRDCalibraMode())
   ,fDebugStreamer(0)
   ,fDebugLevel(0)
@@ -142,14 +160,18 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   ,fROBPrevious(-1)
   ,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)
@@ -180,15 +202,15 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   fNumberUsedPh[1]       = 0;
   
   fGeo = new AliTRDgeometry();
-
+  fCalibDB = AliTRDcalibDB::Instance();
 }
 
 //______________________________________________________________________________________
 AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   :TObject(c)
   ,fGeo(0)
+  ,fCalibDB(0)
   ,fIsHLT(c.fIsHLT)
-  ,fMcmCorrectAngle(c.fMcmCorrectAngle)
   ,fCH2dOn(c.fCH2dOn)
   ,fPH2dOn(c.fPH2dOn)
   ,fPRF2dOn(c.fPRF2dOn)
@@ -203,6 +225,12 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fNormalizeNbOfCluster(c.fNormalizeNbOfCluster)
   ,fMaxCluster(c.fMaxCluster)
   ,fNbMaxCluster(c.fNbMaxCluster)
+  ,fVersionGainUsed(c.fVersionGainUsed)
+  ,fSubVersionGainUsed(c.fSubVersionGainUsed)
+  ,fVersionGainLocalUsed(c.fVersionGainLocalUsed)
+  ,fSubVersionGainLocalUsed(c.fSubVersionGainLocalUsed)
+  ,fVersionVdriftUsed(c.fVersionVdriftUsed) 
+  ,fSubVersionVdriftUsed(c.fSubVersionVdriftUsed)
   ,fCalibraMode(0x0)
   ,fDebugStreamer(0)
   ,fDebugLevel(c.fDebugLevel)
@@ -211,6 +239,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)
@@ -263,6 +295,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;
+
 }
 
 //____________________________________________________________________________________
@@ -290,6 +329,7 @@ AliTRDCalibraFillHisto::~AliTRDCalibraFillHisto()
     TLinearFitter *f = (TLinearFitter*)fLinearFitterArray.At(idet);
     if(f) { delete f;}
   }
+  if(fLinearVdriftFit) delete fLinearVdriftFit;
   if (fGeo) {
     delete fGeo;
   }
@@ -343,7 +383,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 
@@ -363,12 +403,17 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
   }
 
   // Some parameters
-  fTimeMax            = cal->GetNumberOfTimeBins();
+  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)(0.5*fTimeMax);
+  fNumberClusters     = (Int_t)(fNumberClustersProcent*fTimeMax);
  
   // Init linear fitter
   if(!fLinearFitterTracklet) {
@@ -376,12 +421,6 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
     fLinearFitterTracklet->StoreData(kTRUE);
   }
 
-  //calib object from database used for reconstruction
-  if( fCalDetGain ){ 
-    fCalDetGain->~AliTRDCalDet();
-    new(fCalDetGain) AliTRDCalDet(*(cal->GetGainFactorDet()));
-  }else fCalDetGain = new AliTRDCalDet(*(cal->GetGainFactorDet()));
-  
   // Calcul Xbins Chambd0, Chamb2
   Int_t ntotal0 = CalculateTotalNumberOfBins(0);
   Int_t ntotal1 = CalculateTotalNumberOfBins(1);
@@ -404,23 +443,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
@@ -467,11 +493,12 @@ 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();
   }
@@ -487,11 +514,103 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos()
     }
   }
 
+  return kTRUE;
+
+}
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////
+Bool_t AliTRDCalibraFillHisto::InitCalDet()
+{
+  //
+  // Init the Gain Cal Det 
+  //
+
+  // DB Setting
+  // Get cal
+  AliCDBEntry *entry = AliCDBManager::Instance()->Get("TRD/Calib/ChamberGainFactor",AliCDBManager::Instance()->GetRun(),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));
+  
+  
+  // title CH2d
+  TString name("Ver");
+  name += fVersionGainUsed;
+  name += "Subver";
+  name += fSubVersionGainUsed;
+  name += "Nz";
+  name += fCalibraMode->GetNz(0);
+  name += "Nrphi";
+  name += fCalibraMode->GetNrphi(0);
+
+  fCH2d->SetTitle(name);  
+  
+  // title PH2d
+  TString namee("Ver");
+  namee += fVersionVdriftUsed;
+  namee += "Subver";
+  namee += fSubVersionVdriftUsed;
+  namee += "Nz";
+  namee += fCalibraMode->GetNz(1);
+  namee += "Nrphi";
+  namee += fCalibraMode->GetNrphi(1);
+  
+  fPH2d->SetTitle(namee);  
+
+
+  return kTRUE;
+
+}
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////
+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));
+  
+
+  
+  
   return kTRUE;
 
 }
 //____________Offline tracking in the AliTRDtracker____________________________
-Bool_t AliTRDCalibraFillHisto::UpdateHistograms(AliTRDtrack *t)
+Bool_t AliTRDCalibraFillHisto::UpdateHistograms(const AliTRDtrack *t)
 {
   //
   // Use AliTRDtrack for the calibration
@@ -566,12 +685,7 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistograms(AliTRDtrack *t)
       }
       
       // Get calib objects
-      if( fCalROCGain ){ 
-       if(!fIsHLT){
-         fCalROCGain->~AliTRDCalROC();
-         new(fCalROCGain) AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
-       }
-      }else fCalROCGain = new AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
+      if(!fIsHLT) InitCalPad(detector);
       
     }
     
@@ -628,7 +742,7 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistograms(AliTRDtrack *t)
   
 }
 //____________Offline tracking in the AliTRDtracker____________________________
-Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
+Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(const AliTRDtrackV1 *t)
 {
   //
   // Use AliTRDtrackV1 for the calibration
@@ -637,14 +751,22 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
   
   const AliTRDseedV1 *tracklet = 0x0;          // tracklet per plane
   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();
+  //  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
@@ -675,13 +797,8 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
       //Localise the detector bin
       LocalisationDetectorXbins(detector);
       // Get calib objects
-      if( fCalROCGain ){ 
-       if(!fIsHLT){    
-         fCalROCGain->~AliTRDCalROC();
-         new(fCalROCGain) AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
-       }
-      }else fCalROCGain = new AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
-      
+      if(!fIsHLT) InitCalPad(detector);        
+            
       // reset
       fDetectorPreviousTrack = detector;
     }
@@ -691,7 +808,7 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
     // loop over the clusters
     ////////////////////////////
     Int_t nbclusters = 0;
-    for(int jc=0; jc<AliTRDseed::knTimebins; jc++){
+    for(int jc=0; jc<AliTRDseedV1::kNtb; jc++){
       if(!(cl = tracklet->GetClusters(jc))) continue;
       nbclusters++;
       
@@ -702,17 +819,22 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(AliTRDtrackV1 *t)
       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(jc),group,row,col);
+      // Add the charge if shared cluster
+      cls = tracklet->GetClusters(jc+AliTRDseedV1::kNtb);
+      //
+      StoreInfoCHPHtrack(cl, tracklet->GetdQdl(jc),group,row,col,cls);
     }
     
     ////////////////////////////////////////
     // 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);
@@ -735,7 +857,7 @@ 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)
+Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(const AliTRDtrack *t, Int_t index0, Int_t index1)
 {
   //
   // Drift velocity calibration:
@@ -855,7 +977,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
   if(npoints < fNumberClusters) return kFALSE;
   if(npoints > fNumberClustersf) return kFALSE;
   if(pointError >= 0.1) return kFALSE;
-  if(crossrow == 1) return kFALSE;
+  if(crossrow == 1) return kTRUE;
 
   ////////////////////////////
   // fill
@@ -864,11 +986,11 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(AliTRDtrack *t, Int_t index
     //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]);
+       (GetLinearFitter(detector,kTRUE))->AddPoint(&x,dydt);
+       fEntriesLinearFitter[detector]++;
       }
-      fEntriesLinearFitter[detector]++;
+      fLinearVdriftFit->Update(detector,x,pars[1]);
     }
   }
   
@@ -927,7 +1049,17 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   ////////////////////////////
   Int_t  nbli = 0;
   AliTRDcluster *cl                   = 0x0;
-  for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
+  //////////////////////////////
+  // Check no shared clusters
+  //////////////////////////////
+  for(int icc=AliTRDseedV1::kNtb; icc<AliTRDseedV1::kNclusters; icc++){
+    cl = tracklet->GetClusters(icc);
+    if(cl)  crossrow = 1;
+  }
+  //////////////////////////////////
+  // Loop clusters
+  //////////////////////////////////
+  for(int ic=0; ic<AliTRDseedV1::kNtb; ic++){
     if(!(cl = tracklet->GetClusters(ic))) continue;
     if((fLimitChargeIntegration) && (!cl->IsInChamber())) continue;
     
@@ -942,6 +1074,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
     fLinearFitterTracklet->AddPoint(&timeis,ycluster,1);
     nbli++;  
 
+    
   }
   
   ////////////////////////////////////
@@ -998,11 +1131,11 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   /////////////////////////
   // Cuts quality
   ////////////////////////
-
+  
   if(nbclusters < fNumberClusters) return kFALSE;
   if(nbclusters > fNumberClustersf) return kFALSE;
   if(pointError >= 0.3) return kFALSE;
-  if(crossrow == 1) return kFALSE;
+  if(crossrow == 1) return kTRUE;
   
   ///////////////////////
   // Fill
@@ -1012,18 +1145,18 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
     //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]);
+       (GetLinearFitter(fDetectorPreviousTrack,kTRUE))->AddPoint(&x,dydt);
+       fEntriesLinearFitter[fDetectorPreviousTrack]++;
       }
-      fEntriesLinearFitter[fDetectorPreviousTrack]++;
+      fLinearVdriftFit->Update(fDetectorPreviousTrack,x,pars[1]);
     }
   }
   
   return kTRUE;
 }
 //____________Offine tracking in the AliTRDtracker_____________________________
-Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, Int_t index1)
+Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t index0, Int_t index1)
 {
   //
   // PRF width calibration
@@ -1087,8 +1220,9 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
   for(Int_t k = 0;  k < npoints; k++){
     //Take the cluster
     AliTRDcluster *cl  = (AliTRDcluster *) t->GetCluster(k+index0);
+    if(!cl) continue;
     Short_t  *signals  = cl->GetSignals();
-    Double_t     time  = cl->GetLocalTimeBin();
+    Double_t     time  = cl->GetPadTime();
     //Calculate x if possible 
     Float_t xcenter    = 0.0;    
     Bool_t  echec1      = kTRUE;   
@@ -1143,8 +1277,8 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(AliTRDtrack *t, Int_t index0, I
   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->GetLocalTimeBin();              // time bin
+    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 
@@ -1388,11 +1522,9 @@ 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;
-  } 
+  // 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");
@@ -1400,10 +1532,15 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   // loop over the clusters
   ////////////////////////////
   AliTRDcluster *cl                   = 0x0;
-  for(int ic=0; ic<AliTRDseed::knTimebins; ic++){
-    if(!(cl = tracklet->GetClusters(ic))) continue;
-    
-    Double_t     time  = cl->GetLocalTimeBin();
+  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;    
@@ -1448,9 +1585,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   // fit with a straight line
   /////////////////////////////
   if(nb3pc < 3){ 
-    delete [] padPositions;
     fLinearFitterTracklet->ClearPoints();  
-    delete [] padPositions;
     return kFALSE;
   }
   fLinearFitterTracklet->Eval();
@@ -1466,11 +1601,16 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   ////////////////////////////////////////////////
   // 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->GetLocalTimeBin();         // 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 
@@ -1663,15 +1803,13 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet,
   } // second loop over clusters
 
 
-  delete [] padPositions;
   return kTRUE;
-  
 }
 ///////////////////////////////////////////////////////////////////////////////////////
 // Pad row col stuff: see if masked or not
 ///////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________________
-void AliTRDCalibraFillHisto::CheckGoodTrackletV1(AliTRDcluster *cl)
+void AliTRDCalibraFillHisto::CheckGoodTrackletV1(const AliTRDcluster *cl)
 {
   //
   // See if we are not near a masked pad
@@ -1682,7 +1820,7 @@ void AliTRDCalibraFillHisto::CheckGoodTrackletV1(AliTRDcluster *cl)
   
 }
 //_____________________________________________________________________________
-void AliTRDCalibraFillHisto::CheckGoodTrackletV0(Int_t detector, Int_t row, Int_t col)
+void AliTRDCalibraFillHisto::CheckGoodTrackletV0(const Int_t detector,const Int_t row,const Int_t col)
 {
   //
   // See if we are not near a masked pad
@@ -1903,16 +2041,26 @@ 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 = 1.0;
   if(fIsHLT) correctthegain = fCalDetGain->GetValue(fDetectorPreviousTrack);
@@ -1929,13 +2077,16 @@ void AliTRDCalibraFillHisto::StoreInfoCHPHtrack(AliTRDcluster *cl, Double_t dqdl
 
   // Fill the fAmpTotal with the charge
   if (fCH2dOn) {
-    if((!fLimitChargeIntegration) || (cl->IsInChamber())) fAmpTotal[(Int_t) group[0]] += correction;
+    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;
   }
   
 }
@@ -1978,6 +2129,8 @@ 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;
 
@@ -1985,9 +2138,12 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
   // 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++;
@@ -1997,6 +2153,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)
     { 
@@ -2284,18 +2441,24 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
 // DAQ process functions
 /////////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamBase *rawStream, Bool_t nocheck)
-{
+Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliRawReader *rawReader)
+ { //main
   //
-  // Event Processing loop - AliTRDrawStreamBase
-  // TestBeam 2007 version
+  // Event Processing loop - AliTRDrawStream
+  // 
   // 0 timebin problem
   // 1 no input
   // 2 input
-  //
   // Same algorithm as TestBeam but different reader
   //
-  
+
+  AliTRDrawStream *rawStream = new AliTRDrawStream(rawReader);
+  rawStream->SetNoErrorWarning();
+  rawStream->SetSharedPadReadout(kFALSE);
+
+  AliTRDdigitsManager *digitsManager = new AliTRDdigitsManager(kTRUE);
+  digitsManager->CreateArrays();
+    
   Int_t withInput = 1;
   
   Double_t phvalue[16][144][36];
@@ -2310,190 +2473,112 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamBase *rawStream, Bo
   fDetectorPreviousTrack = -1;
   fMCMPrevious           = -1;
   fROBPrevious           = -1;
+  
   Int_t nbtimebin = 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
-
-      //printf("Detector %d\n",idetector);
-
-      if((fDetectorPreviousTrack != idetector) && (fDetectorPreviousTrack != -1)){
-       
-       // Fill
-       withInput = TMath::Max(FillDAQ(phvalue),withInput);
+    Int_t det    = 0;
+    while ((det = rawStream->NextChamber(digitsManager, NULL, NULL)) >= 0) { //idetector
 
+      if (digitsManager->GetIndexes(det)->HasEntry()) {//QA
+       //      printf("there is ADC data on this chamber!\n");
 
-       // 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
-      fNumberClustersf    = fTimeMax;
-      fNumberClusters     = (Int_t)(0.6*fTimeMax);
-
-
-      Int_t *signal     = rawStream->GetSignals();                       //  current ADC signal
-      Int_t  col        = rawStream->GetCol();
-      Int_t row         = rawStream->GetRow();    
-
-      
-      //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 = 0; itime < nbtimebin; itime++){
-       phvalue[row][col][itime] = signal[itime]-baseline;
-      }
-    }
-    
-    // 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;
+       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;
          }
-       }
-      }
-    }
-    
-  }
-  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
-
-      //printf("Detector %d\n",idetector);
-
-      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;
+         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()) {
+           
+           Int_t idetector = det;                                             //  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
+           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;
+               }
+             }
            }
          }
-       }
-      }
-      
-      fDetectorPreviousTrack = idetector;
-      fMCMPrevious           = imcm;
-      fROBPrevious           = irob;
-
-      //baseline          = rawStream->GetCommonAdditive();                //  common baseline
-      
-      fTimeMax          = rawStream->GetNumberOfTimeBins();              //  number of time bins read from data
-      fNumberClustersf    = fTimeMax;
-      fNumberClusters     = (Int_t)(0.6*fTimeMax);
-      Int_t *signal     = rawStream->GetSignals();                       //  current ADC signal
-      Int_t col         = rawStream->GetCol();
-      Int_t row         = rawStream->GetRow();   
-
-       
-      //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 = 0; itime < fTimeMax; itime++){
-       phvalue[row][col][itime] = signal[itime]-baseline;
-      }
-    }
-    
-    // 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;
-  
-}
+         
+       }//array
+      }//QA
+      digitsManager->ClearArrays(det);
+    }//idetector
+    delete digitsManager;
+
+    delete rawStream;
+    return withInput;
+ }//main
 //_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliRawReader *rawReader, Bool_t nocheck)
-{
-  //
-  //  Event processing loop - AliRawReader
-  //  Testbeam 2007 version
-  //
-
-  AliTRDrawStreamBase rawStream(rawReader);
-
-  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*/
-           
-#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
-
-}
 //////////////////////////////////////////////////////////////////////////////
 // Routine inside the DAQ process
 /////////////////////////////////////////////////////////////////////////////
@@ -2532,9 +2617,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)
@@ -2546,19 +2631,23 @@ 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)) {
+  if(((imaxRow + fNumberRowDAQ) > 16) || (imaxRow == 0) || ((imaxCol - fNumberColDAQ) <= 1) || ((imaxCol + fNumberColDAQ) >= 144)) {
     used=1;
     return used;
   }
@@ -2570,21 +2659,26 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
   //  ////////////////////////////////////////////////////       
   
   
-  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++;
   }
 
 
@@ -2617,6 +2711,7 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
       "clustera="<<clustera<<
       "it="<<it<<
       "rms="<<rms<<
+      "nbcl="<<nbcl<<
       "\n"; 
     }
   }
@@ -2624,6 +2719,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;
@@ -2638,6 +2734,10 @@ Int_t AliTRDCalibraFillHisto::FillDAQ(Double_t phvalue[16][144][36]){
       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);
+      //}
     }
     
    
@@ -2718,7 +2818,7 @@ void AliTRDCalibraFillHisto::Write2d(const Char_t *filename, Bool_t append)
     }
   }
   if(fLinearFitterOn){
-    AnalyseLinearFitter();
+    if(fLinearFitterDebugOn) AnalyseLinearFitter();
     f.WriteTObject(fLinearVdriftFit);
   }
    
@@ -2795,20 +2895,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);
       }
     }
@@ -2849,7 +2944,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);
@@ -2883,12 +2978,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) {
@@ -2902,6 +2998,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;
 
 }
@@ -2935,6 +3034,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;
 
 }
@@ -2983,7 +3085,11 @@ void AliTRDCalibraFillHisto::CreatePH2d(Int_t nn)
   // Create the 2D histos
   //
 
-  TString name("Nz");
+  TString name("Ver");
+  name += fVersionVdriftUsed;
+  name += "Subver";
+  name += fSubVersionVdriftUsed;
+  name += "Nz";
   name += fCalibraMode->GetNz(1);
   name += "Nrphi";
   name += fCalibraMode->GetNrphi(1);
@@ -3004,11 +3110,15 @@ void AliTRDCalibraFillHisto::CreateCH2d(Int_t nn)
   // Create the 2D histos
   //
 
-  TString name("Nz");
+  TString name("Ver");
+  name += fVersionGainUsed;
+  name += "Subver";
+  name += fSubVersionGainUsed;
+  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");
@@ -3168,16 +3278,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);
+      }
     }
   }
 }
 
+
+