]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - ZDC/AliZDCMerger.cxx
Compiler Warning removed
[u/mrichter/AliRoot.git] / ZDC / AliZDCMerger.cxx
index b37bcf1a3a99aa5b48f0a846ce8a0a0943a6cf7e..3ea80783ba496ed887cd7e71cdc80f5e803548e7 100644 (file)
 ///////////////////////////////////////////////////////////////////////////////
 
 // --- ROOT system
-#include <iostream.h>
+#include <Riostream.h>
 #include <TTree.h>
 #include <TFile.h>
 #include <TDirectory.h>
-#include <TH1.h>
+#include <TNtuple.h>
+#include <TSystem.h>
+#include <TROOT.h>
 
 // --- AliRoot header files
 #include "AliZDCMerger.h"
 #include "AliZDC.h"
 #include "AliZDCHit.h"
+#include "AliZDCMergedHit.h"
 #include "AliZDCDigit.h"
 #include "AliZDCFragment.h"
 #include "AliRun.h"
 #include "AliDetector.h"
 #include "AliHeader.h"
 #include "AliGenEventHeader.h"
-#include "/home/oppedisa/AliRoot/pro/EVGEN/AliGenHijingEventHeader.h"
+#include "AliGenHijingEventHeader.h"
 
 ClassImp(AliZDCMerger)
 
+//int comp(const void *i,const void *j) {return *(int *)i - *(int *)j;}
+
 //____________________________________________________________________________
 AliZDCMerger::AliZDCMerger()
 {
 // Default constructor    
-    fMerge       = kDigitize;
-    fNEvBgr     = 0;
+    //fMerge       = kDigitize -> Only digitization
+    //fMerge       = kMerge    -> Digitization + Merging
+    fMerge       = kMerge;     
     fFnBgr       = 0;
-//    fFnBgr       = "/home/oppedisa/alirun/hijing.root";
     fBgrFile     = 0;
-    fTrHBgr      = 0;
-    fTrSDBgr     = 0;
+    fNEvBgr     = 0;
+    fHitsBgr     = 0;
     fImpPar      = 0;
     fSpecn       = 0;
     fSpecp       = 0;
     fFreeSpn     = 0;
     fFreeSpp     = 0;
     fFnSpecn     = 0;
-//    fFnSpecn     = "/home/oppedisa/alirun/ZNsignal.root";
     fSpecnFile   = 0;
     fFnSpecp     = 0;
-//    fFnSpecp     = "/home/oppedisa/alirun/ZPsignal.root";
     fSpecpFile   = 0;
-    
+    fNMhits     = 0;
+     
 }
 
 //____________________________________________________________________________
 AliZDCMerger::~AliZDCMerger()
 {
-// Destructor
-    if (fBgrFile)    delete fBgrFile;
-    if (fTrHBgr)     delete fTrHBgr;
-    if (fTrSDBgr)    delete fTrSDBgr;
-    if (fHitsBgr)    delete fHitsBgr;
+    // Destructor
     if (fSpecnFile)  delete fSpecnFile;
     if (fSpecpFile)  delete fSpecpFile;
 }
@@ -84,15 +84,14 @@ void AliZDCMerger::InitMerging()
     // Hits tree, impact parameter, num. of spectators n & p 
     //                 in background (full Hijing) event
     Float_t b;
-    Int_t nspecn, nspecp;
-    Background(b, nspecn, nspecp);
+    Background(b, fSpecn, fSpecp);
 
     // Production of nuclear fragments -> num. of FREE spectators n & p
-    Fragmentation(b, nspecn, nspecp, fFreeSpn, fFreeSpp);
+    Fragmentation(b, fSpecn, fSpecp, fFreeSpn, fFreeSpp);
     
     // Extract from spectators distribution the signal events:
     // NFreeSpectatorN spectator n & NFreeSpectatorP spectator p 
-    Mixing(fFreeSpn, fFreeSpp);
+    Mixing();
 }
 
 //____________________________________________________________________________
@@ -100,39 +99,46 @@ void AliZDCMerger::Background(Float_t &fImpPar, Int_t &fSpecn, Int_t &fSpecp)
 {
     
     // --- Open the background file
-    if (fMerge && !fBgrFile) fBgrFile = OpenBgrFile();
+  if (fMerge && !fBgrFile) fBgrFile = OpenBgrFile();
     
     // --- Read from the TreeE impact parameter (b),
     //     # of spectators n and p (fSpecn, fSpecp)
     fBgrFile->cd();
 
-    // Get AliRun object from file or create it if not on file
-    gAlice = (AliRun*)fBgrFile->Get("gAlice");
-    if (!gAlice) {
-      gAlice = (AliRun*)fBgrFile->Get("gAlice");
-      if (gAlice) printf("AliRun object found on file\n");
-      if (!gAlice) {
-         printf("\n create new gAlice object");
-         gAlice = new AliRun("gAlice","Alice test program");
-      }
-    }
-
+//    // Get AliRun object from file or create it if not on file
 //    gAlice = (AliRun*)fBgrFile->Get("gAlice");
+//    if (!gAlice) {
+//      gAlice = (AliRun*)fBgrFile->Get("gAlice");
+//      if (gAlice) printf("AliRun object found on file\n");
+//      if (!gAlice) {
+//       printf("\n create new gAlice object");
+//       gAlice = new AliRun("gAlice","Alice test program");
+//      }
+//    }
+    
+    //    gAlice->GetEvent(fNEvBgr);  this is done in the steering macro
     AliHeader *header = gAlice->GetHeader();
     AliGenEventHeader* mcHeader = header->GenEventHeader();
     fImpPar = ((AliGenHijingEventHeader*) mcHeader)->ImpactParameter();
-    fSpecn  = ((AliGenHijingEventHeader*) mcHeader)->Spectatorsn();
-    fSpecp  = ((AliGenHijingEventHeader*) mcHeader)->Spectatorsp();
-//      printf("\n     HIJING simulation - b = %f fm, Nspecn = %d, Nspecp = %d\n",fImpPar,fSpecn,fSpecp);
+    Int_t dSpecn  = ((AliGenHijingEventHeader*) mcHeader)->Spectatorsn();
+    Int_t dSpecp  = ((AliGenHijingEventHeader*) mcHeader)->Spectatorsp();
+    // Until there is only 1 ZDC set the # of spectators must be divided by 2!!!
+    fSpecn = dSpecn/2;
+    fSpecp = dSpecp/2;
+    printf("\n HIJING ev. #%d - b = %f fm, Nspecn = %d, Nspecp = %d\n",
+            fNEvBgr,fImpPar,fSpecn,fSpecp);
 }
 
 //____________________________________________________________________________
 TFile* AliZDCMerger::OpenBgrFile()
 {
     // Initialise background event
-    TFile *file = new TFile(fFnBgr,"UPDATE");
-//      printf("\n AliZDCMerger --- Background event -> %s file opened \n", fFnBgr);
+  TFile *file = (TFile*)gROOT->GetListOfFiles()->FindObject(fFnBgr);
+  if(!file)cerr<<"AliZDCMerger: background file "<<fFnBgr<<" not found\n";
+  //    TFile *file = new TFile(fFnBgr,"UPDATE");
+    printf("\n AliZDCMerger --- Background event -> %s file opened \n", fFnBgr);
     fHitsBgr = new TClonesArray("AliZDCHit",1000);
+    fMHits   = new TClonesArray("AliZDCMergedHit",1000);
     return file;
 }
 
@@ -140,7 +146,8 @@ TFile* AliZDCMerger::OpenBgrFile()
 void AliZDCMerger::Fragmentation(Float_t fImpPar, Int_t fSpecn, Int_t fSpecp,
                                  Int_t &fFreeSpn, Int_t &fFreeSpp)
 {
-    Int_t j, zz[100], nn[100], nAlpha, Ztot, Ntot;
+    //printf("\n       Fragmentation -> fSpecn = %d, fSpecp = %d\n",fSpecn,fSpecp);
+    Int_t j, zz[100], nn[100], nAlpha, ztot, ntot;
     AliZDCFragment *frag = new AliZDCFragment(fImpPar);
     for(j=0; j<=99; j++){
        zz[j] =0;
@@ -151,334 +158,284 @@ void AliZDCMerger::Fragmentation(Float_t fImpPar, Int_t fSpecn, Int_t fSpecp,
     frag->GenerateIMF(zz, nAlpha);
 
     // Attach neutrons
-    Ztot=0;
-    Ntot=0;
-    frag->AttachNeutrons(zz, nn, Ztot, Ntot);
-    fFreeSpn = fSpecn-Ztot-2*nAlpha;
-    fFreeSpp = fSpecp-Ntot-2*nAlpha;
-//      printf("\n     Fragmentation -> FreeSpn = %d, FreeSpp = %d\n",fFreeSpn,fFreeSpp);
+    ztot=0;
+    ntot=0;
+    frag->AttachNeutrons(zz, nn, ztot, ntot);
+    fFreeSpn = fSpecn-ntot-2*nAlpha;
+    fFreeSpp = fSpecp-ztot-2*nAlpha;
+    if(fFreeSpn<0) fFreeSpn=0;
+    if(fFreeSpp<0) fFreeSpp=0;
+    //printf("\n                       2*nAlpha = %d, ztot = %d, ntot = %d\n",2*nAlpha, ztot,    ntot);
+    printf("\n Fragmentation -> FreeSpn = %d, FreeSpp = %d\n",fFreeSpn,fFreeSpp);
 }
 
 //____________________________________________________________________________
-void AliZDCMerger::Mixing(Int_t fFreeSpn, Int_t fFreeSpp)
+void AliZDCMerger::Mixing()
 {
-
-//      printf("\n     AliZDCMerger->Mixing\n");
-        
+    
+    //printf("\n       AliZDCMerger->Mixing\n");
+       
     // ### Background event Hits ###########################################
     fBgrFile->cd();
 //    fBgrFile->ls();
     
-    AliZDC *ZDC = (AliZDC *)gAlice->GetModule("ZDC");
-//    if(ZDC) printf("\n       Ho trovato lo ZDC!\n");
+   AliZDC *zdc = (AliZDC *)gAlice->GetModule("ZDC");
+//    if(zdc) printf("\n       Ho trovato lo ZDC!\n");
 
+//    fNEvBgr = 0; // Let's suppose to have 1 full Hijing event per file
     // Hits tree
-    if(fTrHBgr) delete fTrHBgr;
-    fTrHBgr = 0;    
-    // SDigits tree
-    if(fTrSDBgr) delete fTrSDBgr;
-    fTrSDBgr = 0;    
-
-//    // Read from Event Tree the # of events of the Hijing file 
-//    TTree *thead = (TTree *) fBgrFile->Get("TE");
-//    if(!te){
-//      printf("\n     ERROR! -> File %s does not contain TreeE\n");
-//    }
-//    fNEvBgr = (Int_t) te->GetEntries();
-    fNEvBgr = 0; // Let's suppose to have 1 full Hijing event per file
-
-    // Hits Tree
     char treeBgrName[20];
     sprintf(treeBgrName,"TreeH%d",fNEvBgr);
-    fTrHBgr = (TTree*)gDirectory->Get(treeBgrName); // TreeH
+    fTrHBgr = (TTree*)gDirectory->Get(treeBgrName); 
     if(!fTrHBgr){
       printf("\n ERROR -> Can't find TreeH%d in background file\n",fNEvBgr);
     }    
+//    fTrHBgr->Print();
+    
+    // Branch address
+    TBranch *branch;
+    char branchname[20];
+    sprintf(branchname,"%s",zdc->GetName());
+    if(fTrHBgr && fHitsBgr){
+//      printf("\n     fTrHBgr!=0 && fHitsBgr!=0\n");
+      branch = fTrHBgr->GetBranch(branchname);
+      if(branch) branch->SetAddress(&fHitsBgr);
+    }
+    
     Int_t ntracks =  (Int_t) fTrHBgr->GetEntries();
-//    printf("\n       --- ntracks = %d\n",ntracks);
-
-    // SDigits Tree
-    char treeSBgrName[20];
-    sprintf(treeSBgrName,"TreeS%d",fNEvBgr);
-    fTrSDBgr = (TTree*)gDirectory->Get(treeSBgrName); // TreeH
-    if(!fTrSDBgr){
-      printf("\n ERROR -> Can't find TreeS%d in background file\n",fNEvBgr);
-    }    
+    //printf("\n       --- ntracks = %d\n\n", ntracks);
+    
+    Int_t itrack, nhits, ihit, j, sector[2];
+    AliZDCHit* zdcHit;
+    AliZDCMergedHit *mHit;
+    Float_t mHits[7];
+    TClonesArray &sdigits = *fMHits;   // SDigits TCArray
+    fNMhits = 0;
 
-    Int_t itrack, i, volume[2], detector, quadrant;
-    TClonesArray &sdigits = *fHitsBgr; // SDigits TCArray
-    Float_t hits[10];
     // --- Tracks loop
     for(itrack=0; itrack<ntracks; itrack++){
-//       printf("              itrack = %d \n", itrack);
        fTrHBgr->GetEvent(itrack);
        
-       fTrack = itrack;
-       Int_t NMhits  = 0;
-       for(AliZDCHit* zdcHit=(AliZDCHit*)ZDC->FirstHit(-1); 
-           zdcHit;
-          zdcHit = (AliZDCHit*)ZDC->NextHit()){ 
-
-         for(i=0; i<2; i++) volume[i] = zdcHit->GetVolume(i);
-//       printf("\n            volume[0] = %d volume[1]= %d \n",volume[0], volume[1]);
-         if(volume[0] != 3){
-           // Background hits  
-           hits[7] = zdcHit->GetLightPMQ();
-           hits[8] = zdcHit->GetLightPMC();
-//         printf("\n  Prima ### Background -> PMQ = %f, PMC = %f\n",hits[7],hits[8]);
-
-            //Signal hits
-           detector = volume[0];
-           quadrant = volume[1];
-           ExtractSignal(detector, quadrant, fQuadLight, fComLight);      
-           hits[7] += fQuadLight;
-           hits[8] += fComLight;
-//         printf("\n  Bckg + Signal -> PMQ = %f, PMC = %f\n",hits[7],hits[8]);
-
-            zdcHit->SetLightPMQ(hits[7]);
-            zdcHit->SetLightPMC(hits[8]);
-           hits[7] = zdcHit->GetLightPMQ();
-           hits[8] = zdcHit->GetLightPMC();
-//         printf("\n  Dopo ### Background -> PMQ = %f, PMC = %f\n",hits[7],hits[8]);
-                   
-           new (sdigits[NMhits++]) AliZDCHit(zdcHit);
-//            printf("\n       NMhits = %d\n",NMhits);
-         }
-          fBgrFile->cd();
-          fTrSDBgr->Fill();
-          fTrSDBgr->Write(0,TObject::kOverwrite);
-       } //Hits loop
-       Digitize();
+       nhits = fHitsBgr->GetEntries();
+//       printf("            nhits = %d \n", nhits);
+       for(ihit=0; ihit<nhits; ihit++){    
+         zdcHit = (AliZDCHit*) fHitsBgr->UncheckedAt(ihit);
+
+           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();
+           mHit = new AliZDCMergedHit(sector, mHits);
+//         mHit->Print("");
+           new((*fMHits)[fNMhits]) AliZDCMergedHit(*mHit);
+           new (sdigits[fNMhits])  AliZDCMergedHit(*mHit);
+           delete mHit;
+           fNMhits++;
+         }//Hits loop
+         
     } // Tracks loop
+    //printf(" fNMhits (after bckg) = %d, \n",fNMhits);     
         
-//    fBgrFile->Close();
+    // ### Signal event Hits ###########################################
+    // --- Neutrons
+    ExtractSignal(1);
+        
+    // --- Protons
+    ExtractSignal(2);
+    //printf(" fNMhits (after signal) = %d \n",fNMhits); 
         
 }
 
 //____________________________________________________________________________
-void AliZDCMerger::ExtractSignal(Int_t detector, Int_t quadrant,
-                                    Float_t &fQuadLight, Float_t &fComLight)
+void AliZDCMerger::ExtractSignal(Int_t SpecType)
 {
 
-//    printf("\n       Entering ExtractSignal method -> detector = %d quadrant = %d\n",
-//           detector, quadrant);
-
-    // Connect spectator n histo's file
-    fSpecnFile = TFile::Open("/home/oppedisa/alirun/ZNsignal.root");
-    fSpecnFile->cd();
-//    fSpecnFile->ls();
-    TH1F *hPMQ1zn = (TH1F*) gDirectory->Get("hPMQ1zn;1");
-    TH1F *hPMQ2zn = (TH1F*) gDirectory->Get("hPMQ2zn;1");
-    TH1F *hPMQ3zn = (TH1F*) gDirectory->Get("hPMQ3zn;1");
-    TH1F *hPMQ4zn = (TH1F*) gDirectory->Get("hPMQ4zn;1");
-    TH1F *hPMC1zn = (TH1F*) gDirectory->Get("hPMC1zn;1");
-    TH1F *hPMC2zn = (TH1F*) gDirectory->Get("hPMC2zn;1");
-    TH1F *hPMC3zn = (TH1F*) gDirectory->Get("hPMC3zn;1");
-    TH1F *hPMC4zn = (TH1F*) gDirectory->Get("hPMC4zn;1");
-//    Axis_t x = hPMQ1zn -> GetRandom();
-//    printf(" hPMQ1zn -> GetRandom() = %f\n",x);
-    
-    // Connect spectator p histo's file
-    fSpecpFile = TFile::Open("/home/oppedisa/alirun/ZPsignal.root");
-    fSpecpFile->cd(); 
-//    fSpecpFile->ls();
-    TH1F *hPMQ1zp = (TH1F*) gDirectory->Get("hPMQ1zp;1");
-    TH1F *hPMQ2zp = (TH1F*) gDirectory->Get("hPMQ2zp;1");
-    TH1F *hPMQ3zp = (TH1F*) gDirectory->Get("hPMQ3zp;1");
-    TH1F *hPMQ4zp = (TH1F*) gDirectory->Get("hPMQ4zp;1");
-    TH1F *hPMC1zp = (TH1F*) gDirectory->Get("hPMC1zp;1");
-    TH1F *hPMC2zp = (TH1F*) gDirectory->Get("hPMC2zp;1");
-    TH1F *hPMC3zp = (TH1F*) gDirectory->Get("hPMC3zp;1");
-    TH1F *hPMC4zp = (TH1F*) gDirectory->Get("hPMC4zp;1");
-
-    if(detector == 1){ // --- ZN
-      if(quadrant == 1){
-        fQuadLight = (Float_t) hPMQ1zn -> GetRandom();
-       fComLight = (Float_t) hPMC1zn -> GetRandom();
-      }
-      else if(quadrant == 2){
-        fQuadLight = (Float_t) hPMQ2zn -> GetRandom();
-       fComLight = (Float_t) hPMC2zn -> GetRandom();
-      }
-      else if(quadrant == 3){
-        fQuadLight = (Float_t) hPMQ3zn -> GetRandom();
-       fComLight = (Float_t) hPMC3zn -> GetRandom();
-      }
-      else if(quadrant == 4){
-        fQuadLight = (Float_t) hPMQ4zn -> GetRandom();
-       fComLight = (Float_t) hPMC4zn -> GetRandom();
-      }
-    }
-
-    else if(detector == 2){ // --- ZP
-      fSpecpFile->cd();        // Connect spectator p histo's file
-//      fSpecpFile->ls();
-      if(quadrant == 1){
-        fQuadLight = (Float_t) hPMQ1zp -> GetRandom();
-       fComLight = (Float_t) hPMC1zp -> GetRandom();
-      }
-      else if(quadrant == 2){
-        fQuadLight = (Float_t) hPMQ2zp -> GetRandom();
-       fComLight = (Float_t) hPMC2zp -> GetRandom();
-      }
-      else if(quadrant == 3){
-        fQuadLight = (Float_t) hPMQ3zp -> GetRandom();
-       fComLight = (Float_t) hPMC3zp -> GetRandom();
-      }
-      else if(quadrant == 4){
-        fQuadLight = (Float_t) hPMQ4zp -> GetRandom();
-       fComLight = (Float_t) hPMC4zp -> GetRandom();
-      }
-    }
-//    printf(" ---     Exiting ExtractSignal -> fQuadLight = %f, fComLight = %f",
-//           fQuadLight,fComLight);
-
+// printf("\n  Entering in Extract Signal\n");
+ Int_t numEvents = 0;
+ if(SpecType == 1){            // --- Signal for spectator neutrons
+   fFnSpecn = gSystem->ExpandPathName("$ALICE/$ALICE_LEVEL/ZDC/ZNsignalntu.root");
+   fSpecnFile = TFile::Open(fFnSpecn,"R");
+   fSpecnFile->cd();
+   printf("\n  --- ExtractSignal x n: file %s opened\n", fFnSpecn);
+   numEvents = fFreeSpn;
+ }
+ else if(SpecType == 2){       // --- Signal for spectator protons
+   fFnSpecp = gSystem->ExpandPathName("$ALICE/$ALICE_LEVEL/ZDC/ZPsignalntu.root");
+   fSpecpFile = TFile::Open(fFnSpecp,"R");
+   fSpecpFile->cd();
+   printf("\n  --- ExtractSignal x p: file %s opened\n", fFnSpecp);
+   numEvents = fFreeSpp;
+ }
+ //printf("\n          # of free spectators = %d\n", numEvents);
+ //printf("\n         fNMhits (before adding signal) = %d\n",fNMhits);
+
+  TNtuple *zdcSignal = (TNtuple*) gDirectory->Get("ZDCSignal");
+  Int_t nentries = (Int_t) zdcSignal->GetEntries();
+  //printf("\n   # entries = %d\n", nentries);
+  
+  AliZDCMergedHit *mHit; 
+  Float_t *entry, hitsSpec[7];
+  Int_t pl, i, j, k, iev=0, rnd[125], volume[2];
+  for(pl=0;pl<125;pl++){
+     rnd[pl] = 0;
+  }
+  for(pl=0;pl<numEvents;pl++){
+     rnd[pl] = (Int_t) (9999*gRandom->Rndm());
+     if(rnd[pl] >= 9998) rnd[pl] = 9997;
+     //printf("        rnd[%d] = %d\n",pl,rnd[pl]);     
+  }
+  // Sorting vector in ascending order with C function QSORT 
+  qsort((void*)rnd,numEvents,sizeof(Int_t),comp);
+  //for(pl=0;pl<numEvents;pl++){
+  ////printf(" rnd[%d] = %d\n",pl,rnd[pl]);     
+  //}
+  do{
+     for(i=0; i<nentries; i++){  
+       zdcSignal->GetEvent(i);
+       entry = zdcSignal->GetArgs();
+       if(entry[0] == rnd[iev]){
+          for(k=0; k<2; k++) volume[k] = (Int_t) entry[k+1];
+          for(j=0; j<7; j++){
+             hitsSpec[j] = entry[j+3];
+          }
+          //printf("\n   i = %d, iev = %d, entry[0] = %f, rnd[%d] = %d ",i,iev,entry[0],iev,rnd[iev]);
+         mHit = new AliZDCMergedHit(volume, hitsSpec);
+         new((*fMHits)[fNMhits++]) AliZDCMergedHit(*mHit);
+         delete mHit;
+       }
+       else if(entry[0] > rnd[iev]){
+         iev++;
+         continue;
+       }
+     }
+  }while(iev<numEvents);
+  
+  if(SpecType ==1){
+    //printf("\n         fNMhits (after n signal) = %d\n",fNMhits);
+    fSpecnFile->Close();
+  }
+  else if(SpecType == 2){
+    //printf("\n         fNMhits (after p signal) = %d\n",fNMhits);
+    fSpecpFile->Close();
+  }
+      
 }
 
 //____________________________________________________________________________
-void AliZDCMerger::Digitize()
+void AliZDCMerger::Digitize(Int_t fNMhits, TClonesArray *fMHits)
 {
+// Digitization
 
-//  printf("\n AliZDCMerger->Digitize()");
-
-  AliZDC *ZDC = (AliZDC *)gAlice->GetModule("ZDC");
-//  if(ZDC) printf("\n         Ho trovato lo ZDC!\n");
+  printf("\n   AliZDCMerger->Digitize()");
 
-  Int_t itrack, lightQ, lightC, sector[2], digit;
-  Int_t PMCZN = 0, PMCZP = 0, PMQZN[4], PMQZP[4], PMZEM = 0;
+  AliZDC *zdc = (AliZDC *)gAlice->GetModule("ZDC");
+//  if(zdc) printf("\n         Ho trovato lo ZDC!\n");
+  Int_t lightQ, lightC, sector[2], digit;
+  Int_t pmCZN = 0, pmCZP = 0, pmQZN[4], pmQZP[4], pmZEM1 = 0, pmZEM2 = 0;
   Int_t i;
   for(i=0; i<4; i++){
-     PMQZN[i] = 0;
-     PMQZP[i] = 0;
+     pmQZN[i] = 0;
+     pmQZP[i] = 0;
   }
-
-  // ### Digitization "on the flight" (no merging) #######################
-  if(fMerge == kDigitize){
-//    printf("\n               fMerge == kDigitize\n");
-    fTrHBgr = gAlice->TreeH(); // TTree
-    TTree *treeH = gAlice->TreeH();
-    if(!fTrHBgr){
-      printf("\n ERROR -> Can't find TreeH%d in background file\n",fNEvBgr);
-    }    
-    Int_t ntracks =  (Int_t) treeH->GetEntries();
-//    printf("\n       --- ntracks = %d\n",ntracks);
-        
-    // Loop over tracks
-    for(itrack=0; itrack<ntracks; itrack++){
-       gAlice->ResetHits();
-       gAlice->TreeH()->GetEvent(itrack);
-       
-       // Loop over hits
-       for(AliZDCHit* zdcHit=(AliZDCHit*)ZDC->FirstHit(-1); 
-           zdcHit;
-          zdcHit = (AliZDCHit*)ZDC->NextHit()){ 
-         sector[0] = zdcHit->GetVolume(0);
-         sector[1] = zdcHit->GetVolume(1);
-         lightQ    = Int_t(zdcHit->GetLightPMQ());
-         lightC    = Int_t(zdcHit->GetLightPMC());
-//       printf("\n    Digitise -> DET. = %d, quad = %d", sector[0], sector[1]);
-//       printf("                  PMQ = %d, PMC = %d",  lightQ, lightC);
-          
-         if(sector[0] == 1){   //ZN 
-           PMCZN = PMCZN + lightC;
-           PMQZN[sector[1]-1] = PMQZN[sector[1]-1] + lightQ;
-         }
-          else if(sector[0] == 2){     //ZP 
-           PMCZP = PMCZP + lightC;
-           PMQZP[sector[1]-1] = PMQZP[sector[1]-1] + lightQ;
-         }
-          else if(sector[0] == 3){     //ZEM 
-           PMZEM = PMZEM + lightC;
-         }
-       } // hits loop
-    } // tracks loop
-  } // if(fMerge)
-
-  // ### Merging and digitization #######################################
-  else if(fMerge == kMerge){
-//    printf("\n               fMerge == kMerge\n");
-
-//    Int_t ntracks =  (Int_t) fTrHBgr->GetEntries();
-//    printf("\n        --- ntracks = %d\n",ntracks);
-//        
-//    // Loop over tracks
-//    for(itrack=0; itrack<ntracks; itrack++){
-       fTrHBgr->GetEvent(fTrack);
-       
-//       printf("\n\n  Track # %d --- Digitise -> ", fTrack);
-       // Loop over hits
-       for(AliZDCHit* zdcHit=(AliZDCHit*)ZDC->FirstHit(-1); 
-           zdcHit;
-          zdcHit = (AliZDCHit*)ZDC->NextHit()){ 
-         sector[0] = zdcHit->GetVolume(0);
-         sector[1] = zdcHit->GetVolume(1);
-         lightQ    = Int_t(zdcHit->GetLightPMQ());
-         lightC    = Int_t(zdcHit->GetLightPMC());
-//       printf("\n    DET. = %d, quad = %d,PMQ = %d, PMC = %d", 
-//              sector[0], sector[1],lightQ, lightC);
-          
-         if(sector[0] == 1){   //ZN 
-           PMCZN = PMCZN + lightC;
-           PMQZN[sector[1]-1] = PMQZN[sector[1]-1] + lightQ;
-         }
-          else if(sector[0] == 2){     //ZP 
-           PMCZP = PMCZP + lightC;
-           PMQZP[sector[1]-1] = PMQZP[sector[1]-1] + lightQ;
-         }
-          else if(sector[0] == 3){     //ZEM 
-           PMZEM = PMZEM + lightC;
-         }
-       } // hits loop
-//    } // tracks loop
-  } // if(fMerge)
-
+  
+  AliZDCMergedHit *mHit;
+  Int_t imhit;
+  printf("        fNMHits = %d\n", fNMhits);
+  // Loop over SDigits
+  for(imhit=0; imhit<fNMhits; imhit++){
+     
+     mHit = (AliZDCMergedHit*) fMHits->UncheckedAt(imhit);
+     sector[0] = mHit->GetSector(0);
+     sector[1] = mHit->GetSector(1);
+   // tmp -> c'erano i quadranti cannati!
+  if((sector[1]!=1) && (sector[1]!=2) && (sector[1]!=3) && (sector[1]!=4)){
+     printf("\n  *** ERROR!!! sector[0] = %d, sector[1] = %d\n",
+             sector[0], sector[1]);
+       sector[1] = 0;
+   }
+     lightQ    = Int_t(mHit->GetLightPMQ());
+     lightC    = Int_t(mHit->GetLightPMC());
+//     printf("    imhit = %d -> DET. = %d, quad = %d,PMQ = %d, PMC = %d\n", 
+//      imhit,sector[0], sector[1],lightQ, lightC);
+     
+     if(sector[0] == 1){          //ZN 
+       pmCZN = pmCZN + lightC;
+       pmQZN[sector[1]-1] = pmQZN[sector[1]-1] + lightQ;
+     }
+     else if(sector[0] == 2){     //ZP 
+       pmCZP = pmCZP + lightC;
+       pmQZP[sector[1]-1] = pmQZP[sector[1]-1] + lightQ;
+     }
+     else if(sector[0] == 3){     //ZEM 
+       if(sector[1] ==1) pmZEM1 = pmZEM1 + lightC;
+       else              pmZEM2 = pmZEM2 + lightQ;
+     }
+  } // SDigits loop
       
+  // ### Digits creation ###############################################
   // Create digits for ZN
-  Int_t PedValue;
+  Int_t pedValue;
   sector[0] = 1; // Detector = ZN
   sector[1] = 0; // Common PM ADC
-  digit = Phe2ADCch(1, 0, PMCZN);
-//  printf("\n\n       ZN ###  PMCZN = %d      ADCZN = %d",PMCZN, digit);
-  ZDC->AddDigit(sector, digit);
+  digit = Phe2ADCch(1, 0, pmCZN);
+  printf("\n\n ZN ###  pmCZN = %d      ADCZN = %d",pmCZN, digit);
+  pedValue = AddPedestal();
+  digit += pedValue;
+//  printf("   pedValue = %d",pedValue);
+  zdc->AddDigit(sector, digit);
   Int_t j;
   for(j=0; j<4; j++){
     sector[1] = j+1; // Towers PM ADCs
-    digit = Phe2ADCch(1, j+1, PMQZN[j]);
-//    printf("\n               PMQZN[%d] = %d  ADCZN[%d] = %d",j,PMQZN[j],j,digit);
-    PedValue = AddPedestal();
-    digit += PedValue;
-//    printf(" PedValue = %d",PedValue);
-    ZDC->AddDigit(sector, digit);
+    digit = Phe2ADCch(1, j+1, pmQZN[j]);
+    printf("\n         pmQZN[%d] = %d  phe     ADCZN[%d] = %d adcCh",j,pmQZN[j],j,digit);
+    pedValue = AddPedestal();
+    digit += pedValue;
+//    printf(" pedValue = %d",pedValue);
+    zdc->AddDigit(sector, digit);
   }
-//    printf("\n");
+  printf("\n");
   
   // Create digits for ZP
   sector[0] = 2; // Detector = ZP
   sector[1] = 0; // Common PM ADC
-  digit = Phe2ADCch(2, 0, PMCZP);
-//  printf("\n ZP --- PMCZP = %d       ADCZP = %d",PMCZP,digit);
-  ZDC->AddDigit(sector, digit);
+  digit = Phe2ADCch(2, 0, pmCZP);
+  printf("\n   ZP --- pmCZP = %d       phe     ADCZP = %d adcCh",pmCZP,digit);
+  pedValue = AddPedestal();
+  digit += pedValue;
+//  printf("   pedValue = %d",pedValue);
+  zdc->AddDigit(sector, digit);
   for(j=0; j<4; j++){
     sector[1] = j+1; // Towers PM ADCs
-    digit = Phe2ADCch(2, j+1, PMQZP[j]);
-//    printf("\n              PMQZP[%d] = %d   ADCZP[%d] = %d",j,PMQZP[j],j,digit);
-    PedValue = AddPedestal();
-    digit += PedValue;
-//    printf(" PedValue = %d",PedValue);
-    ZDC->AddDigit(sector, digit);
+    digit = Phe2ADCch(2, j+1, pmQZP[j]);
+    printf("\n        pmQZP[%d] = %d   phe     ADCZP[%d] = %d adcCh",j,pmQZP[j],j,digit);
+    pedValue = AddPedestal();
+    digit += pedValue;
+//    printf(" pedValue = %d",pedValue);
+    zdc->AddDigit(sector, digit);
   }
-//    printf("\n");
+  printf("\n");
   
   // Create digits for ZEM
-  sector[0] = 3; // Detector = ZZEM
-  sector[1] = 0; // Single PM ADC
-  digit  = Phe2ADCch(3, 0, PMZEM);
-//  printf("\n ZEM *** PMZEM = %d      ADCZEM = %d",PMZEM,digit);
-    PedValue = AddPedestal();
-    digit += PedValue;
-//    printf(" PedValue = %d\n",PedValue);
-  ZDC->AddDigit(sector, digit); 
-
+  sector[0] = 3; 
+  sector[1] = 1; // Detector = ZEM1
+  digit  = Phe2ADCch(3, 1, pmZEM1);
+  printf("\n   ZEM *** pmZEM1 = %d      phe     ADCZEM1 = %d adcCh",pmZEM1,digit);
+  pedValue = AddPedestal();
+  digit += pedValue;
+//  printf("  pedValue = %d\n",pedValue);
+  zdc->AddDigit(sector, digit); 
+  sector[1] = 2; // Detector = ZEM2
+  digit  = Phe2ADCch(3, 2, pmZEM2);
+  printf("\n   ZEM *** pmZEM2 = %d      phe     ADCZEM2 = %d adcCh\n",pmZEM2,digit);
+  pedValue = AddPedestal();
+  digit += pedValue;
+//  printf("  pedValue = %d\n",pedValue);
+  zdc->AddDigit(sector, digit); 
 }
 
 //_____________________________________________________________________________
@@ -490,10 +447,7 @@ Int_t AliZDCMerger::Phe2ADCch(Int_t Det, Int_t Quad, Int_t Light)
 //    printf("\n  Phe2ADCch -> Detector = %d, Quadrant = %d, Light = %d\n", Det, Quad, Light);
   }
   
-  Int_t ADCch = 0;
-  // Parameters for conversion of light yield in ADC channels
-  Float_t fPMGain[3][5];      // PM gain
-  Float_t fADCRes;            // ADC conversion factor
+  Int_t adcCh = 0;
   
   Int_t j,i;
   for(i=0; i<3; i++){
@@ -501,11 +455,11 @@ Int_t AliZDCMerger::Phe2ADCch(Int_t Det, Int_t Quad, Int_t Light)
         fPMGain[i][j]   = 100000.;
      }
   }
-  fADCRes   = 0.00000064; // ADC Resolution: 250 fC/ADCch
+  fADCRes   = 0.00000064; // ADC Resolution: 250 fC/adcCh
   
-  ADCch = (Int_t) (Light*fPMGain[Det-1][Quad]*fADCRes);
+  adcCh = (Int_t) (Light*fPMGain[Det-1][Quad]*fADCRes);
      
-  return ADCch;
+  return adcCh;
 }
 
 //_____________________________________________________________________________
@@ -514,11 +468,11 @@ Int_t AliZDCMerger::AddPedestal()
   // --- Pedestal value -> extracted from a gaussian distribution
   // obtained from the beam test on the ZEM prototype (Aug. 2000)
   
-  Int_t PedValue;
-  Float_t PedMean  = 50.;
-  Float_t PedWidth = 10.;
+  Int_t pedValue;
+  Float_t pedMean  = 50.;
+  Float_t pedWidth = 5.;
   
-  PedValue    = (Int_t) gRandom->Gaus(PedMean,PedWidth);
+  pedValue    = (Int_t) gRandom->Gaus(pedMean,pedWidth);
   
-  return PedValue;
+  return pedValue;
 }