00001
00002
00003
00004 #include <TMath.h>
00005 #include <TGraph2D.h>
00006 #include <TRandom.h>
00007 #include <TStyle.h>
00008 #include <TCanvas.h>
00009 #include <TF2.h>
00010 #include <TH1.h>
00011
00012 TCanvas* graph2dfit()
00013 {
00014 gStyle->SetOptStat(0);
00015 gStyle->SetOptFit();
00016
00017 TCanvas *c = new TCanvas("c","Graph2D example",0,0,600,800);
00018 c->Divide(2,3);
00019
00020 Double_t rnd, x, y, z;
00021 Double_t e = 0.3;
00022 Int_t nd = 400;
00023 Int_t np = 10000;
00024
00025 TRandom r;
00026 Double_t fl = 6;
00027 TF2 *f2 = new TF2("f2","1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",
00028 -fl,fl,-fl,fl);
00029 f2->SetParameters(1,1);
00030 TGraph2D *dt = new TGraph2D();
00031
00032
00033 Double_t zmax = 0;
00034 for (Int_t N=0; N<nd; N++) {
00035 f2->GetRandom2(x,y);
00036
00037 rnd = 2*r.Rndm()*e-e;
00038 z = f2->Eval(x,y)*(1+rnd);
00039 if (z>zmax) zmax = z;
00040 dt->SetPoint(N,x,y,z);
00041 }
00042
00043 Double_t hr = 350;
00044 TH1D *h1 = new TH1D("h1",
00045 "#splitline{Difference between Original}{#splitline{function and Function}{with noise}}",
00046 100, -hr, hr);
00047 TH1D *h2 = new TH1D("h2",
00048 "#splitline{Difference between Original}{#splitline{function and Delaunay triangles}{interpolation}}",
00049 100, -hr, hr);
00050 TH1D *h3 = new TH1D("h3",
00051 "#splitline{Difference between Original}{function and Minuit fit}",
00052 500, -hr, hr);
00053
00054 f2->SetParameters(0.5,1.5);
00055 dt->Fit(f2);
00056 TF2 *fit2 = (TF2*)dt->FindObject("f2");
00057
00058 f2->SetParameters(1,1);
00059
00060 for (Int_t N=0; N<np; N++) {
00061 f2->GetRandom2(x,y);
00062
00063 rnd = 2*r.Rndm()*e-e;
00064 z = f2->Eval(x,y)*(1+rnd);
00065 h1->Fill(f2->Eval(x,y)-z);
00066 z = dt->Interpolate(x,y);
00067 h2->Fill(f2->Eval(x,y)-z);
00068 z = fit2->Eval(x,y);
00069 h3->Fill(f2->Eval(x,y)-z);
00070 }
00071
00072 gStyle->SetPalette(1);
00073 c->cd(1);
00074 f2->SetTitle("Original function with Graph2D points on top");
00075 f2->SetMaximum(zmax);
00076 gStyle->SetHistTopMargin(0);
00077 f2->Draw("surf1");
00078 dt->Draw("same p0");
00079
00080 c->cd(3);
00081 dt->SetMargin(0.1);
00082 dt->SetFillColor(36);
00083 dt->SetTitle("Histogram produced with Delaunay interpolation");
00084 dt->Draw("surf4");
00085
00086 c->cd(5);
00087 fit2->SetTitle("Minuit fit result on the Graph2D points");
00088 fit2->Draw("surf1");
00089
00090 h1->SetFillColor(47);
00091 h2->SetFillColor(38);
00092 h3->SetFillColor(29);
00093
00094 c->cd(2); h1->Fit("gaus","Q") ; h1->Draw();
00095 c->cd(4); h2->Fit("gaus","Q") ; h2->Draw();
00096 c->cd(6); h3->Fit("gaus","Q") ; h3->Draw();
00097 c->cd();
00098 return c;
00099 }