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