def checkPU(): inFiles = glob(eos + '/ntuples/scalefactors/TnP_EGamma_trees_*/*.root') print '\n\tinDirs:', inFiles for inFile in inFiles: #[:1]: if '_v' in inFile: continue tFile = rt.TFile(inFile) print '\n\tinDir:', inFile tDir = tFile.Get('tnpEleIDs') t = tDir.Get('fitter_tree') print '\n\tentries:', t.GetEntries() h = rt.TH1F('n_vtx', 'n_vtx', 100, 0., 100) t.Draw('truePU>>n_vtx') mode = 'fast' if 'fast' in inFile else 'full' year = '17' if '17' in inFile else '18' c = rt.TCanvas('nvtx', 'nvtx') c.cd() h.SetTitle(';number of vertices') h.Draw() pf.showlumi('truePU_' + mode + '_' + year) pf.showlogo('CMS') c.Modified() c.Update() save(c, 'truePU', 'SUSY', mode, year, DIR=plotDir)
def computeSFs(mode, ID): f_in = {}; h_in = {} f_in['full'] = rt.TFile(plotDir + 'SUSY_%s_eff_pt_eta_%s.root' %(mode,ID)).Get('eff') f_in['fast'] = rt.TFile(plotDir + 'SUSY_%s_eff_pt_eta_%s_FS.root'%(mode,ID)).Get('eff') for tag in l_eta_tag: h_in['%s_full'%tag] = f_in['full'].GetPrimitive('eff_pt_eta_%s'%tag) h_in['%s_fast'%tag] = f_in['fast'].GetPrimitive('eff_pt_eta_%s'%tag) sf_pt_eta_00t08 = rt.TH1F('sf_pt_eta_00t08', 'sf_pt_eta_00t08', len(b_pt)-1, b_pt) sf_pt_eta_08t14 = rt.TH1F('sf_pt_eta_08t14', 'sf_pt_eta_08t14', len(b_pt)-1, b_pt) sf_pt_eta_14t16 = rt.TH1F('sf_pt_eta_14t16', 'sf_pt_eta_14t16', len(b_pt)-1, b_pt) sf_pt_eta_16t20 = rt.TH1F('sf_pt_eta_16t20', 'sf_pt_eta_16t20', len(b_pt)-1, b_pt) sf_pt_eta_20t25 = rt.TH1F('sf_pt_eta_20t25', 'sf_pt_eta_20t25', len(b_pt)-1, b_pt) sf_pt_eta_00t08.Divide(h_in['00t08_fast'], h_in['00t08_full']) sf_pt_eta_00t08.SetMarkerColor(rt.kGreen+2) sf_pt_eta_00t08.SetLineColor(rt.kGreen+2) sf_pt_eta_08t14.Divide(h_in['08t14_fast'], h_in['08t14_full']) sf_pt_eta_08t14.SetMarkerColor(rt.kBlue+2) sf_pt_eta_08t14.SetLineColor(rt.kBlue+2) sf_pt_eta_14t16.Divide(h_in['14t16_fast'], h_in['14t16_full']) sf_pt_eta_14t16.SetMarkerColor(rt.kRed+2) sf_pt_eta_14t16.SetLineColor(rt.kRed+2) sf_pt_eta_16t20.Divide(h_in['16t20_fast'], h_in['16t20_full']) sf_pt_eta_16t20.SetMarkerColor(rt.kYellow+2) sf_pt_eta_16t20.SetLineColor(rt.kYellow+2) sf_pt_eta_20t25.Divide(h_in['20t25_fast'], h_in['20t25_full']) sf_pt_eta_20t25.SetMarkerColor(rt.kCyan+2) sf_pt_eta_20t25.SetLineColor(rt.kCyan+2) c_sf = rt.TCanvas('sf','sf'); c_sf.cd() sf_framer.Draw() sf_pt_eta_00t08.Draw('same') sf_pt_eta_08t14.Draw('same') sf_pt_eta_14t16.Draw('same') sf_pt_eta_16t20.Draw('same') sf_pt_eta_20t25.Draw('same') leg = rt.TLegend(0.57, 0.18, 0.80, 0.4) leg.AddEntry(sf_pt_eta_00t08, '0.000 < |#eta| < 0.800') leg.AddEntry(sf_pt_eta_08t14, '0.800 < |#eta| < 1.444') leg.AddEntry(sf_pt_eta_14t16, '1.444 < |#eta| < 1.566') leg.AddEntry(sf_pt_eta_16t20, '1.566 < |#eta| < 2.000') leg.AddEntry(sf_pt_eta_20t25, '2.000 < |#eta| < 2.500') leg.Draw() pf.showlumi('SF_'+re.sub('Run2017_','',ID)) pf.showlogo('CMS') c_sf.SetLogx() c_sf.SetGridx(0) c_sf.Modified(); c_sf.Update() save(c_sf, 'pt_eta', 'SUSY', mode, ID)
def makeSFratios(): mode = 'ele' for ID in eleIDs.keys(): f_in_17 = rt.TFile( plotDir + 'final_ele_18_reDone/SUSY_%s_eff_pt_eta_%s_FastSim.root' % (mode, ID)).Get('eff') ID = ID.replace('2017', '2018') f_in_18 = rt.TFile( plotDir + 'final_ele_18_fastSim18/SUSY_%s_eff_pt_eta_%s_FastSim.root' % (mode, ID)).Get('eff') h_in_17 = f_in_17.GetPrimitive('eta_pass') h_in_18 = f_in_18.GetPrimitive('eta_pass') h_in_18.Divide(h_in_17) c_sf = rt.TCanvas('CHECK', 'CHECK') c_sf.cd() if mode == 'mu': h_in_18.SetTitle(';p_{T} [GeV]; |#eta|; FullSim/FastSim') c_sf.SetLogx() if mode == 'ele': h_in_18.SetTitle( ';super-cluster-#eta; p_{T} [GeV]; FullSim/FastSim') c_sf.SetLogy() h_in_18.SetAxisRange(0.8, 1.2, 'Z') h_in_18.Draw('colztextE') pf.showlumi('CHECK_Fast18/Fast17_' + re.sub('Run201._', '', ID)) # pf.showlogo('CMS') # c_sf.SetGridx(0) c_sf.Modified() c_sf.Update() save(c_sf, 'pt_eta', 'SUSY', mode, ID, DIR=plotDir)
def computeSFs2D(mode, ID, yr, DIR): if mode == 'ele': if yr == 18: ID = ID.replace('2017', '2018') f_in_fast = rt.TFile(plotDir + 'SUSY_%s_eff_pt_eta_%s_FastSim.root' % (mode, ID)).Get('eff') if yr == 18: ID = ID.replace('2017', '2018') f_in_full = rt.TFile(plotDir + 'SUSY_%s_eff_pt_eta_%s_FullSim.root' % (mode, ID)).Get('eff') h_in_full = f_in_full.GetPrimitive('eta_pass') h_in_fast = f_in_fast.GetPrimitive('eta_pass') h_in_full.Divide(h_in_fast) c_sf = rt.TCanvas('sf', 'sf') c_sf.cd() if mode == 'mu': h_in_full.SetTitle(';p_{T} [GeV]; |#eta|; FullSim/FastSim') c_sf.SetLogx() if mode == 'ele': h_in_full.SetTitle(';super-cluster-#eta; p_{T} [GeV]; FullSim/FastSim') c_sf.SetLogy() h_in_full.SetAxisRange(0.8, 1.2, 'Z') h_in_full.Draw('colztextE') pf.showlumi('SF_' + re.sub('Run201._', '', ID)) # pf.showlogo('CMS') # c_sf.SetGridx(0) c_sf.Modified() c_sf.Update() save(c_sf, 'pt_eta', 'SUSY', mode, ID, DIR=DIR)
def makeEffs2D(mode='mu', FAST=True, RDF=True, yr=17): fast = 'FullSim_' if FAST: fast = 'FastSim_' plotDir = makeFolder('EFF_%s_%s_%s' % (mode, fast, yr)) print '\n\tplotDir:', plotDir sys.stdout = Logger(plotDir + 'EFF_%s_%s_%s' % (mode, fast, yr)) if mode == 'mu': b_eta = b_eta_mu b_pt = b_pt_std if mode == 'ele': b_eta = b_eta_ele_low b_pt = b_pt_low cuts_all = None cuts_pass = None eta_cut = None print '\n\tFastSim:', FAST if mode == 'ele': if FAST == False: MODE = 'full' if FAST == True: MODE = 'fast' inFile = glob(eos + '/ntuples/scalefactors/TnP_EGamma_trees_%s_%s/*.root' % (yr, MODE))[0] print '\n\tinDir:', inFile tFile = rt.TFile(inFile) tDir = tFile.Get('tnpEleIDs') t = tDir.Get('fitter_tree') entries = t.GetEntries() print '\n\tentries:', entries IDs = eleIDs cuts_all = 'tag_Ele_pt > 30 && abs(tag_sc_eta) < 2.17 && mcTrue == 1 && abs(mass - 91.19) < 30 && el_q * tag_Ele_q < 0' cuts_all += ' && el_ecalEnergy * sin( 2 * atan( exp(el_sc_eta) ) ) > 0.5 && abs(el_sc_eta) < 2.5' # this is now used only for the multi-iso ID's # if RDF == False: # cuts_all += ' && tag_Ele_trigMVA > 0.92 && sqrt( 2*mpfMET*tag_Ele_pt*(1-cos(mpfPhi-tag_Ele_phi))) < 45' lep_pt = 'el_pt' #lep_eta = 'abs_el_sc_eta' lep_eta = 'el_sc_eta' if RDF == True: df = rdf(t) f_all = df #.Filter(cuts_all).Define('abs_el_sc_eta', 'abs(el_sc_eta)') if mode == 'mu': if FAST == False: MODE = 'full' if FAST == True: MODE = 'fast' inDir = eos + '/ntuples/scalefactors/TnP_Muon_trees_%s_%s/' % (yr, MODE) if yr == 18 and MODE == 'fast': inDir = eos + '/ntuples/scalefactors/TnP_Muon_trees_%s_%s_4Jun/' % ( yr, MODE) print '\n\tinDir:', inDir files = glob(inDir + '*.root') t = rt.TChain('Events') for f in files: t.Add(f) entries = t.GetEntries() print '\n\tentries:', entries IDs = muonIDs if FAST == False: cuts_all = 'Probe_isGenMatched == 1 && Probe_charge * Tag_charge < 0' if yr != 18 else 'Probe_charge * Tag_charge < 0' if FAST == True: cuts_all = 'Probe_charge * Tag_charge < 0' lep_pt = 'Probe_pt' lep_eta = 'abs_probe_eta' if RDF == True: df = rdf(t) f_all = df.Filter(cuts_all).Define('abs_probe_eta', 'abs(Probe_eta)') if RDF == True: n_bef = df.Count().GetValue() n_aft = f_all.Count().GetValue() if RDF == False: n_bef = t.GetEntries() n_aft = t.GetEntries(cuts_all) print '\n\tentries before selection: %d' % n_bef print '\n\tentries after pre-selection: %d' % n_aft print '\n\tcuts_all: %s\n' % cuts_all repl = 'el_noIsoMVA94X' if yr == 18 else 'el_MVA94Xnoiso' for ID in IDs.keys(): IDs[ID] = IDs[ID].replace('el_MVA94Xnoiso', repl) cutBase_mvaVLooseTightIP2D = CutBase_mvaVLooseTightIP2D.replace( 'el_MVA94Xnoiso', repl) cutBase_mvaTightIDEmuTightIP2DTightIP3D = CutBase_mvaTightIDEmuTightIP2DTightIP3D.replace( 'el_MVA94Xnoiso', repl) cutBase_mvaTightIDEmuTightIP2DTightIP3DConvVetoMissHits = CutBase_mvaTightIDEmuTightIP2DTightIP3DConvVetoMissHits.replace( 'el_MVA94Xnoiso', repl) mva_Tight = mvaTight.replace('el_MVA94Xnoiso', repl) for ID in IDs.keys(): #[:1]: filtr_all = '1 == 1' #set_trace() if ID in eleIDs: ## special IDs ### wrt MVA VLoose ID + TightIP2D, 'passingMVAVLoose == 1 && passingTightIP2D == 1' if ID in eleIDs_mvaVLooseTightIP2D: filtr_all = IDs[ 'Run2017_MVAVLooseIP2D'] + ' && ' + cutBase_mvaVLooseTightIP2D if RDF == True: df_all = f_all.Filter(filtr_all) ### wrt MVA Tight ID + ID Emu + TightIP2D + TightIP3D, 'passingMVATight == 1 && passingIDEmu == 1 && passingTightIP2D == 1 && passingTightIP3D == 1' if ID in eleIDs_mvaTightIDEmuTightIP2DTightIP3D: # df_all = f_all.Filter('passingMVATight == 1 && passingIDEmu == 1 && passingTightIP2D == 1 && passingTightIP3D == 1') # 3/6/19 gio: mvaTight is new filtr_all = mva_Tight + ' && passingIDEmu == 1 && passingTightIP2D == 1 && passingTightIP3D == 1 && ' + cutBase_mvaTightIDEmuTightIP2DTightIP3D if RDF == True: df_all = f_all.Filter(filtr_all) # wrt MVA Tight ID + ID Emu + TightIP2D + TightIP3D + ConvVeto + MissHits = 0, 'passingMVATight == 1 && passingIDEmu == 1'\ # ' && passingTightIP2D == 1 && passingTightIP3D == 1 && passingConvVeto == 1 && el_mHits == 0' if ID in eleIDs_mvaTightIDEmuTightIP2DTightIP3DConvVetoMissHits: # df_all = f_all.Filter('passingMVATight == 1 && passingIDEmu == 1 && passingTightIP2D == 1 && passingTightIP3D == 1 && passingConvVeto == 1 && el_mHits == 0') # 3/6/19 gio: mvaTight is new filtr_all = mva_Tight + ' && passingIDEmu == 1 && passingTightIP2D == 1 && passingTightIP3D == 1 && passingConvVeto == 1 && el_mHits == 0' \ ' && ' + cutBase_mvaTightIDEmuTightIP2DTightIP3DConvVetoMissHits if RDF == True: df_all = f_all.Filter(filtr_all) if RDF == True: if ID not in eleIDs_mvaTightIDEmuTightIP2DTightIP3DConvVetoMissHits \ and ID not in eleIDs_mvaTightIDEmuTightIP2DTightIP3DConvVetoMissHits \ and ID not in eleIDs_mvaVLooseTightIP2D: df_all = f_all if ID in muonIDs: ## special IDs ### wrt MVA VLoose ID + TightIP2D, 'passingMVAVLoose == 1 && passingTightIP2D == 1' if ID in muonIDs_Loose: filtr_all = 'Probe_passL == 1' if RDF == True: df_all = f_all.Filter(filtr_all) ### wrt MVA Tight ID + ID Emu + TightIP2D + TightIP3D, 'passingMVATight == 1 && passingIDEmu == 1 && passingTightIP2D == 1 && passingTightIP3D == 1' if ID in muonIDs_Medium: filtr_all = 'Probe_passM == 1' if RDF == True: df_all = f_all.Filter(filtr_all) ### wrt MVA Tight ID + ID Emu + TightIP2D + TightIP3D + ConvVeto + MissHits = 0, 'passingMVATight == 1 && passingIDEmu == 1 && passingTightIP2D == 1'\ ### '&& passingTightIP3D == 1 && passingConvVeto == 1 && el_mHits == 0' if ID in muonIDs_MediumPrompt: filtr_all = 'Probe_passMP == 1' if RDF == True: df_all = f_all.Filter(filtr_all) if RDF == True: if ID not in muonIDs_Loose and ID not in muonIDs_Medium and ID not in muonIDs_MediumPrompt: df_all = f_all if RDF == True: df_pass = df_all.Filter(IDs[ID]) if mode == 'mu': h_all_ptr = df_all.Histo2D( ('eta_all', 'eta_all', len(b_pt) - 1, b_pt, len(b_eta) - 1, b_eta), lep_pt, lep_eta) h_pass_ptr = df_pass.Histo2D( ('eta_pass', 'eta_pass', len(b_pt) - 1, b_pt, len(b_eta) - 1, b_eta), lep_pt, lep_eta) if mode == 'ele': h_all_ptr = df_all.Histo2D( ('eta_all', 'eta_all', len(b_eta) - 1, b_eta, len(b_pt) - 1, b_pt), lep_eta, lep_pt) h_pass_ptr = df_pass.Histo2D( ('eta_pass', 'eta_pass', len(b_eta) - 1, b_eta, len(b_pt) - 1, b_pt), lep_eta, lep_pt) h_all = h_all_ptr.GetPtr() h_pass = h_pass_ptr.GetPtr() if RDF == False: filtr_pass = filtr_all + ' && ' + IDs[ID] if mode == 'mu': h_all = rt.TH2F('eta_all', 'eta_all', len(b_pt) - 1, b_pt, len(b_eta) - 1, b_eta) h_pass = rt.TH2F('eta_pass', 'eta_pass', len(b_pt) - 1, b_pt, len(b_eta) - 1, b_eta) if mode == 'ele': h_all = rt.TH2F('eta_all', 'eta_all', len(b_eta) - 1, b_eta, len(b_pt) - 1, b_pt) h_pass = rt.TH2F('eta_pass', 'eta_pass', len(b_eta) - 1, b_eta, len(b_pt) - 1, b_pt) CUTS_ALL = cuts_all + ' && ' + filtr_all CUTS_PASS = cuts_all + ' && ' + filtr_pass t.Draw('el_pt:el_sc_eta>>eta_all', CUTS_ALL) t.Draw('el_pt:el_sc_eta>>eta_pass', CUTS_PASS) if yr == 18: ID = ID.replace('2017', '2018') print '\n\tmode: %s, ID: %s, all entries: %i, passing: %i, avg eff: %.2f' % ( mode, ID, h_all.GetEntries(), h_pass.GetEntries(), h_pass.GetEntries() / h_all.GetEntries()) print '\n\tfiltr_all: %s' % filtr_all print '\n\tfiltr_pass: %s\n' % ( filtr_all + ' && ' + IDs[re.sub('Run201._', 'Run2017_', ID)]) h_pass.Divide(h_all) c_eff = rt.TCanvas('eff', 'eff') c_eff.cd() if mode == 'mu': c_eff.SetLogx() h_pass.SetTitle(';Lepton p_{T} [GeV]; |#eta|; Efficiency') if mode == 'ele': c_eff.SetLogy() h_pass.SetTitle( ';super-cluster-#eta; Lepton p_{T} [GeV]; Efficiency') h_pass.Draw('colztextE') h_pass.SetAxisRange(0., 1., 'Z') if mode == 'mu': h_pass.GetXaxis().SetNoExponent() h_pass.GetXaxis().SetMoreLogLabels() if mode == 'ele': h_pass.GetYaxis().SetNoExponent() h_pass.GetYaxis().SetMoreLogLabels() # pf.showlogo('CMS') pf.showlumi( re.sub('Run201._', '', ID + '_FastSim' if FAST else ID + '_FullSim')) # c_eff.SetGridx(0) c_eff.Modified() c_eff.Update() save(c_eff, 'pt_eta', 'SUSY', mode, ID + '_FastSim' if FAST else ID + '_FullSim', DIR=plotDir) sys.stderr = sys.__stderr__ sys.stdout = sys.__stdout__
h_eff_disp_Dxy_enum.SetLineColor(rt.kGreen + 2) h_eff_disp_Dxy_enum.SetMarkerColor(rt.kGreen + 2) h_eff_disp_Chi2_sMu_enum.Draw() h_eff_disp_Chi2_enum.Draw('same') h_eff_disp_Dxy_sMu_enum.Draw('same') h_eff_disp_Dxy_enum.Draw('same') efleg = rt.TLegend(.4, .75, .8, .88) efleg.AddEntry(h_eff_disp_Chi2_sMu_enum, 'Chi2, sMu', 'EP') efleg.AddEntry(h_eff_disp_Chi2_enum, 'Chi2, sMu && dSAMu', 'EP') efleg.AddEntry(h_eff_disp_Dxy_sMu_enum, 'Dxy, sMu', 'EP') efleg.AddEntry(h_eff_disp_Dxy_enum, 'Dxy, sMu && dSAMu', 'EP') efleg.Draw('apez same') pf.showlumi('%d entries' % (h_eff_disp_Dxy_enum.GetEntries())) pf.showlogopreliminary('CMS', 'Simulation Preliminary') c_eff_disp.Modified() c_eff_disp.Update() #c_eff_disp.SaveAs(output_dir + 'c_eff_disp.pdf') #c_eff_disp.SaveAs(output_dir + 'c_eff_disp.root') ######################################### # Reconstruction Purity ######################################### print('Filling purity vs displacement histos') c_pur_disp = rt.TCanvas('c_pur_disp', 'c_pur_disp')
# tt.Draw('(sv_reco_z-sv_z)/(sv_z):hnl_2d_disp >> h_SV_z_res','abs(l1_eta)<2.4 & abs(l2_eta)<2.4 & l1_pt>5 & l2_pt>5 & sv_reco_x != -99 & sv_reco_y != -99 & sv_reco_z != -99') # h_SV_z_res.SetTitle(';HNL 2D displacement [cm]; Vtx Resolution #left[#frac{RecoSV_z - GenSV_z}{GenSV_z}#right] ') # h_SV_z_res.Draw('colz') # pf.showlogopreliminary('CMS','Simulation Preliminary') # pf.showlumi('%d entries'%(h_SV_z_res.GetEntries())) # c_SV_z_res.SetLogz() #2d_displ c_corrx = rt.TCanvas('c_corrx', 'c_corrx') h_corrx = rt.TH2F('h_corrx','',50,0.,200.,50,0.,200.) tt.Draw('hnl_2d_disp:dimuonMaxCosBPA_disp2DFromBS >> h_corrx','l1_charge!=l2_charge & abs(l1_eta)<2.4 & abs(l2_eta)<2.4 & l1_pt>5 & l2_pt>5& sv_reco_x != -99 & sv_reco_y != -99 & sv_reco_z != -99') tt.Draw('dimuonMaxCosBPA_disp2DFromBS:hnl_2d_disp >> h_corrx','l1_charge!=l2_charge & abs(l1_eta)<2.4 & abs(l2_eta)<2.4 & l1_pt>5 & l2_pt>5& sv_reco_x != -99 & sv_reco_y != -99 & sv_reco_z != -99') h_corrx.SetTitle(';hnl_2d_disp [cm];dimuonMaxCosBPA_disp2DFromBS [cm] ') h_corrx.Draw('colz') pf.showlogopreliminary('CMS','Simulation Preliminary') pf.showlumi('%d entries'%(h_corrx.GetEntries())) c_corrx.SetLogz() # #corry # c_corry = rt.TCanvas('c_corry', 'c_corry') # h_corry = rt.TH2F('h_corry','',50,-200.,200.,50,-200.,200.) # tt.Draw('sv_reco_y:sv_y >> h_corry','l1_charge!=l2_charge & abs(l1_eta)<2.4 & abs(l2_eta)<2.4 & l1_pt>5 & l2_pt>5& sv_reco_x != -99 & sv_reco_y != -99 & sv_reco_z != -99') # h_corry.SetTitle(';GenSV_y [cm];RecoSV_y [cm] ') # h_corry.Draw('colz') # pf.showlogopreliminary('CMS','Simulation Preliminary') # pf.showlumi('%d entries'%(h_corry.GetEntries())) # c_corry.SetLogz() # #corrz # c_corrz = rt.TCanvas('c_corrz', 'c_corrz') # h_corrz = rt.TH2F('h_corrz','',50,-350.,350.,50,-350.,350.)
h_smu_l1_cf.Divide(h_smu_sum) h_smu_l1_nf.Divide(h_smu_sum) h_dsmu_l1_cf.Divide(h_dsmu_sum) h_dsmu_l1_nf.Divide(h_dsmu_sum) hs = ROOT.THStack('h', '') hs.Add(h_smu_l1_cf) hs.Add(h_dsmu_l1_cf) hs.Draw('nostack') for hh in [hs]: hh.SetTitle(';pt [GeV];Chargeflip ratio') leg1 = ROOT.TLegend(.2, .65, .5, .85) leg1.SetBorderSize(0) leg1.SetFillColor(ROOT.kWhite) leg1.SetFillStyle(1001) leg1.SetTextFont(42) leg1.SetTextSize(0.03) leg1.AddEntry(h_smu_l1_cf, 'slimmedMuon', 'EP') leg1.AddEntry(h_dsmu_l1_cf, 'displacedStandAloneMuon', 'EP') leg1.Draw('apez same') pf.showlumi(' l1+l2 / eta<2.4 / alldispl / %.2f M entries' % (nentries / 1000000.)) for cc in [c]: cc.Update()
dnfl0.Add(dnfl2) dcfl0.Add(dcfl1) dcfl0.Add(dcfl2) dsum.Add(dcfl0) dsum.Add(dnfl0) dcf.Divide(dcfl0,dsum) dnf.Divide(dnfl0,dsum) dcf.SetFillColor(2) dnf.SetFillColor(4) dstack.Add(dnf) dstack.Add(dcf) dstack.Draw("hist") dstack.GetXaxis().SetTitle('p_{T}[GeV]') dstack.GetYaxis().SetTitle('Entries (normalized)') ROOT.gPad.BuildLegend(0.6,0.21,0.85,0.36,"") pf.showlumi(' l1+l2 / eta<2.4 / alldispl / %.2f M entries'%(ntries / 1000000.)) #print('no error until d1') t1.cd() d1nfl0.Add(d1nfl1) d1nfl0.Add(d1nfl2) d1cfl0.Add(d1cfl1) d1cfl0.Add(d1cfl2) d1sum.Add(d1cfl0) d1sum.Add(d1nfl0) d1cf.Divide(d1cfl0,d1sum) d1nf.Divide(d1nfl0,d1sum) d1cf.SetFillColor(2) d1nf.SetFillColor(4) d1stack.Add(d1nf)
def draw_limits(input_file, output_dir, ch='mmm', twoD=False, verbose=False): ''' ############################################################################# ## producing coupling vs r limits for each signal mass and a given channel ## ## also has the option 2D, in order to draw mass vs coupling limits (this ## ## uses the intersections of the 1D limits and the r=1 line) ## ############################################################################# ''' # create signal and limits dictionary limits, masses = get_lim_dict(input_file, output_dir, ch=ch) signals = get_signals() b = np.arange(0., 11, 1) req1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ixs = OrderedDict() #sort the dictionary after v values limits = OrderedDict(sorted(limits.items(), key=lambda t: t[1]['V'])) ExclusionLimits2D_low = {} ExclusionLimits2D_high = {} for ExclusionLimits2D in [ExclusionLimits2D_low, ExclusionLimits2D_high]: ExclusionLimits2D['masses'] = [] ExclusionLimits2D['x_err'] = [] ExclusionLimits2D['y_exp'] = [] ExclusionLimits2D['y_ep1s'] = [] ExclusionLimits2D['y_ep2s'] = [] ExclusionLimits2D['y_em1s'] = [] ExclusionLimits2D['y_em2s'] = [] for mass in masses: # for m in [1,2,3,4,5,6,7,8,9,10]: # print(colored('doing MASS = %d GeV'%mass,'green')) print('### doing MASS = %d GeV' % mass) y_exp = [] y_ep1s = [] y_ep2s = [] y_em2s = [] y_em1s = [] v2s = [lim for lim in limits if 'M%d_' % mass in lim] b_V2 = [] for v2 in v2s: # if limits[v2].has_key('exp'): if 'exp' in limits[v2]: try: y_exp.append(limits[v2]['exp']) y_ep1s.append(limits[v2]['ep1s']) y_ep2s.append(limits[v2]['ep2s']) y_em1s.append(limits[v2]['em1s']) y_em2s.append(limits[v2]['em2s']) b_V2.append(limits[v2]['V2']) # if m == 5: set_trace() except: set_trace() if len(b_V2) == 0: print( 'combine failed processing this channel, continue doing to next one' ) continue x_err = np.zeros(len(b_V2)) b_V2.sort(reverse=False) for i in range(len(y_exp)): y_ep1s[i] = abs(y_ep1s[i] - y_exp[i]) y_ep2s[i] = abs(y_ep2s[i] - y_exp[i]) y_em1s[i] = abs(y_em1s[i] - y_exp[i]) y_em2s[i] = abs(y_em2s[i] - y_exp[i]) # if Mass == '5' and V == '0.00145602197786': set_trace() exp = rt.TGraph(len(b_V2), np.array(b_V2), np.array(y_exp)) gr1 = rt.TGraphAsymmErrors(len(b_V2), np.array(b_V2), np.array(y_exp), np.array(x_err), np.array(x_err), np.array(y_em1s), np.array(y_ep1s)) gr2 = rt.TGraphAsymmErrors(len(b_V2), np.array(b_V2), np.array(y_exp), np.array(x_err), np.array(x_err), np.array(y_em2s), np.array(y_ep2s)) rt.gStyle.SetOptStat(0000) # B_V2 = np.logspace(-11, -1, 10, base=10) # B_Y = np.logspace(-4, 4, 10, base=10) B_V2 = np.logspace(-11, 1, 120, base=10) B_Y = np.logspace(-4, 9, 150, base=10) r1g = rt.TGraph(len(B_V2), np.array(B_V2), np.ones(len(B_V2))) r1g.SetLineColor(rt.kRed + 1) r1g.SetLineWidth(1) framer = rt.TH2F('framer', 'framer', len(B_V2) - 1, B_V2, len(B_Y) - 1, B_Y) # framer.GetYaxis().SetRangeUser(0.0001,10000) # framer.GetXaxis().SetRangeUser(0.00000000001, 0.1) framer.GetYaxis().SetRangeUser(0.0001, 1000000000) framer.GetXaxis().SetRangeUser(0.00000000001, 10.) if ch == 'mmm': framer.SetTitle('m_{N} = %d GeV, #mu#mu#mu; |V_{#mu N}|^{2}; r' % mass) if ch == 'eee': framer.SetTitle('m_{N} = %d GeV, eee; |V_{e N}|^{2}; r' % mass) if ch == 'mem_OS': framer.SetTitle('m_{N} = %d GeV, #mu#mue OS; |V_{#mu N}|^{2}; r' % mass) if ch == 'mem_SS': framer.SetTitle('m_{N} = %d GeV, #mu#mue SS; |V_{#mu N}|^{2}; r' % mass) if ch == 'eem_OS': framer.SetTitle('m_{N} = %d GeV, ee#mu OS; |V_{e N}|^{2}; r' % mass) if ch == 'eem_SS': framer.SetTitle('m_{N} = %d GeV, ee#mu SS; |V_{e N}|^{2}; r' % mass) exp.SetMarkerStyle(22) exp.SetMarkerSize(1) exp.SetMarkerColor(rt.kRed + 2) exp.SetLineColor(rt.kRed + 2) gr1.SetFillColor(rt.kGreen + 2) gr1.SetLineColor(rt.kGreen + 2) gr1.SetLineWidth(2) gr2.SetFillColor(rt.kOrange) gr2.SetLineColor(rt.kOrange) gr2.SetLineWidth(2) can = rt.TCanvas('limits', 'limits') can.cd() can.SetLogy() can.SetLogx() # can.SetBottomMargin(0.15) framer.Draw() # can.Update() gr2.Draw('same, E1') gr2.Draw('same, E3') # can.Update() gr1.Draw('same, E1') gr1.Draw('same, E3') # can.Update() exp.Draw('same, LP') # can.Update() r1g.Draw('same') # can.Update() leg = rt.TLegend(.4, .75, .8, .88) leg.AddEntry(exp, 'Expected', 'LP') leg.AddEntry(gr1, 'Expected #pm 1 #sigma', 'E3') leg.AddEntry(gr2, 'Expected #pm 2 #sigma', 'E3') leg.Draw('apez same') # plotfactory.showlogopreliminary() plotfactory.showlumi('N#rightarrow%s; mass = %d GeV' % (ch, mass)) can.Update() #calculate and plot the intersection # if mass != 5: continue intersection_exp_x, intersection_exp_y = intersection( np.array(b_V2), np.array(y_exp), np.array(b_V2), np.ones(len(b_V2))) intersection_ep1_x, intersection_ep1_y = intersection( np.array(b_V2), np.array(y_exp) + np.array(y_ep1s), np.array(b_V2), np.ones(len(b_V2))) intersection_ep2_x, intersection_ep2_y = intersection( np.array(b_V2), np.array(y_exp) + np.array(y_ep2s), np.array(b_V2), np.ones(len(b_V2))) intersection_em1_x, intersection_em1_y = intersection( np.array(b_V2), np.array(y_exp) - np.array(y_em1s), np.array(b_V2), np.ones(len(b_V2))) intersection_em2_x, intersection_em2_y = intersection( np.array(b_V2), np.array(y_exp) - np.array(y_em2s), np.array(b_V2), np.ones(len(b_V2))) containsEmptyArrays = False for intersections in [ intersection_exp_x, intersection_ep1_x, intersection_ep2_x, intersection_em1_x, intersection_em2_x ]: if len(intersections) == 0: containsEmptyArrays = True continue if containsEmptyArrays: continue if mass == 5: continue intersection_exp = rt.TGraph(len(intersection_exp_x), np.array(intersection_exp_x), np.array(intersection_exp_y)) intersection_ep1 = rt.TGraph(len(intersection_ep1_x), np.array(intersection_ep1_x), np.array(intersection_ep1_y)) intersection_ep2 = rt.TGraph(len(intersection_ep2_x), np.array(intersection_ep2_x), np.array(intersection_ep2_y)) intersection_em1 = rt.TGraph(len(intersection_em1_x), np.array(intersection_em1_x), np.array(intersection_em1_y)) intersection_em2 = rt.TGraph(len(intersection_em2_x), np.array(intersection_em2_x), np.array(intersection_em2_y)) intersection_exp.SetMarkerColor(rt.kRed + 2) intersection_ep1.SetMarkerColor(rt.kGreen + 2) intersection_ep2.SetMarkerColor(rt.kOrange) intersection_em1.SetMarkerColor(rt.kGreen + 2) intersection_em2.SetMarkerColor(rt.kOrange) for intersectionPoints in [ intersection_exp, intersection_ep1, intersection_ep2, intersection_em1, intersection_em2 ]: intersectionPoints.SetMarkerStyle(29) intersectionPoints.SetMarkerSize(2) intersectionPoints.Draw('same, P') minNumberOfIntersections = 10 for intersections in [ intersection_exp_x, intersection_ep1_x, intersection_ep2_x, intersection_em1_x, intersection_em2_x ]: if len(intersections) < minNumberOfIntersections: minNumberOfIntersections = len(intersections) for i in range(2): try: if i == 0: index = 0 ExclusionLimits2D_low['masses'].append(float(mass)) ExclusionLimits2D_low['x_err'].append(0.) ExclusionLimits2D_low['y_exp'].append( float(min(intersection_exp_x))) ExclusionLimits2D_low['y_ep1s'].append( abs( float(min(intersection_ep1_x)) - float(min(intersection_exp_x)))) ExclusionLimits2D_low['y_ep2s'].append( abs( float(min(intersection_ep2_x)) - float(min(intersection_exp_x)))) ExclusionLimits2D_low['y_em1s'].append( abs( float(min(intersection_exp_x)) - float(min(intersection_em1_x)))) ExclusionLimits2D_low['y_em2s'].append( abs( float(min(intersection_exp_x)) - float(min(intersection_em2_x)))) if (i == 1) and (minNumberOfIntersections > 1): index = -1 ExclusionLimits2D_high['masses'].append(float(mass)) ExclusionLimits2D_high['x_err'].append(0.) ExclusionLimits2D_high['y_exp'].append( float(max(intersection_exp_x))) ExclusionLimits2D_high['y_ep1s'].append( abs( float(max(intersection_em1_x)) - float(max(intersection_exp_x)))) ExclusionLimits2D_high['y_ep2s'].append( abs( float(max(intersection_em2_x)) - float(max(intersection_exp_x)))) ExclusionLimits2D_high['y_em1s'].append( abs( float(max(intersection_exp_x)) - float(max(intersection_ep1_x)))) ExclusionLimits2D_high['y_em2s'].append( abs( float(max(intersection_exp_x)) - float(max(intersection_ep2_x)))) except: print('exception!!!!!!') set_trace() # if mass == 8: set_trace() if not os.path.isdir(output_dir + 'pdf'): os.mkdir(output_dir + 'pdf') if not os.path.isdir(output_dir + 'png'): os.mkdir(output_dir + 'png') if not os.path.isdir(output_dir + 'root'): os.mkdir(output_dir + 'root') can.SaveAs(output_dir + 'pdf/M%d_%s_root.pdf' % (mass, ch)) can.SaveAs(output_dir + 'png/M%d_%s_root.png' % (mass, ch)) can.SaveAs(output_dir + 'root/M%d_%s_root.root' % (mass, ch)) return ExclusionLimits2D_low, ExclusionLimits2D_high
def draw_limits2DWithPrompt(ExclusionLimits2D_low, ExclusionLimits2D_high, input_file, output_dir, ch='mmm', twoD=False, verbose=False): ''' ############################################################################# ## producing coupling vs r limits for each signal mass and a given channel ## ## also has the option 2D, in order to draw mass vs coupling limits (this ## ## uses the intersections of the 1D limits and the r=1 line) ## ############################################################################# ''' # promptMuonDirectory = '/Users/dehuazhu/SynologyDrive/PhD/7_Writing/1912_AnalysisNote_HNL/AN-19-272/1_Sections/6_Interpretation/figures/Limits/limitsMuonMixing.root' promptMuonDirectory = '/work/dezhu/3_figures/2_Limits/PromptAnalysis/limitsMuonMixing.root' promptMuonFile = rt.TFile(promptMuonDirectory) promptMuonGraph_ep2 = promptMuonFile.Get('expected_2sigmaUp') promptMuonGraph_ep1 = promptMuonFile.Get('expected_1sigmaUp') promptMuonGraph_exp = promptMuonFile.Get('expected_central') promptMuonGraph_em1 = promptMuonFile.Get('expected_1sigmaDown') promptMuonGraph_em2 = promptMuonFile.Get('expected_2sigmaDown') # promptElectronDirectory = '/Users/dehuazhu/SynologyDrive/PhD/7_Writing/1912_AnalysisNote_HNL/AN-19-272/1_Sections/6_Interpretation/figures/Limits/limitsElectronMixing.root' promptElectronDirectory = '/work/dezhu/3_figures/2_Limits/PromptAnalysis/limitsElectronMixing.root' promptElectronFile = rt.TFile(promptElectronDirectory) promptElectronGraph_ep2 = promptMuonFile.Get('expected_2sigmaUp') promptElectronGraph_ep1 = promptMuonFile.Get('expected_1sigmaUp') promptElectronGraph_exp = promptMuonFile.Get('expected_central') promptElectronGraph_em1 = promptMuonFile.Get('expected_1sigmaDown') promptElectronGraph_em2 = promptMuonFile.Get('expected_2sigmaDown') low_masses = ExclusionLimits2D_low['masses'] low_x_err = ExclusionLimits2D_low['x_err'] low_y_exp = ExclusionLimits2D_low['y_exp'] low_y_ep1s = ExclusionLimits2D_low['y_ep1s'] low_y_ep2s = ExclusionLimits2D_low['y_ep2s'] low_y_em1s = ExclusionLimits2D_low['y_em1s'] low_y_em2s = ExclusionLimits2D_low['y_em2s'] high_masses = ExclusionLimits2D_high['masses'] high_x_err = ExclusionLimits2D_high['x_err'] high_y_exp = ExclusionLimits2D_high['y_exp'] high_y_ep1s = ExclusionLimits2D_high['y_ep1s'] high_y_ep2s = ExclusionLimits2D_high['y_ep2s'] high_y_em1s = ExclusionLimits2D_high['y_em1s'] high_y_em2s = ExclusionLimits2D_high['y_em2s'] low_exp = rt.TGraph (len(low_masses), np.array(low_masses), np.array(low_y_exp)) low_gr1 = rt.TGraphAsymmErrors(len(low_masses), np.array(low_masses), np.array(low_y_exp), np.array(low_x_err), np.array(low_x_err), np.array(low_y_em1s), np.array(low_y_ep1s)) low_gr2 = rt.TGraphAsymmErrors(len(low_masses), np.array(low_masses), np.array(low_y_exp), np.array(low_x_err), np.array(low_x_err), np.array(low_y_em2s), np.array(low_y_ep2s)) high_exp = rt.TGraph (len(high_masses), np.array(high_masses), np.array(high_y_exp)) high_gr1 = rt.TGraphAsymmErrors(len(high_masses), np.array(high_masses), np.array(high_y_exp), np.array(high_x_err), np.array(high_x_err), np.array(high_y_em1s), np.array(high_y_ep1s)) high_gr2 = rt.TGraphAsymmErrors(len(high_masses), np.array(high_masses), np.array(high_y_exp), np.array(high_x_err), np.array(high_x_err), np.array(high_y_em2s), np.array(high_y_ep2s)) rt.gStyle.SetOptStat(0000) # B_X = np.logspace(0,2.5,10,base=10) # B_X = np.logspace(0.,1.,10) B_X = np.array([1.,2.,3.,4.,5.,6.,7.,8.,9.,10.]) # B_Y = np.logspace(-6.7, -1, 10, base=10) B_Y = np.logspace(-5.9, -1, 10, base=10) framer = rt.TH2F('framer', 'framer', len(B_X)-1, B_X, len(B_Y)-1, B_Y) # framer.GetXaxis().SetRangeUser(1, 10.) framer.GetXaxis().SetRangeUser(1, 8.) framer.GetYaxis().SetRangeUser(1e-12,1e-1) if ch == 'mmm': framer.SetTitle('#mu#mu#mu; m_{N}(GeV); |V_{N#mu N}|^{2}' ) if ch == 'eee': framer.SetTitle('eee; m_{N}(GeV); |V_{e N}|^{2}' ) if ch == 'mem_OS': framer.SetTitle('#mu#mue OS; m_{N}(GeV); |V_{#mu N}|^{2}' ) if ch == 'mem_SS': framer.SetTitle('#mu#mue SS; m_{N}(GeV); |V_{#mu N}|^{2}' ) if ch == 'eem_OS': framer.SetTitle('ee#mu OS; m_{N}(GeV); |V_{e N}|^{2}' ) if ch == 'eem_SS': framer.SetTitle('ee#mu SS; m_{N}(GeV); |V_{e N}|^{2}' ) low_exp.SetMarkerStyle(22) low_exp.SetMarkerSize(1) low_exp.SetMarkerColor(rt.kBlack) low_exp.SetLineColor(rt.kBlack) low_exp.SetLineWidth(2) low_gr1.SetFillColor(rt.kGreen+1) low_gr1.SetLineColor(rt.kGreen+1) low_gr1.SetLineWidth(2) low_gr2.SetFillColor(rt.kOrange) low_gr2.SetLineColor(rt.kOrange) low_gr2.SetLineWidth(2) high_exp.SetMarkerStyle(22) high_exp.SetMarkerSize(1) high_exp.SetMarkerColor(rt.kBlack) high_exp.SetLineColor(rt.kBlack) high_exp.SetLineWidth(2) high_gr1.SetFillColor(rt.kGreen+1) high_gr1.SetLineColor(rt.kGreen+1) high_gr1.SetLineWidth(2) high_gr2.SetFillColor(rt.kOrange) high_gr2.SetLineColor(rt.kOrange) high_gr2.SetLineWidth(2) for graph in [promptMuonGraph_ep2, promptElectronGraph_ep2, promptMuonGraph_em2, promptElectronGraph_em2]: graph.SetFillColor(rt.kAzure+6) graph.SetLineColor(rt.kAzure+6) graph.SetLineStyle(2) graph.SetLineWidth(2) for graph in [promptMuonGraph_ep1, promptElectronGraph_ep1, promptMuonGraph_em1, promptElectronGraph_em1]: graph.SetFillColor(rt.kAzure+1) graph.SetLineColor(rt.kAzure+1) graph.SetLineStyle(7) graph.SetLineWidth(2) for graph in [promptMuonGraph_exp, promptElectronGraph_exp]: graph.SetMarkerStyle(22) graph.SetMarkerSize(1) graph.SetMarkerColor(rt.kAzure+2) graph.SetLineColor( rt.kAzure+2) graph.SetLineStyle(1) graph.SetLineWidth(2) can = rt.TCanvas('limits', 'limits',700,530) # can = rt.TCanvas('limits', 'limits') can.cd() can.SetLogy() # can.SetLogx() if ch == 'mCombined': framer.GetYaxis().SetTitle('|V_{N#mu}|^{2}') if ch == 'eCombined': framer.GetYaxis().SetTitle('|V_{Ne}|^{2}') framer.GetYaxis().SetTitleOffset(1.5) framer.GetXaxis().SetTitle('m_{N} (GeV)') framer.GetXaxis().SetTitleOffset(1.5) framer.Draw() # low_gr2.Draw('same, E1') low_gr2.Draw('same, E3') # low_gr1.Draw('same, E1') low_gr1.Draw('same, E3') # low_exp.Draw('same, LP') low_exp.Draw('same, L') DelphiDisplaced_exp = makeDelphiDisplaced() DelphiPrompt_exp = makeDelphiPrompt() AtlasDisplacedMuonLNV_exp = makeAtlasDisplacedMuonLNV() AtlasDisplacedMuonLNC_exp = makeAtlasDisplacedMuonLNV() for graph in [DelphiDisplaced_exp, DelphiPrompt_exp, AtlasDisplacedMuonLNV_exp, AtlasDisplacedMuonLNC_exp]: graph.SetMarkerStyle(0) graph.SetMarkerSize(1) graph.SetLineStyle(1) graph.SetLineWidth(2) DelphiDisplaced_exp.SetMarkerColor(rt.kOrange+2) DelphiDisplaced_exp.SetLineColor( rt.kOrange+2) DelphiPrompt_exp.SetMarkerColor(rt.kOrange+2) DelphiPrompt_exp.SetLineColor( rt.kOrange+2) DelphiPrompt_exp.SetLineStyle(2) AtlasDisplacedMuonLNV_exp.SetMarkerColor(rt.kPink+10) AtlasDisplacedMuonLNV_exp.SetLineColor( rt.kPink+10) if ch == 'mCombined': promptMuonGraph_ep2.Draw('same, L') promptMuonGraph_ep1.Draw('same, L') promptMuonGraph_exp.Draw('same, L') promptMuonGraph_em1.Draw('same, L') promptMuonGraph_em2.Draw('same, L') DelphiDisplaced_exp.Draw('same, L') DelphiPrompt_exp.Draw('same, L') AtlasDisplacedMuonLNV_exp.Draw('same, L') # AtlasDisplacedMuonLNC_exp.Draw('same, L') if ch == 'eCombined': promptElectronGraph_exp.Draw('same, L') promptElectronGraph_ep1.Draw('same, L') promptElectronGraph_exp.Draw('same, L') promptElectronGraph_em1.Draw('same, L') promptElectronGraph_em2.Draw('same, L') # leg = rt.TLegend(.4,.75,.8,.88) leg = rt.TLegend(.53,.58,.80,.88) leg.SetBorderSize(0) leg.AddEntry(low_exp, 'Expected', 'L') leg.AddEntry(low_gr1, 'Expected #pm 1 #sigma', 'CFL') leg.AddEntry(low_gr2, 'Expected #pm 2 #sigma', 'CFL') leg.AddEntry(promptMuonGraph_exp, 'CMS [EXO-17-012]', 'L') leg.AddEntry(DelphiDisplaced_exp, 'DELPHI long-lived') leg.AddEntry(DelphiPrompt_exp, 'DELPHI prompt') leg.AddEntry(AtlasDisplacedMuonLNV_exp, 'ATLAS') # leg.AddEntry(AtlasDisplacedMuonLNV_exp, 'ATLAS LNV') # leg.AddEntry(AtlasDisplacedMuonLNC_exp, 'ATLAS LNC') leg.Draw('apez same') plotfactory.showlogopreliminary() # plotfactory.showlumi('N#rightarrow%s; 59.7 fb^{-1} (13 TeV)'%(ch)) plotfactory.showlumi('59.7 fb^{-1} (13 TeV)') can.Update() if not os.path.isdir(output_dir + 'pdf'): os.mkdir(output_dir + 'pdf') if not os.path.isdir(output_dir + 'png'): os.mkdir(output_dir + 'png') if not os.path.isdir(output_dir + 'root'): os.mkdir(output_dir + 'root') if not os.path.isdir(output_dir + 'tex'): os.mkdir(output_dir + 'tex') can.SaveAs(output_dir + 'pdf/%s.pdf' %(ch)) can.SaveAs(output_dir + 'png/%s.png' %(ch)) can.SaveAs(output_dir + 'root/%s.root' %(ch)) can.SaveAs(output_dir + 'tex/%s.tex' %(ch))
pf.setpfstyle() output_dir = 'temp/' chain = ROOT.TChain('tree') file = '/Users/dehuazhu/lxplus/CMSSW_10_2_X_2018-05-24-2300/src/overlap_ntuple.root' chain.Add(file) c_pt = ROOT.TCanvas('c_pt', 'c_pt') binsx = np.arange(-1., 1., 0.05) h_pt = ROOT.TH1F('h_pt', '', len(binsx) - 1, binsx) h_pt.SetTitle(';(pt_{dsa} - pt_{smu}) / pt_{smu};entries') chain.Draw('(dSAMu_pt-sMu_pt)/sMu_pt >> h_pt', 'dSAMu_pt > 0. & sMu_pt > 0.') h_pt.Draw() pf.showlumi('ttbar %d entries' % (h_pt.GetEntries())) c_pt.Update() c_pt.SaveAs(output_dir + 'c_pt.root') c_pt.SaveAs(output_dir + 'c_pt.pdf') c_ptscatter = ROOT.TCanvas('c_ptscatter', 'c_ptscatter') c_ptscatter.cd() binsx = np.arange(0., 100., 2.) binsy = np.arange(0., 100., 2.) h_ptscatter = ROOT.TH2F('h_ptscatter', '', len(binsx) - 1, binsx, len(binsy) - 1, binsy) h_ptscatter.SetTitle(';pt_{sMu};pt_{dSAMu}') chain.Draw('dSAMu_pt:sMu_pt >> h_ptscatter', 'dSAMu_pt>0. & sMu_pt>0.') h_ptscatter.Draw('colz') pf.showlumi('ttbar %d entries' % (h_ptscatter.GetEntries()))
else: setting = True print('Using all samples') tt = pf.makechain(setting) nentries = tt.GetEntries() print('number of events: %d' % (nentries)) ######################################### # Make Plot ######################################### c = ROOT.TCanvas('c', 'c') binsx = np.arange(0., 200., 10.) binsy = np.arange(0., 600., 30.) h = ROOT.TH2F('h', '', len(binsx) - 1, binsx, len(binsy) - 1, binsy) h.SetTitle(';pt [GeV];production radius [cm];entries') tt.Draw( 'hnl_2d_disp:l0_pt >> h', 'abs(l0_eta)<2.4', ) h.Draw('colz') pf.showlumi('xxx fb^{-1} (xxx TeV)') pf.showlogopreliminary('CMS', 'Preliminary') c.Update()
def checkAcc(mode='e'): cut_denom = '' cut = 'l1_pt > 0 & l2_pt > 0' if mode == 'e': # cut_mll += ' & l0_matched_electron_pt > 30 & l0_matched_electron_reliso05 < 0.15' Mlist = Mlist_e if mode == 'mu': # cut_mll += ' & l0_matched_muon_pt > 25 & l0_matched_muon_reliso05 < 0.15' Mlist = Mlist_mu if mode == 'cmbd': Mlist = Mlist_em for m, mass in Mlist: t = rt.TChain('tree') for f in mass: t.Add(f) b_m = np.arange(0., 10, 0.1) b_eff = np.arange(0., 1, 0.1) # if m=='all': m=0 # if m=='cmbd':m=11 # if float(m) == 1: # b_m = np.arange(0.,450,50) # if float(m) == 4: # b_m = np.concatenate( (np.arange(0.,200,5),np.arange(200.,300,10), np.arange(300.,350,25)), axis=None ) # if float(m) > 4: # b_m = np.concatenate( (np.arange(0.,150,25), np.arange(150.,200,50), np.arange(200.,350,150)), axis=None) # if float(m) == 7: # b_m = np.concatenate( (np.arange(0.,25,1), np.arange(25.,100,25)), axis=None) # if float(m) == 8: # b_m = np.concatenate( (np.arange(0.,10,1), np.arange(10.,20,5), np.arange(20.,30,10)), axis=None) # if float(m) == 9: # b_m = np.concatenate( (np.arange(0.,5,1), np.arange(5,15,2.5)), axis=None) # if float(m) == 10: # b_m = np.concatenate( (np.arange(0.,2.5,0.5), np.arange(2.5,10,2.5)), axis=None) # if m==0: m='all' # if m==11: m='cmbd' framer = rt.TH2F('framer', 'framer', len(b_m) - 1, b_m, len(b_eff) - 1, b_eff) framer.SetTitle('; 2D displacement [cm]; Signal acceptance') framer.SetAxisRange(0., 350, "X") framer.SetAxisRange(0., 1, "Y") # if m=='all': m=0 # if float(m) == 1: # framer.SetAxisRange(0.,450,"X") # if float(m) == 7: # framer.SetAxisRange(0.,100,"X") # if float(m) == 8: # framer.SetAxisRange(0.,30,"X") # if float(m) == 9: # framer.SetAxisRange(0.,15,"X") # if float(m) == 10: # framer.SetAxisRange(0.,10,"X") # if m==0: m='1-10' if mode == 'mu': mode = '#mu' if mode == 'cmbd': mode = '(e+#mu)' mll = rt.TH1F('mll', 'mll', len(b_m) - 1, b_m) mll.SetTitle('; di-muon mass [GeV]; a.u.') print '\t mass:', m, 'entries:', t.GetEntries() print '\t done\n\thist entries: %i,' % t.GetEntries( cut), 'drawing mll ...' t.Draw('hnl_m_12 >> mll', cut) c_mll = rt.TCanvas('mll_M_%s_%s' % (m, mode), 'mll_M_%s_%s' % (m, mode)) mll.DrawNormalized() pf.showlogoprelimsim('CMS') pf.showlumi('m(N) = %s GeV' % m) pf.showTitle('%s#mu#mu' % mode) c_mll.Modified() c_mll.Update() c_mll.SaveAs(plotDir + c_mll.GetTitle() + '.root') c_mll.SaveAs(plotDir + c_mll.GetTitle() + '.pdf') c_mll.SaveAs(plotDir + c_mll.GetTitle() + '.png')
def makeFakeRatePlot(varName): tt = makechain() dataframe = ROOT.RDataFrame(tt) plotfactory.setpfstyle() can = ROOT.TCanvas('c1', 'c1') print('running variable %s' % varName) if varName == 'l1_pt': binsx = np.array([0., 5., 10., 15., 20., 30., 50., 70.]) # for pt if varName == 'abs_l1_eta': binsx = np.arange(0., 2.5, 0.4) if varName == 'abs_l1_dxy': binsx = np.logspace(-1.9, -1.4, 5) if varName == 'abs_l1_dz': binsx = np.logspace(-1.8, -0.5, 10) if varName == 'hnl_2d_disp': binsx = np.logspace(-0.7, 0.4, 5) if varName == 'hnl_dr_12': binsx = np.logspace(-1.3, -0.4, 9) if varName == 'hnl_m_12': binsx = np.arange(1., 4., 0.5) if varName == 'sv_prob': binsx = np.arange(0., 1.0, 0.1) if varName == 'hnl_w_vis_m': binsx = np.arange(0., 200.0, 30) # if varName == 'sv_prob': binsx = np.logspace(-1.6, 0., 5) # binsx = np.logspace(-7,2,40) # binsx = np.arange(0.,200.,10.) MeasurementRegion = Selections.Region('cr', 'mmm', 'CustomRegion') ht = dataframe\ .Define('abs_l1_eta','abs(l1_eta)')\ .Define('abs_l1_dxy','abs(l1_dxy)')\ .Define('abs_l1_dz','abs(l1_dz)')\ .Filter(MeasurementRegion.data)\ .Histo1D(('','',len(binsx)-1,binsx),'%s'%varName) hl = dataframe\ .Define('abs_l1_eta','abs(l1_eta)')\ .Define('abs_l1_dxy','abs(l1_dxy)')\ .Define('abs_l1_dz','abs(l1_dz)')\ .Filter(MeasurementRegion.baseline)\ .Histo1D(('','',len(binsx)-1,binsx),'%s'%varName) hist_tight = ht.Clone() hist_loose = hl.Clone() if varName == 'l1_pt': hist_tight.SetTitle(';lepton p_{T} (GeV); fakerate') if varName == 'abs_l1_eta': hist_tight.SetTitle(';lepton #eta; fakerate') if varName == 'abs_l1_dxy': hist_tight.SetTitle(';lepton impact parameter d_{xy} (cm); fakerate') if varName == 'abs_l1_dz': hist_tight.SetTitle(';lepton impact parameter d_{z} (cm); fakerate') if varName == 'hnl_2d_disp': hist_tight.SetTitle('; 2D displacement (cm); fakerate') if varName == 'hnl_dr_12': hist_tight.SetTitle('; #Delta R_{12}; fakerate') if varName == 'hnl_m_12': hist_tight.SetTitle('; dilepton invariant mass m_{12} (GeV); fakerate') if varName == 'sv_prob': hist_tight.SetTitle('; dilepton vertex quality; fakerate') if varName == 'hnl_w_vis_m': hist_tight.SetTitle('; Tri-Lepton Mass (GeV); fakerate') hist_tight.Divide(hist_loose) hist_tight.SetMarkerColor(ROOT.kBlue + 2) hist_tight.SetLineColor(ROOT.kBlue + 2) hist_tight.GetYaxis().SetRangeUser(0., 0.40) hist_tight.Draw() if varName == 'hnl_2d_disp': can.SetLogx() if varName == 'hnl_dr_12': can.SetLogx() if varName == 'abs_l1_dz': can.SetLogx() # if varName == 'sv_prob': can.SetLogx() # can.SetLogy() plotfactory.showlumi('59.7 fb^{-1} (13 TeV)') plotfactory.showlogopreliminary() can.Update() plotName = 'fr_%s' % varName for datatype in ['pdf', 'png', 'tex', 'root']: can.SaveAs('fakerateplots/%s/%s.%s' % (datatype, plotName, datatype)) for histogram in [ht, hl, hist_tight, hist_loose]: histogram.Reset()
def draw_limits(input_file, output_dir, ch='mmm', twoD=False, verbose=False): ''' ############################################################################# ## producing coupling vs r limits for each signal mass and a given channel ## ## also has the option 2D, in order to draw mass vs coupling limits (this ## ## uses the intersections of the 1D limits and the r=1 line) ## ############################################################################# ''' # create signal and limits dictionary limits, masses = get_lim_dict(input_file, output_dir, ch=ch) signals = get_signals() b = np.arange(0., 11, 1) req1 = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1] ixs = OrderedDict() for m in masses: print(colored('doing MASS = %d GeV' % m, 'green')) y_exp = [] y_ep1s = [] y_ep2s = [] y_em2s = [] y_em1s = [] v2s = [lim for lim in limits if 'M%d_' % m in lim] b_V2 = [] for v2 in v2s: # if limits[v2].has_key('exp'): if 'exp' in limits[v2]: y_exp.append(limits[v2]['exp']) y_ep1s.append(limits[v2]['ep1s']) y_ep2s.append(limits[v2]['ep2s']) y_em1s.append(limits[v2]['em1s']) y_em2s.append(limits[v2]['em2s']) try: b_V2.append(limits[v2]['V']) except: set_trace() if len(b_V2) == 0: print( 'combine failed processing this channel, continue doing to next one' ) continue x_err = np.zeros(len(b_V2)) b_V2.sort(reverse=False) for i in range(len(y_exp)): y_ep1s[i] = abs(y_ep1s[i] - y_exp[i]) y_ep2s[i] = abs(y_ep2s[i] - y_exp[i]) y_em1s[i] = abs(y_em1s[i] - y_exp[i]) y_em2s[i] = abs(y_em2s[i] - y_exp[i]) exp = rt.TGraph(len(b_V2), np.array(b_V2), np.array(y_exp)) gr1 = rt.TGraphAsymmErrors(len(b_V2), np.array(b_V2), np.array(y_exp), np.array(x_err), np.array(x_err), np.array(y_em1s), np.array(y_ep1s)) gr2 = rt.TGraphAsymmErrors(len(b_V2), np.array(b_V2), np.array(y_exp), np.array(x_err), np.array(x_err), np.array(y_em2s), np.array(y_ep2s)) rt.gStyle.SetOptStat(0000) B_V2 = np.logspace(-8, -1, 10, base=10) B_Y = np.logspace(-4, 4, 10, base=10) r1g = rt.TGraph(len(B_V2), np.array(B_V2), np.ones(len(B_V2))) r1g.SetLineColor(rt.kRed + 1) r1g.SetLineWidth(1) framer = rt.TH2F('framer', 'framer', len(B_V2) - 1, B_V2, len(B_Y) - 1, B_Y) framer.GetYaxis().SetRangeUser(0.0001, 10000) framer.GetXaxis().SetRangeUser(0.00000001, 0.1) if ch == 'mmm': framer.SetTitle('m_{N} = %d GeV, #mu#mu#mu; |V_{#mu N}|^{2}; r' % m) if ch == 'eee': framer.SetTitle('m_{N} = %d GeV, eee; |V_{e N}|^{2}; r' % m) if ch == 'mem_OS': framer.SetTitle('m_{N} = %d GeV, #mu#mue OS; |V_{#mu N}|^{2}; r' % m) if ch == 'mem_SS': framer.SetTitle('m_{N} = %d GeV, #mu#mue SS; |V_{#mu N}|^{2}; r' % m) if ch == 'eem_OS': framer.SetTitle('m_{N} = %d GeV, ee#mu OS; |V_{e N}|^{2}; r' % m) if ch == 'eem_SS': framer.SetTitle('m_{N} = %d GeV, ee#mu SS; |V_{e N}|^{2}; r' % m) exp.SetMarkerStyle(22) exp.SetMarkerSize(1) exp.SetMarkerColor(rt.kRed + 2) exp.SetLineColor(rt.kRed + 2) gr1.SetFillColor(rt.kGreen + 2) gr1.SetLineColor(rt.kGreen + 2) gr1.SetLineWidth(2) gr2.SetFillColor(rt.kOrange) gr2.SetLineColor(rt.kOrange) gr2.SetLineWidth(2) can = rt.TCanvas('limits', 'limits') can.cd() can.SetLogy() can.SetLogx() # can.SetBottomMargin(0.15) framer.Draw() can.Update() gr2.Draw('same, E1') gr2.Draw('same, E3') can.Update() gr1.Draw('same, E1') gr1.Draw('same, E3') can.Update() exp.Draw('same, LP') can.Update() r1g.Draw('same') can.Update() leg = rt.TLegend(.4, .75, .8, .88) leg.AddEntry(exp, 'Expected', 'LP') leg.AddEntry(gr1, 'Expected #pm 1 #sigma', 'E3') leg.AddEntry(gr2, 'Expected #pm 2 #sigma', 'E3') leg.Draw('apez same') # plotfactory.showlogopreliminary() plotfactory.showlumi('N#rightarrow%s; mass = %d GeV' % (ch, m)) can.Update() if not os.path.isdir(output_dir + 'pdf'): os.mkdir(output_dir + 'pdf') if not os.path.isdir(output_dir + 'png'): os.mkdir(output_dir + 'png') if not os.path.isdir(output_dir + 'root'): os.mkdir(output_dir + 'root') can.SaveAs(output_dir + 'pdf/M%d_%s_root.pdf' % (m, ch)) can.SaveAs(output_dir + 'png/M%d_%s_root.png' % (m, ch)) can.SaveAs(output_dir + 'root/M%d_%s_root.root' % (m, ch))
h_eff_mass_d2_Dxy_enum.SetLineColor(rt.kGreen + 2) h_eff_mass_d2_Dxy_enum.SetMarkerColor(rt.kGreen + 2) h_eff_mass_d2_Chi2_sMu_enum.Draw() h_eff_mass_d2_Chi2_enum.Draw('same') h_eff_mass_d2_Dxy_sMu_enum.Draw('same') h_eff_mass_d2_Dxy_enum.Draw('same') efleg = rt.TLegend(.4, .75, .8, .88) efleg.AddEntry(h_eff_mass_d2_Chi2_sMu_enum, 'Chi2, sMu', 'EP') efleg.AddEntry(h_eff_mass_d2_Chi2_enum, 'Chi2, sMu && dSAMu', 'EP') efleg.AddEntry(h_eff_mass_d2_Dxy_sMu_enum, 'Dxy, sMu', 'EP') efleg.AddEntry(h_eff_mass_d2_Dxy_enum, 'Dxy, sMu && dSAMu', 'EP') efleg.Draw('apez same') pf.showlumi('%d entries' % (h_eff_mass_d2_Dxy_enum.GetEntries())) pf.showlogopreliminary('CMS', 'Simulation Preliminary') c_eff_mass_d2.Modified() c_eff_mass_d2.Update() c_eff_mass_d2.SaveAs(output_dir + 'c_eff_mass_d2.pdf') c_eff_mass_d2.SaveAs(output_dir + 'c_eff_mass_d2.root') ######################################### # Reconstruction Purity ######################################### print('Filling purity vs hn mass histos @ d2') c_pur_mass_d2 = rt.TCanvas('c_pur_mass_d2', 'c_pur_mass_d2')
def checkAcc(mode='mu'): cut_denom = '' cut_num = 'l1_matched_dsmuon_pt > 5 & l2_matched_dsmuon_pt > 5' # CHECK this should work for e os and m os # cut_num += ' & is_in_acc == 1' if mode == 'e': cut_num += ' & l0_matched_electron_pt > 30 & l0_matched_electron_reliso05 < 0.15' Mlist = Mlist_e if mode == 'mu': cut_num += ' & l0_matched_muon_pt > 25 & l0_matched_muon_reliso05 < 0.15' # Mlist = Mlist_mu Mlist = [['2',M2_mu],['5',M5_mu],['8',M8_mu]] for m, mass in Mlist: t = rt.TChain('tree') for f in mass: t.Add(f) b_2disp = np.arange(0.,350,5) b_eff = np.arange(0.,1,0.1) if m=='all': m=0 if float(m) == 1: b_2disp = np.arange(0.,450,50) if float(m) == 4: b_2disp = np.concatenate( (np.arange(0.,200,5),np.arange(200.,300,10), np.arange(300.,350,25)), axis=None ) if float(m) > 4: b_2disp = np.concatenate( (np.arange(0.,150,25), np.arange(150.,200,50), np.arange(200.,350,150)), axis=None) if float(m) == 7: b_2disp = np.concatenate( (np.arange(0.,25,1), np.arange(25.,100,25)), axis=None) if float(m) == 8: b_2disp = np.concatenate( (np.arange(0.,10,1), np.arange(10.,20,5), np.arange(20.,30,10)), axis=None) if float(m) == 9: b_2disp = np.concatenate( (np.arange(0.,5,1), np.arange(5,15,2.5)), axis=None) if float(m) == 10: b_2disp = np.concatenate( (np.arange(0.,2.5,0.5), np.arange(2.5,10,2.5)), axis=None) if m==0: m='all' framer = rt.TH2F('framer','framer',len(b_2disp)-1,b_2disp,len(b_eff)-1,b_eff) framer.SetTitle('; 2D displacement [cm]; Signal acceptance') framer.SetAxisRange(0.,350,"X") framer.SetAxisRange(0.,1,"Y") if m=='all': m=0 if float(m) == 1: framer.SetAxisRange(0.,450,"X") if float(m) == 7: framer.SetAxisRange(0.,100,"X") if float(m) == 8: framer.SetAxisRange(0.,30,"X") if float(m) == 9: framer.SetAxisRange(0.,15,"X") if float(m) == 10: framer.SetAxisRange(0.,10,"X") if m==0: m='1-10' if mode =='mu': mode = '#mu' num = rt.TH1F('num' , 'num' , len(b_2disp)-1, b_2disp) denom = rt.TH1F('denom','denom', len(b_2disp)-1, b_2disp) print '\t mass:', m, 'entries:', t.GetEntries() print '\t denom entries: %i,'%t.GetEntries(cut_denom), 'drawing denom ...' t.Draw('hnl_2d_disp >> denom', cut_denom) print '\t denom done\n\tnum entries: %i,'%t.GetEntries(cut_num), 'drawing num ...' t.Draw('hnl_2d_disp >> num', cut_num) eff = rt.TEfficiency(num, denom) c_eff = rt.TCanvas('eff_M_%s_%s'%(m,mode),'eff_M_%s_%s'%(m,mode)) framer.Draw() eff.Draw('same') pf.showlogoprelimsim('CMS') pf.showlumi('m(N) = %s GeV'%m) pf.showTitle('%s#mu#mu'%mode) c_eff.Modified(); c_eff.Update() c_eff.SaveAs(plotDir + c_eff.GetTitle() + '.root') c_eff.SaveAs(plotDir + c_eff.GetTitle() + '.pdf') c_eff.SaveAs(plotDir + c_eff.GetTitle() + '.png')
def computeEffs(mode, ID, FAST=False, teff=False): # TODO make this a function fast = '' if FAST == True: fast = 'FS_' f_in = {} for tag in l_eta_tag: f_in['%s_all' %tag] = rt.TFile(plotDir + 'tmp/pt_eta_%s_%s_%s_%sall.root' %(mode,ID,tag,fast)) f_in['%s_pass'%tag] = rt.TFile(plotDir + 'tmp/pt_eta_%s_%s_%s_%spass.root'%(mode,ID,tag,fast)) h_pt_eta_00t08_all = f_in['00t08_all' ].Get('ALL') h_pt_eta_00t08_all_rbn = h_pt_eta_00t08_all.Rebin(7,'pt_eta_%s_%s_00t08_all_rbn',b_pt) h_pt_eta_00t08_pass = f_in['00t08_pass'].Get('PASS') h_pt_eta_00t08_pass_rbn = h_pt_eta_00t08_pass.Rebin(7,'pt_eta_%s_%s_00t08_pass_rbn',b_pt) h_pt_eta_08t14_all = f_in['08t14_all' ].Get('ALL') h_pt_eta_08t14_all_rbn = h_pt_eta_08t14_all.Rebin(7,'pt_eta_%s_%s_08t14_all_rbn',b_pt) h_pt_eta_08t14_pass = f_in['08t14_pass'].Get('PASS') h_pt_eta_08t14_pass_rbn = h_pt_eta_08t14_pass.Rebin(7,'pt_eta_%s_%s_08t14_pass_rbn',b_pt) h_pt_eta_14t16_all = f_in['14t16_all' ].Get('ALL') h_pt_eta_14t16_all_rbn = h_pt_eta_14t16_all.Rebin(7,'pt_eta_%s_%s_14t16_all_rbn',b_pt) h_pt_eta_14t16_pass = f_in['14t16_pass'].Get('PASS') h_pt_eta_14t16_pass_rbn = h_pt_eta_14t16_pass.Rebin(7,'pt_eta_%s_%s_14t16_pass_rbn',b_pt) h_pt_eta_16t20_all = f_in['16t20_all' ].Get('ALL') h_pt_eta_16t20_all_rbn = h_pt_eta_16t20_all.Rebin(7,'pt_eta_%s_%s_16t20_all_rbn',b_pt) h_pt_eta_16t20_pass = f_in['16t20_pass'].Get('PASS') h_pt_eta_16t20_pass_rbn = h_pt_eta_16t20_pass.Rebin(7,'pt_eta_%s_%s_16t20_pass_rbn',b_pt) h_pt_eta_20t25_all = f_in['20t25_all' ].Get('ALL') h_pt_eta_20t25_all_rbn = h_pt_eta_20t25_all.Rebin(7,'pt_eta_%s_%s_20t25_all_rbn',b_pt) h_pt_eta_20t25_pass = f_in['20t25_pass'].Get('PASS') h_pt_eta_20t25_pass_rbn = h_pt_eta_20t25_pass.Rebin(7,'pt_eta_%s_%s_20t25_pass_rbn',b_pt) if teff == False: eff_pt_eta_00t08 = rt.TH1F('eff_pt_eta_00t08', 'eff_pt_eta_00t08', len(b_pt)-1, b_pt) eff_pt_eta_08t14 = rt.TH1F('eff_pt_eta_08t14', 'eff_pt_eta_08t14', len(b_pt)-1, b_pt) eff_pt_eta_14t16 = rt.TH1F('eff_pt_eta_14t16', 'eff_pt_eta_14t16', len(b_pt)-1, b_pt) eff_pt_eta_16t20 = rt.TH1F('eff_pt_eta_16t20', 'eff_pt_eta_16t20', len(b_pt)-1, b_pt) eff_pt_eta_20t25 = rt.TH1F('eff_pt_eta_20t25', 'eff_pt_eta_20t25', len(b_pt)-1, b_pt) eff_pt_eta_00t08.Divide(h_pt_eta_00t08_pass_rbn, h_pt_eta_00t08_all_rbn) eff_pt_eta_08t14.Divide(h_pt_eta_08t14_pass_rbn, h_pt_eta_08t14_all_rbn) eff_pt_eta_14t16.Divide(h_pt_eta_14t16_pass_rbn, h_pt_eta_14t16_all_rbn) eff_pt_eta_16t20.Divide(h_pt_eta_16t20_pass_rbn, h_pt_eta_16t20_all_rbn) eff_pt_eta_20t25.Divide(h_pt_eta_20t25_pass_rbn, h_pt_eta_20t25_all_rbn) if teff == True: eff_pt_eta_00t08 = rt.TEfficiency(h_pt_eta_00t08_pass_rbn, h_pt_eta_00t08_all_rbn) eff_pt_eta_08t14 = rt.TEfficiency(h_pt_eta_08t14_pass_rbn, h_pt_eta_08t14_all_rbn) eff_pt_eta_14t16 = rt.TEfficiency(h_pt_eta_14t16_pass_rbn, h_pt_eta_14t16_all_rbn) eff_pt_eta_16t20 = rt.TEfficiency(h_pt_eta_16t20_pass_rbn, h_pt_eta_16t20_all_rbn) eff_pt_eta_20t25 = rt.TEfficiency(h_pt_eta_20t25_pass_rbn, h_pt_eta_20t25_all_rbn) eff_pt_eta_00t08.SetMarkerColor(rt.kGreen+2) eff_pt_eta_00t08.SetLineColor(rt.kGreen+2) eff_pt_eta_08t14.SetMarkerColor(rt.kBlue+2) eff_pt_eta_08t14.SetLineColor(rt.kBlue+2) eff_pt_eta_14t16.SetMarkerColor(rt.kRed+2) eff_pt_eta_14t16.SetLineColor(rt.kRed+2) eff_pt_eta_16t20.SetMarkerColor(rt.kYellow+2) eff_pt_eta_16t20.SetLineColor(rt.kYellow+2) eff_pt_eta_20t25.SetMarkerColor(rt.kCyan+2) eff_pt_eta_20t25.SetLineColor(rt.kCyan+2) c_eff = rt.TCanvas('eff','eff'); c_eff.cd() eff_framer.Draw() eff_pt_eta_00t08.Draw('same') eff_pt_eta_08t14.Draw('same') eff_pt_eta_14t16.Draw('same') eff_pt_eta_16t20.Draw('same') eff_pt_eta_20t25.Draw('same') leg = rt.TLegend(0.57, 0.18, 0.80, 0.4) leg.AddEntry(eff_pt_eta_00t08, '0.000 < |#eta| < 0.800') leg.AddEntry(eff_pt_eta_08t14, '0.800 < |#eta| < 1.444') leg.AddEntry(eff_pt_eta_14t16, '1.444 < |#eta| < 1.566') leg.AddEntry(eff_pt_eta_16t20, '1.566 < |#eta| < 2.000') leg.AddEntry(eff_pt_eta_20t25, '2.000 < |#eta| < 2.500') leg.Draw() pf.showlumi(re.sub('Run2017_','',ID + '_FS' if FAST else ID + '')) pf.showlogopreliminary() c_eff.SetLogx() c_eff.SetGridx(0) c_eff.Modified(); c_eff.Update() save(c_eff, 'pt_eta', 'SUSY', mode, ID + '_FS' if FAST else ID + '')
def draw_limits2D(ExclusionLimits2D_low, ExclusionLimits2D_high, input_file, output_dir, ch='mmm', twoD=False, verbose=False): ''' ############################################################################# ## producing coupling vs r limits for each signal mass and a given channel ## ## also has the option 2D, in order to draw mass vs coupling limits (this ## ## uses the intersections of the 1D limits and the r=1 line) ## ############################################################################# ''' # masses = [1.,2.,3.,4.,5.,6.,8.,10.,20.] # x_err = np.zeros(len(masses)) # y_exp = [2e-4 ,2.5e-6,2e-7 ,3e-8,8e-9 ,4e-9,7e-10 ,4e-10,7e-9 ] # y_ep1s = [1e-4 ,3.5e-6,1e-7 ,2e-8,7e-9 ,2e-9,2e-10 ,2e-10,3e-9 ] # y_ep2s = [5e-4 ,5.5e-6,6e-7 ,5e-8,12e-9,5e-9,8e-10 ,6e-10,8e-9] # y_em1s = [1e-4 ,0.5e-6,1e-7 ,1e-8,3e-9 ,2e-9,3e-10 ,2e-10,4e-9 ] # y_em2s = [1.4e-4,1.1e-6,1.2e-7,2e-8,5e-9 ,3e-9,5e-10 ,3e-10,6e-9 ] low_masses = ExclusionLimits2D_low['masses'] low_x_err = ExclusionLimits2D_low['x_err'] low_y_exp = ExclusionLimits2D_low['y_exp'] low_y_ep1s = ExclusionLimits2D_low['y_ep1s'] low_y_ep2s = ExclusionLimits2D_low['y_ep2s'] low_y_em1s = ExclusionLimits2D_low['y_em1s'] low_y_em2s = ExclusionLimits2D_low['y_em2s'] high_masses = ExclusionLimits2D_high['masses'] high_x_err = ExclusionLimits2D_high['x_err'] high_y_exp = ExclusionLimits2D_high['y_exp'] high_y_ep1s = ExclusionLimits2D_high['y_ep1s'] high_y_ep2s = ExclusionLimits2D_high['y_ep2s'] high_y_em1s = ExclusionLimits2D_high['y_em1s'] high_y_em2s = ExclusionLimits2D_high['y_em2s'] low_exp = rt.TGraph(len(low_masses), np.array(low_masses), np.array(low_y_exp)) low_gr1 = rt.TGraphAsymmErrors(len(low_masses), np.array(low_masses), np.array(low_y_exp), np.array(low_x_err), np.array(low_x_err), np.array(low_y_em1s), np.array(low_y_ep1s)) low_gr2 = rt.TGraphAsymmErrors(len(low_masses), np.array(low_masses), np.array(low_y_exp), np.array(low_x_err), np.array(low_x_err), np.array(low_y_em2s), np.array(low_y_ep2s)) high_exp = rt.TGraph(len(high_masses), np.array(high_masses), np.array(high_y_exp)) high_gr1 = rt.TGraphAsymmErrors(len(high_masses), np.array(high_masses), np.array(high_y_exp), np.array(high_x_err), np.array(high_x_err), np.array(high_y_em1s), np.array(high_y_ep1s)) high_gr2 = rt.TGraphAsymmErrors(len(high_masses), np.array(high_masses), np.array(high_y_exp), np.array(high_x_err), np.array(high_x_err), np.array(high_y_em2s), np.array(high_y_ep2s)) rt.gStyle.SetOptStat(0000) B_X = np.logspace(-0.1, 1, 50, base=10) B_Y = np.logspace(-11, 2, 50, base=10) framer = rt.TH2F('framer', 'framer', len(B_X) - 1, B_X, len(B_Y) - 1, B_Y) framer.GetXaxis().SetRangeUser(0, 10) framer.GetYaxis().SetRangeUser(1e-7, 1) # B_X = np.logspace(-1,2.5,10,base=10) # B_Y = np.logspace(-12, 0, 10, base=10) # framer = rt.TH2F('framer', 'framer', len(B_X)-1, B_X, len(B_Y)-1, B_Y) # framer.GetXaxis().SetRangeUser(0.1, 150.) # framer.GetYaxis().SetRangeUser(1e-12,1.) if ch == 'mmm': framer.SetTitle('#mu#mu#mu; m_{N}(GeV); |V_{#mu N}|^{2}') if ch == 'eee': framer.SetTitle('eee; m_{N}(GeV); |V_{e N}|^{2}') if ch == 'mem_OS': framer.SetTitle('#mu#mue OS; m_{N}(GeV); |V_{#mu N}|^{2}') if ch == 'mem_SS': framer.SetTitle('#mu#mue SS; m_{N}(GeV); |V_{#mu N}|^{2}') if ch == 'eem_OS': framer.SetTitle('ee#mu OS; m_{N}(GeV); |V_{e N}|^{2}') if ch == 'eem_SS': framer.SetTitle('ee#mu SS; m_{N}(GeV); |V_{e N}|^{2}') low_exp.SetMarkerStyle(22) low_exp.SetMarkerSize(1) low_exp.SetMarkerColor(rt.kRed + 2) low_exp.SetLineColor(rt.kRed + 2) low_exp.SetLineWidth(2) low_gr1.SetFillColor(rt.kGreen + 2) low_gr1.SetLineColor(rt.kGreen + 2) low_gr1.SetLineWidth(2) low_gr2.SetFillColor(rt.kOrange) low_gr2.SetLineColor(rt.kOrange) low_gr2.SetLineWidth(2) high_exp.SetMarkerStyle(22) high_exp.SetMarkerSize(1) high_exp.SetMarkerColor(rt.kRed + 2) high_exp.SetLineColor(rt.kRed + 2) high_exp.SetLineWidth(2) high_gr1.SetFillColor(rt.kGreen + 2) high_gr1.SetLineColor(rt.kGreen + 2) high_gr1.SetLineWidth(2) high_gr2.SetFillColor(rt.kOrange) high_gr2.SetLineColor(rt.kOrange) high_gr2.SetLineWidth(2) can = rt.TCanvas('limits', 'limits', 1000, 500) # can = rt.TCanvas('limits', 'limits') can.cd() can.SetLogy() # can.SetLogx() framer.Draw() # low_gr2.Draw('same, E1') low_gr2.Draw('same, E3') # low_gr1.Draw('same, E1') low_gr1.Draw('same, E3') # low_exp.Draw('same, LP') low_exp.Draw('same, L') # high_gr2.Draw('same, E1') high_gr2.Draw('same, E3') # high_gr1.Draw('same, E1') high_gr1.Draw('same, E3') # high_exp.Draw('same, LP') high_exp.Draw('same, L') can.Update() leg = rt.TLegend(.4, .75, .8, .88) leg.AddEntry(low_exp, 'Expected', 'LP') leg.AddEntry(low_gr1, 'Expected #pm 1 #sigma', 'E3') leg.AddEntry(low_gr2, 'Expected #pm 2 #sigma', 'E3') leg.Draw('apez same') # plotfactory.showlogopreliminary() plotfactory.showlumi('N#rightarrow%s; 59.7 fb^{-1} (13 TeV)' % (ch)) can.Update() if not os.path.isdir(output_dir + 'pdf'): os.mkdir(output_dir + 'pdf') if not os.path.isdir(output_dir + 'png'): os.mkdir(output_dir + 'png') if not os.path.isdir(output_dir + 'root'): os.mkdir(output_dir + 'root') can.SaveAs(output_dir + 'pdf/%s.pdf' % (ch)) can.SaveAs(output_dir + 'png/%s.png' % (ch)) can.SaveAs(output_dir + 'root/%s.root' % (ch))
print('number of events: %d' % (nentries)) ######################################### # Make Plot ######################################### # kinematic fitter c_kinpf = ROOT.TCanvas('c_kinpf', 'c_kinpf') binsx = np.arange(0., 100., 2.) binsy = np.arange(0., 100., 2.) h_kinpf = ROOT.TH2F('h', '', len(binsx) - 1, binsx, len(binsy) - 1, binsy) h_kinpf.SetTitle( ';Gen production radius [cm] ; RECO production radius [cm];entries') tt_kinpf.Draw('hnl_2d_disp:hnl_2d_gen_disp >> h') h_kinpf.Draw('colz') pf.showlumi('KinVtxFit, PF#mu') c_kinpf.Update() # kalman fitter c_kalpf = ROOT.TCanvas('c_kalpf', 'c_kalpf') binsx = np.arange(0., 100., 2.) h_kalpf = ROOT.TH2F('h', '', len(binsx) - 1, binsx, len(binsy) - 1, binsy) h_kalpf.SetTitle( ';Gen production radius [cm] ; RECO production radius [cm];entries') tt_kalpf.Draw('hnl_2d_disp:hnl_2d_gen_disp >> h') h_kalpf.Draw('colz') pf.showlumi('KalVtxFit, PF#mu') c_kalpf.Update() # kinematic fitter c_kinsa = ROOT.TCanvas('c_kinsa', 'c_kinsa')
dsmucfl1.Add(dsmucfl2) dsmusum.Add(dsmucfl1) dsmusum.Add(dsmunfl1) dsmucf.Divide(dsmucfl1,dsmusum) dsmucf.Draw('same') dsmucf.SetMarkerColor(2) smucf.GetXaxis().SetTitle('p_{T}[GeV]') smucf.GetYaxis().SetTitle('Chargeflip Ratio') smucf.GetXaxis().SetTitleOffset(1.2) smucf.GetYaxis().SetTitleOffset(1.4) slim = smusum.GetEntries() dsa = dsmusum.GetEntries() pf.showlumi('dSA#mu: %.2f / S#mu: %.2f M entries'%((slim / 1000000.),dsa / 1000000.)) pf.showlogosim('CMS') leg = ROOT.TLegend(.18,.76,.4,.9) leg.SetBorderSize(0) leg.SetFillColor(ROOT.kWhite) leg.SetFillStyle(0) leg.SetTextFont(42) leg.SetTextSize(0.045) leg.AddEntry(dsmucf, 'dSA#mu', 'EP') leg.AddEntry(smucf, 'S#mu', 'EP') leg.Draw('apez same') t.Update() print('Updating pads')
smucf.SetMarkerColor(4) dsmunfl1.Add(dsmunfl2) dsmucfl1.Add(dsmucfl2) dsmusum.Add(dsmucfl1) dsmusum.Add(dsmunfl1) dsmucf.Divide(dsmucfl1, dsmusum) dsmucf.Draw('same') dsmucf.SetMarkerColor(2) smucf.GetXaxis().SetTitle('p_{T}[GeV]') smucf.GetYaxis().SetTitle('Entries (normalized)') smucf.GetXaxis().SetTitleOffset(1.2) smucf.GetYaxis().SetTitleOffset(1.4) pf.showlumi('%.2f M entries' % (ntries / 1000000.)) pf.showlogoprelimsim('CMS') leg = ROOT.TLegend(.18, .76, .4, .9) leg.SetBorderSize(0) leg.SetFillColor(ROOT.kWhite) leg.SetFillStyle(0) leg.SetTextFont(42) leg.SetTextSize(0.03) leg.AddEntry(dsmucf, 'dSA#mu', 'EP') leg.AddEntry(smucf, 'S#mu', 'EP') leg.Draw('apez same') t.Update() print('Updating pads')
def measureSFR(self, drawPlot = False): sample_dict = {} samples_all, samples_singlefake, samples_doublefake = createSampleLists(analysis_dir=self.analysis_dir, server = self.server, channel=self.channel) working_samples = samples_singlefake working_samples = setSumWeights(working_samples) print('###########################################################') print('# measuring singlefakerake...') print('# %d samples to be used:'%(len(working_samples))) print('###########################################################') for w in working_samples: print('{:<20}{:<20}'.format(*[w.name,('path: '+w.ana_dir)])) chain = TChain('tree') #TChain'ing all data samples together for i,s in enumerate(working_samples): sample = working_samples[0] file_name = '/'.join([sample.ana_dir, sample.dir_name, sample.tree_prod_name, 'tree.root']) chain.Add(file_name) dataframe = RDataFrame(chain) weight = 'weight * lhe_weight' dataframe = dataframe.Define('w',weight)\ .Define('ptCone',self.ptCone())\ .Define('abs_hnl_hn_vis_eta','abs(hnl_hn_vis_eta)')\ .Define('abs_hnl_hn_eta','abs(hnl_hn_eta)')\ .Define('abs_l1_eta','abs(l1_eta)')\ .Define('abs_l2_eta','abs(l2_eta)')\ .Define('abs_l1_jet_flavour_parton','abs(l1_jet_flavour_parton)')\ .Define('abs_l2_jet_flavour_parton','abs(l2_jet_flavour_parton)')\ # bins_ptCone = np.array([5.,10., 20., 30., 40.,70., 2000]) # bins_eta = np.array([0., 0.8, 1.2, 2.4]) bins_ptCone = np.array([5.,10., 20., 30., 40.,70.]) bins_eta = np.array([0., 0.8, 1.2, 2.4]) selection_baseline = getSelection(self.channel,'MR_SF') selection_LL_uncorrelated = '(' + ' & '\ .join([\ selection_baseline,\ getSelection(self.channel,'L_L_uncorrelated')\ ]) + ')' selection_TT_uncorrelated = '(' + ' & '\ .join([\ selection_baseline,\ getSelection(self.channel,'L_L_uncorrelated'),\ getSelection(self.channel,'T_T')\ ]) + ')' h_LL_uncorrelated = dataframe\ .Filter(selection_LL_uncorrelated)\ .Histo2D(('h_LL_uncorrelated','h_LL_uncorrelated',len(bins_ptCone)-1,bins_ptCone, len(bins_eta)-1, bins_eta),'ptCone','abs_hnl_hn_vis_eta','w') #name the axis, also initiate the dataframe call h_LL_uncorrelated.SetTitle(';ptCone [GeV]; dimuon #eta') h_TT_uncorrelated = dataframe\ .Filter(selection_TT_uncorrelated)\ .Histo2D(('h_TT_uncorrelated','h_TT_uncorrelated',len(bins_ptCone)-1,bins_ptCone, len(bins_eta)-1, bins_eta),'ptCone','abs_hnl_hn_vis_eta','w') #name the axis, also initiate the dataframe call h_TT_uncorrelated.SetTitle(';ptCone [GeV]; dimuon #eta') # preparing the histo and save it into a .root file sfr_TH2_dir = '/home/dehuazhu/HNL/CMSSW_9_4_6_patch1/src/PlotFactory/DataBkgPlots/modules/DDE_singlefake.root' sfr_hist = h_TT_uncorrelated.Clone() # sfr_hist = h_LL_uncorrelated.Clone() # sfrhist = h_baseline.Clone() # sfr_hist.Divide(h_LL_uncorrelated.Clone()) # dfr_hist.SaveAs(sfr_TH2_dir) #uncomment this to save the TH2 # draw the histo if required if drawPlot == True: can = TCanvas('can', '') # sfr_hist.Draw('colzTextE') # sfr_hist.Draw('colz') sfr_hist.Draw() pf.showlumi('%d entries'%(sfr_hist.GetEntries())) # pf.showlogopreliminary() can.Update() set_trace()