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