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