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