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
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")
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)