]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PMD/AliPMDDigitizer.cxx
AliPMDPreprocessor and AliPMDRawToSDigits added
[u/mrichter/AliRoot.git] / PMD / AliPMDDigitizer.cxx
index 7e3cd77b5ad11deb9a457f88ba4ff15e20048936..3161a5706be3e1734bce5e62420c37349955a195 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 "AliRunDigitizer.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 "AliPMDDigitizer.h"
-#include "AliMC.h"
+
 
 ClassImp(AliPMDDigitizer)
 
@@ -57,21 +64,14 @@ AliPMDDigitizer::AliPMDDigitizer() :
   fPMDHit(0),
   fPMD(0),
   fPMDLoader(0),
-  fHits(0),
-  fPArray(0),
-  fParticle(0),
-  fTreeH(0),
-  fTreeS(0),
-  fTreeD(0),
-  fSDigits(new TClonesArray("AliPMDsdigit", 1000)),
-  fDigits(new TClonesArray("AliPMDdigit", 1000)),
-  fCell(new TObjArray()),
-  fPMDcell(0),
-  fDebug(0),
+  fCalibData(GetCalibData()),
+  fSDigits(0),
+  fDigits(0),
+  fCell(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
   //
@@ -90,16 +90,72 @@ AliPMDDigitizer::AliPMDDigitizer() :
        }
     }
 
+
 }
 //____________________________________________________________________________
-AliPMDDigitizer::AliPMDDigitizer(AliRunDigitizer* manager) 
-    :AliDigitizer(manager)
+AliPMDDigitizer::AliPMDDigitizer(const AliPMDDigitizer& digitizer):
+  AliDigitizer(digitizer),
+  fRunLoader(0),
+  fPMDHit(0),
+  fPMD(0),
+  fPMDLoader(0),
+  fCalibData(GetCalibData()),
+  fSDigits(0),
+  fDigits(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(AliRunDigitizer* manager):
+  AliDigitizer(manager),
+  fRunLoader(0),
+  fPMDHit(0),
+  fPMD(0),
+  fPMDLoader(0),
+  fCalibData(GetCalibData()),
+  fSDigits(new TClonesArray("AliPMDsdigit", 1000)),
+  fDigits(new TClonesArray("AliPMDdigit", 1000)),
+  fCell(0),
+  fNsdigit(0),
+  fNdigit(0),
+  fDetNo(0),
+  fZPos(361.5)// in units of cm, This is the default position of PMD
 {
   // ctor which should be used
 
-  cerr<<"AliPMDDigitizer::AliPMDDigitizer"
-      <<"(AliRunDigitizer* manager) was processed"<<endl;
+
+  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; 
+           }
+       }
+    }
 }
+
 //____________________________________________________________________________
 AliPMDDigitizer::~AliPMDDigitizer()
 {
@@ -115,11 +171,7 @@ AliPMDDigitizer::~AliPMDDigitizer()
     delete fDigits;
     fDigits=0;
   }
-  if (fCell) {
-    fCell->Delete();
-    delete fCell;
-    fCell=0;
-  }
+  fCell.Delete();
 }
 //
 // Member functions
@@ -131,15 +183,15 @@ void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
   // hits and sdigits or digits depending on the option
   //
 
-  TString evfoldname = AliConfig::fgkDefaultEventFolderName;
+  TString evfoldname = AliConfig::GetDefaultEventFolderName();
   fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
   if (!fRunLoader)
-    fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName,
+    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();
@@ -148,24 +200,20 @@ void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
 
   gAlice = fRunLoader->GetAliRun();
   
-  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 (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");
@@ -194,7 +242,6 @@ 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;
@@ -211,49 +258,50 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
   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.
   // Get pointers to Alice detectors and Hits containers
 
-  fTreeH = fPMDLoader->TreeH();
+  TTree* treeH = fPMDLoader->TreeH();
   
-  Int_t ntracks    = (Int_t) fTreeH->GetEntries();
-  if (fDebug) printf("Number of Tracks in the TreeH = %d \n", ntracks);
-
-  fTreeS = fPMDLoader->TreeS();
-  if (fTreeS == 0x0)
+  Int_t ntracks    = (Int_t) treeH->GetEntries();
+  AliDebug(1,Form("Number of Tracks in the TreeH = %d", ntracks));
+  TTree* treeS = fPMDLoader->TreeS();
+  if (treeS == 0x0)
     {
       fPMDLoader->MakeTree("S");
-      fTreeS = fPMDLoader->TreeS();
+      treeS = fPMDLoader->TreeS();
     }
   Int_t bufsize = 16000;
-  fTreeS->Branch("PMDSDigit", &fSDigits, bufsize); 
+  treeS->Branch("PMDSDigit", &fSDigits, bufsize); 
   
-  if (fPMD) fHits   = fPMD->Hits();
+  TClonesArray* hits = 0;
+  if (fPMD) hits = fPMD->Hits();
 
   // Start loop on tracks in the hits containers
 
   for (Int_t track=0; track<ntracks;track++) 
     {
       gAlice->ResetHits();
-      fTreeH->GetEvent(track);
+      treeH->GetEvent(track);
       if (fPMD) 
        {
-         npmd = fHits->GetEntriesFast();
+         npmd = hits->GetEntriesFast();
          for (int ipmd = 0; ipmd < npmd; ipmd++) 
            {
-             fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
+             fPMDHit = (AliPMDhit*) hits->UncheckedAt(ipmd);
              trackno = fPMDHit->GetTrack();
              //  get kinematics of the particles
 
-             fParticle = gAlice->GetMCApp()->Particle(trackno);
-             trackpid  = fParticle->GetPdgCode();
+             TParticle* mparticle = gAlice->GetMCApp()->Particle(trackno);
+             trackpid  = mparticle->GetPdgCode();
 
              Int_t igatr = -999;
              Int_t ichtr = -999;
@@ -262,7 +310,6 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
              Int_t igen = 0;
              Int_t idmo = -999;
 
-             TParticle*  mparticle = fParticle;
              Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
              if (mparticle->GetFirstMother() == -1)
                {
@@ -323,38 +370,41 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
              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(7); // UnitModule
+             Int_t vol8 = fPMDHit->GetVolume(8); // SuperModule
+
 
              // -----------------------------------------//
+             // 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;
+             smnumber = (vol8-1)*6 + vol7;
 
-             if (vol6 == 1 || vol6 == 2)
-               {
-                 xpad = vol1;
-                 ypad = vol2;
-               }
-             else if (vol6 == 3 || vol6 == 4)
+             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;
-
-             Float_t zposition = TMath::Abs(zPos);
-             if (zposition < fZPos)
+             AliDebug(2,Form("Zposition = %f Edeposition = %f",zPos,edep));
+             //Float_t zposition = TMath::Abs(zPos);
+             if (zPos < fZPos)
                {
                  // CPV
                  fDetNo = 1;
                }
-             else if (zposition > fZPos)
+             else if (zPos > fZPos)
                {
                  // PMD
                  fDetNo = 0;
@@ -367,9 +417,9 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
                  fPRE[smn][ixx][iyy] += edep;
                  fPRECounter[smn][ixx][iyy]++;
 
-                 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
+                 AliPMDcell* cell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
 
-                 fCell->Add(fPMDcell);
+                 fCell.Add(cell);
                }
              else if(fDetNo == 1)
                {
@@ -413,14 +463,12 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
                    }
                }
            }
-         fTreeS->Fill();
+         treeS->Fill();
          ResetSDigit();
        }
     }
   fPMDLoader->WriteSDigits("OVERWRITE");
   ResetCellADC();
-
-  //  cout << " -------- End of Hits2SDigit ----------- " << endl;
 }
 //____________________________________________________________________________
 
@@ -443,12 +491,12 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
   Float_t edep;
   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.
@@ -459,42 +507,43 @@ 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");
     }
-  fTreeH = fPMDLoader->TreeH();
-  Int_t ntracks    = (Int_t) fTreeH->GetEntries();
-  if (fDebug) printf("Number of Tracks in the TreeH = %d \n", ntracks);
+  TTree* treeH = fPMDLoader->TreeH();
+  Int_t ntracks    = (Int_t) treeH->GetEntries();
+  AliDebug(1,Form("Number of Tracks in the TreeH = %d", ntracks));
   fPMDLoader->LoadDigits("recreate");
-  fTreeD = fPMDLoader->TreeD();
-  if (fTreeD == 0x0)
+  TTree* treeD = fPMDLoader->TreeD();
+  if (treeD == 0x0)
     {
       fPMDLoader->MakeTree("D");
-      fTreeD = fPMDLoader->TreeD();
+      treeD = fPMDLoader->TreeD();
     }
   Int_t bufsize = 16000;
-  fTreeD->Branch("PMDDigit", &fDigits, bufsize); 
+  treeD->Branch("PMDDigit", &fDigits, bufsize); 
   
-  if (fPMD) fHits   = fPMD->Hits();
+  TClonesArray* hits = 0;
+  if (fPMD) hits = fPMD->Hits();
 
   // Start loop on tracks in the hits containers
 
   for (Int_t track=0; track<ntracks;track++) 
     {
       gAlice->ResetHits();
-      fTreeH->GetEvent(track);
+      treeH->GetEvent(track);
       
       if (fPMD) 
        {
-         npmd = fHits->GetEntriesFast();
+         npmd = hits->GetEntriesFast();
          for (int ipmd = 0; ipmd < npmd; ipmd++) 
            {
-             fPMDHit = (AliPMDhit*) fHits->UncheckedAt(ipmd);
+             fPMDHit = (AliPMDhit*) hits->UncheckedAt(ipmd);
              trackno = fPMDHit->GetTrack();
              
              //  get kinematics of the particles
              
-             fParticle = gAlice->GetMCApp()->Particle(trackno);
-             trackpid  = fParticle->GetPdgCode();
+             TParticle* mparticle = gAlice->GetMCApp()->Particle(trackno);
+             trackpid  = mparticle->GetPdgCode();
 
              Int_t igatr = -999;
              Int_t ichtr = -999;
@@ -503,7 +552,6 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
              Int_t igen = 0;
              Int_t idmo = -999;
 
-             TParticle*  mparticle = fParticle;
              Int_t tracknoOld=0, trackpidOld=0, statusOld = 0;
              if (mparticle->GetFirstMother() == -1)
                {
@@ -562,43 +610,43 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
              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(7); // UnitModule
+             Int_t vol8 = fPMDHit->GetVolume(8); // SuperModule
+
 
              // -----------------------------------------//
+             // 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;
+             smnumber = (vol8-1)*6 + vol7;
 
-             if (vol6 == 1 || vol6 == 2)
-               {
-                 xpad = vol1;
-                 ypad = vol2;
-               }
-             else if (vol6 == 3 || vol6 == 4)
+             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 = %d",zPos,edep));
+             //Float_t zposition = TMath::Abs(zPos);
 
-             if (zposition < fZPos)
+             if (zPos < fZPos)
                {
                  // CPV
                  fDetNo = 1;
                }
-             else if (zposition > fZPos)
+             else if (zPos > fZPos)
                {
                  // PMD
                  fDetNo = 0;
@@ -612,9 +660,9 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
                  fPRE[smn][ixx][iyy] += edep;
                  fPRECounter[smn][ixx][iyy]++;
 
-                 fPMDcell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
+                 AliPMDcell* cell = new AliPMDcell(mtrackno,smn,ixx,iyy,edep);
 
-                 fCell->Add(fPMDcell);
+                 fCell.Add(cell);
                }
              else if(fDetNo == 1)
                {
@@ -628,11 +676,11 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
   TrackAssignment2Cell();
   ResetCell();
 
+  Float_t gain1;
   Float_t adc;
   Float_t deltaE = 0.;
   Int_t detno = 0;
   Int_t trno = 1;
-
   for (Int_t idet = 0; idet < 2; idet++)
     {
       for (Int_t ism = 0; ism < fgkTotUM; ism++)
@@ -643,13 +691,16 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
                {
                  if (idet == 0)
                    {
-                     deltaE = fPRE[ism][jrow][kcol];
+                     gain1 = Gain(idet,ism,jrow,kcol);
+
+                     deltaE = fPRE[ism][jrow][kcol]*gain1;
                      trno   = fPRETrackNo[ism][jrow][kcol];
                      detno = 0;
                    }
                  else if (idet == 1)
                    {
-                     deltaE = fCPV[ism][jrow][kcol];
+                     gain1 = Gain(idet,ism,jrow,kcol);
+                     deltaE = fCPV[ism][jrow][kcol]*gain1;
                      trno   = fCPVTrackNo[ism][jrow][kcol];
                      detno = 1;
                    }
@@ -660,15 +711,14 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
                    }
                } // column loop
            } // row    loop
+         treeD->Fill();
+         ResetDigit();
        } // supermodule loop
-      fTreeD->Fill();
-      ResetDigit();
     } // detector loop
-
+  
   fPMDLoader->WriteDigits("OVERWRITE");
   ResetCellADC();
   
-  //  cout << " -------- End of Hits2Digit ----------- " << endl;
 }
 //____________________________________________________________________________
 
@@ -678,34 +728,42 @@ 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);
 
-  fTreeS = fPMDLoader->TreeS();
+  TTree* treeS = fPMDLoader->TreeS();
   AliPMDsdigit  *pmdsdigit;
-  TBranch *branch = fTreeS->GetBranch("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);
 
-  fTreeD = fPMDLoader->TreeD();
-  if (fTreeD == 0x0)
+  TTree* treeD = fPMDLoader->TreeD();
+  if (treeD == 0x0)
     {
       fPMDLoader->MakeTree("D");
-      fTreeD = fPMDLoader->TreeD();
+      treeD = fPMDLoader->TreeD();
     }
   Int_t bufsize = 16000;
-  fTreeD->Branch("PMDDigit", &fDigits, bufsize); 
+  if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
+  treeD->Branch("PMDDigit", &fDigits, bufsize); 
 
   Int_t   trno, det, smn;
   Int_t   irow, icol;
   Float_t edep, adc;
 
-  Int_t nmodules = (Int_t) fTreeS->GetEntries();
+  Int_t nmodules = (Int_t) treeS->GetEntries();
+  AliDebug(1,Form("Number of modules = %d",nmodules));
 
   for (Int_t imodule = 0; imodule < nmodules; imodule++)
     {
-      fTreeS->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);
@@ -719,31 +777,24 @@ void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
          MeV2ADC(edep,adc);
          AddDigit(trno,det,smn,irow,icol,adc);      
        }
-      fTreeD->Fill();
+      treeD->Fill();
       ResetDigit();
     }
   fPMDLoader->WriteDigits("OVERWRITE");
-  //  cout << " -------- End of SDigits2Digit ----------- " << endl;
+
 }
 //____________________________________________________________________________
 void AliPMDDigitizer::Exec(Option_t *option)
 {
   // Does the event merging and digitization
-
-  fDebug = 0;
   const char *cdeb = strstr(option,"deb");
   if(cdeb)
     {
-      cout << "**************** PMD Exec *************** " << endl;
-      fDebug = 1;
+      AliDebug(100," *** PMD Exec is called ***");
     }
-  fDigits = new TClonesArray("AliPMDdigit", 1000);
-  
+
   Int_t ninputs = fManager->GetNinputs();
-  if(fDebug)
-    {
-      cout << " Number of files = " << ninputs << endl;
-    }
+  AliDebug(1,Form("Number of files to be processed = %d",ninputs));
   ResetCellADC();
 
   for (Int_t i = 0; i < ninputs; i++)
@@ -757,17 +808,18 @@ void AliPMDDigitizer::Exec(Option_t *option)
   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("recreate");
-  fTreeD = fPMDLoader->TreeD();
-  if (fTreeD == 0x0)
+  fPMDLoader->LoadDigits("update");
+  TTree* treeD = fPMDLoader->TreeD();
+  if (treeD == 0x0)
     {
       fPMDLoader->MakeTree("D");
-      fTreeD = fPMDLoader->TreeD();
+      treeD = fPMDLoader->TreeD();
     }
   Int_t bufsize = 16000;
-  fTreeD->Branch("PMDDigit", &fDigits, bufsize); 
+  if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
+  treeD->Branch("PMDDigit", &fDigits, bufsize); 
 
   Float_t adc;
   Float_t deltaE = 0.;
@@ -801,13 +853,13 @@ void AliPMDDigitizer::Exec(Option_t *option)
                    }
                } // column loop
            } // row    loop
-         fTreeD->Fill();
+         treeD->Fill();
          ResetDigit();
        } // supermodule loop
     } // detector loop
-  ResetCellADC();
-
   fPMDLoader->WriteDigits("OVERWRITE");  
+  fPMDLoader->UnloadDigits();
+  ResetCellADC();
 }
 //____________________________________________________________________________
 
@@ -817,29 +869,23 @@ void AliPMDDigitizer::MergeSDigits(Int_t filenumber, Int_t troffset)
   fRunLoader = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(filenumber));
   fPMDLoader = fRunLoader->GetLoader("PMDLoader");
   fPMDLoader->LoadSDigits("read");
-  fTreeS = fPMDLoader->TreeS();
+  TTree* treeS = fPMDLoader->TreeS();
   AliPMDsdigit  *pmdsdigit;
-  TBranch *branch = fTreeS->GetBranch("PMDSDigit");
+  TBranch *branch = treeS->GetBranch("PMDSDigit");
+  if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
   branch->SetAddress(&fSDigits);
 
   Int_t   itrackno, idet, ism;
   Int_t   ixp, iyp;
   Float_t edep;
-  
-  Int_t nmodules = (Int_t) fTreeS->GetEntries();
-  if(fDebug)
-    {
-      cout << " nmodules = " << nmodules << endl;
-      cout << " tr offset = " << troffset << endl;
-    }
+  Int_t nmodules = (Int_t) treeS->GetEntries();
+  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++)
     {
-      fTreeS->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);
@@ -849,7 +895,6 @@ void AliPMDDigitizer::MergeSDigits(Int_t filenumber, Int_t troffset)
          ixp       = pmdsdigit->GetRow();
          iyp       = pmdsdigit->GetColumn();
          edep      = pmdsdigit->GetCellEdep();
-
          if (idet == 0)
            {
              if (fPRE[ism][ixp][iyp] < edep)
@@ -931,22 +976,21 @@ void AliPMDDigitizer::TrackAssignment2Cell()
     }
 
 
-  Int_t nentries = fCell->GetEntries();
+  Int_t nentries = fCell.GetEntries();
 
   Int_t   mtrackno, ism, ixp, iyp;
   Float_t edep;
 
   for (i = 0; i < nentries; i++)
     {
-      fPMDcell = (AliPMDcell*)fCell->UncheckedAt(i);
+      AliPMDcell* cell = (AliPMDcell*)fCell.UncheckedAt(i);
       
-      mtrackno = fPMDcell->GetTrackNumber();
-      ism      = fPMDcell->GetSMNumber();
-      ixp      = fPMDcell->GetX();
-      iyp      = fPMDcell->GetY();
-      edep     = fPMDcell->GetEdep();
+      mtrackno = cell->GetTrackNumber();
+      ism      = cell->GetSMNumber();
+      ixp      = cell->GetX();
+      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]++;
@@ -1080,7 +1124,35 @@ void AliPMDDigitizer::MeV2ADC(Float_t mev, Float_t & adc) const
   // Test Beam Data
   // To be done
   //
-  adc = mev*1.;
+
+  // 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;
+
+  // 12 bit adc
+
+  Int_t adc12bit = (Int_t) (4.0*adc10bit);
+
+  if(adc12bit < 3000)
+    {
+      adc = (Float_t) adc12bit;
+    }
+  else if (adc12bit >= 3000)
+    {
+      adc = 3000.0;
+    }
+
 }
 //____________________________________________________________________________
 void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber, 
@@ -1088,6 +1160,7 @@ void AliPMDDigitizer::AddSDigit(Int_t trnumber, Int_t det, Int_t smnumber,
 {
   // Add SDigit
   //
+  if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
   TClonesArray &lsdigits = *fSDigits;
   new(lsdigits[fNsdigit++])  AliPMDsdigit(trnumber,det,smnumber,irow,icol,adc);
 }
@@ -1098,6 +1171,7 @@ void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber,
 {
   // Add Digit
   //
+  if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
   TClonesArray &ldigits = *fDigits;
   new(ldigits[fNdigit++]) AliPMDdigit(trnumber,det,smnumber,irow,icol,adc);
 }
@@ -1119,7 +1193,7 @@ void AliPMDDigitizer::ResetCell()
   // clears the cell array and also the counter
   //  for each cell
   //
-  fCell->Clear();
+  fCell.Delete();
   for (Int_t i = 0; i < fgkTotUM; i++)
     {
       for (Int_t j = 0; j < fgkRow; j++)
@@ -1136,14 +1210,14 @@ void AliPMDDigitizer::ResetSDigit()
 {
   // Clears SDigits
   fNsdigit = 0;
-  if (fSDigits) fSDigits->Clear();
+  if (fSDigits) fSDigits->Delete();
 }
 //____________________________________________________________________________
 void AliPMDDigitizer::ResetDigit()
 {
   // Clears Digits
   fNdigit = 0;
-  if (fDigits) fDigits->Clear();
+  if (fDigits) fDigits->Delete();
 }
 //____________________________________________________________________________
 
@@ -1164,6 +1238,7 @@ void AliPMDDigitizer::ResetCellADC()
        }
     }
 }
+//------------------------------------------------------
 //____________________________________________________________________________
 
 void AliPMDDigitizer::UnLoad(Option_t *option)
@@ -1187,3 +1262,51 @@ 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(!fCalibData) {
+    AliError("No calibration data loaded from CDB!!!");
+    return 1;
+  }
+
+  Float_t GainFact;
+  GainFact = fCalibData->GetGainFact(det,smn,row,col);
+  printf("\t gain=%10.3f\n",GainFact);
+  return GainFact;
+}
+//----------------------------------------------------------------------
+AliPMDCalibData* AliPMDDigitizer::GetCalibData() const
+{
+  // The run number will be centralized in AliCDBManager,
+  // you don't need to set it here!
+  // Added this method by ZA
+  AliCDBEntry  *entry = AliCDBManager::Instance()->Get("PMD/Calib/Data");
+  
+  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);  
+  }
+  
+  AliPMDCalibData *calibdata=0;
+  if (entry) calibdata = (AliPMDCalibData*) entry->GetObject();
+  
+  if (!calibdata)  AliError("No calibration data from calibration database !");
+  
+  return calibdata;
+}