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
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 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 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
f = ROOT.TFile("unfoldSample.root", "RECREATE") responseBB = RooUnfoldResponse(recBB, genBB, reMBB, "reBB", "reBB") #responseBB.UseOverflow() print recBB.GetNbinsX() print reMBB.GetNbinsX() #responseBB=RooUnfoldResponse(reMBB) print "Print values of RooUnfold response matrix for BB category" responseBB.Print() print "Print values of response matrix for calculated by hand for BB category" reMBBList = [] for i in range(1, reMBB.GetNbinsX() + 1): column = [] for j in range(1, reMBB.GetNbinsX() + 1): column.append(reMBBn.GetBinContent(i, j)) print column invertBB = RooUnfoldInvert(responseBB, hmuBB, "UnfoldedBB", "UnfoldedBB") inverthmuBB = invertBB.Hreco() hmuBB.Write() inverthmuBB.Write() c1 = ROOT.TCanvas("c1", "c1", 800, 800) c1.SetLogx() c1.SetLogy() inverthmuBB.SetLineColor(2) inverthmuBB.Draw("hist") hmuBB.Draw("samehist") #genBB.Draw() c1.Print("test1.pdf") responseBE = RooUnfoldResponse(recBE, genBE, reMBE, "reBB", "reBB") #responseBE.UseOverflow() print "Print values of RooUnfold response matrix for BE category" responseBE.Print()
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) hraw = Proj2D_Y(Reco2d, minMass, maxMass, Gen2d.GetName(), True) hUnf1d = Proj2D_Y(hUnf2d, minMass, maxMass, Gen2d.GetName(), True)
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