1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 #include "AliMUONDataInterface.h"
19 #include "AliMUONGeometryTransformer.h"
20 #include "AliMUONVDigit.h"
21 #include "AliMUONVCluster.h"
22 #include "AliMUONLocalTrigger.h"
23 #include "AliMUONRegionalTrigger.h"
24 #include "AliMUONGlobalTrigger.h"
25 #include "AliMUONTriggerCircuit.h"
26 #include "AliMUONVClusterStore.h"
27 #include "AliMUONVDigitStore.h"
28 #include "AliMUONVTriggerStore.h"
31 #include "AliMpEncodePair.h"
32 #include "AliMpDEManager.h"
33 #include "AliMpConstants.h"
36 #include "AliLoader.h"
37 #include "AliRunLoader.h"
38 #include "AliHeader.h"
39 #include "AliCDBManager.h"
42 #include <Riostream.h>
47 #include <TIterator.h>
51 //-----------------------------------------------------------------------------
52 /// \class AliMUONDataInterface
54 /// An easy to use interface to the MUON data data stored in
55 /// TreeS, TreeD and TreeR.
57 /// For MC related information (i.e. TreeH, TreeK, TreeTR), see
58 /// AliMUONMCDataInterface.
61 /// This interface in not necessarily the fastest way to fetch the data but
62 /// it is the easiest.
64 /// \author Laurent Aphecetche, Subatech & Artur Szostak <artursz@iafrica.com> (University of Cape Town)
65 //-----------------------------------------------------------------------------
68 ClassImp(AliMUONDataInterface)
72 Int_t AliMUONDataInterface::fgInstanceCounter(0);
74 //______________________________________________________________________________
75 AliMUONDataInterface::AliMUONDataInterface(const char* filename)
84 fCurrentIteratorType(kNoIterator),
91 /// @param filename should be the full path to a valid galice.root file
95 if ( AliCDBManager::Instance() != NULL &&
96 AliCDBManager::Instance()->GetDefaultStorage() == NULL ) {
97 AliFatal("CDB default storage not defined.");
103 if ( ! AliMpCDB::LoadDDLStore() ) {
104 AliFatal("Could not access mapping from OCDB !");
108 //______________________________________________________________________________
109 AliMUONDataInterface::~AliMUONDataInterface()
116 //______________________________________________________________________________
118 AliMUONDataInterface::DigitStore(Int_t event)
120 /// Return digitStore for a given event.
121 /// Return 0x0 if event not found.
122 /// Returned pointer should not be deleted
124 /// \note If a previous store has been retrieved by one of the methods of
125 /// this class, but for a different event number, then those stores will
126 /// be deleted and no longer valid.
127 /// If you require access to the data for the earlier retrieved store,
128 /// but for different events, then you should deep copy / clone the object.
130 if (not IsValid()) return 0x0;
132 if (event == fCurrentEvent)
134 if (fDigitStore != 0x0)
140 if ( not LoadEvent(event) ) return 0x0;
143 fLoader->LoadDigits();
145 TTree* treeD = fLoader->TreeD();
148 AliError("Could not get treeD");
152 fDigitStore = AliMUONVDigitStore::Create(*treeD);
153 if ( fDigitStore != 0x0 )
155 fDigitStore->Clear();
156 fDigitStore->Connect(*treeD);
160 fLoader->UnloadDigits();
165 //______________________________________________________________________________
166 AliMUONVClusterStore*
167 AliMUONDataInterface::ClusterStore(Int_t event)
169 /// Return clusterStore for a given event.
170 /// Return 0x0 if event not found.
171 /// Returned pointer should not be deleted
173 /// \note If a previous store has been retrieved by one of the methods of
174 /// this class, but for a different event number, then those stores will
175 /// be deleted and no longer valid.
176 /// If you require access to the data for the earlier retrieved store,
177 /// but for different events, then you should deep copy / clone the object.
179 if (not IsValid()) return 0x0;
181 if (event == fCurrentEvent)
183 if (fClusterStore != 0x0)
184 return fClusterStore;
189 if ( not LoadEvent(event) ) return 0x0;
192 fLoader->LoadRecPoints();
194 TTree* treeR = fLoader->TreeR();
197 AliError("Could not get treeR");
201 fClusterStore = AliMUONVClusterStore::Create(*treeR);
202 if ( fClusterStore != 0x0 )
204 fClusterStore->Clear();
205 fClusterStore->Connect(*treeR);
209 fLoader->UnloadRecPoints();
211 return fClusterStore;
214 //_____________________________________________________________________________
215 AliMUONVTriggerStore*
216 AliMUONDataInterface::TriggerStore(Int_t event, const char* treeLetter)
218 /// Return the triggerStore for a given event.
219 /// Return 0x0 if event not found.
220 /// Returned pointer should not be deleted
221 /// treeLetter can be R or D to tell from which tree to read the information
223 /// \note If a previous store has been retrieved by one of the methods of
224 /// this class, but for a different event number, then those stores will
225 /// be deleted and no longer valid.
226 /// If you require access to the data for the earlier retrieved store,
227 /// but for different events, then you should deep copy / clone the object.
229 if (not IsValid()) return 0x0;
231 if (event == fCurrentEvent)
233 if (fTreeLetter == treeLetter)
235 if (fTriggerStore != 0x0)
236 return fTriggerStore;
240 // Reset only the fTriggerStore since the others might still be valid
241 // for the same event.
242 if (fTriggerStore != 0x0)
244 delete fTriggerStore;
251 // Event has changed so reset all the stores.
253 if ( not LoadEvent(event) ) return 0x0;
258 TString stree(treeLetter);
263 fLoader->LoadDigits();
264 tree = fLoader->TreeD();
266 else if ( stree == "R" )
268 fLoader->LoadRecPoints();
269 tree = fLoader->TreeR();
274 AliError(Form("Could not get tree%s",treeLetter));
278 fTriggerStore = AliMUONVTriggerStore::Create(*tree);
279 if ( fTriggerStore != 0x0 )
281 fTriggerStore->Clear();
282 fTriggerStore->Connect(*tree);
288 fLoader->UnloadDigits();
290 else if ( stree == "R" )
292 fLoader->UnloadRecPoints();
296 return fTriggerStore;
299 //______________________________________________________________________________
301 AliMUONDataInterface::DumpDigits(Int_t event, Bool_t sorted)
303 /// Dump the digits for a given event, sorted if so required
305 if ( fDigitStore != 0x0 )
309 DumpSorted(*fDigitStore);
313 fDigitStore->Print();
318 //______________________________________________________________________________
320 AliMUONDataInterface::DumpRecPoints(Int_t event, Bool_t sorted)
322 /// Dump the recpoints for a given event, sorted if so required
324 if ( fClusterStore != 0x0 )
328 DumpSorted(*fClusterStore);
332 fClusterStore->Print();
337 //_____________________________________________________________________________
339 AliMUONDataInterface::DumpSorted(const AliMUONVStore& store) const
341 /// Dump the given store, in sorted order
343 TIter next(store.CreateIterator());
346 list.SetOwner(kFALSE);
348 while ( ( object = next() ) )
358 //_____________________________________________________________________________
360 AliMUONDataInterface::DumpTrigger(Int_t event, const char* treeLetter)
362 /// Dump trigger for a given event from a given tree (if event>=0)
363 /// or loop over all events and build a trigger ntuple if event<0
364 /// treeLetter can be R or D to tell from which tree to read the information
368 NtupleTrigger(treeLetter);
372 TriggerStore(event,treeLetter);
374 if ( fTriggerStore != 0x0 )
376 fTriggerStore->Print();
381 //_____________________________________________________________________________
383 AliMUONDataInterface::NtupleTrigger(const char* treeLetter)
385 //// Loop over events to build trigger ntuples
388 TString sTreeLetter(treeLetter);
389 sTreeLetter.ToUpper();
391 if ( sTreeLetter != "R" && sTreeLetter != "D" )
393 AliError(Form("Cannot handle tree%s. Use D or R",treeLetter));
398 TNtuple tupleGlo("TgtupleGlo","Global Trigger Ntuple",
399 "ev:slpt:shpt:uplpt:uphpt:lplpt:lplpt");
400 TNtuple tupleLoc("TgtupleLoc","Local Trigger Ntuple",
401 "ev:LoCircuit:LoStripX:LoDev:StripY:LoLpt:LoHpt:y11:y21:x11");
403 // initialize counters
411 AliMUONGeometryTransformer transformer;
412 transformer.LoadGeometryData(Form("%s/geometry.root",
413 gSystem->DirName(fLoader->GetRunLoader()->GetFileName())));
414 AliMUONTriggerCircuit triggerCircuit(&transformer);
416 // select output file name from selected Tree
417 Char_t fileNameOut[30];
418 if (sTreeLetter == "D")
420 AliInfo(Form("reading from Digits\n"));
421 sprintf(fileNameOut,"TriggerCheckFromDigits.root");
423 else if (sTreeLetter == "R")
425 AliInfo(Form("reading from RecPoints\n"));
426 sprintf(fileNameOut,"TriggerCheckFromRP.root");
430 Int_t nevents = NumberOfEvents();
432 for (Int_t ievent=0; ievent<nevents; ++ievent)
434 if (ievent%100==0) AliInfo(Form("Processing event %d\n",ievent));
436 AliMUONVTriggerStore* triggerStore = TriggerStore(ievent,treeLetter);
440 AliError(Form("Could not read %s from tree%s","Trigger",treeLetter));
444 // get global trigger info
445 AliMUONGlobalTrigger* gloTrg = triggerStore->Global();
446 sLowpt+=gloTrg->SingleLpt();
447 sHighpt+=gloTrg->SingleHpt();
448 uSLowpt+=gloTrg->PairUnlikeLpt();
449 uSHighpt+=gloTrg->PairUnlikeHpt();
450 lSLowpt+=gloTrg->PairLikeLpt();
451 lSHighpt+=gloTrg->PairLikeHpt();
453 // loop on local triggers
454 TIter next(triggerStore->CreateIterator());
455 AliMUONLocalTrigger* locTrg(0x0);
456 while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
458 Bool_t xTrig=locTrg->IsTrigX();
459 Bool_t yTrig=locTrg->IsTrigY();
462 { // fill ntuple if trigger in X and Y
463 tupleLoc.Fill(ievent,locTrg->LoCircuit(),
469 triggerCircuit.GetY11Pos(locTrg->LoCircuit(),locTrg->LoStripX()),
470 triggerCircuit.GetY21Pos(locTrg->LoCircuit(),locTrg->LoStripX()+locTrg->LoDev()+1),
471 triggerCircuit.GetX11Pos(locTrg->LoCircuit(),locTrg->LoStripY()));
473 tupleGlo.Fill(ievent,gloTrg->SingleLpt(),gloTrg->SingleHpt(),
474 gloTrg->PairUnlikeLpt(),gloTrg->PairUnlikeHpt(),
475 gloTrg->PairLikeLpt(),gloTrg->PairLikeHpt());
476 } // end of loop on local triggers
477 } // end of loop on events
479 // print info and store ntuples
481 printf("=============================================\n");
482 printf("================ SUMMARY ==================\n");
484 printf("Total number of events processed %d \n",nevents);
486 printf(" Global Trigger output Low pt High pt\n");
487 printf(" number of Single :\t");
488 printf("%i\t%i\t",sLowpt,sHighpt);
490 printf(" number of UnlikeSign pair :\t");
491 printf("%i\t%i\t",uSLowpt,uSHighpt);
493 printf(" number of LikeSign pair :\t");
494 printf("%i\t%i\t",lSLowpt,lSHighpt);
496 printf("=============================================\n");
499 TFile myFile(fileNameOut, "RECREATE");
505 //_____________________________________________________________________________
507 AliMUONDataInterface::LoadEvent(Int_t event)
509 /// Load event if different from the current one.
510 /// Returns kFALSE on error and kTRUE if the event was loaded.
514 AliDebug(1,Form("Loading event %d using runLoader %p",event,fLoader->GetRunLoader()));
515 if (fLoader->GetRunLoader()->GetEvent(event) == 0)
517 fCurrentEvent = event;
524 //______________________________________________________________________________
526 AliMUONDataInterface::NumberOfEvents() const
528 /// Number of events in the current galice.root file we're attached to
529 if (not IsValid()) return -1;
530 return fLoader->GetRunLoader()->GetNumberOfEvents();
533 //_____________________________________________________________________________
535 AliMUONDataInterface::Open(const char* filename)
537 /// Connect to a given galice.root file
543 if ( fLoader != 0x0 )
545 delete fLoader->GetRunLoader();
552 TString foldername(Form("%s-%d",ClassName(),fgInstanceCounter));
554 while (AliRunLoader::GetRunLoader(foldername) != 0x0)
556 delete AliRunLoader::GetRunLoader(foldername);
559 AliRunLoader* runLoader = AliRunLoader::Open(filename,foldername);
560 if (runLoader == 0x0)
562 AliError(Form("Cannot open file %s",filename));
567 runLoader->LoadHeader();
568 if ( ! runLoader->GetHeader() ) {
569 AliError("Cannot load header.");
573 Int_t runNumber = runLoader->GetHeader()->GetRun();
574 AliCDBManager::Instance()->SetRun(runNumber>=0 ? runNumber : 1);
576 runLoader->UnloadHeader();
578 fLoader = runLoader->GetDetectorLoader("MUON");
581 AliError("Cannot get AliMUONLoader");
587 AliError(Form("Could not access %s filename. Object is unuseable",filename));
591 //_____________________________________________________________________________
592 Bool_t AliMUONDataInterface::GetEvent(Int_t event)
594 /// Loads all reconstructed data for the given event.
596 if (DigitStore(event) == 0x0) return kFALSE;
597 if (ClusterStore(event) == 0x0) return kFALSE;
598 if (TriggerStore(event) == 0x0) return kFALSE;
602 //_____________________________________________________________________________
603 Int_t AliMUONDataInterface::NumberOfDigits(Int_t detElemId)
605 /// Returns the number of digits to be found on a given detector element.
606 /// @param detElemId The detector element ID number to search on.
608 TIterator* iter = GetIterator(kDigitIteratorByDetectorElement, detElemId);
609 return CountObjects(iter);
612 //_____________________________________________________________________________
613 AliMUONVDigit* AliMUONDataInterface::Digit(Int_t detElemId, Int_t index)
615 /// Returns the a pointer to the index'th digit on the specified detector element.
616 /// @param detElemId The detector element ID number to search on.
617 /// @param index The index number of the digit to fetch in the range [0 .. N-1],
618 /// where N = NumberOfDigits(detElemId)
620 TIterator* iter = GetIterator(kDigitIteratorByDetectorElement, detElemId);
621 return static_cast<AliMUONVDigit*>( FetchObject(iter, index) );
624 //_____________________________________________________________________________
625 Int_t AliMUONDataInterface::NumberOfDigits(Int_t chamber, Int_t cathode)
627 /// Returns the number of digits to be found on a specific chamber and cathode.
628 /// @param chamber The chamber number in the range [0 .. 13].
629 /// @param cathode The cathode in the range [0 .. 1], where 0 is the bending and
630 /// 1 is the non-bending plane.
632 TIterator* iter = GetIterator(kDigitIteratorByChamberAndCathode, chamber, cathode);
633 return CountObjects(iter);
636 //_____________________________________________________________________________
637 AliMUONVDigit* AliMUONDataInterface::Digit(Int_t chamber, Int_t cathode, Int_t index)
639 /// Returns the a pointer to the index'th digit on the specified chamber and cathode.
640 /// @param chamber The chamber number in the range [0 .. 13].
641 /// @param cathode The cathode in the range [0 .. 1], where 0 is the bending and
642 /// 1 is the non-bending plane.
643 /// @param index The index number of the digit to fetch in the range [0 .. N-1],
644 /// where N = NumberOfDigits(chamber, cathode)
646 TIterator* iter = GetIterator(kDigitIteratorByChamberAndCathode, chamber, cathode);
647 return static_cast<AliMUONVDigit*>( FetchObject(iter, index) );
650 //_____________________________________________________________________________
651 Int_t AliMUONDataInterface::NumberOfRawClusters(Int_t chamber)
653 /// Returns the number of reconstructed raw clusters on the specified chamber.
654 /// @param chamber The chamber number in the range [0 .. 13].
656 TIterator* iter = GetIterator(kRawClusterIterator, chamber);
657 return CountObjects(iter);
660 //_____________________________________________________________________________
661 AliMUONVCluster* AliMUONDataInterface::RawCluster(Int_t chamber, Int_t index)
663 /// Returns a pointer to the index'th raw cluster on the specified chamber.
664 /// @param chamber The chamber number in the range [0 .. 13].
665 /// @param index The index number of the raw cluster to fetch in the range [0 .. N-1],
666 /// where N = NumberOfRawClusters(chamber)
668 TIterator* iter = GetIterator(kRawClusterIterator, chamber);
669 return static_cast<AliMUONVCluster*>( FetchObject(iter, index) );
672 //_____________________________________________________________________________
673 Int_t AliMUONDataInterface::NumberOfLocalTriggers()
675 /// Returns the number of reconstructed local trigger objects.
677 TIterator* iter = GetIterator(kLocalTriggerIterator);
678 return CountObjects(iter);
681 //_____________________________________________________________________________
682 AliMUONLocalTrigger* AliMUONDataInterface::LocalTrigger(Int_t index)
684 /// Returns a pointer to the index'th local trigger object.
685 /// @param index The index number of the local trigger object to fetch in the range [0 .. N-1],
686 /// where N = NumberOfLocalTriggers()
688 TIterator* iter = GetIterator(kLocalTriggerIterator);
689 return static_cast<AliMUONLocalTrigger*>( FetchObject(iter, index) );
692 //_____________________________________________________________________________
693 Int_t AliMUONDataInterface::NumberOfRegionalTriggers()
695 /// Returns the number of regional trigger objects reconstructed.
697 TIterator* iter = GetIterator(kRegionalTriggerIterator);
698 return CountObjects(iter);
701 //_____________________________________________________________________________
702 AliMUONRegionalTrigger* AliMUONDataInterface::RegionalTrigger(Int_t index)
704 /// Returns a pointer to the index'th regional trigger object.
705 /// @param index The index number of the regional trigger object to fetch in the range [0 .. N-1],
706 /// where N = NumberOfRegionalTriggers()
708 TIterator* iter = GetIterator(kRegionalTriggerIterator);
709 return static_cast<AliMUONRegionalTrigger*>( FetchObject(iter, index) );
712 //_____________________________________________________________________________
713 AliMUONGlobalTrigger* AliMUONDataInterface::GlobalTrigger()
715 /// Returns a pointer to the reconstructed global trigger object for the event.
717 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
718 if (store == 0x0) return 0x0;
719 return store->Global();
722 //_____________________________________________________________________________
723 void AliMUONDataInterface::ResetStores()
725 /// Deletes all the store objects that have been created and resets the pointers to 0x0.
726 /// The temporary iterator object is automatically reset. See ResetIterator for more details.
729 if (fDigitStore != 0x0)
734 if (fTriggerStore != 0x0)
736 delete fTriggerStore;
739 if (fClusterStore != 0x0)
741 delete fClusterStore;
746 //_____________________________________________________________________________
747 TIterator* AliMUONDataInterface::GetIterator(IteratorType type, Int_t x, Int_t y)
749 /// Creates an appropriate iterator object and returns it.
750 /// If the iterator has already been created then that one is returned otherwise
751 /// a new object is created.
752 /// Depending on the value of 'type' the semantics of parameters x and y can change.
753 /// @param type The type of iterator to create.
754 /// @param x This is the detector element ID if type == kDigitIteratorByDetectorElement
755 /// If type equals kDigitIteratorByChamberAndCathode or kRawClusterIterator
756 /// then this is the chamber number. In all other cases this parameter is
758 /// @param y If type == kDigitIteratorByChamberAndCathode then this parameter is the
759 /// cathode number. In all other cases this parameter is
762 if (type == fCurrentIteratorType and fDataX == x and fDataY == y)
765 if (fCurrentEvent == -1)
767 AliError("No event was selected. Try first using GetEvent().");
775 case kDigitIteratorByDetectorElement:
778 AliMUONVDigitStore* store = DigitStore(fCurrentEvent);
779 if (store == 0x0) return 0x0;
780 fIterator = store->CreateIterator(detElem, detElem, 2);
781 if (fIterator == 0x0) return 0x0;
782 fCurrentIteratorType = kDigitIteratorByDetectorElement;
787 case kDigitIteratorByChamberAndCathode:
791 if (chamber < 0 or AliMpConstants::NofChambers() <= chamber)
794 "Must have give a chamber value in the range [0..%d], but got a value of: %d",
795 AliMpConstants::NofChambers() - 1,
800 if (cathode < 0 or 1 < cathode)
802 AliError(Form("Must have give a cathode value in the range [0..1], but got a value of: %d", cathode));
806 AliMUONVDigitStore* store = DigitStore(fCurrentEvent);
807 if (store == 0x0) return 0x0;
808 MpPair_t pair = AliMpDEManager::GetDetElemIdRange(chamber);
809 fIterator = store->CreateIterator(AliMp::PairFirst(pair), AliMp::PairSecond(pair), cathode);
810 if (fIterator == 0x0) return 0x0;
811 fCurrentIteratorType = kDigitIteratorByChamberAndCathode;
817 case kRawClusterIterator:
820 AliMUONVClusterStore* store = ClusterStore(fCurrentEvent);
821 if (store == 0x0) return 0x0;
822 fIterator = store->CreateChamberIterator(chamber, chamber);
823 if (fIterator == 0x0) return 0x0;
824 fCurrentIteratorType = kRawClusterIterator;
829 case kLocalTriggerIterator:
831 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
832 if (store == 0x0) return 0x0;
833 fIterator = store->CreateLocalIterator();
834 if (fIterator == 0x0) return 0x0;
835 fCurrentIteratorType = kLocalTriggerIterator;
839 case kRegionalTriggerIterator:
841 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
842 if (store == 0x0) return 0x0;
843 fIterator = store->CreateRegionalIterator();
844 if (fIterator == 0x0) return 0x0;
845 fCurrentIteratorType = kRegionalTriggerIterator;
854 //_____________________________________________________________________________
855 void AliMUONDataInterface::ResetIterator()
857 /// The temporary iterator object is deleted if it exists and the pointer reset to 0x0.
858 /// The iterator type and temporary data indicating the state of the iterator are
861 if (fIterator != 0x0) delete fIterator;
862 fCurrentIteratorType = kNoIterator;
863 fCurrentIndex = fDataX = fDataY = -1;
867 //_____________________________________________________________________________
868 Int_t AliMUONDataInterface::CountObjects(TIterator* iter)
870 /// Counts the number of objects in the iterator and resets it.
871 /// @return The number of objects in 'iter'.
873 if (iter == 0x0) return -1;
876 while ( iter->Next() != 0x0 ) count++;
882 //_____________________________________________________________________________
883 TObject* AliMUONDataInterface::FetchObject(TIterator* iter, Int_t index)
885 /// Fetches the index'th object from the iterator counting the first object
886 /// returned by iterator after it is reset as index == 0. The next object
887 /// has index == 1 and so on where the last object returned by the iterator
888 /// has index == N-1 where N = CountObjects(iter)
889 /// This method will only reset the iterator if index is smaller than
890 /// fCurrentIndex, which is used to track the iteration progress and is
891 /// updated when a new object if returned by this method.
892 /// @param iter The iterator to fetch an object from.
893 /// @param index The index number of the object to fetch in the range [0 .. N-1]
894 /// where N = CountObjects(iter)
898 AliError(Form("Index is out of bounds. Got a value of %d.", index));
902 if (iter == 0x0) return 0x0;
903 if (index <= fCurrentIndex)
909 TObject* object = 0x0;
910 while (fCurrentIndex < index)
912 object = iter->Next();
915 AliError(Form("Index is out of bounds. Got a value of %d.", index));