]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSDigitizer.cxx
Time smearing added, def. value of t.s. set to 0.5 ns
[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 "TBenchmark.h"
65
66 // --- Standard library ---
67 #include <iomanip.h>
68
69 // --- AliRoot header files ---
70
71 #include "AliRun.h"
72 #include "AliRunDigitizer.h"
73 #include "AliPHOSDigit.h"
74 #include "AliPHOS.h"
75 #include "AliPHOSGetter.h"
76 #include "AliPHOSDigitizer.h"
77 #include "AliPHOSSDigitizer.h"
78 #include "AliPHOSGeometry.h"
79 #include "AliPHOSTick.h"
80
81 ClassImp(AliPHOSDigitizer)
82
83
84 //____________________________________________________________________________ 
85   AliPHOSDigitizer::AliPHOSDigitizer() 
86 {
87   // ctor
88
89   fPinNoise           = 0.01 ;
90   fEMCDigitThreshold  = 0.01 ;
91   fCPVNoise           = 0.01;
92   fCPVDigitThreshold  = 0.09 ;
93   fTimeResolution     = 0.5e-9 ;
94   fTimeSignalLength   = 1.0e-9 ;
95   fDigitsInRun  = 0 ; 
96   fADCchanelEmc = 0.0015;        // width of one ADC channel in GeV
97   fADCpedestalEmc = 0.005 ;      //
98   fNADCemc = (Int_t) TMath::Power(2,16) ;  // number of channels in EMC ADC
99
100   fADCchanelCpv = 0.0012 ;          // width of one ADC channel in CPV 'popugais'
101   fADCpedestalCpv = 0.012 ;         // 
102   fNADCcpv = (Int_t) TMath::Power(2,12);      // number of channels in CPV ADC
103
104   fTimeThreshold = 0.001*10000000 ; //Means 1 MeV in terms of SDigits amplitude
105   fARD = 0 ;                        // We work in the standalong mode
106 }
107
108 //____________________________________________________________________________ 
109 AliPHOSDigitizer::AliPHOSDigitizer(const char *headerFile,const char * name)
110 {
111   // ctor
112   SetName(name) ;
113   SetTitle(headerFile) ;
114   fARD = 0 ;                     // We work in the standalong mode
115   Init() ;
116   
117 }
118
119 //____________________________________________________________________________ 
120 AliPHOSDigitizer::AliPHOSDigitizer(AliRunDigitizer * ard)
121 {
122   // ctor
123   fARD = ard ;
124   SetName("Default");
125   SetTitle("aliroot") ;
126   Init() ;
127   
128 }
129
130 //____________________________________________________________________________ 
131   AliPHOSDigitizer::~AliPHOSDigitizer()
132 {
133   // dtor
134
135
136 }
137
138 //____________________________________________________________________________
139 void AliPHOSDigitizer::Digitize(const Int_t event) 
140
141   
142   // Makes the digitization of the collected summable digits.
143   //  It first creates the array of all PHOS modules
144   //  filled with noise (different for EMC, CPV and PPSD) and
145   //  then adds contributions from SDigits. 
146   // This design avoids scanning over the list of digits to add 
147   // contribution to new SDigits only.
148
149   if( strcmp(GetName(), "") == 0 )
150     Init() ;
151
152   AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ; 
153   TClonesArray * digits = gime->Digits(GetName()) ; 
154   AliPHOSSDigitizer * sdiz = gime->SDigitizer(GetName()) ; 
155
156   digits->Clear() ;
157
158   const AliPHOSGeometry *geom = gime->PHOSGeometry() ; 
159
160   //Making digits with noise, first EMC
161   Int_t nEMC = geom->GetNModules()*geom->GetNPhi()*geom->GetNZ();
162   
163   Int_t nCPV ;
164   Int_t absID ;
165   TString name      =  geom->GetName() ;
166   
167   nCPV = nEMC + geom->GetNumberOfCPVPadsZ()*geom->GetNumberOfCPVPadsPhi()*
168     geom->GetNModules() ;
169
170   digits->Expand(nCPV) ;
171
172
173   // sdigitize random gaussian noise and add it to all cells (EMCA+CPV+PPSD) 
174   // get first the sdigitizer from the tasks list (must have same name as the digitizer)
175   const AliPHOSSDigitizer * sDigitizer = gime->SDigitizer(GetName()); 
176   if ( !sDigitizer) {
177     cerr << "ERROR: AliPHOSDigitizer::Digitize -> SDigitizer with name " << GetName() << " not found " << endl ; 
178     abort() ; 
179   }
180     
181   // loop through the sdigits posted to the White Board and add them to the noise
182   TCollection * folderslist = gime->SDigitsFolder()->GetListOfFolders() ; 
183   TIter next(folderslist) ; 
184   TFolder * folder = 0 ; 
185   TClonesArray * sdigits = 0 ;
186   Int_t input = 0 ;
187   TObjArray * sdigArray = new TObjArray(2) ;
188   while ( (folder = (TFolder*)next()) ) 
189     if ( (sdigits = (TClonesArray*)folder->FindObject(GetName()) ) ) {
190       cout << "INFO: AliPHOSDigitizer::Digitize -> Adding SDigits " 
191            << GetName() << " from " << folder->GetName() << endl ; 
192       sdigArray->AddAt(sdigits, input) ;
193       input++ ;
194     }
195
196   //Find the first crystall with signal
197   Int_t nextSig = 200000 ; 
198   Int_t i;
199   for(i=0; i<input; i++){
200     sdigits = (TClonesArray *)sdigArray->At(i) ;
201     if ( !sdigits->GetEntries() )
202       continue ; 
203     Int_t curNext = ((AliPHOSDigit *)sdigits->At(0))->GetId() ;
204      if(curNext < nextSig) 
205        nextSig = curNext ;
206   }
207
208   TArrayI index(input) ;
209   index.Reset() ;  //Set all indexes to zero
210
211   AliPHOSDigit * digit ;
212   AliPHOSDigit * curSDigit ;
213
214   TClonesArray * ticks = new TClonesArray("AliPHOSTick",1000) ;
215
216   //Put Noise contribution
217   for(absID = 1; absID <= nEMC; absID++){
218     Float_t noise = gRandom->Gaus(0., fPinNoise) ; 
219     new((*digits)[absID-1]) AliPHOSDigit( -1,absID,sDigitizer->Digitize(noise), TimeOfNoise() ) ;
220     //look if we have to add signal?
221     if(absID==nextSig){
222       //Add SDigits from all inputs 
223       digit = (AliPHOSDigit *) digits->At(absID-1) ;
224
225       ticks->Clear() ;
226       Int_t contrib = 0 ;
227       Float_t a = digit->GetAmp() ;
228       Float_t b = TMath::Abs( a /fTimeSignalLength) ;
229       //Mark the beginnign of the signal
230       new((*ticks)[contrib++]) AliPHOSTick(digit->GetTime(),0, b);  
231       //Mark the end of the ignal     
232       new((*ticks)[contrib++]) AliPHOSTick(digit->GetTime()+fTimeSignalLength, -a, -b); 
233
234       //loop over inputs
235       for(i=0; i<input; i++){
236         if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
237           curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;         
238         else
239           curSDigit = 0 ;
240         //May be several digits will contribute from the same input
241         while(curSDigit && curSDigit->GetId() == absID){           
242           //Shift primary to separate primaries belonging different inputs
243           Int_t primaryoffset ;
244           if(fARD)
245             primaryoffset = fARD->GetMask(i) ; 
246           else
247             primaryoffset = 10000000*i ;
248           curSDigit->ShiftPrimary(primaryoffset) ;
249           
250           a = curSDigit->GetAmp() ;
251           b = a /fTimeSignalLength ;
252           new((*ticks)[contrib++]) AliPHOSTick(curSDigit->GetTime(),0, b);  
253           new((*ticks)[contrib++]) AliPHOSTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b); 
254
255           *digit = *digit + *curSDigit ;  //add energies
256
257           index[i]++ ;
258           if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
259             curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;       
260           else
261             curSDigit = 0 ;
262         }
263       }
264
265       //calculate and set time
266       Float_t time = FrontEdgeTime(ticks) ;
267       digit->SetTime(time) ;
268
269       //Find next signal module
270       nextSig = 200000 ;
271       for(i=0; i<input; i++){
272         sdigits = ((TClonesArray *)sdigArray->At(i)) ;
273         Int_t curNext = nextSig ;
274         if(sdigits->GetEntriesFast() > index[i] ){
275           curNext = ((AliPHOSDigit *) sdigits->At(index[i]))->GetId() ;
276           
277         }
278         if(curNext < nextSig) nextSig = curNext ;
279       }
280     }
281   }
282   
283   ticks->Delete() ;
284   delete ticks ;
285
286
287   //Now CPV digits (different noise and no timing)
288   for(absID = nEMC+1; absID <= nCPV; absID++){
289     Float_t noise = gRandom->Gaus(0., fCPVNoise) ; 
290     new((*digits)[absID-1]) AliPHOSDigit( -1,absID,sDigitizer->Digitize(noise), TimeOfNoise() ) ;
291     //look if we have to add signal?
292     if(absID==nextSig){
293       digit = (AliPHOSDigit *) digits->At(absID-1) ;
294       //Add SDigits from all inputs
295       for(i=0; i<input; i++){
296         if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
297           curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;         
298         else
299           curSDigit = 0 ;
300         //May be several digits will contribute from the same input
301         while(curSDigit && curSDigit->GetId() == absID){           
302           //Shift primary to separate primaries belonging different inputs
303           Int_t primaryoffset ;
304           if(fARD)
305             primaryoffset = fARD->GetMask(i) ; 
306           else
307             primaryoffset = 10000000*i ;
308           curSDigit->ShiftPrimary(primaryoffset) ;
309
310           //add energies
311           *digit = *digit + *curSDigit ;  
312           index[i]++ ;
313           if(((TClonesArray *)sdigArray->At(i))->GetEntriesFast() > index[i] )
314             curSDigit = (AliPHOSDigit*)((TClonesArray *)sdigArray->At(i))->At(index[i]) ;       
315           else
316             curSDigit = 0 ;
317         }
318       }
319       
320       //Find next signal module
321       for(i=0; i<input; i++){
322         sdigits = (TClonesArray *)sdigArray->At(i) ;
323         Int_t curNext = nextSig ;
324         if(sdigits->GetEntriesFast() > index[i] )
325           curNext = ((AliPHOSDigit *) sdigits->At(index[i]))->GetId() ;
326         if(curNext < nextSig) nextSig = curNext ;
327       }
328       
329     }
330   }
331   delete sdigArray ; //We should not delete its contents
332   
333   
334   
335   //remove digits below thresholds
336   for(absID = 0; absID < nEMC ; absID++){
337     digit = (AliPHOSDigit*) digits->At(absID) ;
338     if(sDigitizer->Calibrate( digit->GetAmp() ) < fEMCDigitThreshold)
339       digits->RemoveAt(absID) ;
340     else
341       digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
342   }
343
344   for(absID = nEMC; absID < nCPV ; absID++)
345     if(sDigitizer->Calibrate(((AliPHOSDigit*)digits->At(absID))->GetAmp()) < fCPVDigitThreshold)
346       digits->RemoveAt(absID) ;
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 = sdiz->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(fARD){
399     treeD = fARD->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   if(treeD == 0 ){
408     cerr << " AliPHOSDigitizer :: Can not find TreeD " << endl ;
409     return ;
410   }
411
412   //Check, if this branch already exits
413   TObjArray * lob = (TObjArray*)treeD->GetListOfBranches() ;
414   TIter next(lob) ; 
415   TBranch * branch = 0 ;  
416   Bool_t phosfound = kFALSE, digitizerfound = kFALSE ; 
417   
418   while ( (branch = (TBranch*)next()) && (!phosfound || !digitizerfound) ) {
419     if ( (strcmp(branch->GetName(), "PHOS")==0) && 
420          (strcmp(branch->GetTitle(), GetName())==0) ) 
421       phosfound = kTRUE ;
422     
423     else if ( (strcmp(branch->GetName(), "AliPHOSDigitizer")==0) && 
424               (strcmp(branch->GetTitle(), GetName())==0) ) 
425       digitizerfound = kTRUE ; 
426   }
427
428   if ( phosfound ) {
429     cerr << "WARNING: AliPHOSDigitizer -> Digits branch with name " << GetName() 
430          << " already exits" << endl ;
431     return ; 
432   }   
433   if ( digitizerfound ) {
434     cerr << "WARNING: AliPHOSDigitizer -> Digitizer branch with name " << GetName() 
435          << " already exits" << endl ;
436     return ; 
437   }   
438
439   Int_t ievent ;
440
441   for(ievent = 0; ievent < nevents; ievent++){
442     
443     if(fARD){
444       Int_t input ;
445       for(input = 0 ; input < fARD->GetNinputs(); input ++){
446         TTree * treeS = fARD->GetInputTreeS(input) ;
447         if(!treeS){
448           cerr << "AliPHOSDigitizer -> No Input " << endl ;
449           return ;
450         }
451         gime->ReadTreeS(treeS,input) ;
452       }
453     }
454     else
455       gime->Event(ievent,"S") ;
456     
457     Digitize(ievent) ; //Add prepared SDigits to digits and add the noise
458     
459     WriteDigits(ievent) ;
460     
461     if(strstr(option,"deb"))
462       PrintDigits(option);
463   }
464   
465   if(strstr(option,"tim")){
466     gBenchmark->Stop("PHOSDigitizer");
467     cout << "AliPHOSDigitizer:" << endl ;
468     cout << "  took " << gBenchmark->GetCpuTime("PHOSDigitizer") << " seconds for Digitizing " 
469          <<  gBenchmark->GetCpuTime("PHOSDigitizer")/nevents << " seconds per event " << endl ;
470     cout << endl ;
471   }
472   
473 }
474
475 //____________________________________________________________________________ 
476 Float_t AliPHOSDigitizer::FrontEdgeTime(TClonesArray * ticks) 
477 { // 
478   ticks->Sort() ; //Sort in accordance with times of ticks
479   TIter it(ticks) ;
480   AliPHOSTick * ctick = (AliPHOSTick *) it.Next() ;
481   Float_t time = ctick->CrossingTime(fTimeThreshold) ;    
482
483   AliPHOSTick * t ;  
484   while((t=(AliPHOSTick*) it.Next())){
485     if(t->GetTime() < time)  //This tick starts before crossing
486       *ctick+=*t ;
487     else
488       return time ;
489
490     time = ctick->CrossingTime(fTimeThreshold) ;    
491   }
492   return time ;
493 }
494 //____________________________________________________________________________ 
495 Bool_t AliPHOSDigitizer::Init()
496 {
497   fPinNoise           = 0.01 ;
498   fEMCDigitThreshold  = 0.01 ;
499   fCPVNoise           = 0.01;
500   fCPVDigitThreshold  = 0.09 ;
501   fTimeResolution     = 0.5e-9 ;
502   fTimeSignalLength   = 1.0e-9 ;
503   fDigitsInRun  = 0 ; 
504   fADCchanelEmc = 0.0015;        // width of one ADC channel in GeV
505   fADCpedestalEmc = 0.005 ;      //
506   fNADCemc = (Int_t) TMath::Power(2,16) ;  // number of channels in EMC ADC
507
508   fADCchanelCpv = 0.0012 ;          // width of one ADC channel in CPV 'popugais'
509   fADCpedestalCpv = 0.012 ;         // 
510   fNADCcpv = (Int_t) TMath::Power(2,12);      // number of channels in CPV ADC
511
512   fTimeThreshold = 0.001*10000000 ; //Means 1 MeV in terms of SDigits amplitude
513
514   // Makes all memory allocations
515   // Adds Digitizer task to the folder of PHOS tasks
516
517    //============================================================= YS
518   //  The initialisation is now done by AliPHOSGetter
519     
520   if( strcmp(GetTitle(), "") == 0 )
521     SetTitle("galice.root") ;
522   
523   // the SDigits name is stored by AliPHOSGetter as the name of the TClones Array 
524   // //YSAlice/WhiteBoard/SDigits/PHOS/headerFile/branchname and has branchTitle as title.    
525     
526   AliPHOSGetter * gime = AliPHOSGetter::GetInstance(GetTitle(), GetName()) ; 
527   if ( gime == 0 ) {
528     cerr << "ERROR: AliPHOSDigitizer::Init -> Could not obtain the Getter object !" << endl ; 
529     return kFALSE;
530   } 
531   
532   const AliPHOSGeometry * geom = gime->PHOSGeometry() ;
533   fEmcCrystals = geom->GetNModules() *  geom->GetNCristalsInModule() ;
534      
535   // create a folder on the white board //YSAlice/WhiteBoard/Digits/PHOS/headerFile/digitsTitle
536   gime->PostDigits(GetName() ) ;   
537   
538   //add Task to //YSAlice/tasks/Digitizer/PHOS
539     gime->PostDigitizer(this) ;
540   
541   //Mark that we will use current header file
542   if(!fARD){
543     gime->PostSDigits(GetName(),GetTitle()) ;
544     gime->PostSDigitizer(GetName(),GetTitle()) ;
545   }
546   return kTRUE ;
547 }
548
549 //__________________________________________________________________
550 void AliPHOSDigitizer::MixWith(const char* headerFile)
551 {
552   // Allows to produce digits by superimposing background and signal event.
553   // It is assumed, that headers file with SIGNAL events is opened in 
554   // the constructor. 
555   // Sets the BACKGROUND event, with which the SIGNAL event is to be mixed 
556   // Thus we avoid writing (changing) huge and expensive 
557   // backgound files: all output will be writen into SIGNAL, i.e. 
558   // opened in constructor file. 
559   //
560   // One can open as many files to mix with as one needs.
561   // However only Sdigits with the same name (i.e. constructed with the same SDigitizer)
562   // can be mixed.
563
564   if( strcmp(GetName(), "") == 0 )
565     Init() ;
566
567   if(fARD){
568     cout << "Can not use this method under AliRunDigitizer " << endl ;
569     return ;
570   }
571   
572   // check if the specified SDigits do not already exist on the White Board:
573   // //YSAlice/WhiteBoard/SDigits/PHOS/headerFile/sDigitsTitle
574
575   TString path = "YSAlice/WhiteBoard/SDigits/PHOS/" ; 
576   path += headerFile ; 
577   path += "/" ; 
578   path += GetName() ;
579   if ( gROOT->FindObjectAny(path.Data()) ) {
580     cerr << "WARNING: AliPHOSDigitizer::MixWith -> Entry already exists, do not add" << endl ;
581     return;
582   }
583
584   AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ;
585   gime->PostSDigits(GetName(),headerFile) ;
586   
587   // check if the requested file is already open or exist and if SDigits Branch exist
588   TFile * file = (TFile*)gROOT->FindObject(headerFile); 
589   if ( !file ) { 
590     file = new TFile(headerFile, "READ") ; 
591     if (!file) { 
592       cerr << "ERROR: AliPHOSDigitizer::MixWith -> File " << headerFile << " does not exist!" << endl ; 
593       return ; 
594     }
595   }
596   
597 }
598
599 //__________________________________________________________________
600 void AliPHOSDigitizer::Print(Option_t* option)const {
601   // Print Digitizer's parameters
602   if( strcmp(GetName(), "") != 0 ){
603     
604     cout << "------------------- "<< GetName() << " -------------" << endl ;
605     cout << "Digitizing sDigits from file(s): " <<endl ;
606     
607      TCollection * folderslist = ((TFolder*)gROOT->FindObjectAny("YSAlice/WhiteBoard/SDigits/PHOS"))->GetListOfFolders() ; 
608     TIter next(folderslist) ; 
609     TFolder * folder = 0 ; 
610     
611     while ( (folder = (TFolder*)next()) ) {
612       if ( folder->FindObject(GetName())  ) 
613         cout << "Adding SDigits " << GetName() << " from " << folder->GetName() << endl ; 
614     }
615     cout << endl ;
616     cout << "Writing digits to " << GetTitle() << endl ;
617     
618     cout << endl ;
619     cout << "With following parameters: " << endl ;
620     cout << "     Electronics noise in EMC (fPinNoise) = " << fPinNoise << endl ;
621     cout << "  Threshold  in EMC  (fEMCDigitThreshold) = " << fEMCDigitThreshold  << endl ; ;
622     cout << "                 Noise in CPV (fCPVNoise) = " << fCPVNoise << endl ; 
623     cout << "    Threshold in CPV (fCPVDigitThreshold) = " << fCPVDigitThreshold << endl ; 
624     cout << "---------------------------------------------------" << endl ;
625   }
626   else
627     cout << "AliPHOSDigitizer not initialized " << endl ;
628   
629 }
630
631 //__________________________________________________________________
632 void AliPHOSDigitizer::PrintDigits(Option_t * option){
633   // Print a table of digits
634
635   AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ; 
636   TClonesArray * digits = gime->Digits() ; 
637
638   cout << "AliPHOSDigitiser: event " << gAlice->GetEvNumber() << endl ;
639   cout << "       Number of entries in Digits list " << digits->GetEntriesFast() << endl ;
640   cout << endl ;
641   if(strstr(option,"all")||strstr(option,"EMC")){
642     
643     //loop over digits
644     AliPHOSDigit * digit;
645     cout << "Digit Id    Amplitude     Index "  <<  " Nprim " << " Primaries list " <<  endl;      
646     Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
647     Int_t index ;
648     for (index = 0 ; (index < digits->GetEntries()) && 
649          (((AliPHOSDigit * )  digits->At(index))->GetId() <= maxEmc) ; index++) {
650       digit = (AliPHOSDigit * )  digits->At(index) ;
651       if(digit->GetNprimary() == 0) continue;
652       cout << setw(6)  <<  digit->GetId() << "   "  <<  setw(10)  <<  digit->GetAmp() <<   "    "  
653            << setw(6)  <<  digit->GetIndexInList() << "    "   
654            << setw(5)  <<  digit->GetNprimary() <<"    ";
655       
656       Int_t iprimary;
657       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
658         cout << setw(5)  <<  digit->GetPrimary(iprimary+1) << "    ";
659       cout << endl;      
660     }    
661     cout << endl;
662   }
663
664   if(strstr(option,"all")||strstr(option,"CPV")){
665     
666     //loop over CPV digits
667     AliPHOSDigit * digit;
668     cout << "Digit Id " << " Amplitude " <<  " Index "  <<  " Nprim " << " Primaries list " <<  endl;      
669     Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
670     Int_t index ;
671     for (index = 0 ; index < digits->GetEntries(); index++) {
672       digit = (AliPHOSDigit * )  digits->At(index) ;
673       if(digit->GetId() > maxEmc){
674         cout << setw(6)  <<  digit->GetId() << "   "  <<        setw(10)  <<  digit->GetAmp() <<   "    "  
675              << setw(6)  <<  digit->GetIndexInList() << "    "   
676              << setw(5)  <<  digit->GetNprimary() <<"    ";
677         
678         Int_t iprimary;
679         for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++)
680           cout << setw(5)  <<  digit->GetPrimary(iprimary+1) << "    ";
681         cout << endl;    
682       }    
683     }
684   }
685
686 }
687
688 //__________________________________________________________________
689 void AliPHOSDigitizer::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   AliPHOSGetter::GetInstance()->SDigits()->SetName(title) ; 
696  
697 }
698 //__________________________________________________________________
699 Float_t AliPHOSDigitizer::TimeOfNoise(void)
700 {  // Calculates the time signal generated by noise
701   //to be rewritten, now returns just big number
702   return 1. ;
703
704 }
705 //____________________________________________________________________________
706 void AliPHOSDigitizer::Reset() 
707
708   // sets current event number to the first simulated event
709
710   if( strcmp(GetName(), "") == 0 )
711     Init() ;
712
713  //  Int_t inputs ;
714 //   for(inputs = 0; inputs < fNinputs ;inputs++)
715 //       fIevent->AddAt(-1, inputs ) ;
716   
717 }
718
719 //____________________________________________________________________________
720 void AliPHOSDigitizer::WriteDigits(Int_t event)
721 {
722
723   // Makes TreeD in the output file. 
724   // Check if branch already exists: 
725   //   if yes, exit without writing: ROOT TTree does not support overwriting/updating of 
726   //      already existing branches. 
727   //   else creates branch with Digits, named "PHOS", title "...",
728   //      and branch "AliPHOSDigitizer", with the same title to keep all the parameters
729   //      and names of files, from which digits are made.
730
731   AliPHOSGetter * gime = AliPHOSGetter::GetInstance() ; 
732   TClonesArray * digits = gime->Digits(GetName()) ; 
733
734   TTree * treeD ;
735
736   if(fARD)
737     treeD = fARD->GetTreeD() ;
738  else
739     treeD = gAlice->TreeD();
740   
741   // create new branches
742   // -- generate file name if necessary
743   char * file =0;
744   if(gSystem->Getenv("CONFIG_SPLIT_FILE")){ //generating file name
745     file = new char[strlen(gAlice->GetBaseFile())+20] ;
746     sprintf(file,"%s/PHOS.Digits.root",gAlice->GetBaseFile()) ;
747   }
748
749   TDirectory *cwd = gDirectory;
750   // -- create Digits branch
751   Int_t bufferSize = 32000 ;    
752   TBranch * digitsBranch = treeD->Branch("PHOS",&digits,bufferSize);
753   digitsBranch->SetTitle(GetName());
754   if (file) {
755     digitsBranch->SetFile(file);
756     TIter next( digitsBranch->GetListOfBranches());
757     TBranch * sbr ;
758     while ((sbr=(TBranch*)next())) {
759       sbr->SetFile(file);
760     }   
761     cwd->cd();
762   } 
763     
764   // -- Create Digitizer branch
765   Int_t splitlevel = 0 ;
766   AliPHOSDigitizer * d = gime->Digitizer(GetName()) ;
767   TBranch * digitizerBranch = treeD->Branch("AliPHOSDigitizer", "AliPHOSDigitizer", &d,bufferSize,splitlevel); 
768   digitizerBranch->SetTitle(GetName());
769   if (file) {
770     digitizerBranch->SetFile(file);
771     TIter next( digitizerBranch->GetListOfBranches());
772     TBranch * sbr;
773     while ((sbr=(TBranch*)next())) {
774       sbr->SetFile(file);
775     }   
776     cwd->cd();
777   }
778   
779   digitsBranch->Fill() ;      
780   digitizerBranch->Fill() ;
781
782   treeD->Write(0,kOverwrite) ;  
783  
784 }