]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONDataInterface.cxx
Checking the TreeR content before reading rawcluster and trigger data for tracking...
[u/mrichter/AliRoot.git] / MUON / AliMUONDataInterface.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 #include <TError.h>
19 #include <TParticle.h>
20
21 #include "AliRunLoader.h"
22 #include "AliLoader.h"
23
24 #include "AliMUONDataInterface.h"
25 #include "AliMUONLocalTrigger.h"
26 #include "AliMUONGlobalTrigger.h"
27 #include "AliMUONHit.h"
28 #include "AliMUONDigit.h"
29 #include "AliMUONRawCluster.h"
30 #include "AliMUONTrack.h"
31 #include "AliLog.h"
32
33 #include <iostream>
34 using std::endl;
35 using std::cout;
36
37 ///
38 /// \class AliMUONDataInterface
39 ///
40 /// An easy to use interface to the MUON module data stored in
41 /// TreeK, TreeH, TreeS, TreeD and TreeR
42 /// One can fetch any of the data objects with all the calls to runloader, 
43 /// muon loader and AliMUONData done behind the scenes and automatically.
44 ///
45 /// This interface in not necessarily the fastest way to fetch the data but
46 /// it is the easiest.
47 /// Note: If independant calls to the run loader, muon loader or 
48 /// AliMUONData objects are interspersed with calls to the 
49 /// AliMUONDataInterface to fetch data, one might need to call the Reset 
50 /// method between these method calls at some point to prevent 
51 /// AliMUONDataInterface from getting confused.
52 /// This is necessary since this object assumes the state of runloader,
53 /// muon loader nor AliMUONData has not changed between calls.
54 /// If the state has changes then one must call Reset so that 
55 /// AliMUONDataInterface refreshes what it knows about the state
56 /// of the loader and AliMUONData objects.
57 ///
58 /// \deprecated We have to revisit all this AliMUONData stuff anyway,
59 /// and probably make a real AliMUONLoader instead...
60 ///
61 /// \author Artur Szostak
62 /// email: artur@alice.phy.uct.ac.za
63
64
65 /// \cond CLASSIMP
66 ClassImp(AliMUONDataInterface)
67 /// \endcond
68
69 AliMUONDataInterface::AliMUONDataInterface()
70         : TObject(), 
71           fCreatedRunLoader(kFALSE),
72           fHitAddressSet(kFALSE),
73           fSDigitAddressSet(kFALSE),
74           fDigitAddressSet(kFALSE),
75           fClusterAddressSet(kFALSE),
76           fTriggerAddressSet(kFALSE),
77           fRecTracksAddressSet(kFALSE),
78           fRunloader(0x0),
79           fMuonloader(0x0),
80           fData(0x0, "MUON", "MUON"),
81           fFilename(),
82           fFoldername(),
83           fEventnumber(-1),
84           fTrack(-1),
85           fSCathode(-1),
86           fCathode(-1)
87 {
88 /// Set all internal pointers to 0x0 and indices to -1.
89
90         Reset();
91 }
92
93 AliMUONDataInterface::~AliMUONDataInterface()
94 {
95 /// Delete the runloader if we created it.
96 /// If the runloader is not to be deleted then call Reset just before 
97 /// the destructor is called.
98
99         if (fRunloader != NULL && fCreatedRunLoader)
100                 delete fRunloader;
101 }
102
103 void AliMUONDataInterface::Reset()
104 {
105 /// Sets all internal pointers to NULL and indices to -1.
106 /// Note: No resources are released!
107 /// Specificaly AliRunLoader is not deleted.
108
109         fCreatedRunLoader = kFALSE;
110         fRunloader = NULL;
111         fMuonloader = NULL;
112         fEventnumber = -1;
113         fTrack = -1;
114         fSCathode = -1;
115         fCathode = -1;
116         fHitAddressSet = kFALSE;
117         fSDigitAddressSet = kFALSE;
118         fDigitAddressSet = kFALSE;
119         fClusterAddressSet = kFALSE;
120         fTriggerAddressSet = kFALSE;
121         fRecTracksAddressSet = kFALSE;
122 }
123
124
125 Bool_t AliMUONDataInterface::UseCurrentRunLoader()
126 {
127 /// Tries to fetch the current runloader with AliRunLoader::GetRunLoader. If nothing is
128 /// currently loaded then kFALSE is returned and AliMUONDataInterface is reset.
129
130         Reset();
131         fRunloader = AliRunLoader::GetRunLoader();
132         if (fRunloader == NULL) return kFALSE;
133         // Fetch the current file name, folder name and event number.
134         fFilename = fRunloader->GetFileName();
135         fFoldername = fRunloader->GetEventFolder()->GetName();
136         fEventnumber = fRunloader->GetEventNumber();
137
138         if ( ! FetchMuonLoader(fFilename.Data(), fFoldername.Data()) )
139         {
140                 Reset();
141                 return kFALSE;
142         }               
143
144         return kTRUE;
145 }
146
147
148 Bool_t AliMUONDataInterface::FetchMuonLoader(TString filename, TString foldername)
149 {
150 /// Fetches the muon loader for the given filename/foldername
151
152         fMuonloader = fRunloader->GetLoader("MUONLoader");
153         if (fMuonloader == NULL)
154         {
155                 AliError(Form("Could not find the MUON loader in file: %s and folder: %s", 
156                         (const char*)filename, (const char*)foldername));
157                 return kFALSE;
158         }
159         
160         // Need to connect the muon loader to the AliMUONData object,
161         // else class to fData will return NULL.
162         fData.SetLoader(fMuonloader);
163         return kTRUE;
164 }
165
166
167 Bool_t AliMUONDataInterface::LoadLoaders(TString filename, TString foldername)
168 {
169 /// Load the run and muon loaders from the specified file and folder.
170 /// kTRUE is returned on success and kFALSE on failure.
171
172         fRunloader = AliRunLoader::Open(filename, foldername, "READ");
173         if (fRunloader == NULL)
174         {
175                 AliError(Form("Could not find or load the run loader for the file: %s and folder: %s", 
176                         (const char*)filename, (const char*)foldername));
177                 return kFALSE;
178         }
179         fCreatedRunLoader = kTRUE;
180         if ( ! FetchMuonLoader(filename, foldername) )
181         {
182                 fRunloader = NULL;
183                 return kFALSE;
184         }
185         
186         fFilename = filename;
187         fFoldername = foldername;
188         fEventnumber = -1;  // Reset the event number to force the event to be loaded.
189         return kTRUE;
190 }
191
192
193 Bool_t AliMUONDataInterface::FetchLoaders(TString filename, TString foldername)
194 {
195 /// Fetch the run loader and muon loader objects from memory if they already exist,
196 /// or from memory if they do not. 
197 /// If the currently loaded run loader (if any) is not refering to the file and folder
198 /// we are interested in then it is deleted and reopened with the required file and
199 /// folder names.
200
201         if (fRunloader == NULL)
202         {
203                 fRunloader = AliRunLoader::GetRunLoader();
204                 if (fRunloader == NULL)
205                         return LoadLoaders(filename, foldername);
206                 else
207                 {
208                         if (fMuonloader == NULL)
209                         {
210                                 if ( ! FetchMuonLoader(filename, foldername) )
211                                 {
212                                         fRunloader = NULL;
213                                         return kFALSE;
214                                 }
215                         }
216                 }
217                 
218                 // Fetch the current file and folder names.
219                 fFilename = fRunloader->GetFileName();
220                 fFoldername = fRunloader->GetEventFolder()->GetName();
221         }
222
223         // If filename or foldername are not the same as the ones currently selected then
224         // reopen the file.
225         if ( filename.CompareTo(fFilename) != 0 || foldername.CompareTo(fFoldername) != 0 )
226         {
227                 delete fRunloader;
228                 return LoadLoaders(filename, foldername);
229         }
230         return kTRUE;
231 }
232
233
234 Bool_t AliMUONDataInterface::FetchEvent(Int_t event)
235 {
236 /// Fetch the specified event from the runloader and reset all the track, cathode
237 /// and address flags to force them to be reloaded.
238 /// If a negative event number is specified then the current runloader event
239 /// number is used.
240
241         if (fEventnumber < 0)
242         {
243                 fEventnumber = fRunloader->GetEventNumber();
244                 fTrack = -1;
245                 fSCathode = -1;
246                 fCathode = -1;
247                 fHitAddressSet = kFALSE;
248                 fSDigitAddressSet = kFALSE;
249                 fDigitAddressSet = kFALSE;
250                 fClusterAddressSet = kFALSE;
251                 fTriggerAddressSet = kFALSE;
252                 fRecTracksAddressSet = kFALSE;
253         }
254         if ( event != fEventnumber )
255         {
256                 if ( fRunloader->GetEvent(event) < 0 ) return kFALSE;
257                 fEventnumber = event;
258                 fTrack = -1;
259                 fSCathode = -1;
260                 fCathode = -1;
261                 fHitAddressSet = kFALSE;
262                 fSDigitAddressSet = kFALSE;
263                 fDigitAddressSet = kFALSE;
264                 fClusterAddressSet = kFALSE;
265                 fTriggerAddressSet = kFALSE;
266                 fRecTracksAddressSet = kFALSE;
267         }
268         return kTRUE;
269 }
270
271
272 Bool_t AliMUONDataInterface::FetchTreeK()
273 {
274 /// Fetch the Kine tree from the current run loader.
275
276         if (fRunloader->TreeK() == NULL)
277         {
278                 fRunloader->LoadKinematics("READ");
279                 if (fRunloader->TreeK() == NULL)
280                 {
281                         AliError("Could not load TreeK.");
282                         return kFALSE;
283                 }
284         }
285         return kTRUE;
286 }
287
288
289 Bool_t AliMUONDataInterface::FetchTreeH()
290 {
291 /// Fetch the Hits tree from the current muon loader.
292 /// Set all the required addresses etc...
293
294         if (fMuonloader->TreeH() == NULL)
295         {
296                 fMuonloader->LoadHits("READ");
297                 if (fMuonloader->TreeH() == NULL)
298                 {
299                         AliError("Could not load TreeH.");
300                         return kFALSE;
301                 }
302                 fData.SetTreeAddress("H");
303                 fHitAddressSet = kTRUE;
304         }
305         else if ( ! fHitAddressSet )
306         {
307                 fData.SetTreeAddress("H");
308                 fHitAddressSet = kTRUE;
309         }
310         return kTRUE;
311 }
312
313
314 Bool_t AliMUONDataInterface::FetchTreeS()
315 {
316 /// Fetch the S-Digits tree from the current muon loader.
317 /// Set all the required addresses etc...
318
319         if (fMuonloader->TreeS() == NULL)
320         {
321                 fMuonloader->LoadSDigits("READ");
322                 if (fMuonloader->TreeS() == NULL)
323                 {
324                         AliError("Could not load TreeS.");
325                         return kFALSE;
326                 }
327                 fData.SetTreeAddress("S");
328                 fSDigitAddressSet = kTRUE;
329         }
330         else if ( ! fSDigitAddressSet )
331         {
332                 fData.SetTreeAddress("S");
333                 fSDigitAddressSet = kTRUE;
334         }
335         return kTRUE;
336 }
337
338
339 Bool_t AliMUONDataInterface::FetchTreeD()
340 {
341 /// Fetch the digits tree from the current muon loader.
342 /// Set all the required addresses etc...
343
344         if (fMuonloader->TreeD() == NULL)
345         {
346                 fMuonloader->LoadDigits("READ");
347                 if (fMuonloader->TreeD() == NULL)
348                 {
349                         AliError("Could not load TreeD.");
350                         return kFALSE;
351                 }
352                 fData.SetTreeAddress("D");
353                 fDigitAddressSet = kTRUE;
354         }
355         else if ( ! fDigitAddressSet )
356         {
357                 fData.SetTreeAddress("D");
358                 fDigitAddressSet = kTRUE;
359         }
360         return kTRUE;
361 }
362
363
364 Bool_t AliMUONDataInterface::FetchTreeR()
365 {
366 /// Fetch the reconstructed objects tree from the current muon loader.
367 /// Note: The addresses must still be set. 
368   
369   if (fMuonloader->TreeR() == NULL)
370     {
371       fMuonloader->LoadRecPoints("READ");
372       if (fMuonloader->TreeR() == NULL)
373         {
374           AliError("Could not load TreeR.");
375           return kFALSE;
376         }
377       
378       // Need to reset these flags so that the cluster and trigger address
379       // gets reset after this method. 
380       fClusterAddressSet = kFALSE;
381       fTriggerAddressSet = kFALSE;
382     }
383   return kTRUE;
384 }
385
386 Bool_t AliMUONDataInterface::FetchTreeT()
387 {
388 /// fetch the reconstructed tracks tree from the current muon loader
389 /// note : the addresses must still be set.
390   if (fMuonloader->TreeT() == NULL)
391     {
392       fMuonloader->LoadTracks("READ");
393       if (fMuonloader->TreeT() == NULL)
394         {
395           AliError("Could not load TreeT.");
396           return kFALSE;
397         }
398       
399       // Need to reset these flags so that the rec tracks address
400       // gets reset after this method. 
401       fRecTracksAddressSet = kFALSE;
402     }
403   return kTRUE;
404 }
405   
406 Int_t AliMUONDataInterface::NumberOfEvents(TString filename, TString foldername)
407 {
408 /// Returns the number of events in the specified file/folder, and -1 on error.
409
410         if ( ! FetchLoaders(filename, foldername) ) return -1;
411         return fRunloader->GetNumberOfEvents();
412 }
413
414
415 Int_t AliMUONDataInterface::NumberOfParticles(TString filename, TString foldername, Int_t event)
416 {
417 /// Returns the number of events in the specified file/folder, and -1 on error.
418
419         if ( ! FetchLoaders(filename, foldername) ) return -1;
420         if ( ! FetchEvent(event) ) return -1;
421         if ( ! FetchTreeK() ) return -1;
422         return (Int_t) fRunloader->TreeK()->GetEntriesFast();
423 }
424
425
426 TParticle* AliMUONDataInterface::Particle(
427                 TString filename, TString foldername, Int_t event, Int_t particle
428         )
429 {
430 /// Returns the specified particle in the given file, folder and event.
431 /// NULL is returned on error.
432
433         if ( ! FetchLoaders(filename, foldername) ) return NULL;
434         if ( ! FetchEvent(event) ) return NULL;
435         if ( ! FetchTreeK() ) return NULL;
436         
437         TTree* treeK = fRunloader->TreeK();
438         TParticle* p = NULL;
439         treeK->GetBranch("Particles")->SetAddress(&p);
440         treeK->GetEvent(particle);
441         return p;
442 }
443
444
445 Int_t AliMUONDataInterface::NumberOfTracks(TString filename, TString foldername, Int_t event)
446 {
447 /// Returns the number of tracks in the specified file/folder and event.
448 /// -1 is returned on error.
449
450         if ( ! FetchLoaders(filename, foldername) ) return -1;
451         if ( ! FetchEvent(event) ) return -1;
452         if ( ! FetchTreeH() ) return -1;
453         return fData.GetNtracks();
454 }
455
456
457 Int_t AliMUONDataInterface::NumberOfHits(
458                 TString filename, TString foldername, Int_t event, Int_t track
459         )
460 {
461 /// Returns the number of hits in the specified file/folder, event and track.
462 /// -1 is returned on error.
463
464         if ( ! FetchLoaders(filename, foldername) ) return -1;
465         if ( ! FetchEvent(event) ) return -1;
466         if ( ! FetchTreeH() ) return -1;
467
468         if (fTrack < 0 || fTrack != track)
469         {
470                 fData.ResetHits();
471                 fData.GetTrack(track);
472                 fTrack = track;
473         }
474         return fData.Hits()->GetEntriesFast();
475 }
476
477
478 AliMUONHit* AliMUONDataInterface::Hit(
479                 TString filename, TString foldername, Int_t event,
480                 Int_t track, Int_t hit
481         )
482 {
483 /// Returns the specified hit in the given file, folder, event and track.
484 /// NULL is returned on error.
485
486         if ( ! FetchLoaders(filename, foldername) ) return NULL;
487         if ( ! FetchEvent(event) ) return NULL;
488         if ( ! FetchTreeH() ) return NULL;
489
490         if (fTrack < 0 || fTrack != track)
491         {
492                 fData.ResetHits();
493                 fData.GetTrack(track);
494                 fTrack = track;
495         }
496         return static_cast<AliMUONHit*>( fData.Hits()->At(hit) );
497 }
498
499
500 Int_t AliMUONDataInterface::NumberOfSDigits(
501                 TString filename, TString foldername, Int_t event,
502                 Int_t chamber, Int_t cathode
503         )
504 {
505 /// Returns the number of s-digits in the given file, folder, event,
506 /// chamber and cathode. -1 is returned on error.
507
508         Assert( 0 <= chamber && chamber <= 13 );
509         Assert( 0 <= cathode && cathode <= 1 );
510         
511         if ( ! FetchLoaders(filename, foldername) ) return -1;
512         if ( ! FetchEvent(event) ) return -1;
513         if ( ! FetchTreeS() ) return -1;
514
515         if ( fSCathode != cathode )
516         {
517                 fData.ResetSDigits();
518                 fData.GetSDigits();
519                 fSCathode = cathode;
520         }
521         return fData.SDigits(chamber)->GetEntriesFast();
522 }
523
524
525 AliMUONDigit* AliMUONDataInterface::SDigit(
526                 TString filename, TString foldername, Int_t event,
527                 Int_t chamber, Int_t cathode, Int_t sdigit
528         )
529 {
530 /// Returns the specified s-digit in the given file, folder, event,
531 /// chamber and cathode. NULL is returned on error.
532
533         Assert( 0 <= chamber && chamber <= 13 );
534         Assert( 0 <= cathode && cathode <= 1 );
535         
536         if ( ! FetchLoaders(filename, foldername) ) return NULL;
537         if ( ! FetchEvent(event) ) return NULL;
538         if ( ! FetchTreeS() ) return NULL;
539
540         if ( fSCathode != cathode )
541         {
542                 fData.ResetSDigits();
543                 fData.GetSDigits();
544                 fSCathode = cathode;
545         }
546         return static_cast<AliMUONDigit*>( fData.SDigits(chamber)->At(sdigit) );
547 }
548
549
550 Int_t AliMUONDataInterface::NumberOfDigits(
551                 TString filename, TString foldername, Int_t event,
552                 Int_t chamber, Int_t cathode
553         )
554 {
555 /// Returns the number of digits in the given file, folder, event,
556 /// chamber and cathode. -1 is returned on error.
557         Assert( 0 <= chamber && chamber <= 13 );
558         Assert( 0 <= cathode && cathode <= 1 );
559         
560         if ( ! FetchLoaders(filename, foldername) ) return -1;
561         if ( ! FetchEvent(event) ) return -1;
562         if ( ! FetchTreeD() ) return -1;
563
564         if ( fCathode != cathode )
565         {
566                 fData.ResetDigits();
567                 fData.GetDigits();
568                 fCathode = cathode;
569         }
570         return fData.Digits(chamber)->GetEntriesFast();
571 }
572
573
574 AliMUONDigit* AliMUONDataInterface::Digit(
575                 TString filename, TString foldername, Int_t event,
576                 Int_t chamber, Int_t cathode, Int_t digit
577         )
578 {
579 /// Returns the specified digit in the given file, folder, event,
580 /// chamber and cathode. NULL is returned on error.
581
582         Assert( 0 <= chamber && chamber <= 13 );
583         Assert( 0 <= cathode && cathode <= 1 );
584         
585         if ( ! FetchLoaders(filename, foldername) ) return NULL;
586         if ( ! FetchEvent(event) ) return NULL;
587         if ( ! FetchTreeD() ) return NULL;
588
589         if ( fCathode != cathode )
590         {
591                 fData.ResetDigits();
592                 fData.GetDigits();
593                 fCathode = cathode;
594         }
595         return static_cast<AliMUONDigit*>( fData.Digits(chamber)->At(digit) );
596 }
597
598
599 Int_t AliMUONDataInterface::NumberOfRawClusters(
600                 TString filename, TString foldername, Int_t event, Int_t chamber
601         )
602 {
603 /// Returns the number of raw clusters in the specified file, folder, event and chamber.
604 /// -1 is returned or error.
605
606         Assert( 0 <= chamber && chamber <= 13 );
607         if ( ! FetchLoaders(filename, foldername) ) return -1;
608         if ( ! FetchEvent(event) ) return -1;
609         if ( ! FetchTreeR() ) return -1;
610         if ( ! fClusterAddressSet )
611         {
612                 // If the raw cluster address in TreeR is not set yet then set it now.
613                 fData.SetTreeAddress("RC");
614                 fData.ResetRawClusters();
615                 fData.GetRawClusters();
616                 fClusterAddressSet = kTRUE;
617         }
618         return fData.RawClusters(chamber)->GetEntriesFast();
619 }
620
621
622 AliMUONRawCluster* AliMUONDataInterface::RawCluster(
623                 TString filename, TString foldername, Int_t event,
624                 Int_t chamber, Int_t cluster
625         )
626 {
627 /// Fetch the specified raw cluster from the given file, folder, event and chamber number.
628 /// NULL is returned on error.
629
630         Assert( 0 <= chamber && chamber <= 13 );
631         if ( ! FetchLoaders(filename, foldername) ) return NULL;
632         if ( ! FetchEvent(event) ) return NULL;
633         if ( ! FetchTreeR() ) return NULL;
634         if ( ! fClusterAddressSet )
635         {
636                 // If the raw cluster address in TreeR is not set yet then set it now.
637                 fData.SetTreeAddress("RC");
638                 fData.ResetRawClusters();
639                 fData.GetRawClusters();
640                 fClusterAddressSet = kTRUE;
641         }
642         return static_cast<AliMUONRawCluster*>( fData.RawClusters(chamber)->At(cluster) );
643 }
644
645
646 Int_t AliMUONDataInterface::NumberOfLocalTriggers(TString filename, TString foldername, Int_t event)
647 {
648 /// Return the number of local trigger objects in the specified file, folder and
649 /// event number. -1 is returned on error.
650
651         if ( ! FetchLoaders(filename, foldername) ) return -1;
652         if ( ! FetchEvent(event) ) return -1;
653         if ( ! FetchTreeD() ) return -1;
654         if ( ! fTriggerAddressSet )
655         {
656                 // If the local trigger address in TreeR is not set yet then set it now.
657                 fData.SetTreeAddress("GLT");
658                 fData.ResetTrigger();
659                 fData.GetTriggerD();
660                 fTriggerAddressSet = kTRUE;
661         }
662         return fData.LocalTrigger()->GetEntriesFast();
663 }
664
665
666 AliMUONLocalTrigger* AliMUONDataInterface::LocalTrigger(
667                 TString filename, TString foldername, Int_t event, Int_t trigger
668         )
669 {
670 /// Fetch the specified local trigger object from the given file, folder and event number.
671 /// NULL is returned on error.
672
673         if ( ! FetchLoaders(filename, foldername) ) return NULL;
674         if ( ! FetchEvent(event) ) return NULL;
675         if ( ! FetchTreeD() ) return NULL;
676         if ( ! fTriggerAddressSet )
677         {
678                 // If the local trigger address in TreeR is not set yet then set it now.
679                 fData.SetTreeAddress("GLT");
680                 fData.ResetTrigger();
681                 fData.GetTriggerD();
682                 fTriggerAddressSet = kTRUE;
683         }
684         return static_cast<AliMUONLocalTrigger*>( fData.LocalTrigger()->At(trigger) );
685 }
686
687 Bool_t AliMUONDataInterface::SetFile(TString filename, TString foldername)
688 {
689 /// Set the current file and folder from which to fetch data.
690 /// kTRUE is returned if the run and muon loaders were found, else kFALSE. 
691
692         return FetchLoaders(filename, foldername);
693 }
694
695
696 Bool_t AliMUONDataInterface::GetEvent(Int_t event)
697 {
698 /// Select the current event from which to fetch data.
699 /// kTRUE is returned if the event was found, else kFALSE is returned.
700
701         if (fRunloader == NULL)
702         {
703                 AliError("File not set.");
704                 return kFALSE;
705         }
706         else
707                 return FetchEvent(event);
708 }
709
710
711 Int_t AliMUONDataInterface::NumberOfEvents()
712 {
713 /// Get the number of events in the currently selected file.
714 /// -1 is returned on error.
715
716         if (fRunloader == NULL)
717         {
718                 AliError("File not set.");
719                 return -1;
720         }
721         return fRunloader->GetNumberOfEvents();
722 }
723
724
725 Int_t AliMUONDataInterface::NumberOfParticles()
726 {
727 /// Get the number of particles in the current event.
728 /// -1 is returned on error.
729
730         if (fRunloader == NULL)
731         {
732                 AliError("File not set.");
733                 return -1;
734         }
735         if ( ! FetchTreeK() ) return -1;
736         return (Int_t) fRunloader->TreeK()->GetEntriesFast();
737 }
738
739
740 TParticle* AliMUONDataInterface::Particle(Int_t particle)
741 {
742 /// Fetch the specified particle from the current event.
743 /// NULL is returned on error.
744
745         if (fRunloader == NULL)
746         {
747                 AliError("File not set.");
748                 return NULL;
749         }
750         if (fEventnumber < 0)
751         {
752                 AliError("Event not chosen.");
753                 return NULL;
754         }
755         if ( ! FetchTreeK() ) return NULL;
756         TTree* treeK = fRunloader->TreeK();
757         TParticle* p = NULL;
758         treeK->GetBranch("Particles")->SetAddress(&p);
759         treeK->GetEvent(particle);
760         return p;
761 }
762
763
764 Int_t AliMUONDataInterface::NumberOfTracks()
765 {
766 /// Get the number of tracks in the current event.
767 /// -1 is returned on error.
768
769         if (fRunloader == NULL)
770         {
771                 AliError("File not set.");
772                 return -1;
773         }
774         if (fEventnumber < 0)
775         {
776                 AliError( "Event not chosen.");
777                 return -1;
778         }
779         if ( ! FetchTreeH() ) return -1;
780         return fData.GetNtracks();
781 }
782
783
784 Int_t AliMUONDataInterface::NumberOfHits(Int_t track)
785 {
786 /// Get the number of hits for the given track in the current event.
787 /// -1 is returned on error.
788
789         if (fRunloader == NULL)
790         {
791                 AliError("File not set.");
792                 return -1;
793         }
794         if (fEventnumber < 0)
795         {
796                 AliError("Event not chosen.");
797                 return -1;
798         }
799         if ( ! FetchTreeH() ) return -1;
800         if (fTrack < 0 || fTrack != track)
801         {
802                 fData.ResetHits();
803                 fData.GetTrack(track);
804                 fTrack = track;
805         }
806         return fData.Hits()->GetEntriesFast();
807 }
808
809
810 AliMUONHit* AliMUONDataInterface::Hit(Int_t track, Int_t hit)
811 {
812 /// Fetch the specified hit from the current event.
813 /// NULL is returned on error.
814
815         if (fRunloader == NULL)
816         {
817                 AliError("File not set.");
818                 return NULL;
819         }
820         if (fEventnumber < 0)
821         {
822                 AliError("Event not chosen.");
823                 return NULL;
824         }
825         if ( ! FetchTreeH() ) return NULL;
826         if (fTrack < 0 || fTrack != track)
827         {
828                 fData.ResetHits();
829                 fData.GetTrack(track);
830                 fTrack = track;
831         }
832         return static_cast<AliMUONHit*>( fData.Hits()->At(hit) );
833 }
834
835
836 Int_t AliMUONDataInterface::NumberOfSDigits(Int_t chamber, Int_t cathode)
837 {
838 /// Get the number of s-digits on the chamber, cathode in the current event.
839 /// -1 is returned on error.
840
841         Assert( 0 <= chamber && chamber <= 13 );
842         Assert( 0 <= cathode && cathode <= 1 );
843         
844         if (fRunloader == NULL)
845         {
846                 AliError("File not set.");
847                 return -1;
848         }
849         if (fEventnumber < 0)
850         {
851                 AliError("Event not chosen.");
852                 return -1;
853         }
854
855         if ( ! FetchTreeS() ) return -1;
856         if ( fSCathode != cathode )
857         {
858                 fData.ResetSDigits();
859                 fData.GetSDigits();
860                 fSCathode = cathode;
861         }
862         return fData.SDigits(chamber)->GetEntriesFast();
863 }
864
865
866 AliMUONDigit* AliMUONDataInterface::SDigit(Int_t chamber, Int_t cathode, Int_t sdigit)
867 {
868 /// Fetch the specified s-digits on the chamber, cathode from the current event.
869 /// NULL is returned on error.
870
871         Assert( 0 <= chamber && chamber <= 13 );
872         Assert( 0 <= cathode && cathode <= 1 );
873         
874         if (fRunloader == NULL)
875         {
876                 AliError("File not set.");
877                 return NULL;
878         }
879         if (fEventnumber < 0)
880         {
881                 AliError("Event not chosen.");
882                 return NULL;
883         }
884
885         if ( ! FetchTreeS() ) return NULL;
886         if ( fSCathode != cathode )
887         {
888                 fData.ResetSDigits();
889                 fData.GetSDigits();
890                 fSCathode = cathode;
891         }
892         return static_cast<AliMUONDigit*>( fData.SDigits(chamber)->At(sdigit) );
893 }
894
895
896 Int_t AliMUONDataInterface::NumberOfDigits(Int_t chamber, Int_t cathode)
897 {
898 /// Get the number of digits on the chamber, cathode in the current event.
899 /// -1 is returned on error.
900
901         Assert( 0 <= chamber && chamber <= 13 );
902         Assert( 0 <= cathode && cathode <= 1 );
903         
904         if (fRunloader == NULL)
905         {
906                 AliError("File not set.");
907                 return -1;
908         }
909         if (fEventnumber < 0)
910         {
911                 AliError("Event not chosen.");
912                 return -1;
913         }
914         
915         if ( ! FetchTreeD() ) return -1;
916         if ( fCathode != cathode )
917         {
918                 fData.ResetDigits();
919                 fData.GetDigits();
920                 fCathode = cathode;
921         }
922         return fData.Digits(chamber)->GetEntriesFast();
923 }
924
925
926 AliMUONDigit* AliMUONDataInterface::Digit(Int_t chamber, Int_t cathode, Int_t digit)
927 {
928 /// Fetch the specified digits on the chamber, cathode from the current event.
929 /// NULL is returned on error.
930
931         Assert( 0 <= chamber && chamber <= 13 );
932         Assert( 0 <= cathode && cathode <= 1 );
933         
934         if (fRunloader == NULL)
935         {
936                 AliError("File not set.");
937                 return NULL;
938         }
939         if (fEventnumber < 0)
940         {
941                 AliError("Event not chosen.");
942                 return NULL;
943         }
944
945         if ( ! FetchTreeD() ) return NULL;
946         if ( fCathode != cathode )
947         {
948                 fData.ResetDigits();
949                 fData.GetDigits();
950                 fCathode = cathode;
951         }
952         return static_cast<AliMUONDigit*>( fData.Digits(chamber)->At(digit) );
953 }
954
955
956 Int_t AliMUONDataInterface::NumberOfRawClusters(Int_t chamber)
957 {
958 /// Get the number of raw clusters on the given chamber in the current event.
959 /// -1 is returned on error.
960
961         Assert( 0 <= chamber && chamber <= 13 );
962
963         if (fRunloader == NULL)
964         {
965                 AliError("File not set.");
966                 return -1;
967         }
968         if (fEventnumber < 0)
969         {
970                 AliError("Event not chosen.");
971                 return -1;
972         }
973
974         if ( ! FetchTreeR() ) return -1;
975         if ( ! fClusterAddressSet )
976         {
977                 fData.SetTreeAddress("RC");
978                 fData.ResetRawClusters();
979                 fData.GetRawClusters();
980                 fClusterAddressSet = kTRUE;
981         }
982         return fData.RawClusters(chamber)->GetEntriesFast();
983 }
984
985
986 AliMUONRawCluster* AliMUONDataInterface::RawCluster(Int_t chamber, Int_t cluster)
987 {
988 /// Fetch the specified raw cluster on the given chamber from the current event.
989 /// NULL is returned on error.
990
991         Assert( 0 <= chamber && chamber <= 13 );
992
993         if (fRunloader == NULL)
994         {
995                 AliError("File not set.");
996                 return NULL;
997         }
998         if (fEventnumber < 0)
999         {
1000                 AliError("Event not chosen.");
1001                 return NULL;
1002         }
1003
1004         if ( ! FetchTreeR() ) return NULL;
1005         if ( ! fClusterAddressSet )
1006         {
1007                 fData.SetTreeAddress("RC");
1008                 fData.ResetRawClusters();
1009                 fData.GetRawClusters();
1010                 fClusterAddressSet = kTRUE;
1011         }
1012         return static_cast<AliMUONRawCluster*>( fData.RawClusters(chamber)->At(cluster) );
1013 }
1014
1015
1016 Int_t AliMUONDataInterface::NumberOfLocalTriggers()
1017 {
1018 /// Get the number of local trigger objects in the current event.
1019 /// -1 is returned on error.
1020
1021         if (fRunloader == NULL)
1022         {
1023                 AliError("File not set.");
1024                 return -1;
1025         }
1026         if (fEventnumber < 0)
1027         {
1028                 AliError("Event not chosen.");
1029                 return -1;
1030         }
1031
1032         if ( ! FetchTreeD() ) return -1;
1033         if ( ! fTriggerAddressSet )
1034         {
1035                 fData.SetTreeAddress("GLT");
1036                 fData.ResetTrigger();
1037                 fData.GetTriggerD();
1038                 fTriggerAddressSet = kTRUE;
1039         }
1040         return fData.LocalTrigger()->GetEntriesFast();
1041 }
1042
1043
1044 AliMUONLocalTrigger* AliMUONDataInterface::LocalTrigger(Int_t trigger)
1045 {
1046 /// Fetch the specified local trigger object from the current event.
1047 /// NULL is returned on error.
1048
1049         if (fRunloader == NULL)
1050         {
1051                 AliError("File not set.");
1052                 return NULL;
1053         }
1054         if (fEventnumber < 0)
1055         {
1056                 AliError( "Event not chosen.");
1057                 return NULL;
1058         }
1059
1060         if ( ! FetchTreeD() ) return NULL;
1061         if ( ! fTriggerAddressSet )
1062         {
1063                 fData.SetTreeAddress("GLT");
1064                 fData.ResetTrigger();
1065                 fData.GetTriggerD();
1066                 fTriggerAddressSet = kTRUE;
1067         }
1068         return static_cast<AliMUONLocalTrigger*>( fData.LocalTrigger()->At(trigger) );
1069 }
1070
1071 Int_t AliMUONDataInterface::NumberOfGlobalTriggers()
1072 {
1073 /// Get the number of local trigger objects in the current event.
1074 /// -1 is returned on error.
1075   
1076   if (fRunloader == NULL)
1077     {
1078       AliError("File not set.");
1079       return -1;
1080     }
1081   if (fEventnumber < 0)
1082     {
1083       AliError("Event not chosen.");
1084       return -1;
1085     }
1086   
1087   if ( ! FetchTreeD() ) return -1;
1088   if ( ! fTriggerAddressSet )
1089     {
1090       fData.SetTreeAddress("GLT");
1091       fData.ResetTrigger();
1092       fData.GetTriggerD();
1093       fTriggerAddressSet = kTRUE;
1094     }
1095   return fData.GlobalTrigger()->GetEntriesFast();
1096 }
1097
1098 AliMUONGlobalTrigger* AliMUONDataInterface::GlobalTrigger(Int_t trigger)
1099 {
1100 /// Fetch the specified local trigger object from the current event.
1101 /// NULL is returned on error.
1102   
1103   if (fRunloader == NULL)
1104     {
1105       AliError("File not set.");
1106       return NULL;
1107     }
1108   if (fEventnumber < 0)
1109     {
1110       AliError( "Event not chosen.");
1111       return NULL;
1112     }
1113   
1114   if ( ! FetchTreeD() ) return NULL;
1115   if ( ! fTriggerAddressSet )
1116     {
1117       fData.SetTreeAddress("GLT");
1118       fData.ResetTrigger();
1119       fData.GetTriggerD();
1120       fTriggerAddressSet = kTRUE;
1121     }
1122   return static_cast<AliMUONGlobalTrigger*>( fData.GlobalTrigger()->At(trigger) );
1123 }
1124
1125 Int_t AliMUONDataInterface::NumberOfRecTracks()
1126 {
1127 /// Fetch the number of reconstructed tracks from the current event.
1128 /// NULL is returned on error.
1129   
1130   if (fRunloader == NULL)
1131     {
1132       AliError("File not set.");
1133       return -1;
1134     }
1135   if (fEventnumber < 0)
1136     {
1137       AliError( "Event not chosen.");
1138       return -1;
1139     }
1140   
1141   if ( ! FetchTreeT() ) return -1;
1142   if ( ! fRecTracksAddressSet )
1143     {
1144       fData.SetTreeAddress("RT");
1145       fData.ResetRecTracks();
1146       fData.GetRecTracks();
1147       fRecTracksAddressSet = kTRUE;
1148     }
1149   return fData.RecTracks()->GetEntriesFast();
1150 }
1151
1152 AliMUONTrack* AliMUONDataInterface::RecTrack(Int_t rectrack)
1153 {
1154 /// Fetch the specified reconstructed track object from the current event.
1155 /// NULL is returned on error.
1156   
1157   if (fRunloader == NULL)
1158     {
1159       AliError("File not set.");
1160       return NULL;
1161     }
1162   if (fEventnumber < 0)
1163     {
1164       AliError( "Event not chosen.");
1165       return NULL;
1166     }
1167   
1168   if ( ! FetchTreeT() ) return NULL;
1169   if ( ! fRecTracksAddressSet )
1170     {
1171       fData.SetTreeAddress("RT");
1172       fData.ResetRecTracks();
1173       fData.GetRecTracks();
1174       fRecTracksAddressSet = kTRUE;
1175     }
1176   return static_cast<AliMUONTrack*>( fData.RecTracks()->At(rectrack) );
1177   // return (AliMUONTrack*)(fData.RecTracks()->At(rectrack));
1178 }