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 &&
97 AliCDBManager::Instance()->GetDefaultStorage() == NULL ) {
98 AliFatal("CDB default storage not defined.");
104 if ( ! AliMpCDB::LoadDDLStore() ) {
105 AliFatal("Could not access mapping from OCDB !");
109 //______________________________________________________________________________
110 AliMUONDataInterface::~AliMUONDataInterface()
114 if ( fLoader != 0x0 )
116 delete fLoader->GetRunLoader();
121 //______________________________________________________________________________
123 AliMUONDataInterface::DigitStore(Int_t event)
125 /// Return digitStore for a given event.
126 /// Return 0x0 if event not found.
127 /// Returned pointer should not be deleted
129 if (not IsValid()) return 0x0;
130 if (event == fCurrentEvent and fDigitStore != 0x0) return fDigitStore;
133 if (not LoadEvent(event)) return 0x0;
135 fLoader->LoadDigits();
137 TTree* treeD = fLoader->TreeD();
140 AliError("Could not get treeD");
144 fDigitStore = AliMUONVDigitStore::Create(*treeD);
145 if ( fDigitStore != 0x0 )
147 fDigitStore->Clear();
148 fDigitStore->Connect(*treeD);
152 fLoader->UnloadDigits();
157 //______________________________________________________________________________
158 AliMUONVClusterStore*
159 AliMUONDataInterface::ClusterStore(Int_t event)
161 /// Return clusterStore for a given event.
162 /// Return 0x0 if event not found.
163 /// Returned pointer should not be deleted
165 if (not IsValid()) return 0x0;
166 if (event == fCurrentEvent and fClusterStore != 0x0) return fClusterStore;
169 if (not LoadEvent(event)) return 0x0;
171 fLoader->LoadRecPoints();
173 TTree* treeR = fLoader->TreeR();
176 AliError("Could not get treeR");
180 fClusterStore = AliMUONVClusterStore::Create(*treeR);
181 if ( fClusterStore != 0x0 )
183 fClusterStore->Clear();
184 fClusterStore->Connect(*treeR);
188 fLoader->UnloadRecPoints();
190 return fClusterStore;
193 //_____________________________________________________________________________
194 AliMUONVTriggerStore*
195 AliMUONDataInterface::TriggerStore(Int_t event, const char* treeLetter)
197 /// Return the triggerStore for a given event.
198 /// Return 0x0 if event not found.
199 /// Returned pointer should not be deleted
200 /// treeLetter can be R or D to tell from which tree to read the information
202 if (not IsValid()) return 0x0;
203 if (event == fCurrentEvent and fTriggerStore != 0x0) return fTriggerStore;
206 if (not LoadEvent(event)) return 0x0;
210 TString stree(treeLetter);
215 fLoader->LoadDigits();
216 tree = fLoader->TreeD();
218 else if ( stree == "R" )
220 fLoader->LoadRecPoints();
221 tree = fLoader->TreeR();
226 AliError(Form("Could not get tree%s",treeLetter));
230 fTriggerStore = AliMUONVTriggerStore::Create(*tree);
231 if ( fTriggerStore != 0x0 )
233 fTriggerStore->Clear();
234 fTriggerStore->Connect(*tree);
240 fLoader->UnloadDigits();
242 else if ( stree == "R" )
244 fLoader->UnloadRecPoints();
247 return fTriggerStore;
250 //______________________________________________________________________________
252 AliMUONDataInterface::DumpDigits(Int_t event, Bool_t sorted)
254 /// Dump the digits for a given event, sorted if so required
256 if ( fDigitStore != 0x0 )
260 DumpSorted(*fDigitStore);
264 fDigitStore->Print();
269 //______________________________________________________________________________
271 AliMUONDataInterface::DumpRecPoints(Int_t event, Bool_t sorted)
273 /// Dump the recpoints for a given event, sorted if so required
275 if ( fClusterStore != 0x0 )
279 DumpSorted(*fClusterStore);
283 fClusterStore->Print();
288 //_____________________________________________________________________________
290 AliMUONDataInterface::DumpSorted(const AliMUONVStore& store) const
292 /// Dump the given store, in sorted order
294 TIter next(store.CreateIterator());
297 list.SetOwner(kFALSE);
299 while ( ( object = next() ) )
309 //_____________________________________________________________________________
311 AliMUONDataInterface::DumpTrigger(Int_t event, const char* treeLetter)
313 /// Dump trigger for a given event from a given tree (if event>=0)
314 /// or loop over all events and build a trigger ntuple if event<0
315 /// treeLetter can be R or D to tell from which tree to read the information
319 NtupleTrigger(treeLetter);
323 TriggerStore(event,treeLetter);
325 if ( fTriggerStore != 0x0 )
327 fTriggerStore->Print();
332 //_____________________________________________________________________________
334 AliMUONDataInterface::NtupleTrigger(const char* treeLetter)
336 //// Loop over events to build trigger ntuples
339 TString sTreeLetter(treeLetter);
340 sTreeLetter.ToUpper();
342 if ( sTreeLetter != "R" && sTreeLetter != "D" )
344 AliError(Form("Cannot handle tree%s. Use D or R",treeLetter));
349 TNtuple tupleGlo("TgtupleGlo","Global Trigger Ntuple",
350 "ev:slpt:shpt:uplpt:uphpt:lplpt:lplpt");
351 TNtuple tupleLoc("TgtupleLoc","Local Trigger Ntuple",
352 "ev:LoCircuit:LoStripX:LoDev:StripY:LoLpt:LoHpt:y11:y21:x11");
354 // initialize counters
362 AliMUONGeometryTransformer transformer;
363 transformer.LoadGeometryData(Form("%s/geometry.root",
364 gSystem->DirName(fLoader->GetRunLoader()->GetFileName())));
365 AliMUONTriggerCircuit triggerCircuit(&transformer);
367 // select output file name from selected Tree
368 Char_t fileNameOut[30];
369 if (sTreeLetter == "D")
371 AliInfo(Form("reading from Digits\n"));
372 sprintf(fileNameOut,"TriggerCheckFromDigits.root");
374 else if (sTreeLetter == "R")
376 AliInfo(Form("reading from RecPoints\n"));
377 sprintf(fileNameOut,"TriggerCheckFromRP.root");
381 Int_t nevents = NumberOfEvents();
383 for (Int_t ievent=0; ievent<nevents; ++ievent)
385 if (ievent%100==0) AliInfo(Form("Processing event %d\n",ievent));
387 AliMUONVTriggerStore* triggerStore = TriggerStore(ievent,treeLetter);
391 AliError(Form("Could not read %s from tree%s","Trigger",treeLetter));
395 // get global trigger info
396 AliMUONGlobalTrigger* gloTrg = triggerStore->Global();
397 sLowpt+=gloTrg->SingleLpt();
398 sHighpt+=gloTrg->SingleHpt();
399 uSLowpt+=gloTrg->PairUnlikeLpt();
400 uSHighpt+=gloTrg->PairUnlikeHpt();
401 lSLowpt+=gloTrg->PairLikeLpt();
402 lSHighpt+=gloTrg->PairLikeHpt();
404 // loop on local triggers
405 TIter next(triggerStore->CreateIterator());
406 AliMUONLocalTrigger* locTrg(0x0);
407 while ( ( locTrg = static_cast<AliMUONLocalTrigger*>(next()) ) )
409 Bool_t xTrig=locTrg->IsTrigX();
410 Bool_t yTrig=locTrg->IsTrigY();
413 { // fill ntuple if trigger in X and Y
414 tupleLoc.Fill(ievent,locTrg->LoCircuit(),
420 triggerCircuit.GetY11Pos(locTrg->LoCircuit(),locTrg->LoStripX()),
421 triggerCircuit.GetY21Pos(locTrg->LoCircuit(),locTrg->LoStripX()+locTrg->LoDev()+1),
422 triggerCircuit.GetX11Pos(locTrg->LoCircuit(),locTrg->LoStripY()));
424 tupleGlo.Fill(ievent,gloTrg->SingleLpt(),gloTrg->SingleHpt(),
425 gloTrg->PairUnlikeLpt(),gloTrg->PairUnlikeHpt(),
426 gloTrg->PairLikeLpt(),gloTrg->PairLikeHpt());
427 } // end of loop on local triggers
428 } // end of loop on events
430 // print info and store ntuples
432 printf("=============================================\n");
433 printf("================ SUMMARY ==================\n");
435 printf("Total number of events processed %d \n",nevents);
437 printf(" Global Trigger output Low pt High pt\n");
438 printf(" number of Single :\t");
439 printf("%i\t%i\t",sLowpt,sHighpt);
441 printf(" number of UnlikeSign pair :\t");
442 printf("%i\t%i\t",uSLowpt,uSHighpt);
444 printf(" number of LikeSign pair :\t");
445 printf("%i\t%i\t",lSLowpt,lSHighpt);
447 printf("=============================================\n");
450 TFile myFile(fileNameOut, "RECREATE");
456 //_____________________________________________________________________________
458 AliMUONDataInterface::LoadEvent(Int_t event)
460 /// Load event if different from the current one.
461 /// Returns kFALSE on error and kTRUE if the event was loaded.
465 AliDebug(1,Form("Loading event %d using runLoader %p",event,fLoader->GetRunLoader()));
466 if (fLoader->GetRunLoader()->GetEvent(event) == 0)
468 fCurrentEvent = event;
475 //______________________________________________________________________________
477 AliMUONDataInterface::NumberOfEvents() const
479 /// Number of events in the current galice.root file we're attached to
480 if (not IsValid()) return -1;
481 return fLoader->GetRunLoader()->GetNumberOfEvents();
484 //_____________________________________________________________________________
486 AliMUONDataInterface::Open(const char* filename)
488 /// Connect to a given galice.root file
494 if ( fLoader != 0x0 )
496 delete fLoader->GetRunLoader();
503 TString foldername(Form("%s-%d",ClassName(),fgInstanceCounter));
505 while (AliRunLoader::GetRunLoader(foldername) != 0x0)
507 delete AliRunLoader::GetRunLoader(foldername);
510 AliRunLoader* runLoader = AliRunLoader::Open(filename,foldername);
511 if (runLoader == 0x0)
513 AliError(Form("Cannot open file %s",filename));
517 runLoader->LoadHeader();
518 if ( ! runLoader->GetHeader() ) {
519 AliError("Cannot load header.");
523 Int_t runNumber = runLoader->GetHeader()->GetRun();
524 AliCDBManager::Instance()->SetRun(runNumber);
526 runLoader->UnloadHeader();
528 fLoader = runLoader->GetDetectorLoader("MUON");
531 AliError("Cannot get AliMUONLoader");
537 AliError(Form("Could not access %s filename. Object is unuseable",filename));
541 //_____________________________________________________________________________
542 Bool_t AliMUONDataInterface::GetEvent(Int_t event)
544 /// Loads all reconstructed data for the given event.
546 if (DigitStore(event) == 0x0) return kFALSE;
547 if (ClusterStore(event) == 0x0) return kFALSE;
548 if (TriggerStore(event) == 0x0) return kFALSE;
552 //_____________________________________________________________________________
553 Int_t AliMUONDataInterface::NumberOfDigits(Int_t detElemId)
555 /// Returns the number of digits to be found on a given detector element.
556 /// @param detElemId The detector element ID number to search on.
558 TIterator* iter = GetIterator(kDigitIteratorByDetectorElement, detElemId);
559 return CountObjects(iter);
562 //_____________________________________________________________________________
563 AliMUONVDigit* AliMUONDataInterface::Digit(Int_t detElemId, Int_t index)
565 /// Returns the a pointer to the index'th digit on the specified detector element.
566 /// @param detElemId The detector element ID number to search on.
567 /// @param index The index number of the digit to fetch in the range [0 .. N-1],
568 /// where N = NumberOfDigits(detElemId)
570 TIterator* iter = GetIterator(kDigitIteratorByDetectorElement, detElemId);
571 return static_cast<AliMUONVDigit*>( FetchObject(iter, index) );
574 //_____________________________________________________________________________
575 Int_t AliMUONDataInterface::NumberOfDigits(Int_t chamber, Int_t cathode)
577 /// Returns the number of digits to be found on a specific chamber and cathode.
578 /// @param chamber The chamber number in the range [0 .. 13].
579 /// @param cathode The cathode in the range [0 .. 1], where 0 is the bending and
580 /// 1 is the non-bending plane.
582 TIterator* iter = GetIterator(kDigitIteratorByChamberAndCathode, chamber, cathode);
583 return CountObjects(iter);
586 //_____________________________________________________________________________
587 AliMUONVDigit* AliMUONDataInterface::Digit(Int_t chamber, Int_t cathode, Int_t index)
589 /// Returns the a pointer to the index'th digit on the specified chamber and cathode.
590 /// @param chamber The chamber number in the range [0 .. 13].
591 /// @param cathode The cathode in the range [0 .. 1], where 0 is the bending and
592 /// 1 is the non-bending plane.
593 /// @param index The index number of the digit to fetch in the range [0 .. N-1],
594 /// where N = NumberOfDigits(chamber, cathode)
596 TIterator* iter = GetIterator(kDigitIteratorByChamberAndCathode, chamber, cathode);
597 return static_cast<AliMUONVDigit*>( FetchObject(iter, index) );
600 //_____________________________________________________________________________
601 Int_t AliMUONDataInterface::NumberOfRawClusters(Int_t chamber)
603 /// Returns the number of reconstructed raw clusters on the specified chamber.
604 /// @param chamber The chamber number in the range [0 .. 13].
606 TIterator* iter = GetIterator(kRawClusterIterator, chamber);
607 return CountObjects(iter);
610 //_____________________________________________________________________________
611 AliMUONVCluster* AliMUONDataInterface::RawCluster(Int_t chamber, Int_t index)
613 /// Returns a pointer to the index'th raw cluster on the specified chamber.
614 /// @param chamber The chamber number in the range [0 .. 13].
615 /// @param index The index number of the raw cluster to fetch in the range [0 .. N-1],
616 /// where N = NumberOfRawClusters(chamber)
618 TIterator* iter = GetIterator(kRawClusterIterator, chamber);
619 return static_cast<AliMUONVCluster*>( FetchObject(iter, index) );
622 //_____________________________________________________________________________
623 Int_t AliMUONDataInterface::NumberOfLocalTriggers()
625 /// Returns the number of reconstructed local trigger objects.
627 TIterator* iter = GetIterator(kLocalTriggerIterator);
628 return CountObjects(iter);
631 //_____________________________________________________________________________
632 AliMUONLocalTrigger* AliMUONDataInterface::LocalTrigger(Int_t index)
634 /// Returns a pointer to the index'th local trigger object.
635 /// @param index The index number of the local trigger object to fetch in the range [0 .. N-1],
636 /// where N = NumberOfLocalTriggers()
638 TIterator* iter = GetIterator(kLocalTriggerIterator);
639 return static_cast<AliMUONLocalTrigger*>( FetchObject(iter, index) );
642 //_____________________________________________________________________________
643 Int_t AliMUONDataInterface::NumberOfRegionalTriggers()
645 /// Returns the number of regional trigger objects reconstructed.
647 TIterator* iter = GetIterator(kRegionalTriggerIterator);
648 return CountObjects(iter);
651 //_____________________________________________________________________________
652 AliMUONRegionalTrigger* AliMUONDataInterface::RegionalTrigger(Int_t index)
654 /// Returns a pointer to the index'th regional trigger object.
655 /// @param index The index number of the regional trigger object to fetch in the range [0 .. N-1],
656 /// where N = NumberOfRegionalTriggers()
658 TIterator* iter = GetIterator(kRegionalTriggerIterator);
659 return static_cast<AliMUONRegionalTrigger*>( FetchObject(iter, index) );
662 //_____________________________________________________________________________
663 AliMUONGlobalTrigger* AliMUONDataInterface::GlobalTrigger()
665 /// Returns a pointer to the reconstructed global trigger object for the event.
667 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
668 if (store == 0x0) return 0x0;
669 return store->Global();
672 //_____________________________________________________________________________
673 void AliMUONDataInterface::ResetStores()
675 /// Deletes all the store objects that have been created and resets the pointers to 0x0.
676 /// The temporary iterator object is automatically reset. See ResetIterator for more details.
679 if (fDigitStore != 0x0)
684 if (fTriggerStore != 0x0)
686 delete fTriggerStore;
689 if (fClusterStore != 0x0)
691 delete fClusterStore;
696 //_____________________________________________________________________________
697 TIterator* AliMUONDataInterface::GetIterator(IteratorType type, Int_t x, Int_t y)
699 /// Creates an appropriate iterator object and returns it.
700 /// If the iterator has already been created then that one is returned otherwise
701 /// a new object is created.
702 /// Depending on the value of 'type' the semantics of parameters x and y can change.
703 /// @param type The type of iterator to create.
704 /// @param x This is the detector element ID if type == kDigitIteratorByDetectorElement
705 /// If type equals kDigitIteratorByChamberAndCathode or kRawClusterIterator
706 /// then this is the chamber number. In all other cases this parameter is
708 /// @param y If type == kDigitIteratorByChamberAndCathode then this parameter is the
709 /// cathode number. In all other cases this parameter is
712 if (type == fCurrentIteratorType and fDataX == x and fDataY == y)
715 if (fCurrentEvent == -1)
717 AliError("No event was selected. Try first using GetEvent().");
725 case kDigitIteratorByDetectorElement:
728 AliMUONVDigitStore* store = DigitStore(fCurrentEvent);
729 if (store == 0x0) return 0x0;
730 fIterator = store->CreateIterator(detElem, detElem, 2);
731 if (fIterator == 0x0) return 0x0;
732 fCurrentIteratorType = kDigitIteratorByDetectorElement;
737 case kDigitIteratorByChamberAndCathode:
741 if (chamber < 0 or AliMpConstants::NofChambers() <= chamber)
744 "Must have give a chamber value in the range [0..%d], but got a value of: %d",
745 AliMpConstants::NofChambers() - 1,
750 if (cathode < 0 or 1 < cathode)
752 AliError(Form("Must have give a cathode value in the range [0..1], but got a value of: %d", cathode));
756 AliMUONVDigitStore* store = DigitStore(fCurrentEvent);
757 if (store == 0x0) return 0x0;
758 AliMpIntPair pair = AliMpDEManager::GetDetElemIdRange(chamber);
759 fIterator = store->CreateIterator(pair.GetFirst(), pair.GetSecond(), cathode);
760 if (fIterator == 0x0) return 0x0;
761 fCurrentIteratorType = kDigitIteratorByChamberAndCathode;
767 case kRawClusterIterator:
770 AliMUONVClusterStore* store = ClusterStore(fCurrentEvent);
771 if (store == 0x0) return 0x0;
772 fIterator = store->CreateChamberIterator(chamber, chamber);
773 if (fIterator == 0x0) return 0x0;
774 fCurrentIteratorType = kRawClusterIterator;
779 case kLocalTriggerIterator:
781 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
782 if (store == 0x0) return 0x0;
783 fIterator = store->CreateLocalIterator();
784 if (fIterator == 0x0) return 0x0;
785 fCurrentIteratorType = kLocalTriggerIterator;
789 case kRegionalTriggerIterator:
791 AliMUONVTriggerStore* store = TriggerStore(fCurrentEvent);
792 if (store == 0x0) return 0x0;
793 fIterator = store->CreateRegionalIterator();
794 if (fIterator == 0x0) return 0x0;
795 fCurrentIteratorType = kRegionalTriggerIterator;
804 //_____________________________________________________________________________
805 void AliMUONDataInterface::ResetIterator()
807 /// The temporary iterator object is deleted if it exists and the pointer reset to 0x0.
808 /// The iterator type and temporary data indicating the state of the iterator are
811 if (fIterator != 0x0) delete fIterator;
812 fCurrentIteratorType = kNoIterator;
813 fCurrentIndex = fDataX = fDataY = -1;
817 //_____________________________________________________________________________
818 Int_t AliMUONDataInterface::CountObjects(TIterator* iter)
820 /// Counts the number of objects in the iterator and resets it.
821 /// @return The number of objects in 'iter'.
823 if (iter == 0x0) return -1;
826 while ( iter->Next() != 0x0 ) count++;
832 //_____________________________________________________________________________
833 TObject* AliMUONDataInterface::FetchObject(TIterator* iter, Int_t index)
835 /// Fetches the index'th object from the iterator counting the first object
836 /// returned by iterator after it is reset as index == 0. The next object
837 /// has index == 1 and so on where the last object returned by the iterator
838 /// has index == N-1 where N = CountObjects(iter)
839 /// This method will only reset the iterator if index is smaller than
840 /// fCurrentIndex, which is used to track the iteration progress and is
841 /// updated when a new object if returned by this method.
842 /// @param iter The iterator to fetch an object from.
843 /// @param index The index number of the object to fetch in the range [0 .. N-1]
844 /// where N = CountObjects(iter)
848 AliError(Form("Index is out of bounds. Got a value of %d.", index));
852 if (iter == 0x0) return 0x0;
853 if (index <= fCurrentIndex)
859 TObject* object = 0x0;
860 while (fCurrentIndex < index)
862 object = iter->Next();
865 AliError(Form("Index is out of bounds. Got a value of %d.", index));