def _draw(self): passed = self.sim_dir.Get(self.passed_name) total = self.sim_dir.Get(self.total_name) if not TEfficiency.CheckConsistency(passed, total): return None eff = TEfficiency(passed, total) self.hist = eff.CreateHistogram()
def getEfficiencyForPtCut(listMatchedPairs, ptCut): global efficiencyCounter efficiencyCounter += 1 effObj = TEfficiency("efficiency" + str(efficiencyCounter), "Efficiency for p_{T,cut} = " + str(ptCut) + " GeV", 251, -0.5, 250.5) ROOT.SetOwnership(effObj, False) truePtValues = range(0, 200) countPassed = [0] * 200 countTotal = [0] * 200 yVal = [0] * 200 xErr = [0.5] * 200 yErr = [0] * 200 for event in listMatchedPairs: for pair in event: effObj.Fill(pair[0].pt >= ptCut, pair[1].pt) countTotal[int(pair[1].pt)] += 1 if (pair[0].pt >= ptCut): countPassed[int(pair[1].pt)] += 1 for i, val in enumerate(countPassed): if (countTotal[i] > 0): yVal[i] = float(countPassed[i] / countTotal[i]) yErr[i] = float(math.sqrt(yVal[i] * (1 - yVal[i]) / countTotal[i])) #print countPassed #print countTotal c = TCanvas("can", "vas", 1200, 1200) #effObj.Draw() return c, effObj
def efficiencytracking(): hadron_list = ["pion", "proton", "electron", "muon"] color_list = [1, 2, 4, 6] fileo2 = TFile("../codeHF/AnalysisResults_O2.root") c1 = TCanvas("c1", "A Simple Graph Example") c1.SetCanvasSize(1500, 1500) c1.cd() gPad.SetLogx() gPad.SetLogy() eff_list = [] hempty = TH1F("hempty", ";p_{T};efficiency", 100, 0.001, 5.0) hempty.Draw() leg = TLegend(0.1, 0.7, 0.3, 0.9, "") leg.SetFillColor(0) for i, had in enumerate(hadron_list): hnum = fileo2.Get("qa-tracking-efficiency-%s/num" % had) hden = fileo2.Get("qa-tracking-efficiency-%s/den" % had) hnum.Rebin(4) hden.Rebin(4) eff = TEfficiency(hnum, hden) eff.SetLineColor(color_list[i]) eff_list.append(eff) eff.Draw("same") leg.AddEntry(eff_list[i], had) leg.Draw() c1.SaveAs("efficiency_tracking.pdf")
def intervalFromValues( self, values, weights ): ## Calculates the selected interval from weighted values # Symmetrically removes entries on both side until the desired fraction # of weights remains. Uncertainties are estimated by interpreting the fraction # of entries below and above the limits as an efficiency measurements. # Limits are then given from the 1-sigma confidence interval. # @param values list of values # @param weights list of weights (should match length of values) # @result (lower, upper) boundary self._reset() from ROOT import TEfficiency import numpy values = numpy.array( values ) # simply calculate the interval that contains the desired fraction of weights self.low, self.up = truncatedInterval( values, self.fraction, weights ) nTotal = len( values ) nLow = len( values[values <= self.low] ) nUp = len( values[values < self.up] ) # interpret entries below and above as counting experiment # for 1-sigma confidence intervals the corresponding percentiles are calculated # as an estimate of the uncertainty on the percentile values above self.lowLow, self.lowUp, self.upLow, self.upUp = percentiles( values, [ TEfficiency.ClopperPearson( nTotal, nLow, 0.68, False ), TEfficiency.ClopperPearson( nTotal, nLow, 0.68, True ), TEfficiency.ClopperPearson( nTotal, nUp, 0.68, False ), TEfficiency.ClopperPearson( nTotal, nUp, 0.68, True ) ], weights ) return self.low, self.up
def add_points(graph, directory, layer, usePU): ipt = graph.GetN() # List runs for root, directories, files in os.walk(directory): for rundir in sorted(directories): if "run_" in rundir: # start to process run run = rundir[4:] #print "processing run ", run lumi = 0 lumi_err = 0 # Get informations for a given run frun = TFile(directory + "/" + rundir + "/withMasking/rootfile/SiStripHitEffHistos_run" + run + ".root") fdir = frun.GetDirectory("SiStripHitEff") # for efficiency hfound = fdir.Get("found") htotal = fdir.Get("all") if htotal == None: print ' Missing histogram in file ' + frun.GetName() continue # lumi if usePU == 0: hlumi = fdir.Get("instLumi") else: hlumi = fdir.Get("PU") if hlumi == None: print ' Missing lumi/pu histogram in file ' + frun.GetName( ) continue lumi = hlumi.GetMean() lumi_err = hlumi.GetRMS() #print "lumi (avg+/-rms): ", lumi, "+/-", lumi_err # efficiency for a given layer found = hfound.GetBinContent(layer) total = htotal.GetBinContent(layer) if total > 0: eff = found / total else: eff = 0 #print run, eff, lumi, lumi_err # remove run without lumi informations if lumi > 1: eff_vs_lumi.SetPoint(ipt, lumi, eff) low = TEfficiency.Bayesian(total, found, .683, 1, 1, False) up = TEfficiency.Bayesian(total, found, .683, 1, 1, True) if eff - low > 0.01: print 'large error bar for run', run, 'layer', layer, 'eff:', '{:.4f}'.format( eff), 'err:', '{:.4f}'.format(eff - low) #if lumi_err > lumi/3.: print 'wide lumi range for run', run, 'layer', layer, 'eff:', '{:.4f}'.format(eff), 'lumi/pu:', '{:.4f}'.format(lumi), 'rms:', '{:.4f}'.format(lumi_err) eff_vs_lumi.SetPointError(ipt, lumi_err, lumi_err, eff - low, up - eff) ipt += 1 frun.Close()
def histsToRoc(hsig, hbg, w_error=False): '''Produce ROC curve from 2 input histograms. Partly adapted from Giovanni's ttH code. ''' nbins = hsig.GetNbinsX() + 2 - 1 # include under/overflow; remove events not passing selection si = [hsig.GetBinContent(i) for i in xrange(nbins+1)] bi = [hbg.GetBinContent(i) for i in xrange(nbins+1)] del si[1] del bi[1] if hsig.GetMean() > hbg.GetMean(): si.reverse() bi.reverse() sums, sumb = sum(si), sum(bi) if sums == 0 or sumb == 0: print 'WARNING: Either signal or background histogram empty', sums, sumb return None for i in xrange(1, nbins): si[i] += si[i - 1] bi[i] += bi[i - 1] fullsi, fullbi = si[:], bi[:] si, bi = [], [] for i in xrange(1, nbins): # skip negative weights if si and (fullsi[i] < si[-1] or fullbi[i] < bi[-1]): continue # skip repetitions if fullsi[i] != fullsi[i - 1] or fullbi[i] != fullbi[i - 1]: si.append(fullsi[i]) bi.append(fullbi[i]) if len(si) == 2: si = [si[0]] bi = [bi[0]] bins = len(si) if not w_error: roc = ROOT.TGraph(bins) for i in xrange(bins): roc.SetPoint(i, si[i] / sums, bi[i] / sumb) return roc roc = ROOT.TGraphAsymmErrors(bins) for i in xrange(bins): interval = 0.683 e_s_low = si[i] / sums - TEfficiency.ClopperPearson(sums, si[i], interval, False) e_s_up = TEfficiency.ClopperPearson(sums, si[i], interval, True) - si[i] / sums e_b_low = bi[i] / sumb - TEfficiency.ClopperPearson(sumb, bi[i], interval, False) e_b_up = TEfficiency.ClopperPearson(sumb, bi[i], interval, True) - bi[i] / sumb roc.SetPoint(i, si[i] / sums, bi[i] / sumb) roc.SetPointError(i, e_s_low, e_s_up, e_b_low, e_b_up) return roc
def FeldmanCousins(this_ctr, total_ctr, confidence_interval): from ROOT import TEfficiency k = TEfficiency() eff_mean = float(this_ctr) / float(total_ctr) eff_upper = k.FeldmanCousins(total_ctr, this_ctr, confidence_interval, True) eff_lower = k.FeldmanCousins(total_ctr, this_ctr, confidence_interval, False) return eff_mean, [eff_lower, eff_upper]
def efficiencyhadron(had, var): fileo2 = TFile("../codeHF/AnalysisResults_O2.root") ceffhf = TCanvas("ceffhf", "A Simple Graph Example") ceffhf.SetCanvasSize(1500, 700) ceffhf.Divide(2, 1) gPad.SetLogy() hnum = fileo2.Get("hf-task-%s-mc/h%sRecSig" % (had, var)) hden = fileo2.Get("hf-task-%s-mc/h%sGen" % (had, var)) hnum.Rebin(4) hden.Rebin(4) eff = TEfficiency(hnum, hden) eff.Draw() ceffhf.SaveAs("efficiency_hfcand%s%s.pdf" % (had, var))
def makeEfficiencies(passedtotal): result = {} # Calculate raw efficiencies for proc, histos in passedtotal.iteritems(): proc_effs = {} for key, (p, f) in histos.iteritems(): eff = TEfficiency(p, f) proc_effs[key] = eff result[proc] = proc_effs # # Calculate background corrected efficiencies # # I.e. subtract ttbar MC from data # for key in passedtotal.values()[0].keys(): # print '... processing', key # passdata,totdata = passedtotal['data'][key] # passtt, tottt = passedtotal['ttbar'][key] # pcorr = passdata.Clone("pass_corrected") # fcorr = totdata.Clone("tot_corrected") # pcorr.Add(passtt, -1.0) # fcorr.Add(tottt, -1.0) # result.setdefault('data_corr', {})[key] = TEfficiency(pcorr, fcorr) return result
def makeEfficiency(y_test, y_predClasses, pt, definedIds, outName): nClasses = len(definedIds) passedHists = [ TH1D("num_" + str(ID), "label " + str(ID), 41, -0.5, 40.5) for i, ID in enumerate(definedIds) ] totalHists = [ TH1D("eff_" + str(ID), "label " + str(ID), 41, -0.5, 40.5) for i, ID in enumerate(definedIds) ] #break it down by class for j, ID in enumerate(definedIds): for i, n in enumerate(zip(y_test, y_predClasses)): if n[0] == ID: if n[0] == n[1]: #correct match passedHists[j].Fill(pt.iloc[i]) totalHists[j].Fill(pt.iloc[i]) elif n[0] != n[1]: #incorrect match totalHists[j].Fill(pt.iloc[i]) goodEff = [ TEfficiency(passedHists[i], totalHists[i]) for i in range(nClasses) ] for i, ID in enumerate(goodEff): ID.SetTitle("class " + str(definedIds[i]) + " eff") outfile = TFile("./test.root", "RECREATE") [outfile.WriteTObject(x) for x in goodEff] plotEfficiency(goodEff, outName, outfile)
def doHisto(file, var, thecolor, i, lmin, lmax, sel): tt = file.Get('tree') if var[9] == -1: hnum = ROOT.TH1F('hnum', 'hnum', var[3], var[4][0], var[4][1]) hden = ROOT.TH1F('hden', 'hden', var[3], var[4][0], var[4][1]) else: print len(var[9]) - 1 print np.asarray(var[9]) hnum = ROOT.TH1F('hnum', 'hnum', len(var[9]) - 1, array('d', var[9])) hden = ROOT.TH1F('hden', 'hden', len(var[9]) - 1, array('d', var[9])) tt.Draw( 'tau_gen_%s>>+hnum' % (var[0]), 'tau_gen_lxy>-100 && tau_gen_vis_pt>15 && abs(tau_gen_vis_eta)<2.1 && tau_gen_vis_pt>20 && tau_gen_vis_pt<1000 && tau_gen_lxy>%s && tau_gen_lxy<%s && tau_l1_iso==1 && tau_l1_pt>20 ' % (lmin, lmax) + sel, 'goff') tt.Draw( 'tau_gen_%s>>+hden' % (var[0]), 'tau_gen_lxy>-100 && tau_gen_vis_pt>15 && abs(tau_gen_vis_eta)<2.1 && tau_gen_vis_pt>20 && tau_gen_vis_pt<1000 && tau_gen_lxy>%s && tau_gen_lxy<%s' % (lmin, lmax) + sel, 'goff') pEff = dc(TEfficiency(hnum, hden)) pEff.SetLineColor(thecolor) pEff.SetMarkerColor(thecolor) pEff.SetMarkerStyle(8) pEff.SetMarkerSize(0.8) pEff.SetTitle(";" + var[1] + ";" + var[2]) return pEff
def efficiencyhadron(had, var): # extract the efficiency vs pT for single species(D0, Lc, Jpsi) fileo2 = TFile("../codeHF/AnalysisResults_O2.root") ceffhf = TCanvas("ceffhf", "A Simple Graph Example") ceffhf.SetCanvasSize(1500, 700) ceffhf.Divide(2, 1) gPad.SetLogy() # hnum = fileo2.Get("qa-tracking-efficiency-%s/%s/num" % (had, var)) hnum = fileo2.Get("hf-task-%s-mc/h%sRecSig" % (had, var)) # hden = fileo2.Get("qa-tracking-efficiency-%s/%s/den" % (had, var)) hden = fileo2.Get("hf-task-%s-mc/h%sGen" % (had, var)) hnum.Rebin(4) hden.Rebin(4) eff = TEfficiency(hnum, hden) eff.Draw() saveCanvas(ceffhf, "efficiency_hfcand%s%s" % (had, var))
def add_points(graph, directory, subdir, layer): ipt = graph.GetN() labels = [] # List runs for root, directories, files in os.walk(directory): for rundir in sorted(directories): if "run_" in rundir: # start to process run run = rundir[4:] #print "processing run ", run # for efficiency frun = TFile(directory + "/" + rundir + "/" + subdir + "/rootfile/SiStripHitEffHistos_run" + run + ".root") fdir = frun.GetDirectory("SiStripHitEff") hfound = fdir.Get("found") htotal = fdir.Get("all") if htotal == None: print ' Missing histogram in file ' + frun.GetName() continue # efficiency for a given layer found = hfound.GetBinContent(int(layer)) total = htotal.GetBinContent(int(layer)) if total > 0: eff = found / total else: eff = 0 #print run, eff graph.SetPoint(ipt, ipt + 1, eff) labels.append(run) low = TEfficiency.Bayesian(total, found, .683, 1, 1, False) up = TEfficiency.Bayesian(total, found, .683, 1, 1, True) #eff_vs_run.SetPointError(ipt, 0, 0, eff-low, up-eff) ipt += 1 frun.Close() axis = graph.GetXaxis() for i in range(graph.GetN()): axis.SetBinLabel(axis.FindBin(i + 1), labels[i]) #print i, axis.FindBin(i+1), labels[i] return labels
def __retrieve_efficiencies(self, store, path, inname, outname): rawobj = dict() inname = (path + '/' + inname).replace('//', '/') outname = (path + '/' + outname).replace('//', '/') rawobj['et'] = store.histogram(inname + 'et') rawobj['eta'] = store.histogram(inname + 'eta') rawobj['mu'] = store.histogram(inname + 'mu') rawobj['nvtx'] = store.histogram(inname + 'nvtx') rawobj['match_et'] = store.histogram(outname + 'et') rawobj['match_eta'] = store.histogram(outname + 'eta') rawobj['match_mu'] = store.histogram(outname + 'mu') rawobj['match_nvtx'] = store.histogram(outname + 'nvtx') from ROOT import TProfile, TEfficiency, TH1F if not rawobj['nvtx']: rawobj['nvtx'] = TH1F() if not rawobj['match_nvtx']: rawobj['match_nvtx'] = TH1F() passed_eta = rawobj['match_eta'].GetEntries() passed_eta = rawobj['match_et'].GetEntries() total_eta = rawobj['eta'].GetEntries() total_eta = rawobj['et'].GetEntries() if total_eta != 0: eff_eta = passed_eta / float(total_eta) * 100 else: eff_eta = 0 # create output dictionary obj = dict() obj['eff_et'] = TEfficiency(rawobj['match_et'], rawobj['et']) obj['eff_eta'] = TEfficiency(rawobj['match_eta'], rawobj['eta']) obj['eff_mu'] = TEfficiency(rawobj['match_mu'], rawobj['mu']) obj['eff_nvtx'] = TEfficiency(rawobj['match_nvtx'], rawobj['nvtx']) obj['eff_et'].SetTitle('#epsilon(E_{T}); E_{T}[GeV] ; #epsilon(E_{T})') obj['eff_eta'].SetTitle('#epsilon(#eta); #eta ; #epsilon(#eta)') obj['eff_mu'].SetTitle('#epsilon(#mu); #mu ; #epsilon(#mu)') obj['eff_nvtx'].SetTitle( '#epsilon(N_{vtx}); N_{vtx} ; #epsilon(N_{vtx})') return obj, eff_eta, passed_eta, total_eta
def PrintEff(self, Nev): table = PrettyTable([self.SRname, 'cut-name', 'Eff']) table.padding_width = 2 nden = int(Nev) OrderedDict(sorted(self.id.items(), key=lambda t: t[1])) for name, idd in self.id.iteritems(): nsel = int(self.cut[name]) Eff = float(nsel) / float(Nev) err_plus = TEfficiency.Bayesian(nden, nsel, .6827, 0.5,0.5, True) err_minus = TEfficiency.Bayesian(nden, nsel, .6827, 0.5,0.5, False) Eff = str(Eff) + ' + ' + str(abs(err_plus-Eff)) +" - " +str(abs(err_minus-Eff)) table.add_row( [idd, name, Eff] ) #print '='*10 +' '+ self.SRname +' '+ '='*10 print table
def draw_geff(t, title, h_bins, to_draw, den_cut, extra_num_cut, opt = "", color = kBlue, marker_st = 1, marker_sz = 1.): """Make an efficiency plot""" ## total numerator selection cut num_cut = AND(den_cut,extra_num_cut) ## PyROOT works a little different than ROOT when you are plotting ## histograms directly from tree. Hence, this work-around nBins = int(h_bins[1:-1].split(',')[0]) minBin = float(h_bins[1:-1].split(',')[1]) maxBin = float(h_bins[1:-1].split(',')[2]) num = TH1F("num", "", nBins, minBin, maxBin) den = TH1F("den", "", nBins, minBin, maxBin) t.Draw(to_draw + ">>num", num_cut, "goff") t.Draw(to_draw + ">>den", den_cut, "goff") debug = False if debug: print("Denominator cut", den_cut, den.GetEntries()) print("Numerator cut", num_cut, num.GetEntries()) ## check if the number of passed entries larger than total entries doConsistencyCheck = False if doConsistencyCheck: for i in range(0,nBins): print(i, num.GetBinContent(i), den.GetBinContent(i)) if num.GetBinContent(i) > den.GetBinContent(i): print(">>>Error: passed entries > total entries") eff = TEfficiency(num, den) ## plotting options if not "same" in opt: num.Reset() num.GetYaxis().SetRangeUser(0.0,1.1) num.SetStats(0) num.SetTitle(title) num.Draw() eff.SetLineWidth(2) eff.SetLineColor(color) eff.SetMarkerStyle(marker_st) eff.SetMarkerColor(color) eff.SetMarkerSize(marker_sz) eff.Draw(opt + " same") SetOwnership(eff, False) return eff
def makeEfficiency(passed, total, title, lineColor): if TEfficiency.CheckConsistency(passed, total): efficiency = TEfficiency(passed, total) #title = std::regex_replace(title, std::regex("\\muCandGenEtaMuons"), "tagging efficiency"); efficiency.SetTitle(title) efficiency.SetStatisticOption(6) #TEfficiency.EStatOption.kBUniform efficiency.SetPosteriorMode() efficiency.SetLineColor(lineColor) return efficiency else: print( "makeEfficiency TEfficiency::CheckConsistency(*ptGenPtTTMuonNom, *ptGenPtTTMuonDenom) failed" ) exit(1)
def fillAcceptance(self): ''' This is to calculate the acceptance of the sample ''' if debug: print('Info in Sample.fillAcceptance()') #f = TFile.Open(self.infileName) #t = f.Get(self.treeName) #if not t: # raise RuntimeError( 'ERROR: no tree in file %s' % self.infileName) chain = TChain(self.treeName) for fn in self.infileNames: chain.Add(fn) self.effnum = ROOT.TH1F( 'effnum', 'effnum', 1, 0, 13000 ) #dict([(k, ROOT.TH1F('effnum_%s' % k, 'effnum_%s' % k, 1, 0, 1)) for k in self.settings]) self.effden = ROOT.TH1F('effden', 'effden', 1, 0, 13000) if doInclusive: cutsnum = '(l0_pt>{mp} && abs(l0_eta)<1.5'.format(mp=muTrigPt) else: cutsnum = '(l0_pt>{mp} && abs(l0_eta)<1.5 && k_pt>1 && abs(k_eta)<2.5 && pi_pt>1 && abs(pi_eta)<2.5'.format( mp=muTrigPt) if doSkipDispl: cutsnum += '' else: cutsnum += '&& Lxy < 1000' if doSkipHNLptEta: cutsnum += '' else: cutsnum += '&& l1_pt>3 && abs(l1_eta)<2.5 && pi1_pt>0.8 && abs(pi1_eta)<2.5' cutsnum += ')' cutsden = '(l0_pt>{mp} && abs(l0_eta)<1.5)'.format(mp=muTrigPt) chain.Draw('hnl_pt>>effnum', cutsnum + '*' + self.evt_w, 'goff') chain.Draw('hnl_pt>>effden', cutsden + '*' + self.evt_w, 'goff') if TEfficiency.CheckConsistency(self.effnum, self.effden): peff = TEfficiency(self.effnum, self.effden) self.acc = peff.GetEfficiency(1) self.acc_errup = peff.GetEfficiencyErrorUp(1) self.acc_errdn = peff.GetEfficiencyErrorLow(1) # for debugging purposes self.num = self.effnum.GetEntries() if self.num == 0: print('**** 0 entries for mass={}'.format(self.mass)) self.den = self.effden.GetEntries()
def fillFilterEff(self, dostamp=True): ''' To retrieve and save the filter efficiency - from the minigentree TODO: retrieve the cross-section => for that you would need to run without separate jobs ''' if debug: print('Info in Sample.fillFilterEff()') efffnum = ROOT.TH1F('efffnum', 'efffnum', 1, 0, 13000) efffden = ROOT.TH1F('efffden', 'efffden', 1, 0, 13000) chain = TChain(self.treeName) for fn in self.infileNames: chain.Add(fn) efffnum.SetBinContent(1, chain.GetEntries()) efffnum.SetBinError(1, ROOT.TMath.Sqrt(chain.GetEntries())) # denominator = number of events that were run in the first place # access storage element... #self.ngenevts_succ_afterfilter=0 self.njobs_succ = 0 path = '/pnfs/psi.ch/cms/trivcat/store/user/{u}/BHNLsGen/{pl}/mass{m}_ctau{ctau}/step1*root'.format( u=os.environ['USER'], pl=self.label, m=self.mass, ctau=self.ctau) for fname in glob(path): if debug: print 'fname=', fname f = TFile.Open(fname) if f.GetListOfKeys().Contains('Events'): self.njobs_succ += 1 #njobs_succ = len(glob(path)) self.ngenevts_succ = float(self.ngenevts) / float(self.njobs) * float( self.njobs_succ) efffden.SetBinContent(1, self.ngenevts_succ) efffden.SetBinError(1, ROOT.TMath.Sqrt(self.ngenevts_succ)) if TEfficiency.CheckConsistency(efffnum, efffden): geneff = TEfficiency(efffnum, efffden) self.effFilter = geneff.GetEfficiency(1) self.effFilter_errup = geneff.GetEfficiencyErrorUp(1) self.effFilter_errdn = geneff.GetEfficiencyErrorLow(1) # stamp basic info about filter eff if dostamp: print('mass={m}GeV, VV={vv:.1e}, nTot={nt}, nSucc={ns}, nSuccAfterFilter={nsf}, effFilter={ef:.3f}%, errup={eu:.3f}%, errdn={ed:.3f}% '.format( \ m=self.mass,vv=self.vv,nt=self.ngenevts,ns=self.ngenevts_succ,nsf=efffnum.GetBinContent(1),ef=self.effFilter*100,eu=self.effFilter_errup*100,ed=self.effFilter_errdn*100))
def acceptance(cutlist, labellist): basecut = cutlist[0] dim = len(cutlist) #cutlist.remove(cutlist[:1][0]) if basecut == 'SSemu': basecutstr = '((Lepton_pdgId[0]*Lepton_pdgId[1]==11*13)||(Lepton_pdgId[0]*Lepton_pdgId[1]==-11*-13))' elif basecut == 'SSmue': basecutstr = '((Lepton_pdgId[0]*Lepton_pdgId[1]==13*11)||(Lepton_pdgId[0]*Lepton_pdgId[1]==-13*-11))' elif basecut == 'SSee': basecutstr = '((Lepton_pdgId[0]*Lepton_pdgId[1]==11*11)||(Lepton_pdgId[0]*Lepton_pdgId[1]==-11*-11))' elif basecut == 'SSmumu': basecutstr = '((Lepton_pdgId[0]*Lepton_pdgId[1]==13*13)||(Lepton_pdgId[0]*Lepton_pdgId[1]==-13*-13))' cutlist = [basecutstr if x == basecut else x for x in cutlist] file = {} tree = {} effs = {} hist = {} GrAsym = {} yErrorUp = {} yErrorDown = {} totEve = 0 #Compute Eff on different triggers for i, s in enumerate(signals): file[s] = TFile(NTUPLESIG + samples[s]['files'][0] + ".root", "READ") # Read TFile tree[s] = file[s].Get("Events") # Read TTree effs[s] = [0] * (dim + 1) yErrorUp[s] = [0] * (dim + 1) yErrorDown[s] = [0] * (dim + 1) totEve = tree[s].GetEntries(basecutstr) GrAsym[s] = TGraphAsymmErrors() for j, c in enumerate(cutlist): n = tree[s].GetEntries(basecutstr + " && " + cutlist[j]) effs[s][j] = float(n) / (totEve) yErrorUp[s][j] = float( TEfficiency.ClopperPearson(totEve, n, 0.68, True) - effs[s][j]) yErrorDown[s][j] = float( effs[s][j] - TEfficiency.ClopperPearson(totEve, n, 0.68, False)) GrAsym[s].SetPoint(j, j + 0.5, effs[s][j]) # i , exl, exh, eyl, eyh GrAsym[s].SetPointError(j, 0, 0, yErrorUp[s][j], yErrorDown[s][j]) GrAsym[s].SetLineColor(colors[i]) GrAsym[s].SetLineWidth(3) GrAsym[s].SetMarkerStyle(8) GrAsym[s].SetMarkerColor(colors[i]) for k, cs in enumerate(labellist): GrAsym[s].GetHistogram().GetXaxis().Set(dim, 0, dim) GrAsym[s].GetHistogram().GetXaxis().SetBinLabel( k + 1, "%s" % labellist[k]) leg = TLegend(0.7, 0.9 - 0.035 * len(signals), 0.9, 0.9) leg.SetBorderSize(0) leg.SetFillStyle(1001) leg.SetFillColor(0) for i, s in enumerate(signals): leg.AddEntry(GrAsym[s], s, "l") #leg.AddEntry(hist[s], samples[s]['label'][0], "l") c1 = TCanvas("c1", "Signals Acceptance", 800, 600) c1.cd() c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetTicks(1, 1) gStyle.SetOptStat(0) GrAsym[signals[0]].SetMaximum(1.3) GrAsym[signals[0]].SetMinimum(0.) for i, s in enumerate(signals): if i == 0: GrAsym[s].GetHistogram().GetXaxis().SetTitle("Trigg") GrAsym[s].GetHistogram().GetYaxis().SetTitle("Signal Acceptance") #hist[s].GetYaxis().SetRangeUser(0., 1.) GrAsym[s].Draw("pa" if i == 0 else "SAME p") leg.Draw() drawCMS(LUMI, "Work In Progress") drawRegion(basecut) if not os.path.exists('plots/Signal/Acceptance/'): os.system('mkdir -p plots/Signal/Acceptance/') c1.Print("plots/Signal/Acceptance/Acc_" + basecut + ".png") c1.Print("plots/Signal/Acceptance/Acc_" + basecut + ".pdf") #if not options.runBash: raw_input("Press Enter to continue...") pass
def significanceSB(cutlist, labellist): basecut = labellist[0] dim = len(cutlist) significance = [0] * (dim + 1) file = {} tree = {} effs = {} hist = {} GrAsym = {} yErrorUp = {} yErrorDown = {} totEve = 0 GrAsym = TGraphAsymmErrors() cuts = "" for j, c in enumerate(cutlist): s = 0. b = 0. cuts += cutlist[0] if j == 0 else " && " + cutlist[j] print "cuts = ", cuts for num1, v in enumerate(signals): #print "Signal = ", v for num2, filename in enumerate(samples[v]['files']): #print "Signal rootfile read = ", filename file[filename] = TFile(NTUPLESIG + filename + ".root", "READ") # Read TFile tree[filename] = file[filename].Get("Events") # Read TTree nevents = float(sample[filename]['nevents']) xs = float(sample[filename]['xsec']) * float( sample[filename]['kfactor']) LumiMC = nevents / xs Weight = float(LUMI) / float(LumiMC) sig_entries = tree[filename].GetEntries(cuts) #print "s = ", float(sig_entries) * float(Weight) s += float(sig_entries) * float(Weight) print "TOT SIG = ", s for num1, k in enumerate(back): #print "backgrounds = ", k for num2, filename in enumerate(samples[k]['files']): #print "backgrounds rootfile read = ", filename file[filename] = TFile(NTUPLEDIR + filename + ".root", "READ") # Read TFile tree[filename] = file[filename].Get("Events") # Read TTree nevents = float(sample[filename]['nevents']) xs = float(sample[filename]['xsec']) * float( sample[filename]['kfactor']) LumiMC = nevents / xs Weight = float(LUMI) / float(LumiMC) bkg_entries = tree[filename].GetEntries(cuts) #print "b = ", float(bkg_entries) * float(Weight) b += float(bkg_entries) * float(Weight) print "TOT BKG = ", b ##End of cutlist #COMPUTE #print "s = ", s #print "b = ", b #print "sqrt(b) = ", math.sqrt(b) #print "significance = ", float(s/math.sqrt(b)) significance[j] = float(s / math.sqrt(b)) yErrorUp[j] = float( TEfficiency.ClopperPearson(math.sqrt(b), s, 0.68, True) - significance[j]) yErrorDown[j] = float( significance[j] - TEfficiency.ClopperPearson(math.sqrt(b), s, 0.68, False)) GrAsym.SetPoint(j, j + 0.5, significance[j]) GrAsym.SetPointError(j, 0, 0, yErrorUp[j], yErrorDown[j]) for k, cs in enumerate(labellist): GrAsym.GetHistogram().GetXaxis().Set(dim, 0, dim) GrAsym.GetHistogram().GetXaxis().SetBinLabel(k + 1, "%s" % labellist[k]) GrAsym.SetLineColor(2) GrAsym.SetLineWidth(3) GrAsym.SetMarkerStyle(8) GrAsym.SetMarkerColor(2) c1 = TCanvas("c1", "Signals Acceptance", 800, 600) c1.cd() c1.GetPad(0).SetTopMargin(0.06) c1.GetPad(0).SetRightMargin(0.05) c1.GetPad(0).SetTicks(1, 1) gStyle.SetOptStat(0) #GrAsym.SetMaximum(1.3) #GrAsym.SetMinimum(0.) GrAsym.GetHistogram().GetXaxis().SetTitle("") GrAsym.GetHistogram().GetYaxis().SetTitle("Significance (S/#sqrt{B})") GrAsym.Draw("pa") drawCMS(LUMI, "Work In Progress") drawRegion(basecut) if not os.path.exists('plots/Signal/Significance/'): os.system('mkdir -p plots/Signal/Significance/') c1.Print("plots/Signal/Significance/Sigf_SB_" + basecut + ".png") c1.Print("plots/Signal/Significance/Sigf_SB_" + basecut + ".pdf") #if not options.runBash: raw_input("Press Enter to continue...") pass
# Open file tfile = TFile.Open(infile) # ____________________________________________________________________________ # emtf_eff_vs_genpt_l1pt20 hname = "emtf_eff_vs_genpt_l1pt20" h1a_denom = tfile.Get(hname + "_denom") h1a_numer = tfile.Get(hname + "_numer") h1b_denom = tfile.Get(hname2026_f(hname) + "_denom") h1b_numer = tfile.Get(hname2026_f(hname) + "_numer") print h1a_denom.GetEntries(), h1a_numer.GetEntries() print h1b_denom.GetEntries(), h1b_denom.GetEntries() h1a_eff = TEfficiency(h1a_numer, h1a_denom) h1a_eff.SetStatisticOption(0) # kFCP h1a_eff.SetConfidenceLevel(0.682689492137) # one sigma h1a_eff.SetMarkerColor(632) # kRed h1a_eff.SetLineColor(632) # kRed h1a_eff.SetLineWidth(2) h1b_eff = TEfficiency(h1b_numer, h1b_denom) h1b_eff.SetStatisticOption(0) # kFCP h1b_eff.SetConfidenceLevel(0.682689492137) # one sigma h1b_eff.SetMarkerColor(600) # kBlue h1b_eff.SetLineColor(600) # kBlue h1b_eff.SetLineWidth(2) gr = h1a_eff.CreateGraph() gr.Draw("ap")
def fill_ntuple(): print('*** starting fill_ntuple() ') AtlasStyle.SetAtlasStyle() # # get key list # tfile = TFile(BasicConfig.workdir + 'systTree.root') # key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()] # regex = re.compile('PRW|JET|MET.*') # key_list = [key for key in key_list_all if re.match(regex, key)] # tfile.Close() # start making ttree #output_tfile = TFile('rhadron_v06-00-05.root', 'recreate') output_tfile = TFile(args.outputFile, 'recreate') # initialize TTree tree = TTree('rhadron', 'tree of rhadron properties for limit setting') # leaf variables from array import array mass_gluino = array('f', [0.]) delta_mass = array('f', [0.]) ctau = array('f', [0.]) eff = array('f', [0.]) eff_stat_error = array('f', [0.]) eff_syst_error = array('f', [0.]) eff_syst_error_ISR = array('f', [0.]) eff_syst_error_PRW = array('f', [0.]) eff_syst_error_JET = array('f', [0.]) eff_syst_error_MET = array('f', [0.]) # set branch tree.Branch("mGluino", mass_gluino, 'mGluino/F') tree.Branch("deltaM", delta_mass, 'deltaM/F') tree.Branch("ctau", ctau, 'ctau/F') tree.Branch("eff", eff, 'eff/F') tree.Branch("effRelStatErr", eff_stat_error, 'effRelStatErr/F') tree.Branch("effRelSystErr", eff_syst_error, 'effRelSystErr/F') tree.Branch("effRelSystErrISR", eff_syst_error_ISR, 'effRelSystErrISR/F') tree.Branch("effRelSystErrPRW", eff_syst_error_PRW, 'effRelSystErrPRW/F') tree.Branch("effRelSystErrJET", eff_syst_error_JET, 'effRelSystErrJET/F') tree.Branch("effRelSystErrMET", eff_syst_error_MET, 'effRelSystErrMET/F') #directory = '/afs/cern.ch/work/k/kmotohas/DisplacedVertex/DV_xAODAnalysis/submitDir_LSF/mc/hist_DVPlusMETSys/' #directory = BasicConfig.workdir + 'hist_DVPlusMETSys/' #directory = '/home/motohash/data/mc15_13TeV/DVPlusMETSys/v06-00-05/' #tfile = TFile(args.referenceFile) tfile = TFile(args.inputFile) key_list_all = [key.GetName() for key in gDirectory.GetListOfKeys()] print(len(key_list_all), key_list_all) regex = re.compile('Nominal|PRW|JET|MET.*') key_list = [key for key in key_list_all if re.match(regex, key)] print(len(key_list), key_list) tfile.Close() #c = 299792458. # [m/s] #tchains = [[dsid, TChain('Nominal', str(dsid))] for dsid in range(402700, 402740)] #tchains = [[dsid, TChain('Nominal', str(dsid))] for dsid in mc.parameters.keys()] #tchains = [[dsid, [TChain(key, key+str(dsid)) for key in key_list]] for dsid in mc.parameters.keys()] dsids = [args.DSID] tchains = [[dsid, [TChain(key, key + str(dsid)) for key in key_list]] for dsid in dsids] cut_flow = [ 'Initial', 'Trigger', 'Filter', 'Cleaning', 'GRL', 'PV', 'NCB veto', 'MET', 'DV Selection' ] #systematic_tables = TFile('systematic_summary_SimpleMETFilter.root', 'open') #table = TH1F() m_MET_min = 250. # loop over dsid try: for dsid, each_tchain in tchains: print('') print(dsid) #index = 0 #for input in glob(directory + 'systTree_' + str(dsid) + '_*.root'): for tchain in each_tchain: #for input_file in glob(directory+'systTree_mc15_13TeV.' + str(dsid) + '*.root'): # print(input_file) # tchain.Add(input_file) tchain.Add(args.inputFile) mass_gluino[0] = mc.parameters[dsid]['g'] delta_mass[0] = mass_gluino[0] - mc.parameters[dsid]['chi0'] n_reweight_steps = 40 xmin = 1. xmax = 10000. ratio = xmax / xmin bins = [] for ii in range(n_reweight_steps): bins.append( xmax * 10**(ii * TMath.Log10(xmax / xmin) / n_reweight_steps - TMath.Log10(xmax / xmin))) #n_passed_w1 = [0. for _ in range(n_reweight_steps)] #n_passed = [0. for _ in range(n_reweight_steps)] from array import array limitsLifetime = array('d', bins) # tefficiency = [[ TEfficiency('tefficiency_{0}_{1}_{2}'.format(key, step, dsid), ';c#tau [mm]; Event-level efficiency', len(limitsLifetime) - 1, limitsLifetime) for step in range(n_reweight_steps) ] for key in key_list] #h_syst_diff = [[TH1F('syst_diff_{0}_{1}_{2}'.format(key, step, dsid), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list)+1, 0, len(key_list)+1) # for step in range(n_reweight_steps)] for key in key_list] h_syst_diff = [ TH1F('syst_diff_{0}_{1}_{2}'.format(key, step, dsid), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list) + 1, 0, len(key_list) + 1) for step in range(n_reweight_steps) ] for step in range(n_reweight_steps): for jj, key in enumerate(key_list): h_syst_diff[step].GetXaxis().SetBinLabel(jj + 1, key) h_syst_diff[step].GetXaxis().SetBinLabel( len(key_list) + 1, 'ISR_Py2MG_SF_removed') n_events_weighted = [[0. for _ in range(n_reweight_steps)] for key in key_list] n_events_weighted_noISR = [[0. for _ in range(n_reweight_steps)] for key in key_list] # loop over tchain of each systematic for ii, tchain in enumerate(each_tchain): entries = tchain.GetEntries() print('*** processed systs: {0} / {1}'.format( ii, len(each_tchain))) #n_reweight_steps = 50 #for step in range(n_reweight_steps): # tefficiency.append(TEfficiency('tefficiency_'+str(step), ';c#tau [mm]; Event-level efficiency', # len(limitsLifetime)-1, limitsLifetime)) # h_syst_diff.append(TH1F('syst_diff_'+str(step), ';;(N_{shifted} - N_{nominal}) / N_{nominal}', len(key_list)+1, 0, len(key_list)+1)) for step in range(n_reweight_steps): tefficiency[ii][step].SetUseWeightedEvents() #for jj, key in enumerate(key_list): # h_syst_diff[ii][step].GetXaxis().SetBinLabel(jj+1, key) #h_syst_diff[ii][step].GetXaxis().SetBinLabel(len(key_list)+1, 'ISR_Py2MG_SF_removed') # h_syst_diff[step].SetMinimum(-0.3) # h_syst_diff[step].SetMaximum(0.3) if entries == 0: continue for entry in range(entries): #if entry % 1000 == 0: # print('* processed events: {0} / {1}'.format(entry, entries)) utils.show_progress(entry, entries) #if entry == 605: # break # get the next tree in the chain and verify ientry = tchain.LoadTree(entry) if ientry < 0: break # copy next entry into memory and verify nb = tchain.GetEntry(entry) if nb <= 0: continue event_weight = tchain.McEventWeight * tchain.PileupWeight * tchain.ISRWeight ctau_MC = TMath.C( ) * mc.parameters[dsid]['t'] * 1e-9 # [nm]->[m] for step in range(n_reweight_steps): #print(tchain.GetListOfBranches()) pass_all = pass_event_cut(tchain, len(cut_flow) - 1) if pass_all: matched = False for idv in range(len(tchain.DV_x)): matched = matched or match( tchain, idv, cut=1.0) #print('pass_all is ', pass_all, ', matched is ', matched) pass_all = pass_all and matched target_ctau = xmax * 10**( step * TMath.Log10(xmax / xmin) / n_reweight_steps - TMath.Log10(xmax / xmin)) * 1e-3 # [mm]->[m] #print(target_ctau) lifetime_weight = get_lifetime_weight( tchain, target_ctau, ctau_MC) n_events_weighted[ii][ step] += event_weight * lifetime_weight n_events_weighted_noISR[ii][ step] += tchain.McEventWeight * tchain.PileupWeight * lifetime_weight #print(event_weight) #print(event_weight*lifetime_weight) #print(pass_all) tefficiency[ii][step].FillWeighted( pass_all, event_weight * lifetime_weight, target_ctau * 1e3) # end of loop over entries of each TChain # end loop over tchain of each systematic for step in range(n_reweight_steps): n_events_nominal = [0. for _ in range(n_reweight_steps)] for ii in range(len(each_tchain)): # if Nominal TTree, set syst diff of ISR as well if ii == 0: n_events_nominal[step] = n_events_weighted[ii][step] if n_events_nominal[step] < 1e-4: #h_syst_diff[ii][step].SetBinContent(len(key_list)+1, 0) h_syst_diff[step].SetBinContent( len(key_list) + 1, 0) else: #h_syst_diff[ii][step].SetBinContent(len(key_list)+1, h_syst_diff[step].SetBinContent( len(key_list) + 1, float((n_events_weighted_noISR[ii][step] - n_events_nominal[step]) / n_events_nominal[step])) #float((n_events_weighted[ii][step]-n_events_nominal[step])/n_events_nominal[step])) diff = n_events_weighted[ii][step] - n_events_nominal[step] #print(n_events_nominal, n_events_weighted, diff) if n_events_nominal[step] < 1e-4: #h_syst_diff[ii][step].SetBinContent(ii+1, 0) h_syst_diff[step].SetBinContent(ii + 1, 0) else: #h_syst_diff[ii][step].SetBinContent(ii+1, float(diff/n_events_nominal[step])) h_syst_diff[step].SetBinContent( ii + 1, float(diff / n_events_nominal[step])) #systematic_tables.GetObject('systematic_table_'+str(dsid), table) #syst_up, syst_down = root_sum_squares(table, 'x') #systs = root_sum_squares(h_syst_diff[ii][step], 'x') systs = root_sum_squares(h_syst_diff[step], 'x') #eff_syst_error[0] = max(syst_up, syst_down) # TODO #eff_syst_error[0] = (syst_up**2 + syst_down**2)**0.5 #### ############################ eff_syst_error[0] = (systs[0]**2 + systs[1]**2)**0.5 eff_syst_error_ISR[0] = systs[2] eff_syst_error_PRW[0] = systs[3] eff_syst_error_JET[0] = systs[4] eff_syst_error_MET[0] = systs[5] if eff_syst_error[0] > 1: print('eff_syst_error[0] = ' + str(eff_syst_error[0])) #eff_syst_error[0] = 1. #for step in range(n_reweight_steps): #for ct in bins: # print(len(bins), bins) #print(n_total_w1[step], n_total[step]) #sf = n_total_w1[step] / n_total[step] #n_passed[step] *= sf #n_total[step] *= sf #eff_no_weight, stat_error_no_weight = utils.division_error_propagation(n_passed_w1[step], n_total_w1[step]) #ctau[0] = TMath.Power(300, step/float(n_reweight_steps-1)) * 1e-3 # [mm]->[m] ct = bins[step] #print(ct) ctau[0] = ct * 1e-3 # [mm]->[m] #print(ctau[0]) bin_ctau = tefficiency[0][step].GetPassedHistogram().FindBin( ct) print(tefficiency[0][step].GetPassedHistogram().GetBinContent( bin_ctau)) print(tefficiency[0][step].GetTotalHistogram().GetBinContent( bin_ctau)) #print(bin_ctau) #print('ct', ct, 'bin_ctau', bin_ctau) eff[0] = tefficiency[0][step].GetEfficiency(bin_ctau) print(eff[0]) abs_stat_error = ( tefficiency[0][step].GetEfficiencyErrorLow(bin_ctau)**2 + tefficiency[0][step].GetEfficiencyErrorUp(bin_ctau)** 2)**0.5 #eff[0], abs_stat_error = utils.binomial_ratio_and_error(n_passed[step], n_total[step]) #if eff[0] < 1e-4: if eff[0] == 0: eff_stat_error[ 0] = 1. # avoid zero division error and divergence continue # not fill values in tree if efficiency is too small else: eff_stat_error[0] = abs_stat_error / eff[0] #if eff_stat_error[0] > 1: # print(n_passed[step], n_total[step], abs_stat_error, eff[0], eff_stat_error[0]) # eff_stat_error[0] = 1. tree.Fill() # end loop over n_reweight_steps except KeyboardInterrupt: pass output_tfile.Write() output_tfile.Close()
def makeEoverEtrueAnalysis(inputfile, det, et, iseeding, igathering, nevts, outputdir): print '*******************************************' print 'Starting analysis for det={}, Et={}, seed thrs={}, gather thrs={}'.format( det, et, iseeding, igathering) print 'inputfile={}'.format(inputfile) print '*******************************************' result = EoverEtrueAnalysisResult() if not os.path.isfile(inputfile): return False, result ################### # READ ################## histoname = 'h_PFclusters_genMatched_{det}_eOverEtrue_{Et}'.format(det=det, Et=et) inputdir = 'ecalnoisestudy' subdir = 'EtBinnedQuantities' f = TFile(inputfile, 'READ') histo = f.Get('{}/{}/{}'.format(inputdir, subdir, histoname)) if not histo: return False, result histo.SetMarkerStyle(20) histo.GetXaxis().SetTitle( 'E_{{PFcluster}} / E_{{True}} (GeV)'.format(det=det)) histo.GetYaxis().SetTitle('Entries') histo.GetXaxis().SetRangeUser(0, 2) if det == 'EB': histo.GetYaxis().SetRangeUser(0, 500) else: histo.GetYaxis().SetRangeUser(0, 150) histo.Rebin(2) #histo.GetYaxis().SetRangeUser(0., 1600) #if 'EE' in det: # histo.GetYaxis().SetRangeUser(0., 600) # better to avoid setting the range, since the mean calculation changes #histo.GetXaxis().SetRangeUser(xrange[0], xrange[1]) ################### # FIT ################## #f1 = TF1('f1','crystalball',0.4, 2.) #f1.SetParameters(200, 1, 0.05, 3, 2) # my guess: constant (normalization)=integral, mean = 1, sigma = 0.1, alpha (quanto lontano dal picco si innesta la coda) = 0.7, N = 0.5 (lunghezza della coda(?) #f1.SetLineColor(kRed) f1 = TF1('f1', 'gaus', 0.4, 2.) f1.SetParameter(0, 200) f1.SetParameter(1, histo.GetMean()) f1.SetParameter(2, histo.GetRMS()) # do one first fit on the full range fitresult = histo.Fit(f1, 'RM') # L for loglikelihood , mean = f1.GetParameter(1) sigma = f1.GetParameter(2) f1.SetParameter(1, mean) f1.SetParameter(2, sigma) f1.SetRange(mean - 3 * sigma, mean + 3 * sigma) fitresult = histo.Fit(f1, 'SRM') c = TCanvas() histo.Draw('PE') #fitresult = histo.Fit(f1, 'RS') f1.Draw('same') # save later #fo.cd() #fitresult.Write() #fo.Close() # Get the fitted function parameters and write them to txt file #fit_params = [ ('Param {}'.format(i),'{:.2f}'.format(f1.GetParameter(i)), '{:.2f}'.format(f1.GetParError(i)) ) for i in range(0,5)] #ffitout = open(det + '_' + fitoutfile , 'a') #ffitout.write('\n\nFit results for seeding={} gathering={} subdet={}:\n'.format(iseeding, igathering, det)) #ffitout.write('\nChi2/Ndf=' + str(f1.GetChisquare()) + '/' + str(f1.GetNDF()) + '\n') #par_string = '\n'.join("%s: val=%s err=%s" % tup for tup in fit_params) #ffitout.write(par_string) ################### # Efficiency ################## hpass = TH1F('hpass', 'hpass', 1, 0., 1.) #Npass = histo.GetEntries() # compute efficiency only in +-3 sigma fitted peak Npass = histo.Integral(6, histo.FindLastBinAbove(0.)) # 0.2 cut in EoverEtrue for i in range(0, int(Npass)): hpass.Fill(0.5) #htot = f.Get('{}/{}/{}'.format(inputdir,'general', 'h_genP_pt_{d}'.format(d=det))) htot = TH1F('htot', 'htot', 1, 0., 1.) histoname = 'h_genP_{det}_nEvts_{Et}'.format(det=det, Et=et) h_genP = f.Get('{}/{}/{}'.format(inputdir, subdir, histoname)) Ntot = h_genP.GetEntries() for i in range(0, int(Ntot)): htot.Fill(0.5) print 'eff calculated from npass {} over ntot {}'.format( hpass.GetEntries(), htot.GetEntries()) if TEfficiency.CheckConsistency(hpass, htot): pEff = TEfficiency(hpass, htot) # default stat option is clopper pearson eff = pEff.GetEfficiency(1) erru = pEff.GetEfficiencyErrorUp(1) errd = pEff.GetEfficiencyErrorLow(1) else: eff = 1.0 erru = 1.0 errd = 1.0 eff_label = '{:.4f}+/-{:.4f}'.format(eff, erru) # erru and errd are the same #fout = open(det + '_' + outfile, 'a') #fout.write('Total efficiency for seeding={} gathering={}: {} \n'.format(iseeding,igathering,eff_label)) #fout.close() eff_label = 'N_{{reco}}/N_{{gen}}={}'.format(eff_label) defaultLabels([eff_label], x=0.62, y=0.65, spacing=0.04, size=0.06, dx=0.12) sample_label = '#gamma#gamma, no tracker' et_label = 'Et=({},{})GeV'.format(et.split('_')[0], et.split('_')[1]) det_label = 'Region={}'.format(det) defaultLabels([sample_label, et_label, det_label], x=0.25, y=0.85, spacing=0.04, size=0.06, dx=0.12) ################### # RESULTS ################## c.SaveAs('{o}/EoverEtrue_{det}_Et{et}_seed{s}_gather{g}.pdf'.format( o=outputdir, det=det, s=iseeding, g=igathering, et=et)) c.SaveAs('{o}/EoverEtrue_{det}_Et{et}_seed{s}_gather{g}.png'.format( o=outputdir, det=det, s=iseeding, g=igathering, et=et)) return True, EoverEtrueAnalysisResult(det=det, et=et, eff=eff, erreff=erru, mean=f1.GetParameter(1), errmean=f1.GetParError(1), sigma=f1.GetParameter(2), errsigma=f1.GetParError(2), rms=histo.GetRMS(), errrms=histo.GetRMSError(), iseeding=iseeding, igathering=igathering)
def trackmatch(): garanainit.init() #ROOT.gROOT.SetBatch(True) # run in batch mode (don't display plots) tm = ROOT.TreeManager("./test.root") #structuredtree.root") # truth matching utility bt = ROOT.Backtracker(tm) print("constructed treemanager") #tree accessors #gen = tm.GetGenTree() g4 = tm.GetG4Tree() rec = tm.GetRecoTree() nmatch = 0 # number of reco tracks matched to a G4Particle ntrack = 0 # number of reco tracks nprimary = 0 # true number of primaries nprimary_match = 0 #number of primaries matched to at least one track hg4_nmatch = TH1F('hg4_nmatch', 'G4Particle -> Track matching;N^{0} matches', 10, 0, 10) hfrac = TH1F("hfrac", "particle contributing most energy;fraction of total", 50, 0, 1.1) hncont = TH1F("hncont", "particles contributing energy;N^{0} particles", 10, 0, 10) hncontfrac = TH2F( 'hncontfrac', 'particles contributing energy;N^{0} particles;max fraction of total', 10, 0, 25, 0, 1.1) eff_g4match = TEfficiency( 'eff_g4match', 'G4Particle -> Track matching; initial energy [GeV]; efficiency', 10, 0, 5) # loop over events for ientry in range(rec.NEntries()): if ientry % 100 == 0: print('processing event ' + str(ientry)) tm.GetEntry(ientry) bt.FillMaps() #load associations for this entry #nprimary += g4.NPrimary() for ig4 in range(g4.NSim()): if not g4.IsPrimary(ig4): continue # only consider primaries if not abs(g4.PDG(ig4)) == 2212: continue # muons only nprimary += 1 if bt.G4ParticleToTracks(ig4).size() > 0: nprimary_match += 1 # matched to something? hg4_nmatch.Fill(bt.G4ParticleToTracks(ig4).size()) eff_g4match.Fill( bt.G4ParticleToTracks(ig4).size() > 0, g4.SimMomEnter(ig4, 0).E()) ntrack += rec.NTrack() for itrack in range(rec.NTrack()): imatch = bt.TrackToG4Particle(itrack) if imatch < g4.NSim(): nmatch += 1 #imatches = bt.TrackToG4Particles(itrack) hncont.Fill(rec.TrackNTrueTrack(itrack)) #len(imatches)) for ip in range(rec.TrackNTrueTrack(itrack)): hfrac.Fill(rec.TrackMaxDepositFrac(itrack)) hncontfrac.Fill(rec.TrackNTrueTrack(itrack), rec.TrackMaxDepositFrac(itrack)) if nprimary != 0: print('number of primaries found: ', nprimary) print('number of matched primaries: ', nprimary_match) print('g4particle -> track matching efficiency: ', 1.0 * nprimary_match / nprimary) else: print('no G4particles found!') if ntrack != 0: print('track -> g4particle matching efficiency: ', 1.0 * nmatch / ntrack) else: print('no tracks found!') print('hncont size is ', hncont.Integral()) print('hfrac size is ', hfrac.Integral()) cg4_nmatch = TCanvas() hg4_nmatch.Draw() cg4_nmatch.SaveAs('g4_nmatch.png') cncont = TCanvas() hncont.Draw() cncont.SaveAs('ncont.png') cfrac = TCanvas() hfrac.Draw() cfrac.SaveAs('frac.png') cncontfrac = TCanvas() hncontfrac.Draw() cncontfrac.SaveAs('ncont_v_frac.png') ceff_g4match = TCanvas() eff_g4match.Draw() ceff_g4match.SaveAs('g4match_eff.png') # ask for user input as a way to keep the program from ending so we can # see the histogram. # python doesn't like empty input strings so I abuse exception handling try: null = input("press <Enter> to close canvas and exit program.") except: null = 'null'
canv = mkcanvas() for key in keys: name = key.GetName() if not 'Pass' in name: continue if 'Vs' in name: continue hpass = f.Get(name) hall = f.Get(name.replace('Pass', 'All')) histoStyler(hall, kCyan - 8) hall.SetFillColor(hall.GetLineColor()) hall.SetFillStyle(1001) eff = TEfficiency(hpass, hall) eff.SetLineWidth(2) kinvar = name[1:].replace('Pass', '') hframe = hpass.Clone('hframe') hframe.Reset() hframe.GetYaxis().SetRangeUser(0, 1.3) hframe.GetXaxis().SetTitle(kinvar + ' [' + units[kinvar] + ']') hframe.GetYaxis().SetTitle('#epsilon') hframe.SetTitle('') hframe.Draw() # hall.Scale(1.0/hall.Integral(),'width') hall.Scale(1 / hall.GetMaximum()) hall.Draw('hist same')
eff_QMisID_name = (histname + "_Efficiency_" + append_str).replace("Fake","QMisID") eff_fake_scaled_name = (histname + "_Efficiency_" + append_str).replace("Fake","ScaledFake") hist_eff_QMisID.SetName(eff_QMisID_name) hist_eff_fake_scaled.SetName(eff_fake_scaled_name) hists[eff_QMisID_name] = hist_eff_QMisID hists[eff_fake_scaled_name] = hist_eff_fake_scaled # ********************************************************************************************************************************* # 2. # The TEfficiency class handles the special cases not covered by TH1::Divide # t_efficiency = None if TEfficiency.CheckConsistency(hist_pass, hist_tot,"w"): t_efficiency = TEfficiency(hist_pass, hist_tot) t_efficiency.SetName(histname + "_TEfficiency_" + append_str) t_efficiency.SetConfidenceLevel(0.683) # Use TEfficiency, with the frequentist Clopper-Pearson confidence interval at XX% CL (set before) # (this handles the eff = 0, 1 case) # # DOES NOT SEEM TO WORK FOR WEIGHTED HISTOGRAMS --> IT REDUCES TO THE NORMAL APPROX # #t_efficiency.SetStatisticOption(TEfficiency.kFCP) # # Use TEfficiency, with the Bayesian uniform prior at XX% CL (set before) # (This is the same as the TGraphAsymmErrors below) #
messyclean.Draw("mass:D1>>messyclean" + dimensions, cuts, "colz") messycleanhist = gDirectory.Get("messyclean") c.Print(outfilename + ".pdf") c.SetLogz(0) effhist = messyhist.Clone("eff") effhist.Divide(cleanhist) effhistnopickup = messycleanhist.Clone("effnopickup") effhistnopickup.Sumw2() #effhist = messyhist.Clone("eff") effhistnopickup.Divide(cleanhist) effhist.GetZaxis().SetRangeUser(0, 1.0) eff = TEfficiency(messycleanhist, cleanhist) eff.SetName("teff") eff.Write() outfile.Write() effhist.Draw("surf") c.Print(outfilename + ".pdf") effhist.Draw("colz") c.Print(outfilename + ".pdf") effhistnopickup.Draw("surf") c.Print(outfilename + ".pdf") effhistnopickup.Draw("colz")
for tau in gen_taus: value = tau.check_reconstructed() efficiency_pt.Fill(value, tau.visible_pt()) # Fill fake rate histogram rec_taus = taus.get_rec_taus() for tau in rec_taus: fakerate_pt.Fill(tau.check_fake(), tau.pt()) # Files inf = TFile('data/delphes_output.root') outf = TFile('data/rec_efficiency.root', 'RECREATE') # Create histograms efficiency_pt = TEfficiency('efficiency', 'efficiency (pT)', 13, 0, 130) fakerate_pt = TEfficiency('fake rate', 'fake rate (pT)', 13, 0, 130) n_tag = 0 # Count of correctly tagged true taus n_gen = 0 # Count of true taus n_rec = 0 # Count of reconstructed tau jets n_fake = 0 # Count of fake tau jets # Read events tree = inf.Get('events') n_tot = tree.GetEntries() for event in range(n_tot): tree.GetEntry(event) taus = EventTauFinder(tree) # Count taus
def makeEoverEtrueAnalysis(inputfile, eta, et, iseeding, igathering, nevts, outputdir, doCBfit=False): gROOT.SetBatch(True) gROOT.ProcessLine('.L /work/mratti/CMS_style/tdrstyleGraph.C') gROOT.ProcessLine('setTDRStyle()') print '*******************************************' print 'Starting analysis for Eta={}, Et={}, seed thrs={}, gather thrs={}'.format( eta, et, iseeding, igathering) print 'inputfile={}'.format(inputfile) print '*******************************************' result = EoverEtrueAnalysisResult() if not os.path.isfile(inputfile): print 'ERROR: did not find inputfile', inputfile return False, result ################### # READ ################## #histoname = 'h_PFclusters_genMatched_eOverEtrue_Eta{eta}_Et{et}'.format(eta=eta, et=et) #histoname = 'h_superClusters_genMatched_eOverEtrue_Eta{eta}_Et{et}'.format(eta=eta, et=et) histonameFull = histoname + '_Eta{eta}_Et{et}'.format(eta=eta, et=et) inputdir = 'ecalnoisestudy' subdir = 'EtaEtBinnedQuantities' f = TFile(inputfile, 'READ') histo = f.Get('{}/{}/{}'.format(inputdir, subdir, histonameFull)) if not histo: print 'ERROR: did not find histogram', inputdir, subdir, histonameFull return False, result histo.SetMarkerStyle(20) histo.GetXaxis().SetTitle( 'E_{{PFcluster}} / E_{{True}} (GeV)'.format(eta=eta)) histo.GetYaxis().SetTitle('Entries') histo.GetXaxis().SetRangeUser(0, 2) #histo.GetYaxis().SetRangeUser(0,1500) if eta == "0p00_0p50" and et == "1_20": if '450ifb_nominal' in outputdir: histo.Rebin(2) #histo.GetYaxis().SetRangeUser(0., 1600) #if 'EE' in det: # histo.GetYaxis().SetRangeUser(0., 600) # better to avoid setting the range, since the mean calculation changes #histo.GetXaxis().SetRangeUser(xrange[0], xrange[1]) ################### # FIT ################## if doCBfit: # first fit a gaussian starting from reasonable parameters f0 = TF1('f1', 'gaus', 0.4, 2.) #f0.SetParameter(0, 200) f0.SetParameter(1, histo.GetMean()) f0.SetParameter(2, histo.GetRMS()) fitresult = histo.Fit(f0, 'SRM') mean = f0.GetParameter(1) sigma = f0.GetParameter(2) # refit to gaussian, just to make sure f0.SetParameter(1, mean) f0.SetParameter(2, sigma) f0.SetRange(mean - 3 * sigma, mean + 3 * sigma) fitresult = histo.Fit(f0, 'SRM') mean = f0.GetParameter(1) sigma = f0.GetParameter(2) # then restrict yourself to +/- 3 sigma and fit a crystal ball there f1 = TF1('f1', 'crystalball', 0.4, 2.) Nsigma = 4 if (eta == "1p48_2p00" and et != "1_20") or (eta == "2p50_3p00" and et == "40_60"): Nsigma = 3 f1.SetRange(mean - Nsigma * sigma, mean + Nsigma * sigma) f1.SetParameters( 200, 1, 0.05, 3, 2 ) # my guess: constant (normalization)=integral, mean = 1, sigma = 0.1, alpha (quanto lontano dal picco si innesta la coda) = 0.7, N = 0.5 (lunghezza della coda(?) f1.SetLineColor(kRed) fitresult = histo.Fit(f1, 'SRM') # fit it one more time, starting from fitted parameters f1.SetParameters(f1.GetParameters()) fitresult = histo.Fit(f1, 'SRM') # ... and one more time f1.SetParameters(f1.GetParameters()) fitresult = histo.Fit(f1, 'SRM') # ... and one more time f1.SetParameters(f1.GetParameters()) fitresult = histo.Fit(f1, 'SRM') else: # do one first fit on the full range f1 = TF1('f1', 'gaus', 0.4, 2.) f1.SetParameter(0, 200) f1.SetParameter(1, histo.GetMean()) f1.SetParameter(2, histo.GetRMS()) fitresult = histo.Fit(f1, 'SRM') # then set the initial parameters to the fit parameters and restrict to +/- 3 sigma mean = f1.GetParameter(1) sigma = f1.GetParameter(2) f1.SetParameter(1, mean) f1.SetParameter(2, sigma) f1.SetRange(mean - 3 * sigma, mean + 3 * sigma) fitresult = histo.Fit(f1, 'SRM') # then re-set the initial parameters to the fit parameters and restrict to +/- 2 sigma mean = f1.GetParameter(1) sigma = f1.GetParameter(2) f1.SetParameter(1, mean) f1.SetParameter(2, sigma) f1.SetRange(mean - 2 * sigma, mean + 2 * sigma) fitresult = histo.Fit(f1, 'SRM') # mean = f1.GetParameter(1) # sigma = f1.GetParameter(2) # f1.SetParameter(1, mean) # f1.SetParameter(2, sigma) # f1.SetRange(mean-2*sigma, mean+2*sigma) # fitresult = histo.Fit(f1, 'SRM') c = TCanvas() histo.Draw('PE') #fitresult = histo.Fit(f1, 'RS') f1.Draw('same') # save later ################### # Efficiency ################## hpass = TH1F('hpass', 'hpass', 1, 0., 1.) #Npass = histo.GetEntries() #Npass = histo.Integral(6,histo.FindLastBinAbove(0.)) # 0.2 cut in EoverEtrue Npass = histo.Integral(histo.FindBin(0.4), histo.FindBin(1.4)) # does it make sense to instead compute efficiency only in +-3 sigma fitted peak for i in range(0, int(Npass)): hpass.Fill(0.5) htot = TH1F('htot', 'htot', 1, 0., 1.) histonameFull = 'h_genP_nEvts_Eta{eta}_Et{Et}'.format(eta=eta, Et=et) h_genP = f.Get('{}/{}/{}'.format(inputdir, subdir, histonameFull)) Ntot = h_genP.GetEntries() for i in range(0, int(Ntot)): htot.Fill(0.5) print 'eff calculated from npass {} over ntot {}'.format( hpass.GetEntries(), htot.GetEntries()) if TEfficiency.CheckConsistency(hpass, htot): pEff = TEfficiency(hpass, htot) # default stat option is clopper pearson eff = pEff.GetEfficiency(1) erru = pEff.GetEfficiencyErrorUp(1) errd = pEff.GetEfficiencyErrorLow(1) else: eff = 1.0 erru = 1.0 errd = 1.0 eff_label = '{:.4f}+/-{:.4f}'.format(eff, erru) # erru and errd are the same #fout = open(det + '_' + outfile, 'a') #fout.write('Total efficiency for seeding={} gathering={}: {} \n'.format(iseeding,igathering,eff_label)) #fout.close() eff_label = 'N_{{reco}}/N_{{gen}}={}'.format(eff_label) defaultLabels([eff_label], x=0.62, y=0.65, spacing=0.04, size=0.06, dx=0.12) #sample_label = '#gamma#gamma, no tracker' sample_label = anaLabel et_label = 'Et=({},{})GeV'.format(et.split('_')[0], et.split('_')[1]) eta_label = 'Region=({},{})'.format(eta.split('_')[0], eta.split('_')[1]) defaultLabels([sample_label, et_label, eta_label], x=0.25, y=0.85, spacing=0.04, size=0.06, dx=0.12) ################### # RESULTS ################## c.SaveAs('{o}/EoverEtrue_Eta{eta}_Et{et}_s{s}_g{g}.pdf'.format( o=outputdir, eta=eta, s=iseeding, g=igathering, et=et)) c.SaveAs('{o}/EoverEtrue_Eta{eta}_Et{et}_s{s}_g{g}.png'.format( o=outputdir, eta=eta, s=iseeding, g=igathering, et=et)) return True, EoverEtrueAnalysisResult(eta=eta, et=et, eff=eff, erreff=erru, mean=f1.GetParameter(1), errmean=f1.GetParError(1), sigma=f1.GetParameter(2), errsigma=f1.GetParError(2), rms=histo.GetRMS(), errrms=histo.GetRMSError(), iseeding=iseeding, igathering=igathering)