def _project(tree, var, selection='', weight=1.0, bins=None, includeover=False): h = None if var.count(':') == 0: ## Hist (1D) if bins: if isinstance(bins, tuple): assert len(bins) == 3 h = Hist(*bins) elif isinstance(bins, list): h = Hist(bins) else: assert False else: assert False elif var.count(':') == 1: ## Hist2D ## like rootpy, we use a convention where var=x:y, unlike ROOT varx, vary = var.split(':') var = ':'.join([vary, varx]) if bins: if isinstance(bins, tuple): assert len(bins) == 6 h = Hist2D(*bins) elif isinstance(bins, list): ## TODO: support variable bins for Hist2D h = Hist2D(*bins) #assert False else: assert False else: assert False else: assert False assert h # kwargs['hist'] = h ## add the weight to the selection via a TCut weighted_selection = str(selection) if weight and weight != 1.0: weighted_selection = Cut(weighted_selection) weighted_selection = weighted_selection * weight weighted_selection = str(weighted_selection) tree.Draw('%s>>%s' % (var, h.GetName()), weighted_selection) ## print tree.GetSelectedRows() ## debuging # for event in t: # x = getattr(event, var) # h.fill(x) if h: h.SetDirectory(0) # elist = ROOT.gDirectory.Get('elist') # elist.Print('all') if var.count(':') == 0 and includeover: ## include overflow for Hist (1D) nbins = h.GetNbinsX() c1 = h.GetBinContent(nbins) c2 = h.GetBinContent(nbins + 1) e1 = h.GetBinError(nbins) e2 = h.GetBinError(nbins + 1) h.SetBinContent(nbins, c1 + c2) h.SetBinError( nbins, math.sqrt((c1 * e1 * e1 + c2 * e2 * e2) / (c1 + c2)) if c1 + c2 != 0.0 else 0.0) h.SetBinContent(nbins + 1, 0.0) h.SetBinError(nbins + 1, 0.0) return h
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()
def unfolding_toy_diagnostics(indir, variable): plotter = BasePlotter(defaults={ 'clone': False, 'name_canvas': True, 'show_title': True, 'save': { 'png': True, 'pdf': False } }, ) styles = { 'dots': { 'linestyle': 0, 'markerstyle': 21, 'markercolor': 1 }, 'compare': { 'linesstyle': [1, 0], 'markerstyle': [0, 21], 'markercolor': [2, 1], 'linecolor': [2, 1], 'drawstyle': ['hist', 'pe'], 'legendstyle': ['l', 'p'] } } xaxislabel = set_pretty_label(variable) true_distribution = None curdir = os.getcwd() os.chdir(indir) toydirs = get_immediate_subdirectories(".") methods = [] pulls_lists = {} pull_means_lists = {} pull_mean_errors_lists = {} pull_sums_lists = {} pull_sigmas_lists = {} pull_sigma_errors_lists = {} deltas_lists = {} delta_means_lists = {} delta_mean_errors_lists = {} delta_sigmas_lists = {} delta_sigma_errors_lists = {} ratio_sums_lists = {} nneg_bins_lists = {} unfoldeds_lists = {} unfolded_sigmas_lists = {} taus_lists = {} histos_created = False lists_created = False idir = 0 true_distro = None #loop over toys for directory in toydirs: if not directory.startswith('toy_'): continue os.chdir(directory) log.debug('Inspecting toy %s' % directory) idir = idir + 1 i = 0 if not os.path.isfile("result_unfolding.root"): raise ValueError('root file not found in %s' % os.getcwd()) with io.root_open("result_unfolding.root") as inputfile: log.debug('Iteration %s over the file' % i) i = i + 1 if not methods: keys = [i.name for i in inputfile.keys()] for key in keys: if hasattr(getattr(inputfile, key), "hdata_unfolded"): methods.append(key) unfolded_hists = [ inputfile.get('%s/hdata_unfolded' % i) for i in methods ] unfolded_wps_hists = [ inputfile.get('%s/hdata_unfolded_ps_corrected' % i) for i in methods ] for unf, unfps, method in zip(unfolded_hists, unfolded_wps_hists, methods): unf.name = method unfps.name = method if true_distro is None: true_distribution = inputfile.true_distribution ROOT.TH1.AddDirectory(False) true_distro = true_distribution.Clone() taus = prettyjson.loads(inputfile.best_taus.GetTitle()) if len(taus_lists) == 0: taus_lists = dict((i, []) for i in taus) for i, t in taus.iteritems(): taus_lists[i].append(t) for histo in unfolded_hists: #create pull/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: for ibin in range(1, nbins + 1): outname = "pull_" + name + "_bin" + str(ibin) pulls_lists[outname] = [] outname = "delta_" + name + "_bin" + str(ibin) deltas_lists[outname] = [] outname = "unfolded_" + name + "_bin" + str(ibin) unfoldeds_lists[outname] = [] unfolded_sigmas_lists[outname] = [] outname = "pull_" + name pull_means_lists[outname] = {} pull_mean_errors_lists[outname] = {} pull_sigmas_lists[outname] = {} pull_sigma_errors_lists[outname] = {} outname = "delta_" + name delta_means_lists[outname] = {} delta_mean_errors_lists[outname] = {} delta_sigmas_lists[outname] = {} delta_sigma_errors_lists[outname] = {} for ibin in range(1, nbins + 1): outname = "pull_" + name + "_bin" + str(ibin) unfolded_bin_content = histo.GetBinContent(ibin) unfolded_bin_error = histo.GetBinError(ibin) true_bin_content = true_distro.GetBinContent(ibin) true_bin_error = true_distro.GetBinError(ibin) total_bin_error = math.sqrt(unfolded_bin_error**2) #??? if (total_bin_error != 0): pull = (unfolded_bin_content - true_bin_content) / total_bin_error else: pull = 9999 log.debug( 'unfolded bin content %s +/- %s, true bin content %s, pull %s' % (unfolded_bin_content, unfolded_bin_error, true_bin_content, pull)) pulls_lists[outname].append(pull) outname = "delta_" + name + "_bin" + str(ibin) delta = unfolded_bin_content - true_bin_content log.debug( 'unfolded bin content %s +/- %s, true bin content %s, delta %s' % (unfolded_bin_content, unfolded_bin_error, true_bin_content, delta)) deltas_lists[outname].append(delta) outname = "unfolded_" + name + "_bin" + str(ibin) unfoldeds_lists[outname].append(unfolded_bin_content) unfolded_sigmas_lists[outname].append(unfolded_bin_error) nneg_bins_hists = [ i for i in inputfile.keys() if i.GetName().startswith("nneg_bins") ] nneg_bins_hists = [asrootpy(i.ReadObj()) for i in nneg_bins_hists] for histo in nneg_bins_hists: #create pull/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: outname = name nneg_bins_lists[outname] = [] outname = name nneg_bins_lists[outname].append(histo.GetBinContent(1)) pull_sums_hists = [ i for i in inputfile.keys() if i.GetName().startswith("sum_of_pulls") ] pull_sums_hists = [asrootpy(i.ReadObj()) for i in pull_sums_hists] for histo in pull_sums_hists: #create pull/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: outname = name pull_sums_lists[outname] = [] outname = name pull_sums_lists[outname].append(histo.GetBinContent(1)) ratio_sums_hists = [ i for i in inputfile.keys() if i.GetName().startswith("sum_of_ratios") ] ratio_sums_hists = [ asrootpy(i.ReadObj()) for i in ratio_sums_hists ] for histo in ratio_sums_hists: #create ratio/delta containers during first iteration name = histo.name nbins = histo.nbins() log.debug("name = %s, n bins = %s" % (name, nbins)) if not lists_created: outname = name ratio_sums_lists[outname] = [] outname = name ratio_sums_lists[outname].append(histo.GetBinContent(1)) #after the first iteration on the file all the lists are created lists_created = True os.chdir("..") #create histograms #histo containers taus = {} for name, vals in taus_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if val_min == val_max: if tau_nbins % 2: #if odd val_min, val_max = val_min - 0.01, val_min + 0.01 else: brange = 0.02 bwidth = brange / tau_nbins val_min, val_max = val_min - 0.01 + bwidth / 2., val_min + 0.01 + bwidth / 2. title = '#tau choice - %s ;#tau;N_{toys}' % (name) histo = Hist(tau_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) taus[name] = histo pulls = {} for name, vals in pulls_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max abs_max = max(abs(val_min), abs(val_max)) if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Pulls - %s - %s ;Pull;N_{toys}' % (binno, method) histo = Hist(pull_nbins, -abs_max, abs_max, name=name, title=title) for val in vals: histo.Fill(val) pulls[name] = histo deltas = {} for name, vals in deltas_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Deltas - %s - %s ;Delta;N_{toys}' % (binno, method) histo = Hist(delta_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) deltas[name] = histo unfoldeds = {} for name, vals in unfoldeds_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Unfoldeds - %s - %s ;Unfolded;N_{toys}' % (binno, method) histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) unfoldeds[name] = histo nneg_bins = {} for name, vals, in nneg_bins_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0 if val_min > 0 else val_min - 1 val_max = max(vals) val_max = 0 if val_max < 0 else val_max + 1 if 'L_curve' in name: method = 'L_curve' else: set_trace() _, method, _ = tuple(name.split('_')) title = 'N of negative bins - %s ;N. neg bins;N_{toys}' % method histo = Hist(int(val_max - val_min + 1), val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) nneg_bins[name] = histo pull_sums = {} for name, vals in pull_sums_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' else: set_trace() _, _, _, _, _, method = tuple(name.split('_')) title = 'Pull sums - %s ;#Sigma(pull)/N_{bins};N_{toys}' % method histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) pull_sums[name] = histo ratio_sums = {} for name, vals in ratio_sums_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: set_trace() _, _, _, _, _, method = tuple(name.split('_')) title = 'Ratio sums - %s;#Sigma(ratio)/N_{bins};N_{toys}' % method histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) ratio_sums[name] = histo unfolded_sigmas = {} for name, vals in unfolded_sigmas_lists.iteritems(): ROOT.TH1.AddDirectory(False) #repeat, you never know val_min = min(vals) val_min = 0.8 * val_min if val_min > 0 else 1.2 * val_min val_max = max(vals) val_max = 0.8 * val_max if val_max < 0 else 1.2 * val_max if 'L_curve' in name: method = 'L_curve' binno = name.split('_')[-1] else: _, method, binno = tuple(name.split('_')) title = 'Unfolded uncertainties - %s - %s ;Uncertainty;N_{toys}' % ( binno, method) histo = Hist(unfolded_nbins, val_min, val_max, name=name, title=title) for val in vals: histo.Fill(val) unfolded_sigmas[name] = histo for name, histo in pulls.iteritems(): log.debug("name is %s and object type is %s" % (name, type(histo))) histo.Fit("gaus", 'Q') if not histo.GetFunction("gaus"): log.warning("Function not found for histogram %s" % name) continue mean = histo.GetFunction("gaus").GetParameter(1) meanError = histo.GetFunction("gaus").GetParError(1) sigma = histo.GetFunction("gaus").GetParameter(2) sigmaError = histo.GetFunction("gaus").GetParError(2) general_name, idx = tuple(name.split('_bin')) idx = int(idx) pull_means_lists[general_name][idx] = mean pull_mean_errors_lists[general_name][idx] = meanError pull_sigmas_lists[general_name][idx] = sigma pull_sigma_errors_lists[general_name][idx] = sigmaError for name, histo in deltas.iteritems(): log.debug("name is %s and object type is %s" % (name, type(histo))) histo.Fit("gaus", 'Q') if not histo.GetFunction("gaus"): log.warning("Function not found for histogram %s" % name) continue mean = histo.GetFunction("gaus").GetParameter(1) meanError = histo.GetFunction("gaus").GetParError(1) sigma = histo.GetFunction("gaus").GetParameter(2) sigmaError = histo.GetFunction("gaus").GetParError(2) general_name, idx = tuple(name.split('_bin')) idx = int(idx) delta_means_lists[general_name][idx] = mean delta_mean_errors_lists[general_name][idx] = meanError delta_sigmas_lists[general_name][idx] = sigma delta_sigma_errors_lists[general_name][idx] = sigmaError outfile = rootpy.io.File("unfolding_diagnostics.root", "RECREATE") outfile.cd() pull_means = {} pull_sigmas = {} pull_means_summary = {} pull_sigmas_summary = {} delta_means = {} delta_sigmas = {} delta_means_summary = {} delta_sigmas_summary = {} for outname, pmeans in pull_means_lists.iteritems(): outname_mean = outname + "_mean" outtitle = "Pull means - " + outname + ";Pull mean; N_{toys}" pull_mean_min = min(pmeans.values()) pull_mean_max = max(pmeans.values()) pull_mean_newmin = pull_mean_min - (pull_mean_max - pull_mean_min) * 0.5 pull_mean_newmax = pull_mean_max + (pull_mean_max - pull_mean_min) * 0.5 pull_means[outname] = plotting.Hist(pull_mean_nbins, pull_mean_newmin, pull_mean_newmax, name=outname_mean, title=outtitle) outname_mean_summary = outname + "_mean_summary" outtitle_mean_summary = "Pull mean summary - " + outname histocloned = true_distro.Clone(outname_mean_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Pull mean' histocloned.title = outtitle_mean_summary pull_means_summary[outname] = histocloned for idx, pmean in pmeans.iteritems(): pull_means[outname].Fill(pmean) histocloned[idx].value = pmean histocloned[idx].error = pull_mean_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(pmeans.values()), max(pmeans.values())) for outname, psigmas in pull_sigmas_lists.iteritems(): outname_sigma = outname + "_sigma" outtitle_sigma = "Pull #sigma's - " + outname + ";Pull #sigma; N_{toys}" pull_sigma_min = min(psigmas.values()) pull_sigma_max = max(psigmas.values()) pull_sigma_newmin = pull_sigma_min - (pull_sigma_max - pull_sigma_min) * 0.5 pull_sigma_newmax = pull_sigma_max + (pull_sigma_max - pull_sigma_min) * 0.5 pull_sigmas[outname] = plotting.Hist(pull_sigma_nbins, pull_sigma_newmin, pull_sigma_newmax, name=outname_sigma, title=outtitle_sigma) outname_sigma_summary = outname + "_sigma_summary" outtitle_sigma_summary = "Pull #sigma summary - " + outname histocloned = true_distro.Clone(outname_sigma_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Pull #sigma' histocloned.title = outtitle_sigma_summary pull_sigmas_summary[outname] = histocloned for idx, psigma in psigmas.iteritems(): pull_sigmas[outname].Fill(psigma) histocloned[idx].value = psigma histocloned[idx].error = pull_sigma_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(psigmas.values()), max(psigmas.values())) for outname, dmeans in delta_means_lists.iteritems(): outname_mean = outname + "_mean" outtitle = "Delta means - " + outname + ";Delta mean; N_{toys}" delta_mean_min = min(dmeans.values()) delta_mean_max = max(dmeans.values()) delta_mean_newmin = delta_mean_min - (delta_mean_max - delta_mean_min) * 0.5 delta_mean_newmax = delta_mean_max + (delta_mean_max - delta_mean_min) * 0.5 delta_means[outname] = plotting.Hist(delta_mean_nbins, delta_mean_newmin, delta_mean_newmax, name=outname_mean, title=outtitle) outname_mean_summary = outname + "_mean_summary" outtitle_mean_summary = "Delta mean summary - " + outname histocloned = true_distro.Clone(outname_mean_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Delta mean' histocloned.title = outtitle_mean_summary delta_means_summary[outname] = histocloned for idx, dmean in dmeans.iteritems(): delta_means[outname].Fill(dmean) histocloned[idx].value = dmean histocloned[idx].error = delta_mean_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(dmeans.values()), max(dmeans.values())) for outname, dsigmas in delta_sigmas_lists.iteritems(): outname_sigma = outname + "_sigma" outtitle_sigma = "Delta #sigma's - " + outname + ";Delta #sigma; N_{toys}" delta_sigma_min = min(dsigmas.values()) delta_sigma_max = max(dsigmas.values()) delta_sigma_newmin = delta_sigma_min - (delta_sigma_max - delta_sigma_min) * 0.5 delta_sigma_newmax = delta_sigma_max + (delta_sigma_max - delta_sigma_min) * 0.5 delta_sigmas[outname] = plotting.Hist(delta_sigma_nbins, delta_sigma_newmin, delta_sigma_newmax, name=outname_sigma, title=outtitle_sigma) outname_sigma_summary = outname + "_sigma_summary" outtitle_sigma_summary = "Delta #sigma summary - " + outname histocloned = true_distro.Clone(outname_sigma_summary) histocloned.Reset() histocloned.xaxis.title = xaxislabel histocloned.yaxis.title = 'Delta #sigma' histocloned.title = outtitle_sigma_summary delta_sigmas_summary[outname] = histocloned for idx, dsigma in dsigmas.iteritems(): delta_sigmas[outname].Fill(dsigma) histocloned[idx].value = dsigma histocloned[idx].error = delta_sigma_errors_lists[outname][idx] histocloned.yaxis.SetRangeUser(min(dsigmas.values()), max(dsigmas.values())) unfolded_summary = {} unfolded_average = {} unfolded_envelope = {} for name, histo in unfoldeds.iteritems(): log.debug("name is %s and object type is %s" % (name, type(histo))) histo.Fit("gaus", 'Q') if not histo.GetFunction("gaus"): log.warning("Function not found for histogram %s" % name) continue mean = histo.GetFunction("gaus").GetParameter(1) meanError = histo.GetFunction("gaus").GetParError(1) sigma = histo.GetFunction("gaus").GetParameter(2) sigmaError = histo.GetFunction("gaus").GetParError(2) general_name, idx = tuple(name.split('_bin')) idx = int(idx) if general_name not in unfolded_summary: histo = true_distro.Clone("%s_unfolded_summary" % general_name) outtitle_unfolded_summary = "Unfolded summary - " + general_name histo.Reset() histo.xaxis.title = xaxislabel histo.yaxis.title = 'N_{events}' histo.title = outtitle_unfolded_summary unfolded_summary[general_name] = histo unfolded_envelope[general_name] = histo.Clone( "%s_unfolded_envelope" % general_name) unfolded_average[general_name] = histo.Clone( "%s_unfolded_average" % general_name) unfolded_summary[general_name][idx].value = mean unfolded_summary[general_name][idx].error = meanError unfolded_envelope[general_name][idx].value = mean unfolded_envelope[general_name][idx].error = sigma unfolded_average[general_name][idx].value = mean unfolded_average[general_name][idx].error = \ unfolded_sigmas['%s_bin%i' % (general_name, idx)].GetMean() plotter.set_subdir('taus') for name, histo in taus.iteritems(): #canvas = plotter.create_and_write_canvas_single(0, 21, 1, False, False, histo, write=False) plotter.canvas.cd() histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) info = plotter.make_text_box( 'mode #tau = %.5f' % histo[histo.GetMaximumBin()].x.center, position=(plotter.pad.GetLeftMargin(), plotter.pad.GetTopMargin(), 0.3, 0.025)) info.Draw() plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('pulls') for name, histo in pulls.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in pull_means.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() for name, histo in pull_sigmas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() plotter.set_subdir('pull_summaries') for name, histo in pull_means_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) line = ROOT.TLine(histo.GetBinLowEdge(1), 0, histo.GetBinLowEdge(histo.GetNbinsX() + 1), 0) line.Draw("same") plotter.save() histo.Write() plotter.canvas.Write() for name, histo in pull_sigmas_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) line = ROOT.TLine(histo.GetBinLowEdge(1), 1, histo.GetBinLowEdge(histo.GetNbinsX() + 1), 1) line.Draw("same") plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('deltas') for name, histo in deltas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in delta_means.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() for name, histo in delta_sigmas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.Write() plotter.save() plotter.set_subdir('delta_summaries') for name, histo in delta_means_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in delta_sigmas_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) #histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('unfolding_unc') for name, histo in unfolded_sigmas.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('unfolded') for name, histo in unfoldeds.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() plotter.set_subdir('unfolded_summaries') for name, histo in unfolded_summary.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in unfolded_summary.iteritems(): leg = LegendDefinition("Unfolding comparison", 'NE', labels=['Truth', 'Unfolded']) plotter.overlay_and_compare([true_distro], histo, legend_def=leg, **styles['compare']) plotter.canvas.name = 'Pull_' + name plotter.save() plotter.canvas.Write() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, method='ratio', **styles['compare']) plotter.canvas.name = 'Ratio_' + name plotter.save() plotter.canvas.Write() plotter.set_subdir('unfolded_average') for name, histo in unfolded_average.iteritems(): leg = LegendDefinition("Unfolding comparison", 'NE', labels=['Truth', 'Unfolded']) #set_trace() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, **styles['compare']) plotter.canvas.name = 'Pull_' + name plotter.save() plotter.canvas.Write() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, method='ratio', **styles['compare']) plotter.canvas.name = 'Ratio_' + name plotter.save() plotter.canvas.Write() plotter.set_subdir('unfolded_envelope') for name, histo in unfolded_envelope.iteritems(): leg = LegendDefinition("Unfolding comparison", 'NE', labels=['Truth', 'Unfolded']) plotter.overlay_and_compare([true_distro], histo, legend_def=leg, **styles['compare']) plotter.canvas.name = 'Pull_' + name plotter.save() plotter.canvas.Write() plotter.overlay_and_compare([true_distro], histo, legend_def=leg, method='ratio', **styles['compare']) plotter.canvas.name = 'Ratio_' + name plotter.save() plotter.canvas.Write() plotter.set_subdir('figures_of_merit') for name, histo in nneg_bins.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in pull_sums.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() for name, histo in ratio_sums.iteritems(): histo = plotter.plot(histo, **styles['dots']) histo.SetStats(True) plotter.save() histo.Write() plotter.canvas.Write() outfile.close() os.chdir(curdir)
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()
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()