]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TRD/AliTRDCalibraFillHisto.cxx
Remove old methods using AliTRDtrack
[u/mrichter/AliRoot.git] / TRD / AliTRDCalibraFillHisto.cxx
index 9ce515cd5b48d7a32bc1926cafd1fd53582ceed6..75acca8bb28c3bf10e2126d46703726517b7aab6 100644 (file)
 #include "AliTRDCommonParam.h"
 #include "AliTRDpadPlane.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 "AliTRDrawFastStream.h"
 #include "AliTRDdigitsManager.h"
 #include "AliTRDdigitsParam.h"
 #include "AliTRDSignalIndex.h"
@@ -78,6 +76,8 @@
 
 #include "AliTRDrawStream.h"
 
+#include "AliCDBEntry.h"
+#include "AliCDBManager.h"
 
 #ifdef ALI_DATE
 #include "event.h"
@@ -147,6 +147,8 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto()
   ,fNbMaxCluster(0)
   ,fVersionGainUsed(0)
   ,fSubVersionGainUsed(0)
+  ,fVersionGainLocalUsed(0)
+  ,fSubVersionGainLocalUsed(0)
   ,fVersionVdriftUsed(0) 
   ,fSubVersionVdriftUsed(0)
   ,fCalibraMode(new AliTRDCalibraMode())
@@ -224,6 +226,8 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   ,fNbMaxCluster(c.fNbMaxCluster)
   ,fVersionGainUsed(c.fVersionGainUsed)
   ,fSubVersionGainUsed(c.fSubVersionGainUsed)
+  ,fVersionGainLocalUsed(c.fVersionGainLocalUsed)
+  ,fSubVersionGainLocalUsed(c.fSubVersionGainLocalUsed)
   ,fVersionVdriftUsed(c.fVersionVdriftUsed) 
   ,fSubVersionVdriftUsed(c.fSubVersionVdriftUsed)
   ,fCalibraMode(0x0)
@@ -291,6 +295,12 @@ AliTRDCalibraFillHisto::AliTRDCalibraFillHisto(const AliTRDCalibraFillHisto &c)
   }
   fGeo = new AliTRDgeometry();
   fCalibDB = AliTRDcalibDB::Instance();
+
+  fNumberUsedCh[0]       = 0;
+  fNumberUsedCh[1]       = 0;
+  fNumberUsedPh[0]       = 0;
+  fNumberUsedPh[1]       = 0;
+
 }
 
 //____________________________________________________________________________________
@@ -410,12 +420,6 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos(Int_t nboftimebin)
     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);
@@ -512,142 +516,97 @@ Bool_t AliTRDCalibraFillHisto::Init2Dhistos(Int_t nboftimebin)
   return kTRUE;
 
 }
-//____________Offline tracking in the AliTRDtracker____________________________
-Bool_t AliTRDCalibraFillHisto::UpdateHistograms(const 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",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));
   
-  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 += "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 += "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 ((GetLayer(detector) == GetLayer(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
 
+  return kTRUE;
 
-    /////////////////////////////////////////////////////////////
-    // 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 ){ 
-       if(!fIsHLT){
-         fCalROCGain->~AliTRDCalROC();
-         new(fCalROCGain) AliTRDCalROC(*(cal->GetGainFactorROC(detector)));
-       }
-      }else 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();
-    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,t->GetClusterdQdl(index1),group,row,col);
-         
-    index1++;
-
-  } // while on clusters
+}
+///////////////////////////////////////////////////////////////////////////////////////////////////////////////
+Bool_t AliTRDCalibraFillHisto::InitCalPad(Int_t detector)
+{
+  //
+  // Init the Gain Cal Pad 
+  //
 
-  ///////////////////////////
-  // 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
-    
+  // 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(const AliTRDtrackV1 *t)
@@ -705,13 +664,8 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(const AliTRDtrackV1 *t)
       //Localise the detector bin
       LocalisationDetectorXbins(detector);
       // Get calib objects
-      if( fCalROCGain ){ 
-       if(!fIsHLT){    
-         fCalROCGain->~AliTRDCalROC();
-         new(fCalROCGain) AliTRDCalROC(*(fCalibDB->GetGainFactorROC(detector)));
-       }
-      }else fCalROCGain = new AliTRDCalROC(*(fCalibDB->GetGainFactorROC(detector)));
-      
+      if(!fIsHLT) InitCalPad(detector);        
+            
       // reset
       fDetectorPreviousTrack = detector;
     }
@@ -770,146 +724,6 @@ Bool_t AliTRDCalibraFillHisto::UpdateHistogramsV1(const AliTRDtrackV1 *t)
 // Routine inside the update with AliTRDtrack
 ///////////////////////////////////////////////////////////////////////////////////
 //____________Offine tracking in the AliTRDtracker_____________________________
-Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtracklet(const 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(GetLayer(detector)); // sin angle in the plan yx track
-  Double_t tgl                        = t->GetTglPlane(GetLayer(detector)); // dz/dl and not dz/dx!  
-  Double_t tnp                        = 0.0;                                // tan angle in the plan xy track
-  if( TMath::Abs(snp) <  1.){
-    tnp = snp / TMath::Sqrt((1.-snp)*(1.+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(GetLayer(detector),GetStack(detector));
-  Double_t tiltingangle               = padplane->GetTiltingAngle();        // tiltingangle of the pad      
-  Float_t  tnt                        = TMath::Tan(tiltingangle/180.*TMath::Pi()); // tan tiltingangle
-  // linear fit
-  fLinearFitterTracklet->ClearPoints();  
-  Double_t dydt                       = 0.0;                                // dydt tracklet after straight line fit
-  Double_t errorpar                   = 0.0;                                // error after straight line fit on dy/dt
-  Double_t pointError                 = 0.0;                                // error after straight line fit 
-  Int_t     nbli                      = 0;                                  // number linear fitter points
-  
-  //////////////////////////////
-  // loop over clusters
-  ////////////////////////////
-  for(Int_t k = 0; k < npoints; k++){
-    
-    AliTRDcluster *cl                 = (AliTRDcluster *) t->GetCluster(k+index0);
-    if((fLimitChargeIntegration) && (!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;
-
-    fLinearFitterTracklet->AddPoint(&timeis,ycluster,1);
-    nbli++;
-
-  }
-
-  //////////////////////////////
-  // linear fit
-  //////////////////////////////
-  if(nbli <= 2){ 
-    fLinearFitterTracklet->ClearPoints();  
-    return kFALSE; 
-  }
-  TVectorD pars;
-  fLinearFitterTracklet->Eval();
-  fLinearFitterTracklet->GetParameters(pars);
-  pointError  =  TMath::Sqrt(fLinearFitterTracklet->GetChisquare()/(nbli-2));
-  errorpar    =  fLinearFitterTracklet->GetParError(1)*pointError;
-  dydt        = pars[1]; 
-  fLinearFitterTracklet->ClearPoints();  
-    
-  /////////////////////////////
-  // 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 layer      = GetLayer(fDetectorPreviousTrack);
-
-    (* fDebugStreamer) << "FindP1TrackPHtracklet1"<<
-      //"snpright="<<snpright<<
-      "nbclusters="<<nbclusters<<
-      "detector="<<fDetectorPreviousTrack<<
-      "layer="<<layer<<
-      "\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; 
-      if(fLinearFitterDebugOn) {
-       (GetLinearFitter(detector,kTRUE))->AddPoint(&x,dydt);
-       fEntriesLinearFitter[detector]++;
-      }
-      fLinearVdriftFit->Update(detector,x,pars[1]);
-    }
-  }
-  
-  return kTRUE;
-}
-//____________Offine tracking in the AliTRDtracker_____________________________
 Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *tracklet, Int_t nbclusters)
 {
   //
@@ -966,7 +780,8 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   // Check no shared clusters
   //////////////////////////////
   for(int icc=AliTRDseedV1::kNtb; icc<AliTRDseedV1::kNclusters; icc++){
-    if((cl = tracklet->GetClusters(icc)))  crossrow = 1;
+    cl = tracklet->GetClusters(icc);
+    if(cl)  crossrow = 1;
   }
   //////////////////////////////////
   // Loop clusters
@@ -1043,11 +858,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
@@ -1068,7 +883,7 @@ Bool_t AliTRDCalibraFillHisto::FindP1TrackPHtrackletV1(const AliTRDseedV1 *track
   return kTRUE;
 }
 //____________Offine tracking in the AliTRDtracker_____________________________
-Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t index0, Int_t index1)
+Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet, Int_t nbclusters)
 {
   //
   // PRF width calibration
@@ -1079,34 +894,29 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
   //
   //
 
-
-  //////////////////////////
-  // 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(GetLayer(detector)); //dz/dl and not dz/dx
-  Double_t  snp = t->GetSnpPlane(GetLayer(detector)); // sin angle in xy plan
-  Float_t  dzdx = 0.0;                                // dzdx
-  Float_t  tnp  = 0.0;
-  if(TMath::Abs(snp) < 1.0){
+  //printf("begin\n");
+  ///////////////////////////////////////////
+  // Take the parameters of the track
+  //////////////////////////////////////////
+  // take now the snp, tnp and tgl from the 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)*(1.+snp));
-    dzdx = tgl*TMath::Sqrt(1+tnp*tnp);
-  }
-  // linear fitter
-  fLinearFitterTracklet->ClearPoints();
+  } 
+  Double_t tgl  = tracklet->GetTgl();           // dz/dl
+  Double_t dzdx = tgl*TMath::Sqrt(1+tnp*tnp);   // dz/dx calculated from dz/dl
+  // at the entrance
+  //Double_t tnp = tracklet->GetYref(1);      // dy/dx at the entrance of the chamber
+  //Double_t tgl = tracklet->GetZref(1);      // dz/dl at the entrance of the chamber
+  //Double_t dzdx = tgl;                      //*TMath::Sqrt(1+tnp*tnp); // dz/dx from dz/dl
+  // at the end with correction due to linear fit
+  //Double_t tnp = tracklet->GetYfit(1);      // dy/dx at the end of the chamber after fit correction
+  //Double_t tgl = tracklet->GetZfit(1);      // dz/dl at the end of the chamber after fit correction 
 
-  ///////////////////////////
-  // calcul the tnp group
-  ///////////////////////////
+  ///////////////////////////////
+  // Calculate tnp group shift
+  ///////////////////////////////
   Bool_t echec   = kFALSE;
   Double_t shift = 0.0;
   //Calculate the shift in x coresponding to this tnp
@@ -1121,25 +931,42 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
       }
     }
   }
-  if(echec) {
-    delete [] padPositions;
-    return kFALSE;
-  }
+  // do nothing if out of tnp range
+  //printf("echec %d\n",(Int_t)echec);
+  if(echec) return kFALSE;
 
+  ///////////////////////
+  // Variables
   //////////////////////
-  // loop clusters
-  /////////////////////
-  for(Int_t k = 0;  k < npoints; k++){
-    //Take the cluster
-    AliTRDcluster *cl  = (AliTRDcluster *) t->GetCluster(k+index0);
+
+  Int_t nb3pc    = 0;              // number of three pads clusters used for fit 
+  // 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<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;
-    Short_t  *signals  = cl->GetSignals();
     Double_t     time  = cl->GetPadTime();
-    //Calculate x if possible 
+    if((time<=7) || (time>=21)) continue;
+    Short_t  *signals  = cl->GetSignals(); 
     Float_t xcenter    = 0.0;    
     Bool_t  echec1      = kTRUE;   
-    if((time<=7) || (time>=21)) continue; 
+
+    /////////////////////////////////////////////////////////////
     // Center 3 balanced: position with the center of the pad
+    /////////////////////////////////////////////////////////////
     if ((((Float_t) signals[3]) > 0.0) && 
        (((Float_t) signals[2]) > 0.0) && 
        (((Float_t) signals[4]) > 0.0)) {
@@ -1155,22 +982,27 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
        echec1 = kTRUE;
       }
     }
-    if(TMath::Abs(xcenter) > 0.5) echec = kTRUE;
-    if(echec) continue;
-    //if no echec: calculate with the position of the pad
+    if(TMath::Abs(xcenter) > 0.5) echec1 = kTRUE;
+    if(echec1) continue;
+
+    ////////////////////////////////////////////////////////
+    //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[k]            = padPosition;
+    padPositions[ic]            = padPosition;
     nb3pc++;
     fLinearFitterTracklet->AddPoint(&time, padPosition,1);
-  }//clusters loop
 
 
+  }//clusters loop
+
+  //printf("Fin loop clusters \n");
+  //////////////////////////////
+  // fit with a straight line
   /////////////////////////////
-  // fit
-  ////////////////////////////
-  if(nb3pc < 3) {
-    delete [] padPositions;
+  if(nb3pc < 3){ 
     fLinearFitterTracklet->ClearPoints();  
     return kFALSE;
   }
@@ -1179,18 +1011,24 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
   fLinearFitterTracklet->GetParameters(line);
   Float_t  pointError  = -1.0;
   if( fLinearFitterTracklet->GetChisquare()>=0.0) {
-    pointError  =  TMath::Sqrt( fLinearFitterTracklet->GetChisquare()/(nb3pc-2));
+  pointError  =  TMath::Sqrt( fLinearFitterTracklet->GetChisquare()/(nb3pc-2));
   }
   fLinearFitterTracklet->ClearPoints();  
-  
-  /////////////////////////////////////////////////////
-  // 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
+  //printf("PRF second loop \n");
+  ////////////////////////////////////////////////
+  // Fill the PRF: Second loop over clusters
+  //////////////////////////////////////////////
+  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
     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 
@@ -1198,7 +1036,9 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
     Float_t ymin           = 0.0;                           // relative left charge
     Float_t ymax           = 0.0;                           // relative right charge
   
-    //Requiere simply two pads clusters at least
+    ////////////////////////////////////////////////////////////////
+    // Calculate ycenter, ymin and ymax even for two pad clusters
+    ////////////////////////////////////////////////////////////////
     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]);
@@ -1207,15 +1047,17 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
       if(sum > 0.0) ymax    = ((Float_t) signals[4])/ sum; 
     }
     
-    //calibration group
+    /////////////////////////
+    // 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     layer        = GetLayer(detector);                        // layer 
-    Int_t     stack        = GetStack(detector);                        // stack
+    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
@@ -1225,9 +1067,12 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
     Float_t   signal4      = (Float_t)signals[4];                       // signal
     Float_t   signal5      = (Float_t)signals[5];                       // signal at the border
    
-    //////////////////////////////
-    // debug
-    /////////////////////////////  
+
+
+    /////////////////////
+    // Debug stuff
+    ////////////////////
+
     if(fDebugLevel > 0){
       if ( !fDebugStreamer ) {
        //debug stream
@@ -1235,26 +1080,25 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
        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"<<
+      (* fDebugStreamer) << "HandlePRFtrackletV1"<<
        "caligroup="<<caligroup<<
-       "detector="<<detector<<
+       "detector="<<fDetectorPreviousTrack<<
        "layer="<<layer<<
-       "stack="<< stack <<
-       "npoints="<<npoints<<
+       "stack="<<stack<<
+       "npoints="<<nbclusters<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
        "type="<<type<<
-       "snp="<<snp<<
-       "tnp="<<tnp<<
+               "snp="<<snp<<
+               "tnp="<<tnp<<
        "tgl="<<tgl<<  
        "dzdx="<<dzdx<< 
        "padPos="<<padPos<<
-       "padPosition="<<padPositions[k]<<
+       "padPosition="<<padPositions[ic]<<
        "padPosTracklet="<<padPosTracklet<<
        "x="<<x<<
        "y="<<y<<           
@@ -1270,21 +1114,21 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
       x=-(xdiff+1);
       y = ymin;
       type=-1;
-      (* fDebugStreamer) << "HandlePRFtracklet"<<
+      (* fDebugStreamer) << "HandlePRFtrackletV1"<<
        "caligroup="<<caligroup<<
-       "detector="<<detector<<
+       "detector="<<fDetectorPreviousTrack<<
        "layer="<<layer<<
        "stack="<<stack<<
-       "npoints="<<npoints<<
+       "npoints="<<nbclusters<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
        "type="<<type<<
-       "snp="<<snp<<
-       "tnp="<<tnp<<
+       "snp="<<snp<<
+       "tnp="<<tnp<<
        "tgl="<<tgl<<  
        "dzdx="<<dzdx<< 
        "padPos="<<padPos<<
-       "padPosition="<<padPositions[k]<<
+       "padPosition="<<padPositions[ic]<<
        "padPosTracklet="<<padPosTracklet<<
        "x="<<x<<
        "y="<<y<<
@@ -1300,21 +1144,21 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
       x=1-xdiff;
       y = ymax;
       type=1;
-      (* fDebugStreamer) << "HandlePRFtracklet"<<
+      (* fDebugStreamer) << "HandlePRFtrackletV1"<<
        "caligroup="<<caligroup<<
-       "detector="<<detector<<
+       "detector="<<fDetectorPreviousTrack<<
        "layer="<<layer<<
        "stack="<<stack<<
-       "npoints="<<npoints<<
+       "npoints="<<nbclusters<<
        "Np="<<nb3pc<<
        "ep="<<ep<<
        "type="<<type<<
-       "snp="<<snp<<
-       "tnp="<<tnp<<   
+               "snp="<<snp<<   
+               "tnp="<<tnp<<   
        "tgl="<<tgl<<  
        "dzdx="<<dzdx<< 
        "padPos="<<padPos<<
-       "padPosition="<<padPositions[k]<<
+       "padPosition="<<padPositions[ic]<<
        "padPosTracklet="<<padPosTracklet<<
        "x="<<x<<
        "y="<<y<<
@@ -1329,24 +1173,26 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
        "\n";
       
     }
-
-    ////////////////////////////
-    // quality cuts
-    ///////////////////////////
-    if(npoints < fNumberClusters) continue;
-    if(npoints > fNumberClustersf) continue;
+    
+    /////////////////////
+    // Cuts quality
+    /////////////////////
+    if(nbclusters < fNumberClusters) continue;
+    if(nbclusters > 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( TMath::Abs(snp) >  1.) continue;
+
+
+    ////////////////////////
+    // Fill the histos
+    ///////////////////////
     if (fHisto2d) {
       if(TMath::Abs(dpad) < 1.5) {
        fPRF2d->Fill(shift+dpad,(caligroup+0.5),ycenter);
        fPRF2d->Fill(shift-dpad,(caligroup+0.5),ycenter);
+       //printf("place %f, ycenter %f\n",(shift+dpad),ycenter);
       }
       if((ymin > 0.0) && (TMath::Abs(dpad+1.0) < 1.5)) {
        fPRF2d->Fill(shift-(dpad+1.0),(caligroup+0.5),ymin);
@@ -1357,6 +1203,7 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
        fPRF2d->Fill(shift-1.0+dpad,(caligroup+0.5),ymax);
       }
     }
+    // vector method
     if (fVector2d) {
       if(TMath::Abs(dpad) < 1.5) {
        fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift+dpad,ycenter);
@@ -1371,405 +1218,68 @@ Bool_t AliTRDCalibraFillHisto::HandlePRFtracklet(const AliTRDtrack *t, Int_t ind
        fCalibraVector->UpdateVectorPRF(fDetectorPreviousTrack,grouplocal,shift-1.0+dpad,ymax);
       }
     }
-  }
-  delete [] padPositions;
+  } // second loop over clusters
+
+
   return kTRUE;
-  
 }
-//____________Offine tracking in the AliTRDtracker_____________________________
-Bool_t AliTRDCalibraFillHisto::HandlePRFtrackletV1(const AliTRDseedV1 *tracklet, Int_t nbclusters)
+///////////////////////////////////////////////////////////////////////////////////////
+// Pad row col stuff: see if masked or not
+///////////////////////////////////////////////////////////////////////////////////////
+//_____________________________________________________________________________
+void AliTRDCalibraFillHisto::CheckGoodTrackletV1(const AliTRDcluster *cl)
 {
   //
-  // 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
-  //
+  // See if we are not near a masked pad
   //
 
-  //printf("begin\n");
-  ///////////////////////////////////////////
-  // Take the parameters of the track
-  //////////////////////////////////////////
-  // take now the snp, tnp and tgl from the 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)*(1.+snp));
-  } 
-  Double_t tgl  = tracklet->GetTgl();           // dz/dl
-  Double_t dzdx = tgl*TMath::Sqrt(1+tnp*tnp);   // dz/dx calculated from dz/dl
-  // at the entrance
-  //Double_t tnp = tracklet->GetYref(1);      // dy/dx at the entrance of the chamber
-  //Double_t tgl = tracklet->GetZref(1);      // dz/dl at the entrance of the chamber
-  //Double_t dzdx = tgl;                      //*TMath::Sqrt(1+tnp*tnp); // dz/dx from dz/dl
-  // at the end with correction due to linear fit
-  //Double_t tnp = tracklet->GetYfit(1);      // dy/dx at the end of the chamber after fit correction
-  //Double_t tgl = tracklet->GetZfit(1);      // dz/dl at the end of the chamber after fit correction 
+  if(cl->IsMasked()) fGoodTracklet = kFALSE;
 
-  ///////////////////////////////
-  // Calculate tnp group shift
-  ///////////////////////////////
-  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;
-      }
-    }
-  }
-  // do nothing if out of tnp range
-  //printf("echec %d\n",(Int_t)echec);
-  if(echec) return 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
+  //
 
-  ///////////////////////
-  // Variables
-  //////////////////////
+  if (!IsPadOn(detector, col, row)) {
+    fGoodTracklet = kFALSE;
+  }
 
-  Int_t nb3pc    = 0;              // number of three pads clusters used for fit 
-  // 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<AliTRDseedV1::kNtb; ic++){
-    // reject shared clusters on pad row
-    if((ic+AliTRDseedV1::kNtb) < AliTRDseedV1::kNclusters) {
-      if((cl = tracklet->GetClusters(ic+AliTRDseedV1::kNtb))) continue;
+  if (col > 0) {
+    if (!IsPadOn(detector, col-1, row)) {
+      fGoodTracklet = kFALSE;
     }
-    if(!(cl = tracklet->GetClusters(ic))) continue;
-    Double_t     time  = cl->GetPadTime();
-    if((time<=7) || (time>=21)) continue;
-    Short_t  *signals  = cl->GetSignals(); 
-    Float_t xcenter    = 0.0;    
-    Bool_t  echec1      = kTRUE;   
+  }
 
-    /////////////////////////////////////////////////////////////
-    // 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)) {
-      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) {
-       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 {
-       echec1 = kTRUE;
-      }
+  if (col < 143) {
+    if (!IsPadOn(detector, col+1, row)) {
+      fGoodTracklet = kFALSE;
     }
-    if(TMath::Abs(xcenter) > 0.5) echec1 = kTRUE;
-    if(echec1) continue;
-
-    ////////////////////////////////////////////////////////
-    //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++;
-    fLinearFitterTracklet->AddPoint(&time, padPosition,1);
-
-
-  }//clusters loop
+  }
+  
+}
+//_____________________________________________________________________________
+Bool_t AliTRDCalibraFillHisto::IsPadOn(Int_t detector, Int_t row, Int_t col) const
+{
+  //
+  // Look in the choosen database if the pad is On.
+  // If no the track will be "not good"
+  //
 
-  //printf("Fin loop clusters \n");
-  //////////////////////////////
-  // fit with a straight line
-  /////////////////////////////
-  if(nb3pc < 3){ 
-    fLinearFitterTracklet->ClearPoints();  
+  // Get the parameter object
+  AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
+  if (!cal) {
+    AliInfo("Could not get calibDB");
     return kFALSE;
   }
-  fLinearFitterTracklet->Eval();
-  TVectorD line(2);
-  fLinearFitterTracklet->GetParameters(line);
-  Float_t  pointError  = -1.0;
-  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<AliTRDseedV1::kNtb; ic++){
-    // reject shared clusters on pad row
-    if(((ic+AliTRDseedV1::kNtb) < AliTRDseedV1::kNclusters) && (cl = tracklet->GetClusters(ic+AliTRDseedV1::kNtb))) continue;
-    //
-    if(!(cl = tracklet->GetClusters(ic))) continue;
-
-    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
-  
-    ////////////////////////////////////////////////////////////////
-    // Calculate ycenter, ymin and ymax even for two pad clusters
-    ////////////////////////////////////////////////////////////////
-    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     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
-    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 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
-      }     
-     
-      x = xdiff;
-      Int_t type=0;
-      Float_t y = ycenter;
-      (* fDebugStreamer) << "HandlePRFtrackletV1"<<
-       "caligroup="<<caligroup<<
-       "detector="<<fDetectorPreviousTrack<<
-       "layer="<<layer<<
-       "stack="<<stack<<
-       "npoints="<<nbclusters<<
-       "Np="<<nb3pc<<
-       "ep="<<ep<<
-       "type="<<type<<
-               "snp="<<snp<<
-               "tnp="<<tnp<<
-       "tgl="<<tgl<<  
-       "dzdx="<<dzdx<< 
-       "padPos="<<padPos<<
-       "padPosition="<<padPositions[ic]<<
-       "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) << "HandlePRFtrackletV1"<<
-       "caligroup="<<caligroup<<
-       "detector="<<fDetectorPreviousTrack<<
-       "layer="<<layer<<
-       "stack="<<stack<<
-       "npoints="<<nbclusters<<
-       "Np="<<nb3pc<<
-       "ep="<<ep<<
-       "type="<<type<<
-       "snp="<<snp<<
-       "tnp="<<tnp<<
-       "tgl="<<tgl<<  
-       "dzdx="<<dzdx<< 
-       "padPos="<<padPos<<
-       "padPosition="<<padPositions[ic]<<
-       "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) << "HandlePRFtrackletV1"<<
-       "caligroup="<<caligroup<<
-       "detector="<<fDetectorPreviousTrack<<
-       "layer="<<layer<<
-       "stack="<<stack<<
-       "npoints="<<nbclusters<<
-       "Np="<<nb3pc<<
-       "ep="<<ep<<
-       "type="<<type<<
-               "snp="<<snp<<   
-               "tnp="<<tnp<<   
-       "tgl="<<tgl<<  
-       "dzdx="<<dzdx<< 
-       "padPos="<<padPos<<
-       "padPosition="<<padPositions[ic]<<
-       "padPosTracklet="<<padPosTracklet<<
-       "x="<<x<<
-       "y="<<y<<
-       "xcl="<<xcl<<
-       "qcl="<<qcl<<
-       "signal1="<<signal1<<
-       "signal2="<<signal2<<
-       "signal3="<<signal3<<
-       "signal4="<<signal4<<
-       "signal5="<<signal5<<
-       "time="<<time<<
-       "\n";
-      
-    }
-    
-    /////////////////////
-    // Cuts quality
-    /////////////////////
-    if(nbclusters < fNumberClusters) continue;
-    if(nbclusters > fNumberClustersf) continue;
-    if(nb3pc <= 5) continue;
-    if((time >= 21) || (time < 7)) continue;
-    if(TMath::Abs(qcl) < 80) continue; 
-    if( TMath::Abs(snp) >  1.) continue;
-
-
-    ////////////////////////
-    // Fill the histos
-    ///////////////////////
-    if (fHisto2d) {
-      if(TMath::Abs(dpad) < 1.5) {
-       fPRF2d->Fill(shift+dpad,(caligroup+0.5),ycenter);
-       fPRF2d->Fill(shift-dpad,(caligroup+0.5),ycenter);
-       //printf("place %f, ycenter %f\n",(shift+dpad),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);
-      }
-    }
-    // vector method
-    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);
-      }
-    }
-  } // second loop over clusters
-
-
-  return kTRUE;
-}
-///////////////////////////////////////////////////////////////////////////////////////
-// Pad row col stuff: see if masked or not
-///////////////////////////////////////////////////////////////////////////////////////
-//_____________________________________________________________________________
-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
-  //
-
-  if (!IsPadOn(detector, col, row)) {
-    fGoodTracklet = kFALSE;
-  }
-
-  if (col > 0) {
-    if (!IsPadOn(detector, col-1, row)) {
-      fGoodTracklet = kFALSE;
-    }
-  }
-
-  if (col < 143) {
-    if (!IsPadOn(detector, col+1, row)) {
-      fGoodTracklet = kFALSE;
-    }
-  }
-  
-}
-//_____________________________________________________________________________
-Bool_t AliTRDCalibraFillHisto::IsPadOn(Int_t detector, Int_t row, Int_t col) const
-{
-  //
-  // Look in the choosen database if the pad is On.
-  // If no the track will be "not good"
-  //
-
-  // Get the parameter object
-  AliTRDcalibDB *cal = AliTRDcalibDB::Instance();
-  if (!cal) {
-    AliInfo("Could not get calibDB");
-    return kFALSE;
-  }
-  
-  if (!cal->IsChamberInstalled(detector)     || 
-       cal->IsChamberMasked(detector)        ||
-       cal->IsPadMasked(detector,col,row)) {
-    return kFALSE;
+  
+  if (!cal->IsChamberInstalled(detector)     || 
+       cal->IsChamberMasked(detector)        ||
+       cal->IsPadMasked(detector,col,row)) {
+    return kFALSE;
   }
   else {
     return kTRUE;
@@ -2136,7 +1646,7 @@ void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackCH(Int_t nbclusters)
       }
       break;
     default: break;
-    }
+  }
 }
 //____________Offine tracking in the AliTRDtracker_____________________________
 void AliTRDCalibraFillHisto::FillTheInfoOfTheTrackPH()
@@ -2175,540 +1685,181 @@ 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;
-  
-  switch(nb)
-    {
-    case 1:
-      fNumberUsedPh[0]++;
-      for (Int_t i = 0; i < fTimeMax; i++) {
-       if (fHisto2d) {
-         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) {
-         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;
-    case 2:
-      if ((fd1 == fd2+1) || 
-         (fd2 == fd1+1)) {
-       // One of the two fast all the think
-       if (k2 > (k1+fDifference)) {
-         //we choose to fill the fd1 with all the values
-         fNumberUsedPh[1]++;
-         for (Int_t i = 0; i < fTimeMax; i++) {
-           if (fHisto2d) {
-             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) {
-             if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
-             else {
-               if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
-                 }
-           }
-         }
-       }
-       if ((k2+fDifference) < fTimeMax) {
-         //we choose to fill the fd2 with all the values
-         fNumberUsedPh[1]++;
-         for (Int_t i = 0; i < fTimeMax; i++) {
-           if (fHisto2d) {
-             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) {
-           if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
-           else {
-             if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
-           }
-         }
-         }
-       }
-      }
-      // Two zones voisines sinon rien!
-      if (fCalibraMode->GetNfragZ(1) > 1) {
-       // Case 2
-       if ((fd1+fCalibraMode->GetNfragZ(1)) < (fCalibraMode->GetNfragZ(1)*fCalibraMode->GetNfragRphi(1))) {
-         if (fd2 == (fd1+fCalibraMode->GetNfragZ(1))) {
-           // One of the two fast all the think
-           if (k2 > (k1+fDifference)) {
-             //we choose to fill the fd1 with all the values
-             fNumberUsedPh[1]++;
-             for (Int_t i = 0; i < fTimeMax; i++) {
-               if (fHisto2d) {
-                 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) {
-                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
-                 else {
-                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
-                 }
-               }
-             }
-           }
-           if ((k2+fDifference) < fTimeMax) {
-             //we choose to fill the fd2 with all the values
-             fNumberUsedPh[1]++;
-             for (Int_t i = 0; i < fTimeMax; i++) {
-               if (fHisto2d) {
-                 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) {
-                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
-                 else {
-                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
-                 }
-               }
-             }
-           }
-         }
-       }
-       // Two zones voisines sinon rien!
-       // Case 3
-       if ((fd1 - fCalibraMode->GetNfragZ(1)) >= 0) {
-         if (fd2 == (fd1 - fCalibraMode->GetNfragZ(1))) {
-           // One of the two fast all the think
-           if (k2 > (k1 + fDifference)) {
-             //we choose to fill the fd1 with all the values
-             fNumberUsedPh[1]++;
-             for (Int_t i = 0; i < fTimeMax; i++) {
-               if (fHisto2d) {
-                 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) {
-                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
-                 else {
-                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
-                 }
-               }
-             }
-           }
-           if ((k2+fDifference) < fTimeMax) {
-             //we choose to fill the fd2 with all the values
-             fNumberUsedPh[1]++;
-             for (Int_t i = 0; i < fTimeMax; i++) {
-               if (fHisto2d) {
-                 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) {
-                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
-                 else {
-                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
-                 }
-               }
-             }
-           }
-         }
-       }
-      }
-      break;
-    default: break;
-    } 
-}
-//////////////////////////////////////////////////////////////////////////////////////////
-// DAQ process functions
-/////////////////////////////////////////////////////////////////////////////////////////
-//_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliTRDrawStreamBase *rawStream, Bool_t nocheck)
-{
-  //
-  // Event Processing loop - AliTRDrawStreamBase
-  // TestBeam 2007 version
-  // 0 timebin problem
-  // 1 no input
-  // 2 input
-  // Same algorithm as TestBeam but different reader
-  //
-
-  rawStream->SetSharedPadReadout(kFALSE);
-  
-  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  = 10;  
-  //printf("------------Detector\n");
-  
-  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);
-
-       
-       // 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)(fNumberClustersProcent*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;
-         }
-       }
-      }
-    }
-    
-  }
-  else{
-
-    while (rawStream->Next()) { //iddetecte
-
-      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;
-           }
-         }
-       }
-      }
-      
-      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)(fNumberClustersProcent*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;
-       /*if(phvalue[row][col][itime] >= 20) {
-               printf("----------> phvalue[%d][%d][%d] %d  baseline %d \n",
-                      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;
-  
-}
-//_____________________________________________________________________
-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
-                                              const eventHeaderStruct *event,
-                                              Bool_t nocheck
-#else
-                                              const 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
-
-}
-//_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ2(AliRawReader *rawReader)
- { //main
-  //
-  // Event Processing loop - AliTRDrawFastStream
-  // 
-  // 0 timebin problem
-  // 1 no input
-  // 2 input
-  // Same algorithm as TestBeam but different reader
-  //
-
-   //  AliTRDrawFastStream *rawStream = AliTRDrawFastStream::GetRawStream(rawReader);
-  AliTRDrawFastStream *rawStream = new AliTRDrawFastStream(rawReader);
-  rawStream->SetNoErrorWarning();
-  rawStream->SetSharedPadReadout(kFALSE);
-
-  AliTRDdigitsManager *digitsManager = new AliTRDdigitsManager(kTRUE);
-  digitsManager->CreateArrays();
-    
-  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  = 10;  
-
-  
-    fTimeMax = 0;
-       
-    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");
+  // 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;
+      }
+    }
+  }
 
-       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;
+  //printf("nbclusters %d, low limit %d, high limit %d\n",nbclusters,fNumberClusters,fNumberClustersf);
+
+  if(nbclusters < fNumberClusters) return;
+  if(nbclusters > fNumberClustersf) return;
+  
+  switch(nb)
+    {
+    case 1:
+      fNumberUsedPh[0]++;
+      for (Int_t i = 0; i < fTimeMax; i++) {
+       if (fHisto2d) {
+         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) {
+         if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+         else {
+           if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);  
          }
-         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;
+       }
+      }
+      break;
+    case 2:
+      if ((fd1 == fd2+1) || 
+         (fd2 == fd1+1)) {
+       // One of the two fast all the think
+       if (k2 > (k1+fDifference)) {
+         //we choose to fill the fd1 with all the values
+         fNumberUsedPh[1]++;
+         for (Int_t i = 0; i < fTimeMax; i++) {
+           if (fHisto2d) {
+             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) {
+             if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+             else {
+               if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 }
+           }
+         }
+       }
+       if ((k2+fDifference) < fTimeMax) {
+         //we choose to fill the fd2 with all the values
+         fNumberUsedPh[1]++;
+         for (Int_t i = 0; i < fTimeMax; i++) {
+           if (fHisto2d) {
+             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) {
+           if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+           else {
+             if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+           }
+         }
+         }
+       }
+      }
+      // Two zones voisines sinon rien!
+      if (fCalibraMode->GetNfragZ(1) > 1) {
+       // Case 2
+       if ((fd1+fCalibraMode->GetNfragZ(1)) < (fCalibraMode->GetNfragZ(1)*fCalibraMode->GetNfragRphi(1))) {
+         if (fd2 == (fd1+fCalibraMode->GetNfragZ(1))) {
+           // One of the two fast all the think
+           if (k2 > (k1+fDifference)) {
+             //we choose to fill the fd1 with all the values
+             fNumberUsedPh[1]++;
+             for (Int_t i = 0; i < fTimeMax; i++) {
+               if (fHisto2d) {
+                 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) {
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
                  }
                }
              }
            }
-           
-           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);
-                      }*/
+           if ((k2+fDifference) < fTimeMax) {
+             //we choose to fill the fd2 with all the values
+             fNumberUsedPh[1]++;
+             for (Int_t i = 0; i < fTimeMax; i++) {
+               if (fHisto2d) {
+                 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) {
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 }
+               }
+             }
            }
-           
-         }//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;
+         }
+       }
+       // Two zones voisines sinon rien!
+       // Case 3
+       if ((fd1 - fCalibraMode->GetNfragZ(1)) >= 0) {
+         if (fd2 == (fd1 - fCalibraMode->GetNfragZ(1))) {
+           // One of the two fast all the think
+           if (k2 > (k1 + fDifference)) {
+             //we choose to fill the fd1 with all the values
+             fNumberUsedPh[1]++;
+             for (Int_t i = 0; i < fTimeMax; i++) {
+               if (fHisto2d) {
+                 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) {
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd1,i,fPHValue[i]);
+                 }
+               }
+             }
+           }
+           if ((k2+fDifference) < fTimeMax) {
+             //we choose to fill the fd2 with all the values
+             fNumberUsedPh[1]++;
+             for (Int_t i = 0; i < fTimeMax; i++) {
+               if (fHisto2d) {
+                 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) {
+                 if(fFillWithZero) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 else {
+                   if(((Float_t) fPHValue[i] > 0.0)) fCalibraVector->UpdateVectorPH(fDetectorPreviousTrack,fd2,i,fPHValue[i]);
+                 }
                }
              }
            }
          }
-         
-       }//array
-      }//QA
-      digitsManager->ClearArrays(det);
-    }//idetector
-    delete digitsManager;
-
-    delete rawStream;
-    return withInput;
- }//main
+       }
+      }
+      break;
+    default: break;
+    } 
+}
+//////////////////////////////////////////////////////////////////////////////////////////
+// DAQ process functions
+/////////////////////////////////////////////////////////////////////////////////////////
 //_____________________________________________________________________
-Int_t AliTRDCalibraFillHisto::ProcessEventDAQ3(AliRawReader *rawReader)
+Int_t AliTRDCalibraFillHisto::ProcessEventDAQ(AliRawReader *rawReader)
  { //main
   //
   // Event Processing loop - AliTRDrawStream
@@ -2719,7 +1870,6 @@ Int_t AliTRDCalibraFillHisto::ProcessEventDAQ3(AliRawReader *rawReader)
   // Same algorithm as TestBeam but different reader
   //
 
-   //  AliTRDrawFastStream *rawStream = AliTRDrawFastStream::GetRawStream(rawReader);
   AliTRDrawStream *rawStream = new AliTRDrawStream(rawReader);
   rawStream->SetNoErrorWarning();
   rawStream->SetSharedPadReadout(kFALSE);
@@ -3163,20 +2313,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);
       }
     }
@@ -3217,7 +2362,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);
@@ -3251,12 +2396,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) {
@@ -3270,6 +2416,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;
 
 }
@@ -3303,6 +2452,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;
 
 }
@@ -3544,16 +2696,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);
+      }
     }
   }
 }
 
+
+