]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSDigitizer.cxx
Added the option to process 1 event(default) or a range of events
[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 //_________________________________________________________________________
20 //*-- Author :  Dmitri Peressounko (SUBATECH & Kurchatov Institute) 
21 //////////////////////////////////////////////////////////////////////////////
22 // This TTask performs digitization of Summable digits (in the PHOS case it is just
23 // the sum of contributions from all primary particles into a given cell). 
24 // In addition it performs mixing of summable digits from different events.
25 // The name of the TTask is also the title of the branch that will contain 
26 // the created SDigits
27 // The title of the TTAsk is the name of the file that contains the hits from
28 // which the SDigits are created
29 //
30 // For each event two branches are created in TreeD:
31 //   "PHOS" - list of digits
32 //   "AliPHOSDigitizer" - AliPHOSDigitizer with all parameters used in digitization
33 //
34 // Note, that one can set a title for new digits branch, and repeat digitization with
35 // another set of parameters.
36 //
37 // Use case:
38 // root[0] AliPHOSDigitizer * d = new AliPHOSDigitizer() ;
39 // root[1] d->ExecuteTask()             
40 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
41 //                       //Digitizes SDigitis in all events found in file galice.root 
42 //
43 // root[2] AliPHOSDigitizer * d1 = new AliPHOSDigitizer("galice1.root") ;  
44 //                       // Will read sdigits from galice1.root
45 // root[3] d1->MixWith("galice2.root")       
46 // Warning in <TDatabasePDG::TDatabasePDG>: object already instantiated
47 //                       // Reads another set of sdigits from galice2.root
48 // root[3] d1->MixWith("galice3.root")       
49 //                       // Reads another set of sdigits from galice3.root
50 // root[4] d->ExecuteTask("deb timing")    
51 //                       // Reads SDigits from files galice1.root, galice2.root ....
52 //                       // mixes them and stores produced Digits in file galice1.root          
53 //                       // deb - prints number of produced digits
54 //                       // deb all - prints list of produced digits
55 //                       // timing  - prints time used for digitization
56 //
57
58 // --- ROOT system ---
59 #include "TTree.h"
60 #include "TSystem.h"
61 #include "TBenchmark.h"
62 #include "TRandom.h"
63
64 // --- Standard library ---
65
66 // --- AliRoot header files ---
67
68 #include "AliRunDigitizer.h"
69 #include "AliPHOSDigit.h"
70 #include "AliPHOSGetter.h"
71 #include "AliPHOSDigitizer.h"
72 #include "AliPHOSSDigitizer.h"
73 #include "AliPHOSGeometry.h"
74 #include "AliPHOSTick.h"
75
76 ClassImp(AliPHOSDigitizer)
77
78
79 //____________________________________________________________________________ 
80   AliPHOSDigitizer::AliPHOSDigitizer():AliDigitizer("",""),
81                                        fInput(0),
82                                        fInputFileNames(0x0),
83                                        fEventNames(0x0)
84 {
85   // ctor
86   InitParameters() ; 
87   fDefaultInit = kTRUE ;
88   fManager = 0 ;                     // We work in the standalong mode
89   fEventFolderName = "" ; 
90 }
91
92 //____________________________________________________________________________ 
93 AliPHOSDigitizer::AliPHOSDigitizer(TString alirunFileName, TString eventFolderName):
94   AliDigitizer("PHOS"+AliConfig::fgkDigitizerTaskName, alirunFileName),
95   fInputFileNames(0), fEventNames(0), fEventFolderName(eventFolderName)
96 {
97   // ctor
98   InitParameters() ; 
99   Init() ;
100   fDefaultInit = kFALSE ; 
101   fManager = 0 ;                     // We work in the standalong mode
102 }
103
104 //____________________________________________________________________________ 
105 AliPHOSDigitizer::AliPHOSDigitizer(const AliPHOSDigitizer & d)
106   : AliDigitizer(d)
107 {
108   // copyy ctor 
109
110   SetName(d.GetName()) ; 
111   SetTitle(d.GetTitle()) ; 
112   fPinNoise = d.fPinNoise ; 
113   fEMCDigitThreshold = d.fEMCDigitThreshold ; 
114   fCPVNoise          = d.fCPVNoise ; 
115   fCPVDigitThreshold = d.fCPVDigitThreshold ; 
116   fTimeResolution    = d.fTimeResolution ; 
117   fTimeThreshold     = d.fTimeThreshold ; 
118   fTimeSignalLength  = d.fTimeSignalLength ; 
119   fADCchanelEmc      = d.fADCchanelEmc ; 
120   fADCpedestalEmc    = d.fADCpedestalEmc ; 
121   fNADCemc           = d.fNADCemc ;
122   fADCchanelCpv      = d.fADCchanelCpv ;
123   fADCpedestalCpv    = d.fADCpedestalCpv ;
124   fNADCcpv           = d.fNADCcpv ; 
125   fEventFolderName   = d.fEventFolderName;
126 }
127
128 //____________________________________________________________________________ 
129 AliPHOSDigitizer::AliPHOSDigitizer(AliRunDigitizer * rd):
130  AliDigitizer(rd,"PHOS"+AliConfig::fgkDigitizerTaskName),
131  fEventFolderName(0)
132 {
133   // ctor Init() is called by RunDigitizer
134   fManager = rd ; 
135   fEventFolderName = fManager->GetInputFolderName(0) ;
136   SetTitle(dynamic_cast<AliStream*>(fManager->GetInputStream(0))->GetFileName(0));
137   InitParameters() ; 
138   fDefaultInit = kFALSE ; 
139 }
140
141 //____________________________________________________________________________ 
142   AliPHOSDigitizer::~AliPHOSDigitizer()
143 {
144   // dtor
145   AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(),fEventFolderName);
146   gime->PhosLoader()->CleanDigitizer();
147   delete [] fInputFileNames ; 
148   delete [] fEventNames ; 
149  
150 }
151
152 //____________________________________________________________________________
153 void AliPHOSDigitizer::Digitize(Int_t event) 
154
155   
156   // Makes the digitization of the collected summable digits.
157   //  It first creates the array of all PHOS modules
158   //  filled with noise (different for EMC, CPV and PPSD) and
159   //  then adds contributions from SDigits. 
160   // This design avoids scanning over the list of digits to add 
161   // contribution to new SDigits only.
162
163   AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName) ; 
164   Int_t ReadEvent = event ; 
165   if (fManager) 
166     ReadEvent = dynamic_cast<AliStream*>(fManager->GetInputStream(0))->GetCurrentEventNumber() ; 
167   Info("Digitize", "Adding event %d from input stream 0", ReadEvent) ; 
168   gime->Event(ReadEvent, "S") ;
169   TClonesArray * digits = gime->Digits() ; 
170   digits->Clear() ;
171
172   const AliPHOSGeometry *geom = gime->PHOSGeometry() ; 
173   //Making digits with noise, first EMC
174   Int_t nEMC = geom->GetNModules()*geom->GetNPhi()*geom->GetNZ();
175   
176   Int_t nCPV ;
177   Int_t absID ;
178   
179   nCPV = nEMC + geom->GetNumberOfCPVPadsZ() * geom->GetNumberOfCPVPadsPhi() * geom->GetNModules() ;
180   
181   digits->Expand(nCPV) ;
182
183   // get first the sdigitizer from the tasks list 
184   if ( !gime->SDigitizer() ) 
185     gime->LoadSDigitizer();
186   AliPHOSSDigitizer * sDigitizer = gime->SDigitizer(); 
187
188   if ( !sDigitizer )
189     Fatal("Digitize", "SDigitizer with name %s %s not found", fEventFolderName.Data(), GetTitle() ) ; 
190
191   //take all the inputs to add together and load the SDigits
192   TObjArray * sdigArray = new TObjArray(fInput) ;
193   sdigArray->AddAt(gime->SDigits(), 0) ;
194   Int_t i ;
195   for(i = 1 ; i < fInput ; i++){
196     TString tempo(fEventNames[i]) ; 
197     tempo += i ;
198     AliPHOSGetter * gime = AliPHOSGetter::Instance(fInputFileNames[i], tempo) ;
199     if (fManager) 
200       ReadEvent = dynamic_cast<AliStream*>(fManager->GetInputStream(i))->GetCurrentEventNumber() ; 
201     Info("Digitize", "Adding event %d from input stream %d", ReadEvent, i) ; 
202     gime->Event(ReadEvent,"S");
203     sdigArray->AddAt(gime->SDigits(), i) ;
204   }
205
206   //Find the first crystall with signal
207   Int_t nextSig = 200000 ; 
208   TClonesArray * sdigits ;  
209   for(i = 0 ; i < fInput ; i++){
210     sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
211     if ( !sdigits->GetEntriesFast() )
212       continue ; 
213     Int_t curNext = dynamic_cast<AliPHOSDigit *>(sdigits->At(0))->GetId() ;
214     if(curNext < nextSig) 
215       nextSig = curNext ;
216   }
217   
218   TArrayI index(fInput) ;
219   index.Reset() ;  //Set all indexes to zero
220   
221   AliPHOSDigit * digit ;
222   AliPHOSDigit * curSDigit ;
223   
224   TClonesArray * ticks = new TClonesArray("AliPHOSTick",1000) ;
225   
226   //Put Noise contribution
227   for(absID = 1 ; absID <= nEMC ; absID++){
228     Float_t noise = gRandom->Gaus(0., fPinNoise) ; 
229     new((*digits)[absID-1]) AliPHOSDigit( -1, absID, sDigitizer->Digitize(noise), TimeOfNoise() ) ;
230     //look if we have to add signal?
231     digit = dynamic_cast<AliPHOSDigit *>(digits->At(absID-1)) ;
232     
233     if(absID==nextSig){
234       //Add SDigits from all inputs 
235       ticks->Clear() ;
236       Int_t contrib = 0 ;
237       Float_t a = digit->GetAmp() ;
238       Float_t b = TMath::Abs( a / fTimeSignalLength) ;
239       //Mark the beginning of the signal
240       new((*ticks)[contrib++]) AliPHOSTick(digit->GetTime(),0, b);  
241       //Mark the end of the ignal     
242       new((*ticks)[contrib++]) AliPHOSTick(digit->GetTime()+fTimeSignalLength, -a, -b); 
243       
244       //loop over inputs
245       for(i = 0 ; i < fInput ; i++){
246         if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
247           curSDigit = dynamic_cast<AliPHOSDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;       
248         else
249           curSDigit = 0 ;
250         //May be several digits will contribute from the same input
251         while(curSDigit && curSDigit->GetId() == absID){           
252           //Shift primary to separate primaries belonging different inputs
253           Int_t primaryoffset ;
254           if(fManager)
255             primaryoffset = fManager->GetMask(i) ; 
256           else
257             primaryoffset = 10000000*i ;
258           curSDigit->ShiftPrimary(primaryoffset) ;
259           
260           a = curSDigit->GetAmp() ;
261           b = a /fTimeSignalLength ;
262           new((*ticks)[contrib++]) AliPHOSTick(curSDigit->GetTime(),0, b);  
263           new((*ticks)[contrib++]) AliPHOSTick(curSDigit->GetTime()+fTimeSignalLength, -a, -b); 
264           
265           *digit = *digit + *curSDigit ;  //add energies
266           
267           index[i]++ ;
268           if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
269             curSDigit = dynamic_cast<AliPHOSDigit*>(dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;     
270           else
271             curSDigit = 0 ;
272         }
273       }
274       
275       //calculate and set time
276       Float_t time = FrontEdgeTime(ticks) ;
277       digit->SetTime(time) ;
278       
279       //Find next signal module
280       nextSig = 200000 ;
281       for(i = 0 ; i < fInput ; i++){
282         sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
283         Int_t curNext = nextSig ;
284         if(sdigits->GetEntriesFast() > index[i] ){
285           curNext = dynamic_cast<AliPHOSDigit *>(sdigits->At(index[i]))->GetId() ;
286         }
287         if(curNext < nextSig) nextSig = curNext ;
288       }
289     }
290   }
291   
292   ticks->Delete() ;
293   delete ticks ;
294   
295   //Now CPV digits (different noise and no timing)
296   for(absID = nEMC+1; absID <= nCPV; absID++){
297     Float_t noise = gRandom->Gaus(0., fCPVNoise) ; 
298     new((*digits)[absID-1]) AliPHOSDigit( -1,absID,sDigitizer->Digitize(noise), TimeOfNoise() ) ;
299     //look if we have to add signal?
300     if(absID==nextSig){
301       digit = dynamic_cast<AliPHOSDigit *>(digits->At(absID-1)) ;
302       //Add SDigits from all inputs
303       for(i = 0 ; i < fInput ; i++){
304         if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
305           curSDigit = dynamic_cast<AliPHOSDigit*>( dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i])) ;      
306         else
307           curSDigit = 0 ;
308
309         //May be several digits will contribute from the same input
310         while(curSDigit && curSDigit->GetId() == absID){           
311           //Shift primary to separate primaries belonging different inputs
312           Int_t primaryoffset ;
313           if(fManager)
314             primaryoffset = fManager->GetMask(i) ; 
315           else
316             primaryoffset = 10000000*i ;
317           curSDigit->ShiftPrimary(primaryoffset) ;
318
319           //add energies
320           *digit = *digit + *curSDigit ;  
321           index[i]++ ;
322           if( dynamic_cast<TClonesArray *>(sdigArray->At(i))->GetEntriesFast() > index[i] )
323             curSDigit = dynamic_cast<AliPHOSDigit*>( dynamic_cast<TClonesArray *>(sdigArray->At(i))->At(index[i]) ) ;   
324           else
325             curSDigit = 0 ;
326         }
327       }
328
329       //Find next signal module
330       nextSig = 200000 ;
331       for(i = 0 ; i < fInput ; i++){
332         sdigits = dynamic_cast<TClonesArray *>(sdigArray->At(i)) ;
333         Int_t curNext = nextSig ;
334         if(sdigits->GetEntriesFast() > index[i] )
335           curNext = dynamic_cast<AliPHOSDigit *>( sdigits->At(index[i]) )->GetId() ;
336         if(curNext < nextSig) nextSig = curNext ;
337       }
338       
339     }
340   }
341
342   delete sdigArray ; //We should not delete its contents 
343   
344   //remove digits below thresholds
345   for(i = 0 ; i < nEMC ; i++){
346     digit = dynamic_cast<AliPHOSDigit*>( digits->At(i) ) ;
347     if(sDigitizer->Calibrate( digit->GetAmp() ) < fEMCDigitThreshold)
348       digits->RemoveAt(i) ;
349     else
350       digit->SetTime(gRandom->Gaus(digit->GetTime(),fTimeResolution) ) ;
351   }
352
353
354   for(i = nEMC; i < nCPV ; i++)
355     if( sDigitizer->Calibrate( dynamic_cast<AliPHOSDigit*>(digits->At(i))->GetAmp() ) < fCPVDigitThreshold )
356       digits->RemoveAt(i) ;
357     
358   digits->Compress() ;  
359   
360   Int_t ndigits = digits->GetEntriesFast() ;
361   digits->Expand(ndigits) ;
362
363   //Set indexes in list of digits and make true digitization of the energy
364   for (i = 0 ; i < ndigits ; i++) { 
365     digit = dynamic_cast<AliPHOSDigit*>( digits->At(i) ) ; 
366     digit->SetIndexInList(i) ;     
367     Float_t energy = sDigitizer->Calibrate(digit->GetAmp()) ;
368     digit->SetAmp(DigitizeEnergy(energy,digit->GetId()) ) ;
369   }
370 }
371
372 //____________________________________________________________________________
373 Int_t AliPHOSDigitizer::DigitizeEnergy(Float_t energy, Int_t absId)
374 {
375   // Returns digitized value of the energy in a cell absId
376
377   Int_t chanel ;
378   if(absId <= fEmcCrystals){ //digitize as EMC 
379     chanel = (Int_t) TMath::Ceil((energy - fADCpedestalEmc)/fADCchanelEmc) ;       
380     if(chanel > fNADCemc ) chanel =  fNADCemc ;
381   }
382   else{ //Digitize as CPV
383     chanel = (Int_t) TMath::Ceil((energy - fADCpedestalCpv)/fADCchanelCpv) ;       
384     if(chanel > fNADCcpv ) chanel =  fNADCcpv ;
385   }
386   return chanel ;
387 }
388
389 //____________________________________________________________________________
390 void AliPHOSDigitizer::Exec(Option_t *option) 
391
392   // Steering method to process digitization for events
393   // in the range from fFirstEvent to fLastEvent.
394   // This range is optionally set by SetEventRange().
395   // if fLastEvent=-1, then process events until the end.
396   // by default fLastEvent = fFirstEvent (process only one event)
397
398   if (!fInit) { // to prevent overwrite existing file
399     Error( "Exec", "Give a version name different from %s", fEventFolderName.Data() ) ;
400     return ;
401   }   
402
403   if (strstr(option,"print")) {
404     Print();
405     return ; 
406   }
407   
408   if(strstr(option,"tim"))
409     gBenchmark->Start("PHOSDigitizer");
410   
411 //   if (fManager) 
412 //     fInput = fManager->GetNinputs() ;
413  
414   AliPHOSGetter * gime = AliPHOSGetter::Instance() ;
415
416   if (fLastEvent == -1) 
417     fLastEvent = gime->MaxEvent() - 1 ;
418  
419   Int_t nEvents   = fLastEvent - fFirstEvent + 1;
420   
421   Int_t ievent ;
422
423   for (ievent = fFirstEvent; ievent <= fLastEvent; ievent++) {
424  
425     gime->Event(ievent,"S") ;
426
427     Digitize(ievent) ; //Add prepared SDigits to digits and add the noise
428
429     WriteDigits() ;
430
431     if(strstr(option,"deb"))
432       PrintDigits(option);
433     
434     //increment the total number of Digits per run 
435     fDigitsInRun += gime->Digits()->GetEntriesFast() ;  
436  }
437   
438   if(strstr(option,"tim")){
439     gBenchmark->Stop("PHOSDigitizer");
440     TString message ; 
441     message = "  took %f seconds for Digitizing %f seconds per event\n" ; 
442     Info("Exec", message.Data(), 
443          gBenchmark->GetCpuTime("PHOSDigitizer"), 
444          gBenchmark->GetCpuTime("PHOSDigitizer")/nEvents ); 
445   } 
446 }
447
448 //____________________________________________________________________________ 
449 Float_t AliPHOSDigitizer::FrontEdgeTime(TClonesArray * ticks) const
450 {
451   // Returns the shortest time among all time ticks
452
453   ticks->Sort() ; //Sort in accordance with times of ticks
454   TIter it(ticks) ;
455   AliPHOSTick * ctick = (AliPHOSTick *) it.Next() ;
456   Float_t time = ctick->CrossingTime(fTimeThreshold) ;    
457
458   AliPHOSTick * t ;  
459   while((t=(AliPHOSTick*) it.Next())){
460     if(t->GetTime() < time)  //This tick starts before crossing
461       *ctick+=*t ;
462     else
463       return time ;
464
465     time = ctick->CrossingTime(fTimeThreshold) ;    
466   }
467   return time ;
468 }
469
470 //____________________________________________________________________________ 
471 Bool_t AliPHOSDigitizer::Init()
472 {
473   // Makes all memory allocations
474   fInit = kTRUE ; 
475   AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName) ; 
476   if ( gime == 0 ) {
477     Fatal("Init" ,"Could not obtain the Getter object for file %s and event %s !", GetTitle(), fEventFolderName.Data()) ;  
478     return kFALSE;
479   } 
480   
481   const AliPHOSGeometry * geom = gime->PHOSGeometry() ;
482
483   fEmcCrystals = geom->GetNModules() * geom->GetNCristalsInModule() ;
484   
485   TString opt("Digits") ; 
486   if(gime->VersionExists(opt) ) { 
487     Error( "Init", "Give a version name different from %s", fEventFolderName.Data() ) ;
488     fInit = kFALSE ; 
489   }
490
491   // Post Digitizer to the white board
492   gime->PostDigitizer(this) ;
493   
494   fFirstEvent = 0 ; 
495   fLastEvent = fFirstEvent ; 
496   if (fManager) 
497     fInput = fManager->GetNinputs() ; 
498   else 
499     fInput           = 1 ;
500   
501   fInputFileNames  = new TString[fInput] ; 
502   fEventNames      = new TString[fInput] ; 
503   fInputFileNames[0] = GetTitle() ; 
504   fEventNames[0]     = fEventFolderName.Data() ; 
505   Int_t index ; 
506   for (index = 1 ; index < fInput ; index++) {
507     fInputFileNames[index] = dynamic_cast<AliStream*>(fManager->GetInputStream(index))->GetFileName(0); 
508     TString tempo = fManager->GetInputFolderName(index) ;
509     fEventNames[index] = tempo.Remove(tempo.Length()-1) ; // strip of the stream number added by fManager
510   }
511
512   //to prevent cleaning of this object while GetEvent is called
513   gime->PhosLoader()->GetDigitsDataLoader()->GetBaseTaskLoader()->SetDoNotReload(kTRUE);
514
515   return fInit ; 
516 }
517
518 //____________________________________________________________________________ 
519 void AliPHOSDigitizer::InitParameters()
520 {
521   // Set initial parameters Digitizer
522
523   fPinNoise           = 0.004 ;
524   fEMCDigitThreshold  = 0.012 ;
525   fCPVNoise           = 0.01;
526   fCPVDigitThreshold  = 0.09 ;
527   fTimeResolution     = 0.5e-9 ;
528   fTimeSignalLength   = 1.0e-9 ;
529   fDigitsInRun  = 0 ; 
530   fADCchanelEmc = 0.0015;        // width of one ADC channel in GeV
531   fADCpedestalEmc = 0.005 ;      //
532   fNADCemc = (Int_t) TMath::Power(2,16) ;  // number of channels in EMC ADC
533
534   fADCchanelCpv = 0.0012 ;          // width of one ADC channel in CPV 'popugais'
535   fADCpedestalCpv = 0.012 ;         // 
536   fNADCcpv = (Int_t) TMath::Power(2,12);      // number of channels in CPV ADC
537
538   fTimeThreshold = 0.001*10000000 ; //Means 1 MeV in terms of SDigits amplitude
539   SetEventRange(0,-1) ;
540     
541 }
542
543 //__________________________________________________________________
544 void AliPHOSDigitizer::MixWith(TString alirunFileName, TString eventFolderName)
545 {
546   // Allows to produce digits by superimposing background and signal event.
547   // It is assumed, that headers file with SIGNAL events is opened in 
548   // the constructor. 
549   // Sets the BACKGROUND event, with which the SIGNAL event is to be mixed 
550   // Thus we avoid writing (changing) huge and expensive 
551   // backgound files: all output will be writen into SIGNAL, i.e. 
552   // opened in constructor file. 
553   //
554   // One can open as many files to mix with as one needs.
555   // However only Sdigits with the same name (i.e. constructed with the same SDigitizer)
556   // can be mixed.
557
558   if( strcmp(fEventFolderName, "") == 0 )
559     Init() ;
560
561   if(fManager){
562     Warning("MixWith", "Cannot use this method with AliRunDigitizer\n" ) ;
563     return ;
564   }
565   // looking for file which contains AliRun
566   if (gSystem->AccessPathName(alirunFileName)) {// file does not exist
567     Error("MixWith", "File %s does not exist!", alirunFileName.Data()) ;
568     return ; 
569   }
570   // looking for the file which contains SDigits
571   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; 
572   TString fileName( gime->GetSDigitsFileName() ) ; 
573     if ( eventFolderName != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
574       fileName = fileName.ReplaceAll(".root", "") + "_" + eventFolderName + ".root" ;
575     if ( (gSystem->AccessPathName(fileName)) ) { 
576       Error("MixWith", "The file %s does not exist!", fileName.Data()) ;
577       return ;
578     }
579     // need to increase the arrays
580     TString tempo = fInputFileNames[fInput-1] ; 
581     delete [] fInputFileNames ; 
582     fInputFileNames = new TString[fInput+1] ; 
583     fInputFileNames[fInput-1] = tempo ; 
584  
585     tempo = fEventNames[fInput-1] ; 
586     delete [] fEventNames ; 
587     fEventNames = new TString[fInput+1] ; 
588     fEventNames[fInput-1] = tempo ; 
589
590     fInputFileNames[fInput] = alirunFileName ; 
591     fEventNames[fInput]     = eventFolderName ;
592     fInput++ ;
593 }
594
595 //__________________________________________________________________
596 void AliPHOSDigitizer::Print()const 
597 {
598   // Print Digitizer's parameters
599   Info("Print", "\n------------------- %s -------------", GetName() ) ; 
600   if( strcmp(fEventFolderName.Data(), "") != 0 ){
601     printf(" Writing Digits to branch with title  %s\n", fEventFolderName.Data()) ;
602     
603     Int_t nStreams ; 
604     if (fManager) 
605       nStreams =  GetNInputStreams() ;
606     else 
607       nStreams = fInput ; 
608     
609     Int_t index = 0 ;  
610     for (index = 0 ; index < nStreams ; index++) {  
611       TString tempo(fEventNames[index]) ; 
612       tempo += index ;
613       AliPHOSGetter * gime = AliPHOSGetter::Instance(fInputFileNames[index], tempo) ; 
614       TString fileName( gime->GetSDigitsFileName() ) ; 
615       if ( fEventNames[index] != AliConfig::fgkDefaultEventFolderName) // only if not the default folder name 
616         fileName = fileName.ReplaceAll(".root", "") + "_" + fEventNames[index]  + ".root" ;
617       printf ("Adding SDigits from %s %s\n", fInputFileNames[index].Data(), fileName.Data()) ; 
618     }
619     AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName) ; 
620     printf("\nWriting digits to %s", gime->GetDigitsFileName().Data()) ;
621     
622     printf("\nWith following parameters:\n") ;
623     printf("  Electronics noise in EMC (fPinNoise) = %f\n", fPinNoise ) ; 
624     printf("  Threshold  in EMC  (fEMCDigitThreshold) = %f\n", fEMCDigitThreshold ) ;  
625     printf("  Noise in CPV (fCPVNoise) = %f\n", fCPVNoise ) ; 
626     printf("  Threshold in CPV (fCPVDigitThreshold) = %f\n",fCPVDigitThreshold ) ;  
627     printf(" ---------------------------------------------------\n") ;   
628   }
629   else
630     Info("Print", "AliPHOSDigitizer not initialized" ) ;
631   
632 }
633
634 //__________________________________________________________________
635  void AliPHOSDigitizer::PrintDigits(Option_t * option)
636 {
637   // Print a table of digits
638   
639   AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName) ; 
640   TClonesArray * digits = gime->Digits() ; 
641   
642   Info("PrintDigits", "%d", digits->GetEntriesFast()) ; 
643   printf("\nevent %d", gAlice->GetEvNumber()) ;
644   printf("\n       Number of entries in Digits list %d", digits->GetEntriesFast() )  ;  
645
646  
647   if(strstr(option,"all")||strstr(option,"EMC")){  
648     //loop over digits
649     AliPHOSDigit * digit;
650     printf("\nEMC digits (with primaries):\n")  ;
651     printf("\n   Id  Amplitude    Time          Index Nprim: Primaries list \n") ;    
652     Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
653     Int_t index ;
654     for (index = 0 ; (index < digits->GetEntriesFast()) && 
655            (dynamic_cast<AliPHOSDigit *>(digits->At(index))->GetId() <= maxEmc) ; index++) {
656       digit = (AliPHOSDigit * )  digits->At(index) ;
657       if(digit->GetNprimary() == 0) 
658         continue;
659       printf("%6d  %8d    %6.5e %4d      %2d :",
660               digit->GetId(), digit->GetAmp(), digit->GetTime(), digit->GetIndexInList(), digit->GetNprimary()) ;  
661       Int_t iprimary;
662       for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
663         printf("%d ",digit->GetPrimary(iprimary+1) ) ; 
664       }    
665       printf("\n") ;
666     }
667   }
668   
669   if(strstr(option,"all")||strstr(option,"CPV")){
670     
671     //loop over CPV digits
672     AliPHOSDigit * digit;
673     printf("\nCPV digits (with primaries):\n")  ;
674     printf("\n   Id  Amplitude    Time          Index Nprim: Primaries list \n") ;    
675     Int_t maxEmc = gime->PHOSGeometry()->GetNModules()*gime->PHOSGeometry()->GetNCristalsInModule() ;
676     Int_t index ;
677     for (index = 0 ; index < digits->GetEntriesFast(); index++) {
678       digit = (AliPHOSDigit * )  digits->At(index) ;
679       if(digit->GetId() > maxEmc){
680         printf("%6d  %8d    %4d      %2d :",
681                 digit->GetId(), digit->GetAmp(), digit->GetIndexInList(), digit->GetNprimary()) ;  
682         Int_t iprimary;
683         for (iprimary=0; iprimary<digit->GetNprimary(); iprimary++) {
684           printf("%d ",digit->GetPrimary(iprimary+1) ) ; 
685         }    
686         printf("\n") ;
687       }
688     }
689   }
690
691 }
692
693 //__________________________________________________________________
694 Float_t AliPHOSDigitizer::TimeOfNoise(void) const
695 {  // Calculates the time signal generated by noise
696   //to be rewritten, now returns just big number
697   return 1. ;
698
699 }
700
701 //__________________________________________________________________
702 void AliPHOSDigitizer::Unload() 
703 {  
704   
705   Int_t i ; 
706   for(i = 1 ; i < fInput ; i++){
707     TString tempo(fEventNames[i]) ; 
708     tempo += i ;
709     AliPHOSGetter * gime = AliPHOSGetter::Instance(fInputFileNames[i], tempo) ; 
710     gime->PhosLoader()->UnloadSDigits() ; 
711   }
712   
713   AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName) ; 
714   gime->PhosLoader()->UnloadDigits() ; 
715 }
716
717 //____________________________________________________________________________
718 void AliPHOSDigitizer::WriteDigits()
719 {
720
721   // Makes TreeD in the output file. 
722   // Check if branch already exists: 
723   //   if yes, exit without writing: ROOT TTree does not support overwriting/updating of 
724   //      already existing branches. 
725   //   else creates branch with Digits, named "PHOS", title "...",
726   //      and branch "AliPHOSDigitizer", with the same title to keep all the parameters
727   //      and names of files, from which digits are made.
728
729   AliPHOSGetter * gime = AliPHOSGetter::Instance(GetTitle(), fEventFolderName) ; 
730   const TClonesArray * digits = gime->Digits() ; 
731   TTree * treeD = gime->TreeD();
732
733   // -- create Digits branch
734   Int_t bufferSize = 32000 ;    
735   TBranch * digitsBranch = treeD->Branch("PHOS",&digits,bufferSize);
736   digitsBranch->SetTitle(fEventFolderName);
737   digitsBranch->Fill() ;
738   
739   gime->WriteDigits("OVERWRITE");
740   gime->WriteDigitizer("OVERWRITE");
741
742   Unload() ; 
743
744 }