source: trunk/Mars/mbase/MArrayF.h@ 18679

Last change on this file since 18679 was 17768, checked in by tbretz, 11 years ago
Replaced my by scale.
File size: 4.3 KB
Line 
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
10class TArrayF;
11class MArrayF : public MArray
12{
13private:
14 Float_t *fArray; //[fN] Array of fN chars
15
16public:
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(const T *src, UInt_t n, UInt_t beg=0, Float_t scale=1)
173 {
174 if (!src || beg >= fN)
175 return;
176
177 Float_t *ptr = fArray+beg;
178 const Float_t *end = beg+n>fN ? fArray+fN : ptr+n;
179
180 // we treat the case m==1. special, in order to speed up the code a bit
181 // since when m==1. the multiplication can be omitted and this should be a bit faster then.
182 if (scale==1)
183 {
184 while (ptr<end)
185 *ptr++ += *src++;
186 }
187 else
188 {
189 while (ptr<end)
190 *ptr++ += (*src++) * scale;
191 }
192 }
193
194 template<class T>
195 void AddClipped(Double_t th, const T *src, UInt_t n, UInt_t beg=0)
196 {
197 if (!src || beg>=fN)
198 return;
199
200 Float_t *ptr = fArray + beg;
201 const Float_t *end = beg+n>fN ? fArray+fN : ptr+n;
202
203 while (ptr<end)
204 *ptr++ += TMath::Min(*src++, th);
205 }
206
207 static void StripZeros(TArrayF &arr);
208 void StripZeros();
209
210 ClassDef(MArrayF, 1) //Array of Float_t
211};
212
213#endif
Note: See TracBrowser for help on using the repository browser.