]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PMD/AliPMDClusterFinder.cxx
Preprocessor modified not to send the correct gain file
[u/mrichter/AliRoot.git] / PMD / AliPMDClusterFinder.cxx
index dde50851ddd2bd7dd76365b70e003686199bee8d..23bcbb7f35ba9ebad9150f9e1eaeb641997ff39b 100644 (file)
@@ -26,6 +26,7 @@
 #include <TTree.h>
 #include <TObjArray.h>
 #include <TClonesArray.h>
+#include <TSystem.h>
 
 #include "AliLog.h"
 #include "AliRunLoader.h"
 #include "AliPMDrechit.h"
 #include "AliPMDRawStream.h"
 #include "AliPMDCalibData.h"
+#include "AliPMDPedestal.h"
 #include "AliPMDddldata.h"
+#include "AliPMDHotData.h"
+#include "AliPMDNoiseCut.h"
+#include "AliPMDRecoParam.h"
+#include "AliPMDReconstructor.h"
 
 #include "AliDAQ.h"
 #include "AliCDBManager.h"
@@ -54,7 +60,11 @@ ClassImp(AliPMDClusterFinder)
 AliPMDClusterFinder::AliPMDClusterFinder():
   fRunLoader(0),
   fPMDLoader(0),
-  fCalibData(GetCalibData()),
+  fCalibGain(GetCalibGain()),
+  fCalibPed(GetCalibPed()),
+  fCalibHot(GetCalibHot()),
+  fNoiseCut(GetNoiseCut()),
+  fRecoParam(0x0),
   fTreeD(0),
   fTreeR(0),
   fDigits(new TClonesArray("AliPMDdigit", 1000)),
@@ -62,8 +72,7 @@ AliPMDClusterFinder::AliPMDClusterFinder():
   fRechits(new TClonesArray("AliPMDrechit", 1000)),
   fNpoint(0),
   fNhit(0),
-  fDetNo(0),
-  fEcut(0.)
+  fDetNo(0)
 {
 //
 // Constructor
@@ -73,7 +82,11 @@ AliPMDClusterFinder::AliPMDClusterFinder():
 AliPMDClusterFinder::AliPMDClusterFinder(AliRunLoader* runLoader):
   fRunLoader(runLoader),
   fPMDLoader(runLoader->GetLoader("PMDLoader")),
-  fCalibData(GetCalibData()),
+  fCalibGain(GetCalibGain()),
+  fCalibPed(GetCalibPed()),
+  fCalibHot(GetCalibHot()),
+  fNoiseCut(GetNoiseCut()),
+  fRecoParam(0x0),
   fTreeD(0),
   fTreeR(0),
   fDigits(new TClonesArray("AliPMDdigit", 1000)),
@@ -81,8 +94,7 @@ AliPMDClusterFinder::AliPMDClusterFinder(AliRunLoader* runLoader):
   fRechits(new TClonesArray("AliPMDrechit", 1000)),
   fNpoint(0),
   fNhit(0),
-  fDetNo(0),
-  fEcut(0.)
+  fDetNo(0)
 {
 //
 // Constructor
@@ -93,7 +105,11 @@ AliPMDClusterFinder::AliPMDClusterFinder(const AliPMDClusterFinder & finder):
   TObject(finder),
   fRunLoader(0),
   fPMDLoader(0),
-  fCalibData(GetCalibData()),
+  fCalibGain(GetCalibGain()),
+  fCalibPed(GetCalibPed()),
+  fCalibHot(GetCalibHot()),
+  fNoiseCut(GetNoiseCut()),
+  fRecoParam(0x0),
   fTreeD(0),
   fTreeR(0),
   fDigits(NULL),
@@ -101,8 +117,7 @@ AliPMDClusterFinder::AliPMDClusterFinder(const AliPMDClusterFinder & finder):
   fRechits(NULL),
   fNpoint(0),
   fNhit(0),
-  fDetNo(0),
-  fEcut(0.)
+  fDetNo(0)
 {
   // copy constructor
   AliError("Copy constructor not allowed");
@@ -120,30 +135,27 @@ AliPMDClusterFinder::~AliPMDClusterFinder()
   // Destructor
   if (fDigits)
     {
-      fDigits->Delete();
-      delete fDigits;
-      fDigits=0;
+      fDigits->Clear();
     }
   if (fRecpoints)
     {
-      fRecpoints->Delete();
-      delete fRecpoints;
-      fRecpoints=0;
+      fRecpoints->Clear();
     }
   if (fRechits)
     {
-      fRechits->Delete();
-      delete fRechits;
-      fRechits=0;
+      fRechits->Clear();
     }
+
 }
 // ------------------------------------------------------------------------- //
 
-void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt)
+void AliPMDClusterFinder::Digits2RecPoints(TTree *digitsTree,
+                                          TTree *clustersTree)
 {
   // Converts digits to recpoints after running clustering
   // algorithm on CPV plane and PREshower plane
   //
+  // This algorithm is called during the reconstruction from digits
 
   Int_t    det = 0,smn = 0;
   Int_t    xpos,ypos;
@@ -152,43 +164,39 @@ void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt)
   Int_t    idet;
   Float_t  clusdata[6];
 
-  TObjArray *pmdcont = new TObjArray();
-  AliPMDClustering *pmdclust = new AliPMDClusteringV1();
+  AliPMDcluster *pmdcl = 0x0;
 
-  pmdclust->SetEdepCut(fEcut);
+  TObjArray *pmdcont = new TObjArray();
 
-  fRunLoader->GetEvent(ievt);
+  AliPMDClustering *pmdclust = new AliPMDClusteringV1();
 
+  // Fetch the reco param object
 
-  fTreeD = fPMDLoader->TreeD();
-  if (fTreeD == 0x0)
+  fRecoParam = AliPMDReconstructor::GetRecoParam();
+  if(fRecoParam == 0x0)
     {
-      AliFatal("AliPMDClusterFinder: Can not get TreeD");
-
+       AliFatal("No Reco Param found for PMD!!!");
     }
+
+
   AliPMDdigit  *pmddigit;
-  TBranch *branch = fTreeD->GetBranch("PMDDigit");
+  TBranch *branch = digitsTree->GetBranch("PMDDigit");
   branch->SetAddress(&fDigits);
 
   ResetRecpoint();
 
-  fTreeR = fPMDLoader->TreeR();
-  if (fTreeR == 0x0)
-    {
-      fPMDLoader->MakeTree("R");
-      fTreeR = fPMDLoader->TreeR();
-    }
-
   Int_t bufsize = 16000;
-  TBranch * branch1 = fTreeR->Branch("PMDRecpoint", &fRecpoints, bufsize); 
-  TBranch * branch2 = fTreeR->Branch("PMDRechit", &fRechits, bufsize); 
+  TBranch * branch1 = clustersTree->Branch("PMDRecpoint", &fRecpoints, bufsize); 
+  TBranch * branch2 = clustersTree->Branch("PMDRechit", &fRechits, bufsize); 
 
-  Int_t nmodules = (Int_t) fTreeD->GetEntries();
+  Int_t nmodules = (Int_t) digitsTree->GetEntries();
 
   for (Int_t imodule = 0; imodule < nmodules; imodule++)
     {
+
+      Int_t totADCMod = 0;
       ResetCellADC();
-      fTreeD->GetEntry(imodule); 
+      digitsTree->GetEntry(imodule); 
       Int_t nentries = fDigits->GetLast();
       for (Int_t ient = 0; ient < nentries+1; ient++)
        {
@@ -199,126 +207,71 @@ void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt)
          xpos   = pmddigit->GetRow();
          ypos   = pmddigit->GetColumn();
          adc    = pmddigit->GetADC();
-         
-         // CALIBRATION
-         Float_t gain = fCalibData->GetGainFact(det,smn,xpos,ypos);
-         // printf("adc = %d gain = %f\n",adc,gain);
-         
-         adc = adc*gain;
-
-         //Int_t trno   = pmddigit->GetTrackNumber();
-         fCellADC[xpos][ypos] = (Double_t) adc;
-       }
 
-      idet = det;
-      ismn = smn;
-      pmdclust->DoClust(idet,ismn,fCellADC,pmdcont);
-      
-      Int_t nentries1 = pmdcont->GetEntries();
-
-      AliDebug(1,Form("Total number of clusters/module = %d",nentries1));
-
-      for (Int_t ient1 = 0; ient1 < nentries1; ient1++)
-       {
-         AliPMDcluster *pmdcl = (AliPMDcluster*)pmdcont->UncheckedAt(ient1);
-         idet        = pmdcl->GetDetector();
-         ismn        = pmdcl->GetSMN();
-         clusdata[0] = pmdcl->GetClusX();
-         clusdata[1] = pmdcl->GetClusY();
-         clusdata[2] = pmdcl->GetClusADC();
-         clusdata[3] = pmdcl->GetClusCells();
-         clusdata[4] = pmdcl->GetClusSigmaX();
-         clusdata[5] = pmdcl->GetClusSigmaY();
-
-         AddRecPoint(idet,ismn,clusdata);
+         if(det < 0 || det > 1)
+           {
+             AliError(Form("*CPV/PRE NUMBER WRONG %d *",det));
+             continue; 
+           }
+         if(smn == -1 || smn > 23)
+           {
+             AliError(Form("*MODULE NUMBER WRONG %d *",smn));
+             continue; 
+           }
 
-         Int_t ncell = (Int_t) clusdata[3];
-         for(Int_t ihit = 0; ihit < ncell; ihit++)
+         if(xpos < 0 || xpos > 47 || ypos < 0 || ypos > 95)
            {
-             Int_t celldataX = pmdcl->GetClusCellX(ihit);
-             Int_t celldataY = pmdcl->GetClusCellY(ihit);
-             AddRecHit(celldataX, celldataY);
+             AliError(Form("*Row %d and Column NUMBER %d NOT Valid *",
+                           xpos, ypos));
+             continue; 
            }
-         branch2->Fill();
-         ResetRechit();
-       }
-      pmdcont->Clear();
-      
-      branch1->Fill();
-      ResetRecpoint();
+         
+         // Pedestal Subtraction
+         Int_t   pedmeanrms = fCalibPed->GetPedMeanRms(det,smn,xpos,ypos);
+         Int_t   pedrms1    = (Int_t) pedmeanrms%100;
+         Float_t pedrms     = (Float_t)pedrms1/10.;
+         Float_t pedmean    = (Float_t) (pedmeanrms - pedrms1)/1000.0;
+         //printf("%f %f\n",pedmean, pedrms);
 
-    } // modules
+         Float_t adc1 = adc - (pedmean + 3.0*pedrms);
 
-  ResetCellADC();
-  fPMDLoader = fRunLoader->GetLoader("PMDLoader");  
-  fPMDLoader->WriteRecPoints("OVERWRITE");
+         // Hot cell - set the cell adc = 0
+         Float_t hotflag = fCalibHot->GetHotChannel(det,smn,xpos,ypos);
+         if (hotflag == 1.) adc1 = 0;
 
-  //   delete the pointers
-  delete pmdclust;
-  delete pmdcont;
-    
-}
-// ------------------------------------------------------------------------- //
+         // CALIBRATION
+         Float_t gain = fCalibGain->GetGainFact(det,smn,xpos,ypos);
+         // printf("adc = %d gain = %f\n",adc,gain);
 
-void AliPMDClusterFinder::Digits2RecPoints(TTree *digitsTree,
-                                          TTree *clustersTree)
-{
-  // Converts digits to recpoints after running clustering
-  // algorithm on CPV plane and PREshower plane
-  //
+         adc = adc1*gain;
 
-  Int_t    det = 0,smn = 0;
-  Int_t    xpos,ypos;
-  Float_t  adc;
-  Int_t    ismn;
-  Int_t    idet;
-  Float_t  clusdata[6];
+         fCellTrack[xpos][ypos] = pmddigit->GetTrackNumber();
+         fCellPid[xpos][ypos] = pmddigit->GetTrackPid();
+         fCellADC[xpos][ypos] = (Double_t) adc;
 
-  TObjArray *pmdcont = new TObjArray();
-  AliPMDClustering *pmdclust = new AliPMDClusteringV1();
+         totADCMod += (Int_t) adc;
 
-  pmdclust->SetEdepCut(fEcut);
+       }
 
-  AliPMDdigit  *pmddigit;
-  TBranch *branch = digitsTree->GetBranch("PMDDigit");
-  branch->SetAddress(&fDigits);
+      idet = det;
+      ismn = smn;
 
-  ResetRecpoint();
+      if (totADCMod <= 0) continue;
 
-  Int_t bufsize = 16000;
-  TBranch * branch1 = clustersTree->Branch("PMDRecpoint", &fRecpoints, bufsize); 
-  TBranch * branch2 = clustersTree->Branch("PMDRechit", &fRechits, bufsize); 
+      // Set the minimum noise cut per module before clustering
 
-  Int_t nmodules = (Int_t) digitsTree->GetEntries();
+      // Int_t imod = idet*24 + ismn;
 
-  for (Int_t imodule = 0; imodule < nmodules; imodule++)
-    {
-      ResetCellADC();
-      digitsTree->GetEntry(imodule); 
-      Int_t nentries = fDigits->GetLast();
-      for (Int_t ient = 0; ient < nentries+1; ient++)
-       {
-         pmddigit = (AliPMDdigit*)fDigits->UncheckedAt(ient);
-         
-         det    = pmddigit->GetDetector();
-         smn    = pmddigit->GetSMNumber();
-         xpos   = pmddigit->GetRow();
-         ypos   = pmddigit->GetColumn();
-         adc    = pmddigit->GetADC();
-         
-         // CALIBRATION
-         Float_t gain = fCalibData->GetGainFact(det,smn,xpos,ypos);
-         // printf("adc = %d gain = %f\n",adc,gain);
-         
-         adc = adc*gain;
 
-         //Int_t trno   = pmddigit->GetTrackNumber();
-         fCellADC[xpos][ypos] = (Double_t) adc;
-       }
+      // Int_t cluspar = fRecoParam->GetPbPbParam()->GetClusteringParam();
+      Int_t cluspar = fRecoParam->GetPPParam()->GetClusteringParam();
+      // Int_t cluspar = fRecoParam->GetCosmicParam()->GetClusteringParam();
 
-      idet = det;
-      ismn = smn;
-      pmdclust->DoClust(idet,ismn,fCellADC,pmdcont);
+      pmdclust->SetClusteringParam(cluspar);
+
+      Float_t encut = 4.;
+      pmdclust->SetEdepCut(encut);
+      pmdclust->DoClust(idet,ismn,fCellTrack,fCellPid,fCellADC,pmdcont);
       
       Int_t nentries1 = pmdcont->GetEntries();
 
@@ -326,7 +279,7 @@ void AliPMDClusterFinder::Digits2RecPoints(TTree *digitsTree,
 
       for (Int_t ient1 = 0; ient1 < nentries1; ient1++)
        {
-         AliPMDcluster *pmdcl = (AliPMDcluster*)pmdcont->UncheckedAt(ient1);
+         pmdcl = (AliPMDcluster*)pmdcont->UncheckedAt(ient1);
          idet        = pmdcl->GetDetector();
          ismn        = pmdcl->GetSMN();
          clusdata[0] = pmdcl->GetClusX();
@@ -339,28 +292,32 @@ void AliPMDClusterFinder::Digits2RecPoints(TTree *digitsTree,
          AddRecPoint(idet,ismn,clusdata);
 
          Int_t ncell = (Int_t) clusdata[3];
+         if (ncell > 19) ncell = 19;
          for(Int_t ihit = 0; ihit < ncell; ihit++)
            {
              Int_t celldataX = pmdcl->GetClusCellX(ihit);
              Int_t celldataY = pmdcl->GetClusCellY(ihit);
-             AddRecHit(celldataX, celldataY);
+             Int_t celldataTr = pmdcl->GetClusCellTrack(ihit);
+             Int_t celldataPid   = pmdcl->GetClusCellPid(ihit);
+             Float_t celldataAdc = pmdcl->GetClusCellAdc(ihit);
+             AddRecHit(celldataX, celldataY, celldataTr, celldataPid, celldataAdc);
            }
          branch2->Fill();
          ResetRechit();
        }
-      pmdcont->Clear();
-      
+      pmdcont->Delete();
+
       branch1->Fill();
       ResetRecpoint();
 
     } // modules
 
+
   ResetCellADC();
 
   //   delete the pointers
   delete pmdclust;
   delete pmdcont;
-    
 }
 // ------------------------------------------------------------------------- //
 
@@ -370,16 +327,55 @@ void AliPMDClusterFinder::Digits2RecPoints(AliRawReader *rawReader,
   // Converts RAW data to recpoints after running clustering
   // algorithm on CPV and PREshower plane
   //
-  // This method is called at the time of reconstruction
+  // This method is called at the time of reconstruction from RAW data
+
 
+  AliPMDddldata *pmdddl = 0x0;
+  AliPMDcluster *pmdcl  = 0x0;
 
   Float_t  clusdata[6];
   TObjArray pmdddlcont;
 
   TObjArray *pmdcont = new TObjArray();
+
   AliPMDClustering *pmdclust = new AliPMDClusteringV1();
 
-  pmdclust->SetEdepCut(fEcut);
+  // open the ddl file info to know the module
+  TString ddlinfofileName(gSystem->Getenv("ALICE_ROOT"));
+  ddlinfofileName += "/PMD/PMD_ddl_info.dat";
+  
+  ifstream infileddl;
+  infileddl.open(ddlinfofileName.Data(), ios::in); // ascii file
+  if(!infileddl) AliError("Could not read the ddl info file");
+
+  Int_t ddlno;
+  Int_t modno;
+  Int_t modulePerDDL;
+  Int_t moduleddl[6];
+
+  for(Int_t jddl = 0; jddl < 6; jddl++)
+    {
+      if (infileddl.eof()) break;
+      infileddl >> ddlno >> modulePerDDL;
+      moduleddl[jddl] = modulePerDDL;
+
+      if (modulePerDDL == 0) continue;
+      for (Int_t im = 0; im < modulePerDDL; im++)
+       {
+         infileddl >> modno;
+       }
+    }
+
+  infileddl.close();
+
+  // Set the minimum noise cut per module before clustering
+
+  fRecoParam = AliPMDReconstructor::GetRecoParam();
+
+  if(fRecoParam == 0x0)
+    {
+       AliFatal("No Reco Param found for PMD!!!");
+    }
 
   ResetRecpoint();
 
@@ -388,24 +384,19 @@ void AliPMDClusterFinder::Digits2RecPoints(AliRawReader *rawReader,
 
   TBranch * branch2 = clustersTree->Branch("PMDRechit", &fRechits, bufsize); 
 
-  const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
   const Int_t kRow = 48;
   const Int_t kCol = 96;
 
   Int_t idet = 0;
   Int_t iSMN = 0;
 
+  Int_t indexDDL = -1;
+  AliPMDRawStream pmdinput(rawReader);
 
-  for (Int_t indexDDL = 0; indexDDL < kDDL; indexDDL++)
+  while ((indexDDL = pmdinput.DdlData(&pmdddlcont)) >=0)
     {
-      if (indexDDL < 4)
-       {
-         iSMN = 6;
-       }
-      else if (indexDDL >= 4)
-       {
-         iSMN = 12;
-       }
+      iSMN = moduleddl[indexDDL];
+
       Int_t ***precpvADC;
       precpvADC = new int **[iSMN];
       for (Int_t i=0; i<iSMN; i++) precpvADC[i] = new int *[kRow];
@@ -424,18 +415,12 @@ void AliPMDClusterFinder::Digits2RecPoints(AliRawReader *rawReader,
            }
        }
       ResetCellADC();
-      rawReader->Reset();
-      AliPMDRawStream pmdinput(rawReader);
-
-      rawReader->Select("PMD", indexDDL, indexDDL);
-
-      pmdinput.DdlData(indexDDL,&pmdddlcont);
 
       Int_t indexsmn = 0;
       Int_t ientries = pmdddlcont.GetEntries();
       for (Int_t ient = 0; ient < ientries; ient++)
        {
-         AliPMDddldata *pmdddl = (AliPMDddldata*)pmdddlcont.UncheckedAt(ient);
+         pmdddl = (AliPMDddldata*)pmdddlcont.UncheckedAt(ient);
          
          Int_t det = pmdddl->GetDetector();
          Int_t smn = pmdddl->GetSMN();
@@ -445,243 +430,65 @@ void AliPMDClusterFinder::Digits2RecPoints(AliRawReader *rawReader,
          Int_t col = pmdddl->GetColumn();
          Int_t sig = pmdddl->GetSignal();
 
-         Float_t sig1 = (Float_t) sig;
-         // CALIBRATION
-         Float_t gain = fCalibData->GetGainFact(det,smn,row,col);
-         //printf("sig = %d gain = %f\n",sig,gain);
-         sig = (Int_t) (sig1*gain);
 
-         if (indexDDL < 4)
+         if(det < 0 || det > 1)
            {
-             if (det != 0)
-               AliError(Form("*DDL %d and Detector NUMBER %d NOT MATCHING *",
-                             indexDDL, det));
-             indexsmn = smn - indexDDL * 6;
+             AliError(Form("*CPV/PRE NUMBER WRONG %d *",det));
+             continue; 
            }
-         else if (indexDDL == 4)
+         if(smn < 0 || smn > 23)
            {
-             if (det != 1)
-               AliError(Form("*DDL %d and Detector NUMBER %d NOT MATCHING *",
-                             indexDDL, det));
-             if (smn < 6)
-               {
-                 indexsmn = smn;
-               }
-             else if (smn >= 18 && smn < 24)
-               {
-                 indexsmn = smn - 12;
-               }
+             AliError(Form("*MODULE NUMBER WRONG %d *",smn));
+             continue; 
            }
-         else if (indexDDL == 5)
+         if(row < 0 || row > 47 || col < 0 || col > 95)
            {
-             if (det != 1)
-               AliError(Form("*DDL %d and Detector NUMBER %d NOT MATCHING *",
-                             indexDDL, det));
-             if (smn >= 6 && smn < 12)
-               {
-                 indexsmn = smn - 6;
-               }
-             else if (smn >= 12 && smn < 18)
-               {
-                 indexsmn = smn - 6;
-               }
-           }         
-         precpvADC[indexsmn][row][col] = sig;
-       }
-      
-      pmdddlcont.Clear();
+             AliError(Form("*Row %d and Column NUMBER %d NOT Valid *",
+                           row, col));
 
-      Int_t ismn = 0;
-      for (Int_t indexsmn = 0; indexsmn < iSMN; indexsmn++)
-       {
-         ResetCellADC();
-         for (Int_t irow = 0; irow < kRow; irow++)
-           {
-             for (Int_t icol = 0; icol < kCol; icol++)
-               {
-                 fCellADC[irow][icol] = 
-                   (Double_t) precpvADC[indexsmn][irow][icol];
-               } // row
-           }     // col
-         
-         if (indexDDL < 4)
-           {
-             ismn = indexsmn + indexDDL * 6;
-             idet = 0;
+             continue; 
            }
-         else if (indexDDL == 4)
-           {
-             if (indexsmn < 6)
-               {
-                 ismn = indexsmn;
-               }
-             else if (indexsmn >= 6 && indexsmn < 12)
-               {
-                 ismn = indexsmn + 12;
-               }
-             idet = 1;
-           }
-         else if (indexDDL == 5)
-           {
-             if (indexsmn < 6)
-               {
-                 ismn = indexsmn + 6;
-               }
-             else if (indexsmn >= 6 && indexsmn < 12)
-               {
-                 ismn = indexsmn + 6;
-               }
-             idet = 1;
-           }
-
-         pmdclust->DoClust(idet,ismn,fCellADC,pmdcont);
-         Int_t nentries1 = pmdcont->GetEntries();
-
-         AliDebug(1,Form("Total number of clusters/module = %d",nentries1));
-
-         for (Int_t ient1 = 0; ient1 < nentries1; ient1++)
-           {
-             AliPMDcluster *pmdcl = 
-               (AliPMDcluster*)pmdcont->UncheckedAt(ient1);
-             idet        = pmdcl->GetDetector();
-             ismn        = pmdcl->GetSMN();
-             clusdata[0] = pmdcl->GetClusX();
-             clusdata[1] = pmdcl->GetClusY();
-             clusdata[2] = pmdcl->GetClusADC();
-             clusdata[3] = pmdcl->GetClusCells();
-             clusdata[4] = pmdcl->GetClusSigmaX();
-             clusdata[5] = pmdcl->GetClusSigmaY();
-
-             AddRecPoint(idet,ismn,clusdata);
-
-             Int_t ncell = (Int_t) clusdata[3];
-             for(Int_t ihit = 0; ihit < ncell; ihit++)
-               {
-                 Int_t celldataX = pmdcl->GetClusCellX(ihit);
-                 Int_t celldataY = pmdcl->GetClusCellY(ihit);
-                 AddRecHit(celldataX, celldataY);
-               }
-             branch2->Fill();
-             ResetRechit();
-
-           }
-         pmdcont->Clear();
-         
-         branch1->Fill();
-         ResetRecpoint();
-
-
-       } // smn
 
-      for (Int_t i=0; i<iSMN; i++)
-       {
-         for (Int_t j=0; j<kRow; j++) delete [] precpvADC[i][j];
-       }
-      for (Int_t i=0; i<iSMN; i++) delete [] precpvADC[i];
-      delete precpvADC;
-    } // DDL Loop
-  
-  ResetCellADC();
-  
-  //   delete the pointers
-  delete pmdclust;
-  delete pmdcont;
 
-}
-// ------------------------------------------------------------------------- //
-
-void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt, AliRawReader *rawReader)
-{
-  // Converts RAW data to recpoints after running clustering
-  // algorithm on CPV and PREshower plane
-  //
-
-  Float_t  clusdata[6];
-  TObjArray pmdddlcont;
-  TObjArray *pmdcont = new TObjArray();
-
-  AliPMDClustering *pmdclust = new AliPMDClusteringV1();
+         // Pedestal Subtraction
+         Int_t   pedmeanrms = fCalibPed->GetPedMeanRms(det,smn,row,col);
+         Int_t   pedrms1    = (Int_t) pedmeanrms%100;
+         Float_t pedrms     = (Float_t)pedrms1/10.;
+         Float_t pedmean    = (Float_t) (pedmeanrms - pedrms1)/1000.0;
 
-  pmdclust->SetEdepCut(fEcut);
+         //printf("%f %f\n",pedmean, pedrms);
 
-  fRunLoader->GetEvent(ievt);
+         // Float_t sig1 = (Float_t) sig;
+         Float_t sig1 = (Float_t) sig - (pedmean + 3.0*pedrms);
 
-  ResetRecpoint();
 
-  fTreeR = fPMDLoader->TreeR();
-  if (fTreeR == 0x0)
-    {
-      fPMDLoader->MakeTree("R");
-      fTreeR = fPMDLoader->TreeR();
-    }
-  Int_t bufsize = 16000;
-  TBranch *branch1 = fTreeR->Branch("PMDRecpoint", &fRecpoints, bufsize); 
-  TBranch *branch2 = fTreeR->Branch("PMDRechit", &fRechits, bufsize); 
+         // Hot cell - set the cell adc = 0
+         Float_t hotflag = fCalibHot->GetHotChannel(det,smn,row,col);
+         if (hotflag == 1.) sig1 = 0;
 
-  const Int_t kDDL = AliDAQ::NumberOfDdls("PMD");
-  const Int_t kRow = 48;
-  const Int_t kCol = 96;
+         // CALIBRATION
+         Float_t gain = fCalibGain->GetGainFact(det,smn,row,col);
+         //printf("sig = %d gain = %f\n",sig,gain);
+         sig = (Int_t) (sig1*gain);
 
-  Int_t idet = 0;
-  Int_t iSMN = 0;
-  
-  for (Int_t indexDDL = 0; indexDDL < kDDL; indexDDL++)
-    {
-      if (indexDDL < 4)
-       {
-         iSMN = 6;
-       }
-      else if (indexDDL >= 4)
-       {
-         iSMN = 12;
-       }
-      Int_t ***precpvADC;
-      precpvADC = new int **[iSMN];
-      for (Int_t i=0; i<iSMN; i++) precpvADC[i] = new int *[kRow];
-      for (Int_t i=0; i<iSMN;i++)
-       {
-         for (Int_t j=0; j<kRow; j++) precpvADC[i][j] = new int [kCol];
-       }
-      for (Int_t i = 0; i < iSMN; i++)
-       {
-         for (Int_t j = 0; j < kRow; j++)
+         if (indexDDL == 0)
            {
-             for (Int_t k = 0; k < kCol; k++)
+             if (det != 0)
+               AliError(Form("*DDL %d and Detector NUMBER %d NOT MATCHING *",
+                             indexDDL, det));
+             if (iSMN == 6)
                {
-                 precpvADC[i][j][k] = 0;
+                 indexsmn = smn;
+               }
+             else if (iSMN == 12)
+               {
+                 if (smn < 6)
+                   indexsmn = smn;
+                 else if (smn >= 18 && smn < 24)
+                   indexsmn = smn-12;
                }
            }
-       }
-      ResetCellADC();
-      rawReader->Reset();
-      rawReader->Select("PMD", indexDDL, indexDDL);
-
-      AliPMDRawStream pmdinput(rawReader);
-      pmdinput.DdlData(indexDDL,&pmdddlcont);
-    
-      Int_t indexsmn = 0;
-      Int_t ientries = pmdddlcont.GetEntries();
-      for (Int_t ient = 0; ient < ientries; ient++)
-       {
-         AliPMDddldata *pmdddl = (AliPMDddldata*)pmdddlcont.UncheckedAt(ient);
-         
-         Int_t det = pmdddl->GetDetector();
-         Int_t smn = pmdddl->GetSMN();
-         //Int_t mcm = pmdddl->GetMCM();
-         //Int_t chno = pmdddl->GetChannel();
-         Int_t row = pmdddl->GetRow();
-         Int_t col = pmdddl->GetColumn();
-         Int_t sig = pmdddl->GetSignal();
-
-         Float_t sig1 = (Float_t) sig;
-         // CALIBRATION
-         Float_t gain = fCalibData->GetGainFact(det,smn,row,col);
-
-         //printf("sig = %d gain = %f\n",sig,gain);
-         sig = (Int_t) (sig1*gain);
-
-
-         if (indexDDL < 4)
+         else if (indexDDL >= 1 && indexDDL < 4)
            {
              if (det != 0)
                AliError(Form("*DDL %d and Detector NUMBER %d NOT MATCHING *",
@@ -707,36 +514,54 @@ void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt, AliRawReader *rawReader)
              if (det != 1)
                AliError(Form("*DDL %d and Detector NUMBER %d NOT MATCHING *",
                              indexDDL, det));
-             if (smn >= 6 && smn < 12)
-               {
-                 indexsmn = smn - 6;
-               }
-             else if (smn >= 12 && smn < 18)
+             if (smn >= 6 && smn < 18)
                {
                  indexsmn = smn - 6;
                }
            }         
-         precpvADC[indexsmn][row][col] = sig;
 
+         precpvADC[indexsmn][row][col] = sig;
        }
       
-      pmdddlcont.Clear();
+      pmdddlcont.Delete();
+
+      Int_t totAdcMod = 0;
 
       Int_t ismn = 0;
-      for (Int_t indexsmn = 0; indexsmn < iSMN; indexsmn++)
+      for (indexsmn = 0; indexsmn < iSMN; indexsmn++)
        {
          ResetCellADC();
+         totAdcMod = 0;
          for (Int_t irow = 0; irow < kRow; irow++)
            {
              for (Int_t icol = 0; icol < kCol; icol++)
                {
+                 fCellTrack[irow][icol] = -1;
+                 fCellPid[irow][icol]   = -1;
+
                  fCellADC[irow][icol] = 
                    (Double_t) precpvADC[indexsmn][irow][icol];
+                 totAdcMod += precpvADC[indexsmn][irow][icol];
                } // row
            }     // col
-
          
-         if (indexDDL < 4)
+         if (indexDDL == 0)
+           {
+             if (iSMN == 6)
+               {
+                 ismn = indexsmn;
+               }
+             else if (iSMN == 12)
+               {
+                 
+                 if (indexsmn < 6)
+                   ismn = indexsmn;
+                 else if (indexsmn >= 6 && indexsmn < 12)
+                   ismn = indexsmn + 12;
+               }
+             idet = 0;
+           }
+         else if (indexDDL >= 1 && indexDDL < 4)
            {
              ismn = indexsmn + indexDDL * 6;
              idet = 0;
@@ -755,26 +580,30 @@ void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt, AliRawReader *rawReader)
            }
          else if (indexDDL == 5)
            {
-             if (indexsmn < 6)
-               {
-                 ismn = indexsmn + 6;
-               }
-             else if (indexsmn >= 6 && indexsmn < 12)
-               {
-                 ismn = indexsmn + 6;
-               }
+             ismn = indexsmn + 6;
              idet = 1;
            }
 
-         pmdclust->DoClust(idet,ismn,fCellADC,pmdcont);
+         if (totAdcMod <= 0) continue;
+
+         Int_t imod = idet*24 + ismn;
+
+         // Int_t cluspar = fRecoParam->GetPbPbParam()->GetClusteringParam();
+         Int_t cluspar = fRecoParam->GetPPParam()->GetClusteringParam();
+         // Int_t cluspar = fRecoParam->GetCosmicParam()->GetClusteringParam();
+         pmdclust->SetClusteringParam(cluspar);
+         Float_t encut = fNoiseCut->GetNoiseCut(imod);
+
+         pmdclust->SetEdepCut(encut);
+         pmdclust->DoClust(idet,ismn,fCellTrack,fCellPid,fCellADC,pmdcont);
+
          Int_t nentries1 = pmdcont->GetEntries();
 
          AliDebug(1,Form("Total number of clusters/module = %d",nentries1));
 
          for (Int_t ient1 = 0; ient1 < nentries1; ient1++)
            {
-             AliPMDcluster *pmdcl = 
-               (AliPMDcluster*)pmdcont->UncheckedAt(ient1);
+             pmdcl = (AliPMDcluster*)pmdcont->UncheckedAt(ient1);
              idet        = pmdcl->GetDetector();
              ismn        = pmdcl->GetSMN();
              clusdata[0] = pmdcl->GetClusX();
@@ -787,18 +616,22 @@ void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt, AliRawReader *rawReader)
              AddRecPoint(idet,ismn,clusdata);
 
              Int_t ncell = (Int_t) clusdata[3];
+             if (ncell > 19) ncell = 19;
              for(Int_t ihit = 0; ihit < ncell; ihit++)
                {
                  Int_t celldataX = pmdcl->GetClusCellX(ihit);
                  Int_t celldataY = pmdcl->GetClusCellY(ihit);
-                 AddRecHit(celldataX, celldataY);
+                 Int_t celldataTr = pmdcl->GetClusCellTrack(ihit);
+                 Int_t celldataPid   = pmdcl->GetClusCellPid(ihit);
+                 Float_t celldataAdc = pmdcl->GetClusCellAdc(ihit);
+                 AddRecHit(celldataX, celldataY, celldataTr, celldataPid, celldataAdc);
                }
              branch2->Fill();
              ResetRechit();
 
            }
-         pmdcont->Clear();
-         
+         pmdcont->Delete();
+
          branch1->Fill();
          ResetRecpoint();
 
@@ -810,23 +643,16 @@ void AliPMDClusterFinder::Digits2RecPoints(Int_t ievt, AliRawReader *rawReader)
          for (Int_t j=0; j<kRow; j++) delete [] precpvADC[i][j];
        }
       for (Int_t i=0; i<iSMN; i++) delete [] precpvADC[i];
-      delete precpvADC;
+      delete [] precpvADC;
+
     } // DDL Loop
+
   
   ResetCellADC();
   
-  fPMDLoader = fRunLoader->GetLoader("PMDLoader");  
-  fPMDLoader->WriteRecPoints("OVERWRITE");
-
   //   delete the pointers
   delete pmdclust;
   delete pmdcont;
-
-}
-// ------------------------------------------------------------------------- //
-void AliPMDClusterFinder::SetCellEdepCut(Float_t ecut)
-{
-  fEcut = ecut;
 }
 // ------------------------------------------------------------------------- //
 void AliPMDClusterFinder::AddRecPoint(Int_t idet,Int_t ismn,Float_t *clusdata)
@@ -840,13 +666,15 @@ void AliPMDClusterFinder::AddRecPoint(Int_t idet,Int_t ismn,Float_t *clusdata)
   delete newrecpoint;
 }
 // ------------------------------------------------------------------------- //
-void AliPMDClusterFinder::AddRecHit(Int_t celldataX,Int_t celldataY)
+void AliPMDClusterFinder::AddRecHit(Int_t celldataX,Int_t celldataY,
+                                   Int_t celldataTr, Int_t celldataPid,
+                                   Float_t celldataAdc)
 {
   // Add associated cell hits to the Reconstructed points
   //
   TClonesArray &lrechits = *fRechits;
   AliPMDrechit *newrechit;
-  newrechit = new AliPMDrechit(celldataX, celldataY);
+  newrechit = new AliPMDrechit(celldataX, celldataY, celldataTr, celldataPid, celldataAdc);
   new(lrechits[fNhit++]) AliPMDrechit(newrechit);
   delete newrechit;
 }
@@ -859,12 +687,13 @@ void AliPMDClusterFinder::ResetCellADC()
     {
       for(Int_t icol = 0; icol < fgkCol; icol++)
        {
-         fCellADC[irow][icol] = 0.;
+         fCellTrack[irow][icol] = -1;
+         fCellPid[irow][icol]   = -1;
+         fCellADC[irow][icol]   = 0.;
        }
     }
 }
 // ------------------------------------------------------------------------- //
-
 void AliPMDClusterFinder::ResetRecpoint()
 {
   // Clear the list of reconstructed points
@@ -909,31 +738,68 @@ void AliPMDClusterFinder::UnLoadClusters()
   fPMDLoader->UnloadRecPoints();
 }
 // ------------------------------------------------------------------------- //
-
-AliPMDCalibData* AliPMDClusterFinder::GetCalibData() const
+AliPMDCalibData* AliPMDClusterFinder::GetCalibGain() const
 {
   // The run number will be centralized in AliCDBManager,
   // you don't need to set it here!
   // Added by ZA
-  AliCDBEntry  *entry = AliCDBManager::Instance()->Get("PMD/Calib/Data");
+  AliCDBEntry  *entry = AliCDBManager::Instance()->Get("PMD/Calib/Gain");
   
-  if(!entry){
-    AliWarning("Calibration object retrieval failed! Dummy calibration will be used.");
-    
-    // this just remembers the actual default storage. No problem if it is null.
-    AliCDBStorage *origStorage = AliCDBManager::Instance()->GetDefaultStorage();
-    AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
-    
-    entry = AliCDBManager::Instance()->Get("PMD/Calib/Data");
-    
-    // now reset the original default storage to AliCDBManager...
-    AliCDBManager::Instance()->SetDefaultStorage(origStorage);  
-  }
+  if(!entry)  AliFatal("Calibration object retrieval failed! ");
   
   AliPMDCalibData *calibdata=0;
   if (entry) calibdata = (AliPMDCalibData*) entry->GetObject();
   
-  if (!calibdata)  AliError("No calibration data from calibration database !");
+  if (!calibdata)  AliFatal("No calibration data from calibration database !");
   
   return calibdata;
 }
+// ------------------------------------------------------------------------- //
+AliPMDPedestal* AliPMDClusterFinder::GetCalibPed() const
+{
+  // The run number will be centralized in AliCDBManager,
+  // you don't need to set it here!
+  AliCDBEntry  *entry = AliCDBManager::Instance()->Get("PMD/Calib/Ped");
+  
+  if(!entry) AliFatal("Pedestal object retrieval failed!");
+    
+  AliPMDPedestal *pedestal = 0;
+  if (entry) pedestal = (AliPMDPedestal*) entry->GetObject();
+  
+  if (!pedestal)  AliFatal("No pedestal data from pedestal database !");
+  
+  return pedestal;
+}
+//--------------------------------------------------------------------//
+AliPMDHotData* AliPMDClusterFinder::GetCalibHot() const
+{
+  // The run number will be centralized in AliCDBManager,
+  // you don't need to set it here!
+  AliCDBEntry  *entry = AliCDBManager::Instance()->Get("PMD/Calib/Hot");
+  
+  if(!entry) AliFatal("HotData object retrieval failed!");
+  
+  AliPMDHotData *hot = 0;
+  if (entry) hot = (AliPMDHotData*) entry->GetObject();
+  
+  if (!hot)  AliFatal("No hot data from  database !");
+  
+  return hot;
+}
+//--------------------------------------------------------------------//
+AliPMDNoiseCut* AliPMDClusterFinder::GetNoiseCut() const
+{
+  // The run number will be centralized in AliCDBManager,
+  // you don't need to set it here!
+  AliCDBEntry  *entry = AliCDBManager::Instance()->Get("PMD/Calib/NoiseCut");
+  
+  if(!entry) AliFatal("Noisecut object retrieval failed!");
+  
+  AliPMDNoiseCut *ncut = 0;
+  if (entry) ncut = (AliPMDNoiseCut*) entry->GetObject();
+  
+  if (!ncut)  AliFatal("No noise cut data from  database !");
+  
+  return ncut;
+}
+