source: branches/Mars_McMismatchStudy/mbase/MArrayD.h@ 18007

Last change on this file since 18007 was 9211, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 3.4 KB
Line 
1#ifndef MARS_MArrayD
2#define MARS_MArrayD
3
4#ifndef MARS_MArray
5#include "MArray.h"
6#endif
7
8#include <string.h>
9
10class TArrayD;
11class MArrayD : public MArray
12{
13private:
14 Double_t *fArray; //[fN] Array of fN chars
15
16public:
17
18 MArrayD()
19 {
20 fN = 0;
21 fArray = NULL;
22 }
23
24 MArrayD(UInt_t n)
25 {
26 fN = 0;
27 fArray = NULL;
28 Set(n);
29 }
30
31 MArrayD(UInt_t n, Double_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 MArrayD(const MArrayD &array) : MArray()
40 {
41 // Copy constructor.
42 fArray = NULL;
43 Set(array.fN, array.fArray);
44 }
45
46 MArrayD &operator=(const MArrayD &rhs)
47 {
48 // TArrayC assignment operator.
49 if (this != &rhs)
50 Set(rhs.fN, rhs.fArray);
51 return *this;
52 }
53
54 virtual ~MArrayD()
55 {
56 // Delete TArrayC object.
57 delete [] fArray;
58 fArray = NULL;
59 }
60
61 void Adopt(UInt_t n, Double_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(Double_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 Double_t At(UInt_t i) const
79 {
80 return fArray[i];
81 }
82
83 Double_t *GetArray() const
84 {
85 return fArray;
86 }
87
88 void Reset()
89 {
90 memset(fArray, 0, fN*sizeof(Double_t));
91 }
92
93 void Reset(Double_t v)
94 {
95 for (Double_t *d=fArray; d<fArray+fN; d++)
96 *d = 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 Double_t *temp = fArray;
109 if (n == 0)
110 fArray = NULL;
111 else
112 {
113 fArray = new Double_t[n];
114 if (n < fN)
115 memcpy(fArray, temp, n*sizeof(Double_t));
116 else
117 {
118 memcpy(fArray, temp, fN*sizeof(Double_t));
119 memset(&fArray[fN], 0, (n-fN)*sizeof(Double_t));
120 }
121 }
122
123 if (fN)
124 delete [] temp;
125
126 fN = n;
127 }
128
129 void Set(UInt_t n, Double_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 Double_t[fN];
147
148 memcpy(fArray, array, n*sizeof(Double_t));
149 }
150
151 Double_t &operator[](UInt_t i)
152 {
153 return fArray[i];
154 }
155 const Double_t &operator[](UInt_t i) const
156 {
157 return fArray[i];
158 }
159
160 Double_t GetSum() const
161 {
162 const Double_t *end = fArray+fN;
163
164 Double_t sum = 0;
165 for (Double_t *ptr = fArray; ptr<end; ptr++)
166 sum += *ptr;
167 return sum;
168 }
169
170 static void StripZeros(TArrayD &arr);
171 void StripZeros();
172
173 ClassDef(MArrayD, 1) //Array of Double_t
174};
175
176#endif
Note: See TracBrowser for help on using the repository browser.