myfit.C

Go to the documentation of this file.
00001 // Get in memory an histogram from a root file and fit a user defined function.
00002 // Note that a user defined function must always be defined
00003 // as in this example:
00004 //  - first parameter: array of variables (in this example only 1-dimension)
00005 //  - second parameter: array of parameters
00006 // Note also that in case of user defined functions, one must set
00007 // an initial value for each parameter.
00008 //Author: Rene Brun
00009    
00010 Double_t fitf(Double_t *x, Double_t *par)
00011 {
00012    Double_t arg = 0;
00013    if (par[2] != 0) arg = (x[0] - par[1])/par[2];
00014 
00015    Double_t fitval = par[0]*TMath::Exp(-0.5*arg*arg);
00016    return fitval;
00017 }
00018 void myfit()
00019 {
00020    TString dir = gSystem->UnixPathName(gInterpreter->GetCurrentMacroName());
00021    dir.ReplaceAll("myfit.C","../hsimple.C");
00022    dir.ReplaceAll("/./","/");
00023    if (!gInterpreter->IsLoaded(dir.Data())) gInterpreter->LoadMacro(dir.Data());
00024    TFile *hsimple = (TFile*)gROOT->ProcessLineFast("hsimple(1)");
00025    if (!hsimple) return;
00026 
00027    TCanvas *c1 = new TCanvas("c1","the fit canvas",500,400);
00028 
00029    TH1F *hpx = (TH1F*)hsimple->Get("hpx");
00030 
00031 // Creates a Root function based on function fitf above
00032    TF1 *func = new TF1("fitf",fitf,-2,2,3);
00033 
00034 // Sets initial values and parameter names
00035    func->SetParameters(100,0,1);
00036    func->SetParNames("Constant","Mean_value","Sigma");
00037 
00038 // Fit histogram in range defined by function
00039    hpx->Fit(func,"r");
00040 
00041 // Gets integral of function between fit limits
00042    printf("Integral of function = %g\n",func->Integral(-2,2));
00043 }

Generated on Tue Jul 5 15:44:08 2011 for ROOT_528-00b_version by  doxygen 1.5.1