Logo ROOT   6.08/07
Reference Guide
testTMath.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <vector>
3 #include <string>
4 #include <cstring>
5 
6 #include <TMath.h>
7 
8 using namespace std;
9 using namespace TMath;
10 
11 bool showVector = true;
12 
13 template <typename T>
15 {
16  //Float_t NormCross(const Float_t v1[3],const Float_t v2[3],Float_t out[3])
17 
18  T fv1[] = {1,2,3};
19  T fv2[] = {4,5,6};
20  T fout[3];
21 
22  std::string type;
23  if ( strcmp( typeid(T).name(), "f" ) == 0 )
24  type = "Float_t";
25  else if ( strcmp( typeid(T).name(), "d" ) == 0 )
26  type = "Double_t";
27  else
28  type = typeid(T).name();
29 
30  TMath::NormCross(fv1,fv2,fout);
31 
32  cout << "NormCross(const " << type << " v1[3],const "
33  << type << " v2[3]," << type << " out[3]): out = ["
34  << fout[0] << ", " << fout[1] << ", " << fout[2] << "]"
35  << endl;
36 }
37 
38 
39 template <typename T, typename U>
41 {
42  const U n = 10;
43  const U k = 3;
44  U index[n];
45  U is;
46 
47  T sa[n] = { 2, 55 ,23, 57, -9, 24, 6, 82, -4, 10};
48 
49  if ( showVector )
50  {
51  cout << "Vector a[] = {" << sa[0];
52  for ( Int_t i = 1; i < n; ++i )
53  cout << ", " << sa[i];
54  cout << "}\n" << endl;
55  showVector = false;
56  }
57 
58  cout << "Min: a[" << LocMin(n, sa) << "] = " << MinElement(n, sa)
59  << " Max: a[" << LocMax(n, sa) << "] = " << MaxElement(n, sa)
60  << " Mean: " << Mean(n, sa)
61  << " GeomMean: " << GeomMean(n, sa)
62  << " RMS: " << RMS(n, sa)
63  << " Median: " << Median(n, sa)
64  << " KOrdStat(3): " << KOrdStat(n, sa, k)
65  << endl;
66 
67  Sort(n, sa, index, kFALSE);
68  cout << "Sorted a[] = {" << sa[index[0]];
69  for ( Int_t i = 1; i < n; ++i )
70  cout << ", " << sa[index[i]];
71  cout << "}" << endl;
72 
73  sort(sa, sa+n);
74  is = BinarySearch(n, sa, (T) 57);
75  cout << "BinarySearch(n, a, 57) = " << is << "\n" << endl;
76 }
77 
78 template <typename T>
80 {
81  const Long64_t n = 10;
82  vector<Int_t> index(n);
83  Long64_t is;
84 
85  T tsa[n] = { 2, 55 ,23, 57, -9, 24, 6, 82, -4, 10};
86  vector<T> sa(n);
87  for ( int i = 0; i < n; ++i ) sa[i] = tsa[i];
88 
89  if ( showVector )
90  {
91  cout << "\nVector a[] = {" << sa[0];
92  for ( Int_t i = 1; i < n; ++i )
93  cout << ", " << sa[i];
94  cout << "}\n" << endl;
95  showVector = false;
96  }
97 
98  cout << "Min: " << *LocMin(sa.begin(), sa.end())
99  << " Max: " << *LocMax(sa.begin(), sa.end())
100  << " Mean: " << Mean(sa.begin(), sa.end())
101  << " GeomMean: " << GeomMean(sa.begin(), sa.end())
102  << " RMS: " << RMS(sa.begin(), sa.end())
103  << endl;
104 
105  TMath::SortItr(sa.begin(), sa.end(), index.begin(), kFALSE);
106  cout << "Sorted a[] = {" << sa[ index[0] ];
107  for ( Int_t i = 1; i < n; ++i )
108  cout << ", " << sa[ index[i] ];
109  cout << "}" << endl;
110 
111  sort(&sa[0], &sa[0]+n);
112  is = BinarySearch(n, &sa[0], (T) 57);
113  cout << "BinarySearch(n, a, 57) = " << is << "\n" << endl;
114 }
115 
116 template <typename T>
117 void testPoints(T x, T y)
118 {
119  const Int_t n = 4;
120 
121  T dx[4] = {0, 0, 2, 2};
122  T dy[4] = {0, 2, 2, 0};
123  cout << "Point(" << x << "," << y << ") IsInside?: "
124  << IsInside( x, y, n, dx, dy) << endl;
125 
126 }
127 
128 template <typename T>
129 void testPlane()
130 {
131  T dp1[3] = {0,0,0};
132  T dp2[3] = {1,0,0};
133  T dp3[3] = {0,1,0};
134  T dn[3];
135  Normal2Plane(dp1, dp2, dp3, dn);
136  cout << "Normal: ("
137  << dn[0] << ", "
138  << dn[1] << ", "
139  << dn[2] << ")"
140  << endl;
141 }
142 
143 void testTMath()
144 {
145  cout << "Starting tests on TMath..." << endl;
146 
147  cout << "\nNormCross tests: " << endl;
148 
149  testNormCross<Float_t>();
150  testNormCross<Double_t>();
151 
152  cout << "\nArray functions tests: " << endl;
153 
154  testArrayFunctions<Short_t,Long64_t>();
155  testArrayFunctions<Int_t,Long64_t>();
156  testArrayFunctions<Float_t,Long64_t>();
157  testArrayFunctions<Double_t,Long64_t>();
158  testArrayFunctions<Double_t,Int_t>();
159  testArrayFunctions<Long_t,Long64_t>();
160  testArrayFunctions<Long64_t,Long64_t>();
161 
162  cout << "\nIterator functions tests: " << endl;
163 
164  testIteratorFunctions<Short_t>();
165  testIteratorFunctions<Int_t>();
166  testIteratorFunctions<Float_t>();
167  testIteratorFunctions<Double_t>();
168  testIteratorFunctions<Long_t>();
169  testIteratorFunctions<Long64_t>();
170 
171  cout << "\nPoint functions tests: " << endl;
172 
173  testPoints<Double_t>(1.3, 0.5);
174  testPoints<Float_t>(-0.2, 1.7);
175  testPoints<Int_t>(1, 1);
176 
177  cout << "\nPLane functions tests: " << endl;
178 
179  testPlane<Double_t>();
180  testPlane<Float_t>();
181 }
182 
183 int main()
184 {
185  testTMath();
186 
187  return 0;
188 }
long long Long64_t
Definition: RtypesCore.h:69
Long64_t LocMax(Long64_t n, const T *a)
Definition: TMath.h:711
void testPlane()
Definition: testTMath.cxx:129
double T(double x)
Definition: ChebyshevPol.h:34
void testIteratorFunctions()
Definition: testTMath.cxx:79
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
STL namespace.
Double_t GeomMean(Long64_t n, const T *a)
Definition: TMath.h:842
void testTMath()
Definition: testTMath.cxx:143
T * Normal2Plane(const T v1[3], const T v2[3], const T v3[3], T normal[3])
Definition: TMath.h:1019
Double_t RMS(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:903
void testArrayFunctions()
Definition: testTMath.cxx:40
T NormCross(const T v1[3], const T v2[3], T out[3])
Definition: TMath.h:661
Double_t x[n]
Definition: legend1.C:17
bool showVector
Definition: testTMath.cxx:11
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Definition: TMath.h:989
Bool_t IsInside(T xp, T yp, Int_t np, T *x, T *y)
Definition: TMath.h:1043
int main()
Definition: testTMath.cxx:183
Double_t Mean(Long64_t n, const T *a, const Double_t *w=0)
Definition: TMath.h:811
int type
Definition: TGX11.cxx:120
Double_t Median(Long64_t n, const T *a, const Double_t *w=0, Long64_t *work=0)
Definition: TMath.h:1064
T MaxElement(Long64_t n, const T *a)
Definition: TMath.h:675
Double_t y[n]
Definition: legend1.C:17
Element KOrdStat(Size n, const Element *a, Size k, Size *work=0)
Definition: TMath.h:1154
void testPoints(T x, T y)
Definition: testTMath.cxx:117
void testNormCross()
Definition: testTMath.cxx:14
Long64_t LocMin(Long64_t n, const T *a)
Definition: TMath.h:682
const Int_t n
Definition: legend1.C:16
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMath.h:931
char name[80]
Definition: TGX11.cxx:109
void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE)
Definition: TMath.h:964
T MinElement(Long64_t n, const T *a)
Definition: TMath.h:668