StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
TRArray.cxx
1 #include <stdarg.h>
2 #include <iomanip>
3 #include "TRArray.h"
4 #include "TString.h"
5 #if ROOT_VERSION_CODE < 331013
6 #include "TCL.h"
7 #else
8 #include "TCernLib.h"
9 #endif
10 #include "TObjString.h"
11 #include "TObjArray.h"
12 using namespace std;
13 ClassImp(TRArray);
14 //________________________________________________________________________________
15 TRArray::TRArray(Int_t N,const Float_t *Array): TArrayD(N), fValid(kTRUE), fIsNotOwn(kFALSE) {
16  TCL::ucopy(Array,fArray,N);
17 }
18 //________________________________________________________________________________
19 TRArray::TRArray(Int_t N,Double_t va_(a0), ...) : TArrayD(N), fValid(kTRUE), fIsNotOwn(kFALSE) {
20  __VA_LIST__(a0);
21 }
22 //________________________________________________________________________________
23 TRArray::TRArray(Int_t N,const Char_t *s): TArrayD(N), fValid(kTRUE), fIsNotOwn(kFALSE) {
24  static TString separator = "([^\t ;,]+)";
25  TString opt(s);
26  TObjArray *array = opt.Tokenize(separator);
27  TIter next(array);
28  TObjString *objs;
29  Int_t i = 0;
30  while ((objs = (TObjString *) next()) && i < N) {fArray[i++] = objs->GetString().Atof();}
31  delete array;
32 }
33 //________________________________________________________________________________
34 Double_t TRArray::Mag2() const {
35  return TCL::vdot(fArray,fArray,fN);
36 }
37 //________________________________________________________________________________
38 ostream& operator<<(ostream& s,const TRArray &target) {
39  s << "Size \t" << target.fN << endl;
40  if (target.fArray)
41  for (int i = 0; i< target.fN; i++) {
42  s << Form("%10.3f", target.fArray[i]);
43  if ((i+1)%10 == 0) s << endl;
44  }
45  else s << " Empty";
46  s << endl;
47  return s;
48 }
49 //________________________________________________________________________________
50 istream & operator>>(istream &s, TRArray &target) {
51  Int_t N;
52  s >> N;
53  if (N != target.fN) target.Set(N);
54  for (int i = 0; i < N; i++) s >> target.fArray[i];
55  return s;
56 }
57 //________________________________________________________________________________
58 TRArray &TRArray::operator=(const TRArray &rhs) { // TRArray assignment operator.
59  if (this != &rhs) {
60  if (! fIsNotOwn) Set(rhs.fN, rhs.fArray);
61  else {
62  fN = rhs.fN;
63  memcpy(fArray,rhs.fArray, fN*sizeof(Double_t));
64  }
65  }
66  return *this;
67 }
68 //________________________________________________________________________________
69 Bool_t TRArray::Verify(const TRArray &A, Double_t zeru, Int_t Level) const {
70  // TRUE if test failed
71  Int_t fails = 0;
72  if (fN != A.GetSize()) {
73  if (Level) cout << "Check length is inconsistent:" << fN << " != " << A.GetSize() << endl;
74  return kTRUE;
75  }
76  const Double_t *aArray = A.GetArray();
77  for (int i=0; i<fN; i++) {
78  Double_t diff = TMath::Abs(aArray[i] - fArray[i]);
79  Double_t sum = TMath::Abs(aArray[i] + fArray[i]);
80  if (diff > zeru || (sum > 2. && (2 * diff ) / sum > zeru)) {
81  fails++;
82  if (Level)
83  cout << "Failed:[" << i << "]\t" << aArray[i] << "\t" << fArray[i] << "\tdiff\t" << diff << endl;
84  continue;
85  }
86  else if (Level > 1)
87  cout << "Passed:[" << i << "]\t" << aArray[i] << "\t" << fArray[i] << "\tdiff\t" << diff << endl;
88  }
89  if (fails) {
90  cout << "Failed " << fails << " times" << endl;
91  }
92  return fails != 0;
93 }
94 //________________________________________________________________________________
95 void TRArray::Print(Option_t *opt) const {if (opt) {}; cout << *this << endl;}
96 //______________________________________________________________________________
97 void TRArray::AdoptA(Int_t n, Double_t *arr) {
98  // Adopt array arr into TRArray, i.e. don't copy arr but use it directly
99  // in TRArray. User may delete arr, TRArray dtor will not do it.
100  fN = n;
101  if (fArray == arr) return;
102  if (fArray && ! fIsNotOwn) delete [] fArray;
103  fIsNotOwn = kTRUE;
104  fArray = arr;
105 }
106 //______________________________________________________________________________
107 void TRArray::Set(Int_t n) {
108  // Set size of this array to n doubles.
109  // A new array is created, the old contents copied to the new array,
110  // then the old array is deleted.
111  // This function should not be called if the array was declared via Adopt.
112 
113  if (n < 0) return;
114  if (fIsNotOwn) {
115  memset(&fArray[fN],0,(n-fN)*sizeof(Double_t));
116  fN = n;
117  return;
118  }
119  if (n != fN) {
120  Double_t *temp = fArray;
121  if (n != 0) {
122  fArray = new Double_t[n];
123  if (n < fN) memcpy(fArray,temp, n*sizeof(Double_t));
124  else {
125  memcpy(fArray,temp,fN*sizeof(Double_t));
126  memset(&fArray[fN],0,(n-fN)*sizeof(Double_t));
127  }
128  } else {
129  fArray = 0;
130  }
131  if (fN) delete [] temp;
132  fN = n;
133  }
134 }
135 //________________________________________________________________________________
136 void TRArray::Set(Int_t n, const Float_t *array) {
137  // Set size of this array to n doubles and set the contents
138  // This function should not be called if the array was declared via Adopt.
139  if (fArray && fN != n && ! fIsNotOwn) {
140  delete [] fArray;
141  fArray = 0;
142  }
143  fN = n;
144  if (fN == 0) return;
145  if (array == 0) return;
146  if (!fArray) {
147  fIsNotOwn = kFALSE;
148  fArray = new Double_t[fN];
149  }
150  TCL::ucopy(array,fArray,n);
151 }
152 //______________________________________________________________________________
153 void TRArray::Set(Int_t n, const Double_t *array) {
154  // Set size of this array to n doubles and set the contents
155  // This function should not be called if the array was declared via Adopt.
156  if (fArray && fN != n && ! fIsNotOwn) {
157  delete [] fArray;
158  fArray = 0;
159  }
160  fN = n;
161  if (fN == 0) return;
162  if (array == 0) return;
163  if (!fArray) {
164  fIsNotOwn = kFALSE;
165  fArray = new Double_t[fN];
166  }
167  memcpy(fArray,array, n*sizeof(Double_t));
168 }