# Extract BDT score of training and testing samples

**URL:** <https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609>\
**Category:** TMVA\
**Created:** [April 15, 2019, 12:44pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609 "2019-04-15T12:44:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Einsiedler](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@Einsiedler](https://root-forum.cern.ch/u/Einsiedler)\
**Post date:** [April 15, 2019, 12:44pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/1 "2019-04-15T12:44:34Z")

</div>

Dear TMVA users,

I was wondering if there was any simple way to extract the BDT Score of the training and testing samples in TMVAClassification.C to a txt file (for example).

Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [April 15, 2019, 3:11pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/2 "2019-04-15T15:11:06Z")

</div>

Hi,

The values of the BDT score are stored in a ROOT file (“TMVA.root”). With a very simple Python (or C++) code you should be able to extract the information and write in a text file. If you need an example, I could provide for you

Lorenzo

---

<div class="post-metadata">

**Author:** ![Einsiedler](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/e/51bf81/32.png) [@Einsiedler](https://root-forum.cern.ch/u/Einsiedler)\
**Post date:** [April 15, 2019, 3:26pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/3 "2019-04-15T15:26:52Z")

</div>

Thank you Lorenzo, it looks like I can easily extract that information from the .root file resulting from the training. In the meantime, I also manage to simply apply the .class.C file along with the .xml to the samples I used with GetMvaValue and I found the same values and histograms. Thank you very much for your suggestion 🙂

---

<div class="post-metadata">

**Author:** ![anprodri](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@anprodri](https://root-forum.cern.ch/u/anprodri)\
**Post date:** [September 10, 2019, 3:36pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/4 "2019-09-10T15:36:50Z")

</div>

Hi!

I’m really lost about this. Could you show me the script for reading the BDT score? I would really appreciate it.

Kind regards.

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [September 10, 2019, 5:41pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/5 "2019-09-10T17:41:41Z")

</div>

Hi,

I’ll provide a few simple examples to help you start out. The ROOT examples are for the interactive prompt but can easily be converted to stand-alone scripts.

This assumes you are looking at the tutorial TMVA.root (generate by running `root -l TMVAClassifcation.C` in your `$ROOTSYS/tutorials/tmva` directory.) Modify to fit your usecase, change “BDTG” to the name of your classifier e.g. 🙂

If you’re using ROOT directly:

```auto
root [0] reader = TTreeReader("dataset/TrainTree", TFile::Open("TMVA.root"));
root [1] reader_bdtg = TTreeReaderValue<Float_t>(reader, "BDTG");
root [2] reader.SetEntry(0);
root [3] *reader_bdtg.Get()
(float) 0.976866f

```

or,

```auto
root [0] reader = TTreeReader("dataset/TrainTree", TFile::Open("TMVA.root"));
root [1] reader_bdtg = TTreeReaderValue<Float_t>(reader, "BDTG");
root [2] while (reader.Next()) {std::cout << *reader_bdtg.Get() << std::endl;}
// Entire tree will be printed :)

```

If you’re using python:

```auto
import ROOT
f = ROOT.TFile.Open('TMVA.root')
t = f.Get('dataset/TrainTree')

# Print a single entry:
t.SetEntry(0)
t.BDTG

# Print a all entries:
for entry in tree:
    print(entry.BDTG)

```

Further references:

- ROOT
  - [TTreeReader tutorial](https://root.cern/doc/master/hsimpleReader_8C.html) – check this first, the others on demand.
  - [TTree tutorials](https://root.cern/doc/master/group __tutorial__ tree.html)
  - [TTreeReader class reference](https://root.cern.ch/doc/master/classTTreeReader.html)
  - [ROOT Users’ Guide on reading TTrees](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#examples-for-writing-and-reading-trees)

- python
  - [Reading a TTree with pyROOT](https://root.cern/doc/master/pyroot002__TTreeAsMatrix_8py.html) – check this first, the others on demand.
  - [pyROOT tutorials](https://root.cern/doc/master/group __tutorial__ pyroot.html)
  - [ROOT Users’ Guide on reading TTrees with pyROOT](https://root.cern.ch/root/htmldoc/guides/users-guide/ROOTUsersGuide.html#working-with-trees)

- General
  - [ROOT tutorials](https://root.cern/doc/master/group__Tutorials.html)

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![anprodri](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@anprodri](https://root-forum.cern.ch/u/anprodri)\
**Post date:** [September 10, 2019, 7:04pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/6 "2019-09-10T19:04:27Z")

</div>

Thank you so much! It worked. Now, I was wondering if there’s a way of getting the Kolmogorov-Smirnov test as an output value in Python. Do you have any idea?

Kind regards.

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [September 11, 2019, 2:30pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/7 "2019-09-11T14:30:29Z")

</div>

TMVA uses the function found [here](https://root.cern.ch/doc/master/classTH1.html#aeadcf087afe6ba203bcde124cfabbee4) to perform the test.

Usage, something like so:

```auto
TFile * file = TFile::Open("TMVA.root");
TString base_path = "dataset/Method_BDT/BDTG/";
TH1 * sig_test = file->Get<TH1>(base_path + "MVA_BDTG_S");
TH1 * sig_train = file->Get<TH1>(base_path + "MVA_BDTG_Train_S");
Double_t kol_sig = sig_test->KolmogorovTest(sig_train, "X");

std::cout << "K-S score: " << kol_sig << std::endl;

```

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![anprodri](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@anprodri](https://root-forum.cern.ch/u/anprodri)\
**Post date:** [September 13, 2019, 5:45pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/8 "2019-09-13T17:45:09Z")

</div>

Thank you so much! It was really helpful.

Have a nice weekend!

---

<div class="post-metadata">

**Author:** ![anprodri](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@anprodri](https://root-forum.cern.ch/u/anprodri)\
**Post date:** [September 24, 2019, 12:59pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/9 "2019-09-24T12:59:33Z")

</div>

Hi again! I have another question.

When I run the K-S test using:  
sig\_test-\>KolmogorovTest(sig\_train, “X”)  
I assume this is the unbinned case, and considering this assumption, if the returned value is close to zero, would that mean that the distributions are similar?

---

<div class="post-metadata">

**Author:** ![kialbert](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/kialbert/32/16537_2.png) [@kialbert](https://root-forum.cern.ch/u/kialbert)\
**Post date:** [September 24, 2019, 5:01pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/10 "2019-09-24T17:01:44Z")

</div>

Hi,

Just an FYI: We would prefer that you create a new topic when there is a solution provided in a previous one.

Now: The statistic for the “X” option ranges from 0 to 1 where 1 means the two distributions are equal.

You can test this yourself like so:

```auto
root [0] a = TH1F{"a", "a", 20, -10., 10.};
root [1] b = TH1F{"b", "b", 20, -10., 10.};
root [2] c = TH1F{"c", "c", 20, -10., 10.};
root [3] a.FillRandom("gaus", 10000);
root [4] b.FillRandom("gaus", 10000);
root [5] c.FillRandom("expo", 10000);
root [6] a.KolmogorovTest(&a, "X")
(double) 1.0000000
root [7] a.KolmogorovTest(&b, "X")
(double) 0.93600000
root [8] a.KolmogorovTest(&c, "X")
(double) 0.00000000

```

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![anprodri](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/a/8dc957/32.png) [@anprodri](https://root-forum.cern.ch/u/anprodri)\
**Post date:** [September 26, 2019, 3:24pm UTC](https://root-forum.cern.ch/t/extract-bdt-score-of-training-and-testing-samples/33609/11 "2019-09-26T15:24:17Z")

</div>

I understand now. Thanks a lot!

Have a nice week.
