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