]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALDigitizer.cxx
Set the name as EMCALDigitizer and set the folder name from the runloader
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALDigitizer.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 //////////////////////////////////////////////////////////////////////////////
21 // Class performs digitization of Summable digits 
22 //  
23 // In addition it performs mixing of summable digits from different events.
24 //
25 // For each event two branches are created in TreeD:
26 //   "EMCAL" - list of digits
27 //   "AliEMCALDigitizer" - AliEMCALDigitizer with all parameters used in digitization
28 //
29 // Note, that one cset title for new digits branch, and repeat digitization with
30 // another set of parameters.
31 //
32 // Examples of use:
33 // root[0] AliEMCALDigitizer * d = new AliEMCALDigitizer() ;
34 // root[1] d->ExecuteTask()             
35 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
36 //                       //Digitizes SDigitis in all events found in file galice.root 
37 //
38 // root[2] AliEMCALDigitizer * d1 = new AliEMCALDigitizer("galice1.root") ;  
39 //                       // Will read sdigits from galice1.root
40 // root[3] d1->MixWith("galice2.root")       
41 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
42 //                       // Reads another portion of sdigits from galice2.root
43 // root[3] d1->MixWith("galice3.root")       
44 //                       // Reads another portion of sdigits from galice3.root
45 // root[4] d->ExecuteTask("deb timing")    
46 //                       // Reads SDigits from files galice1.root, galice2.root ....
47 //                       // mixes them and stores produced Digits in file galice1.root          
48 //                       // deb - prints number of produced digits
49 //                       // deb all - prints list of produced digits
50 //                       // timing  - prints time used for digitization
51 ////////////////////////////////////////////////////////////////////////////////////
52 //
53 //*-- Author: Sahal Yacoob (LBL)
54 // based on : AliEMCALDigitizer
55 // Modif: 
56 //  August 2002 Yves Schutz: clone PHOS as closely as possible and intoduction
57 //                           of new  IO (à la PHOS)
58 ///_________________________________________________________________________________
59
60 // --- ROOT system ---
61 #include "TFile.h"
62 #include "TTree.h"
63 #include "TSystem.h"
64 #include "TROOT.h"
65 #include "TFolder.h"
66 #include "TObjString.h"
67 #include "TGeometry.h"
68 #include "TBenchmark.h"
69
70 // --- Standard library ---
71
72 // --- AliRoot header files ---
73 #include "AliRun.h"
74 #include "AliHeader.h"
75 #include "AliStream.h"
76 #include "AliRunDigitizer.h"
77 #include "AliEMCALDigit.h"
78 #include "AliEMCAL.h"
79 #include "AliEMCALGetter.h"
80 #include "AliEMCALDigitizer.h"
81 #include "AliEMCALSDigitizer.h"
82 #include "AliEMCALGeometry.h"
83 #include "AliEMCALTick.h"
84
85 ClassImp(AliEMCALDigitizer)
86
87
88 //____________________________________________________________________________ 
89   AliEMCALDigitizer::AliEMCALDigitizer():AliDigitizer("",""),
90                                        fInput(0),
91                                        fInputFileNames(0x0),
92                                        fEventNames(0x0)
93 {
94   // ctor
95   InitParameters() ; 
96   fDefaultInit = kTRUE ; 
97   fManager = 0 ;                     // We work in the standalong mode
98   fEventFolderName = "" ; 
99 }
100
101 //____________________________________________________________________________ 
102 AliEMCALDigitizer::AliEMCALDigitizer(const TString alirunFileName, const TString eventFolderName):
103   AliDigitizer("EMCAL"+AliConfig::fgkDigitizerTaskName, alirunFileName),
104   fInputFileNames(0), fEventNames(0), fEventFolderName(eventFolderName)
105 {
106   // ctor
107
108   InitParameters() ; 
109   Init() ;
110   fDefaultInit = kFALSE ; 
111   fManager = 0 ;                     // We work in the standalong mode
112 }
113
114 //____________________________________________________________________________ 
115 AliEMCALDigitizer::AliEMCALDigitizer(const AliEMCALDigitizer & d) : AliDigitizer(d)
116 {
117   // copyy ctor 
118
119   SetName(d.GetName()) ; 
120   SetTitle(d.GetTitle()) ; 
121   fDigitThreshold = d.fDigitThreshold ; 
122   fMeanPhotonElectron = d.fMeanPhotonElectron ; 
123   fPedestal           = d.fPedestal ; 
124   fSlope              = d.fSlope ; 
125   fPinNoise           = d.fPinNoise ; 
126   fTimeResolution     = d.fTimeResolution ; 
127   fTimeThreshold      = d.fTimeThreshold ; 
128   fTimeSignalLength   = d.fTimeSignalLength ; 
129   fADCchannelEC       = d.fADCchannelEC ; 
130   fADCpedestalEC      = d.fADCpedestalEC ; 
131   fNADCEC             = d.fNADCEC ;
132   fEventFolderName    = d.fEventFolderName;
133  }
134
135 //____________________________________________________________________________ 
136 AliEMCALDigitizer::AliEMCALDigitizer(AliRunDigitizer * rd):
137  AliDigitizer(rd,"EMCAL"+AliConfig::fgkDigitizerTaskName),
138  fEventFolderName(0)
139 {
140   // ctor
141   fManager = rd ; 
142   fEventFolderName = fManager->GetInputFolderName(0) ;
143   SetTitle(dynamic_cast<AliStream*>(fManager->GetInputStream(0))->GetFileName(0));
144   InitParameters() ; 
145   Init() ; 
146   fDefaultInit = kFALSE ; 
147 }
148
149 //____________________________________________________________________________ 
150   AliEMCALDigitizer::~AliEMCALDigitizer()
151 {
152   // dtor
153   AliEMCALGetter * gime =AliEMCALGetter::Instance(GetTitle(),fEventFolderName);
154   gime->EmcalLoader()->CleanDigitizer();
155   delete [] fInputFileNames ; 
156   delete [] fEventNames ; 
157
158 }
159
160 //____________________________________________________________________________
161 void AliEMCALDigitizer::Digitize(const Int_t event) 
162
163
164   // Makes the digitization of the collected summable digits
165   // for this it first creates the array of all EMCAL modules
166   // filled with noise (different for EMC, CPV and PPSD) and
167   // after that adds contributions from SDigits. This design 
168   // helps to avoid scanning over the list of digits to add 
169   // contribution of any new SDigit.
170
171   AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle(), fEventFolderName) ; 
172   TClonesArray * digits = gime->Digits() ; 
173   digits->Clear() ;
174
175   const AliEMCALGeometry *geom = gime->EMCALGeometry() ; 
176
177   //Making digits from noise first
178   Int_t nEMC = 0 ;
179   nEMC = geom->GetNPhi()*geom->GetNZ(); //max number of digits possible
180   
181   Int_t absID ;
182
183   digits->Expand(nEMC) ;
184
185   // get first the sdigitizer from the tasks list (must have same name as the digitizer)
186   if ( !gime->SDigitizer() ) 
187     gime->LoadSDigitizer();
188   AliEMCALSDigitizer * sDigitizer = gime->SDigitizer(); 
189   
190   if ( !sDigitizer )
191     Fatal("Digitize", "SDigitizer with name %s %s not found", fEventFolderName.Data(), GetTitle() ) ; 
192
193   //take all the inputs to add together and load the SDigits
194   TObjArray * sdigArray = new TObjArray(fInput) ;
195   sdigArray->AddAt(gime->SDigits(), 0) ;
196   Int_t i ;
197   for(i = 1 ; i < fInput ; i++){
198     TString tempo(fEventNames[i]) ; 
199     tempo += i ;
200     AliEMCALGetter * gime = AliEMCALGetter::Instance(fInputFileNames[i], tempo) ; 
201     gime->Event(event,"S");
202     sdigArray->AddAt(gime->SDigits(), i) ;
203   }
204   
205   //Find the first tower with signal
206   Int_t nextSig = nEMC + 1 ; 
207   TClonesArray * sdigits ;  
208   for(i = 0 ; i < fInput ; i++){
209     sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
210     if ( !sdigits->GetEntriesFast() )
211       continue ; 
212     Int_t curNext = dynamic_cast<AliEMCALDigit *>(sdigits->At(0))->GetId() ;
213      if(curNext < nextSig) 
214        nextSig = curNext ;
215   }
216
217   TArrayI index(fInput) ;
218   index.Reset() ;  //Set all indexes to zero
219
220   AliEMCALDigit * digit ;
221   AliEMCALDigit * curSDigit ;
222
223   TClonesArray * ticks = new TClonesArray("AliEMCALTick",1000) ;
224
225   //Put Noise contribution
226   for(absID = 1; absID <= nEMC; absID++){
227     Float_t amp = 0 ;
228     // amplitude set to zero, noise will be added later
229     new((*digits)[absID-1]) AliEMCALDigit( -1, -1, absID, 0, TimeOfNoise() ) ;
230     //look if we have to add signal?
231     digit = dynamic_cast<AliEMCALDigit *>(digits->At(absID-1)) ;
232     
233     if(absID==nextSig){
234       //Add SDigits from all inputs    
235       ticks->Clear() ;
236       Int_t contrib = 0 ;
237       Float_t a = digit->GetAmp() ;
238       Float_t b = TMath::Abs( a /fTimeSignalLength) ;
239       //Mark the beginning of the signal
240       new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime(),0, b);  
241       //Mark the end of the signal     
242       new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime()+fTimeSignalLength, -a, -b);
243       
244       // loop over input
245       for(i = 0; i< fInput ; i++){  //loop over (possible) merge sources
246         if(dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
247           curSDigit = dynamic_cast<AliEMCALDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;      
248         else
249           curSDigit = 0 ;
250         //May be several digits will contribute from the same input
251         while(curSDigit && (curSDigit->GetId() == absID)){         
252           //Shift primary to separate primaries belonging different inputs
253           Int_t primaryoffset ;
254           if(fManager)
255             primaryoffset = fManager->GetMask(i) ; 
256           else
257             primaryoffset = i ;
258           curSDigit->ShiftPrimary(primaryoffset) ;
259           
260           a = curSDigit->GetAmp() ;
261           b = a /fTimeSignalLength ;
262           new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime(),0, b);  
263           new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b); 
264
265           *digit = *digit + *curSDigit ;  //add energies
266
267           index[i]++ ;
268           if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
269             curSDigit = dynamic_cast<AliEMCALDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;            
270           else
271             curSDigit = 0 ;
272         }
273       }
274       // add fluctuations for photo-electron creation
275       amp = sDigitizer->Calibrate(digit->GetAmp()) ; // GeV
276       amp *= static_cast<Float_t>(gRandom->Poisson(fMeanPhotonElectron)) / static_cast<Float_t>(fMeanPhotonElectron) ;
277   
278       //calculate and set time
279       Float_t time = FrontEdgeTime(ticks) ;
280       digit->SetTime(time) ;
281
282       //Find next signal module
283       nextSig = nEMC + 1 ;
284       for(i = 0 ; i < fInput ; i++){
285         sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
286         Int_t curNext = nextSig ;
287         if(sdigits->GetEntriesFast() > index[i] ){
288           curNext = dynamic_cast<AliEMCALDigit *>(sdigits->At(index[i]))->GetId() ;       
289         }
290         if(curNext < nextSig) nextSig = curNext ;
291       }
292     }
293     // add the noise now
294     amp += TMath::Abs(gRandom->Gaus(0., fPinNoise)) ;
295     digit->SetAmp(sDigitizer->Digitize(amp)) ;  
296   }
297   
298   ticks->Delete() ;
299   delete ticks ;
300
301   delete sdigArray ; //We should not delete its contents
302
303   //remove digits below thresholds
304   for(i = 0 ; i < nEMC ; i++){
305     digit = dynamic_cast<AliEMCALDigit*>( digits->At(i) ) ;
306     Float_t threshold = fDigitThreshold ; 
307     if(sDigitizer->Calibrate( digit->GetAmp() ) < threshold)
308       digits->RemoveAt(i) ;
309     else 
310       digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
311   }
312   
313   digits->Compress() ;  
314   
315   Int_t ndigits = digits->GetEntriesFast() ; 
316   digits->Expand(ndigits) ;
317   
318   //Set indexes in list of digits
319   for (i = 0 ; i < ndigits ; i++) { 
320     digit = dynamic_cast<AliEMCALDigit *>( digits->At(i) ) ; 
321     digit->SetIndexInList(i) ; 
322     Float_t energy = sDigitizer->Calibrate(digit->GetAmp()) ;
323     digit->SetAmp(DigitizeEnergy(energy) ) ;
324   }
325 }
326
327 //____________________________________________________________________________
328
329 Int_t AliEMCALDigitizer::DigitizeEnergy(Float_t energy)
330
331   Int_t channel = -999; 
332   channel = static_cast<Int_t> (TMath::Ceil( (energy + fADCpedestalEC)/fADCchannelEC ))  ;
333   if(channel > fNADCEC ) 
334     channel =  fNADCEC ;   
335   return channel ;
336 }
337
338 //____________________________________________________________________________
339 void AliEMCALDigitizer::Exec(Option_t *option) 
340
341   // Managing method
342
343   if (!fInit) { // to prevent overwrite existing file
344     Error( "Exec", "Give a version name different from %s", fEventFolderName.Data() ) ;
345     return ;
346   } 
347
348   if (strstr(option,"print")) {
349     Print();
350     return ; 
351   }
352   
353   if(strstr(option,"tim"))
354     gBenchmark->Start("EMCALDigitizer");
355
356   if (fManager) 
357     fInput = fManager->GetNinputs() ;
358   
359   AliEMCALGetter * gime = AliEMCALGetter::Instance() ;
360   
361   Int_t nevents = gime->MaxEvent() ;;
362   
363   Int_t ievent ;
364
365   for(ievent = 0; ievent < nevents; ievent++){
366     
367     gime->Event(ievent,"S") ; 
368
369     Digitize(ievent) ; //Add prepared SDigits to digits and add the noise
370
371     WriteDigits() ;
372
373     if(strstr(option,"deb"))
374       PrintDigits(option);
375
376     //increment the total number of Digits per run 
377     fDigitsInRun += gime->Digits()->GetEntriesFast() ;  
378   }
379   
380   if(strstr(option,"tim")){
381     gBenchmark->Stop("EMCALDigitizer");
382     printf("Exec: took %f seconds for Digitizing %f seconds per event", 
383          gBenchmark->GetCpuTime("EMCALDigitizer"), gBenchmark->GetCpuTime("EMCALDigitizer")/nevents ) ;
384   } 
385 }
386
387 //____________________________________________________________________________ 
388 Float_t AliEMCALDigitizer::FrontEdgeTime(TClonesArray * ticks) 
389
390   //  Returns the shortest time among all time ticks
391
392   ticks->Sort() ; //Sort in accordance with times of ticks
393   TIter it(ticks) ;
394   AliEMCALTick * ctick = (AliEMCALTick *) it.Next() ;
395   Float_t time = ctick->CrossingTime(fTimeThreshold) ;    
396   
397   AliEMCALTick * t ;  
398   while((t=(AliEMCALTick*) it.Next())){
399     if(t->GetTime() < time)  //This tick starts before crossing
400       *ctick+=*t ;
401     else
402       return time ;
403     
404     time = ctick->CrossingTime(fTimeThreshold) ;    
405   }
406   return time ;
407 }
408
409 //____________________________________________________________________________ 
410 Bool_t AliEMCALDigitizer::Init()
411 {
412   // Makes all memory allocations
413   fInit = kTRUE ; 
414   AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle(), fEventFolderName) ;  
415   if ( gime == 0 ) {
416     Error("Init", "Could not obtain the Getter object for file %s and event %s !", GetTitle(), fEventFolderName.Data()) ;   
417     return kFALSE;
418   } 
419   
420   TString opt("Digits") ; 
421   if(gime->VersionExists(opt) ) { 
422     Error( "Init", "Give a version name different from %s", fEventFolderName.Data() ) ;
423     fInit = kFALSE ; 
424   }
425
426   // Post Digitizer to the white board
427   gime->PostDigitizer(this) ;
428  
429   if(fManager)
430     fInput = fManager->GetNinputs() ; 
431   else 
432     fInput           = 1 ;
433
434   fInputFileNames  = new TString[fInput] ; 
435   fEventNames      = new TString[fInput] ; 
436   fInputFileNames[0] = GetTitle() ; 
437   fEventNames[0]     = fEventFolderName.Data() ; 
438   Int_t index ; 
439   for (index = 1 ; index < fInput ; index++) {
440     fInputFileNames[index] = dynamic_cast<AliStream*>(fManager->GetInputStream(index))->GetFileName(0); 
441     TString tempo = fManager->GetInputFolderName(index) ;
442     fEventNames[index] = tempo.Remove(tempo.Length()-1) ; // strip of the stream number added bt fManager 
443   }
444   
445   //to prevent cleaning of this object while GetEvent is called
446   gime->EmcalLoader()->GetDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
447   
448   return fInit ;    
449 }
450
451 //____________________________________________________________________________ 
452 void AliEMCALDigitizer::InitParameters()
453 {
454   fMeanPhotonElectron = 1250 ; // electrons per GeV
455   fPinNoise           = 0.001 ; // noise equivalent GeV (random choice)
456   if (fPinNoise == 0. ) 
457     Warning("InitParameters", "No noise added\n") ; 
458   fDigitThreshold     = fPinNoise * 3; //2 sigma
459   fTimeResolution     = 0.5e-9 ;
460   fTimeSignalLength   = 1.0e-9 ;
461
462   fADCchannelEC    = 0.000220;                     // width of one ADC channel in GeV
463   fADCpedestalEC   = 0.005 ;                       // GeV
464   fNADCEC          = (Int_t) TMath::Power(2,16) ;  // number of channels in Tower ADC
465
466   fTimeThreshold      = 0.001*10000000 ; //Means 1 MeV in terms of SDigits amplitude
467  
468 }
469
470 //__________________________________________________________________
471 void AliEMCALDigitizer::MixWith(const TString alirunFileName, const TString eventFolderName)
472 {
473   // Allows to produce digits by superimposing background and signal event.
474   // It is assumed, that headers file with SIGNAL events is opened in 
475   // the constructor. 
476   // Sets the BACKGROUND event, with which the SIGNAL event is to be mixed 
477   // Thus we avoid writing (changing) huge and expensive 
478   // backgound files: all output will be writen into SIGNAL, i.e. 
479   // opened in constructor file. 
480   //
481   // One can open as many files to mix with as one needs.
482   // However only Sdigits with the same name (i.e. constructed with the same SDigitizer)
483   // can be mixed.
484
485   if( strcmp(GetName(), "") == 0 )
486     Init() ;
487   
488   if(fManager){
489     Error("MixWith", "Cannot use this method under AliRunDigitizer") ;
490     return ;
491   } 
492   // looking for file which contains AliRun
493   if (gSystem->AccessPathName(alirunFileName)) {// file does not exist
494     Error("MixWith", "File %s does not exist!", alirunFileName.Data()) ;
495     return ; 
496   }
497   // looking for the file which contains SDigits
498   AliEMCALGetter * gime = AliEMCALGetter::Instance() ; 
499   TString fileName( gime->GetSDigitsFileName() ) ; 
500     if ( eventFolderName != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
501       fileName = fileName.ReplaceAll(".root", "") + "_" + eventFolderName + ".root" ;
502     if ( (gSystem->AccessPathName(fileName)) ) { 
503       Error("MixWith", "The file %s does not exist!", fileName.Data()) ;
504       return ;
505     }
506     // need to increase the arrays
507     TString tempo = fInputFileNames[fInput-1] ; 
508     delete [] fInputFileNames ; 
509     fInputFileNames = new TString[fInput+1] ; 
510     fInputFileNames[fInput-1] = tempo ; 
511  
512     tempo = fEventNames[fInput-1] ; 
513     delete [] fEventNames ; 
514     fEventNames = new TString[fInput+1] ; 
515     fEventNames[fInput-1] = tempo ; 
516
517     fInputFileNames[fInput] = alirunFileName ; 
518     fEventNames[fInput]     = eventFolderName ;
519     fInput++ ;
520 }  
521  
522 //__________________________________________________________________
523 void AliEMCALDigitizer::Print()const 
524 {
525   // Print Digitizer's parameters
526   printf("Print: \n------------------- %s -------------", GetName() ) ; 
527   if( strcmp(fEventFolderName.Data(), "") != 0 ){
528     printf(" Writing Digits to branch with title  %s\n", fEventFolderName.Data()) ;
529     
530     Int_t nStreams ; 
531     if (fManager) 
532       nStreams =  GetNInputStreams() ;
533     else 
534       nStreams = fInput ; 
535     
536     Int_t index = 0 ;  
537     for (index = 0 ; index < nStreams ; index++) {  
538       TString tempo(fEventNames[index]) ; 
539       tempo += index ;
540       AliEMCALGetter * gime = AliEMCALGetter::Instance(fInputFileNames[index], tempo) ; 
541       TString fileName( gime->GetSDigitsFileName() ) ; 
542       if ( fEventNames[index] != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
543         fileName = fileName.ReplaceAll(".root", "") + "_" + fEventNames[index]  + ".root" ;
544       printf ("Adding SDigits from %s %s\n", fInputFileNames[index].Data(), fileName.Data()) ; 
545     }
546     AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle(), fEventFolderName) ; 
547     printf("\nWriting digits to %s", gime->GetDigitsFileName().Data()) ;
548     
549     printf("\nWith following parameters:\n") ;
550     
551     printf("    Electronics noise in EMC (fPinNoise) = %f\n", fPinNoise) ;
552     printf("    Threshold  in EMC  (fDigitThreshold) = %f\n", fDigitThreshold)  ;
553     printf("---------------------------------------------------\n")  ;
554   }
555   else
556     printf("Print: AliEMCALDigitizer not initialized") ; 
557 }
558
559 //__________________________________________________________________
560 void AliEMCALDigitizer::PrintDigits(Option_t * option){
561     
562   AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle(), fEventFolderName) ; 
563   TClonesArray * digits = gime->Digits() ;
564   
565   printf("PrintDigits: %d", digits->GetEntriesFast()) ; 
566   printf("\nevent %d", gAlice->GetEvNumber()) ;
567   printf("\n       Number of entries in Digits list %d", digits->GetEntriesFast() )  ;  
568   
569   if(strstr(option,"all")){  
570     //loop over digits
571     AliEMCALDigit * digit;
572     printf("\nEMC digits (with primaries):\n")  ;
573     printf("\n   Id  Amplitude    Time          Index Nprim: Primaries list \n") ;    
574     Int_t index ;
575     for (index = 0 ; index < digits->GetEntries() ; index++) {
576       digit = dynamic_cast<AliEMCALDigit *>(digits->At(index)) ;
577       printf("\n%6d  %8d    %6.5e %4d      %2d : ",
578               digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;  
579       Int_t iprimary;
580       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
581         printf("%d ",digit->GetPrimary(iprimary+1) ) ; 
582       }
583     }   
584   }
585 }
586
587 //__________________________________________________________________
588 Float_t AliEMCALDigitizer::TimeOfNoise(void)
589 {  // Calculates the time signal generated by noise
590   //to be rewritten, now returns just big number
591   return 1. ;
592
593 }
594
595 //__________________________________________________________________
596 void AliEMCALDigitizer::Unload() 
597 {  
598   // Unloads the SDigits and Digits
599   Int_t i ; 
600   for(i = 1 ; i < fInput ; i++){
601     TString tempo(fEventNames[i]) ; 
602     tempo += i ;
603     AliEMCALGetter * gime = AliEMCALGetter::Instance(fInputFileNames[i], tempo) ; 
604     gime->EmcalLoader()->UnloadSDigits() ; 
605   }
606   
607   AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle(), fEventFolderName) ; 
608   gime->EmcalLoader()->UnloadDigits() ; 
609 }
610
611 //_________________________________________________________________________________________
612 void AliEMCALDigitizer::WriteDigits()
613 {
614
615   // Makes TreeD in the output file. 
616   // Check if branch already exists: 
617   //   if yes, exit without writing: ROOT TTree does not support overwriting/updating of 
618   //      already existing branches. 
619   //   else creates branch with Digits, named "EMCAL", title "...",
620   //      and branch "AliEMCALDigitizer", with the same title to keep all the parameters
621   //      and names of files, from which digits are made.
622
623   AliEMCALGetter * gime = AliEMCALGetter::Instance(GetTitle(), fEventFolderName) ; 
624   const TClonesArray * digits = gime->Digits() ; 
625   TTree * treeD = gime->TreeD(); 
626
627   // -- create Digits branch
628   Int_t bufferSize = 32000 ;    
629   TBranch * digitsBranch = treeD->Branch("EMCAL",&digits,bufferSize);
630   digitsBranch->SetTitle(fEventFolderName);
631   digitsBranch->Fill() ;
632   
633   gime->WriteDigits("OVERWRITE");
634   gime->WriteDigitizer("OVERWRITE");
635
636   Unload() ; 
637
638 }
639