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