1 | #ifndef MARS_MArrayF
|
---|
2 | #define MARS_MArrayF
|
---|
3 |
|
---|
4 | #ifndef MARS_MArray
|
---|
5 | #include "MArray.h"
|
---|
6 | #endif
|
---|
7 |
|
---|
8 | #include <string.h>
|
---|
9 |
|
---|
10 | class TArrayF;
|
---|
11 | class MArrayF : public MArray
|
---|
12 | {
|
---|
13 | private:
|
---|
14 | Float_t *fArray; //[fN] Array of fN chars
|
---|
15 |
|
---|
16 | public:
|
---|
17 |
|
---|
18 | MArrayF()
|
---|
19 | {
|
---|
20 | fN = 0;
|
---|
21 | fArray = NULL;
|
---|
22 | }
|
---|
23 |
|
---|
24 | MArrayF(UInt_t n)
|
---|
25 | {
|
---|
26 | fN = 0;
|
---|
27 | fArray = NULL;
|
---|
28 | Set(n);
|
---|
29 | }
|
---|
30 |
|
---|
31 | MArrayF(UInt_t n, Float_t *array)
|
---|
32 | {
|
---|
33 | // Create TArrayC object and initialize it with values of array.
|
---|
34 | fN = 0;
|
---|
35 | fArray = NULL;
|
---|
36 | Set(n, array);
|
---|
37 | }
|
---|
38 |
|
---|
39 | MArrayF(const MArrayF &array) : MArray()
|
---|
40 | {
|
---|
41 | // Copy constructor.
|
---|
42 | fArray = NULL;
|
---|
43 | Set(array.fN, array.fArray);
|
---|
44 | }
|
---|
45 |
|
---|
46 | MArrayF &operator=(const MArrayF &rhs)
|
---|
47 | {
|
---|
48 | // TArrayC assignment operator.
|
---|
49 | if (this != &rhs)
|
---|
50 | Set(rhs.fN, rhs.fArray);
|
---|
51 | return *this;
|
---|
52 | }
|
---|
53 |
|
---|
54 | virtual ~MArrayF()
|
---|
55 | {
|
---|
56 | // Delete TArrayC object.
|
---|
57 | delete [] fArray;
|
---|
58 | fArray = NULL;
|
---|
59 | }
|
---|
60 |
|
---|
61 | void Adopt(UInt_t n, Float_t *array)
|
---|
62 | {
|
---|
63 | // Adopt array arr into TArrayC, i.e. don't copy arr but use it directly
|
---|
64 | // in TArrayC. User may not delete arr, TArrayC dtor will do it.
|
---|
65 | if (fArray)
|
---|
66 | delete [] fArray;
|
---|
67 |
|
---|
68 | fN = n;
|
---|
69 | fArray = array;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void AddAt(Float_t c, UInt_t i)
|
---|
73 | {
|
---|
74 | // Add char c at position i. Check for out of bounds.
|
---|
75 | fArray[i] = c;
|
---|
76 | }
|
---|
77 |
|
---|
78 | Float_t At(UInt_t i) const
|
---|
79 | {
|
---|
80 | return fArray[i];
|
---|
81 | }
|
---|
82 |
|
---|
83 | Float_t *GetArray() const
|
---|
84 | {
|
---|
85 | return fArray;
|
---|
86 | }
|
---|
87 |
|
---|
88 | void Reset()
|
---|
89 | {
|
---|
90 | memset(fArray, 0, fN*sizeof(Float_t));
|
---|
91 | }
|
---|
92 |
|
---|
93 | void Reset(Float_t v)
|
---|
94 | {
|
---|
95 | for (Float_t *f=fArray; f<fArray+fN; f++)
|
---|
96 | *f = v;
|
---|
97 | }
|
---|
98 |
|
---|
99 | void Set(UInt_t n)
|
---|
100 | {
|
---|
101 | // Set size of this array to n chars.
|
---|
102 | // A new array is created, the old contents copied to the new array,
|
---|
103 | // then the old array is deleted.
|
---|
104 |
|
---|
105 | if (n==fN)
|
---|
106 | return;
|
---|
107 |
|
---|
108 | Float_t *temp = fArray;
|
---|
109 | if (n == 0)
|
---|
110 | fArray = NULL;
|
---|
111 | else
|
---|
112 | {
|
---|
113 | fArray = new Float_t[n];
|
---|
114 | if (n < fN)
|
---|
115 | memcpy(fArray, temp, n*sizeof(Float_t));
|
---|
116 | else
|
---|
117 | {
|
---|
118 | memcpy(fArray, temp, fN*sizeof(Float_t));
|
---|
119 | memset(&fArray[fN], 0, (n-fN)*sizeof(Float_t));
|
---|
120 | }
|
---|
121 | }
|
---|
122 |
|
---|
123 | if (fN)
|
---|
124 | delete [] temp;
|
---|
125 |
|
---|
126 | fN = n;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void Set(UInt_t n, Float_t *array)
|
---|
130 | {
|
---|
131 | // Set size of this array to n chars and set the contents.
|
---|
132 | if (!array)
|
---|
133 | return;
|
---|
134 |
|
---|
135 | if (fArray && fN != n)
|
---|
136 | {
|
---|
137 | delete [] fArray;
|
---|
138 | fArray = 0;
|
---|
139 | }
|
---|
140 | fN = n;
|
---|
141 |
|
---|
142 | if (fN == 0)
|
---|
143 | return;
|
---|
144 |
|
---|
145 | if (!fArray)
|
---|
146 | fArray = new Float_t[fN];
|
---|
147 |
|
---|
148 | memcpy(fArray, array, n*sizeof(Float_t));
|
---|
149 | }
|
---|
150 |
|
---|
151 | Float_t &operator[](UInt_t i)
|
---|
152 | {
|
---|
153 | return fArray[i];
|
---|
154 | }
|
---|
155 | const Float_t &operator[](UInt_t i) const
|
---|
156 | {
|
---|
157 | return fArray[i];
|
---|
158 | }
|
---|
159 |
|
---|
160 | Double_t GetSum() const
|
---|
161 | {
|
---|
162 | const Float_t *end = fArray+fN;
|
---|
163 |
|
---|
164 | Double_t sum = 0;
|
---|
165 | for (Float_t *ptr=fArray; ptr<end; ptr++)
|
---|
166 | sum += *ptr;
|
---|
167 |
|
---|
168 | return sum;
|
---|
169 | }
|
---|
170 |
|
---|
171 | template<class T>
|
---|
172 | void Add(T *src, Int_t n, Int_t p=0, Float_t m=1.)
|
---|
173 | {
|
---|
174 | // under these 3 conditions, we return immediately, because they either make no sense,
|
---|
175 | // or result in an invariant array.
|
---|
176 | if (p >= fN)
|
---|
177 | {
|
---|
178 | // maybe throw a warning, because p >= fN makes no sense at all
|
---|
179 | return;
|
---|
180 | }
|
---|
181 | if ( n <= 0)
|
---|
182 | {
|
---|
183 | return;
|
---|
184 | }
|
---|
185 | if (m == 0.)
|
---|
186 | {
|
---|
187 | return;
|
---|
188 | }
|
---|
189 | if (src == NULL)
|
---|
190 | {
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | Float_t *dest = fArray + p;
|
---|
195 | Float_t *end = dest;
|
---|
196 | if (n < fN-p)
|
---|
197 | {
|
---|
198 | end += n;
|
---|
199 | }
|
---|
200 | else
|
---|
201 | {
|
---|
202 | end += fN-p;
|
---|
203 | }
|
---|
204 |
|
---|
205 | // we treat the case m==1. special, in order to speed up the code a bit
|
---|
206 | // since when m==1. the multiplication can be omitted and this should be a bit faster then.
|
---|
207 | if (m == 1.)
|
---|
208 | {
|
---|
209 | while (dest<end)
|
---|
210 | *dest++ += *src++;
|
---|
211 | }
|
---|
212 | else
|
---|
213 | {
|
---|
214 | while (dest<end)
|
---|
215 | *dest++ += (*src++) * m;
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | template<class T>
|
---|
220 | void AddClipped(Double_t th, T src, Int_t n, Int_t p=0)
|
---|
221 | {
|
---|
222 | Float_t *dest = fArray + p;
|
---|
223 | Float_t *end = dest + n;
|
---|
224 |
|
---|
225 | while (dest<end)
|
---|
226 | *dest++ += TMath::Min(*src++, th);
|
---|
227 | }
|
---|
228 |
|
---|
229 | static void StripZeros(TArrayF &arr);
|
---|
230 | void StripZeros();
|
---|
231 |
|
---|
232 | ClassDef(MArrayF, 1) //Array of Float_t
|
---|
233 | };
|
---|
234 |
|
---|
235 | #endif
|
---|