]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EMCAL/AliEMCALLoader.cxx
bugfix
[u/mrichter/AliRoot.git] / EMCAL / AliEMCALLoader.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 AliEMCALAnalyser):
28 //  for(Int_t irecp = 0; irecp < gime->NRecParticles() ; irecp++)
29 //     AliEMCALRecParticle * 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 AliEMCALIndexToObject and make
37 //*--         systematic usage of TFolders without changing the interface        
38 //////////////////////////////////////////////////////////////////////////////
39
40
41 // --- ROOT system ---
42
43 #include "TTree.h"
44
45 // --- Standard library ---
46
47 // --- AliRoot header files ---
48
49 #include "AliEMCALLoader.h"
50 #include "AliEMCAL.h"
51 #include "AliEMCALHit.h"
52
53 ClassImp(AliEMCALLoader)
54   
55   
56 const TString AliEMCALLoader::fgkHitsName("HITS");//Name for TClonesArray with hits from one event
57 const TString AliEMCALLoader::fgkSDigitsName("SDIGITS");//Name for TClonesArray 
58 const TString AliEMCALLoader::fgkDigitsName("DIGITS");//Name for TClonesArray 
59 const TString AliEMCALLoader::fgkPRERecPointsName("PRERECPOINTS");//Name for TClonesArray 
60 const TString AliEMCALLoader::fgkECARecPointsName("ECARECPOINTS");//Name for TClonesArray 
61 const TString AliEMCALLoader::fgkHCARecPointsName("HCARECPOINTS");//Name for TClonesArray 
62 const TString AliEMCALLoader::fgkTracksName("TRACKS");//Name for TClonesArray 
63 const TString AliEMCALLoader::fgkRecParticlesName("RECPARTICLES");//Name for TClonesArray
64
65 const TString AliEMCALLoader::fgkPRERecPointsBranchName("EMCALPRERP");//Name for branch with PreShower Reconstructed Points
66 const TString AliEMCALLoader::fgkECARecPointsBranchName("EMCALECARP");//Name for branch with ECA Reconstructed Points
67 const TString AliEMCALLoader::fgkHCARecPointsBranchName("EMCALHCARP");//Name for branch with HCA Reconstructed Points
68 const TString AliEMCALLoader::fgkTrackSegmentsBranchName("EMCALTS");//Name for branch with TrackSegments
69 const TString AliEMCALLoader::fgkRecParticlesBranchName("EMCALRP");//Name for branch with Reconstructed Particles
70
71 //____________________________________________________________________________ 
72 AliEMCALLoader::AliEMCALLoader()
73 {
74   fDebug = 0;
75   fRecParticlesLoaded = kFALSE;
76 }
77
78 //____________________________________________________________________________ 
79 AliEMCALLoader::AliEMCALLoader(const Char_t *detname,const Char_t *eventfoldername):
80   AliLoader(detname,eventfoldername)
81 {
82   fDebug=0;
83   fRecParticlesLoaded = kFALSE;
84 }
85
86 //____________________________________________________________________________ 
87 AliEMCALLoader::~AliEMCALLoader()
88 {
89   //remove and delete arrays
90   Clean(fgkHitsName);
91   Clean(fgkSDigitsName);
92   Clean(fgkDigitsName);
93   Clean(fgkPRERecPointsName);
94   Clean(fgkECARecPointsName);
95   Clean(fgkHCARecPointsName);
96   Clean(fgkTracksName);
97   Clean(fgkRecParticlesName);
98 }
99
100 //____________________________________________________________________________ 
101 void AliEMCALLoader::CleanFolders()
102 {
103   CleanRecParticles();
104   AliLoader::CleanFolders();
105 }
106
107 //____________________________________________________________________________ 
108 Int_t AliEMCALLoader::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 (PRERecPoints()) PRERecPoints()->Clear();
126   if (ECARecPoints()) ECARecPoints()->Clear();
127   if (HCARecPoints()) HCARecPoints()->Clear();
128   if (TrackSegments()) TrackSegments()->Clear();
129   if (RecParticles()) RecParticles()->Clear();
130    
131   return 0;
132 }
133
134 //____________________________________________________________________________ 
135 Int_t AliEMCALLoader::GetEvent()
136 {
137   //Overloads GetEvent method called by AliRunLoader::GetEvent(Int_t) method
138   //to add Rec Particles specific for EMCAL
139   
140   //First call the original method to get whatever from std. setup is needed
141   Int_t retval;
142   
143   retval = AliLoader::GetEvent();
144   if (retval)
145     {
146       Error("GetEvent","AliLoader::GetEvent returned error");
147       return retval;
148     }
149   
150   if (GetHitsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadHits();
151   if (GetSDigitsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadSDigits();
152   if (GetDigitsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadDigits();
153   if (GetRecPointsDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadRecPoints();
154   if (GetTracksDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadTracks();
155   if (GetRecParticlesDataLoader()->GetBaseDataLoader()->IsLoaded()) ReadRecParticles();
156
157
158   //Now, check if RecPart were loaded  
159   return 0;
160 }
161
162 //____________________________________________________________________________ 
163 const AliEMCAL * AliEMCALLoader::EMCAL() 
164 {
165   // returns the EMCAL object 
166   AliEMCAL * emcal = dynamic_cast<AliEMCAL*>(GetModulesFolder()->FindObject(fDetectorName));
167   if ( emcal == 0x0) 
168     if (fDebug)
169       cout << "WARNING: AliEMCALLoader::EMCAL -> EMCAL module not found in Folders" << endl ; 
170   return emcal ; 
171 }  
172
173 //____________________________________________________________________________ 
174 const AliEMCALGeometry * AliEMCALLoader::EMCALGeometry() 
175 {
176   // Gets the EMCAL Geometry object 
177   AliEMCALGeometry * rv = 0 ; 
178   if (EMCAL() )
179     rv =  EMCAL()->GetGeometry();
180   return rv ; 
181
182
183 //____________________________________________________________________________ 
184 Int_t AliEMCALLoader::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 Int_t AliEMCALLoader::LoadSDigits(Option_t* opt)
207 {  
208   // Loads the SDigits array in the folder structure
209   Int_t res;
210   //First call the AliLoader's method to send the TreeS to folder
211   res = AliLoader::LoadSDigits(opt);
212   if (res)
213     {//oops, error
214       Error("PostSDigits","AliLoader::LoadSDigits returned error");
215       return res;
216     }
217   return ReadSDigits();
218   
219
220
221 //____________________________________________________________________________ 
222 Int_t AliEMCALLoader::LoadDigits(Option_t* opt)
223
224   // Loads the Digits array in the folder structure
225   
226   Int_t res;
227   //First call the AliLoader's method to send the TreeS to folder
228   res = AliLoader::LoadDigits(opt);
229   if (res)
230     {//oops, error
231       Error("LoadDigits","AliLoader::LoadDigits returned error");
232       return res;
233     }
234   return ReadDigits();
235 }
236
237 //____________________________________________________________________________ 
238 Int_t AliEMCALLoader::LoadRecPoints(Option_t* opt) 
239
240   // Loads the RecPoints array in the folder structure
241   Int_t res;
242   //First call the AliLoader's method to send the TreeR to folder
243   res = AliLoader::LoadRecPoints(opt);
244   if (res)
245     {//oops, error
246       Error("LoadRecPoints","AliLoader::LoadRecPoints returned error");
247       return res;
248     }
249   
250   TFolder * emcalFolder = GetDetectorDataFolder();
251   if ( emcalFolder  == 0x0 ) 
252     {
253       Error("LoadRecPoints","Can not get detector data folder");
254       return 1;
255     }
256   return ReadRecPoints();
257 }
258
259 //____________________________________________________________________________ 
260 Int_t  AliEMCALLoader::LoadTracks(Option_t* opt)
261 {
262   //Loads Tracks: Open File, Reads Tree and posts, Read Data and Posts
263   if (GetDebug()) 
264     Info("LoadTracks","opt = %s",opt);
265   if (fTracksLoaded)
266     {
267       Warning("LoadTracks","Tracks are already loaded");
268       return 0;
269     }
270   Int_t res;
271   //First call the AliLoader's method to send the TreeS to folder
272   if (GetTracksDataLoader()->GetBaseLoader(0)->IsLoaded() == kFALSE) 
273     {//tracks can be loaded by LoadRecPoints
274       res = AliLoader::LoadTracks(opt);
275       if (res)
276         {//oops, error
277           Error("LoadTracks","AliLoader::LoadTracks returned error");
278           return res;
279         }
280     }
281   res = ReadTracks();
282   if (res)
283     {
284       Error("LoadTracks","Error occured while reading Tracks");
285       return res;
286     }
287   
288   fTracksLoaded = kTRUE;
289   return 0;
290 }
291
292 //____________________________________________________________________________ 
293 Int_t AliEMCALLoader::LoadRecParticles(Option_t* opt) 
294
295   // Loads the RecParticles array in the folder structure
296   Int_t res;
297   //First call the AliLoader's method to send the TreeS to folder
298   res = AliLoader::LoadRecParticles(opt);
299   if (res)
300     {//oops, error
301       Error("LoadRecParticles","AliLoader::LoadRecParticles returned error");
302       return res;
303     }
304   
305   TFolder * emcalFolder = GetDetectorDataFolder();
306   if ( emcalFolder  == 0x0 ) 
307     {
308       Error("PostDigits","Can not get detector data folder");
309       return 1;
310     }
311   return ReadRecParticles();
312 }
313
314 //____________________________________________________________________________ 
315 Int_t AliEMCALLoader::PostHits()
316 {
317   Int_t reval = AliLoader::PostHits();
318   if (reval)
319     {
320      Error("PostHits","AliLoader::  returned error");
321      return reval;
322     }
323   return ReadHits();
324 }
325
326 //____________________________________________________________________________ 
327 Int_t AliEMCALLoader::PostSDigits()
328 {
329   // Posts the SDigits array to the folder structure
330   Int_t reval = AliLoader::PostSDigits();
331   if (reval)
332    {
333      Error("PostSDigits","AliLoader::PostSDigits  returned error");
334      return reval;
335    }
336   return ReadSDigits();
337 }
338
339 //____________________________________________________________________________ 
340 Int_t AliEMCALLoader::PostDigits()
341 {
342   // Posts the Digits array to the folder structure
343   Int_t reval = AliLoader::PostDigits();
344   if (reval)
345     {
346       Error("PostDigits","AliLoader::PostDigits  returned error");
347       return reval;
348     }
349   return ReadDigits();
350 }
351
352 //____________________________________________________________________________ 
353 Int_t AliEMCALLoader::PostRecPoints()
354 {
355   // Posts the RecPoints array to the folder structure
356   Int_t reval = AliLoader::PostRecPoints();
357   if (reval)
358    {
359      Error("PostRecPoints","AliLoader::PostRecPoints  returned error");
360      return reval;
361    }
362   return ReadRecPoints();
363 }
364
365 //____________________________________________________________________________ 
366 Int_t AliEMCALLoader::PostRecParticles()
367 {
368   // Posts the RecParticles array to the folder structure
369   
370   Int_t reval = AliLoader::PostRecParticles();
371   if (reval)
372     {
373       Error("PostRecParticles","AliLoader::PostRecParticles  returned error");
374       return reval;
375     }
376   return ReadRecParticles();
377 }
378
379 //____________________________________________________________________________ 
380 Int_t AliEMCALLoader::PostTracks()
381 {
382   // Posts the Tracks array to the folder structure
383   Int_t reval = AliLoader::PostTracks();
384   if (reval)
385     {
386       Error("PostTracks","AliLoader::PostTracks  returned error");
387       return reval;
388     }
389   return ReadTracks();
390 }
391
392 //____________________________________________________________________________ 
393 Int_t AliEMCALLoader::ReadHits()
394 {
395   // If there is no Clones Array in folder creates it and sends to folder
396   // then tries to read
397   // Reads the first entry of EMCAL branch in hit tree TreeH()
398   // Reads data from TreeH and stores it in TClonesArray that sits in DetectorDataFolder
399   //
400   TObject** hitref = HitsRef();
401   if(hitref == 0x0)
402     {
403       MakeHitsArray();
404      hitref = HitsRef();
405     }
406   
407   TClonesArray* hits = dynamic_cast<TClonesArray*>(*hitref);
408   
409   TTree* treeh = TreeH();
410   
411   if(treeh == 0)
412     {
413       Error("ReadHits"," Cannot read TreeH from folder");
414       return 1;
415     }
416   
417   TBranch * hitsbranch = treeh->GetBranch(fDetectorName);
418   if (hitsbranch == 0) 
419     {
420       Error("ReadHits"," Cannot find branch EMCAL"); 
421       return 1;
422     }
423   
424   if (GetDebug()) 
425     Info("ReadHits","Reading Hits");
426   
427   if (hitsbranch->GetEntries() > 1)
428     {
429       TClonesArray * tempo =  new TClonesArray("AliEMCALHit",1000);
430       
431       hitsbranch->SetAddress(&tempo);
432       Int_t index = 0 ; 
433       Int_t i = 0 ;
434       for (i = 0 ; i < hitsbranch->GetEntries(); i++) 
435         {
436           hitsbranch->GetEntry(i) ;
437           Int_t j = 0 ;
438           for ( j = 0 ; j < tempo->GetEntries() ; j++) 
439             {
440               AliEMCALHit* hit = (AliEMCALHit*)tempo->At(j); 
441               new((*hits)[index]) AliEMCALHit( *hit ) ;
442               index++ ; 
443             }
444         }
445       delete tempo;
446     }
447   else 
448     {
449       hitsbranch->SetAddress(hitref);
450       hitsbranch->GetEntry(0) ;
451     }
452   
453   return 0;
454 }
455
456 //____________________________________________________________________________ 
457 Int_t AliEMCALLoader::ReadSDigits()
458 {
459   // Read the summable digits tree TreeS():
460   // Check if TClones is in folder
461   // if not create and add to folder
462   // connect to tree if available
463   // Read the data
464   
465   TObject** sdref = SDigitsRef();
466   if(sdref == 0x0)
467     {
468       MakeSDigitsArray();
469       sdref = SDigitsRef();
470     }
471   
472   TTree * treeS = TreeS();
473   if(treeS==0)
474     {
475       //May happen if file is truncated or new in LoadSDigits
476       //Error("ReadSDigits","There is no SDigit Tree");
477       return 0;
478     }
479   
480   TBranch * branch = treeS->GetBranch(fDetectorName);
481   if (branch == 0) 
482     {//easy, maybe just a new tree
483       //Error("ReadSDigits"," Cannot find branch EMCAL"); 
484       return 0;
485     }
486   
487   branch->SetAddress(SDigitsRef());
488   branch->GetEntry(0);
489   return 0;
490 }
491
492 //____________________________________________________________________________ 
493 Int_t AliEMCALLoader::ReadDigits()
494 {
495   // Read the summable digits tree TreeS():
496   // Check if TClones is in folder
497   // if not create and add to folder
498   // connect to tree if available
499   // Read the data
500   
501   TObject** dref = DigitsRef();
502   if(dref == 0x0)
503     {//if there is not array in folder, create it and put it there
504       MakeDigitsArray();
505       dref = DigitsRef();
506     }
507   
508   TTree * treeD = TreeD();
509   if(treeD==0)
510     {
511       //May happen if file is truncated or new in LoadSDigits
512       //Error("ReadDigits","There is no Digit Tree");
513       return 0;
514     }
515   
516   TBranch * branch = treeD->GetBranch(fDetectorName);
517   if (branch == 0) 
518     {//easy, maybe just a new tree
519       //Error("ReadDigits"," Cannot find branch ",fDetectorName.Data()); 
520       return 0;
521     }
522   
523   branch->SetAddress(dref);//connect branch to buffer sitting in folder
524   branch->GetEntry(0);//get first event 
525   
526   return 0;  
527 }
528
529 //____________________________________________________________________________ 
530 void AliEMCALLoader::UnloadRecParticles()
531 {
532   // Unloads the RecParticles array fromthe folder structure
533   fRecParticlesLoaded = kFALSE;
534   CleanRecParticles();
535   if (fTracksLoaded == kFALSE) UnloadTracks();
536 }
537
538 //____________________________________________________________________________ 
539 void AliEMCALLoader::UnloadTracks()
540 {
541   // Unloads the Tracks array fromthe folder structure
542   CleanTracks();//free the memory
543   //in case RecPart are loaded we can not onload tree and close the file
544   if (fRecParticlesLoaded == kFALSE) AliLoader::UnloadTracks();
545   fTracksLoaded = kFALSE;//mark that nobody needs them
546 }
547
548 //____________________________________________________________________________ 
549 void AliEMCALLoader::Track(Int_t itrack)
550 {
551   // Read the first entry of EMCAL branch in hit tree gAlice->TreeH()
552   if(TreeH()== 0)
553     {
554       if (LoadHits())
555         {
556           Error("Track","Can not load hits.");
557           return;
558         } 
559     }
560   
561   TBranch * hitsbranch = dynamic_cast<TBranch*>(TreeH()->GetListOfBranches()->FindObject("EMCAL")) ;
562   if ( !hitsbranch ) {
563     if (fDebug)
564       cout << "WARNING:  AliEMCALLoader::ReadTreeH -> Cannot find branch EMCAL" << endl ; 
565     return ;
566   }  
567   if(!Hits()) PostHits();
568   
569   hitsbranch->SetAddress(HitsRef());
570   hitsbranch->GetEntry(itrack);
571   
572 }
573
574 //____________________________________________________________________________ 
575 void AliEMCALLoader::ReadTreeQA()
576 {
577   // Read the digit tree gAlice->TreeQA()
578   // so far only EMCAL knows about this Tree  
579   
580   if(EMCAL()->TreeQA()== 0){
581     cerr <<   "ERROR: AliEMCALLoader::ReadTreeQA: can not read TreeQA " << endl ;
582     return ;
583   }
584   
585   TBranch * qabranch = EMCAL()->TreeQA()->GetBranch("EMCAL");
586   if (!qabranch) { 
587     if (fDebug)
588       cout << "WARNING: AliEMCALLoader::ReadTreeQA -> Cannot find QA Alarms for EMCAL" << endl ;
589     return ; 
590   }   
591   
592   //  if(!Alarms()) PostQA();
593   
594   qabranch->SetAddress(AlarmsRef()) ;
595   
596   qabranch->GetEntry(0) ;
597   
598 }
599
600 //____________________________________________________________________________ 
601 Int_t AliEMCALLoader::ReadRecPoints()
602 {
603   //Creates and posts to folder an array container, 
604   //connects branch in tree (if exists), and reads data to array
605   
606   MakeRecPointsArray();
607   
608   TObjArray * pre = 0x0 ; 
609   TObjArray * eca = 0x0 ; 
610   TObjArray * hca = 0x0 ; 
611
612   TTree * treeR = TreeR();
613   
614   if(treeR==0)
615     {
616       //May happen if file is truncated or new in LoadSDigits
617       return 0;
618     }
619   
620   Int_t retval = 0;
621   TBranch * prebranch = treeR->GetBranch(fgkPRERecPointsBranchName);
622   
623   if (prebranch == 0x0)
624     {
625       Error("ReadRecPoints","Can not get branch with PRE Rec. Points named %s",fgkPRERecPointsBranchName.Data());
626       retval = 1;
627     }
628   else
629     {
630       prebranch->SetAddress(&pre) ;
631       prebranch->GetEntry(0) ;
632     }
633   TBranch * ecabranch = treeR->GetBranch(fgkECARecPointsBranchName);
634   if (ecabranch == 0x0)
635     {
636       Error("ReadRecPoints","Can not get branch with ECA Rec. Points named %s",fgkECARecPointsBranchName.Data());
637       retval = 2;
638     }
639   else
640     {
641       ecabranch->SetAddress(&eca);
642       ecabranch->GetEntry(0) ;
643     }
644   TBranch * hcabranch = treeR->GetBranch(fgkHCARecPointsBranchName);
645   if (hcabranch == 0x0)
646     {
647       Error("ReadRecPoints","Can not get branch with HCA Rec. Points named %s",fgkHCARecPointsBranchName.Data());
648       retval = 2;
649     }
650   else
651     {
652       hcabranch->SetAddress(&hca);
653       hcabranch->GetEntry(0) ;
654     }
655   
656   Int_t ii ; 
657   Int_t maxpre = pre->GetEntries() ; 
658   for ( ii= 0 ; ii < maxpre ; ii++ ) 
659     PRERecPoints()->Add(pre->At(ii)) ;
660   
661   Int_t maxeca = eca->GetEntries() ; 
662   for ( ii= 0 ; ii < maxeca ; ii++ ) 
663     ECARecPoints()->Add(eca->At(ii)) ;
664   
665   Int_t maxhca = hca->GetEntries() ; 
666   for ( ii= 0 ; ii < maxhca ; ii++ ) 
667     HCARecPoints()->Add(hca->At(ii)) ;
668   
669   return retval;
670 }
671
672 //____________________________________________________________________________ 
673 Int_t AliEMCALLoader::ReadTracks()
674 {
675   //Creates and posts to folder an array container, 
676   //connects branch in tree (if exists), and reads data to arry
677   
678   TObject** trkref = TracksRef();
679   if ( trkref == 0x0 )   
680     {//Create and post array
681       MakeTrackSegmentsArray();
682       trkref = TracksRef();
683     }
684   
685   TTree * treeT = TreeT();
686   if(treeT==0)
687     {
688       //May happen if file is truncated or new in LoadSDigits, or the file is in update mode, 
689       //but tracking was not performed yet for a current event
690       //Error("ReadTracks","There is no Tree with Tracks");
691       return 0;
692     }
693   
694   TBranch * branch = treeT->GetBranch(fgkTrackSegmentsBranchName);
695   if (branch == 0) 
696     {//easy, maybe just a new tree
697       Error("ReadTracks"," Cannot find branch named %s",fgkTrackSegmentsBranchName.Data());
698       return 0;
699     }
700   
701   branch->SetAddress(trkref);//connect branch to buffer sitting in folder
702   branch->GetEntry(0);//get first event 
703   
704   return 0;
705 }
706
707 //____________________________________________________________________________ 
708 Int_t AliEMCALLoader::ReadRecParticles()
709 {
710   //Reads Reconstructed  Particles from file
711   //Creates and posts to folder an array container, 
712   //connects branch in tree (if exists), and reads data to arry
713   
714   TObject** recpartref = RecParticlesRef();
715   
716   if ( recpartref == 0x0 )   
717     {//Create and post array
718       MakeRecParticlesArray();
719       recpartref = RecParticlesRef();
720     }
721   
722   TTree * treeP = TreeP();
723   if(treeP==0)
724     {
725       //May happen if file is truncated or new in LoadSDigits, 
726       //or the file is in update mode, 
727       //but tracking was not performed yet for a current event
728       //     Error("ReadRecParticles","There is no Tree with Tracks and Reconstructed Particles");
729       return 0;
730     }
731   
732   TBranch * branch = treeP->GetBranch(fgkRecParticlesBranchName);
733   if (branch == 0) 
734     {//easy, maybe just a new tree
735       Error("ReadRecParticles"," Cannot find branch %s",fgkRecParticlesBranchName.Data()); 
736       return 0;
737     }
738   
739   branch->SetAddress(recpartref);//connect branch to buffer sitting in folder
740   branch->GetEntry(0);//get first event 
741   
742   return 0;
743 }
744
745 //____________________________________________________________________________ 
746 AliEMCALGeometry* AliEMCALLoader::GetEMCALGeometry()
747 {
748   //returns EMCAL geometry from gAlice 
749   //static Method used by some classes where it is not convienient to pass eventfoldername
750   if (gAlice == 0x0)
751     return 0x0;
752   AliEMCAL* emcal=dynamic_cast<AliEMCAL*>(gAlice->GetDetector("EMCAL"));
753   if (emcal == 0x0)
754     return 0x0;
755   return emcal->GetGeometry();
756 }
757
758 //____________________________________________________________________________ 
759 AliEMCALLoader* AliEMCALLoader::GetEMCALLoader(const  char* eventfoldername)
760 {
761   // Get an instance of the EMCALLoader object
762   AliRunLoader* rn  = AliRunLoader::GetRunLoader(eventfoldername);
763   if (rn == 0x0)
764     {
765       cerr<<"Error: <AliEMCALLoader::GetEMCALLoader>: "
766           << "Can not find Run Loader in folder "<<eventfoldername<<endl;
767       return 0x0;
768     }
769   return dynamic_cast<AliEMCALLoader*>(rn->GetLoader("EMCALLoader"));
770 }
771
772 //____________________________________________________________________________ 
773 Bool_t AliEMCALLoader::BranchExists(const TString& recName)
774 {
775   if (fBranchTitle.IsNull()) return kFALSE;
776   TString dataname, zername ;
777   TTree* tree;
778   if(recName == "SDigits") {
779     tree = TreeS();
780     dataname = GetDetectorName();
781     zername = "AliEMCALSDigitizer" ;
782   }
783   else if(recName == "Digits"){
784     tree = TreeD();
785     dataname = GetDetectorName();
786     zername = "AliEMCALDigitizer" ;
787   }
788   else if(recName == "PRERecPoints"){
789     tree = TreeR();
790     dataname = fgkPRERecPointsBranchName;
791     zername = "AliEMCALClusterizer" ;
792   }
793   else if(recName == "ECARecPoints"){
794     tree = TreeR();
795     dataname = fgkECARecPointsBranchName;
796     zername = "AliEMCALClusterizer" ;
797   }
798   else if(recName == "HCARecPoints"){
799     tree = TreeR();
800     dataname = fgkHCARecPointsBranchName;
801     zername = "AliEMCALClusterizer" ;
802   }
803   else if(recName == "TrackSegments"){
804     tree = TreeT();
805     dataname = fgkTrackSegmentsBranchName;
806     zername = "AliEMCALTrackSegmentMaker";
807   }        
808   else if(recName == "RecParticles"){
809     tree = TreeP();
810     dataname = fgkRecParticlesBranchName;
811     zername = "AliEMCALPID";
812   }
813   else
814     return kFALSE ;
815   
816   if(!tree ) 
817     return kFALSE ;
818   
819   TObjArray * lob = static_cast<TObjArray*>(tree->GetListOfBranches()) ;
820   TIter next(lob) ; 
821   TBranch * branch = 0 ;  
822   TString titleName(fBranchTitle);
823   titleName+=":";
824   
825   while ((branch = (static_cast<TBranch*>(next())))) {
826     TString branchName(branch->GetName() ) ; 
827     TString branchTitle(branch->GetTitle() ) ;  
828     if ( branchName.BeginsWith(dataname) && branchTitle.BeginsWith(fBranchTitle) ){  
829       Warning("BranchExists","branch %s  with title  %s ",dataname.Data(),fBranchTitle.Data());
830       return kTRUE ;
831     }
832     if ( branchName.BeginsWith(zername) &&  branchTitle.BeginsWith(titleName) ){
833       Warning("BranchExists","branch AliEMCAL... with title  %s ",branch->GetTitle());
834       return kTRUE ; 
835     }
836   }
837   return kFALSE ;
838   
839 }
840
841 //____________________________________________________________________________ 
842 void AliEMCALLoader::SetBranchTitle(const TString& btitle)
843 {
844   // Gives a name to a branch in the folder structure
845   if (btitle.CompareTo(fBranchTitle) == 0) return;
846   fBranchTitle = btitle;
847   ReloadAll();
848 }
849
850 //____________________________________________________________________________ 
851 void AliEMCALLoader::CleanHits()
852 {
853   AliLoader::CleanHits();
854   //Clear an array 
855   TClonesArray* hits = Hits();
856   if (hits) hits->Clear();
857 }
858
859 //____________________________________________________________________________ 
860 void AliEMCALLoader::CleanSDigits()
861 {
862   // Cleans the SDigits array in the folder structure
863   AliLoader::CleanSDigits();
864   TClonesArray* sdigits = SDigits();
865   if (sdigits) sdigits->Clear();
866   
867 }
868 //____________________________________________________________________________ 
869
870 void AliEMCALLoader::CleanDigits()
871 {
872   // Cleans the Digits array in the folder structure
873   AliLoader::CleanDigits();
874   TClonesArray* digits = Digits();
875   if (digits) digits->Clear();
876 }
877
878 //____________________________________________________________________________ 
879 void AliEMCALLoader::CleanRecPoints()
880 {
881   // Cleans the RecPoints array in the folder structure
882   AliLoader::CleanRecPoints();
883   TObjArray* recpoints = PRERecPoints();
884   if (recpoints) 
885     recpoints->Clear();
886   recpoints = ECARecPoints();
887   if (recpoints) 
888     recpoints->Clear();
889   recpoints = HCARecPoints();
890   if (recpoints) 
891     recpoints->Clear();
892 }
893
894 //____________________________________________________________________________ 
895 void AliEMCALLoader::CleanTracks()
896 {
897   // Cleans the Tracks array in the folder structure
898   AliLoader::CleanTracks();//tree
899   //and clear the array
900   TClonesArray* tracks = TrackSegments();
901   if (tracks) 
902     tracks->Clear();  
903 }
904
905 //____________________________________________________________________________ 
906 void AliEMCALLoader::CleanRecParticles()
907 {
908   // Cleans the RecParticles array in the folder structure
909   TClonesArray *recpar = RecParticles();
910   if (recpar) 
911     recpar->Clear();
912 }
913
914 //____________________________________________________________________________ 
915 // void AliEMCALLoader::ReadCalibrationDB(const char * database,const char * filename)
916 // {
917
918 //   if(fcdb && (strcmp(database,fcdb->GetTitle())==0))
919 //     return ;
920
921 //   TFile * file = gROOT->GetFile(filename) ;
922 //   if(!file)
923 //     file = TFile::Open(filename);
924 //   if(!file){
925 //     Error ("ReadCalibrationDB", "Cannot open file %s", filename) ;
926 //     return ;
927 //   }
928 //   if(fcdb)
929 //     fcdb->Delete() ;
930 //   fcdb = dynamic_cast<AliEMCALCalibrationDB *>(file->Get("AliEMCALCalibrationDB")) ;
931 //   if(!fcdb)
932 //     Error ("ReadCalibrationDB", "No database %s in file %s", database, filename) ;
933 // }
934 //____________________________________________________________________________ 
935
936 // AliEMCALSDigitizer*  AliEMCALLoader::EMCALSDigitizer() 
937 // { 
938 // //return EMCAL SDigitizer
939 //  return  dynamic_cast<AliEMCALSDigitizer*>(SDigitizer()) ;
940 // }
941
942 //____________________________________________________________________________ 
943 void AliEMCALLoader::MakeHitsArray()
944 {
945   // Create the array for Hits
946   if (Hits()) return;
947   TClonesArray* hits = new TClonesArray("AliEMCALHit",1000);
948   hits->SetName(fgkHitsName);
949   GetDetectorDataFolder()->Add(hits);
950 }
951
952 //____________________________________________________________________________ 
953 void AliEMCALLoader::MakeSDigitsArray()
954 {
955   // Create the array for SDigits
956   if ( SDigits()) return;
957   TClonesArray* sdigits = new TClonesArray("AliEMCALDigit",1);
958   sdigits->SetName(fgkSDigitsName);
959   GetDetectorDataFolder()->Add(sdigits);
960 }
961
962 //____________________________________________________________________________ 
963 void AliEMCALLoader::MakeDigitsArray()
964 {
965   // Create the array for Digits
966   if ( Digits()) return;
967   TClonesArray* digits = new TClonesArray("AliEMCALDigit",1);
968   digits->SetName(fgkDigitsName);
969   GetDetectorDataFolder()->Add(digits);
970   
971 }
972
973 //____________________________________________________________________________ 
974 void AliEMCALLoader::MakeRecPointsArray()
975
976   // Create the array for RecPoints
977   if ( PRERecPoints() == 0x0) {
978     if (GetDebug()>9) 
979       Info("MakeRecPointsArray","Making array for PRE");
980     TObjArray* pre = new TObjArray(100) ;
981     pre->SetName(fgkPRERecPointsName) ;
982     GetDetectorDataFolder()->Add(pre);
983   }
984   if ( ECARecPoints() == 0x0) {
985     if (GetDebug()>9) 
986       Info("MakeRecPointsArray","Making array for ECA");
987     TObjArray* eca = new TObjArray(100) ;
988     eca->SetName(fgkECARecPointsName) ;
989     GetDetectorDataFolder()->Add(eca);
990   }
991   if ( HCARecPoints() == 0x0) {
992     if (GetDebug()>9) 
993       Info("MakeRecPointsArray","Making array for HCA");
994     TObjArray* hca = new TObjArray(100) ;
995     hca->SetName(fgkHCARecPointsName) ;
996     GetDetectorDataFolder()->Add(hca);
997   }
998 }
999
1000 //____________________________________________________________________________ 
1001 void AliEMCALLoader::MakeTrackSegmentsArray()
1002 {
1003   // Create the array for TrackSegments
1004   if ( TrackSegments()) 
1005     return;
1006   TClonesArray * ts = new TClonesArray("AliEMCALTrackSegment",100) ;
1007   ts->SetName(fgkTracksName);
1008   GetDetectorDataFolder()->Add(ts);
1009 }
1010
1011 //____________________________________________________________________________ 
1012 void AliEMCALLoader::MakeRecParticlesArray()
1013 {  
1014   // Create the array for RecParticles
1015   if ( RecParticles()) return;
1016   TClonesArray * rp = new TClonesArray("AliEMCALRecParticle",100) ;
1017   rp->SetName(fgkRecParticlesName);
1018   GetDetectorDataFolder()->Add(rp);
1019 }