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