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