2009年7月10日金曜日

How to use pyROOT 応用編 その3

任意関数での ヒストグラムの fitting

  1. #/bin/usr/env python  
  2.   
  3. import sys, math  
  4. import ROOT  
  5.   
  6.   
  7. class Linear:  
  8.     def __call__(self, x, par):  
  9.         return par[0] + x[0] * par[1]  
  10.   
  11. if __name__=='__main__':  
  12.   
  13.     cv  = ROOT.TCanvas("cv""Fitting Example"20010700500)  
  14.   
  15.     # create a linear function for fitting  
  16.     f = ROOT.TF1('f', Linear(), -1.1.2)  
  17.   
  18.     # create histogram ( identify, Title, ?, xmin, xmax )  
  19.     h = ROOT.TH1F('h''px'100, -1.1.)  
  20.     f2= ROOT.TF1('f2''6. + x * 4.5', -11.)  
  21.   
  22.     h.FillRandom('f2'10000)  
  23.       
  24.   
  25.     # fits the hitstogram with the 'Linear' function  
  26.     h.Fit(f)  
  27.   
  28.     # export  
  29.     cv.Print("graphtest4.png")  
  30.   
  31.     # print results  
  32.     par = f.GetParameters()  
  33.     print 'fitting result: const = ', par[0], ', pitch = ', par[1]  


実行結果


$ python graphtest4.py
FCN=101.388 FROM MIGRAD STATUS=CONVERGED 29 CALLS 30 TOTAL
EDM=2.65063e-16 STRATEGY= 1 ERROR MATRIX ACCURATE
EXT PARAMETER STEP FIRST
NO. NAME VALUE ERROR SIZE DERIVATIVE
1 p0 9.89861e+01 9.94918e-01 4.29016e-03 -2.65160e-08
2 p1 7.57130e+01 1.55891e+00 6.72214e-03 8.21832e-09
Info in : file graphtest4.png has been created
fitting result: const = 98.9861239576 , pitch = 75.7130326833



0 件のコメント:

コメントを投稿