]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ZDC/AliZDCDigitizer.cxx
Adding some features for DQM shifter histos
[u/mrichter/AliRoot.git] / ZDC / AliZDCDigitizer.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17
18 ///////////////////////////////////////////////////////////////////////////////
19 //                                                                           //
20 //                      ZDC digitizer class                                  //
21 //                                                                           //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include <stdlib.h>
25
26 // --- ROOT system
27 #include <TTree.h>
28 #include <TFile.h>
29 #include <TNtuple.h>
30 #include <TRandom.h>
31
32 // --- AliRoot header files
33 #include "AliLog.h"
34 #include "AliRun.h"
35 #include "AliHeader.h"
36 #include "AliGenHijingEventHeader.h"
37 #include "AliRunDigitizer.h"
38 #include "AliRunLoader.h"
39 #include "AliGRPObject.h"
40 #include "AliCDBManager.h"
41 #include "AliCDBEntry.h"
42 #include "AliZDCSDigit.h"
43 #include "AliZDCDigit.h"
44 #include "AliZDCFragment.h"
45 #include "AliZDCv3.h"
46 #include "AliZDCDigitizer.h"
47
48 class AliCDBStorage;
49 class AliZDCPedestals;
50
51 ClassImp(AliZDCDigitizer)
52
53
54 //____________________________________________________________________________
55 AliZDCDigitizer::AliZDCDigitizer() :
56   fIsCalibration(0), 
57   fIsSignalInADCGate(kFALSE),
58   fFracLostSignal(0.),
59   fPedData(0), 
60   fSpectators2Track(kFALSE),
61   fBeamEnergy(0.)
62 {
63   // Default constructor    
64
65 }
66
67 //____________________________________________________________________________
68 AliZDCDigitizer::AliZDCDigitizer(AliRunDigitizer* manager):
69   AliDigitizer(manager),
70   fIsCalibration(0), //By default the simulation doesn't create calib. data
71   fIsSignalInADCGate(kFALSE),
72   fFracLostSignal(0.),
73   fPedData(GetPedData()), 
74   fSpectators2Track(kFALSE),
75   fBeamEnergy(0.)
76 {
77   // Get calibration data
78   if(fIsCalibration!=0) printf("\n\t AliZDCDigitizer -> Creating calibration data (pedestals)\n");
79   for(Int_t i=0; i<6; i++){
80     for(Int_t j=0; j<5; j++)
81        fPMGain[i][j] = 0.;
82   }
83 }
84
85 //____________________________________________________________________________
86 AliZDCDigitizer::~AliZDCDigitizer()
87 {
88 // Destructor
89 // Not implemented
90 }
91
92
93 //____________________________________________________________________________
94 AliZDCDigitizer::AliZDCDigitizer(const AliZDCDigitizer &digitizer):
95   AliDigitizer(),
96   fIsCalibration(digitizer.fIsCalibration),
97   fIsSignalInADCGate(digitizer.fIsSignalInADCGate),
98   fFracLostSignal(digitizer.fFracLostSignal),
99   fPedData(digitizer.fPedData),
100   fSpectators2Track(digitizer.fSpectators2Track),
101   fBeamEnergy(digitizer.fBeamEnergy)
102 {
103   // Copy constructor
104
105   for(Int_t i=0; i<6; i++){
106      for(Int_t j=0; j<5; j++){
107         fPMGain[i][j]   = digitizer.fPMGain[i][j];           
108      }
109   }
110   for(Int_t i=0; i<2; i++) fADCRes[i] = digitizer.fADCRes[i];
111
112
113 }
114
115 //____________________________________________________________________________
116 Bool_t AliZDCDigitizer::Init()
117 {
118   // Initialize the digitizer
119   
120   AliCDBEntry*  entry = AliCDBManager::Instance()->Get("GRP/GRP/Data");
121   AliGRPObject* grpData = 0x0;
122   if(entry){
123     TMap* m = dynamic_cast<TMap*>(entry->GetObject());  // old GRP entry
124     if(m){
125       //m->Print();
126       grpData = new AliGRPObject();
127       grpData->ReadValuesFromMap(m);
128     }
129     else{
130       grpData = dynamic_cast<AliGRPObject*>(entry->GetObject());  // new GRP entry
131     }
132     entry->SetOwner(0);
133     AliCDBManager::Instance()->UnloadFromCache("GRP/GRP/Data");
134   }
135   if(!grpData) AliError("No GRP entry found in OCDB!");
136   
137   TString beamType = grpData->GetBeamType();
138   if(beamType==AliGRPObject::GetInvalidString()){
139     AliError("\t UNKNOWN beam type from GRP obj -> PMT gains not set in ZDC digitizer!!!\n");
140   }
141   
142   fBeamEnergy = grpData->GetBeamEnergy();
143   if(fBeamEnergy==AliGRPObject::GetInvalidFloat()){
144     AliWarning("GRP/GRP/Data entry:  missing value for the beam energy ! Using 0.");
145     AliError("\t UNKNOWN beam type from GRP obj -> PMT gains not set in ZDC digitizer!!!\n");
146     fBeamEnergy = 0.;
147   }
148
149   if((beamType.CompareTo("P-P")) == 0 || (beamType.CompareTo("p-p")) == 0){
150     //PTM gains rescaled to beam energy for p-p
151     if(fBeamEnergy != 0){
152       for(Int_t j = 0; j < 5; j++){
153         fPMGain[0][j] = (661.444/fBeamEnergy+0.000740671)*10000000;
154         fPMGain[1][j] = (864.350/fBeamEnergy+0.002344)*10000000;
155         fPMGain[2][j] = (1.32312-0.000101515*fBeamEnergy)*10000000;
156         fPMGain[3][j] = fPMGain[0][j];
157         fPMGain[4][j] = fPMGain[1][j] ;
158       }
159       AliInfo(Form("    PMT gains for p-p @ %1.0f+%1.0f GeV: ZN(%1.0f), ZP(%1.0f), ZEM(%1.0f)\n",
160         fBeamEnergy, fBeamEnergy, fPMGain[0][0], fPMGain[1][0], fPMGain[2][0]));
161     }
162   }
163   else if((beamType.CompareTo("A-A")) == 0){
164     // PTM gains for Pb-Pb @ 2.7+2.7 A TeV ***************
165     // rescaled for Pb-Pb @ 1.38+1.38 A TeV ***************
166     Float_t scalGainFactor = fBeamEnergy/2760.;
167     for(Int_t j = 0; j < 5; j++){
168       fPMGain[0][j] = 50000./scalGainFactor;
169       fPMGain[1][j] = 100000./scalGainFactor;
170       fPMGain[2][j] = 100000./scalGainFactor;
171       fPMGain[3][j] = 50000./scalGainFactor;
172       fPMGain[4][j] = 100000./scalGainFactor;
173       fPMGain[5][j] = 100000./scalGainFactor;
174     }
175     AliInfo(Form("    PMT gains for Pb-Pb @ %1.0f+%1.0f A GeV: ZN(%1.0f), ZP(%1.0f), ZEM(%1.0f)\n",
176         fBeamEnergy, fBeamEnergy, fPMGain[0][0], fPMGain[1][0], fPMGain[2][0]));
177   }
178     
179   // ADC Caen V965
180   fADCRes[0] = 0.0000008; // ADC Resolution high gain: 200 fC/adcCh
181   fADCRes[1] = 0.0000064; // ADC Resolution low gain:  25  fC/adcCh
182
183   return kTRUE;
184 }
185
186 //____________________________________________________________________________
187 void AliZDCDigitizer::Exec(Option_t* /*option*/)
188 {
189   // Execute digitization
190
191   // ------------------------------------------------------------
192   // !!! 2nd ZDC set added 
193   // *** 1st 3 arrays are digits from REAL (simulated) hits
194   // *** last 2 are copied from simulated digits
195   // --- pm[0][...] = light in ZN side C  [C, Q1, Q2, Q3, Q4]
196   // --- pm[1][...] = light in ZP side C [C, Q1, Q2, Q3, Q4]
197   // --- pm[2][...] = light in ZEM [x, 1, 2, x, x]
198   // --- pm[3][...] = light in ZN side A [C, Q1, Q2, Q3, Q4] ->NEW!
199   // --- pm[4][...] = light in ZP side A [C, Q1, Q2, Q3, Q4] ->NEW!
200   // ------------------------------------------------------------
201   Float_t pm[5][5]; 
202   for(Int_t iSector1=0; iSector1<5; iSector1++) 
203     for(Int_t iSector2=0; iSector2<5; iSector2++){
204       pm[iSector1][iSector2] = 0;
205     }
206     
207   // ------------------------------------------------------------
208   // ### Out of time ADC added (22 channels)
209   // --- same codification as for signal PTMs (see above)
210   // ------------------------------------------------------------
211   Float_t pmoot[5][5];
212   for(Int_t iSector1=0; iSector1<5; iSector1++) 
213     for(Int_t iSector2=0; iSector2<5; iSector2++){
214       pmoot[iSector1][iSector2] = 0;
215     }
216
217   // impact parameter and number of spectators
218   Float_t impPar = -1;
219   Int_t specNTarg = 0, specPTarg = 0;
220   Int_t specNProj = 0, specPProj = 0;
221   Float_t signalTime0 = 0.;
222
223   // loop over input streams
224   for(Int_t iInput = 0; iInput<fManager->GetNinputs(); iInput++){
225
226     // get run loader and ZDC loader
227     AliRunLoader* runLoader = 
228       AliRunLoader::GetRunLoader(fManager->GetInputFolderName(iInput));
229     AliLoader* loader = runLoader->GetLoader("ZDCLoader");
230     if(!loader) continue;
231
232     // load sdigits
233     loader->LoadSDigits();
234     TTree* treeS = loader->TreeS();
235     if(!treeS) continue;
236     AliZDCSDigit sdigit;
237     AliZDCSDigit* psdigit = &sdigit;
238     treeS->SetBranchAddress("ZDC", &psdigit);
239
240     // loop over sdigits
241     for(Int_t iSDigit=0; iSDigit<treeS->GetEntries(); iSDigit++){
242       treeS->GetEntry(iSDigit);
243       //
244       if(!psdigit) continue;
245       if((sdigit.GetSector(1) < 0) || (sdigit.GetSector(1) > 4)){
246         AliError(Form("\nsector[0] = %d, sector[1] = %d\n", 
247                       sdigit.GetSector(0), sdigit.GetSector(1)));
248         continue;
249       }
250       // Checking if signal is inside ADC gate
251       if(iSDigit==0) signalTime0 = sdigit.GetTrackTime();
252       else{
253         // Assuming a signal lenght of 20 ns, signal is in gate if 
254         // signal ENDS in signalTime0+50. -> sdigit.GetTrackTime()+20<=signalTime0+50.
255         if(sdigit.GetTrackTime()<=signalTime0+30.) fIsSignalInADCGate = kTRUE;
256         if(sdigit.GetTrackTime()>signalTime0+30.){
257           fIsSignalInADCGate = kFALSE;
258           // Vedi quaderno per spiegazione approx. usata 
259           // nel calcolo della fraz. di segnale perso
260           fFracLostSignal = (sdigit.GetTrackTime()-30)*(sdigit.GetTrackTime()-30)/280.;
261         }
262       }
263       
264       Float_t sdSignal = sdigit.GetLightPM();
265       if(fIsSignalInADCGate == kFALSE){
266         AliDebug(2,Form("\t Signal time %f -> fraction %f of ZDC signal for det.(%d, %d) out of ADC gate\n",
267                 sdigit.GetTrackTime(),fFracLostSignal,sdigit.GetSector(0),sdigit.GetSector(1)));
268         sdSignal = (1-fFracLostSignal)*sdSignal;
269       }
270       
271       pm[(sdigit.GetSector(0))-1][sdigit.GetSector(1)] += sdigit.GetLightPM();
272       //Ch. debug
273       /*printf("\t Detector %d, Tower %d -> pm[%d][%d] = %.0f \n",
274           sdigit.GetSector(0), sdigit.GetSector(1),sdigit.GetSector(0)-1,
275           sdigit.GetSector(1), pm[sdigit.GetSector(0)-1][sdigit.GetSector(1)]); 
276       */
277       
278     }
279
280     loader->UnloadSDigits();
281
282     // get the impact parameter and the number of spectators in case of hijing
283     if(!runLoader->GetAliRun()) runLoader->LoadgAlice();
284     AliHeader* header = runLoader->GetHeader();
285     if(!header) continue;
286     AliGenEventHeader* genHeader = header->GenEventHeader();
287     if(!genHeader) continue;
288     if(!genHeader->InheritsFrom(AliGenHijingEventHeader::Class())) continue;
289     
290     if(fSpectators2Track==kTRUE){
291       impPar = ((AliGenHijingEventHeader*) genHeader)->ImpactParameter(); 
292       specNProj = ((AliGenHijingEventHeader*) genHeader)->ProjSpectatorsn();
293       specPProj = ((AliGenHijingEventHeader*) genHeader)->ProjSpectatorsp();
294       specNTarg = ((AliGenHijingEventHeader*) genHeader)->TargSpectatorsn();
295       specPTarg = ((AliGenHijingEventHeader*) genHeader)->TargSpectatorsp();
296       printf("\n\t AliZDCDigitizer: b = %1.2f fm\n"
297       " \t    PROJ.:  #spectator n %d, #spectator p %d\n"
298       " \t    TARG.:  #spectator n %d, #spectator p %d\n\n", 
299       impPar, specNProj, specPProj, specNTarg, specPTarg);
300     }
301     
302   }
303
304   // Applying fragmentation algorithm and adding spectator signal
305   if(fSpectators2Track==kTRUE && impPar) {
306     Int_t freeSpecNProj, freeSpecPProj;
307     Fragmentation(impPar, specNProj, specPProj, freeSpecNProj, freeSpecPProj);
308     Int_t freeSpecNTarg, freeSpecPTarg;
309     Fragmentation(impPar, specNTarg, specPTarg, freeSpecNTarg, freeSpecPTarg);
310     SpectatorSignal(1, freeSpecNProj, pm);
311     //printf("    AliZDCDigitizer -> Signal for %d PROJ free spectator n",freeSpecNProj);
312     SpectatorSignal(2, freeSpecPProj, pm);
313     //printf(" and %d free spectator p added\n",freeSpecPProj);
314     SpectatorSignal(3, freeSpecNTarg, pm);
315     //printf("    AliZDCDigitizer -> Signal for %d TARG free spectator n",freeSpecNTarg);
316     SpectatorSignal(4, freeSpecPTarg, pm);
317     //printf("and %d free spectator p added\n",freeSpecPTarg);
318   }
319
320
321   // get the output run loader and loader
322   AliRunLoader* runLoader = 
323     AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
324   AliLoader* loader = runLoader->GetLoader("ZDCLoader");
325   if(!loader) {
326     AliError("no ZDC loader found");
327     return;
328   }
329
330   // create the output tree
331   const char* mode = "update";
332   if(runLoader->GetEventNumber() == 0) mode = "recreate";
333   loader->LoadDigits(mode);
334   loader->MakeTree("D");
335   TTree* treeD = loader->TreeD();
336   AliZDCDigit digit;
337   AliZDCDigit* pdigit = &digit;
338   const Int_t kBufferSize = 4000;
339   treeD->Branch("ZDC", "AliZDCDigit", &pdigit, kBufferSize);
340
341   // Create digits
342   Int_t sector[2];
343   Int_t digi[2], digioot[2];
344   for(sector[0]=1; sector[0]<6; sector[0]++){
345     for(sector[1]=0; sector[1]<5; sector[1]++){
346         if((sector[0]==3) && ((sector[1]<1) || (sector[1]>2))) continue;
347         for(Int_t res=0; res<2; res++){
348            digi[res] = Phe2ADCch(sector[0], sector[1], pm[sector[0]-1][sector[1]], res) 
349                     + Pedestal(sector[0], sector[1], res);
350         }
351         new(pdigit) AliZDCDigit(sector, digi);
352         treeD->Fill();
353
354         //Ch. debug
355         //printf("\t DIGIT added -> det %d quad %d - digi[0,1] = [%d, %d]\n",
356         //     sector[0], sector[1], digi[0], digi[1]); // Chiara debugging!
357         
358     }
359   } // Loop over detector
360   // Adding in-time digits for 2 reference PTM signals (after signal ch.)
361   // (for the moment the ref. signal is completely invented assuming a PMgain of 5*10^4!)
362   Int_t sectorRef[2];
363   sectorRef[1] = 5;
364   Int_t sigRef[2];
365   // Reference signal are set to 100 (high gain chain) and 800 (low gain chain)
366   if(fIsCalibration==0) {sigRef[0]=100;  sigRef[1]=800;}
367   else {sigRef[0]=0;  sigRef[1]=0;} // calibration -> simulation of pedestal values
368   //
369   for(Int_t iref=0; iref<2; iref++){
370      sectorRef[0] = 3*iref+1;
371      for(Int_t res=0; res<2; res++){
372        sigRef[res] += Pedestal(sectorRef[0], sectorRef[1], res);
373      }
374      new(pdigit) AliZDCDigit(sectorRef, sigRef);
375      treeD->Fill();     
376      
377      //Ch. debug
378      //printf("\t RefDigit added -> det = %d, quad = %d - digi[0,1] = [%d, %d]\n",
379      //    sectorRef[0], sectorRef[1], sigRef[0], sigRef[1]); // Chiara debugging!     
380   }
381   //
382   // --- Adding digits for out-of-time channels after signal digits
383   for(sector[0]=1; sector[0]<6; sector[0]++){
384     for(sector[1]=0; sector[1]<5; sector[1]++){
385         if((sector[0]==3) && ((sector[1]<1) || (sector[1]>2))) continue;
386         for(Int_t res=0; res<2; res++){
387            digioot[res] = Pedestal(sector[0], sector[1], res); // out-of-time ADCs
388         }
389         new(pdigit) AliZDCDigit(sector, digioot);
390         treeD->Fill();
391
392         //Ch. debug
393         //printf("\t DIGIToot added -> det = %d, quad = %d - digi[0,1] = [%d, %d]\n",
394         //     sector[0], sector[1], digioot[0], digioot[1]); // Chiara debugging!      
395     }
396   }
397   // Adding out-of-time digits for 2 reference PTM signals (after out-of-time ch.)
398   Int_t sigRefoot[2];
399   for(Int_t iref=0; iref<2; iref++){
400      sectorRef[0] = 3*iref+1;
401      for(Int_t res=0; res<2; res++){
402        sigRefoot[res] = Pedestal(sectorRef[0], sectorRef[1], res);
403      }
404      new(pdigit) AliZDCDigit(sectorRef, sigRefoot);
405      treeD->Fill();
406      //Ch. debug
407      //printf("\t RefDigitoot added -> det = %d, quad = %d - digi[0,1] = [%d, %d]\n",
408      //    sectorRef[0], sectorRef[1], sigRefoot[0], sigRefoot[1]); // Chiara debugging!
409      
410   }
411   //printf("\t AliZDCDigitizer -> TreeD has %d entries\n",(Int_t) treeD->GetEntries());
412
413   // write the output tree
414   loader->WriteDigits("OVERWRITE");
415   loader->UnloadDigits();
416 }
417
418
419 //_____________________________________________________________________________
420 void AliZDCDigitizer::Fragmentation(Float_t impPar, Int_t specN, Int_t specP,
421                                     Int_t &freeSpecN, Int_t &freeSpecP) const
422 {
423 // simulate fragmentation of spectators
424
425   AliZDCFragment frag(impPar);
426
427   // Fragments generation
428   frag.GenerateIMF();
429   Int_t nAlpha = frag.GetNalpha();
430
431   // Attach neutrons
432   Int_t ztot = frag.GetZtot();
433   Int_t ntot = frag.GetNtot();
434   frag.AttachNeutrons();
435   freeSpecN = specN-ntot-2*nAlpha;
436   freeSpecP = specP-ztot-2*nAlpha;
437   // Removing deuterons
438   Int_t ndeu = (Int_t) (freeSpecN*frag.DeuteronNumber());
439   freeSpecN -= ndeu;
440   //
441   if(freeSpecN<0) freeSpecN=0;
442   if(freeSpecP<0) freeSpecP=0;
443   AliDebug(2, Form("FreeSpn = %d, FreeSpp = %d", freeSpecN, freeSpecP));
444 }
445
446 //_____________________________________________________________________________
447 void AliZDCDigitizer::SpectatorSignal(Int_t SpecType, Int_t numEvents, 
448                                       Float_t pm[5][5]) const
449 {
450 // add signal of the spectators
451   
452   TFile *specSignalFile = TFile::Open("$ALICE_ROOT/ZDC/SpectatorSignal.root");
453   if(!specSignalFile || !specSignalFile->IsOpen()) {
454     AliError((" Opening file $ALICE_ROOT/ZDC/SpectatorSignal.root failed\n"));
455     return;
456   }
457
458   TNtuple* zdcSignal;
459   
460   Float_t sqrtS = 2*fBeamEnergy;
461   //
462   if(TMath::Abs(sqrtS-5500) < 100.){
463     specSignalFile->cd("energy5500");
464     //
465     if(SpecType == 1) {    // --- Signal for projectile spectator neutrons
466       specSignalFile->GetObject("energy5500/ZNCSignal;1",zdcSignal);
467       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZNCSignal from SpectatorSignal.root file");
468     } 
469     else if(SpecType == 2) { // --- Signal for projectile spectator protons
470       specSignalFile->GetObject("energy5500/ZPCSignal;1",zdcSignal);
471       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZPCSignal from SpectatorSignal.root file");
472     }
473     else if(SpecType == 3) { // --- Signal for target spectator neutrons
474       specSignalFile->GetObject("energy5500/ZNASignal;1",zdcSignal);
475       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZNASignal from SpectatorSignal.root file");
476     }
477     else if(SpecType == 4) { // --- Signal for target spectator protons
478       specSignalFile->GetObject("energy5500/ZPASignal;1",zdcSignal);
479       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZPASignal from SpectatorSignal.root file");
480     }
481   }
482   else if(TMath::Abs(sqrtS-2760) < 100.){
483     specSignalFile->cd("energy2760");
484     //
485     if(SpecType == 1) {    // --- Signal for projectile spectator neutrons
486       specSignalFile->GetObject("energy2760/ZNCSignal;1",zdcSignal);
487       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZNCSignal from SpectatorSignal.root file");
488     } 
489     else if(SpecType == 2) { // --- Signal for projectile spectator protons
490       specSignalFile->GetObject("energy2760/ZPCSignal;1",zdcSignal);
491       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZPCSignal from SpectatorSignal.root file");
492     }
493     else if(SpecType == 3) { // --- Signal for target spectator neutrons
494       specSignalFile->GetObject("energy2760/ZNASignal;1",zdcSignal);
495       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZNASignal from SpectatorSignal.root file");
496     }
497     else if(SpecType == 4) { // --- Signal for target spectator protons
498       specSignalFile->GetObject("energy2760/ZPASignal;1",zdcSignal);
499       if(!zdcSignal) AliError("  PROBLEM!!! Can't retrieve ZPASignal from SpectatorSignal.root file");
500     }
501   }
502   
503   Int_t nentries = (Int_t) zdcSignal->GetEntries();
504   
505   Float_t *entry;
506   Int_t pl, i, k, iev=0, rnd[125], volume[2];
507   for(pl=0;pl<125;pl++) rnd[pl] = 0;
508   if(numEvents > 125) {
509     AliDebug(2,Form("numEvents (%d) is larger than 125", numEvents));
510     numEvents = 125;
511   }
512   for(pl=0;pl<numEvents;pl++){
513      rnd[pl] = (Int_t) (9999*gRandom->Rndm());
514      if(rnd[pl] >= 9999) rnd[pl] = 9998;
515      //printf(" rnd[%d] = %d\n",pl,rnd[pl]);     
516   }
517   // Sorting vector in ascending order with C function QSORT 
518   qsort((void*)rnd,numEvents,sizeof(Int_t),comp);
519   do{
520      for(i=0; i<nentries; i++){  
521         zdcSignal->GetEvent(i);
522         entry = zdcSignal->GetArgs();
523         if(entry[0] == rnd[iev]){
524           for(k=0; k<2; k++) volume[k] = (Int_t) entry[k+1];
525           //
526           Float_t lightQ = entry[7];
527           Float_t lightC = entry[8];
528           //
529           if(volume[0] != 3) {  // ZN or ZP
530             pm[volume[0]-1][0] += lightC;
531             pm[volume[0]-1][volume[1]] += lightQ;
532             //printf("\n   pm[%d][0] = %.0f, pm[%d][%d] = %.0f\n",(volume[0]-1),pm[volume[0]-1][0],
533             //  (volume[0]-1),volume[1],pm[volume[0]-1][volume[1]]);
534           } 
535           else { 
536             if(volume[1] == 1) pm[2][1] += lightC; // ZEM 1
537             else               pm[2][2] += lightQ; // ZEM 2
538             //printf("\n   pm[2][1] = %.0f, pm[2][2] = %.0f\n",pm[2][1],pm[2][2]);
539           }
540         }
541         else if(entry[0] > rnd[iev]){
542           iev++;
543           continue;
544         }
545      }
546   }while(iev<numEvents);
547   
548   specSignalFile->Close();
549   delete specSignalFile;
550 }
551
552
553 //_____________________________________________________________________________
554 Int_t AliZDCDigitizer::Phe2ADCch(Int_t Det, Int_t Quad, Float_t Light, 
555                                  Int_t Res) const
556 {
557   // Evaluation of the ADC channel corresponding to the light yield Light
558   Int_t vADCch = (Int_t) (Light * fPMGain[Det-1][Quad] * fADCRes[Res]);
559   // Ch. debug
560   //printf("\t Phe2ADCch -> det %d quad %d - PMGain[%d][%d] %1.0f phe %1.2f  ADC %d\n", 
561   //    Det,Quad,Det-1,Quad,fPMGain[Det-1][Quad],Light,vADCch);
562
563   return vADCch;
564 }
565
566 //_____________________________________________________________________________
567 Int_t AliZDCDigitizer::Pedestal(Int_t Det, Int_t Quad, Int_t Res) const
568 {
569   // Returns a pedestal for detector det, PM quad, channel with res.
570   //
571   Float_t pedValue;
572   // Normal run
573   if(fIsCalibration == 0){
574     Int_t index=0, kNch=24;
575     if(Quad!=5){
576       if(Det==1)        index = Quad+kNch*Res;    // ZNC
577       else if(Det==2)   index = (Quad+5)+kNch*Res;  // ZPC
578       else if(Det==3)   index = (Quad+9)+kNch*Res;  // ZEM
579       else if(Det==4)   index = (Quad+12)+kNch*Res; // ZNA
580       else if(Det==5)   index = (Quad+17)+kNch*Res; // ZPA
581     }
582     else index = (Det-1)/3+22+kNch*Res; // Reference PMs
583     //
584     Float_t meanPed = fPedData->GetMeanPed(index);
585     Float_t pedWidth = fPedData->GetMeanPedWidth(index);
586     pedValue = gRandom->Gaus(meanPed,pedWidth);
587     //
588     /*printf("\t  AliZDCDigitizer::Pedestal -> det %d quad %d res %d - Ped[%d] = %d\n",
589         Det, Quad, Res, index,(Int_t) pedValue); // Chiara debugging!
590     */
591   }
592   // To create calibration object
593   else{
594     if(Res == 0) pedValue = gRandom->Gaus((35.+10.*gRandom->Rndm()),(0.5+0.2*gRandom->Rndm())); //High gain
595     else  pedValue = gRandom->Gaus((250.+100.*gRandom->Rndm()),(3.5+2.*gRandom->Rndm())); //Low gain
596   }
597
598   return (Int_t) pedValue;
599 }
600
601 //_____________________________________________________________________________
602 AliCDBStorage* AliZDCDigitizer::SetStorage(const char *uri) 
603 {
604
605   Bool_t deleteManager = kFALSE;
606   
607   AliCDBManager *manager = AliCDBManager::Instance();
608   AliCDBStorage *defstorage = manager->GetDefaultStorage();
609   
610   if(!defstorage || !(defstorage->Contains("ZDC"))){ 
611      AliWarning("No default storage set or default storage doesn't contain ZDC!");
612      manager->SetDefaultStorage(uri);
613      deleteManager = kTRUE;
614   }
615  
616   AliCDBStorage *storage = manager->GetDefaultStorage();
617
618   if(deleteManager){
619     AliCDBManager::Instance()->UnsetDefaultStorage();
620     defstorage = 0;   // the storage is killed by AliCDBManager::Instance()->Destroy()
621   }
622
623   return storage; 
624 }
625
626 //_____________________________________________________________________________
627 AliZDCPedestals* AliZDCDigitizer::GetPedData() const
628 {
629
630   // Getting pedestal calibration object for ZDC set
631
632   AliCDBEntry  *entry = AliCDBManager::Instance()->Get("ZDC/Calib/Pedestals");
633   if(!entry) AliFatal("No calibration data loaded!");  
634
635   AliZDCPedestals *calibdata = dynamic_cast<AliZDCPedestals*>  (entry->GetObject());
636   if(!calibdata)  AliFatal("Wrong calibration object in calibration  file!");
637
638   return calibdata;
639 }
640