multifit.C

Go to the documentation of this file.
00001 #include "TH1.h"
00002 #include "TF1.h"
00003 
00004 void multifit() {
00005 //  Fitting multiple functions to different ranges of a 1-D histogram
00006 // To see the output of this macro, click begin_html <a href="gif/multifit.gif" >here</a> end_html
00007 //      Example showing how to fit in a sub-range of an histogram
00008 //  An histogram is created and filled with the bin contents and errors
00009 //  defined in the table below.
00010 //  3 gaussians are fitted in sub-ranges of this histogram.
00011 //  A new function (a sum of 3 gaussians) is fitted on another subrange
00012 //  Note that when fitting simple functions, such as gaussians, the initial
00013 //  values of parameters are automatically computed by ROOT.
00014 //  In the more complicated case of the sum of 3 gaussians, the initial values
00015 //  of parameters must be given. In this particular case, the initial values
00016 //  are taken from the result of the individual fits.
00017 //Author: Rene Brun
00018 
00019    const Int_t np = 49;
00020    Float_t x[np] = {1.913521, 1.953769, 2.347435, 2.883654, 3.493567,
00021                     4.047560, 4.337210, 4.364347, 4.563004, 5.054247,
00022                     5.194183, 5.380521, 5.303213, 5.384578, 5.563983,
00023                     5.728500, 5.685752, 5.080029, 4.251809, 3.372246,
00024                     2.207432, 1.227541, 0.8597788,0.8220503,0.8046592,
00025                     0.7684097,0.7469761,0.8019787,0.8362375,0.8744895,
00026                     0.9143721,0.9462768,0.9285364,0.8954604,0.8410891,
00027                     0.7853871,0.7100883,0.6938808,0.7363682,0.7032954,
00028                     0.6029015,0.5600163,0.7477068,1.188785, 1.938228,
00029                     2.602717, 3.472962, 4.465014, 5.177035};
00030 
00031    TH1F *h = new TH1F("h","Example of several fits in subranges",np,85,134);
00032    h->SetMaximum(7);
00033 
00034    for (int i=0;i<np;i++) {
00035       h->SetBinContent(i+1,x[i]);
00036    }
00037 
00038    Double_t par[9];
00039    TF1 *g1    = new TF1("g1","gaus",85,95);
00040    TF1 *g2    = new TF1("g2","gaus",98,108);
00041    TF1 *g3    = new TF1("g3","gaus",110,121);
00042    TF1 *total = new TF1("total","gaus(0)+gaus(3)+gaus(6)",85,125);
00043    total->SetLineColor(2);
00044    h->Fit(g1,"R");
00045    h->Fit(g2,"R+");
00046    h->Fit(g3,"R+");
00047    g1->GetParameters(&par[0]);
00048    g2->GetParameters(&par[3]);
00049    g3->GetParameters(&par[6]);
00050    total->SetParameters(par);
00051    h->Fit(total,"R+");
00052 }
00053 

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