def save_templates( templates, outputDir, lead_ptrange=(None,None), namePostfix='' ) : if outputDir is None : return draw_templates = {'lead' : {}, 'subl' : {} } draw_templates['lead']['real'] = templates['lead']['real']['Data'].Clone( 'draw_%s' %templates['lead']['real']['Data'].GetName()) draw_templates['lead']['fake'] = templates['lead']['fake']['Data'].Clone( 'draw_%s' %templates['lead']['fake']['Data'].GetName()) if templates['lead']['real']['Background'] is not None : draw_templates['lead']['real'].Add( templates['lead']['real']['Background']) if templates['lead']['fake']['Background'] is not None : draw_templates['lead']['fake'].Add( templates['lead']['fake']['Background']) can_lead_real = ROOT.TCanvas('can_lead_real', '') can_lead_fake = ROOT.TCanvas('can_lead_fake', '') pt_label_lead = None if lead_ptrange[0] is not None : if lead_ptrange[1] == None : pt_label_lead = ' p_{T} > %d ' %( lead_ptrange[0] ) else : pt_label_lead = ' %d < p_{T} < %d ' %( lead_ptrange[0], lead_ptrange[1] ) draw_template( can_lead_real, draw_templates['lead']['real'], sampManFit, normalize=1, label=pt_label_lead, outputName = outputDir+'/template_lead_real%s.pdf' %namePostfix ) draw_template( can_lead_fake, draw_templates['lead']['fake'], sampManFit, normalize=1, label=pt_label_lead, outputName = outputDir+'/template_lead_fake%s.pdf' %namePostfix )
def run_photon_fit( templates, gg_hist, lead_reg, fitvar='sigmaIEIE', pid='Medium', lead_ptrange=(None,None), outputDir=None, outputPrefix='', systematics=None ) : accept_reg = ['EB', 'EE'] if lead_reg not in accept_reg : print 'Lead region does not make sense' return # get the defaults samples = get_default_samples() plotbinning = get_default_binning(var=fitvar) cuts = get_default_cuts(var=fitvar, pid=pid) # Find the bins corresponding to the cuts # lead photon on X axis, subl on Y axis bins_lead_tight = ( gg_hist.GetXaxis().FindBin( cuts[lead_reg]['tight'][0] ), gg_hist.GetXaxis().FindBin( cuts[lead_reg]['tight'][1] ) ) bins_lead_loose = ( gg_hist.GetXaxis().FindBin( cuts[lead_reg]['loose'][0] ), gg_hist.GetXaxis().FindBin( cuts[lead_reg]['loose'][1] ) ) print 'cuts, bins lead, tight = %f-%f ( %d - %d ) ' %( cuts[lead_reg]['tight'][0], cuts[lead_reg]['tight'][1], bins_lead_tight[0], bins_lead_tight[1] ) print 'cuts, bins lead, loose = %f-%f ( %d - %d ) ' %( cuts[lead_reg]['loose'][0], cuts[lead_reg]['loose'][1], bins_lead_loose[0], bins_lead_loose[1] ) # Integrate to the the data in the four regions Ndata_T = gg_hist.Integral( bins_lead_tight[0], bins_lead_tight[1]) Ndata_L = gg_hist.Integral( bins_lead_loose[0], bins_lead_loose[1]) # ufloat it! Ndata = {} Ndata['T'] = ufloat( Ndata_T, math.sqrt(Ndata_T ), 'Ndata_T' ) Ndata['L'] = ufloat( Ndata_L, math.sqrt(Ndata_L ), 'Ndata_L' ) print 'N data T = ', Ndata['T'] print 'N data L = ', Ndata['L'] # arragnge the cuts by eff_cuts = {} eff_cuts['lead'] = {} eff_cuts['lead']['tight'] = cuts[lead_reg]['tight'] eff_cuts['lead']['loose'] = cuts[lead_reg]['loose'] # get 2-d efficiencies from 1-d inputs (eff_1d_stat, eff_1d_syst) =generate_1d_efficiencies( templates, eff_cuts, lead_reg, lead_ptrange, systematics=systematics ) print 'eff_1d' print eff_1d_stat results_stat = run_fit( {'T': Ndata['T'], 'L' : Ndata['L']}, eff_1d_stat ) results_syst = run_fit( {'T': Ndata['T'], 'L' : Ndata['L']}, eff_1d_syst ) print 'Real Norm = ', results_stat.item(0) print 'Fake Norm = ', results_stat.item(1) hist_temp_lead_real = templates['lead']['real']['Data'].Clone( 'hist_temp_lead_real' ) hist_temp_lead_fake = templates['lead']['fake']['Data'].Clone( 'hist_temp_lead_fake' ) if templates['lead']['real']['Background'] is not None : hist_temp_lead_real.Add( templates['lead']['real']['Background'] ) if templates['lead']['fake']['Background'] is not None : hist_temp_lead_fake.Add( templates['lead']['fake']['Background'] ) p_R_T = results_stat.item(0)*eff_1d_stat['eff_R_T_lead'] p_R_L = results_stat.item(0)*eff_1d_stat['eff_R_L_lead'] p_F_T = results_stat.item(1)*eff_1d_stat['eff_F_T_lead'] p_F_L = results_stat.item(1)*eff_1d_stat['eff_F_L_lead'] print 'nPred Real Tight = ', p_R_T print 'nPred Real Loose = ', p_R_L print 'nPred Fake Tight = ', p_F_T print 'nPred Fake Loose = ', p_F_L #normalize lead real template according to fit hist_temp_lead_real.Scale( ( p_R_T+p_R_L ).n/ hist_temp_lead_real.Integral() ) hist_temp_lead_fake.Scale( ( p_F_T+p_F_L ).n/ hist_temp_lead_fake.Integral() ) hist_temp_lead_real.SetLineColor(ROOT.kMagenta) hist_temp_lead_real.SetMarkerColor(ROOT.kMagenta) hist_temp_lead_real.SetLineWidth(2) hist_temp_lead_fake.SetLineColor(ROOT.kRed) hist_temp_lead_fake.SetMarkerColor(ROOT.kRed) hist_temp_lead_fake.SetLineWidth(2) hist_temp_lead_real.SetStats(0) hist_temp_lead_fake.SetStats(0) can_proj_lead = ROOT.TCanvas('proj_lead', '') namePostfix = '__%s' %( lead_reg ) if lead_ptrange[0] is not None : if lead_ptrange[1] is None : namePostfix += '__pt_%d-max' %( lead_ptrange[0]) else : namePostfix += '__pt_%d-%d' %( lead_ptrange[0], lead_ptrange[1]) outputNamePL=None if outputDir is not None : outputNamePL = outputDir + '/fit_with_data_projlead%s%s.pdf' %(outputPrefix, namePostfix) if lead_reg == 'EE' : gg_hist.Rebin(10) hist_temp_lead_real.Rebin(10) hist_temp_lead_fake.Rebin(10) draw_template(can_proj_lead, [gg_hist, hist_temp_lead_real, hist_temp_lead_fake], sampManFit, normalize=False, first_hist_is_data=True, legend_entries=['Data', 'real+fake prediction', 'fake+fake prediction' ], outputName=outputNamePL ) text_results_stat = collect_results( results_stat, Ndata, eff_1d_stat, templates, bins_lead_loose, bins_lead_tight ) text_results_syst = collect_results( results_syst, Ndata, eff_1d_stat, templates, bins_lead_loose, bins_lead_tight ) return text_results_stat, text_results_syst