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