]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpExMap.cxx
Move setting of cluster in array after the cluster has been modified with distance...
[u/mrichter/AliRoot.git] / MUON / mapping / AliMpExMap.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 // $MpId: AliMpExMap.cxx,v 1.5 2006/05/24 13:58:29 ivana Exp $
18 // Category: basic
19
20 //-----------------------------------------------------------------------------
21 // Class AliMpExMap
22 // ------------------------
23 // Helper class making Root persistent TExMap
24 // Author:Ivana Hrivnacova; IPN Orsay
25 //-----------------------------------------------------------------------------
26
27 #include "AliMpExMap.h"
28 #include "AliMpExMapIterator.h"
29
30 #include "AliLog.h"
31
32 #include <TClass.h>
33 #include <TString.h>
34 #include <Riostream.h>
35
36 #include <stdlib.h>
37
38 /// \cond CLASSIMP
39 ClassImp(AliMpExMap)
40 /// \endcond
41
42 //
43 // static members
44 //
45
46 const Int_t   AliMpExMap::fgkDefaultSize = 300;
47 const Bool_t  AliMpExMap::fgkDefaultOwnership = true;
48
49 const Int_t AliMpExMap::fgkSeparator1 = 10000;
50 const Int_t AliMpExMap::fgkSeparator2 = 100;
51
52 //
53 // static methods
54 //
55
56 //_____________________________________________________________________________
57 const TString&  AliMpExMap::GetCharacterMap()
58 {
59   /// Return the string mapping characters to integers
60   static const TString kCharacterMap 
61     = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
62   return kCharacterMap;  
63 }
64
65 //_____________________________________________________________________________
66 Long_t  AliMpExMap::GetIndex(const TString& s)
67 {
68 /// Convert the TString to integer.
69
70   if (s.Length() > 5) {
71     AliErrorClass("String too long.");
72     return -1;
73   }  
74
75   Long_t index = 0;
76   for (Int_t i=s.Length()-1; i>=0; --i)  
77     index = index*fgkSeparator2 + GetCharacterMap().First(s(i));
78   
79   return index;
80 }
81
82 //_____________________________________________________________________________
83 TString  AliMpExMap::GetString(Long_t index)
84 {
85 /// Convert the integer index to the string.
86
87   TString s;
88   while (index >0) {
89     Char_t c = GetCharacterMap()(index%fgkSeparator2);
90     s += c;
91     index = index/fgkSeparator2;
92   }
93   return s;
94 }
95
96 //
97 // constructors/destructor
98 //
99
100 //_____________________________________________________________________________
101 AliMpExMap::AliMpExMap() 
102   : TObject(),
103     fMap(fgkDefaultSize),
104     fObjects(fgkDefaultSize),
105     fKeys(fgkDefaultSize)
106 {
107       /// Default constructor
108
109   fObjects.SetOwner(fgkDefaultOwnership);
110 }
111
112 //_____________________________________________________________________________
113 AliMpExMap::AliMpExMap(TRootIOCtor*) 
114   : TObject(),
115     fMap(),
116     fObjects(),
117     fKeys()
118 {
119       /// "Root - I/O" constructor
120 }
121
122
123 //_____________________________________________________________________________
124 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
125   : TObject(),
126     fMap(),
127     fObjects(),
128     fKeys()
129
130 {
131   /// Copy ctor
132   rhs.Copy(*this);
133 }
134
135 //_____________________________________________________________________________
136 AliMpExMap&
137 AliMpExMap::operator=(const AliMpExMap& rhs)
138 {
139   /// Assignment operator
140
141   // check assignment to self
142   if (this == &rhs) return *this;
143
144   rhs.Copy(*this);
145   return *this;
146 }
147
148 //_____________________________________________________________________________
149 AliMpExMap::~AliMpExMap() 
150 {
151 /// Destructor 
152 }
153
154 //
155 // private static methods
156 //
157
158 //______________________________________________________________________________
159 Long_t  AliMpExMap::GetIndex(Int_t first, Int_t second)
160 {
161 /// Convert the pair of integers to integer.
162
163   if ( first >= 0xFFFF || second >= 0xFFFF ) 
164   {
165     AliFatalClass("Index out of limit");
166     return 0;
167   }
168   
169   return 1 + ( first | ( second << 16 ) );
170            
171 //  if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
172 //    AliFatalClass("Index out of limit.");
173 //    exit(1); 
174 //  }  
175 //      
176 //  return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
177 }  
178
179 //______________________________________________________________________________
180 Int_t  AliMpExMap::GetPairFirst(Long_t index) 
181 {
182 /// Return first integer from index (encoded pair)
183
184   return (index-1) & 0xFFFF ;
185 }  
186
187 //______________________________________________________________________________
188 Int_t  AliMpExMap::GetPairSecond(Long_t index)
189 {
190 /// Return second integer from index (encoded pair)
191
192   return ( (index-1) & 0xFFFF0000 ) >> 16 ;
193 }  
194
195 //
196 // private methods
197 //
198
199 //_____________________________________________________________________________
200 void AliMpExMap::FillMap()
201 {
202 /// Fill transient map from the arrays of objects and keys
203
204   for (Int_t i=0; i<fObjects.GetEntriesFast(); i++) 
205     fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i)); 
206 }
207
208 //_____________________________________________________________________________
209 void AliMpExMap::AddKey(Long_t key)
210 {
211 /// Add key in array with checking size
212
213   // Resize array if needed
214   if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
215    fKeys.Set(2*fKeys.GetSize());
216    AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
217   } 
218    
219   fKeys.AddAt(key, fObjects.GetEntriesFast());      
220 }
221
222 //_____________________________________________________________________________
223 void
224 AliMpExMap::Copy(TObject& dest) const
225 {
226   /// Copy this to dest
227   /// Copy implies that dest will become owner of its objects, whatever
228   /// the ownership of (*this) is.
229   
230   AliDebug(1,"");
231   
232   TObject::Copy(dest);
233   AliMpExMap& m = static_cast<AliMpExMap&>(dest);
234   m.fKeys = fKeys;
235   m.fMap.Delete();
236   m.fObjects.Clear();
237   
238   for ( Int_t i = 0; i <= fObjects.GetLast(); ++i ) 
239   {
240     TObject* o = fObjects.At(i)->Clone();
241     if (!o)
242     {
243       AliError("Object was not cloned properly ! Please investigate...");
244     }
245     m.fObjects.AddLast(o);
246   }
247   m.FillMap();
248   m.fObjects.SetOwner(kTRUE);
249 }
250
251 //
252 // public methods
253 //
254
255 //_____________________________________________________________________________
256 void AliMpExMap::Clear(Option_t* option)
257 {
258 /// Clear memory
259
260   fMap.Delete();
261   fObjects.Clear(option);
262   fKeys.Reset();
263 }
264
265 //_____________________________________________________________________________
266 void AliMpExMap::Print(Option_t* opt) const
267 {
268 /// Print out
269
270   cout << Form("fMap size/capacity %d/%d",fMap.GetSize(),fMap.Capacity()) 
271        << Form(" fObjects.GetSize/Entries %d/%d",fObjects.GetSize(),fObjects.GetEntries()) 
272        << Form(" fKeys.GetSize %d",fKeys.GetSize()) << endl;
273   
274   TString sopt(opt);
275   sopt.ToUpper();
276   
277   if ( sopt.Contains("FULL") ) 
278   {
279     TIter next(CreateIterator());
280     TObject* o;
281     while ( ( o = next() ) )
282     {
283       o->Print();
284     }
285   }
286 }
287
288 //_____________________________________________________________________________
289 void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
290 {
291 /// Add object with its key to the map and arrays
292   
293   fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
294   AddKey(GetIndex(keyFirst, keySecond));
295   fObjects.Add(object);
296 }
297
298 //_____________________________________________________________________________
299 void AliMpExMap::Add(const TString& key, TObject* object)
300 {
301 /// Add object with its key to the map and arrays
302   
303   fMap.Add(GetIndex(key), (Long_t)object);
304   AddKey(GetIndex(key));
305   fObjects.Add(object);
306 }
307
308 //_____________________________________________________________________________
309 void AliMpExMap::Add(Int_t key, TObject* object)
310 {
311 /// Add object with its key to the map and arrays
312   
313   fMap.Add(key, (Long_t)object);
314   AddKey(key);
315   fObjects.Add(object);
316 }
317
318 //_____________________________________________________________________________
319 void AliMpExMap::SetSize(Int_t size)
320 {
321 /// Set given size to the key array
322
323   // fMap.Set(size);
324   // fObjects.Set(size);
325   fKeys.Set(size);
326
327
328 //_____________________________________________________________________________
329 void AliMpExMap::SetOwner(Bool_t owner)
330 {
331 /// Set given ownership to object array
332
333   fObjects.SetOwner(owner);
334 }  
335
336 //_____________________________________________________________________________
337 Int_t AliMpExMap::GetSize() const
338 {
339 /// Return the map size
340
341   return fObjects.GetEntriesFast();
342 }
343
344 //_____________________________________________________________________________
345 Int_t AliMpExMap::GetCapacity() const
346 {
347   /// Return the map capacity
348   
349   return fObjects.GetSize();
350 }
351
352 //_____________________________________________________________________________
353 AliMpExMapIterator*
354 AliMpExMap::CreateIterator() const
355 {
356 /// Return iterator set to the beginning of the map
357
358   return new AliMpExMapIterator(*this);
359 }
360
361 //_____________________________________________________________________________
362 TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
363 {
364 /// Return the object associated with the given key if found,
365 /// otherwise return 0
366
367   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
368 }
369
370 //_____________________________________________________________________________
371 TObject*  AliMpExMap::GetValue(const TString& key) const
372 {
373 /// Return the object associated with the given key if found,
374 /// otherwise return 0
375
376   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
377 }
378
379 //_____________________________________________________________________________
380 TObject*  AliMpExMap::GetValue(Int_t key) const
381 {
382 /// Return the object associated with the given key if found,
383 /// otherwise return 0
384
385   return reinterpret_cast<TObject*>(fMap.GetValue(key));
386 }
387
388 //_____________________________________________________________________________
389 void AliMpExMap::Streamer(TBuffer &R__b)
390 {
391 // Customized streamer                                                     \n
392 // After the arrays are read, fill the transient map
393
394   if (R__b.IsReading()) {
395     AliMpExMap::Class()->ReadBuffer(R__b, this);
396     FillMap();
397   } 
398   else {
399     AliMpExMap::Class()->WriteBuffer(R__b, this);
400   }
401 }