def getDataFromFile(variable, whichBinFromFile): dataTemplate = inputTemplates[variable]['data'][whichBinFromFile] nBins = len(inputTemplates[variable]['data'][whichBinFromFile]) h_data = Hist(nBins, 0, nBins, title='data') for bin in range(1, nBins + 1): h_data.SetBinContent(bin, dataTemplate[bin - 1]) pass h_data.Scale(absolute_eta_initialValues['data'][whichBinFromFile][0]) h_data.Sumw2() for bin in range(1, nBins + 1): h_data.SetBinError(bin, sqrt(h_data.GetBinContent(bin))) pass return h_data
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()
if useT2: templates['t2'] = h_t2 if useT3: templates['t3'] = h_t3 if useT4: templates['t4'] = h_t4 fit_data = FitData(h_data, templates, fit_boundaries=(0, h_data.nbins())) fit_collection = FitDataCollection() fit_collection.add(fit_data) minuit_fitter = Minuit(fit_collection, method='logLikelihood', verbose=True) minuit_fitter.fit() results = minuit_fitter.readResults() c.cd(2) ymax = h_data.GetBinContent(h_data.GetMaximumBin()) * 1.1 h_data.GetYaxis().SetRangeUser(0, ymax) h_data.Draw('PE') leg = Legend(nTemplates + 2) leg.AddEntry(h_data, style='LEP') h_tSumAfter = 0 print '----> Target \t Fit Result' if useT1: h_t1After = h_t1.Clone() h_t1After.Scale(results['t1'][0] / h_t1.Integral()) h_t1After.Draw('SAME HIST') h_tSumAfter += h_t1After leg.AddEntry(h_t1After, style='L') t1AfterCont = h_t1.Integral() * t1Scale * h_data.Integral() / ( h_t1.Integral() * t1Scale + h_t2.Integral() * t2Scale +
bin = h_response.FindBin(12, 12) h_response.SetBinContent(bin, 400) h_response.SetBinError(bin, sqrt(h_response.GetBinContent(bin))) bin = h_response.FindBin(20, 20) h_response.SetBinContent(bin, 100) h_response.SetBinError(bin, sqrt(h_response.GetBinContent(bin))) bin = h_response.FindBin(50, 50) h_response.SetBinContent(bin, 5) h_response.SetBinError(bin, sqrt(h_response.GetBinContent(bin))) # Set errors in all histograms for bin in range(0, len(bins)): h_truth.SetBinError(bin, sqrt(h_truth.GetBinContent(bin))) h_measured.SetBinError(bin, sqrt(h_measured.GetBinContent(bin))) h_data.SetBinError(bin, sqrt(h_data.GetBinContent(bin))) # # Alternatively use histograms from our main analysis # # measurement_config = XSectionConfig( 13 ) # channel = 'electron' # h_truth, h_measured, h_response, h_fakes = get_unfold_histogram_tuple( inputfile = File( measurement_config.unfolding_central, 'read' ), # variable = 'MET', # channel = channel, # met_type = 'patType1CorrectedPFMet', # centre_of_mass = 13, # ttbar_xsection = measurement_config.ttbar_xsection,
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()
xrange(sci_trigger_r_obj.begin_entry, sci_trigger_r_obj.end_entry)): sci_trigger_r_obj.t_trigger.get_entry(i) if not sci_trigger_r_obj.t_trigger.abs_gps_valid: continue trigger_hist.fill((sci_trigger_r_obj.t_trigger.abs_gps_week - sci_trigger_r_obj.start_week) * 604800 + (sci_trigger_r_obj.t_trigger.abs_gps_second - sci_trigger_r_obj.start_second)) for j in xrange(25): if sci_trigger_r_obj.t_trigger.trig_accepted[j]: modules_hist[j].fill((sci_trigger_r_obj.t_trigger.abs_gps_week - sci_trigger_r_obj.start_week) * 604800 + (sci_trigger_r_obj.t_trigger.abs_gps_second - sci_trigger_r_obj.start_second)) for i in xrange(1, nbins + 1): trigger_hist.SetBinContent(i, trigger_hist.GetBinContent(i) / args.bw) trigger_hist.SetBinError(i, trigger_hist.GetBinError(i) / args.bw) for j in xrange(25): modules_hist[j].SetBinContent( i, modules_hist[j].GetBinContent(i) / args.bw) modules_hist[j].SetBinError(i, modules_hist[j].GetBinError(i) / args.bw) print ' - drawing ... ' y_max = 0 for i in xrange(25): if modules_hist[i].GetMaximum() > y_max: y_max = modules_hist[i].GetMaximum() for i in xrange(25): modules_hist[i].SetMaximum(y_max * 1.1) canvas_trigger = Canvas(1000,
class Test(unittest.TestCase): def setUp(self): # create histograms self.h1 = Hist(60, 40, 100, title='1D') self.h2 = Hist2D(60, 40, 100, 100, 40, 140) # fill the histograms with our distributions map(self.h1.Fill, x1) self.h2.fill_array( np.random.multivariate_normal(mean=(100, 140), cov=np.arange(4).reshape(2, 2), size=(int(1E6), ))) # map(h2.Fill, (x1, x2, 1)) def tearDown(self): pass def test_overflow_1D(self): last_bin = self.h1.nbins() overflow_bin = last_bin + 1 overflow = self.h1.GetBinContent(overflow_bin) last_bin_content = self.h1.GetBinContent(last_bin) self.assertGreater(overflow, 0, '1D hist: No overflow present, wrong setup.') h1 = fix_overflow(self.h1) self.assertEqual(h1.GetBinContent(overflow_bin), 0., '1D hist: Overflow bin is not 0.') self.assertEqual(h1.GetBinContent(last_bin), last_bin_content + overflow, '1D hist: last bin is not correct.') def test_overflow_2D(self): before_fix = check_overflow_in_2DHist(self.h2) has_overflow_in_x = before_fix['has_overflow_in_x'] has_overflow_in_y = before_fix['has_overflow_in_y'] self.assertGreater(has_overflow_in_x, 0, '2D hist: No overflow in x present, wrong setup.') self.assertGreater(has_overflow_in_y, 0, '2D hist: No overflow in y present, wrong setup.') h2 = fix_overflow(self.h2) after_fix = check_overflow_in_2DHist(h2) has_overflow_in_x = after_fix['has_overflow_in_x'] has_overflow_in_y = after_fix['has_overflow_in_y'] # check if overflow has been reset self.assertEqual(has_overflow_in_x, 0, '2D hist: Overflow in x is not 0.') self.assertEqual(has_overflow_in_y, 0, '2D hist: Overflow in y is not 0.') # now check if new last bin content is equal to the old one plus overflow overflow_x_before = before_fix['overflow_x'] overflow_y_before = before_fix['overflow_y'] last_bin_content_x_before = before_fix['last_bin_content_x'] last_bin_content_y_before = before_fix['last_bin_content_y'] last_bin_content_x_after = after_fix['last_bin_content_x'] last_bin_content_y_after = after_fix['last_bin_content_y'] check_last_bin_content_x = [ overflow + last_bin_content for overflow, last_bin_content in zip( overflow_x_before, last_bin_content_x_before) ] check_last_bin_content_y = [ overflow + last_bin_content for overflow, last_bin_content in zip( overflow_y_before, last_bin_content_y_before) ] # remember, the last item in each list is actually the overflow, which should be 0 and the above calculation is not correct. self.assertTrue( check_equal_lists(check_last_bin_content_x[:-2], last_bin_content_x_after[:-2]), '2D hist: last bins in x are not correct.') self.assertTrue( check_equal_lists(check_last_bin_content_y[:-2], last_bin_content_y_after[:-2]), '2D hist: last bins in y are not correct.')
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()