def getSummedShapes(path, tag, chan=''): ## FIXME get the following numbers from the file lxy_max = 5. lxy_bins = 50 if chan != '': chan = '_' + chan ## prepare a summed histogram summed_cat = [] ## open the corresponfing file tfile = mRF.openTFile(path) # nevents = mRF.getHist(tfile,'map_counter').Integral(0,2) nevents = 0. for i in xrange(ybin - 1): map_cat = mRF.getHist(tfile, 'map' + chan + '_cat' + str(i)) # n = map_cat.Integral(0,map_cat.GetNbinsX()+1) n = map_cat.Integral() nevents += n checksum = 0. checkweight = 0. ## loop over categories for i in xrange(ybin - 1): summed = TH1F(tag + '_summed_lxy' + chan + '_cat' + str(i), tag + '_summed_lxy' + chan + '_cat' + str(i), lxy_bins, 0., lxy_max).Clone() summed.Sumw2() ## get the map histo map_cat = mRF.getHist(tfile, 'map' + chan + '_cat' + str(i)) map_cat.Sumw2() checksum += map_cat.GetEntries() ## get the gamma binning per category nbins = map_cat.GetNbinsX() for j in xrange(nbins): w = float(map_cat.GetBinContent(j + 1)) checkweight += w if tag == 'templ': hist = mRF.getHist(tfile, tag + '_lxy_cat' + str(i) + '_bin' + str(j)) else: hist = mRF.getHist( tfile, tag + '_lxy' + chan + '_cat' + str(i) + '_bin' + str(j)) hist.Sumw2() ## scale the histogram integr = float(hist.Integral(0, hist.GetNbinsX() + 1)) if integr > 0 and tag == 'templ': hist.Scale(w / integr) ## sum the histograms summed.Add(hist.Clone()) summed_cat.append(summed) print 'checksum (map): ', checksum print 'checksum: ', sum([h.GetEntries() for h in summed_cat]) print 'checkweight: ', checkweight return summed_cat
def getSummedShapes(path, tag, chan=''): ## FIXME get the following numbers from the file lxy_max = 5. lxy_bins = 50 if chan != '': chan = '_'+chan ## prepare a summed histogram summed_cat = [] ## open the corresponfing file tfile = mRF.openTFile(path) # nevents = mRF.getHist(tfile,'map_counter').Integral(0,2) nevents = 0. for i in xrange(ybin-1): map_cat = mRF.getHist(tfile,'map'+chan+'_cat'+str(i)) # n = map_cat.Integral(0,map_cat.GetNbinsX()+1) n = map_cat.Integral() nevents+=n checksum = 0. checkweight = 0. ## loop over categories for i in xrange(ybin-1): summed = TH1F(tag+'_summed_lxy'+chan+'_cat'+str(i),tag+'_summed_lxy'+chan+'_cat'+str(i),lxy_bins, 0., lxy_max).Clone() summed.Sumw2() ## get the map histo map_cat = mRF.getHist(tfile,'map'+chan+'_cat'+str(i)) map_cat.Sumw2() checksum+=map_cat.GetEntries() ## get the gamma binning per category nbins = map_cat.GetNbinsX() for j in xrange(nbins): w = float(map_cat.GetBinContent(j+1)) checkweight+=w if tag=='templ': hist = mRF.getHist(tfile,tag+'_lxy_cat'+str(i)+'_bin'+str(j)) else: hist = mRF.getHist(tfile,tag+'_lxy'+chan+'_cat'+str(i)+'_bin'+str(j)) hist.Sumw2() ## scale the histogram integr = float(hist.Integral(0, hist.GetNbinsX()+1)) if integr > 0 and tag=='templ': hist.Scale(w/integr) ## sum the histograms summed.Add(hist.Clone()) summed_cat.append(summed) print 'checksum (map): ',checksum print 'checksum: ',sum([h.GetEntries() for h in summed_cat]) print 'checkweight: ',checkweight return summed_cat
def getNormalization(f,chan,tag,verbose=0): h_wjets = mRF.getHist(f,'W+jets/'+tag) h_dyll = mRF.getHist(f,'Z#rightarrow ll/'+tag) h_stop = mRF.getHist(f,'Single top/'+tag) h_ttbar = mRF.getHist(f, 't#bar{t}172.5/'+tag) h_qcd = mRF.getHist(f, 'QCD/'+tag) h_vv = mRF.getHist(f, 'VV/'+tag) h_ttv = mRF.getHist(f, 'other t#bar{t}/'+tag) h_others = h_qcd.Clone("others") h_others.Add(h_vv) h_others.Add(h_ttv) h_data = mRF.getHist(f,'data/'+tag) mcsum = h_wjets.Integral()+h_stop.Integral()+h_dyll.Integral()+h_qcd.Integral()+h_vv.Integral()+h_ttv.Integral()+h_ttbar.Integral() if verbose > 0: print '=========================================================================' print ' %-25s ' % tag print '-----------------------------' print ' %16s | %8.0f (total yields)' % ('Single Top', h_stop.Integral()) print ' %16s | %8.0f ' % ('Drell-Yan', h_dyll.Integral()) print ' %16s | %8.0f ' % ('QCD', h_qcd.Integral()) print ' %16s | %8.0f ' % ('VV', h_vv.Integral()) print ' %16s | %8.0f ' % ('ttW/Z', h_ttv.Integral()) print ' %16s | %8.0f ' % ('ttbar', h_ttbar.Integral()) print ' %16s | %8.0f ' % ('W+jets', h_wjets.Integral()) print '-----------------------------' print ' %16s | %8.0f ' % ('Sum MC', mcsum) print ' %16s | %8.0f ' % ('Data', h_data.Integral()) ## sum single top to it # st_samples = ['T_s-channel','T_t-channel','T_tW-channel','Tbar_s-channel','Tbar_t-channel','Tbar_tW-channel'] # st_weight = 1 # for st_sample in st_samples: # f_st = mRF.openTFile(path+'/'+st_sample+'.root') # h_st = mRF.getHist(f_st,'hist_charge'+chan) # for key in topinfo.samples.keys(): # if st_sample in key: # nevents = topinfo.samples[key][0] # xsec = topinfo.samples[key][1] # st_weight = topinfo.lumi[chan.replace('_','')] * xsec / nevents # h_st.Scale(st_weight) # h_wjets.Add(h_st) ## Yields n_wjets_plus = h_wjets.GetBinContent(3) n_wjets_minus = h_wjets.GetBinContent(1) n_data_plus = h_data.GetBinContent(3) n_data_minus = h_data.GetBinContent(1) n_dyll_plus = h_dyll.GetBinContent(3) n_dyll_minus = h_dyll.GetBinContent(1) n_stop_plus = h_stop.GetBinContent(3) n_stop_minus = h_stop.GetBinContent(1) n_others_plus = h_others.GetBinContent(3) n_others_minus = h_others.GetBinContent(1) n_ttbar_plus = h_ttbar.GetBinContent(3) n_ttbar_minus = h_ttbar.GetBinContent(1) n_mc_plus = n_dyll_plus+n_stop_plus+n_others_plus+n_ttbar_plus #+ n_wjets_plus n_mc_minus = n_dyll_minus+n_stop_minus+n_others_minus+n_ttbar_minus #+ n_wjets_minus ## Asymmetries a_wjets = float(n_wjets_plus-n_wjets_minus)/float(n_wjets_plus+n_wjets_minus) a_data = float(n_data_plus-n_data_minus)/float(n_data_plus+n_data_minus) a_dyll = float(n_dyll_plus-n_dyll_minus)/float(n_dyll_plus+n_dyll_minus) a_stop = float(n_stop_plus-n_stop_minus)/float(n_stop_plus+n_stop_minus) a_others = float(n_others_plus-n_others_minus)/float(n_others_plus+n_others_minus) a_ttbar = float(n_ttbar_plus-n_ttbar_minus)/float(n_ttbar_plus+n_ttbar_minus) ## Scale factors n_corr = float(n_data_plus-n_data_minus) / a_wjets n_uncorr = n_wjets_plus+n_wjets_minus # n_uncorr = h_wjets.Integral(0,4) ## Juerg? weight = float(h_wjets.Integral())/n_uncorr ## is always == 1.0? ## We're scaling actually the DIFFERENCE, not the asymmetry w_sf = (n_data_plus-n_data_minus) / (n_wjets_plus-n_wjets_minus) ## == n_corr/n_uncorr w_frac = n_corr / (n_data_plus+n_data_minus) ## Equal to a_data/a_wjets ## Errors n_wjets_plus_err = h_wjets.GetBinError(3) n_wjets_minus_err = h_wjets.GetBinError(1) n_data_plus_err = h_data.GetBinError(3) n_data_minus_err = h_data.GetBinError(1) n_dyll_plus_err = h_dyll.GetBinError(3) n_dyll_minus_err = h_dyll.GetBinError(1) n_stop_plus_err = h_stop.GetBinError(3) n_stop_minus_err = h_stop.GetBinError(1) n_others_plus_err = h_others.GetBinError(3) n_others_minus_err = h_others.GetBinError(1) n_ttbar_plus_err = h_ttbar.GetBinError(3) n_ttbar_minus_err = h_ttbar.GetBinError(1) sum_wjets_err = sqrt(pow(n_wjets_plus_err,2)+pow(n_wjets_minus_err,2)) sum_data_err = sqrt(pow(n_data_plus_err,2)+pow(n_data_minus_err,2)) sum_dyll_err = sqrt(pow(n_dyll_plus_err,2)+pow(n_dyll_minus_err,2)) sum_stop_err = sqrt(pow(n_stop_plus_err,2)+pow(n_stop_minus_err,2)) sum_others_err = sqrt(pow(n_others_plus_err,2)+pow(n_others_minus_err,2)) sum_ttbar_err = sqrt(pow(n_ttbar_plus_err,2)+pow(n_ttbar_minus_err,2)) a_wjets_err = a_wjets * sqrt(pow((sum_wjets_err /float(n_wjets_plus-n_wjets_minus)),2) +pow((sum_wjets_err /float(n_wjets_plus+n_wjets_minus)),2)) a_data_err = a_data * sqrt(pow((sum_data_err/float(n_data_plus-n_data_minus)),2)+pow((sum_data_err/float(n_data_plus+n_data_minus)),2)) a_dyll_err = a_dyll * sqrt(pow((sum_dyll_err /float(n_dyll_plus-n_dyll_minus)),2) +pow((sum_dyll_err /float(n_dyll_plus+n_dyll_minus)),2)) a_stop_err = a_stop * sqrt(pow((sum_stop_err /float(n_stop_plus-n_stop_minus)),2) +pow((sum_stop_err /float(n_stop_plus+n_stop_minus)),2)) a_others_err = a_others * sqrt(pow((sum_others_err /float(n_others_plus-n_others_minus)),2) +pow((sum_others_err /float(n_others_plus+n_others_minus)),2)) a_ttbar_err = a_ttbar * sqrt(pow((sum_ttbar_err /float(n_ttbar_plus-n_ttbar_minus)),2) +pow((sum_ttbar_err /float(n_ttbar_plus+n_ttbar_minus)),2)) n_corr_err = n_corr * sqrt(pow(sum_data_err/float(n_data_plus-n_data_minus),2)+pow(a_wjets_err/a_wjets,2)) n_uncorr_err = sqrt(h_wjets.Integral())*weight w_sf_err = w_sf * sqrt(pow(n_uncorr_err/n_uncorr,2)+pow(n_corr_err/n_corr,2)) if verbose == 0: line = '%-16s : %5.2f+-%4.2f | %9.0f | %9.0f | %9.0f | %5.2f+-%4.2f | %5.2f+-%4.2f | %5.2f%% | %5.2f ' print line % (tag[:-7], w_sf, w_sf_err, n_data_plus-n_data_minus, n_mc_plus-n_mc_minus, n_wjets_plus-n_wjets_minus, a_data*100, a_data_err*100, a_wjets*100, a_wjets_err*100, (h_wjets.Integral()/mcsum)*100, h_data.Integral()/mcsum) if verbose > 0: print '-------------------------------------------------------------------' print ' sample | N+ | N- | N+ - N- | Asymmetry |' print '-------------------------------------------------------------------' print ' Wjets | %9.0f | %9.0f | %9.0f | %7.4f +- %7.4f |' % ( n_wjets_plus, n_wjets_minus, (n_wjets_plus -n_wjets_minus), a_wjets, a_wjets_err) print ' ttbar | %9.0f | %9.0f | %9.0f | %7.4f +- %7.4f |' % ( n_ttbar_plus, n_ttbar_minus, (n_ttbar_plus-n_ttbar_minus), a_ttbar, a_ttbar_err) print ' s-top | %9.0f | %9.0f | %9.0f | %7.4f +- %7.4f |' % ( n_stop_plus, n_stop_minus, (n_stop_plus-n_stop_minus), a_stop, a_stop_err) print ' DY+jets | %9.0f | %9.0f | %9.0f | %7.4f +- %7.4f |' % ( n_dyll_plus, n_dyll_minus, (n_dyll_plus-n_dyll_minus), a_dyll, a_dyll_err) print ' others | %9.0f | %9.0f | %9.0f | %7.4f +- %7.4f |' % ( n_others_plus, n_others_minus, (n_others_plus-n_others_minus), a_others, a_others_err) print ' Data | %9.0f | %9.0f | %9.0f | %7.4f +- %7.4f |' % ( n_data_plus, n_data_minus, (n_data_plus-n_data_minus), a_data, a_data_err) print '-------------------------------------------------------------------' print ' W_sf | %6.3f +- %6.3f ' % (w_sf, w_sf_err) # print '----------------------------------------------------------------' # print ' Ncorr | (%5.2f +- %5.2f) x 10^3' % (n_corr/1e3, n_corr_err/1e3) print '----------------------------------------------------------------' print ' This SF corresponds to an estimated fraction of Wjets of %5.2f%%' % (w_frac*100) print ' (uncorrected fraction on MC was: %5.2f%%' % ((h_wjets.Integral()/mcsum)*100) print '----------------------------------------------------------------' n_wjets = [n_wjets_plus, n_wjets_minus, n_wjets_plus_err, n_wjets_minus_err] n_data = [n_data_plus, n_data_minus, n_data_plus_err, n_data_minus_err] a_wjets = [a_wjets, a_wjets_err] a_data = [a_data, a_data_err] n_corr = [n_corr, n_corr_err] n_uncorr = [n_uncorr, n_uncorr] return [w_sf, w_sf_err]
def main(): usage = 'usage: %prog [options]' parser = optparse.OptionParser(usage) parser.add_option('-i', '--inputfile' , dest='inputfile' , help='Name of the input file containing the histograms.' , default=None) parser.add_option('-o', '--outputfile', dest='outputfile' , help='Name of the output file containing the histograms.', default='lxyhist.root') parser.add_option('-b', '--batch' , dest='batch' , help='Use this flag to run root in batch mode.' , default=False, action='store_true') (opt, args) = parser.parse_args() if opt.inputfile is None: parser.error('No input file defined!') inputfile = opt.inputfile outputfile = opt.outputfile batch = opt.batch if batch: sys.argv.append( '-b' ) ROOT.gROOT.SetBatch() i_file = mRF.openTFile(inputfile) channels = ['e','mu'] procs = [k for k in mRF.GetKeyNames(i_file,'')] hists = {} for proc in procs: keys = [k for k in mRF.GetKeyNames(i_file,proc)] for key in keys: ## Select jetlxy or secvtxmass histograms for e and mu channels: try: chan, var = key.split('_', 1) except ValueError: continue if not (chan == 'e' or chan == 'mu'): continue if not (var == 'jetlxy' or var == 'secvtxmass'): continue hist = mRF.getHist(i_file, proc + '/' + key) if var == 'jetlxy': var = var.replace('jet','max') if var == 'secvtxmass': var = var.replace('secvtx','jet') ## Extract generated top mass if not 'ttbar' in proc_dict[proc]: mass = '0' else: try: mass = proc.split('}')[-1].replace('.','') except IndexError: mass = '1725' if mass == '' or 'syst' in mass: mass = '1725' name = chan+'_'+var+'_'+proc_dict[proc]+'_'+mass # print name hists[name] = hist.Clone() ## FIXME (name.replace(' ','')) o_file = ROOT.TFile.Open(outputfile,'recreate') for chan in channels: o_file.mkdir(chan) for h in hists: channel = h.split('_')[0] if channel == 'e': o_file.cd(channel) elif channel == 'mu': o_file.cd(channel) else: continue hists[h].Write() o_file.Close()
def getNormalization(i_file,chan,level,outputdir=''): postfix = chan+'_'+level postfix_qcd = chan+'_antiiso_'+level # data = mRF.getHist(i_file, chan+'/'+chan+'_'+level+'_data_0') data = mRF.getHist(i_file,'data/'+postfix) data.SetMarkerStyle(20) data.GetXaxis().SetTitle('#slashed{E}_{T} [GeV]') qcd_shape_hist = mRF.getHist(i_file,'data/'+postfix_qcd) ## take the qcd templates from antiiso data # qcd = mRF.getHist(i_file, chan+'/'+chan+'_'+level+'_qcd_0') qcd = mRF.getHist(i_file,'QCD/'+postfix) qcd.SetLineWidth(2) qcd.SetLineColor(qcd_color) qcd.SetLineStyle(qcd_color) qcd.GetXaxis().SetTitle('#slashed{E}_{T} [GeV]') # wjets = mRF.getHist(i_file, chan+'/'+chan+'_'+level+'_wjets_0').Clone() # ttbar = mRF.getHist(i_file, chan+'/'+chan+'_'+level+'_ttbar_1725').Clone() # zjets = mRF.getHist(i_file, chan+'/'+chan+'_'+level+'_zjets_0').Clone() # stop = mRF.getHist(i_file, chan+'/'+chan+'_'+level+'_singletop_0').Clone() wjets = mRF.getHist(i_file,'W+jets/'+postfix).Clone() ttbar = mRF.getHist(i_file,'t#bar{t}172.5/'+postfix).Clone() zjets = mRF.getHist(i_file,'Z#rightarrow ll/'+postfix).Clone() stop = mRF.getHist(i_file,'Single top/'+postfix).Clone() vv = mRF.getHist(i_file,'VV/'+postfix).Clone() mc = wjets.Clone('mc') mc.SetLineWidth(2) mc.SetLineColor(mc_color) mc.SetLineStyle(mc_color) mc.GetXaxis().SetTitle('#slashed{E}_{T} [GeV]') mc.Add(zjets) mc.Add(ttbar) mc.Add(stop) print 'Summary: ',level print '-'*25 print 'ttbar : %9.0f' % ttbar.Integral() print 'singletop : %9.0f' % stop.Integral() print 'wjets : %9.0f' % wjets.Integral() print 'zjets : %9.0f' % zjets.Integral() print '-'*25 print 'mc sum : %9.0f' % mc.Integral() print 'qcd : %9.0f' % qcd.Integral() print '-'*25 print 'data : %9.0f' % data.Integral() print '-'*25 frac_start = qcd.Integral(0, qcd.GetNbinsX()+1) / ( mc.Integral(0, mc.GetNbinsX()+1) + qcd.Integral(0, qcd.GetNbinsX()+1) ) start = 0.2 frac = RooRealVar('frac','frac',start,0.0,1.) met = RooRealVar('met','met',0.,300.) data_shape = RooDataHist('data','data', RooArgList(met),data) mc_shape = RooDataHist('mc_shape','mc_shape', RooArgList(met),mc) qcd_shape = RooDataHist('qcd_shape','qcd_shape',RooArgList(met),qcd_shape_hist) mc_pdf = RooHistPdf('mc_pdf' ,'mc_pdf' ,RooArgSet(met),mc_shape) qcd_pdf = RooHistPdf('qcd_pdf','qcd_pdf',RooArgSet(met),qcd_shape) model = RooAddPdf('model', 'model', RooArgList(qcd_pdf, mc_pdf), RooArgList(frac)) model.fitTo(data_shape) c = TCanvas('qcd_'+chan,'qcd_'+chan,600,600) ROOT.gStyle.SetOptTitle(0) ROOT.gStyle.SetOptStat(0) frame = met.frame() data_shape.plotOn(frame,RooFit.Name('data')) model.plotOn(frame,RooFit.Name('model')) model.plotOn(frame,RooFit.Components('mc_pdf'),RooFit.LineStyle(mc_color),RooFit.LineColor(mc_color)) model.plotOn(frame,RooFit.Components('qcd_pdf'),RooFit.LineStyle(qcd_color),RooFit.LineColor(qcd_color)) c.SetLeftMargin(0.2) frame.GetXaxis().SetTitle('#slash{E}_{T} [GeV]') frame.GetYaxis().SetTitleOffset(2.5) frame.Draw() leg = TLegend(0.6,0.65,0.85,0.85) leg.SetFillColor(0) leg.SetLineColor(0) leg.SetHeader(chandict[chan]+'-Channel') leg.AddEntry(data,'Data','p') leg.AddEntry(mc,'t#bar{t},t,W+jets,DY','l') leg.AddEntry(qcd,'QCD','l') leg.Draw() pave = TPaveText(0.6,0.5,0.85,0.65,"NDC") pave.SetBorderSize(0) pave.SetFillStyle(0) # pave.SetTextAlign(32) # pave.SetTextFont(42) pave.AddText('f_{QCD} = %.3f' % frac.getVal()+' #pm %.3f ' % frac.getError()) pave.AddText('#chi^{2}/N_{dof} = %.2f' % frame.chiSquare('model','data')) pave.AddText('SF = %.2f' % float(frac.getVal()/frac_start)) pave.Draw() outdir = '' if len(outputdir)>0: outdir = createDir(outputdir) c.Update() c.Print(outdir+'qcd_'+chan+'_'+level+'.png') c.Print(outdir+'qcd_'+chan+'_'+level+'.pdf') # c.Print('qcd_'+chan+'_'+level+'.eps') # c.Print('qcd_'+chan+'_'+level+'.C') return float(frac.getVal()/frac_start),0.