]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ZDC/AliZDC.cxx
Fixes in order to write correct raw-data on big-endian platforms (Marco)
[u/mrichter/AliRoot.git] / ZDC / AliZDC.cxx
index f09fd3aca805ed60f9378addb7f03ddde728899e..12f8fe548dff8c506323860f3cc62e7277e7e0bf 100644 (file)
  * provided "as is" without express or implied warranty.                  *
  **************************************************************************/
 
-/*
-$Log$
-Revision 1.28  2002/02/04 09:18:08  coppedis
-Merging and reconstruction code review
-
-Revision 1.26.2.2  2001/11/12 18:41:44  hristov
-All the changes from the head are merged to the release
-
-Revision 1.27  2001/10/21 18:27:45  hristov
-Several pointers were set to zero in the default constructors to avoid memory management problems
-
-Revision 1.26  2001/10/04 14:30:28  coppedis
-Event merging for ZDC
-
-Revision 1.25  2001/10/04 14:24:15  coppedis
-Event merging for ZDC
-
-Revision 1.24  2001/09/26 16:03:41  coppedis
-Merging implemented
-
-Revision 1.23  2001/05/15 13:44:57  coppedis
-Changes in AddHit method
-
-Revision 1.22  2001/05/14 09:53:32  coppedis
-Adding functions ZMin and ZMax
-
-Revision 1.21  2001/04/20 10:05:02  coppedis
-Minor changes
-
-Revision 1.20  2001/03/26 13:39:20  coppedis
-Comment prints
-
-Revision 1.19  2001/03/26 09:10:23  coppedis
-Corrected bug in constructor (fIshunt has to be =1)
-
-Revision 1.18  2001/03/20 08:21:55  coppedis
-ZDC needs PIPE, ABSO, DIPO and SHIL
-
-Revision 1.17  2001/03/16 16:18:03  coppedis
-Correction for superposition of ZDC volumes with MUON arm one
-
-Revision 1.16  2001/03/15 16:01:11  coppedis
-Code review
-
-Revision 1.15  2001/01/26 19:56:27  hristov
-Major upgrade of AliRoot code
-
-Revision 1.14  2000/12/12 13:17:01  coppedis
-Minor corrections suggested by P. Hristov
-
-Revision 1.12  2000/12/01 08:19:01  coppedis
-Adding a message error if ZDC is constructed without DIPO
-
-Revision 1.11  2000/11/30 17:21:03  coppedis
-Introduce hit array fStHits reset only at the end of the event (for digitization)
-
-Revision 1.10  2000/11/22 11:32:58  coppedis
-Major code revision
-
-Revision 1.9  2000/10/02 21:28:20  fca
-Removal of useless dependecies via forward declarations
-
-Revision 1.8  2000/07/10 13:58:01  fca
-New version of ZDC from E.Scomparin & C.Oppedisano
-
-Revision 1.7  2000/01/19 17:17:40  fca
-
-Revision 1.6  1999/09/29 09:24:35  fca
-Introduction of the Copyright and cvs Log
-
-*/
+/* $Id$ */
 
 ///////////////////////////////////////////////////////////////////////////////
 //                                                                           //
@@ -94,89 +24,86 @@ Introduction of the Copyright and cvs Log
 //                                                                           //
 ///////////////////////////////////////////////////////////////////////////////
 
-#include <stdlib.h>
-#include <iostream.h>
-
 // --- ROOT system
 #include <TBRIK.h>
-#include <TNode.h>
+#include <TClonesArray.h>
 #include <TGeometry.h>
-#include <TFile.h>
+#include <TNode.h>
 #include <TTree.h>
-#include <TDirectory.h>
-#include <TF1.h>
+#include <TFile.h>
+#include <TSystem.h>
+#include <TRandom.h>
 
 // --- AliRoot header files
+#include "AliDetector.h"
+#include "AliRawDataHeader.h"
+#include "AliRawReader.h"
+#include "AliLoader.h"
+#include "AliRun.h"
+#include "AliMC.h"
+#include "AliLog.h"
+#include "AliDAQ.h"
 #include "AliZDC.h"
 #include "AliZDCHit.h"
-#include "AliZDCMergedHit.h"
-#include "AliZDCMerger.h"
+#include "AliZDCSDigit.h"
 #include "AliZDCDigit.h"
-#include "AliZDCReco.h"
-#include "AliDetector.h"
-#include "AliCallf77.h"
-#include "AliConst.h"
-#include "AliMC.h"
-#include "AliRun.h"
-#include "AliHeader.h"
+#include "AliZDCDigitizer.h"
+#include "AliZDCRawStream.h"
+#include "AliZDCCalibData.h"
+#include "AliFstream.h"
 
  
 ClassImp(AliZDC)
+
+AliZDC *gAliZDC;
  
 //_____________________________________________________________________________
-AliZDC::AliZDC()
+AliZDC::AliZDC() :
+  AliDetector(),
+  fNoShower  (0),
+  fCalibData (0)
+
 {
   //
   // Default constructor for the Zero Degree Calorimeter base class
   //
   
-  fIshunt     = 1;
-  fNoShower   = 0;
-  fMerger     = new AliZDCMerger();
-
-  fHits       = 0;
-  fNhits      = 0;
-
-  fDigits     = 0;
-  fNdigits    = 0;
-
-  fMergedHits = 0;
-  fTreeSD     = 0;
-  fTreeMD     = 0;
-
-  fNRecPoints = 0;
-  fRecPoints  = 0;
+  fIshunt = 1;
+  fNhits  = 0;
+  fHits = 0;
+  fDigits = 0;
+  fNdigits = 0;
   
 }
  
 //_____________________________________________________________________________
-AliZDC::AliZDC(const char *name, const char *title)
-  : AliDetector(name,title)
+AliZDC::AliZDC(const char *name, const char *title) : 
+  AliDetector(name,title),
+  fNoShower  (0),
+  fCalibData (0)
+                
 {
   //
   // Standard constructor for the Zero Degree Calorimeter base class
   //
-
-  fIshunt   = 1;
-  fNoShower = 0;
-  fMerger   = 0;
-
-  // Allocate the hits array  
-  fHits   = new TClonesArray("AliZDCHit",1000);
-  gAlice->AddHitList(fHits);
-  // Allocate the merged hits array  
-  fMergedHits = new TClonesArray("AliZDCMergedHit",1000);
-
-  // Allocate the digits array  
-  fDigits = new TClonesArray("AliZDCDigit",1000);
+    
+  fIshunt = 1;
+  fNhits  = 0;
+  fDigits = 0;
+  fNdigits = 0;
+  fHits = new TClonesArray("AliZDCHit",1000);
+  gAlice->GetMCApp()->AddHitList(fHits);
   
-  fTreeSD = 0;
-  fTreeMD = 0;
+  char sensname[5],senstitle[25];
+  sprintf(sensname,"ZDC");
+  sprintf(senstitle,"ZDC dummy");
+  SetName(sensname); SetTitle(senstitle);
 
-  fNRecPoints = 0;
-  fRecPoints = 0;  
+  gAliZDC = this;
 
 }
+
 //____________________________________________________________________________ 
 AliZDC::~AliZDC()
 {
@@ -184,21 +111,34 @@ AliZDC::~AliZDC()
   // ZDC destructor
   //
 
-  fIshunt   = 0;
-  
-  if(fMerger) delete fMerger;
+  fIshunt = 0;
+  gAliZDC = 0;
+
+  delete fCalibData;
 
-//  if(fHits){
-//    fHits->Delete();
-//    delete fHits;
-//  }
+}
 
-//  if(fDigits){
-//    fDigits->Delete();
-//    delete fDigits;
-//  }
+//_____________________________________________________________________________
+AliZDC::AliZDC(const AliZDC& ZDC) :
+  AliDetector("ZDC","ZDC")
+{
+  // copy constructor
+    fNoShower = ZDC.fNoShower;
+    fCalibData = ZDC.fCalibData;
+    fZDCCalibFName = ZDC.fZDCCalibFName;
+}
 
+//_____________________________________________________________________________
+AliZDC& AliZDC::operator=(const AliZDC& ZDC)
+{
+  // assignement operator
+  if(this!=&ZDC){
+    fNoShower = ZDC.fNoShower;
+    fCalibData = ZDC.fCalibData;
+    fZDCCalibFName = ZDC.fZDCCalibFName;
+  } return *this;
 }
+
 //_____________________________________________________________________________
 void AliZDC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
 {
@@ -218,24 +158,23 @@ void AliZDC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
   
   if(fNhits==0){
       // First hit -> setting flag for primary or secondary particle
-      Int_t primary = gAlice->GetPrimary(track);     
+      Int_t primary = gAlice->GetMCApp()->GetPrimary(track);     
       if(track != primary){
-        newquad->fSFlag = 1;  // SECONDARY particle entering the ZDC
+        newquad->SetSFlag(1);  // SECONDARY particle entering the ZDC
       }
       else if(track == primary){
-        newquad->fSFlag = 0;  // PRIMARY particle entering the ZDC
+        newquad->SetSFlag(0);  // PRIMARY particle entering the ZDC
       }  
-//      fNPrimaryHits += 1;
-      sFlag    = newquad->fSFlag;
-      primKinEn = newquad->fPrimKinEn;
-      xImpact  = newquad->fXImpact;
-      yImpact  = newquad->fYImpact;
+      sFlag    = newquad->GetSFlag();
+      primKinEn = newquad->GetPrimKinEn();
+      xImpact  = newquad->GetXImpact();
+      yImpact  = newquad->GetYImpact();
    }
    else{       
-      newquad->fPrimKinEn = primKinEn;
-      newquad->fXImpact        = xImpact;
-      newquad->fYImpact = yImpact;
-      newquad->fSFlag  = sFlag;
+      newquad->SetPrimKinEn(primKinEn);
+      newquad->SetXImpact(xImpact);
+      newquad->SetYImpact(yImpact);
+      newquad->SetSFlag(sFlag);
    }
  
   Int_t j;
@@ -244,32 +183,34 @@ void AliZDC::AddHit(Int_t track, Int_t *vol, Float_t *hits)
      curprimquad = (AliZDCHit*) lhits[j];
      if(*curprimquad == *newquad){
         *curprimquad = *curprimquad+*newquad;
+        // CH. debug
+        /*if(newquad->GetEnergy() != 0. || newquad->GetLightPMC() != 0. || 
+          newquad->GetLightPMQ() != 0.){
+         printf("\n\t --- Equal hits found\n");
+         curprimquad->Print("");
+         newquad->Print("");
+          printf("\t --- Det. %d, Quad. %d: X = %f, E = %f, LightPMC = %f, LightPMQ = %f\n",
+          curprimquad->GetVolume(0),curprimquad->GetVolume(1),curprimquad->GetXImpact(),
+          curprimquad->GetEnergy(), curprimquad->GetLightPMC(), curprimquad->GetLightPMQ());
+       }*/
+       //
        delete newquad;
        return;
      } 
   }
 
     //Otherwise create a new hit
-    new(lhits[fNhits]) AliZDCHit(newquad);
+    new(lhits[fNhits]) AliZDCHit(*newquad);
     fNhits++;
-    
+    // CH. debug
+    /*printf("\n\t New ZDC hit added! fNhits = %d\n", fNhits);
+    printf("\t Det. %d, Quad.t %d: X = %f, E = %f, LightPMC = %f, LightPMQ = %f\n",
+    newquad->GetVolume(0),newquad->GetVolume(1),newquad->GetXImpact(),
+    newquad->GetEnergy(), newquad->GetLightPMC(), newquad->GetLightPMQ());
+    */
     delete newquad;
 }
 
-//_____________________________________________________________________________
-void  AliZDC::AddDigit(Int_t *sect, Int_t digit)
-{
-//
-  AliZDCDigit *newdigit;
-  newdigit = new AliZDCDigit(sect, digit);
-  
-//  printf("\n AddDigit -> sector[0] = %d, sector[1] = %d, digit = %d",
-//         sect[0], sect[1], digit);
-  new((*fDigits)[fNdigits]) AliZDCDigit(*newdigit);
-  fNdigits++;
-  delete newdigit;
-}
-      
 //_____________________________________________________________________________
 void AliZDC::BuildGeometry()
 {
@@ -294,33 +235,23 @@ void AliZDC::BuildGeometry()
     fNodes->Add(node);
 }
 
-//_____________________________________________________________________________
-Int_t AliZDC::DistancetoPrimitive(Int_t , Int_t )
-{
-  //
-  // Distance from the mouse to the Zero Degree Calorimeter
-  // Dummy routine
-  //
-  return 9999;
-}
-
 //____________________________________________________________________________
 Float_t AliZDC::ZMin(void) const
 {
   // Minimum dimension of the ZDC module in z
-  return 11600.;
+  return -11600.;
 }
 
 //____________________________________________________________________________
 Float_t AliZDC::ZMax(void) const
 {
   // Maximum dimension of the ZDC module in z
-  return  11750.;
+  return -11750.;
 }
   
 
 //_____________________________________________________________________________
- void AliZDC::MakeBranch(Option_t *opt, const char *file)
+void AliZDC::MakeBranch(Option_t *opt)
 {
   //
   // Create Tree branches for the ZDC
@@ -328,473 +259,535 @@ Float_t AliZDC::ZMax(void) const
 
   char branchname[10];
   sprintf(branchname,"%s",GetName());
-  
-  AliDetector::MakeBranch(opt);
-
-  const char *cS = strstr(opt,"S");
-
-  if (gAlice->TreeS() && cS) {
-    if(fMergedHits!=0) fMergedHits->Clear();
-    else fMergedHits = new TClonesArray ("AliZDCMergedHit",1000);
-    MakeBranchInTree(gAlice->TreeS(), 
-                     branchname, &fMergedHits, fBufferSize, file) ;
-    printf("* AliZDC::MakeBranch    * Making Branch %s for SDigits\n\n",branchname);
-  }
-
-    
-  const char *cD = strstr(opt,"D");
-
-  if (gAlice->TreeD() && cD) {
-    if(fDigits!=0) fDigits->Clear();
-    else fDigits = new TClonesArray ("AliZDCDigit",1000);
-    MakeBranchInTree(gAlice->TreeD(), 
-                     branchname, &fDigits, fBufferSize, file) ;
-    printf("* AliZDC::MakeBranch    * Making Branch %s for Digits\n\n",branchname);
-  }
 
+  const char *cH = strstr(opt,"H");
   
-  const char *cR = strstr(opt,"R");
-
-  if (gAlice->TreeR() && cR) {
-    MakeBranchInTree(gAlice->TreeR(), 
-                    branchname, &fRecPoints, fBufferSize, file) ;
-    printf("* AliZDC::MakeBranch    * Making Branch %s for RecPoints\n\n",branchname);   }
-          
-}
-
-//_____________________________________________________________________________
- void AliZDC::MakeBranchInTreeSD(TTree *treeSD, const char *file)
-{
-  // MakeBranchInTree
-  const Int_t kBufferSize = 4000;
-  char  branchname[20];
-  sprintf(branchname,"%s",GetName());
-  MakeBranchInTree(treeSD, branchname, &fMergedHits, kBufferSize, file) ;
-  printf("* AliZDC::MakeBranch    * Making Branch %s for SDigits\n\n",branchname);
-
-}
-//_____________________________________________________________________________
- void AliZDC::MakeBranchInTreeD(TTree *treeD, const char *file)
-{
-  // MakeBranchInTree
-  const Int_t kBufferSize = 4000;
-  char  branchname[20];
-  sprintf(branchname,"%s",GetName());
-  MakeBranchInTree(treeD, branchname, &fDigits, kBufferSize, file) ;
-  printf("* AliZDC::MakeBranch    * Making Branch %s for Digits\n\n",branchname);
-
+  if(cH && fLoader->TreeH())
+   fHits   = new TClonesArray("AliZDCHit",1000); 
+  
+  AliDetector::MakeBranch(opt);
 }
-//_____________________________________________________________________________
- void AliZDC::MakeBranchInTreeR(TTree *treeR, const char *file)
-{
-  // MakeBranchInTree
-  const Int_t kBufferSize = 4000;
-  char  branchname[20];
-  sprintf(branchname,"%s",GetName());
-  MakeBranchInTree(treeR, branchname, &fRecPoints, kBufferSize, file) ;
-  printf("* AliZDC::MakeBranch    * Making Branch %s for RecPoints\n\n",branchname);
 
-}
 //_____________________________________________________________________________
 void AliZDC::Hits2SDigits()
 {
-  printf("\n   Entering AliZDC::SDigits2Digits() ");
+  // Create summable digits from hits
   
-  //----------------------------------------------------------------
-  if(!fMerger){ 
-    printf("   ZDC digitization (without merging)\n");
-
-    AliZDCMergedHit *MHit;
-    Int_t j, sector[2];
-    Float_t MHits[7];
-    fNMergedhits = 0;
-
-    TTree *treeH = gAlice->TreeH();
-    Int_t ntracks = (Int_t) treeH->GetEntries();
-    gAlice->ResetHits();
+  AliDebug(1,"\n       Entering AliZDC::Hits2Digits() ");
   
+  fLoader->LoadHits("read");
+  fLoader->LoadSDigits("recreate");
+  AliRunLoader* runLoader = fLoader->GetRunLoader();
+  AliZDCSDigit sdigit;
+  AliZDCSDigit* psdigit = &sdigit;
+
+  // Event loop
+  for(Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); iEvent++) {
+    Float_t pmCZN = 0, pmCZP = 0, pmQZN[4], pmQZP[4], pmZEM1 = 0, pmZEM2 = 0;
+    for(Int_t i = 0; i < 4; i++) pmQZN[i] = pmQZP[i] = 0;
+
+    runLoader->GetEvent(iEvent);
+    TTree* treeH = fLoader->TreeH();
+    Int_t ntracks = (Int_t) treeH->GetEntries();
+    ResetHits();
+
     // Tracks loop
-    for(Int_t itrack=0; itrack<ntracks; itrack++){
-       treeH->GetEvent(itrack);
-       for(AliZDCHit* zdcHit=(AliZDCHit*)this->FirstHit(-1); zdcHit;
-                      zdcHit = (AliZDCHit*)this->NextHit()){ 
+    Int_t sector[2];
+    for(Int_t itrack = 0; itrack < ntracks; itrack++) {
+      treeH->GetEntry(itrack);
+      for(AliZDCHit* zdcHit = (AliZDCHit*)FirstHit(-1); zdcHit;
+                      zdcHit = (AliZDCHit*)NextHit()) { 
                      
-          for(j=0; j<2; j++) sector[j] = zdcHit->GetVolume(j);
-          MHits[0] = zdcHit->GetPrimKinEn();
-          MHits[1] = zdcHit->GetXImpact();
-          MHits[2] = zdcHit->GetYImpact();
-          MHits[3] = zdcHit->GetSFlag();
-          MHits[4] = zdcHit->GetLightPMQ();
-          MHits[5] = zdcHit->GetLightPMC();
-          MHits[6] = zdcHit->GetEnergy();
-       }//Hits loop
-       
-         MHit = new AliZDCMergedHit(sector, MHits);
-         new((*fMergedHits)[fNMergedhits]) AliZDCMergedHit(*MHit);       
-         TClonesArray &sdigits = *fMergedHits;
-         new (sdigits[fNMergedhits]) AliZDCMergedHit(*MHit);
-         fNMergedhits++;
-         delete MHit;
-    }
-//    printf("\n       ### Filling SDigits tree\n");
-    gAlice->TreeS()->Fill();
-    gAlice->TreeS()->Write(0,TObject::kOverwrite);  
-    gAlice->TreeS()->Reset();  
-  }
-  //----------------------------------------------------------------
-  else if(fMerger){
-    printf("   ZDC merging and digitization\n");
-    // ### Initialise merging
-    fMerger -> InitMerging();
-
-    TFile *bgrFile = fMerger->BgrFile();
-    bgrFile->cd();
-
-    // SDigits tree
-    Int_t fNEvBgr = fMerger->EvNum();
-    char treeSDBgrName[20];
-    sprintf(treeSDBgrName,"TreeS%d",fNEvBgr);
-    fTreeSD = (TTree*)gDirectory->Get(treeSDBgrName); // TreeH
-    if(!fTreeSD){
-      printf("\n ERROR -> Can't find TreeS%d in background file\n",fNEvBgr);
-    }   
-    // Branch address
-    char branchSDname[20];
-    sprintf(branchSDname,"%s",GetName());
-    if(fTreeSD && fMergedHits){
-      TBranch *branchSD = fTreeSD->GetBranch(branchSDname);
-      if(branchSD) branchSD->SetAddress(&fMergedHits);
-      else if(!branchSD) MakeBranchInTreeSD(fTreeSD);
+       sector[0] = zdcHit->GetVolume(0);
+       sector[1] = zdcHit->GetVolume(1);
+       if((sector[1] < 1) || (sector[1] > 4)) {
+         Error("Hits2SDigits", "sector[0] = %d, sector[1] = %d", 
+               sector[0], sector[1]);
+         continue;
+       }
+       Float_t lightQ = zdcHit->GetLightPMQ();
+       Float_t lightC = zdcHit->GetLightPMC();
+     
+       if(sector[0] == 1) {          //ZN 
+         pmCZN += lightC;
+         pmQZN[sector[1]-1] += lightQ;
+       } else if(sector[0] == 2) {   //ZP 
+         pmCZP += lightC;
+         pmQZP[sector[1]-1] += lightQ;
+       } else if(sector[0] == 3) {   //ZEM 
+         if(sector[1] == 1) pmZEM1 += lightC;
+         else                pmZEM2 += lightQ;
+       }
+      }//Hits loop
     }
 
-    // ### Get TCA of MergedHits from AliZDCMerger
-    fMergedHits  = fMerger->MergedHits();
-    fNMergedhits = fMerger->GetNMhits();
-//    printf("\n         fNMergedhits (from AliZDCMerger) = %d\n", fNMergedhits);   
-    AliZDCMergedHit *MHit;
-    TClonesArray &sdigits = *fMergedHits;
-    Int_t imhit;
-    //Merged Hits loop
-    for(imhit=0; imhit<fNMergedhits; imhit++){
-       MHit = (AliZDCMergedHit*) fMergedHits->UncheckedAt(imhit);
-       new (sdigits[imhit]) AliZDCMergedHit(*MHit);
+    // create the output tree
+    fLoader->MakeTree("S");
+    TTree* treeS = fLoader->TreeS();
+    const Int_t kBufferSize = 4000;
+    treeS->Branch(GetName(), "AliZDCSDigit", &psdigit, kBufferSize);
+
+    // Create sdigits for ZN1
+    sector[0] = 1; // Detector = ZN1
+    sector[1] = 0; // Common PM ADC
+    new(psdigit) AliZDCSDigit(sector, pmCZN);
+    if(pmCZN > 0) treeS->Fill();
+    for(Int_t j = 0; j < 4; j++) {
+      sector[1] = j+1; // Towers PM ADCs
+      new(psdigit) AliZDCSDigit(sector, pmQZN[j]);
+      if(pmQZN[j] > 0) treeS->Fill();
     }
-
-//    printf("\n ### Filling SDigits tree\n");
-    bgrFile->cd();
-    fTreeSD->Fill();
-    fTreeSD->Write(0,TObject::kOverwrite);
-  }
   
-}
-
-//_____________________________________________________________________________
-void AliZDC::SDigits2Digits()
-{
-  //printf("\n Entering AliZDC::SDigits2Digits() ");
-  if(!fMerger){ // Only digitization
-    printf("   ZDC digitization (without merging) \n");
-    fMerger = new AliZDCMerger();    
-    fMerger->Digitize(fNMergedhits, fMergedHits);
-
-    char hname[30];
-    sprintf(hname,"TreeD%d",gAlice->GetHeader()->GetEvent());
-    gAlice->TreeD()->Fill();
-    gAlice->TreeD()->Write(0,TObject::kOverwrite);
-    gAlice->TreeD()->Reset();  
-  }
-  else if(fMerger){    // Merging and digitization
-    printf("   ZDC merging and digitization\n");
-    fMerger->Digitize(fNMergedhits, fMergedHits);
-
-    TFile *bgrFile = fMerger->BgrFile();
-    bgrFile->cd();
-    // Digits tree
-    Int_t fNEvBgr = fMerger->EvNum();
-    //printf(" fNEvBgr = %d\n",fNEvBgr);
-    char treeDBgrName[20];
-    sprintf(treeDBgrName,"TreeD%d",fNEvBgr);
-    fTreeMD = (TTree*)gDirectory->Get(treeDBgrName); // TreeH
-    if(!fTreeMD){
-      printf("\n ERROR -> Can't find TreeD%d in background file\n",fNEvBgr);
-    }   
-    // Branch address
-    char branchDname[20];
-    sprintf(branchDname,"%s",GetName());
-    if(fTreeMD && fDigits){
-//      printf("\n     fTreeMD!=0 && fDigits!=0\n");
-      TBranch *branchD = fTreeMD->GetBranch(branchDname);
-      if(branchD) branchD->SetAddress(&fDigits);
-      else if(!branchD) MakeBranchInTreeD(fTreeMD);
+    // Create sdigits for ZP1
+    sector[0] = 2; // Detector = ZP1
+    sector[1] = 0; // Common PM ADC
+    new(psdigit) AliZDCSDigit(sector, pmCZP);
+    if(pmCZP > 0) treeS->Fill();
+    for(Int_t j = 0; j < 4; j++) {
+      sector[1] = j+1; // Towers PM ADCs
+      new(psdigit) AliZDCSDigit(sector, pmQZP[j]);
+      if(pmQZP[j] > 0) treeS->Fill();
     }
-    
-    fTreeMD->Fill();
-    fTreeMD->Write(0,TObject::kOverwrite);
+
+    // Create sdigits for ZEM
+    sector[0] = 3; 
+    sector[1] = 1; // Detector = ZEM1
+    new(psdigit) AliZDCSDigit(sector, pmZEM1);
+    if(pmZEM1 > 0) treeS->Fill();
+    sector[1] = 2; // Detector = ZEM2
+    new(psdigit) AliZDCSDigit(sector, pmZEM2);
+    if(pmZEM2 > 0) treeS->Fill();
+
+    // write the output tree
+    fLoader->WriteSDigits("OVERWRITE");
   }
-  
-  
+
+  fLoader->UnloadHits();
+  fLoader->UnloadSDigits();
 }
+
 //_____________________________________________________________________________
-void AliZDC::Hits2Digits()
+AliDigitizer* AliZDC::CreateDigitizer(AliRunDigitizer* manager) const
 {
-    gAlice->Hits2SDigits();
-    gAlice->SDigits2Digits();
+  // Create the digitizer for ZDC
+
+  return new AliZDCDigitizer(manager);
 }
 
 //_____________________________________________________________________________
-void AliZDC::Digits2Reco()
+void AliZDC::Digits2Raw()
 {
-  //printf("\n Entering AliZDC::Digits2Reco() ");
+  // Convert ZDC digits to raw data
+
+  // Format: 22 interger values -> ZN1 (C+Q1-4), ZP1 (C+Q1-4), ZEM1, 2, ZN (C+Q1-4), ZP2 (C+Q1-4))
+  //        + 22 interger values for the out of time channels
+  // For the CAEN module V965 we have an Header, the Data Words and an End Of Block
+  //   12 channels x 2 gain chains read from 1st ADC module
+  //   10 channels x 2 gain chains read from 2nd ADC module
+  //   12 channels x 2 gain chains read from 3rd ADC module (o.o.t.)
+  //   10 channels x 2 gain chains read from 4rth ADC module (o.o.t.)
+  //
+  const int knADCData1=24, knADCData2=20; 
+  UInt_t lADCHeader1; 
+  UInt_t lADCData1[knADCData1];
+  //
+  UInt_t lADCHeader2; 
+  UInt_t lADCData2[knADCData2];
+  //
+  UInt_t lADCHeader3; 
+  UInt_t lADCData3[knADCData1];
+  //
+  UInt_t lADCHeader4; 
+  UInt_t lADCData4[knADCData2];
+  //
+  UInt_t lADCEndBlock;
+
+  // load the digits
+  fLoader->LoadDigits("read");
+  AliZDCDigit digit;
+  AliZDCDigit* pdigit = &digit;
+  TTree* treeD = fLoader->TreeD();
+  if(!treeD) return;
+  treeD->SetBranchAddress("ZDC", &pdigit);
+  //printf("\t AliZDC::Digits2raw -> TreeD has %d entries\n",(Int_t) treeD->GetEntries());
+
+  // Fill data array
+  // ADC header
+  UInt_t lADCHeaderGEO = 0;
+  UInt_t lADCHeaderCRATE = 0;
+  UInt_t lADCHeaderCNT1 = knADCData1;
+  UInt_t lADCHeaderCNT2 = knADCData2;
     
-    Int_t fNEvBgr = fMerger->EvNum();
-    //printf(" fNEvBgr = %d\n",fNEvBgr);
-    gAlice->GetEvent(fNEvBgr);
-
-    AliDetector *ZDC  = gAlice->GetDetector("ZDC");
-    TClonesArray *ZDCdigits = ZDC->Digits();
+  lADCHeader1 = lADCHeaderGEO << 27 | 0x1 << 25 | lADCHeaderCRATE << 16 |
+               lADCHeaderCNT1 << 8 ;
+  //          
+  lADCHeader2 = lADCHeaderGEO << 27 | 0x1 << 25 | lADCHeaderCRATE << 16 |
+               lADCHeaderCNT2 << 8 ;
+  //          
+  lADCHeader3 = lADCHeaderGEO << 27 | 0x1 << 25 | lADCHeaderCRATE << 16 |
+               lADCHeaderCNT1 << 8 ;
+  //          
+  lADCHeader4 = lADCHeaderGEO << 27 | 0x1 << 25 | lADCHeaderCRATE << 16 |
+               lADCHeaderCNT2 << 8 ;
+  //printf("\t lADCHeader1 = %x, lADCHeader2 = %x\n",lADCHeader1, lADCHeader2);
+      
+  // ADC data word
+  UInt_t lADCDataGEO = lADCHeaderGEO;
+  UInt_t lADCDataValue1[knADCData1];
+  UInt_t lADCDataValue2[knADCData2];
+  UInt_t lADCDataValue3[knADCData1];
+  UInt_t lADCDataValue4[knADCData2];
+  UInt_t lADCDataOvFlw1[knADCData1];
+  UInt_t lADCDataOvFlw2[knADCData2];
+  UInt_t lADCDataOvFlw3[knADCData1];
+  UInt_t lADCDataOvFlw4[knADCData2];
+  for(Int_t i=0; i<knADCData1 ; i++){
+    lADCDataValue1[i] = 0;
+    lADCDataOvFlw1[i] = 0;
+    lADCDataValue3[i] = 0;
+    lADCDataOvFlw3[i] = 0;
+  }
+  for(Int_t i=0; i<knADCData2 ; i++){
+    lADCDataValue2[i] = 0;
+    lADCDataOvFlw2[i] = 0;
+    lADCDataValue4[i] = 0;
+    lADCDataOvFlw4[i] = 0;
+  }
+  UInt_t lADCDataChannel = 0;
+  
+  // loop over digits
+  for(Int_t iDigit=0; iDigit<treeD->GetEntries(); iDigit++){
+    treeD->GetEntry(iDigit);
+    if(!pdigit) continue;
+    //digit.Print("");
     
-    char tdname[20];
-    sprintf(tdname,"TreeD%d",fNEvBgr);
-    TTree *TD = (TTree*)gDirectory->Get(tdname);
-    //TTree *TD = gAlice->TreeD();
-    if(TD){
-      //printf("       TreeD found in gAlice object\n");
-      char brname[20];
-      sprintf(brname,"%s",ZDC->GetName());
-      TBranch *br = TD->GetBranch(brname);
-      if(br) br->SetAddress(&ZDCdigits);
-    }
-    else if(!TD) printf("      ERROR -> TreeD NOT found in gAlice object\n");
+    // *** ADC data
+    Int_t index1=0, index2=0;
+    // *** ADC1 (ZN1, ZP1, ZEM1,2) o ADC3 (ZN1, ZP1, ZEM1,2 o.o.t.)
+    if(digit.GetSector(0)==1 || digit.GetSector(0)==2 || digit.GetSector(0)==3){
+      if(digit.GetSector(0)==1 || digit.GetSector(0)==2){
+        index1 = (digit.GetSector(0)-1) + digit.GetSector(1)*4; // ZN1 or ZP1
+        lADCDataChannel = (digit.GetSector(0)-1)*8 + digit.GetSector(1);
+      }
+      else if(digit.GetSector(0)==3){ // ZEM 1,2
+        index1 = 20 + (digit.GetSector(1)-1);
+        lADCDataChannel = 5 + (digit.GetSector(1)-1)*8;
+      }
+      //
+      /*printf("\t AliZDC::Digits2raw -> det %d, quad %d, index = %d, ADCch = %d\n",
+               digit.GetSector(0),digit.GetSector(1),index1,lADCDataChannel);// Ch. debug
+      */
+      //
+      if(iDigit<22){
+        lADCDataValue1[index1] = digit.GetADCValue(0);         // High gain ADC ch.    
+        if(lADCDataValue1[index1] > 2047) lADCDataOvFlw1[index1] = 1; 
+        lADCDataValue1[index1+2] = digit.GetADCValue(1); // Low gain ADC ch.
+        if(lADCDataValue1[index1+2] > 2047) lADCDataOvFlw1[index1+2] = 1; 
     
-    Int_t nt = (Int_t) (TD->GetEntries());
-    //printf("\n               #entries in TreeD = %d\n",nt);
-    gAlice->ResetDigits();    
+        lADCData1[index1] = lADCDataGEO << 27 | lADCDataChannel << 17 | 
+                        lADCDataOvFlw1[index1] << 12 | (lADCDataValue1[index1] & 0xfff); 
+        lADCData1[index1+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
+                        lADCDataOvFlw1[index1+2] << 12 | (lADCDataValue1[index1+2] & 0xfff);  
+      }
+      else{
+        lADCDataValue3[index1] = digit.GetADCValue(0);         // High gain ADC ch.    
+        if(lADCDataValue3[index1] > 2047) lADCDataOvFlw3[index1] = 1; 
+        lADCDataValue3[index1+2] = digit.GetADCValue(1); // Low gain ADC ch.
+        if(lADCDataValue3[index1+2] > 2047) lADCDataOvFlw3[index1+2] = 1; 
     
-    AliZDCDigit *dig;
-    Int_t j, idig, ndigits, ZNraw=0, ZPraw=0, ZEMraw=0;
-    // ---      Summing raw ADCs for each detector to obtain total light
-    for(j=0; j<nt; j++){
-      TD->GetEvent(j);
-      ndigits = ZDCdigits->GetEntries();
-      //printf("\n Entry #%d, ndigits = %d",j,ndigits);
-      ZNraw=0;
-      ZPraw=0; 
-      ZEMraw=0;
-      //  ---  Loop over event digits
-      for(idig=0; idig<ndigits; idig++){
-         dig = (AliZDCDigit*) ZDCdigits->UncheckedAt(idig);
-         if(dig->GetSector(0) == 1)     ZNraw  += dig->GetADCValue();
-         else if(dig->GetSector(0) == 2) ZPraw  += dig->GetADCValue();
-         else if(dig->GetSector(0) == 3) ZEMraw += dig->GetADCValue();
-      } // Digits loop
-    } //  TreeD entries loop
-    printf("\n ---     ZNraw = %d, ZPraw = %d, ZEMraw = %d\n",ZNraw, ZPraw, ZEMraw);
+        lADCData3[index1] = lADCDataGEO << 27 | lADCDataChannel << 17 | 
+                        lADCDataOvFlw3[index1] << 12 | (lADCDataValue3[index1] & 0xfff); 
+        lADCData3[index1+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
+                        lADCDataOvFlw3[index1+2] << 12 | (lADCDataValue3[index1+2] & 0xfff);  
+      }                  
+    }
+    // *** ADC2 (ZN2, ZP2) o ADC4 (ZN2, ZP2 o.o.t.)
+    else if(digit.GetSector(0)==4 || digit.GetSector(0)==5){
+      index2 = (digit.GetSector(0)-4) + digit.GetSector(1)*4; // ZN2 or ZP2
+      lADCDataChannel = (digit.GetSector(0)-4)*8 + digit.GetSector(1);
+      //
+      /*printf("\t AliZDC::Digits2raw -> det %d, quad %d, index = %d, ADCch = %d\n",
+               digit.GetSector(0),digit.GetSector(1),index1,lADCDataChannel); // Ch. debug
+      */
+      //
+      if(iDigit<22){
+        lADCDataValue2[index2] = digit.GetADCValue(0);
+        if(lADCDataValue2[index2] > 2047) lADCDataOvFlw2[index2] = 1; 
+        lADCDataValue2[index2+2] = digit.GetADCValue(1);
+        if(lADCDataValue2[index2+2] > 2047) lADCDataOvFlw2[index2+2] = 1; 
+        //
+        lADCData2[index2] =   lADCDataGEO << 27 | lADCDataChannel << 17 | 
+                        lADCDataOvFlw2[index2] << 12 | (lADCDataValue2[index2] & 0xfff); 
+        lADCData2[index2+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
+                        lADCDataOvFlw2[index2+2] << 12 | (lADCDataValue2[index2+2] & 0xfff);   
+      }                 
+      else{
+        lADCDataValue4[index2] = digit.GetADCValue(0);
+        if(lADCDataValue4[index2] > 2047) lADCDataOvFlw4[index2] = 1; 
+        lADCDataValue4[index2+2] = digit.GetADCValue(1);
+        if(lADCDataValue4[index2+2] > 2047) lADCDataOvFlw4[index2+2] = 1; 
+        //
+        lADCData4[index2] =   lADCDataGEO << 27 | lADCDataChannel << 17 | 
+                        lADCDataOvFlw4[index2] << 12 | (lADCDataValue4[index2] & 0xfff); 
+        lADCData4[index2+2] = lADCDataGEO << 27 | lADCDataChannel << 17 | 0x1 << 16 |
+                        lADCDataOvFlw4[index2+2] << 12 | (lADCDataValue4[index2+2] & 0xfff);   
+      }                 
+    } 
+    if((index1<0) || (index1>23)) {
+      Error("Digits2Raw", "sector[0] = %d, sector[1] = %d", 
+           digit.GetSector(0), digit.GetSector(1));
+      continue;
+    }
+    if((index2<0) || (index2>19)) {
+      Error("Digits2Raw", "sector[0] = %d, sector[1] = %d", 
+           digit.GetSector(0), digit.GetSector(1));
+      continue;
+    }
     
-  //  ---      Pedestal subtraction
-  Int_t ZNcorr, ZPcorr, ZEMcorr, MeanPed=50;
-  ZNcorr  = ZNraw  - 5*MeanPed;
-  ZPcorr  = ZPraw  - 5*MeanPed;
-  ZEMcorr = ZEMraw - 2*MeanPed;
-  if(ZNcorr<0)  ZNcorr=0;
-  if(ZPcorr<0)  ZPcorr=0;
-  if(ZEMcorr<0) ZEMcorr=0;
- printf("\n    ZNcorr = %d, ZPcorr = %d, ZEMcorr = %d\n",ZNcorr,ZPcorr,ZEMcorr);
-  
-  //  ---      ADCchannel -> photoelectrons
-  // NB-> PM gain = 10^(5), ADC resolution = 6.4*10^(-7)
-  Float_t ZNphe, ZPphe, ZEMphe, ConvFactor = 0.064;
-  ZNphe  = ZNcorr/ConvFactor;
-  ZPphe  = ZPcorr/ConvFactor;
-  ZEMphe = ZEMcorr/ConvFactor;
-  printf("\n    ZNphe = %f, ZPphe = %f, ZEMphe = %f\n",ZNphe, ZPphe, ZEMphe);
-  
-  //  ---      Energy calibration
-  // Conversion factors for hadronic ZDCs goes from phe yield to TRUE incident 
-  //  energy (conversion from GeV to TeV is included); while for EM calos 
-  // conversion is from light yield to detected energy calculated by GEANT
-  // NB -> ZN and ZP conversion factors are constant since incident spectators
-  // have all the same energy, ZEM energy is obtained through a fit over the whole
-  // range of incident particle energies (obtained with full HIJING simulations) 
-  Float_t ZNenergy, ZPenergy, ZEMenergy, ZDCenergy;
-  Float_t ZNphexTeV=329., ZPphexTeV=369.;
-  ZNenergy  = ZNphe/ZNphexTeV;
-  ZPenergy  = ZPphe/ZPphexTeV;
-  ZDCenergy = ZNenergy+ZPenergy;
-  ZEMenergy = -4.81+0.3238*ZEMphe;
-  if(ZEMenergy<0) ZEMenergy=0;
-  printf("    ZNenergy = %f TeV, ZPenergy = %f TeV, ZDCenergy = %f GeV, "
-         "\n           ZEMenergy = %f TeV\n", ZNenergy, ZPenergy, 
-        ZDCenergy, ZEMenergy);
-  
-  if(ZDCenergy==0)
-    printf("\n\n       ###     ATTENZIONE!!! -> ev# %d: ZNenergy = %f TeV, ZPenergy = %f TeV, ZDCenergy = %f GeV, "
-         " ZEMenergy = %f TeV\n\n", fNEvBgr, ZNenergy, ZPenergy, ZDCenergy, ZEMenergy); 
+    
+  }
+  /*  for(Int_t i=0;i<24;i++) printf("\t ADCData1[%d] = %x\n",i,lADCData1[i]);
+  for(Int_t i=0;i<20;i++) printf("\t ADCData2[%d] = %x\n",i,lADCData2[i]);
+  for(Int_t i=0;i<24;i++) printf("\t ADCData3[%d] = %x\n",i,lADCData3[i]);
+  for(Int_t i=0;i<20;i++) printf("\t ADCData4[%d] = %x\n",i,lADCData4[i]);
+  */
+  // End of Block
+  UInt_t lADCEndBlockGEO = lADCHeaderGEO;
+  UInt_t lADCEndBlockEvCount = gAlice->GetEventNrInRun();
   
-  //  ---      Number of incident spectator nucleons
-  Int_t NDetSpecN, NDetSpecP;
-  NDetSpecN = (Int_t) (ZNenergy/2.760);
-  NDetSpecP = (Int_t) (ZPenergy/2.760);
-  printf("\n    NDetSpecN = %d, NDetSpecP = %d\n",NDetSpecN, NDetSpecP);
+  lADCEndBlock = lADCEndBlockGEO << 27 | 0x1 << 26 | lADCEndBlockEvCount;
   
-  //  ---      Number of generated spectator nucleons and impact parameter
-  // --------------------------------------------------------------------------------------------------
-  // [1] ### Results in Chiara's PhD thesis -> 0<b<15 fm (Dec 2001)
-  /*// Fit results for neutrons (Nspectator n true vs. EZN)
-  TF1 *fZNCen = new TF1("fZNCen",
-      "(-2.116909+sqrt(2.116909*2.116909-4*(-0.00651)*(14.556798-x)))/(2*(-0.00651))",0.,158.5);
-  TF1 *fZNPer = new TF1("fZNPer",
-      "(-34.695134-sqrt(34.695134*34.695134-4*(-0.174780)*(-1562.283443-x)))/(2*(-0.174780))",0.,158.5);
-  // Fit results for protons (Nspectator p true vs. EZP)
-  TF1 *fZPCen = new TF1("fZPCen",
-      "(-1.3217+sqrt(1.3217*1.3217-4*(-0.007934)*(4.742873-x)))/(2*(-0.007934))",0.,58.91);
-  TF1 *fZPPer = new TF1("fZPPer",
-      "(-15.788267-sqrt(15.788267*15.788267-4*(-0.133359)*(-383.800673-x)))/(2*(-0.133359))",0.,58.91);
-  // Fit results for total number of spectators (Nspectators true vs. EZDC)
-  TF1 *fZDCCen = new TF1("fZDCCen",
-      "(-1.867335+sqrt(1.867335*1.867335-4*(-0.004119)*(19.100289-x)))/(2*(-0.004119))",0.,220.4);
-  TF1 *fZDCPer = new TF1("fZDCPer",
-      "(-22.429097-sqrt(22.429097*22.429097-4*(-0.072435)*(-1482.034526-x)))/(2*(-0.072435))",0.,220.4);*/
-  // --------------------------------------------------------------------------------------------------
-  // [1] ### Results from a new production  -> 0<b<18 fm (Apr 2002)
-  // Fit results for neutrons (Nspectator n true vs. EZN)
-  TF1 *fZNCen = new TF1("fZNCen",
-      "(-2.287920+sqrt(2.287920*2.287920-4*(-0.007629)*(11.921710-x)))/(2*(-0.007629))",0.,164.);
-  TF1 *fZNPer = new TF1("fZNPer",
-      "(-37.812280-sqrt(37.812280*37.812280-4*(-0.190932)*(-1709.249672-x)))/(2*(-0.190932))",0.,164.);
-  // Fit results for protons (Nspectator p true vs. EZP)
-  TF1 *fZPCen = new TF1("fZPCen",
-       "(-1.321353+sqrt(1.321353*1.321353-4*(-0.007283)*(3.550697-x)))/(2*(-0.007283))",0.,60.);
-  TF1 *fZPPer = new TF1("fZPPer",
-      "(-42.643308-sqrt(42.643308*42.643308-4*(-0.310786)*(-1402.945615-x)))/(2*(-0.310786))",0.,60.);
-  // Fit results for total number of spectators (Nspectators true vs. EZDC)
-  TF1 *fZDCCen = new TF1("fZDCCen",
-      "(-1.934991+sqrt(1.934991*1.934991-4*(-0.004080)*(15.111124-x)))/(2*(-0.004080))",0.,225.);
-  TF1 *fZDCPer = new TF1("fZDCPer",
-      "(-34.380639-sqrt(34.380639*34.380639-4*(-0.104251)*(-2612.189017-x)))/(2*(-0.104251))",0.,225.);
-  // --------------------------------------------------------------------------------------------------
-  // [1] ### Results in Chiara's PhD thesis -> 0<b<15 fm (Dec 2001)
-  /*// Fit results for b (b vs. EZDC)
-  //TF1 *fbCen = new TF1("fbCen","0.611543+0.052231*x-0.000112*x*x+0.000000374*x*x*x",0.,222.);
-  //TF1 *fbPer = new TF1("fbPer","16.552010-0.023866*x-0.00001*x*x",0.,222.);
-  TF1 *fbCen = new TF1("fbCen","0.612769+0.051929*x-0.0001074*x*x+0.0000003724*x*x*x",0.,225.);
-  TF1 *fbPer = new TF1("fbPer","16.6131016-0.026053*x+0.000006893*x*x",0.,225.);*/
-  // --------------------------------------------------------------------------------------------------
-  // [2] ### Results from a new production  -> 0<b<18 fm (Apr 2002)
-  TF1 *fbCen = new TF1("fbCen","-0.056923+0.079703*x-0.0004301*x*x+0.000001366*x*x*x",0.,220.);
-  TF1 *fbPer = new TF1("fbPer","17.943998-0.046846*x+0.000074*x*x",0.,220.);
-  // --------------------------------------------------------------------------------------------------
-  // Evaluating Nspectators and b from ZEM energy
-  // [1] ### Results in Chiara's PhD thesis -> 0<b<15 fm (Dec 2001)
-  /*TF1 *fZEMn  = new TF1("fZEMn","124.2-0.0566*x+0.000006014*x*x",0.,3500.);
-  TF1 *fZEMp  = new TF1("fZEMp","81.3-0.03834*x+0.000004359*x*x",0.,3500.);
-  TF1 *fZEMsp = new TF1("fZEMsp","205.6-0.09567*x+0.00001056*x*x",0.,3500.);
-  TF1 *fZEMb  = new TF1("fZEMb","15.8-0.02084*x+2.802e-5*x*x-2.007e-8*x*x*x+6.586e-12*x*x*x*x-8.042e-16*x*x*x*x*x",0.,3500.);*/
-  // --------------------------------------------------------------------------------------------------
-  // [2] ### Results from a new production  -> 0<b<18 fm (Apr 2002)
-  TF1 *fZEMn  = new TF1("fZEMn","126.2-0.05399*x+0.000005679*x*x",0.,4000.);
-  TF1 *fZEMp  = new TF1("fZEMp","82.49-0.03611*x+0.00000385*x*x",0.,4000.);
-  TF1 *fZEMsp = new TF1("fZEMsp","208.7-0.09006*x+0.000009526*x*x",0.,4000.);
-  TF1 *fZEMb  = new TF1("fZEMb","16.06-0.01633*x+1.44e-5*x*x-6.778e-9*x*x*x+1.438e-12*x*x*x*x-1.112e-16*x*x*x*x*x",0.,4000.);
+  //printf("\t ADCEndBlock = %d\n",lADCEndBlock);
+
+
+  // open the output file
+  char fileName[30];
+  strcpy(fileName,AliDAQ::DdlFileName("ZDC",0));
+
+  AliFstream* file = new AliFstream(fileName);
+
+  // write the DDL data header
+  AliRawDataHeader header;
+  header.fSize = sizeof(header) + 
+                 sizeof(lADCHeader1) + sizeof(lADCData1) + sizeof(lADCEndBlock) +
+                sizeof(lADCHeader2) + sizeof(lADCData2) + sizeof(lADCEndBlock) +
+                 sizeof(lADCHeader3) + sizeof(lADCData3) + sizeof(lADCEndBlock) +
+                sizeof(lADCHeader4) + sizeof(lADCData4) + sizeof(lADCEndBlock);
+  /*printf("sizeof header = %d, ADCHeader1 = %d, ADCData1 = %d, ADCEndBlock = %d\n",
+          sizeof(header),sizeof(lADCHeader1),sizeof(lADCData1),sizeof(lADCEndBlock));
+  printf("sizeof header = %d, ADCHeader2 = %d, ADCData2 = %d, ADCEndBlock = %d\n",
+          sizeof(header),sizeof(lADCHeader2),sizeof(lADCData2),sizeof(lADCEndBlock));*/
+  header.SetAttribute(0);  // valid data
+  file->WriteBuffer((char*)(&header), sizeof(header));
+
+  // write the raw data and close the file
+  file->WriteBuffer((char*) &lADCHeader1, sizeof (lADCHeader1));
+  file->WriteBuffer((char*)(lADCData1), sizeof(lADCData1));
+  file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
+  file->WriteBuffer((char*) &lADCHeader2, sizeof (lADCHeader2));
+  file->WriteBuffer((char*)(lADCData2), sizeof(lADCData2));
+  file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
+  file->WriteBuffer((char*) &lADCHeader3, sizeof (lADCHeader3));
+  file->WriteBuffer((char*)(lADCData3), sizeof(lADCData3));
+  file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
+  file->WriteBuffer((char*) &lADCHeader4, sizeof (lADCHeader4));
+  file->WriteBuffer((char*)(lADCData4), sizeof(lADCData4));
+  file->WriteBuffer((char*) &lADCEndBlock, sizeof(lADCEndBlock));
+  delete file;
+
+  // unload the digits
+  fLoader->UnloadDigits();
+}
+
+//_____________________________________________________________________________
+Bool_t AliZDC::Raw2SDigits(AliRawReader* rawReader)
+{
+  // Convert ZDC raw data to Sdigits
   
-  Int_t NGenSpecN=0, NGenSpecP=0, NGenSpec=0;
-  Double_t ImpPar=0;
-  // Cut value for Ezem (GeV)
-  // [1] ### Results in Chiara's PhD thesis -> 0<b<15 fm (Dec 2001)
-  //Float_t EZEMCut = 360.; 
-  // [2] ### Results from a new production  -> 0<b<18 fm (Apr 2002)
-  Float_t EZEMCut = 420.;
-  Float_t DeltaEZEMSup = 690.; 
-  Float_t DeltaEZEMInf = 270.; 
-  if(ZEMenergy > (EZEMCut+DeltaEZEMSup)){
-    NGenSpecN = (Int_t) (fZNCen->Eval(ZNenergy));
-    NGenSpecP = (Int_t) (fZPCen->Eval(ZPenergy));
-    NGenSpec  = (Int_t) (fZDCCen->Eval(ZDCenergy));
-    ImpPar    = fbCen->Eval(ZDCenergy);
-    //printf("    fZNCen = %f, fZPCen = %f, fZDCCen = %f\n",fZNCen->Eval(ZNenergy),
-    //            fZPCen->Eval(ZPenergy),fZDCCen->Eval(ZDCenergy));
-  }
-  else if(ZEMenergy < (EZEMCut-DeltaEZEMInf)){
-    NGenSpecN = (Int_t) (fZNPer->Eval(ZNenergy)); 
-    NGenSpecP = (Int_t) (fZPPer->Eval(ZPenergy));
-    NGenSpec  = (Int_t) (fZDCPer->Eval(ZDCenergy));
-    ImpPar    = fbPer->Eval(ZDCenergy);
-    //printf("    fZNPer = %f, fZPPer = %f, fZDCPer = %f\n",fZNPer->Eval(ZNenergy),
-    //            fZPPer->Eval(ZPenergy),fZDCPer->Eval(ZDCenergy));
+  AliLoader* loader = (gAlice->GetRunLoader())->GetLoader("ZDCLoader");
+  if(!loader) {
+    AliError("no ZDC loader found");
+    return kFALSE;
   }
-  else if(ZEMenergy >= (EZEMCut-DeltaEZEMInf) && ZEMenergy <= (EZEMCut+DeltaEZEMSup)){
-    NGenSpecN = (Int_t) (fZEMn->Eval(ZEMenergy));
-    NGenSpecP = (Int_t) (fZEMp->Eval(ZEMenergy));
-    NGenSpec  = (Int_t)(fZEMsp->Eval(ZEMenergy));
-    ImpPar    =  fZEMb->Eval(ZEMenergy);
-    //printf("    Nspec ZEM = %f, Nspec ZDC = %f\n",fZEMsp->Eval(ZNenergy),fZDCPer->Eval(ZDCenergy));
+
+//  // Event loop
+  Int_t iEvent = 0;
+  while(rawReader->NextEvent()){
+    (gAlice->GetRunLoader())->GetEvent(iEvent++);
+    // Create the output digit tree
+    TTree* treeS = loader->TreeS();
+    if(!treeS){
+      loader->MakeTree("S");
+      treeS = loader->TreeS();
+    }
+    //
+    AliZDCSDigit sdigit;
+    AliZDCSDigit* psdigit = &sdigit;
+    const Int_t kBufferSize = 4000;
+    treeS->Branch("ZDC", "AliZDCSDigit",  &psdigit, kBufferSize);
+    //
+    AliZDCRawStream rawStream(rawReader);
+    Int_t sector[2], ADCRes, ADCRaw, ADCPedSub, nPheVal;
+    Int_t jcount = 0;
+    while(rawStream.Next()){
+      if(rawStream.IsADCDataWord()){
+        //For the moment only in-time SDigits are foreseen (1st 44 raw values)
+        if(jcount < 44){ 
+          for(Int_t j=0; j<2; j++) sector[j] = rawStream.GetSector(j);
+         ADCRaw = rawStream.GetADCValue();
+         ADCRes = rawStream.GetADCGain();
+         //printf("\t RAw2SDigits raw%d ->  RawADC[%d, %d, %d] read\n",
+         //    jcount, sector[0], sector[1], ADCRaw);
+         //
+         ADCPedSub = ADCRaw - Pedestal(sector[0], sector[1], ADCRes);
+         if(ADCPedSub<0) ADCPedSub=0;
+         nPheVal = ADCch2Phe(sector[0], sector[1], ADCPedSub, ADCRes);
+          //
+         //printf("\t \t ->  SDigit[%d, %d, %d] created\n",
+         //    sector[0], sector[1], nPheVal);
+         //
+          new(psdigit) AliZDCSDigit(sector, (Float_t) nPheVal);
+          treeS->Fill();
+          jcount++;
+        }
+      }//IsADCDataWord
+    }//rawStream.Next
+    // write the output tree
+    fLoader->WriteSDigits("OVERWRITE");
+    fLoader->UnloadSDigits();
+  }//Event loop 
+   
+  return kTRUE;
+}
+
+//_____________________________________________________________________________
+Int_t AliZDC::Pedestal(Int_t Det, Int_t Quad, Int_t Res) const
+{
+  // Returns a pedestal for detector det, PM quad, channel with res.
+  //
+  // Getting calibration object for ZDC set
+  AliCDBManager *man = AliCDBManager::Instance();
+  man->SetDefaultStorage("local://$ALICE_ROOT");
+  man->SetRun(0);
+  AliCDBEntry  *entry = man->Get("ZDC/Calib/Data");
+  AliZDCCalibData *CalibData = (AliZDCCalibData*) entry->GetObject();
+  //
+  if(!CalibData){
+    printf("\t No calibration object found for ZDC!");
+    return -1;
   }
-  // [1] ### Results in Chiara's PhD thesis -> 0<b<15 fm (Dec 2001)
-  /*if(ZNenergy>158.5)  NGenSpecN = (Int_t) (fZEMn->Eval(ZEMenergy));
-  if(ZPenergy>58.91)  NGenSpecP = (Int_t) (fZEMp->Eval(ZEMenergy));
-  if(ZDCenergy>220.4) NGenSpec  = (Int_t)(fZEMsp->Eval(ZEMenergy));
-  if(ZDCenergy>225.)  ImpPar    =          fZEMb->Eval(ZEMenergy);*/
-  // [2] ### Results from a new production  -> 0<b<18 fm (Apr 2002)
-  if(ZNenergy>162.)  NGenSpecN = (Int_t) (fZEMn->Eval(ZEMenergy));
-  if(ZPenergy>59.75)  NGenSpecP = (Int_t) (fZEMp->Eval(ZEMenergy));
-  if(ZDCenergy>221.5) NGenSpec  = (Int_t)(fZEMsp->Eval(ZEMenergy));
-  if(ZDCenergy>220.)  ImpPar    =  fZEMb->Eval(ZEMenergy);
-  
-  if(NGenSpecN>125)    NGenSpecN=125;
-  else if(NGenSpecN<0) NGenSpecN=0;
-  if(NGenSpecP>82)     NGenSpecP=82;
-  else if(NGenSpecP<0) NGenSpecP=0;
-  if(NGenSpec>207)     NGenSpec=207;
-  else if(NGenSpec<0)  NGenSpec=0;
-  //printf("    NRecSpecN = %d, NRecSpecP = %d, NRecSpec = %d\n",NGenSpecN,NGenSpecP,NGenSpec);
-  
-  //  ---      Number of participants
-  Int_t NPart, NPartTot;
-  NPart = 207-NGenSpecN-NGenSpecP;
-  NPartTot = 207-NGenSpec;
-  //printf("   ###     NPart(ZP+ZN) = %d, NPart(ZDC) = %d, b = %f fm\n",NPart,NPartTot,ImpPar);
-  printf("     ###     NPart = %d, b = %f fm\n",NPartTot,ImpPar);
+  //
+  Float_t PedValue;
+  Float_t meanPed, Pedwidth;
+  Int_t index=0;
+  if(Det==1|| Det==2)        index = 10*(Det-1)+Quad+5*Res;   // ZN1, ZP1
+  else if(Det==3)            index = 10*(Det-1)+(Quad-1)+Res; // ZEM
+  else if(Det==4|| Det==5)    index = 10*(Det-2)+Quad+5*Res+4; // ZN2, ZP2
+  //
+  //
+  meanPed = CalibData->GetMeanPed(index);
+  Pedwidth = CalibData->GetMeanPedWidth(index);
+  PedValue = gRandom->Gaus(meanPed,Pedwidth);
+  //
+  //printf("\t AliZDC::Pedestal - det(%d, %d) - Ped[%d] = %d\n",Det, Quad, index,(Int_t) PedValue); // Chiara debugging!
   
-  //  ---     Writing RecPoints TCA
-  // Allocate the RecPoints TCA 
-  fRecPoints = new TClonesArray("AliZDCReco",1000);
-  AliZDCReco *reco = new AliZDCReco(ZNenergy,ZPenergy,ZDCenergy,ZEMenergy,
-             NDetSpecN,NDetSpecP,NGenSpecN,NGenSpecP,NGenSpec,NPartTot,ImpPar);
-  new((*fRecPoints)[fNRecPoints]) AliZDCReco(*reco);
-  //printf("   fNRecPoints = %d \n",fNRecPoints );
-  //fNRecPoints++;
-  //fRecPoints->Dump();
-  delete reco;
   
-  // TreeR
-  TTree *treeR = gAlice->TreeR();
-  char tname[20];
-  sprintf(tname,"TreeR%d",fNEvBgr);
-  if(!treeR) printf("\n ERROR -> Can't find TreeR%d in background file\n",fNEvBgr);
-  // Branch address
-  char branchRname[20];
-  sprintf(branchRname,"%s",GetName());
-  if(fRecPoints){
-    TBranch *branchR = treeR->GetBranch(branchRname);
-    if(branchR) branchR->SetAddress(&fRecPoints);
-    else if(!branchR) MakeBranchInTreeR(treeR);
+
+  return (Int_t) PedValue;
+}
+
+
+//_____________________________________________________________________________
+Int_t AliZDC::ADCch2Phe(Int_t Det, Int_t Quad, Int_t ADCVal, Int_t Res) const
+{
+  // Evaluation of the no. of phe produced
+  Float_t PMGain[6][5];
+  Float_t ADCRes[2];
+  for(Int_t j = 0; j < 5; j++){
+    PMGain[0][j] = 50000.;
+    PMGain[1][j] = 100000.;
+    PMGain[2][j] = 100000.;
+    PMGain[3][j] = 50000.;
+    PMGain[4][j] = 100000.;
+    PMGain[5][j] = 100000.;
   }
-  treeR->Fill();
-  treeR->Write(tname,TObject::kOverwrite);
-  treeR->Reset();
+  // ADC Caen V965
+  ADCRes[0] = 0.0000008; // ADC Resolution high gain: 200 fC/adcCh
+  ADCRes[1] = 0.0000064; // ADC Resolution low gain:  25  fC/adcCh
+  //
+  Int_t nPhe = (Int_t) (ADCVal * PMGain[Det-1][Quad] * ADCRes[Res]);
+  //
+  //printf("\t AliZDC::ADCch2Phe -> det(%d, %d) - ADC %d  phe %d\n",Det,Quad,ADCVal,nPhe);
+
+  return nPhe;
 }
 
+//______________________________________________________________________
+void AliZDC::SetTreeAddress(){
+
+  // Set branch address for the Trees.
+  if(fLoader->TreeH() && (fHits == 0x0))
+    fHits   = new TClonesArray("AliZDCHit",1000);
+      
+  AliDetector::SetTreeAddress();
+}
  
-//_____________________________________________________________________________
-void   AliZDC::SetMerger(AliZDCMerger* merger)
+//________________________________________________________________
+void AliZDC::CreateCalibData()
+{
+  // 
+  //if(fCalibData) delete fCalibData; // delete previous version
+  fCalibData = new AliZDCCalibData(GetName());
+}
+//________________________________________________________________
+void AliZDC::WriteCalibData(Int_t option)
 {
-// Set pointer to merger 
-    fMerger = merger;
+  //
+  const int kCompressLevel = 9;
+  char* fnam = GetZDCCalibFName();
+  if(!fnam || fnam[0]=='\0') {
+    fnam = gSystem->ExpandPathName("$(ALICE_ROOT)/data/AliZDCCalib.root");
+    Warning("WriteCalibData","No File Name is provided, using default %s",fnam);
+  }
+  TFile* cdfile = TFile::Open(fnam,"UPDATE","",kCompressLevel);
+
+  // Writes Calibration Data to current directory. 
+  // User MUST take care of corresponding file opening and ->cd()... !!!
+  // By default, the object is overwritten. Use 0 option for opposite.
+  if(option) option = TObject::kOverwrite;
+  if(fCalibData) fCalibData->Write(0,option);
+  else if(fCalibData) fCalibData->Write(0,option);
+
+  cdfile->Close();
+  delete cdfile;
 }
 
-//_____________________________________________________________________________
-AliZDCMerger*  AliZDC::Merger()
+//________________________________________________________________
+void AliZDC::LoadCalibData()
 {
-// Return pointer to merger
-    return fMerger;
+  //
+  char* fnam = GetZDCCalibFName();
+  if(!fnam || fnam[0]=='\0') return; 
+  if(!gAlice->IsFileAccessible(fnam)) {
+    Error("LoadCalibData","ZDC Calibration Data file is not accessible, %s",fnam);
+    exit(1);
+  }
+  TFile* cdfile = TFile::Open(fnam);
+
+  // Loads Calibration Data from current directory. 
+  // User MUST take care of corresponding file opening and ->cd()...!!!
+  //
+  if(fCalibData) delete fCalibData; // delete previous version
+  TString dtname = "Calib_";
+  dtname += GetName();
+  fCalibData = (AliZDCCalibData*) gDirectory->Get(dtname.Data());
+  if(!fCalibData) { 
+    Error("LoadCalibData","No Calibration data found for %s",GetName());
+    exit(1);
+  }
+
+  cdfile->Close();
+  delete cdfile;
 }