def UnfoldFF(rawFF, detResponse, tag): padFF.Clear() padFF.Divide(2) # Detector response ResponseNorm(detResponse) padFF.cd(1) detResponse.Draw('COLZ') response = RooUnfoldResponse(rawFF, rawFF, detResponse) bayes = RooUnfoldBayes(response, rawFF) # Unfolding - Bayesian ana_util.COLOR = SelectColor(0) ana_util.MARKER = SelectMarker(0) padFF.cd(2) # Legend lgd = TLegend(0.12, 0.6, 0.3, 0.85) lgd.SetName('lgd' + tag) lgd.SetBorderSize(0) lgd.SetFillColor(0) # Z measured rawFF.SetBins(10, 0, 1.0) DrawFF(rawFF, 'hRaw_' + tag) lgd.AddEntry(rawFF, 'Raw') for nIter in range(4, 5): bayes.SetIterations(nIter) hist = DrawFF(bayes.Hreco(0), 'hBayes' + repr(nIter) + '_' + tag) for ix in range(1, hist.GetNbinsX()): hist.SetBinError(ix, rawFF.GetBinError(ix)) lgd.AddEntry(hist, 'Bayes (N=%d)' % nIter) lgd.Draw('same') padFF.Print(printFile, 'Title:' + tag) padFF.Write('c' + tag)
def unfold_real_data(self): logging.info('Unfolding real data') regularisation = int(self.params['regularisation'].m) unfold_bg = self.params['unfold_bg'].value unfold_eff = self.params['unfold_eff'].value raw_data_0 = self._data['nuall'] if regularisation == 0: raise AssertionError('Regularisation is set to 0') # Get the inversed efficiency histogram if not unfold_eff: inv_eff = self.get_inv_eff() # Load response object from disk cache response = self.create_response() # Background Subtraction if unfold_bg: raw_data_1 = raw_data_0 else: bg_hist = self.get_bg_hist() raw_data_1 = raw_data_0 - bg_hist r_flat = roounfold._flatten_to_1d(raw_data_1) r_th1d = convert_to_th1d(r_flat, errors=True) # Unfold unfold = RooUnfoldBayes(response, r_th1d, regularisation) unfold.SetVerbose(0) unfolded_flat = unfold.Hreco(1) unfold_map = unflatten_thist(in_th1d=unfolded_flat, binning=self.true_binning, name=self.output_str, errors=True) # Efficiency correction if not unfold_eff: unfold_map *= inv_eff del r_th1d del unfold logging.info('Unfolded reco sum {0}'.format( np.sum(unp.nominal_values(unfold_map.hist)))) return unfold_map
def unfolderFactory(self, response, hMeas, BasisSpline_m0=0): if "Bayes" in self.optunf: # Bayes unfoldung until chi^2 cut (max 10): unfold = RooUnfoldBayes(response, hMeas, 10, False, True) elif "SVD" in self.optunf: # SVD unfolding with free regularisation: unfold = RooUnfoldSvd(response, hMeas) elif "TUnfold" in self.optunf: # TUnfold with fixed regularisation tau=0.002 # unfold= RooUnfoldTUnfold( response, hMeas, 0.002 ) unfold = RooUnfoldTUnfold(response, hMeas) elif "Invert" in self.optunf: unfold = RooUnfoldInvert(response, hMeas) elif "Reverse" in self.optunf: # Use equivalent Bayes with 1 iteration: unfold = RooUnfoldBayes(response, hMeas, 1) elif "BasisSplines" in self.optunf: # BasisSplines with automatic tau determination unfold = RooUnfoldBasisSplines(response, hMeas, self.nrebin, 0.0, BasisSpline_m0, 2) return unfold
def setup_unfolding ( self, data ): self.data = data if not self.unfoldObject: if not self.unfoldResponse: self.unfoldResponse = self._makeUnfoldResponse() if self.method == 'RooUnfoldBayes': self.unfoldObject = RooUnfoldBayes ( self.unfoldResponse, self.data, self.Bayes_n_repeat ) elif self.method == 'RooUnfoldBinByBin': self.unfoldObject = RooUnfoldBinByBin ( self.unfoldResponse, self.data ) elif self.method == 'RooUnfoldInvert': self.unfoldObject = RooUnfoldInvert ( self.unfoldResponse, self.data ) elif self.method == 'RooUnfoldSvd': if self.k_value > 0: self.unfoldObject = RooUnfoldSvd( self.unfoldResponse, self.data, self.k_value, self.n_toy ) else: if self.tau >= 0: self.unfoldObject = RooUnfoldSvd( self.unfoldResponse, self.data, self.tau, self.n_toy ) elif self.method == 'RooUnfoldTUnfold': self.unfoldObject = RooUnfoldTUnfold ( self.unfoldResponse, data ) if self.tau >= 0: self.unfoldObject.FixTau( self.tau ) elif self.method == 'TSVDUnfold': new_data = Hist( list( self.data.xedges() ), type = 'D' ) new_data.Add( self.data ) new_measured = Hist( list( self.measured.xedges() ), type = 'D' ) new_measured.Add( self.measured ) new_truth = Hist( list( self.truth.xedges() ), type = 'D' ) new_truth.Add( self.truth ) if self.fakes: new_fakes = Hist( list ( self.fakes.xedges() ), type = 'D' ) new_fakes.Add ( self.fakes ) new_measured = new_measured - new_fakes new_response = Hist2D( list( self.response.xedges() ), list( self.response.yedges() ), type = 'D' ) new_response.Add( self.response ) # replace global objects with new ones self.data = new_data self.measured = new_measured self.truth = new_truth self.response = new_response self.unfoldObject = TSVDUnfold( self.data, self.measured, self.truth, self.response )
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()
mc_hist = supplementaryMC.Get('PFJet_pt_m_AK8') mc_hist.Scale(1. / mc_hist.Integral()) responses_softdrop = [] unfolded_softdrop = [] mc_hist_softdrop = supplementaryMC.Get('PFJet_pt_m_AK8SD') mc_hist_softdrop.Scale(1. / mc_hist_softdrop.Integral()) for i in range(0, 10): responses_softdrop.append(jackknife_mc.Get('2d_response_softdrop' + str(i))) responses.append(jackknife_mc.Get('2d_response' + str(i))) unfolded_softdrop.append( RooUnfoldBayes(responses_softdrop[i], mc_hist_softdrop, 4).Hreco()) unfolded.append(RooUnfoldBayes(responses[i], mc_hist, 4).Hreco()) unfolded_data.append(RooUnfoldBayes(responses[i], data_hist, 4).Hreco()) unfolded_data_softdrop.append( RooUnfoldBayes(responses_softdrop[i], data_hist_softdrop, 4).Hreco()) unfolded_data[i].Scale(1. / unfolded_data[i].Integral()) unfolded_data_softdrop[i].Scale(1. / unfolded_data_softdrop[i].Integral()) unfolded_softdrop[i].Scale(1. / unfolded_softdrop[i].Integral()) unfolded[i].Scale(1. / unfolded[i].Integral()) del responses[:] del responses_softdrop[:]
def main(optunf="Bayes"): optunfs = ["Bayes", "SVD", "TUnfold", "Invert", "Reverse"] if not optunf in optunfs: txt = "Unfolding option " + optunf + " not recognised" raise ValueError(txt) global hReco, hMeas, hTrue, hTrueEE, hTrueMM, hMeasMM, hMeasEE #c2 = TCanvas("c2") print "==================================== TRAIN ====================================" # Create response matrix object inputdirZZ = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/UNFOLDacc/HADD/UnfoldInput/" #ZZ # inputdirWZ = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/UNFOLDWZ/HADD/UnfoldInput/" #WZ # inputdirNoPrep = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/ZZoutput/" #data, NRB inputdirDY = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/DYbkgd/" #gamma #inputGEN = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/Unfolding/PT.root" inputdir = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/EWKSET/HADD/UnfoldInput/" #ZZ inputdirWZ = inputdir #WZ inputdirNoPrep = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/EWKSET/" #data, NRB #inputdirNoPrep2 = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/" #data, NRB inputdir2 = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/NOSET/HADD/UnfoldInput/" # inputdirWZ = inputdir fw = '/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/Unfolding/MCFM_withAcceptance/ZZlept_tota_MSTW200_90__90__test.root' fwo = '/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/Unfolding/MCFM_withoutAcceptance/ZZlept_tota_MSTW200_90__90__test.root' #inputdirOTH = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/NOSET/HADD/UnfoldInput/" #inputdirWZOTH = inputdirOTH #inputdirEWK = "/afs/cern.ch/work/c/chasco/CMSSW_5_3_11/src/CMGTools/HtoZZ2l2nu/test/results/EWKSET/HADD/UnfoldInput/" #ZZ PAIR = ['zptG', 'zpt'] outputnameGR = PAIR[0] + '_vs_' + PAIR[1] + '.png' outputnameG = PAIR[0] + '.png' outputnameR = PAIR[1] + '.png' cutData = "(zpt>45)*(preco>0)" #"preco" cutGMM = "((L1GId*L2GId)==-13*13)" cutRMM = "((l1id*l2id)==-13*13)" cutGEE = "((L1GId*L2GId)==-11*11)" cutREE = "((l1id*l2id)==-11*11)" cutQQ = "(abs(l1id*l2id) == 11*13)" hsWW = makehistos("DataFused.root", inputdirNoPrep) hsDY = makehistosDY('gamma_out_8_MoreBins_ll.root', inputdirDY) print "MEASURED DATA " + "=" * 30 hMeasMM = Make1DPlot(inputdirNoPrep + "DataFused.root", "finalTree", PAIR[1], "Data.png", cutRMM + "*" + cutData) hMeasEE = Make1DPlot(inputdirNoPrep + "DataFused.root", "finalTree", PAIR[1], "Data.png", cutREE + "*" + cutData) #hMeasQQ = Make1DPlot(inputdirNoPrep+"DataFused.root","finalTree",PAIR[1],"Data.png",cutQQ + "*" + cutData) print "MEASURED DATA " + "=" * 30 print hMeasMM.Integral(), hMeasEE.Integral(), "Measured" print "NRB and DY" print hsWW[0].Integral(), hsWW[1].Integral(), "WW" print hsDY[0].Integral(), hsDY[1].Integral(), "DY" #MCFM rescaling and plotting # hMCFM = GetMCFMHisto(fwo, True) hMCFM.Scale(143.2 / hMCFM.Integral()) hMCFM = REBIN(hMCFM) print hMCFM.Integral() hMCFM.Print("range") #sys.exit("donesies") MWO = GetMCFMHisto(fwo, True) MW = GetMCFMHisto(fw, False) MWO.Divide(MW) MWO.Print("range") MWO = REBIN(MWO) MWO.Print("range") # MWO = REBIN(GetMCFMHisto(fwo,True)) # MW = REBIN(GetMCFMHisto(fw, False)) # MWO.Divide(MW) # MWO.Print("range") #sys.exit("donesies") systematic = [ '_jer', '_jes', '_umet', '_les', '_pu', '_btag', '_qcd', '_pdf' ] #'_jer','_jes','_umet','_les','_pu','_btag','_sherpa','_qcd','_pdf'] # systematic = ['_jer','_jes','_qcd','_pdf'] UD = ['up', 'down'] SYST_UD = [''] for syst in systematic: for ud in UD: SYST_UD.append(syst + ud) MCGenHistos = [] MCRecoHistos = [] hTrues = [] hDiffs = [] hCloses = [] hunfolds = [] ######## MCGenHistosMM = [] MCRecoHistosMM = [] hTruesMM = [] hDiffsMM = [] hClosesMM = [] hunfoldsMM = [] ######## MCGenHistosEE = [] MCRecoHistosEE = [] hTruesEE = [] hDiffsEE = [] hClosesEE = [] hunfoldsEE = [] ###### MCGenHistosMM2 = [] MCGenHistosEE2 = [] MCGenHistos2 = [] for syst_ud in SYST_UD: print "=O" * 40 print syst_ud print "O=" * 40 SYS = "(sys" + syst_ud + ">0)" cutR = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(preco>0)*" + SYS # cutG = "Eweight*BR*(B2/B3)*(pgen>0)*"+SYS #Accstring # [5718.8469039797783, 1737.4891278286839, 1737.4891278286839] fb #SSS = 2.1299863918*(3.0) #SSS = 2.1299863918*(3.0/2.0) #SSS = (3.0/2.0) #SSS = (3.0) #cutG = str(SSS)+"*Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*"+SYS #Accstring cutG = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*" + SYS #Accstring # cutG = str(SSS)+"*Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(1/Acc2)*(pgen>0)*"+SYS #Accstring #cutG = str(SSS)+"*Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*"+SYS #Accstring cutGR = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(pgen>0)*(preco>0)*" + SYS #+Accstring cutWZ = "Eweight*XS*BR*LUM*(1/NGE)*(B2/B3)*(preco>0)*" + SYS cutGEN = "XS*LUM*(1/NGE)*(B2/B3)*(g_pt>45.0)" cutee = "(ee>0.0)" cutmm = "(mumu>0.0)" # MCRecoHistoMM = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[1],outputnameR,cutR+"*"+cutRMM)) # MCRecoHistoEE = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[1],outputnameR,cutR+"*"+cutREE)) # MCGenHistoMM = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[0],outputnameG,cutG+"*"+cutGMM)) # MCGenHistoEE = REBIN(Make1DPlot(inputdir+"ZZ.root","tmvatree",PAIR[0],outputnameG,cutG+"*"+cutGEE)) MCRecoHistoMM = Make1DPlot(inputdir + "ZZ.root", "tmvatree", PAIR[1], outputnameR, cutR + "*" + cutRMM) MCRecoHistoEE = Make1DPlot(inputdir + "ZZ.root", "tmvatree", PAIR[1], outputnameR, cutR + "*" + cutREE) MCGenHistoMM = Make1DPlotGEN(inputdir + "ZZ.root", "tmvatree", PAIR[0], outputnameG, cutG + "*" + cutGMM) MCGenHistoEE = Make1DPlotGEN(inputdir + "ZZ.root", "tmvatree", PAIR[0], outputnameG, cutG + "*" + cutGEE) # MCGenHistoMM = Make1DPlotGEN(inputGEN,"GPT","g_pt",outputnameG,cutGEN+"*"+cutmm) # MCGenHistoEE = Make1DPlotGEN(inputGEN,"GPT","g_pt",outputnameG,cutGEN+"*"+cutee) # MCGenHistoMM.Print("range") # sys.exit("done") GenVsReco2DHistoMM = Make2DPlot(inputdir + "ZZ.root", "tmvatree", PAIR, 'MM' + outputnameGR, cutGR + "*" + cutGMM + "*" + cutRMM, syst_ud) GenVsReco2DHistoEE = Make2DPlot(inputdir + "ZZ.root", "tmvatree", PAIR, 'EE' + outputnameGR, cutGR + "*" + cutGEE + "*" + cutREE, syst_ud) # sys.exit("done") # MCGenHistoMM2 = Make1DPlotGEN(inputdir2 + "ZZ.root", "tmvatree", PAIR[0], outputnameG, cutG + "*" + cutGMM) MCGenHistoEE2 = Make1DPlotGEN(inputdir2 + "ZZ.root", "tmvatree", PAIR[0], outputnameG, cutG + "*" + cutGEE) #MCGenHistoMM2 = Make1DPlotMCFM(1) #MCGenHistoEE2 = Make1DPlotMCFM(1) hWZMM = Make1DPlot(inputdirWZ + "WZ.root", "tmvatree", PAIR[1], "WZ.png", cutWZ + "*" + cutRMM) #MC hWZEE = Make1DPlot(inputdirWZ + "WZ.root", "tmvatree", PAIR[1], "WZ.png", cutWZ + "*" + cutREE) #MC print hWZMM.Integral(), hWZEE.Integral(), "WZ MM EE" print MCRecoHistoMM.Integral(), MCRecoHistoEE.Integral(), "ZZ MM EE" BKGDSMM = [REBIN(hsWW[0]), REBIN(hsDY[0]), hWZMM] BKGDSEE = [REBIN(hsWW[1]), REBIN(hsDY[1]), hWZEE] for bk in (BKGDSMM + BKGDSEE): print bk.Integral(), "subtract" print hMeasMM.Integral() print hMeasEE.Integral() hDiffMM = SubtractMany1DPlots(hMeasMM, BKGDSMM) hDiffEE = SubtractMany1DPlots(hMeasEE, BKGDSEE) # hDiffMM = hMeasMM # hDiffEE = hMeasEE print hDiffMM.Integral(), quadadd(BinError(hDiffMM)) print hDiffEE.Integral(), quadadd(BinError(hDiffEE)) print "Difference between Data and Background^" print hMeasMM.Integral(), quadadd(BinError(hMeasMM)) print hMeasEE.Integral(), quadadd(BinError(hMeasEE)) print "DATA yield^" print REBIN(hsWW[0]).Integral(), quadadd(BinError(REBIN(hsWW[0]))) print REBIN(hsWW[1]).Integral(), quadadd(BinError(REBIN(hsWW[1]))) print "NRB yield^" print REBIN(hsDY[0]).Integral(), quadadd(BinError(REBIN(hsDY[0]))) print REBIN(hsDY[1]).Integral(), quadadd(BinError(REBIN(hsDY[1]))) print "DY yield^" print hWZMM.Integral(), quadadd(BinError(hWZMM)) print hWZEE.Integral(), quadadd(BinError(hWZEE)) print "WZ yield^" print MCRecoHistoMM.Integral(), quadadd(BinError(MCRecoHistoMM)) print MCRecoHistoEE.Integral(), quadadd(BinError(MCRecoHistoEE)) print "ZZ yield^" #sys.exit("donesies") responseMM = RooUnfoldResponse(MCRecoHistoMM, MCGenHistoMM, GenVsReco2DHistoMM) responseEE = RooUnfoldResponse(MCRecoHistoEE, MCGenHistoEE, GenVsReco2DHistoEE) # MCRecoHistoMM.Print("range") # MCGenHistoMM.Print("range") # GenVsReco2DHistoMM.Print("range") #sys.exit("DONE") #sys.exit("done") # print "==================================== TEST =====================================" # #hTrue= TH1D( "true", "Test Truth", 20, -40.0, 40.0 ) # #hMeas= TH1D( "meas", "Test Measured", 40, -10.0, 10.0 ) # #hTrue= TH1D( "true", "Test Truth", 10, 40.0, 760.0 ) # # hMeas= TH1D( "meas", "Test Measured", 10, 0.0, 800.0 ) print "==================================== UNFOLD ===================================" print "Unfolding method:", optunf if "Bayes" in optunf: # Bayes unfoldung with 4 iterations #unfoldMM= RooUnfoldBayes( responseMM, hDiffMM, 4) closureMM = RooUnfoldBayes(responseMM, MCRecoHistoMM, 4) #unfoldEE= RooUnfoldBayes( responseEE, hDiffEE, 4) closureEE = RooUnfoldBayes(responseEE, MCRecoHistoEE, 4) unfoldsysMM = RooUnfoldBayes(responseMM, hDiffMM, 4) unfoldsysEE = RooUnfoldBayes(responseEE, hDiffEE, 4) # if syst_ud == "": # unfoldsysMM.SetNToys(100) # unfoldsysMM.IncludeSystematics() # unfoldsysEE.SetNToys(100) # unfoldsysEE.IncludeSystematics() print "Bayes " * 20 #closure= RooUnfoldBayes( response, MCRecoHisto , 4 ) #unfold= RooUnfoldBayes( response, hMeas, 10, False, True ) elif "SVD" in optunf: # SVD unfoding with free regularisation # unfold= RooUnfoldSvd( response, hMeas, 20 ) unfold = RooUnfoldSvd(response, hMeas) elif "TUnfold" in optunf: # TUnfold with fixed regularisation tau=0.002 # unfold= RooUnfoldTUnfold( response, hMeas ) unfold = RooUnfoldTUnfold(response, hMeas, 0.002) elif "Invert" in optunf: unfold = RooUnfoldInvert(response, hMeas) elif "Reverse" in optunf: unfold = RooUnfoldBayes(response, hMeas, 1) #hTrueMM= unfoldMM.Hreco() hCloseMM = closureMM.Hreco() #hTrueEE= unfoldEE.Hreco() hCloseEE = closureEE.Hreco() if syst_ud == "": #unfoldsysMM.SetNToys(500) # unfoldsysMM.IncludeSystematics(0) # 0 DATA ONLY unfoldsysMM.IncludeSystematics(1) # 1 EVERYTHING # unfoldsysMM.IncludeSystematics(2) # 2 MC RESPONSE #unfoldsysEE.SetNToys(500) unfoldsysEE.IncludeSystematics(1) # hunfoldMM = unfoldsysMM.Hreco(2) # 2 DIRECT ERROR PROAGATION, ONLY WITH IncludeSystematics(0) -- data only. Can't directly propagate MC response unc. hunfoldMM = unfoldsysMM.Hreco(2) hunfoldEE = unfoldsysEE.Hreco( 2 ) # 3 Toy MC error evaluation. Would apply to both data-MC uncertainty, and MC response uncertainty. print "STUFF" * 40 hunfoldMM.Print("range") print "UNFOLDED NUMBERS :--:" * 10 print hunfoldMM.Integral(), quadadd(BinError(hunfoldMM)) print hunfoldEE.Integral(), quadadd(BinError(hunfoldEE)) print "UNFOLDED NUMBERS :--:" * 10 ############################# #IMPORTANT!!! Includesys&Hreco: 0&2, 1&2 (some errors), N&2 (bayesian)*, 1&3, 2&3 ############################# else: hunfoldMM = unfoldsysMM.Hreco() hunfoldEE = unfoldsysEE.Hreco() print " ---- TEST @@@@@@@@@@@@@@@@@@@@@@@@@ " hunfoldEE.Print("range") hunfoldMM.Print("range") # print hTrueMM.Integral(), hTrueEE.Integral(), "True" print MCGenHistoMM.Integral(), MCGenHistoEE.Integral(), "Gen" print hunfoldMM.Integral(), hunfoldEE.Integral(), "unfold" #makeComparison(MCGenHistoMM,MCRecoHistoMM,hTrueMM,hDiffMM,hCloseMM,"MM"+syst_ud) #makeComparison(MCGenHistoEE,MCRecoHistoEE,hTrueEE,hDiffEE,hCloseEE,"EE"+syst_ud) #RESCALE TO MCFM with kinematic cut ratios hunfoldEE.Multiply(MWO) hunfoldMM.Multiply(MWO) MCGenHistoEE.Multiply(MWO) MCGenHistoMM.Multiply(MWO) MCGenHistoEE2.Multiply(MWO) MCGenHistoMM2.Multiply(MWO) SSS = 3.0 #3.0 for single channel, 3.0/2.0 for both hunfoldMM.Scale(SSS) hunfoldEE.Scale(SSS) MCGenHistoMM.Scale(SSS) MCGenHistoEE.Scale(SSS) MCGenHistoMM2.Scale(SSS) MCGenHistoEE2.Scale(SSS) print MCGenHistoMM.Integral(), MCGenHistoEE.Integral(), "Gen" print hunfoldMM.Integral(), hunfoldEE.Integral(), "unfold" #sys.exit("donesies") #For mumu channel hDiffsEE.append([hDiffEE, syst_ud]) hunfoldsEE.append([hunfoldEE, syst_ud]) if syst_ud == "": hClosesEE.append([hCloseEE, syst_ud]) MCGenHistosEE.append([MCGenHistoEE, syst_ud]) MCRecoHistosEE.append([MCRecoHistoEE, syst_ud]) #MCGenHistosEE2.append([hMCFM,syst_ud]) #For mumu channel hDiffsMM.append([hDiffMM, syst_ud]) hunfoldsMM.append([hunfoldMM, syst_ud]) if syst_ud == "": hClosesMM.append([hCloseMM, syst_ud]) MCGenHistosMM.append([MCGenHistoMM, syst_ud]) MCRecoHistosMM.append([MCRecoHistoMM, syst_ud]) MCGenHistosMM2.append([hMCFM, syst_ud]) #For combined channel hDiffs.append([ADDER(hDiffMM, hDiffEE, 1), syst_ud]) hunfolds.append([ADDER(hunfoldMM, hunfoldEE, 1), syst_ud]) if syst_ud == "": hCloses.append([ADDER(hCloseMM, hCloseEE, 1), syst_ud]) MCGenHistos.append([ADDER(MCGenHistoMM, MCGenHistoEE, 1), syst_ud]) MCRecoHistos.append( [ADDER(MCRecoHistoMM, MCRecoHistoEE, 1), syst_ud]) MCGenHistos2.append( [ADDER(MCGenHistoMM2, MCGenHistoEE2, 1), syst_ud]) print "=|" * 20 print syst_ud * 20 print "unfold yield:" print(hunfoldMM.Integral() + hunfoldEE.Integral()) / 19.7 print(MCGenHistoMM.Integral() + MCGenHistoEE.Integral()) / 19.7, "Gen" hunfoldMM.Print("range") hunfoldEE.Print("range") print ">GEN<" * 40 MCGenHistoMM.Print("range") MCGenHistoEE.Print("range") print "=|" * 20 # print "=|"*20 # print "unfold yield:" # print (hunfoldMM.Integral()+hunfoldEE.Integral())/19.7 # print "=|"*20 # hDiffs.append([hDiffEE,syst_ud]) # hunfolds.append([hunfoldEE,syst_ud]) # if syst_ud == "": # hCloses.append([hCloseEE,syst_ud]) # MCGenHistos.append([MCGenHistoEE,syst_ud]) # MCRecoHistos.append([MCRecoHistoEE,syst_ud]) #QQQ = makeComparison(LUMscale(MCGenHistos),LUMscale(MCRecoHistos),LUMscale(hunfolds),LUMscale(hDiffs),LUMscale(MCGenHistos2),"comb") #QQQ= makeComparison(LUMscale(MCGenHistosEE),LUMscale(MCRecoHistosEE),LUMscale(hunfoldsEE),LUMscale(hDiffsEE),MCGenHistosMM2,"ee") #QQQ = makeComparison(LUMscale(MCGenHistosMM),LUMscale(MCRecoHistosMM),LUMscale(hunfoldsMM),LUMscale(hDiffsMM),MCGenHistosMM2,"mm") #QQQ = makeComparison(LUMscale(MCGenHistos),LUMscale(MCRecoHistos),LUMscale(hunfolds),LUMscale(hDiffs),MCGenHistosMM2,"comb") QQQ = makeComparison(LUMscale(MCGenHistosEE), LUMscale(MCRecoHistosEE), LUMscale(hunfoldsEE), LUMscale(hDiffsEE), MCGenHistosMM2, "ee") #QQQ = makeComparison(LUMscale(MCGenHistosMM),LUMscale(MCRecoHistosMM),LUMscale(hunfoldsMM),LUMscale(hDiffsMM),MCGenHistosMM2,"mm") Neemm = QQQ[0] #Nee = makeComparison(LUMscale(MCGenHistosEE),LUMscale(MCRecoHistosEE),LUMscale(hunfoldsEE),LUMscale(hDiffsEE),LUMscale(hClosesEE),"ee") #Nmm = makeComparison(LUMscale(MCGenHistosMM),LUMscale(MCRecoHistosMM),LUMscale(hunfoldsMM),LUMscale(hDiffsMM),LUMscale(hClosesMM),"mm") # #makeComparison(MCGenHistos,MCRecoHistos,hunfolds,hDiffs,hCloses) # print len(MCRecoHistos) # print len(hunfolds) # print "Bin Content Check" # print "#"*30 # print "#"*10, "DIMUON CHANNEL", "#"*10 # print "MCReco", MCRecoHistosMM[0][0].Integral(), BinContent(MCRecoHistosMM[0][0]) # print "MCGen", MCGenHistosMM[0][0].Integral(), BinContent(MCGenHistosMM[0][0]) # print "True", hunfoldsMM[0][0].Integral(), BinContent(hunfoldsMM[0][0]) # print "Diff", hDiffsMM[0][0].Integral(), BinContent(hDiffsMM[0][0]) # print "#"*30 # print "#"*10, "DIELECTRON CHANNEL", "#"*10 # print "MCReco", MCRecoHistosEE[0][0].Integral(), BinContent(MCRecoHistosEE[0][0]) # print "MCGen", MCGenHistosEE[0][0].Integral(), BinContent(MCGenHistosEE[0][0]) # print "True", hunfoldsEE[0][0].Integral(), BinContent(hunfoldsEE[0][0]) # print "Diff", hDiffsEE[0][0].Integral(), BinContent(hDiffsEE[0][0]) # print "#"*30 # print "#"*10, "COMBINED CHANNEL", "#"*10 # print "MCReco", MCRecoHistos[0][0].Integral(), BinContent(MCRecoHistos[0][0]) # print "MCGen", MCGenHistos[0][0].Integral(), BinContent(MCGenHistos[0][0]) # print "True", hunfolds[0][0].Integral(), BinContent(hunfolds[0][0]) # print "Diff", hDiffs[0][0].Integral(), BinContent(hDiffs[0][0]) # print "#"*30 # print "#"*30 # print "#"*30 # print hunfoldsMM[0][0].Integral()*(3.0)*2.13, "ZZ->2l2nu cross section, muon" # print hunfoldsEE[0][0].Integral()*(3.0)*2.13, "ZZ->2l2nu cross section, electron" # print hunfolds[0][0].Integral()*(3.0/2.0)*2.13, "ZZ->2l2nu cross section, combined" # print "#"*30 # print Nmm # print Nee print Neemm print QQQ[1] #print Nmm #print Nee # print "@"*30 # print "@"*30 # print "@"*30 # S=2.1299863918 # dS=0.0451194907919 # print numpy.multiply(Nmm,S*3) # print numpy.multiply(Nee,S*3) # print numpy.multiply(Neemm,S*3.0/2.0) # print "#"*30 # print "#"*30 # print "#"*30 # print numpy.multiply(RescaleToPreZpt45(Nmm,S,dS),3.0), "muons" # print numpy.multiply(RescaleToPreZpt45(Nee,S,dS),3.0), "electrons" # print numpy.multiply(RescaleToPreZpt45(Neemm,S,dS),3.0/2.0), "combined" # Just unfolding # [ 317.58667967 124.62000891 124.62000891] muons # [ 204.44743361 93.817413 93.817413 ] electrons # [ 261.01705568 78.68129176 78.68129176] combined #all else # Bin Content Check # ############################## # ########## DIMUON CHANNEL ########## # MCReco 5.48821856594 [1.6566265821456909, 1.5307177305221558, 2.0848486423492432, 0.21176119148731232, 0.0042644194327294827] # MCGen 50.3628177345 [28.911533355712891, 8.1944065093994141, 11.555961608886719, 1.5890809297561646, 0.11183533072471619] # True 49.7008933779 [27.718360900878906, 7.7494301795959473, 13.04161262512207, 1.2007522583007812, -0.0092625860124826431] # Diff 5.54152165353 [1.6073417663574219, 1.4832497835159302, 2.371938943862915, 0.16530285775661469, -0.086311697959899902] # ############################## # ########## DIELECTRON CHANNEL ########## # MCReco 3.674643751 [1.0442177057266235, 0.9972614049911499, 1.4791457653045654, 0.15098364651203156, 0.0030352284666150808] # MCGen 33.3869778588 [18.415399551391602, 5.4404187202453613, 8.2116708755493164, 1.2221240997314453, 0.097364611923694611] # True 31.9951079488 [22.219881057739258, 3.5517585277557373, 5.7815923690795898, 0.44187599420547485, 0.0] # Diff 3.02480854467 [1.2913563251495361, 0.70207256078720093, 1.0841209888458252, 0.059399198740720749, -0.11214052885770798] # ############################## # ########## COMBINED CHANNEL ########## # MCReco 9.16539874254 [2.7004456520080566, 2.5281918048858643, 3.5664489269256592, 0.36300751566886902, 0.0073048430494964123] # MCGen 83.7497940361 [47.326930999755859, 13.634824752807617, 19.767633438110352, 2.8112049102783203, 0.2091999351978302] # True 81.6960010286 [49.938240051269531, 11.301189422607422, 18.823205947875977, 1.6426281929016113, -0.0092625860124826431] # Diff 8.56633037329 [2.8986983299255371, 2.1853222846984863, 3.4560599327087402, 0.22470206022262573, -0.19845223426818848] # ############################## # ############################## # ############################## # 317.588708685 ZZ->2l2nu cross section, muon # 204.448739793 ZZ->2l2nu cross section, electron # 261.018723287 ZZ->2l2nu cross section, combined # ############################## # [49.700893377885222, 24.356035601246898, 24.954409212519852] # [31.99510794878006, 17.444453802993618, 19.251802135458732] # [81.696001028642058, 34.234254955780528, 37.056285190785381] # ############################## # ############################## # ############################## # [ 317.58667967 155.77940538 159.59950658] muons 93.47233023637472982875, 99.70885557387058841952 quaddifference to just unfolding # [ 204.44743361 111.55344624 123.09443832] electrons 60.35283246052335146028, 79.69023631100434516015 # [ 261.01705568 109.51740717 118.52311228] combined 76.17950380658385143785, 88.63962134122213690629 # os.system(str(Npm)+' > output.txt') # SystematicErrors(MCRecoHistos,MCGenHistos,hTrues,hDiffs,hCloses) # #testtest return
xt = gRandom.BreitWigner(0.3, 2.5) x = smear(xt) if x != None: response.Fill(x, xt) else: response.Miss(xt) print "==================================== TEST =====================================" hTrue = TH1D("true", "Test Truth", 40, -10.0, 10.0) hMeas = TH1D("meas", "Test Measured", 40, -10.0, 10.0) # Test with a Gaussian, mean 0 and width 2. for i in xrange(10000): xt = gRandom.Gaus(0.0, 2.0) x = smear(xt) hTrue.Fill(xt) if x != None: hMeas.Fill(x) print "==================================== UNFOLD ===================================" unfold = RooUnfoldBayes(response, hMeas, 4) # OR # unfold= RooUnfoldSvd (response, hMeas, 20); # OR # unfold= RooUnfoldTUnfold (response, hMeas); # OR # unfold= RooUnfoldIds (response, hMeas, 3); # OR hReco = unfold.Hreco() unfold.PrintTable(cout, hTrue) hReco.Draw() hMeas.Draw("SAME") hTrue.SetLineColor(8) hTrue.Draw("SAME")
else: chiHists1d.append( Proj2D_Y(Reco2d, minMass, maxMass, Reco2d.GetName(), True)) if compToGen: chiTests1d.append( Proj2D_Y(Gen2d, minMass, maxMass, Gen2d.GetName(), True)) else: chiTests1d.append( Proj2D_Y(Reco2d, minMass, maxMass, Reco2d.GetName(), True)) ## now do unfolding print "CCLA: Unfolding ", Reco2d, "using matix ", response if DAgostini == True: unfold2d = RooUnfoldBayes(response, Reco2d, NITER) else: unfold2d = RooUnfoldInvert(response, Reco2d) unfold2d.Print() new_hists.append(unfold2d) unfold2d.SetNToys(30000) hUnf2d = unfold2d.Hreco(2) hErr2d = unfold2d.Ereco(2) #chi2 = RooUnfold.Chi2 (unfold2d,2) chi2comp = Proj2D_Y(Gen2d, minMass, maxMass, Gen2d.GetName(), True)
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")
##################### pvalues = array('f', []) n_iterations = array('f', []) y_error = array('f', []) x_error = array('f', []) c_refoldeds = [] refolded_hists = [] c_sanitys = [] sanity_hists = [] for i in range(1, 20, 1): print '--------- i = ', i unfoldreco = RooUnfoldBayes(response, reco, i) reco_unfolded = unfoldreco.Vreco() reco_asvector = unfoldreco.Vmeasured() m = response.Mresponse() print 'response : ', m.GetNrows(), ' x ', m.GetNcols() refolded = ROOT.TVectorD(reco_unfolded) refolded *= m #TMatrixD(m,TMatrixD.kMult,reco_unfolded) print 'refolded : ', refolded.GetNrows() refolded_hist = ROOT.TH1D("refolded_hist_" + str(i), "refolded_hist_" + str(i), reco.GetNbinsX() * reco.GetNbinsY(), 0, reco.GetNbinsX() * reco.GetNbinsY()) sanity_hist = ROOT.TH1D("sanity_hist_" + str(i), "sanity_hist_" + str(i), reco.GetNbinsX() * reco.GetNbinsY(), 0, reco.GetNbinsX() * reco.GetNbinsY()) for ibin in xrange(0, refolded.GetNrows()):
def MyRooUnfold(matrix_name=args.h_matrix, h_reco_getG0_name=args.h_data, h_ptcl_getG0_name = args.h_particle,h_reco_get_bkg_name = args.h_background,outputname=args.h_data+"_unfolded",nrebin = args.nrebin): rfile_data = TFile(args.rfile_data, 'read') rfile_particle = TFile(args.rfile_particle, 'read') rfile_matrix = TFile(args.rfile_matrix, 'read') rfile_background = TFile(args.rfile_background, 'read') myfbu = fbu.PyFBU() myfbu.verbose = True #GET DATA h_reco_get = rfile_data.Get(h_reco_getG0_name) h_reco_get.Rebin(nrebin) #GET PARTICLE h_ptcl_get = rfile_particle.Get(h_ptcl_getG0_name) h_ptcl_get.Rebin(nrebin) #GET MATRIX h_response_unf = rfile_matrix.Get(matrix_name) h_response_unf.ClearUnderflowAndOverflow() h_response_unf.GetXaxis().SetRange(1, h_response_unf.GetXaxis().GetNbins() ) h_response_unf.GetYaxis().SetRange(1, h_response_unf.GetYaxis().GetNbins() ) h_response_unf.Rebin2D(nrebin,nrebin) h_response_unf.SetName("Migration_Matrix_simulation") ########### ACCEPTANCY h_acc = h_response_unf.ProjectionX("reco_recoandparticleX") # Reco M h_acc.Divide(h_reco_get) ########### AKCEPTANCE saved in h_acc ############# ########### EFFICIENCY h_eff = h_response_unf.ProjectionY("reco_recoandparticleY") # Ptcl M h_eff.Divide(h_ptcl_get) h_reco_get_input = rfile_data.Get(h_reco_getG0_name) h_reco_get_bkg = rfile_background.Get(h_reco_get_bkg_name) h_reco_get_bkg.Rebin(nrebin) h_reco_get_input_clone=h_reco_get_input.Clone("") h_reco_get_input_clone.Add(h_reco_get_bkg,-1) h_reco_get_input_clone.Multiply(h_acc) h_reco_or = rfile_data.Get(h_reco_getG0_name) h_ptcl_or = rfile_particle.Get(h_ptcl_getG0_name) h_ptcl_or.SetMaximum(h_ptcl_or.GetMaximum()*1.5) ### ROOUNFOLD METHOD ### m_RooUnfold = RooUnfoldBayes() m_RooUnfold.SetRegParm( 4 ) m_RooUnfold.SetNToys( 10000 ) m_RooUnfold.SetVerbose( 0 ) m_RooUnfold.SetSmoothing( 0 ) response = RooUnfoldResponse(None, None, h_response_unf, "response", "methods") m_RooUnfold.SetResponse( response ) m_RooUnfold.SetMeasured( h_reco_get_input_clone ) ### SVD METHOD ### m_RooUnfold_svd = RooUnfoldSvd (response, h_reco_get_input_clone, int(round(h_reco_get_input_clone.GetNbinsX()/2.0,0))) #8 svd_par = int(round(h_reco_get_input_clone.GetNbinsX()/2.0,0)) m_RooUnfold_T = RooUnfoldTUnfold (response, h_reco_get_input_clone) # OR m_RooUnfold_Ids= RooUnfoldIds (response, h_reco_get_input_clone,int(round(h_reco_get_input_clone.GetNbinsX()/12.0,0))) ## TO DO, SET PARAMETERS TO THE BINNING Ids_par = int(round(h_reco_get_input_clone.GetNbinsX()/12.0,0)) ### FBU METHOD ### h_response_unf_fbu = TransposeMatrix(h_response_unf) h_response_unf_fbu_norm = NormalizeResponse(h_response_unf_fbu) h_response_unf_fbu_norm.SetName("Migration_Matrix_simulation_transpose") histograms.append(h_response_unf_fbu_norm) myfbu.response = MakeListResponse(h_response_unf_fbu_norm) myfbu.data = MakeListFromHisto(h_reco_get_input_clone) myfbu.lower = [] myfbu.upper = [] h_det_div_ptcl=h_reco_get_input_clone.Clone("") h_det_div_ptcl.Divide(h_ptcl_or) h_det_div_ptcl.Divide(h_eff) h_det_div_ptcl.SetName("det_div_ptcl") histograms.append(h_det_div_ptcl) for l in range(len(myfbu.data)): if ( args.SplitFromBinLow != 0) and ( l+1 <= args.SplitFromBinLow ): myfbu.lower.append(h_reco_get_input_clone.GetBinContent(l+1)*(2-args.ParameterSplitFromBinLow)*h_det_div_ptcl.GetBinContent(l+1)) myfbu.upper.append(h_reco_get_input_clone.GetBinContent(l+1)*args.ParameterSplitFromBinLow*h_det_div_ptcl.GetBinContent(l+1)) elif ( args.SplitFromBinHigh != 0 ) and ( l+1 >= args.SplitFromBinHigh ): myfbu.lower.append(h_reco_get_input_clone.GetBinContent(l+1)*(2-args.ParameterSplitFromBinHigh)*h_det_div_ptcl.GetBinContent(l+1)) myfbu.upper.append(h_reco_get_input_clone.GetBinContent(l+1)*args.ParameterSplitFromBinHigh*h_det_div_ptcl.GetBinContent(l+1)) else: myfbu.lower.append(h_reco_get_input_clone.GetBinContent(l+1)*(2-args.par)*h_det_div_ptcl.GetBinContent(l+1)) myfbu.upper.append(h_reco_get_input_clone.GetBinContent(l+1)*args.par*h_det_div_ptcl.GetBinContent(l+1)) #myfbu.regularization = Regularization('Tikhonov',parameters=[{'refcurv':0.1,'alpha':0.2}]) works for old FBU package and python2.7 and old pymc myfbu.run() trace = myfbu.trace traceName = 'Posterior_1_iteration' posteriors_diag = MakeTH1Ds(trace, traceName) h_reco_unfolded, h_reco_unfolded_Mean = MakeUnfoldedHisto(h_reco_or, posteriors_diag) PlotPosteriors(posteriors_diag,outputname+'_iteration_1') # EFFICIENCY AND ACCEPTANCY CORRECTIONS h_reco_unfolded.Divide(h_eff) h_reco_unfolded_Mean.Divide(h_eff) h_reco_unfolded_roof = m_RooUnfold.Hreco() h_reco_unfolded_roof.Divide(h_eff) h_reco_unfolded_svd = m_RooUnfold_svd.Hreco() h_reco_unfolded_svd.Divide(h_eff) h_reco_unfolded_T = m_RooUnfold_T.Hreco() h_reco_unfolded_T.Divide(h_eff) h_reco_unfolded_Ids = m_RooUnfold_Ids.Hreco() h_reco_unfolded_Ids.Divide(h_eff) PlotRatio(h_reco_unfolded_Mean, h_ptcl_or, h_reco_unfolded_roof, h_reco_unfolded_svd, h_reco_unfolded_T,h_reco_unfolded_Ids, svd_par, Ids_par, outputname+'_iteration_1') Repeat = True j = 2 while Repeat: print("Runnig iteration number: ",j) myfbu.lower = [] myfbu.upper = [] for l in range(len(myfbu.data)): posteriors_diag[l].Fit("gaus") fit = posteriors_diag[l].GetFunction("gaus") p1 = fit.GetParameter(1) p2 = fit.GetParameter(2) myfbu.lower.append(p1-4*p2) myfbu.upper.append(p1+4*p2) myfbu.run() trace = myfbu.trace traceName = 'Posterior_'+str(j)+'_iteration' posteriors_diag = MakeTH1Ds(trace, traceName) h_reco_unfolded, h_reco_unfolded_Mean = MakeUnfoldedHisto(h_reco_or, posteriors_diag) Repeat = PlotPosteriors(posteriors_diag,outputname+'_iteration_'+str(j)) # EFFICIENCY AND ACCEPTANCY CORRECTIONS h_reco_unfolded.Divide(h_eff) h_reco_unfolded_Mean.Divide(h_eff) h_reco_unfolded_roof = m_RooUnfold.Hreco() h_reco_unfolded_roof.Divide(h_eff) h_reco_unfolded_svd = m_RooUnfold_svd.Hreco() h_reco_unfolded_svd.Divide(h_eff) h_reco_unfolded_T = m_RooUnfold_T.Hreco() h_reco_unfolded_T.Divide(h_eff) h_reco_unfolded_Ids = m_RooUnfold_Ids.Hreco() h_reco_unfolded_Ids.Divide(h_eff) PlotRatio(h_reco_unfolded_Mean, h_ptcl_or, h_reco_unfolded_roof, h_reco_unfolded_svd, h_reco_unfolded_T,h_reco_unfolded_Ids, svd_par, Ids_par, outputname+'_iteration_'+str(j)) if j == args.maxiterations: break j = j+1 h_reco_unfolded.SetName("result_fbu_fit") histograms.append(h_reco_unfolded) h_reco_unfolded_Mean.SetName("result_fbu_Mean") histograms.append(h_reco_unfolded_Mean) h_reco_unfolded_roof.SetName("result_roof") histograms.append(h_reco_unfolded_roof) h_reco_unfolded_svd.SetName("result_svd") histograms.append(h_reco_unfolded_svd) h_reco_unfolded_T.SetName("result_T") histograms.append(h_reco_unfolded_T) h_reco_unfolded_Ids.SetName("result_Ids") histograms.append(h_reco_unfolded_Ids) h_eff.SetName("efficiency") histograms.append(h_eff) h_acc.SetName("acceptancy") histograms.append(h_acc) h_reco_or.SetName("reco") histograms.append(h_reco_or) h_ptcl_or.SetName("ptcl_simulation") histograms.append(h_ptcl_or) h_ratio = h_reco_unfolded.Clone("") h_ratio.Divide(h_ptcl_or) h_ratio.SetName("ratio_fbu_fit") histograms.append(h_ratio) h_ratio = h_reco_unfolded_Mean.Clone("") h_ratio.Divide(h_ptcl_or) h_ratio.SetName("ratio_fbu_Mean") histograms.append(h_ratio) h_ratio = h_reco_unfolded_roof.Clone("") h_ratio.Divide(h_ptcl_or) h_ratio.SetName("ratio_roof") histograms.append(h_ratio) h_ratio = h_reco_unfolded_svd.Clone("") h_ratio.Divide(h_ptcl_or) h_ratio.SetName("ratio_svd") histograms.append(h_ratio) m_RooUnfold_svd.PrintTable (cout, h_ptcl_or) m_RooUnfold.PrintTable (cout, h_ptcl_or) # CORRECTIONS TO GET CROSS SECTION #DivideBinWidth(h_reco_unfolded_Mean) #DivideBinWidth(h_reco_unfolded_roof) #DivideBinWidth(h_reco_unfolded_svd) #DivideBinWidth(h_reco_unfolded_T) #DivideBinWidth(h_reco_unfolded_Ids) #DivideBinWidth(h_ptcl_or) #Lumi = 36.1e3 #for j in range(1,h_reco_unfolded_Mean.GetXaxis().GetNbins()+1): # h_reco_unfolded_Mean.SetBinContent(j,h_reco_unfolded_Mean.GetBinContent(j)/(Lumi)) # h_reco_unfolded_Mean.SetBinError(j,h_reco_unfolded_Mean.GetBinError(j)/(Lumi)) # h_reco_unfolded_roof.SetBinContent(j,h_reco_unfolded_roof.GetBinContent(j)/(Lumi)) # h_reco_unfolded_roof.SetBinError(j,h_reco_unfolded_roof.GetBinError(j)/(Lumi)) # h_reco_unfolded_svd.SetBinContent(j,h_reco_unfolded_svd.GetBinContent(j)/(Lumi)) # h_reco_unfolded_svd.SetBinError(j,h_reco_unfolded_svd.GetBinError(j)/(Lumi)) # h_reco_unfolded_T.SetBinContent(j,h_reco_unfolded_T.GetBinContent(j)/(Lumi)) # h_reco_unfolded_T.SetBinError(j,h_reco_unfolded_T.GetBinError(j)/(Lumi)) # h_reco_unfolded_Ids.SetBinContent(j,h_reco_unfolded_Ids.GetBinContent(j)/(Lumi)) # h_reco_unfolded_Ids.SetBinError(j,h_reco_unfolded_Ids.GetBinError(j)/(Lumi)) # h_ptcl_or.SetBinContent(j,h_ptcl_or.GetBinContent(j)/(Lumi)) # h_ptcl_or.SetBinError(j,h_ptcl_or.GetBinError(j)/(Lumi)) #h_reco_unfolded_Mean_clone=h_reco_unfolded_Mean.Clone("FBU_cross_section") #h_reco_unfolded_roof_clone=h_reco_unfolded_roof.Clone("DAgostini_cross_section") #h_reco_unfolded_svd_clone=h_reco_unfolded_svd.Clone("SVD_cross_section") #h_reco_unfolded_T_clone=h_reco_unfolded_T.Clone("TUnfold_cross_section") #h_reco_unfolded_Ids_clone=h_reco_unfolded_Ids.Clone("Ids_cross_section") #h_ptcl_or_clone=h_ptcl_or.Clone("Truth_cross_section") # #print("CONTROL*******************************************************************: ",h_reco_unfolded_Mean_clone.GetXaxis().GetNbins(),h_reco_unfolded_roof_clone.GetXaxis().GetNbins(),h_reco_unfolded_svd_clone.GetXaxis().GetNbins(),h_reco_unfolded_T_clone.GetXaxis().GetNbins(),h_reco_unfolded_Ids_clone.GetXaxis().GetNbins(),h_ptcl_or_clone.GetXaxis().GetNbins()) #histograms.append(h_reco_unfolded_Mean_clone) #histograms.append(h_reco_unfolded_roof_clone) #histograms.append(h_reco_unfolded_svd_clone) #histograms.append(h_reco_unfolded_T_clone) #histograms.append(h_reco_unfolded_Ids_clone) #histograms.append(h_ptcl_or_clone) SaveHistograms(outputname)
def unfold_mc(self): logging.debug('Unfolding monte carlo sample') regularisation = int(self.params['regularisation'].m) unfold_bg = self.params['unfold_bg'].value unfold_eff = self.params['unfold_eff'].value unfold_unweighted = self.params['unfold_unweighted'].value # Split data into signal, bg and all (signal+bg) signal_data, bg_data, all_data = self.split_data() # Load generator level data for signal gen_data = self.load_gen_data() # Return true map is regularisation is set to 0 if regularisation == 0: logging.info('Returning baseline MapSet') true = roounfold._histogram(events=gen_data, binning=self.true_binning, weights=gen_data['pisa_weight'], errors=True, name=self.output_str) return MapSet([true]) # Get the inversed efficiency histogram if not unfold_eff: inv_eff = self.get_inv_eff(signal_data, gen_data) # Set the reco and true data based on cfg file settings reco_norm_data = None true_norm_data = None data = signal_data if unfold_bg: reco_norm_data = all_data if unfold_eff: true_norm_data = gen_data if reco_norm_data is None: reco_norm_data = signal_data if true_norm_data is None: true_norm_data = signal_data if unfold_unweighted: ones = np.ones(reco_norm_data['pisa_weight'].shape) reco_norm_data['pisa_weight'] = ones true_norm_data['pisa_weight'] = ones data['pisa_weight'] = ones # Create response object response = self.create_response(reco_norm_data, true_norm_data, data) # Make pseduodata all_hist = self._histogram(events=all_data, binning=self.reco_binning, weights=all_data['pisa_weight'], errors=False, name='all', tex=r'\rm{all}') seed = int(self.params['stat_fluctuations'].m) if seed != 0: if self.random_state is None or seed != self.seed: self.seed = seed self.random_state = get_random_state(seed) all_hist = all_hist.fluctuate('poisson', self.random_state) else: self.seed = None self.random_state = None all_hist.set_poisson_errors() # Background Subtraction if unfold_bg: reco = all_hist else: bg_hist = self.get_bg_hist(bg_data) reco = all_hist - bg_hist reco.name = 'reco_signal' reco.tex = r'\rm{reco_signal}' r_flat = roounfold._flatten_to_1d(reco) r_th1d = convert_to_th1d(r_flat, errors=True) # Find optimum value for regularisation parameter if self.params['optimize_reg'].value: chisq = None for r_idx in range(regularisation): unfold = RooUnfoldBayes(response, r_th1d, r_idx + 1) unfold.SetVerbose(0) idx_chisq = unfold.Chi2(self.t_th1d, 1) if chisq is None: pass elif idx_chisq > chisq: regularisation = r_idx break chisq = idx_chisq # Unfold unfold = RooUnfoldBayes(response, r_th1d, regularisation) unfold.SetVerbose(0) unfolded_flat = unfold.Hreco(1) unfold_map = unflatten_thist(in_th1d=unfolded_flat, binning=self.true_binning, name=self.output_str, errors=True) # Efficiency correction if not unfold_eff: unfold_map *= inv_eff del r_th1d del unfold logging.info('Unfolded reco sum {0}'.format( np.sum(unp.nominal_values(unfold_map.hist)))) return unfold_map
def unfoldJetPt(meas, response, xbins): unfold = RooUnfoldBayes(response, meas) unfold.SetVerbose(0) hReco = makeHist(unfold.Hreco(), bins=xbins) return hReco
def main(): gROOT.SetBatch() #range for |t| ptmin = 0. ptmax = 0.109 # 0.109 0.01 for interference range #default binning ptbin = 0.004 # 0.004 0.0005 for interference range #long bins at high |t| ptmid = 0.06 # 0.08, value > ptmax will switch it off 0.06 ptlon = 0.01 # 0.01 #short bins at low |t| ptlow = 0.01 ptshort = 0.0005 #mass interval mmin = 2.8 mmax = 3.2 #dy = 2. # rapidity interval, for integrated sigma dy = 1. ngg = 131 # number of gamma-gamma from mass fit lumi = 13871.907 # lumi in inv. ub #correction to luminosity for ana/triggered events ratio_ana = 3420950. / 3694000 #scale the lumi for |z| around nominal bunch crossing ratio_zdc_vtx = 0.502 Reta = 0.503 # pseudorapidity preselection #Reta = 1. trg_eff = 0.67 # bemc trigger efficiency ratio_tof = 1.433 # tof correction to efficiency bbceff = 0.97 # BBC veto inefficiency zdc_acc = 0.49 # ZDC acceptance to XnXn 0.7 #zdc_acc = 1. br = 0.05971 # dielectrons branching ratio #data basedir = "../../../star-upc-data/ana/muDst/muDst_run1/sel5" infile = "ana_muDst_run1_all_sel5z.root" #MC basedir_sl = "../../../star-upc-data/ana/starsim/slight14e/sel5" #infile_sl = "ana_slight14e1x2_s6_sel5z.root" infile_sl = "ana_slight14e1x3_s6_sel5z.root" # basedir_sart = "../../../star-upc-data/ana/starsim/sartre14a/sel5" infile_sart = "ana_sartre14a1_sel5z_s6_v2.root" # basedir_bgen = "../../../star-upc-data/ana/starsim/bgen14a/sel5" infile_bgen = "ana_bgen14a1_v0_sel5z_s6.root" #infile_bgen = "ana_bgen14a2_sel5z_s6.root" # basedir_gg = "../../../star-upc-data/ana/starsim/slight14e/sel5" infile_gg = "ana_slight14e2x1_sel5_nzvtx.root" #model predictions gSlight = load_starlight(dy) gSartre = load_sartre() gFlat = loat_flat_pt2() gMS = load_ms() gCCK = load_cck() #open the inputs inp = TFile.Open(basedir + "/" + infile) tree = inp.Get("jRecTree") # inp_gg = TFile.Open(basedir_gg + "/" + infile_gg) tree_gg = inp_gg.Get("jRecTree") # inp_sl = TFile.Open(basedir_sl + "/" + infile_sl) tree_sl_gen = inp_sl.Get("jGenTree") # inp_sart = TFile.Open(basedir_sart + "/" + infile_sart) tree_sart_gen = inp_sart.Get("jGenTree") # inp_bgen = TFile.Open(basedir_bgen + "/" + infile_bgen) tree_bgen_gen = inp_bgen.Get("jGenTree") #evaluate binning #print "bins:", ut.get_nbins(ptbin, ptmin, ptmax) bins = ut.get_bins_vec_2pt(ptbin, ptlon, ptmin, ptmax, ptmid) #bins = ut.get_bins_vec_3pt(ptshort, ptbin, ptlon, ptmin, ptmax, ptlow, ptmid) #print "bins2:", bins.size()-1 #load the data strsel = "jRecM>{0:.3f} && jRecM<{1:.3f}".format(mmin, mmax) hPt = ut.prepare_TH1D_vec("hPt", bins) tree.Draw("jRecPt*jRecPt >> hPt", strsel) #distribution for bin centers hPtCen = hPt.Clone("hPtCen") #gamma-gamma component hPtGG = ut.prepare_TH1D_vec("hPtGG", bins) tree_gg.Draw("jRecPt*jRecPt >> hPtGG", strsel) #normalize the gamma-gamma component ut.norm_to_num(hPtGG, ngg, rt.kGreen) #incoherent functional shape func_incoh_pt2 = TF1("func_incoh", "[0]*exp(-[1]*x)", 0., 10.) func_incoh_pt2.SetParameters(873.04, 3.28) #fill incoherent histogram from functional shape hPtIncoh = ut.prepare_TH1D_vec("hPtIncoh", bins) ut.fill_h1_tf(hPtIncoh, func_incoh_pt2, rt.kRed) #print "Entries before gamma-gamma and incoherent subtraction:", hPt.GetEntries() #subtract gamma-gamma and incoherent components hPt.Sumw2() hPt.Add(hPtGG, -1) #print "Gamma-gamma entries:", hPtGG.Integral() #print "Entries after gamma-gamma subtraction:", hPt.Integral() #print "Incoherent entries:", hPtIncoh.Integral() hPt.Add(hPtIncoh, -1) #print "Entries after all subtraction:", hPt.Integral() #scale the luminosity lumi_scaled = lumi * ratio_ana * ratio_zdc_vtx #print "lumi_scaled:", lumi_scaled #denominator for deconvoluted distribution, conversion ub to mb den = Reta * br * zdc_acc * trg_eff * bbceff * ratio_tof * lumi_scaled * 1000. * dy #deconvolution deconv_min = bins[0] deconv_max = bins[bins.size() - 1] deconv_nbin = bins.size() - 1 gROOT.LoadMacro("fill_response_matrix.C") #Starlight response #resp_sl = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max) resp_sl = RooUnfoldResponse(hPt, hPt) rt.fill_response_matrix(tree_sl_gen, resp_sl) # unfold_sl = RooUnfoldBayes(resp_sl, hPt, 15) #unfold_sl = RooUnfoldSvd(resp_sl, hPt, 15) hPtSl = unfold_sl.Hreco() #ut.set_H1D(hPtSl) #apply the denominator and bin width ut.norm_to_den_w(hPtSl, den) #Sartre response #resp_sart = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max) #resp_sart = RooUnfoldResponse(hPt, hPt) #rt.fill_response_matrix(tree_sart_gen, resp_sart) # #unfold_sart = RooUnfoldBayes(resp_sart, hPt, 10) #hPtSart = unfold_sart.Hreco() #ut.set_H1D(hPtSart) #hPtSart.SetMarkerStyle(21) #Flat pT^2 response #resp_bgen = RooUnfoldResponse(deconv_nbin, deconv_min, deconv_max, deconv_nbin, deconv_min, deconv_max) resp_bgen = RooUnfoldResponse(hPt, hPt) rt.fill_response_matrix(tree_bgen_gen, resp_bgen) # unfold_bgen = RooUnfoldBayes(resp_bgen, hPt, 14) hPtFlat = unfold_bgen.Hreco() #ut.set_H1D(hPtFlat) #apply the denominator and bin width ut.norm_to_den_w(hPtFlat, den) #hPtFlat.SetMarkerStyle(22) #hPtFlat.SetMarkerSize(1.3) #systematical errors err_zdc_acc = 0.1 err_bemc_eff = 0.03 #sys_err = rt.TMath.Sqrt(err_zdc_acc*err_zdc_acc + err_bemc_eff*err_bemc_eff) sys_err = err_zdc_acc * err_zdc_acc + err_bemc_eff * err_bemc_eff #print "Total sys err:", sys_err hSys = ut.prepare_TH1D_vec("hSys", bins) hSys.SetOption("E2") hSys.SetFillColor(rt.kOrange + 1) hSys.SetLineColor(rt.kOrange) for ibin in xrange(1, hPtFlat.GetNbinsX() + 1): hSys.SetBinContent(ibin, hPtFlat.GetBinContent(ibin)) sig_sl = hPtSl.GetBinContent(ibin) sig_fl = hPtFlat.GetBinContent(ibin) err_deconv = TMath.Abs(sig_fl - sig_sl) / sig_fl #print "err_deconv", err_deconv #sys_err += err_deconv*err_deconv sys_err_sq = sys_err + err_deconv * err_deconv sys_err_bin = TMath.Sqrt(sys_err_sq) stat_err = hPtFlat.GetBinError(ibin) / hPtFlat.GetBinContent(ibin) tot_err = TMath.Sqrt(stat_err * stat_err + sys_err_sq) #hSys.SetBinError(ibin, hPtFlat.GetBinContent(ibin)*err_deconv) hSys.SetBinError(ibin, hPtFlat.GetBinContent(ibin) * sys_err_bin) #hPtFlat.SetBinError(ibin, hPtFlat.GetBinContent(ibin)*tot_err) #draw the results gStyle.SetPadTickX(1) gStyle.SetFrameLineWidth(2) #frame for models plot only frame = ut.prepare_TH1D("frame", ptbin, ptmin, ptmax) can = ut.box_canvas() #ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.03, 0.03) ut.set_margin_lbtr(gPad, 0.1, 0.09, 0.055, 0.01) ytit = "d#it{#sigma}/d#it{t}d#it{y} (mb/(GeV/c)^{2})" xtit = "|#kern[0.3]{#it{t}}| ((GeV/c)^{2})" ut.put_yx_tit(frame, ytit, xtit, 1.4, 1.2) frame.SetMaximum(11) #frame.SetMinimum(1.e-6) #frame.SetMinimum(2e-4) frame.SetMinimum(1e-5) # 3e-5 frame.Draw() #hSys.Draw("e2same") #bin center points from data #gSig = apply_centers(hPtFlat, hPtCen) gSig = fixed_centers(hPtFlat) ut.set_graph(gSig) #hPtSl.Draw("e1same") #hPtSart.Draw("e1same") #hPtFlat.Draw("e1same") #put model predictions #gSartre.Draw("lsame") #gFlat.Draw("lsame") gMS.Draw("lsame") gCCK.Draw("lsame") gSlight.Draw("lsame") gSig.Draw("P") frame.Draw("same") gPad.SetLogy() cleg = ut.prepare_leg(0.1, 0.96, 0.14, 0.01, 0.035) cleg.AddEntry( None, "Au+Au #rightarrow J/#psi + Au+Au + XnXn, #sqrt{#it{s}_{#it{NN}}} = 200 GeV", "") cleg.Draw("same") leg = ut.prepare_leg(0.45, 0.82, 0.18, 0.1, 0.035) leg.AddEntry(None, "#bf{|#kern[0.3]{#it{y}}| < 1}", "") hx = ut.prepare_TH1D("hx", 1, 0, 1) leg.AddEntry(hx, "STAR") hx.Draw("same") leg.Draw("same") #legend for models mleg = ut.prepare_leg(0.68, 0.76, 0.3, 0.16, 0.035) #mleg = ut.prepare_leg(0.68, 0.8, 0.3, 0.12, 0.035) mleg.AddEntry(gSlight, "STARLIGHT", "l") mleg.AddEntry(gMS, "MS", "l") mleg.AddEntry(gCCK, "CCK-hs", "l") #mleg.AddEntry(gSartre, "Sartre", "l") #mleg.AddEntry(gFlat, "Flat #it{p}_{T}^{2}", "l") mleg.Draw("same") #legend for deconvolution method dleg = ut.prepare_leg(0.3, 0.75, 0.2, 0.18, 0.035) #dleg = ut.prepare_leg(0.3, 0.83, 0.2, 0.1, 0.035) dleg.AddEntry(None, "Unfolding with:", "") dleg.AddEntry(hPtSl, "Starlight", "p") #dleg.AddEntry(hPtSart, "Sartre", "p") dleg.AddEntry(hPtFlat, "Flat #it{p}_{T}^{2}", "p") #dleg.Draw("same") ut.invert_col(rt.gPad) can.SaveAs("01fig.pdf") #to prevent 'pure virtual method called' gPad.Close() #save the cross section to output file out = TFile("sigma.root", "recreate") gSig.Write("sigma") out.Close() #beep when finished gSystem.Exec("mplayer ../computerbeep_1.mp3 > /dev/null 2>&1")
# get pythia 8 reco and normalize pdf_reco = pdffile.Get('PFJet_pt_m_AK8') pdf_reco_softdrop = pdffile.Get('PFJet_pt_m_AK8SD') pdf_reco.Scale(1. / pdf_reco.Integral()) pdf_reco_softdrop.Scale(1. / pdf_reco_softdrop.Integral()) # get truth and normalize it pdf_gen = pdffile.Get('PFJet_pt_m_AK8Gen') pdf_gen_softdrop = pdffile.Get('PFJet_pt_m_AK8SDgen') pdf_gen.Scale(1. / pdf_gen.Integral()) pdf_gen_softdrop.Scale(1. / pdf_gen_softdrop.Integral()) ##################################################################################################### Unfold Pythia8 with PDF-UP unfold_pdfup = RooUnfoldBayes(pdfup_response, pdf_reco, 4) unfolded_pdfup = unfold_pdfup.Hreco() canvases_up = [] namesreco_up = [] legends_up = [] for x in range(0, nptbin): canvases_up.append(TCanvas("canvas_pdfup" + str(x))) namesreco_up.append(None) legends_up.append(TLegend(.7, .5, .9, .7)) for i, canvas in enumerate(canvases_up): canvas.cd() namesreco_up[i] = unfolded_pdfup.ProjectionX('pdf_up' + str(i), i + 1, i + 1)
# hDiff_tmp = TH1F("diff_bin"+str(ibin)+"_"+str(iiter),"; unfolded-truth; number of events",100,-0.2,0.2) #elif ibin < 7: # hDiff_tmp = TH1F("diff_bin"+str(ibin)+"_"+str(iiter),"; unfolded-truth; number of events",100,-0.4,0.4) #else : hDiff_tmp = TH1F("diff_bin"+str(ibin)+"_"+str(iiter),"; unfolded-truth; number of events",100,-1,1) hDiff_bin.append(hDiff_tmp) lowedge = 399. highedge = 1199. for iiter in xrange(1,tot_iter+1) : for itoy in xrange(0,ntoys) : #print 'iiter = ', iiter, ' itoy = ', itoy unfold = RooUnfoldBayes(response, hToy_i[itoy], iiter) #unfold = RooUnfoldSvd(response, hToy_i[itoy], iiter) hReco_tmp = unfold.Hreco() for ibin in range(0, nbins): if myTrue.GetBinLowEdge(ibin+1) > lowedge and myTrue.GetBinLowEdge(ibin+1) < highedge: hDiff_bin[(iiter-1)*nbins + ibin].Fill( (myTrue.GetBinContent(ibin+1) - hReco_tmp.GetBinContent(ibin+1)) / myTrue.GetBinContent(ibin+1)) color = [1,2,3,4,5,6,7,8,9,14] for ibin in xrange(0,nbins) : c = TCanvas() if hDiff_bin[1*nbins + ibin].GetSum() == 0: continue
reco.Scale(1. / reco.Integral()) truthSD.Scale(1. / truthSD.Integral()) recoSD.Scale(1. / recoSD.Integral()) pt_bin = { 0: '200-240', 1: '240-310', 2: '310-400', 3: '400-530', 4: '530-650', 5: '650-760', 6: '760-Inf' } unfold = RooUnfoldBayes(response, reco, 6) unfoldSD = RooUnfoldBayes(responseSD, recoSD, 6) #unfold= RooUnfoldSvd(response, reco, 5); reco_unfolded = unfold.Hreco() reco_unfoldedSD = unfoldSD.Hreco() ################### New Correlation matrix stuff cov = unfold.Ereco() covSD = unfoldSD.Ereco() nb = cov.GetNrows() import math cor = ROOT.TH2F("cor", "", nb, 0, nb, nb, 0, nb) corSD = ROOT.TH2F("corSD", "", nb, 0, nb, nb, 0, nb)
herwig_reco.Scale(1. / herwig_reco.Integral()) herwig_reco_softdrop.Scale(1. / herwig_reco_softdrop.Integral()) # get truth and normalize it pythia8_gen = pythia8file.Get('PFJet_pt_m_AK8Gen') pythia8_gen_softdrop = pythia8file.Get('PFJet_pt_m_AK8SDgen') pythia8_gen.Scale(1. / pythia8_gen.Integral()) pythia8_gen_softdrop.Scale(1. / pythia8_gen_softdrop.Integral()) herwig_gen = herwigfile.Get('PFJet_pt_m_AK8Gen') herwig_gen_softdrop = herwigfile.Get('PFJet_pt_m_AK8SDgen') herwig_gen.Scale(1. / herwig_gen.Integral()) herwig_gen_softdrop.Scale(1. / herwig_gen_softdrop.Integral()) ########################################################################################################### Unfold ungroomed pythia 8 with herwig unfold_ps = RooUnfoldBayes(herwig_response, pythia8_reco, 4) unfolded_ps = unfold_ps.Hreco() pyth8_uherwig = unfolded_ps.ProjectionX() pyth8_uherwig.Scale(1.0 / pyth8_uherwig.Integral()) pyth8_uherwig.SetName("pyth8_uherwig") canvases = [] namesreco = [] namesgen = [] legends = [] for x in range(0, nptbin): canvases.append(TCanvas("canvas" + str(x))) namesreco.append(None) namesgen.append(None) legends.append(TLegend(.7, .5, .9, .7)) for i, canvas in enumerate(canvases):
elif xt == xt and xm != xm: response.Miss(xt) for i in range(N / 2, N): tree.GetEntry(i) xm = tree._recoTop_0_Eta xt = tree.trueTop_genParticleSelector_0_Eta if xm == xm: hMeas_test.Fill(xm) if xt == xt: hTrue_test.Fill(xt) of = ROOT.TFile("out_unfold.root", "RECREATE" if unfold else "READ") if unfold: unfoldBayes = RooUnfoldBayes(response, hMeas_test, 4, False, "bayes") kReg = 10 #Nbins/2 unfoldSVD = RooUnfoldSvd(response, hMeas_test, kReg, 1000, "svd") unfoldTUnf = RooUnfoldTUnfold(response, hMeas_test, ROOT.TUnfold.kRegModeDerivative, "tunf") hRecoBayes = unfoldBayes.Hreco() hRecoSVD = unfoldSVD.Hreco() hRecoTUnf = unfoldTUnf.Hreco() of.Write() else: hRecoBayes = of.Get("bayes") hRecoSVD = of.Get("svd") hRecoTUnf = of.Get("tunf")
from ROOT import gRandom, TH1, cout, TH2, TLegend, TFile from ROOT import RooUnfoldResponse from ROOT import RooUnfold from ROOT import RooUnfoldBayes from ROOT import TCanvas from ROOT import RooUnfoldSvd myfile = TFile('qcd_2d_unfold_MC.root') response = myfile.Get('2d_response') truth = myfile.Get('PFJet_pt_m_AK8Gen') reco = myfile.Get('PFJet_pt_m_AK8') response.Draw('colz') outfile = TFile('unfolded2d.root', 'RECREATE') unfold = RooUnfoldBayes(response, reco, 6) #unfold= RooUnfoldSvd(response, reco, 5); reco_unfolded = unfold.Hreco() reco_unfolded.Draw() truth.SetLineColor(4) truth.Draw('SAME') c2 = TCanvas() c3 = TCanvas() c4 = TCanvas() c5 = TCanvas() c6 = TCanvas()
def main(optunf="Bayes"): optunfs = ["Bayes", "SVD", "TUnfold", "Invert", "Reverse"] if not optunf in optunfs: txt = "Unfolding option " + optunf + " not recognised" raise ValueError(txt) global hReco, hMeas, hTrue print "==================================== TRAIN ====================================" # Create response matrix object for 40 measured and 20 # unfolded bins: response = RooUnfoldResponse(40, -10.0, 10.0, 20, -10.0, 10.0) # Train with a Breit-Wigner, mean 0.3 and width 2.5. for i in xrange(100000): # xt= gRandom.BreitWigner( 0.3, 2.5 ) xt = gRandom.Gaus(0.0, 5.0) x = smear(xt) if x != None: response.Fill(x, xt) else: response.Miss(xt) print "==================================== TEST =====================================" hTrue = TH1D("true", "Test Truth", 20, -10.0, 10.0) hMeas = TH1D("meas", "Test Measured", 40, -10.0, 10.0) # Test with a Gaussian, mean 0 and width 2. for i in xrange(10000): # xt= gRandom.Gaus( 0.0, 2.0 ) xt = gRandom.BreitWigner(0.3, 2.5) x = smear(xt) hTrue.Fill(xt) if x != None: hMeas.Fill(x) print "==================================== UNFOLD ===================================" print "Unfolding method:", optunf if "Bayes" in optunf: # Bayes unfoldung with 4 iterations # unfold= RooUnfoldBayes( response, hMeas, 4 ) unfold = RooUnfoldBayes(response, hMeas, 10, False, True) elif "SVD" in optunf: # SVD unfoding with free regularisation # unfold= RooUnfoldSvd( response, hMeas, 20 ) unfold = RooUnfoldSvd(response, hMeas) elif "TUnfold" in optunf: # TUnfold with fixed regularisation tau=0.002 # unfold= RooUnfoldTUnfold( response, hMeas ) unfold = RooUnfoldTUnfold(response, hMeas, 0.002) elif "Invert" in optunf: unfold = RooUnfoldInvert(response, hMeas) elif "Reverse" in optunf: unfold = RooUnfoldBayes(response, hMeas, 1) hReco = unfold.Hreco() # unfold.PrintTable( cout, hTrue ) unfold.PrintTable(cout, hTrue, 2) hReco.Draw() hMeas.Draw("SAME") hTrue.SetLineColor(8) hTrue.Draw("SAME") return
matrix_unfolded1 = np.zeros((Nbins,Nbins)) for i_Eg1 in range(Nbins):#range(60,62): hMeas= TH1D ("meas", "Test Measured", Nbins, Emin, Emax); print("Now doing i_Eg1 =", i_Eg1, flush=True) for i in range(Nbins): Ei = E_resp_array[i] hMeas.Fill(Ei,matrix_folded[i_Eg1,i]) # hack to recalculate the Uncertainties now, after the histogram is filled hMeas.Sumw2(False) hMeas.Sumw2(True) # hTrue.Sumw2(False) # doesn't work yet? # hTrue.Sumw2(True) # doesn't work yet? # print("==================================== UNFOLD ===================================") unfold= RooUnfoldBayes (response, hMeas, Niterations); # OR # unfold= RooUnfoldSvd (response, hMeas, 20); # OR #unfold= RooUnfoldTUnfold (response, hMeas); # OR # unfold= RooUnfoldIds (response, hMeas, 3); # OR # unfold= RooUnfoldInvert (response, hMeas); # OR hReco= unfold.Hreco(); # unfold.PrintTable (cout, hTrue); matrix_unfolded1[i_Eg1,:] = np.array(hReco)[0:Nbins] matrix_unfolded1 = np.nan_to_num(matrix_unfolded1) np.save(fname_save_unf1, matrix_unfolded1)
h_data_toUse.SetBinContent(bin, newContent) h_data_toUse.SetBinError(bin, sqrt(newContent)) h_measured_toUse = asrootpy(h_measured) # Remove fakes before unfolding h_fakes = h_measured_toUse - h_response.ProjectionX() nonFakeRatio = 1 - h_fakes / h_measured_toUse h_measured_toUse *= nonFakeRatio / nonFakeRatio h_data_toUse *= nonFakeRatio / nonFakeRatio # Unfold with SVD response = RooUnfoldResponse(h_measured_toUse, h_truth, h_response) svdUnfolding = RooUnfoldSvd(response, h_data_toUse, 0.7) svdUnfolding.SetVerbose(0) h_unfolded_data_svd = svdUnfolding.Hreco(3) # Unfold with Bayes response_bayes = RooUnfoldResponse(h_measured_toUse, h_truth, h_response) bayesUnfolding = RooUnfoldBayes(response_bayes, h_data_toUse, 4) h_unfolded_data_bayes = bayesUnfolding.Hreco(3) # Store result of first bin h_svdFirstBin.Fill(h_unfolded_data_svd.GetBinContent(1)) h_bayesFirstBin.Fill(h_unfolded_data_bayes.GetBinContent(1)) # Draw histograms h_svdFirstBin.Draw() raw_input('SVD') h_bayesFirstBin.Draw() raw_input('Bayes')
histBE = checkBE.Hreco() histBE.SetLineColor(2) c4 = ROOT.TCanvas("c4", "c4", 800, 800) c4.SetLogx() c4.SetLogy() histBE.GetXaxis().SetTitle("m[GeV]") histBE.SetTitle("Unfolded reco and gen for BE") DY2 = ROOT.TLegend(0.1, 0.7, 0.3, 0.9) DY2.AddEntry(histBE, "reco") DY2.AddEntry(genBE, "gen") histBE.Draw("hist") genBE.Draw("samehist") DY2.Draw() c4.Print("test4.pdf") checkBB1 = RooUnfoldBayes(responseBB, recBB) histBB1 = checkBB1.Hreco() histBB1.SetLineColor(2) c5 = ROOT.TCanvas("c5", "c5", 800, 800) c5.SetLogx() c5.SetLogy() histBB1.GetXaxis().SetTitle("m[GeV]") histBB1.SetTitle("Unfolded reco and gen for BB") DY3 = ROOT.TLegend(0.1, 0.7, 0.3, 0.9) DY3.AddEntry(histBB1, "reco") DY3.AddEntry(genBB, "gen") histBB1.Draw("hist") genBB.Draw("samehist") DY3.Draw() c5.Print("test5.pdf")
def doUnfold(self, data, back=0, regdiff=0, iteration=0): self.data = data.Clone() if back != 0: self.data.Sumw2() back.Sumw2() self.data.Add(back, -1.) self.back = back.Clone() for i in range(self.data.GetNbinsX() + 2): if self.data.GetBinContent(i) < 0: self.data.SetBinContent(i, 0) if regdiff != 0: Unfold = RooUnfoldBayes(self.response, self.data, 1, False, self.name + "_reg", self.name + "_reg") h2 = self.data.Clone() for i in range(9999): h1 = Unfold.Hreco().Clone() vaild = 0 val = 0 for j in range(1, h2.GetNbinsX() + 1): if h2.GetBinContent(j) > 0: val = h1.GetBinContent(j) / h2.GetBinContent(j) val = abs(val - 1) if vaild < val: vaild = val print vaild if vaild < regdiff: print "%d times iteration" % i self.UnfoldedData = h1 return h1 else: h2 = h1.Clone() Unfold.Reset() Unfold = RooUnfoldBayes(self.response, self.data, i + 2, False, self.name + "_reg", self.name + "_reg") elif iteration != 0: s = "iter_%d" % iteration Unfold = RooUnfoldBayes(self.response, self.data, iteration, False, self.name + s, self.name + s) h1 = Unfold.Hreco().Clone() return h1 else: Unfold = RooUnfoldInvert(self.response, self.data, self.name + "_invert", self.name + "_invert") Unfold.SetMeasured(self.data) self.Unfold = Unfold h1 = Unfold.Hreco().Clone() self.UnfoldedData = h1 return h1
matrix_unfolded1 = np.zeros((Nbins, Nbins)) for i_Eg1 in range(Nbins): #range(60,62): hMeas = TH1D("meas", "Test Measured", Nbins, Emin, Emax) print("Now doing i_Eg1 =", i_Eg1, flush=True) for i in range(Nbins): Ei = E_resp_array[i] hMeas.Fill(Ei, matrix_folded[i_Eg1, i]) # hack to recalculate the Uncertainties now, after the histogram is filled hMeas.Sumw2(False) hMeas.Sumw2(True) # hTrue.Sumw2(False) # doesn't work yet? # hTrue.Sumw2(True) # doesn't work yet? # print("==================================== UNFOLD ===================================") unfold = RooUnfoldBayes(response, hMeas, Niterations) # OR # unfold= RooUnfoldSvd (response, hMeas, 20); # OR #unfold= RooUnfoldTUnfold (response, hMeas); # OR # unfold= RooUnfoldIds (response, hMeas, 3); # OR # unfold= RooUnfoldInvert (response, hMeas); # OR hReco = unfold.Hreco() # unfold.PrintTable (cout, hTrue); matrix_unfolded1[i_Eg1, :] = np.array(hReco)[0:Nbins] matrix_unfolded1 = np.nan_to_num(matrix_unfolded1) np.save(fname_save_unf1, matrix_unfolded1) # cbar_ax3 = ax3.pcolormesh(E_array_folded, E_array_folded, matrix_unfolded1, norm=LogNorm(vmin=1e-1, vmax=1e3))
responseSD = mcfile.Get('2d_response_softdrop' + options.extension) truth = mcfile.Get('PFJet_pt_m_AK8Gen') truthSD = mcfile.Get('PFJet_pt_m_AK8SDgen') reco = datafile.Get('PFJet_pt_m_AK8') recoSD = datafile.Get('PFJet_pt_m_AK8SD') truth.Scale(1. / truth.Integral()) reco.Scale(1. / reco.Integral()) truthSD.Scale(1. / truthSD.Integral()) recoSD.Scale(1. / recoSD.Integral()) response.Draw('colz') unfold = RooUnfoldBayes(response, reco, 4) unfoldSD = RooUnfoldBayes(responseSD, recoSD, 4) reco_unfolded = unfold.Hreco() recoSD_unfolded = unfoldSD.Hreco() reco_unfolded.Draw() truth.SetLineColor(4) truth.Draw('SAME') pt_bin = { 0: '200 < p_{T} < 260', 1: '260 < p_{T} < 350', 2: '350 < p_{T} < 460',
DnResponse = g.Get('2d_response_jecdn') UpResponseSD = g.Get('2d_response_softdrop_jecup') DnResponseSD = g.Get('2d_response_softdrop_jecdn') else: UpResponse = g.Get('2d_response_' + options.sys + 'up') DnResponse = g.Get('2d_response_' + options.sys + 'dn') UpResponseSD = g.Get('2d_response_softdrop_' + options.sys + 'up') DnResponseSD = g.Get('2d_response_softdrop_' + options.sys + 'dn') if options.hist == 'h_msd': unfold_hnom = RooUnfoldBayes(NomResponseSD, hnom1, 4) unfold_hup = RooUnfoldBayes(UpResponseSD, hnom1, 4) unfold_hdn = RooUnfoldBayes(DnResponseSD, hnom1, 4) else: unfold_hnom = RooUnfoldBayes(NomResponse, hnom1, 4) unfold_hup = RooUnfoldBayes(UpResponse, hnom1, 4) unfold_hdn = RooUnfoldBayes(DnResponse, hnom1, 4) hnom2 = unfold_hnom.Hreco() hup2 = unfold_hup.Hreco() hdn2 = unfold_hdn.Hreco() hnom = hnom2.ProjectionY() hup = hup2.ProjectionY()
fout = TFile( "UnfoldingPlots/unfold_" + options.toUnfold + "_PowhegPythia8_" + options.lepType + "_" + syst_flag + closureout + norm_flag + ".root", "recreate") # ------------------------------------------------------------------------------------- # do the actual unfolding # ------------------------------------------------------------------------------------- print "------------ UNFOLDING (syst: " + syst_flag + ") ------------" n_iter = 3 print " using " + str(n_iter) + " iterations" unfold = RooUnfoldBayes(response, hMeas, n_iter) #unfold = RooUnfoldSvd(response, hMeas, 2); # get the unfolded distribution hReco = unfold.Hreco() hReco.Sumw2() # ------------------------------------------------------------------------------------- # Translate to cross section (not events) in bins of pt N/L/BR) # ------------------------------------------------------------------------------------- # TODO: should fix BR print "WARNING: treatment of branching ratio is wrong!" hTrue.Scale(1.0 / (lum * 0.438 / 3.)) # true @ parton level hMeas.Scale(1.0 / (lum * 0.438 / 3.)) # measured @ reco level