def __init__(self, directory, filenames, isData): self.dataPlotters = [] sampleTypes = filenames.split(',') for filename in os.listdir(directory): for sampleType in sampleTypes: if filename.find(sampleType) != -1: fnameParts = filename.split('.') fname = fnameParts[0] ext = fnameParts[1] if ext.find("root") == -1: continue self.dataPlotters.append( TreePlotter(directory + '/' + fname + '.root', 'tree')) if not isData: self.dataPlotters[-1].setupFromFile(directory + '/' + fname + '.pck') self.dataPlotters[-1].addCorrectionFactor( 'xsec', 'tree') self.dataPlotters[-1].addCorrectionFactor( 'genWeight', 'tree') self.dataPlotters[-1].addCorrectionFactor( 'puWeight', 'tree') self.plotter = MergedPlotter(self.dataPlotters)
cuts['b'] = '(lnujj_nMediumBTags>0)*lnujj_btagWeight' cuts['resW']='(lnujj_l2_mergedVTruth==1)' cuts['nonres']='(lnujj_l2_mergedVTruth==0)' #change the CMS_lumi variables (see CMS_lumi.py) lumi_13TeV = "35.9 fb^{-1}" lumi_sqrtS = "13 TeV" # used with iPeriod = 0, e.g. for simulation-only plots (default is an empty string) iPeriod=4 iPos = 11 #zjPlotters=[] VJetsPlotters = getPlotters('DYJetsToLL_M50_HT,WJetsToLNu_HT',False) VJets = MergedPlotter(VJetsPlotters) TTVVPlotters = getPlotters('TT_pow,T_tWch,TBar_tWch,WWTo1L1Nu2Q,WZTo1L1Nu2Q',False) ttVV=MergedPlotter(TTVVPlotters) BKGPlotters = getPlotters('DYJetsToLL_M50_HT,WJetsToLNu_HT,TT_pow,T_tWch,TBar_tWch,WWTo1L1Nu2Q,WZTo1L1Nu2Q',False,cuts['nonres']) BKG = MergedPlotter(BKGPlotters) WPlotters = getPlotters('TT_pow,T_tWch,TBar_tWch,WWTo1L1Nu2Q,WZTo1L1Nu2Q',False,cuts['resW']) W = MergedPlotter(WPlotters)
TTJets.addCorrectionFactor('genWeight','genWeight',0.0,'lnN') TTJets.setFillProperties(1001,ROOT.kGreen-5) qcdPlotters=[] for sample in [ "QCD_Pt1000to1400_50ns","QCD_Pt120to170_50ns","QCD_Pt1400to1800_50ns","QCD_Pt170to300_50ns","QCD_Pt1800to2400_50ns","QCD_Pt2400to3200_50ns","QCD_Pt300to470_50ns","QCD_Pt3200_50ns","QCD_Pt470to600_50ns","QCD_Pt600to800_50ns","QCD_Pt800to1000_50ns","QCD_Pt80to120_50ns" ]: qcdPlotters.append(TreePlotter('samples/'+sample+'.root','tree')) qcdPlotters[-1].setupFromFile('samples/'+sample+'.pck') qcdPlotters[-1].addCorrectionFactor('xsec','xsec',0.0,'lnN') qcdPlotters[-1].addCorrectionFactor('genWeight','genWeight',0.0,'lnN') QCD = MergedPlotter(qcdPlotters) QCD.setFillProperties(1001,ROOT.kAzure-9) #QCD.setFillProperties(1001,ROOT.kGreen-5) singleMu = TreePlotter('samples/SingleMuon_Run2015B.root','tree') singleMu.addCorrectionFactor('hlt','(HLT_ISOMU_50ns==1||(HLT_MU==1))',0.0,'lnN') singleEle = TreePlotter('samples/SingleElectron_Run2015B.root','tree') singleEle.addCorrectionFactor('hlt','((HLT_ISOELE_50ns==1||(HLT_ELE==1))&&(!(HLT_ISOMU_50ns==1||(HLT_MU==1))))',0.0,'lnN') jetHT = TreePlotter('samples/JetHT_Run2015B.root','tree') #jetHT.addCorrectionFactor('hlt','((HLT_JJ==1||HLT_HT==1)&&(!(HLT_ISOMU_50ns==1||(HLT_MU==1)||(HLT_ISOELE_50ns==1||(HLT_ELE==1)))))',0.0,'lnN')
for sampleType in sampleTypes: 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')) 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]))
#create the W+jets plotters wjPlotters = [] for sample in [ "WJetsToLNu_HT1200to2500", "WJetsToLNu_HT2500toInf", "WJetsToLNu_HT400to600", "WJetsToLNu_HT600to800", "WJetsToLNu_HT800to1200", 'WJetsToLNu_HT100to200', 'WJetsToLNu_HT200to400' ]: wjPlotters.append(TreePlotter('samples/' + sample + '.root', 'tree')) wjPlotters[-1].setupFromFile('samples/' + sample + '.pck') wjPlotters[-1].addCorrectionFactor('xsec', 'tree') wjPlotters[-1].addCorrectionFactor('genWeight', 'tree') wjPlotters[-1].addCorrectionFactor('puWeight', 'tree') WJets = MergedPlotter(wjPlotters) tt = TreePlotter('samples/TTJets.root', 'tree') tt.setupFromFile('samples/TTJets.pck') tt.addCorrectionFactor('xsec', 'tree') tt.addCorrectionFactor('genWeight', 'tree') tt.addCorrectionFactor('puWeight', 'tree') #create the Z+jets plotters #zPlotters=[] #for sample in ['DYJetsToLL_M50_HT100to200','DYJetsToLL_M50_HT200to400','DYJetsToLL_M50_HT400to600','DYJetsToLL_M50_HT600toInf']: # zPlotters.append(TreePlotter('samples/'+sample+'.root','tree')) # zPlotters[-1].setupFromFile('samples/'+sample+'.pck') # zPlotters[-1].addCorrectionFactor('xsec','tree') # zPlotters[-1].addCorrectionFactor('genWeight','tree')
cuts['mu'] = '(abs(ljj_l1_l1_pdgId)==13)' cuts['e'] = '(abs(lljj_l1_l1_pdgId)==11)' cuts['HP'] = '(lljj_l2_tau2/lljj_l2_tau1<0.55)' cuts['LP'] = '(lljj_l2_tau2/lljj_l2_tau1>0.55&&lljj_l2_tau2/lljj_l2_tau1<0.75)' #change the CMS_lumi variables (see CMS_lumi.py) lumi_13TeV = "35.9 fb^{-1}" lumi_sqrtS = "13 TeV" # used with iPeriod = 0, e.g. for simulation-only plots (default is an empty string) iPeriod = 4 iPos = 11 #zjPlotters=[] BKGPlotters = getPlotters( 'DYJetsToLL_M50_HT,WJetsToLNu_HT,TT_pow,T_tWch,TBar_tWch', False) BKG = MergedPlotter(BKGPlotters) VVPlotters = getPlotters('WZTo2L2Q,WZTo1L1Nu2Q', False) VV = MergedPlotter(VVPlotters) DATAPlotters = getPlotters('SingleMuon_,SingleElectron_', True) data = MergedPlotter(DATAPlotters) SigPlotters = getPlotters('WprimeToWZToWhadZlep_narrow_1400', False) sig = MergedPlotter(SigPlotters) #Fill properties BKG.setFillProperties(1001, ROOT.kAzure - 9) VV.setFillProperties(1001, ROOT.kSpring - 5) #Stack for lnu+J
TreePlotter(args[0] + '/' + fname + '.root', 'tree')) if options.data == 0 or options.data == 2: dataPlotters[-1].setupFromFile(args[0] + '/' + fname + '.pck') dataPlotters[-1].addCorrectionFactor('xsec', 'tree') dataPlotters[-1].addCorrectionFactor('genWeight', 'tree') dataPlotters[-1].addCorrectionFactor('puWeight', 'tree') corrFactors = options.factors.split(',') for c in corrFactors: if len(c.split(':')) < 2: continue if c.split(':')[0] in fname: print "Add correction factor:", fname, c.split(':')[1] dataPlotters[-1].addCorrectionFactor( float(c.split(':')[1]), 'flat') 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]))
for filename in os.listdir(args[0]): for sampleType in sampleTypes: 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)
legs = ["l1", "l2"] plotters = [] for name in samples.keys(): plotters.append( TreePlotter(args[0] + '/' + samples[name] + '.root', 'tree')) plotters[-1].setupFromFile(args[0] + '/' + samples[name] + '.pck') plotters[-1].addCorrectionFactor('xsec', 'tree') plotters[-1].addCorrectionFactor('genWeight', 'tree') plotters[-1].addCorrectionFactor('puWeight', 'tree') corrFactor = options.corrFactorW if samples[name].find('Z') != -1: corrFactor = options.corrFactorZ plotters[-1].addCorrectionFactor(corrFactor, 'flat') plotter = MergedPlotter(plotters) print 'Fitting Mjet:' for leg in legs: fitter = Fitter(['x']) #fitter.jetResonanceVjets('model','x') 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
for filename in os.listdir(args[0]): for sampleType in sampleTypes: 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) histos = [] 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)
# h2=p2.drawTH1Binned(var,cut2,lumi,bins,title,unit, "SAMEHIST") legend.AddEntry(h1, leg1, "LEP") legend.AddEntry(h2, leg2, "LF") legend.AddEntry(h3, leg3, "LF") legend.Draw("same") cmslabel_prelim(canvas, '2016', 11) canvas.Update() return canvas, h1, h2, h3, legend #,pt QCDPtPlotters = getPlotters(nonResTemplate, False) QCDhtPlotters = getPlotters('QCD_HT', False) QCDherwigPlotters = getPlotters('QCD_Pt-', False) QCD = MergedPlotter(QCDPtPlotters) QCDht = MergedPlotter(QCDhtPlotters) QCDherwig = MergedPlotter(QCDherwigPlotters) # # # DATAPlotters = getPlotters('SingleJet',True) # # data=MergedPlotter(DATAPlotters) # # SigPlotters = getPlotters('QstarToQW_2000',False) # sig = MergedPlotter(SigPlotters) # # QCD.setFillProperties(1001, 921) QCDht.setLineProperties(1, 633, 2) QCDht.setFillProperties(1001, 0)
from CMGTools.VVResonances.plotting.MergedPlotter import MergedPlotter from CMGTools.VVResonances.plotting.StackPlotter import StackPlotter #create the W+jets plotters wjPlotters=[] for sample in ['WJetsToLNu_HT100to200','WJetsToLNu_HT200to400','WJetsToLNu_HT400to600','WJetsToLNu_HT600toInf']: wjPlotters.append(TreePlotter('samples/'+sample+'.root','tree')) wjPlotters[-1].setupFromFile('samples/'+sample+'.pck') wjPlotters[-1].addCorrectionFactor('xsec','xsec',0.0,'lnN') wjPlotters[-1].addCorrectionFactor('genWeight','genWeight',0.0,'lnN') WJets = MergedPlotter(wjPlotters) WJets.setFillProperties(1001,ROOT.kAzure-9) RSGWWLNuQQ = TreePlotter('samples/RSGravToWWToLNQQ_kMpl01_2500.root','tree') RSGWWLNuQQ.setupFromFile('samples/RSGravToWWToLNQQ_kMpl01_2500.pck') RSGWWLNuQQ.setFillProperties(0,ROOT.kWhite) RSGWWLNuQQ.setLineProperties(1,ROOT.kOrange+10,3) RSGWWLNuQQ.addCorrectionFactor('xsec',0.001,0.0,'lnN') #Stack vvStack = StackPlotter() vvStack.addPlotter(WJets,"W+jets","W+Jets","background") vvStack.addPlotter(RSGWWLNuQQ,"RSG2000","RSGWW #rightarrow l#nu QQ","signal")
TreePlotter(args[0] + '/' + fname + '.root', 'tree')) if options.data == 0 or options.data == 2: 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()
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') 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') data = MergedPlotter(dataPlotters) orderInfo = {} orders = options.order.split('|') for v in orders: sp = v.split(':') orderInfo[sp[0]] = int(sp[1]) h = 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(h) fitter = Fitter(['M', 'mjj']) fitter.w.var("M").setVal((options.maxx - options.minx) / 2.0) fitter.w.var("M").setMax(options.maxx)
continue dataPlotters.append( TreePlotter(args[0] + '/' + fname + '.root', 'tree')) if options.data == 0 or options.data == 2: 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') data = MergedPlotter(dataPlotters) cBinsStr = options.condBins.split(',') cBins = [] for b in cBinsStr: cBins.append(float(b)) 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)
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') 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")
for filename in os.listdir(args[0]): for sampleType in sampleTypes: 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) #STandard histogramming histo = data.drawTH2Keys(options.varx + "," + options.vary, options.cut, options.binsx, options.minx, options.maxx, options.binsy, options.miny, options.maxy) if histo.Integral() > 0: histoNorm = data.drawTH2(options.vary + ":" + options.varx, options.cut, "1", options.binsx, options.minx, options.maxx, options.binsy, options.miny, options.maxy) histo.Scale(1.0 / histo.Integral()) histo.Scale(histoNorm.Integral()) F = ROOT.TFile(options.output, "UPDATE") F.cd() histo.Write(options.name) F.Close()
for filename in os.listdir(args[0]): for sampleType in sampleTypes: 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) 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()
for sample in [ "WJetsToLNu_HT1200to2500", "WJetsToLNu_HT2500toInf", "WJetsToLNu_HT400to600", "WJetsToLNu_HT600to800", "WJetsToLNu_HT800to1200", 'WJetsToLNu_HT100to200', 'WJetsToLNu_HT200to400' ]: #for sample in ["WJetsToLNu_HT1200to2500","WJetsToLNu_HT2500toInf","WJetsToLNu_HT400to600","WJetsToLNu_HT600to800","WJetsToLNu_HT800to1200",'WJetsToLNu_HT100to200','WJetsToLNu_HT200to400']: wjPlotters.append(TreePlotter('samples/' + sample + '.root', 'tree')) wjPlotters[-1].setupFromFile('samples/' + sample + '.pck') wjPlotters[-1].addCorrectionFactor('xsec', 'tree') wjPlotters[-1].addCorrectionFactor('genWeight', 'tree') wjPlotters[-1].addCorrectionFactor('puWeight', 'tree') # wjPlotters[-1].addCorrectionFactor('0.82','flat') WJets = MergedPlotter(wjPlotters) ttO = TreePlotter('samples/TTJets.root', 'tree') ttO.setupFromFile('samples/TTJets.pck') ttO.addCorrectionFactor('xsec', 'tree') ttO.addCorrectionFactor('genWeight', 'tree') ttO.addCorrectionFactor('puWeight', 'tree') ttO.addCorrectionFactor( '(!(lnujj_l2_mergedVTruth==1&&lnujj_l2_nearestBDRTruth>0.8))', 'tree') ttM = TreePlotter('samples/TTJets.root', 'tree') ttM.setupFromFile('samples/TTJets.pck') ttM.addCorrectionFactor('xsec', 'tree') ttM.addCorrectionFactor('genWeight', 'tree') ttM.addCorrectionFactor('puWeight', 'tree') ttM.addCorrectionFactor(
for filename in os.listdir(args[0]): for sampleType in sampleTypes: 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) #STandard histogramming if not options.keys: histo = data.drawTH2(options.vary + ":" + options.varx, options.cut, "1", options.binsx, options.minx, options.maxx, options.binsy, options.miny, options.maxy) histoSUP = data.drawTH2(options.vary + "Up:" + options.varx, options.cut, "1", options.binsx, options.minx, options.maxx, options.binsy, options.miny, options.maxy) histoSDWN = data.drawTH2(options.vary + "Down:" + options.varx, options.cut, "1", options.binsx, options.minx, options.maxx, options.binsy, options.miny, options.maxy) histoRUP = data.drawTH2(options.vary + "Smear:" + options.varx, options.cut, "1", options.binsx, options.minx,
leg1.AddEntry( profiles[4], "400-500 GeV, slope = %.3f #pm %.3f" % (frRes4.Parameter(1), frRes4.ParError(1)), "LEP") profiles[5].Draw("same") leg1.AddEntry( profiles[5], "1000-1100 GeV, slope = %.3f #pm %.3f" % (frRes5.Parameter(1), frRes5.ParError(1)), "LEP") profiles[7].Draw("same") leg1.AddEntry( profiles[7], "All jets, slope = %.4f #pm %.4f" % (frRes7.Parameter(1), frRes7.ParError(1)), "LEP") leg1.Draw("") cmslabel_sim(canvas1, '2016', 11) canvas1.Update() canvas1.SaveAs("rho_" + file.replace(".root", ".pdf")) sleep(10) QCDPtPlotters = getPlotters("QCD_Pt_", False) QCD = MergedPlotter(QCDPtPlotters) # QCDherwigPlotters = getPlotters('QCD_Pt-',False) # QCDherwig = MergedPlotter(QCDherwigPlotters) QCD.setLineProperties(1, 434, 2) QCD.setFillProperties(1001, 0) file1 = doDDT(QCD, cuts['common'], "rho_pythia.root") file2 = doDDT(QCD, cuts['commonTOT'], "rho_pythia_FullSel.root") doFit("rho_pythia.root", False) doFit("rho_pythia_FullSel.root", True)
def loadSignalNtuples(samples, sampleDir, minMX, maxMX, corr=1.): sampleTypes = samples.split(',') plotters = {} pcuts = {} filelist = [] if sampleDir == 'ntuples': filelist = [ g for flist in [[(path + '/' + f) for f in os.listdir(sampleDir + '/' + path)] for path in os.listdir(sampleDir)] for g in flist ] else: filelist = os.listdir(sampleDir) for filename in filelist: for sampleType in sampleTypes: if filename.find(sampleType) != -1: fnameParts = filename.split('.') fname = fnameParts[0] ext = fnameParts[1] if ext.find("root") == -1: continue fpath = sampleDir + '/' + fname mass = float(fname.split('_')[-1]) if mass < minMX or mass > maxMX: continue if not mass in plotters.keys(): plotters[mass] = [] pcuts[mass] = [] plotters[mass].append(TreePlotter(fpath + '.root', 'tree')) ## Fix for cuts and weights for which the branches differ between years: if "ntuples2016" in fpath: pcuts[mass].append( '(HLT_MU||HLT_ELE||HLT_ISOMU||HLT_ISOELE||HLT_MET||HLT_PHOTON)*L1PrefireWeight' ) elif "ntuples2017" in fpath: pcuts[mass].append( '(HLT_MU||HLT_ELE||HLT_ISOMU||HLT_ISOELE||HLT_MET||HLT_PHOTON)*L1PrefireWeight' ) elif "ntuples2018" in fpath: pcuts[mass].append( '(HLT_MU||HLT_ELE||HLT_ISOMU||HLT_ISOELE||HLT_MET)') else: sys.exit("Year not found, aborting.") plotters[mass][-1].setupFromFile(fpath + '.pck') plotters[mass][-1].addCorrectionFactor('xsec', 'tree') plotters[mass][-1].addCorrectionFactor('genWeight', 'tree') plotters[mass][-1].addCorrectionFactor('puWeight', 'tree') #plotters[mass][-1].addCorrectionFactor('lnujj_sfWV','branch') #plotters[mass][-1].addCorrectionFactor('lnujj_btagWeight','branch') plotters[mass][-1].addCorrectionFactor(corr, 'flat') plotters[mass][-1].filename = fname print 'found', fpath, 'mass', str(mass) mergedplotters = {} for mass in sorted(plotters.keys()): if len(plotters[mass]) != ( 1, (3, 2)['Wprime_VBF_Wh_Wlephinc_narrow' in samples] )[sampleDir == 'ntuples']: ##FIXME: rechange when 2017 VBFWH is ready continue mergedplotters[mass] = MergedPlotter(plotters[mass], pcuts[mass]) return mergedplotters
from CMGTools.VVResonances.plotting.TreePlotter import TreePlotter from CMGTools.VVResonances.plotting.MergedPlotter import MergedPlotter from CMGTools.VVResonances.plotting.StackPlotter import StackPlotter #create the W+jets plotters wjPlotters = [] for sample in [ 'WJetsToLNu_HT100to200', 'WJetsToLNu_HT200to400', 'WJetsToLNu_HT400to600', 'WJetsToLNu_HT600toInf' ]: wjPlotters.append(TreePlotter('samples/' + sample + '.root', 'tree')) wjPlotters[-1].setupFromFile('samples/' + sample + '.pck') wjPlotters[-1].addCorrectionFactor('xsec', 'xsec', 0.0, 'lnN') WJets = MergedPlotter(wjPlotters) WJets.setFillProperties(1001, ROOT.kAzure - 9) RSGWWLNuQQ = TreePlotter('samples/RSGravToWWToLNQQ_2000.root', 'tree') RSGWWLNuQQ.setupFromFile('samples/RSGravToWWToLNQQ_2000.pck') RSGWWLNuQQ.setFillProperties(0, ROOT.kWhite) RSGWWLNuQQ.setLineProperties(1, ROOT.kOrange + 10, 3) #RSGWWLNuQQ..addCorrectionFactor('xsec',0.001,0.0,'lnN') #Stack vvStack = StackPlotter() vvStack.addPlotter(WJets, "W+jets", "W+Jets", "background") vvStack.addPlotter(RSGWWLNuQQ, "RSG2000", "RSGWW #rightarrow l#nu QQ", "signal")
for filename in os.listdir(args[0]): for sampleType in sampleTypes: if filename.find(sampleType)!=-1: fnameParts=filename.split('.') fname=fnameParts[0] ext=fnameParts[1] print fname 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) binsxStr=options.binsx.split(',') binsx=[] for b in binsxStr: binsx.append(float(b)) binsz=[] for b in range(0,51): binsz.append(0.7+0.7*b/50.0) scalexHisto=ROOT.TH1F("scalexHisto","scaleHisto",len(binsx)-1,array('d',binsx)) resxHisto=ROOT.TH1F("resxHisto","resHisto",len(binsx)-1,array('d',binsx))
for filename in os.listdir(args[0]): for sampleType in sampleTypes: 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)
from CMGTools.VVResonances.plotting.MergedPlotter import MergedPlotter from CMGTools.VVResonances.plotting.StackPlotter import StackPlotter #create the W+jets plotters wjPlotters=[] for sample in ["WJetsToLNu_HT1200to2500","WJetsToLNu_HT2500toInf","WJetsToLNu_HT400to600","WJetsToLNu_HT600to800","WJetsToLNu_HT800to1200",'WJetsToLNu_HT100to200','WJetsToLNu_HT200to400']: wjPlotters.append(TreePlotter('samples/'+sample+'.root','tree')) wjPlotters[-1].setupFromFile('samples/'+sample+'.pck') wjPlotters[-1].addCorrectionFactor('xsec','tree') wjPlotters[-1].addCorrectionFactor('genWeight','tree') wjPlotters[-1].addCorrectionFactor('puWeight','tree') WJets = MergedPlotter(wjPlotters) tt=TreePlotter('samples/TTJets.root','tree') tt.setupFromFile('samples/TTJets.pck') tt.addCorrectionFactor('xsec','tree') tt.addCorrectionFactor('genWeight','tree') tt.addCorrectionFactor('puWeight','tree') #create the Z+jets plotters zPlotters=[] for sample in ['DYJetsToLL_M50_HT100to200','DYJetsToLL_M50_HT200to400','DYJetsToLL_M50_HT400to600','DYJetsToLL_M50_HT600toInf']: zPlotters.append(TreePlotter('samples/'+sample+'.root','tree'))
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) binningx=map(float,options.binningx.split(',')) binningy=map(float,options.binningy.split(',')) #STandard histogramming histo=data.drawTH2Binned(options.vary+":"+options.varx,options.cut,"1",binningx,binningy) histoSUP=data.drawTH2Binned(options.vary+"Up:"+options.varx,options.cut,"1",binningx,binningy) histoSDWN=data.drawTH2Binned(options.vary+"Down:"+options.varx,options.cut,"1",binningx,binningy) histoRUP=data.drawTH2Binned(options.vary+"Smear:"+options.varx,options.cut,"1",binningx,binningy) histoRDWN = mirror(histo,histoRUP)
def getPlotters(samples, sampleDir, isData=False, corr="1"): sampleTypes = samples.split(',') plotters = [] pcuts = [] filelist = [] if sampleDir == 'ntuples': filelist = [ g for flist in [[(path + '/' + f) for f in os.listdir(sampleDir + '/' + path)] for path in os.listdir(sampleDir)] for g in flist ] else: filelist = os.listdir(sampleDir) for filename in filelist: for sampleType in sampleTypes: if filename.find(sampleType) != -1: fnameParts = filename.split('.') fname = fnameParts[0] ext = fnameParts[1] if ext.find("root") == -1: continue fpath = sampleDir + '/' + fname print 'Adding file', fpath plotters.append(TreePlotter(fpath + '.root', 'tree')) ## Fix for cuts and weights for which the branches differ between years: if "ntuples2016" in fpath: pcuts.append( '(HLT_MU||HLT_ELE||HLT_ISOMU||HLT_ISOELE||HLT_MET||HLT_PHOTON)*L1PrefireWeight' ) elif "ntuples2017" in fpath: pcuts.append( '(HLT_MU||HLT_ELE||HLT_ISOMU||HLT_ISOELE||HLT_MET||HLT_PHOTON)*L1PrefireWeight' ) elif "ntuples2018" in fpath: pcuts.append( '(HLT_MU||HLT_ELE||HLT_ISOMU||HLT_ISOELE||HLT_MET)') else: sys.exit("Year not found, aborting.") if not isData: plotters[-1].setupFromFile(fpath + '.pck') plotters[-1].addCorrectionFactor('xsec', 'tree') plotters[-1].addCorrectionFactor('genWeight', 'tree') plotters[-1].addCorrectionFactor('puWeight', 'tree') plotters[-1].addCorrectionFactor('truth_genTop_weight', 'branch') ##plotters[-1].addCorrectionFactor('lnujj_sfWV','branch') ##plotters[-1].addCorrectionFactor('lnujj_btagWeight','branch') plotters[-1].addCorrectionFactor(corr, 'flat') #''' ## remove the Madgraph background NLO k-factors if REMOVEBKGDKFAC: if fname.find("WJetsToLNu_HT") != -1: wjetsAntiKFactor = 1. / 1.21 plotters[-1].addCorrectionFactor( wjetsAntiKFactor, 'flat') print ' reweighting ' + fpath + ' ' + str( wjetsAntiKFactor) elif fname.find("DYJetsToLL_M50_HT") != -1: dyAntiKFactor = 1. / 1.23 plotters[-1].addCorrectionFactor( dyAntiKFactor, 'flat') print ' reweighting ' + fpath + ' ' + str( dyAntiKFactor) #''' #''' ## rescale the W+jets yields (the current factors were computed from 30 < mjet < 50 GeV, on top of the NLO k-factors) if RESCALEWJETS: if fname.find("WJetsToLNu_HT") != -1: wjetsFactor = 1. if "ntuples2016" in fpath: wjetsFactor = 1.022 elif "ntuples2017" in fpath: wjetsFactor = 0.926 elif "ntuples2018" in fpath: wjetsFactor = 0.860 plotters[-1].addCorrectionFactor( wjetsFactor, 'flat') print ' reweighting ' + fpath + ' ' + str( wjetsFactor) #''' return MergedPlotter(plotters, pcuts)
'branch') ##dataPlotters[-1].addCorrectionFactor('lnujj_sf','branch') ##dataPlotters[-1].addCorrectionFactor('lnujj_btagWeight','branch') dataPlotters[-1].filename = fname dataPlottersNW.append( TreePlotter(args[0] + '/' + fname + '.root', 'tree')) dataPlottersNW[-1].addCorrectionFactor('genWeight', 'tree') dataPlottersNW[-1].addCorrectionFactor('puWeight', 'tree') dataPlottersNW[-1].addCorrectionFactor('truth_genTop_weight', 'branch') ##dataPlottersNW[-1].addCorrectionFactor('lnujj_sf','branch') ##dataPlottersNW[-1].addCorrectionFactor('lnujj_btagWeight','branch') dataPlottersNW[-1].filename = fname data = MergedPlotter(dataPlotters) ## Make histograms for uncertainty weights uncWeights = options.uncweight.split(',') uncw1 = uncWeights[0] hGenPtUp = ROOT.TH1F("hGenPtUp", "hGenPtUp", 5000, 0, 5000) hGenPtDn = ROOT.TH1F("hGenPtDn", "hGenPtDn", 5000, 0, 5000) fGenPtUp = ROOT.TF1("fGenPtUp", uncw1.replace("lnujj_l2_gen_pt", "x"), 0, 5000) fGenPtDn = ROOT.TF1("fGenPtDn", '1/' + uncw1.replace("lnujj_l2_gen_pt", "x"), 0, 5000) for i in range(5000): hGenPtUp.Fill(i + 0.5, fGenPtUp.Eval(i + 0.5)) hGenPtDn.Fill(i + 0.5, fGenPtDn.Eval(i + 0.5)) histograms = []
for sampleType in sampleTypes: 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')) 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]))
qcdPlotters = [] for sample in [ "QCD_Pt1000to1400_50ns", "QCD_Pt120to170_50ns", "QCD_Pt1400to1800_50ns", "QCD_Pt170to300_50ns", "QCD_Pt1800to2400_50ns", "QCD_Pt2400to3200_50ns", "QCD_Pt300to470_50ns", "QCD_Pt3200_50ns", "QCD_Pt470to600_50ns", "QCD_Pt600to800_50ns", "QCD_Pt800to1000_50ns", "QCD_Pt80to120_50ns" ]: qcdPlotters.append(TreePlotter('samples/' + sample + '.root', 'tree')) qcdPlotters[-1].setupFromFile('samples/' + sample + '.pck') qcdPlotters[-1].addCorrectionFactor('xsec', 'xsec', 0.0, 'lnN') qcdPlotters[-1].addCorrectionFactor('genWeight', 'genWeight', 0.0, 'lnN') QCD = MergedPlotter(qcdPlotters) QCD.setFillProperties(1001, ROOT.kAzure - 9) #QCD.setFillProperties(1001,ROOT.kGreen-5) singleMu = TreePlotter('samples/SingleMuon_Run2015B.root', 'tree') singleMu.addCorrectionFactor('hlt', '(HLT_ISOMU_50ns==1||(HLT_MU==1))', 0.0, 'lnN') singleEle = TreePlotter('samples/SingleElectron_Run2015B.root', 'tree') singleEle.addCorrectionFactor( 'hlt', '((HLT_ISOELE_50ns==1||(HLT_ELE==1))&&(!(HLT_ISOMU_50ns==1||(HLT_MU==1))))', 0.0, 'lnN') jetHT = TreePlotter('samples/JetHT_Run2015B.root', 'tree')
samples = {} sampleTypes = options.samples.split(',') dataPlotters = [] for filename in os.listdir(args[0]): for sampleType in sampleTypes: 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) graphs = runFits(data, options) f = ROOT.TFile(options.output + ".root", "RECREATE") f.cd() for i, g in enumerate(graphs): g.Write("p" + str(i)) f.Close()
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') 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'] = {