testTMath.cxx

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <vector>
00003 #include <string>
00004 #include <cstring>
00005 
00006 #include <TMath.h>
00007 
00008 using namespace std;
00009 using namespace TMath;
00010 
00011 bool showVector = true;
00012 
00013 template <typename T>
00014 void testNormCross()
00015 {
00016    //Float_t NormCross(const Float_t v1[3],const Float_t v2[3],Float_t out[3])
00017 
00018    T fv1[] = {1,2,3};
00019    T fv2[] = {4,5,6};
00020    T fout[3];
00021 
00022    std::string type;
00023    if ( strcmp( typeid(T).name(), "f" ) == 0 )
00024       type = "Float_t";
00025    else if ( strcmp( typeid(T).name(), "d" ) == 0 )
00026       type = "Double_t";
00027    else 
00028       type = typeid(T).name();
00029 
00030    TMath::NormCross(fv1,fv2,fout);
00031 
00032    cout << "NormCross(const " << type << " v1[3],const " 
00033         << type << " v2[3]," << type << " out[3]): out = [" 
00034         << fout[0] << ", " << fout[1] << ", " << fout[2] << "]"
00035         << endl;
00036 }
00037 
00038 
00039 template <typename T, typename U>
00040 void testArrayFunctions()
00041 {
00042    const U n = 10;
00043    const U k = 3;
00044    U index[n];
00045    U is;
00046 
00047    T sa[n] = { 2, 55 ,23, 57, -9, 24, 6, 82, -4, 10};
00048 
00049    if ( showVector )
00050    {
00051       cout << "Vector a[] = {" << sa[0];
00052       for ( Int_t i = 1; i < n; ++i )
00053          cout << ", " << sa[i];
00054       cout << "}\n" << endl;
00055       showVector = false;
00056    }
00057 
00058    cout << "Min: a[" << LocMin(n, sa) << "] = " << MinElement(n, sa)
00059         << " Max: a[" << LocMax(n, sa) << "] = " << MaxElement(n, sa)
00060         << " Mean: " << Mean(n, sa)
00061         << " GeomMean: " << GeomMean(n, sa)
00062         << " RMS: " << RMS(n, sa)
00063         << " Median: " << Median(n, sa)
00064         << " KOrdStat(3): " << KOrdStat(n, sa, k)
00065         << endl;
00066 
00067    Sort(n, sa, index, kFALSE);
00068    cout << "Sorted a[] = {" << sa[index[0]];
00069    for ( Int_t i = 1; i < n; ++i )
00070       cout << ", " << sa[index[i]];
00071    cout << "}" << endl;
00072 
00073    sort(sa, sa+n);
00074    is = BinarySearch(n, sa, (T) 57);
00075    cout << "BinarySearch(n, a, 57) = " << is << "\n" << endl;
00076 }
00077 
00078 template <typename T>
00079 void testIteratorFunctions()
00080 {
00081    const Long64_t n = 10;
00082    vector<Int_t> index(n);
00083    Long64_t is;
00084 
00085    T tsa[n] = { 2, 55 ,23, 57, -9, 24, 6, 82, -4, 10};
00086    vector<T> sa(n);
00087    for ( int i = 0;  i < n; ++i ) sa[i] = tsa[i];
00088 
00089    if ( showVector )
00090    {
00091       cout << "\nVector a[] = {" << sa[0];
00092       for ( Int_t i = 1; i < n; ++i )
00093          cout << ", " << sa[i];
00094       cout << "}\n" << endl;
00095       showVector = false;
00096    }
00097 
00098    cout << "Min: " << *LocMin(sa.begin(), sa.end())
00099         << " Max: " << *LocMax(sa.begin(), sa.end())
00100         << " Mean: " << Mean(sa.begin(), sa.end())
00101         << " GeomMean: " << GeomMean(sa.begin(), sa.end())
00102         << " RMS: " << RMS(sa.begin(), sa.end())
00103         << endl;
00104 
00105    TMath::SortItr(sa.begin(), sa.end(), index.begin(), kFALSE);
00106    cout << "Sorted a[] = {" << sa[ index[0] ];
00107    for ( Int_t i = 1; i < n; ++i )
00108       cout << ", " << sa[ index[i] ];
00109    cout << "}" << endl;
00110 
00111    sort(&sa[0], &sa[n]);
00112    is = BinarySearch(n, &sa[0], (T) 57);
00113    cout << "BinarySearch(n, a, 57) = " << is << "\n" << endl;
00114 }
00115 
00116 template <typename T>
00117 void testPoints(T x, T y)
00118 {
00119    const Int_t n = 4;
00120 
00121    T dx[4] = {0, 0, 2, 2};
00122    T dy[4] = {0, 2, 2, 0};
00123    cout << "Point(" << x << "," << y << ") IsInside?: " 
00124         << IsInside( x, y, n, dx, dy) << endl;
00125 
00126 }
00127 
00128 template <typename T>
00129 void testPlane()
00130 {
00131    T dp1[3] = {0,0,0};
00132    T dp2[3] = {1,0,0};
00133    T dp3[3] = {0,1,0};
00134    T dn[3];
00135    Normal2Plane(dp1, dp2, dp3, dn);
00136    cout << "Normal: (" 
00137         << dn[0] << ", "
00138         << dn[1] << ", "
00139         << dn[2] << ")" 
00140         << endl;
00141 }
00142 
00143 void testTMath() 
00144 {
00145    cout << "Starting tests on TMath..." << endl;
00146 
00147    cout << "\nNormCross tests: " << endl;
00148 
00149    testNormCross<Float_t>();
00150    testNormCross<Double_t>();
00151 
00152    cout << "\nArray functions tests: " << endl;
00153 
00154    testArrayFunctions<Short_t,Long64_t>();
00155    testArrayFunctions<Int_t,Long64_t>();
00156    testArrayFunctions<Float_t,Long64_t>();
00157    testArrayFunctions<Double_t,Long64_t>();
00158    testArrayFunctions<Double_t,Int_t>();
00159    testArrayFunctions<Long_t,Long64_t>();
00160    testArrayFunctions<Long64_t,Long64_t>();
00161 
00162    cout << "\nIterator functions tests: " << endl;
00163 
00164    testIteratorFunctions<Short_t>();
00165    testIteratorFunctions<Int_t>();
00166    testIteratorFunctions<Float_t>();
00167    testIteratorFunctions<Double_t>();
00168    testIteratorFunctions<Long_t>();
00169    testIteratorFunctions<Long64_t>();
00170 
00171    cout << "\nPoint functions tests: " << endl;
00172 
00173    testPoints<Double_t>(1.3, 0.5);
00174    testPoints<Float_t>(-0.2, 1.7);
00175    testPoints<Int_t>(1, 1);
00176 
00177    cout << "\nPLane functions tests: " << endl;
00178 
00179    testPlane<Double_t>();
00180    testPlane<Float_t>();
00181 }
00182 
00183 int main()
00184 {
00185    testTMath();
00186 
00187    return 0;
00188 }

Generated on Tue Jul 5 14:34:31 2011 for ROOT_528-00b_version by  doxygen 1.5.1