<jupyter_start><jupyter_text>T M V A_Tutorial_Regression_Tmva_TestTMVA example, for regression with following objectives: * Test a BDT with TMVA**Author:** <NAME>  This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository  on Monday, April 26, 2021 at 03:51 PM.<jupyter_code>from ROOT import TMVA, TFile, TTree, TCut, TH1F, TCanvas, gROOT, TLegend
from subprocess import call
from os.path import isfile
 
gROOT.SetStyle("ATLAS")
 
outfileName = 'TMVA_tutorial_reg_1.root'
tfin = TFile.Open(outfileName, 'READ')<jupyter_output>Welcome to JupyROOT 6.22/07<jupyter_text>Helper function to fill histograms<jupyter_code>def fill_hists(tr, tag):
  nevents = tr.GetEntries()

  hname="mtt_truth_{0}".format(tag)
  nbins, xmin, xmax=200, 0, 2000
  h1 = TH1F(hname, hname, nbins, xmin, xmax)
  h1.Sumw2()
  hname="mtt_reco_{0}".format(tag)
  h2 = TH1F(hname, hname, nbins, xmin, xmax)
  h2.Sumw2()
  hname="BDT_{0}".format(tag)
  h3 = TH1F(hname, hname, nbins, xmin, xmax)
  h3.Sumw2()
  hname="BDTG_{0}".format(tag)
  h4 = TH1F(hname, hname, nbins, xmin, xmax)
  h4.Sumw2()
  
  for i in range(nevents):
    tr.GetEntry(i)
 
    mtt_truth = tr.mtt_truth     
    # mtt_reco = tr.mtt_reco     
    BDT = tr.BDT     
    BDTG = tr.BDTG     
    h1.Fill(mtt_truth)
    # h2.Fill(mtt_reco)
    h3.Fill(BDT)
    h4.Fill(BDTG)
    
  return [h1, h2, h3, h4]<jupyter_output><empty_output><jupyter_text>Helper function to normalize hists<jupyter_code>def norm_hists(h):

  h_new = h.Clone()
  hname = h.GetName() + "_normalized"
  h_new.SetName(hname)
  h_new.SetTitle(hname)
  ntot = h.Integral()
  if ntot!=0:
    h_new.Scale(1./ntot)

  return h_new<jupyter_output><empty_output><jupyter_text>Fill histograms<jupyter_code>tr_train = tfin.Get("dataset/TrainTree")
h_train = fill_hists(tr_train, "train")

tr_test = tfin.Get("dataset/TestTree")
h_test = fill_hists(tr_test, "test")<jupyter_output><empty_output><jupyter_text>Plotting<jupyter_code>myc = TCanvas("c", "c", 800, 600)
myc.SetFillColor(0)
myc.cd()<jupyter_output><empty_output><jupyter_text>Train vs Test===============BDT<jupyter_code>h1 = norm_hists(h_train[2])
h1.GetXaxis().SetTitle("BDT Regression m_{t#bar{t}} [GeV]")
h1.GetYaxis().SetTitle("A.U.")
h1.Draw()
h2 = norm_hists(h_test[2])
h2.SetLineColor(2)
h2.SetMarkerColor(2)
h2.Draw("same")<jupyter_output><empty_output><jupyter_text>Draw legends<jupyter_code>lIy = 0.92
lg = TLegend(0.60, lIy-0.25, 0.85, lIy)
lg.SetBorderSize(0)
lg.SetFillStyle(0)
lg.SetTextFont(42)
lg.SetTextSize(0.04)
lg.AddEntry(h1, "Training", "l")
lg.AddEntry(h2, "Testing", "l")
lg.Draw()

myc.Draw()
myc.SaveAs("TMVA_tutorial_reg_test_1.png")<jupyter_output>Info in <TCanvas::Print>: png file TMVA_tutorial_reg_test_1.png has been created<jupyter_text>BDTG<jupyter_code>h1 = norm_hists(h_train[3])
h1.GetXaxis().SetTitle("BDTG Regression m_{t#bar{t}} [GeV]")
h1.GetYaxis().SetTitle("A.U.")
h1.Draw()
h2 = norm_hists(h_test[3])
h2.SetLineColor(2)
h2.SetMarkerColor(2)
h2.Draw("same")<jupyter_output><empty_output><jupyter_text>Draw legends<jupyter_code>lIy = 0.92
lg = TLegend(0.60, lIy-0.25, 0.85, lIy)
lg.SetBorderSize(0)
lg.SetFillStyle(0)
lg.SetTextFont(42)
lg.SetTextSize(0.04)
lg.AddEntry(h1, "Training", "l")
lg.AddEntry(h2, "Testing", "l")
lg.Draw()

myc.Draw()
myc.SaveAs("TMVA_tutorial_reg_test_2.png")<jupyter_output>Info in <TCanvas::Print>: png file TMVA_tutorial_reg_test_2.png has been created<jupyter_text>Regression vs Target<jupyter_code>h1 = norm_hists(h_train[2])
h1.GetXaxis().SetTitle("m_{t#bar{t}} [GeV]")
h1.GetYaxis().SetTitle("A.U.")
h1.Draw()
h2 = norm_hists(h_train[3])
h2.SetLineColor(2)
h2.SetMarkerColor(2)
h2.Draw("same")
h3 = norm_hists(h_train[0])
h3.SetLineColor(4)
h3.SetMarkerColor(4)
h3.Draw("same")<jupyter_output><empty_output><jupyter_text>Draw legends<jupyter_code>lIy = 0.92
lg = TLegend(0.60, lIy-0.25, 0.85, lIy)
lg.SetBorderSize(0)
lg.SetFillStyle(0)
lg.SetTextFont(42)
lg.SetTextSize(0.04)
lg.AddEntry(h1, "Regression BDT", "l")
lg.AddEntry(h2, "Regression BDTG", "l")
lg.AddEntry(h3, "Target", "l")
lg.Draw()

myc.Draw()
myc.SaveAs("TMVA_tutorial_reg_test_3.png")<jupyter_output>Info in <TCanvas::Print>: png file TMVA_tutorial_reg_test_3.png has been created<jupyter_text>Draw all canvases<jupyter_code>from ROOT import gROOT 
gROOT.GetListOfCanvases().Draw()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1598079561042524, 'fraction_numerical': 0.04000914494741655, 'mean_word_length': 6.291666666666667, 'pattern_counts': {'":': 0, '<': 49, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '662852', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 2078, 'n_tokens_neox': 1971, 'n_words': 307, 'path': 'MVA/TMVA_tutorial_regression_tmva_test.py.nbconvert.ipynb', 'repo_name': 'LailinXu/hepstat-tutorial', 'size': '4377'}