testGraph.cxx

Go to the documentation of this file.
00001 // @(#)root/minuit2:$Id: testGraph.cxx 24400 2008-06-20 07:28:49Z moneta $
00002 // Author: L. Moneta    10/2005  
00003 
00004 /**********************************************************************
00005  *                                                                    *
00006  * Copyright (c) 2005 ROOT Foundation,  CERN/PH-SFT                   *
00007  *                                                                    *
00008  **********************************************************************/
00009 
00010 #include "TApplication.h"
00011 #include "TGraphErrors.h"
00012 #include "TF1.h"
00013 #include "TRandom3.h"
00014 #include "TCanvas.h"
00015 #include "TLegend.h"
00016 #include "TPaveLabel.h"
00017 #include "TStopwatch.h"
00018 #include "TVirtualFitter.h"
00019 #include "TMath.h"
00020 #include "TStyle.h"
00021 
00022 #include <vector>
00023 #include <iterator>
00024 #include <cassert>
00025 
00026 double fitFunc( double *x , double * p) { 
00027 
00028   double A = p[0];
00029   double B = p[1];
00030   double C = p[2];
00031   return    A*TMath::Sin(C*x[0]) + B*TMath::Sin(2*C*x[0]);
00032 }
00033 
00034 
00035 
00036 void makePoints(Int_t n, std::vector<double> &  x, std::vector<double> & y, std::vector<double> & e)
00037 {
00038   Int_t i;
00039   TRandom3 r;
00040 
00041   double A = 1; 
00042   double B = 2; 
00043   double C = 1; 
00044 
00045   for (i=0; i<n; i++) {
00046     x[i] = r.Uniform(-2, 2);
00047     y[i]=A*TMath::Sin(C*x[i]) + B*TMath::Sin(2*C*x[i]) + r.Gaus()*0.3;
00048     e[i] = 0.1;
00049   }
00050 
00051 }
00052 
00053 
00054 void doFit(int n,const char * fitter) 
00055 { 
00056 
00057 
00058    std::vector<double> x(n); 
00059    std::vector<double> y(n); 
00060    std::vector<double> e(n); 
00061 
00062    double initPar[3] = { 1, 1, 2 };
00063 
00064    //Generate points along a sin(x)+sin(2x) function
00065    makePoints(n, x , y, e);
00066 
00067    TGraphErrors *gre3 = new TGraphErrors(n, &x.front(), &y.front(), 0, &e.front());
00068    gre3->SetMarkerStyle(24);
00069    gre3->SetMarkerSize(0.3);
00070    gre3->Draw("ap");
00071 
00072 
00073    //Fit the graph with the predefined "pol3" function
00074    TF1 *f = new TF1("f2",fitFunc, -2, 2, 3);
00075 
00076    printf("fitting with %s",fitter);
00077    TVirtualFitter::SetDefaultFitter(fitter);
00078 
00079 
00080    int npass = 100; 
00081    TStopwatch timer;
00082    timer.Start();
00083    for (int i = 0; i < npass; ++i) { 
00084      f->SetParameters(initPar);
00085      //f->FixParameter(1,2.);
00086      gre3->Fit(f,"q");
00087    }
00088    timer.Stop();
00089    printf("%s,: RT=%7.3f s, Cpu=%7.3f s\n",fitter,timer.RealTime(),timer.CpuTime());
00090 
00091    // get covariance matrix
00092    TVirtualFitter * theFitter = TVirtualFitter::GetFitter();
00093    int np = theFitter->GetNumberFreeParameters();
00094    std::cout << "Number of free parameters " << np << "\nCovariance Matrix :\n";
00095    double * cv = theFitter->GetCovarianceMatrix();
00096    assert(cv != 0);
00097    for (int i = 0; i < np ; ++i) {
00098       for (int j = 0; j < np ; ++j) 
00099          std::cout << cv[j + i*np] << "\t";
00100       std::cout << std::endl;
00101    }
00102 
00103 
00104    
00105    //Access the fit results
00106    TF1 *f3 = gre3->GetFunction("f2");
00107    //std::cout << "draw function" << f3 << std::endl;
00108    if (f3) { 
00109      f3->SetLineWidth(1);
00110      f3->SetLineColor(kRed);
00111      f3->Draw("same");
00112    }
00113 
00114 
00115    TLegend *leg = new TLegend(0.1, 0.8, 0.35, 0.9);
00116    leg->AddEntry(gre3, "sin(x) + sin(2*x)", "p");
00117    leg->Draw();
00118    leg->SetFillColor(42);
00119 
00120    TPaveLabel *pl = new TPaveLabel(0.5,0.7,0.85,0.8,Form("%s CPU= %g s",fitter,timer.CpuTime()),"brNDC");
00121    pl->Draw();
00122 
00123 
00124 }
00125 
00126 void testGraph(int n = 500) { 
00127   TCanvas *myc = new TCanvas("myc", "Fitting 3 TGraphErrors with linear functions");
00128   myc->Divide(1,2);
00129   myc->SetFillColor(42);
00130   myc->SetGrid();
00131   gStyle->SetOptFit();
00132 
00133   myc->cd(1); 
00134  doFit(n,"Minuit");
00135  myc->Update();
00136 
00137   myc->cd(2); 
00138   doFit(n,"Minuit2");
00139  myc->Update();
00140 
00141 }
00142 
00143 #ifndef __CINT__
00144 int main(int argc, char **argv)
00145 {
00146    if (argc > 1) { 
00147       TApplication theApp("App", &argc, argv);
00148       testGraph(500);
00149       theApp.Run();
00150    } 
00151    else 
00152       testGraph(500);
00153 
00154    return 0;
00155 }
00156 #endif
00157 

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