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