Rf 2 0 3_Ranges

Addition and convolution: fitting and plotting in sub ranges

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 Saturday, November 28, 2020 at 10:40 AM.

In [1]:
from __future__ import print_function
import ROOT
Welcome to JupyROOT 6.23/01

Set up model

Construct observables x

In [2]:
x = ROOT.RooRealVar("x", "x", -10, 10)
RooFit v3.60 -- Developed by Wouter Verkerke and David Kirkby 
                Copyright (C) 2000-2013 NIKHEF, University of California & Stanford University
                All rights reserved, please read http://roofit.sourceforge.net/license.txt

Construct gaussx(x,mx,1)

In [3]:
mx = ROOT.RooRealVar("mx", "mx", 0, -10, 10)
gx = ROOT.RooGaussian("gx", "gx", x, mx, ROOT.RooFit.RooConst(1))

px = 1 (flat in x)

In [4]:
px = ROOT.RooPolynomial("px", "px", x)

model = f*gx + (1-f)px

In [5]:
f = ROOT.RooRealVar("f", "f", 0., 1.)
model = ROOT.RooAddPdf(
    "model", "model", ROOT.RooArgList(gx, px), ROOT.RooArgList(f))

Generated 10000 events in (x,y) from pdf model

In [6]:
modelData = model.generate(ROOT.RooArgSet(x), 10000)

Fit full range

Fit pdf to all data

In [7]:
r_full = model.fitTo(modelData, ROOT.RooFit.Save(ROOT.kTRUE))
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
[#1] INFO:Minization --  The following expressions have been identified as constant and will be precalculated and cached: (px)
[#1] INFO:Minization --  The following expressions will be evaluated in cache-and-track mode: (gx)
 **********
 **    1 **SET PRINT           1
 **********
 **********
 **    2 **SET NOGRAD
 **********
 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 f            5.00000e-01  1.00000e-01    0.00000e+00  1.00000e+00
     2 mx           0.00000e+00  2.00000e+00   -1.00000e+01  1.00000e+01
 **********
 **    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        1000           1
 **********
 FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
 START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
 FCN=25940.4 FROM MIGRAD    STATUS=INITIATE        8 CALLS           9 TOTAL
                     EDM= unknown      STRATEGY= 1      NO ERROR MATRIX       
  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  f            5.00000e-01   1.00000e-01   2.01358e-01  -5.48446e+01
   2  mx           0.00000e+00   2.00000e+00   2.01358e-01   6.82864e+02
                               ERR DEF= 0.5
 MIGRAD MINIMIZATION HAS CONVERGED.
 MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=25939.4 FROM MIGRAD    STATUS=CONVERGED      23 CALLS          24 TOTAL
                     EDM=7.58337e-06    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  f            5.04406e-01   6.32281e-03   1.40982e-03   6.67377e-02
   2  mx          -2.16053e-02   1.77430e-02   1.97826e-04  -1.47892e+00
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
  3.998e-05  3.708e-07 
  3.708e-07  3.148e-04 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2
        1  0.00331   1.000  0.003
        2  0.00331   0.003  1.000
 **********
 **    7 **SET ERR         0.5
 **********
 **********
 **    8 **SET PRINT           1
 **********
 **********
 **    9 **HESSE        1000
 **********
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=25939.4 FROM HESSE     STATUS=OK             10 CALLS          34 TOTAL
                     EDM=7.58116e-06    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                INTERNAL      INTERNAL  
  NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE   
   1  f            5.04406e-01   6.32281e-03   2.81963e-04   8.81295e-03
   2  mx          -2.16053e-02   1.77430e-02   3.95651e-05  -2.16053e-03
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
  3.998e-05  4.063e-07 
  4.063e-07  3.148e-04 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2
        1  0.00362   1.000  0.004
        2  0.00362   0.004  1.000
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization

Fit partial range

Define "signal" range in x as [-3,3]

In [8]:
x.setRange("signal", -3, 3)
[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'signal' created with bounds [-3,3]

Fit pdf only to data in "signal" range

In [9]:
r_sig = model.fitTo(modelData, ROOT.RooFit.Save(
    ROOT.kTRUE), ROOT.RooFit.Range("signal"))
[#1] INFO:Fitting -- RooAbsOptTestStatistic::ctor(nll_model_modelData) constructing test statistic for sub-range named signal
[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'NormalizationRangeForsignal' created with bounds [-10,10]
[#1] INFO:Eval -- RooRealVar::setRange(x) new range named 'fit_nll_model_modelData' created with bounds [-3,3]
[#1] INFO:Fitting -- RooAbsOptTestStatistic::ctor(nll_model_modelData) fixing interpretation of coefficients of any RooAddPdf to full domain of observables 
[#1] INFO:Minization -- RooMinimizer::optimizeConst: activating const optimization
[#1] INFO:Minization --  The following expressions have been identified as constant and will be precalculated and cached: (px)
[#1] INFO:Minization --  The following expressions will be evaluated in cache-and-track mode: (gx)
 **********
 **   10 **SET PRINT           1
 **********
 **********
 **   11 **SET NOGRAD
 **********
 PARAMETER DEFINITIONS:
    NO.   NAME         VALUE      STEP SIZE      LIMITS
     1 f            5.04406e-01  6.32281e-03    0.00000e+00  1.00000e+00
     2 mx          -2.16053e-02  1.77430e-02   -1.00000e+01  1.00000e+01
 **********
 **   12 **SET ERR         0.5
 **********
 **********
 **   13 **SET PRINT           1
 **********
 **********
 **   14 **SET STR           1
 **********
 NOW USING STRATEGY  1: TRY TO BALANCE SPEED AGAINST RELIABILITY
 **********
 **   15 **MIGRAD        1000           1
 **********
 FIRST CALL TO USER FUNCTION AT NEW START POINT, WITH IFLAG=4.
 START MIGRAD MINIMIZATION.  STRATEGY  1.  CONVERGENCE WHEN EDM .LT. 1.00e-03
 FCN=10339.9 FROM MIGRAD    STATUS=INITIATE        6 CALLS           7 TOTAL
                     EDM= unknown      STRATEGY= 1      NO ERROR MATRIX       
  EXT PARAMETER               CURRENT GUESS       STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  f            5.04406e-01   6.32281e-03   1.26465e-02   2.65620e+01
   2  mx          -2.16053e-02   1.77430e-02   1.77431e-03  -2.76970e+00
                               ERR DEF= 0.5
 MIGRAD MINIMIZATION HAS CONVERGED.
 MIGRAD WILL VERIFY CONVERGENCE AND ERROR MATRIX.
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=10339.5 FROM MIGRAD    STATUS=CONVERGED      28 CALLS          29 TOTAL
                     EDM=3.38795e-07    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                   STEP         FIRST   
  NO.   NAME      VALUE            ERROR          SIZE      DERIVATIVE 
   1  f            4.90142e-01   1.61947e-02   2.27369e-03   1.69088e-02
   2  mx          -2.17006e-02   1.79160e-02   1.25822e-04   1.06356e-01
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
  2.624e-04  3.238e-06 
  3.238e-06  3.210e-04 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2
        1  0.01116   1.000  0.011
        2  0.01116   0.011  1.000
 **********
 **   16 **SET ERR         0.5
 **********
 **********
 **   17 **SET PRINT           1
 **********
 **********
 **   18 **HESSE        1000
 **********
 COVARIANCE MATRIX CALCULATED SUCCESSFULLY
 FCN=10339.5 FROM HESSE     STATUS=OK             10 CALLS          39 TOTAL
                     EDM=3.38988e-07    STRATEGY= 1      ERROR MATRIX ACCURATE 
  EXT PARAMETER                                INTERNAL      INTERNAL  
  NO.   NAME      VALUE            ERROR       STEP SIZE       VALUE   
   1  f            4.90142e-01   1.61948e-02   9.09474e-05  -1.97171e-02
   2  mx          -2.17006e-02   1.79161e-02   2.51643e-05  -2.17006e-03
                               ERR DEF= 0.5
 EXTERNAL ERROR MATRIX.    NDIM=  25    NPAR=  2    ERR DEF=0.5
  2.624e-04  3.402e-06 
  3.402e-06  3.210e-04 
 PARAMETER  CORRELATION COEFFICIENTS  
       NO.  GLOBAL      1      2
        1  0.01172   1.000  0.012
        2  0.01172   0.012  1.000
[#1] INFO:Minization -- RooMinimizer::optimizeConst: deactivating const optimization

Plot/print results

Make plot frame in x and add data and fitted model

In [10]:
frame = x.frame(ROOT.RooFit.Title("Fitting a sub range"))
modelData.plotOn(frame)
model.plotOn(
    frame, ROOT.RooFit.Range("Full"), ROOT.RooFit.LineStyle(
        ROOT.kDashed), ROOT.RooFit.LineColor(
            ROOT.kRed)) # Add shape in full ranged dashed
model.plotOn(frame)  # By default only fitted range is shown
Out[10]:
<cppyy.gbl.RooPlot object at 0x8128680>
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f was fitted in a subrange and no explicit NormRange() was specified. Plotting / normalising in fit range. To override, do one of the following
	- Clear the automatic fit range attribute: <pdf>.setStringAttribute("fitrange", nullptr);
	- Explicitly specify the plotting range: Range("<rangeName>").
	- Explicitly specify where to compute the normalisation: NormRange("<rangeName>").
	The default (full) range can be denoted with Range("") / NormRange("").
[#0] ERROR:Plotting -- Range 'Full' not defined for variable 'x'. Ignoring ...
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) only plotting range 'Full'
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f. curve is normalized using explicit choice of ranges 'fit_nll_model_modelData'
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f was fitted in a subrange and no explicit Range() and NormRange() was specified. Plotting / normalising in fit range. To override, do one of the following
	- Clear the automatic fit range attribute: <pdf>.setStringAttribute("fitrange", nullptr);
	- Explicitly specify the plotting range: Range("<rangeName>").
	- Explicitly specify where to compute the normalisation: NormRange("<rangeName>").
	The default (full) range can be denoted with Range("") / NormRange("").
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) only plotting range 'fit_nll_model_modelData'
[#1] INFO:Plotting -- RooAbsPdf::plotOn(model) p.d.f. curve is normalized using explicit choice of ranges 'fit_nll_model_modelData'

Print fit results

In [11]:
print("result of fit on all data ")
r_full.Print()
print("result of fit in in signal region (note increased error on signal fraction)")
r_sig.Print()
result of fit on all data 
result of fit in in signal region (note increased error on signal fraction)

  RooFitResult: minimized FCN value: 25939.4, estimated distance to minimum: 7.58116e-06
                covariance matrix quality: Full, accurate covariance matrix
                Status : MINIMIZE=0 HESSE=0 

    Floating Parameter    FinalValue +/-  Error   
  --------------------  --------------------------
                     f    5.0441e-01 +/-  6.32e-03
                    mx   -2.1605e-02 +/-  1.77e-02


  RooFitResult: minimized FCN value: 10339.5, estimated distance to minimum: 3.38988e-07
                covariance matrix quality: Full, accurate covariance matrix
                Status : MINIMIZE=0 HESSE=0 

    Floating Parameter    FinalValue +/-  Error   
  --------------------  --------------------------
                     f    4.9014e-01 +/-  1.62e-02
                    mx   -2.1701e-02 +/-  1.79e-02

Draw frame on canvas

In [12]:
c = ROOT.TCanvas("rf203_ranges", "rf203_ranges", 600, 600)
ROOT.gPad.SetLeftMargin(0.15)
frame.GetYaxis().SetTitleOffset(1.4)
frame.Draw()

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

Draw all canvases

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