]> git.uio.no Git - u/mrichter/AliRoot.git/blob - FMD/AliFMDMap.h
Removing memory leak
[u/mrichter/AliRoot.git] / FMD / AliFMDMap.h
1 #ifndef ALIFMDMAP_H
2 #define ALIFMDMAP_H
3 /* Copyright(c) 1998-2000, ALICE Experiment at CERN, All rights
4  * reserved. 
5  *
6  * See cxx source for full Copyright notice                               
7  */
8 #ifndef ROOT_TObject
9 # include <TObject.h>
10 #endif 
11 //____________________________________________________________________
12 //
13 // Base class for caches of per-strip information.
14 //
15
16 class AliFMDMap : public TObject 
17 {
18 public:
19   enum { 
20     kMaxDetectors = 3, 
21     kMaxRings     = 2, 
22     kMaxSectors   = 20, 
23     kMaxStrips    = 512
24   };
25   AliFMDMap(size_t maxDet = kMaxDetectors, 
26             size_t maxRing= kMaxRings, 
27             size_t maxSec = kMaxSectors, 
28             size_t maxStr = kMaxStrips);
29   virtual ~AliFMDMap() {}
30 protected:
31   size_t CalcIndex(size_t det, Char_t ring, size_t sec, size_t str) const;
32   size_t fMaxDetectors;             // Maximum # of detectors
33   size_t fMaxRings;                 // Maximum # of rings
34   size_t fMaxSectors;               // Maximum # of sectors
35   size_t fMaxStrips;                // Maximum # of strips
36   ClassDef(AliFMDMap, 1) // Cache of per strip information
37 };
38
39 #ifdef  MAY_USE_TEMPLATES
40 #ifndef __VECTOR__
41 # include <vector>
42 #endif 
43 //____________________________________________________________________
44 //
45 // Class template for classes that cache per strip information.  
46 // Access to the data goes via 
47 //
48 //   Type& AliFMDMap<Type>::operator()(size_t detector,
49 //                                     Char_t ring, 
50 //                                     size_t sector,
51 //                                     size_t strip);
52 // 
53 // (as well as a const version of this member function). 
54 // The elements can be reset to the default value by calling 
55 // AliFMDMap<Type>::Clear().  This resets the values to `Type()'. 
56 //
57 template <typename Type> 
58 class AliFMDMap : public TObject 
59 {
60 public:
61   AliFMDMap(size_t maxDet=3, size_t maxRing=2, size_t maxSec=40, 
62             size_t maxStr=512);
63   virtual ~AliFMDMap() {}
64   void Clear(const Type& val=Type());
65   Type& operator()(size_t det, Char_t ring, size_t sec, size_t str);
66   const Type& operator()(size_t det, Char_t ring, size_t sec, size_t str)const;
67 private:
68   typedef std::vector<Type> ValueVector; // Type of container
69   ValueVector fValues;                   // Contained values
70   size_t      fMaxDetectors;             // Maximum # of detectors
71   size_t      fMaxRings;                 // Maximum # of rings
72   size_t      fMaxSectors;               // Maximum # of sectors
73   size_t      fMaxStrips;                // Maximum # of strips
74   
75   size_t CalcIndex(size_t det, Char_t ring, size_t sec, size_t str) const;
76   ClassDef(AliFMDMap, 0); // Map of FMD index's to values 
77 };
78
79
80 //____________________________________________________________________
81 template <typename Type>
82 inline 
83 AliFMDMap<Type>::AliFMDMap(size_t maxDet, 
84                            size_t maxRing, 
85                            size_t maxSec, 
86                            size_t maxStr)
87   : fValues(maxDet * maxRing * maxSec * maxStr), 
88     fMaxDetectors(maxDet), 
89     fMaxRings(maxRing), 
90     fMaxSectors(maxSec), 
91     fMaxStrips(maxStr)
92 {
93   // Construct a map
94   //
95   // Parameters:
96   //     maxDet       Maximum # of detectors
97   //     maxRinf      Maximum # of rings
98   //     maxSec       Maximum # of sectors
99   //     maxStr       Maximum # of strips
100 }
101
102
103 //____________________________________________________________________
104 template <typename Type>
105 inline size_t 
106 AliFMDMap<Type>::CalcIndex(size_t det, Char_t ring, size_t sec, size_t str) const
107 {
108   // Calculate index into storage from arguments. 
109   // 
110   // Parameters: 
111   //     det       Detector #
112   //     ring      Ring ID
113   //     sec       Sector # 
114   //     str       Strip # 
115   //
116   // Returns appropriate index into storage 
117   //
118   size_t ringi = (ring == 'I' ||  ring == 'i' ? 0 : 1);
119   size_t idx = 
120     (det + fMaxDetectors * (ringi + fMaxRings * (sec + fMaxSectors * str)));
121   if (idx >= fMaxDetectors * fMaxRings * fMaxSectors * fMaxStrips) {
122     Fatal("CalcIndex", "Index (%d,'%c',%d,%d) out of bounds, "
123           "in particular the %s index", 
124           det, ring, sec, str, 
125           (det >= fMaxDetectors ? "Detector" : 
126            (ringi >= fMaxRings ? "Ring" : 
127             (sec >= fMaxSectors ? "Sector" : "Strip"))));
128     return 0;
129   }
130   return idx;
131 }
132
133 //____________________________________________________________________
134 template <typename Type>
135 inline void
136 AliFMDMap<Type>::Clear(const Type& val) 
137 {
138   // Resets stored values to the default value for that type 
139   for (size_t i = 0; i < fValues.size(); ++i) fValues[i] = val;
140 }
141
142 //____________________________________________________________________
143 template <typename Type>
144 inline Type& 
145 AliFMDMap<Type>::operator()(size_t det, Char_t ring, size_t sec, size_t str)
146 {
147   // Parameters: 
148   //     det       Detector #
149   //     ring      Ring ID
150   //     sec       Sector # 
151   //     str       Strip # 
152   //
153   // Returns data[det][ring][sec][str]
154   return fValues[CalcIndex(det, ring, sec, str)];
155 }
156
157 //____________________________________________________________________
158 template <typename Type>
159 inline const Type& 
160 AliFMDMap<Type>::operator()(size_t det, 
161                             Char_t ring, 
162                             size_t sec, 
163                             size_t str) const
164 {
165   // Parameters: 
166   //     det       Detector #
167   //     ring      Ring ID
168   //     sec       Sector # 
169   //     str       Strip # 
170   //
171   // Returns data[det][ring][sec][str]
172   return fValues[CalcIndex(det, ring, sec, str)];
173 }
174
175
176 //____________________________________________________________________
177 // 
178 // Some specialisations 
179 //
180 typedef AliFMDMap<UShort_t> AliFMDAdcMap;
181 typedef AliFMDMap<std::pair<Float_t, UShort_t> > AliFMDEdepMap;
182
183 #endif
184 #endif 
185 //____________________________________________________________________
186 //
187 // Local Variables:
188 //   mode: C++
189 // End:
190 //
191 // EOF
192 //
193
194