Logo ROOT   6.12/07
Reference Guide
multifit.py
Go to the documentation of this file.
1 ## \file
2 ## \ingroup tutorial_pyroot
3 ## \notebook
4 ## Example showing how to fit in a sub-range of an histogram
5 ## An histogram is created and filled with the bin contents and errors
6 ## defined in the table below.
7 ## 3 gaussians are fitted in sub-ranges of this histogram.
8 ## A new function (a sum of 3 gaussians) is fitted on another subrange
9 ## Note that when fitting simple functions, such as gaussians, the initial
10 ## values of parameters are automatically computed by ROOT.
11 ## In the more complicated case of the sum of 3 gaussians, the initial values
12 ## of parameters must be given. In this particular case, the initial values
13 ## are taken from the result of the individual fits.
14 ##
15 ## \macro_output
16 ## \macro_code
17 ##
18 ## \author Wim Lavrijsen
19 
20 from ROOT import TH1F, TF1
21 from ROOT import gROOT
22 from array import array
23 
24 x = ( 1.913521, 1.953769, 2.347435, 2.883654, 3.493567,
25  4.047560, 4.337210, 4.364347, 4.563004, 5.054247,
26  5.194183, 5.380521, 5.303213, 5.384578, 5.563983,
27  5.728500, 5.685752, 5.080029, 4.251809, 3.372246,
28  2.207432, 1.227541, 0.8597788,0.8220503,0.8046592,
29  0.7684097,0.7469761,0.8019787,0.8362375,0.8744895,
30  0.9143721,0.9462768,0.9285364,0.8954604,0.8410891,
31  0.7853871,0.7100883,0.6938808,0.7363682,0.7032954,
32  0.6029015,0.5600163,0.7477068,1.188785, 1.938228,
33  2.602717, 3.472962, 4.465014, 5.177035 )
34 
35 np = len(x)
36 h = TH1F( 'h', 'Example of several fits in subranges', np, 85, 134 )
37 h.SetMaximum( 7 )
38 
39 for i in range(np):
40  h.SetBinContent( i+1, x[i] )
41 
42 par = array( 'd', 9*[0.] )
43 g1 = TF1( 'g1', 'gaus', 85, 95 )
44 g2 = TF1( 'g2', 'gaus', 98, 108 )
45 g3 = TF1( 'g3', 'gaus', 110, 121 )
46 
47 total = TF1( 'total', 'gaus(0)+gaus(3)+gaus(6)', 85, 125 )
48 total.SetLineColor( 2 )
49 h.Fit( g1, 'R' )
50 h.Fit( g2, 'R+' )
51 h.Fit( g3, 'R+' )
52 
53 par1 = g1.GetParameters()
54 par2 = g2.GetParameters()
55 par3 = g3.GetParameters()
56 
57 par[0], par[1], par[2] = par1[0], par1[1], par1[2]
58 par[3], par[4], par[5] = par2[0], par2[1], par2[2]
59 par[6], par[7], par[8] = par3[0], par3[1], par3[2]
60 
61 total.SetParameters( par )
62 h.Fit( total, 'R+' )
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:567
1-Dim function class
Definition: TF1.h:211