]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUONMCDataInterface.cxx
Adding comment lines to class description needed for Root documentation,
[u/mrichter/AliRoot.git] / MUON / AliMUONMCDataInterface.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 /// \class AliMUONMCDataInterface
20 ///
21 /// Easy to use MC data accessor
22 ///
23 /// \author Laurent Aphecetche, Subatech
24 //-----------------------------------------------------------------------------
25
26 #include "AliMUONMCDataInterface.h"
27 #include "AliMUONVDigitStore.h"
28 #include "AliMUONVHitStore.h"
29 #include "AliMUONVStore.h"
30 #include "AliMUONVTriggerStore.h"
31
32 #include "AliLog.h"
33 #include "AliRunLoader.h"
34 #include "AliStack.h"
35
36 #include <Riostream.h>
37 #include <TClonesArray.h>
38 #include <TList.h>
39 #include <TParticle.h>
40
41 /// \cond CLASSIMP
42 ClassImp(AliMUONMCDataInterface)
43 /// \endcond
44
45 Int_t AliMUONMCDataInterface::fgInstanceCounter(0);
46
47 //_____________________________________________________________________________
48 AliMUONMCDataInterface::AliMUONMCDataInterface(const char* filename) :
49 TObject(),
50 fLoader(0x0),
51 fHitStore(0x0),
52 fSDigitStore(0x0),
53 fDigitStore(0x0),
54 fTriggerStore(0x0),
55 fTrackRefs(0x0),
56 fCurrentEvent(-1),
57 fIsValid(kFALSE)
58 {
59   /// ctor
60   
61   ++fgInstanceCounter;
62   
63   Open(filename);
64 }
65
66 //_____________________________________________________________________________
67 AliMUONMCDataInterface::~AliMUONMCDataInterface()
68 {
69   /// dtor
70   if ( fLoader ) 
71   {
72     delete fLoader->GetRunLoader();
73   }
74   --fgInstanceCounter;
75 }
76
77 //_____________________________________________________________________________
78 AliMUONVDigitStore*
79 AliMUONMCDataInterface::DigitStore(Int_t event)
80 {
81   /// Return a pointer to the digitStore for a given event (or 0 if not found)
82   /// Returned pointer should not be deleted
83   
84   if ( LoadEvent(event) ) return 0x0;
85   
86   fLoader->LoadDigits();
87   
88   TTree* treeD = fLoader->TreeD();
89   
90   if (!treeD)
91   {
92     AliError("Could not get treeD");
93     return 0x0;
94   }
95   
96   if (!fDigitStore)
97   {
98     fDigitStore = AliMUONVDigitStore::Create(*treeD);
99   }
100   
101   if ( fDigitStore ) 
102   {
103     fDigitStore->Clear();
104     fDigitStore->Connect(*treeD);
105     treeD->GetEvent(0);
106   }
107   
108   fLoader->UnloadDigits();
109   
110   return fDigitStore;
111 }
112
113 //_____________________________________________________________________________
114 void
115 AliMUONMCDataInterface::DumpDigits(Int_t event, Bool_t sorted)
116 {
117   /// Dump the digits for a given event, sorted if requested.
118   DigitStore(event);
119   
120   if ( fDigitStore ) 
121   {
122     if ( sorted ) 
123     {
124       DumpSorted(*fDigitStore);
125     }
126     else
127     {
128       fDigitStore->Print();
129     }  
130   }
131 }
132
133 //_____________________________________________________________________________
134 void
135 AliMUONMCDataInterface::DumpHits(Int_t event)
136 {
137   /// Dump all the hits for one event
138   
139   Int_t ntracks = NumberOfTracks(event);
140   
141   for ( Int_t i = 0; i < ntracks; ++i ) 
142   {
143     cout << ">> Track " << i << endl;
144     HitStore(event,i);
145     if ( fHitStore )
146     {
147       fHitStore->Print("","full");
148     }
149   }
150 }
151
152 //_____________________________________________________________________________
153 void
154 AliMUONMCDataInterface::DumpKine(Int_t event)
155 {
156   /// Dump all generated particles for one event
157   AliStack* stack = Stack(event);
158   
159   if ( stack ) 
160   {
161     Int_t nparticles = (Int_t) stack->GetNtrack();
162   
163     for (Int_t iparticle=0; iparticle<nparticles; ++iparticle) 
164     {
165       stack->Particle(iparticle)->Print("");  
166     }
167   }
168   else
169   {
170     AliError("Could not get stack");
171   }
172 }
173
174 //_____________________________________________________________________________
175 void
176 AliMUONMCDataInterface::DumpSDigits(Int_t event, Bool_t sorted)
177 {
178   /// Dump the SDigits for a given event, sorted if requested
179   SDigitStore(event);
180   
181   if ( fSDigitStore ) 
182   {
183     if ( sorted ) 
184     {
185       DumpSorted(*fSDigitStore);
186     }
187     else
188     {
189       fSDigitStore->Print();
190     }
191   }
192 }
193
194 //_____________________________________________________________________________
195 void
196 AliMUONMCDataInterface::DumpSorted(const AliMUONVStore& store) const
197 {
198   /// Dump the given store in sorted order
199   
200   TIter next(store.CreateIterator());
201   TObject* object;
202   TList list;
203   list.SetOwner(kFALSE);
204   
205   while ( ( object = next() ) )
206   {
207     list.Add(object);
208   }
209   
210   list.Sort();
211   
212   list.Print();
213 }
214
215 //_____________________________________________________________________________
216 void
217 AliMUONMCDataInterface::DumpTrackRefs(Int_t event)
218 {
219   /// Dump track references for one event
220   Int_t ntrackrefs = NumberOfTrackRefs(event);
221   
222   for ( Int_t i = 0; i < ntrackrefs; ++i ) 
223   {
224     TrackRefs(event,i);
225     if ( fTrackRefs ) 
226     {
227       fTrackRefs->Print("","*");
228     }
229   }
230 }
231
232 //_____________________________________________________________________________
233 void
234 AliMUONMCDataInterface::DumpTrigger(Int_t event)
235 {
236   /// Dump trigger for a given event (trigger is read from TreeD)
237   
238   TriggerStore(event);
239
240   if ( fTriggerStore ) 
241   {
242     fTriggerStore->Print();
243   }
244 }
245
246 //_____________________________________________________________________________
247 AliMUONVHitStore* 
248 AliMUONMCDataInterface::HitStore(Int_t event, Int_t track)
249 {
250   /// Return the hitStore for a given track of one event
251   /// Return 0 if event and/or track not found
252   /// Returned pointer should not be deleted
253   
254   if ( !IsValid() ) return 0x0;
255   
256   if ( LoadEvent(event) ) return 0x0;
257
258   if ( fHitStore) fHitStore->Clear();
259   
260   fLoader->LoadHits();
261   
262   TTree* treeH = fLoader->TreeH();
263   
264   if (!treeH) 
265   {
266     AliError("Could not get treeH");
267     return 0x0;
268   }
269   
270   if ( !fHitStore ) 
271   {
272     fHitStore = AliMUONVHitStore::Create(*treeH);
273     AliDebug(1,"Creating hitStore from treeH");
274   }
275   
276   if ( fHitStore )
277   {
278     fHitStore->Connect(*treeH);
279     if ( treeH->GetEvent(track) == 0 ) 
280     {
281       AliError(Form("Could not read track %d",track));
282       fHitStore->Clear();
283     }
284   }
285   
286   fLoader->UnloadHits();
287
288   return fHitStore;
289 }
290
291 //_____________________________________________________________________________
292 Bool_t
293 AliMUONMCDataInterface::IsValid() const
294 {
295   /// Whether we were initialized properly or not
296   return fIsValid;
297 }
298
299 //_____________________________________________________________________________
300 Int_t
301 AliMUONMCDataInterface::LoadEvent(Int_t event)
302 {
303   /// Load event if different from the current one.
304   if ( event != fCurrentEvent ) 
305   {
306     fCurrentEvent = event;
307     AliDebug(1,Form("Loading event %d using runLoader %p",event,fLoader->GetRunLoader()));
308     if ( event < NumberOfEvents() )
309     {
310       return fLoader->GetRunLoader()->GetEvent(event);
311     }
312     else
313     {
314       return 1;
315     }
316   }
317   return 0;
318 }
319
320
321 //_____________________________________________________________________________
322 Int_t 
323 AliMUONMCDataInterface::NumberOfEvents() const
324 {
325   /// Number of events in the file we're connected to
326   if (!IsValid()) return 0;
327   return fLoader->GetRunLoader()->GetNumberOfEvents();
328 }
329
330 //_____________________________________________________________________________
331 Int_t 
332 AliMUONMCDataInterface::NumberOfTracks(Int_t event)
333 {
334   /// Number of tracks in the event
335   if (!IsValid()) return 0;
336   
337   if ( LoadEvent(event) ) return 0;
338   
339   fLoader->LoadHits();
340   
341   Int_t rv(0);
342   
343   TTree* treeH = fLoader->TreeH();
344   if (treeH)
345   {
346     rv = static_cast<Int_t>(treeH->GetEntries());
347   }
348   else
349   {
350     AliError("Could not get TreeH");
351   }
352
353   fLoader->UnloadHits();
354   
355   return rv;
356 }
357
358 //_____________________________________________________________________________
359 Int_t 
360 AliMUONMCDataInterface::NumberOfTrackRefs(Int_t event)
361 {
362   /// Number of track references in the event
363   if (!IsValid()) return 0;
364   
365   if ( LoadEvent(event) ) return 0;
366
367   fLoader->GetRunLoader()->LoadTrackRefs();
368   
369   Int_t rv(0);
370   
371   TTree* treeTR = fLoader->GetRunLoader()->TreeTR();
372   if (treeTR)
373   {
374     rv = static_cast<Int_t>(treeTR->GetEntries());
375   }
376   else
377   {
378     AliError("Could not get TreeTR");
379   }
380   
381   fLoader->GetRunLoader()->UnloadTrackRefs();
382   
383   return rv;
384 }
385
386 //_____________________________________________________________________________
387 void
388 AliMUONMCDataInterface::Open(const char* filename)
389 {
390   /// Connect to a given galice.root file
391   
392   delete fHitStore; 
393   fHitStore=0x0;
394   delete fSDigitStore;
395   fSDigitStore=0x0;
396   delete fDigitStore;
397   fDigitStore=0x0;
398   delete fTrackRefs;
399   fTrackRefs=0x0;
400   delete fTriggerStore;
401   fTriggerStore=0x0;
402   
403   fCurrentEvent=-1;
404
405   if ( fLoader ) 
406   {
407     delete fLoader->GetRunLoader();
408   }
409   
410   fLoader = 0x0;
411   
412   fIsValid = kTRUE;
413   
414   TString foldername(Form("%s-%d",ClassName(),fgInstanceCounter));
415   
416   while (AliRunLoader::GetRunLoader(foldername)) 
417   {
418     delete AliRunLoader::GetRunLoader(foldername);
419   }
420   
421   AliRunLoader* runLoader = AliRunLoader::Open(filename,foldername);
422   if (!runLoader) 
423   {
424     AliError(Form("Cannot open file %s",filename));    
425     fIsValid = kFALSE;
426   }
427   fLoader = runLoader->GetDetectorLoader("MUON");
428   if (!fLoader) 
429   {
430     AliError("Cannot get AliMUONLoader");
431     fIsValid = kFALSE;
432   }
433   
434   if (!IsValid())
435   {
436     AliError(Form("Could not access %s filename. Object is unuseable",filename));
437   }
438 }
439
440 //_____________________________________________________________________________
441 AliMUONVDigitStore*
442 AliMUONMCDataInterface::SDigitStore(Int_t event)
443 {
444   /// Return the SDigit store for a given event.
445   /// Return 0 if event not found
446   /// Returned pointer should not be deleted
447   
448   if ( LoadEvent(event) ) return 0x0;
449   
450   fLoader->LoadSDigits();
451   
452   TTree* treeS = fLoader->TreeS();
453   
454   if (!treeS)
455   {
456     AliError("Could not get treeS");
457     return 0x0;
458   }
459   
460   if (!fSDigitStore)
461   {
462     fSDigitStore = AliMUONVDigitStore::Create(*treeS);
463   }
464   
465   if ( fSDigitStore ) 
466   {
467     fSDigitStore->Clear();
468     fSDigitStore->Connect(*treeS);
469     treeS->GetEvent(0);
470   }
471   
472   fLoader->UnloadSDigits();
473   
474   return fSDigitStore;
475 }
476
477 //_____________________________________________________________________________
478 AliStack*
479 AliMUONMCDataInterface::Stack(Int_t event)
480 {
481   /// Get the Stack (list of generated particles) for one event
482   /// Returned pointer should not be deleted
483   
484   if (!IsValid()) return 0x0;
485
486   if ( LoadEvent(event) ) return 0;
487   
488   fLoader->GetRunLoader()->LoadKinematics();
489   
490   return fLoader->GetRunLoader()->Stack();
491 }
492
493 //_____________________________________________________________________________
494 TClonesArray*
495 AliMUONMCDataInterface::TrackRefs(Int_t event, Int_t track)
496 {
497   /// Get the track references for a given (generated) track of one event
498   /// Returned pointer should not be deleted
499   
500   if ( !IsValid() ) return 0x0;
501
502   if ( LoadEvent(event) ) return 0;
503   
504   fLoader->GetRunLoader()->LoadTrackRefs();
505   
506   TTree* treeTR = fLoader->GetRunLoader()->TreeTR();
507   
508   if ( fTrackRefs ) fTrackRefs->Clear("C");
509   
510   if (treeTR)
511   {
512     if ( treeTR->GetEvent(track) > 0 ) 
513     {
514       TBranch* branch = treeTR->GetBranch("MUON");
515       branch->SetAddress(&fTrackRefs);
516       branch->GetEvent(track);
517     }
518   }
519   else
520   {
521     AliError("Could not get TreeTR");
522   }
523   
524   fLoader->GetRunLoader()->UnloadTrackRefs();
525   
526   return fTrackRefs;
527 }
528
529 //_____________________________________________________________________________
530 AliMUONVTriggerStore*
531 AliMUONMCDataInterface::TriggerStore(Int_t event)
532 {
533   /// Return the triggerStore for a given event.
534   /// Return 0x0 if event not found.
535   /// Returned pointer should not be deleted.
536   
537   if ( LoadEvent(event) ) return 0x0;
538   
539   fLoader->LoadDigits();
540   
541   TTree* treeD = fLoader->TreeD();
542   
543   if ( !treeD ) 
544   {
545     AliError("Could not get treeD");
546     return 0x0;
547   }
548   
549   if (!fTriggerStore)
550   {
551     fTriggerStore = AliMUONVTriggerStore::Create(*treeD);
552   }
553   
554   if ( fTriggerStore ) 
555   {
556     fTriggerStore->Clear();
557     fTriggerStore->Connect(*treeD);
558     treeD->GetEvent(0);
559   }
560   
561   fLoader->UnloadDigits();
562   
563   return fTriggerStore;
564 }