#!/usr/bin/env python # coding: utf-8 # # rf505_asciicfg # Organization and simultaneous fits: reading and writing ASCII configuration files # # # # # **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, April 17, 2024 at 11:18 AM. # In[1]: from __future__ import print_function import ROOT # Create pdf # ------------------ # Construct gauss(x,m,s) # In[2]: x = ROOT.RooRealVar("x", "x", -10, 10) m = ROOT.RooRealVar("m", "m", 0, -10, 10) s = ROOT.RooRealVar("s", "s", 1, -10, 10) gauss = ROOT.RooGaussian("g", "g", x, m, s) # Construct poly(x,p0) # In[3]: p0 = ROOT.RooRealVar("p0", "p0", 0.01, 0.0, 1.0) poly = ROOT.RooPolynomial("p", "p", x, [p0]) # model = f*gauss(x) + (1-f)*poly(x) # In[4]: f = ROOT.RooRealVar("f", "f", 0.5, 0.0, 1.0) model = ROOT.RooAddPdf("model", "model", [gauss, poly], [f]) # Fit model to toy data # ----------------------------------------- # In[5]: d = model.generate({x}, 1000) model.fitTo(d, PrintLevel=-1) # Write parameters to ASCII file # ----------------------------------------------------------- # Obtain set of parameters # In[6]: params = model.getParameters({x}) # Write parameters to file # In[7]: params.writeToFile("rf505_asciicfg_example.txt") # Read parameters from ASCII file # ---------------------------------------------------------------- # Read parameters from file # In[8]: params.readFromFile("rf505_asciicfg_example.txt") params.Print("v") configFile = ROOT.gROOT.GetTutorialDir().Data() + "/roofit/rf505_asciicfg.txt" # Read parameters from section 'Section2' of file # In[9]: params.readFromFile(configFile, "", "Section2") params.Print("v") # Read parameters from section 'Section3' of file. Mark all # variables that were processed with the "READ" attribute # In[10]: params.readFromFile(configFile, "READ", "Section3") # Print the list of parameters that were not read from Section3 # In[11]: print("The following parameters of the were _not_ read from Section3: ", params.selectByAttrib("READ", False)) # Read parameters from section 'Section4' of file, contains # 'include file' statement of rf505_asciicfg_example.txt # so that we effective read the same # In[12]: params.readFromFile(configFile, "", "Section4") params.Print("v")