]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - TPC/AliTPCclustererMI.cxx
GetClusterFast function implemented (No getter before) (Marian)
[u/mrichter/AliRoot.git] / TPC / AliTPCclustererMI.cxx
index 82cc2d80cf32e9c91cb48cb91bf7566af3f3651d..129c7c2b639de16e3117d9e1c8dd80142496aa08 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.4  2003/03/05 11:16:15  kowal2
-Logs added
-
-*/
-
-
-
+/* $Id$ */
 
 //-------------------------------------------------------
 //          Implementation of the TPC clusterer
@@ -29,18 +21,33 @@ Logs added
 //   Origin: Marian Ivanov 
 //-------------------------------------------------------
 
+#include "AliTPCReconstructor.h"
 #include "AliTPCclustererMI.h"
 #include "AliTPCclusterMI.h"
 #include <TObjArray.h>
 #include <TFile.h>
+#include "TGraph.h"
+#include "TF1.h"
+#include "TRandom.h"
+#include "AliMathBase.h"
+
 #include "AliTPCClustersArray.h"
 #include "AliTPCClustersRow.h"
 #include "AliDigits.h"
 #include "AliSimDigits.h"
 #include "AliTPCParam.h"
-#include "AliTPCBuffer160.h"
+#include "AliRawReader.h"
+#include "AliTPCRawStream.h"
+#include "AliRunLoader.h"
+#include "AliLoader.h"
 #include "Riostream.h"
 #include <TTree.h>
+#include "AliTPCcalibDB.h"
+#include "AliTPCCalPad.h"
+#include "AliTPCCalROC.h"
+#include "TTreeStream.h"
+#include "AliLog.h"
+
 
 ClassImp(AliTPCclustererMI)
 
@@ -48,10 +55,21 @@ ClassImp(AliTPCclustererMI)
 
 AliTPCclustererMI::AliTPCclustererMI(const AliTPCParam* par)
 {
+  fPedSubtraction = kFALSE;
+  fIsOldRCUFormat = kFALSE;
   fInput =0;
   fOutput=0;
   fParam = par;
+  fDebugStreamer = new TTreeSRedirector("TPCsignal.root");
+  fAmplitudeHisto = 0;
 }
+
+AliTPCclustererMI::~AliTPCclustererMI(){
+  DumpHistos();
+  if (fAmplitudeHisto) delete fAmplitudeHisto;
+  if (fDebugStreamer) delete fDebugStreamer;
+}
+
 void AliTPCclustererMI::SetInput(TTree * tree)
 {
   //
@@ -80,7 +98,7 @@ void AliTPCclustererMI::SetOutput(TTree * tree)
 
 Float_t  AliTPCclustererMI::GetSigmaY2(Int_t iz){
   // sigma y2 = in digits  - we don't know the angle
-  Float_t z = iz*fParam->GetZWidth();
+  Float_t z = iz*fParam->GetZWidth()+fParam->GetNTBinsL1()*fParam->GetZWidth();
   Float_t sd2 = (z*fParam->GetDiffL()*fParam->GetDiffL())/
     (fPadWidth*fPadWidth);
   Float_t sres = 0.25;
@@ -91,7 +109,7 @@ Float_t  AliTPCclustererMI::GetSigmaY2(Int_t iz){
 
 Float_t  AliTPCclustererMI::GetSigmaZ2(Int_t iz){
   //sigma z2 = in digits - angle estimated supposing vertex constraint
-  Float_t z = iz*fZWidth;
+  Float_t z = iz*fZWidth+fParam->GetNTBinsL1()*fParam->GetZWidth();
   Float_t sd2 = (z*fParam->GetDiffL()*fParam->GetDiffL())/(fZWidth*fZWidth);
   Float_t angular = fPadLength*(fParam->GetZLength()-z)/(fRx*fZWidth);
   angular*=angular;
@@ -102,7 +120,7 @@ Float_t  AliTPCclustererMI::GetSigmaZ2(Int_t iz){
   return res;
 }
 
-void AliTPCclustererMI::MakeCluster(Int_t k,Int_t max,Int_t *bins, UInt_t m,
+void AliTPCclustererMI::MakeCluster(Int_t k,Int_t max,Float_t *bins, UInt_t /*m*/,
 AliTPCclusterMI &c) 
 {
   Int_t i0=k/max;  //central pad
@@ -110,8 +128,8 @@ AliTPCclusterMI &c)
 
   // set pointers to data
   //Int_t dummy[5] ={0,0,0,0,0};
-  Int_t * matrix[5]; //5x5 matrix with digits  - indexing i = 0 ..4  j = -2..2
-  Int_t * resmatrix[5];
+  Float_t * matrix[5]; //5x5 matrix with digits  - indexing i = 0 ..4  j = -2..2
+  Float_t * resmatrix[5];
   for (Int_t di=-2;di<=2;di++){
     matrix[di+2]  =  &bins[k+di*max];
     resmatrix[di+2]  =  &fResBins[k+di*max];
@@ -123,7 +141,7 @@ AliTPCclusterMI &c)
   Float_t vmatrix[5][5];
   vmatrix[2][2] = matrix[2][0];
   c.SetType(0);
-  c.SetMax(Short_t(vmatrix[2][2])); // write maximal amplitude
+  c.SetMax((UShort_t)(vmatrix[2][2])); // write maximal amplitude
   for (Int_t di =-1;di <=1;di++)
     for (Int_t dj =-1;dj <=1;dj++){
       Float_t amp = matrix[di+2][dj];
@@ -208,7 +226,7 @@ AliTPCclusterMI &c)
     //remove cluster data from data
     for (Int_t di=-2;di<=2;di++)
       for (Int_t dj=-2;dj<=2;dj++){
-       resmatrix[di+2][dj] -= Int_t(vmatrix[di+2][dj+2]);
+       resmatrix[di+2][dj] -= vmatrix[di+2][dj+2];
        if (resmatrix[di+2][dj]<0) resmatrix[di+2][dj]=0;
       }
     resmatrix[2][0] =0;
@@ -218,8 +236,8 @@ AliTPCclusterMI &c)
   //unfolding when neccessary  
   //
   
-  Int_t * matrix2[7]; //7x7 matrix with digits  - indexing i = 0 ..6  j = -3..3
-  Int_t dummy[7]={0,0,0,0,0,0};
+  Float_t * matrix2[7]; //7x7 matrix with digits  - indexing i = 0 ..6  j = -3..3
+  Float_t dummy[7]={0,0,0,0,0,0};
   for (Int_t di=-3;di<=3;di++){
     matrix2[di+3] =  &bins[k+di*max];
     if ((k+di*max)<3)  matrix2[di+3] = &dummy[3];
@@ -251,7 +269,7 @@ AliTPCclusterMI &c)
 
 
 
-void AliTPCclustererMI::UnfoldCluster(Int_t * matrix2[7], Float_t recmatrix[5][5], Float_t & meani, Float_t & meanj, 
+void AliTPCclustererMI::UnfoldCluster(Float_t * matrix2[7], Float_t recmatrix[5][5], Float_t & meani, Float_t & meanj, 
                                      Float_t & sumu, Float_t & overlap )
 {
   //
@@ -429,9 +447,12 @@ void AliTPCclustererMI::AddCluster(AliTPCclusterMI &c){
   c.SetSigmaZ2(s2*w*w);
   c.SetY((meani - 2.5 - 0.5*fMaxPad)*fParam->GetPadPitchWidth(fSector));
   c.SetZ(fZWidth*(meanj-3)); 
-  c.SetZ(c.GetZ()   - 3.*fParam->GetZSigma()); // PASA delay 
+  c.SetZ(c.GetZ() - 3.*fParam->GetZSigma() + fParam->GetNTBinsL1()*fParam->GetZWidth()); // PASA delay + L1 delay
   c.SetZ(fSign*(fParam->GetZLength() - c.GetZ()));
-  
+  c.SetX(fRx);
+  c.SetDetector(fSector);
+  c.SetRow(fRow);
+
   if (ki<=1 || ki>=fMaxPad-1 || kj==1 || kj==fMaxTime-2) {
     //c.SetSigmaY2(c.GetSigmaY2()*25.);
     //c.SetSigmaZ2(c.GetSigmaZ2()*4.);
@@ -448,43 +469,43 @@ void AliTPCclustererMI::AddCluster(AliTPCclusterMI &c){
 
 
 //_____________________________________________________________________________
-void AliTPCclustererMI::Digits2Clusters(const AliTPCParam *par, Int_t eventn)
+void AliTPCclustererMI::Digits2Clusters()
 {
   //-----------------------------------------------------------------
   // This is a simple cluster finder.
   //-----------------------------------------------------------------
-  TDirectory *savedir=gDirectory; 
 
if (fInput==0)
-    cerr<<"AliTPC::Digits2Clusters(): input tree not initialised !\n";
 if (!fInput) 
+    Error("Digits2Clusters", "input tree not initialised");
     return;
   }
  
-  if (fOutput==0) {
-     cerr<<"AliTPC::Digits2Clusters(): output tree not initialised !\n";
-     return;
+  if (!fOutput) {
+    Error("Digits2Clusters", "output tree not initialised");
+    return;
   }
 
+  AliTPCCalPad * gainTPC = AliTPCcalibDB::Instance()->GetPadGainFactor();
+
   AliSimDigits digarr, *dummy=&digarr;
   fRowDig = dummy;
   fInput->GetBranch("Segment")->SetAddress(&dummy);
   Stat_t nentries = fInput->GetEntries();
   
-  fMaxTime=par->GetMaxTBin()+6; // add 3 virtual time bins before and 3   after
+  fMaxTime=fParam->GetMaxTBin()+6; // add 3 virtual time bins before and 3   after
     
-  fParam = par;
-  ((AliTPCParam*)par)->Write(par->GetTitle());
-
   Int_t nclusters  = 0;
-  
+
   for (Int_t n=0; n<nentries; n++) {
     fInput->GetEvent(n);
-    Int_t row;
-    if (!par->AdjustSectorRow(digarr.GetID(),fSector,row)) {
+    if (!fParam->AdjustSectorRow(digarr.GetID(),fSector,fRow)) {
       cerr<<"AliTPC warning: invalid segment ID ! "<<digarr.GetID()<<endl;
       continue;
     }
-        
+    Int_t row = fRow;
+    AliTPCCalROC * gainROC = gainTPC->GetCalROC(fSector);  // pad gains per given sector
+    
+    //
     AliTPCClustersRow *clrow= new AliTPCClustersRow();
     fRowCl = clrow;
     clrow->SetClass("AliTPCclusterMI");
@@ -492,35 +513,37 @@ void AliTPCclustererMI::Digits2Clusters(const AliTPCParam *par, Int_t eventn)
 
     clrow->SetID(digarr.GetID());
     fOutput->GetBranch("Segment")->SetAddress(&clrow);
-    fRx=par->GetPadRowRadii(fSector,row);
+    fRx=fParam->GetPadRowRadii(fSector,row);
     
     
-    const Int_t kNIS=par->GetNInnerSector(), kNOS=par->GetNOuterSector();
+    const Int_t kNIS=fParam->GetNInnerSector(), kNOS=fParam->GetNOuterSector();
     fZWidth = fParam->GetZWidth();
     if (fSector < kNIS) {
-      fMaxPad = par->GetNPadsLow(row);
+      fMaxPad = fParam->GetNPadsLow(row);
       fSign = (fSector < kNIS/2) ? 1 : -1;
-      fPadLength = par->GetPadPitchLength(fSector,row);
-      fPadWidth = par->GetPadPitchWidth();
+      fPadLength = fParam->GetPadPitchLength(fSector,row);
+      fPadWidth = fParam->GetPadPitchWidth();
     } else {
-      fMaxPad = par->GetNPadsUp(row);
+      fMaxPad = fParam->GetNPadsUp(row);
       fSign = ((fSector-kNIS) < kNOS/2) ? 1 : -1;
-      fPadLength = par->GetPadPitchLength(fSector,row);
-      fPadWidth  = par->GetPadPitchWidth();
+      fPadLength = fParam->GetPadPitchLength(fSector,row);
+      fPadWidth  = fParam->GetPadPitchWidth();
     }
     
     
     fMaxBin=fMaxTime*(fMaxPad+6);  // add 3 virtual pads  before and 3 after
-    fBins    =new Int_t[fMaxBin];
-    fResBins =new Int_t[fMaxBin];  //fBins with residuals after 1 finder loop 
-    memset(fBins,0,sizeof(Int_t)*fMaxBin);
+    fBins    =new Float_t[fMaxBin];
+    fResBins =new Float_t[fMaxBin];  //fBins with residuals after 1 finder loop 
+    memset(fBins,0,sizeof(Float_t)*fMaxBin);
+    memset(fResBins,0,sizeof(Float_t)*fMaxBin);
     
     if (digarr.First()) //MI change
       do {
-       Short_t dig=digarr.CurrentDigit();
-       if (dig<=par->GetZeroSup()) continue;
+       Float_t dig=digarr.CurrentDigit();
+       if (dig<=fParam->GetZeroSup()) continue;
        Int_t j=digarr.CurrentRow()+3, i=digarr.CurrentColumn()+3;
-       fBins[i*fMaxTime+j]=dig;
+       Float_t gain = gainROC->GetValue(row,digarr.CurrentColumn());
+       fBins[i*fMaxTime+j]=dig/gain;
       } while (digarr.Next());
     digarr.ExpandTrackBuffer();
 
@@ -532,142 +555,193 @@ void AliTPCclustererMI::Digits2Clusters(const AliTPCParam *par, Int_t eventn)
     delete[] fBins;      
     delete[] fResBins;  
   }  
-  cerr<<"Number of found clusters : "<<nclusters<<"                        \n";
-  
-  fOutput->Write();
-  savedir->cd();
+
+  Info("Digits2Clusters", "Number of found clusters : %d", nclusters);
 }
 
-void AliTPCclustererMI::Digits2Clusters(const char* fileName)
+void AliTPCclustererMI::Digits2Clusters(AliRawReader* rawReader)
 {
 //-----------------------------------------------------------------
-// This is a cluster finder for raw data.
-//
-// It assumes, that the input file contains uncompressed data 
-// in TPC Altro format without mini headers.
-// It also assumes, that the data is sorted in a such a way that
-// data from different pad rows is not mixed. The order of pads
-// in a pad row can be random.
+// This is a cluster finder for the TPC raw data.
+// The method assumes NO ordering of the altro channels.
+// The pedestal subtraction can be switched on and off
+// using an option of the TPC reconstructor
 //-----------------------------------------------------------------
 
-  TDirectory* savedir = gDirectory; 
-
-  AliTPCBuffer160 buffer(fileName, 0);  // buffer for reading raw data
-  fRowDig = NULL;
   if (!fOutput) {
-    Error("Digits2Clusters", "output tree not initialised !\n");
+    Error("Digits2Clusters", "output tree not initialised");
     return;
   }
-  fOutput->GetCurrentFile()->cd();
-  const_cast<AliTPCParam*>(fParam)->Write();
+
+  fRowDig = NULL;
+
+  AliTPCCalPad * gainTPC = AliTPCcalibDB::Instance()->GetPadGainFactor();
 
   Int_t nclusters  = 0;
   
   fMaxTime = fParam->GetMaxTBin() + 6; // add 3 virtual time bins before and 3 after
   const Int_t kNIS = fParam->GetNInnerSector();
   const Int_t kNOS = fParam->GetNOuterSector();
+  const Int_t kNS = kNIS + kNOS;
   fZWidth = fParam->GetZWidth();
   Int_t zeroSup = fParam->GetZeroSup();
-  Int_t offset = 1;
-
-  Int_t nWords, iPad, iRow;
-  Int_t prevSector = -1;
-  Int_t prevRow = -1;
-  fBins = NULL;
-  while (buffer.ReadTrailerBackward(nWords, iPad, iRow, fSector) == 0) {
-    Int_t nSkip = (4 - (nWords % 4)) % 4;
-
-    // when the sector or row number has changed ...
-    if ((fSector != prevSector) || (iRow != prevRow)) {
-
-      // ... find clusters in the previous pad row, and ...
-      if (fBins) {
-       FindClusters();
-
-       fOutput->Fill();
-       delete fRowCl;    
-       nclusters += fNcluster;    
-       delete[] fBins;      
-       delete[] fResBins;  
+
+  Float_t** allBins = NULL;
+  Float_t** allBinsRes = NULL;
+
+  // Loop over sectors
+  for(fSector = 0; fSector < kNS; fSector++) {
+
+    AliTPCCalROC * gainROC = gainTPC->GetCalROC(fSector);  // pad gains per given sector
+    Int_t nRows = 0;
+    Int_t nDDLs = 0, indexDDL = 0;
+    if (fSector < kNIS) {
+      nRows = fParam->GetNRowLow();
+      fSign = (fSector < kNIS/2) ? 1 : -1;
+      nDDLs = 2;
+      indexDDL = fSector * 2;
+    }
+    else {
+      nRows = fParam->GetNRowUp();
+      fSign = ((fSector-kNIS) < kNOS/2) ? 1 : -1;
+      nDDLs = 4;
+      indexDDL = (fSector-kNIS) * 4 + kNIS * 2;
+    }
+
+    allBins = new Float_t*[nRows];
+    allBinsRes = new Float_t*[nRows];
+
+    for (Int_t iRow = 0; iRow < nRows; iRow++) {
+      Int_t maxPad;
+      if (fSector < kNIS)
+       maxPad = fParam->GetNPadsLow(iRow);
+      else
+       maxPad = fParam->GetNPadsUp(iRow);
+      
+      Int_t maxBin = fMaxTime*(maxPad+6);  // add 3 virtual pads  before and 3 after
+      allBins[iRow] = new Float_t[maxBin];
+      allBinsRes[iRow] = new Float_t[maxBin];
+      memset(allBins[iRow],0,sizeof(Float_t)*maxBin);
+      memset(allBinsRes[iRow],0,sizeof(Float_t)*maxBin);
+    }
+    
+    // Loas the raw data for corresponding DDLs
+    rawReader->Reset();
+    AliTPCRawStream input(rawReader);
+    input.SetOldRCUFormat(fIsOldRCUFormat);
+    rawReader->Select(0,indexDDL,indexDDL+nDDLs-1);
+    
+    // Begin loop over altro data
+    while (input.Next()) {
+
+      if (input.GetSector() != fSector)
+       AliFatal(Form("Sector index mismatch ! Expected (%d), but got (%d) !",fSector,input.GetSector()));
+
+      if (input.GetTime() < 40) continue;
+      
+      Int_t iRow = input.GetRow();
+      if (iRow < 0 || iRow >= nRows)
+       AliFatal(Form("Pad-row index (%d) outside the range (%d -> %d) !",
+                     iRow, 0, nRows -1));
+
+      Int_t iPad = input.GetPad() + 3;
+
+      Int_t maxPad;
+      if (fSector < kNIS)
+       maxPad = fParam->GetNPadsLow(iRow);
+      else
+       maxPad = fParam->GetNPadsUp(iRow);
+
+      if (input.GetPad() < 0 || input.GetPad() >= maxPad)
+       AliFatal(Form("Pad index (%d) outside the range (%d -> %d) !",
+                     input.GetPad(), 0, maxPad -1));
+
+      Int_t iTimeBin = input.GetTime() + 3;
+      if ( input.GetTime() < 0 || input.GetTime() >= fParam->GetMaxTBin())
+       AliFatal(Form("Timebin index (%d) outside the range (%d -> %d) !",
+                     input.GetTime(), 0, fParam->GetMaxTBin() -1));
+
+      Int_t maxBin = fMaxTime*(maxPad+6);  // add 3 virtual pads  before and 3 after
+
+      if (((iPad*fMaxTime+iTimeBin) >= maxBin) ||
+         ((iPad*fMaxTime+iTimeBin) < 0))
+       AliFatal(Form("Index outside the allowed range"
+                     " Sector=%d Row=%d Pad=%d Timebin=%d"
+                     " (Max.index=%d)",fSector,iRow,iPad,iTimeBin,maxBin));
+
+      Float_t signal = input.GetSignal();
+      if (!fPedSubtraction && signal <= zeroSup) continue;
+
+      Float_t gain = gainROC->GetValue(iRow,input.GetPad());
+      allBins[iRow][iPad*fMaxTime+iTimeBin] = signal/gain;
+      allBins[iRow][iPad*fMaxTime+0] = 1;  // pad with signal
+    } // End of the loop over altro data
+
+    // Now loop over rows and perform pedestal subtraction
+    if (fPedSubtraction) {
+      for (Int_t iRow = 0; iRow < nRows; iRow++) {
+       Int_t maxPad;
+       if (fSector < kNIS)
+         maxPad = fParam->GetNPadsLow(iRow);
+       else
+         maxPad = fParam->GetNPadsUp(iRow);
+
+       for (Int_t iPad = 0; iPad < maxPad + 6; iPad++) {
+         if (allBins[iRow][iPad*fMaxTime+0] !=1) continue;  // no data
+         Float_t *p = &allBins[iRow][iPad*fMaxTime+3];
+         //Float_t pedestal = TMath::Median(fMaxTime, p);      
+         Int_t id[3] = {fSector, iRow, iPad-3};
+         Float_t pedestal = ProcesSignal(p, fMaxTime, id);
+         for (Int_t iTimeBin = 0; iTimeBin < fMaxTime; iTimeBin++) {
+           allBins[iRow][iPad*fMaxTime+iTimeBin] -= pedestal;
+           if (allBins[iRow][iPad*fMaxTime+iTimeBin] < zeroSup)
+             allBins[iRow][iPad*fMaxTime+iTimeBin] = 0;
+         }
+       }
       }
+    }
 
-      // ... prepare for the next pad row
+    // Now loop over rows and find clusters
+    for (fRow = 0; fRow < nRows; fRow++) {
       fRowCl = new AliTPCClustersRow;
       fRowCl->SetClass("AliTPCclusterMI");
       fRowCl->SetArray(1);
-
-      fRowCl->SetID(fParam->GetIndex(fSector, iRow));
+      fRowCl->SetID(fParam->GetIndex(fSector, fRow));
       fOutput->GetBranch("Segment")->SetAddress(&fRowCl);
-      fRx = fParam->GetPadRowRadii(fSector, iRow);
-    
-      if (fSector < kNIS) {
-       fMaxPad = fParam->GetNPadsLow(iRow);
-       fSign = (fSector < kNIS/2) ? 1 : -1;
-      } else {
-       fMaxPad = fParam->GetNPadsUp(iRow);
-       fSign = ((fSector-kNIS) < kNOS/2) ? 1 : -1;
-      }
-      fPadLength = fParam->GetPadPitchLength(fSector, iRow);
+
+      fRx = fParam->GetPadRowRadii(fSector, fRow);
+      fPadLength = fParam->GetPadPitchLength(fSector, fRow);
       fPadWidth  = fParam->GetPadPitchWidth();
-    
+      if (fSector < kNIS)
+       fMaxPad = fParam->GetNPadsLow(fRow);
+      else
+       fMaxPad = fParam->GetNPadsUp(fRow);
       fMaxBin = fMaxTime*(fMaxPad+6);  // add 3 virtual pads  before and 3 after
-      fBins    = new Int_t[fMaxBin];
-      fResBins = new Int_t[fMaxBin];  //fBins with residuals after 1 finder loop 
-      memset(fBins, 0, sizeof(Int_t)*fMaxBin);
 
-      prevSector = fSector;
-      prevRow = iRow;
-    }
+      fBins = allBins[fRow];
+      fResBins = allBinsRes[fRow];
 
-    // fill fBins with digits data
-    for (Int_t iWord = 0; iWord < nSkip; iWord++) {
-      if (buffer.GetNextBackWord() != 0x2AA) {
-       Error("Digits2Clusters", "could not read dummy data\n");
-       break;
-      }
-    }
-    while (nWords > 0) {
-      Int_t bunchLength = buffer.GetNextBackWord() - 2;
-      if (bunchLength < 0) {
-       Error("Digits2Clusters", "could not read bunch length\n");
-       break;
-      }
-      Int_t timeBin = buffer.GetNextBackWord();
-      if (timeBin < 0) {
-       Error("Digits2Clusters", "could not read time bin\n");
-       break;
-      }
-      for (Int_t iTimeBin = timeBin; iTimeBin > timeBin-bunchLength; iTimeBin--) {
-       Int_t dig = buffer.GetNextBackWord();
-       if (dig < 0) {
-         Error("Digits2Clusters", "could not read sample amplitude\n");
-         break;
-       }
-       dig += offset;
-       if (dig <= zeroSup) continue;
-       Int_t i = iPad + 3;
-       Int_t j = iTimeBin + 3;
-       fBins[i*fMaxTime+j] = dig;
-      }
-      nWords -= 2+bunchLength;
+      FindClusters();
+
+      fOutput->Fill();
+      delete fRowCl;    
+      nclusters += fNcluster;    
+    } // End of loop to find clusters
+
+    for (Int_t iRow = 0; iRow < nRows; iRow++) {
+      delete [] allBins[iRow];
+      delete [] allBinsRes[iRow];
     }
-  }  
 
-  // find clusters in the last pad row
-  if (fBins) {
-    FindClusters();
+    delete [] allBins;
+    delete [] allBinsRes;
 
-    fOutput->Fill();
-    delete fRowCl;    
-    nclusters += fNcluster;    
-    delete[] fBins;      
-    delete[] fResBins;  
-  }
+  } // End of loop over sectors
 
   Info("Digits2Clusters", "Number of found clusters : %d\n", nclusters);
 
-  fOutput->Write();
-  savedir->cd();
 }
 
 void AliTPCclustererMI::FindClusters()
@@ -683,7 +757,7 @@ void AliTPCclustererMI::FindClusters()
       amp0 = (amp1*amp1/amp2)*TMath::Exp(-1./sigma2);  
       if (gDebug>4) printf("\n%f\n",amp0);
     }  
-    fBins[i+2*fMaxTime] = Int_t(amp0);    
+    fBins[i+2*fMaxTime] = amp0;    
     amp0 = 0;
     amp1 = fBins[(fMaxPad+2)*fMaxTime+i];
     if (amp1>0){
@@ -693,7 +767,7 @@ void AliTPCclustererMI::FindClusters()
       amp0 = (amp1*amp1/amp2)*TMath::Exp(-1./sigma2);          
       if (gDebug>4) printf("\n%f\n",amp0);
     }        
-    fBins[(fMaxPad+3)*fMaxTime+i] = Int_t(amp0);           
+    fBins[(fMaxPad+3)*fMaxTime+i] = amp0;           
   }
 
 //  memcpy(fResBins,fBins, fMaxBin*2);
@@ -702,8 +776,8 @@ void AliTPCclustererMI::FindClusters()
   fNcluster=0;
   //first loop - for "gold cluster" 
   fLoop=1;
-  Int_t *b=&fBins[-1]+2*fMaxTime;
-  Int_t crtime = Int_t((fParam->GetZLength()-1.05*fRx)/fZWidth-5);
+  Float_t *b=&fBins[-1]+2*fMaxTime;
+  Int_t crtime = Int_t((fParam->GetZLength()-AliTPCReconstructor::GetCtgRange()*fRx)/fZWidth-fParam->GetNTBinsL1()-5);
 
   for (Int_t i=2*fMaxTime; i<fMaxBin-2*fMaxTime; i++) {
     b++;
@@ -737,3 +811,190 @@ void AliTPCclustererMI::FindClusters()
   }
   */
 }
+
+
+Double_t AliTPCclustererMI::ProcesSignal(Float_t *signal, Int_t nchannels, Int_t id[3]){
+  //
+  // process signal on given pad - + streaming of additional information in special mode
+  //
+  // id[0] - sector
+  // id[1] - row
+  // id[2] - pad  
+  Int_t offset =100;
+  Float_t kMin =50;
+  Float_t kMaxNoise = 3;
+  Double_t median = TMath::Median(nchannels-offset, &(signal[offset]));
+  if (AliLog::GetDebugLevel("","AliTPCclustererMI")==0) return median;
+  //
+
+  Double_t mean   = TMath::Mean(nchannels-offset, &(signal[offset]));
+  Double_t rms    = TMath::RMS(nchannels-offset, &(signal[offset]));
+  Double_t *dsignal = new Double_t[nchannels];
+  Double_t *dtime   = new Double_t[nchannels];
+  Float_t max    =  0;
+  Float_t maxPos =  0;
+  for (Int_t i=0; i<nchannels; i++){
+    dtime[i] = i;
+    dsignal[i] = signal[i];
+    if (signal[i]>max && i <fMaxTime-100) {  // temporary remove spike signals at the end
+      max = signal[i];
+      maxPos = i;
+    }    
+  }
+  
+  Double_t mean06=0,mean07=0, mean08=0, mean09=0;
+  Double_t rms06=0, rms07=0,  rms08, rms09=0; 
+  AliMathBase::EvaluateUni(nchannels-offset, &(dsignal[offset]), mean06, rms06, int(0.6*(nchannels-offset)));
+  AliMathBase::EvaluateUni(nchannels-offset, &(dsignal[offset]), mean07, rms07, int(0.7*(nchannels-offset)));
+  AliMathBase::EvaluateUni(nchannels-offset, &(dsignal[offset]), mean08, rms08, int(0.8*(nchannels-offset)));
+  AliMathBase::EvaluateUni(nchannels-offset, &(dsignal[offset]), mean09, rms09, int(0.9*(nchannels-offset)));
+  
+  //
+  UInt_t uid[3] = {UInt_t(id[0]),UInt_t(id[1]),UInt_t(id[2])};
+  if (uid[0]< AliTPCROC::Instance()->GetNSectors() 
+      && uid[1]<  AliTPCROC::Instance()->GetNRows(uid[0])  && 
+      uid[2] < AliTPCROC::Instance()->GetNPads(uid[0], uid[1])){
+    if (!fAmplitudeHisto){
+      fAmplitudeHisto = new TObjArray(72);
+    }
+    TObjArray  * sectorArray = (TObjArray*)fAmplitudeHisto->UncheckedAt(uid[0]);
+    if (!sectorArray){
+      Int_t npads = AliTPCROC::Instance()->GetNChannels(uid[0]);
+      sectorArray = new TObjArray(npads);
+      fAmplitudeHisto->AddAt(sectorArray, uid[0]);
+    }
+    Int_t position =  uid[2]+ AliTPCROC::Instance()->GetRowIndexes(uid[0])[uid[1]];
+    TH1F * histo = (TH1F*)sectorArray->UncheckedAt(position);
+    if (!histo){
+      char hname[100];
+      sprintf(hname,"Amp_%d_%d_%d",uid[0],uid[1],uid[2]);
+      TFile * backup = gFile;
+      fDebugStreamer->GetFile()->cd();
+      histo = new TH1F(hname, hname, 100, 1,100);
+      //histo->SetDirectory(0);     // histogram not connected to directory -(File)
+      sectorArray->AddAt(histo, position);
+      if (backup) backup->cd();
+    }
+    for (Int_t i=0; i<nchannels; i++){
+      if (signal[i]>0) histo->Fill(signal[i]);
+    }
+  }
+  //
+  TGraph * graph;
+  Bool_t random = (gRandom->Rndm()<0.0001);
+  if (max-median>kMin || rms06>2.*fParam->GetZeroSup() || random){
+    graph =new TGraph(nchannels, dtime, dsignal);
+    if (rms06>2.*fParam->GetZeroSup() || random)
+      (*fDebugStreamer)<<"SignalN"<<    //noise pads - or random sample of pads
+       "Sector="<<uid[0]<<
+       "Row="<<uid[1]<<
+       "Pad="<<uid[2]<<
+       "Graph.="<<graph<<
+       "Max="<<max<<
+       "MaxPos="<<maxPos<<
+       //
+       "Median="<<median<<
+       "Mean="<<mean<<
+       "RMS="<<rms<<      
+       "Mean06="<<mean06<<
+       "RMS06="<<rms06<<
+       "Mean07="<<mean07<<
+       "RMS07="<<rms07<<
+       "Mean08="<<mean08<<
+       "RMS08="<<rms08<<
+       "Mean09="<<mean09<<
+       "RMS09="<<rms09<<
+       "\n";
+    if (max-median>kMin) 
+      (*fDebugStreamer)<<"SignalB"<<     // pads with signal
+       "Sector="<<uid[0]<<
+       "Row="<<uid[1]<<
+       "Pad="<<uid[2]<<
+       "Graph.="<<graph<<
+       "Max="<<max<<
+       "MaxPos="<<maxPos<<     
+       //
+       "Median="<<median<<
+       "Mean="<<mean<<
+       "RMS="<<rms<<      
+       "Mean06="<<mean06<<
+       "RMS06="<<rms06<<
+       "Mean07="<<mean07<<
+       "RMS07="<<rms07<<
+       "Mean08="<<mean08<<
+       "RMS08="<<rms08<<
+       "Mean09="<<mean09<<
+       "RMS09="<<rms09<<
+       "\n";
+    delete graph;
+  }
+  
+  (*fDebugStreamer)<<"Signal"<<
+    "Sector="<<uid[0]<<
+    "Row="<<uid[1]<<
+    "Pad="<<uid[2]<<
+    "Max="<<max<<
+    "MaxPos="<<maxPos<<
+    //
+    "Median="<<median<<
+    "Mean="<<mean<<
+    "RMS="<<rms<<      
+    "Mean06="<<mean06<<
+    "RMS06="<<rms06<<
+    "Mean07="<<mean07<<
+    "RMS07="<<rms07<<
+    "Mean08="<<mean08<<
+    "RMS08="<<rms08<<
+    "Mean09="<<mean09<<
+    "RMS09="<<rms09<<
+    "\n";
+  delete [] dsignal;
+  delete [] dtime;
+  if (rms06>kMaxNoise) return 1024+median; // sign noisy channel in debug mode
+  return median;
+}
+
+
+
+void AliTPCclustererMI::DumpHistos(){
+  if (!fAmplitudeHisto) return;
+  for (UInt_t isector=0; isector<AliTPCROC::Instance()->GetNSectors(); isector++){
+    TObjArray * array = (TObjArray*)fAmplitudeHisto->UncheckedAt(isector);
+    if (!array) continue;
+    for (UInt_t ipad = 0; ipad <(UInt_t)array->GetEntriesFast(); ipad++){
+      TH1F * histo = (TH1F*) array->UncheckedAt(ipad);
+      if (!histo) continue;
+      if (histo->GetEntries()<100) continue;
+      histo->Fit("gaus","q");
+      Float_t mean =  histo->GetMean();
+      Float_t rms  =  histo->GetRMS();
+      Float_t gmean = histo->GetFunction("gaus")->GetParameter(1);
+      Float_t gsigma = histo->GetFunction("gaus")->GetParameter(2);
+      Float_t max = histo->GetFunction("gaus")->GetParameter(0);
+
+      // get pad number
+      UInt_t row=0, pad =0;
+      const UInt_t *indexes = AliTPCROC::Instance()->GetRowIndexes(isector);
+      for (UInt_t irow=0; irow< AliTPCROC::Instance()->GetNRows(isector); irow++){
+       if (indexes[irow]<ipad){
+         row = irow;
+         pad = ipad-indexes[irow];
+       }
+      }      
+      Int_t rpad = pad - (AliTPCROC::Instance()->GetNPads(isector,row))/2;
+      //
+      (*fDebugStreamer)<<"Fit"<<
+       "Sector="<<isector<<
+       "Row="<<row<<
+       "Pad="<<pad<<
+       "RPad="<<rpad<<
+       "Max="<<max<<
+       "Mean="<<mean<<
+       "RMS="<<rms<<      
+       "GMean="<<gmean<<
+       "GSigma="<<gsigma<<
+       "\n";
+      if (array->UncheckedAt(ipad)) fDebugStreamer->StoreObject(array->UncheckedAt(ipad));
+    }
+  }
+}