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