]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSGetter.cxx
Changed the way the Alarms are retrieved from the folder
[u/mrichter/AliRoot.git] / PHOS / AliPHOSGetter.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 /* $Log:
19    29.05.2001 Yuri Kharlov:
20               Everywhere reading the treese TTree->GetEvent(i)
21               is replaced by reading the branches TBranch->GetEntry(0)
22 */
23 /* $Log:
24    08.2002 Dmitri Peressounko:
25
26 */
27
28 //_________________________________________________________________________
29 //  A singleton. This class should be used in the analysis stage to get 
30 //  reconstructed objects: Digits, RecPoints, TrackSegments and RecParticles,
31 //  instead of directly reading them from galice.root file. This container 
32 //  ensures, that one reads Digits, made of these particular digits, RecPoints, 
33 //  made of these particular RecPoints, TrackSegments and RecParticles. 
34 //  This becomes non trivial if there are several identical branches, produced with
35 //  different set of parameters. 
36 //
37 //  An example of how to use (see also class AliPHOSAnalyser):
38 //  AliPHOSGetter * gime = AliPHOSGetter::GetInstance("galice.root","test") ;
39 //  for(Int_t irecp = 0; irecp < gime->NRecParticles() ; irecp++)
40 //     AliPHOSRecParticle * part = gime->RecParticle(1) ;
41 //     ................
42 //  gime->Event(event) ;    // reads new event from galice.root
43 //                  
44 //*-- Author: Yves Schutz (SUBATECH) & Dmitri Peressounko (RRC KI & SUBATECH)
45 //*--         Completely redesigned by Dmitri Peressounko March 2001  
46 //
47 //*-- YS June 2001 : renamed the original AliPHOSIndexToObject and make
48 //*--         systematic usage of TFolders without changing the interface        
49 //////////////////////////////////////////////////////////////////////////////
50
51 // --- ROOT system ---
52 #include "TFile.h"
53 #include "TTree.h"
54 #include "TROOT.h"
55 #include "TObjString.h"
56 #include "TFolder.h"
57 #include "TParticle.h"
58
59 // --- Standard library ---
60
61 // --- AliRoot header files ---
62 #include "AliRun.h"
63 #include "AliConfig.h"
64 #include "AliPHOSGetter.h"
65 #include "AliPHOS.h"
66 #include "AliPHOSDigitizer.h"
67 #include "AliPHOSSDigitizer.h"
68 #include "AliPHOSClusterizerv1.h"
69 #include "AliPHOSTrackSegmentMakerv1.h"
70 #include "AliPHOSTrackSegment.h"
71 #include "AliPHOSPIDv1.h" 
72 #include "AliPHOSGeometry.h"
73 #include "AliPHOSRaw2Digits.h"
74 //#include "AliPHOSCalibrationDB.h"
75 #include "AliPHOSBeamTestEvent.h"
76 ClassImp(AliPHOSGetter)
77   
78   AliPHOSGetter * AliPHOSGetter::fgObjGetter = 0 ; 
79   TFile * AliPHOSGetter::fFile = 0 ; 
80
81 //____________________________________________________________________________ 
82 AliPHOSGetter::AliPHOSGetter(const char* headerFile, const char* branchTitle, const Bool_t toSplit )
83 {
84   // This is the ctor called by GetInstance and the only one that can be used 
85
86   if( fHeaderFile.Contains("_") ) {
87     Fatal("AliPHOSGetter", "Invalid file name (_ not allowed) %s", fHeaderFile.Data() ) ;
88   }
89
90   //Initialize  all data
91
92   fFailed = kFALSE ;   
93   fDebug  = 0 ; 
94   fAlice  = 0 ; 
95   fBTE    = 0 ;
96
97   fToSplit    = toSplit ;
98   fHeaderFile = headerFile ; 
99
100   fPrimaries = new TObjArray(1) ;
101
102   fModuleFolder    = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Run/Configuration/Modules")); 
103   fPrimariesFolder = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/RunMC/Event/Data")); 
104   fHitsFolder      = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/RunMC/Event/Data/Hits")); 
105   fSDigitsFolder   = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/RunMC/Event/Data/SDigits")); 
106   fDigitsFolder    = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Run/Event/Data")); 
107   fRecoFolder      = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Run/Event/RecData")); 
108   fQAFolder        = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Run/Conditions/QA")); 
109   fTasksFolder     = dynamic_cast<TFolder*>(gROOT->FindObjectAny("Folders/Tasks")) ; 
110
111   //Set titles to branches and create PHOS specific folders
112   SetTitle(branchTitle) ;
113
114   if ( fHeaderFile != "aliroot"  ) { // to call the getter without a file
115     //open headers file
116     fFile = static_cast<TFile*>(gROOT->GetFile(fHeaderFile.Data() ) ) ;
117
118     if(!fFile) {    //if file was not opened yet, read gAlice
119       fFile = TFile::Open(fHeaderFile.Data(),"update") ;
120       if (!fFile->IsOpen()) {
121         Error("AliPHOSGetter", "Cannot open %s", fHeaderFile.Data() ) ; 
122         fFailed = kTRUE ;
123         return ;  
124       }
125     }
126     gAlice = dynamic_cast<AliRun *>(fFile->Get("gAlice")) ;
127   }
128   
129   if (!gAlice) {
130     Error("AliPHOSGetter", "Cannot find gAlice in %s", fHeaderFile.Data() ) ; 
131     fFailed = kTRUE ;
132     return ; 
133   }
134   if (!PHOS()) {
135     if (fDebug)
136       Info("AliPHOSGetter", "-> Posting PHOS to Folders") ; 
137     if (gAlice->GetDetector("PHOS")) {
138       AliConfig * conf = AliConfig::Instance() ; 
139       conf->Add(static_cast<AliDetector*>(gAlice->GetDetector("PHOS"))) ; 
140       conf->Add(static_cast<AliModule*>(gAlice->GetDetector("PHOS"))) ; 
141     }
142     else 
143       Error("AliPHOSGetter", "detector PHOS not found") ;    
144   }
145
146   fDebug=0;
147 }
148
149 //____________________________________________________________________________ 
150 AliPHOSGetter::~AliPHOSGetter(){
151
152   if (fPrimaries) {
153     fPrimaries->Delete() ; 
154     delete fPrimaries ; 
155   }
156
157   TFolder * phosF = dynamic_cast<TFolder *>(fSDigitsFolder->FindObject("PHOS")) ;
158   TCollection * folderslist = phosF->GetListOfFolders() ; 
159   TIter next(folderslist) ; 
160   TFolder * folder = 0 ; 
161   while ( (folder = static_cast<TFolder*>(next())) ) 
162     phosF->Remove(folder) ; 
163   
164   if (fFile) { 
165     fFile->Close() ;  
166     delete fFile ;
167     fFile = 0 ;
168   }
169   fgObjGetter = 0 ; 
170   
171 }
172
173 //____________________________________________________________________________ 
174 void AliPHOSGetter::CloseFile()
175 {
176   if(gAlice)
177     delete gAlice ;  
178   gAlice = 0 ; 
179   if(fAlice)
180     delete fAlice ; 
181   fAlice = 0 ; 
182 }
183
184 //____________________________________________________________________________ 
185 const TFolder * AliPHOSGetter::Folder(const TString what) const {
186
187   // returns the PHOS folder required by what
188   // what = hits, sdigits, digits
189
190   if ( what == "hits" ) 
191     return dynamic_cast<const TFolder *>(fHitsFolder->FindObject("PHOS")) ; 
192   else if ( what == "sdigits" ) 
193     return  dynamic_cast<const TFolder *>(fSDigitsFolder->FindObject("PHOS")) ; 
194   else if ( what == "digits" ) 
195     return  dynamic_cast<const TFolder *>(fDigitsFolder->FindObject("PHOS")) ; 
196   else {
197     Error("GetFolder", " %s illegal option (hits, sdigits, digits)", what.Data() ) ; 
198     return 0 ; 
199   }
200 }
201
202 //____________________________________________________________________________ 
203 AliPHOSGetter * AliPHOSGetter::GetInstance()
204 {
205   // Returns the pointer of the unique instance already defined
206   
207   if ( fgObjGetter ) {
208     return fgObjGetter ;
209   }
210   else {
211     //Warning("GetInstance", "not yet initialized") ;
212     return 0 ; 
213   }
214 }
215
216 //____________________________________________________________________________ 
217 AliPHOSGetter * AliPHOSGetter::GetInstance(const char* headerFile,
218                                            const char* branchTitle,
219                                            const Bool_t toSplit)
220 {
221   // Creates and returns the pointer of the unique instance
222   // Must be called only when the environment has changed
223
224   if(!fgObjGetter){
225     fgObjGetter = new AliPHOSGetter(headerFile,branchTitle,toSplit) ;
226     if(fgObjGetter->fFailed)
227       return 0;
228     else
229       return fgObjGetter ;
230   }
231
232   //First checks, if header file already opened
233   if(!fgObjGetter->fFile){
234      fgObjGetter = new AliPHOSGetter(headerFile,branchTitle,toSplit) ;
235     if(fgObjGetter->fFailed)
236       return 0;
237     else
238       return fgObjGetter ;
239   }
240
241   if(fgObjGetter->fHeaderFile.CompareTo(headerFile)==0){ //Opened the same header file
242     if((fgObjGetter->fBranchTitle.CompareTo(branchTitle) == 0)&&   //Open the same branch title
243        (toSplit==fgObjGetter->fToSplit)){                          //Nothing should be cleaned
244     }
245     else{ //Clean all data and AliPHOS...zers
246       if(fgObjGetter->fToSplit)
247         fgObjGetter->CloseSplitFiles() ;
248       fgObjGetter->CleanWhiteBoard() ;
249       fgObjGetter->fToSplit = toSplit ;
250       fgObjGetter->SetTitle(branchTitle) ;
251     }
252   }
253   else{  //Close already opened files, clean memory and open new header file
254     if(gAlice){ //should first delete gAlice, then close file
255       //Should be in dtor of PHOS, but if one changes path ...
256       fgObjGetter->fModuleFolder->Remove(fgObjGetter->fModuleFolder->FindObject("PHOS")) ; 
257       delete gAlice ;      
258     }
259     if(fgObjGetter->fFile){
260       fgObjGetter->fFile->Close() ;
261       fgObjGetter->fFile=0;
262     }
263     if(fgObjGetter->fToSplit)
264       fgObjGetter->CloseSplitFiles() ;
265     fgObjGetter->CleanWhiteBoard() ;    
266     fgObjGetter = new AliPHOSGetter(headerFile,branchTitle,toSplit) ;
267
268   }
269   return fgObjGetter ; 
270   
271 }
272
273 //____________________________________________________________________________ 
274 const Bool_t AliPHOSGetter::BranchExists(const TString recName) const
275 {
276   //Looks in the tree Tree"name" if branch with current name olready exists
277
278   TString filename("") ;
279   TString name, dataname, zername ;
280   if(recName == "SDigits"){
281     filename=fSDigitsFileName ;
282     name = "TreeS0" ;
283     dataname = "PHOS" ;
284     zername = "AliPHOSSDigitizer" ;
285   }
286   else
287     if(recName == "Digits"){
288       filename=fDigitsFileName ;
289       name = "TreeD0" ;
290       dataname = "PHOS" ;
291       zername = "AliPHOSDigitizer" ;
292     }
293     else
294       if(recName == "RecPoints"){
295         filename=fRecPointsFileName ;
296         name = "TreeR0" ;
297         dataname = "PHOSEmcRP" ;
298         zername = "AliPHOSClusterizer" ;
299       }
300       else
301         if(recName == "TrackSegments"){
302           filename=fTrackSegmentsFileName ;
303           name = "TreeR0" ;
304           dataname = "PHOSTS" ;
305           zername = "AliPHOSTrackSegmentMaker" ;
306         }        
307         else
308           if(recName == "RecParticles"){
309             filename= fRecParticlesFileName ;
310             name = "TreeR0" ;
311             dataname = "PHOSRP" ;
312             zername = "AliPHOSPID" ;
313           }
314           else
315             return kFALSE ;
316
317   TFile * file ;
318   TTree * tree ;
319   if(fToSplit){
320     file = static_cast<TFile*>(gROOT->GetFile(filename.Data() ) ) ;
321     if(!file)
322       file = TFile::Open(fSDigitsFileName.Data(),"update");
323   }
324   else
325     file = fFile ;
326
327   tree = (TTree *)file->Get(name.Data()) ;
328   
329   if(!tree ) 
330     return kFALSE ;
331
332   TObjArray * lob = static_cast<TObjArray*>(tree->GetListOfBranches()) ;
333   TIter next(lob) ; 
334   TBranch * branch = 0 ;  
335   TString titleName(fBranchTitle);
336   titleName+=":";
337   while ((branch = (static_cast<TBranch*>(next())))) {
338     TString branchName(branch->GetName() ) ; 
339     TString branchTitle(branch->GetTitle() ) ;  
340     if ( branchName.BeginsWith(dataname) && branchTitle.BeginsWith(fBranchTitle) ){  
341       Warning("BranchExists", "branch %s with title %s already exists in %s", dataname.Data(), fBranchTitle.Data(), name.Data() ) ;
342       return kTRUE ;
343     }
344     if ( branchName.BeginsWith(zername) &&  branchTitle.BeginsWith(titleName) ){
345       Warning("BranchExists", "branch AliPHOS... with title %s already exists in %s", branch->GetTitle(), name.Data() ) ;     
346       return kTRUE ; 
347     }
348   }
349   //We can't delete three if gAlice points to it... To be redisigned somehow???!!!
350   if(!fToSplit){
351     if(name.Contains("TreeS"))
352       if(tree!=gAlice->TreeS())
353         tree->Delete();
354     if(name.Contains("TreeD"))
355       if(tree!=gAlice->TreeD())
356         tree->Delete();
357     if(name.Contains("TreeR"))
358       if(tree!=gAlice->TreeR())
359         tree->Delete();    
360   }
361   return kFALSE ;
362   
363 }
364
365 //____________________________________________________________________________ 
366 void AliPHOSGetter::ListBranches(Int_t event) const  
367 {
368   
369   TBranch * branch = 0 ; 
370   if (gAlice->GetEvent(event) == -1)
371     return ; 
372   
373   TTree * t =  gAlice->TreeH() ; 
374   if(t){
375     Info("ListBranches", "-> ****** Hits    : ") ; 
376     TObjArray * lob = t->GetListOfBranches() ;
377     TIter next(lob) ; 
378     while ( (branch = static_cast<TBranch*>(next())) )
379       Info("ListBranches", "             %s", branch->GetName()) ; 
380   } else
381     Warning("ListBranches", "TreeH not found for event %d", event ) ;  
382   
383   t = gAlice->TreeS() ;
384   if(t){
385     Info("ListBranches", "-> ****** SDigits : ") ; 
386     TObjArray * lob = t->GetListOfBranches() ;
387     TIter next(lob) ; 
388     while ( (branch = static_cast<TBranch*>(next())) )
389       Info("ListBranches", "             %s %s", branch->GetName(), branch->GetTitle()) ; 
390   } else 
391     Warning("ListBranches", "TreeS not found for event %d", event)  ;  
392   
393   
394   t = gAlice->TreeD() ;
395   if(t){
396     Info("ListBranches", "-> ****** Digits  : ") ; 
397     TObjArray * lob = t->GetListOfBranches() ;
398     TIter next(lob) ; 
399     while ( (branch = static_cast<TBranch*>(next())) )
400       Info("ListBranches", "             %s %s", branch->GetName(), branch->GetTitle()) ; 
401   } else 
402     Warning("ListBranches", "TreeD not found for event %d", event) ;  
403   
404
405   t = gAlice->TreeR() ;
406   if(t){
407     Info("ListBranches", "-> ****** Recon   : ") ; 
408     TObjArray * lob = t->GetListOfBranches() ;
409     TIter next(lob) ; 
410     while ( (branch = static_cast<TBranch*>(next())) )
411      Info("ListBranches", "             %s %s", branch->GetName(), branch->GetTitle()) ; 
412   } else 
413     Warning("ListBranches", "TreeR not found for event %d", event) ;  
414   
415 }
416
417 //____________________________________________________________________________ 
418 void AliPHOSGetter::NewBranch(TString name, Int_t event)  
419 {
420   fBranchTitle = fSDigitsTitle = fDigitsTitle = fRecPointsTitle = fTrackSegmentsTitle = fRecParticlesTitle =  name ; 
421   Event(event) ; 
422 }
423
424 //____________________________________________________________________________ 
425 Bool_t AliPHOSGetter::NewFile(TString name)  
426 {
427   fHeaderFile = name ; 
428   fFile->Close() ; 
429   fFailed = kFALSE; 
430
431   fFile = static_cast<TFile*>(gROOT->GetFile(fHeaderFile.Data() ) ) ;
432   if(!fFile) {    //if file was not opened yet, read gAlice
433     fFile = TFile::Open(fHeaderFile.Data(),"update") ;
434     if (!fFile->IsOpen()) {
435       Error("NewFile", "Cannot open %s", fHeaderFile.Data() ) ; 
436       fFailed = kTRUE ;
437       return fFailed ;  
438     }
439     gAlice = static_cast<AliRun *>(fFile->Get("gAlice")) ;
440   } 
441   
442   if (!gAlice) {
443     Error("AliPHOSGetter", "Cannot find gAlice in %s", fHeaderFile.Data() ) ; 
444     fFailed = kTRUE ;
445     return fFailed ; 
446   }
447   return fFailed ; 
448 }
449
450 //____________________________________________________________________________ 
451 const AliPHOS * AliPHOSGetter::PHOS() 
452 {
453   // returns the PHOS object 
454   AliPHOS * phos = dynamic_cast<AliPHOS*>(fModuleFolder->FindObject("PHOS")) ;  
455   if (!phos) 
456     if (fDebug)
457       Warning("PHOS", "-> PHOS module not found in Folders") ; 
458   return phos ; 
459 }  
460
461 //____________________________________________________________________________ 
462 const AliPHOSGeometry * AliPHOSGetter::PHOSGeometry() 
463 {
464   AliPHOSGeometry * rv = 0 ; 
465   if (PHOS() )
466     rv =  PHOS()->GetGeometry() ;
467   return rv ; 
468
469
470 //____________________________________________________________________________ 
471 const Bool_t AliPHOSGetter::PostPrimaries(void) const 
472 {  //------- Primaries ----------------------
473
474   // the hierarchy is //Folders/RunMC/Event/Data/Primaries
475   
476   TFolder * primariesFolder = dynamic_cast<TFolder*>(fPrimariesFolder->FindObject("Primaries")) ; 
477   if ( !primariesFolder ) {
478     if (fDebug) {
479       Warning("PostPrimaries", "-> Folder //%s/Primaries/ not found!", fPrimariesFolder->GetName()) ;
480       Info("PostPrimaries", "-> Adding Folder //%s/Primaries", fPrimariesFolder->GetName()) ;
481     }
482     primariesFolder = fPrimariesFolder->AddFolder("Primaries", "Primaries particles from TreeK") ; 
483   }    
484   TClonesArray *primaries=  new TClonesArray("TParticle",1000) ;
485   primaries->SetName("Primaries") ;
486   primariesFolder->Add(primaries) ; 
487   
488   return kTRUE;
489
490
491 //____________________________________________________________________________ 
492 TObject** AliPHOSGetter::PrimariesRef(void) const 
493 {  //------- Primaries ----------------------
494
495   
496   // the hierarchy is //Folders/RunMC/Event/Data/Primaries
497   if ( !fPrimariesFolder ) {
498     Fatal("PrimariesRef", "Folder //%s not found", fPrimariesFolder) ;
499   }    
500  
501   TFolder * primariesFolder = dynamic_cast<TFolder *>(fPrimariesFolder->FindObject("Primaries")) ;
502   if ( !primariesFolder ) {
503     Fatal("PrimariesRef", "Folder //%s/Primaries/ not found", fPrimariesFolder) ;  
504   }
505  
506   TObject * p = primariesFolder->FindObject("Primaries") ;
507   if(!p) {
508     Fatal("PrimariesRef","%s /Primaries not found !", primariesFolder->GetName() ) ; 
509   }
510
511   return primariesFolder->GetListOfFolders()->GetObjectRef(p) ;
512 }
513
514 //____________________________________________________________________________ 
515 const Bool_t AliPHOSGetter::PostHits(void) const 
516 {  //------- Hits ----------------------
517
518   // the hierarchy is //Folders/RunMC/Event/Data/PHOS/Hits
519   
520   TFolder * phosFolder = dynamic_cast<TFolder*>(fHitsFolder->FindObject("PHOS")) ; 
521   if ( !phosFolder ) {
522     if (fDebug) {
523       Warning("PostHits", "-> Folder //%s/PHOS/ not found!", fHitsFolder) ;
524       Info("PostHits", "-> Adding Folder //%s/PHOS/", fHitsFolder) ;
525     }
526     phosFolder = fHitsFolder->AddFolder("PHOS", "Hits from PHOS") ; 
527   }    
528   TClonesArray *hits=  new TClonesArray("AliPHOSHit",1000) ;
529   hits->SetName("Hits") ;
530   phosFolder->Add(hits) ; 
531   
532   return kTRUE;
533
534
535 //____________________________________________________________________________ 
536 TObject** AliPHOSGetter::HitsRef(void) const 
537 {  //------- Hits ----------------------
538
539   
540   // the hierarchy is //Folders/RunMC/Event/Data/PHOS/Hits
541   if ( !fHitsFolder ) {
542     Fatal("HitsRef", "Folder //%s not found !", fHitsFolder) ;
543   }    
544  
545   TFolder * phosFolder = dynamic_cast<TFolder *>(fHitsFolder->FindObject("PHOS")) ;
546   if ( !phosFolder ) {
547     Fatal("HitsRef", "Folder //%s/PHOS/ not found !", fHitsFolder) ;  
548   }
549  
550   TObject * h = phosFolder->FindObject("Hits") ;
551   if(!h) {
552     Fatal("HitsRef", "%s/Hits not fount !", phosFolder->GetName() ) ; 
553   }
554
555   return phosFolder->GetListOfFolders()->GetObjectRef(h) ;
556 }
557
558 //____________________________________________________________________________ 
559 const Bool_t AliPHOSGetter::PostSDigits(const char * name, const char * headerFile) const 
560 {  //---------- SDigits -------------------------
561
562   
563   // the hierarchy is //Folders/RunMC/Event/Data/PHOS/SDigits/headerFile/sdigitsname
564   // because you can have sdigits from several hit files for mixing
565   
566   TFolder * phosFolder = dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("PHOS")) ;
567   if ( !phosFolder ) {
568     if (fDebug) {
569       Warning("PostSDigits", "-> Folder //%s/PHOS/ not found!", fSDigitsFolder) ;
570       Info("PostSDigits", "-> Adding Folder //%s/PHOS/", fHitsFolder) ;
571     }
572     phosFolder = fSDigitsFolder->AddFolder("PHOS", "SDigits from PHOS") ; 
573   }    
574
575   
576   TString subdir(headerFile) ;
577   if(fToSplit){
578     subdir.Remove(subdir.Last('/')+1,subdir.Length()) ;
579     subdir.ReplaceAll("/","_") ; 
580     subdir+="PHOS.SDigits." ;
581     if(name && (strcmp(name,"Default")!=0)){
582       subdir+=name ;
583       subdir+="." ;
584     }
585     subdir+="root" ;
586   }
587     
588   TFolder * phosSubFolder = dynamic_cast<TFolder*>(phosFolder->FindObject(subdir)) ; 
589   if ( !phosSubFolder ) 
590     phosSubFolder = phosFolder->AddFolder(subdir, ""); 
591   
592
593   TObject * sd  = phosSubFolder->FindObject(name); 
594   if ( !sd ) {
595     TClonesArray * sdigits = new TClonesArray("AliPHOSDigit",1) ;
596     sdigits->SetName(name) ;
597     phosSubFolder->Add(sdigits) ;
598   }
599   
600   return kTRUE;
601
602 //____________________________________________________________________________ 
603 TObject** AliPHOSGetter::SDigitsRef(const char * name, const char * foldername) const 
604 {  //------- SDigits ----------------------
605   
606   // the hierarchy is //Folders/RunMC/Event/Data/PHOS/SDigits/filename/SDigits
607
608   if ( !fSDigitsFolder ) {
609     Fatal("SDigitsRef", "Folder //%s not found !", fSDigitsFolder) ;
610   }    
611  
612   TFolder * phosFolder = static_cast<TFolder *>(fSDigitsFolder->FindObject("PHOS")) ;
613   if ( !phosFolder ) {
614     Fatal("SDigitsRef", "Folder //%s/PHOS not found !", fSDigitsFolder) ;
615   }
616
617   TFolder * phosSubFolder = 0 ;
618
619   phosSubFolder = dynamic_cast<TFolder *>(phosFolder->FindObject(foldername)) ;
620   
621   if(!phosSubFolder) {
622     Fatal("SDigitsRef", "Folder //Folders/RunMC/Event/Data/PHOS/%s not found !", foldername) ;
623   }
624
625   TObject * dis = phosSubFolder->FindObject(name) ;
626   if(!dis){
627     Fatal("SDigitsRef", "object %s not found !", name) ; 
628   }
629
630   return phosSubFolder->GetListOfFolders()->GetObjectRef(dis) ;
631
632 }
633
634 //____________________________________________________________________________ 
635 const Bool_t AliPHOSGetter::PostSDigitizer(AliPHOSSDigitizer * sdigitizer) const 
636 {  //---------- SDigitizer -------------------------
637     
638   // the hierarchy is //Folders/Tasks/SDigitizer/PHOS/sdigitsname
639
640
641   TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ; 
642
643   if ( !sd ) {
644     Error("PostDigitizer", "Task //%s/SDigitizer not found !", fTasksFolder) ;
645     return kFALSE ;
646   }        
647   TTask * phos = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("PHOS")) ; 
648   if ( !phos )  {
649     if (fDebug) {
650       Warning("PostSDigitizer", "->//%s/SDigitizer/PHOS/ not found!", fTasksFolder) ;  
651       Info("PostSDigitizer", "-> Adding //%s/SDigitizer/PHOS/", fTasksFolder) ;
652     }
653     phos = new TTask("PHOS", "") ; 
654     sd->Add(phos) ; 
655   } 
656   AliPHOSSDigitizer * phossd  = dynamic_cast<AliPHOSSDigitizer *>(phos->GetListOfTasks()->FindObject( sdigitizer->GetName() )); 
657   if (phossd) { 
658     if (fDebug)
659       Info("PostSDigitizer", "-> Task %s already exists", sdigitizer->GetName()) ; 
660     phos->GetListOfTasks()->Remove(phossd) ;
661   }
662   phos->Add(sdigitizer) ;       
663   return kTRUE; 
664   
665 }
666
667 //____________________________________________________________________________ 
668 TObject** AliPHOSGetter::SDigitizerRef(const char * name) const 
669 {  
670
671   TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ; 
672   if ( !sd ) {
673     Fatal("SDigitizerRef", "Task //%s/SDigitizer not found !", fTasksFolder) ;
674   }        
675
676   TTask * phos = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("PHOS")) ; 
677   if ( !phos )  {
678     Fatal("SDigitizerRef", "//%s/SDigitizer/PHOS not found !", fTasksFolder) ;
679   }        
680
681   TTask * task = dynamic_cast<TTask*>(phos->GetListOfTasks()->FindObject(name)) ; 
682
683   return phos->GetListOfTasks()->GetObjectRef(task) ;
684
685 }
686
687 //____________________________________________________________________________ 
688 const Bool_t AliPHOSGetter::PostSDigitizer(const char * name, const char * file) const 
689 {  //---------- SDigitizer -------------------------
690   
691  // the hierarchy is //Folders/Tasks/SDigitizer/PHOS/sdigitsname
692
693   TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ; 
694   if ( !sd ) {
695     Error("PostSDigitizer", "Task //%s/SDigitizer not found !", fTasksFolder) ;
696     return kFALSE ;
697   }        
698
699   TTask * phos = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("PHOS")) ; 
700   if ( !phos )  {
701     if (fDebug) {
702       Error("PostSDigitizer", "->  //%s/SDigitizer/PHOS/ not found!", fTasksFolder) ;
703       Info("PostSDigitizer", "-> Adding  //%s/SDigitizer/PHOS", fTasksFolder) ;
704     }
705     phos = new TTask("PHOS", "") ; 
706     sd->Add(phos) ; 
707   } 
708
709   TString sdname(name) ;
710   sdname.Append(":") ;
711   sdname.Append(file);
712   sdname.ReplaceAll("/","_") ; 
713   AliPHOSSDigitizer * phossd  = dynamic_cast<AliPHOSSDigitizer *>(phos->GetListOfTasks()->FindObject( sdname )); 
714   if (!phossd) {
715     phossd = new AliPHOSSDigitizer() ;  
716     //Note, we can not call constructor with parameters: it will call Getter and scew up everething
717     phossd->SetName(sdname) ;
718     phossd->SetTitle(file) ;
719     phos->Add(phossd) ; 
720   }
721   return kTRUE; 
722   
723 }
724
725 //____________________________________________________________________________ 
726 const Bool_t AliPHOSGetter::PostDigits(const char * name) const 
727 {  //---------- Digits -------------------------
728
729   // the hierarchy is //Folders/Run/Event/Data/PHOS/SDigits/name
730
731   TFolder * phosFolder  = dynamic_cast<TFolder*>(fDigitsFolder->FindObject("PHOS")) ;
732
733   if ( !phosFolder ) {
734     if (fDebug) {
735       Warning("PostDigitizer", "-> Folder //%s/PHOS/ not found!", fDigitsFolder) ;
736       Info("PostDigitizer", "-> Adding Folder //%s/PHOS/", fDigitsFolder) ;
737     }
738     phosFolder = fDigitsFolder->AddFolder("PHOS", "Digits from PHOS") ;  
739   }    
740  
741   TObject*  dig = phosFolder->FindObject( name ) ;
742   if ( !dig ) {
743     TClonesArray * digits = new TClonesArray("AliPHOSDigit",1000) ;
744     digits->SetName(name) ;
745     phosFolder->Add(digits) ;  
746   }
747   return kTRUE; 
748 }
749
750 //____________________________________________________________________________ 
751 TObject** AliPHOSGetter::DigitsRef(const char * name) const 
752 { //------- Digits ----------------------
753   
754   // the hierarchy is //Folders/Run/Event/Data/PHOS/Digits/name
755
756   if ( !fDigitsFolder ) {
757     Fatal("DigitsRef", "Folder //%s not found !", fDigitsFolder) ;
758   }    
759   
760   TFolder * phosFolder  = dynamic_cast<TFolder*>(fDigitsFolder->FindObject("PHOS")) ; 
761   if ( !phosFolder ) {
762     Fatal("DigitsRef", "Folder //%s/PHOS/ not found !", fDigitsFolder) ;
763   }    
764
765   TObject * d = phosFolder->FindObject(name) ;
766   if(!d) {
767     Fatal("DigitsRef", "object %s not found !", name) ; 
768   }
769
770   return phosFolder->GetListOfFolders()->GetObjectRef(d) ;
771
772 }
773
774 //____________________________________________________________________________ 
775 const Bool_t AliPHOSGetter::PostDigitizer(AliPHOSDigitizer * digitizer) const 
776 {  //---------- Digitizer -------------------------
777   
778   TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ; 
779
780   if ( !sd ) {
781     Error("PostDigitizer", "Task //%s/Digitizer not found !", fTasksFolder) ;
782     return kFALSE ;
783   }        
784   TTask * phos = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("PHOS")) ; 
785   if ( !phos )  {
786     if (fDebug) {
787       Error("PostDigitizer", "//%s/Digitizer/PHOS not found!", fTasksFolder) ;
788       Info("PostDigitizer", "Adding //%s/Digitizer/PHOS", fTasksFolder) ; 
789     }
790     phos = new TTask("PHOS", "") ; 
791     sd->Add(phos) ; 
792   } 
793
794     AliPHOSDigitizer * phosd = dynamic_cast<AliPHOSDigitizer*>(phos->GetListOfTasks()->FindObject(digitizer->GetName())) ; 
795     if (phosd) { 
796       phosd->Delete() ;
797       phos->GetListOfTasks()->Remove(phosd) ;
798     }
799     phos->Add(digitizer) ; 
800     return kTRUE; 
801 }  
802
803 //____________________________________________________________________________ 
804 const Bool_t AliPHOSGetter::PostDigitizer(const char * name) const 
805 {  //---------- Digitizer -------------------------
806   
807  // the hierarchy is //Folders/Tasks/SDigitizer/PHOS/sdigitsname
808
809   TTask * d  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ; 
810   if ( !d ) {
811     Error("PostDigitizer", "Task //%s/Digitizer not found !", fTasksFolder) ;
812     return kFALSE ;
813   }        
814
815   TTask * phos = dynamic_cast<TTask*>(d->GetListOfTasks()->FindObject("PHOS")) ; 
816   if ( !phos )  {
817     if (fDebug) {
818       Warning("PostDigitizer", "//%s/Digitizer/PHOS not found!", fTasksFolder) ; 
819       Info("PostDigitizer", "Adding //%s/Digitizer/PHOS", fTasksFolder) ;
820     }
821     phos = new TTask("PHOS", "") ; 
822     d->Add(phos) ; 
823
824
825   TTask * phosd = dynamic_cast<TTask*>(phos->GetListOfTasks()->FindObject(fDigitsTitle)) ; 
826   if (!phosd) { 
827     if(strcmp(name, "Digitizer")==0){
828       phosd = new AliPHOSDigitizer() ;
829       phosd->SetName(fDigitsTitle) ;
830       phosd->SetTitle(fHeaderFile) ;
831       phos->Add(phosd) ;
832     } 
833     else{
834       phosd = new AliPHOSRaw2Digits() ;
835       phosd->SetName(fDigitsTitle) ;
836       phosd->SetTitle(fHeaderFile) ;
837       phos->Add(phosd) ;
838     }      
839   }
840   return kTRUE;  
841 }
842
843 //____________________________________________________________________________ 
844 TObject** AliPHOSGetter::DigitizerRef(const char * name) const 
845 {  
846   TTask * sd  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ; 
847   if ( !sd ) {
848     Fatal("DigitizerRef", "Task //%s/Digitizer not found !", fTasksFolder) ;
849   }        
850
851   TTask * phos = dynamic_cast<TTask*>(sd->GetListOfTasks()->FindObject("PHOS")) ; 
852   if ( !phos )  {
853     Fatal("DigitizerRef", "//%s/Digitizer/PHOS", fTasksFolder) ;
854   }        
855
856   TTask * task = dynamic_cast<TTask*>(phos->GetListOfTasks()->FindObject(name)) ; 
857
858   return phos->GetListOfTasks()->GetObjectRef(task) ;
859
860 }
861  
862 //____________________________________________________________________________ 
863 const Bool_t AliPHOSGetter::PostRecPoints(const char * name) const 
864 { // -------------- RecPoints -------------------------------------------
865   
866   // the hierarchy is //Folders/Run/Event/RecData/PHOS/EMCARecPoints/name
867   // the hierarchy is //Folders/Run/Event/RecData/PHOS/CPVRecPoints/name
868
869   TFolder * phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS")) ; 
870   
871   if ( !phosFolder ) {
872     if (fDebug) {
873       Warning("PostRecPoints", "-> Folder //%s/PHOS/ not found!", fRecoFolder->GetName()) ;
874       Info("PostRecPoints", "-> Adding Folder //%s/PHOS/", fRecoFolder->GetName()) ;
875     }
876     phosFolder = fRecoFolder->AddFolder("PHOS", "Reconstructed data from PHOS") ;  
877   }    
878   
879   // EMCA RecPoints 
880   TFolder * phosRPoEMCAFolder  = dynamic_cast<TFolder*>(phosFolder->FindObject("EMCARecPoints")) ;
881   if ( !phosRPoEMCAFolder ) {
882     if (fDebug) {
883       Warning("PostRecPoints", "-> Folder //%s/PHOS/EMCARecPoints/ not found!", fRecoFolder->GetName()) ;
884       Info("PostRecPoints", "-> Adding Folder //%s/PHOS/EMCARecPoints", fRecoFolder->GetName()) ;
885     }
886     phosRPoEMCAFolder = phosFolder->AddFolder("EMCARecPoints", "EMCA RecPoints from PHOS") ;  
887   }    
888   
889   TObject * erp = phosFolder->FindObject( name ) ;
890   if ( !erp )   {
891     TObjArray * emcrp = new TObjArray(100) ;
892     emcrp->SetName(name) ;
893     phosRPoEMCAFolder->Add(emcrp) ;  
894   }
895
896   // CPV RecPoints 
897   TFolder * phosRPoCPVFolder  = dynamic_cast<TFolder*>(phosFolder->FindObject("CPVRecPoints")) ;
898   if ( !phosRPoCPVFolder ) {
899     if (fDebug) {
900       Warning("PostRecPoints", "-> Folder //%s/PHOS/CPVRecPoints/ not found!", fRecoFolder->GetName()) ;
901       Info("PostRecPoints", "-> Adding Folder //%s/PHOS/CPVRecPoints/", fRecoFolder->GetName()) ;
902     }
903     phosRPoCPVFolder = phosFolder->AddFolder("CPVRecPoints", "CPV RecPoints from PHOS") ;  
904   }    
905   
906   TObject * crp =  phosRPoCPVFolder->FindObject( name ) ;
907   if ( !crp )   {
908     TObjArray * cpvrp = new TObjArray(100) ;
909     cpvrp->SetName(name) ;
910     phosRPoCPVFolder->Add(cpvrp) ;  
911   }
912   return kTRUE; 
913 }
914
915 //____________________________________________________________________________ 
916 TObject** AliPHOSGetter::EmcRecPointsRef(const char * name) const 
917 { // -------------- RecPoints -------------------------------------------
918   
919   // the hierarchy is //Folders/Run/Event/RecData/PHOS/EMCARecPoints/name
920    
921   if ( !fRecoFolder ) {
922     Fatal("EmcRecPointsRef", "Folder //%s not found !", fRecoFolder->GetName() ) ;
923   }    
924
925   TFolder * phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/EMCARecPoints")) ; 
926   if ( !phosFolder ) {
927      Fatal("EmcRecPointsRef", "Folder //%s/PHOS/EMCARecPoints/ not found !", fRecoFolder->GetName() ) ;
928   }    
929
930
931   TObject * erp = phosFolder->FindObject(name ) ;
932   if ( !erp )   {
933     Fatal("EmcRecPointsRef", "object %s not found !", name) ; 
934   }
935   return phosFolder->GetListOfFolders()->GetObjectRef(erp) ;
936   
937
938
939 //____________________________________________________________________________ 
940 TObject** AliPHOSGetter::CpvRecPointsRef(const char * name) const 
941 { // -------------- RecPoints -------------------------------------------
942   
943   // the hierarchy is //Folders/Run/Event/RecData/PHOS/CPVRecPoints/name
944    
945   if ( !fRecoFolder ) {
946     Fatal("CpvRecPointsRef", "Folder //%s not found !", fRecoFolder->GetName() ) ;
947   }    
948
949   TFolder * phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/CPVRecPoints")) ; 
950   if ( !phosFolder ) {
951     Fatal("CpvRecPointsRef", "Folder //%s/PHOS/CPVRecPoints/ not found !", fRecoFolder->GetName() ) ;
952   }    
953
954   TObject * crp = phosFolder->FindObject(name ) ;
955   if ( !crp )   {
956     Fatal("CpvRecPointsRef", "object %s nott found", name ) ; 
957   }
958   return phosFolder->GetListOfFolders()->GetObjectRef(crp) ;
959   
960
961
962 //____________________________________________________________________________ 
963 const Bool_t AliPHOSGetter::PostClusterizer(AliPHOSClusterizer * clu) const 
964 { // ------------------ AliPHOSClusterizer ------------------------
965   
966   // the hierarchy is //Folders/Tasks/Reconstructioner/PHOS/sdigitsname
967
968   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
969
970   if ( !tasks ) {
971     Error("PostClusterizer", "Task //%s/Reconstructioner not found !", fTasksFolder) ;
972     return kFALSE ;
973   }        
974         
975   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
976   if ( !phos )  {
977     if (fDebug) {
978       Warning("PostClusterizer", "//%s/Reconstructioner/PHOS not found!", fTasksFolder) ; 
979       Info("PostClusterizer", "Adding //%s/Reconstructioner/PHOS", fTasksFolder) ; 
980     }
981     phos = new TTask("PHOS", "") ; 
982     tasks->Add(phos) ; 
983   } 
984
985   AliPHOSClusterizer * phoscl = dynamic_cast<AliPHOSClusterizer*>(phos->GetListOfTasks()->FindObject(clu->GetName())) ; 
986   if (phoscl) { 
987     if (fDebug)
988       Info("PostClusterizer", "Task %s already exists", clu->GetName()) ; 
989     phoscl->Delete() ; 
990     phos->GetListOfTasks()->Remove(phoscl) ;
991   }
992   phos->Add(clu) ;      
993   return kTRUE; 
994
995
996 //____________________________________________________________________________ 
997 TObject** AliPHOSGetter::ClusterizerRef(const char * name) const 
998 { // ------------------ AliPHOSClusterizer ------------------------
999   
1000   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1001
1002   if ( !tasks ) {
1003     Fatal("ClusterizerRef", "Task //%s/Reconstructioner not found !", fTasksFolder->GetName() ) ;
1004   }        
1005         
1006   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1007   if ( !phos )  {
1008     Fatal("ClusterizerRef", " //%s/Reconstructioner/PHOS not founf !", fTasksFolder->GetName() ) ; 
1009   }   
1010
1011   TList * l = phos->GetListOfTasks() ; 
1012   TIter it(l) ;
1013   TTask * task ;
1014   TTask * clu = 0 ;
1015   TString cluname(name) ;
1016   cluname+=":clu" ;
1017   while((task = static_cast<TTask *>(it.Next()) )){
1018     TString taskname(task->GetName()) ;
1019     if(taskname.BeginsWith(cluname)){
1020       clu = task ;
1021       break ;
1022     }
1023   }
1024
1025   if(!clu) {
1026     Fatal("ClusterizerRef", "Task //%s/Reconstructioner/clusterizer/%s not found", fTasksFolder->GetName(), name) ;
1027   }
1028
1029   return l->GetObjectRef(clu) ;
1030
1031 }
1032
1033 //____________________________________________________________________________ 
1034 const Bool_t AliPHOSGetter::PostClusterizer(const char * name) const 
1035 { // ------------------ AliPHOSClusterizer ------------------------
1036
1037   // the hierarchy is //Folders/Tasks/Reconstructioner/PHOS/sdigitsname
1038   
1039   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1040
1041   if ( !tasks ) {
1042     Error("PostClusterizer", "Task//%s/Reconstructioner not found !", fTasksFolder) ; 
1043     return kFALSE ;
1044   }        
1045   
1046   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1047   if ( !phos )  {
1048     if (fDebug) {
1049       Warning("PostClusterizer", "//%s/Reconstructioner/PHOS not found!", fTasksFolder) ;
1050       Info("PostClusterizer", "Adding //%s/Reconstructioner/PHOS", fTasksFolder) ;
1051     }
1052     phos = new TTask("PHOS", "") ; 
1053     tasks->Add(phos) ; 
1054   } 
1055
1056   TList * l = phos->GetListOfTasks() ;   
1057   TIter it(l) ;
1058   TString clun(name) ;
1059   clun+=":clu" ; 
1060   TTask * task ;
1061   while((task = static_cast<TTask *>(it.Next()) )){
1062     TString taskname(task->GetName()) ;
1063     if(taskname.BeginsWith(clun))
1064       return kTRUE ;
1065   }
1066
1067   AliPHOSClusterizerv1 * phoscl = new AliPHOSClusterizerv1() ;
1068   clun+="-v1" ; 
1069   phoscl->SetName(clun) ;
1070   phoscl->SetTitle(fHeaderFile) ;
1071   phos->Add(phoscl) ;
1072   return kTRUE; 
1073   
1074 }
1075
1076 //____________________________________________________________________________ 
1077 const Bool_t AliPHOSGetter::PostTrackSegments(const char * name) const 
1078 { // ---------------TrackSegments -----------------------------------
1079   
1080   // the hierarchy is //Folders/Run/Event/RecData/PHOS/TrackSegments/name
1081
1082   TFolder * phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS")) ; 
1083   
1084   if ( !phosFolder ) {
1085     if (fDebug) {
1086       Warning("PostTrackSegments", "-> Folder //%s/PHOS/ not found", fRecoFolder->GetName()) ;
1087       Info("PostTrackSegments", "-> Adding Folder //%s/PHOS", fRecoFolder->GetName()) ;
1088     }
1089     phosFolder = fRecoFolder->AddFolder("PHOS", "Reconstructed data from PHOS") ;  
1090   }    
1091
1092   TFolder * phosTSFolder  = dynamic_cast<TFolder*>(phosFolder->FindObject("TrackSegments")) ;
1093   if ( !phosTSFolder ) {
1094     if (fDebug) {
1095       Warning("PostTrackSegments", "-> Folder //%s/PHOS/TrackSegments/ not found!", fRecoFolder->GetName() ) ; 
1096       Info("PostTrackSegments", "-> Adding Folder //%s/PHOS/TrackSegments/", fRecoFolder->GetName()) ; 
1097     }
1098     phosTSFolder = phosFolder->AddFolder("TrackSegments", "TrackSegments from PHOS") ;  
1099   }    
1100   
1101   TObject * tss =  phosTSFolder->FindObject( name ) ;
1102   if (!tss) {
1103     TClonesArray * ts = new TClonesArray("AliPHOSTrackSegment",100) ;
1104     ts->SetName(name) ;
1105     phosTSFolder->Add(ts) ;  
1106   }
1107   return kTRUE; 
1108
1109
1110 //____________________________________________________________________________ 
1111 TObject** AliPHOSGetter::TrackSegmentsRef(const char * name) const 
1112 { // ---------------TrackSegments -----------------------------------
1113   
1114   // the hierarchy is //Folders/Run/Event/RecData/PHOS/TrackSegments/name
1115
1116  if ( !fRecoFolder ) {
1117     Fatal("TrackSegmentsRef", "Folder //%s not found !", fRecoFolder->GetName() ) ;
1118   }    
1119
1120   TFolder * phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/TrackSegments")) ; 
1121   if ( !phosFolder ) {
1122     Fatal("TrackSegmentsRef", "Folder //%s/PHOS/TrackSegments/ not found !", fRecoFolder->GetName() ) ;
1123   }    
1124   
1125   TObject * tss =  phosFolder->FindObject(name) ;
1126   if (!tss) {
1127     Fatal("TrackSegmentsRef", "object %s not found !", name) ;  
1128   }
1129   return phosFolder->GetListOfFolders()->GetObjectRef(tss) ;
1130
1131
1132 //____________________________________________________________________________ 
1133 const Bool_t AliPHOSGetter::PostTrackSegmentMaker(AliPHOSTrackSegmentMaker * tsmaker) const 
1134 { //------------Track Segment Maker ------------------------------
1135   
1136   // the hierarchy is //Folders/Tasks/Reconstructioner/PHOS/sdigitsname
1137
1138   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1139
1140   if ( !tasks ) {
1141     Error("PostTrackSegmentMaker", "Task //%s/Reconstructioner not found !", fTasksFolder) ;
1142     return kFALSE ;
1143   }        
1144         
1145   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1146   if ( !phos )  {
1147     if (fDebug) {
1148       Warning("PostTrackSegmentMaker", "//%s/Reconstructioner/PHOS not found!", fTasksFolder) ; 
1149       Info("PostTrackSegmentMaker", "Adding //%s/Reconstructioner/PHOS", fTasksFolder) ;
1150     }
1151     phos = new TTask("PHOS", "") ; 
1152     tasks->Add(phos) ; 
1153   } 
1154
1155   AliPHOSTrackSegmentMaker * phosts = 
1156     dynamic_cast<AliPHOSTrackSegmentMaker*>(phos->GetListOfTasks()->FindObject(tsmaker->GetName())) ; 
1157   if (phosts) { 
1158     phosts->Delete() ;
1159     phos->GetListOfTasks()->Remove(phosts) ;
1160   }
1161   phos->Add(tsmaker) ;      
1162   return kTRUE; 
1163   
1164
1165 //____________________________________________________________________________ 
1166 const Bool_t AliPHOSGetter::PostTrackSegmentMaker(const char * name) const 
1167 { //------------Track Segment Maker ------------------------------
1168   
1169   // the hierarchy is //Folders/Tasks/Reconstructioner/PHOS/sdigitsname
1170   
1171   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1172   
1173   if ( !tasks ) {
1174     Error("PostTrackSegmentMaker", "Task //%s/Reconstructioner not found !", fTasksFolder->GetName() ) ;
1175     return kFALSE ;
1176   }        
1177   
1178   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1179   if ( !phos )  {
1180     if (fDebug) {
1181       Warning("PostTrackSegmentMaker", "//%s/Reconstructioner/PHOS not found!", fTasksFolder->GetName() ) ; 
1182       Info("PostTrackSegmentMaker", "Adding //%s/Reconstructioner/PHOS", fTasksFolder->GetName()) ;
1183     }
1184     phos = new TTask("PHOS", "") ; 
1185     tasks->Add(phos) ; 
1186   } 
1187
1188   TList * l = phos->GetListOfTasks() ;   
1189   TIter it(l) ;
1190   TString tsn(name);
1191   tsn+=":tsm" ; 
1192   TTask * task ;
1193   while((task = static_cast<TTask *>(it.Next()) )){
1194     TString taskname(task->GetName()) ;
1195     if(taskname.BeginsWith(tsn))
1196       return kTRUE ;
1197   }
1198   
1199   AliPHOSTrackSegmentMakerv1 * phosts = new AliPHOSTrackSegmentMakerv1() ;
1200   tsn+="-v1" ;
1201   phosts->SetName(tsn) ;
1202   phosts->SetTitle(fHeaderFile) ;
1203   phos->Add(phosts) ;      
1204   return kTRUE; 
1205   
1206
1207
1208 //____________________________________________________________________________ 
1209 TObject** AliPHOSGetter::TSMakerRef(const char * name) const 
1210 { //------------Track Segment Maker ------------------------------
1211   
1212   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1213
1214   if ( !tasks ) {
1215     Fatal("TSMakerRef", "Task //%s/Reconstructioner not found !", fTasksFolder->GetName() ) ;
1216   }        
1217         
1218   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1219   if ( !phos )  {
1220     Fatal("TSMakerRef", "//%s/Reconstructioner/PHOS not found !", fTasksFolder->GetName() ) ; 
1221   }   
1222
1223   TList * l = phos->GetListOfTasks() ; 
1224   TIter it(l) ;
1225   TTask * task ;
1226   TTask * tsm = 0 ;
1227   TString tsmname(name) ;
1228   tsmname+=":tsm" ;
1229   while((task = static_cast<TTask *>(it.Next()) )){
1230     TString taskname(task->GetName()) ;
1231     if(taskname.BeginsWith(tsmname)){
1232       tsm = task ;
1233       break ;
1234     }
1235   }
1236   
1237   if(!tsm) {
1238    Fatal("TSMakerRef", "Task //%s/Reconstructioner/PHOS/TrackSegmentMarker/%s not found !", fTasksFolder->GetName(),  name) ;
1239   }
1240  
1241   return l->GetObjectRef(tsm) ;
1242
1243
1244
1245 //____________________________________________________________________________ 
1246 const Bool_t AliPHOSGetter::PostRecParticles(const char * name) const 
1247 {  // -------------------- RecParticles ------------------------
1248   
1249   // the hierarchy is //Folders/Run/Event/RecData/PHOS/RecParticles/name
1250
1251   TFolder * phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS")) ; 
1252   
1253   if ( !phosFolder ) {
1254     if (fDebug) {
1255       Warning("PostRecParticles", "-> Folder //%s/PHOS/ not found!", fRecoFolder->GetName()) ;
1256       Info("PostRecParticles", "-> Adding Folder //%s/PHOS/", fRecoFolder->GetName()) ;
1257     }
1258     phosFolder = fRecoFolder->AddFolder("PHOS", "Reconstructed data from PHOS") ;  
1259   }    
1260
1261  TFolder * phosRPaFolder  = dynamic_cast<TFolder*>(phosFolder->FindObject("RecParticles")) ;
1262   if ( !phosRPaFolder ) {
1263     if (fDebug) {
1264       Warning("PostRecParticles", "-> Folder //%s/PHOS/RecParticles/ not found!", fRecoFolder->GetName()) ;
1265       Info("PostRecParticles", "-> Adding Folder //%s/PHOS/RecParticles/", fRecoFolder->GetName()) ;
1266     }
1267     phosRPaFolder = phosFolder->AddFolder("RecParticles", "RecParticles from PHOS") ;  
1268   } 
1269
1270   TObject * rps = phosRPaFolder->FindObject( name )  ;
1271   if ( !rps ) {
1272     TClonesArray * rp = new TClonesArray("AliPHOSRecParticle",100) ;
1273     rp->SetName(name) ;    
1274     phosRPaFolder->Add(rp) ;  
1275   }
1276   return kTRUE; 
1277
1278
1279 //____________________________________________________________________________ 
1280 TObject** AliPHOSGetter::RecParticlesRef(const char * name) const 
1281 { // ---------------RecParticles -----------------------------------
1282   
1283   // the hierarchy is //Folders/Run/Event/RecData/PHOS/TrackSegments/name
1284
1285  if ( !fRecoFolder ) {
1286     Fatal("RecParticlesRef", "Folder//%s not found !", fRecoFolder->GetName() ) ; 
1287   }    
1288
1289   TFolder * phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/RecParticles")) ; 
1290   if ( !phosFolder ) {
1291     Fatal("RecParticlesRef", "Folder //%s/PHOS/RecParticles/ not found !", fRecoFolder->GetName() ) ;
1292   }    
1293
1294   TObject * tss =  phosFolder->FindObject(name  ) ;
1295   if (!tss) {
1296     Fatal("RecParticlesRef", "object %s not found !", name) ; 
1297   }
1298   return phosFolder->GetListOfFolders()->GetObjectRef(tss) ;
1299 }
1300 //____________________________________________________________________________ 
1301 const UShort_t AliPHOSGetter::EventPattern(void){
1302   if(fBTE)
1303     return fBTE->GetPattern() ;
1304   else
1305     return 0 ;
1306 }
1307 //____________________________________________________________________________ 
1308 const Float_t AliPHOSGetter::BeamEnergy(void){
1309   if(fBTE)
1310     return fBTE->GetBeamEnergy() ;
1311   else
1312     return 0 ;
1313 }
1314 //____________________________________________________________________________ 
1315 const Bool_t AliPHOSGetter::PostPID(AliPHOSPID * pid) const 
1316 {      // ------------AliPHOS PID -----------------------------
1317
1318   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1319
1320   if ( !tasks ) {
1321     Error("PostPID", "Task //%s/Reconstructioner not found !", fTasksFolder) ;
1322     return kFALSE ;
1323   }        
1324   
1325   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1326   if ( !phos )  {
1327     if (fDebug) {
1328       Warning("PostPID", "//%s/Reconstructioner/PHOS not found!", fTasksFolder) ; 
1329       Info("PostPID", "Adding //%s/Reconstructioner/PHOS", fTasksFolder) ;
1330     }
1331     phos = new TTask("PHOS", "") ; 
1332     tasks->Add(phos) ; 
1333   } 
1334
1335   AliPHOSPID * phospid = dynamic_cast<AliPHOSPID*>(phos->GetListOfTasks()->FindObject(pid->GetName())) ; 
1336   if (phospid) { 
1337     if (fDebug)
1338       Info("PostPID", "-> Task %s qlready exists", pid->GetName()) ; 
1339     phos->GetListOfTasks()->Remove(phospid) ;
1340   }
1341   
1342   phos->Add(pid) ;      
1343   return kTRUE; 
1344
1345
1346 //____________________________________________________________________________ 
1347 const Bool_t AliPHOSGetter::PostPID(const char * name) const 
1348 {     
1349   // the hierarchy is //Folders/Tasks/Reconstructioner/PHOS/sdigitsname
1350   
1351   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1352
1353   if ( !tasks ) {
1354     Error("PostPID", "Task //%s/Reconstructioner not found !", fTasksFolder->GetName() ) ;
1355     return kFALSE ;
1356   }        
1357   
1358   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1359   if ( !phos )  {
1360     if (fDebug) {
1361       Warning("PostPID", "//%s/Reconstructioner/PHOS not found!", fTasksFolder->GetName()) ; 
1362       Info("PostPID", "Adding //%s/Reconstructioner/PHOS", fTasksFolder->GetName()) ;
1363     }
1364     phos = new TTask("PHOS", "") ; 
1365     tasks->Add(phos) ; 
1366   } 
1367
1368   TList * l = phos->GetListOfTasks() ;   
1369   TIter it(l) ;
1370   TString pidname(name) ;
1371   pidname+=":pid" ;
1372   TTask * task ;
1373   while((task = static_cast<TTask *>(it.Next()) )){
1374     TString taskname(task->GetName()) ;
1375     if(taskname.BeginsWith(pidname))
1376       return kTRUE ;
1377   }
1378  
1379   AliPHOSPIDv1 * phospid = new AliPHOSPIDv1() ;
1380   pidname+="-v1" ;
1381   phospid->SetName(pidname) ; 
1382   phospid->SetTitle(fHeaderFile) ;
1383   phos->Add(phospid) ;      
1384   
1385   return kTRUE; 
1386
1387
1388 //____________________________________________________________________________ 
1389 TObject** AliPHOSGetter::PIDRef(const char * name) const 
1390 { //------------PID ------------------------------
1391
1392   TTask * tasks  = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ; 
1393
1394   if ( !tasks ) {
1395     Fatal("PIDRef", "Task //%s/Reconstructioner not found !", fTasksFolder->GetName() ) ;
1396   }        
1397         
1398   TTask * phos = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
1399   if ( !phos )  {
1400     Fatal("PIDRef", "//%s/Reconstructioner/PHOS not found !", fTasksFolder->GetName() ) ; 
1401   }   
1402   
1403   TList * l = phos->GetListOfTasks() ; 
1404   TIter it(l) ;
1405   TTask * task ;
1406   TTask * pid = 0 ;
1407   TString pidname(name) ;
1408   pidname+=":pid" ;
1409   while((task = static_cast<TTask *>(it.Next()) )){
1410     TString taskname(task->GetName()) ;
1411     if(taskname.BeginsWith(pidname)){
1412       pid = task ;
1413       break ;
1414     }
1415   }
1416   
1417   if(!pid) {
1418     Fatal("PIDRef", "Task //%s/Reconstructioner/PHOS/PID/%s not found !", fTasksFolder->GetName(), name) ;
1419   }
1420   
1421     return l->GetObjectRef(pid) ;
1422
1423
1424 //____________________________________________________________________________ 
1425 const Bool_t AliPHOSGetter::PostQA(void) const 
1426 { // ------------------ QA ---------------------------------
1427
1428   // the hierarchy is //Folders/Run/Conditions/QA/PHOS/alarmsName
1429
1430   TFolder * phosFolder = dynamic_cast<TFolder*>(fQAFolder->FindObject("PHOS")) ; 
1431   if ( !phosFolder ) {
1432     if (fDebug) {
1433       Warning("PostQA", "-> Folder //%s/PHOS/ not found!", fQAFolder) ;
1434       Info("PostQA", "-> Adding Folder //%s/PHOS", fQAFolder) ;
1435     }
1436     phosFolder = fQAFolder->AddFolder("PHOS", "QA from PHOS") ; 
1437   }      
1438
1439   return kTRUE;
1440 }
1441
1442 //____________________________________________________________________________ 
1443 TObject** AliPHOSGetter::AlarmsRef(void) const 
1444 {  //------- Alarms ----------------------
1445
1446   
1447   // the hierarchy is //Folders/Run/Conditions/QA/PHOS
1448   if ( !fQAFolder ) {
1449     Fatal("AlarmsRef", "Folder //%s not found !", fQAFolder) ;
1450   }    
1451  
1452   TFolder * phosFolder = dynamic_cast<TFolder *>(fQAFolder->FindObject("PHOS")) ;
1453   if ( !phosFolder ) {
1454     Fatal("AlarmsRef", "Folder //%s/PHOS/ not found !", fQAFolder) ;
1455   }
1456    
1457   return fQAFolder->GetListOfFolders()->GetObjectRef(phosFolder) ;
1458 }
1459
1460
1461 //____________________________________________________________________________ 
1462 TTree * AliPHOSGetter::TreeK(TString filename)  
1463 {
1464
1465   // returns TreeK from file filename
1466   // usefull in case of split file
1467
1468   if ( filename.IsNull() ) 
1469     filename = fHeaderFile ; 
1470
1471   TFile * file = 0 ; 
1472   file = static_cast<TFile*>(gROOT->GetFile(filename.Data() ) ) ;
1473 //   if (file && (filename != fHeaderFile) ) {  // file already open 
1474 //     file->Close() ; 
1475 //     //delete fAlice ; 
1476 //   }
1477   if(!file || !file->IsOpen())    
1478     file = TFile::Open(filename.Data(), "read") ;
1479   if(filename != fHeaderFile ){
1480     fAlice = dynamic_cast<AliRun *>(file->Get("gAlice")) ;
1481   } 
1482   TString treeName("TreeK") ; 
1483   treeName += EventNumber()  ; 
1484   TTree * tree = dynamic_cast<TTree *>(file->Get(treeName.Data())) ;
1485   if (!tree && fDebug)  
1486     Warning("TreeK", "-> %s not found in %s", treeName.Data(), filename.Data()) ; 
1487   
1488   return tree ;                       
1489 }
1490
1491 //____________________________________________________________________________ 
1492 TTree * AliPHOSGetter::TreeH(TString filename)  
1493 {
1494
1495   // returns TreeH from file filename
1496   // usefull in case of split file
1497
1498   if ( filename.IsNull() ) 
1499     filename = fHeaderFile ; 
1500
1501   TFile * file = 0 ; 
1502   file = static_cast<TFile*>(gROOT->GetFile(filename.Data() ) ) ;
1503   if (!file) { // file not open yet
1504     file = TFile::Open(filename.Data(), "read") ; 
1505   }
1506   TString treeName("TreeH") ; 
1507   treeName += EventNumber()  ; 
1508   TTree * tree = static_cast<TTree *>(file->Get(treeName.Data())) ;
1509   if (!tree && fDebug)  
1510     Warning("TreeH", "-> %s not found in %s", treeName.Data(), filename.Data()) ; 
1511   
1512   return tree ;                       
1513 }
1514
1515 //____________________________________________________________________________ 
1516 TTree * AliPHOSGetter::TreeS(TString filename)  
1517 {
1518
1519   // returns TreeS from file filename
1520   // usefull in case of split file
1521
1522   if ( filename.IsNull() ) 
1523     filename = fHeaderFile ; 
1524
1525   TFile * file = 0 ; 
1526   file = static_cast<TFile*>(gROOT->GetFile(filename.Data() ) ) ;
1527   if (!file) { // file not open yet
1528     file = TFile::Open(filename.Data(), "read") ; 
1529   }
1530   TString treeName("TreeS") ; 
1531   treeName += EventNumber()  ; 
1532   TTree * tree = static_cast<TTree *>(file->Get(treeName.Data())) ;
1533   if (!tree && fDebug)  
1534     Warning("TreeS", "-> %s not found in %s", treeName.Data(), filename.Data() ); 
1535   
1536   return tree ;                       
1537 }
1538
1539 //____________________________________________________________________________ 
1540 TTree * AliPHOSGetter::TreeD(TString filename)  
1541 {
1542
1543   // returns TreeD from file filename
1544   // usefull in case of split file
1545
1546   if ( filename.IsNull() ) 
1547     filename = fHeaderFile ; 
1548
1549   TFile * file = 0 ; 
1550   file = static_cast<TFile*>(gROOT->GetFile(filename.Data() ) ) ;
1551   if (!file) { // file not open yet
1552     file = TFile::Open(filename.Data(), "read") ; 
1553   }
1554   TString treeName("TreeD") ; 
1555   treeName += EventNumber()  ; 
1556   TTree * tree = static_cast<TTree *>(file->Get(treeName.Data())) ;
1557   if (!tree && fDebug)  
1558     Warning("TreeD", "-> %s not found in %s", treeName.Data(), filename.Data()) ; 
1559   
1560   return tree ;                       
1561 }
1562
1563 //____________________________________________________________________________ 
1564 const TParticle * AliPHOSGetter::Primary(Int_t index) const 
1565 {
1566   // Return primary particle numbered by <index>
1567
1568   if(index < 0) 
1569     return 0 ;
1570   TParticle *  p = 0 ;
1571   if (fAlice) 
1572     p = fAlice->Particle(index) ; 
1573   else 
1574     p = gAlice->Particle(index) ; 
1575   
1576   return p ; 
1577     
1578 }
1579
1580 //____________________________________________________________________________ 
1581 const TParticle * AliPHOSGetter::Secondary(TParticle* p, Int_t index) const
1582 {
1583   // Return first (index=1) or second (index=2) secondary particle of primary particle p 
1584
1585   if(index <= 0) 
1586     return 0 ;
1587   if(index > 2)
1588     return 0 ;
1589
1590   if(p) {
1591   Int_t daughterIndex = p->GetDaughter(index-1) ; 
1592   return  gAlice->Particle(daughterIndex) ; 
1593   }
1594   else
1595     return 0 ;
1596 }
1597
1598 //____________________________________________________________________________ 
1599 Int_t AliPHOSGetter::ReadTreeD(const Int_t event)
1600 {
1601   // Read the digit tree gAlice->TreeD()  
1602   
1603   TTree * treeD ;
1604   if(fToSplit){
1605     TFile * file = static_cast<TFile*>(gROOT->GetFile(fDigitsFileName)); 
1606     if(!file) 
1607       file = TFile::Open(fDigitsFileName) ;      
1608     // Get Digits Tree header from file
1609     TString treeName("TreeD") ;
1610     treeName += event ; 
1611     treeD = dynamic_cast<TTree*>(file->Get(treeName.Data()));
1612     if(!treeD){ // TreeD not found in header file
1613       if (fDebug)
1614         Warning("ReadTreeD", "-> Cannot find TreeD in %s", fDigitsFileName.Data()) ;
1615       return 1;
1616     }
1617   }
1618   else
1619     treeD = gAlice->TreeD() ;
1620   
1621   TObjArray * lob = static_cast<TObjArray*>(treeD->GetListOfBranches()) ;
1622   TIter next(lob) ; 
1623   TBranch * branch = 0 ; 
1624   TBranch * digitsbranch = 0 ; 
1625   TBranch * digitizerbranch = 0 ; 
1626   Bool_t phosfound = kFALSE, digitizerfound = kFALSE ; 
1627   
1628   while ( (branch = static_cast<TBranch*>(next())) && (!phosfound || !digitizerfound) ) {
1629     if ( (strcmp(branch->GetName(), "PHOS")==0) && (strcmp(branch->GetTitle(), fDigitsTitle)==0) ) {
1630       digitsbranch = branch ; 
1631       phosfound = kTRUE ;
1632     }
1633     else if ( ((strcmp(branch->GetName(), "AliPHOSDigitizer")==0)||
1634                (strcmp(branch->GetName(), "AliPHOSRaw2Digits")==0)) &&
1635               (strcmp(branch->GetTitle(), fDigitsTitle)==0) ) {
1636       digitizerbranch = branch ; 
1637       digitizerfound = kTRUE ; 
1638     }
1639   }
1640   
1641   if ( !phosfound || !digitizerfound ) {
1642     if (fDebug)
1643       Warning("ReadTreeD", "-> Cannot find Digits and/or Digitizer with name %s", fDigitsTitle.Data()) ;
1644     return 2; 
1645   }   
1646   
1647   //read digits
1648   if(!Digits(fDigitsTitle) ) 
1649     PostDigits(fDigitsTitle);
1650   else
1651     Digits(fDigitsTitle)->Clear() ;
1652   digitsbranch->SetAddress(DigitsRef(fDigitsTitle)) ;
1653   digitsbranch->GetEntry(0) ;
1654   
1655   // read  the Digitizer
1656   if(Digitizer()){
1657     if(strcmp(Digitizer()->IsA()->GetName(),digitizerbranch->GetName())!=0){
1658       RemoveTask("D", fDigitsTitle) ;
1659       if(strcmp(digitizerbranch->GetName(), "AliPHOSDigitizer")==0)
1660         PostDigitizer("Digitizer") ;
1661       else
1662         PostDigitizer("Raw2Digits") ;
1663     }
1664   }
1665   else{
1666     if(strcmp(digitizerbranch->GetName(), "AliPHOSDigitizer")==0)
1667       PostDigitizer("Digitizer") ;
1668     else
1669       PostDigitizer("Raw2Digits") ;
1670   }
1671     
1672
1673   digitizerbranch->SetAddress(DigitizerRef(fDigitsTitle)) ;
1674   digitizerbranch->GetEntry(0) ;
1675   
1676
1677 //   if((!fcdb)&&(strcmp(digitizerbranch->GetName(), "AliPHOSRaw2Digits")==0))
1678 //     ReadCalibrationDB("Primordial","beamtest.root") ;
1679
1680
1681   if(gAlice->TreeD()!=treeD)
1682     treeD->Delete();
1683
1684   return 0 ; 
1685 }
1686 //____________________________________________________________________________ 
1687 //void AliPHOSGetter::ReadCalibrationDB(const char * database,const char * filename){
1688 //
1689 //  if(fcdb && (strcmp(database,fcdb->GetTitle())==0))
1690 //    return ;
1691 //
1692 //  TFile * file = gROOT->GetFile(filename) ;
1693 //  if(!file)
1694 //    file = TFile::Open(filename);
1695 //  if(!file){
1696 //    Error ("ReadCalibrationDB", "Cannot open file %s", filename) ;
1697 //    return ;
1698 //  }
1699 //  if(fcdb)
1700 //    fcdb->Delete() ;
1701 //  fcdb = dynamic_cast<AliPHOSCalibrationDB *>(file->Get("AliPHOSCalibrationDB")) ;
1702 //  if(!fcdb)
1703 //    Error ("ReadCalibrationDB", "No database %s in file %s", database, filename) ;
1704 //}
1705
1706 //____________________________________________________________________________ 
1707 Int_t AliPHOSGetter::ReadTreeH()
1708 {
1709   // Read the first entry of PHOS branch in hit tree gAlice->TreeH()
1710   
1711   TTree * treeH = gAlice->TreeH() ;
1712
1713   if(!treeH) {// TreeH not found in header file
1714  
1715     if (fDebug) 
1716       Warning("ReadTreeH", "-> Cannot find TreeH in %s", fHeaderFile.Data() ) ;
1717     
1718     TString searchFileName("PHOS.Hits") ; 
1719     if((strcmp(fBranchTitle.Data(),"Default")!=0)&&(strcmp(fBranchTitle.Data(),"")!=0)){
1720       searchFileName+="." ;
1721       searchFileName += fBranchTitle ;
1722     }
1723     searchFileName+=".root" ;
1724     
1725     if ( (treeH = TreeH(searchFileName)) ) { //found TreeH in the file which contains the hits
1726       if (fDebug) 
1727         Info("ReadTreeH", "-> TreeH found in %s", searchFileName.Data()) ; 
1728       
1729     } else {
1730       Error("ReadTreeH", "TreeH not found") ; 
1731       return 1;
1732     }  
1733   }
1734   
1735   TBranch * hitsbranch = static_cast<TBranch*>(treeH->GetBranch("PHOS")) ;
1736   if ( !hitsbranch ) {
1737     if (fDebug)
1738       Warning("ReadTreeH", "-> Cannot find branch PHOS") ; 
1739     return 2;
1740   }
1741   if(!Hits())
1742     PostHits() ;
1743
1744   if (hitsbranch->GetEntries() > 1 ) {
1745     (dynamic_cast<TClonesArray*> (*HitsRef()))->Clear() ;
1746     TClonesArray * tempo =  new TClonesArray("AliPHOSHit",1000) ;
1747     TClonesArray * hits = dynamic_cast<TClonesArray*>(*HitsRef()) ; 
1748     hitsbranch->SetAddress(&tempo) ;
1749     Int_t index = 0 ; 
1750     Int_t i = 0 ;
1751     for (i = 0 ; i < hitsbranch->GetEntries() ; i++) {
1752       hitsbranch->GetEntry(i) ;
1753       Int_t j = 0 ; 
1754       for ( j = 0 ; j < tempo->GetEntries() ; j++) { 
1755         const AliPHOSHit * hit = static_cast<const AliPHOSHit *>(tempo->At(j)) ; 
1756         new((*hits)[index]) AliPHOSHit( *hit ) ;
1757         index++ ; 
1758       }
1759     }
1760     delete tempo ; 
1761   }
1762   else {
1763     (dynamic_cast<TClonesArray*> (*HitsRef()))->Clear() ;
1764     hitsbranch->SetAddress(HitsRef()) ;
1765     hitsbranch->GetEntry(0) ;
1766   }
1767   return 0 ; 
1768 }
1769
1770 //____________________________________________________________________________ 
1771 void AliPHOSGetter::Track(const Int_t itrack) 
1772 {
1773   // Read the first entry of PHOS branch in hit tree gAlice->TreeH()
1774
1775   if(gAlice->TreeH()== 0){
1776     Error("Track", "Cannot read TreeH") ;
1777     return ;
1778   }
1779   
1780   TBranch * hitsbranch = dynamic_cast<TBranch*>(gAlice->TreeH()->GetListOfBranches()->FindObject("PHOS")) ;
1781   if ( !hitsbranch ) {
1782     if (fDebug)
1783       Warning("Track", "Cannot find branch PHOS") ; 
1784     return ;
1785   }  
1786   if(!Hits())
1787     PostHits() ;
1788
1789   (dynamic_cast<TClonesArray*> (*HitsRef()))->Clear() ;
1790   hitsbranch->SetAddress(HitsRef()) ;
1791   hitsbranch->GetEntry(itrack) ;
1792
1793 }
1794
1795 //____________________________________________________________________________ 
1796 void AliPHOSGetter::ReadTreeQA()
1797 {
1798   // Read the digit tree gAlice->TreeQA()
1799   // so far only PHOS knows about this Tree  
1800
1801   if(PHOS()->TreeQA()== 0){
1802     Error("ReadTreeQA", "Cannot read TreeQA") ;
1803     return ;
1804   }
1805   
1806   TBranch * qabranch = PHOS()->TreeQA()->GetBranch("PHOS") ; 
1807   if (!qabranch) { 
1808     if (fDebug)
1809       Warning("ReadTreeQA", "Cannot find QA Alarms for PHOS");
1810     return ; 
1811   }   
1812   
1813   if(!Alarms())
1814     PostQA() ; 
1815
1816   qabranch->SetAddress(AlarmsRef()) ;
1817
1818   qabranch->GetEntry(0) ;
1819  
1820 //   PostQA("PHOS") ; 
1821 //   TFolder * alarmsF = Alarms() ; 
1822 //   alarmsF->Clear() ; 
1823 //   qabranch->SetAddress(&alarmsF) ;
1824 //   qabranch->GetEntry(0) ;
1825   
1826 }
1827
1828 //____________________________________________________________________________ 
1829 Int_t AliPHOSGetter::ReadTreeR(const Int_t event)
1830 {
1831   // Read the reconstrunction tree gAlice->TreeR()
1832   // A particularity has been introduced here :
1833   //  if gime->Event(ievent,"R") is called branches with the current title are read, the current title
1834   //   being for example give in AliPHOSPID(fileName, title)
1835   //  if gime(Event(ievent, "RA") is called the title of the branches is not checked anymore, "A" stands for any
1836   // This is a feature needed by PID to be able to reconstruct several times particles (each time a ther title is given)
1837   // from a given set of TrackSegments (with a given name)
1838   // This is why any is NOT used to read the branch of RecParticles
1839   // any migh have become obsolete : to be checked
1840   // See AliPHOSPIDv1    
1841
1842   //first - clean if necessary
1843   if(EmcRecPoints(fRecPointsTitle)){
1844     EmcRecPoints(fRecPointsTitle)->Delete() ;
1845     CpvRecPoints(fRecPointsTitle)->Delete() ;
1846   }
1847   //clear TrackSegments
1848   if(TrackSegments(fTrackSegmentsTitle))
1849     TrackSegments(fTrackSegmentsTitle)->Clear() ;
1850   if(RecParticles(fRecParticlesTitle))
1851     RecParticles(fRecParticlesTitle)->Clear() ;
1852         
1853   TTree * treeR ;
1854   if(fToSplit){
1855     TFile * file = static_cast<TFile*>(gROOT->GetFile(fRecPointsFileName)); 
1856     if(!file) 
1857       file = TFile::Open(fRecPointsFileName) ;      
1858     // Get Digits Tree header from file
1859     TString treeName("TreeR") ;
1860     treeName += event ; 
1861     treeR = dynamic_cast<TTree*>(file->Get(treeName.Data()));
1862     if(!treeR){ // TreeR not found in header file
1863       if (fDebug)
1864         Warning("ReadTreeD", "-> Cannot find TreeR in %s", fRecPointsFileName.Data()) ;
1865       return 1;
1866     }
1867   }
1868   else
1869     treeR = gAlice->TreeR() ;
1870   
1871   // RecPoints 
1872   TObjArray * lob = static_cast<TObjArray*>(treeR->GetListOfBranches()) ;
1873   TIter next(lob) ; 
1874   TBranch * branch = 0 ; 
1875   TBranch * emcbranch = 0 ; 
1876   TBranch * cpvbranch = 0 ; 
1877   TBranch * clusterizerbranch = 0 ; 
1878   Bool_t phosemcrpfound = kFALSE, phoscpvrpfound = kFALSE, clusterizerfound = kFALSE ; 
1879
1880   
1881   while ( (branch = static_cast<TBranch*>(next())) && (!phosemcrpfound || !phoscpvrpfound || !clusterizerfound) ) {
1882     if(strcmp(branch->GetTitle(), fRecPointsTitle)==0 ) {
1883       if ( strcmp(branch->GetName(), "PHOSEmcRP")==0) {
1884         emcbranch = branch ; 
1885         phosemcrpfound = kTRUE ;
1886       }
1887       else if ( strcmp(branch->GetName(), "PHOSCpvRP")==0) {
1888         cpvbranch = branch ; 
1889         phoscpvrpfound = kTRUE ;
1890       }
1891       else if(strcmp(branch->GetName(), "AliPHOSClusterizer")==0){
1892         clusterizerbranch = branch ; 
1893         clusterizerfound = kTRUE ; 
1894       }
1895     }
1896   }
1897
1898   if ( !phosemcrpfound || !phoscpvrpfound || !clusterizerfound) {
1899     if (fDebug)
1900       Warning("ReadTreeR", "-> Cannot find RecPoints and/or Clusterizer with name %s", fRecPointsTitle.Data() ) ;
1901  
1902   } else { 
1903     if(!EmcRecPoints(fRecPointsTitle) ) 
1904       PostRecPoints(fRecPointsTitle) ;
1905     emcbranch->SetAddress(EmcRecPointsRef(fRecPointsTitle)) ;
1906     emcbranch->GetEntry(0) ;
1907
1908     cpvbranch->SetAddress(CpvRecPointsRef(fRecPointsTitle)) ; 
1909     cpvbranch->GetEntry(0) ;  
1910     
1911     if(!Clusterizer(fRecPointsTitle) )
1912       PostClusterizer(fRecPointsTitle) ;
1913     
1914     clusterizerbranch->SetAddress(ClusterizerRef(fRecPointsTitle)) ;
1915     clusterizerbranch->GetEntry(0) ;
1916   }
1917   
1918   //------------------- TrackSegments ---------------------
1919   next.Reset() ; 
1920   TBranch * tsbranch = 0 ; 
1921   TBranch * tsmakerbranch = 0 ; 
1922   Bool_t phostsfound = kFALSE, tsmakerfound = kFALSE ; 
1923   while ( (branch = static_cast<TBranch*>(next())) && (!phostsfound || !tsmakerfound) ) {
1924     if(strcmp(branch->GetTitle(), fTrackSegmentsTitle)==0 )  {
1925       if ( strcmp(branch->GetName(), "PHOSTS")==0){
1926         tsbranch = branch ; 
1927         phostsfound = kTRUE ;
1928       }
1929       else if(strcmp(branch->GetName(), "AliPHOSTrackSegmentMaker")==0) {
1930         tsmakerbranch = branch ; 
1931         tsmakerfound  = kTRUE ; 
1932       }
1933     }
1934   }
1935
1936   if ( !phostsfound || !tsmakerfound ) {
1937     if (fDebug)
1938       Warning("ReadTreeR", "-> Cannot find TrackSegments and/or TrackSegmentMaker with name %s", fTrackSegmentsTitle.Data() ) ;
1939   } else { 
1940     // Read and Post the TrackSegments
1941     if(!TrackSegments(fTrackSegmentsTitle))
1942       PostTrackSegments(fTrackSegmentsTitle) ;
1943     tsbranch->SetAddress(TrackSegmentsRef(fTrackSegmentsTitle)) ;
1944     tsbranch->GetEntry(0) ;
1945
1946     // Read and Post the TrackSegment Maker
1947     if(!TrackSegmentMaker(fTrackSegmentsTitle))
1948       PostTrackSegmentMaker(fTrackSegmentsTitle) ;
1949     tsmakerbranch->SetAddress(TSMakerRef(fTrackSegmentsTitle)) ;
1950     tsmakerbranch->GetEntry(0) ;
1951  }
1952   
1953   
1954   //------------ RecParticles ----------------------------
1955   next.Reset() ; 
1956   TBranch * rpabranch = 0 ; 
1957   TBranch * pidbranch = 0 ; 
1958   Bool_t phosrpafound = kFALSE, pidfound = kFALSE ; 
1959   
1960   while ( (branch = static_cast<TBranch*>(next())) && (!phosrpafound || !pidfound) ) 
1961     if(strcmp(branch->GetTitle(), fRecParticlesTitle)==0) {   
1962       if ( strcmp(branch->GetName(), "PHOSRP")==0) {   
1963         rpabranch = branch ; 
1964         phosrpafound = kTRUE ;
1965       }
1966       else if (strcmp(branch->GetName(), "AliPHOSPID")==0) {
1967         pidbranch = branch ; 
1968         pidfound  = kTRUE ; 
1969       }
1970     }
1971   
1972   if ( !phosrpafound || !pidfound ) {
1973     if (fDebug)
1974       Warning("ReadTreeR", "-> Cannot find RecParticles and/or PID with name %s", fRecParticlesTitle.Data() ) ; 
1975   } else { 
1976     // Read and Post the RecParticles
1977     if(!RecParticles(fRecParticlesTitle)) 
1978       PostRecParticles(fRecParticlesTitle) ;
1979     rpabranch->SetAddress(RecParticlesRef(fRecParticlesTitle)) ;
1980     rpabranch->GetEntry(0) ;
1981     // Read and Post the PID
1982     if(!PID(fRecParticlesTitle))
1983       PostPID(fRecParticlesTitle) ;
1984     pidbranch->SetAddress(PIDRef(fRecParticlesTitle)) ;
1985     pidbranch->GetEntry(0) ;
1986   }
1987
1988   if(gAlice->TreeR()!=treeR)
1989     treeR->Delete();
1990   return 0 ; 
1991 }
1992
1993 //____________________________________________________________________________ 
1994 Int_t AliPHOSGetter::ReadTreeS(const Int_t event)
1995 {
1996   // Reads the SDigits treeS from all files  
1997   // Files, which should be opened are listed in phosF
1998   // So, first get list of files
1999   TFolder * phosF = dynamic_cast<TFolder *>(fSDigitsFolder->FindObject("PHOS")) ;
2000   if (!phosF) 
2001     phosF = fSDigitsFolder->AddFolder("PHOS", "SDigits from PHOS") ; 
2002   TCollection * folderslist = phosF->GetListOfFolders() ; 
2003   
2004   // Now iterate over the list of files and read TreeS into Whiteboard
2005   TIter next(folderslist) ; 
2006   TFolder * folder = 0 ; 
2007   TFile * file; 
2008   TTree * treeS = 0;
2009   while ( (folder = static_cast<TFolder*>(next())) ) {
2010     TString fileName("") ;
2011     fileName = folder->GetName() ; 
2012     fileName.ReplaceAll("_","/") ; 
2013     file = static_cast<TFile*>(gROOT->GetFile(fileName)); 
2014     if(!file) 
2015       file = TFile::Open(fileName) ;      
2016     // Get SDigits Tree header from file
2017     TString treeName("TreeS") ;
2018     treeName += event ; 
2019     treeS = dynamic_cast<TTree*>(file->Get(treeName.Data()));
2020
2021     if(!treeS){ // TreeS not found in header file
2022       if (fDebug)
2023         Warning("ReadTreeS", "-> Cannot find TreeS in %s", fileName.Data()) ;
2024       return 1;
2025     }
2026     
2027     //set address of the SDigits and SDigitizer
2028     TBranch   * sdigitsBranch    = 0;
2029     TBranch   * sdigitizerBranch = 0;
2030     TBranch   * branch           = 0 ;  
2031     TObjArray * lob = static_cast<TObjArray*>(treeS->GetListOfBranches()) ;
2032     TIter next(lob) ; 
2033     Bool_t phosfound = kFALSE, sdigitizerfound = kFALSE ; 
2034
2035     while ( (branch = static_cast<TBranch*>(next())) && (!phosfound || !sdigitizerfound) ) {
2036       if ( (strcmp(branch->GetName(), "PHOS")==0) && (strcmp(branch->GetTitle(), fSDigitsTitle)==0) ) {
2037         phosfound = kTRUE ;
2038         sdigitsBranch = branch ; 
2039       }
2040       
2041       else if ( (strcmp(branch->GetName(), "AliPHOSSDigitizer")==0) && 
2042                 (strcmp(branch->GetTitle(), fSDigitsTitle)==0) ) {
2043         sdigitizerfound = kTRUE ; 
2044         sdigitizerBranch = branch ;
2045       }
2046     }
2047     if ( !phosfound || !sdigitizerfound ) {
2048       if (fDebug)
2049         Warning("ReadSDigits", "-> Digits and/or Digitizer branch with name %s not found", GetName()) ;
2050       return 2; 
2051     }   
2052     
2053     if ( !folder->FindObject(fSDigitsTitle) ){  
2054       TClonesArray * sdigits = new TClonesArray("AliPHOSDigit",1) ;
2055       sdigits->SetName(fSDigitsTitle) ;
2056       folder->Add(sdigits) ;
2057     }
2058
2059     ((TClonesArray*) (*SDigitsRef(fSDigitsTitle,folder->GetName())))->Clear() ;
2060     sdigitsBranch->SetAddress(SDigitsRef(fSDigitsTitle,folder->GetName())) ;
2061     sdigitsBranch->GetEntry(0) ;
2062     
2063     TString sdname(fSDigitsTitle) ;
2064     sdname+=":" ;
2065     sdname+=folder->GetName() ;
2066     if(!SDigitizer(sdname) ) 
2067       PostSDigitizer(fSDigitsTitle,folder->GetName()) ;
2068     sdigitizerBranch->SetAddress(SDigitizerRef(sdname)) ;
2069     sdigitizerBranch->GetEntry(0) ; 
2070     if(gAlice->TreeS()!=treeS)
2071       treeS->Delete();
2072   }    
2073   return 0 ; 
2074 }
2075
2076 //____________________________________________________________________________ 
2077 void AliPHOSGetter::ReadTreeS(TTree * treeS, Int_t input)
2078
2079 {  // Read the summable digits fron treeS()  
2080
2081
2082   TString filename("mergefile") ;
2083   filename+= input ;
2084
2085   TFolder * phosFolder = dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("PHOS")) ; 
2086   if ( !phosFolder ) { 
2087    phosFolder = fSDigitsFolder->AddFolder("PHOS", "SDigits from PHOS") ; 
2088   } 
2089   TFolder * folder=(TFolder*)phosFolder->FindObject(filename) ;
2090   //set address of the SDigits and SDigitizer
2091   TBranch   * sdigitsBranch    = 0;
2092   TBranch   * sdigitizerBranch = 0;
2093   TBranch   * branch           = 0 ;  
2094   TObjArray * lob = (TObjArray*)treeS->GetListOfBranches() ;
2095   TIter next(lob) ; 
2096   Bool_t phosfound = kFALSE, sdigitizerfound = kFALSE ; 
2097   
2098   while ( (branch = (TBranch*)next()) && (!phosfound || !sdigitizerfound) ) {
2099     if ( strcmp(branch->GetName(), "PHOS")==0) {
2100       phosfound = kTRUE ;
2101       sdigitsBranch = branch ; 
2102     }
2103     
2104     else if ( strcmp(branch->GetName(), "AliPHOSSDigitizer")==0) {
2105       sdigitizerfound = kTRUE ; 
2106       sdigitizerBranch = branch ;
2107     }
2108   }
2109   if ( !phosfound || !sdigitizerfound ) {
2110     if (fDebug)
2111       Warning("ReadTreeS", "-> Digits and/or Digitizer branch not found") ;
2112     return ; 
2113   }   
2114   
2115   if (!folder || !(folder->FindObject(sdigitsBranch->GetTitle()) ) )
2116     PostSDigits(sdigitsBranch->GetTitle(),filename) ;
2117
2118   sdigitsBranch->SetAddress(SDigitsRef(sdigitsBranch->GetTitle(),folder->GetName())) ;
2119   sdigitsBranch->GetEntry(0) ;
2120   
2121   TString sdname(sdigitsBranch->GetTitle()) ;
2122   sdname+=":" ;
2123   sdname+=filename ;
2124   
2125   if(!SDigitizer(sdigitsBranch->GetTitle()) )
2126     PostSDigitizer(sdigitsBranch->GetTitle(),filename) ;
2127   sdigitizerBranch->SetAddress(SDigitizerRef(sdname)) ;
2128   sdigitizerBranch->GetEntry(0) ;
2129   if(gAlice->TreeS()!=treeS)
2130     treeS->Delete();
2131 }    
2132
2133
2134 //____________________________________________________________________________ 
2135 void AliPHOSGetter::ReadPrimaries()
2136 {
2137   // a lot simplified.... if 2 files are opened then we have a problem
2138
2139   TClonesArray * ar = 0  ; 
2140   if(! (ar = Primaries()) ) { 
2141     PostPrimaries() ;
2142     ar = Primaries() ; 
2143   }
2144   ar->Delete() ; 
2145   
2146   if (TreeK(fHeaderFile)) { // treeK found in header file
2147     if (fDebug) 
2148       Info("ReadPrimaries", "-> TreeK found in %s", fHeaderFile.Data() ); 
2149     fNPrimaries = gAlice->GetNtrack() ; 
2150     fAlice = 0 ; 
2151   
2152   } else { // treeK not found in header file
2153     Error("ReadPrimaries", "TreeK not found") ; 
2154     return ;  
2155   }
2156
2157   Int_t index = 0 ; 
2158   for (index = 0 ; index < fNPrimaries; index++) { 
2159     new ((*ar)[index]) TParticle(*(Primary(index)));
2160   }
2161 }
2162
2163 //____________________________________________________________________________ 
2164 void AliPHOSGetter::Event(const Int_t event, const char* opt)  
2165 {
2166   // Reads the content of all Tree's S, D and R
2167
2168   if (event >= gAlice->TreeE()->GetEntries() ) {
2169     Error("Event", "%d not found in TreeE !", event) ; 
2170     return ; 
2171   }
2172
2173   TBranch * btb = gAlice->TreeE()->GetBranch("AliPHOSBeamTestEvent") ;
2174   if(btb){
2175     if(!fBTE)
2176       fBTE = new AliPHOSBeamTestEvent() ;
2177     btb->SetAddress(&fBTE) ;
2178     btb->GetEntry(event) ;
2179   }
2180   else{
2181     if(fBTE){
2182       delete fBTE ;
2183       fBTE = 0 ;
2184     }
2185   }
2186
2187   Bool_t any = kFALSE ; 
2188   if (strstr(opt,"A") ) // do not check the title of the branches
2189     any = kTRUE; 
2190
2191   gAlice->GetEvent(event) ; 
2192
2193   if( strstr(opt,"R") )
2194     ReadTreeR(event) ;
2195
2196   if( strstr(opt,"D") )
2197     ReadTreeD(event) ;
2198
2199   if(strstr(opt,"S") )
2200     ReadTreeS(event) ;
2201
2202   if(strstr(opt,"H") )
2203     ReadTreeH() ;
2204    
2205   if( strstr(opt,"Q") )
2206     ReadTreeQA() ;
2207
2208   if( strstr(opt,"P") || (strcmp(opt,"")==0) )
2209     ReadPrimaries() ;
2210
2211  
2212 }
2213
2214 //____________________________________________________________________________ 
2215 TObject * AliPHOSGetter::ReturnO(TString what, TString name, TString file) const 
2216 {
2217   // get the object named "what" from the folder
2218   // folders are named like //Folders
2219
2220   if ( file.IsNull() ) 
2221     file = fHeaderFile ; 
2222   if( name.IsNull() )
2223     name = fBranchTitle ;
2224
2225   TFolder * folder = 0 ;
2226   TObject * phosO  = 0 ; 
2227
2228   if ( what.CompareTo("Primaries") == 0 ) {
2229     folder = dynamic_cast<TFolder *>(fPrimariesFolder->FindObject("Primaries")) ; 
2230     if (folder) 
2231       phosO  = dynamic_cast<TObject *>(folder->FindObject("Primaries")) ;  
2232     else 
2233       return 0 ; 
2234   }
2235   else if ( what.CompareTo("Hits") == 0 ) {
2236     folder = dynamic_cast<TFolder *>(fHitsFolder->FindObject("PHOS")) ; 
2237     if (folder) 
2238       phosO  = dynamic_cast<TObject *>(folder->FindObject("Hits")) ;  
2239   }
2240   else if ( what.CompareTo("SDigits") == 0 ) {
2241     if(fToSplit){
2242       file.Remove(file.Last('/')+1,file.Length()-file.Last('/')-1) ;
2243       file.ReplaceAll("/","_") ; 
2244       file+="PHOS.SDigits." ;
2245       if(name && (strcmp(name,"Default")!=0)){
2246         file+=name ;
2247         file+="." ;
2248       }
2249       file+="root" ;
2250     }
2251     TString path = "PHOS/" + file  ;
2252     folder = dynamic_cast<TFolder *>(fSDigitsFolder->FindObject(path.Data())) ; 
2253     if (folder) { 
2254       if (name.IsNull())
2255         name = fSDigitsTitle ; 
2256       phosO  = dynamic_cast<TObject *>(folder->FindObject(name)) ; 
2257     }
2258   }
2259   else if ( what.CompareTo("Digits") == 0 ){
2260     folder = dynamic_cast<TFolder *>(fDigitsFolder->FindObject("PHOS")) ; 
2261     if (folder) { 
2262       if (name.IsNull())
2263         name = fDigitsTitle ; 
2264       phosO  = dynamic_cast<TObject *>(folder->FindObject(name)) ; 
2265     } 
2266   }
2267   else if ( what.CompareTo("EmcRecPoints") == 0 ) {
2268     folder = dynamic_cast<TFolder *>(fRecoFolder->FindObject("PHOS/EMCARecPoints")) ; 
2269     if (folder) { 
2270       if (name.IsNull())
2271         name = fRecPointsTitle ; 
2272       phosO  = dynamic_cast<TObject *>(folder->FindObject(name)) ;
2273     } 
2274   }
2275   else if ( what.CompareTo("CpvRecPoints") == 0 ) {
2276     folder = dynamic_cast<TFolder *>(fRecoFolder->FindObject("PHOS/CPVRecPoints")) ; 
2277     if (folder) { 
2278       if (name.IsNull())
2279         name = fRecPointsTitle ; 
2280       phosO  = dynamic_cast<TObject *>(folder->FindObject(name)) ; 
2281     }   
2282   }
2283   else if ( what.CompareTo("TrackSegments") == 0 ) {
2284     folder = dynamic_cast<TFolder *>(fRecoFolder->FindObject("PHOS/TrackSegments")) ; 
2285     if (folder) { 
2286       if (name.IsNull())
2287         name = fTrackSegmentsTitle ; 
2288       phosO  = dynamic_cast<TObject *>(folder->FindObject(name)) ; 
2289     }   
2290   }
2291   else if ( what.CompareTo("RecParticles") == 0 ) {
2292     folder = dynamic_cast<TFolder *>(fRecoFolder->FindObject("PHOS/RecParticles")) ; 
2293    if (folder) { 
2294       if (name.IsNull())
2295         name = fRecParticlesTitle ; 
2296       phosO  = dynamic_cast<TObject *>(folder->FindObject(name)) ;
2297     }   
2298  }
2299   else if ( what.CompareTo("Alarms") == 0 ){ 
2300       phosO = dynamic_cast<TFolder *>(fQAFolder->FindObject("PHOS")) ;        
2301   }
2302   if (!phosO) {
2303     if(fDebug)
2304       Warning("ReturnO", "Object %s not found in PHOS", what.Data() ) ; 
2305     return 0 ;
2306   }
2307
2308   return phosO ;
2309 }
2310   
2311 //____________________________________________________________________________ 
2312 const TTask * AliPHOSGetter::ReturnT(TString what, TString name) const 
2313 {
2314   // get the TTask named "what" from the folder
2315   // folders are named like //Folders/Tasks/what/PHOS/name
2316
2317   TString search(what) ; 
2318   if ( what.CompareTo("Clusterizer") == 0 ) 
2319     search = "Reconstructioner" ; 
2320   else if ( what.CompareTo("TrackSegmentMaker") == 0 ) 
2321     search = "Reconstructioner" ; 
2322   else if ( what.CompareTo("PID") == 0 ) 
2323     search = "Reconstructioner" ; 
2324   else if ( what.CompareTo("QATasks") == 0 ) 
2325     search = "QA" ; 
2326
2327   TTask * tasks = dynamic_cast<TTask*>(fTasksFolder->FindObject(search)) ; 
2328
2329   if (!tasks) {
2330     Error("ReturnT", "Task %s not found !", what.Data() ) ;  
2331     return 0 ; 
2332   }
2333
2334   TTask * phosT = dynamic_cast<TTask*>(tasks->GetListOfTasks()->FindObject("PHOS")) ; 
2335   if (!phosT) { 
2336      Error("ReturnT", "Task %s/PHOS not found !", what.Data() ) ;  
2337     return 0 ; 
2338   }
2339   
2340   TList * list = phosT->GetListOfTasks() ; 
2341  
2342   if (what.CompareTo("SDigitizer") == 0) {  
2343     if ( name.IsNull() )
2344       name =  fSDigitsTitle ; 
2345   } else  if (what.CompareTo("Digitizer") == 0){ 
2346     if ( name.IsNull() )
2347       name =  fDigitsTitle ;
2348   } else  if (what.CompareTo("Clusterizer") == 0){ 
2349     if ( name.IsNull() )
2350       name =  fRecPointsTitle ;
2351     name.Append(":clu") ;
2352   }
2353   else  if (what.CompareTo("TrackSegmentMaker") == 0){ 
2354     if ( name.IsNull() )
2355       name =  fTrackSegmentsTitle ;
2356     name.Append(":tsm") ;
2357   }
2358   else  if (what.CompareTo("PID") == 0){ 
2359     if ( name.IsNull() )
2360       name =  fRecParticlesTitle ;
2361     name.Append(":pid") ;
2362   }
2363   else  if (what.CompareTo("QATasks") == 0){ 
2364     if ( name.IsNull() )
2365       return phosT ;
2366   }
2367   
2368   TIter it(list) ;
2369   TTask * task = 0 ; 
2370   while((task = static_cast<TTask *>(it.Next()) )){
2371     TString taskname(task->GetName()) ;
2372     if(taskname.BeginsWith(name))
2373       return task ;
2374   }
2375   
2376   if(fDebug)
2377     Warning("ReturnT", "-> Task %s/PHOS/%s not found", search.Data(), name.Data() ) ; 
2378   return 0 ;
2379 }
2380
2381 //____________________________________________________________________________ 
2382 void AliPHOSGetter::RemoveTask(TString opt, TString name) const 
2383 {
2384   // remove a task from the folder
2385   // path is fTasksFolder/SDigitizer/PHOS/name
2386   
2387   TTask * task = 0 ; 
2388   TTask * phos = 0 ; 
2389   TList * lofTasks = 0 ; 
2390
2391   if (opt == "S") { // SDigitizer
2392     task = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ;
2393     if (!task) 
2394       return ; 
2395   }
2396   else if (opt == "D") { // Digitizer
2397     task = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ;
2398     if (!task) 
2399       return ; 
2400   }
2401   else if (opt == "C" || opt == "T" || opt == "P"  ) { // Clusterizer, TrackSegmentMaker, PID
2402     task = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ;
2403     if (!task) 
2404       return ; 
2405   }
2406   else {
2407     Warning("RemoveTask", "Unknown option %s", opt.Data() ); 
2408     return ; 
2409   }
2410   phos =  dynamic_cast<TTask*>(task->GetListOfTasks()->FindObject("PHOS")) ;
2411   if (!phos)
2412     return ; 
2413   lofTasks = phos->GetListOfTasks() ;
2414   if (!lofTasks) 
2415     return ; 
2416   TObject * obj = lofTasks->FindObject(name) ; 
2417   if (obj) 
2418     lofTasks->Remove(obj) ;
2419    
2420 }
2421
2422 //____________________________________________________________________________ 
2423 void AliPHOSGetter::RemoveObjects(TString opt, TString name) const 
2424 {
2425   // remove SDigits from the folder
2426   // path is fSDigitsFolder/fHeaderFileName/name
2427
2428   TFolder * phos     = 0 ; 
2429   TFolder * phosmain = 0 ; 
2430
2431   if (opt == "H") { // Hits
2432     phos = dynamic_cast<TFolder*>(fHitsFolder->FindObject("PHOS")) ;
2433     if (!phos) 
2434       return ;
2435     name = "Hits" ; 
2436   }
2437
2438   else if ( opt == "S") { // SDigits
2439     phosmain = dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("PHOS")) ;
2440     if (!phosmain) 
2441       return ;
2442     phos = dynamic_cast<TFolder*>(phosmain->FindObject(fHeaderFile)) ;
2443     if (!phos) 
2444       return ;
2445   }
2446   
2447   else if (opt == "D") { // Digits
2448     phos = dynamic_cast<TFolder*>(fDigitsFolder->FindObject("PHOS")) ;
2449     if (!phos) 
2450       return ;
2451   }
2452
2453   else if (opt == "RE") { // EMCARecPoints
2454     phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/EMCARecPoints")) ;
2455     if (!phos) 
2456       return ;
2457   }
2458
2459   else if (opt == "RC") { // CPVRecPoints
2460     phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/CPVRecPoints")) ;
2461     if (!phos) 
2462       return ;
2463   }  
2464
2465   else if (opt == "T") { // TrackSegments
2466     phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/TrackSegments")) ;
2467     if (!phos) 
2468       return ;
2469   }
2470
2471   else if (opt == "P") { // RecParticles
2472     phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/RecParticles")) ;
2473     if (!phos) 
2474       return ;
2475   }
2476   
2477   else {
2478     Warning("RemoveObjects", "Unknown option %s", opt.Data() ) ; 
2479     return ; 
2480   }
2481   
2482   TObjArray * ar  = dynamic_cast<TObjArray*>(phos->FindObject(name)) ; 
2483   if (ar) { 
2484     phos->Remove(ar) ;
2485     ar->Delete() ; 
2486     delete ar ; 
2487   }
2488
2489   if (opt == "S") 
2490     phosmain->Remove(phos) ; 
2491   
2492 }
2493
2494 //____________________________________________________________________________ 
2495 void AliPHOSGetter::RemoveSDigits() const 
2496 {
2497   TFolder * phos= dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("PHOS")) ;
2498   if (!phos) 
2499     return ;
2500   
2501   phos->SetOwner() ; 
2502   phos->Clear() ; 
2503 }
2504
2505 //____________________________________________________________________________ 
2506 void AliPHOSGetter::CleanWhiteBoard(void){
2507
2508   TFolder * phosmain = 0 ; 
2509   TFolder * phos ;
2510   TObjArray * ar ;
2511   TList * lofTasks = 0 ; 
2512   TTask * task = 0 ; 
2513   TTask * phost = 0 ; 
2514   
2515   // Hits  
2516   phos = dynamic_cast<TFolder*>(fHitsFolder->FindObject("PHOS")) ;
2517   if (phos){  
2518     TObjArray * ar  = dynamic_cast<TObjArray*>(phos->FindObject("Hits")) ; 
2519     if (ar) { 
2520       phos->Remove(ar) ;
2521       ar->Delete() ; 
2522       delete ar ; 
2523     }
2524   }
2525   
2526   // SDigits
2527   phosmain = dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("PHOS")) ;
2528   if (phosmain){ 
2529     phos = dynamic_cast<TFolder*>(phosmain->FindObject(fHeaderFile)) ;
2530     if (phos) {
2531       ar  = dynamic_cast<TObjArray*>(phos->FindObject(fSDigitsTitle)) ; 
2532       if (ar) { 
2533         phos->Remove(ar) ;
2534         ar->Delete() ; 
2535         delete ar ; 
2536       }
2537     }
2538     phosmain->Remove(phos) ; 
2539   }
2540
2541   
2542   // Digits
2543   phos = dynamic_cast<TFolder*>(fDigitsFolder->FindObject("PHOS")) ;
2544   if (phos){ 
2545     ar  = dynamic_cast<TObjArray*>(phos->FindObject(fDigitsTitle)) ; 
2546     if (ar) { 
2547       phos->Remove(ar) ;
2548       ar->Delete() ; 
2549       delete ar ; 
2550     }
2551   }
2552
2553
2554   // EMCARecPoints
2555   phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/EMCARecPoints")) ;
2556   if (phos){ 
2557     ar  = dynamic_cast<TObjArray*>(phos->FindObject(fRecPointsTitle)) ; 
2558     if (ar) { 
2559       phos->Remove(ar) ;
2560       ar->Delete() ; 
2561       delete ar ; 
2562     }
2563   }
2564
2565   
2566   // CPVRecPoints
2567   phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/CPVRecPoints")) ;
2568   if (phos){ 
2569     ar  = dynamic_cast<TObjArray*>(phos->FindObject(fRecPointsTitle)) ; 
2570     if (ar) { 
2571       phos->Remove(ar) ;
2572       ar->Delete() ; 
2573       delete ar ; 
2574     }
2575   }  
2576
2577   
2578   // TrackSegments
2579   phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/TrackSegments")) ;
2580   if (phos) { 
2581     ar  = dynamic_cast<TObjArray*>(phos->FindObject(fTrackSegmentsTitle)) ; 
2582     if (ar) { 
2583       phos->Remove(ar) ;
2584       ar->Delete() ; 
2585       delete ar ; 
2586     }
2587   }
2588   
2589
2590
2591   // RecParticles
2592   phos = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS/RecParticles")) ;
2593   if (phos){ 
2594     ar  = dynamic_cast<TObjArray*>(phos->FindObject(fRecParticlesTitle)) ; 
2595     if (ar) { 
2596       phos->Remove(ar) ;
2597       ar->Delete() ; 
2598       delete ar ; 
2599     }
2600   }
2601
2602
2603   //---- Now Tasks ----------- 
2604
2605   TObject * obj ;
2606   TString sdname(fSDigitsTitle);
2607   
2608   // Digitizer
2609   task = dynamic_cast<TTask*>(fTasksFolder->FindObject("Digitizer")) ;
2610   if (task){ 
2611     phost =  dynamic_cast<TTask*>(task->GetListOfTasks()->FindObject("PHOS")) ;
2612     if (phost){
2613       lofTasks = phost->GetListOfTasks() ;
2614       if (lofTasks){ 
2615         obj = lofTasks->FindObject(sdname.Data()) ; 
2616         if (obj) 
2617           lofTasks->Remove(obj) ;
2618       }
2619     }      
2620   }
2621   
2622
2623   sdname.Append(":") ;
2624   // Clusterizer, TrackSegmentMaker, PID
2625   task = dynamic_cast<TTask*>(fTasksFolder->FindObject("Reconstructioner")) ;
2626   if (task){ 
2627     phost =  dynamic_cast<TTask*>(task->GetListOfTasks()->FindObject("PHOS")) ;
2628     if (phost){
2629       lofTasks = phost->GetListOfTasks() ;
2630       TIter next(lofTasks);
2631       while((obj=next())){ 
2632         TString oname(obj->GetName()) ;
2633         if (oname.BeginsWith(sdname)){ 
2634           lofTasks->Remove(obj) ;
2635         }
2636       }
2637     }  
2638   }
2639
2640
2641   // SDigitizer
2642   sdname.Append(fHeaderFile) ;
2643   task = dynamic_cast<TTask*>(fTasksFolder->FindObject("SDigitizer")) ;
2644   if (task) {
2645     phost =  dynamic_cast<TTask*>(task->GetListOfTasks()->FindObject("PHOS")) ;
2646     if (phost){
2647       lofTasks = phost->GetListOfTasks() ;
2648       if (lofTasks){ 
2649         obj = lofTasks->FindObject(sdname.Data()) ; 
2650         if (obj) 
2651           lofTasks->Remove(obj) ;
2652       }
2653     }
2654   }  
2655
2656 }
2657 //____________________________________________________________________________ 
2658 void AliPHOSGetter::SetTitle(const char * branchTitle ) 
2659 {
2660   fBranchTitle        = branchTitle ;
2661   fSDigitsTitle       = branchTitle ; 
2662   fDigitsTitle        = branchTitle ; 
2663   fRecPointsTitle     = branchTitle ; 
2664   fRecParticlesTitle  = branchTitle ; 
2665   fTrackSegmentsTitle = branchTitle ; 
2666   if(fToSplit){
2667     //First - extract full path if necessary
2668     TString sFileName(fHeaderFile) ;
2669     Ssiz_t islash = sFileName.Last('/') ;
2670     if(islash<sFileName.Length())
2671       sFileName.Remove(islash+1,sFileName.Length()) ;
2672     else
2673       sFileName="" ;
2674     //Now construct file names
2675     fSDigitsFileName       = sFileName ;
2676     fDigitsFileName        = sFileName ; 
2677     fRecPointsFileName     = sFileName ; 
2678     fRecParticlesFileName  = sFileName ; 
2679     fTrackSegmentsFileName = sFileName ; 
2680     fSDigitsFileName      += "PHOS.SDigits." ;
2681     fDigitsFileName       += "PHOS.Digits." ; 
2682     fRecPointsFileName    += "PHOS.RecData." ; 
2683     fTrackSegmentsFileName+= "PHOS.RecData." ; 
2684     fRecParticlesFileName += "PHOS.RecData." ; 
2685     if((strcmp(fBranchTitle.Data(),"Default")!=0)&&(strcmp(fBranchTitle.Data(),"")!=0)){
2686       fSDigitsFileName      += fBranchTitle ;
2687       fSDigitsFileName      += "." ;
2688       fDigitsFileName       += fBranchTitle ; 
2689       fDigitsFileName       += "." ; 
2690       fRecPointsFileName    += fBranchTitle ; 
2691       fRecPointsFileName    += "." ; 
2692       fRecParticlesFileName += fBranchTitle ; 
2693       fRecParticlesFileName += "." ; 
2694       fTrackSegmentsFileName+= fBranchTitle ; 
2695       fTrackSegmentsFileName+= "." ; 
2696     }
2697     fSDigitsFileName      += "root" ;
2698     fDigitsFileName       += "root" ; 
2699     fRecPointsFileName    += "root" ; 
2700     fRecParticlesFileName += "root" ; 
2701     fTrackSegmentsFileName+= "root" ; 
2702   }else{
2703     fSDigitsFileName       = fHeaderFile ;
2704
2705     fDigitsFileName        = "" ; 
2706     fRecPointsFileName     = "" ; 
2707     fRecParticlesFileName  = "" ; 
2708     fTrackSegmentsFileName = "" ; 
2709   }
2710   TFolder * phosFolder ; 
2711   phosFolder = dynamic_cast<TFolder*>(fHitsFolder->FindObject("PHOS")) ; 
2712   if ( !phosFolder ) 
2713     phosFolder = fHitsFolder->AddFolder("PHOS", "Hits from PHOS") ; 
2714
2715   phosFolder = dynamic_cast<TFolder*>(fSDigitsFolder->FindObject("PHOS")) ;
2716   if ( !phosFolder ) 
2717     phosFolder = fSDigitsFolder->AddFolder("PHOS", "SDigits from PHOS") ; 
2718
2719   //Make folder for SDigits
2720   fSDigitsFileName.ReplaceAll("/","_") ;
2721   phosFolder->AddFolder(fSDigitsFileName.Data(),"");
2722
2723   phosFolder  = dynamic_cast<TFolder*>(fDigitsFolder->FindObject("PHOS")) ;
2724   if ( !phosFolder ) 
2725     phosFolder = fDigitsFolder->AddFolder("PHOS", "Digits from PHOS") ;  
2726
2727   phosFolder  = dynamic_cast<TFolder*>(fRecoFolder->FindObject("PHOS")) ; 
2728   if ( !phosFolder )
2729     phosFolder = fRecoFolder->AddFolder("PHOS", "Reconstructed data from PHOS") ;  
2730   
2731 }
2732 //____________________________________________________________________________ 
2733 void AliPHOSGetter::CloseSplitFiles(void){
2734   TFile * file ;
2735   file = static_cast<TFile*>(gROOT->GetFile(fSDigitsFileName.Data() ) ) ;
2736   if(file)
2737     file->Close() ;
2738   file = static_cast<TFile*>(gROOT->GetFile(fDigitsFileName.Data() ) ) ;
2739   if(file)
2740     file->Close() ;
2741   file = static_cast<TFile*>(gROOT->GetFile(fRecPointsFileName.Data() ) ) ;
2742   if(file)
2743     file->Close() ;
2744   file = static_cast<TFile*>(gROOT->GetFile(fTrackSegmentsFileName.Data() ) ) ;
2745   if(file)
2746     file->Close() ;
2747   file = static_cast<TFile*>(gROOT->GetFile(fRecParticlesFileName.Data() ) ) ;
2748   if(file)
2749     file->Close() ;
2750
2751 }