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)
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()
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)
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 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))"),
class CutFlow: def __init__(self, dataset, selection, lumi, main): self.dataset = dataset self.selection = selection self.lumi = lumi self.main = main self.Ntotal = 0 self.Nselected = [] self.Nrejected = [] self.eff = [] self.effcumu = [] self.formulaSBratio() 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 calculateBSratio(self, B, eB, S, eS): value = Measure() value.mean = self.Mformula.Eval(S, B) value.error = self.Eformula.Eval(S, B, eS, eB) return value @staticmethod def binomialNEventError(k, N): if N == 0: return 0. else: return sqrt(float(k * abs(N - k)) / float(N)) @staticmethod def binomialError(k, N): if N == 0: return 0. else: return sqrt(float(k * abs(N - k)) / float(N * N * N)) def initializeFromCutflow(self, cutflows): # Ntotal self.Ntotal = Measure() for item in cutflows: self.Ntotal.mean += item.Ntotal.mean self.Ntotal.error += item.Ntotal.error**2 self.Ntotal.error = sqrt(self.Ntotal.error) # Prepare vectors for i in range(0, len(cutflows[0].Nselected)): self.Nselected.append(Measure()) self.Nrejected.append(Measure()) self.eff.append(Measure()) self.effcumu.append(Measure()) # Fill selected and rejected for iset in range(0, len(cutflows)): for icut in range(0, len(cutflows[iset].Nselected)): self.Nselected[icut].mean += cutflows[iset].Nselected[ icut].mean self.Nrejected[icut].mean += cutflows[iset].Nrejected[ icut].mean self.Nselected[icut].error += cutflows[iset].Nselected[ icut].error**2 self.Nrejected[icut].error += cutflows[iset].Nrejected[ icut].error**2 self.Nselected[icut].error = sqrt(self.Nselected[icut].error) self.Nrejected[icut].error = sqrt(self.Nrejected[icut].error) # Compute efficiencies for i in range(0, len(self.eff)): if self.Ntotal.mean != 0: self.effcumu[i].mean = float(self.Nselected[i].mean) / float( self.Ntotal.mean) self.effcumu[i].error = CutFlow.binomialError( self.Nselected[i].mean, self.Ntotal.mean) if i == 0: if self.Ntotal.mean != 0: self.eff[i].mean = float(self.Nselected[i].mean) / float( self.Ntotal.mean) self.eff[i].error = CutFlow.binomialError( self.Nselected[i].mean, self.Ntotal.mean) else: if self.Nselected[i - 1].mean != 0: self.eff[i].mean = float(self.Nselected[i].mean) / float( self.Nselected[i - 1].mean) self.eff[i].error = CutFlow.binomialError( self.Nselected[i].mean, self.Nselected[i - 1].mean) def initializeFromFile(self, file): # Getting cuts from ROOT file cut = file.Get("cuts/cuts", "TVectorT<float>") if cut is None: return False # Preparing architecture for vectors self.Ntotal = Measure() for iabscut in range(0, len(self.selection)): if self.selection[iabscut].__class__.__name__ != "Cut": continue self.Nselected.append(Measure()) self.Nrejected.append(Measure()) self.eff.append(Measure()) self.effcumu.append(Measure()) # Extracting Nselected information for icut in range(0, len(self.Nselected)): self.Nselected[icut].mean = cut[icut] # Extracting Ntotal self.Ntotal.mean = self.dataset.measured_n return True def calculate(self): # Calculating Nrejected for icut in range(0, len(self.Nselected)): if icut == 0: self.Nrejected[ icut].mean = self.Ntotal.mean - self.Nselected[icut].mean else: self.Nrejected[icut].mean = self.Nselected[ icut - 1].mean - self.Nselected[icut].mean # Calculating errors on Naccepted and Nrejected for icut in range(0, len(self.Nselected)): if icut == 0: self.Nselected[icut].error = CutFlow.binomialNEventError( self.Nselected[icut].mean, self.Ntotal.mean) self.Nrejected[icut].error = CutFlow.binomialNEventError( self.Nrejected[icut].mean, self.Ntotal.mean) else: self.Nselected[icut].error = CutFlow.binomialNEventError( self.Nselected[icut].mean, self.Ntotal.mean) self.Nrejected[icut].error = CutFlow.binomialNEventError( self.Nrejected[icut].mean, self.Ntotal.mean) # efficiency calculation and its error for icut in range(0, len(self.Nselected)): if icut == 0: if self.Ntotal.mean == 0: self.eff[icut].mean = 0 else: self.eff[icut].mean = float(self.Nselected[icut].mean) / \ float(self.Ntotal.mean) self.eff[icut].error = CutFlow.binomialError( self.Nselected[icut].mean, self.Ntotal.mean) else: if self.Nselected[icut - 1].mean == 0: self.eff[icut].mean = 0 else: self.eff[icut].mean = float(self.Nselected[icut].mean) / \ float(self.Nselected[icut-1].mean) self.eff[icut].error = CutFlow.binomialError( self.Nselected[icut].mean, self.Nselected[icut - 1].mean) if self.Ntotal.mean == 0: self.effcumu[icut].mean = 0 else: self.effcumu[icut].mean = float(self.Nselected[icut].mean) / \ float(self.Ntotal.mean) self.effcumu[icut].error = CutFlow.binomialError( self.Nselected[icut].mean, self.Ntotal.mean) # Getting xsection xsection = self.dataset.measured_xsection xerror = self.dataset.measured_xerror if self.dataset.xsection != 0.: xsection = self.dataset.xsection xerror = 0 # Saving ntotal ntot = 0. + self.Ntotal.mean # Scaling Ntotal if self.main.normalize == NormalizeType.LUMI: self.Ntotal.error = xerror * self.lumi * 1000 self.Ntotal.mean = xsection * self.lumi * 1000 elif self.main.normalize == NormalizeType.LUMI_WEIGHT: self.Ntotal.error = xerror * self.lumi * 1000 * \ self.dataset.weight self.Ntotal.mean = xsection * self.lumi * 1000 * \ self.dataset.weight # Scaling Nselected for icut in range(0, len(self.Nselected)): if self.main.normalize == NormalizeType.LUMI: # error due to xsec errXsec = xerror * self.lumi * 1000 * self.Nselected[ icut].mean / ntot # scale factor factor = xsection * self.lumi * 1000 / ntot self.Nselected[icut].mean *= factor self.Nselected[icut].error = CutFlow.binomialNEventError( self.Nselected[icut].mean, self.Ntotal.mean) # compute final error self.Nselected[icut].error = sqrt(\ self.Nselected[icut].error**2 + errXsec**2) elif self.main.normalize == NormalizeType.LUMI_WEIGHT: # error due to xsec errXsec = xerror * self.lumi * 1000 * self.dataset.weight * self.Nselected[ icut].mean / ntot # scale factor factor = xsection * self.lumi * self.dataset.weight * 1000 / ntot self.Nselected[icut].mean *= factor self.Nselected[icut].error = CutFlow.binomialNEventError( self.Nselected[icut].mean, self.Ntotal.mean) # compute final error self.Nselected[icut].error = sqrt(\ self.Nselected[icut].error**2 + errXsec**2) # Scaling Nrejected for icut in range(0, len(self.Nrejected)): if self.main.normalize == NormalizeType.LUMI: # error due to xsec errXsec = xerror * self.lumi * 1000 * self.Nrejected[ icut].mean / ntot # scale factor factor = xsection * self.lumi * 1000 / ntot self.Nrejected[icut].mean *= factor self.Nrejected[icut].error = CutFlow.binomialNEventError( self.Nrejected[icut].mean, self.Ntotal.mean) # compute final error self.Nrejected[icut].error = sqrt(\ self.Nrejected[icut].error**2 + errXsec**2) elif self.main.normalize == NormalizeType.LUMI_WEIGHT: # error due to xsec errXsec = xerror * self.lumi * 1000 * self.dataset.weight * self.Nrejected[ icut].mean / ntot # scale factor factor = xsection * self.lumi * self.dataset.weight * 1000 / ntot self.Nrejected[icut].mean *= factor self.Nrejected[icut].error = CutFlow.binomialNEventError( self.Nrejected[icut].mean, self.Ntotal.mean) # compute final error self.Nrejected[icut].error = sqrt(\ self.Nrejected[icut].error**2 + errXsec**2)
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)
import ROOT, sys, os, re, string from ROOT import TCanvas, TFile, TProfile, TNtuple, TH1F, TH1D, TH2F,TF1, TPad, TPaveLabel, TPaveText, TLegend, TLatex, THStack, TFormula from ROOT import gROOT, gBenchmark, gRandom, gSystem, Double, gPad TFormula.SetMaxima(5000,5000,5000) from array import array from LoadData_LPC import * global txtfile txtfile = open('eventtables.txt', 'w') CutList = ['preselection', 'zerobtags', 'onebtags', 'ge1btags', 'ge2btags', 'final' ] #CutList = ['preselection', # ] def FillTables(channel, varName, bin, low, high): VariablesPre = {} VariablesZero = {} VariablesOne = {} VariablesGEOne = {} VariablesGETwo = {}