]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - PMD/AliPMDDigitizer.cxx
Bug fixes
[u/mrichter/AliRoot.git] / PMD / AliPMDDigitizer.cxx
index 03f6454c44bd74135bd2baad1a954e11973ce37a..e16e879175416282b3c27240306368a2d2366319 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "AliRun.h"
 #include "AliPMD.h"
+#include "AliPMDhit.h"
 #include "AliHit.h"
 #include "AliDetector.h"
 #include "AliRunLoader.h"
 #include "AliConfig.h"
 #include "AliMagF.h"
 #include "AliRunDigitizer.h"
+#include "AliDigitizer.h"
 #include "AliHeader.h"
 
 #include "AliPMDcell.h"
 #include "AliPMDsdigit.h"
 #include "AliPMDdigit.h"
 #include "AliPMDDigitizer.h"
-#include "AliPMDClustering.h"
 #include "AliMC.h"
 
 ClassImp(AliPMDDigitizer)
 
-AliPMDDigitizer::AliPMDDigitizer()
+AliPMDDigitizer::AliPMDDigitizer() :
+  fRunLoader(0),
+  fPMDHit(0),
+  fPMD(0),
+  fPMDLoader(0),
+  fSDigits(0),
+  fDigits(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
 {
   // Default Constructor
   //
-  if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);  
-  fNsdigit = 0;
-  if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);  
-  fNdigit = 0;
-
   for (Int_t i = 0; i < fgkTotUM; i++)
     {
       for (Int_t j = 0; j < fgkRow; j++)
@@ -68,54 +76,104 @@ AliPMDDigitizer::AliPMDDigitizer()
            {
              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; 
            }
        }
     }
 
-  if (!fCell) fCell = new TObjArray();
+}
+//____________________________________________________________________________
+AliPMDDigitizer::AliPMDDigitizer(AliRunDigitizer* manager) 
+  :AliDigitizer(manager),
+  fRunLoader(0),
+  fPMDHit(0),
+  fPMD(0),
+  fPMDLoader(0),
+  fSDigits(new TClonesArray("AliPMDsdigit", 1000)),
+  fDigits(new TClonesArray("AliPMDdigit", 1000)),
+  fCell(0),
+  fDebug(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
 
-  fZPos = 361.5; // in units of cm, This is the default position of PMD
+  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()
 {
   // Default Destructor
   //
-  delete fSDigits;
-  delete fDigits;
-  delete fCell;
+  if (fSDigits) {
+    fSDigits->Delete();
+    delete fSDigits;
+    fSDigits=0;
+  }
+  if (fDigits) {
+    fDigits->Delete();
+    delete fDigits;
+    fDigits=0;
+  }
+  fCell.Delete();
 }
 //
 // Member functions
 //
+//____________________________________________________________________________
 void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
 {
   // Loads galice.root file and corresponding header, kinematics
   // hits and sdigits or digits depending on the option
   //
-  fRunLoader = AliRunLoader::Open(file,AliConfig::fgkDefaultEventFolderName);
+
+  TString evfoldname = AliConfig::GetDefaultEventFolderName();
+  fRunLoader = AliRunLoader::GetRunLoader(evfoldname);
+  if (!fRunLoader)
+    fRunLoader = AliRunLoader::Open(file,AliConfig::GetDefaultEventFolderName(),
+                                   "UPDATE");
   
   if (!fRunLoader)
    {
      Error("Open","Can not open session for file %s.",file);
    }
   
-  fRunLoader->LoadgAlice();
-  fRunLoader->LoadHeader();
-  fRunLoader->LoadKinematics();
+  if (!fRunLoader->GetAliRun()) fRunLoader->LoadgAlice();
+  if (!fRunLoader->TreeE()) fRunLoader->LoadHeader();
+  if (!fRunLoader->TreeK()) fRunLoader->LoadKinematics();
 
   gAlice = fRunLoader->GetAliRun();
   
-  if (gAlice)
-    {
-      printf("<AliPMDdigitizer::Open> ");
-      printf("AliRun object found on file.\n");
-    }
-  else
-    {
-      printf("<AliPMDdigitizer::Open> ");
-      printf("Could not find AliRun object.\n");
-    }
-
+  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");
+      }
+  }
+  
   fPMD  = (AliPMD*)gAlice->GetDetector("PMD");
   fPMDLoader = fRunLoader->GetLoader("PMDLoader");
   if (fPMDLoader == 0x0)
@@ -142,8 +200,8 @@ void AliPMDDigitizer::OpengAliceFile(const char *file, Option_t *option)
       fPMDLoader->LoadSDigits("READ");
       fPMDLoader->LoadDigits("recreate");
     }
-
 }
+//____________________________________________________________________________
 void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
 {
   // This reads the PMD Hits tree and assigns the right track number
@@ -166,50 +224,51 @@ 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();
 
-  printf("Event Number =  %d \n",ievt); 
+  if (fDebug) printf("Event Number =  %d \n",ievt); 
   Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
-  printf("Number of Particles = %d \n", nparticles);
+  if (fDebug) printf("Number of Particles = %d \n", nparticles);
   fRunLoader->GetEvent(ievt);
-  //  fPArray = gAlice->GetMCApp()->Particles();
   // ------------------------------------------------------- //
   // 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();
-  printf("Number of Tracks in the TreeH = %d \n", ntracks);
+  Int_t ntracks    = (Int_t) treeH->GetEntries();
+  if (fDebug) printf("Number of Tracks in the TreeH = %d \n", ntracks);
 
-  fTreeS = fPMDLoader->TreeS();
-  if (fTreeS == 0x0)
+  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;
@@ -218,7 +277,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)
                {
@@ -275,11 +333,13 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
              xPos = fPMDHit->X();
              yPos = fPMDHit->Y();
              zPos = fPMDHit->Z();
-             edep       = fPMDHit->fEnergy;
-             Int_t vol1 = fPMDHit->fVolume[1]; // Column
-             Int_t vol2 = fPMDHit->fVolume[2]; // Row
-             Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
-             Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
+             
+             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
+
              // -----------------------------------------//
              // For Super Module 1 & 2                   //
              //  nrow = 96, ncol = 48                    //
@@ -321,9 +381,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)
                {
@@ -340,7 +400,6 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
   Float_t deltaE      = 0.;
   Int_t   detno       = 0;
   Int_t   trno        = -1;
-  Int_t   cellno      = 0;
 
   for (Int_t idet = 0; idet < 2; idet++)
     {
@@ -350,7 +409,6 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
            {
              for (Int_t kcol = 0; kcol < fgkCol; kcol++)
                {
-                 cellno = jrow*fgkCol + kcol;
                  if (idet == 0)
                    {
                      deltaE = fPRE[ism][jrow][kcol];
@@ -365,11 +423,11 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
                    }
                  if (deltaE > 0.)
                    {
-                     AddSDigit(trno,detno,ism,cellno,deltaE);
+                     AddSDigit(trno,detno,ism,jrow,kcol,deltaE);
                    }
                }
            }
-         fTreeS->Fill();
+         treeS->Fill();
          ResetSDigit();
        }
     }
@@ -378,6 +436,7 @@ void AliPMDDigitizer::Hits2SDigits(Int_t ievt)
 
   //  cout << " -------- End of Hits2SDigit ----------- " << endl;
 }
+//____________________________________________________________________________
 
 void AliPMDDigitizer::Hits2Digits(Int_t ievt)
 {
@@ -398,14 +457,14 @@ 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();
 
-  printf("Event Number =  %d \n",ievt); 
+  if (fDebug) printf("Event Number =  %d \n",ievt); 
 
   Int_t nparticles = fRunLoader->GetHeader()->GetNtrack();
-  printf("Number of Particles = %d \n", nparticles);
+  if (fDebug) printf("Number of Particles = %d \n", nparticles);
   fRunLoader->GetEvent(ievt);
-  //  fPArray = gAlice->GetMCApp()->Particles();
   // ------------------------------------------------------- //
   // Pointer to specific detector hits.
   // Get pointers to Alice detectors and Hits containers
@@ -417,40 +476,41 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
     {
       cerr<<"Hits2Digits method : Can not find PMD or PMDLoader\n";
     }
-  fTreeH = fPMDLoader->TreeH();
-  Int_t ntracks    = (Int_t) fTreeH->GetEntries();
-  printf("Number of Tracks in the TreeH = %d \n", ntracks);
+  TTree* treeH = fPMDLoader->TreeH();
+  Int_t ntracks    = (Int_t) treeH->GetEntries();
+  if (fDebug) printf("Number of Tracks in the TreeH = %d \n", 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;
@@ -459,7 +519,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)
                {
@@ -518,11 +577,12 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
              xPos = fPMDHit->X();
              yPos = fPMDHit->Y();
              zPos = fPMDHit->Z();
-             edep       = fPMDHit->fEnergy;
-             Int_t vol1 = fPMDHit->fVolume[1]; // Column
-             Int_t vol2 = fPMDHit->fVolume[2]; // Row
-             Int_t vol3 = fPMDHit->fVolume[3]; // UnitModule
-             Int_t vol6 = fPMDHit->fVolume[6]; // SuperModule
+
+             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
 
              // -----------------------------------------//
              // For Super Module 1 & 2                   //
@@ -567,9 +627,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)
                {
@@ -583,10 +643,10 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
   TrackAssignment2Cell();
   ResetCell();
 
+  Float_t adc;
   Float_t deltaE = 0.;
   Int_t detno = 0;
   Int_t trno = 1;
-  Int_t cellno;
 
   for (Int_t idet = 0; idet < 2; idet++)
     {
@@ -596,7 +656,6 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
            {
              for (Int_t kcol = 0; kcol < fgkCol; kcol++)
                {
-                 cellno = jrow*fgkCol + kcol;
                  if (idet == 0)
                    {
                      deltaE = fPRE[ism][jrow][kcol];
@@ -611,12 +670,13 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
                    }
                  if (deltaE > 0.)
                    {
-                     AddDigit(trno,detno,ism,cellno,deltaE);
+                     MeV2ADC(deltaE,adc);
+                     AddDigit(trno,detno,ism,jrow,kcol,adc);
                    }
                } // column loop
            } // row    loop
        } // supermodule loop
-      fTreeD->Fill();
+      treeD->Fill();
       ResetDigit();
     } // detector loop
 
@@ -625,6 +685,7 @@ void AliPMDDigitizer::Hits2Digits(Int_t ievt)
   
   //  cout << " -------- End of Hits2Digit ----------- " << endl;
 }
+//____________________________________________________________________________
 
 
 void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
@@ -635,29 +696,31 @@ void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
   //  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 (!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   cellno;
+  Int_t   irow, icol;
   Float_t edep, adc;
 
-  Int_t nmodules = (Int_t) fTreeS->GetEntries();
+  Int_t nmodules = (Int_t) treeS->GetEntries();
 
   for (Int_t imodule = 0; imodule < nmodules; imodule++)
     {
-      fTreeS->GetEntry(imodule); 
+      treeS->GetEntry(imodule); 
       Int_t nentries = fSDigits->GetLast();
       //cout << " nentries = " << nentries << endl;
       for (Int_t ient = 0; ient < nentries+1; ient++)
@@ -666,19 +729,166 @@ void AliPMDDigitizer::SDigits2Digits(Int_t ievt)
          trno   = pmdsdigit->GetTrackNumber();
          det    = pmdsdigit->GetDetector();
          smn    = pmdsdigit->GetSMNumber();
-         cellno = pmdsdigit->GetCellNumber();
+         irow   = pmdsdigit->GetRow();
+         icol   = pmdsdigit->GetColumn();
          edep   = pmdsdigit->GetCellEdep();
 
          MeV2ADC(edep,adc);
-         AddDigit(trno,det,smn,cellno,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;
+    }
+  
+  Int_t ninputs = fManager->GetNinputs();
+  if(fDebug)
+    {
+      cout << " Number of files = " << ninputs << endl;
+    }
+  ResetCellADC();
+
+  for (Int_t i = 0; i < ninputs; i++)
+    {
+      Int_t troffset = fManager->GetMask(i);
+      MergeSDigits(i, troffset);
+    }
+
+  fRunLoader = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
+  fPMD  = (AliPMD*)gAlice->GetDetector("PMD");
+  fPMDLoader = fRunLoader->GetLoader("PMDLoader");
+  if (fPMDLoader == 0x0)
+    {
+      cerr<<"AliPMDDigitizer::Exec : Can not find PMD or PMDLoader\n";
+    }
+  fPMDLoader->LoadDigits("update");
+  TTree* treeD = fPMDLoader->TreeD();
+  if (treeD == 0x0)
+    {
+      fPMDLoader->MakeTree("D");
+      treeD = fPMDLoader->TreeD();
+    }
+  Int_t bufsize = 16000;
+  if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
+  treeD->Branch("PMDDigit", &fDigits, bufsize); 
+
+  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++)
+       {
+         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;
+                   }
+                 else if (idet == 1)
+                   {
+                     deltaE = fCPV[ism][jrow][kcol];
+                     trno   = fCPVTrackNo[ism][jrow][kcol];
+                     detno = 1;
+                   }
+                 if (deltaE > 0.)
+                   {
+                     MeV2ADC(deltaE,adc);
+                     AddDigit(trno,detno,ism,jrow,kcol,adc);
+                   }
+               } // column loop
+           } // row    loop
+         treeD->Fill();
+         ResetDigit();
+       } // supermodule loop
+    } // detector loop
+  fPMDLoader->WriteDigits("OVERWRITE");  
+  fPMDLoader->UnloadDigits();
+  ResetCellADC();
+}
+//____________________________________________________________________________
+
+void AliPMDDigitizer::MergeSDigits(Int_t filenumber, Int_t troffset)
+{
+  // merging sdigits
+  fRunLoader = AliRunLoader::GetRunLoader(fManager->GetInputFolderName(filenumber));
+  fPMDLoader = fRunLoader->GetLoader("PMDLoader");
+  fPMDLoader->LoadSDigits("read");
+  TTree* treeS = fPMDLoader->TreeS();
+  AliPMDsdigit  *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) treeS->GetEntries();
+  if(fDebug)
+    {
+      cout << " nmodules = " << nmodules << endl;
+      cout << " tr offset = " << troffset << endl;
+    }
+  for (Int_t imodule = 0; imodule < nmodules; imodule++)
+    {
+      treeS->GetEntry(imodule); 
+      Int_t nentries = fSDigits->GetLast();
+      if(fDebug)
+       {
+         cout << " nentries = " << nentries << endl;
+       }
+      for (Int_t ient = 0; ient < nentries+1; ient++)
+       {
+         pmdsdigit = (AliPMDsdigit*)fSDigits->UncheckedAt(ient);
+         itrackno  = pmdsdigit->GetTrackNumber();
+         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;
+               }
+             fPRE[ism][ixp][iyp] += edep;
+           }
+         else if (idet == 1)
+           {
+             if (fCPV[ism][ixp][iyp] < edep)
+               {
+                 fCPVTrackNo[ism][ixp][iyp] = troffset + itrackno;
+               }
+             fCPV[ism][ixp][iyp] += edep;
+           }
+       }
+    }
+
+}
+// ----------------------------------------------------------------------
 void AliPMDDigitizer::TrackAssignment2Cell()
 {
   // 
@@ -739,20 +949,20 @@ 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;
@@ -795,8 +1005,8 @@ void AliPMDDigitizer::TrackAssignment2Cell()
                        }                             
                      trackOld = track;
                    }
-                 delete status1;
-                 delete status2;
+                 delete [] status1;
+                 delete [] status2;
                  Float_t totEdp = 0.;
                  trEdp = new Float_t[trCount];
                  fracEdp = new Float_t[trCount];
@@ -826,9 +1036,9 @@ void AliPMDDigitizer::TrackAssignment2Cell()
                        }
                    }
                  fPRETrackNo[im][ix][iy] = trnarray[ilOld];
-                 delete fracEdp;
-                 delete trEdp;
-                 delete trnarray;
+                 delete [] fracEdp;
+                 delete [] trEdp;
+                 delete [] trnarray;
                }
              else if (nn == 1)
                {
@@ -875,60 +1085,95 @@ void AliPMDDigitizer::TrackAssignment2Cell()
       delete [] pmdTrack[i];
       delete [] pmdEdep[i];
     }
-  delete pmdTrack;
-  delete pmdEdep;
+  delete [] pmdTrack;
+  delete [] pmdEdep;
   // 
   // End of the cell id assignment
   //
 }
-
+//____________________________________________________________________________
 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.;
+
+  //  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, 
-  Int_t cellnumber, Float_t adc)
+  Int_t irow, Int_t icol, Float_t adc)
 {
   // Add SDigit
   //
+  if (!fSDigits) fSDigits = new TClonesArray("AliPMDsdigit", 1000);
   TClonesArray &lsdigits = *fSDigits;
-  AliPMDsdigit *newcell;
-  newcell = new AliPMDsdigit(trnumber,det,smnumber,cellnumber,adc);
-  new(lsdigits[fNsdigit++]) AliPMDsdigit(newcell);
-  delete newcell;
+  new(lsdigits[fNsdigit++])  AliPMDsdigit(trnumber,det,smnumber,irow,icol,adc);
 }
+//____________________________________________________________________________
 
 void AliPMDDigitizer::AddDigit(Int_t trnumber, Int_t det, Int_t smnumber, 
-  Int_t cellnumber, Float_t adc)
+  Int_t irow, Int_t icol, Float_t adc)
 {
   // Add Digit
   //
+  if (!fDigits) fDigits = new TClonesArray("AliPMDdigit", 1000);
   TClonesArray &ldigits = *fDigits;
-  AliPMDdigit *newcell;
-  newcell = new AliPMDdigit(trnumber,det,smnumber,cellnumber,adc);
-  new(ldigits[fNdigit++]) AliPMDdigit(newcell);
-  delete newcell;
+  new(ldigits[fNdigit++]) AliPMDdigit(trnumber,det,smnumber,irow,icol,adc);
 }
+//____________________________________________________________________________
 
 void AliPMDDigitizer::SetZPosition(Float_t zpos)
 {
   fZPos = zpos;
 }
+//____________________________________________________________________________
 Float_t AliPMDDigitizer::GetZPosition() const
 {
   return fZPos;
 }
+//____________________________________________________________________________
 
 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++)
@@ -940,22 +1185,25 @@ void AliPMDDigitizer::ResetCell()
        }
     }
 }
+//____________________________________________________________________________
 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();
 }
+//____________________________________________________________________________
 
 void AliPMDDigitizer::ResetCellADC()
 {
-  // Clears individual cells edep
+  // Clears individual cells edep and track number
   for (Int_t i = 0; i < fgkTotUM; i++)
     {
       for (Int_t j = 0; j < fgkRow; j++)
@@ -964,10 +1212,13 @@ void AliPMDDigitizer::ResetCellADC()
            {
              fCPV[i][j][k] = 0.; 
              fPRE[i][j][k] = 0.; 
+             fPRETrackNo[i][j][k] = 0;
+             fCPVTrackNo[i][j][k] = 0;
            }
        }
     }
 }
+//____________________________________________________________________________
 
 void AliPMDDigitizer::UnLoad(Option_t *option)
 {