# How to extract Cut Value?

**URL:** <https://root-forum.cern.ch/t/how-to-extract-cut-value/27653>\
**Category:** TMVA\
**Created:** [January 21, 2018, 12:37pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653 "2018-01-21T12:37:33Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![maffe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@maffe](https://root-forum.cern.ch/u/maffe)\
**Post date:** [January 21, 2018, 12:37pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/1 "2018-01-21T12:37:33Z")

</div>

Hi !  
I’m doing some TMVA Boosted Decision Tree exercise and I’d like to save the Cut Value as a variable ? Basically the 0.019 variable. I couldn’t find the method to call.

— Factory : --------------------  
— Factory : Method: Cut value:  
— Factory : --------------------  
— Factory : BDT: +0.019  
— Factory : --------------------

Thanks !!

---

<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:** [January 26, 2018, 12:03pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/2 "2018-01-26T12:03:31Z")

</div>

Hi,

What is it that you want to achieve with this cut value one you have it and what is your configuration to produce the given output?

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![maffe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@maffe](https://root-forum.cern.ch/u/maffe)\
**Post date:** [January 26, 2018, 12:53pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/3 "2018-01-26T12:53:04Z")

</div>

> [@kialbert](#):
>
> What is it that you want to achieve with this cut value one you have it and what is your configuration to produce the given output?

I’m looping over a different choice of train and test subsamples, and would like to compute the accuracy on the results by counting the true events I get. For that I would need the cut values I guess.

The factory configurations is: factory.BookMethod(TMVA.Types.kBDT, “BDT”, "BoostType=AdaBoost:"nCuts=20:"SeparationType=GiniIndex:“Ntrees=500:MaxDepth=3”).

Thanks !

---

<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:** [January 26, 2018, 2:49pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/4 "2018-01-26T14:49:16Z")

</div>

I’m not sure I still understand what it is you want to do. Is the cut value presented as the cut value that optimises signal efficiency \* purity?

Maybe this piece of code can help you calculate the accuracy of your classifier. Basically, you can loop over the results and calculate it for a given cut value manually.

```auto
UInt_t numEvents = ...;
TMVA::Factory factory = new Factory(...);

TMVA::IMethod * imethod = factory->GetMethod("datasetName", "methodName");
TMVA::MethodBase * method = dynamic_cast<MethodBase *>(imethod);

TMVA::DataSet * dataset = method->Data();
TMVA::DataSetInfo * dsi = method->DataInfo();
TMVA::Results * results = dataset->GetResults("methodName", TMVA::Types::kTesting, TMVA::Types::kClassification);

Double_t cutValue = 0.019;
UInt_t true_positives = 0;
UInt_t false_positives = 0;
UInt_t true_negatives = 0;
UInt_t false_negatives = 0;
for (UInt_t iEvent = 0; iEvent < numEvents; ++iEvent) {
    Double_t valueEvent = results[iEvent][0];
    TMVA::Event ev = dataset->GetEvent(iEvent, TMVA::Types::kTesting);
    if (valueEvent > cutValue) {
        // Event passed cut, consider it signal
        true_positives += dsi->IsSignal(ev); // True signal
        true_positives += not (dsi->IsSignal(ev)); // True background
    } else {
        // Event failed cut, consider it background
        false_negative += dsi->IsSignal(ev); // True signal
        true_negative += not (dsi->IsSignal(ev)); // True background
    }
}

```

Hope this helps!

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![maffe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@maffe](https://root-forum.cern.ch/u/maffe)\
**Post date:** [January 26, 2018, 3:04pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/5 "2018-01-26T15:04:00Z")

</div>

Yes, that is what I want to achieve but I need to extract the cutValue (0.019) automatically, through some  
.GetCutValue -like method.

cheers, maffe.

---

<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:** [January 26, 2018, 5:09pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/6 "2018-01-26T17:09:10Z")

</div>

Hi,

I think this is what you are looking for

```auto
TMVA::IMethod * imethod = factory->GetMethod("datasetName", "methodName");
TMVA::MethodBase * method = dynamic_cast<MethodBase *>(imethod);

Double_t cut = method->GetSignalReferenceCut();

```

This is what is printed when `EvaluateAllMethods` is run.

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![maffe](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/48db29/32.png) [@maffe](https://root-forum.cern.ch/u/maffe)\
**Post date:** [January 26, 2018, 8:09pm UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/7 "2018-01-26T20:09:47Z")

</div>

Marvellous Kim.  
cut = GetMethod(“BDT”).GetSignalReferenceCut()  
was what I was looking for.

Thanks,  
maffe.

---

<div class="post-metadata">

**Author:** ![lgolino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/9f8e36/32.png) [@lgolino](https://root-forum.cern.ch/u/lgolino)\
**Post date:** [July 26, 2024, 8:27am UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/8 "2024-07-26T08:27:18Z")

</div>

Hi, this seems to no longer work. Is there a better way to get the confusion matrix of a dataset? I want the accuracy and F1 score etc. Further, is there a way to get the signal at B=0.01 and the AUC of ROC from the factory/dataset?

---

<div class="post-metadata">

**Author:** ![lgolino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/9f8e36/32.png) [@lgolino](https://root-forum.cern.ch/u/lgolino)\
**Post date:** [July 26, 2024, 8:35am UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/9 "2024-07-26T08:35:44Z")

</div>

For example:

```auto
In file included from input_line_8:1:
macro.C:591:46: error: unknown type name 'MethodBase'
TMVA::MethodBase * methodbase = dynamic_cast<MethodBase *>(imethod);
                                             ^
macro.C:591:20: error: cannot initialize a variable of type 'TMVA::MethodBase *' with an lvalue of type 'TMVA::IMethod *'
TMVA::MethodBase * methodbase = dynamic_cast<MethodBase *>(imethod);
                   ^ ~~~~~~~
macro.C:594:21: error: no viable conversion from 'DataSetInfo' to 'TMVA::DataSetInfo *'
TMVA::DataSetInfo * dsi = methodbase->DataInfo();
                    ^ ~~~~~~~~~~~~~~~~~~~~~~
macro.C:602:34: error: use of undeclared identifier 'numEvents'
for (UInt_t iEvent = 0; iEvent < numEvents; ++iEvent) {
                                 ^
macro.C:603:14: error: no viable conversion from 'const std::vector<Float_t>' (aka 'const vector<float>') to 'Double_t' (aka 'double')
    Double_t valueEvent = results[iEvent][0];
             ^ ~~~~~~~~~~~~~~~~~~
macro.C:604:17: error: no viable conversion from 'const Event *' to 'TMVA::Event'
    TMVA::Event ev = dataset->GetEvent(iEvent, TMVA::Types::kTesting);
                ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/alpha/packages/root_gpu/install/include/TMVA/Event.h:59:7: note: candidate constructor not viable: no known conversion from 'const Event *' to 'const Event &' for 1st argument; dereference the argument with *
      Event( const Event& );
      ^
In file included from input_line_8:1:
macro.C:607:41: error: no viable conversion from 'TMVA::Event' to 'const Event *'
        true_positives += dsi->IsSignal(ev); // True signal
                                        ^~
/home/alpha/packages/root_gpu/install/include/TMVA/DataSetInfo.h:156:49: note: passing argument to parameter 'ev' here
      Bool_t IsSignal( const Event* ev ) const;
                                                ^
In file included from input_line_8:1:
macro.C:608:46: error: no viable conversion from 'TMVA::Event' to 'const Event *'
        true_positives += not (dsi->IsSignal(ev)); // True background
                                             ^~
/home/alpha/packages/root_gpu/install/include/TMVA/DataSetInfo.h:156:49: note: passing argument to parameter 'ev' here
      Bool_t IsSignal( const Event* ev ) const;
                                                ^
In file included from input_line_8:1:
macro.C:611:9: error: use of undeclared identifier 'false_negative'
        false_negative += dsi->IsSignal(ev); // True signal
        ^
macro.C:611:41: error: no viable conversion from 'TMVA::Event' to 'const Event *'
        false_negative += dsi->IsSignal(ev); // True signal
                                        ^~
/home/alpha/packages/root_gpu/install/include/TMVA/DataSetInfo.h:156:49: note: passing argument to parameter 'ev' here
      Bool_t IsSignal( const Event* ev ) const;
                                                ^
In file included from input_line_8:1:
macro.C:612:9: error: use of undeclared identifier 'true_negative'
        true_negative += not (dsi->IsSignal(ev)); // True background
        ^
macro.C:612:45: error: no viable conversion from 'TMVA::Event' to 'const Event *'
        true_negative += not (dsi->IsSignal(ev)); // True background
                                            ^~
/home/alpha/packages/root_gpu/install/include/TMVA/DataSetInfo.h:156:49: note: passing argument to parameter 'ev' here
      Bool_t IsSignal( const Event* ev ) const;

```

---

<div class="post-metadata">

**Author:** ![lgolino](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/9f8e36/32.png) [@lgolino](https://root-forum.cern.ch/u/lgolino)\
**Post date:** [July 28, 2024, 5:08am UTC](https://root-forum.cern.ch/t/how-to-extract-cut-value/27653/10 "2024-07-28T05:08:25Z")

</div>

fixed like this:

```auto
TMVA::IMethod * imethod = factory->GetMethod("dataset", myMethodList);
TMVA::MethodBase * methodbase = dynamic_cast<TMVA::MethodBase *>(imethod);

TMVA::DataSet * dataset = methodbase->Data();
//TMVA::DataSetInfo * dsi = methodbase->DataInfo();
TMVA::DataSetInfo& dsi = methodbase->DataInfo();
TMVA::Results * results = dataset->GetResults(myMethodList, TMVA::Types::kTesting, TMVA::Types::kClassification);

Double_t cutValue = methodbase->GetSignalReferenceCut();
UInt_t numEvents = methodbase->GetNEvents();
UInt_t true_positives = 0;
UInt_t false_positives = 0;
UInt_t true_negatives = 0;
UInt_t false_negatives = 0;
for (UInt_t iEvent = 0; iEvent < numEvents; ++iEvent) {
    double valueEvent = results[0][iEvent][0];
    const TMVA::Event* ev = dataset->GetEvent(iEvent, TMVA::Types::kTesting);
    if (valueEvent > cutValue) {
        // Event passed cut, consider it signal
        true_positives += dsi.IsSignal(ev); // True signal
        true_positives += not (dsi.IsSignal(ev)); // True background
    } else {
        // Event failed cut, consider it background
        false_negatives += dsi.IsSignal(ev); // True signal
        true_negatives += not (dsi.IsSignal(ev)); // True background
    }
}

```
