def test_stack(): stack = HistStack() assert_equal(len(stack), 0) stack.Add(Hist(10, 0, 1, fillstyle='solid', color='red')) stack.Add(Hist(10, 0, 1, fillstyle='solid', color='blue')) stack.Add(Hist(10, 0, 1, fillstyle='solid', color='green')) assert_equal(len(stack), 3) stack2 = stack.Clone() assert_equal(stack2[2].linecolor, 'green') # test stacked=True a = Hist(2, 0, 1) b = Hist(2, 0, 1) a.Fill(0.2) b.Fill(0.2) b.Fill(0.8, 5) stack = HistStack([a, b]) assert_equal(stack.min(), 2) assert_equal(stack.max(), 5) # test stacked=False a = Hist(2, 0, 20) b = Hist(2, 10, 20) # binning may be different a.Fill(0.2) b.Fill(15, 5) stack = HistStack([a, b], stacked=False) assert_equal(stack.min(), 0) assert_equal(stack.max(), 5)
def test_overflow_underflow(): h1d = Hist(10, 0, 1) h1d.Fill(-1) h1d.Fill(2) assert_equal(h1d.underflow(), 1) assert_equal(h1d.overflow(), 1) h2d = Hist2D(10, 0, 1, 10, 0, 1) h2d.Fill(-1, .5) h2d.Fill(2, .5) assert_equal(h2d.underflow()[h2d.axis(1).FindBin(.5)], 1) assert_equal(h2d.overflow()[h2d.axis(1).FindBin(.5)], 1) h2d.Fill(.5, -1) h2d.Fill(.5, 2) assert_equal(h2d.underflow(axis=1)[h2d.axis(0).FindBin(.5)], 1) assert_equal(h2d.overflow(axis=1)[h2d.axis(0).FindBin(.5)], 1) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.Fill(-1, .5, .5) h3d.Fill(2, .5, .5) assert_equal(h3d.underflow()[h3d.axis(1).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) assert_equal(h3d.overflow()[h3d.axis(1).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) h3d.Fill(.5, -1, .5) h3d.Fill(.5, 2, .5) assert_equal(h3d.underflow(axis=1)[h3d.axis(0).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) assert_equal(h3d.overflow(axis=1)[h3d.axis(0).FindBin(.5)][h3d.axis(2).FindBin(.5)], 1) h3d.Fill(.5, .5, -1) h3d.Fill(.5, .5, 2) assert_equal(h3d.underflow(axis=2)[h3d.axis(0).FindBin(.5)][h3d.axis(1).FindBin(.5)], 1) assert_equal(h3d.overflow(axis=2)[h3d.axis(0).FindBin(.5)][h3d.axis(1).FindBin(.5)], 1)
def test_kylefix(): a = Hist(10, 0, 1) for w in xrange(1, 11): a.Fill(.5, w) b = kylefix(a) assert_equal(b[0], 5.5)
def create_rootpy_hist(self, vals=None, bins=None, hist=None, norm=None, upperCut=False, htype=''): ''' htype = VALS, ROOT or ROOTPY ''' if htype == "VALS": h_rtpy = Hist(bins) for v in vals: h_rtpy.Fill(v) elif htype == "ROOTPY": h_rtpy = hist elif htype == "ROOT": h_rtpy = asrootpy(hist) else: print "Please provide some histograms..." return None if upperCut: h_rtpy = self.apply_max_cutoff(h_rtpy) if norm: h_rtpy.Scale(norm) return h_rtpy
def test_indexing(): hist = Hist(10, 0, 1) hist.Fill(0.5) assert_equal(hist[6].value, 1) assert_equal(hist[10].value, 0) assert_raises(IndexError, hist.__getitem__, -13) assert_raises(IndexError, hist.__getitem__, 12)
def create_ratioplot(h, xbound_bin=14, ybound_bin=4): nbins_per_slice = 2 # ybound_bin = 4 ybound_val = h.yedges(ybound_bin + 1) h_vert = Hist(h.GetNbinsX() / nbins_per_slice, h.xaxis.min, h.xaxis.max, title=';|#Delta#phi|;High/low ratio', drawstyle='e1', legendstyle='LEP') for i in range(1, h.GetNbinsX() + 1, nbins_per_slice): lo = h.integral(i, i + nbins_per_slice - 1, 1, ybound_bin) hi = h.integral(i, i + nbins_per_slice - 1, ybound_bin + 1, h.GetNbinsY()) ratio, ratio_err = -1, 0. if lo > 0 and hi > 0: ratio = hi / lo ratio_err = ratio * math.sqrt(1 / lo + 1 / hi) h_vert.Fill(h.xedges(i), ratio) h_vert.SetBinError(int(i / nbins_per_slice) + 1, ratio_err) xax = h_vert.xaxis decorate_axis_pi(xax) nbins_per_slice = 2 # xbound_bin = 14 xbound_val = h.xedges(xbound_bin + 1) h_hori = Hist(h.GetNbinsY() / nbins_per_slice, h.yaxis.min, h.yaxis.max, title=';Iso;High/low ratio', drawstyle='e1', legendstyle='LEP') for i in range(1, h.GetNbinsY() + 1, nbins_per_slice): lo = h.integral(1, xbound_bin, i, i + nbins_per_slice - 1) hi = h.integral(xbound_bin + 1, h.GetNbinsX(), i, i + nbins_per_slice - 1) ratio, ratio_err = -1, 0. if lo > 0 and hi > 0: ratio = hi / lo ratio_err = ratio * math.sqrt(1 / lo + 1 / hi) h_hori.Fill(h.yedges(i), ratio) h_hori.SetBinError(int(i / nbins_per_slice) + 1, ratio_err) return h_vert, h_hori, ybound_val, xbound_val
def checkOnMC(unfolding, method): global bins, nbins RooUnfold.SVD_n_toy = 1000 pulls = [] for sub in range(1,9): inputFile2 = File('../data/unfolding_merged_sub%d.root' % sub, 'read') h_data = asrootpy(inputFile2.unfoldingAnalyserElectronChannel.measured.Rebin(nbins, 'measured', bins)) nEvents = inputFile2.EventFilter.EventCounter.GetBinContent(1) lumiweight = 164.5 * 5050 / nEvents # print sub, nEvents h_data.Scale(lumiweight) doUnfoldingSequence(unfolding, h_data, method, '_sub%d' %sub) pull = unfolding.pull_inputErrorOnly() # unfolding.printTable() pulls.append(pull) unfolding.Reset() allpulls = [] for pull in pulls: allpulls.extend(pull) h_allpulls = Hist(100,-30,30) filling = h_allpulls.Fill for entry in allpulls: filling(entry) fit = h_allpulls.Fit('gaus', 'WWS') h_fit = asrootpy(h_allpulls.GetFunction("gaus").GetHistogram()) canvas = Canvas(width=1600, height=1000) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.10) canvas.SetRightMargin(0.05) h_allpulls.Draw() fit.Draw('same') canvas.SaveAs('plots/Pull_allBins_withFit.png') plt.figure(figsize=(16, 10), dpi=100) rplt.errorbar(h_allpulls, label=r'Pull distribution for all bins', emptybins=False) rplt.hist(h_fit, label=r'fit') plt.xlabel('(unfolded-true)/error', CMS.x_axis_title) plt.ylabel('entries', CMS.y_axis_title) plt.title('Pull distribution for all bins', CMS.title) plt.tick_params(**CMS.axis_label_major) plt.tick_params(**CMS.axis_label_minor) plt.legend(numpoints=1) plt.savefig('plots/Pull_allBins.png') #individual bins for bin_i in range(nbins): h_pull = Hist(100,-30,30) for pull in pulls: h_pull.Fill(pull[bin_i]) plt.figure(figsize=(16, 10), dpi=100) rplt.errorbar(h_pull, label=r'Pull distribution for bin %d' % (bin_i + 1), emptybins=False) plt.xlabel('(unfolded-true)/error', CMS.x_axis_title) plt.ylabel('entries', CMS.y_axis_title) plt.title('Pull distribution for bin %d' % (bin_i + 1), CMS.title) plt.savefig('Pull_bin_%d.png' % (bin_i + 1))
def test_indexing(): hist = Hist(10, 0, 1) hist.Fill(0.5) assert_equals(hist[5], 1) assert_equals(hist[9], 0) assert_raises(IndexError, hist.__getitem__, -1) assert_raises(IndexError, hist.__getitem__, 10)
def test_uniform_binning(): a = Hist([0, 10, 100, 10000, 100000]) for v in [5, 20, 200, 20000]: a.Fill(v, gauss(10, 5)) b = uniform_binning(a) assert_equal(list(a), list(b)) assert_equal(list(a.yerrh()), list(b.yerrh()))
def test_zero_negs(): a = Hist(100, 0, 1) for i in xrange(1000): a.Fill(random(), gauss(.3, 5)) assert_equal((np.array(list(a)) < 0).any(), True) b = zero_negs(a) assert_equal((np.array(list(b)) < 0).any(), False)
def test_merge_bins(): h1d = Hist(10, 0, 1) h1d.FillRandom('gaus', 1000) h1d_merged = h1d.merge_bins([(0, -1)]) assert_equal(h1d_merged.nbins(0), 1) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.FillRandom('gaus') h3d_merged = h3d.merge_bins([(1, 3), (-4, -2)], axis=1) assert_equal(h3d.GetEntries(), h3d_merged.GetEntries()) assert_equal(h3d.GetSumOfWeights(), h3d_merged.GetSumOfWeights()) assert_equal(h3d_merged.nbins(1), 6) h = Hist(4, 0, 1) h.Fill(-1) h.Fill(2) h.Fill(0.8, 2) h.Fill(0.5, 1) # invalid ranges assert_raises(ValueError, h.merge_bins, [(0, 0)]) assert_raises(ValueError, h.merge_bins, [(0, 1, 2)]) assert_raises(ValueError, h.merge_bins, [(2, 1)]) assert_raises(ValueError, h.merge_bins, [(-2, 1)]) # overlapping windows assert_raises(ValueError, h.merge_bins, [(0, 1), (1, 2)]) assert_equal(list(h.y(overflow=True)), [1., 0., 0., 1., 2., 1.]) assert_equal(list(h.merge_bins([(1, 2)]).y(overflow=True)), [1., 0., 1., 2., 1.]) assert_equal(list(h.merge_bins([(-3, -2)]).y(overflow=True)), [1., 0., 0., 3., 1.]) assert_equal(list(h.merge_bins([(-2, -1)]).y(overflow=True)), [1., 0., 0., 1., 3., 0.]) assert_equal(list(h.merge_bins([(0, 1), (-2, -1)]).y(overflow=True)), [0., 1., 0., 1., 3., 0.]) assert_equal( list(h.merge_bins([(0, 1)]).merge_bins([(-2, -1)]).y(overflow=True)), [0., 1., 0., 1., 3., 0.]) assert_equal(list(h.merge_bins([(1, 2), (3, 4)]).y(overflow=True)), [1., 0., 3., 1.])
def test_cmstdr(): style = get_style('CMSTDR') with style: hpx = Hist(100, -4, 4, name="hpx", title="This is the px distribution") ROOT.gRandom.SetSeed() for i in xrange(1000): hpx.Fill(ROOT.gRandom.Gaus()) hpx.GetXaxis().SetTitle("random variable [unit]") hpx.GetYaxis().SetTitle("#frac{dN}{dr} [unit^{-1}]") hpx.SetMaximum(100.) hpx.Draw() labels.CMS_label("Testing 2050", sqrts=100)
def test_atlas(): style = get_style('ATLAS') with style: hpx = Hist(100, -4, 4, name="hpx", title="This is the px distribution") ROOT.gRandom.SetSeed() for i in xrange(1000): hpx.Fill(ROOT.gRandom.Gaus()) hpx.GetXaxis().SetTitle("random variable [unit]") hpx.GetYaxis().SetTitle("#frac{dN}{dr} [unit^{-1}]") hpx.SetMaximum(80.) hpx.Draw() ATLAS_label(.4, .8)
def test_lhcb(): style = get_style('LHCb') with style: canvas = Canvas() hpx = Hist(100, -4, 4, name="hpx", title="This is the px distribution") ROOT.gRandom.SetSeed() for i in xrange(1000): hpx.Fill(ROOT.gRandom.Gaus()) hpx.GetXaxis().SetTitle("random variable [unit]") hpx.GetYaxis().SetTitle("#frac{dN}{dr} [unit^{-1}]") hpx.SetMaximum(80.) hpx.Draw() LHCb_label("R", "preliminary") if INTERACTIVE: wait()
def rate_test(et_values, event_runs, et_cuts): events = {} for et, (run_event) in zip(et_values, event_runs): event = tuple(run_event) if event in events: if et > events[event]: events[event] = et else: events[event] = et histo = Hist(256, -0.5, 255.5) for event, et in events.items(): histo.Fill(et) total_events = histo.integral(overflow=True) rates = [] for et_cut in et_cuts: bin = histo.GetXaxis().FindBin(et_cut) pass_events = histo.integral(bin, overflow=True) rates.append([ et_cut, float(pass_events) / float(total_events) * nbunches * c_light / lhc_length * 1.e-3 ]) return np.array(rates, dtype=np.float64)
from rootpy.extern.six.moves import range try: kwargs = {} for arg in extra: name, value = arg.lstrip('--').split('=') kwargs[name] = value except ValueError: print("specify style parameters with --name=value") try: style = get_style(args.style, **kwargs) except ValueError: print('Invalid style: `{0}`. Using the `ATLAS` style.'.format(args.style)) style = get_style('ATLAS') # Use styles as context managers. The selected style will only apply # within the following context: with style: c = Canvas() hpx = Hist(100, -4, 4, name="hpx", title="This is the px distribution") # generate some random data ROOT.gRandom.SetSeed() for i in range(25000): hpx.Fill(ROOT.gRandom.Gaus()) hpx.GetXaxis().SetTitle("random variable [unit]") hpx.GetYaxis().SetTitle("#frac{dN}{dr} [unit^{-1}]") hpx.SetMaximum(1000.) hpx.Draw() wait()
import ROOT ROOT.gROOT.SetBatch(True) from higgstautau.tauid import uncertainty from rootpy.plotting import Hist, Canvas from matplotlib import pyplot as plt from rootpy.plotting import root2matplotlib as rplt nominal = Hist(50, 0, 1, title='nominal') high = nominal.Clone(title='high') low = nominal.Clone(title='low') high.linecolor = 'red' low.linecolor = 'blue' for weight, event in tauid_sample.iter(): high_score, low_score = uncertainty(event.tau1_BDTJetScore, pt, event.tau1_numTrack, event.number_of_good_vertices) nominal.Fill(event.tau1_BDTJetScore, weight) low.Fill(low_score, weight) high.Fill(high_score, weight) fig = plt.figure() rplt.hist(nominal, histtype='stepfilled') rplt.hist(high, histtype='stepfilled') rplt.hist(low, histtype='stepfilled') plt.xlabel('BDT Score') plt.ylabel('Events') plt.legend(loc='upper left') plt.savefig('tauid_test.png')
class JtUnfolder(object): def __init__(self, name, **kwargs): self._name = name print("Create Unfolder {}".format(name)) self._Njets = kwargs.get("Njets", 0) self._fEff = None self._jetBinBorders = kwargs.get("jetBinBorders", None) if self._jetBinBorders is not None: self._jetPtBins = [ (a, b) for a, b in zip(self._jetBinBorders, self._jetBinBorders[1:]) ] self._fakeRate = kwargs.get("fakeRate", 1) self._randomSeed = kwargs.get("randomSeed", 123) self._weight = kwargs.get("weight", True) self._fillFakes = kwargs.get("fillFakes", False) self._NBINSJt = kwargs.get("NBINSJt", 64) self._NBINS = kwargs.get("NBINS", 64) self._responseJetPt = None self._responseJetPtCoarse = None self._IsData = kwargs.get("Data", False) self._hJtTrue2D = None self._Niter = kwargs.get("Iterations", 4) self._NFin = kwargs.get("NFin", 0) self._hBgJt = None self._hBgJtNormalized = None self._numberBackgroundBin = None def fill_jt_histogram(self, histo, jt, pt): if self._weight: histo.Fill(jt, pt, 1.0 / jt) else: histo.Fill(jt, pt) def setTrackMatch(self, hists): self._matching = hists def setJtBins(self, bins): self._LogBinsJt = bins def setPtBins(self, bins): self._LogBinsX = bins def setNbinsJt(self, nbins): self._NBINSJt = nbins def setNbinsPt(self, nbins): self._NBINS = nbins def setRandomSeed(self, seed): self._randomSeed = seed def setJtTestMeas2D(self, hJtMeas): self._hJtTestMeas2D = hJtMeas def setJtTestTrue2D(self, hJtTrue): self._hJtTestTrue2D = hJtTrue def setJtMeas2D(self, hJtMeas): self._hJtMeas2D = hJtMeas def setJtTrue2D(self, hJtTrue): self._hJtTrue2D = hJtTrue def setJetPtMeas(self, hPtMeas): self._hJetPtMeas = hPtMeas def setJetPtTrue(self, hPtTrue): self._hJetPtTrue = hPtTrue def setJetPtMeasCoarse(self, hPtMeas): self._hJetPtMeasCoarse = hPtMeas def setJetPtTrueCoarse(self, hPtTrue): self._hJetPtTrueCoarse = hPtTrue def setFakeRate(self, rate): self._fakeRate = rate def setWeight(self, weight): self._weight = weight def setMisses2D(self, misses): self._misses2D = misses def setFakes2D(self, fakes): self._hJtFake2D = fakes def setJetPtResponseHisto(self, hresponse): self._hresponseJetPt = hresponse def setJtBackground(self, background): self._hBgJt = background if self._numberBackgroundBin is not None: self._hBgJtNormalized = normalize(self._hBgJt, self._numberBackgroundBin) print("Jt background normalized") def setJtBackgroundNumbers(self, n): self._numberBackgroundBin = n if self._hBgJt is not None: self._hBgJtNormalized = normalize(self._hBgJt, self._numberBackgroundBin) print("Jt background normalized") def setJetPtResponseCoarseHisto(self, hresponse): self._hresponseJetPtCoarse = hresponse def setJetPtResponse(self, response): self._responseJetPt = response def setJetPtResponseCoarse(self, response): self._responseJetPtCoarse = response def setNumberJetsMeas(self, n): self._numberJetsMeasBin = n def setNumberJetsTestMeas(self, n): self._numberJetsTestMeasBin = n def setNumberJetsTrue(self, n): self._numberJetsTrueBin = n def setNumberJetsTestTrue(self, n): self._numberJetsTestTrueBin = n def setNumberJetsMeasTrain(self, n): self._numberJetsMeasTrain = n def set2Dresponse(self, responses): self._2Dresponses = responses def drawTrackMatch(self, name, option): drawing.drawMatchHisto(self._matching, self._jetPtBins, name, option) def createToyTraining(self, rootFile, numberEvents): self._f = root_open(rootFile, "read") self._hJetPtIn = self._f.Get( "AliJJetJtTask/AliJJetJtHistManager/JetPt/JetPtNFin{:02d}".format( 2)) self._hZIn = self._f.Get( "AliJJetJtTask/AliJJetJtHistManager/Z/ZNFin{:02d}".format(2)) LimL = 0.1 LimH = 500 logBW = (TMath.Log(LimH) - TMath.Log(LimL)) / self._NBINS self._LogBinsX = [ LimL * math.exp(ij * logBW) for ij in range(0, self._NBINS + 1) ] self._hJetPtMeas = Hist(self._LogBinsX) self._hJetPtTrue = Hist(self._LogBinsX) self._myRandom = TRandom3(self._randomSeed) if self._fEff is None: self._fEff = TF1("fEff", "1-0.5*exp(-x)") if self._jetBinBorders is None: self._jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500] self._hJetPtMeasCoarse = Hist(self._jetBinBorders) self._hJetPtTrueCoarse = Hist(self._jetBinBorders) low = 0.01 high = 10 BinW = (TMath.Log(high) - TMath.Log(low)) / self._NBINSJt self._LogBinsJt = [ low * math.exp(i * BinW) for i in range(self._NBINSJt + 1) ] self._jetBinBorders = self._jetBinBorders self._jetPtBins = [ (a, b) for a, b in zip(self._jetBinBorders, self._jetBinBorders[1:]) ] self._hJtTrue2D = Hist2D(self._LogBinsJt, self._jetBinBorders) self._hJtMeas2D = Hist2D(self._LogBinsJt, self._jetBinBorders) self._hJtFake2D = Hist2D(self._LogBinsJt, self._jetBinBorders) # Histogram to store jT with respect to the leading hadron self._hJtTestMeas2D = Hist2D( self._LogBinsJt, self._jetBinBorders) # Needs a better name self._hJtTestTrue2D = Hist2D( self._LogBinsJt, self._jetBinBorders) # Needs a better name self._responseJetPt = RooUnfoldResponse(self._hJetPtMeas, self._hJetPtTrue) self._responseJetPtCoarse = RooUnfoldResponse(self._hJetPtMeasCoarse, self._hJetPtTrueCoarse) self._hresponseJetPt = Hist2D(self._jetBinBorders, self._jetBinBorders) self._hresponseJetPtCoarse = Hist2D(self._jetBinBorders, self._jetBinBorders) # Histogram index is jet pT index, Bin 0 is 5-10 GeV # Histogram X axis is observed jT, Bin 0 is underflow # Histogram Y axis is observed jet Pt, Bin 0 is underflow # Histogram Z axis is True jT, Bin 0 is underflow self._2Dresponses = [ Hist3D(self._LogBinsJt, self._jetBinBorders, self._LogBinsJt) for i in self._jetPtBins ] self._misses2D = Hist2D(self._LogBinsJt, self._jetBinBorders) self._fakes2D = Hist2D(self._LogBinsJt, self._jetBinBorders) self._numberJetsMeasBin = [0 for i in self._jetBinBorders] self._numberJetsTrueBin = [0 for i in self._jetBinBorders] self._numberJetsTestMeasBin = [0 for i in self._jetBinBorders] self._numberJetsTestTrueBin = [0 for i in self._jetBinBorders] self._numberFakesBin = [0 for i in self._jetBinBorders] ieout = numberEvents / 10 if ieout > 20000: ieout = 20000 start_time = datetime.now() print("Processing MC Training Events") for ievt in range(numberEvents): tracksTrue = [] tracksMeas = [0 for x in range(100)] if ievt % ieout == 0 and ievt > 0: time_elapsed = datetime.now() - start_time time_left = timedelta(seconds=time_elapsed.total_seconds() * 1.0 * (numberEvents - ievt) / ievt) print("Event {} [{:.2f}%] Time Elapsed: {} ETA: {}".format( ievt, 100.0 * ievt / numberEvents, fmtDelta(time_elapsed), fmtDelta(time_left), )) jetTrue = TVector3(0, 0, 0) jetMeas = TVector3(0, 0, 0) jetPt = self._hJetPtIn.GetRandom() remainder = jetPt if jetPt < 5: continue nt = 0 nt_meas = 0 while remainder > 0: trackPt = self._hZIn.GetRandom() * jetPt if trackPt < remainder: track = TVector3() remainder = remainder - trackPt else: trackPt = remainder remainder = -1 if trackPt > 0.15: track.SetPtEtaPhi( trackPt, self._myRandom.Gaus(0, 0.1), self._myRandom.Gaus(math.pi, 0.2), ) tracksTrue.append(track) jetTrue += track if self._fEff.Eval(trackPt) > self._myRandom.Uniform(0, 1): tracksMeas[nt] = 1 jetMeas += track nt_meas += 1 else: tracksMeas[nt] = 0 nt += 1 fakes = [] for it in range(self._fakeRate * 100): if self._myRandom.Uniform(0, 1) > 0.99: fake = TVector3() fake.SetPtEtaPhi( self._myRandom.Uniform(0.15, 1), self._myRandom.Gaus(0, 0.1), self._myRandom.Gaus(math.pi, 0.2), ) fakes.append(fake) jetMeas += fake self._responseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt()) self._responseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt()) self._hresponseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt()) self._hresponseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt()) ij_meas = GetBin(self._jetBinBorders, jetMeas.Pt()) ij_true = GetBin(self._jetBinBorders, jetTrue.Pt()) if nt < 5 or nt_meas < 5: continue if ij_meas >= 0: self._numberJetsMeasBin[ij_meas] += 1 if ij_true >= 0: self._numberJetsTrueBin[ij_true] += 1 for track, it in zip(tracksTrue, range(100)): zTrue = (track * jetTrue.Unit()) / jetTrue.Mag() jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag() if ij_meas >= 0: if tracksMeas[it] == 1: zMeas = (track * jetMeas.Unit()) / jetMeas.Mag() jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag() self._2Dresponses[ij_true].Fill( jtMeas, jetMeas.Pt(), jtTrue) else: self._misses2D.Fill(jtTrue, jetTrue.Pt()) if ij_meas >= 0: for fake in fakes: zFake = (fake * jetMeas.Unit()) / jetMeas.Mag() jtFake = (fake - scaleJet(jetMeas, zFake)).Mag() if self._weight: self._hJtFake2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake) else: self._hJtFake2D.Fill(jtFake, jetMeas.Pt()) if self._fillFakes: self._fakes2D.Fill(jtFake, jetMeas.Pt()) print("Event {} [{:.2f}%] Time Elapsed: {}".format( numberEvents, 100.0, fmtDelta(time_elapsed))) self._numberJetsMeasTrain = sum(self._numberJetsMeasBin) def createToyData(self, rootFile, numberEvents): self._f = root_open(rootFile, "read") self._hJetPtIn = self._f.Get( "AliJJetJtTask/AliJJetJtHistManager/JetPt/JetPtNFin{:02d}".format( 2)) self._hZIn = self._f.Get( "AliJJetJtTask/AliJJetJtHistManager/Z/ZNFin{:02d}".format(2)) if self._fEff is None: self._fEff = TF1("fEff", "1-0.5*exp(-x)") if self._jetBinBorders is None: self._jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500] self._hMultiTrue = Hist(50, 0, 50) self._hMultiMeas = Hist(50, 0, 50) self._hZMeas = Hist(50, 0, 1) self._hZTrue = Hist(50, 0, 1) self._hZFake = Hist(50, 0, 1) self._numberJetsMeasBin = [0 for i in self._jetBinBorders] self._numberJetsTrueBin = [0 for i in self._jetBinBorders] print("Create testing data") start_time = datetime.now() numberEvents = numberEvents ieout = numberEvents / 10 jtLeadingMeas = 0 if ieout > 20000: ieout = 20000 for ievt in range(numberEvents): tracksTrue = [] tracksMeas = [0 for x in range(100)] if ievt % ieout == 0 and ievt > 0: time_elapsed = datetime.now() - start_time time_left = timedelta(seconds=time_elapsed.total_seconds() * 1.0 * (numberEvents - ievt) / ievt) print("Event {} [{:.2f}%] Time Elapsed: {} ETA: {}".format( ievt, 100.0 * ievt / numberEvents, fmtDelta(time_elapsed), fmtDelta(time_left), )) jetTrue = TVector3(0, 0, 0) jetMeas = TVector3(0, 0, 0) jetPt = self._hJetPtIn.GetRandom() remainder = jetPt if jetPt < 5: continue nt = 0 nt_meas = 0 leading = None leadingPt = 0 while remainder > 0: trackPt = self._hZIn.GetRandom() * jetPt if trackPt < remainder: track = TVector3() remainder = remainder - trackPt else: trackPt = remainder remainder = -1 if trackPt > 0.15: track.SetPtEtaPhi( trackPt, self._myRandom.Gaus(0, 0.1), self._myRandom.Gaus(math.pi, 0.2), ) if track.Pt() > leadingPt: leading = track leadingPt = leading.Pt() tracksTrue.append(track) jetTrue += track if self._fEff.Eval(trackPt) > self._myRandom.Uniform(0, 1): tracksMeas[nt] = 1 jetMeas += track nt_meas += 1 else: tracksMeas[nt] = 0 nt += 1 fakes = [] if leadingPt > 0.25 * jetPt: doLeading = True else: doLeading = False for it in range(self._fakeRate * 100): if self._myRandom.Uniform(0, 1) > 0.99: fake = TVector3() fake.SetPtEtaPhi( self._myRandom.Uniform(0.15, 1), self._myRandom.Gaus(0, 0.1), self._myRandom.Gaus(math.pi, 0.2), ) fakes.append(fake) jetMeas += fake self._hJetPtMeas.Fill(jetMeas.Pt()) self._hJetPtTrue.Fill(jetTrue.Pt()) self._hMultiTrue.Fill(nt) self._hMultiMeas.Fill(nt_meas) ij_meas = GetBin(self._jetBinBorders, jetMeas.Pt()) ij_true = GetBin(self._jetBinBorders, jetTrue.Pt()) if nt < 5 or nt_meas < 5: continue if ij_meas >= 0: self._numberJetsMeasBin[ij_meas] += 1 self._hJetPtMeasCoarse.Fill(jetMeas.Pt()) if doLeading: self._numberJetsTestMeasBin[ij_meas] += 1 if ij_true >= 0: self._numberJetsTrueBin[ij_true] += 1 self._hJetPtTrueCoarse.Fill(jetTrue.Pt()) if doLeading: self._numberJetsTestTrueBin[ij_true] += 1 for track, it in zip(tracksTrue, range(100)): zTrue = (track * jetTrue.Unit()) / jetTrue.Mag() jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag() self._hZTrue.Fill(zTrue) if track.Pt() < 0.95 * leadingPt and doLeading: zLeadingTrue = (track * leading.Unit()) / leading.Mag() jtLeadingTrue = (track - scaleJet(leading, zLeadingTrue)).Mag() if ij_true >= 0: self.fill_jt_histogram(self._hJtTrue2D, jtTrue, jetTrue.Pt()) if (track.Pt() < 0.95 * leading.Pt()) and doLeading: self.fill_jt_histogram(self._hJtTestTrue2D, jtLeadingTrue, jetTrue.Pt()) if ij_meas >= 0 and tracksMeas[it] == 1: zMeas = (track * jetMeas.Unit()) / jetMeas.Mag() jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag() if track.Pt() < 0.95 * leading.Pt(): zLeadingMeas = (track * leading.Unit()) / leading.Mag() jtLeadingMeas = ( track - scaleJet(leading, zLeadingMeas)).Mag() self._hZMeas.Fill(zMeas) self.fill_jt_histogram(self._hJtMeas2D, jtMeas, jetMeas.Pt()) if (track.Pt() < 0.95 * leadingPt and doLeading and jtLeadingMeas > 0): self.fill_jt_histogram(self._hJtTestMeas2D, jtLeadingMeas, jetMeas.Pt()) if ij_meas < 0: continue for fake in fakes: zFake = (fake * jetMeas.Unit()) / jetMeas.Mag() jtFake = (fake - scaleJet(jetMeas, zFake)).Mag() zLeadingFake = (fake * leading.Unit()) / leading.Mag() jtLeadingFake = (fake - scaleJet(leading, zLeadingFake)).Mag() self._hZMeas.Fill(zFake) self._hZFake.Fill(zFake) self.fill_jt_histogram(self._hJtMeas2D, jtFake, jetMeas.Pt()) self.fill_jt_histogram(self._hJtTestMeas2D, jtLeadingFake, leadingPt) # self.fill_jt_histogram(self._hJtFake2D, jtFake,jetMeas.Pt()) time_elapsed = datetime.now() - start_time print("Event {} [{:.2f}%] Time Elapsed: {}".format( numberEvents, 100.0, fmtDelta(time_elapsed))) def unfold(self): if self._fillFakes: fakes = self._hJtFake2D else: fakes = None if self._hJtTrue2D: self._response2D = make2Dresponse( self._2Dresponses, self._jetPtBins, self._hJtMeas2D, self._hJtTrue2D, misses=self._misses2D, fakes=fakes, ) else: self._response2D = make2Dresponse( self._2Dresponses, self._jetPtBins, self._hJtMeas2D, self._hJtMeas2D, misses=self._misses2D, fakes=fakes, ) if not self._fillFakes: print("Scale hJtFake2D by {}".format(1.0 * sum(self._numberJetsMeasBin) / self._numberJetsMeasTrain)) self._hJtFake2D.Scale(1.0 * sum(self._numberJetsMeasBin) / self._numberJetsMeasTrain) self._hJtMeas2D.Add(self._hJtFake2D, -1) self._hJtFakeProjBin = [ makeHist( self._hJtFake2D.ProjectionX("histFake{}".format(i), i, i), bins=self._LogBinsJt, ) for i in range(1, len(self._jetBinBorders)) ] for h, nj in zip(self._hJtFakeProjBin, self._numberJetsMeasBin): if nj > 0: h.Scale(1.0 / nj, "width") if self._responseJetPtCoarse is None: print(self._responseJetPtCoarse) self._responseJetPtCoarse = createResponse( self._hJetPtMeasCoarse, self._hresponseJetPtCoarse) if self._responseJetPt is None: self._responseJetPt = createResponse(self._hJetPtMeas, self._hresponseJetPt) self._hJetPtRecoCoarse = unfoldJetPt(self._hJetPtMeasCoarse, self._responseJetPtCoarse, self._jetBinBorders) self._hJetPtReco = unfoldJetPt(self._hJetPtMeas, self._responseJetPt, self._LogBinsX) self._numberJetsMeasFromHist = [ self._hJetPtMeasCoarse.GetBinContent(i) for i in range(1, self._hJetPtMeasCoarse.GetNbinsX() + 1) ] if not self._IsData: self._numberJetsTrueFromHist = [ self._hJetPtTrueCoarse.GetBinContent(i) for i in range(1, self._hJetPtTrueCoarse.GetNbinsX() + 1) ] self._numberJetsFromReco = [ self._hJetPtRecoCoarse.GetBinContent(i) for i in range(1, self._hJetPtRecoCoarse.GetNbinsX()) ] self.printJetNumbers() unfold2D = RooUnfoldBayes(self._response2D, self._hJtMeas2D, self._Niter) self._hJtReco2D = make2DHist(unfold2D.Hreco(), xbins=self._LogBinsJt, ybins=self._jetBinBorders) self._hJtMeasBin = [ makeHist( self._hJtMeas2D.ProjectionX("histMeas{}".format(i), i, i), bins=self._LogBinsJt, ) for i in range(1, len(self._jetBinBorders)) ] if not self._IsData: self._hJtTestMeasBin = [ makeHist( self._hJtTestMeas2D.ProjectionX("histTestMeas{}".format(i), i, i), bins=self._LogBinsJt, ) for i in range(1, len(self._jetBinBorders)) ] self._hJtTrueBin = [ makeHist( self._hJtTrue2D.ProjectionX("histMeas{}".format(i), i, i), bins=self._LogBinsJt, ) for i in range(1, len(self._jetBinBorders)) ] self._hJtTestTrueBin = [ makeHist( self._hJtTestTrue2D.ProjectionX("histMeas{}".format(i), i, i), bins=self._LogBinsJt, ) for i in range(1, len(self._jetBinBorders)) ] self._hJtRecoBin = [ makeHist( self._hJtReco2D.ProjectionX("histMeas{}".format(i), i, i), bins=self._LogBinsJt, ) for i in range(1, len(self._jetBinBorders)) ] for h, n, in zip( self._hJtMeasBin, self._numberJetsMeasFromHist, ): if n > 0: h.Scale(1.0 / n, "width") if not self._IsData: for h, h2, h3, n, n2, n3 in zip( self._hJtTrueBin, self._hJtTestTrueBin, self._hJtTestMeasBin, self._numberJetsTrueFromHist, self._numberJetsTestTrueBin, self._numberJetsTestMeasBin, ): if n > 0: h.Scale(1.0 / n, "width") if n2 > 0: h2.Scale(1.0 / n2, "width") if n3 > 0: h3.Scale(1.0 / n3, "width") for h, n in zip(self._hJtRecoBin, self._numberJetsFromReco): if n > 0: h.Scale(1.0 / n, "width") def plotJetPt(self): if self._IsData: drawing.drawJetPt( self._hJetPtMeas, None, self._hJetPtReco, filename="JetPtUnfoldedData.pdf", ) drawing.drawJetPt( self._hJetPtMeasCoarse, None, self._hJetPtRecoCoarse, filename="JetPtCoarseUnfoldedData.pdf", ) else: drawing.drawJetPt( self._hJetPtMeas, self._hJetPtTrue, self._hJetPtReco, filename="JetPtUnfolded.pdf", ) drawing.drawJetPt( self._hJetPtMeasCoarse, self._hJetPtTrueCoarse, self._hJetPtRecoCoarse, filename="JetPtCoarseUnfolded.pdf", ) def plotJt(self, filename, **kwargs): nRebin = kwargs.get("Rebin", 1) histlist = [self._hJtMeasBin, self._hJtRecoBin, self._hJtFakeProjBin] if not self._IsData: histlist.append(self._hJtTrueBin) if self._hBgJtNormalized is not None: histlist.append(self._hBgJtNormalized) bg = self._hBgJtNormalized else: bg = None if nRebin > 0: for hists in histlist: for h in hists: h.Rebin(nRebin) h.Scale(1.0 / nRebin) if self._IsData: drawing.draw8gridcomparison( self._hJtMeasBin, None, self._jetPtBins, xlog=True, ylog=True, name="{}.pdf".format(filename), unf2d=self._hJtRecoBin, start=4, stride=1, backgroundJt=bg, ) drawing.draw8gridcomparison( self._hJtMeasBin, None, self._jetPtBins, xlog=True, ylog=True, name="{}_Extra.pdf".format(filename), unf2d=self._hJtRecoBin, start=0, stride=1, backgroundJt=bg, ) else: drawing.draw8gridcomparison( self._hJtMeasBin, self._hJtTrueBin, self._jetPtBins, xlog=True, ylog=True, name="{}.pdf".format(filename), unf2d=self._hJtRecoBin, fake=self._hJtFakeProjBin, start=4, stride=1, ) drawing.draw8gridcomparison( self._hJtMeasBin, self._hJtTrueBin, self._jetPtBins, xlog=True, ylog=True, name="{}_Extra.pdf".format(filename), unf2d=self._hJtRecoBin, fake=self._hJtFakeProjBin, start=0, stride=1, ) def plotLeadingJtComparison(self, filename, **kwargs): nRebin = kwargs.get("Rebin", 1) histlist = [self._hJtTestTrueBin, self._hJtTrueBin, self._hJtMeasBin] if nRebin > 1: for hists in histlist: for h in hists: h.Rebin(nRebin) h.Scale(1.0 / nRebin) drawing.draw8gridcomparison( self._hJtMeasBin, self._hJtTrueBin, self._jetPtBins, xlog=True, ylog=True, name="{}.pdf".format(filename), leadingJt=self._hJtTestTrueBin, start=2, stride=1, ) drawing.draw8gridcomparison( self._hJtMeasBin, self._hJtTrueBin, self._jetPtBins, xlog=True, ylog=True, name="{}_Extra.pdf".format(filename), leadingJt=self._hJtTestTrueBin, start=0, stride=1, ) def plotResponse(self): fig, axs = plt.subplots(2, 1, figsize=(10, 10)) axs = axs.reshape(2) for ax, h in zip(axs, (self._responseJetPt, self._response2D)): hResponse = h.Hresponse() xbins = [ hResponse.GetXaxis().GetBinLowEdge(iBin) for iBin in range(1, hResponse.GetNbinsX() + 2) ] ybins = [ hResponse.GetYaxis().GetBinLowEdge(iBin) for iBin in range(1, hResponse.GetNbinsY() + 2) ] drawing.drawResponse( ax, make2DHist(hResponse, xbins=xbins, ybins=ybins)) plt.show() # Draw figure on screen fig, ax = plt.subplots(1, 1, figsize=(10, 10)) hResponse = self._responseJetPt.Hresponse() xbins = [ hResponse.GetXaxis().GetBinLowEdge(iBin) for iBin in range(1, hResponse.GetNbinsX() + 2) ] ybins = [ hResponse.GetYaxis().GetBinLowEdge(iBin) for iBin in range(1, hResponse.GetNbinsY() + 2) ] drawing.drawPtResponse(ax, make2DHist(hResponse, xbins=xbins, ybins=ybins)) plt.savefig("JetPtResponse", format="pdf") # Save figure plt.show() def printJetNumbers(self): print("Measured jets by bin") print(self._numberJetsMeasBin) print(self._numberJetsMeasFromHist) if not self._IsData: print("True jets by bin") print(self._numberJetsTrueBin) print(self._numberJetsTrueFromHist) print("Unfolded jet numbers by bin:") print(self._numberJetsFromReco) print("Jet bin centers:") print([ self._hJetPtRecoCoarse.GetBinCenter(i) for i in range(1, self._hJetPtRecoCoarse.GetNbinsX()) ]) def write_files(self, filename, **kwargs): """ Write output histograms to file Args: filename: Name of output file """ print("{}: write results to file {} and folder".format( self._name, filename)) base_folder = "{}/BayesSubUnfolding/".format(self._name) print(base_folder) open_as = "append" if kwargs.get("append", False) else "recreate" with root_open(filename, open_as) as output_file: TDir = output_file.mkdir("{}{}".format(base_folder, "JetConeJtWeightBin"), title="JetConeJtWeightBin", recurse=True) TDir.cd() # output_file.cd(TDir) for i, (jt, pt) in enumerate(zip(self._hJtMeasBin, self._jetPtBins)): jt.name = "JetConeJtWeightBinNFin{0[NFin]:02d}JetPt{0[pT]:02d}".format( { "NFin": self._NFin, "pT": i }) jt.title = "Finder:Full_Jets_R04_00 p_{{T,jet}} : {} - {}".format( pt[0], pt[1]) jt.Write() if self._hBgJtNormalized is not None: TDir = output_file.mkdir("{}{}".format(base_folder, "BgJtWeightBin"), title="BgJtWeightBin", recurse=True) TDir.cd() for i, (jt, pt) in enumerate( zip(self._hBgJtNormalized, self._jetPtBins)): jt.name = "BgJtWeightBinNFin{0[NFin]:02d}JetPt{0[pT]:02d}".format( { "NFin": self._NFin, "pT": i }) jt.Write()
toy = simple.Get('toys/toy_%d' % i) toy_nfo = {} tot = 0 for bin in toy: if args.allbins: key = bin.leaves['CMS_channel'].index, bin.leaves[ 'CMS_th1x'].value else: key = bin.leaves['CMS_channel'].index if key not in toy_nfo: #toys[key] = [] toy_nfo[key] = 0 #toys[key].append(bin.weight) toy_nfo[key] += bin.weight tot += bin.weight total.Fill(tot) for key, v in toy_nfo.iteritems(): if key not in toys: toys[key] = [] toys[key].append(v) for val in toys.itervalues(): val.sort() avgs = [(k, sum(v) / len(v)) for k, v in toys.iteritems()] meds = [(k, v[len(v) / 2]) for k, v in toys.iteritems()] avgs.sort(key=sorting) meds.sort(key=sorting) height = total.max()
def make_plot(raw_data, binning, title_text, outfn): h = Hist(*binning, drawstyle='hist e1', color=sigCOLORS[0], linewidth=2, title=';Percent difference[%];Events') for sample, value in raw_data.items(): h.Fill(value['variation'], value['count']) hc = asrootpy(h.GetCumulative()) hc.linecolor = 'gold' hc.fillcolor = 'lightyellow' hc.fillstyle = 'solid' hc.scale(h.max(include_error=True) / hc.max()) xmin_, xmax_, ymin_, ymax_ = get_limits([h, hc]) draw([hc, h], ylimits=(0, ymax_)) x95, x99 = None, None cumsum = 0 for i in range(1, h.GetNbinsX() + 1): cumsum += h.GetBinContent(i) if x95 is None and cumsum / h.Integral() > 0.95: x95 = h.GetXaxis().GetBinUpEdge(i) if x99 is None and cumsum / h.Integral() > 0.99: x99 = h.GetXaxis().GetBinUpEdge(i) title = TitleAsLatex(title_text) title.Draw() # print(title_text, ROOT.gPad.GetUymax(), hc.max()) # draw a second axis on the right. ROOT.gPad.SetTicks( 1, 0 ) # Draw top ticks but not right ticks (https://root.cern.ch/root/roottalk/roottalk99/2908.html) low, high = 0, ROOT.gPad.GetUymax() / hc.max() raxis = ROOT.TGaxis(ROOT.gPad.GetUxmax(), ROOT.gPad.GetUymin(), ROOT.gPad.GetUxmax(), ROOT.gPad.GetUymax(), low, high, 510, "+L") raxis.SetLabelSize(0.03) raxis.SetLabelFont(42) raxis.SetLabelColor(convert_color('gold', 'root')) raxis.Draw() frame = canvas.FindObject('TFrame') lo, hi = frame.GetY1(), frame.GetY2() l95 = ROOT.TLine(x95, lo, x95, hi) l95.SetLineStyle(2) l95.SetLineColor(convert_color(sigCOLORS[1], 'root')) l95.SetLineWidth(2) l95.Draw() l99 = ROOT.TLine(x99, lo, x99, hi) l99.SetLineStyle(3) l99.SetLineColor(convert_color(sigCOLORS[2], 'root')) l99.Draw() leg = Legend(3, margin=0.25, leftmargin=0.45, topmargin=0.02, entrysep=0.01, entryheight=0.02, textsize=10) leg.AddEntry(hc, label='cumulative (norm.)', style='LF') leg.AddEntry(l95, label='95% @ {}'.format(x95), style='L') leg.AddEntry(l99, label='99% @ {}'.format(x99), style='L') leg.Draw() canvas.SaveAs(outfn) canvas.clear()
class processor : def __init__(self, outputfile, inputFiles, modules=[], branches=None, entrysteps=None, nbatches=None, treename="Events", decode="utf8"): self.outputfile = outputfile self.inputFiles = inputFiles self.nbatches = nbatches self.modules = modules self.decode = decode ## Tracking the process information self.isData = False self.isFastsim = False self.isSUSY = False self.Lumi = 0 self.CrossSection = 0 self.ExpectedNEvent = 0 self.era = 0 self.process = None self.period = None self.process_full = None ## Tracking the global histogram self.BookGlobalHist() ## Tracking the reading self.FirstEvent=True self.curfilename = None self.curTFile = None ## Tracking the performance self.totalevents = 0 self.startime = 0 self.loadingtime = 0 self.analyzingtime = 0 self.totaltime = 0 ## Iterate the file print("Hello! Start to read files...") if self.inputFiles.endswith(".root"): lines=[self.inputFiles] else: with open(self.inputFiles) as filelist: lines = [l.strip() for l in filelist.readlines()] # self.cache = uproot.cache.ArrayCache(200 *1024**2) self.it = uproot.iterate(lines, treename, branches=branches, # cache=self.cache, namedecode=decode, entrysteps=entrysteps, reportpath=True, reportfile=True, reportentries=True, xrootdsource=dict(chunkbytes=80*1024, limitbytes=10*1024**2)) def BookGlobalHist(self): self.hEvents = Hist(5, 0, 5, name = "NEvent", title="Number of Events") InfoLabels = ["isData", "isFastsim", "Lumi", "CrossSection", "GeneratedNEvent", "era"] self.hInfo = Profile(len(InfoLabels), 0, len(InfoLabels), name="Info", title="Information of the process") [ self.hInfo.GetXaxis().SetBinLabel(i+1, n) for i, n in enumerate(InfoLabels) ] def FillNEvent(self, events): if "genWeight" not in events: return False genW = events["genWeight"] posW = genW[genW > 0].sum() negW = len(genW) - posW self.hEvents.Fill(2, posW ) self.hEvents.Fill(3, negW) self.hEvents.Fill(4, posW - negW ) def CalTimeLasted(self, totsec): h = int(totsec/3600) m = int((totsec % 3600)/60) s = (totsec % 3600)%60 if h == 0: return "%d:%d minutes" % (m, s) else: return "%d:%d hours" % (h, m) def run(self): nbatch = 0 self.startime = time.time() while True: try: t0 = time.time() curfilename, self.curTFile, start, end, self.events = next(self.it) t1 = time.time() nEvents = end -start self.totalevents = end self.loadingtime += t1-t0 if self.FirstEvent: self.GetFileInformation() for m in self.modules: m.ObtainInfo(self.isData, self.isFastsim, self.isSUSY, self.Lumi, self.CrossSection, self.era, self.process, self.period) self.FirstEvent = False self.FillNEvent(self.events) #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Running Modules ~~~~~ for m in self.modules: m.analyze(self.events) t2 = time.time() self.analyzingtime += t2-t1 print("Loaded %d events for %.2f second, analyzed with %.2f kHz" % (nEvents, t1-t0, nEvents/(1000*(t2-t1)))) nbatch += 1 if self.nbatches is not None and nbatch == self.nbatches: print("Finished run with %d batches" % nbatch) self.EndRun() break except StopIteration: print("End of the loop", sys.exc_info()[0]) self.EndRun() break def EndRun(self): self.totaltime = time.time() - self.startime print("Run over %d events with %s ( %.1f%% loading, %.1f%% analyzing )" % \ (self.totalevents, self.CalTimeLasted(self.totaltime), self.loadingtime/self.totaltime*100,\ self.analyzingtime/self.totaltime*100)) print("Saving output to %s" % self.outputfile) for m in self.modules: m.endJob(self.totalevents) outfile = root_open(self.outputfile, "RECREATE") outfile.cd() self.hEvents.Fill(0, self.totalevents) self.hEvents.Write() if self.process_full is not None: self.hInfo.SetTitle(self.process_full) self.hInfo.Fill(0, self.isData) self.hInfo.Fill(1, self.isFastsim) self.hInfo.Fill(2, self.Lumi) self.hInfo.Fill(3, self.CrossSection) self.hInfo.Fill(4, self.ExpectedNEvent) self.hInfo.Fill(5, self.era) self.hInfo.Write() for m in self.modules: m.SaveHist(outfile) outfile.close() def GetFileInformation(self): Eventbranch = self.curTFile.get("Events") if "Stop0l_evtWeight" in Eventbranch: import re infostr = Eventbranch.get("Stop0l_evtWeight").title.decode(self.decode) mat =re.match(".*\(Lumi=([0-9]*[.]?[0-9]+)\)", infostr) if mat is not None: self.Lumi = float(mat.group(1)) mat =re.match(".*\(CrossSection=([0-9]*[.]?[0-9]+),\s+nEvent=([0-9]*[.]?[0-9]+)\)", infostr) if mat is not None: self.CrossSection = float(mat.group(1)) self.ExpectedNEvent = float(mat.group(2)) mat =re.match(".*for(.*)\(.*\)", infostr) if mat is not None: self.process_full = mat.groups()[0].strip() mat2 = re.match("(.*)_(2016|2017|2018)(_Period(\w))?", self.process_full) if mat2 is not None: self.process, self.era, _, self.period = mat2.groups() if self.era is not None: self.era = int(self.era) if self.process is not None: if "Data" in self.process: self.isData = True if "fastsim" in self.process: self.isFastsim = True if "SMS" in self.process: self.isSUSY = True ## Temp fix for v2p6 pro if self.process is None: self.isData = True return False
def do_analysis(f, eventtype, analyze_this, outfile): # get trees from files t = f.Get("Delphes") # get number of entries nentries = t.GetEntries() # initialize everything here before filling histograms if (analyze_this['Jet.PT']): print "\nInitializing Jet.PT...\n" # create the histograms numJets = Hist(NBINS, NLO, NHI, title='numJets ' + eventtype, legendstyle='L') # interesting values to plot max_jetpt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max JetPT/Event ' + eventtype, legendstyle='L') min_jetpt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min JetPT/Event ' + eventtype, legendstyle='L') else: print "Skipped Jet.PT" if (analyze_this['Jet.BTag']): print "Initializing Jet.BTag...\n" # create the histograms loose = Hist(NBINS, NLO, NHI, title='loose ' + eventtype, legendstyle='L') medium = Hist(NBINS, NLO, NHI, title='medium ' + eventtype, legendstyle='L') tight = Hist(NBINS, NLO, NHI, title='tight ' + eventtype, legendstyle='L') else: print "Skipped Jet.BTag" if (analyze_this['Electron.PT']): print "Initializing Electron.PT...\n" # create the histograms numElectrons = Hist(NBINS, NLO, NHI, title='numElectrons ' + eventtype, legendstyle='L') # interesting values to plot max_ept_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max ElectronPT/Event ' + eventtype, legendstyle='L') min_ept_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min ElectronPT/Event ' + eventtype, legendstyle='L') # initialize any variable-specific constants here: # counter for no electrons noeleaf = 0 else: print "Skipped Electron.PT" if (analyze_this['MuonTight.PT']): print "Initializing MuonTight.PT...\n" # create the histograms numMuons = Hist(NBINS, NLO, NHI, title='numMuons ' + eventtype, legendstyle='L') # interesting values to plot max_upt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max MuonPT/Event ' + eventtype, legendstyle='L') min_upt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min MuonPT/Event ' + eventtype, legendstyle='L') # initialize any variable-specific constants here: # counter for no electrons nouleaf = 0 else: print "Skipped MuonTight.PT" if (analyze_this['MissingET.MET']): print "Initializing MissingET.MET...\n" # create the histograms MET = Hist(MET_NBINS, MET_NLO, MET_NHI, title='MET ' + eventtype, legendstyle='L') else: print "Skipped MissingET.MET" if (analyze_this['MT (NON-LEAF)']): print "Initializing MT...\n" # create the histograms MT = Hist(MT_NBINS, MT_NLO, MT_NHI, title='MT ' + eventtype, legendstyle='L') else: print "Skipped HT" if (analyze_this['HT (NON-LEAF)']): print "Initializing HT...\n" # create the histograms HT = Hist(HT_NBINS, HT_NLO, HT_NHI, title='HT ' + eventtype, legendstyle='L') else: print "Skipped HT" # Now fill histograms print "\nFilling histograms...\n" # get float version of num entries to normalize below; subtract 1 to get # actual integral value of hist norm = float(nentries - 1) for e in range(nentries): entry = t.GetEntry(e) # to check whether each entry is electron or muon is_electron = False is_muon = False e_maxpt = 0 e_maxpt_phi = 0 u_maxpt = 0 u_maxpt_phi = 0 if (analyze_this['Jet.PT']): # define leaves var = 'Jet.PT' leaf = t.GetLeaf(var) # analyze with Jet.PT because HT is sum of Jet.PTs if (analyze_this['HT (NON-LEAF)']): HTfill = True else: HTfill = False fill_JetPT_hist(t, leaf, entry, numJets, min_jetpt_per_event, max_jetpt_per_event, HTfill, HT) if (analyze_this['Jet.BTag']): # define leaves var = "Jet.BTag" leaf = t.GetLeaf(var) fill_JetBTag_hist(t, leaf, entry, loose, medium, tight) if (analyze_this['Electron.PT']): # define leaves var = "Electron.PT" leaf = t.GetLeaf(var) phileaf = t.GetLeaf('Electron.Phi') # returns phi of max pt for entry (e_maxpt, e_maxpt_phi) = fill_Electron_hist(t, leaf, entry, numElectrons, min_ept_per_event, max_ept_per_event, phileaf) if (leaf.GetLen() == 0): noeleaf += 1 e_maxpt = 0 e_maxpt_phi = 0 else: is_electron = True if (analyze_this['MuonTight.PT']): # define leaves var = "MuonTight.PT" leaf = t.GetLeaf(var) phileaf = t.GetLeaf('MuonTight.Phi') (u_maxpt, u_maxpt_phi) = fill_Muon_hist(t, leaf, entry, numMuons, min_upt_per_event, max_upt_per_event, phileaf) if leaf.GetLen() == 0: nouleaf += 1 u_maxpt = 0 u_maxpt_phi = 0 else: is_muon = True if (analyze_this['MissingET.MET']): # define leaves var = "MissingET.MET" leaf = t.GetLeaf(var) phileaf = t.GetLeaf('MissingET.Phi') (met, metphi) = fill_MET_hist(t, leaf, entry, MET, phileaf) if (analyze_this['MT (NON-LEAF)']): #print "ok got here" #print "here ",e_maxpt, e_maxpt_phi, u_maxpt, u_maxpt_phi, met, metphi mt_val = get_mt(e_maxpt, e_maxpt_phi, u_maxpt, u_maxpt_phi, met, metphi) if mt_val == 0: MT.Fill(INVALID) else: MT.Fill(mt_val) #print mt_val if (analyze_this['Jet.PT']): # normalize numJets.Scale(1 / norm) max_jetpt_per_event.Scale(1 / norm) min_jetpt_per_event.Scale(1 / norm) if (analyze_this['Jet.BTag']): # normalize tight.Scale(1 / norm) medium.Scale(1 / norm) loose.Scale(1 / norm) if (analyze_this['Electron.PT']): # normalize numElectrons.Scale(1 / norm) max_ept_per_event.Scale(1 / norm) min_ept_per_event.Scale(1 / norm) print "\nentries: " + str(nentries) + " noeleaf number: " + str( noeleaf) if (analyze_this['MuonTight.PT']): # normalize numMuons.Scale(1 / norm) max_upt_per_event.Scale(1 / norm) min_upt_per_event.Scale(1 / norm) print "\nentries: " + str(nentries) + " nouleaf number: " + str( nouleaf) if (analyze_this['MissingET.MET']): # normalize MET.Scale(1 / norm) if (analyze_this['MT (NON-LEAF)']): #normalize MT.Scale(1 / norm) if (analyze_this['HT (NON-LEAF)']): #normalize HT.Scale(1 / norm) print "" print "\nDone!\n" numJets.Write(eventtype + "numJets") max_jetpt_per_event.Write(eventtype + "max_jetpt_per_event") min_jetpt_per_event.Write(eventtype + "min_jetpt_per_event") loose.Write(eventtype + "loose") medium.Write(eventtype + "medium") tight.Write(eventtype + "tight") numElectrons.Write(eventtype + "numElectrons") max_ept_per_event.Write(eventtype + "max_ept_per_event") min_ept_per_event.Write(eventtype + "min_ept_per_event") numMuons.Write(eventtype + "numMuons") max_upt_per_event.Write(eventtype + "max_upt_per_event") min_upt_per_event.Write(eventtype + "min_upt_per_event") MET.Write(eventtype + "MET") MT.Write(eventtype + "MT") HT.Write(eventtype + "HT")
toys = [''] if args.asimov else [ i.name for i in mlfit.keys() if i.name.startswith('toy_') ] tot += len(toys) for i in toys: path = os.path.join(i, 'fit_s') toy = mlfit.Get(path) pars = asrootpy(toy.floatParsFinal()) prefit = asrootpy(toy.floatParsInit()) cover_tot += 1 if args.asimov: csf = float(pars['charmSF'].value) if not args.conly: lsf = float(pars['lightSF'].value) else: csf.Fill(float(pars['charmSF'].value)) if not args.conly: lsf.Fill(float(pars['lightSF'].value)) if not args.conly and covers(pars['lightSF'], lval): lcover_ok += 1 if covers(pars['charmSF'], cval): ccover_ok += 1 if args.systematic: if not args.conly and covers(pars['lightSF'], lval, args.systematic): lcoverp_ok += 1 if covers(pars['charmSF'], cval, args.systematic): ccoverp_ok += 1 if not args.asimov: csf_prefit.Fill(float(prefit['charmSF'].value)) csf2d.Fill(prefit['charmSF'].value,
def plot_trigger_efficiency_curves(trigger_1, trigger_2, pT_upper_limit=800): properties = parse_file_turn_on('/home/aashish/turn_on.dat') # properties = parse_file_turn_on('./trigger_proper_turn_on.dat') event_numbers = properties['event_number'] pTs = properties['corrected_hardest_pts'] trigger_names = properties['trigger_names'] prescales = properties['prescale'] colors = ['magenta', 'blue', 'orange', 'green', 'black', 'red'] expected_trigger_names = [ "HLT\_Jet180U", "HLT\_Jet140U", "HLT\_Jet100U", "HLT\_Jet70U", "HLT\_Jet50U", "HLT\_Jet30U" ] color = colors[expected_trigger_names.index(trigger_1.replace("_", "\_"))] pt_hist_trigger_1 = Hist(100, 0, pT_upper_limit, title=trigger_1[4:], color=color, markersize=1.0, linewidth=5) pt_hist_trigger_2 = Hist(100, 0, pT_upper_limit, title=trigger_2[4:], color=color, markersize=1.0, linewidth=5) for i in range(0, len(pTs)): if trigger_1 in trigger_names[i]: pt_hist_trigger_1.Fill(pTs[i], prescales[i]) # The len thingy is to make sure trigger names like HLT_Jet15U_HcalNoiseFiltered_v3 are excluded. # if trigger_2 in trigger_names[i] and len(trigger_names[i]) > (len(trigger_2) + 3): if trigger_2 in trigger_names[i]: pt_hist_trigger_2.Fill(pTs[i], prescales[i]) pt_hist_trigger_1.Divide(pt_hist_trigger_2) rplt.errorbar(pt_hist_trigger_1, color=color, markersize=10, pickradius=8, capthick=5, capsize=8, elinewidth=5) data_plot = rplt.errorbar(pt_hist_trigger_1, color=color, markersize=10, pickradius=8, capthick=5, capsize=8, elinewidth=5) data_points_x = data_plot[0].get_xdata() data_points_y = np.log(data_plot[0].get_ydata()) # print data_points_y[0:5] fitted_poly_coeffs = np.polyfit(data_points_x, data_points_y, 1) print fitted_poly_coeffs fitted_poly = np.polynomial.Polynomial(fitted_poly_coeffs) x = np.arange(120, 200, 5) plt.plot(x, fitted_poly(x), lw=5, color="red") plt.gca().xaxis.set_tick_params(width=5, length=20, labelsize=70) plt.gca().yaxis.set_tick_params(width=5, length=20, labelsize=70) ab = AnnotationBbox(OffsetImage(read_png( get_sample_data( "/home/aashish/root-6.04.06/macros/MODAnalyzer/mod_logo.png", asfileobj=False)), zoom=0.15, resample=1, dpi_cor=1), (0.23, 0.895), xycoords='figure fraction', frameon=0) plt.gca().add_artist(ab) plt.gcf().text(0.29, 0.885, "Prelim. (20\%)", fontsize=50, weight='bold', color='#444444', multialignment='center') plt.gcf().set_snap(1) # Horizontal Line. plt.plot(list(pt_hist_trigger_1.x()), [1] * len(list(pt_hist_trigger_1.x())), color="black", linewidth=5, linestyle="dashed") if trigger_1 == "HLT_L1Jet6U": lower_pT = 37 elif trigger_1 == "HLT_Jet15U": lower_pT = 56 elif trigger_1 == "HLT_Jet30U": lower_pT = 84 elif trigger_1 == "HLT_Jet50U": lower_pT = 114 elif trigger_1 == "HLT_Jet70U": lower_pT = 153 elif trigger_1 == "HLT_Jet100U": lower_pT = 196 else: lower_pT = 0 if lower_pT != 0: # CMS Vertical line. plt.plot([lower_pT, lower_pT], [plt.gca().get_ylim()[0], 1.], color=color, linewidth=3, linestyle="dashed") # # MOD Vertical line. # efficient_pt_x = 0.0 # efficient_pt_y = 0.0 # efficient_pt_x_s = [] # efficient_pt_y_s = [] # distance_to_previous_point_close_to_one = 0 # for i in range(0, len(list(pt_hist_trigger_1.x()))): # if abs(list(pt_hist_trigger_1.y())[i] - 1.00) < 0.1: # if distance_to_previous_point_close_to_one > 25: # # Last point close to one too far away. # # Empty the lists. # efficient_pt_x_s, efficient_pt_y_s = [], [] # efficient_pt_x_s.append(list(pt_hist_trigger_1.x())[i]) # efficient_pt_y_s.append(list(pt_hist_trigger_1.y())[i]) # distance_to_previous_point_close_to_one = 0 # else: # distance_to_previous_point_close_to_one += 1 # if len(efficient_pt_x_s) > 75: # efficient_pt_x = efficient_pt_x_s[0] # efficient_pt_y = efficient_pt_y_s[0] # break # mod_efficient_pTs = [325, 260, 196, 153, 114, 84, 50, 32] # mod_efficient_pT = mod_efficient_pTs[expected_trigger_names.index(trigger_1.replace("_", "\_"))] # plt.plot([mod_efficient_pT, mod_efficient_pT], [plt.gca().get_ylim()[0], 1.], color="purple", linewidth=3, linestyle="dashed") # if lower_pT != 0: # plt.gca().annotate("CMS\n" + str(lower_pT) + " GeV", xy=(lower_pT, 1.), xycoords='data', xytext=(-100, 250), textcoords='offset points', color=color, size=40, va="center", ha="center", arrowprops=dict(arrowstyle="simple", facecolor=color, zorder=9999, connectionstyle="angle3,angleA=0,angleB=90") ) # plt.gca().annotate("MOD\n" + str(int(mod_efficient_pT)) + " GeV", xy=(mod_efficient_pT, 1.), xycoords='data', xytext=(250, 200), textcoords='offset points', color="purple", size=40, va="center", ha="center", arrowprops=dict(arrowstyle="simple", facecolor="purple", zorder=9999, connectionstyle="angle3,angleA=45,angleB=-90") ) plt.yscale('log') plt.gca().set_ylim(plt.gca().get_ylim()[0], 100) plt.legend(frameon=0) plt.xlabel('$p_T~\mathrm{(GeV)}$', fontsize=55, rotation=0) plt.ylabel('$\mathrm{A.U.}$', fontsize=75, rotation=0, labelpad=50.) plt.gcf().set_size_inches(30, 21.4285714, forward=1) plt.gcf().set_snap(True) plt.tight_layout(pad=1.08, h_pad=1.08, w_pad=1.08) plt.savefig("plots/Version 4/trigger_efficiency_fit/efficiency_curves_" + trigger_1 + ".pdf") # plt.show() plt.clf()
# Alternatively you can specify the name (and the title or any other style # attributes) in the constructor: h_simple = Hist(10, -4, 12, name='my hist', title='Some Data', drawstyle='hist', legendstyle='F', fillstyle='/') # fill the histogram for i in range(1000): # all ROOT CamelCase methods are aliased by equivalent snake_case methods # so you can call fill() instead of Fill() h_simple.Fill(random.gauss(4, 3)) # easily set visual attributes h_simple.linecolor = 'blue' h_simple.fillcolor = 'green' h_simple.fillstyle = '/' # attributes may be accessed in the same way print(h_simple.name) print(h_simple.title) print(h_simple.markersize) # plot canvas = Canvas(width=700, height=500) canvas.SetLeftMargin(0.15) canvas.SetBottomMargin(0.15)
h_data_toUse.SetBinContent(bin, newContent) h_data_toUse.SetBinError(bin, sqrt(newContent)) h_measured_toUse = asrootpy(h_measured) # Remove fakes before unfolding h_fakes = h_measured_toUse - h_response.ProjectionX() nonFakeRatio = 1 - h_fakes / h_measured_toUse h_measured_toUse *= nonFakeRatio / nonFakeRatio h_data_toUse *= nonFakeRatio / nonFakeRatio # Unfold with SVD response = RooUnfoldResponse(h_measured_toUse, h_truth, h_response) svdUnfolding = RooUnfoldSvd(response, h_data_toUse, 0.7) svdUnfolding.SetVerbose(0) h_unfolded_data_svd = svdUnfolding.Hreco(3) # Unfold with Bayes response_bayes = RooUnfoldResponse(h_measured_toUse, h_truth, h_response) bayesUnfolding = RooUnfoldBayes(response_bayes, h_data_toUse, 4) h_unfolded_data_bayes = bayesUnfolding.Hreco(3) # Store result of first bin h_svdFirstBin.Fill(h_unfolded_data_svd.GetBinContent(1)) h_bayesFirstBin.Fill(h_unfolded_data_bayes.GetBinContent(1)) # Draw histograms h_svdFirstBin.Draw() raw_input('SVD') h_bayesFirstBin.Draw() raw_input('Bayes')
class Test( unittest.TestCase ): def setUp( self ): # create histograms self.h1 = Hist( 100, 0, 100, title = '1D' ) self.h2 = Hist2D( 60, 40, 100, 60, 40, 100 ) self.simple = Hist( 100, 0, 100, title = '1D' ) # fill the histograms with our distributions map( self.h1.Fill, x1 ) self.h2.fill_array( np.random.multivariate_normal( mean = ( 50, 50 ), cov = np.arange( 4 ).reshape( 2, 2 ), size = ( int( 1E6 ), ) ) ) self.bins = [40, 45, 50, 60, 65, 70, 75, 80, 100] # rebin them self.h2_rebinned = self.h2.rebinned( self.bins, axis = 0 ) self.h2_rebinned = self.h2_rebinned.rebinned( self.bins, axis = 1 ) self.h2_rebinned_2 = rebin_2d( self.h2, self.bins, self.bins ) # we only test symmetric bins for now self.n_bins_x = 5 self.n_bins_y = 5 # only entries in diagonals, p = 1, s = 1 for all bins self.diagonals = Hist2D( self.n_bins_x, 0, 10, self.n_bins_y, 0, 10 ) # this creates # [0, 0, 0, 0, 1], # [0, 0, 0, 1, 0], # [0, 0, 1, 0, 0], # [0, 1, 0, 0, 0], # [1, 0, 0, 0, 0] for i in range( 1, self.n_bins_x + 1 ): self.diagonals.SetBinContent( i, i, 1 ) # the following should result in # [0, 0, 2], # [0, 2, 0], # [1, 0, 0] self.bin_edges_nice = [0, 2, 6, 10] self.result_nice = [1, 2, 2] # the following should result in # [0, 0, 0, 2], # [0, 0, 2, 0] # [0, 1, 0, 0] # [0, 0, 0, 0] self.bin_edges_out_of_bound = [-2, 0, 2, 6, 20] self.result_out_of_bound = [0, 1, 2, 2] # the following should result in # [0, 0, 2], # [0, 1, 0], # [2, 0, 0] self.bin_edges_not_on_boundaries = [0, 3.5, 6, 20] self.result_not_on_boundaries = [2, 1, 2] for i in range(100): self.simple.Fill(i, 1) def tearDown( self ): pass def test_rebin_2d_vs_rootpy_rebin( self ): for i, _ in enumerate( self.bins[1:] ): bin_content = self.h2_rebinned.GetBinContent( i + 1, i + 1 ) bin_content_2 = self.h2_rebinned_2.GetBinContent( i + 1, i + 1 ) self.assertEqual( bin_content, bin_content_2 ) def test_2D_integral( self ): for i, bin_i in enumerate( self.bins[:-1] ): current_bin_start = self.h2.FindBin( bin_i ) current_bin_end = self.h2.FindBin( self.bins[i + 1] ) integral = self.h2.Integral( current_bin_start, current_bin_end - 1, current_bin_start, current_bin_end - 1 ) bin_content = self.h2_rebinned.GetBinContent( i + 1 , i + 1 ) self.assertEqual( bin_content, integral ) def test_rebin_2d_nice( self ): new_hist = rebin_2d( self.diagonals, self.bin_edges_nice, self.bin_edges_nice ) for i in range( 1, new_hist.nbins() + 1 ): self.assertEqual( new_hist.GetBinContent( i, i ), self.result_nice[i - 1], 'histogram contents do not match' ) def test_rebin_2d_out_of_bound( self ): new_hist = rebin_2d( self.diagonals, self.bin_edges_out_of_bound, self.bin_edges_out_of_bound ) for i in range( 1, new_hist.nbins() + 1 ): self.assertEqual( new_hist.GetBinContent( i, i ), self.result_out_of_bound[i - 1], 'histogram contents do not match' ) def test_rebin_2d_not_on_boundaries( self ): new_hist = rebin_2d( self.diagonals, self.bin_edges_not_on_boundaries, self.bin_edges_not_on_boundaries ) for i in range( 1, new_hist.nbins() + 1 ): self.assertEqual( new_hist.GetBinContent( i, i ), self.result_not_on_boundaries[i - 1], 'histogram contents do not match' ) def test_adjust_overflow_to_limit_simple( self ): x_min = 0 x_max = 95 adjusted = adjust_overflow_to_limit(self.simple, x_min, x_max) # for entry_1, entry_2 in zip(hist_to_value_error_tuplelist(self.simple), hist_to_value_error_tuplelist(adjusted)): # print entry_1, entry_2 # print self.simple.integral( overflow = True ), adjusted.integral() # print self.simple.GetBinContent(1), self.simple.GetBinContent(self.simple.nbins()) # number of events should be unchanged # the adjusted histogram should have no overflow for this example self.assertEqual( self.simple.integral( overflow = True ), adjusted.integral() ) # first bin (x_min) should contain all events # with x <= x_min x_min_bin = self.simple.FindBin(x_min) x_max_bin = self.simple.FindBin(x_max) self.assertEqual(self.simple.integral(0, x_min_bin), adjusted.GetBinContent(x_min_bin)) # last bin (x_max) should contain all events # with x >= x_max self.assertEqual( self.simple.integral( x_max_bin, self.simple.nbins() + 1), adjusted.GetBinContent( x_max_bin ) ) def test_adjust_overflow_to_limit( self ): x_min = 40 x_max = 80 adjusted = adjust_overflow_to_limit(self.h1, x_min, x_max) # number of events should be unchanged # the adjusted histogram should have no overflow for this example self.assertEqual(self.h1.integral( overflow = True ), adjusted.Integral()) # first bin (x_min) should contain all events # with x <= x_min x_min_bin = self.h1.FindBin(x_min) x_max_bin = self.h1.FindBin(x_max) self.assertEqual(self.h1.integral(0, x_min_bin), adjusted.GetBinContent(x_min_bin)) # last bin (x_max) should contain all events # with x >= x_max self.assertEqual( self.h1.integral( x_max_bin, self.h1.nbins() + 1 ), adjusted.GetBinContent( x_max_bin ) )
x = smear(xt) if x != None: response.Fill(x, xt) else: response.Miss(xt) print "==================================== TEST =====================================" hTrue = Hist(40, -10.0, 10.0) hMeas = Hist(40, -10.0, 10.0) hTrue.SetLineColor('red') hMeas.SetLineColor('blue') # Test with a Gaussian, mean 0 and width 2. for i in xrange(10000): xt = gRandom.Gaus(0.0, 2.0) x = smear(xt) hTrue.Fill(xt) if x != None: hMeas.Fill(x) print "==================================== UNFOLD ===================================" unfold = RooUnfoldBayes(response, hMeas, 4) # OR # unfold= RooUnfoldSvd (response, hMeas, 20); # OR # unfold= RooUnfoldTUnfold (response, hMeas); hReco = unfold.Hreco() h_unfolded = asrootpy(hReco) unfold.PrintTable(cout, hTrue) plt.figure(figsize=(16, 12), dpi=100) rplt.hist(hTrue, label='truth', stacked=False) rplt.hist(hMeas, label='measured', stacked=False)
can = Canvas(name="can", title="can", width=600, height=450) rand = ROOT.TRandom3() h1 = Hist(100, -5, 5, name="h1", title="Histogram 1") h1.Sumw2() h1.SetLineColor(ROOT.kRed) h2 = Hist(100, -5, 5, name="h2", title="Histogram 2") h2.SetLineColor(ROOT.kBlue) for ievt in xrange(10000): #some test histograms: #1. let 2 histograms screwed #h1.Fill(rand.Gaus(0.5, 0.8)) #h2.Fill(rand.Gaus(0, 1)) #2. long tail and short tail h1.Fill(rand.Gaus(0, 0.8)) h2.Fill(rand.Gaus(0, 1)) #hs = ROOT.THStack("hs", "2 example distributions") #hs.Add(h1) #hs.Add(h2) #hs.Draw("nostack") h1.Draw() h2.Draw('same') #draw legend leg = ROOT.TLegend(0.7, 0.7, 0.89, 0.89) leg.SetFillColor(0) leg.AddEntry(h1, h1.GetTitle(), "pl") leg.AddEntry(h2, h2.GetTitle(), "pl")
def do_analysis(chain, analyze_this, outfile, eventtype): # get trees from files #t = f.Get("Delphes") # get number of entries nentries = chain.GetEntries() # initialize everything here before filling histograms if (analyze_this['Jet.PT']): print "\nInitializing Jet.PT...\n" # create the histograms numJets = Hist(NBINS, NLO, NHI, title='numJets ' + eventtype, legendstyle='L') # interesting values to plot max_jetpt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max JetPT/Event ' + eventtype, legendstyle='L') min_jetpt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min JetPT/Event ' + eventtype, legendstyle='L') else: print "Skipped Jet.PT" if (analyze_this['Jet.BTag']): print "Initializing Jet.BTag...\n" # create the histograms loose = Hist(NBINS, NLO, NHI, title='loose ' + eventtype, legendstyle='L') medium = Hist(NBINS, NLO, NHI, title='medium ' + eventtype, legendstyle='L') tight = Hist(NBINS, NLO, NHI, title='tight ' + eventtype, legendstyle='L') else: print "Skipped Jet.BTag" if (analyze_this['Electron.PT']): print "Initializing Electron.PT...\n" # create the histograms numElectrons = Hist(NBINS, NLO, NHI, title='numElectrons ' + eventtype, legendstyle='L') # interesting values to plot max_ept_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max ElectronPT/Event ' + eventtype, legendstyle='L') min_ept_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min ElectronPT/Event ' + eventtype, legendstyle='L') # initialize any variable-specific constants here: # counter for no electrons noeleaf = 0 else: print "Skipped Electron.PT" if (analyze_this['MuonTight.PT']): print "Initializing MuonTight.PT...\n" # create the histograms numMuons = Hist(NBINS, NLO, NHI, title='numMuons ' + eventtype, legendstyle='L') # interesting values to plot max_upt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Max MuonPT/Event ' + eventtype, legendstyle='L') min_upt_per_event = Hist(PT_NBINS, PT_NLO, PT_NHI, title='Min MuonPT/Event ' + eventtype, legendstyle='L') # initialize any variable-specific constants here: # counter for no electrons nouleaf = 0 else: print "Skipped MuonTight.PT" if (analyze_this['MissingET.MET']): print "Initializing MissingET.MET...\n" # create the histograms MET = Hist(MET_NBINS, MET_NLO, MET_NHI, title='MET ' + eventtype, legendstyle='L') else: print "Skipped MissingET.MET" if (analyze_this['MT (NON-LEAF)']): print "Initializing MT...\n" # create the histograms MT = Hist(MT_NBINS, MT_NLO, MT_NHI, title='MT ' + eventtype, legendstyle='L') else: print "Skipped HT" if (analyze_this['HT (NON-LEAF)']): print "Initializing HT...\n" # create the histograms HT = Hist(HT_NBINS, HT_NLO, HT_NHI, title='HT ' + eventtype, legendstyle='L') else: print "Skipped HT" if (analyze_this['DELTA PHI (NON-LEAF)']): print "Initializing Delta Phi...\n" # create the histograms DPHI_metlep = Hist(30, -1 * np.pi, np.pi, title='Delta Phi (MET-lep) ' + eventtype, legendstyle='L') DPHI_metjet = Hist(30, -1 * np.pi, np.pi, title='Delta Phi (MET-jet)' + eventtype, legendstyle='L') else: print "Skipped DELTA PHI" if (analyze_this['DELTA R (NON-LEAF)']): print "Initializing Delta R...\n" #create histograms DR = Hist(50, 0, 2 * np.pi, title='DELTA R ' + eventtype, legendstyle='L') else: print "Skipped DELTA R" # Now fill histograms print "\nFilling histograms...\n" # get float version of num entries to normalize below; subtract 1 to get # actual integral value of hist norm = float(nentries - 1) for e in range(nentries): entry = chain.GetEntry(e) # to check whether each entry is electron or muon is_electron = False is_muon = False e_maxpt = 0 e_maxpt_phi = 0 u_maxpt = 0 u_maxpt_phi = 0 jet_maxpt = 0 jet_maxpt_phi = 0 jet_maxpt_eta = 0 met = 0 met_phi = 0 met_eta = 0 lepton_vec = [] if (analyze_this['Electron.PT']): # define leaves var = "Electron.PT" leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('Electron.Phi') etaleaf = chain.GetLeaf('Electron.Eta') # returns phi of max pt for entry (e_maxpt, e_maxpt_phi, e_maxpt_eta) = fill_Electron_hist( chain, leaf, entry, numElectrons, min_ept_per_event, max_ept_per_event, phileaf, etaleaf, lepton_vec) #print lepton_vec if (leaf.GetLen() == 0): noeleaf += 1 e_maxpt = INVALID e_maxpt_phi = INVALID e_maxpt_eta = INVALID else: is_electron = True if (analyze_this['MuonTight.PT']): # define leaves var = "MuonTight.PT" leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('MuonTight.Phi') etaleaf = chain.GetLeaf('MuonTight.Eta') (u_maxpt, u_maxpt_phi, u_maxpt_eta) = fill_Muon_hist( chain, leaf, entry, numMuons, min_upt_per_event, max_upt_per_event, phileaf, etaleaf, lepton_vec) if leaf.GetLen() == 0: nouleaf += 1 u_maxpt = INVALID u_maxpt_phi = INVALID u_maxpt_eta = INVALID else: is_muon = True # Get preferred lepton for future calcs if e_maxpt >= u_maxpt: lpt = e_maxpt lphi = e_maxpt_phi leta = e_maxpt_eta else: lpt = u_maxpt lphi = u_maxpt_phi leta = u_maxpt_eta if (analyze_this['Jet.PT']): # define leaves var = 'Jet.PT' leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('Jet.Phi') etaleaf = chain.GetLeaf('Jet.Eta') # analyze with Jet.PT because HT is sum of Jet.PTs if (analyze_this['HT (NON-LEAF)']): HTfill = True else: HTfill = False # returns phi of max pt for entry (jet_maxpt, jet_maxpt_phi, jet_maxpt_eta) = fill_JetPT_hist( chain, leaf, entry, numJets, min_jetpt_per_event, max_jetpt_per_event, HTfill, HT, phileaf, etaleaf, lepton_vec) if (leaf.GetLen() == 0): jet_maxpt = INVALID jet_maxpt_phi = INVALID jet_maxpt_eta = INVALID if (analyze_this['Jet.BTag']): # define leaves var = "Jet.BTag" leaf = chain.GetLeaf(var) fill_JetBTag_hist(chain, leaf, entry, loose, medium, tight) if (analyze_this['MissingET.MET']): # define leaves var = "MissingET.MET" leaf = chain.GetLeaf(var) phileaf = chain.GetLeaf('MissingET.Phi') etaleaf = chain.GetLeaf('MissingET.Eta') (met, metphi, meteta) = fill_MET_hist(chain, leaf, entry, MET, phileaf, etaleaf) if (analyze_this['MT (NON-LEAF)']): #print "ok got here" #print "here ",e_maxpt, e_maxpt_phi, u_maxpt, u_maxpt_phi, met, metphi if (not (is_muon or is_electron)): mt_val = 0 else: #print e_maxpt, e_maxpt_phi, u_maxpt, u_maxpt_phi, met, metphi #print is_electron, is_muon mt_val = get_mt(lpt, lphi, met, metphi) if mt_val == 0: MT.Fill(INVALID) else: MT.Fill(mt_val) if (analyze_this['DELTA PHI (NON-LEAF)']): dphi_metlep_val = delta_phi(metphi, lphi) dphi_metjet_val = delta_phi(metphi, jet_maxpt_phi) DPHI_metlep.Fill(dphi_metlep_val) DPHI_metjet.Fill(dphi_metjet_val) if (analyze_this['DELTA R (NON-LEAF)']): dr_val = delta_R(jet_maxpt_phi, lphi, jet_maxpt_eta, leta) if dr_val == 0: dr_val = INVALID #elif dr_val > 3.0 and dr_val < 3.3: #print jet_maxpt_phi, lphi, jet_maxpt_eta, leta DR.Fill(dr_val) if (analyze_this['Jet.PT']): # normalize numJets.Scale(1 / (numJets.Integral())) max_jetpt_per_event.Scale(1 / (max_jetpt_per_event.Integral())) min_jetpt_per_event.Scale(1 / (min_jetpt_per_event.Integral())) if (analyze_this['Jet.BTag']): # normalize tight.Scale(1 / (tight.Integral())) medium.Scale(1 / (medium.Integral())) loose.Scale(1 / (loose.Integral())) if (analyze_this['Electron.PT']): # normalize numElectrons.Scale(1 / (numElectrons.Integral())) max_ept_per_event.Scale(1 / (max_ept_per_event.Integral())) min_ept_per_event.Scale(1 / (min_ept_per_event.Integral())) print "\nentries: " + str(nentries) + " noeleaf number: " + str( noeleaf) if (analyze_this['MuonTight.PT']): # normalize numMuons.Scale(1 / (numMuons.Integral())) max_upt_per_event.Scale(1 / (max_upt_per_event.Integral())) min_upt_per_event.Scale(1 / (min_upt_per_event.Integral())) print "\nentries: " + str(nentries) + " nouleaf number: " + str( nouleaf) if (analyze_this['MissingET.MET']): # normalize MET.Scale(1 / (MET.Integral())) if (analyze_this['MT (NON-LEAF)']): #normalize MT.Scale(1 / (MT.Integral())) if (analyze_this['HT (NON-LEAF)']): #normalize HT.Scale(1 / (HT.Integral())) if (analyze_this['DELTA PHI (NON-LEAF)']): #normalize DPHI_metlep.Scale(1 / (DPHI_metlep.Integral())) DPHI_metjet.Scale(1 / (DPHI_metjet.Integral())) if (analyze_this['DELTA R (NON-LEAF)']): #normalize DR.Scale(1 / (DR.Integral())) print "" print "\nDone!\n" numJets.Write(eventtype + "numJets") max_jetpt_per_event.Write(eventtype + "max_jetpt_per_event") min_jetpt_per_event.Write(eventtype + "min_jetpt_per_event") loose.Write(eventtype + "loose") medium.Write(eventtype + "medium") tight.Write(eventtype + "tight") numElectrons.Write(eventtype + "numElectrons") max_ept_per_event.Write(eventtype + "max_ept_per_event") min_ept_per_event.Write(eventtype + "min_ept_per_event") numMuons.Write(eventtype + "numMuons") max_upt_per_event.Write(eventtype + "max_upt_per_event") min_upt_per_event.Write(eventtype + "min_upt_per_event") MET.Write(eventtype + "MET") MT.Write(eventtype + "MT") HT.Write(eventtype + "HT") DPHI_metlep.Write(eventtype + "dphi_metlep") DPHI_metjet.Write(eventtype + "dphi_metjet") DR.Write(eventtype + "deltaR")