]> git.uio.no Git - u/mrichter/AliRoot.git/blob - VZERO/AliVZERODigitizer.cxx
Compilation warning fixed.
[u/mrichter/AliRoot.git] / VZERO / AliVZERODigitizer.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 /// This class constructs Digits out of Hits
21 ///
22 ///
23
24 // --- Standard library ---
25
26 // --- ROOT system ---
27 #include <TMath.h>
28 #include <TTree.h>
29 #include <TMap.h>
30 #include <TGeoManager.h>
31 #include <TGeoPhysicalNode.h>
32 #include <AliGeomManager.h>
33 #include <TRandom.h>
34 #include <TF1.h>
35 #include <TH1F.h>
36
37 // --- AliRoot header files ---
38 #include "AliRun.h"
39 #include "AliVZERO.h"
40 #include "AliVZEROhit.h"
41 #include "AliRunLoader.h"
42 #include "AliLoader.h"
43 #include "AliGRPObject.h"
44 #include "AliRunDigitizer.h"
45 #include "AliCDBManager.h"
46 #include "AliCDBStorage.h"
47 #include "AliCDBEntry.h"
48 #include "AliVZEROCalibData.h"
49 #include "AliCTPTimeParams.h"
50 #include "AliVZEROdigit.h"
51 #include "AliVZERODigitizer.h"
52 #include "AliVZEROSDigit.h"
53
54 ClassImp(AliVZERODigitizer)
55
56  AliVZERODigitizer::AliVZERODigitizer()
57                    :AliDigitizer(),
58                     fCalibData(GetCalibData()),
59                     fPhotoCathodeEfficiency(0.18),
60                     fNdigits(0),
61                     fDigits(0),
62                     fSignalShape(NULL),
63                     fPMResponse(NULL),
64                     fSinglePhESpectrum(NULL),
65                     fEvenOrOdd(kFALSE),
66                     fTask(kHits2Digits),
67                     fVZERO(NULL)
68 {
69   // default constructor
70   // Initialize OCDB and containers used in the digitization
71
72   Init();
73 }
74
75 //____________________________________________________________________________ 
76   AliVZERODigitizer::AliVZERODigitizer(AliVZERO *vzero, DigiTask_t task)
77                     :AliDigitizer(),
78                      fCalibData(GetCalibData()),
79                      fPhotoCathodeEfficiency(0.18),
80                      fNdigits(0),
81                      fDigits(0),
82                      fSignalShape(NULL),
83                      fPMResponse(NULL),
84                      fSinglePhESpectrum(NULL),
85                      fEvenOrOdd(kFALSE),
86                      fTask(task),
87                      fVZERO(vzero)
88 {
89   // constructor
90   // Initialize OCDB and containers used in the digitization
91
92   Init();
93 }
94            
95 //____________________________________________________________________________ 
96   AliVZERODigitizer::AliVZERODigitizer(AliRunDigitizer* manager)
97                     :AliDigitizer(manager),
98                      fCalibData(GetCalibData()),
99                      fPhotoCathodeEfficiency(0.18),
100                      fNdigits(0),
101                      fDigits(0),
102                      fSignalShape(NULL),
103                      fPMResponse(NULL),
104                      fSinglePhESpectrum(NULL),
105                      fEvenOrOdd(kFALSE),
106                      fTask(kHits2Digits),
107                      fVZERO(NULL)
108 {
109   // constructor
110   // Initialize OCDB and containers used in the digitization
111
112   Init();
113 }
114            
115 //____________________________________________________________________________ 
116   AliVZERODigitizer::~AliVZERODigitizer()
117 {
118   // destructor
119   
120   if (fDigits) {
121     fDigits->Delete();
122     delete fDigits;
123     fDigits=0; 
124   }
125
126   if (fSignalShape) {
127     delete fSignalShape;
128     fSignalShape = NULL;
129   }
130   if (fPMResponse) {
131     delete fPMResponse;
132     fPMResponse = NULL;
133   }
134   if (fSinglePhESpectrum) {
135     delete fSinglePhESpectrum;
136     fSinglePhESpectrum = NULL;
137   }
138
139   for(Int_t i = 0 ; i < 64; ++i) {
140     if (fTime[i]) delete [] fTime[i];
141   }
142 }
143
144 //_____________________________________________________________________________
145 Bool_t AliVZERODigitizer::Init()
146 {
147   // Initialises the digitizer
148   // Initialize OCDB and containers used in the digitization
149
150   // check if the digitizer was already initialized
151   if (fSignalShape) return kTRUE;
152
153   fSignalShape = new TF1("VZEROSignalShape",this,&AliVZERODigitizer::SignalShape,0,200,6,"AliVZERODigitizer","SignalShape");
154   //  fSignalShape->SetParameters(0,1.57345e1,-4.25603e-1,2.9,6.40982,3.69339e-01);
155   //  fSignalShape->SetParameters(1.34330e+00,1.13007e+02,-4.95705e-01,
156   //                          3.68911e+00,1.01040e+00, 3.94675e-01);
157   fSignalShape->SetParameters(-1.07335e+00,2.16002e+01,-1.26133e-01,
158                               1.41619e+00,5.50334e-01,3.86111e-01);
159   fPMResponse = new TF1("VZEROPMResponse",this,&AliVZERODigitizer::PMResponse,-kPMRespTime,2.*kPMRespTime,0,"AliVZERODigitizer","PMResponse");
160   fSinglePhESpectrum = new TF1("VZEROSinglePhESpectrum",this,&AliVZERODigitizer::SinglePhESpectrum,0,20,0,"AliVZERODigitizer","SinglePhESpectrum");
161   
162   // Now get the CTP L0->L1 delay
163   AliCDBEntry *entry = AliCDBManager::Instance()->Get("GRP/CTP/CTPtiming");
164   if (!entry) AliFatal("CTP timing parameters are not found in OCDB !");
165   AliCTPTimeParams *ctpParams = (AliCTPTimeParams*)entry->GetObject();
166   Float_t l1Delay = (Float_t)ctpParams->GetDelayL1L0()*25.0;
167
168   AliCDBEntry *entry1 = AliCDBManager::Instance()->Get("GRP/CTP/TimeAlign");
169   if (!entry1) AliFatal("CTP time-alignment is not found in OCDB !");
170   AliCTPTimeParams *ctpTimeAlign = (AliCTPTimeParams*)entry1->GetObject();
171   l1Delay += ((Float_t)ctpTimeAlign->GetDelayL1L0()*25.0);
172
173   AliCDBEntry *entry2 = AliCDBManager::Instance()->Get("VZERO/Calib/TimeDelays");
174   if (!entry2) AliFatal("VZERO time delays are not found in OCDB !");
175   TH1F *delays = (TH1F*)entry2->GetObject();
176
177   for(Int_t i = 0 ; i < 64; ++i) {
178
179     for(Int_t j = 0; j < kNClocks; ++j) fAdc[i][j] = 0;
180     fLeadingTime[i] = fTimeWidth[i] = 0;
181
182     fPmGain[i] = fCalibData->GetGain(i);
183
184     fAdcPedestal[i][0] = fCalibData->GetPedestal(i);
185     fAdcSigma[i][0]    = fCalibData->GetSigma(i); 
186     fAdcPedestal[i][1] = fCalibData->GetPedestal(i+64);
187     fAdcSigma[i][1]    = fCalibData->GetSigma(i+64); 
188
189     Int_t board = AliVZEROCalibData::GetBoardNumber(i);
190     fNBins[i] = TMath::Nint(((Float_t)(fCalibData->GetMatchWindow(board)+1)*25.0+
191                              (Float_t)kMaxTDCWidth*fCalibData->GetWidthResolution(board))/
192                             fCalibData->GetTimeResolution(board));
193     fNBinsLT[i] = TMath::Nint(((Float_t)(fCalibData->GetMatchWindow(board)+1)*25.0)/
194                               fCalibData->GetTimeResolution(board));
195     fBinSize[i] = fCalibData->GetTimeResolution(board);
196     fHptdcOffset[i] = (((Float_t)fCalibData->GetTriggerCountOffset(board)-
197                         (Float_t)fCalibData->GetRollOver(board))*25.0+
198                        fCalibData->GetTimeOffset(i)-
199                        l1Delay+
200                        delays->GetBinContent(i+1)+
201                        kV0Offset);
202
203     fTime[i] = new Float_t[fNBins[i]];
204     memset(fTime[i],0,fNBins[i]*sizeof(Float_t));
205   }
206
207   return kTRUE;
208 }
209
210 //____________________________________________________________________________
211 void AliVZERODigitizer::Exec(Option_t* /*option*/) 
212 {   
213   // Creates digits from hits
214   fNdigits = 0;  
215
216   if (fVZERO && !fManager) {
217     AliLoader *loader = fVZERO->GetLoader();
218     if (!loader) {
219       AliError("Can not get VZERO Loader via AliVZERO object!");
220       return;
221     }
222     AliRunLoader* runLoader = AliRunLoader::Instance();
223     for (Int_t iEvent = 0; iEvent < runLoader->GetNumberOfEvents(); ++iEvent) {
224       runLoader->GetEvent(iEvent);
225       if (fTask == kHits2Digits) {
226         DigitizeHits();
227         DigitizeSDigits();
228         WriteDigits(loader);
229       }
230       else {
231         DigitizeHits();
232         WriteSDigits(loader);
233       }
234     }
235   }
236   else if (fManager) {
237       ReadSDigits();
238       DigitizeSDigits();
239       AliRunLoader *currentLoader = AliRunLoader::GetRunLoader(fManager->GetOutputFolderName());
240       AliLoader *loader = currentLoader->GetLoader("VZEROLoader");
241       if (!loader) { 
242         AliError("Cannot get VZERO Loader via RunDigitizer!");
243         return;
244       }
245       WriteDigits(loader);
246   }
247   else {
248     AliFatal("Invalid digitization task! Exiting!");
249   }
250 }
251
252 //____________________________________________________________________________
253 void AliVZERODigitizer::AddDigit(Int_t pmnumber, Float_t time, Float_t width, Bool_t integrator, Short_t *chargeADC, Int_t *labels) 
254  { 
255  
256 // Adds Digit 
257  
258   TClonesArray &ldigits = *fDigits;  
259          
260   new(ldigits[fNdigits++]) AliVZEROdigit(pmnumber,time,width,integrator,chargeADC,labels);
261          
262 }
263 //____________________________________________________________________________
264 void AliVZERODigitizer::AddSDigit(Int_t pmnumber, Int_t nbins, Float_t *charges, Int_t *labels) 
265  { 
266  
267 // Adds SDigit 
268  
269   TClonesArray &ldigits = *fDigits;  
270          
271   new(ldigits[fNdigits++]) AliVZEROSDigit(pmnumber,nbins,charges,labels);
272          
273 }
274 //____________________________________________________________________________
275 void AliVZERODigitizer::ResetDigits()
276 {
277
278 // Clears Digits
279
280   fNdigits = 0;
281   if (fDigits) fDigits->Clear();
282 }
283
284 //____________________________________________________________________________
285 AliVZEROCalibData* AliVZERODigitizer::GetCalibData() const
286
287 {
288   AliCDBManager *man = AliCDBManager::Instance();
289
290   AliCDBEntry *entry=0;
291
292   entry = man->Get("VZERO/Calib/Data");
293
294 //   if(!entry){
295 //     AliWarning("Load of calibration data from default storage failed!");
296 //     AliWarning("Calibration data will be loaded from local storage ($ALICE_ROOT)");
297 //     Int_t runNumber = man->GetRun();
298 //     entry = man->GetStorage("local://$ALICE_ROOT/OCDB")
299 //       ->Get("VZERO/Calib/Data",runNumber);
300 //      
301 //   }
302
303   // Retrieval of data in directory VZERO/Calib/Data:
304
305
306   AliVZEROCalibData *calibdata = 0;
307
308   if (entry) calibdata = (AliVZEROCalibData*) entry->GetObject();
309   if (!calibdata)  AliFatal("No calibration data from calibration database !");
310
311   return calibdata;
312
313 }
314
315 double AliVZERODigitizer::SignalShape(double *x, double *par)
316 {
317   // this function simulates the time
318   // of arrival of the photons at the
319   // photocathode
320   Double_t xx = x[0];
321   if (xx <= par[0]) return 0;
322   Double_t a = 1./TMath::Power((xx-par[0])/par[1],1./par[2]);
323   if (xx <= par[3]) return a;
324   Double_t b = 1./TMath::Power((xx-par[3])/par[4],1./par[5]);
325   Double_t f = a*b/(a+b);
326   AliDebug(100,Form("x=%f func=%f",xx,f));
327   return f;
328 }
329
330 double AliVZERODigitizer::PMResponse(double *x, double * /* par */)
331 {
332   // this function describes the
333   // PM time response to a single
334   // photoelectron
335   Double_t xx = x[0]+kPMRespTime;
336   return xx*xx*TMath::Exp(-xx*xx/(kPMRespTime*kPMRespTime));
337 }
338
339 double AliVZERODigitizer::SinglePhESpectrum(double *x, double * /* par */)
340 {
341   // this function describes the
342   // PM amplitude response to a single
343   // photoelectron
344   Double_t xx = x[0];
345   if (xx < 0) return 0;
346   return (TMath::Poisson(xx,kPMNbOfSecElec)+kPMTransparency*TMath::Poisson(xx,1.0));
347 }
348
349 Int_t AliVZERODigitizer::Cell2Pmt(Int_t cell) const
350 {
351   // The method maps the scintillator
352   // indexes to the PM ones
353   if (cell < 0 || cell >= 80) {
354     AliError(Form("Wrong VZERO cell index %d",cell));
355     return -1;
356   }
357   if (cell < 16) return cell;
358   if (cell < 48) return 8 + cell/2;
359   return cell - 16;
360 }
361
362 void AliVZERODigitizer::DigitizeHits()
363 {
364   // Digitize the hits to the level of
365   // SDigits (fTime arrays)
366
367   for(Int_t i = 0 ; i < 64; ++i) {
368     memset(fTime[i],0,fNBins[i]*sizeof(Float_t));
369     fLabels[i][0] = fLabels[i][1] = fLabels[i][2] = -1;
370   }
371   Float_t integral = fPMResponse->Integral(-kPMRespTime,2.*kPMRespTime);
372   Float_t meansPhE = fSinglePhESpectrum->Mean(0,20);
373
374      AliLoader* loader = fVZERO->GetLoader();
375      if (!loader) {
376        AliError("Can not get VZERO Loader!");
377        return;
378      }
379      loader->LoadHits();
380      TTree* treeH = loader->TreeH();
381      if (!treeH) {
382        AliError("Cannot get TreeH!");
383        return;
384      }
385      TClonesArray* hits = fVZERO->Hits();
386
387      //     Float_t lightYieldCorr[64] = {0.00707,0.00517,0.00520,0.00537,0.00735,0.00537,0.00733,0.00605,0.00778,0.00749,0.00701,0.00755,0.00732,0.00617,0.00669,0.00525,0.00752,0.00820,0.00797,0.01107,0.01080,0.00889,0.00880,0.01712,0.00866,0.00701,0.00811,0.00602,0.00879,0.00821,0.00861,0.01433,0.00061,0.00032,0.00099,0.00061,0.00034,0.00046,0.00031,0.00122,0.00155,0.00091,0.00032,0.00096,0.00120,0.00067,0.00113,0.00060,0.00158,0.00136,0.00340,0.00066,0.00076,0.00119,0.00129,0.00147,0.00137,0.00117,0.00088,0.00164,0.00128,0.00081,0.00121,0.00250};
388      Float_t lightYieldCorr[64] = {0.01173,0.00874,0.00878,0.00886,0.01151,0.00925,0.01167,0.00983,0.01181,0.01243,0.01115,0.01220,0.01228,0.01053,0.01021,0.00930,0.01270,0.01411,0.01316,0.01894,0.01923,0.01860,0.01738,0.00305,0.01584,0.01251,0.01344,0.00310,0.01302,0.01266,0.01407,0.00338,0.00089,0.00100,0.00130,0.00081,0.00052,0.01230,0.00059,0.02452,0.02980,0.00137,0.01421,0.00116,0.00141,0.00092,0.02480,0.00096,0.00182,0.00174,0.00218,0.00106,0.00116,0.00160,0.00162,0.03097,0.00194,0.00171,0.00132,0.00239,0.00173,0.00118,0.00163,0.00262};
389 //  Now makes Digits from hits
390      Int_t nTracks = (Int_t) treeH->GetEntries();
391      for (Int_t iTrack = 0; iTrack < nTracks; iTrack++) {
392          fVZERO->ResetHits();
393          treeH->GetEvent(iTrack);
394          Int_t nHits = hits->GetEntriesFast();
395          for (Int_t iHit = 0; iHit < nHits; iHit++) {
396            AliVZEROhit* hit = (AliVZEROhit *)hits->UncheckedAt(iHit);
397            Int_t nPhot = hit->Nphot();
398            Int_t cell  = hit->Cell();                          
399            Int_t pmt = Cell2Pmt(cell);
400            Int_t trackLabel = hit->GetTrack();
401            for(Int_t l = 0; l < 3; ++l) {
402              if (fLabels[pmt][l] < 0) {
403                fLabels[pmt][l] = trackLabel;
404                break;
405              }
406            }
407            Float_t dt_scintillator = gRandom->Gaus(0,kIntTimeRes);
408            Float_t t = dt_scintillator + 1e9*hit->Tof();
409            if (pmt < 32) t += kV0CDelayCables;
410            t += fHptdcOffset[pmt];
411            Int_t nPhE;
412            Float_t prob = lightYieldCorr[pmt]*fPhotoCathodeEfficiency; // Optical losses included!
413            if (nPhot > 100)
414              nPhE = (Int_t)gRandom->Gaus(prob*Float_t(nPhot)+0.5,
415                                          sqrt(Float_t(nPhot)*prob*(1.-prob)));
416            else
417              nPhE = gRandom->Binomial(nPhot,prob);
418            Float_t charge = TMath::Qe()*fPmGain[pmt]*fBinSize[pmt]/integral;
419            for (Int_t iPhE = 0; iPhE < nPhE; ++iPhE) {
420              Float_t tPhE = t + fSignalShape->GetRandom(0,fBinSize[pmt]*Float_t(fNBins[pmt]));
421              Float_t gainVar = fSinglePhESpectrum->GetRandom(0,20)/meansPhE;
422              Int_t firstBin = TMath::Max(0,(Int_t)((tPhE-kPMRespTime)/fBinSize[pmt]));
423              Int_t lastBin = TMath::Min(fNBins[pmt]-1,(Int_t)((tPhE+2.*kPMRespTime)/fBinSize[pmt]));
424              for(Int_t iBin = firstBin; iBin <= lastBin; ++iBin) {
425                Float_t tempT = fBinSize[pmt]*(0.5+iBin)-tPhE;
426                fTime[pmt][iBin] += gainVar*charge*fPMResponse->Eval(tempT);
427              }
428            }         // ph.e. loop
429          }           // hit loop
430      }               // track loop
431      loader->UnloadHits();
432 }
433
434
435 void AliVZERODigitizer::DigitizeSDigits()
436 {
437   // Digitize the fTime arrays (SDigits) to the level of
438   // Digits (fAdc arrays)
439   for(Int_t i = 0 ; i < 64; ++i) {
440     for(Int_t j = 0; j < kNClocks; ++j) fAdc[i][j] = 0;
441     fLeadingTime[i] = fTimeWidth[i] = 0;
442   }
443
444   Float_t maximum = 0.9*fSignalShape->GetMaximum(0,200); // Not exact, one needs to do this on the convoluted
445   Float_t integral2 = fSignalShape->Integral(0,200); // function. Anyway the effect is small <10% on the 2.5 ADC thr
446   for (Int_t ipmt = 0; ipmt < 64; ++ipmt) {
447     Float_t thr = fCalibData->GetDiscriThr(ipmt)*kChargePerADC*maximum*fBinSize[ipmt]/integral2;
448     Bool_t ltFound = kFALSE, ttFound = kFALSE;
449     for (Int_t iBin = 0; iBin < fNBins[ipmt]; ++iBin) {
450       Float_t t = fBinSize[ipmt]*Float_t(iBin);
451       if (fTime[ipmt][iBin] > thr) {
452         if (!ltFound && (iBin < fNBinsLT[ipmt])) {
453           ltFound = kTRUE;
454           fLeadingTime[ipmt] = t;
455         }
456       }
457       else {
458         if (ltFound) {
459           if (!ttFound) {
460             ttFound = kTRUE;
461             fTimeWidth[ipmt] = t - fLeadingTime[ipmt];
462           }
463         }
464       }
465       Float_t tadc = t - kClockOffset - fCalibData->GetTimeOffset(ipmt);
466       Int_t clock = kNClocks/2 - Int_t(tadc/25.0);
467       if (clock >= 0 && clock < kNClocks)
468         fAdc[ipmt][clock] += fTime[ipmt][iBin]/kChargePerADC;
469     }
470     Int_t board = AliVZEROCalibData::GetBoardNumber(ipmt);
471     if (ltFound && ttFound) {
472       fTimeWidth[ipmt] = fCalibData->GetWidthResolution(board)*
473         Float_t(Int_t(fTimeWidth[ipmt]/fCalibData->GetWidthResolution(board)));
474       if (fTimeWidth[ipmt] < Float_t(kMinTDCWidth)*fCalibData->GetWidthResolution(board))
475         fTimeWidth[ipmt] = Float_t(kMinTDCWidth)*fCalibData->GetWidthResolution(board);
476       if (fTimeWidth[ipmt] > Float_t(kMaxTDCWidth)*fCalibData->GetWidthResolution(board))
477         fTimeWidth[ipmt] = Float_t(kMaxTDCWidth)*fCalibData->GetWidthResolution(board);
478     }
479   }
480
481   fEvenOrOdd = gRandom->Integer(2);
482   for (Int_t j=0; j<64; ++j){
483     for (Int_t iClock = 0; iClock < kNClocks; ++iClock) {
484       Int_t integrator = (iClock + fEvenOrOdd) % 2;
485       fAdc[j][iClock]  += gRandom->Gaus(fAdcPedestal[j][integrator], fAdcSigma[j][integrator]);
486     }
487   }
488         
489 }
490
491 void AliVZERODigitizer::WriteDigits(AliLoader *loader)
492 {
493   // Take fAdc arrays filled by the previous
494   // method and produce and add digits to the digit Tree
495
496   loader->LoadDigits("UPDATE");
497
498   if (!loader->TreeD()) loader->MakeTree("D");
499   loader->MakeDigitsContainer();
500   TTree* treeD  = loader->TreeD();
501   DigitsArray();
502   treeD->Branch("VZERODigit", &fDigits); 
503   
504   Short_t *chargeADC = new Short_t[kNClocks];
505   for (Int_t i=0; i<64; i++) {      
506     for (Int_t j = 0; j < kNClocks; ++j) {
507       Int_t tempadc = Int_t(fAdc[i][j]);
508       if (tempadc > 1023) tempadc = 1023;
509       chargeADC[j] = tempadc;
510     }
511     AddDigit(i, fLeadingTime[i], fTimeWidth[i], Bool_t((10+fEvenOrOdd)%2), chargeADC, fLabels[i]);
512   }
513   delete [] chargeADC;
514
515   treeD->Fill();
516   loader->WriteDigits("OVERWRITE");  
517   loader->UnloadDigits();     
518   ResetDigits();
519 }
520
521 void AliVZERODigitizer::WriteSDigits(AliLoader *loader)
522 {
523   // Take fTime arrays filled by the previous
524   // method and produce and add sdigits to the sdigit Tree
525
526   loader->LoadSDigits("UPDATE");
527
528   if (!loader->TreeS()) loader->MakeTree("S");
529   loader->MakeSDigitsContainer();
530   TTree* treeS  = loader->TreeS();
531   SDigitsArray();
532   treeS->Branch("VZEROSDigit", &fDigits); 
533   
534   for (Int_t ipmt = 0; ipmt < 64; ++ipmt) {
535     AddSDigit(ipmt,fNBins[ipmt],fTime[ipmt],fLabels[ipmt]);
536   }
537
538   treeS->Fill();
539   loader->WriteSDigits("OVERWRITE");  
540   loader->UnloadSDigits();     
541   ResetDigits();
542 }
543
544 void AliVZERODigitizer::ReadSDigits()
545 {
546   // Read SDigits which are then to precessed
547   // in the following method
548   for(Int_t i = 0 ; i < 64; ++i) {
549     memset(fTime[i],0,fNBins[i]*sizeof(Float_t));
550     fLabels[i][0] = fLabels[i][1] = fLabels[i][2] = -1;
551   }
552
553   // Loop over input files
554   Int_t nFiles= fManager->GetNinputs();
555   for (Int_t inputFile = 0; inputFile < nFiles; inputFile++) {
556     // Get the current loader 
557     AliRunLoader* currentLoader = 
558       AliRunLoader::GetRunLoader(fManager->GetInputFolderName(inputFile));
559
560     AliLoader *loader = currentLoader->GetLoader("VZEROLoader");
561     loader->LoadSDigits("READ");
562   
563     // Get the tree of summable digits
564     TTree* sdigitsTree = loader->TreeS();
565     if (!sdigitsTree)  {
566       AliError("No sdigit tree from manager");
567       continue;
568     }
569
570     // Get the branch 
571     TBranch* sdigitsBranch = sdigitsTree->GetBranch("VZEROSDigit");
572     if (!sdigitsBranch) {
573       AliError("Failed to get sdigit branch");
574       return;
575     }
576
577     // Set the branch address
578     TClonesArray *sdigitsArray = NULL;
579     sdigitsBranch->SetAddress(&sdigitsArray);
580
581     // Sum contributions from the sdigits
582     // Get number of entries in the tree 
583     Int_t nentries  = Int_t(sdigitsBranch->GetEntries());
584     for (Int_t entry = 0; entry < nentries; ++entry)  {
585       sdigitsBranch->GetEntry(entry);
586       // Get the number of sdigits 
587       Int_t nsdigits = sdigitsArray->GetEntries();
588       for (Int_t sdigit = 0; sdigit < nsdigits; sdigit++) {
589         AliVZEROSDigit* sDigit = static_cast<AliVZEROSDigit*>(sdigitsArray->UncheckedAt(sdigit));
590         Int_t pmNumber = sDigit->PMNumber();
591         Int_t nbins = sDigit->GetNBins();
592         if (nbins != fNBins[pmNumber]) {
593           AliError(Form("Incompatible number of bins between digitizer (%d) and sdigit (%d) for PM %d! Skipping sdigit!",
594                         fNBins[pmNumber],nbins,pmNumber));
595           continue;
596         }
597         // Sum the charges
598         Float_t *charges = sDigit->GetCharges();
599         for(Int_t iBin = 0; iBin < nbins; ++iBin) fTime[pmNumber][iBin] += charges[iBin];
600         // and the labels
601         Int_t *labels = sDigit->GetTracks();
602         Int_t j = 0;
603         for(Int_t i = 0; i < 3; ++i) {
604           if (fLabels[pmNumber][i] < 0) {
605             if (labels[j] < 0) break;
606             fLabels[pmNumber][i] = labels[j];
607             j++;
608           }
609         }
610       }
611     }
612     loader->UnloadSDigits();
613   }
614 }
615
616 //____________________________________________________________________
617 TClonesArray*
618 AliVZERODigitizer::DigitsArray() 
619 {
620   // Initialize digit array if not already and
621   // return pointer to it. 
622   if (!fDigits) { 
623     fDigits = new TClonesArray("AliVZEROdigit", 64);
624     fNdigits = 0;
625   }
626   return fDigits;
627 }
628
629 //____________________________________________________________________
630 TClonesArray*
631 AliVZERODigitizer::SDigitsArray() 
632 {
633   // Initialize sdigit array if not already and
634   // return pointer to it. 
635   if (!fDigits) { 
636     fDigits = new TClonesArray("AliVZEROSDigit", 64);
637     fNdigits = 0;
638   }
639   return fDigits;
640 }