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