def test_init(): # constructor arguments are repetitions of #bins, left bound, right bound. h2d = Hist2D(10, 0, 1, 50, -40, 10, name='2d hist') h3d = Hist3D(3, -1, 4, 10, -1000, -200, 2, 0, 1, name='3d hist') # variable-width bins may be created by passing the bin edges directly: h1d_variable = Hist([1, 4, 10, 100]) h2d_variable = Hist2D([2, 4, 7, 100, 200], [-100, -50, 0, 10, 20]) h3d_variable = Hist3D([1, 3, 10], [20, 50, 100], [-10, -5, 10, 20]) # variable-width and constant-width bins can be mixed: h2d_mixed = Hist2D([2, 10, 30], 10, 1, 5)
def events2th3(inputs, values, binsx, binsy, binsz, titlex='', titley='', titlez=''): # Histo used to fill values histo = Hist3D(*(binsx+binsy+binsz)) # Histo used to count entries histo_count = Hist3D(*(binsx+binsy+binsz)) histo.SetXTitle(titlex) histo.SetYTitle(titley) histo.SetZTitle(titlez) fill_hist(histo, inputs, values) fill_hist(histo_count, inputs) # Compute mean values in bins histo.Divide(histo_count) return histo
def store(regressor, name, inputs, outputfile): # Save scikit-learn regression object result_dir = outputfile.GetName().replace('.root', '') if not os.path.exists(result_dir): os.mkdir(result_dir) joblib.dump(regressor, result_dir + '/' + name + '.pkl') # Save result in ROOT histograms if possible if len(inputs) != 2 and len(inputs) != 3: print 'The regression result will not be stored in a ROOT histogram. Only 2D or 3D histograms can be stored for the moment.' return for input in inputs: if not input in binning: print 'Binning is not defined for variable ' + input + '. Please add it in quantile_regression.binning if you want to store results in histograms' return if len(inputs) == 2: histo = function2th2(regressor.predict, binning[inputs[0]], binning[inputs[1]]) elif len(inputs) == 3: histo = Hist3D(*(binning[inputs[0]] + binning[inputs[1]] + binning[inputs[2]]), name=name) histo.SetXTitle(inputs[0]) histo.SetYTitle(inputs[1]) histo.SetZTitle(inputs[2]) for bx in histo.bins_range(0): x = histo.GetXaxis().GetBinCenter(bx) for by in histo.bins_range(1): y = histo.GetYaxis().GetBinCenter(by) for bz in histo.bins_range(2): z = histo.GetZaxis().GetBinCenter(bz) histo[bx, by, bz].value = regressor.predict([[x, y, z]]) outputfile.cd() histo.Write()
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_draw(): with root_open(FILE_PATHS[0]) as f: tree = f.tree tree.draw('a_x') tree.draw('a_x:a_y') tree.draw('a_x:TMath::Exp(a_y)') tree.draw('a_x:a_y:a_z') tree.draw('a_x:a_y:a_z:b_x') tree.draw('a_x:a_y:a_z:b_x:b_y', options='para') h1 = Hist(10, -1, 2, name='h1') h2 = Hist2D(10, -1, 2, 10, -1, 2) h3 = Hist3D(10, -1, 2, 10, -1, 2, 10, -1, 2) # dimensionality does not match assert_raises(TypeError, tree.draw, 'a_x:a_y', hist=h1) # name does not match assert_raises(ValueError, tree.draw, 'a_x>>+something', hist=h1) # hist is not a TH1 assert_raises(TypeError, tree.draw, 'a_x:a_y', hist=ROOT.TGraph()) # name does match and is fine (just redundant) tree.draw('a_x>>h1', hist=h1) assert_equal(h1.Integral() > 0, True) h1.Reset() tree.draw('a_x>>+h1', hist=h1) assert_equal(h1.Integral() > 0, True) h1.Reset() # both binning and hist are specified assert_raises(ValueError, tree.draw, 'a_x>>+h1(10, 0, 1)', hist=h1) tree.draw('a_x', hist=h1) assert_equal(h1.Integral() > 0, True) tree.draw('a_x:a_y', hist=h2) assert_equal(h2.Integral() > 0, True) tree.draw('a_x:a_y:a_z', hist=h3) assert_equal(h3.Integral() > 0, True) h3.Reset() tree.draw('a_x>0:a_y/2:a_z*2', hist=h3) assert_equal(h3.Integral() > 0, True) # create a histogram hist = tree.draw('a_x:a_y:a_z', create_hist=True) assert_equal(hist.Integral() > 0, True) hist = tree.draw('a_x:a_y:a_z>>new_hist_1') assert_equal(hist.Integral() > 0, True) assert_equal(hist.name, 'new_hist_1') # create_hist=True is redundant here hist = tree.draw('a_x:a_y:a_z>>new_hist_2', create_hist=True) assert_equal(hist.Integral() > 0, True) assert_equal(hist.name, 'new_hist_2')
def test_bounds(): h = Hist(10, 0, 1) assert_equal(h.bounds(), (0, 1)) h = Hist2D(10, 0, 1, 10, 1, 2) assert_equal(h.bounds(axis=0), (0, 1)) assert_equal(h.bounds(axis=1), (1, 2)) h = Hist3D(10, 0, 1, 10, 1, 2, 10, 2, 3) assert_equal(h.bounds(axis=0), (0, 1)) assert_equal(h.bounds(axis=1), (1, 2)) assert_equal(h.bounds(axis=2), (2, 3))
def test_quantiles(): h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.FillRandom('gaus') h3d.quantiles(2) h3d.quantiles(2, axis=1) h3d.quantiles([0, .5, 1], axis=2) h2d = Hist2D(100, 0, 1, 100, 0, 1) h2d.FillRandom(F2('x+y')) h2d.quantiles(4, axis=0) h2d.quantiles(4, axis=1)
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)
def test_rebinning(): h1d = Hist(100, 0, 1) h1d.FillRandom('gaus') assert_equal(h1d.rebinned(2).nbins(0), 50) assert_equal(h1d.rebinned((2, )).nbins(0), 50) assert_equal(h1d.rebinned([0, .5, 1]).nbins(0), 2) h3d = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3d.FillRandom('gaus') assert_equal(h3d.rebinned(2).nbins(0), 5) new = h3d.rebinned((2, 5, 1)) assert_equal(new.nbins(0), 5) assert_equal(new.nbins(1), 2) assert_equal(new.nbins(2), 10) new = h3d.rebinned([0, 5, 10], axis=1) assert_equal(new.nbins(1), 2)
def test_cuts(self): with root_open(self.file_paths[0]) as f: tree = f.tree h1 = Hist(10, -1, 2) h2 = Hist2D(10, -1, 2, 10, -1, 2) h3 = Hist3D(10, -1, 2, 10, -1, 2, 10, -1, 2) tree.draw('a_x', hist=h1) assert_equals(h1.Integral() > 0, True) tree.draw('a_x:a_y', hist=h2) assert_equals(h2.Integral() > 0, True) tree.draw('a_x:a_y:a_z', hist=h3) assert_equals(h3.Integral() > 0, True) h3.Reset() tree.draw('a_x>0:a_y/2:a_z*2', hist=h3) assert_equals(h3.Integral() > 0, True)
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_integral_error(): h = Hist(1, 0, 1) h.FillRandom('gaus') ref_integral, ref_error = h.integral(error=True) h1 = Hist(10, 0, 1) h1.FillRandom('gaus') integral, error = h1.integral(error=True) assert_almost_equal(integral, ref_integral) assert_almost_equal(error, ref_error) h2 = Hist2D(10, 0, 1, 10, 0, 1) h2.FillRandom(F2('x+y')) integral, error = h2.integral(error=True) assert_almost_equal(integral, ref_integral) assert_almost_equal(error, ref_error) h3 = Hist3D(10, 0, 1, 10, 0, 1, 10, 0, 1) h3.FillRandom(F3('x+y+z')) integral, error = h3.integral(error=True) assert_almost_equal(integral, ref_integral) assert_almost_equal(error, ref_error)
def function2th3(function, binsx, binsy, binsz, titlex='', titley='', titlez=''): histo = Hist3D(*(binsx+binsy+binsz)) histo.SetXTitle(titlex) histo.SetYTitle(titley) histo.SetZTitle(titlez) # Prepare array of inputs, one entry for each bin values = [] for bx in histo.bins_range(0): x = histo.GetXaxis().GetBinCenter(bx) for by in histo.bins_range(1): y = histo.GetYaxis().GetBinCenter(by) for bz in histo.bins_range(2): z = histo.GetZaxis().GetBinCenter(bz) values.append([x,y,z]) # Call function for each value results = function(np.array(values)) for result,value in zip(results, values): bx = histo.GetXaxis().FindBin(value[0]) by = histo.GetYaxis().FindBin(value[1]) bz = histo.GetZaxis().FindBin(value[2]) histo[bx,by,bz].value = result return histo
# creating histograms from rootpy.plotting import Hist3D # variable width bins hist3d = Hist3D([0, 3, 10, 100], [2.3, 4.2, 5.8, 10, 20, 25.5], [-100, 0, 20]) # easy to mix variable and fixed width bins with rootpy hist3d = Hist3D(3, 0, 5, [2.3, 4.2, 5.8, 10, 20, 25.5], [-100, 0, 20])
canvas.SetBottomMargin(0.15) canvas.SetTopMargin(0.10) canvas.SetRightMargin(0.05) h_simple.Draw() # create the legend legend = Legend([h_simple], pad=canvas, header='Header', leftmargin=0.05, rightmargin=0.5) legend.Draw() # 2D and 3D histograms are handled in the same way # the constructor arguments are repetitions of #bins, left bound, right bound. h2d = Hist2D(10, 0, 1, 50, -40, 10, name='2d hist') h3d = Hist3D(3, -1, 4, 10, -1000, -200, 2, 0, 1, name='3d hist') # variable-width bins may be created by passing the bin edges directly: h1d_variable = Hist([1, 4, 10, 100]) h2d_variable = Hist2D([2, 4, 7, 100, 200], [-100, -50, 0, 10, 20]) h3d_variable = Hist3D([1, 3, 10], [20, 50, 100], [-10, -5, 10, 20]) # variable-width and constant-width bins can be mixed: h2d_mixed = Hist2D([2, 10, 30], 10, 1, 5) # wait for you to close all open canvases before exiting # wait() will have no effect if ROOT is in batch mode: #ROOT.gROOT.SetBatch(True) #wait()
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)
=================================== Fill a histogram with a NumPy array =================================== This example demonstrates how a 1D, 2D, or 3D ROOT histogram can be efficiently filled with a NumPy array. """ print __doc__ import rootpy rootpy.log.basic_config_colorized() from rootpy.interactive import wait from rootpy.plotting import Canvas, Hist, Hist2D, Hist3D import numpy as np c1 = Canvas() a = Hist(1000, -5, 5) a.fill_array(np.random.randn(1000000)) a.Draw('hist') c2 = Canvas() b = Hist2D(100, -5, 5, 100, -5, 5) b.fill_array(np.random.randn(1000000, 2)) b.Draw('LEGO2Z0') c3 = Canvas() c = Hist3D(10, -5, 5, 10, -5, 5, 10, -5, 5) c.markersize = .3 c.fill_array(np.random.randn(10000, 3)) c.Draw('SCAT') wait(True)
def main(): if len(sys.argv) < 3: print("Usage: ToyMC [numberEvents] [randomSeed]") return numberEvents = int(sys.argv[1]) seed = int(sys.argv[2]) print( "==================================== TRAIN ====================================" ) f = root_open( "legotrain_350_20161117-2106_LHCb4_fix_CF_pPb_MC_ptHardMerged.root", "read" ) hJetPt = f.Get("AliJJetJtTask/AliJJetJtHistManager/JetPt/JetPtNFin{:02d}".format(2)) hZ = f.Get("AliJJetJtTask/AliJJetJtHistManager/Z/ZNFin{:02d}".format(2)) FillFakes = False dummy_variable = True weight = True NBINS = 50 LimL = 0.1 LimH = 500 logBW = (TMath.Log(LimH) - TMath.Log(LimL)) / NBINS LogBinsX = [LimL * math.exp(ij * logBW) for ij in range(0, NBINS + 1)] hJetPtMeas = Hist(LogBinsX) hJetPtTrue = Hist(LogBinsX) myRandom = TRandom3(seed) fEff = TF1("fEff", "1-0.5*exp(-x)") jetBinBorders = [5, 10, 20, 30, 40, 60, 80, 100, 150, 500] hJetPtMeasCoarse = Hist(jetBinBorders) hJetPtTrueCoarse = Hist(jetBinBorders) NBINSJt = 64 low = 0.01 high = 10 BinW = (TMath.Log(high) - TMath.Log(low)) / NBINSJt LogBinsJt = [low * math.exp(i * BinW) for i in range(NBINSJt + 1)] hJtTrue = Hist(LogBinsJt) hJtMeas = Hist(LogBinsJt) hJtFake = Hist(LogBinsJt) LogBinsPt = jetBinBorders jetPtBins = [(a, b) for a, b in zip(jetBinBorders, jetBinBorders[1:])] hJtTrue2D = Hist2D(LogBinsJt, LogBinsPt) hJtMeas2D = Hist2D(LogBinsJt, LogBinsPt) hJtFake2D = Hist2D(LogBinsJt, LogBinsPt) hJtMeasBin = [Hist(LogBinsJt) for i in jetBinBorders] hJtTrueBin = [Hist(LogBinsJt) for i in jetBinBorders] response = RooUnfoldResponse(hJtMeas, hJtTrue) response2D = RooUnfoldResponse(hJtMeas2D, hJtTrue2D) responseBin = [RooUnfoldResponse(hJtMeas, hJtTrue) for i in jetBinBorders] responseJetPt = RooUnfoldResponse(hJetPtMeas, hJetPtTrue) responseJetPtCoarse = RooUnfoldResponse(hJetPtMeasCoarse, hJetPtTrueCoarse) # 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 responses = [Hist3D(LogBinsJt, LogBinsPt, LogBinsJt) for i in jetPtBins] misses = Hist2D(LogBinsJt, LogBinsPt) fakes2D = Hist2D(LogBinsJt, LogBinsPt) outFile = TFile("tuple.root", "recreate") responseTuple = TNtuple( "responseTuple", "responseTuple", "jtObs:ptObs:jtTrue:ptTrue" ) hMultiTrue = Hist(50, 0, 50) hMultiMeas = Hist(50, 0, 50) hZMeas = Hist(50, 0, 1) hZTrue = Hist(50, 0, 1) hZFake = Hist(50, 0, 1) responseMatrix = Hist2D(LogBinsJt, LogBinsJt) numberJets = 0 numberFakes = 0 numberJetsMeasBin = [0 for i in jetBinBorders] numberJetsTrueBin = [0 for i in jetBinBorders] numberFakesBin = [0 for i in jetBinBorders] ieout = numberEvents / 10 if ieout > 10000: ieout = 10000 fakeRate = 1 start_time = datetime.now() print("Processing 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 = hJetPt.GetRandom() remainder = jetPt if jetPt < 5: continue nt = 0 nt_meas = 0 while remainder > 0: trackPt = hZ.GetRandom() * jetPt if trackPt < remainder: track = TVector3() remainder = remainder - trackPt else: trackPt = remainder remainder = -1 if trackPt > 0.15: track.SetPtEtaPhi( trackPt, myRandom.Gaus(0, 0.1), myRandom.Gaus(math.pi, 0.2) ) tracksTrue.append(track) jetTrue += track if fEff.Eval(trackPt) > myRandom.Uniform(0, 1): tracksMeas[nt] = 1 jetMeas += track nt_meas += 1 else: tracksMeas[nt] = 0 nt += 1 fakes = [] for it in range(fakeRate * 100): if myRandom.Uniform(0, 1) > 0.99: fake = TVector3() fake.SetPtEtaPhi( myRandom.Uniform(0.15, 1), myRandom.Gaus(0, 0.1), myRandom.Gaus(math.pi, 0.2), ) fakes.append(fake) jetMeas += fake hJetPtMeas.Fill(jetMeas.Pt()) hJetPtTrue.Fill(jetTrue.Pt()) responseJetPt.Fill(jetMeas.Pt(), jetTrue.Pt()) responseJetPtCoarse.Fill(jetMeas.Pt(), jetTrue.Pt()) hMultiTrue.Fill(nt) hMultiMeas.Fill(nt_meas) ij_meas = GetBin(jetBinBorders, jetMeas.Pt()) ij_true = GetBin(jetBinBorders, jetTrue.Pt()) if nt < 5 or nt_meas < 5: continue numberJets += 1 if ij_meas >= 0: numberJetsMeasBin[ij_meas] += 1 hJetPtMeasCoarse.Fill(jetMeas.Pt()) if ij_true >= 0: numberJetsTrueBin[ij_true] += 1 hJetPtTrueCoarse.Fill(jetTrue.Pt()) for track, it in zip(tracksTrue, range(100)): zTrue = (track * jetTrue.Unit()) / jetTrue.Mag() jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag() hZTrue.Fill(zTrue) if ij_true >= 0: if weight: hJtTrue.Fill(jtTrue, 1.0 / jtTrue) hJtTrueBin[ij_true].Fill(jtTrue, 1.0 / jtTrue) hJtTrue2D.Fill(jtTrue, jetTrue.Pt(), 1.0 / jtTrue) else: hJtTrue.Fill(jtTrue) hJtTrueBin[ij_true].Fill(jtTrue) hJtTrue2D.Fill(jtTrue, jetTrue.Pt()) if ij_meas >= 0: if tracksMeas[it] == 1: zMeas = (track * jetMeas.Unit()) / jetMeas.Mag() jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag() hZMeas.Fill(zMeas) if weight: hJtMeasBin[ij_meas].Fill(jtMeas, 1.0 / jtMeas) hJtMeas.Fill(jtMeas, 1.0 / jtMeas) hJtMeas2D.Fill(jtMeas, jetMeas.Pt(), 1.0 / jtMeas) else: hJtMeas.Fill(jtMeas) hJtMeasBin[ij_meas].Fill(jtMeas) hJtMeas2D.Fill(jtMeas, jetMeas.Pt()) response.Fill(jtMeas, jtTrue) responseBin[ij_true].Fill(jtMeas, jtTrue) response2D.Fill(jtMeas, jetMeas.Pt(), jtTrue, jetTrue.Pt()) responseMatrix.Fill(jtMeas, jtTrue) responses[ij_true].Fill(jtMeas, jetMeas.Pt(), jtTrue) responseTuple.Fill(jtMeas, jetMeas.Pt(), jtTrue, jetTrue.Pt()) else: response.Miss(jtTrue) responseBin[ij_true].Miss(jtTrue) response2D.Miss(jtTrue, jetTrue.Pt()) misses.Fill(jtTrue, jetTrue.Pt()) responseTuple.Fill(-1, -1, jtTrue, jetTrue.Pt()) if ij_meas >= 0: for fake in fakes: zFake = (fake * jetMeas.Unit()) / jetMeas.Mag() jtFake = (fake - scaleJet(jetMeas, zFake)).Mag() hZMeas.Fill(zFake) hZFake.Fill(zFake) if weight: hJtMeas.Fill(jtFake, 1.0 / jtFake) hJtMeasBin[ij_meas].Fill(jtFake, 1.0 / jtFake) hJtMeas2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake) hJtFake2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake) hJtFake.Fill(jtFake, 1.0 / jtFake) else: hJtMeas.Fill(jtFake) hJtMeasBin[ij_meas].Fill(jtFake) hJtMeas2D.Fill(jtFake, jetMeas.Pt()) hJtFake2D.Fill(jtFake, jetMeas.Pt()) hJtFake.Fill(jtFake) if FillFakes: response.Fake(jtFake) responseBin[ij_true].Fake(jtFake) response2D.Fake(jtFake, jetMeas.Pt()) fakes2D.Fill(jtFake, jetMeas.Pt()) responseTuple.Fill(jtFake, jetMeas.Pt(), -1, -1) numberFakes += 1 numberFakesBin[ij_true] += 1 response2Dtest = make2Dresponse( responses, jetPtBins, hJtMeas2D, hJtTrue2D, misses=misses, fakes=fakes2D ) if dummy_variable: hJetPtMeas.Reset() hJetPtTrue.Reset() hMultiTrue.Reset() hMultiMeas.Reset() hJetPtMeasCoarse.Reset() hJetPtTrueCoarse.Reset() hZTrue.Reset() hZMeas.Reset() hJtTrue.Reset() hJtTrue2D.Reset() hJtMeas.Reset() hJtMeas2D.Reset() hJtFake.Reset() hJtFake2D.Reset() for h, h2 in zip(hJtTrueBin, hJtMeasBin): h.Reset() h2.Reset() numberJetsMeasBin = [0 for i in jetBinBorders] numberJetsTrueBin = [0 for i in jetBinBorders] numberJets = 0 print("Create testing data") start_time = datetime.now() numberEvents = numberEvents / 2 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 = hJetPt.GetRandom() remainder = jetPt if jetPt < 5: continue nt = 0 nt_meas = 0 while remainder > 0: trackPt = hZ.GetRandom() * jetPt if trackPt < remainder: track = TVector3() remainder = remainder - trackPt else: trackPt = remainder remainder = -1 if trackPt > 0.15: track.SetPtEtaPhi( trackPt, myRandom.Gaus(0, 0.1), myRandom.Gaus(math.pi, 0.2) ) tracksTrue.append(track) jetTrue += track if fEff.Eval(trackPt) > myRandom.Uniform(0, 1): tracksMeas[nt] = 1 jetMeas += track nt_meas += 1 else: tracksMeas[nt] = 0 nt += 1 fakes = [] for it in range(fakeRate * 100): if myRandom.Uniform(0, 1) > 0.99: fake = TVector3() fake.SetPtEtaPhi( myRandom.Uniform(0.15, 1), myRandom.Gaus(0, 0.1), myRandom.Gaus(math.pi, 0.2), ) fakes.append(fake) jetMeas += fake hJetPtMeas.Fill(jetMeas.Pt()) hJetPtTrue.Fill(jetTrue.Pt()) hMultiTrue.Fill(nt) hMultiMeas.Fill(nt_meas) ij_meas = GetBin(jetBinBorders, jetMeas.Pt()) ij_true = GetBin(jetBinBorders, jetTrue.Pt()) if nt < 5 or nt_meas < 5: continue numberJets += 1 if ij_meas >= 0: numberJetsMeasBin[ij_meas] += 1 hJetPtMeasCoarse.Fill(jetMeas.Pt()) if ij_true >= 0: numberJetsTrueBin[ij_true] += 1 hJetPtTrueCoarse.Fill(jetTrue.Pt()) for track, it in zip(tracksTrue, range(100)): zTrue = (track * jetTrue.Unit()) / jetTrue.Mag() jtTrue = (track - scaleJet(jetTrue, zTrue)).Mag() hZTrue.Fill(zTrue) if ij_true >= 0: if weight: hJtTrue.Fill(jtTrue, 1.0 / jtTrue) hJtTrueBin[ij_true].Fill(jtTrue, 1.0 / jtTrue) hJtTrue2D.Fill(jtTrue, jetTrue.Pt(), 1.0 / jtTrue) else: hJtTrue.Fill(jtTrue) hJtTrueBin[ij_true].Fill(jtTrue) hJtTrue2D.Fill(jtTrue, jetTrue.Pt()) if ij_meas >= 0: if tracksMeas[it] == 1: zMeas = (track * jetMeas.Unit()) / jetMeas.Mag() jtMeas = (track - scaleJet(jetMeas, zMeas)).Mag() hZMeas.Fill(zMeas) if weight: hJtMeasBin[ij_meas].Fill(jtMeas, 1.0 / jtMeas) hJtMeas.Fill(jtMeas, 1.0 / jtMeas) hJtMeas2D.Fill(jtMeas, jetMeas.Pt(), 1.0 / jtMeas) else: hJtMeas.Fill(jtMeas) hJtMeasBin[ij_meas].Fill(jtMeas) hJtMeas2D.Fill(jtMeas, jetMeas.Pt()) if ij_meas >= 0: for fake in fakes: zFake = (fake * jetMeas.Unit()) / jetMeas.Mag() jtFake = (fake - scaleJet(jetMeas, zFake)).Mag() hZMeas.Fill(zFake) hZFake.Fill(zFake) if weight: hJtMeas.Fill(jtFake, 1.0 / jtFake) hJtMeasBin[ij_meas].Fill(jtFake, 1.0 / jtFake) hJtMeas2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake) hJtFake2D.Fill(jtFake, jetMeas.Pt(), 1.0 / jtFake) hJtFake.Fill(jtFake, 1.0 / jtFake) else: hJtMeas.Fill(jtFake) hJtMeasBin[ij_meas].Fill(jtFake) hJtMeas2D.Fill(jtFake, jetMeas.Pt()) hJtFake2D.Fill(jtFake, jetMeas.Pt()) hJtFake.Fill(jtFake) time_elapsed = datetime.now() - start_time print( "Event {} [{:.2f}%] Time Elapsed: {}".format( numberEvents, 100.0, fmtDelta(time_elapsed) ) ) if not FillFakes: hJtMeas.Add(hJtFake, -1) hJtMeas2D.Add(hJtFake2D, -1) responseTuple.Print() outFile.Write() # printTuple(responseTuple) hJtMeasProjBin = [ makeHist(hJtMeas2D.ProjectionX("histMeas{}".format(i), i, i), bins=LogBinsJt) for i in range(1, len(jetBinBorders)) ] hJtMeasProj = makeHist(hJtMeas2D.ProjectionX("histMeas"), bins=LogBinsJt) hJtTrueProjBin = [ makeHist(hJtTrue2D.ProjectionX("histTrue{}".format(i), i, i), bins=LogBinsJt) for i in range(1, len(jetBinBorders)) ] hJtTrueProj = makeHist(hJtTrue2D.ProjectionX("histTrue"), bins=LogBinsJt) hJtFakeProjBin = [ makeHist(hJtFake2D.ProjectionX("histFake{}".format(i), i, i), bins=LogBinsJt) for i in range(1, len(jetBinBorders)) ] if not FillFakes: for h, h2 in zip(hJtMeasBin, hJtFakeProjBin): h.Add(h2, -1) for h in ( hJtMeasProj, hJtTrueProj, hJtMeas, hJtTrue, hJtFake, hZFake, hZMeas, hZTrue, ): h.Scale(1.0 / numberJets, "width") for meas, true, n_meas, n_true in zip( hJtMeasBin, hJtTrueBin, numberJetsMeasBin, numberJetsTrueBin ): if n_meas > 0: meas.Scale(1.0 / n_meas, "width") if n_true > 0: true.Scale(1.0 / n_true, "width") numberJetsMeasFromHist = [ hJetPtMeasCoarse.GetBinContent(i) for i in range(1, hJetPtMeasCoarse.GetNbinsX() + 1) ] numberJetsTrueFromHist = [ hJetPtTrueCoarse.GetBinContent(i) for i in range(1, hJetPtTrueCoarse.GetNbinsX() + 1) ] print("Total number of jets: {}".format(numberJets)) print("Total number of fakes: {}".format(numberFakes)) print("Measured jets by bin") print(numberJetsMeasBin) print(numberJetsMeasFromHist) print("True jets by bin") print(numberJetsTrueBin) print(numberJetsTrueFromHist) hRecoJetPtCoarse = unfoldJetPt(hJetPtMeasCoarse, responseJetPtCoarse, jetBinBorders) numberJetsFromReco = [ hRecoJetPtCoarse.GetBinContent(i) for i in range(1, hRecoJetPtCoarse.GetNbinsX()) ] print("Unfolded jet numbers by bin:") print(numberJetsFromReco) print("Fakes by bin") print(numberFakesBin) print( "==================================== UNFOLD ===================================" ) unfold = RooUnfoldBayes(response, hJtMeas, 4) # OR unfoldSVD = RooUnfoldSvd(response, hJtMeas, 20) # OR unfold2D = RooUnfoldBayes(response2D, hJtMeas2D, 4) for u in (unfold, unfoldSVD, unfold2D): u.SetVerbose(0) # response2Dtest = makeResponseFromTuple(responseTuple,hJtMeas2D,hJtTrue2D) unfold2Dtest = RooUnfoldBayes(response2Dtest, hJtMeas2D, 4) unfoldBin = [ RooUnfoldBayes(responseBin[i], hJtMeasBin[i]) for i in range(len(jetBinBorders)) ] for u in unfoldBin: u.SetVerbose(0) hRecoBayes = makeHist(unfold.Hreco(), bins=LogBinsJt) hRecoSVD = makeHist(unfoldSVD.Hreco(), bins=LogBinsJt) hRecoBin = [ makeHist(unfoldBin[i].Hreco(), bins=LogBinsJt) for i in range(len(jetBinBorders)) ] hReco2D = make2DHist(unfold2D.Hreco(), xbins=LogBinsJt, ybins=LogBinsPt) hReco2Dtest = make2DHist(unfold2Dtest.Hreco(), xbins=LogBinsJt, ybins=LogBinsPt) hRecoJetPt = unfoldJetPt(hJetPtMeas, responseJetPt, LogBinsX) hReco2DProjBin = [ makeHist(hReco2D.ProjectionX("histReco{}".format(i), i, i), bins=LogBinsJt) for i in range(1, len(jetBinBorders)) ] hReco2DTestProjBin = [ makeHist( hReco2Dtest.ProjectionX("histRecoTest{}".format(i), i, i), bins=LogBinsJt ) for i in range(1, len(jetBinBorders)) ] hReco2DProj = makeHist(hReco2D.ProjectionX("histReco"), bins=LogBinsJt) hReco2DProj.Scale(1.0 / numberJets, "width") for h, h2, n in zip(hReco2DProjBin, hReco2DTestProjBin, numberJetsFromReco): if n > 0: h.Scale(1.0 / n, "width") h2.Scale(1.0 / n, "width") # unfold.PrintTable (cout, hJtTrue) for h, h2, nj in zip(hJtMeasProjBin, hJtFakeProjBin, numberJetsMeasBin): if nj > 0: h.Scale(1.0 / nj, "width") h2.Scale(1.0 / nj, "width") # else: # print("nj is 0 for {}".format(h.GetName())) for h, nj in zip(hJtTrueProjBin, numberJetsTrueBin): if nj > 0: h.Scale(1.0 / nj, "width") # draw8grid(hJtMeasBin[1:],hJtTrueBin[1:],jetPtBins[1:],xlog = True,ylog = True,name="newfile.pdf",proj = hJtMeasProjBin[2:], unf2d = hReco2DProjBin[2:], unf=hRecoBin[1:]) if numberEvents > 1000: if numberEvents > 1000000: filename = "ToyMC_{}M_events.pdf".format(numberEvents / 1000000) else: filename = "ToyMC_{}k_events.pdf".format(numberEvents / 1000) else: filename = "ToyMC_{}_events.pdf".format(numberEvents) draw8gridcomparison( hJtMeasBin, hJtTrueBin, jetPtBins, xlog=True, ylog=True, name=filename, proj=None, unf2d=hReco2DProjBin, unf2dtest=hReco2DTestProjBin, unf=hRecoBin, fake=hJtFakeProjBin, start=1, stride=1, ) drawQA( hJtMeas, hJtTrue, hJtFake, hRecoBayes, hRecoSVD, hReco2DProj, hZ, hZTrue, hZMeas, hZFake, hMultiMeas, hMultiTrue, hJetPt, hJetPtTrue, hJetPtMeas, hRecoJetPt, responseMatrix, ) outFile.Close()