]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PHOS/AliPHOSLoader.cxx
Effective C++ warnings fixed (Timur Pocheptsov)
[u/mrichter/AliRoot.git] / PHOS / AliPHOSLoader.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 /* History of cvs commits:
19  *
20  * $Log$
21  * Revision 1.17  2006/08/25 16:00:53  kharlov
22  * Compliance with Effective C++AliPHOSHit.cxx
23  *
24  * Revision 1.16  2006/08/01 12:15:04  cvetan
25  * Adding a constructor from TFolder. Needed by AliReconstruction plugin scheme
26  *
27  * Revision 1.15  2005/07/12 20:07:35  hristov
28  * Changes needed to run simulation and reconstrruction in the same AliRoot session
29  *
30  * Revision 1.14  2005/05/28 14:19:04  schutz
31  * Compilation warnings fixed by T.P.
32  *
33  */
34
35 //_________________________________________________________________________
36 //  A singleton. This class should be used in the analysis stage to get 
37 //  reconstructed objects: Digits, RecPoints, TrackSegments and RecParticles,
38 //  instead of directly reading them from galice.root file. This container 
39 //  ensures, that one reads Digits, made of these particular digits, RecPoints, 
40 //  made of these particular RecPoints, TrackSegments and RecParticles. 
41 //  This becomes non trivial if there are several identical branches, produced with
42 //  different set of parameters. 
43 //
44 //  An example of how to use (see also class AliPHOSAnalyser):
45 //  for(Int_t irecp = 0; irecp < gime->NRecParticles() ; irecp++)
46 //     AliPHOSRecParticle * part = gime->RecParticle(1) ;
47 //     ................
48 //  please->GetEvent(event) ;    // reads new event from galice.root
49 //                  
50 //*-- Author: Yves Schutz (SUBATECH) & Dmitri Peressounko (RRC KI & SUBATECH)
51 //*--         Completely redesigned by Dmitri Peressounko March 2001  
52 //
53 //*-- YS June 2001 : renamed the original AliPHOSIndexToObject and make
54 //*--         systematic usage of TFolders without changing the interface        
55 //////////////////////////////////////////////////////////////////////////////
56
57
58 // --- ROOT system ---
59
60 #include "TFile.h"
61 #include "TTree.h"
62 #include "TROOT.h"
63
64 // --- Standard library ---
65
66 // --- AliRoot header files ---
67 #include "AliObjectLoader.h"
68 #include "AliLog.h"
69 #include "AliPHOSLoader.h"
70 #include "AliPHOS.h"
71 #include "AliPHOSHit.h"
72 #include "AliPHOSCalibrationDB.h"
73 #include "AliPHOSGetter.h"
74
75 ClassImp(AliPHOSLoader)
76
77
78 const TString AliPHOSLoader::fgkHitsName("HITS");//Name for TClonesArray with hits from one event
79 const TString AliPHOSLoader::fgkSDigitsName("SDIGITS");//Name for TClonesArray 
80 const TString AliPHOSLoader::fgkDigitsName("DIGITS");//Name for TClonesArray 
81 const TString AliPHOSLoader::fgkEmcRecPointsName("EMCRECPOINTS");//Name for TClonesArray 
82 const TString AliPHOSLoader::fgkCpvRecPointsName("CPVRECPOINTS");//Name for TClonesArray 
83 const TString AliPHOSLoader::fgkTracksName("TRACKS");//Name for TClonesArray 
84 const TString AliPHOSLoader::fgkRecParticlesName("RECPARTICLES");//Name for TClonesArray
85
86 const TString AliPHOSLoader::fgkEmcRecPointsBranchName("PHOSEmcRP");//Name for branch with EMC Reconstructed Points
87 const TString AliPHOSLoader::fgkCpvRecPointsBranchName("PHOSCpvRP");//Name for branch with CPV Reconstructed Points
88 const TString AliPHOSLoader::fgkTrackSegmentsBranchName("PHOSTS");//Name for branch with TrackSegments
89 const TString AliPHOSLoader::fgkRecParticlesBranchName("PHOSRP");//Name for branch with Reconstructed Particles
90 //____________________________________________________________________________ 
91 AliPHOSLoader::AliPHOSLoader() : fBranchTitle(), fcdb(0), fDebug(0)
92 {
93   //def ctor
94 }
95 //____________________________________________________________________________ 
96 AliPHOSLoader::AliPHOSLoader(const Char_t *detname,const Char_t *eventfoldername) :
97       AliLoader(detname, eventfoldername),
98       fBranchTitle(), fcdb(0), fDebug(0)
99 {
100   //ctor
101 }
102 //____________________________________________________________________________ 
103 AliPHOSLoader::AliPHOSLoader(const Char_t *detname,TFolder *topfolder):
104       AliLoader(detname,topfolder),
105       fBranchTitle(), fcdb(0), fDebug(0)
106
107 {
108   //ctor
109 }
110 //____________________________________________________________________________ 
111 AliPHOSLoader::AliPHOSLoader(const AliPHOSLoader & obj):
112   AliLoader(obj),fBranchTitle(obj.GetBranchTitle()),fcdb(obj.CalibrationDB()),
113   fDebug(obj.GetDebug())
114 {
115   // Copy constructor
116 }
117 //____________________________________________________________________________ 
118
119 AliPHOSLoader::~AliPHOSLoader()
120 {
121   //remove and delete arrays
122   Clean(fgkHitsName);
123   Clean(fgkSDigitsName);
124   Clean(fgkDigitsName);
125   Clean(fgkEmcRecPointsName);
126   Clean(fgkCpvRecPointsName);
127   Clean(fgkTracksName);
128   Clean(fgkRecParticlesName);
129   CleanFolders() ;
130   // set to 0x0 the objgetter in AliGetter ... weird isn it !
131   AliPHOSGetter * gime = AliPHOSGetter::Instance() ; // (AliLoader::GetRunLoader()->GetFileName()).Data()) ; 
132   if (gime) 
133     gime->Reset() ;
134 }
135
136 //____________________________________________________________________________ 
137 void AliPHOSLoader::CleanFolders()
138  {
139    CleanRecParticles();
140    AliLoader::CleanFolders();
141  }
142
143 //____________________________________________________________________________ 
144 Int_t AliPHOSLoader::SetEvent()
145 {
146 //Cleans loaded stuff and and sets Files and Directories
147 // do not post any data to folder/tasks
148
149
150  Int_t retval = AliLoader::SetEvent();
151   if (retval)
152    {
153      AliError("returned error");
154      return retval;
155    }
156
157
158   if (Hits()) Hits()->Clear();
159   if (SDigits()) SDigits()->Clear();
160   if (Digits()) Digits()->Clear();
161   if (EmcRecPoints()) EmcRecPoints()->Clear();
162   if (CpvRecPoints()) CpvRecPoints()->Clear();
163   if (TrackSegments()) TrackSegments()->Clear();
164   if (RecParticles()) RecParticles()->Clear();
165    
166   return 0;
167 }
168
169 //____________________________________________________________________________ 
170 Int_t AliPHOSLoader::GetEvent()
171 {
172 //Overloads GetEvent method called by AliRunLoader::GetEvent(Int_t) method
173 //to add Rec Particles specific for PHOS
174
175 //First call the original method to get whatever from std. setup is needed
176   Int_t retval;
177   
178   retval = AliLoader::GetEvent();
179   if (retval)
180    {
181      AliError("returned error");
182      return retval;
183    }
184   
185   if (GetHitsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadHits();
186   if (GetSDigitsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadSDigits();
187   if (GetDigitsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadDigits();
188   if (GetRecPointsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadRecPoints();
189   if (GetTracksDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadTracks();
190   if (GetRecParticlesDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadRecParticles();
191
192
193 //Now, check if RecPart were loaded  
194   return 0;
195 }
196
197 //____________________________________________________________________________ 
198 const AliPHOS * AliPHOSLoader::PHOS() 
199 {
200   // returns the PHOS object 
201   AliPHOS * phos = dynamic_cast<AliPHOS*>(GetModulesFolder()->FindObject(fDetectorName));
202   if ( phos == 0x0) 
203     if (fDebug)
204       cout << "WARNING: AliPHOSLoader::PHOS -> PHOS module not found in Folders" << endl ; 
205   return phos ; 
206 }  
207
208 //____________________________________________________________________________ 
209 const AliPHOSGeometry * AliPHOSLoader::PHOSGeometry() 
210 {
211   // Return PHOS geometry
212   AliPHOSGeometry * rv = 0 ; 
213   if (PHOS() )
214     rv =  PHOS()->GetGeometry();
215   return rv ; 
216
217
218
219 //____________________________________________________________________________ 
220 Int_t AliPHOSLoader::LoadHits(Option_t* opt)
221 {  
222 //------- Hits ----------------------
223 //Overload (extends) LoadHits implemented in AliLoader
224 //
225   Int_t res;
226   
227   //First call the AliLoader's method to send the TreeH to folder
228   res = AliLoader::LoadHits(opt);
229   
230   if (res)
231    {//oops, error
232      AliError("returned error");
233      return res;
234    }
235
236   //read the data from tree in folder and send it to folder
237   res = ReadHits();
238   return 0;
239 }
240
241
242 //____________________________________________________________________________ 
243 Int_t AliPHOSLoader::LoadSDigits(Option_t* opt)
244 {
245   //---------- SDigits -------------------------
246   Int_t res;
247   //First call the AliLoader's method to send the TreeS to folder
248   res = AliLoader::LoadSDigits(opt);
249   if (res)
250    {//oops, error
251      AliError("returned error");
252      return res;
253    }
254   return ReadSDigits();
255    
256
257 //____________________________________________________________________________ 
258 Int_t AliPHOSLoader::LoadDigits(Option_t* opt)
259
260   //---------- Digits -------------------------
261   Int_t res;
262   //First call the AliLoader's method to send the TreeS to folder
263   res = AliLoader::LoadDigits(opt);
264   if (res)
265    {//oops, error
266      AliError("returned error");
267      return res;
268    }
269   return ReadDigits();
270 }
271 //____________________________________________________________________________ 
272 Int_t AliPHOSLoader::LoadRecPoints(Option_t* opt) 
273 {
274   // -------------- RecPoints -------------------------------------------
275   Int_t res;
276   //First call the AliLoader's method to send the TreeR to folder
277   res = AliLoader::LoadRecPoints(opt);
278   if (res)
279    {//oops, error
280      AliError("returned error");
281      return res;
282    }
283
284   TFolder * phosFolder = GetDetectorDataFolder();
285   if ( phosFolder  == 0x0 ) 
286    {
287      AliError("Can not get detector data folder");
288      return 1;
289    }
290   return ReadRecPoints();
291 }
292 //____________________________________________________________________________ 
293
294 Int_t  AliPHOSLoader::LoadTracks(Option_t* opt)
295 {
296  //Loads Tracks: Open File, Reads Tree and posts, Read Data and Posts
297   AliDebug(1, Form("opt = %s",opt));
298  Int_t res;
299  res = AliLoader::LoadTracks(opt);
300  if (res)
301    {//oops, error
302       AliError("returned error");
303       return res;
304    }  
305  return ReadTracks();
306 }
307
308 //____________________________________________________________________________ 
309 Int_t AliPHOSLoader::LoadRecParticles(Option_t* opt) 
310 {
311   // -------------- RecPoints -------------------------------------------
312   Int_t res;
313   //First call the AliLoader's method to send the TreeT to folder
314   res = AliLoader::LoadRecParticles(opt);
315   if (res)
316    {//oops, error
317      AliError("returned error");
318      return res;
319    }
320   return ReadRecParticles();
321 }
322
323 //____________________________________________________________________________ 
324 //PostHits etc. PostXXX must be const - not to hide virtual functions
325 //from base class AliLoader, but they call non-constant functions ReadXXX
326 //so I have to const_cast this pointer
327 Int_t AliPHOSLoader::PostHits()const
328 {
329   // -------------- Hits -------------------------------------------
330   Int_t reval = AliLoader::PostHits();
331   if (reval)
332    {
333      AliError("returned error");
334      return reval;
335    }
336    
337   return const_cast<AliPHOSLoader *>(this)->ReadHits();
338 }
339 //____________________________________________________________________________ 
340
341 Int_t AliPHOSLoader::PostSDigits()const
342 {
343   // -------------- SDigits -------------------------------------------
344   Int_t reval = AliLoader::PostSDigits();
345   if (reval)
346    {
347      AliError("returned error");
348      return reval;
349    }
350   return const_cast<AliPHOSLoader *>(this)->ReadSDigits();
351 }
352 //____________________________________________________________________________ 
353
354 Int_t AliPHOSLoader::PostDigits()const
355 {
356   // -------------- Digits -------------------------------------------
357   Int_t reval = AliLoader::PostDigits();
358   if (reval)
359    {
360      AliError("returned error");
361      return reval;
362    }
363   return const_cast<AliPHOSLoader *>(this)->ReadDigits();
364 }
365 //____________________________________________________________________________ 
366
367 Int_t AliPHOSLoader::PostRecPoints()const
368 {
369   // -------------- RecPoints -------------------------------------------
370   Int_t reval = AliLoader::PostRecPoints();
371   if (reval)
372    {
373      AliError("returned error");
374      return reval;
375    }
376   return const_cast<AliPHOSLoader *>(this)->ReadRecPoints();
377 }
378
379 //____________________________________________________________________________ 
380
381 Int_t AliPHOSLoader::PostRecParticles()const
382 {
383   // -------------- RecParticles -------------------------------------------
384   Int_t reval = AliLoader::PostRecParticles();
385   if (reval)
386    {
387      AliError("returned error");
388      return reval;
389    }
390   return const_cast<AliPHOSLoader *>(this)->ReadRecParticles();
391 }
392 //____________________________________________________________________________ 
393
394 Int_t AliPHOSLoader::PostTracks()const
395 {
396   // -------------- Tracks -------------------------------------------
397   Int_t reval = AliLoader::PostTracks();
398   if (reval)
399    {
400      AliError("returned error");
401      return reval;
402    }
403   return const_cast<AliPHOSLoader *>(this)->ReadTracks();
404 }
405 //____________________________________________________________________________ 
406
407
408
409 //____________________________________________________________________________ 
410 Int_t AliPHOSLoader::ReadHits()
411 {
412 // If there is no Clones Array in folder creates it and sends to folder
413 // then tries to read
414 // Reads the first entry of PHOS branch in hit tree TreeH()
415 // Reads data from TreeH and stores it in TClonesArray that sits in DetectorDataFolder
416 //
417   TObject** hitref = HitsRef();
418   if(hitref == 0x0)
419    {
420      MakeHitsArray();
421      hitref = HitsRef();
422    }
423
424   TClonesArray* hits = dynamic_cast<TClonesArray*>(*hitref);
425
426   TTree* treeh = TreeH();
427   
428   if(treeh == 0)
429    {
430     AliError("Cannot read TreeH from folder");
431     return 1;
432   }
433   
434   TBranch * hitsbranch = treeh->GetBranch(fDetectorName);
435   if (hitsbranch == 0) 
436    {
437     AliError("Cannot find branch PHOS"); 
438     return 1;
439   }
440
441   AliDebug(1, "Reading Hits");
442   
443   if (hitsbranch->GetEntries() > 1)
444    {
445     TClonesArray * tempo =  new TClonesArray("AliPHOSHit",1000);
446
447     hitsbranch->SetAddress(&tempo);
448     Int_t index = 0 ; 
449     Int_t i = 0 ;
450     for (i = 0 ; i < hitsbranch->GetEntries(); i++) 
451      {
452       hitsbranch->GetEntry(i) ;
453       Int_t j = 0 ;
454       for ( j = 0 ; j < tempo->GetEntries() ; j++) 
455        {
456          AliPHOSHit* hit = (AliPHOSHit*)tempo->At(j); 
457          new((*hits)[index]) AliPHOSHit( *hit ) ;
458          index++ ; 
459        }
460      }
461     tempo->Delete();
462     delete tempo;
463    }
464   else 
465    {
466     hitsbranch->SetAddress(hitref);
467     hitsbranch->GetEntry(0) ;
468    }
469
470   return 0;
471 }
472 //____________________________________________________________________________ 
473 Int_t AliPHOSLoader::ReadSDigits()
474 {
475 // Read the summable digits tree TreeS():
476 // Check if TClones is in folder
477 // if not create and add to folder
478 // connect to tree if available
479 // Read the data
480
481   TObject** sdref = SDigitsRef();
482   if(sdref == 0x0)
483    {
484      MakeSDigitsArray();
485      sdref = SDigitsRef();
486    }
487    
488   TTree * treeS = TreeS();
489   if(treeS==0)
490    {
491      //May happen if file is truncated or new in LoadSDigits
492      //AliError("There is no SDigit Tree");
493      return 0;
494    }
495   
496   TBranch * branch = treeS->GetBranch(fDetectorName);
497   if (branch == 0) 
498    {//easy, maybe just a new tree
499     //AliError("Cannot find branch PHOS"); 
500     return 0;
501   }
502     
503   branch->SetAddress(SDigitsRef());
504   branch->GetEntry(0);
505   return 0;
506 }
507
508 //____________________________________________________________________________ 
509 Int_t AliPHOSLoader::ReadDigits()
510 {
511 // Read the summable digits tree TreeS():
512 // Check if TClones is in folder
513 // if not create and add to folder
514 // connect to tree if available
515 // Read the data
516   
517   TObject** dref = DigitsRef();
518   if(dref == 0x0)
519    {//if there is not array in folder, create it and put it there
520      MakeDigitsArray();
521      dref = DigitsRef();
522    }
523
524   TTree * treeD = TreeD();
525   if(treeD==0)
526    {
527      //May happen if file is truncated or new in LoadSDigits
528      //AliError("There is no Digit Tree");
529      return 0;
530    }
531
532   TBranch * branch = treeD->GetBranch(fDetectorName);
533   if (branch == 0) 
534    {//easy, maybe just a new tree
535     //AliError("Cannot find branch ",fDetectorName.Data()); 
536     return 0;
537    }
538   
539   branch->SetAddress(dref);//connect branch to buffer sitting in folder
540   branch->GetEntry(0);//get first event 
541
542   return 0;  
543 }
544
545 //____________________________________________________________________________ 
546
547 void AliPHOSLoader::Track(Int_t itrack)
548 {
549 // Read the first entry of PHOS branch in hit tree gAlice->TreeH()
550   if(TreeH()== 0)
551    {
552      if (LoadHits())
553       {
554         AliError("Can not load hits.");
555         return;
556       } 
557    }
558   
559   TBranch * hitsbranch = dynamic_cast<TBranch*>(TreeH()->GetListOfBranches()->FindObject("PHOS")) ;
560   if ( !hitsbranch ) {
561     if (fDebug)
562       cout << "WARNING:  AliPHOSLoader::ReadTreeH -> Cannot find branch PHOS" << endl ; 
563     return ;
564   }  
565   if(!Hits()) PostHits();
566
567   hitsbranch->SetAddress(HitsRef());
568   hitsbranch->GetEntry(itrack);
569
570 }
571
572 //____________________________________________________________________________ 
573 Int_t AliPHOSLoader::ReadRecPoints()
574 {
575 //Creates and posts to folder an array container, 
576 //connects branch in tree (if exists), and reads data to array
577   
578   MakeRecPointsArray();
579   
580   TObjArray * cpva = 0x0 ; 
581   TObjArray * emca = 0x0 ; 
582   
583   TTree * treeR = TreeR();
584  
585   if(treeR==0)
586    {
587      //May happen if file is truncated or new in LoadSDigits
588      return 0;
589    }
590
591   Int_t retval = 0;
592   TBranch * emcbranch = treeR->GetBranch(fgkEmcRecPointsBranchName);
593
594   if (emcbranch == 0x0)
595    {
596      AliError(Form("Can not get branch with EMC Rec. Points named %s",
597                    fgkEmcRecPointsBranchName.Data()));
598      retval = 1;
599    }
600   else
601    {
602      emcbranch->SetAddress(&emca) ;
603      emcbranch->GetEntry(0) ;
604    }
605   TBranch * cpvbranch = treeR->GetBranch(fgkCpvRecPointsBranchName);
606   if (cpvbranch == 0x0)
607    {
608      AliError(Form("Can not get branch with CPV Rec. Points named %s",
609                    fgkCpvRecPointsBranchName.Data()));
610      retval = 2;
611    }
612   else
613    {
614      cpvbranch->SetAddress(&cpva);
615      cpvbranch->GetEntry(0) ;
616    }
617
618   Int_t ii ; 
619   Int_t maxemc = emca->GetEntries() ; 
620   for ( ii= 0 ; ii < maxemc ; ii++ ) 
621     EmcRecPoints()->Add(emca->At(ii)) ;
622  
623   Int_t maxcpv = cpva->GetEntries() ;
624   for ( ii= 0 ; ii < maxcpv ; ii++ )
625     CpvRecPoints()->Add(cpva->At(ii)) ; 
626
627   return retval;
628 }
629
630 //____________________________________________________________________________ 
631 Int_t AliPHOSLoader::ReadTracks()
632 {
633 //Creates and posts to folder an array container, 
634 //connects branch in tree (if exists), and reads data to arry
635
636   TObject** trkref = TracksRef();
637   if ( trkref == 0x0 )   
638    {//Create and post array
639     MakeTrackSegmentsArray();
640     trkref = TracksRef();
641    }
642
643   TTree * treeT = TreeT();
644   if(treeT==0)
645    {
646      //May happen if file is truncated or new in LoadSDigits, or the file is in update mode, 
647      //but tracking was not performed yet for a current event
648      //AliError("There is no Tree with Tracks");
649      return 0;
650    }
651   
652   TBranch * branch = treeT->GetBranch(fgkTrackSegmentsBranchName);
653   if (branch == 0) 
654    {//easy, maybe just a new tree
655     AliError(Form("Cannot find branch named %s",
656                   fgkTrackSegmentsBranchName.Data()));
657     return 0;
658   }
659
660   branch->SetAddress(trkref);//connect branch to buffer sitting in folder
661   branch->GetEntry(0);//get first event 
662   
663   return 0;
664 }
665 //____________________________________________________________________________ 
666
667 Int_t AliPHOSLoader::ReadRecParticles()
668 {
669 //Reads Reconstructed  Particles from file
670 //Creates and posts to folder an array container, 
671 //connects branch in tree (if exists), and reads data to arry
672   
673   TObject** recpartref = RecParticlesRef();
674   
675   if ( recpartref == 0x0 )   
676    {//Create and post array
677     MakeRecParticlesArray();
678     recpartref = RecParticlesRef();
679    }
680
681   TTree * treeP = TreeP();
682   if(treeP==0)
683    {
684      //May happen if file is truncated or new in LoadSDigits, 
685      //or the file is in update mode, 
686      //but tracking was not performed yet for a current event
687      //     AliError("There is no Tree with Tracks and Reconstructed Particles");
688      return 0;
689    }
690   
691   TBranch * branch = treeP->GetBranch(fgkRecParticlesBranchName);
692   if (branch == 0) 
693    {//easy, maybe just a new tree
694     AliError(Form("Cannot find branch %s",
695                   fgkRecParticlesBranchName.Data())); 
696     return 0;
697   }
698
699   branch->SetAddress(recpartref);//connect branch to buffer sitting in folder
700   branch->GetEntry(0);//get first event 
701   
702   return 0;
703 }
704
705
706 AliPHOSGeometry* AliPHOSLoader::GetPHOSGeometry()
707 {
708   //returns PHOS geometry from gAlice 
709   //static Method used by some classes where it is not convienient to pass eventfoldername
710   if (gAlice == 0x0)
711     return 0x0;
712   AliPHOS* phos=dynamic_cast<AliPHOS*>(gAlice->GetDetector("PHOS"));
713   if (phos == 0x0)
714     return 0x0;
715   return phos->GetGeometry();
716 }
717 /***************************************************************************************/
718
719 AliPHOSLoader* AliPHOSLoader::GetPHOSLoader(const  char* eventfoldername)
720 {
721   // Return PHOS loader
722   AliRunLoader* rn  = AliRunLoader::GetRunLoader(eventfoldername);
723   if (rn == 0x0) {
724     printf("Can not find Run Loader in folder %s", eventfoldername);
725     return 0x0 ; 
726   }
727   return dynamic_cast<AliPHOSLoader*>(rn->GetLoader("PHOSLoader"));
728 }
729 /***************************************************************************************/
730
731 Bool_t AliPHOSLoader::BranchExists(const TString& recName)
732 {
733   // Check if a branch named redName exists
734   if (fBranchTitle.IsNull()) return kFALSE;
735   TString dataname, zername ;
736   TTree* tree;
737   if(recName == "SDigits")
738    {
739     tree = TreeS();
740     dataname = GetDetectorName();
741     zername = "AliPHOSSDigitizer" ;
742    }
743   else
744     if(recName == "Digits"){
745       tree = TreeD();
746       dataname = GetDetectorName();
747       zername = "AliPHOSDigitizer" ;
748     }
749     else
750       if(recName == "RecPoints"){
751        tree = TreeR();
752        dataname = fgkEmcRecPointsBranchName;
753        zername = "AliPHOSClusterizer" ;
754       }
755       else
756        if(recName == "TrackSegments"){
757          tree = TreeT();
758          dataname = fgkTrackSegmentsBranchName;
759          zername = "AliPHOSTrackSegmentMaker";
760        }        
761        else
762          if(recName == "RecParticles"){
763            tree = TreeP();
764            dataname = fgkRecParticlesBranchName;
765            zername = "AliPHOSPID";
766          }
767          else
768            return kFALSE ;
769
770   
771   if(!tree ) 
772     return kFALSE ;
773
774   TObjArray * lob = static_cast<TObjArray*>(tree->GetListOfBranches()) ;
775   TIter next(lob) ; 
776   TBranch * branch = 0 ;  
777   TString titleName(fBranchTitle);
778   titleName+=":";
779
780   while ((branch = (static_cast<TBranch*>(next())))) {
781     TString branchName(branch->GetName() ) ; 
782     TString branchTitle(branch->GetTitle() ) ;  
783     if ( branchName.BeginsWith(dataname) && branchTitle.BeginsWith(fBranchTitle) ){  
784       AliWarning(Form("branch %s  with title  %s ",
785                       dataname.Data(),fBranchTitle.Data()));
786       return kTRUE ;
787     }
788     if ( branchName.BeginsWith(zername) &&  branchTitle.BeginsWith(titleName) ){
789       AliWarning(Form("branch AliPHOS... with title  %s ",
790                       branch->GetTitle()));
791       return kTRUE ; 
792     }
793   }
794   return kFALSE ;
795
796  }
797
798 void AliPHOSLoader::SetBranchTitle(const TString& btitle)
799 {
800   // Set branch title
801   if (btitle.CompareTo(fBranchTitle) == 0) return;
802   fBranchTitle = btitle;
803   ReloadAll();
804  }
805  
806 //____________________________________________________________________________ 
807 //Again, must be const not to hide virtual functions from AliLoader
808 //but there are calls to non-const functions, so I have to const_cast this pointer
809 void AliPHOSLoader::CleanHits()const
810 {
811   // Clean Hits array
812   AliLoader::CleanHits();
813   //Clear an array 
814   TClonesArray* hits = const_cast<AliPHOSLoader *>(this)->Hits();
815   if (hits) hits->Clear();
816 }
817 //____________________________________________________________________________ 
818
819 void AliPHOSLoader::CleanSDigits()const
820 {
821   // Clean SDigits array
822   AliLoader::CleanSDigits();
823   TClonesArray* sdigits = const_cast<AliPHOSLoader *>(this)->SDigits();
824   if (sdigits) sdigits->Clear();
825   
826 }
827 //____________________________________________________________________________ 
828
829 void AliPHOSLoader::CleanDigits()const
830 {
831   // Clean Digits array
832   AliLoader::CleanDigits();
833   TClonesArray* digits = const_cast<AliPHOSLoader *>(this)->Digits();
834   if (digits) digits->Clear();
835 }
836 //____________________________________________________________________________ 
837
838 void AliPHOSLoader::CleanRecPoints()const
839 {
840   // Clean RecPoints array
841   AliLoader::CleanRecPoints();
842   TObjArray* recpoints = const_cast<AliPHOSLoader *>(this)->EmcRecPoints();
843   if (recpoints) recpoints->Clear();
844   recpoints = const_cast<AliPHOSLoader *>(this)->CpvRecPoints();
845   if (recpoints) recpoints->Clear();
846 }
847 //____________________________________________________________________________ 
848
849 void AliPHOSLoader::CleanTracks()const
850 {
851   //Cleans Tracks stuff
852   AliLoader::CleanTracks();//tree
853   
854   //and clear the array
855   TClonesArray* tracks = const_cast<AliPHOSLoader *>(this)->TrackSegments();
856   if (tracks) tracks->Clear();
857
858 }
859 //____________________________________________________________________________ 
860
861 void AliPHOSLoader::CleanRecParticles()
862  {
863    // Clean RecParticles array
864    TClonesArray *recpar = RecParticles();
865    if (recpar) recpar->Clear();
866   
867  
868  }
869 //____________________________________________________________________________ 
870
871 void AliPHOSLoader::ReadCalibrationDB(const char * database,const char * filename)
872 {
873   // Read calibration data base from file
874   if(fcdb && (strcmp(database,fcdb->GetTitle())==0))
875     return ;
876
877   TFile * file = gROOT->GetFile(filename) ;
878   if(!file)
879     file = TFile::Open(filename);
880   if(!file){
881     AliError(Form("Cannot open file %s", filename)) ;
882     return ;
883   }
884   if(fcdb)
885     fcdb->Delete() ;
886   fcdb = dynamic_cast<AliPHOSCalibrationDB *>(file->Get("AliPHOSCalibrationDB")) ;
887   if(!fcdb)
888     AliError(Form("No database %s in file %s", database, filename)) ;
889 }
890 //____________________________________________________________________________ 
891
892 // AliPHOSSDigitizer*  AliPHOSLoader::PHOSSDigitizer() 
893 // { 
894 // //return PHOS SDigitizer
895 //  return  dynamic_cast<AliPHOSSDigitizer*>(SDigitizer()) ;
896 // }
897
898 //____________________________________________________________________________ 
899 void AliPHOSLoader::MakeHitsArray()
900 {
901   // Add Hits array to the data folder
902   if (Hits()) return;
903   TClonesArray* hits = new TClonesArray("AliPHOSHit",1000);
904   hits->SetName(fgkHitsName);
905   GetDetectorDataFolder()->Add(hits);
906 }
907
908 //____________________________________________________________________________ 
909 void AliPHOSLoader::MakeSDigitsArray()
910 {
911   // Add SDigits array to the data folder
912   if ( SDigits()) return;
913   TClonesArray* sdigits = new TClonesArray("AliPHOSDigit",1);
914   sdigits->SetName(fgkSDigitsName);
915   GetDetectorDataFolder()->Add(sdigits);
916 }
917
918 //____________________________________________________________________________ 
919 void AliPHOSLoader::MakeDigitsArray()
920 {
921   // Add Digits array to the data folder
922   if ( Digits()) return;
923   TClonesArray* digits = new TClonesArray("AliPHOSDigit",1);
924   digits->SetName(fgkDigitsName);
925   GetDetectorDataFolder()->Add(digits);
926   
927 }
928
929 //____________________________________________________________________________ 
930 void AliPHOSLoader::MakeRecPointsArray()
931 {
932   // Add RecPoints array to the data folder
933   if ( EmcRecPoints() == 0x0)
934    {
935      AliDebug(9, "Making array for EMC");
936     TObjArray* emc = new TObjArray(100) ;
937     emc->SetName(fgkEmcRecPointsName) ;
938     GetDetectorDataFolder()->Add(emc);
939    }
940
941   if ( CpvRecPoints() == 0x0)
942    { 
943      AliDebug(9, "Making array for CPV");
944      TObjArray* cpv = new TObjArray(100) ;
945      cpv->SetName(fgkCpvRecPointsName);
946      GetDetectorDataFolder()->Add(cpv);
947    }
948 }
949
950 //____________________________________________________________________________ 
951 void AliPHOSLoader::MakeTrackSegmentsArray()
952 {
953   // Add TrackSegments array to the data folder
954   if ( TrackSegments()) return;
955   TClonesArray * ts = new TClonesArray("AliPHOSTrackSegment",100) ;
956   ts->SetName(fgkTracksName);
957   GetDetectorDataFolder()->Add(ts);
958
959 }
960
961 //____________________________________________________________________________ 
962 void AliPHOSLoader::MakeRecParticlesArray()
963 {
964   // Add RecParticles array to the data folder
965   if ( RecParticles()) return;
966   TClonesArray * rp = new TClonesArray("AliPHOSRecParticle",100) ;
967   rp->SetName(fgkRecParticlesName);
968   GetDetectorDataFolder()->Add(rp);
969 }