def computeCrossRatioFromQCDMC(self): ''' Study the correlation of two variables computing the ratio r = N_A * ND / N_B * N_C ''' quantity = Quantity(name_flat='hnl_mass', nbins=1, bin_min=0, bin_max=5.4) #hist_mc_tot_A = self.getHistoMC(quantity=quantity, selection=self.selection+' && b_mass<6.27 && hnl_charge==0') #hist_mc_tot_A = self.getHistoMC(quantity=quantity, selection=self.selection+' && hnl_cos2d>0.993 && hnl_charge==0') #hist_mc_tot_A = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass<6.27 && hnl_charge==0') hist_mc_tot_A = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d>0.993 && sv_prob>0.05') #hist_mc_tot_B = self.getHistoMC(quantity=quantity, selection=self.selection+' && b_mass<6.27 && hnl_charge!=0') #hist_mc_tot_B = self.getHistoMC(quantity=quantity, selection=self.selection+' && hnl_cos2d>0.993 && hnl_charge!=0') #hist_mc_tot_B = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass<6.27 && hnl_charge!=0') hist_mc_tot_B = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d>0.993 && sv_prob<0.05') #hist_mc_tot_C = self.getHistoMC(quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge==0') #hist_mc_tot_C = self.getHistoMC(quantity=quantity, selection=self.selection+' && hnl_cos2d<0.993 && hnl_charge==0') #hist_mc_tot_C = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge==0') hist_mc_tot_C = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d<0.993 && sv_prob>0.05') #hist_mc_tot_D = self.getHistoMC(quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge!=0') #hist_mc_tot_D = self.getHistoMC(quantity=quantity, selection=self.selection+' && hnl_cos2d<0.993 && hnl_charge!=0') #hist_mc_tot_D = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge!=0') hist_mc_tot_D = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d<0.993 && sv_prob<0.05') n_obs_mc_A = hist_mc_tot_A.GetBinContent(1) n_obs_mc_B = hist_mc_tot_B.GetBinContent(1) n_obs_mc_C = hist_mc_tot_C.GetBinContent(1) n_obs_mc_D = hist_mc_tot_D.GetBinContent(1) n_err_mc_A = hist_mc_tot_A.GetBinError(1) #math.sqrt(n_obs_mc_A) n_err_mc_B = hist_mc_tot_B.GetBinError(1) #math.sqrt(n_obs_mc_B) n_err_mc_C = hist_mc_tot_C.GetBinError(1) #math.sqrt(n_obs_mc_C) n_err_mc_D = hist_mc_tot_D.GetBinError(1) #math.sqrt(n_obs_mc_D) ratio = n_obs_mc_A*n_obs_mc_D/(n_obs_mc_B*n_obs_mc_C) err = ratio * (n_err_mc_A/n_obs_mc_A + n_err_mc_B/n_obs_mc_B + n_err_mc_C/n_obs_mc_C + n_err_mc_D/n_obs_mc_D) return ratio, err
def computeBkgYieldsFromMC(self): ''' QCD MC (background) yields are computed as N_exp(SR) = N_obs(SR) * weight(CR) ''' #quantity = Quantity(name_flat='hnl_mass', nbins=1, bin_min=0, bin_max=1000) quantity = Quantity(name_flat='hnl_mass', nbins=1, bin_min=2.83268, bin_max=3.13932) selection_extra = self.selection # we compute the weight in the control region weight, err_weight = self.computeApproxWeightQCDMCtoData(quantity, selection=('hnl_charge!=0' if selection_extra=='' else 'hnl_charge!=0 &&' + selection_extra)) #weight, err_weight = self.computeWeightQCDMCtoData(lumi_data=774) #hist_mc_tot = self.getHistoMC(quantity=quantity, selection='hnl_charge==0' if selection_extra=='' else 'hnl_charge==0 &&' + selection_extra) hist_mc_tot = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection='hnl_charge==0' if selection_extra=='' else 'hnl_charge==0 &&' + selection_extra) n_obs_mc = hist_mc_tot.GetBinContent(1) n_err_mc = math.sqrt(n_obs_mc) #hist_mc_tot.GetBinError(1) n_exp_mc = n_obs_mc * weight err = n_exp_mc * (n_err_mc / n_obs_mc + err_weight / weight) return int(n_exp_mc), int(err), weight
def computeApproxWeightQCDMCtoData(self, quantity, selection): ''' weight = lumi_data / lumi_mc = N_data * sigma_mc / (N_mc * sigma_data) estimated as N_data / N_mc ''' f_data = ROOT.TFile.Open('root://t3dcachedb.psi.ch:1094/'+self.data_file.filename, 'READ') hist_data = PlottingTools.createHisto(self, f_data, 'signal_tree', quantity, branchname='flat', selection=selection) hist_data.Sumw2() n_obs_data = hist_data.GetBinContent(1) n_err_data = math.sqrt(n_obs_data) #hist_data.GetBinError(1) hist_mc_tot = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=selection) n_obs_mc = hist_mc_tot.GetBinContent(1) n_err_mc = math.sqrt(n_obs_mc) #hist_mc_tot.GetBinError(1) weight = n_obs_data / n_obs_mc if n_obs_data != 0 and n_obs_mc != 0: err = weight* (n_err_data / n_obs_data + n_err_mc / n_obs_mc) else: err = 0 return weight, err
def validateABCDOnQCDMC(self, plot_name='closure', label=''): ''' Get mupi mass ditribution from data using the ABCD method A = b_mass < 6.27 && hnl_charge == 0 (SR) B = b_mass < 6.27 && hnl_charge != 0 C = b_mass > 6.27 && hnl_charge == 0 D = b_mass > 6.27 && hnl_charge != 0 and compare it to the actual distribution in the SR ''' #quantity = Quantity(name_flat='hnl_mass', nbins=100, bin_min=0, bin_max=5.6) quantity = Quantity(name_flat='hnl_mass', nbins=50, bin_min=0, bin_max=5.37) bin_selection = self.selection #hist_mc_tot_A = self.getHistoMC(quantity=quantity, selection=self.selection+' && hnl_cos2d>0.993 && sv_prob>0.05') #hist_mc_tot_B = self.getHistoMC(quantity=quantity, selection=self.selection+' && b_mass<6.27 && hnl_charge!=0') #hist_mc_tot_C = self.getHistoMC(quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge==0') #hist_mc_tot_D = self.getHistoMC(quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge!=0') #hist_mc_tot_A = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass<6.27 && hnl_charge==0') #hist_mc_tot_B = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass<6.27 && hnl_charge!=0') #hist_mc_tot_C = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge==0') #hist_mc_tot_D = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && b_mass>6.27 && hnl_charge!=0') hist_mc_tot_A = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d>0.993 && sv_prob>0.05') hist_mc_tot_B = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d>0.993 && sv_prob<0.05') hist_mc_tot_C = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d<0.993 && sv_prob>0.05') hist_mc_tot_D = PlottingTools.createWeightedHistoQCDMC(self, self.qcd_files, self.white_list, quantity=quantity, selection=self.selection+' && hnl_cos2d<0.993 && sv_prob<0.05') #hist_estimate = ROOT.TH1D('hist_estimate', 'hist_estimate', 100, 0, 5.6) hist_estimate = ROOT.TH1D('hist_estimate', 'hist_estimate', 50, 0, 5.37) for ibin in range(1, hist_mc_tot_B.GetNbinsX()+1): n_obs_mc_B = hist_mc_tot_B.GetBinContent(ibin) n_obs_mc_C = hist_mc_tot_C.GetBinContent(ibin) n_obs_mc_D = hist_mc_tot_D.GetBinContent(ibin) n_err_mc_B = hist_mc_tot_B.GetBinError(ibin) n_err_mc_C = hist_mc_tot_C.GetBinError(ibin) n_err_mc_D = hist_mc_tot_D.GetBinError(ibin) content = n_obs_mc_B * (n_obs_mc_C / n_obs_mc_D) if n_obs_mc_C != 0 and n_obs_mc_D != 0 else 0 err = content * (n_err_mc_B / n_obs_mc_B + n_err_mc_C / n_obs_mc_C + n_err_mc_D / n_obs_mc_D) if n_obs_mc_B!=0 and n_obs_mc_C!=0 and n_obs_mc_D!=0 else 0 hist_estimate.SetBinContent(ibin, content) hist_estimate.SetBinError(ibin, err) ROOT.gStyle.SetOptStat(0) canv = PlottingTools.getTCanvas(self, 'canv', 800, 900) pad_up = ROOT.TPad("pad_up","pad_up",0,0.25,1,1) pad_up.SetBottomMargin(0.03) pad_up.Draw() canv.cd() pad_down = ROOT.TPad("pad_down","pad_down",0,0,1,0.25) pad_down.SetBottomMargin(0.25) pad_down.Draw() hist_estimate.SetLineWidth(3) hist_estimate.SetLineColor(ROOT.kOrange) #hist_estimate.SetTitle('Closure of the ABCD method in the Signal Region') hist_estimate.SetTitle('') #hist_estimate.GetXaxis().SetTitle('#mu#pi invariant mass [GeV]') hist_estimate.GetXaxis().SetLabelSize(0.0) hist_estimate.GetXaxis().SetTitleSize(0.0) #hist_estimate.GetXaxis().SetTitleOffset(1.1) hist_estimate.GetYaxis().SetTitle('Entries') hist_estimate.GetYaxis().SetLabelSize(0.037) hist_estimate.GetYaxis().SetTitleSize(0.042) hist_estimate.GetYaxis().SetTitleOffset(1.1) #hist_estimate.GetYaxis().SetRangeUser(1e-9, self.getMaxRangeY(hist_estimate, hist_mc_stack, do_log)) hist_mc_tot_A.SetLineWidth(3) hist_mc_tot_A.SetLineColor(ROOT.kBlue) int_estimate = hist_estimate.Integral() hist_estimate.Scale(1/int_estimate) int_A = hist_mc_tot_A.Integral() hist_mc_tot_A.Scale(1/int_A) pad_up.cd() hist_estimate.Draw('histo') hist_mc_tot_A.Draw('histo same') legend = PlottingTools.getRootTLegend(self, xmin=0.55, ymin=0.65, xmax=0.8, ymax=0.85, size=0.043) legend.AddEntry(hist_estimate, 'QCD ABCD estimate') legend.AddEntry(hist_mc_tot_A, 'QCD true') legend.Draw() label_box = ROOT.TPaveText(0.65,0.4,0.8,0.6, "brNDC") label_box.SetBorderSize(0) label_box.SetFillColor(ROOT.kWhite) label_box.SetTextSize(0.042) label_box.SetTextAlign(11) label_box.SetTextFont(42) label_box.AddText(label) label_box.Draw() pad_down.cd() hist_ratio = PlottingTools.getRatioHistogram(self, hist_estimate, hist_mc_tot_A) hist_ratio.Sumw2() for ibin in range(0, hist_ratio.GetNbinsX()+1): if hist_estimate.GetBinContent(ibin) != 0 and hist_mc_tot_A.GetBinContent(ibin) != 0: #err = hist_ratio.GetBinContent(ibin) * (math.sqrt(hist_data.GetBinContent(ibin))/hist_data.GetBinContent(ibin) + math.sqrt(hist_mc_tot.GetBinContent(ibin))/hist_mc_tot.GetBinContent(ibin)) #err = math.sqrt((math.sqrt(hist_data.GetBinContent(ibin))/hist_mc_tot.GetBinContent(ibin))**2 + (math.sqrt(hist_mc_tot.GetBinContent(ibin))*hist_data.GetBinContent(ibin)/(hist_mc_tot.GetBinContent(ibin))**2)**2) #err = math.sqrt((hist_data.GetBinError(ibin)/hist_mc_tot.GetBinContent(ibin))**2 + (hist_mc_tot.GetBinError(ibin)*hist_data.GetBinContent(ibin)/(hist_mc_tot.GetBinContent(ibin))**2)**2) err = hist_ratio.GetBinContent(ibin) * (hist_estimate.GetBinError(ibin) / hist_estimate.GetBinContent(ibin) + hist_mc_tot_A.GetBinError(ibin) / hist_mc_tot_A.GetBinContent(ibin)) else: err = 0 if hist_ratio.GetBinContent(ibin) != 0: hist_ratio.SetBinError(ibin, err) hist_ratio.SetLineWidth(2) hist_ratio.SetLineColor(ROOT.kBlack) hist_ratio.SetMarkerStyle(20) hist_ratio.SetTitle('') hist_ratio.GetXaxis().SetTitle('#mu#pi invariant mass [GeV]') hist_ratio.GetXaxis().SetLabelSize(0.1) hist_ratio.GetXaxis().SetTitleSize(0.15) hist_ratio.GetXaxis().SetTitleOffset(0.73) hist_ratio.GetYaxis().SetTitle('Estimate/True') hist_ratio.GetYaxis().SetLabelSize(0.1) hist_ratio.GetYaxis().SetTitleSize(0.13) hist_ratio.GetYaxis().SetTitleOffset(0.345) #val_min = hist_ratio.GetBinContent(hist_ratio.GetMinimumBin()) #val_max = hist_ratio.GetBinContent(hist_ratio.GetMaximumBin()) hist_ratio.GetYaxis().SetRangeUser(-1, 3) hist_ratio.Draw('PE') line = ROOT.TLine(0, 1, 5.6, 1) line.SetLineColor(4) line.SetLineWidth(2) line.Draw('same') canv.cd() if not path.exists('./myPlots/ABCDClosure'): os.system('mkdir -p ./myPlots/ABCDClosure') canv.SaveAs('./myPlots/ABCDClosure/{}.png'.format(plot_name)) canv.SaveAs('./myPlots/ABCDClosure/{}.pdf'.format(plot_name)) canv.SaveAs('./myPlots/ABCDClosure/{}.C'.format(plot_name))