def SFB_Lookup(self, Y,HF ): weightSFb,weightSFb_down,weightSFb_up = 1.0,1.0,1.0 #print [weightSFb,weightSFb_down,weightSFb_up] ptminsfb = [100.0,140.0,200.0,300.0,600.0,] ptmaxsfb = [140.0,200.0,300.0,600.0,1000.0,] if HF==5: SFb = TFormula("SFb","0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x)))") SFb_down = [ TFormula("SFb_down_1","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.010811596177518368"), TFormula("SFb_down_2","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.010882497765123844"), TFormula("SFb_down_3","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.013456921093165874"), TFormula("SFb_down_4","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.017094610258936882"), TFormula("SFb_down_5","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.02186630479991436") ] SFb_up = [ TFormula("SFb_up_1","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.010811596177518368"), TFormula("SFb_up_2","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.010882497765123844"), TFormula("SFb_up_3","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.013456921093165874"), TFormula("SFb_up_4","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.017094610258936882"), TFormula("SFb_up_5","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.02186630479991436") ] elif HF==4: SFb = TFormula("SFb","0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x)))") SFb_down = [ TFormula("SFb_down_1","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.027028990909457207"), TFormula("SFb_down_2","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.027206243947148323"), TFormula("SFb_down_3","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.033642303198575974"), TFormula("SFb_down_4","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.04273652657866478"), TFormula("SFb_down_5","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))-0.054665762931108475") ] SFb_up = [ TFormula("SFb_up_1","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.027028990909457207"), TFormula("SFb_up_2","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.027206243947148323"), TFormula("SFb_up_3","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.033642303198575974"), TFormula("SFb_up_4","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.04273652657866478"), TFormula("SFb_up_5","(0.887973*((1.+(0.0523821*x))/(1.+(0.0460876*x))))+0.054665762931108475") ] else: SFb = TFormula("SFb","1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x") SFb_down = [ TFormula("SFb_down_1","(1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x)*(1-(0.0996438+-8.33354e-05*x+4.74359e-08*x*x))"), TFormula("SFb_down_2","(1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x)*(1-(0.0996438+-8.33354e-05*x+4.74359e-08*x*x))"), TFormula("SFb_down_3","(1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x)*(1-(0.0996438+-8.33354e-05*x+4.74359e-08*x*x))"), TFormula("SFb_down_4","(1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x)*(1-(0.0996438+-8.33354e-05*x+4.74359e-08*x*x))"), TFormula("SFb_down_5","(1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x)*(1-(0.0996438+-8.33354e-05*x+4.74359e-08*x*x))") ] SFb_up = [ TFormula("SFb_up_1","(1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x)*(1+(0.0996438+-8.33354e-05*x+4.74359e-08*x*x))"), TFormula("SFb_up_2","(1.13904+-0.000594946*x+1.97303e-06*x*x+-1.38194e-09*x*x*x)*(1+(0.0996438+-8.33354e-05*x+4.74359e-08*x*x))"),
def formulaSBratio(self): from ROOT import TFormula text = self.main.SBratio text = text.replace("S", "x") text = text.replace("B", "y") self.Mformula = TFormula("SBratio", text) text = self.main.SBerror text = text.replace("ES", "z") text = text.replace("EB", "t") text = text.replace("S", "x") text = text.replace("B", "y") self.Eformula = TFormula("SBerror", text)
def getNparams(self): '''Get the number of parameters in the formula (not counting "x" or "y"). Converts the formula formating and uses ROOT's TFormula to count. Returns: int: Number of parameters in the fit (not counting "x" or "y"). ''' return TFormula('tempFormula',roofit_form_to_TF1(self.formula)).GetNpar()
def suggestSBerror(self): # create a TFormula with the SBratio formula text = self.SBratio.replace("S", "x") text = text.replace("B", "y") from ROOT import TFormula ref = TFormula('SBratio', text) ref.Optimize() # Loop over SBerror formula and comparing for k, v in Main.SBformula.iteritems(): text = k.replace("S", "x") text = text.replace("B", "y") error = TFormula('SBerror', text) error.Optimize() if ref.GetExpFormula() == error.GetExpFormula(): logging.info( "Formula corresponding to the uncertainty calculation has been found and set to the variable main.SBerror :" ) logging.info(' ' + v) self.SBerror = v return True # Loop over SBerror formula and comparing # reverse S and B for k, v in Main.SBformula.iteritems(): text = k.replace("S", "y") text = text.replace("B", "x") error = TFormula('SBerror', text) error.Optimize() if ref.GetExpFormula() == error.GetExpFormula(): logging.info( "Formula corresponding to the uncertainty calculation has been found and set to the variable main.SBerror :" ) v = v.replace('ES', 'ZZ') v = v.replace('EB', 'TT') v = v.replace('S', 'SS') v = v.replace('B', 'BB') v = v.replace('SS', 'B') v = v.replace('BB', 'S') v = v.replace('ZZ', 'EB') v = v.replace('TT', 'ES') logging.info(' ' + v) self.SBerror = v return True
def checkSBratio(text): logging.info("Checking the formula ...") text = text.replace("ES", "z") text = text.replace("EB", "t") text = text.replace("S", "x") text = text.replace("B", "y") from ROOT import TFormula formula = TFormula() test = formula.Compile(text) return (test == 0)
c1 = TCanvas( 'c1', 'The FillRandom example', 200, 10, 700, 900 ) c1.SetFillColor( 18 ) pad1 = TPad( 'pad1', 'The pad with the function', 0.05, 0.50, 0.95, 0.95, 21 ) pad2 = TPad( 'pad2', 'The pad with the histogram', 0.05, 0.05, 0.95, 0.45, 21 ) pad1.Draw() pad2.Draw() pad1.cd() gBenchmark.Start( 'fillrandom' ) # # A function (any dimension) or a formula may reference # an already defined formula # form1 = TFormula( 'form1', 'abs(sin(x)/x)' ) sqroot = TF1( 'sqroot', 'x*gaus(0) + [3]*form1', 0, 10 ) sqroot.SetParameters( 10, 4, 1, 20 ) pad1.SetGridx() pad1.SetGridy() pad1.GetFrame().SetFillColor( 42 ) pad1.GetFrame().SetBorderMode( -1 ) pad1.GetFrame().SetBorderSize( 5 ) sqroot.SetLineColor( 4 ) sqroot.SetLineWidth( 6 ) sqroot.Draw() lfunction = TPaveLabel( 5, 39, 9.8, 46, 'The sqroot function' ) lfunction.SetFillColor( 41 ) lfunction.Draw() c1.Update()
# # To see the graphics output of this macro, click begin_html <a href="gif/formula1.gif">here</a>. end_html # from ROOT import TCanvas, TFormula, TF1 from ROOT import gROOT, gObjectTable c1 = TCanvas('c1', 'Example with Formula', 200, 10, 700, 500) # We create a formula object and compute the value of this formula # for two different values of the x variable. form1 = TFormula('form1', 'sqrt(abs(x))') form1.Eval(2) form1.Eval(-45) # Create a one dimensional function and draw it fun1 = TF1('fun1', 'abs(sin(x)/x)', 0, 10) c1.SetGridx() c1.SetGridy() fun1.Draw() c1.Update() # Before leaving this demo, we print the list of objects known to ROOT # if (gObjectTable): gObjectTable.Print()
def test_evaluate(): # create functions and histograms f1 = TF1("f1", "x") f2 = TF2("f2", "x*y") f3 = TF3("f3", "x*y*z") h1 = TH1D("h1", "", 10, 0, 1) h1.FillRandom("f1") h2 = TH2D("h2", "", 10, 0, 1, 10, 0, 1) h2.FillRandom("f2") h3 = TH3D("h3", "", 10, 0, 1, 10, 0, 1, 10, 0, 1) h3.FillRandom("f3") # generate random arrays arr_1d = np.random.rand(5) arr_2d = np.random.rand(5, 2) arr_3d = np.random.rand(5, 3) arr_4d = np.random.rand(5, 4) # evaluate the functions assert_array_equal(rnp.evaluate(f1, arr_1d), map(f1.Eval, arr_1d)) assert_array_equal(rnp.evaluate(f1.GetTitle(), arr_1d), map(f1.Eval, arr_1d)) assert_array_equal(rnp.evaluate(f2, arr_2d), [f2.Eval(*x) for x in arr_2d]) assert_array_equal(rnp.evaluate(f2.GetTitle(), arr_2d), [f2.Eval(*x) for x in arr_2d]) assert_array_equal(rnp.evaluate(f3, arr_3d), [f3.Eval(*x) for x in arr_3d]) assert_array_equal(rnp.evaluate(f3.GetTitle(), arr_3d), [f3.Eval(*x) for x in arr_3d]) # 4d formula f4 = TFormula('test', 'x*y+z*t') assert_array_equal(rnp.evaluate(f4, arr_4d), [f4.Eval(*x) for x in arr_4d]) # evaluate the histograms assert_array_equal(rnp.evaluate(h1, arr_1d), [h1.GetBinContent(h1.FindBin(x)) for x in arr_1d]) assert_array_equal(rnp.evaluate(h2, arr_2d), [h2.GetBinContent(h2.FindBin(*x)) for x in arr_2d]) assert_array_equal(rnp.evaluate(h3, arr_3d), [h3.GetBinContent(h3.FindBin(*x)) for x in arr_3d]) # create a graph g = TGraph(2) g.SetPoint(0, 0, 1) g.SetPoint(1, 1, 2) assert_array_equal(rnp.evaluate(g, [0, .5, 1]), [1, 1.5, 2]) from ROOT import TSpline3 s = TSpline3("spline", g) assert_array_equal(rnp.evaluate(s, [0, .5, 1]), map(s.Eval, [0, .5, 1])) # test exceptions assert_raises(TypeError, rnp.evaluate, object(), [1, 2, 3]) assert_raises(ValueError, rnp.evaluate, h1, arr_2d) assert_raises(ValueError, rnp.evaluate, h2, arr_3d) assert_raises(ValueError, rnp.evaluate, h2, arr_1d) assert_raises(ValueError, rnp.evaluate, h3, arr_1d) assert_raises(ValueError, rnp.evaluate, h3, arr_2d) assert_raises(ValueError, rnp.evaluate, f1, arr_2d) assert_raises(ValueError, rnp.evaluate, f2, arr_3d) assert_raises(ValueError, rnp.evaluate, f2, arr_1d) assert_raises(ValueError, rnp.evaluate, f3, arr_1d) assert_raises(ValueError, rnp.evaluate, f3, arr_2d) assert_raises(ValueError, rnp.evaluate, g, arr_2d) assert_raises(ValueError, rnp.evaluate, s, arr_2d) assert_raises(ValueError, rnp.evaluate, "f", arr_1d) assert_raises(ValueError, rnp.evaluate, "x*y", arr_1d) assert_raises(ValueError, rnp.evaluate, "x", arr_2d) assert_raises(ValueError, rnp.evaluate, "x*y", arr_3d)
from ROOT import TCanvas, TPad, TFormula, TF1, TPaveLabel, TH1F, TFile from ROOT import gROOT, gBenchmark c1 = TCanvas("c1,", "c1", 200, 10, 700, 900) c1.SetFillColor(18) pad1 = TPad("pad1", "pad1", 0.05, 0.50, 0.95, 0.95, 21) pad2 = TPad("pad2", "pad2", 0.05, 0.05, 0.95, 0.45, 21) pad1.Draw() pad2.Draw() gBenchmark.Start("fillrandom") form1 = TFormula("form1", "abs(sin(x)/x)") sqroot = TF1("sqroot", "x*gaus(0) + [3]*form1", 0, 10) sqroot.SetParameters(10, 4, 1, 20) #pad1.SetGridx() pad2.SetGridy() pad1.SetGrid() c1.Update() pad1.cd() sqroot.Draw() pad1.Modified() pad1.Update() pad2.cd() pad2.GetFrame().SetFillColor(42) pad2.GetFrame().SetBorderMode(-1) pad2.GetFrame().SetBorderSize(5) h1f = TH1F("h1f", "h1f", 200, 0, 10) h1f.FillRandom("sqroot", 10000)