TreePlotter(args[0] + '/' + fname + '.root', 'tree')) if options.data == 0: dataPlotters[-1].setupFromFile(args[0] + '/' + fname + '.pck') dataPlotters[-1].addCorrectionFactor('xsec', 'tree') dataPlotters[-1].addCorrectionFactor('genWeight', 'tree') dataPlotters[-1].addCorrectionFactor('puWeight', 'tree') data = MergedPlotter(dataPlotters) pvars = options.vars.split(',') pmins = options.mins.split(',') pmaxes = options.maxes.split(',') pbins = options.bins.split(',') if len(pvars) == 1: histo = data.drawTH1(pvars[0], options.cut, "1", int(pbins[0]), float(pmins[0]), float(pmaxes[0])) if len(pvars) == 2: histo = data.drawTH2(pvars[1] + ":" + pvars[0], options.cut, "1", int(pbins[0]), float(pmins[0]), float(pmaxes[0]), int(pbins[1]), float(pmins[1]), float(pmaxes[1])) if len(pvars) == 3: histo = data.drawTH3(pvars[2] + ":" + pvars[1] + ":" + pvars[0], options.cut, "1", int(pbins[0]), float(pmins[0]), float(pmaxes[0]), int(pbins[1]), float(pmins[1]), float(pmaxes[1]), int(pbins[2]), float(pmins[2]), float(pmaxes[2])) histo.Scale(options.factor)
fnameParts=filename.split('.') fname=fnameParts[0] ext=fnameParts[1] if ext.find("root") ==-1: continue dataPlotters.append(TreePlotter(args[0]+'/'+fname+'.root','tree')) dataPlotters[-1].setupFromFile(args[0]+'/'+fname+'.pck') dataPlotters[-1].addCorrectionFactor('xsec','tree') dataPlotters[-1].addCorrectionFactor('genWeight','tree') dataPlotters[-1].addCorrectionFactor('puWeight','tree') data=MergedPlotter(dataPlotters) histoQ=data.drawTH1(options.mvv+"/13000.0",options.cut+"&&"+options.quarkCut,"1",options.bins,options.min/13000.0,options.max/13000.0) histoA=data.drawTH1(options.mvv+"/13000.0",options.cut,"1",options.bins,options.min/13000.0,options.max/13000.0) histoQ.Divide(histoA) func=ROOT.TF1("func",options.function,options.min/13000.0,options.max/13000.0) histoQ.Fit(func) parameterization={'quarkFraction':returnString(func)} f=open(options.output+".json","w") json.dump(parameterization,f) f.close() F=ROOT.TFile(options.output+".root",'RECREATE') F.cd() histoQ.Write("fraction") F.Close()
if options.data==0: dataPlotters[-1].setupFromFile(args[0]+'/'+fname+'.pck') dataPlotters[-1].addCorrectionFactor('xsec','tree') dataPlotters[-1].addCorrectionFactor('genWeight','tree') dataPlotters[-1].addCorrectionFactor('puWeight','tree') data=MergedPlotter(dataPlotters) pvars=options.vars.split(',') pmins=options.mins.split(',') pmaxes=options.maxes.split(',') pbins=options.bins.split(',') if len(pvars)==1: histo=data.drawTH1(pvars[0],options.cut,"1",int(pbins[0]),float(pmins[0]),float(pmaxes[0])) if len(pvars)==2: histo=data.drawTH2(pvars[1]+":"+pvars[0],options.cut,"1",int(pbins[0]),float(pmins[0]),float(pmaxes[0]),int(pbins[1]),float(pmins[1]),float(pmaxes[1])) if len(pvars)==3: histo=data.drawTH3(pvars[2]+":"+pvars[1]+":"+pvars[0],options.cut,"1",int(pbins[0]),float(pmins[0]),float(pmaxes[0]),int(pbins[1]),float(pmins[1]),float(pmaxes[1]),int(pbins[2]),float(pmins[2]),float(pmaxes[2])) histo.Scale(options.factor) F=ROOT.TFile(options.output,"RECREATE") F.cd() histo.Write("histo") F.Close()
hGenPtUp.Fill(i + 0.5, fGenPtUp.Eval(i + 0.5)) hGenPtDn.Fill(i + 0.5, fGenPtDn.Eval(i + 0.5)) histograms = [] #ok lets populate! 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.factory("GM[15,215]") fitter.factory("weight[0,1e+32]") #datasetPDF=data.makeDataSet('lnujj_l2_gen_softDrop_mass,'+options.var,options.cut,-1) histoPDF = data.drawTH1('lnujj_l2_gen_softDrop_mass', options.cut, "1", options.binsx * 4, options.minx, options.maxx) fitter.w.var("GM").setMin(histoPDF.GetXaxis().GetXmin()) fitter.w.var("GM").setMax(histoPDF.GetXaxis().GetXmax()) fitter.w.var("GM").setBins(histoPDF.GetXaxis().GetNbins()) cList = ROOT.RooArgSet(fitter.w.var("GM"), fitter.w.var("weight")) fcache = ROOT.TFile("cache.root", "RECREATE") dataset = ROOT.RooDataSet("datasetPDF", "", cList, "weight") for i in range(1, histoPDF.GetNbinsX() + 1): x = histoPDF.GetXaxis().GetBinCenter(i) w = histoPDF.GetBinContent(i) cList.setRealValue("GM", x) cList.setRealValue("weight", w) dataset.add(cList, w)
if filename.find(sampleType) != -1: fnameParts = filename.split('.') fname = fnameParts[0] ext = fnameParts[1] if ext.find("root") == -1: continue dataPlotters.append( TreePlotter(args[0] + '/' + fname + '.root', 'tree')) dataPlotters[-1].setupFromFile(args[0] + '/' + fname + '.pck') dataPlotters[-1].addCorrectionFactor('xsec', 'tree') dataPlotters[-1].addCorrectionFactor('genWeight', 'tree') dataPlotters[-1].addCorrectionFactor('puWeight', 'tree') data = MergedPlotter(dataPlotters) denominator = data.drawTH1("0.5", options.cutDenominator, "1", 1, 0, 1) cut = options.cutNumerator nominal = data.drawTH1("0.5", '*'.join([options.cutDenominator, cut]), "1", 1, 0, 1) cut = options.cutNumerator.replace( options.var, "(" + str(options.factor) + "*" + options.var + ")") up = data.drawTH1("0.5", '*'.join([options.cutDenominator, cut]), "1", 1, 0, 1) cut = options.cutNumerator.replace( options.var, "(" + str(1.0 / options.factor) + "*" + options.var + ")") down = data.drawTH1("0.5", '*'.join([options.cutDenominator, cut]), "1", 1, 0, 1) effUp = abs(up.Integral() / nominal.Integral())
axis = ROOT.TAxis(len(cBins) - 1, array('d', cBins)) graph = {'scale': ROOT.TGraphErrors(), 'sigma': ROOT.TGraphErrors()} #dataset = data.makeDataSet(options.genvar,options.genCut,-1) for i in range(1, axis.GetNbins() + 1): cBinLo = axis.GetBinLowEdge(i) cBinHi = axis.GetBinUpEdge(i) cBinCenter = axis.GetBinCenter(i) dataset = data.makeDataSet( options.genvar, options.genCut + '*({cvar}>{lo}&&{cvar}<={hi})'.format( cvar=options.condVar, lo=cBinLo - 100, hi=cBinHi + 100), -1) histo = data.drawTH1( options.var, options.cut + '*({cvar}>{lo}&&{cvar}<={hi})'.format( cvar=options.condVar, lo=cBinLo, hi=cBinHi), "1", options.bins, options.mini, options.maxi) fitter = Fitter([options.var]) fitter.importBinnedData(histo, [options.var], 'data') fitter.gaussianSum('model', options.var, dataset, options.genvar) fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1)]) chi = fitter.projection("model", "data", options.var, "debugPlot_" + str(i) + "_" + options.output, 'x') print i, 'Chi2', chi for nuis in ['scale', 'sigma']: v, vErr = fitter.fetch(nuis) graph[nuis].SetPoint(i, cBinCenter, v) graph[nuis].SetPointError(i, 0, vErr) f2 = ROOT.TFile(options.output, "RECREATE") f2.cd()
if filename.find(sampleType) != -1: fnameParts = filename.split('.') fname = fnameParts[0] ext = fnameParts[1] if ext.find("root") == -1: continue dataPlotters.append( TreePlotter(args[0] + '/' + fname + '.root', 'tree')) dataPlotters[-1].setupFromFile(args[0] + '/' + fname + '.pck') dataPlotters[-1].addCorrectionFactor('xsec', 'tree') dataPlotters[-1].addCorrectionFactor('genWeight', 'tree') dataPlotters[-1].addCorrectionFactor('puWeight', 'tree') data = MergedPlotter(dataPlotters) histo = data.drawTH1(options.var, options.cut, "1", options.bins, options.min, options.max) histoUp = data.drawTH1(options.var + "Up", options.cut, "1", options.bins, options.min, options.max) histoDown = data.drawTH1(options.var + "Down", options.cut, "1", options.bins, options.min, options.max) histoRUp = data.drawTH1(options.var + "Smear", options.cut, "1", options.bins, options.min, options.max) histoRDown = mirror(histo, histoRUp) F = ROOT.TFile(options.output, "UPDATE") F.cd() histo.Write(options.name) histoUp.Write(options.name + "_" + options.systName + "ScaleUp") histoDown.Write(options.name + "_" + options.systName + "ScaleDown") histoRUp.Write(options.name + "_" + options.systName + "ResUp")
fitter.gaus('model', 'x') if options.fixPars != "1": fixedPars = options.fixPars.split(',') if len(fixedPars) > 1: print " - Fix parameters: ", fixedPars for par in fixedPars: if par == "c_0" or par == "c_1" or par == "c_2": continue parVal = par.split(':') fitter.w.var(parVal[0]).setVal(float(parVal[1])) 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()}}
histosSUP = [] histosSDWN = [] histosRUP = [] histosRDWN = [] bins = [ 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1500, 1750, 2000, 2500, 3000, 3500, 4000, 4500, 5000 ] axis = ROOT.TAxis(len(bins) - 1, array('d', bins)) for i in range(1, axis.GetNbins() + 1): center = axis.GetBinCenter(i) histo = data.drawTH1( options.vary, options.cut + "&&{MVV}>{low}&&{MVV}<={high}".format(MVV=options.varx, low=axis.GetBinLowEdge(i), high=axis.GetBinUpEdge(i)), "1", options.binsy, options.miny, options.maxy) histos.append(histo) graphs, histo2D = interpolate(histos, axis, options) renormalizeHisto(histo2D) #renormalizeHisto(histoSUP) #renormalizeHisto(histoSDWN) #renormalizeHisto(histoRUP) #renormalizeHisto(histoRDWN) F = ROOT.TFile(options.output, "UPDATE") F.cd() histo2D.Write(options.name) for bin, g in enumerate(graphs):
dataPlotters[-1].setupFromFile(args[0] + '/' + fname + '.pck') dataPlotters[-1].addCorrectionFactor('xsec', 'tree') dataPlotters[-1].addCorrectionFactor('genWeight', 'tree') dataPlotters[-1].addCorrectionFactor('puWeight', 'tree') if options.data == 2: sigmas = [] for d in dataPlotters: sigmas.append(d.tree.GetMaximum("xsec") / d.weightinv) sigmaW = max(sigmas) for p in dataPlotters: p.addCorrectionFactor(1.0 / sigmaW, 'flat') parameterization = {} data = MergedPlotter(dataPlotters) dataset = data.makeDataSet(options.genvar, options.genCut, -1) histo = data.drawTH1(options.var, options.cut, "1", options.bins, options.mini, options.maxi) fitter = Fitter([options.var]) fitter.importBinnedData(histo, [options.var], 'data') fitter.gaussianSum('model', options.var, dataset, options.genvar) fitter.fit('model', 'data', [ROOT.RooFit.SumW2Error(1)]) chi = fitter.projection("model", "data", options.var, "debugPlot_" + options.output + ".root", 'x') print 'Chi2', chi print 'make systematics' meanDefault = fitter.w.var("scale").getVal() sigmaDefault = fitter.w.var("sigma").getVal() f2 = ROOT.TFile(options.output, "RECREATE") f2.cd() histo = returnHisto("histo", fitter.w, options)
if ext.find("root") ==-1: continue dataPlotters.append(TreePlotter(args[0]+'/'+fname+'.root','tree')) dataPlotters[-1].setupFromFile(args[0]+'/'+fname+'.pck') dataPlotters[-1].addCorrectionFactor('xsec','tree') dataPlotters[-1].addCorrectionFactor('genWeight','tree') dataPlotters[-1].addCorrectionFactor('puWeight','tree') dataPlotters[-1].addCorrectionFactor('lnujj_sf','branch') dataPlotters[-1].addCorrectionFactor('lnujj_btagWeight','branch') dataPlotters[-1].addCorrectionFactor('truth_genTop_weight','branch') dataPlotters[-1].filename=fname data=MergedPlotter(dataPlotters) histI=data.drawTH1(options.var,options.cut,"1",1,0,1000000000) norm=histI.Integral() histograms={} histograms['nominal'] = {'scale':1.0,'weight':'1'} histograms['scale_Up'] = {'scale':1.2,'weight':'1'} histograms['scale_Down'] = {'scale':0.8,'weight':'1'} histograms['ptSpectrum_Up'] = {'scale':1.0,'weight':'(1.2*lnujj_l2_gen_pt)'} histograms['ptSpectrum_Down'] = {'scale':1.0,'weight':'(0.8*lnujj_l2_gen_pt)'} f=ROOT.TFile(options.output,"RECREATE") f.cd() for histo,info in histograms.iteritems(): histSource =data.drawTH1(str(info['scale'])+'*'+options.var,'('+options.cut+'*'+info['weight']+')',"1",int(options.binsx*options.kdeFactor),options.minx*0.8,options.maxx*1.5)
continue dataPlotters.append( TreePlotter(args[0] + '/' + fname + '.root', 'tree')) dataPlotters[-1].setupFromFile(args[0] + '/' + fname + '.pck') dataPlotters[-1].addCorrectionFactor('xsec', 'tree') dataPlotters[-1].addCorrectionFactor('genWeight', 'tree') dataPlotters[-1].addCorrectionFactor('puWeight', 'tree') dataPlotters[-1].addCorrectionFactor('lnujj_sf', 'branch') dataPlotters[-1].addCorrectionFactor('lnujj_btagWeight', 'branch') dataPlotters[-1].addCorrectionFactor('truth_genTop_weight', 'branch') dataPlotters[-1].filename = fname data = MergedPlotter(dataPlotters) variables = options.vars.split(',') histI = data.drawTH1(variables[0], options.cut, "1", 1, 0, 1000000000) norm = histI.Integral() histograms = {} histograms['nominal'] = {'scalex': 1.0, 'scaley': 1.0, 'weight': '1'} histograms['scalex_Up'] = {'scalex': 1.2, 'scaley': 1.0, 'weight': '1'} histograms['scalex_Down'] = {'scalex': 0.8, 'scaley': 1.0, 'weight': '1'} histograms['ptSpectrum_Up'] = { 'scalex': 1.0, 'scaley': 1.0, 'weight': '(1.1*lnujj_l2_gen_pt)' } histograms['ptSpectrum_Down'] = { 'scalex': 1.0, 'scaley': 1.0, 'weight': '(0.8*lnujj_l2_gen_pt)'