def runFits(data,options): # axis=ROOT.TAxis(10,array('d',[600,800,900,1000,1250,1500,2000,2500,3000,3500,4000])) axis=ROOT.TAxis(9,array('d',[800,900,1000,1250,1500,2000,2500,3000,3500,4000])) graphs={'meanW':ROOT.TGraphErrors(),'sigmaW':ROOT.TGraphErrors(),'alphaW':ROOT.TGraphErrors(),'alphaW2':ROOT.TGraphErrors(),'n':ROOT.TGraphErrors(),'meanTop':ROOT.TGraphErrors(),'sigmaTop':ROOT.TGraphErrors(),'alphaTop':ROOT.TGraphErrors(),'alphaTop2':ROOT.TGraphErrors(),'f':ROOT.TGraphErrors(),'f2':ROOT.TGraphErrors(),'slope':ROOT.TGraphErrors()} for i in range(1,axis.GetNbins()+1): center=axis.GetBinCenter(i) h = data.drawTH1(options.varx,options.cut+"*({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) histo=copy.deepcopy(h) fitter=Fitter(['M']) fitter.w.var("M").setVal((options.maxx-options.minx)/2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.jetDoublePeakExp('model','M') if options.doExp==0: fitter.w.var("f2").setVal(1.0) fitter.w.var("f2").setConstant(1) fitter.w.var("slope").setVal(0.0) fitter.w.var("slope").setConstant(1) if options.fixPars!="": fixedPars =options.fixPars.split(',') for par in fixedPars: parVal = par.split(':') fitter.w.var(parVal[0]).setVal(float(parVal[1])) fitter.w.var(parVal[0]).setConstant(1) fitter.importBinnedData(histo,['M'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(0)]) fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)]) chi=fitter.projection("model","data","M","debugfitMJJTop_"+options.output+"_"+str(i)+".png","m_{j} (GeV)") chi=fitter.projection("model","data","M","debugfitMJJTop_"+options.output+"_"+str(i)+".pdf","m_{j} (GeV)") for var,graph in graphs.iteritems(): value,error=fitter.fetch(var) graph.SetPoint(i-1,center,value) graph.SetPointError(i-1,0.0,error) F=ROOT.TFile(options.output+".root","RECREATE") F.cd() for name,graph in graphs.iteritems(): graph.Write(name) F.Close()
def runFits(data,options): axis=ROOT.TAxis(options.binsy,options.miny,options.maxy) #first pass graphs=[] for i in range(0,1): graphs.append(ROOT.TGraphErrors()) for i in range(1,axis.GetNbins()+1): center=axis.GetBinCenter(i) h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) histo=copy.deepcopy(h) fitter=Fitter(['M']) fitter.w.var("M").setVal((options.maxx-options.minx)/2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.pow('model','M') fitter.importBinnedData(histo,['M'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1)]) chi=fitter.projection("model","data","M","debugfit_"+str(i)+".pdf") for j,g in enumerate(graphs): c,cerr=fitter.fetch("c_"+str(j)) g.SetPoint(i-1,center,c) g.SetPointError(i-1,0.0,cerr) pol0=ROOT.TF1("pol3","pol3",options.minx,options.maxx) graphs[0].Fit(pol0) #create json data={} data['p0']=returnString(pol0,options) f=open(options.output+".json","w") json.dump(data,f) f.close() return graphs
def runFits(data,options): jsonFile=open(options.json) info=json.load(jsonFile) meanF = ROOT.TFormula("meanF",info['mean'].replace('MH','x')) sigmaF = ROOT.TFormula("sigmaF",info['sigma'].replace('MH','x')) alphaF = ROOT.TFormula("alphaF",info['alpha'].replace('MH','x')) nF = ROOT.TFormula("nF",info['n'].replace('MH','x')) alpha2F = ROOT.TFormula("alpha2F",info['alpha2'].replace('MH','x')) n2F = ROOT.TFormula("n2F",info['n2'].replace('MH','x')) slopeF = ROOT.TFormula("slopeF",info['slope'].replace('MH','x')) fF = ROOT.TFormula("fF",info['f'].replace('MH','x')) # axis=ROOT.TAxis(10,array('d',[600,800,900,1000,1250,1500,2000,2500,3000,3500,4000])) axis=ROOT.TAxis(10,array('d',[600,650,700,750,800,900,1000,1250,1500,2000,2500])) graphs={'mean':ROOT.TGraphErrors(),'sigma':ROOT.TGraphErrors(),'alpha':ROOT.TGraphErrors(),'n':ROOT.TGraphErrors(),'alpha2':ROOT.TGraphErrors(),'n2':ROOT.TGraphErrors(),'f':ROOT.TGraphErrors(),'slope':ROOT.TGraphErrors()} for i in range(1,axis.GetNbins()+1): center=axis.GetBinCenter(i) h = data.drawTH1(options.varx,options.cut+"*({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) histo=copy.deepcopy(h) fitter=Fitter(['M']) fitter.w.var("M").setVal((options.maxx-options.minx)/2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) if options.doExp>=0: ##MICHALIS CHANGED IT fitter.jetResonance('model','M') fitter.w.var("slope").setVal(slopeF.Eval(center)) fitter.w.var("slope").setMin(-2) fitter.w.var("slope").setMax(2) fitter.w.var("slope").setConstant(0) fitter.w.var("f").setVal(fF.Eval(center)) fitter.w.var("f").setMin(0) fitter.w.var("f").setMax(1.) fitter.w.var("f").setConstant(0) else: fitter.jetResonanceNOEXP('model','M') fitter.w.var("mean").setVal(meanF.Eval(center)) fitter.w.var("mean").setMin(meanF.Eval(center)*0.9) fitter.w.var("mean").setMax(meanF.Eval(center)*1.1) fitter.w.var("sigma").setVal(sigmaF.Eval(center)) fitter.w.var("sigma").setMin(sigmaF.Eval(center)*0.5) fitter.w.var("sigma").setMax(sigmaF.Eval(center)*2) # fitter.w.var("alpha").setVal(alphaF.Eval(center)) fitter.w.var("alpha").setVal(1.59049) fitter.w.var("alpha").setConstant(1) fitter.w.var("alpha2").setVal(1.3) fitter.w.var("alpha2").setConstant(1) # fitter.w.var("alpha2").setConstant(1) fitter.importBinnedData(histo,['M'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(0)]) fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)]) chi=fitter.projection("model","data","M","debugfitMJJTop_"+options.output+"_"+str(i)+".png") for var,graph in graphs.iteritems(): value,error=fitter.fetch(var) graph.SetPoint(i-1,center,value) graph.SetPointError(i-1,0.0,error) F=ROOT.TFile(options.output+".root","RECREATE") F.cd() for name,graph in graphs.iteritems(): graph.Write(name) F.Close()
fitter.w.var("slope").setConstant(1) if options.fixPars!="": fixedPars =options.fixPars.split(',') for par in fixedPars: parVal = par.split(':') fitter.w.var(parVal[0]).setVal(float(parVal[1])) fitter.w.var(parVal[0]).setConstant(1) histo=copy.deepcopy(h) fitter.importBinnedData(histo,['MVV','MJJ'],'data') #fitter.w.Print() fitter.fit('model','data',[ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(fitter.w.var("MVV"))),ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(0)]) fitter.fit('model','data',[ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(fitter.w.var("MVV"))),ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)]) ## this one computes correct asymmetric error bars ## store the result, for nominal and alternative shapes F=ROOT.TFile(options.output+".root","RECREATE") F.cd() ##nominal template = fitter.w.pdf("model").createHistogram("histo", fitter.w.var("MVV"), ROOT.RooFit.Binning(options.binsx,options.minx,options.maxx), ROOT.RooFit.YVar(fitter.w.var("MJJ"),ROOT.RooFit.Binning(options.binsy,options.miny,options.maxy)) ) template.Write("histo") def merge_dicts(*dict_args): result = {}
def runFits(data, options): axis = ROOT.TAxis(5, array('d', [600, 700, 800, 900, 1000, 1200, 1400])) #first pass graphs = [] for i in range(0, 6): graphs.append(ROOT.TGraphErrors()) for i in range(1, axis.GetNbins() + 1): center = axis.GetBinCenter(i) h = data.drawTH1( options.varx, options.cut + "&&({vary}>{mini}&&{vary}<{maxi})".format( vary=options.vary, mini=axis.GetBinLowEdge(i), maxi=axis.GetBinUpEdge(i)), str(options.lumi), options.binsx, options.minx, options.maxx) histo = copy.deepcopy(h) fitter = Fitter(['M']) fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.signalMJJCB('model', 'M') fitter.importBinnedData(histo, ['M'], 'data') fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(0), ROOT.RooFit.Minos(1)]) chi = fitter.projection( "model", "data", "M", "debugfitMJJTop_" + options.output + "_" + str(i) + ".png") for j, g in enumerate(graphs): c, cerr = fitter.fetch("c_" + str(j)) g.SetPoint(i - 1, center, c) g.SetPointError(i - 1, 0.0, cerr) data = {} pol4 = ROOT.TF1("pol4", "pol4", options.minx, options.maxx) pol3 = ROOT.TF1("pol3", "pol3", options.minx, options.maxx) pol2 = ROOT.TF1("pol2", "pol2", options.minx, options.maxx) pol1 = ROOT.TF1("pol1", "pol1", options.minx, options.maxx) pol0 = ROOT.TF1("pol0", "pol0", options.minx, options.maxx) log = ROOT.TF1("log0", "[0]+[1]*log(x)", options.minx, options.maxx) graphs[0].Fit(log) data['mean'] = returnString(log, options) graphs[1].Fit(pol0) data['sigma'] = returnString(pol0, options) graphs[2].Fit(log) data['alpha1'] = returnString(log, options) graphs[3].Fit(pol0) data['n1'] = returnString(pol0, options) graphs[4].Fit(pol0) data['alpha2'] = returnString(pol0, options) graphs[5].Fit(pol0) data['n2'] = returnString(pol0, options) #create json f = open(options.output + ".json", "w") json.dump(data, f) f.close() return graphs
def runFits(data,options): axis=ROOT.TAxis(options.binsy,options.miny,options.maxy) #first pass graphs=[] for i in range(0,3): graphs.append(ROOT.TGraphErrors()) for i in range(1,axis.GetNbins()+1): center=axis.GetBinCenter(i) h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) histo=copy.deepcopy(h) fitter=Fitter(['M']) fitter.w.var("M").setVal((options.maxx-options.minx)/2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.erfpow('model','M') fitter.importBinnedData(histo,['M'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)]) # chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass1_"+str(i)+".png") for j,g in enumerate(graphs): c,cerr=fitter.fetch("c_"+str(j)) if abs(c-fitter.w.var("c_"+str(j)).getMin())<0.1: cerr=abs(c)*10000 g.SetPoint(i-1,center,c) g.SetPointError(i-1,0.0,cerr) parameter0=ROOT.TF1("pol0","pol0",options.minx,options.maxx) graphs[0].Fit(parameter0) #Second pass after fixing par0 for i in range(1,axis.GetNbins()+1): center=axis.GetBinCenter(i) h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) histo=copy.deepcopy(h) fitter=Fitter(['M']) fitter.w.var("M").setVal((options.maxx-options.minx)/2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.erfpow('model','M') fitter.w.var("c_0").setVal(parameter0.Eval(center)) fitter.w.var("c_0").setConstant(1) fitter.importBinnedData(histo,['M'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)]) # chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass2_"+str(i)+".png") for j,g in enumerate(graphs): if j>0: c,cerr=fitter.fetch("c_"+str(j)) if abs(c-fitter.w.var("c_"+str(j)).getMin())<0.1: cerr=abs(c)*10000 g.SetPoint(i-1,center,c) g.SetPointError(i-1,0.0,cerr) parameter2=ROOT.TF1("pol3","pol3",options.minx,options.maxx) # log0.SetParameters(1,1) graphs[2].Fit(parameter2) #Third pass after fixing par0 for i in range(1,axis.GetNbins()+1): center=axis.GetBinCenter(i) h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) #protect for negative weights histo=copy.deepcopy(h) fitter=Fitter(['M']) fitter.w.var("M").setVal((options.maxx-options.minx)/2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.erfpow('model','M') fitter.w.var("c_0").setVal(parameter0.Eval(center)) fitter.w.var("c_0").setConstant(1) fitter.w.var("c_2").setVal(parameter2.Eval(center)) fitter.w.var("c_2").setConstant(1) fitter.importBinnedData(histo,['M'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1),ROOT.RooFit.Minos(1)]) # chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass3_"+str(i)+".png") for j,g in enumerate(graphs): if j==1: c,cerr=fitter.fetch("c_"+str(j)) if abs(c-fitter.w.var("c_"+str(j)).getMin())<0.1: cerr=abs(c)*10000 g.SetPoint(i-1,center,c) g.SetPointError(i-1,0.0,cerr) parameter1=ROOT.TF1("pol3","pol3",options.minx,options.maxx) graphs[1].Fit(parameter1) #Fourth pass - plotting for i in range(1,axis.GetNbins()+1): center=axis.GetBinCenter(i) h = data.drawTH1(options.varx,options.cut+"&&({vary}>{mini}&&{vary}<{maxi})".format(vary=options.vary,mini=axis.GetBinLowEdge(i),maxi=axis.GetBinUpEdge(i)),str(options.lumi),options.binsx,options.minx,options.maxx) #protect for negative weights histo=copy.deepcopy(h) fitter=Fitter(['M']) fitter.w.var("M").setVal((options.maxx-options.minx)/2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.erfpow('model','M') fitter.w.var("c_0").setVal(parameter0.Eval(center)) fitter.w.var("c_0").setConstant(1) fitter.w.var("c_1").setVal(parameter1.Eval(center)) fitter.w.var("c_1").setConstant(1) fitter.w.var("c_2").setVal(parameter2.Eval(center)) fitter.w.var("c_2").setConstant(1) fitter.importBinnedData(histo,['M'],'data') chi=fitter.projection("model","data","M","debugfitMVV_"+options.output+"_pass3_"+str(i)+".png") #create json data={} data['p0']=returnString(parameter0,options) data['p1']=returnString(parameter1,options) data['p2']=returnString(parameter2,options) f=open(options.output+".json","w") json.dump(data,f) f.close() return graphs
def runFits(data, options): # axis=ROOT.TAxis(10,array('d',[600,800,900,1000,1250,1500,2000,2500,3000,3500,4000])) axis = ROOT.TAxis( 10, array('d', [600, 650, 700, 750, 800, 900, 1000, 1250, 1500, 2000, 2500])) graphs = { 'mean': ROOT.TGraphErrors(), 'sigma': ROOT.TGraphErrors(), 'alpha': ROOT.TGraphErrors(), 'n': ROOT.TGraphErrors(), 'alpha2': ROOT.TGraphErrors(), 'n2': ROOT.TGraphErrors(), 'slope': ROOT.TGraphErrors(), 'f': ROOT.TGraphErrors() } for i in range(1, axis.GetNbins() + 1): center = axis.GetBinCenter(i) h = data.drawTH1( options.varx, options.cut + "*({vary}>{mini}&&{vary}<{maxi})".format( vary=options.vary, mini=axis.GetBinLowEdge(i), maxi=axis.GetBinUpEdge(i)), str(options.lumi), options.binsx, options.minx, options.maxx) histo = copy.deepcopy(h) fitter = Fitter(['M']) fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) if options.doExp: fitter.jetResonanceNOEXP('model', 'M') fitter.w.var("alpha").setVal(1.48) fitter.w.var("alpha").setConstant(1) fitter.w.var("alpha2").setVal(1.07) fitter.w.var("alpha2").setConstant(1) # fitter.jetResonance('model','M') else: fitter.jetResonanceNOEXP('model', 'M') fitter.w.var("alpha").setVal(1.48) fitter.w.var("alpha").setConstant(1) fitter.w.var("alpha2").setVal(1.07) fitter.w.var("alpha2").setConstant(1) fitter.importBinnedData(histo, ['M'], 'data') fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Minos(0)]) fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Minos(1)]) chi = fitter.projection( "model", "data", "M", "debugfitMJJTop_" + options.output + "_" + str(i) + ".png") for var, graph in graphs.iteritems(): value, error = fitter.fetch(var) graph.SetPoint(i - 1, center, value) graph.SetPointError(i - 1, 0.0, error) F = ROOT.TFile(options.output + ".root", "RECREATE") F.cd() for name, graph in graphs.iteritems(): graph.Write(name) F.Close()
plotter=TreePlotter(args[0]+'/'+samples[mass]+'.root','tree') plotter.setupFromFile(args[0]+'/'+samples[mass]+'.pck') plotter.addCorrectionFactor('genWeight','tree') plotter.addCorrectionFactor('xsec','tree') plotter.addCorrectionFactor('puWeight','tree') fitter=Fitter(['m','M']) fitter.signal2D('model',options.boson) fitter.w.var("MH").setVal(mass) histo = plotter.drawTH2(options.mjj+":"+options.mvv,options.cutShape,"1",500,0,13000,120,60,140) histoYield = plotter.drawTH2(options.mjj+":"+options.mvv,options.cutYield,"1",130,0,13000,100,25,165) fitter.importBinnedData(histo,['M','m'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(1)]) #create the yield fitter.w.var('m').setMax(options.maxMJJ) fitter.w.var('m').setMin(options.minMJJ) integral = fitter.w.pdf("model").createIntegral(ROOT.RooArgSet(fitter.w.var("m"),fitter.w.var("M"))) analysisIntegral=integral.getVal() fitter.w.var('m').setMin(40) fitter.w.var('m').setMax(120) integral = fitter.w.pdf("model").createIntegral(ROOT.RooArgSet(fitter.w.var("m"),fitter.w.var("M"))) fitRangeIntegral=integral.getVal() events=histoYield.Integral()*options.BR*analysisIntegral/fitRangeIntegral
plotter=TreePlotter(args[0]+'/'+samples[mass]+'.root','tree') plotter.addCorrectionFactor('genWeight','tree') plotter.addCorrectionFactor('puWeight','tree') if options.scaleFactors!='': for s in scaleFactors: plotter.addCorrectionFactor(s,'tree') fitter=Fitter(['MVV']) fitter.signalResonanceCBGaus('model','MVV',mass) fitter.w.var("MH").setVal(mass) histo = plotter.drawTH1(options.mvv,options.cut+"*(jj_LV_mass>%f&&jj_LV_mass<%f)"%(0.8*mass,1.2*mass),"1",140,700,8000) fitter.importBinnedData(histo,['MVV'],'data') fitter.fit('model','data',[ROOT.RooFit.SumW2Error(0)])#,ROOT.RooFit.Range(1000,8000)]) fitter.fit('model','data',[ROOT.RooFit.SumW2Error(0)])#,ROOT.RooFit.Range(1000,8000)]) # fitter.fit('model','data',[ROOT.RooFit.SumW2Error(0)])#,ROOT.RooFit.Range(1000,8000)]) #fitter.projection("model","data","MVV","debugVV_"+options.output+"_"+str(mass)+".root") fitter.projection("model","data","MVV","debugVV_"+options.output+"_"+str(mass)+".png","M_{jj} (GeV)",mass) fitter.w.Print() for var,graph in graphs.iteritems(): value,error=fitter.fetch(var) graph.SetPoint(N,mass,value) graph.SetPointError(N,0.0,error) N=N+1 F=ROOT.TFile(options.output,"RECREATE")
def runFits(data, options, info): axis = ROOT.TAxis( 8, array('d', [600, 650, 700, 750, 800, 850, 1000, 1200, 1400])) #first pass graphs = { 'mean': ROOT.TGraphErrors(), 'alpha2': ROOT.TGraphErrors(), 'n2': ROOT.TGraphErrors() } for i in range(1, axis.GetNbins() + 1): center = axis.GetBinCenter(i) h = data.drawTH1( options.varx, options.cut + "&&({vary}>{mini}&&{vary}<{maxi})".format( vary=options.vary, mini=axis.GetBinLowEdge(i), maxi=axis.GetBinUpEdge(i)), str(options.lumi), options.binsx, options.minx, options.maxx) histo = copy.deepcopy(h) fitter = Fitter(['M']) fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) func = ROOT.TF1("func", info['mean'].replace("MH", "x"), 0, 6000) # fitter.w.factory("mean["+str(func.Eval(center))+"]") fitter.w.factory("mean[1,-20,20]") fitter.w.factory( "expr::mean2('{val}+mean',mean)".format(val=func.Eval(center))) func = ROOT.TF1("func", info['sigma'].replace("MH", "x"), 0, 6000) fitter.w.factory("sigma[" + str(func.Eval(center)) + "]") func = ROOT.TF1("func", info['alpha'].replace("MH", "x"), 0, 6000) fitter.w.factory("alpha[" + str(func.Eval(center)) + "]") func = ROOT.TF1("func", info['n'].replace("MH", "x"), 0, 6000) fitter.w.factory("n[" + str(func.Eval(center)) + "]") func = ROOT.TF1("func", info['slope'].replace("MH", "x"), 0, 6000) fitter.w.factory("slope[" + str(func.Eval(center)) + "]") func = ROOT.TF1("func", info['f'].replace("MH", "x"), 0, 6000) fitter.w.factory("f[" + str(func.Eval(center)) + "]") fitter.w.factory("alpha2[3,0.5,6]") fitter.w.factory("n2[6]") name = 'model' peak = ROOT.RooDoubleCB(name, 'modelS', fitter.w.var("M"), fitter.w.function('mean2'), fitter.w.var('sigma'), fitter.w.var('alpha'), fitter.w.var('n'), fitter.w.var("alpha2"), fitter.w.var("n2")) getattr(fitter.w, 'import')(peak, ROOT.RooFit.Rename(name + 'S')) # fitter.w.factory("RooExponential::"+name+"B(M,slope)") # fitter.w.factory("SUM::"+name+"(f*"+name+"S,"+name+"B)") # fitter.signalMJJCB('model','M') fitter.importBinnedData(histo, ['M'], 'data') fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1)]) fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Minos(1)]) chi = fitter.projection( "model", "data", "M", "debugfitMJJTop_" + options.output + "_" + str(i) + ".png") c, cerr = fitter.fetch('mean') graphs['mean'].SetPoint(i - 1, center, c) graphs['mean'].SetPointError(i - 1, 0.0, c) c, cerr = fitter.fetch('alpha2') graphs['alpha2'].SetPoint(i - 1, center, c) graphs['alpha2'].SetPointError(i - 1, 0.0, c) c, cerr = fitter.fetch('n2') graphs['n2'].SetPoint(i - 1, center, c) graphs['n2'].SetPointError(i - 1, 0.0, c) data = {} pol4 = ROOT.TF1("pol4", "pol4", options.minx, options.maxx) pol3 = ROOT.TF1("pol3", "pol3", options.minx, options.maxx) pol2 = ROOT.TF1("pol2", "pol2", options.minx, options.maxx) pol1 = ROOT.TF1("pol1", "pol1", options.minx, options.maxx) pol0 = ROOT.TF1("pol0", "pol0", options.minx, options.maxx) log = ROOT.TF1("log0", "[0]+[1]*log(x)", options.minx, options.maxx) graphs['mean'].Fit(log) data['mean'] = info['mean'] + "+" + returnString(log, options) graphs['alpha2'].Fit(log) data['alpha2'] = returnString(log, options) graphs['n2'].Fit(pol0) data['n2'] = returnString(pol0, options) # data['mean']=info['mean'] data['sigma'] = info['sigma'] data['alpha1'] = info['alpha'] data['n1'] = info['n'] #create json f = open(options.output + ".json", "w") json.dump(data, f) f.close() return graphs
w = histoPDF.GetBinContent(i) cList.setRealValue("GM", x) cList.setRealValue("weight", w) dataset.add(cList, w) fitter = Fitter(['M']) fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.gaussianSum("model", "M", dataset, "GM") histo = data.drawTH1(options.var, options.cut, "1", options.binsx * 4, options.minx, options.maxx) fitter.importBinnedData(histo, "M") fitter.fit() scale = fitter.w.var("scale").getVal() sigma = fitter.w.var("sigma").getVal() fitter.projection("model", "data", "M", options.output + "_debug.png") h = fitter.w.pdf("model").createHistogram("M", options.binsx) h.SetName("histo") histograms.append(h) #scaleUp fitter.w.var("scale").setVal(scale * 1.15) h = fitter.w.pdf("model").createHistogram("M", options.binsx) h.SetName("histo_scaleUp") histograms.append(h) #scaleDn
fitter.w.var(parVal[0]).setConstant(1) #histo = plotter.drawTH1("jj_"+leg+"_softDrop_mass",options.cut+"*(jj_"+leg+"_mergedVTruth==1)","1",80,options.mini,options.maxi) histo = plotter.drawTH1( "jj_" + leg + "_softDrop_mass", options.cut + "*(jj_" + leg + "_mergedVTruth==1)*(jj_" + leg + "_softDrop_mass>60&&jj_" + leg + "_softDrop_mass<110)", "1", 25, 60, 110) if leg.find("l1") != -1: NRes[0] += histo.Integral() else: NRes[1] += histo.Integral() fitter.importBinnedData(histo, ['x'], 'data') fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Save(1)]) #fitter.fit('model','data',[ROOT.RooFit.SumW2Error(0),ROOT.RooFit.Minos(1)]) fitter.projection("model", "data", "x", "debugJ" + leg + "_" + options.output + "_Res.png") #params[label+"_Res_"+leg]={"mean": {"val": fitter.w.var("mean").getVal(), "err": fitter.w.var("mean").getError()}, "sigma": {"val": fitter.w.var("sigma").getVal(), "err": fitter.w.var("sigma").getError()}, "alpha":{ "val": fitter.w.var("alpha").getVal(), "err": fitter.w.var("alpha")},"alpha2":{"val": fitter.w.var("alpha2").getVal(),"err": fitter.w.var("alpha2").getError()},"n":{ "val": fitter.w.var("n").getVal(), "err": fitter.w.var("n").getError()},"n2": {"val": fitter.w.var("n2").getVal(), "err": fitter.w.var("n2").getError()}} params[label + "_Res_" + leg] = { "mean": { "val": fitter.w.var("mean").getVal(), "err": fitter.w.var("mean").getError() }, "sigma": { "val": fitter.w.var("sigma").getVal(), "err": fitter.w.var("sigma").getError() } }
# fitter.w.var("slope").setConstant(1) if options.fixPars != "": fixedPars = options.fixPars.split(',') for par in fixedPars: parVal = par.split(':') fitter.w.var(parVal[0]).setVal(float(parVal[1])) fitter.w.var(parVal[0]).setConstant(1) histo = copy.deepcopy(h) fitter.importBinnedData(histo, ['MVV', 'MJJ'], 'data') #fitter.w.Print() fitter.fit('model', 'data', [ ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(fitter.w.var("MVV"))), ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Minos(0) ]) fitter.fit('model', 'data', [ ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(fitter.w.var("MVV"))), ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Minos(1) ]) ## this one computes correct asymmetric error bars ## store the result, for nominal and alternative shapes F = ROOT.TFile(options.output + ".root", "RECREATE") F.cd() ##nominal template = fitter.w.pdf("model").createHistogram( "histo", fitter.w.var("MVV"),
def interpolate(histos, axis, options): #create 2D histogram histo2D = ROOT.TH2D("histo2D", "histo2D", options.binsx, options.minx, options.maxx, options.binsy, options.miny, options.maxy) if options.fitFunc == "bernstein3": order = 3 if options.fitFunc == "bernstein4": order = 4 if options.fitFunc == "bernstein5": order = 5 if options.fitFunc == "bifur": order = 3 graphs = [] for i in range(0, order): graphs.append(ROOT.TGraphErrors()) #run the fits for N, h in enumerate(histos): center = axis.GetBinCenter(N + 1) fitter = Fitter(['mjj']) fitter.w.var("mjj").setVal(40) fitter.w.var("mjj").setMax(options.maxy) fitter.w.var("mjj").setMin(options.miny) if options.fitFunc == "bernstein3": fitter.bernstein('model', 'mjj', 3) if options.fitFunc == "bernstein4": fitter.bernstein('model', 'mjj', 4) if options.fitFunc == "bernstein5": fitter.bernstein('model', 'mjj', 5) if options.fitFunc == "bifur": fitter.bifur('model', 'mjj') fitter.importBinnedData(h, ['mjj'], 'data') fitter.fit('model', 'data') fitter.fit('model', 'data') fitter.projection("model", "data", "mjj", "debugProjectionFit_" + str(N) + ".png") for j, g in enumerate(graphs): c, cerr = fitter.fetch("c_" + str(j)) g.SetPoint(N, center, c) g.SetPointError(N, 0.0, cerr) ##OK now interpolate and make histograms! #first make the function fitter = Fitter(['mjj']) if options.fitFunc == "bernstein3": fitter.bernstein('model', 'mjj', 3) order = 3 if options.fitFunc == "bernstein4": fitter.bernstein('model', 'mjj', 4) order = 4 if options.fitFunc == "bernstein5": fitter.bernstein('model', 'mjj', 5) order = 5 if options.fitFunc == "bifur": fitter.bifur('model', 'mjj') order = 3 fitter.w.var("mjj").setVal(80) fitter.w.var("mjj").setMax(options.maxy) fitter.w.var("mjj").setMin(options.miny) for i in range(1, histo2D.GetNbinsX() + 1): x = histo2D.GetXaxis().GetBinCenter(i) print 'Bin:', i, 'Evaluating function at x=', x for j, g in enumerate(graphs): print "c_" + str(j), "=", g.Eval(x) fitter.w.var("c_" + str(j)).setVal(g.Eval(x, 0, "S")) histogram = fitter.w.pdf("model").createHistogram("mjj", options.binsy) for k in range(1, histo2D.GetNbinsY() + 1): bin = histo2D.GetBin(i, k) histo2D.SetBinContent(bin, histogram.GetBinContent(k)) return graphs, histo2D
data = MergedPlotter(dataPlotters) histo = data.drawTH2(options.vary + ":" + options.varx, options.cut, str(options.lumi), options.binsx, options.minx, options.maxx, options.binsy, options.miny, options.maxy) histo = copy.deepcopy(histo) fitter = Fitter(['M', 'm']) fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.w.var("m").setVal((options.maxy - options.miny) / 2.0) fitter.w.var("m").setMax(options.maxy) fitter.w.var("m").setMin(options.miny) fitter.erfpowParam('model', ['M', 'm']) fitter.importBinnedData(histo, ['M', 'm'], 'data') fitter.fit('model', 'data', [ ROOT.RooFit.SumW2Error(0), ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(fitter.w.var('m'))) ]) fitter.fit('model', 'data', [ ROOT.RooFit.SumW2Error(0), ROOT.RooFit.ConditionalObservables(ROOT.RooArgSet(fitter.w.var('m'))) ]) fitter.projectionCond("model", "data", "M", "m", 'debug' + options.output + ".png") data = {} data['p0'] = returnString('c0', fitter.w) data['p1'] = returnString('c1', fitter.w) data['p2'] = returnString('c2', fitter.w) f = open(options.output + ".json", "w") json.dump(data, f)
plotter.setupFromFile(args[0] + '/' + samples[mass] + '.pck') plotter.addCorrectionFactor('genWeight', 'tree') plotter.addCorrectionFactor('xsec', 'tree') plotter.addCorrectionFactor('puWeight', 'tree') fitter = Fitter(['m', 'M']) fitter.signal2D('model', options.boson) fitter.w.var("MH").setVal(mass) histo = plotter.drawTH2(options.mjj + ":" + options.mvv, options.cutShape, "1", 500, 0, 13000, 120, 25, 165) histoYield = plotter.drawTH2(options.mjj + ":" + options.mvv, options.cutYield, "1", 130, 0, 13000, 100, 25, 165) fitter.importBinnedData(histo, ['M', 'm'], 'data') fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(0)]) #create the yield # fitter.w.var('m').setMax(options.maxMJJ) # fitter.w.var('m').setMin(options.minMJJ) # integral = fitter.w.pdf("model").createIntegral(ROOT.RooArgSet(fitter.w.var("m"),fitter.w.var("M"))) # analysisIntegral=integral.getVal() # fitter.w.var('m').setMin(25) # fitter.w.var('m').setMax(165) # integral = fitter.w.pdf("model").createIntegral(ROOT.RooArgSet(fitter.w.var("m"),fitter.w.var("M"))) # fitRangeIntegral=integral.getVal() events = histoYield.Integral() * options.BR #*analysisIntegral/fitRangeIntegral
maxi=maxi)) STR = STR + "+(" + p + str(i) + ")" + (("*mjj") * i) DEPS.append(p + str(i)) formulas[p] = STR fitter.w.factory("expr::{p}('{st}',{deps})".format(p=p, st=STR, deps=','.join(DEPS))) erfpow = ROOT.RooErfPowPdf('modelM', 'modelM', fitter.w.var('M'), fitter.w.function("p0"), fitter.w.function("p1"), fitter.w.function("p2")) getattr(fitter.w, 'import')(erfpow, ROOT.RooFit.Rename('modelM')) fitter.w.factory("PROD::model(modelM|mjj,modelJ)") fitter.importBinnedData(histo, ["M", 'mjj'], "data") fitter.fit("model", "data", [ROOT.RooFit.SumW2Error(1), ROOT.RooFit.NumCPU(8)]) fitter.w.writeToFile("debugWorkspace.root") for p, val in orderInfo.iteritems(): for i in range(0, val + 1): c = fitter.w.var(p + str(i)).getVal() formulas[p] = formulas[p].replace(p + str(i), "(" + str(c) + ")") outF = open(options.output, "w") json.dump(formulas, outF) outF.close() #debug c = ROOT.TCanvas("C") frame = fitter.w.var("M").frame() for val in [40., 50., 60., 70., 80., 90., 100., 110., 120., 130., 140.]:
def runFits(data, options): h = data.drawTH1(options.varx, options.cut, str(options.lumi), options.binsx, options.minx, options.maxx) histo = copy.deepcopy(h) fitter = Fitter(['M']) fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0) fitter.w.var("M").setMax(options.maxx) fitter.w.var("M").setMin(options.minx) fitter.w.var("M").setBins(options.binsx) fitter.jetDoublePeakSF('model', 'M') fitter.importBinnedData(histo, ['M'], 'data') fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Minos(0)]) fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1), ROOT.RooFit.Minos(1)]) fitter.projection("model", "data", "M", options.output + "_debug.png", 'm_{SD}') scaleW = fitter.w.var('meanW').getVal() scaleTop = fitter.w.var('meanTop').getVal() sigmaW = fitter.w.var('sigmaW').getVal() sigmaTop = fitter.w.var('sigmaTop').getVal() slope = fitter.w.var("slope").getVal() fT = fitter.w.var('fT').getVal() fE = fitter.w.var('fE').getVal() NW = fitter.w.var('NW').getVal() NBKG = fitter.w.var('NBKG').getVal() F = ROOT.TFile(options.output, "RECREATE") F.cd() ################################################################# h = fitter.w.pdf("WPeak").createHistogram("M", options.binsx) h.Scale(NW / (options.lumi * h.Integral())) h.Write("W") fitter.w.var('meanW').setVal(scaleW * 1.05) h = fitter.w.pdf("WPeak").createHistogram("M", options.binsx) h.Write("W_scaleUp") fitter.w.var('meanW').setVal(scaleW * 0.95) h = fitter.w.pdf("WPeak").createHistogram("M", options.binsx) h.Write("W_scaleDown") fitter.w.var('meanW').setVal(scaleW) fitter.w.var('sigmaW').setVal(sigmaW * 1.25) h = fitter.w.pdf("WPeak").createHistogram("M", options.binsx) h.Write("W_sigmaUp") fitter.w.var('sigmaW').setVal(sigmaW * 0.75) h = fitter.w.pdf("WPeak").createHistogram("M", options.binsx) h.Write("W_sigmaDown") fitter.w.var('sigmaW').setVal(sigmaW) ################################################################# h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Scale(NBKG / (options.lumi * h.Integral())) h.Write("bkg") fitter.w.var('meanTop').setVal(scaleTop * 1.15) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_scaleUp") fitter.w.var('meanTop').setVal(scaleTop * 0.85) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_scaleDown") fitter.w.var('meanTop').setVal(scaleTop) fitter.w.var('sigmaTop').setVal(sigmaTop * 1.25) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_sigmaUp") fitter.w.var('sigmaTop').setVal(sigmaTop * 0.75) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_sigmaDown") fitter.w.var('sigmaTop').setVal(sigmaTop) fitter.w.var('slope').setVal(slope * 1.5) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_slopeUp") fitter.w.var('slope').setVal(slope * 0.5) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_slopeDown") fitter.w.var('slope').setVal(slope) fitter.w.var('fT').setVal(fT * 1.3) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_fTUp") fitter.w.var('fT').setVal(fT * 0.7) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_fTDown") fitter.w.var('fT').setVal(fT) fitter.w.var('fE').setVal(fE * 1.3) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_fEUp") fitter.w.var('fE').setVal(fE * 0.7) h = fitter.w.pdf("bkg").createHistogram("M", options.binsx) h.Write("bkg_fEDown") fitter.w.var('fE').setVal(fE) F.Close()