SparseDataComparer.cxx

Go to the documentation of this file.
00001 #include "TH2.h"
00002 #include "TF2.h"
00003 #include "TCanvas.h"
00004 #include "TApplication.h"
00005 
00006 #include "TMath.h"
00007 #include "Fit/SparseData.h"
00008 #include "HFitInterface.h"
00009 #include "Fit/Fitter.h"
00010 #include "Math/WrappedMultiTF1.h"
00011 
00012 #include <iostream>
00013 #include <iterator>
00014 #include <algorithm>
00015 #include <functional>
00016 
00017 using namespace std;
00018 
00019 const bool __DRAW__ = 1;
00020 
00021 double minRange[3] = { -5., -5., -5.};
00022 double maxRange[3] = {  5.,  5.,  5.};
00023 int       nbins[3] = {10 , 10 , 100 };
00024 
00025 ostream& operator << (ostream& out, ROOT::Fit::BinData& bd)
00026 {
00027    const unsigned int ndim( bd.NDim() );
00028    const unsigned int npoints( bd.NPoints() );
00029    for ( unsigned int i = 0; i < npoints; ++i )
00030    {
00031       double value, error;
00032       const double *x = bd.GetPoint(i, value, error);
00033       for ( unsigned int j = 0; j < ndim; ++j )
00034       {
00035          out << " x[" << j << "]: " << x[j];
00036       }
00037       out << " value: " << value;
00038       out << " error: " << error;
00039       out << endl;
00040    }
00041    return out;
00042 }
00043 
00044 int findBin(ROOT::Fit::BinData& bd, const double *x)
00045 {
00046    const unsigned int ndim = bd.NDim();
00047    const unsigned int npoints = bd.NPoints();
00048 
00049    for ( unsigned int i = 0; i < npoints; ++i )
00050    {
00051       double value1, error1;
00052       const double *x1 = bd.GetPoint(i, value1, error1);
00053       bool thisIsIt = true;
00054       for ( unsigned int j = 0; j < ndim; ++j )
00055       {
00056          thisIsIt &= fabs(x1[j] - x[j]) < 1E-15;
00057       }
00058       if ( thisIsIt ) return i;
00059    }
00060 
00061    cout << "ERROR FINDING BIN!" << endl;
00062    return -1;
00063 }
00064 
00065 bool operator ==(ROOT::Fit::BinData& bd1, ROOT::Fit::BinData& bd2)
00066 {
00067    const unsigned int ndim = bd1.NDim();
00068    const unsigned int npoints = bd1.NPoints();
00069 //    unsigned int npoints2 = 0;
00070 
00071    bool equals = true;
00072 
00073    cout << "Equals" << endl;
00074 
00075    for ( unsigned int i = 0; i < npoints && equals; ++i )
00076    {
00077       double value1, error1;
00078       const double *x1 = bd1.GetPoint(i, value1, error1);
00079 
00080       int bin = findBin(bd2, x1);
00081 
00082       double value2 = 0, error2;
00083       const double *x2 = bd2.GetPoint(bin, value2, error2);
00084 
00085       equals &= ( value1 == value2 );
00086       cout << " v: " << equals;
00087       equals &= ( error1 == error2 );
00088       cout << " e: " << equals;
00089       for ( unsigned int j = 0; j < ndim; ++j )
00090       {
00091          equals &= fabs(x1[j] - x2[j]) < 1E-15;
00092          cout << " x[" << j << "]: " << equals;
00093       }
00094 
00095       cout << " bd1: ";
00096       std::copy(x1, &x1[ndim], ostream_iterator<double>(cout, " "));
00097       cout << " value:" << value1; 
00098       cout << " error:" << error1; 
00099 
00100       cout << " bd2: ";
00101       std::copy(x2, &x2[ndim], ostream_iterator<double>(cout, " "));
00102       cout << " value:" << value2; 
00103       cout << " error:" << error2; 
00104 
00105       cout << " equals: " << equals;
00106 
00107       cout << endl; 
00108    }
00109 
00110    return equals;   
00111 }
00112 
00113 
00114 void OneDimension()
00115 {
00116    TH1D* h = new TH1D("h1", "h1-title", nbins[0], minRange[0], maxRange[0]);
00117    h->FillRandom("gaus", 40);
00118    h->Draw();
00119 
00120    ROOT::Fit::BinData bd;
00121    ROOT::Fit::FillData(bd, h);
00122 
00123    cout << bd << endl;
00124 
00125    double min[] = { minRange[0] };
00126    double max[] = { maxRange[0] };
00127    ROOT::Fit::SparseData sd(1, min,max);
00128    ROOT::Fit::FillData(sd, h);
00129    ROOT::Fit::BinData bd2;
00130    sd.GetBinData(bd2);
00131 
00132    cout << bd2 << endl;
00133 
00134    cout << " equals : " << (bd == bd2) << endl;
00135 
00136    h->Draw();
00137 }
00138 
00139 double gaus2D(double *x, double *p)
00140 {
00141    return p[0]*TMath::Gaus(x[0],p[1],p[2]) * TMath::Gaus(x[1],p[3],p[4]);
00142 }
00143 
00144 
00145 void TwoDimensions()
00146 {
00147    TH2D* h = new TH2D("h2", "h2-title", 
00148                       nbins[0], minRange[0], maxRange[0],
00149                       nbins[1], minRange[1], maxRange[1]);
00150 
00151    TF2* f2 = new TF2("gaus2D", gaus2D, 
00152                      minRange[0],maxRange[0], minRange[1], maxRange[1], 5);
00153    double initialPars[] = {300,0.,2.,0.,3.};
00154    f2->SetParameters(initialPars);
00155 
00156    h->FillRandom("gaus2D",20);
00157    h->Draw("lego2");
00158 
00159    ROOT::Fit::BinData bd;
00160    ROOT::Fit::FillData(bd, h);
00161 
00162    cout << bd << endl;
00163 
00164    double min[] = { minRange[0], minRange[1] };
00165    double max[] = { maxRange[0], maxRange[1] };
00166    ROOT::Fit::SparseData sd(2, min, max);
00167 
00168    ROOT::Fit::FillData(sd, h);
00169    ROOT::Fit::BinData bd2;
00170    sd.GetBinData(bd2);
00171 
00172    cout << bd2 << endl;
00173 
00174    cout << " equals : " << (bd == bd2) << endl;
00175 }
00176 
00177 int main(int argc, char** argv)
00178 {
00179    TApplication* theApp = 0;
00180 
00181    if ( __DRAW__ )
00182       theApp = new TApplication("App",&argc,argv);
00183 
00184    cout << "\nONE DIMENSION" << endl;
00185    OneDimension();   
00186    cout << "\nTWO DIMENSIONS" << endl;
00187    TwoDimensions();
00188   
00189    if ( __DRAW__ ) {
00190       theApp->Run();
00191       delete theApp;
00192       theApp = 0;
00193    }
00194    
00195    return 0;
00196 }
00197 

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