]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpExMap.cxx
Go from pointer to ifstream to ifstream.
[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 using std::cout;
39 using std::endl;
40 /// \cond CLASSIMP
41 ClassImp(AliMpExMap)
42 /// \endcond
43
44 //
45 // static members
46 //
47
48 const Int_t   AliMpExMap::fgkDefaultSize = 300;
49 const Bool_t  AliMpExMap::fgkDefaultOwnership = true;
50
51 const Int_t AliMpExMap::fgkSeparator1 = 10000;
52 const Int_t AliMpExMap::fgkSeparator2 = 100;
53
54 //
55 // static methods
56 //
57
58 //_____________________________________________________________________________
59 const TString&  AliMpExMap::GetCharacterMap()
60 {
61   /// Return the string mapping characters to integers
62   static const TString kCharacterMap 
63     = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";
64   return kCharacterMap;  
65 }
66
67 //_____________________________________________________________________________
68 Long_t  AliMpExMap::GetIndex(const TString& s)
69 {
70 /// Convert the TString to integer.
71
72   if (s.Length() > 5) {
73     AliErrorClass("String too long.");
74     return -1;
75   }  
76
77   Long_t index = 0;
78   for (Int_t i=s.Length()-1; i>=0; --i)  
79     index = index*fgkSeparator2 + GetCharacterMap().First(s(i));
80   
81   return index;
82 }
83
84 //_____________________________________________________________________________
85 TString  AliMpExMap::GetString(Long_t index)
86 {
87 /// Convert the integer index to the string.
88
89   TString s;
90   while (index >0) {
91     Char_t c = GetCharacterMap()(index%fgkSeparator2);
92     s += c;
93     index = index/fgkSeparator2;
94   }
95   return s;
96 }
97
98 //
99 // constructors/destructor
100 //
101
102 //_____________________________________________________________________________
103 AliMpExMap::AliMpExMap() 
104   : TObject(),
105     fMap(fgkDefaultSize),
106     fObjects(fgkDefaultSize),
107     fKeys(fgkDefaultSize)
108 {
109       /// Default constructor
110
111   fObjects.SetOwner(fgkDefaultOwnership);
112 }
113
114 //_____________________________________________________________________________
115 AliMpExMap::AliMpExMap(TRootIOCtor*) 
116   : TObject(),
117     fMap(),
118     fObjects(),
119     fKeys()
120 {
121       /// "Root - I/O" constructor
122 }
123
124
125 //_____________________________________________________________________________
126 AliMpExMap::AliMpExMap(const AliMpExMap& rhs)
127   : TObject(),
128     fMap(),
129     fObjects(),
130     fKeys()
131
132 {
133   /// Copy ctor
134   rhs.Copy(*this);
135 }
136
137 //_____________________________________________________________________________
138 AliMpExMap&
139 AliMpExMap::operator=(const AliMpExMap& rhs)
140 {
141   /// Assignment operator
142
143   // check assignment to self
144   if (this == &rhs) return *this;
145
146   rhs.Copy(*this);
147   return *this;
148 }
149
150 //_____________________________________________________________________________
151 AliMpExMap::~AliMpExMap() 
152 {
153 /// Destructor 
154 }
155
156 //
157 // private static methods
158 //
159
160 //______________________________________________________________________________
161 Long_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 //______________________________________________________________________________
182 Int_t  AliMpExMap::GetPairFirst(Long_t index) 
183 {
184 /// Return first integer from index (encoded pair)
185
186   return (index-1) & 0xFFFF ;
187 }  
188
189 //______________________________________________________________________________
190 Int_t  AliMpExMap::GetPairSecond(Long_t index)
191 {
192 /// Return second integer from index (encoded pair)
193
194   return ( (index-1) & 0xFFFF0000 ) >> 16 ;
195 }  
196
197 //
198 // private methods
199 //
200
201 //_____________________________________________________________________________
202 void 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 //_____________________________________________________________________________
211 void 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());
218    AliDebugStream(1) << "AliMpExMap::AddKey: resized Key array " << endl;
219   } 
220    
221   fKeys.AddAt(key, fObjects.GetEntriesFast());      
222 }
223
224 //_____________________________________________________________________________
225 void
226 AliMpExMap::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
253 //
254 // public methods
255 //
256
257 //_____________________________________________________________________________
258 void AliMpExMap::Clear(Option_t* option)
259 {
260 /// Clear memory
261
262   fMap.Delete();
263   fObjects.Clear(option);
264   fKeys.Reset();
265 }
266
267 //_____________________________________________________________________________
268 void 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
290 //_____________________________________________________________________________
291 void AliMpExMap::Add(Int_t keyFirst, Int_t keySecond, TObject* object)
292 {
293 /// Add object with its key to the map and arrays
294   
295   fMap.Add(GetIndex(keyFirst, keySecond), (Long_t)object);
296   AddKey(GetIndex(keyFirst, keySecond));
297   fObjects.Add(object);
298 }
299
300 //_____________________________________________________________________________
301 void 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 //_____________________________________________________________________________
311 void 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 //_____________________________________________________________________________
321 void 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 //_____________________________________________________________________________
331 void AliMpExMap::SetOwner(Bool_t owner)
332 {
333 /// Set given ownership to object array
334
335   fObjects.SetOwner(owner);
336 }  
337
338 //_____________________________________________________________________________
339 Int_t AliMpExMap::GetSize() const
340 {
341 /// Return the map size
342
343   return fObjects.GetEntriesFast();
344 }
345
346 //_____________________________________________________________________________
347 Int_t AliMpExMap::GetCapacity() const
348 {
349   /// Return the map capacity
350   
351   return fObjects.GetSize();
352 }
353
354 //_____________________________________________________________________________
355 AliMpExMapIterator*
356 AliMpExMap::CreateIterator() const
357 {
358 /// Return iterator set to the beginning of the map
359
360   return new AliMpExMapIterator(*this);
361 }
362
363 //_____________________________________________________________________________
364 TObject* AliMpExMap::GetValue(Int_t keyFirst, Int_t keySecond) const
365 {
366 /// Return the object associated with the given key if found,
367 /// otherwise return 0
368
369   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(keyFirst, keySecond)));
370 }
371
372 //_____________________________________________________________________________
373 TObject*  AliMpExMap::GetValue(const TString& key) const
374 {
375 /// Return the object associated with the given key if found,
376 /// otherwise return 0
377
378   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
379 }
380
381 //_____________________________________________________________________________
382 TObject*  AliMpExMap::GetValue(Int_t key) const
383 {
384 /// Return the object associated with the given key if found,
385 /// otherwise return 0
386
387   return reinterpret_cast<TObject*>(fMap.GetValue(key));
388 }
389
390 //_____________________________________________________________________________
391 void AliMpExMap::Streamer(TBuffer &R__b)
392 {
393 // Customized streamer                                                     \n
394 // After the arrays are read, fill the transient map
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 }