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