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