rf503_wspaceread

'ORGANIZATION AND SIMULTANEOUS FITS' RooFit tutorial macro #503

Reading and using a workspace

The input file for self macro is generated by rf502_wspaceread.py

Author: Clemens Lange, Wouter Verkerke (C version)
This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Wednesday, November 30, 2022 at 11:23 AM.

In [1]:
import ROOT
Welcome to JupyROOT 6.27/01

Read workspace from file

Open input file with workspace (generated by rf503_wspacewrite)

In [2]:
f = ROOT.TFile("rf502_workspace_py.root")

Retrieve workspace from file

In [3]:
w = f.Get("w")

Retrieve pdf, data from workspace

Retrieve x, and data from workspace

In [4]:
x = w["x"]
model = w["model"]
data = w["modelData"]

Print structure of composite p.d.f.

In [5]:
model.Print("t")
0x8f872f0 RooAddPdf::model = 1/1 [Auto,Clean] 
  0x9243e50/V- RooChebychev::bkg = 1 [Auto,Dirty] 
    0x8ebe790/V- RooRealVar::x = 5
    0x2155980/V- RooRealVar::a0 = 0.5
    0x1c82d40/V- RooRealVar::a1 = 0
  0x92e1150/V- RooRealVar::bkgfrac = 0.5
  0x92d7af0/V- RooAddPdf::sig = 1/1 [Auto,Clean] 
    0x9263d20/V- RooGaussian::sig1 = 1 [Auto,Dirty] 
      0x8ebe790/V- RooRealVar::x = 5
      0x8dc3e70/V- RooRealVar::mean = 5
      0x3820ab0/V- RooRealVar::sigma1 = 0.5
    0x8bd5570/V- RooRealVar::sig1frac = 0.8
    0x90eb4e0/V- RooGaussian::sig2 = 1 [Auto,Dirty] 
      0x8ebe790/V- RooRealVar::x = 5
      0x8dc3e70/V- RooRealVar::mean = 5
      0x8cf6980/V- RooRealVar::sigma2 = 1

Fit model to data, plot model

Fit model to data

In [6]:
model.fitTo(data)
Out[6]:
<cppyy.gbl.RooFitResult object at 0x(nil)>
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: activating const optimization
[#1] INFO:Minimization --  The following expressions will be evaluated in cache-and-track mode: (bkg,sig1,sig2)
 **********
 **    1 **SET PRINT           1
 **********
 **********
 **    2 **SET NOGRAD
 **********
 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 a0           5.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
     2 a1           0.00000e+00  1.00000e-01    0.00000e+00  1.00000e+00
 MINUIT WARNING IN PARAM DEF
 ============== STARTING VALUE IS AT LIMIT.
 MINUIT WARNING IN PARAMETR
 ============== VARIABLE2 IS AT ITS LOWER ALLOWED LIMIT.
 MINUIT WARNING IN PARAMETR
 ============== VARIABLE2 BROUGHT BACK INSIDE LIMITS.
     3 bkgfrac      5.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
     4 mean         5.00000e+00  1.00000e+00    0.00000e+00  1.00000e+01
     5 sig1frac     8.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
 **********
 **    3 **SET ERR         0.5
 **********
 **********
 **    4 **SET PRINT           1
 **********
 **********
 **    5 **SET STR           1
 **********
 NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
 **********
 **    6 **MIGRAD        2500           1
 **********
 FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
 MINUIT WARNING IN MIGrad    
 ============== VARIABLE2 IS AT ITS LOWER ALLOWED LIMIT.
 START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
 FCN=1926.2 FROM MIGRAD    STATUS=INITIATE       37 CALLS          38 TOTAL
                     EDM= unknown      STRATEGY= 1      NO ERROR MATRIX       
  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  a0           5.00000e-01   1.00000e-01   0.00000e+00   2.31850e+00
   2  a1           7.50936e-02   1.00000e-01   5.53213e-01  -4.13475e-03
   3  bkgfrac      5.00000e-01   1.00000e-01   0.00000e+00   2.67741e+01
   4  mean         5.00000e+00   1.00000e+00   0.00000e+00  -1.91257e+01
   5  sig1frac     8.00000e-01   1.00000e-01   0.00000e+00   3.58316e-01
                               ERR DEF= 0.5
 MIGRAD MINIMIZATION HAS CONVERGED.
 MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=1924.14 FROM MIGRAD    STATUS=CONVERGED     144 CALLS         145 TOTAL
                     EDM=4.87682e-06    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  a0           5.05217e-01   8.02459e-02   4.72375e-03  -6.18134e-03
   2  a1           2.66450e-01   1.34580e-01   5.73280e-03   1.14496e-03
   3  bkgfrac      4.27468e-01   3.57738e-02   1.24965e-03  -3.49141e-02
   4  mean         5.00772e+00   3.27931e-02   1.96316e-04  -2.49989e-02
   5  sig1frac     6.40178e-01   9.71885e-02   4.22823e-03   5.49851e-03
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  5    ERR DEF=0.5
  6.495e-03  2.384e-03 -5.200e-04 -3.310e-04 -1.273e-03 
  2.384e-03  1.870e-02 -3.798e-03 -2.595e-05 -8.739e-03 
 -5.200e-04 -3.798e-03  1.282e-03 -4.006e-05  2.493e-03 
 -3.310e-04 -2.595e-05 -4.006e-05  1.075e-03 -1.593e-04 
 -1.273e-03 -8.739e-03  2.493e-03 -1.593e-04  9.578e-03 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2      3      4      5
        1  0.25219   1.000  0.216 -0.180 -0.125 -0.161
        2  0.79261   0.216  1.000 -0.776 -0.006 -0.653
        3  0.82176  -0.180 -0.776  1.000 -0.034  0.711
        4  0.15132  -0.125 -0.006 -0.034  1.000 -0.050
        5  0.73023  -0.161 -0.653  0.711 -0.050  1.000
 **********
 **    7 **SET ERR         0.5
 **********
 **********
 **    8 **SET PRINT           1
 **********
 **********
 **    9 **HESSE        2500
 **********
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=1924.14 FROM HESSE     STATUS=OK             31 CALLS         176 TOTAL
                     EDM=4.84164e-06    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                INTERNAL      INTERNAL  
  NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE   
   1  a0           5.05217e-01   8.02153e-02   9.44750e-04   1.04344e-02
   2  a1           2.66450e-01   1.33745e-01   2.29312e-04  -4.86007e-01
   3  bkgfrac      4.27468e-01   3.55504e-02   2.49929e-04  -1.45579e-01
   4  mean         5.00772e+00   3.27917e-02   3.92633e-05   1.54346e-03
   5  sig1frac     6.40178e-01   9.67194e-02   8.45646e-04   2.84164e-01
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  5    ERR DEF=0.5
  6.490e-03  2.349e-03 -5.109e-04 -3.311e-04 -1.251e-03 
  2.349e-03  1.846e-02 -3.735e-03 -2.611e-05 -8.585e-03 
 -5.109e-04 -3.735e-03  1.266e-03 -3.996e-05  2.454e-03 
 -3.311e-04 -2.611e-05 -3.996e-05  1.075e-03 -1.575e-04 
 -1.251e-03 -8.585e-03  2.454e-03 -1.575e-04  9.484e-03 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2      3      4      5
        1  0.25076   1.000  0.215 -0.178 -0.125 -0.159
        2  0.78957   0.215  1.000 -0.773 -0.006 -0.649
        3  0.81926  -0.178 -0.773  1.000 -0.034  0.708
        4  0.15103  -0.125 -0.006 -0.034  1.000 -0.049
        5  0.72707  -0.159 -0.649  0.708 -0.049  1.000
[#1] INFO:Minimization -- RooAbsMinimizerFcn::setOptimizeConst: deactivating const optimization

Plot data and PDF overlaid

In [7]:
xframe = x.frame(Title="Model and data read from workspace")
data.plotOn(xframe)
model.plotOn(xframe)
Out[7]:
<cppyy.gbl.RooPlot object at 0x9f1e470>

Overlay the background component of model with a dashed line

In [8]:
model.plotOn(xframe, Components="bkg", LineStyle="--")
Out[8]:
<cppyy.gbl.RooPlot object at 0x9f1e470>
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) directly selected PDF components: (bkg)
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) indirectly selected PDF components: ()

Overlay the background+sig2 components of model with a dotted line

In [9]:
model.plotOn(xframe, Components="bkg,sig2", LineStyle=":")
Out[9]:
<cppyy.gbl.RooPlot object at 0x9f1e470>
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) directly selected PDF components: (bkg,sig2)
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) indirectly selected PDF components: (sig)

Draw the frame on the canvas

In [10]:
c = ROOT.TCanvas("rf503_wspaceread", "rf503_wspaceread", 600, 600)
ROOT.gPad.SetLeftMargin(0.15)
xframe.GetYaxis().SetTitleOffset(1.4)
xframe.Draw()

c.SaveAs("rf503_wspaceread.png")
Info in <TCanvas::Print>: png file rf503_wspaceread.png has been created

Draw all canvases

In [11]:
from ROOT import gROOT 
gROOT.GetListOfCanvases().Draw()