]> git.uio.no Git - u/mrichter/AliRoot.git/blobdiff - MUON/mapping/AliMpDEIterator.cxx
No need to be a singleton
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpDEIterator.cxx
index 5dd4cdda9dd59def50c121b1b1d5053bf39f24ef..bf2aa0ded3fbc0d357dd79eb89f98e82b61e89a8 100644 (file)
 // $MpId: AliMpDEIterator.cxx,v 1.6 2006/05/24 13:58:34 ivana Exp $
 // Category: management
 
-// ------------------------
+//-----------------------------------------------------------------------------
 // Class AliMpDEIterator
 // ------------------------
 // The iterator over valid detection elements
 // Author: Ivana Hrivnacova, IPN Orsay
+//-----------------------------------------------------------------------------
 
 #include "AliMpDEIterator.h"
+
+#include "AliMpExMapIterator.h"
 #include "AliMpDEStore.h"
 #include "AliMpDetElement.h"
 #include "AliMpDEManager.h"
@@ -40,59 +43,21 @@ ClassImp(AliMpDEIterator)
 
 //______________________________________________________________________________
 AliMpDEIterator::AliMpDEIterator()
-    : TObject(),
-      fDEStore(AliMpDEStore::Instance()),
-      fIndex(-1),
-      fChamberId(-1)
+: TObject(),
+  fCurrentDE(0x0),
+  fIterator(AliMpDEStore::Instance()->fDetElements.CreateIterator()),
+  fChamberId(-1)
 {  
 /// Standard and default constructor
 }
 
-//______________________________________________________________________________
-AliMpDEIterator::AliMpDEIterator(const AliMpDEIterator& rhs)
- : TObject(rhs),
-   fDEStore(rhs.fDEStore),
-   fIndex(rhs.fIndex),
-   fChamberId(rhs.fChamberId)
-{
-/// Copy constructor
-}
-
 //______________________________________________________________________________
 
 AliMpDEIterator::~AliMpDEIterator()
 {
 /// Destructor
-}
-
-//______________________________________________________________________________
-AliMpDEIterator&  AliMpDEIterator::operator=(const AliMpDEIterator& rhs)
-{
-/// Assignement operator
-
-  // check assignment to self
-  if (this == &rhs) return *this;
-
-  // base class assignment
-  TObject::operator=(rhs);
-
-  fDEStore = rhs.fDEStore;
-  fIndex = rhs.fIndex;
-  fChamberId = rhs.fChamberId;
-
-  return *this;
-} 
-
-//
-// private methods
-//
-
-//______________________________________________________________________________
-AliMpDetElement*  AliMpDEIterator::GetDetElement(Int_t index) const
-{
-/// Return the detection element from the map via index
 
-  return static_cast<AliMpDetElement*>(fDEStore->fDetElements.GetObject(index));
+  delete fIterator;
 }
 
 //
@@ -104,7 +69,8 @@ void AliMpDEIterator::First()
 {
 /// Set iterator to the first DE Id defined 
 
-  fIndex = 0;
+  fIterator->Reset();
+  fCurrentDE = static_cast<AliMpDetElement*>(fIterator->Next());
   fChamberId = -1;
 }  
 
@@ -113,29 +79,22 @@ void AliMpDEIterator::First(Int_t chamberId)
 {
 /// Reset the iterator, so that it points to the first DE
  
-  fChamberId = -1;
-  fIndex = -1;  
   if ( ! AliMpDEManager::IsValidChamberId(chamberId) ) {
     AliErrorStream() << "Invalid chamber Id " << chamberId << endl;
+    fIterator->Reset();
+    fChamberId = -1;    
+    fCurrentDE = 0x0;
     return;
   }    
 
-  Int_t i=0;
-  while ( i < fDEStore->fDetElements.GetSize() && fChamberId < 0 ) {
-    Int_t detElemId = GetDetElement(i)->GetId();
-    if ( AliMpDEManager::GetChamberId(detElemId) == chamberId ) {
-      fChamberId = chamberId;
-      fIndex = i;
-    } 
-    i++; 
+  fIterator->Reset();
+  fChamberId = -1;
+  while ( fChamberId != chamberId ) 
+  {
+    fCurrentDE = static_cast<AliMpDetElement*>(fIterator->Next());
+    if (!fCurrentDE) return;
+    fChamberId = AliMpDEManager::GetChamberId(CurrentDEId());
   }
-
-  if ( fChamberId < 0 ) {
-    AliErrorStream() 
-      << "No DEs of Chamber Id " << chamberId << " found" << endl;
-    return;
-  }    
-
 }
 
 //______________________________________________________________________________
@@ -143,14 +102,20 @@ void AliMpDEIterator::Next()
 {
 /// Increment iterator to next DE
 
-  fIndex++;
-
-  // Invalidate if at the end
-  if ( ( fIndex == fDEStore->fDetElements.GetSize() ) ||
-       ( fChamberId >= 0 &&    
-         AliMpDEManager::GetChamberId(CurrentDEId()) != fChamberId ) ) {
-    fIndex = -1;
-  }   
+  if ( fChamberId < 0 ) 
+  {
+    fCurrentDE = static_cast<AliMpDetElement*>(fIterator->Next());
+  }
+  else
+  {
+    fCurrentDE = static_cast<AliMpDetElement*>(fIterator->Next());
+    
+    while ( fCurrentDE && (AliMpDEManager::GetChamberId(fCurrentDE->GetId()) != fChamberId) )
+    {
+      fCurrentDE = static_cast<AliMpDetElement*>(fIterator->Next());
+      if (!fCurrentDE) return;
+    }
+  }
 }
 
 //______________________________________________________________________________
@@ -158,7 +123,7 @@ Bool_t AliMpDEIterator::IsDone() const
 {
 /// Is the iterator in the end?
 
-  return ( fIndex < 0 );
+  return ( fCurrentDE == 0x0 );
 }   
 
 //______________________________________________________________________________
@@ -166,22 +131,19 @@ AliMpDetElement* AliMpDEIterator::CurrentDE() const
 {
 /// Current DE Id
 
-  if ( ! IsDone() )
-    return GetDetElement(fIndex);
-  else {   
-    AliErrorStream()
-      << "Not in valid position - returning invalid DE." << endl;
-    return 0;
-  }  
+  return fCurrentDE;
 }
     
 //______________________________________________________________________________
-Int_t AliMpDEIterator::CurrentDEId() const
+Int_t 
+AliMpDEIterator::CurrentDEId() const
 {
 /// Current DE Id
 
-  if ( ! IsDone() )
-    return GetDetElement(fIndex)->GetId();
+  if ( fCurrentDE )
+  {
+    return fCurrentDE->GetId();
+  }
   else {   
     AliErrorStream()
       << "Not in valid position - returning invalid DE." << endl;