]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALDigitizer.cxx
Improved the mechanism to divert SDigits into a separate file.
[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 //_________________________________________________________________________________
56
57 // --- ROOT system ---
58 #include "TFile.h"
59 #include "TTree.h"
60 #include "TSystem.h"
61 #include "TROOT.h"
62 #include "TFolder.h"
63 #include "TObjString.h"
64 #include "TGeometry.h"
65 #include "TBenchmark.h"
66 // --- Standard library ---
67 #include <iomanip.h>
68
69 // --- AliRoot header files ---
70
71 #include "AliRun.h"
72 #include "AliHeader.h"
73 #include "AliEMCALDigit.h"
74 #include "AliEMCALHit.h"
75 #include "AliEMCALTick.h"
76 #include "AliEMCALv1.h"
77 #include "AliEMCALDigitizer.h"
78 #include "AliEMCALSDigitizer.h"
79 #include "AliEMCALGeometry.h"
80 #include "AliEMCALGetter.h"
81 #include "AliRunDigitizer.h"
82 ClassImp(AliEMCALDigitizer)
83
84
85 //____________________________________________________________________________ 
86   AliEMCALDigitizer::AliEMCALDigitizer()
87 {
88   // ctor
89
90   fSDigitizer = 0 ;
91   fNinputs = 0 ;
92   fPinNoise = 0.0 ;
93   fTowerDigitThreshold = 0.0 ;
94   fTimeResolution     = 0. ;
95   fTimeSignalLength   = 0. ;
96   fPreShowerDigitThreshold = 0. ;
97   fADCchannelTower = 0.0;      // width of one ADC channel in GeV
98   fADCpedestalTower = 0. ;      // pedestal of ADC
99   fNADCTower = 0;  // number of channels in Tower ADC
100
101   fADCchannelPreSho  = 0.0;          // width of one ADC channel in Pre Shower
102   fADCpedestalPreSho = 0.0 ;         // pedestal of ADC
103   fNADCPreSho = 0;      // number of channels in Pre Shower ADC
104   fTimeThreshold = 0.0; //Means 1 MeV in terms of SDigits amplitude
105   fManager = 0 ;
106   fSplitFile= 0 ; 
107
108
109
110 }
111 //____________________________________________________________________________ 
112 Bool_t AliEMCALDigitizer::Init()
113 {
114   // Makes all memory allocations
115
116   fSDigitizer = 0 ;
117   fNinputs = 1 ;
118   fPinNoise = 0.00001 ;
119   fTowerDigitThreshold = 0.001 ;
120   fTimeResolution     = 0.5e-9 ;
121   fTimeSignalLength   = 1.0e-9 ;
122   fPreShowerDigitThreshold = fTowerDigitThreshold/25. ;
123   fInitialized = kFALSE ;
124   fADCchannelTower = 0.000220;       // width of one ADC channel in GeV
125   fADCpedestalTower = 0.005 ;      // GeV
126   fNADCTower = (Int_t) TMath::Power(2,16) ;  // number of channels in Tower ADC
127
128   fADCchannelPreSho = 0.0000300;          // width of one ADC channel in Pre Shower
129   fADCpedestalPreSho = 0.005 ;         // 
130   fNADCPreSho = (Int_t) TMath::Power(2,12);      // number of channels in Pre ShowerADC
131
132   fTimeThreshold = 0.001*10000000 ; //Means 1 MeV in terms of SDigits amplitude
133  
134
135
136   if(fManager)
137     SetTitle("aliroot") ;
138   else if (strcmp(GetTitle(),"")==0) 
139     SetTitle("galice.root") ;
140   
141   if( strcmp(GetName(), "") == 0 )
142     SetName("Default") ;
143   
144   AliEMCALGetter * gime = AliEMCALGetter::GetInstance(GetTitle(), GetName(), "update") ; 
145   if ( gime == 0 ) {
146     cerr << "ERROR: AliEMCALDigitizer::Init -> Could not obtain the Getter object !" << endl ; 
147     return kFALSE;
148   } 
149   
150   //const AliEMCALGeometry * geom = gime->EMCALGeometry() ;
151   //fEmcCrystals = geom->GetNModules() *  geom->GetNCristalsInModule() ;
152   
153   // Post Digits to the white board
154   gime->PostDigits(GetName() ) ;   
155   
156   // Post Digitizer to the white board
157   gime->PostDigitizer(this) ;
158   
159   //Mark that we will use current header file
160   if(!fManager){
161     gime->PostSDigits(GetName(),GetTitle()) ;
162     gime->PostSDigitizer(GetName(),GetTitle()) ;
163   }
164   return kTRUE ;
165   
166  
167   
168 }
169
170 //____________________________________________________________________________ 
171 AliEMCALDigitizer::AliEMCALDigitizer(const char *headerFile,const char *name)
172 {
173   SetName(name) ;
174   SetTitle(headerFile) ;
175   fManager = 0 ;                     // We work in the standalong mode
176   fSplitFile= 0 ; 
177   Init() ;
178   
179
180   
181 }
182 //____________________________________________________________________________ 
183 AliEMCALDigitizer::AliEMCALDigitizer(AliRunDigitizer * ard):AliDigitizer(ard)
184 {
185   // ctor
186   SetName("");     //Will call init in the digitizing
187   SetTitle("aliroot") ;  
188 }
189
190 //____________________________________________________________________________ 
191   AliEMCALDigitizer::~AliEMCALDigitizer()
192 {
193   // dtor
194
195   if (fSplitFile)       
196     if ( fSplitFile->IsOpen() )
197       fSplitFile->Close() ;
198
199 }
200
201 //____________________________________________________________________________
202 void AliEMCALDigitizer::Reset() { 
203   //sets current event number to the first simulated event
204 if( strcmp(GetName(), "") == 0 )
205   Init() ;
206
207       // Int_t inputs ;
208       // for(inputs = 0; inputs < fNinputs ;inputs++)
209       //  fIevent->AddAt(-1, inputs ) ;
210   
211 }
212
213 //____________________________________________________________________________
214 void AliEMCALDigitizer::Digitize(const Int_t event) { 
215
216   // Makes the digitization of the collected summable digits
217   // for this it first creates the array of all EMCAL modules
218   // filled with noise (different for EMC, CPV and PPSD) and
219   // after that adds contributions from SDigits. This design 
220   // helps to avoid scanning over the list of digits to add 
221   // contribution of any new SDigit.
222
223   AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ; 
224   TClonesArray * digits = gime->Digits(GetName()) ; 
225   
226   digits->Clear() ;
227
228     const AliEMCALGeometry *geom = gime->EMCALGeometry() ; 
229
230
231   //Making digits with noise, first EMC
232   Int_t nEMC = 2*geom->GetNPhi()*geom->GetNZ();
233   Int_t absID ;
234   TString name      =  geom->GetName() ;
235
236  // get first the sdigitizer from the tasks list (must have same name as the digitizer)
237   const AliEMCALSDigitizer * sDigitizer = gime->SDigitizer(GetName()); 
238   if ( !sDigitizer) {
239     cerr << "ERROR: AliEMCALDigitizer::Digitize -> SDigitizer with name " << GetName() << " not found " << endl ; 
240     abort() ; 
241   }
242 // loop through the sdigits posted to the White Board and add them to the noise
243   TCollection * folderslist = gime->SDigitsFolder()->GetListOfFolders() ; 
244   TIter next(folderslist) ; 
245   TFolder * folder = 0 ; 
246   TClonesArray * sdigits = 0 ;
247   Int_t input = 0 ;
248   TObjArray * sdigArray = new TObjArray(2) ;
249   while ( (folder = (TFolder*)next()) ) 
250     if ( (sdigits = (TClonesArray*)folder->FindObject(GetName()) ) ) {
251       TString fileName(folder->GetName()) ;
252       fileName.ReplaceAll("_","/") ;
253       cout << "INFO: AliEMCALDigitizer::Digitize -> Adding SDigits " 
254            << GetName() << " from " << fileName << endl ; 
255       sdigArray->AddAt(sdigits, input) ;
256       input++ ;
257     }
258
259
260   //Find the first tower with signal
261   Int_t nextSig = 200000 ; 
262   Int_t i;
263   for(i=0; i<input; i++){
264     sdigits = (TClonesArray *)sdigArray->At(i) ;
265     if ( !sdigits->GetEntriesFast() )
266       continue ; 
267     Int_t curNext = ((AliEMCALDigit *)sdigits->At(0))->GetId() ;
268      if(curNext < nextSig) 
269        nextSig = curNext ;
270   }
271
272   TArrayI index(input) ;
273   index.Reset() ;  //Set all indexes to zero
274
275   AliEMCALDigit * digit = 0 ;
276   AliEMCALDigit * curSDigit = 0 ;
277
278   TClonesArray * ticks = new TClonesArray("AliEMCALTick",1000) ;
279
280   //Put Noise contribution
281   for(absID = 1; absID <= nEMC; absID++){
282     Float_t noise = gRandom->Gaus(0., fPinNoise); 
283     new((*digits)[absID-1]) AliEMCALDigit( -1, -1, absID,sDigitizer->Digitize(noise), TimeOfNoise() ) ;
284     //look if we have to add signal?
285     digit = (AliEMCALDigit *) digits->At(absID-1) ;
286     if(absID==nextSig){
287       //Add SDigits from all inputs    
288       ticks->Clear() ;
289       Int_t contrib = 0 ;
290       Float_t a = digit->GetAmp() ;
291       Float_t b = TMath::Abs( a /fTimeSignalLength) ;
292       //Mark the beginnign of the signal
293       new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime(),0, b);  
294       //Mark the end of the ignal     
295       new((*ticks)[contrib++]) AliEMCALTick(digit->GetTime()+fTimeSignalLength, -a, -b);
296       
297  // loop over input
298   
299       for(i = 0; i< input ; i++){  //loop over (possible) merge sources
300         if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
301           curSDigit = (AliEMCALDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;        
302         else
303           curSDigit = 0 ;
304         //May be several digits will contribute from the same input
305         while(curSDigit && curSDigit->GetId() == absID){           
306           //Shift primary to separate primaries belonging different inputs
307           Int_t primaryoffset ;
308           if(fManager)
309             primaryoffset = fManager->GetMask(i) ; 
310           else
311             primaryoffset = i ;
312           curSDigit->ShiftPrimary(primaryoffset) ;
313           
314           a = curSDigit->GetAmp() ;
315           b = a /fTimeSignalLength ;
316           new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime(),0, b);  
317           new((*ticks)[contrib++]) AliEMCALTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b); 
318
319           *digit = *digit + *curSDigit ;  //add energies
320
321           index[i]++ ;
322           if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
323             curSDigit = (AliEMCALDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;      
324           else
325             curSDigit = 0 ;
326         }
327       }
328
329 //calculate and set time
330       Float_t time = FrontEdgeTime(ticks) ;
331       digit->SetTime(time) ;
332
333       //Find next signal module
334       nextSig = 200000 ;
335       for(i=0; i<input; i++){
336         sdigits = ((TClonesArray *)sdigArray->At(i)) ;
337         Int_t curNext = nextSig ;
338         if(sdigits->GetEntriesFast() > index[i] ){
339           curNext = ((AliEMCALDigit *) sdigits->At(index[i]))->GetId() ;
340           
341         }
342         if(curNext < nextSig) nextSig = curNext ;
343       }
344     }
345   }
346   
347   ticks->Delete() ;
348   delete ticks ;
349
350
351
352
353   //remove digits below thresholds
354   for(absID = 0; absID < nEMC/2 ; absID++){
355
356     if(sDigitizer->Calibrate(((AliEMCALDigit*)digits->At(absID))->GetAmp()) < fTowerDigitThreshold)
357       digits->RemoveAt(absID) ;
358     else
359       digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
360   }
361   
362   for(absID = nEMC/2; absID < nEMC ; absID++){
363
364     if(sDigitizer->Calibrate(((AliEMCALDigit*)digits->At(absID))->GetAmp()) < fPreShowerDigitThreshold)
365       digits->RemoveAt(absID) ;
366     else
367       digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
368   }
369   
370   digits->Compress() ;  
371   
372   Int_t ndigits = digits->GetEntriesFast() ;
373   
374   digits->Expand(ndigits) ;
375   
376   
377   //Set indexes in list of digits
378   //Int_t i ;
379  for (i = 0 ; i < ndigits ; i++) { 
380     AliEMCALDigit * digit = (AliEMCALDigit *) digits->At(i) ; 
381     digit->SetIndexInList(i) ; 
382     Float_t energy = sDigitizer->Calibrate(digit->GetAmp()) ;
383     digit->SetAmp(DigitizeEnergy(energy,digit->GetId()) ) ;
384   }
385 }
386
387 //____________________________________________________________________________
388
389 Int_t AliEMCALDigitizer::DigitizeEnergy(Float_t energy, Int_t absId)
390
391   Int_t channel = -999;
392   Int_t nphi = AliEMCALGetter::GetInstance()->EMCALGeometry()->GetNPhi() ; 
393   Int_t nz   = AliEMCALGetter::GetInstance()->EMCALGeometry()->GetNZ() ;
394   
395   if(absId <= nphi*nz){  //digitize as tower
396     channel = static_cast<Int_t> (TMath::Ceil( (energy + fADCpedestalTower)/fADCchannelTower ))  ;
397   if(channel > fNADCTower ) 
398     channel =  fNADCTower ;
399   } else {
400     channel =  static_cast<Int_t>(TMath::Ceil( (energy + fADCpedestalPreSho)/fADCchannelPreSho ))  ;
401   if(channel > fNADCPreSho ) 
402     channel =  fNADCPreSho ;
403   }
404   
405   return channel ;
406 }
407
408 //____________________________________________________________________________
409 void AliEMCALDigitizer::Exec(Option_t *option) { 
410   // Managing method
411 if(strcmp(GetName(), "") == 0 )   
412     Init() ;
413   
414   if (strstr(option,"print")) {
415     Print("");
416     return ; 
417   }
418   
419   if(strstr(option,"tim"))
420     gBenchmark->Start("EMCALDigitizer");
421
422   AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ;
423   
424   Int_t nevents ;
425   
426   TTree * treeD ;
427   
428   if(fManager){
429     treeD = fManager->GetTreeD() ;
430     nevents = 1 ;    // Will process only one event
431   }
432   else {
433     gAlice->GetEvent(0) ;
434     nevents = (Int_t) gAlice->TreeE()->GetEntries() ;
435     treeD=gAlice->TreeD() ;
436   }
437  
438
439   //Check, if this branch already exits
440   if (treeD) {
441     TObjArray * lob = (TObjArray*)treeD->GetListOfBranches() ;
442     TIter next(lob) ; 
443     TBranch * branch = 0 ;  
444     Bool_t emcalfound = kFALSE, digitizerfound = kFALSE ; 
445     
446     while ( (branch = (TBranch*)next()) && (!emcalfound || !digitizerfound) ) {
447       if ( (strcmp(branch->GetName(), "EMCAL")==0) && 
448            (strcmp(branch->GetTitle(), GetName())==0) ) 
449         emcalfound = kTRUE ;
450       
451       else if ( (strcmp(branch->GetName(), "AliEMCALDigitizer")==0) && 
452                 (strcmp(branch->GetTitle(), GetName())==0) ) 
453         digitizerfound = kTRUE ; 
454     }
455     
456     if ( emcalfound ) {
457       cerr << "WARNING: AliEMCALDigitizer -> Digits branch with name " << GetName() 
458            << " already exits" << endl ;
459       return ; 
460     }   
461     if ( digitizerfound ) {
462       cerr << "WARNING: AliEMCALDigitizer -> Digitizer branch with name " << GetName() 
463            << " already exits" << endl ;
464       return ; 
465     }   
466   }
467   Int_t ievent ;
468
469   for(ievent = 0; ievent < nevents; ievent++){
470     
471     if(fManager){
472       Int_t input ;
473       for(input = 0 ; input < fManager->GetNinputs(); input ++){
474         TTree * treeS = fManager->GetInputTreeS(input) ;
475         if(!treeS){
476           cerr << "AliEMCALDigitizer -> No Input " << endl ;
477           return ;
478         }
479         gime->ReadTreeS(treeS,input) ;
480       }
481     }
482     else 
483       gime->Event(ievent,"S") ; 
484     
485     Digitize(ievent) ; //Add prepared SDigits to digits and add the noise
486     
487     WriteDigits(ievent) ;
488     
489     if(strstr(option,"deb"))
490       PrintDigits(option);
491     
492     //increment the total number of Digits per run 
493     fDigitsInRun += gime->Digits()->GetEntriesFast() ;  
494   }
495   
496   if (fSplitFile) 
497     if ( fSplitFile->IsOpen() ) 
498       fSplitFile->Close() ; 
499
500   if(strstr(option,"tim")){
501     gBenchmark->Stop("EMCALDigitizer");
502     cout << "AliEMCALDigitizer:" << endl ;
503     cout << "  took " << gBenchmark->GetCpuTime("EMCALDigitizer") << " seconds for Digitizing " 
504          <<  gBenchmark->GetCpuTime("EMCALDigitizer")/nevents << " seconds per event " << endl ;
505     cout << endl ;
506   }
507   
508 }
509
510
511
512 //__________________________________________________________________
513 void AliEMCALDigitizer::MixWith(char* headerFile){
514   // Alows produce digits by superimposing background and signal event.
515   // It is assumed, that headers file with SIGNAL events is opened in 
516   // constructor, and now we set the BACKGROUND event, with which we 
517   // will mix. Thus we avoid writing (changing) huge and expencive 
518   // backgound files: all output will be writen into SIGNAL, i.e. 
519   // opened in constructor file. 
520   //
521   // One can open as many files to mix with as one wants.
522
523 if( strcmp(GetName(), "") == 0 )
524     Init() ;
525   
526  if(fManager){
527     cout << "Can not use this method under AliRunDigitizer " << endl ;
528     return ;
529   } // check if the specified SDigits do not already exist on the White Board:
530   // //Folders/RunMC/Event/Data/EMCAL/SDigits/headerFile/sdigitsname
531
532   TString path = "Folders/RunMC/Event/Data/EMCAL/SDigits" ; 
533   path += headerFile ; 
534   path += "/" ; 
535   path += GetName() ;
536   if ( gROOT->FindObjectAny(path.Data()) ) {
537     cerr << "WARNING: AliEMCALDigitizer::MixWith -> Entry already exists, do not add" << endl ;
538     return;
539   }
540
541   AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ;
542   gime->PostSDigits(GetName(),headerFile) ;
543   
544   // check if the requested file is already open or exist and if SDigits Branch exist
545   TFile * file = (TFile*)gROOT->FindObject(headerFile); 
546   if ( !file ) { 
547     file = new TFile(headerFile, "READ") ; 
548     if (!file) { 
549       cerr << "ERROR: AliEMCALDigitizer::MixWith -> File " << headerFile << " does not exist!" << endl ; 
550       return ; 
551     }
552   }
553   
554 }
555  
556 //__________________________________________________________________
557 void AliEMCALDigitizer::Print(Option_t* option)const {
558   if( strcmp(GetName(), "") != 0) {
559     
560     cout << "------------------- "<< GetName() << " -------------" << endl ;
561     cout << "Digitizing sDigits from file(s): " <<endl ;
562     
563   TCollection * folderslist = ((TFolder*)gROOT->FindObjectAny("Folders/RunMC/Event/Data/EMCAL/SDigits"))->GetListOfFolders() ; 
564     TIter next(folderslist) ; 
565     TFolder * folder = 0 ;
566     while ( (folder = (TFolder*)next()) ) 
567       if ( folder->FindObject(GetName())  ) 
568         {
569         cout << "Adding SDigits " << GetName() << " from " << folder->GetName() << endl ; 
570       cout << endl ;
571       cout << "Writing digits to " << GetTitle() << endl ;
572       
573       cout << endl ;
574       cout << "With following parameters: " << endl ;
575       cout << "     Electronics noise in EMC (fPinNoise) = " << fPinNoise << endl ;
576       cout << "  Threshold  in EMC  (fTowerDigitThreshold) = " << fTowerDigitThreshold  << endl;
577       cout << "  Threshold  in PreShower  (fPreShowerDigitThreshold) = " << fPreShowerDigitThreshold  << endl ; ;
578       cout << "---------------------------------------------------" << endl ;
579     }
580     else
581       cout << "AliEMCALDigitizer not initialized " << endl ;
582     }
583 }
584
585 //__________________________________________________________________
586 void AliEMCALDigitizer::PrintDigits(Option_t * option){
587     
588   AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ; 
589   TClonesArray * fDigits = gime->Digits() ;
590
591   cout << "AliEMCALDigitiser:"<< endl ;
592   cout << "       Number of entries in Digits list " << fDigits->GetEntriesFast() << endl ;
593   cout << endl ;
594   if(strstr(option,"all")){
595     
596     //loop over digits
597     AliEMCALDigit * digit;
598     cout << "Digit Id " << " Amplitude " <<  " Index "  <<  " Nprim " << " Primaries list " <<  endl;      
599     Int_t index ;
600     for (index = 0 ; index < fDigits->GetEntries() ; index++) {
601       digit = (AliEMCALDigit * )  fDigits->At(index) ;
602       cout << setw(8)  <<  digit->GetId() << " "  <<    setw(3)  <<  digit->GetAmp() <<   "  "  
603            << setw(6)  <<  digit->GetIndexInList() << "  "   
604            << setw(5)  <<  digit->GetNprimary() <<"  ";
605       
606       Int_t iprimary;
607       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
608         cout << setw(5)  <<  digit->GetPrimary(iprimary+1) << " ";
609       cout << endl;      
610     }
611     
612   }
613 }
614 //_________________________________________________________________________________________
615 void AliEMCALDigitizer::WriteDigits(Int_t event)
616 {
617
618   // Makes TreeD in the output file. 
619   // Check if branch already exists: 
620   //   if yes, exit without writing: ROOT TTree does not support overwriting/updating of 
621   //      already existing branches. 
622   //   else creates branch with Digits, named "EMCAL", title "...",
623   //      and branch "AliEMCALDigitizer", with the same title to keep all the parameters
624   //      and names of files, from which digits are made.
625
626   AliEMCALGetter * gime = AliEMCALGetter::GetInstance() ; 
627   const TClonesArray * digits = gime->Digits(GetName()) ; 
628  TTree * treeD ;
629
630  if(fManager)
631    treeD = fManager->GetTreeD() ;
632  else {
633    if (!gAlice->TreeD() ) 
634      gAlice->MakeTree("D",fSplitFile);
635    treeD = gAlice->TreeD();
636  }
637  
638  // -- create Digits branch
639  Int_t bufferSize = 32000 ;    
640  TBranch * digitsBranch = treeD->Branch("EMCAL",&digits,bufferSize);
641  digitsBranch->SetTitle(GetName());
642  
643  // -- Create Digitizer branch
644  Int_t splitlevel = 0 ;
645  AliEMCALDigitizer * d = gime->Digitizer(GetName()) ;
646  TBranch * digitizerBranch = treeD->Branch("AliEMCALDigitizer", "AliEMCALDigitizer", &d,bufferSize,splitlevel); 
647  digitizerBranch->SetTitle(GetName());
648  
649  digitsBranch->Fill() ;
650  digitizerBranch->Fill() ; 
651  treeD->AutoSave() ;  
652  
653 }
654 //____________________________________________________________________________ 
655 Float_t AliEMCALDigitizer::FrontEdgeTime(TClonesArray * ticks) 
656 { // 
657   ticks->Sort() ; //Sort in accordance with times of ticks
658   TIter it(ticks) ;
659   AliEMCALTick * ctick = (AliEMCALTick *) it.Next() ;
660   Float_t time = ctick->CrossingTime(fTimeThreshold) ;    
661   
662   AliEMCALTick * t ;  
663   while((t=(AliEMCALTick*) it.Next())){
664     if(t->GetTime() < time)  //This tick starts before crossing
665       *ctick+=*t ;
666     else
667       return time ;
668     
669     time = ctick->CrossingTime(fTimeThreshold) ;    
670   }
671   return time ;
672 }
673 //____________________________________________________________________________ 
674 Float_t AliEMCALDigitizer::TimeOfNoise(void)
675 {  // Calculates the time signal generated by noise
676   //to be rewritten, now returns just big number
677   return 1. ;
678
679 }
680 //____________________________________________________________________________
681 void AliEMCALDigitizer::SetSDigitsBranch(const char* title)
682 {
683   // we set title (comment) of the SDigits branch in the first! header file
684   if( strcmp(GetName(), "") == 0 )
685     Init() ;
686
687   AliEMCALGetter::GetInstance()->SDigits()->SetName(title) ; 
688 }
689
690 //__________________________________________________________________
691 void AliEMCALDigitizer::SetSplitFile(const TString splitFileName)
692 {
693   // Diverts the Digits in a file separate from the hits file
694   
695   // I guess it is not going to work if we do merging
696   if (fManager) {
697     cerr << "ERROR: AliEMCALDigitizer::SetSplitFile -> Not yet available in case of merging activated " << endl ;  
698     return ; 
699   }
700
701   TDirectory * cwd = gDirectory ;
702   if ( !(gAlice->GetTreeDFileName() == splitFileName) ) {
703     if (gAlice->GetTreeDFile() )  
704       gAlice->GetTreeDFile()->Close() ; 
705   }
706
707   fSplitFile = gAlice->InitTreeFile("D",splitFileName.Data());
708   fSplitFile->cd() ; 
709   if ( !fSplitFile->Get("gAlice") ) 
710     gAlice->Write();
711   
712   TTree *treeE  = gAlice->TreeE();
713   if (!treeE) {
714     cerr << "ERROR: AliEMCALDigitizer::SetSplitFile -> No TreeE found "<<endl;
715     abort() ;
716   }      
717   
718   // copy TreeE
719   if ( !fSplitFile->Get("TreeE") ) {
720     AliHeader *header = new AliHeader();
721     treeE->SetBranchAddress("Header", &header);
722     treeE->SetBranchStatus("*",1);
723     TTree *treeENew =  treeE->CloneTree();
724     treeENew->Write();
725   }
726
727   // copy AliceGeom
728   if ( !fSplitFile->Get("AliceGeom") ) {
729     TGeometry *AliceGeom = static_cast<TGeometry*>(cwd->Get("AliceGeom"));
730     if (!AliceGeom) {
731       cerr << "ERROR: AliEMCALDigitizer::SetSplitFile -> AliceGeom was not found in the input file "<<endl;
732       abort() ;
733     }
734     AliceGeom->Write();
735   }
736   
737   gAlice->MakeTree("D",fSplitFile);
738   cwd->cd() ; 
739   cout << "INFO: AliEMCALDigitizer::SetSPlitMode -> Digits will be stored in " << splitFileName.Data() << endl ; 
740 }