]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/AliMUON2DMap.cxx
fc3a4771c9ee961ace0bdce9af7fda7f65f957eb
[u/mrichter/AliRoot.git] / MUON / AliMUON2DMap.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 #include "AliMUON2DMap.h"
19
20 #include "AliLog.h"
21 #include "AliMUON2DMapIterator.h"
22 #include "AliMUON2DMapIteratorByI.h"
23 #include "AliMpExMap.h"
24
25 //-----------------------------------------------------------------------------
26 /// \class AliMUON2DMap
27 /// Basic implementation of AliMUONVStore container using
28 /// AliMpExMap internally.
29 /// What we store is a "double" map : an AliMpExMap of AliMpExMaps
30 ///
31 /// \author Laurent Aphecetche
32 //-----------------------------------------------------------------------------
33
34 /// \cond CLASSIMP
35 ClassImp(AliMUON2DMap)
36 /// \endcond
37
38 const Int_t AliMUON2DMap::fgkOptimalSizeForDEManu = 228;
39
40 namespace
41 {
42   //___________________________________________________________________________
43   TObject* GetValue(TExMapIter& iter, Int_t& theKey) 
44   {
45     /// return the next value corresponding to theKey in iterator iter
46     theKey = -1;
47     Long_t key, value;
48     Bool_t ok = iter.Next(key,value);
49     if (!ok) return 0x0;
50     theKey = (Int_t)(key & 0xFFFF);
51     return reinterpret_cast<TObject*>(value);
52   }
53 }
54
55 //_____________________________________________________________________________
56 AliMUON2DMap::AliMUON2DMap(Bool_t optimizeForDEManu) 
57 : AliMUONVStore(), 
58   fMap(new AliMpExMap(kTRUE)),
59   fOptimizeForDEManu(optimizeForDEManu)
60 {
61   /// Default constructor.
62   // hard-coded constant in order not to depend on mapping
63   // if this number ever change, it will not break the code, simply the
64   // automatic resizing will give a warning...
65   if ( fOptimizeForDEManu ) fMap->SetSize(fgkOptimalSizeForDEManu); 
66 }
67
68 //_____________________________________________________________________________
69 AliMUON2DMap::AliMUON2DMap(const AliMUON2DMap& other)
70 : AliMUONVStore(),
71   fMap(new AliMpExMap(*other.fMap)),
72   fOptimizeForDEManu(other.fOptimizeForDEManu)
73 {
74  /// Copy constructor.
75 }
76
77 //_____________________________________________________________________________
78 AliMUON2DMap&
79 AliMUON2DMap::operator=(const AliMUON2DMap& other)
80 {
81 /// Assignment operator
82   *fMap = *other.fMap;
83   fOptimizeForDEManu = other.fOptimizeForDEManu;
84   return *this;
85 }
86
87 //_____________________________________________________________________________
88 AliMUON2DMap::~AliMUON2DMap()
89 {
90 /// Destructor. 
91 /// We delete the map, which will delete the objects, as we're owner.
92   delete fMap;
93 }
94
95 //_____________________________________________________________________________
96 AliMUONVStore*
97 AliMUON2DMap::Create() const
98 {
99   /// Create a void copy of *this. 
100   return new AliMUON2DMap(fOptimizeForDEManu);
101 }
102
103 //_____________________________________________________________________________
104 Bool_t
105 AliMUON2DMap::Add(TObject* object)
106 {
107   /// Add object, using the decoding of uniqueID into two ints as the key
108   if (!object) return kFALSE;
109   UInt_t uniqueID = object->GetUniqueID();
110   Int_t j = ( uniqueID & 0xFFFF0000 ) >> 16;
111   Int_t i = ( uniqueID & 0xFFFF);
112   return Set(i,j,object,kFALSE);
113 }
114
115 //_____________________________________________________________________________
116 TObject* 
117 AliMUON2DMap::FindObject(Int_t i, Int_t j) const
118 {
119   /// Return the value at position (i,j).
120   AliMpExMap* m = static_cast<AliMpExMap*>(fMap->GetValue(i));
121   return m ? m->GetValue(j) : 0x0;
122 }
123
124 //_____________________________________________________________________________
125 TIterator*
126 AliMUON2DMap::CreateIterator() const
127 {
128   // Create and return an iterator on this map
129   // Returned iterator must be deleted by user.
130   return new AliMUON2DMapIterator(*fMap);
131 }
132
133 //_____________________________________________________________________________
134 TIterator*
135 AliMUON2DMap::CreateIterator(Int_t firstI, Int_t lastI) const
136 {
137   // Create and return an iterator on this map
138   // Returned iterator must be deleted by user.
139   return new AliMUON2DMapIteratorByI(*fMap,firstI,lastI);
140 }
141
142 //_____________________________________________________________________________
143 void 
144 AliMUON2DMap::Clear(Option_t*)
145 {
146   /// Clear memory
147   fMap->Clear();
148 }  
149
150 //_____________________________________________________________________________
151 Int_t 
152 AliMUON2DMap::GetSize() const
153 {
154   /// Return the number of objects we hold
155   TExMapIter iter(fMap->GetIterator());
156   Int_t i;
157   Int_t theSize(0);
158   
159   while ( GetValue(iter,i) ) 
160   {
161     theSize += GetSize(i);
162   }
163   return theSize;
164 }
165
166 //_____________________________________________________________________________
167 Int_t 
168 AliMUON2DMap::GetSize(Int_t i) const
169 {
170   /// Return the number of objects we hold
171   AliMpExMap* m = static_cast<AliMpExMap*>(fMap->GetValue(i));
172   return m ? m->GetSize() : 0;
173 }
174
175 //_____________________________________________________________________________
176 Bool_t 
177 AliMUON2DMap::Set(Int_t i, Int_t j, TObject* object, Bool_t replace)
178 {
179 /// Set the object at position (i,j).
180 /// If replace==kTRUE, we don't care if there's an object there already,
181 /// otherwise we might refuse to set if the (i,j) location is already
182 /// filled (in which case we return kFALSE).
183   
184   TObject* o = fMap->GetValue(i);
185   if ( !o )
186   {
187     AliMpExMap* m = new AliMpExMap(true);
188     if ( fOptimizeForDEManu ) 
189     {
190       m->SetSize(451); // same remark as for the SetSize in ctor...
191     }
192     fMap->Add(i,m);
193     o = fMap->GetValue(i);
194   }
195   AliMpExMap* m = static_cast<AliMpExMap*>(o);
196  
197   o = m->GetValue(j);
198   
199   if ( !o )
200   {
201     m->Add(j,object);
202   }
203   else 
204   {
205     if ( replace ) 
206     {
207       delete o;
208       m->Add(j,object);
209     }
210     else
211     {
212       AliError(Form("Object %p is already there for (i,j)=(%d,%d)",o,i,j));
213       return kFALSE;
214     }
215   }
216
217   return kTRUE;
218 }
219