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 "AliMpIntPair.h"
32 #include "AliMpDEManager.h"
33 #include "AliMpConstants.h"
36 #include "AliLoader.h"
38 #include "AliRunLoader.h"
39 #include "AliHeader.h"
40 #include "AliCDBManager.h"
43 #include <TParticle.h>
44 #include <Riostream.h>
49 #include <TIterator.h>
53 //-----------------------------------------------------------------------------
54 /// \class AliMUONDataInterface
56 /// An easy to use interface to the MUON data data stored in
57 /// TreeS, TreeD and TreeR.
59 /// For MC related information (i.e. TreeH, TreeK, TreeTR), see
60 /// AliMUONMCDataInterface.
63 /// This interface in not necessarily the fastest way to fetch the data but
64 /// it is the easiest.
66 /// \author Laurent Aphecetche, Subatech & Artur Szostak <artursz@iafrica.com> (University of Cape Town)
67 //-----------------------------------------------------------------------------
70 ClassImp(AliMUONDataInterface)
74 Int_t AliMUONDataInterface::fgInstanceCounter(0);
76 //______________________________________________________________________________
77 AliMUONDataInterface::AliMUONDataInterface(const char* filename)
85 fCurrentIteratorType(kNoIterator),
92 /// @param filename should be the full path to a valid galice.root file
96 if (AliCDBManager::Instance() != NULL)
98 if (AliCDBManager::Instance()->GetDefaultStorage() == NULL)
100 AliInfo("Default storage for CDB not set.");
101 AliCDBManager::Instance()->SetDefaultStorage("local://$ALICE_ROOT");
108 if ( ! AliMpCDB::LoadDDLStore() ) {
109 AliFatal("Could not access mapping from OCDB !");
113 //______________________________________________________________________________
114 AliMUONDataInterface::~AliMUONDataInterface()
118 if ( fLoader != 0x0 )
120 delete fLoader->GetRunLoader();
125 //______________________________________________________________________________
127 AliMUONDataInterface::DigitStore(Int_t event)
129 /// Return digitStore for a given event.
130 /// Return 0x0 if event not found.
131 /// Returned pointer should not be deleted
133 if (not IsValid()) return 0x0;
134 if (event == fCurrentEvent and fDigitStore != 0x0) return fDigitStore;
137 if (not LoadEvent(event)) return 0x0;
139 fLoader->LoadDigits();
141 TTree* treeD = fLoader->TreeD();
144 AliError("Could not get treeD");
148 fDigitStore = AliMUONVDigitStore::Create(*treeD);
149 if ( fDigitStore != 0x0 )
151 fDigitStore->Clear();
152 fDigitStore->Connect(*treeD);
156 fLoader->UnloadDigits();
161 //______________________________________________________________________________
162 AliMUONVClusterStore*
163 AliMUONDataInterface::ClusterStore(Int_t event)
165 /// Return clusterStore for a given event.
166 /// Return 0x0 if event not found.
167 /// Returned pointer should not be deleted
169 if (not IsValid()) return 0x0;
170 if (event == fCurrentEvent and fClusterStore != 0x0) return fClusterStore;
173 if (not LoadEvent(event)) return 0x0;
175 fLoader->LoadRecPoints();
177 TTree* treeR = fLoader->TreeR();
180 AliError("Could not get treeR");
184 fClusterStore = AliMUONVClusterStore::Create(*treeR);
185 if ( fClusterStore != 0x0 )
187 fClusterStore->Clear();
188 fClusterStore->Connect(*treeR);
192 fLoader->UnloadRecPoints();
194 return fClusterStore;
197 //_____________________________________________________________________________
198 AliMUONVTriggerStore*
199 AliMUONDataInterface::TriggerStore(Int_t event, const char* treeLetter)
201 /// Return the triggerStore for a given event.
202 /// Return 0x0 if event not found.
203 /// Returned pointer should not be deleted
204 /// treeLetter can be R or D to tell from which tree to read the information
206 if (not IsValid()) return 0x0;
207 if (event == fCurrentEvent and fTriggerStore != 0x0) return fTriggerStore;
210 if (not LoadEvent(event)) return 0x0;
214 TString stree(treeLetter);
219 fLoader->LoadDigits();
220 tree = fLoader->TreeD();
222 else if ( stree == "R" )
224 fLoader->LoadRecPoints();
225 tree = fLoader->TreeR();
230 AliError(Form("Could not get tree%s",treeLetter));
234 fTriggerStore = AliMUONVTriggerStore::Create(*tree);
235 if ( fTriggerStore != 0x0 )
237 fTriggerStore->Clear();
238 fTriggerStore->Connect(*tree);
244 fLoader->UnloadDigits();
246 else if ( stree == "R" )
248 fLoader->UnloadRecPoints();
251 return fTriggerStore;
254 //______________________________________________________________________________
256 AliMUONDataInterface::DumpDigits(Int_t event, Bool_t sorted)
258 /// Dump the digits for a given event, sorted if so required
260 if ( fDigitStore != 0x0 )
264 DumpSorted(*fDigitStore);
268 fDigitStore->Print();
273 //______________________________________________________________________________
275 AliMUONDataInterface::DumpRecPoints(Int_t event, Bool_t sorted)
277 /// Dump the recpoints for a given event, sorted if so required
279 if ( fClusterStore != 0x0 )
283 DumpSorted(*fClusterStore);
287 fClusterStore->Print();
292 //_____________________________________________________________________________
294 AliMUONDataInterface::DumpSorted(const AliMUONVStore& store) const
296 /// Dump the given store, in sorted order
298 TIter next(store.CreateIterator());
301 list.SetOwner(kFALSE);
303 while ( ( object = next() ) )
313 //_____________________________________________________________________________
315 AliMUONDataInterface::DumpTrigger(Int_t event, const char* treeLetter)
317 /// Dump trigger for a given event from a given tree (if event>=0)
318 /// or loop over all events and build a trigger ntuple if event<0
319 /// treeLetter can be R or D to tell from which tree to read the information
323 NtupleTrigger(treeLetter);
327 TriggerStore(event,treeLetter);
329 if ( fTriggerStore != 0x0 )
331 fTriggerStore->Print();
336 //_____________________________________________________________________________
338 AliMUONDataInterface::NtupleTrigger(const char* treeLetter)
340 //// Loop over events to build trigger ntuples
343 TString sTreeLetter(treeLetter);
344 sTreeLetter.ToUpper();
346 if ( sTreeLetter != "R" && sTreeLetter != "D" )
348 AliError(Form("Cannot handle tree%s. Use D or R",treeLetter));
353 TNtuple tupleGlo("TgtupleGlo","Global Trigger Ntuple",
354 "ev:slpt:shpt:uplpt:uphpt:lplpt:lplpt");
355 TNtuple tupleLoc("TgtupleLoc","Local Trigger Ntuple",
356 "ev:LoCircuit:LoStripX:LoDev:StripY:LoLpt:LoHpt:y11:y21:x11");
358 // initialize counters
366 AliMUONGeometryTransformer transformer;
367 transformer.LoadGeometryData(Form("%s/geometry.root",
368 gSystem->DirName(fLoader->GetRunLoader()->GetFileName())));
369 AliMUONTriggerCircuit triggerCircuit(&transformer);
371 // select output file name from selected Tree
372 Char_t fileNameOut[30];
373 if (sTreeLetter == "D")
375 AliInfo(Form("reading from Digits\n"));
376 sprintf(fileNameOut,"TriggerCheckFromDigits.root");
378 else if (sTreeLetter == "R")
380 AliInfo(Form("reading from RecPoints\n"));
381 sprintf(fileNameOut,"TriggerCheckFromRP.root");
385 Int_t nevents = NumberOfEvents();
387 for (Int_t ievent=0; ievent<nevents; ++ievent)
389 if (ievent%100==0) AliInfo(Form("Processing event %d\n",ievent));
391 AliMUONVTriggerStore* triggerStore = TriggerStore(ievent,treeLetter);
395 AliError(Form("Could not read %s from tree%s","Trigger",treeLetter));
399 // get global trigger info
400 AliMUONGlobalTrigger* gloTrg = triggerStore->Global();
401 sLowpt+=gloTrg->SingleLpt();
402 sHighpt+=gloTrg->SingleHpt();
403 uSLowpt+=gloTrg->PairUnlikeLpt();
404 uSHighpt+=gloTrg->PairUnlikeHpt();
405 lSLowpt+=gloTrg->PairLikeLpt();
406 lSHighpt+=gloTrg->PairLikeHpt();
408 // loop on local triggers
409 TIter next(triggerStore->CreateIterator());
410 AliMUONLocalTrigger* locTrg(0x0);
411 while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
413 Bool_t xTrig=locTrg->IsTrigX();
414 Bool_t yTrig=locTrg->IsTrigY();
417 { // fill ntuple if trigger in X and Y
418 tupleLoc.Fill(ievent,locTrg->LoCircuit(),
424 triggerCircuit.GetY11Pos(locTrg->LoCircuit(),locTrg->LoStripX()),
425 triggerCircuit.GetY21Pos(locTrg->LoCircuit(),locTrg->LoStripX()+locTrg->LoDev()+1),
426 triggerCircuit.GetX11Pos(locTrg->LoCircuit(),locTrg->LoStripY()));
428 tupleGlo.Fill(ievent,gloTrg->SingleLpt(),gloTrg->SingleHpt(),
429 gloTrg->PairUnlikeLpt(),gloTrg->PairUnlikeHpt(),
430 gloTrg->PairLikeLpt(),gloTrg->PairLikeHpt());
431 } // end of loop on local triggers
432 } // end of loop on events
434 // print info and store ntuples
436 printf("=============================================\n");
437 printf("================ SUMMARY ==================\n");
439 printf("Total number of events processed %d \n",nevents);
441 printf(" Global Trigger output Low pt High pt\n");
442 printf(" number of Single :\t");
443 printf("%i\t%i\t",sLowpt,sHighpt);
445 printf(" number of UnlikeSign pair :\t");
446 printf("%i\t%i\t",uSLowpt,uSHighpt);
448 printf(" number of LikeSign pair :\t");
449 printf("%i\t%i\t",lSLowpt,lSHighpt);
451 printf("=============================================\n");
454 TFile myFile(fileNameOut, "RECREATE");
460 //_____________________________________________________________________________
462 AliMUONDataInterface::LoadEvent(Int_t event)
464 /// Load event if different from the current one.
465 /// Returns kFALSE on error and kTRUE if the event was loaded.
469 AliDebug(1,Form("Loading event %d using runLoader %p",event,fLoader->GetRunLoader()));
470 if (fLoader->GetRunLoader()->GetEvent(event) == 0)
472 fCurrentEvent = event;
479 //______________________________________________________________________________
481 AliMUONDataInterface::NumberOfEvents() const
483 /// Number of events in the current galice.root file we're attached to
484 if (not IsValid()) return -1;
485 return fLoader->GetRunLoader()->GetNumberOfEvents();
488 //_____________________________________________________________________________
490 AliMUONDataInterface::Open(const char* filename)
492 /// Connect to a given galice.root file
498 if ( fLoader != 0x0 )
500 delete fLoader->GetRunLoader();
507 TString foldername(Form("%s-%d",ClassName(),fgInstanceCounter));
509 while (AliRunLoader::GetRunLoader(foldername) != 0x0)
511 delete AliRunLoader::GetRunLoader(foldername);
514 AliRunLoader* runLoader = AliRunLoader::Open(filename,foldername);
515 if (runLoader == 0x0)
517 AliError(Form("Cannot open file %s",filename));
521 runLoader->LoadHeader();
522 if ( ! runLoader->GetHeader() ) {
523 AliError("Cannot load header.");
527 Int_t runNumber = runLoader->GetHeader()->GetRun();
528 AliCDBManager::Instance()->SetRun(runNumber);
530 runLoader->UnloadHeader();
532 fLoader = runLoader->GetDetectorLoader("MUON");
535 AliError("Cannot get AliMUONLoader");
541 AliError(Form("Could not access %s filename. Object is unuseable",filename));
545 //_____________________________________________________________________________
546 Bool_t AliMUONDataInterface::GetEvent(Int_t event)
548 /// Loads all reconstructed data for the given event.
550 if (DigitStore(event) == 0x0) return kFALSE;
551 if (ClusterStore(event) == 0x0) return kFALSE;
552 if (TriggerStore(event) == 0x0) return kFALSE;
556 //_____________________________________________________________________________
557 Int_t AliMUONDataInterface::NumberOfDigits(Int_t detElemId)
559 /// Returns the number of digits to be found on a given detector element.
560 /// @param detElemId The detector element ID number to search on.
562 TIterator* iter = GetIterator(kDigitIteratorByDetectorElement, detElemId);
563 return CountObjects(iter);
566 //_____________________________________________________________________________
567 AliMUONVDigit* AliMUONDataInterface::Digit(Int_t detElemId, Int_t index)
569 /// Returns the a pointer to the index'th digit on the specified detector element.
570 /// @param detElemId The detector element ID number to search on.
571 /// @param index The index number of the digit to fetch in the range [0 .. N-1],
572 /// where N = NumberOfDigits(detElemId)
574 TIterator* iter = GetIterator(kDigitIteratorByDetectorElement, detElemId);
575 return static_cast<AliMUONVDigit*>( FetchObject(iter, index) );
578 //_____________________________________________________________________________
579 Int_t AliMUONDataInterface::NumberOfDigits(Int_t chamber, Int_t cathode)
581 /// Returns the number of digits to be found on a specific chamber and cathode.
582 /// @param chamber The chamber number in the range [0 .. 13].
583 /// @param cathode The cathode in the range [0 .. 1], where 0 is the bending and
584 /// 1 is the non-bending plane.
586 TIterator* iter = GetIterator(kDigitIteratorByChamberAndCathode, chamber, cathode);
587 return CountObjects(iter);
590 //_____________________________________________________________________________
591 AliMUONVDigit* AliMUONDataInterface::Digit(Int_t chamber, Int_t cathode, Int_t index)
593 /// Returns the a pointer to the index'th digit on the specified chamber and cathode.
594 /// @param chamber The chamber number in the range [0 .. 13].
595 /// @param cathode The cathode in the range [0 .. 1], where 0 is the bending and
596 /// 1 is the non-bending plane.
597 /// @param index The index number of the digit to fetch in the range [0 .. N-1],
598 /// where N = NumberOfDigits(chamber, cathode)
600 TIterator* iter = GetIterator(kDigitIteratorByChamberAndCathode, chamber, cathode);
601 return static_cast<AliMUONVDigit*>( FetchObject(iter, index) );
604 //_____________________________________________________________________________
605 Int_t AliMUONDataInterface::NumberOfRawClusters(Int_t chamber)
607 /// Returns the number of reconstructed raw clusters on the specified chamber.
608 /// @param chamber The chamber number in the range [0 .. 13].
610 TIterator* iter = GetIterator(kRawClusterIterator, chamber);
611 return CountObjects(iter);
614 //_____________________________________________________________________________
615 AliMUONVCluster* AliMUONDataInterface::RawCluster(Int_t chamber, Int_t index)
617 /// Returns a pointer to the index'th raw cluster on the specified chamber.
618 /// @param chamber The chamber number in the range [0 .. 13].
619 /// @param index The index number of the raw cluster to fetch in the range [0 .. N-1],
620 /// where N = NumberOfRawClusters(chamber)
622 TIterator* iter = GetIterator(kRawClusterIterator, chamber);
623 return static_cast<AliMUONVCluster*>( FetchObject(iter, index) );
626 //_____________________________________________________________________________
627 Int_t AliMUONDataInterface::NumberOfLocalTriggers()
629 /// Returns the number of reconstructed local trigger objects.
631 TIterator* iter = GetIterator(kLocalTriggerIterator);
632 return CountObjects(iter);
635 //_____________________________________________________________________________
636 AliMUONLocalTrigger* AliMUONDataInterface::LocalTrigger(Int_t index)
638 /// Returns a pointer to the index'th local trigger object.
639 /// @param index The index number of the local trigger object to fetch in the range [0 .. N-1],
640 /// where N = NumberOfLocalTriggers()
642 TIterator* iter = GetIterator(kLocalTriggerIterator);
643 return static_cast<AliMUONLocalTrigger*>( FetchObject(iter, index) );
646 //_____________________________________________________________________________
647 Int_t AliMUONDataInterface::NumberOfRegionalTriggers()
649 /// Returns the number of regional trigger objects reconstructed.
651 TIterator* iter = GetIterator(kRegionalTriggerIterator);
652 return CountObjects(iter);
655 //_____________________________________________________________________________
656 AliMUONRegionalTrigger* AliMUONDataInterface::RegionalTrigger(Int_t index)
658 /// Returns a pointer to the index'th regional trigger object.
659 /// @param index The index number of the regional trigger object to fetch in the range [0 .. N-1],
660 /// where N = NumberOfRegionalTriggers()
662 TIterator* iter = GetIterator(kRegionalTriggerIterator);
663 return static_cast<AliMUONRegionalTrigger*>( FetchObject(iter, index) );
666 //_____________________________________________________________________________
667 AliMUONGlobalTrigger* AliMUONDataInterface::GlobalTrigger()
669 /// Returns a pointer to the reconstructed global trigger object for the event.
671 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
672 if (store == 0x0) return 0x0;
673 return store->Global();
676 //_____________________________________________________________________________
677 void AliMUONDataInterface::ResetStores()
679 /// Deletes all the store objects that have been created and resets the pointers to 0x0.
680 /// The temporary iterator object is automatically reset. See ResetIterator for more details.
683 if (fDigitStore != 0x0)
688 if (fTriggerStore != 0x0)
690 delete fTriggerStore;
693 if (fClusterStore != 0x0)
695 delete fClusterStore;
700 //_____________________________________________________________________________
701 TIterator* AliMUONDataInterface::GetIterator(IteratorType type, Int_t x, Int_t y)
703 /// Creates an appropriate iterator object and returns it.
704 /// If the iterator has already been created then that one is returned otherwise
705 /// a new object is created.
706 /// Depending on the value of 'type' the semantics of parameters x and y can change.
707 /// @param type The type of iterator to create.
708 /// @param x This is the detector element ID if type == kDigitIteratorByDetectorElement
709 /// If type equals kDigitIteratorByChamberAndCathode or kRawClusterIterator
710 /// then this is the chamber number. In all other cases this parameter is
712 /// @param y If type == kDigitIteratorByChamberAndCathode then this parameter is the
713 /// cathode number. In all other cases this parameter is
716 if (type == fCurrentIteratorType and fDataX == x and fDataY == y)
719 if (fCurrentEvent == -1)
721 AliError("No event was selected. Try first using GetEvent().");
729 case kDigitIteratorByDetectorElement:
732 AliMUONVDigitStore* store = DigitStore(fCurrentEvent);
733 if (store == 0x0) return 0x0;
734 fIterator = store->CreateIterator(detElem, detElem, 2);
735 if (fIterator == 0x0) return 0x0;
736 fCurrentIteratorType = kDigitIteratorByDetectorElement;
741 case kDigitIteratorByChamberAndCathode:
745 if (chamber < 0 or AliMpConstants::NofChambers() <= chamber)
748 "Must have give a chamber value in the range [0..%d], but got a value of: %d",
749 AliMpConstants::NofChambers() - 1,
754 if (cathode < 0 or 1 < cathode)
756 AliError(Form("Must have give a cathode value in the range [0..1], but got a value of: %d", cathode));
760 AliMUONVDigitStore* store = DigitStore(fCurrentEvent);
761 if (store == 0x0) return 0x0;
762 AliMpIntPair pair = AliMpDEManager::GetDetElemIdRange(chamber);
763 fIterator = store->CreateIterator(pair.GetFirst(), pair.GetSecond(), cathode);
764 if (fIterator == 0x0) return 0x0;
765 fCurrentIteratorType = kDigitIteratorByChamberAndCathode;
771 case kRawClusterIterator:
774 AliMUONVClusterStore* store = ClusterStore(fCurrentEvent);
775 if (store == 0x0) return 0x0;
776 fIterator = store->CreateChamberIterator(chamber, chamber);
777 if (fIterator == 0x0) return 0x0;
778 fCurrentIteratorType = kRawClusterIterator;
783 case kLocalTriggerIterator:
785 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
786 if (store == 0x0) return 0x0;
787 fIterator = store->CreateLocalIterator();
788 if (fIterator == 0x0) return 0x0;
789 fCurrentIteratorType = kLocalTriggerIterator;
793 case kRegionalTriggerIterator:
795 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
796 if (store == 0x0) return 0x0;
797 fIterator = store->CreateRegionalIterator();
798 if (fIterator == 0x0) return 0x0;
799 fCurrentIteratorType = kRegionalTriggerIterator;
808 //_____________________________________________________________________________
809 void AliMUONDataInterface::ResetIterator()
811 /// The temporary iterator object is deleted if it exists and the pointer reset to 0x0.
812 /// The iterator type and temporary data indicating the state of the iterator are
815 if (fIterator != 0x0) delete fIterator;
816 fCurrentIteratorType = kNoIterator;
817 fCurrentIndex = fDataX = fDataY = -1;
821 //_____________________________________________________________________________
822 Int_t AliMUONDataInterface::CountObjects(TIterator* iter)
824 /// Counts the number of objects in the iterator and resets it.
825 /// @return The number of objects in 'iter'.
827 if (iter == 0x0) return -1;
830 while ( iter->Next() != 0x0 ) count++;
836 //_____________________________________________________________________________
837 TObject* AliMUONDataInterface::FetchObject(TIterator* iter, Int_t index)
839 /// Fetches the index'th object from the iterator counting the first object
840 /// returned by iterator after it is reset as index == 0. The next object
841 /// has index == 1 and so on where the last object returned by the iterator
842 /// has index == N-1 where N = CountObjects(iter)
843 /// This method will only reset the iterator if index is smaller than
844 /// fCurrentIndex, which is used to track the iteration progress and is
845 /// updated when a new object if returned by this method.
846 /// @param iter The iterator to fetch an object from.
847 /// @param index The index number of the object to fetch in the range [0 .. N-1]
848 /// where N = CountObjects(iter)
852 AliError(Form("Index is out of bounds. Got a value of %d.", index));
856 if (iter == 0x0) return 0x0;
857 if (index <= fCurrentIndex)
863 TObject* object = 0x0;
864 while (fCurrentIndex < index)
866 object = iter->Next();
869 AliError(Form("Index is out of bounds. Got a value of %d.", index));