]> git.uio.no Git - u/mrichter/AliRoot.git/blob - MUON/mapping/AliMpExMap.cxx
Generates realistic DDL sharing and buspatch number calculated from DDL (Christian)
[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 // Class AliMpExMap
21 // ------------------------
22 // Helper class making Root persistent TExMap
23 // Author:Ivana Hrivnacova; IPN Orsay
24
25 #include "AliMpExMap.h"
26 #include "AliMpIntPair.h"
27
28 #include "AliLog.h"
29
30 #include <TClass.h>
31 #include <TString.h>
32 #include <Riostream.h>
33
34 #include <stdlib.h>
35
36 /// \cond CLASSIMP
37 ClassImp(AliMpExMap)
38 /// \endcond
39
40 //
41 // static members
42 //
43
44 const Int_t   AliMpExMap::fgkDefaultSize = 300;
45 const Bool_t  AliMpExMap::fgkDefaultOwnership = true;
46
47 const Int_t AliMpExMap::fgkSeparator1 = 10000;
48 const Int_t AliMpExMap::fgkSeparator2 = 100;
49
50 const TString  AliMpExMap::fgkCharacterMap 
51   = " 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.";
52
53 //
54 // static methods
55 //
56
57 //______________________________________________________________________________
58 Long_t  AliMpExMap::GetIndex(const AliMpIntPair& pair)
59 {
60 /// Convert the pair of integers to integer.
61
62   if (pair.GetFirst() >= fgkSeparator1 || pair.GetSecond() >= fgkSeparator1) {
63     AliFatalClass("Index out of limit.");
64     exit(1); 
65   }  
66       
67   return pair.GetFirst()*fgkSeparator1 + pair.GetSecond() + 1;
68 }  
69
70 //_____________________________________________________________________________
71 Long_t  AliMpExMap::GetIndex(const TString& s)
72 {
73 /// Convert the TString to integer.
74
75   if (s.Length() > 5) {
76     AliFatalClass("String too long.");
77     return 0;
78   }  
79
80   Long_t index = 0;
81   for (Int_t i=s.Length()-1; i>=0; --i)  
82     index = index*fgkSeparator2 + fgkCharacterMap.First(s(i));
83   
84   return index;
85 }
86
87 //______________________________________________________________________________
88 AliMpIntPair  AliMpExMap::GetPair(Long_t index)
89 {
90 /// Convert the integer index to the pair of integers.
91
92   return AliMpIntPair((index-1)/fgkSeparator1,(index-1)%fgkSeparator1);
93 }  
94
95 //_____________________________________________________________________________
96 TString  AliMpExMap::GetString(Long_t index)
97 {
98 /// Convert the integer index to the string.
99
100   TString s;
101   while (index >0) {
102     Char_t c = fgkCharacterMap(index%fgkSeparator2);
103     s += c;
104     index = index/fgkSeparator2;
105   }
106   return s;
107 }
108
109 //
110 // constructors/destructor
111 //
112
113 //_____________________________________________________________________________
114 AliMpExMap::AliMpExMap(Bool_t /*standardConstructor*/) 
115   : TObject(),
116     fMap(fgkDefaultSize),
117     fObjects(fgkDefaultSize),
118     fKeys(fgkDefaultSize)
119 {
120 /// Standard constructor
121
122   fObjects.SetOwner(fgkDefaultOwnership);
123 }
124
125 //_____________________________________________________________________________
126 AliMpExMap::AliMpExMap() 
127   : TObject(),
128     fMap(),
129     fObjects(),
130     fKeys()
131 {
132 /// Default constructor
133 }
134
135 //_____________________________________________________________________________
136 AliMpExMap::~AliMpExMap() 
137 {
138 /// Destructor 
139 }
140
141 //
142 // private methods
143 //
144
145 //_____________________________________________________________________________
146 void AliMpExMap::FillMap()
147 {
148 /// Fill transient map from the arrays of objects and keys
149
150   for (Int_t i=0; i<fObjects.GetEntriesFast(); i++) 
151     fMap.Add(fKeys.At(i), (Long_t)fObjects.At(i)); 
152 }
153
154 //_____________________________________________________________________________
155 void AliMpExMap::AddKey(Long_t key)
156 {
157 /// Add key in array with checking size
158
159   // Resize array if needed
160   if (fObjects.GetEntriesFast() == fKeys.GetSize()) {
161    fKeys.Set(2*fKeys.GetSize());
162    AliWarningStream() << "AliMpExMap::AddKey: resized Key array " << endl;
163   } 
164    
165   fKeys.AddAt(key, fObjects.GetEntriesFast());      
166 }
167
168 //
169 // public methods
170 //
171
172 //_____________________________________________________________________________
173 void AliMpExMap::Add(const AliMpIntPair& key, TObject* object)
174 {
175 /// Add object with its key to the map and arrays
176   
177   fMap.Add(GetIndex(key), (Long_t)object);
178   AddKey(GetIndex(key));
179   fObjects.Add(object);
180 }
181
182 //_____________________________________________________________________________
183 void AliMpExMap::Add(const TString& key, TObject* object)
184 {
185 /// Add object with its key to the map and arrays
186   
187   fMap.Add(GetIndex(key), (Long_t)object);
188   AddKey(GetIndex(key));
189   fObjects.Add(object);
190 }
191
192 //_____________________________________________________________________________
193 void AliMpExMap::Add(Int_t key, TObject* object)
194 {
195 /// Add object with its key to the map and arrays
196   
197   fMap.Add(key, (Long_t)object);
198   AddKey(key);
199   fObjects.Add(object);
200 }
201
202 //_____________________________________________________________________________
203 void AliMpExMap::SetSize(Int_t size)
204 {
205 /// Set given size to the key array
206
207   // fMap.Set(size);
208   // fObjects.Set(size);
209   fKeys.Set(size);
210
211
212 //_____________________________________________________________________________
213 void AliMpExMap::SetOwner(Bool_t owner)
214 {
215 /// Set given ownership to object array
216
217   fObjects.SetOwner(owner);
218 }  
219
220 //_____________________________________________________________________________
221 Int_t AliMpExMap::GetSize() const
222 {
223 /// Return TExMap iterator set to the beginning of the map
224
225   return fObjects.GetEntriesFast();
226 }
227
228 //_____________________________________________________________________________
229 TExMapIter AliMpExMap::GetIterator() const
230 {
231 /// Return TExMap iterator set to the beginning of the map
232
233   return TExMapIter(&fMap);
234 }
235
236 //_____________________________________________________________________________
237 TObject* AliMpExMap::GetValue(const AliMpIntPair& key) const
238 {
239 /// Return the object associated with the given key if found,
240 /// otherwise return 0
241
242   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
243 }
244
245 //_____________________________________________________________________________
246 TObject*  AliMpExMap::GetValue(const TString& key) const
247 {
248 /// Return the object associated with the given key if found,
249 /// otherwise return 0
250
251   return reinterpret_cast<TObject*>(fMap.GetValue(GetIndex(key)));
252 }
253
254 //_____________________________________________________________________________
255 TObject*  AliMpExMap::GetValue(Int_t key) const
256 {
257 /// Return the object associated with the given key if found,
258 /// otherwise return 0
259
260   return reinterpret_cast<TObject*>(fMap.GetValue(key));
261 }
262
263 //_____________________________________________________________________________
264 void AliMpExMap::Streamer(TBuffer &R__b)
265 {
266 /// Customized streamer                                                     \n
267 /// After the arrays are read, fill the transient map
268
269   if (R__b.IsReading()) {
270     AliMpExMap::Class()->ReadBuffer(R__b, this);
271     FillMap();
272   } 
273   else {
274     AliMpExMap::Class()->WriteBuffer(R__b, this);
275   }
276 }