]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PMD/AliPMDDigitizer.cxx
Removing the tasks from the digitization (Ruben)
[u/mrichter/AliRoot.git] / PMD / AliPMDDigitizer.cxx
index c95006ed3a478c35aba7af2c9cafca77a4014222..d77c940d4097a40d4e2ba42434259466fcbee49c 100644 (file)
 #include <TFile.h>
 #include <TNtuple.h>
 #include <TParticle.h>
+#include <TRandom.h>
 
+#include "AliLog.h"
 #include "AliRun.h"
-#include "AliPMD.h"
-#include "AliPMDhit.h"
 #include "AliHit.h"
 #include "AliDetector.h"
 #include "AliRunLoader.h"
 #include "AliLoader.h"
 #include "AliConfig.h"
 #include "AliMagF.h"
-#include "AliRunDigitizer.h"
+#include "AliDigitizationInput.h"
 #include "AliDigitizer.h"
 #include "AliHeader.h"
+#include "AliCDBManager.h"
+#include "AliCDBStorage.h"
+#include "AliCDBEntry.h"
+#include "AliMC.h"
 
+#include "AliPMD.h"
+#include "AliPMDhit.h"
 #include "AliPMDcell.h"
 #include "AliPMDsdigit.h"
 #include "AliPMDdigit.h"
+#include "AliPMDCalibData.h"
+#include "AliPMDPedestal.h"
 #include "AliPMDDigitizer.h"
-#include "AliMC.h"
+
 
 ClassImp(AliPMDDigitizer)
 
@@ -57,14 +65,16 @@ AliPMDDigitizer::AliPMDDigitizer() :
   fPMDHit(0),
   fPMD(0),
   fPMDLoader(0),
+  fCalibGain(GetCalibGain()),
+  fCalibPed(GetCalibPed()),
   fSDigits(0),
   fDigits(0),
+  fCPVCell(0),
   fCell(0),
-  fDebug(0),
   fNsdigit(0),
   fNdigit(0),
   fDetNo(0),
-  fZPos(361.5)// in units of cm, This is the default position of PMD
+  fZPos(361.5)   // in units of cm, default position of PMD
 {
   // Default Constructor
   //
@@ -74,27 +84,62 @@ AliPMDDigitizer::AliPMDDigitizer() :
        {
          for (Int_t k = 0; k < fgkCol; k++)
            {
-             fCPV[i][j][k] = 0.; 
-             fPRE[i][j][k] = 0.; 
-             fPRECounter[i][j][k] =  0; 
-             fPRETrackNo[i][j][k] = -1; 
-             fCPVTrackNo[i][j][k] = -1; 
+             fCPV[i][j][k]         = 0.;
+             fPRE[i][j][k]         = 0.;
+             fCPVCounter[i][j][k]  =  0; 
+             fPRECounter[i][j][k]  =  0;
+             fCPVTrackNo[i][j][k]  = -1;
+             fPRETrackNo[i][j][k]  = -1;
+             fCPVTrackPid[i][j][k] = -1;
+             fPRETrackPid[i][j][k] = -1;
            }
        }
     }
 
 }
 //____________________________________________________________________________
-AliPMDDigitizer::AliPMDDigitizer(AliRunDigitizer* manager) 
-  :AliDigitizer(manager),
+AliPMDDigitizer::AliPMDDigitizer(const AliPMDDigitizer& digitizer):
+  AliDigitizer(digitizer),
+  fRunLoader(0),
+  fPMDHit(0),
+  fPMD(0),
+  fPMDLoader(0),
+  fCalibGain(GetCalibGain()),
+  fCalibPed(GetCalibPed()),
+  fSDigits(0),
+  fDigits(0),
+  fCPVCell(0),
+  fCell(0),
+  fNsdigit(0),
+  fNdigit(0),
+  fDetNo(0),
+  fZPos(361.5)   // in units of cm, default position of PMD
+{
+  // copy constructor
+  AliError("Copy constructor not allowed ");
+  
+}
+//____________________________________________________________________________
+AliPMDDigitizer & AliPMDDigitizer::operator=(const AliPMDDigitizer& /*digitizer*/)
+{
+  // Assignment operator
+  AliError("Assignement operator not allowed ");
+
+  return *this;
+}
+//____________________________________________________________________________
+AliPMDDigitizer::AliPMDDigitizer(AliDigitizationInput* digInput):
+  AliDigitizer(digInput),
   fRunLoader(0),
   fPMDHit(0),
   fPMD(0),
   fPMDLoader(0),
+  fCalibGain(GetCalibGain()),
+  fCalibPed(GetCalibPed()),
   fSDigits(new TClonesArray("AliPMDsdigit", 1000)),
   fDigits(new TClonesArray("AliPMDdigit", 1000)),
+  fCPVCell(0),
   fCell(0),
-  fDebug(0),
   fNsdigit(0),
   fNdigit(0),
   fDetNo(0),
@@ -102,21 +147,26 @@ AliPMDDigitizer::AliPMDDigitizer(AliRunDigitizer* manager)
 {
   // ctor which should be used
 
+
   for (Int_t i = 0; i < fgkTotUM; i++)
     {
       for (Int_t j = 0; j < fgkRow; j++)
        {
          for (Int_t k = 0; k < fgkCol; k++)
            {
-             fCPV[i][j][k] = 0.; 
-             fPRE[i][j][k] = 0.; 
-             fPRECounter[i][j][k] =  0; 
-             fPRETrackNo[i][j][k] = -1; 
-             fCPVTrackNo[i][j][k] = -1; 
+             fCPV[i][j][k]         = 0.;
+             fPRE[i][j][k]         = 0.;
+             fCPVCounter[i][j][k]  =  0; 
+             fPRECounter[i][j][k]  =  0;
+             fCPVTrackNo[i][j][k]  = -1;
+             fPRETrackNo[i][j][k]  = -1;
+             fCPVTrackPid[i][j][k] = -1;
+             fPRETrackPid[i][j][k] = -1;
            }
        }
     }
 }
+
 //____________________________________________________________________________
 AliPMDDigitizer::~AliPMDDigitizer()
 {
@@ -132,6 +182,7 @@ AliPMDDigitizer::~AliPMDDigitizer()
     delete fDigits;
     fDigits=0;
   }
+  fCPVCell.Delete();
   fCell.Delete();
 }
 //
@@ -147,43 +198,43 @@ void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
   TString evfoldname = AliConfig::GetDefaultEventFolderName();
   fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
   if (!fRunLoader)
-    fRunLoader = AliRunLoader::Open(file,AliConfig::GetDefaultEventFolderName(),
-                                   "UPDATE");
+      fRunLoader = AliRunLoader::Open(file,AliConfig::GetDefaultEventFolderName(), "UPDATE");
   
   if (!fRunLoader)
    {
-     Error("Open","Can not open session for file %s.",file);
+     AliError(Form("Can not open session for file %s.",file));
    }
-  
-  if (!fRunLoader->GetAliRun()) fRunLoader->LoadgAlice();
-  if (!fRunLoader->TreeE()) fRunLoader->LoadHeader();
-  if (!fRunLoader->TreeK()) fRunLoader->LoadKinematics();
 
-  gAlice = fRunLoader->GetAliRun();
+  const char *cHS = strstr(option,"HS");
+  const char *cHD = strstr(option,"HD");
+  const char *cSD = strstr(option,"SD");
   
-  if (fDebug) {
-    if (gAlice)
-      {
-       printf("<AliPMDdigitizer::Open> ");
-       printf("AliRun object found on file.\n");
-      }
-    else
-      {
-       printf("<AliPMDdigitizer::Open> ");
-       printf("Could not find AliRun object.\n");
-      }
-  }
+  if(cHS || cHD)
+    {
+      if (!fRunLoader->GetAliRun()) fRunLoader->LoadgAlice();
+      if (!fRunLoader->TreeE()) fRunLoader->LoadHeader();
+      if (!fRunLoader->TreeK()) fRunLoader->LoadKinematics();
   
-  fPMD  = (AliPMD*)gAlice->GetDetector("PMD");
+      gAlice = fRunLoader->GetAliRun();
+  
+      if (gAlice)
+       {
+         AliDebug(1,"Alirun object found");
+       }
+      else
+       {
+         AliError("Could not found Alirun object");
+       }
+  
+      fPMD  = (AliPMD*)gAlice->GetDetector("PMD");
+    }
+
   fPMDLoader = fRunLoader->GetLoader("PMDLoader");
   if (fPMDLoader == 0x0)
     {
-      cerr<<"Hits2Digits : Can not find PMD or PMDLoader\n";
+      AliError("Can not find PMDLoader");
     }
 
-  const char *cHS = strstr(option,"HS");
-  const char *cHD = strstr(option,"HD");
-  const char *cSD = strstr(option,"SD");
 
   if (cHS)
     {
@@ -207,29 +258,31 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
   // This reads the PMD Hits tree and assigns the right track number
   // to a cell and stores in the summable digits tree
   //
-  // cout << " -------- Beginning of Hits2SDigits ----------- " << endl;
 
   const Int_t kPi0 = 111;
   const Int_t kGamma = 22;
-  Int_t npmd;
-  Int_t trackno;
-  Int_t smnumber;
-  Int_t trackpid;
-  Int_t mtrackno;
-  Int_t mtrackpid;
-
-  Float_t xPos, yPos, zPos;
+  Int_t npmd = 0;
+  Int_t trackno = 0;
+  Int_t smnumber = 0;
+  Int_t trackpid = 0;
+  Int_t mtrackno = 0;
+  Int_t mtrackpid = 0;
+
+  Float_t xPos = 0., yPos = 0., zPos = 0.;
   Int_t xpad = -1, ypad = -1;
-  Float_t edep;
+  Float_t edep = 0.;
   Float_t vx = -999.0, vy = -999.0, vz = -999.0;
 
 
   if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
   ResetSDigit();
 
-  if (fDebug) printf("Event Number =  %d \n",ievt); 
+  AliDebug(1,Form("Event Number = %d",ievt));
   Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
-  if (fDebug) printf("Number of Particles = %d \n", nparticles);
+  AliDebug(1,Form("Number of Particles = %d",nparticles));
+
+  //
+
   fRunLoader->GetEvent(ievt);
   // ------------------------------------------------------- //
   // Pointer to specific detector hits.
@@ -238,8 +291,7 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
   TTree* treeH = fPMDLoader->TreeH();
   
   Int_t ntracks    = (Int_t) treeH->GetEntries();
-  if (fDebug) printf("Number of Tracks in the TreeH = %d \n", ntracks);
-
+  AliDebug(1,Form("Number of Tracks in the TreeH = %d", ntracks));
   TTree* treeS = fPMDLoader->TreeS();
   if (treeS == 0x0)
     {
@@ -247,21 +299,21 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
       treeS = fPMDLoader->TreeS();
     }
   Int_t bufsize = 16000;
-  treeS->Branch("PMDSDigit", &fSDigits, bufsize); 
+  treeS->Branch("PMDSDigit", &fSDigits, bufsize);
   
   TClonesArray* hits = 0;
   if (fPMD) hits = fPMD->Hits();
 
   // Start loop on tracks in the hits containers
 
-  for (Int_t track=0; track<ntracks;track++) 
+  for (Int_t track=0; track<ntracks;track++)
     {
-      gAlice->ResetHits();
+      gAlice->GetMCApp()->ResetHits();
       treeH->GetEvent(track);
-      if (fPMD) 
+      if (fPMD)
        {
          npmd = hits->GetEntriesFast();
-         for (int ipmd = 0; ipmd < npmd; ipmd++) 
+         for (Int_t ipmd = 0; ipmd < npmd; ipmd++)
            {
              fPMDHit = (AliPMDhit*) hits->UncheckedAt(ipmd);
              trackno = fPMDHit->GetTrack();
@@ -269,15 +321,10 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
 
              TParticle* mparticle = gAlice->GetMCApp()->Particle(trackno);
              trackpid  = mparticle->GetPdgCode();
-
-             Int_t igatr = -999;
-             Int_t ichtr = -999;
-             Int_t igapid = -999;
+             Int_t  ks = mparticle->GetStatusCode();
              Int_t imo;
-             Int_t igen = 0;
-             Int_t idmo = -999;
-
              Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
+             
              if (mparticle->GetFirstMother() == -1)
                {
                  tracknoOld  = trackno;
@@ -285,95 +332,106 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
                  statusOld   = -1;
                }
              Int_t igstatus = 0;
-             while((imo = mparticle->GetFirstMother()) >= 0)
-               {
-                 igen++;
 
+             Int_t trnotemp = trackno;    // Modified on 25th Nov 2009
+             if(ks==1||(imo = mparticle->GetFirstMother())<0 ){
+               vx = mparticle->Vx();
+               vy = mparticle->Vy();
+               vz = mparticle->Vz();
+               
+               if(trackpid==kGamma||trackpid==11||trackpid==-11||
+                  trackpid==kPi0)igstatus=1;
+             }
+             
+             
+             while(((imo = mparticle->GetFirstMother()) >= 0)&& 
+                   (ks = mparticle->GetStatusCode() <1) )
+               {
                  mparticle =  gAlice->GetMCApp()->Particle(imo);
-                 idmo = mparticle->GetPdgCode();
-                 
+                 trackpid = mparticle->GetPdgCode();
+                 ks = mparticle->GetStatusCode();
                  vx = mparticle->Vx();
                  vy = mparticle->Vy();
                  vz = mparticle->Vz();
-               
-                 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
-                 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
-                 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
+                 
+                 // Modified on 25th Nov 2009
+
+                 trnotemp = trackno;
+                  if(trackpid == 111)
                    {
-                     igatr = imo;
-                     igapid = idmo;
-                     igstatus = 1;
+                     trackno = trnotemp;
                    }
-                 if(igstatus == 0)
+                  if(trackpid != 111)
                    {
-                     if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
-                       {
-                         igatr = imo;
-                         igapid = idmo;
-                       }
+                     trackno=imo;
                    }
-                 ichtr = imo;
+                 // end of modification on 25th Nov 2009
                }
+        
+             if(trackpid==kGamma||trackpid==11||trackpid==-11||
+                trackpid==kPi0)igstatus=1;
+             mtrackpid=trackpid;
+             mtrackno=trackno;
+             trackpid=trackpidOld;
+             trackno=tracknoOld;
+             
+             //-----------------end of modification----------------
+             Float_t ptime = fPMDHit->GetTime()*1e6;   // time in microsec
+             if (ptime < 0. || ptime > 1.2) continue;  
 
-             if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
-               {
-                 mtrackno = igatr;
-                 mtrackpid = igapid;
-               }
-             else
-               {
-                 mtrackno  = ichtr;
-                 mtrackpid = idmo;
-               }
-             if (statusOld == -1)
-               {
-                 mtrackno  = tracknoOld;
-                 mtrackpid = trackpidOld;
-               }
              xPos = fPMDHit->X();
              yPos = fPMDHit->Y();
              zPos = fPMDHit->Z();
-             
+
              edep       = fPMDHit->GetEnergy();
              Int_t vol1 = fPMDHit->GetVolume(1); // Column
              Int_t vol2 = fPMDHit->GetVolume(2); // Row
-             Int_t vol3 = fPMDHit->GetVolume(3); // UnitModule
-             Int_t vol6 = fPMDHit->GetVolume(6); // SuperModule
+             Int_t vol7 = fPMDHit->GetVolume(4); // Serial Module No
+
 
              // -----------------------------------------//
+             // In new geometry after adding electronics //
              // For Super Module 1 & 2                   //
-             //  nrow = 96, ncol = 48                    //
-             // For Super Module 3 & 4                   //
              //  nrow = 48, ncol = 96                    //
+             // For Super Module 3 & 4                   //
+             //  nrow = 96, ncol = 48                    //
              // -----------------------------------------//
-             
-             smnumber = (vol6-1)*6 + vol3;
 
-             if (vol6 == 1 || vol6 == 2)
+             if (vol7 < 24)
                {
-                 xpad = vol1;
-                 ypad = vol2;
+                 smnumber = vol7;
+               }
+             else
+               {
+                 smnumber = vol7 - 24;
                }
-             else if (vol6 == 3 || vol6 == 4)
+             Int_t vol8 = smnumber/6 + 1;   // fake supermodule
+
+             if (vol8 == 1 || vol8 == 2)
                {
                  xpad = vol2;
                  ypad = vol1;
                }
+             else if (vol8 == 3 || vol8 == 4)
+               {
+                 xpad = vol1;
+                 ypad = vol2;
+               }
 
-             //cout << "zpos = " << zPos << " edep = " << edep << endl;
+             AliDebug(2,Form("Zposition = %f Edeposition = %f",zPos,edep));
 
-             Float_t zposition = TMath::Abs(zPos);
-             if (zposition < fZPos)
+             if (vol7 < 24)
                {
-                 // CPV
-                 fDetNo = 1;
+                 // PRE
+                 fDetNo = 0;
                }
-             else if (zposition > fZPos)
+             else
                {
-                 // PMD
-                 fDetNo = 0;
+                 // CPV
+                 fDetNo = 1;
                }
-             Int_t smn = smnumber - 1;
+
+             Int_t smn = smnumber;
              Int_t ixx = xpad     - 1;
              Int_t iyy = ypad     - 1;
              if (fDetNo == 0)
@@ -382,24 +440,26 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
                  fPRECounter[smn][ixx][iyy]++;
 
                  AliPMDcell* cell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
-
                  fCell.Add(cell);
                }
              else if(fDetNo == 1)
                {
                  fCPV[smn][ixx][iyy] += edep;
-                 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
+                 fCPVCounter[smn][ixx][iyy]++;
+                 AliPMDcell* cpvcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep); 
+                 fCPVCell.Add(cpvcell);
                }
            }
        }
     } // Track Loop ended
-
+  TrackAssignment2CPVCell();
   TrackAssignment2Cell();
   ResetCell();
 
   Float_t deltaE      = 0.;
   Int_t   detno       = 0;
   Int_t   trno        = -1;
+  Int_t   trpid       = -99;
 
   for (Int_t idet = 0; idet < 2; idet++)
     {
@@ -423,7 +483,10 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
                    }
                  if (deltaE > 0.)
                    {
-                     AddSDigit(trno,detno,ism,jrow,kcol,deltaE);
+                     // Natasha
+                     TParticle *mparticle = gAlice->GetMCApp()->Particle(trno);
+                     trpid = mparticle->GetPdgCode();
+                     AddSDigit(trno,trpid,detno,ism,jrow,kcol,deltaE);
                    }
                }
            }
@@ -433,8 +496,6 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
     }
   fPMDLoader->WriteSDigits("OVERWRITE");
   ResetCellADC();
-
-  //  cout << " -------- End of Hits2SDigit ----------- " << endl;
 }
 //____________________________________________________________________________
 
@@ -445,25 +506,25 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
   //
   const Int_t kPi0 = 111;
   const Int_t kGamma = 22;
-  Int_t npmd;
-  Int_t trackno;
-  Int_t smnumber;
-  Int_t trackpid;
-  Int_t mtrackno;
-  Int_t mtrackpid;
-
-  Float_t xPos, yPos, zPos;
+  Int_t npmd      = 0;
+  Int_t trackno   = 0;
+  Int_t smnumber  = 0;
+  Int_t trackpid  = 0;
+  Int_t mtrackno  = 0;
+  Int_t mtrackpid = 0;
+
+  Float_t xPos = 0., yPos = 0., zPos = 0.;
   Int_t xpad = -1, ypad = -1;
-  Float_t edep;
+  Float_t edep = 0.;
   Float_t vx = -999.0, vy = -999.0, vz = -999.0;
 
   if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
   ResetDigit();
 
-  if (fDebug) printf("Event Number =  %d \n",ievt); 
-
+  AliDebug(1,Form("Event Number =  %d",ievt));
   Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
-  if (fDebug) printf("Number of Particles = %d \n", nparticles);
+  AliDebug(1,Form("Number of Particles = %d", nparticles));
+
   fRunLoader->GetEvent(ievt);
   // ------------------------------------------------------- //
   // Pointer to specific detector hits.
@@ -474,11 +535,11 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
 
   if (fPMDLoader == 0x0)
     {
-      cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
+      AliError("Can not find PMD or PMDLoader");
     }
   TTree* treeH = fPMDLoader->TreeH();
   Int_t ntracks    = (Int_t) treeH->GetEntries();
-  if (fDebug) printf("Number of Tracks in the TreeH = %d \n", ntracks);
+  AliDebug(1,Form("Number of Tracks in the TreeH = %d", ntracks));
   fPMDLoader->LoadDigits("recreate");
   TTree* treeD = fPMDLoader->TreeD();
   if (treeD == 0x0)
@@ -487,22 +548,22 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
       treeD = fPMDLoader->TreeD();
     }
   Int_t bufsize = 16000;
-  treeD->Branch("PMDDigit", &fDigits, bufsize); 
+  treeD->Branch("PMDDigit", &fDigits, bufsize);
   
   TClonesArray* hits = 0;
   if (fPMD) hits = fPMD->Hits();
 
   // Start loop on tracks in the hits containers
 
-  for (Int_t track=0; track<ntracks;track++) 
+  for (Int_t track=0; track<ntracks;track++)
     {
-      gAlice->ResetHits();
+      gAlice->GetMCApp()->ResetHits();
       treeH->GetEvent(track);
       
-      if (fPMD) 
+      if (fPMD)
        {
          npmd = hits->GetEntriesFast();
-         for (int ipmd = 0; ipmd < npmd; ipmd++) 
+         for (Int_t ipmd = 0; ipmd < npmd; ipmd++)
            {
              fPMDHit = (AliPMDhit*) hits->UncheckedAt(ipmd);
              trackno = fPMDHit->GetTrack();
@@ -511,14 +572,8 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
              
              TParticle* mparticle = gAlice->GetMCApp()->Particle(trackno);
              trackpid  = mparticle->GetPdgCode();
-
-             Int_t igatr = -999;
-             Int_t ichtr = -999;
-             Int_t igapid = -999;
+             Int_t  ks = mparticle->GetStatusCode();
              Int_t imo;
-             Int_t igen = 0;
-             Int_t idmo = -999;
-
              Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
              if (mparticle->GetFirstMother() == -1)
                {
@@ -528,98 +583,101 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
                }
 
              Int_t igstatus = 0;
-             while((imo = mparticle->GetFirstMother()) >= 0)
-               {
-                 igen++;
 
+             Int_t trnotemp = trackno;   // modified on 25th Nov 2009
+             if(ks==1||(imo = mparticle->GetFirstMother())<0 ){
+               vx = mparticle->Vx();
+               vy = mparticle->Vy();
+               vz = mparticle->Vz();
+               
+               if(trackpid==kGamma||trackpid==11||trackpid==-11||trackpid==kPi0)
+                 igstatus=1;
+             }
+             
+             
+             while(((imo = mparticle->GetFirstMother()) >= 0)&& 
+                   (ks = mparticle->GetStatusCode() <1) )
+               {
                  mparticle =  gAlice->GetMCApp()->Particle(imo);
-                 idmo = mparticle->GetPdgCode();
-                 
+                 trackpid = mparticle->GetPdgCode();
+                 ks = mparticle->GetStatusCode();
                  vx = mparticle->Vx();
                  vy = mparticle->Vy();
                  vz = mparticle->Vz();
-               
-                 //printf("==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
-                 //fprintf(fpw1,"==> Mother ID %5d %5d %5d Vertex: %13.3f %13.3f %13.3f\n", igen, imo, idmo, vx, vy, vz);
-                 if ((idmo == kGamma || idmo == -11 || idmo == 11) && vx == 0. && vy == 0. && vz == 0.)
+                 
+                 // Modified on 25th Nov 2009
+                 
+                 trnotemp = trackno;
+                  if(trackpid == 111)
                    {
-                     igatr = imo;
-                     igapid = idmo;
-                     igstatus = 1;
+                     trackno = trnotemp;
                    }
-                 if(igstatus == 0)
+                  if(trackpid != 111)
                    {
-                     if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
-                       {
-                         igatr = imo;
-                         igapid = idmo;
-                       }
+                     trackno=imo;
                    }
-                 ichtr = imo;
-               }
-
-             if (idmo == kPi0 && vx == 0. && vy == 0. && vz == 0.)
-               {
-                 mtrackno = igatr;
-                 mtrackpid = igapid;
-               }
-             else
-               {
-                 mtrackno  = ichtr;
-                 mtrackpid = idmo;
-               }
-             if (statusOld == -1)
-               {
-                 mtrackno  = tracknoOld;
-                 mtrackpid = trackpidOld;
                }
+        
+             if(trackpid==kGamma||trackpid==11||trackpid==-11||trackpid==kPi0)
+               igstatus=1;
+             mtrackpid=trackpid;
+             mtrackno=trackno;
+             trackpid=trackpidOld;
+             trackno=tracknoOld;
+
+             Float_t ptime = fPMDHit->GetTime()*1e6;
+             if (ptime < 0. || ptime > 1.2) continue;
              
              xPos = fPMDHit->X();
              yPos = fPMDHit->Y();
              zPos = fPMDHit->Z();
-
              edep       = fPMDHit->GetEnergy();
              Int_t vol1 = fPMDHit->GetVolume(1); // Column
              Int_t vol2 = fPMDHit->GetVolume(2); // Row
-             Int_t vol3 = fPMDHit->GetVolume(3); // UnitModule
-             Int_t vol6 = fPMDHit->GetVolume(6); // SuperModule
+             Int_t vol7 = fPMDHit->GetVolume(4); // Serial Module No
 
              // -----------------------------------------//
+             // In new geometry after adding electronics //
              // For Super Module 1 & 2                   //
-             //  nrow = 96, ncol = 48                    //
-             // For Super Module 3 & 4                   //
              //  nrow = 48, ncol = 96                    //
+             // For Super Module 3 & 4                   //
+             //  nrow = 96, ncol = 48                    //
              // -----------------------------------------//
              
-             smnumber = (vol6-1)*6 + vol3;
-
-             if (vol6 == 1 || vol6 == 2)
+             if (vol7 < 24)
                {
-                 xpad = vol1;
-                 ypad = vol2;
+                 smnumber = vol7;
+               }
+             else
+               {
+                 smnumber = vol7 - 24;
                }
-             else if (vol6 == 3 || vol6 == 4)
+             Int_t vol8 = smnumber/6 + 1;    // fake supermodule
+
+             if (vol8 == 1 || vol8 == 2)
                {
                  xpad = vol2;
                  ypad = vol1;
                }
+             else if (vol8 == 3 || vol8 == 4)
+               {
+                 xpad = vol1;
+                 ypad = vol2;
+               }
 
-             //cout << "-zpos = " << -zPos << endl;
-
-             Float_t zposition = TMath::Abs(zPos);
+             AliDebug(2,Form("ZPosition = %f Edeposition = %f",zPos,edep));
 
-             if (zposition < fZPos)
+             if (vol7 < 24)
                {
-                 // CPV
-                 fDetNo = 1;
+                 // PRE
+                 fDetNo = 0;
                }
-             else if (zposition > fZPos)
+             else
                {
-                 // PMD
-                 fDetNo = 0;
+                 fDetNo = 1;
                }
 
-             Int_t smn = smnumber - 1;
+             Int_t smn = smnumber;
              Int_t ixx = xpad     - 1;
              Int_t iyy = ypad     - 1;
              if (fDetNo == 0)
@@ -634,56 +692,88 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
              else if(fDetNo == 1)
                {
                  fCPV[smn][ixx][iyy] += edep;
-                 fCPVTrackNo[smn][ixx][iyy] = mtrackno;
+                 fCPVCounter[smn][ixx][iyy]++;
+                 AliPMDcell* cpvcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep); 
+                 fCPVCell.Add(cpvcell);
                }
            }
        }
     } // Track Loop ended
-
+  TrackAssignment2CPVCell();
   TrackAssignment2Cell();
   ResetCell();
 
-  Float_t adc;
+  Float_t gain1  = 1.;
+  Float_t adc    = 0. ;
   Float_t deltaE = 0.;
-  Int_t detno = 0;
-  Int_t trno = 1;
+  Int_t detno    = 0;
+  Int_t trno     = 1;
+  Int_t trpid    = -99;
 
   for (Int_t idet = 0; idet < 2; idet++)
-    {
+  {
       for (Int_t ism = 0; ism < fgkTotUM; ism++)
-       {
+      {
          for (Int_t jrow = 0; jrow < fgkRow; jrow++)
-           {
+         {
              for (Int_t kcol = 0; kcol < fgkCol; kcol++)
-               {
+             {
                  if (idet == 0)
-                   {
+                 {
                      deltaE = fPRE[ism][jrow][kcol];
                      trno   = fPRETrackNo[ism][jrow][kcol];
-                     detno = 0;
-                   }
+                     detno  = 0;
+                 }
                  else if (idet == 1)
-                   {
+                 {
                      deltaE = fCPV[ism][jrow][kcol];
                      trno   = fCPVTrackNo[ism][jrow][kcol];
-                     detno = 1;
-                   }
+                     detno  = 1;
+                 }
                  if (deltaE > 0.)
-                   {
+                 {
                      MeV2ADC(deltaE,adc);
-                     AddDigit(trno,detno,ism,jrow,kcol,adc);
-                   }
-               } // column loop
-           } // row    loop
-       } // supermodule loop
-      treeD->Fill();
-      ResetDigit();
-    } // detector loop
 
+                     // To decalibrate the adc values
+                     //
+                     gain1 = Gain(idet,ism,jrow,kcol);
+                     if (gain1 != 0.)
+                     {
+                         Int_t adcDecalib = (Int_t)(adc/gain1);
+                         adc = (Float_t) adcDecalib;
+                     }
+                     else if(gain1 == 0.)
+                     {
+                         adc = 0.;
+                     }
+
+                     // Pedestal Decalibration
+                     Int_t   pedmeanrms = 
+                         fCalibPed->GetPedMeanRms(idet,ism,jrow,kcol);
+                     Int_t   pedrms1    = (Int_t) pedmeanrms%100;
+                     Float_t pedrms     = (Float_t)pedrms1/10.;
+                     Float_t pedmean    = 
+                         (Float_t) (pedmeanrms - pedrms1)/1000.0;
+                     if (adc > 0.)
+                     {
+                         adc += (pedmean + 3.0*pedrms);
+                         TParticle *mparticle
+                           = gAlice->GetMCApp()->Particle(trno);
+                         trpid = mparticle->GetPdgCode();
+                         
+                         AddDigit(trno,trpid,detno,ism,jrow,kcol,adc);
+                     }
+                 }
+             } // column loop
+         } // row    loop
+         treeD->Fill();
+         ResetDigit();
+      } // supermodule loop
+  } // detector loop
+  
   fPMDLoader->WriteDigits("OVERWRITE");
   ResetCellADC();
-  
-  //  cout << " -------- End of Hits2Digit ----------- " << endl;
+
 }
 //____________________________________________________________________________
 
@@ -693,12 +783,17 @@ void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
   // This reads the PMD sdigits tree and converts energy deposition
   // in a cell to ADC and stores in the digits tree
   //
-  //  cout << " -------- Beginning of SDigits2Digit ----------- " << endl;
+
   fRunLoader->GetEvent(ievt);
 
   TTree* treeS = fPMDLoader->TreeS();
   AliPMDsdigit  *pmdsdigit;
   TBranch *branch = treeS->GetBranch("PMDSDigit");
+  if(!branch)
+    {
+      AliError("PMD Sdigit branch does not exist");
+      return;
+    }
   if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
   branch->SetAddress(&fSDigits);
 
@@ -710,23 +805,25 @@ void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
     }
   Int_t bufsize = 16000;
   if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
-  treeD->Branch("PMDDigit", &fDigits, bufsize); 
+  treeD->Branch("PMDDigit", &fDigits, bufsize);
 
-  Int_t   trno, det, smn;
-  Int_t   irow, icol;
-  Float_t edep, adc;
+  Int_t   trno = 1, trpid = 0, det = 0, smn = 0;
+  Int_t   irow = 0, icol = 0;
+  Float_t edep = 0., adc = 0.;
 
   Int_t nmodules = (Int_t) treeS->GetEntries();
+  AliDebug(1,Form("Number of modules = %d",nmodules));
 
   for (Int_t imodule = 0; imodule < nmodules; imodule++)
     {
-      treeS->GetEntry(imodule); 
+      treeS->GetEntry(imodule);
       Int_t nentries = fSDigits->GetLast();
-      //cout << " nentries = " << nentries << endl;
+      AliDebug(2,Form("Number of entries per module = %d",nentries+1));
       for (Int_t ient = 0; ient < nentries+1; ient++)
        {
          pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
          trno   = pmdsdigit->GetTrackNumber();
+         trpid  = pmdsdigit->GetTrackPid();
          det    = pmdsdigit->GetDetector();
          smn    = pmdsdigit->GetSMNumber();
          irow   = pmdsdigit->GetRow();
@@ -734,47 +831,63 @@ void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
          edep   = pmdsdigit->GetCellEdep();
 
          MeV2ADC(edep,adc);
-         AddDigit(trno,det,smn,irow,icol,adc);      
+
+         // To decalibrte the adc values
+         //
+         Float_t gain1 = Gain(det,smn,irow,icol);
+         if (gain1 != 0.)
+         {
+           Int_t adcDecalib = (Int_t)(adc/gain1);
+           adc = (Float_t) adcDecalib;
+         }
+         else if(gain1 == 0.)
+         {
+             adc = 0.;
+         }
+         // Pedestal Decalibration
+         Int_t   pedmeanrms = fCalibPed->GetPedMeanRms(det,smn,irow,icol);
+         Int_t   pedrms1    = (Int_t) pedmeanrms%100;
+         Float_t pedrms     = (Float_t)pedrms1/10.;
+         Float_t pedmean    = (Float_t) (pedmeanrms - pedrms1)/1000.0;
+         if(adc > 0.)
+         {
+             adc += (pedmean + 3.0*pedrms);
+             AddDigit(trno,trpid,det,smn,irow,icol,adc);
+         }
+
        }
       treeD->Fill();
       ResetDigit();
     }
   fPMDLoader->WriteDigits("OVERWRITE");
-  //  cout << " -------- End of SDigits2Digit ----------- " << endl;
+
 }
 //____________________________________________________________________________
-void AliPMDDigitizer::Exec(Option_t *option)
+void AliPMDDigitizer::Digitize(Option_t *option)
 {
   // Does the event merging and digitization
-
-  fpw = fopen("digits_test.dat","w");
-  fDebug = 0;
   const char *cdeb = strstr(option,"deb");
   if(cdeb)
     {
-      cout << "**************** PMD Exec *************** " << endl;
-      fDebug = 1;
-    }
-  
-  Int_t ninputs = fManager->GetNinputs();
-  if(fDebug)
-    {
-      cout << " Number of files = " << ninputs << endl;
+      AliDebug(100," *** PMD Exec is called ***");
     }
+
+  Int_t ninputs = fDigInput->GetNinputs();
+  AliDebug(1,Form("Number of files to be processed = %d",ninputs));
   ResetCellADC();
 
   for (Int_t i = 0; i < ninputs; i++)
     {
-      Int_t troffset = fManager->GetMask(i);
+      Int_t troffset = fDigInput->GetMask(i);
       MergeSDigits(i, troffset);
     }
 
-  fRunLoader = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
+  fRunLoader = AliRunLoader::GetRunLoader(fDigInput->GetOutputFolderName());
   fPMD  = (AliPMD*)gAlice->GetDetector("PMD");
   fPMDLoader = fRunLoader->GetLoader("PMDLoader");
   if (fPMDLoader == 0x0)
     {
-      cerr<<"AliPMDDigitizer::Exec : Can not find PMD or PMDLoader\n";
+      AliError("Can not find PMD or PMDLoader");
     }
   fPMDLoader->LoadDigits("update");
   TTree* treeD = fPMDLoader->TreeD();
@@ -785,12 +898,13 @@ void AliPMDDigitizer::Exec(Option_t *option)
     }
   Int_t bufsize = 16000;
   if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
-  treeD->Branch("PMDDigit", &fDigits, bufsize); 
+  treeD->Branch("PMDDigit", &fDigits, bufsize);
 
-  Float_t adc;
+  Float_t adc    = 0.;
   Float_t deltaE = 0.;
-  Int_t detno = 0;
-  Int_t trno = 1;
+  Int_t detno    = 0;
+  Int_t trno     = 1;
+  Int_t trpid    = -99;
 
   for (Int_t idet = 0; idet < 2; idet++)
     {
@@ -804,18 +918,47 @@ void AliPMDDigitizer::Exec(Option_t *option)
                    {
                      deltaE = fPRE[ism][jrow][kcol];
                      trno   = fPRETrackNo[ism][jrow][kcol];
-                     detno = 0;
+                     trpid  = fPRETrackPid[ism][jrow][kcol];
+                     detno  = 0;
                    }
                  else if (idet == 1)
                    {
                      deltaE = fCPV[ism][jrow][kcol];
                      trno   = fCPVTrackNo[ism][jrow][kcol];
-                     detno = 1;
+                     trpid  = fCPVTrackPid[ism][jrow][kcol];
+                     detno  = 1;
                    }
                  if (deltaE > 0.)
                    {
                      MeV2ADC(deltaE,adc);
-                     AddDigit(trno,detno,ism,jrow,kcol,adc);
+
+                      //
+                     // Gain decalibration
+                     //
+                     Float_t gain1 = Gain(idet,ism,jrow,kcol);
+
+                     if (gain1 != 0.)
+                     {
+                         Int_t adcDecalib = (Int_t)(adc/gain1);
+                         adc = (Float_t) adcDecalib;
+                     }
+                     else if(gain1 == 0.)
+                     {
+                         adc = 0.;
+                     }
+                     // Pedestal Decalibration
+                     Int_t   pedmeanrms = 
+                         fCalibPed->GetPedMeanRms(idet,ism,jrow,kcol);
+                     Int_t   pedrms1    = (Int_t) pedmeanrms%100;
+                     Float_t pedrms     = (Float_t)pedrms1/10.;
+                     Float_t pedmean    = 
+                         (Float_t) (pedmeanrms - pedrms1)/1000.0;
+                     if (adc > 0.)
+                     {
+                         adc += (pedmean + 3.0*pedrms);
+                         AddDigit(trno,trpid,detno,ism,jrow,kcol,adc);
+                     }
+
                    }
                } // column loop
            } // row    loop
@@ -823,16 +966,216 @@ void AliPMDDigitizer::Exec(Option_t *option)
          ResetDigit();
        } // supermodule loop
     } // detector loop
-  fPMDLoader->WriteDigits("OVERWRITE");  
+  fPMDLoader->WriteDigits("OVERWRITE");
   fPMDLoader->UnloadDigits();
   ResetCellADC();
 }
 //____________________________________________________________________________
+void AliPMDDigitizer::TrackAssignment2CPVCell()
+{
+  // This block assigns the cell id when there are
+  // multiple tracks in a cell according to the
+  // energy deposition
+  // This method added by Ajay
+  Bool_t jsort = false;
+
+  Int_t i = 0, j = 0, k = 0;
+
+  Int_t   *status1;
+  Int_t   *status2;
+  Int_t   *trnarray;  
+  Float_t *fracEdp;
+  Float_t *trEdp;
+  
+  Int_t   ****cpvTrack;
+  Float_t ****cpvEdep;
+
+  cpvTrack = new Int_t ***[fgkTotUM];
+  cpvEdep  = new Float_t ***[fgkTotUM];
+  for (i=0; i<fgkTotUM; i++)
+    {
+      cpvTrack[i] = new Int_t **[fgkRow];
+      cpvEdep[i]  = new Float_t **[fgkRow];
+    }
+
+  for (i = 0; i < fgkTotUM; i++)
+    {
+      for (j = 0; j < fgkRow; j++)
+       {
+         cpvTrack[i][j] = new Int_t *[fgkCol];
+         cpvEdep[i][j]  = new Float_t *[fgkCol];
+       }
+    }
+  for (i = 0; i < fgkTotUM; i++)
+    {
+      for (j = 0; j < fgkRow; j++)
+       {
+         for (k = 0; k < fgkCol; k++)
+           {
+             Int_t nn = fCPVCounter[i][j][k];
+             if(nn > 0)
+               {
+                 cpvTrack[i][j][k] = new Int_t[nn];
+                 cpvEdep[i][j][k] = new Float_t[nn];
+               }
+             else
+               {
+                 nn = 1;
+                 cpvTrack[i][j][k] = new Int_t[nn];
+                 cpvEdep[i][j][k] = new Float_t[nn];
+               }                     
+             fCPVCounter[i][j][k] = 0;
+           }
+       }
+    }
+
+
+  Int_t nentries = fCPVCell.GetEntries();
+  Int_t   mtrackno = 0, ism = 0, ixp = 0, iyp = 0;
+  Float_t edep = 0.;
+  for (i = 0; i < nentries; i++)
+    {
+      AliPMDcell* cpvcell = (AliPMDcell*)fCPVCell.UncheckedAt(i);
+      
+      mtrackno = cpvcell->GetTrackNumber();
+      ism      = cpvcell->GetSMNumber();
+      ixp      = cpvcell->GetX();
+      iyp      = cpvcell->GetY();
+      edep     = cpvcell->GetEdep();
+      Int_t nn = fCPVCounter[ism][ixp][iyp];
+      cpvTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
+      cpvEdep[ism][ixp][iyp][nn] = edep;
+      fCPVCounter[ism][ixp][iyp]++;
+    }
+  
+  Int_t iz = 0, il = 0;
+  Int_t im = 0, ix = 0, iy = 0;
+  Int_t nn = 0;
+  for (im=0; im<fgkTotUM; im++)
+    {
+      for (ix=0; ix<fgkRow; ix++)
+       {
+         for (iy=0; iy<fgkCol; iy++)
+           {
+             nn = fCPVCounter[im][ix][iy];
+             if (nn > 1)
+               {
+                 // This block handles if a cell is fired
+                 // many times by many tracks
+                 status1  = new Int_t[nn];
+                 status2  = new Int_t[2*nn];
+                 trnarray = new Int_t[nn];
+                 for (iz = 0; iz < nn; iz++)
+                   {
+                     status1[iz] = cpvTrack[im][ix][iy][iz];
+                   }
+                 TMath::Sort(nn,status1,status2,jsort);
+                 Int_t trackOld = -99999;
+                 Int_t track, trCount = 0;
+                 for (iz = 0; iz < nn; iz++)
+                   {
+                     track = status1[status2[iz]];
+                     if (trackOld != track)
+                       {
+                         trnarray[trCount] = track;
+                         trCount++;
+                       }                             
+                     trackOld = track;
+                   }
+                 delete [] status1;
+                 delete [] status2;
+                 Float_t totEdp = 0.;
+                 trEdp = new Float_t[trCount];
+                 fracEdp = new Float_t[trCount];
+                 for (il = 0; il < trCount; il++)
+                   {
+                     trEdp[il] = 0.;
+                     track = trnarray[il];
+                     for (iz = 0; iz < nn; iz++)
+                       {
+                         if (track == cpvTrack[im][ix][iy][iz])
+                           {
+                             trEdp[il] += cpvEdep[im][ix][iy][iz];
+                           }
+                       }
+                     totEdp += trEdp[il];
+                   }
+                 Int_t ilOld = 0;
+                 Float_t fracOld = 0.;
+                 
+                 for (il = 0; il < trCount; il++)
+                   {
+                     fracEdp[il] = trEdp[il]/totEdp;
+                     if (fracOld < fracEdp[il])
+                       {
+                         fracOld = fracEdp[il];
+                         ilOld = il;
+                       }
+                   }
+                 fCPVTrackNo[im][ix][iy] = trnarray[ilOld];
+                 delete [] fracEdp;
+                 delete [] trEdp;
+                 delete [] trnarray;
+               }
+             else if (nn == 1)
+               {
+                 // This only handles if a cell is fired
+                 // by only one track
+                 
+                 fCPVTrackNo[im][ix][iy] = cpvTrack[im][ix][iy][0];
+                 
+               }
+             else if (nn ==0)
+               {
+                 // This is if no cell is fired
+                 fCPVTrackNo[im][ix][iy] = -999;
+               }
+           } // end of iy
+       } // end of ix
+    } // end of im
+  
+  // Delete all the pointers
+  
+ for (i = 0; i < fgkTotUM; i++)
+    {
+      for (j = 0; j < fgkRow; j++)
+       {
+         for (k = 0; k < fgkCol; k++)
+           {
+             delete []cpvTrack[i][j][k];
+             delete []cpvEdep[i][j][k];
+           }
+       }
+    }
+  for (i = 0; i < fgkTotUM; i++)
+    {
+      for (j = 0; j < fgkRow; j++)
+       {
+         delete [] cpvTrack[i][j];
+         delete [] cpvEdep[i][j];
+       }
+    }
+  
+  for (i = 0; i < fgkTotUM; i++)
+    {
+      delete [] cpvTrack[i];
+      delete [] cpvEdep[i];
+    }
+  delete [] cpvTrack;
+  delete [] cpvEdep;
+  
+  // 
+  // End of the cell id assignment
+  //
+}
+//____________________________________________________________________________
 
 void AliPMDDigitizer::MergeSDigits(Int_t filenumber, Int_t troffset)
 {
   // merging sdigits
-  fRunLoader = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(filenumber));
+  fRunLoader = AliRunLoader::GetRunLoader(fDigInput->GetInputFolderName(filenumber));
   fPMDLoader = fRunLoader->GetLoader("PMDLoader");
   fPMDLoader->LoadSDigits("read");
   TTree* treeS = fPMDLoader->TreeS();
@@ -841,39 +1184,33 @@ void AliPMDDigitizer::MergeSDigits(Int_t filenumber, Int_t troffset)
   if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
   branch->SetAddress(&fSDigits);
 
-  Int_t   itrackno, idet, ism;
-  Int_t   ixp, iyp;
-  Float_t edep;
-  
+  Int_t   itrackno = 1, itrackpid = 0, idet = 0, ism = 0;
+  Int_t   ixp = 0, iyp = 0;
+  Float_t edep = 0.;
   Int_t nmodules = (Int_t) treeS->GetEntries();
-  if(fDebug)
-    {
-      cout << " nmodules = " << nmodules << endl;
-      cout << " tr offset = " << troffset << endl;
-    }
+  AliDebug(1,Form("Number of Modules in the treeS = %d",nmodules));
+  AliDebug(1,Form("Track Offset = %d",troffset));
   for (Int_t imodule = 0; imodule < nmodules; imodule++)
     {
-      treeS->GetEntry(imodule); 
+      treeS->GetEntry(imodule);
       Int_t nentries = fSDigits->GetLast();
-      if(fDebug)
-       {
-         cout << " nentries = " << nentries << endl;
-       }
+      AliDebug(2,Form("Number of Entries per Module = %d",nentries));
       for (Int_t ient = 0; ient < nentries+1; ient++)
        {
          pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
          itrackno  = pmdsdigit->GetTrackNumber();
+         itrackpid = pmdsdigit->GetTrackPid();
          idet      = pmdsdigit->GetDetector();
          ism       = pmdsdigit->GetSMNumber();
          ixp       = pmdsdigit->GetRow();
          iyp       = pmdsdigit->GetColumn();
          edep      = pmdsdigit->GetCellEdep();
-
          if (idet == 0)
            {
              if (fPRE[ism][ixp][iyp] < edep)
                {
                  fPRETrackNo[ism][ixp][iyp] = troffset + itrackno;
+                 fPRETrackPid[ism][ixp][iyp] = itrackpid;
                }
              fPRE[ism][ixp][iyp] += edep;
            }
@@ -882,6 +1219,7 @@ void AliPMDDigitizer::MergeSDigits(Int_t filenumber, Int_t troffset)
              if (fCPV[ism][ixp][iyp] < edep)
                {
                  fCPVTrackNo[ism][ixp][iyp] = troffset + itrackno;
+                 fCPVTrackPid[ism][ixp][iyp] = itrackpid;
                }
              fCPV[ism][ixp][iyp] += edep;
            }
@@ -899,13 +1237,14 @@ void AliPMDDigitizer::TrackAssignment2Cell()
   //
   Bool_t jsort = false;
 
-  Int_t i, j, k;
+  Int_t i = 0, j = 0, k = 0;
 
+  Int_t   *status1;
+  Int_t   *status2;
+  Int_t   *trnarray;
   Float_t *fracEdp;
   Float_t *trEdp;
-  Int_t *status1;
-  Int_t *status2;
-  Int_t *trnarray;
+  
   Int_t   ****pmdTrack;
   Float_t ****pmdEdep;
 
@@ -943,7 +1282,7 @@ void AliPMDDigitizer::TrackAssignment2Cell()
                  nn = 1;
                  pmdTrack[i][j][k] = new Int_t[nn];
                  pmdEdep[i][j][k] = new Float_t[nn];
-               }                     
+               }
              fPRECounter[i][j][k] = 0;
            }
        }
@@ -965,15 +1304,14 @@ void AliPMDDigitizer::TrackAssignment2Cell()
       iyp      = cell->GetY();
       edep     = cell->GetEdep();
       Int_t nn = fPRECounter[ism][ixp][iyp];
-      //      cout << " nn = " << nn << endl;
       pmdTrack[ism][ixp][iyp][nn] = (Int_t) mtrackno;
       pmdEdep[ism][ixp][iyp][nn] = edep;
       fPRECounter[ism][ixp][iyp]++;
     }
   
-  Int_t iz, il;
-  Int_t im, ix, iy;
-  Int_t nn;
+  Int_t iz = 0, il = 0;
+  Int_t im = 0, ix = 0, iy = 0;
+  Int_t nn = 0;
   
   for (im=0; im<fgkTotUM; im++)
     {
@@ -987,7 +1325,7 @@ void AliPMDDigitizer::TrackAssignment2Cell()
                  // This block handles if a cell is fired
                  // many times by many tracks
                  status1  = new Int_t[nn];
-                 status2  = new Int_t[nn];
+                 status2  = new Int_t[2*nn];
                  trnarray = new Int_t[nn];
                  for (iz = 0; iz < nn; iz++)
                    {
@@ -1003,7 +1341,7 @@ void AliPMDDigitizer::TrackAssignment2Cell()
                        {
                          trnarray[trCount] = track;
                          trCount++;
-                       }                             
+                       }
                      trackOld = track;
                    }
                  delete [] status1;
@@ -1097,64 +1435,59 @@ void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc) const
 {
   // This converts the simulated edep to ADC according to the
   // Test Beam Data
-  // To be done
-  //
-
-  //  adc = mev*1.;
+  // PS Test in June 2010, Voltage @ 1300 V
+  // KeV - ADC conversion for 12bit ADC
+  // MPV data used for the fit and taken here
 
+  // constants are from Test Beam 2010
   
-  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
-  // PS Test in September 2003
-  // MeV - ADC conversion for 10bit ADC
-
-  const Float_t kConstant   = 7.181;
-  const Float_t kErConstant = 0.6899;
-  const Float_t kSlope      = 35.93;
-  const Float_t kErSlope    = 0.306;
-
-  //gRandom->SetSeed();
-
-  Float_t cons   = gRandom->Gaus(kConstant,kErConstant);
-  Float_t slop   = gRandom->Gaus(kSlope,kErSlope);
-
-  Float_t adc10bit = slop*mev*0.001 + cons;
+  const Float_t kConstant   = 0.612796;
+  const Float_t kSlope      = 130.158;
+  
+  Float_t adc12bit = kSlope*mev*0.001 + kConstant;
+  if (adc12bit < 0.) adc12bit = 0.;
 
-  // 12 bit adc
+  //Introducing Readout Resolution for ALICE-PMD
 
-  Int_t adc12bit = (Int_t) (4.0*adc10bit);
+  Float_t sigrr     = 0.605016 - 0.000273*adc12bit + 6.54e-8*adc12bit*adc12bit;
+  Float_t adcwithrr = gRandom->Gaus(adc12bit,sigrr);
 
-  if(adc12bit < 3000)
+  if(adcwithrr < 0.)
     {
-      adc = (Float_t) adc12bit;
+      adc = 0.;
     }
-  else if (adc12bit >= 3000)
+  else if(adcwithrr >= 0. && adcwithrr < 1600.0)
     {
-      adc = 3000.0;
+      adc = adcwithrr;
+    }
+  else if (adcwithrr >= 1600.0)
+    {
+      adc = 1600.0;
     }
-
-  // %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% //
 
 }
 //____________________________________________________________________________
-void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber, 
-  Int_t irow, Int_t icol, Float_t adc)
+void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t trpid, Int_t det,
+                               Int_t smnumber, Int_t irow, Int_t icol,
+                               Float_t adc)
 {
   // Add SDigit
   //
   if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
   TClonesArray &lsdigits = *fSDigits;
-  new(lsdigits[fNsdigit++])  AliPMDsdigit(trnumber,det,smnumber,irow,icol,adc);
+  new(lsdigits[fNsdigit++])  AliPMDsdigit(trnumber,trpid,det,smnumber,irow,icol,adc);
 }
 //____________________________________________________________________________
 
-void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber, 
-  Int_t irow, Int_t icol, Float_t adc)
+void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t trpid, Int_t det,
+                              Int_t smnumber, Int_t irow, Int_t icol,
+                              Float_t adc)
 {
   // Add Digit
   //
   if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
   TClonesArray &ldigits = *fDigits;
-  new(ldigits[fNdigit++]) AliPMDdigit(trnumber,det,smnumber,irow,icol,adc);
+  new(ldigits[fNdigit++]) AliPMDdigit(trnumber,trpid, det,smnumber,irow,icol,adc);
 }
 //____________________________________________________________________________
 
@@ -1174,6 +1507,7 @@ void AliPMDDigitizer::ResetCell()
   // clears the cell array and also the counter
   //  for each cell
   //
+  fCPVCell.Delete();
   fCell.Delete();
   for (Int_t i = 0; i < fgkTotUM; i++)
     {
@@ -1181,7 +1515,8 @@ void AliPMDDigitizer::ResetCell()
        {
          for (Int_t k = 0; k < fgkCol; k++)
            {
-             fPRECounter[i][j][k] = 0; 
+             fCPVCounter[i][j][k] = 0; 
+             fPRECounter[i][j][k] = 0;
            }
        }
     }
@@ -1211,10 +1546,12 @@ void AliPMDDigitizer::ResetCellADC()
        {
          for (Int_t k = 0; k < fgkCol; k++)
            {
-             fCPV[i][j][k] = 0.; 
-             fPRE[i][j][k] = 0.; 
-             fPRETrackNo[i][j][k] = 0;
-             fCPVTrackNo[i][j][k] = 0;
+             fCPV[i][j][k]         = 0.;
+             fPRE[i][j][k]         = 0.;
+             fCPVTrackNo[i][j][k]  = 0;
+             fPRETrackNo[i][j][k]  = 0;
+             fCPVTrackPid[i][j][k] = -1;
+             fPRETrackPid[i][j][k] = -1;
            }
        }
     }
@@ -1242,3 +1579,57 @@ void AliPMDDigitizer::UnLoad(Option_t *option)
       fPMDLoader->UnloadSDigits();
     }
 }
+
+//----------------------------------------------------------------------
+Float_t AliPMDDigitizer::Gain(Int_t det, Int_t smn, Int_t row, Int_t col) const
+{
+  // returns of the gain of the cell
+  // Added this method by ZA
+
+  //cout<<" I am here in gain "<<fCalibData<< "smn,row, col "<<smn
+  //<<" "<<row<<" "<<col<<endl;
+
+  if(!fCalibGain) {
+    AliError("No calibration data loaded from CDB!!!");
+    return 1;
+  }
+
+  Float_t GainFact;
+  GainFact = fCalibGain->GetGainFact(det,smn,row,col);
+  return GainFact;
+}
+//----------------------------------------------------------------------
+AliPMDCalibData* AliPMDDigitizer::GetCalibGain() const
+{
+  // The run number will be centralized in AliCDBManager,
+  // you don't need to set it here!
+  // Added this method by ZA
+  // Cleaned up by Alberto
+  AliCDBEntry  *entry = AliCDBManager::Instance()->Get("PMD/Calib/Gain");
+  
+  if(!entry) AliFatal("Calibration object retrieval failed!");
+  
+  AliPMDCalibData *calibdata=0;
+  if (entry) calibdata = (AliPMDCalibData*) entry->GetObject();
+  
+  if (!calibdata)  AliFatal("No calibration data from calibration database !");
+  
+  return calibdata;
+}
+//----------------------------------------------------------------------
+AliPMDPedestal* AliPMDDigitizer::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 calibration database !");
+  
+  return pedestal;
+}