Fit Data With a Gaussian in ROOT


void plot_gaussfit() {
   //Draw a graph with error bars, label axes and fit to a Gaussian
   //Author: Rene Brun, last modified by Tom Browder
   
   TCanvas *c1 = new TCanvas("c1","A Simple Graph with error bars",200,10,700,500);

   c1->SetFillColor(42);
   c1->SetGrid();
   c1->GetFrame()->SetFillColor(21);
   c1->GetFrame()->SetBorderSize(12);

   const Int_t n = 10;
   // Data points (x,y) with error bars (ex, ey)
   Float_t x[n]  = {-0.22, 0.05, 0.25, 0.35, 0.5, 0.61,0.7,0.85,0.89,0.95};
   Float_t y[n]  = {0,2.9,5.6,7.4,9,9.6,8.7,6.3,4.5,2};
   Float_t ex[n] = {.05,.1,.07,.07,.04,.05,.06,.07,.08,.05};
   Float_t ey[n] = {.8,.7,.6,.5,.4,.4,.5,.6,.7,.8};

   TGraphErrors *gr = new TGraphErrors(n,x,y,ex,ey);
   gr->SetTitle("Points with error bars, Label axes, Fit to a Gaussian");
   gr->SetMarkerColor(4);
   gr->SetMarkerStyle(21);
   gr->Draw("AP");
   // Fit to a 4th order polynomial
   gr->Fit("gaus","q");
   // Label the axes
   gr->GetXaxis()->SetTitle(" Angle (in Radians)");
   gr->GetYaxis()->SetTitle(" Counts/sec");

   c1->Update();
}