# Is it possible to get cuts from TMVA Rectangular Cuts method?

I am using TMVA Rectangular Cuts method to differentiate between signal and background. My sample space is 2-dimensional ( `eTotal` and `pTotal` ).
What if want to draw cuts (for some signal efficiency)? How do I get cuts’ values after training?

You may find information here I guess:
https://github.com/root-project/root/blob/master/documentation/tmva/UsersGuide/TMVAUsersGuide.pdf

I did not see any suggestion how to get cuts after training in that user’s guide. Only application of trained algorithm. Though I could search more accurately.

I think I should use the `xml` file that was created after the training.
So I used the pugixml library to parse `xml` files. After installing it (see this post) I wrote a simple program which is intended neither to be full nor perfect but only for illustrate how to get the cuts from `TMVA Rectangular Cuts` method.
Below I provide program code, my `xml` file and how to use it `README`:

CodeAndFile.zip (7.5 KB)

The Cuts method also provides a method `GetCuts()` which you can use.

``````TMVA::Factory f("", "");
MethodBase * methodCutsBase = f.BookMethod(TMVA::Types::kCuts, "Cuts", "options");
MethodCuts * methodCuts = dynamic_cast<MethodCuts *>(methodCutsBase);
f.TrainAllMethods();

std::vector<Double_t> cutsMin;
std::vector<Double_t> cutsMax;
Double_t true_effS = methodCuts.GetCuts(effS, cutsMin, cutsMax);

// The rectangular cuts can now be read from the cutsMin and cutsMax vectors.
``````

Hope it helps

Cheers,
Kim

1 Like

Thank you. So simple.
It would be useful if this info was in TMVA user’s guide.
Though you declared `methodCuts` as a pointer so you have to use `->` instead of `.`.

1 Like

After calling `GetCuts` method I get the `segmentation violation` error:

``````    factory->BookMethod( dataloader, TMVA::Types::kKNN, "kNN", "nkNN=40");
TMVA::MethodBase* methodCutsBase = factory->BookMethod( dataloader, TMVA::Types::kCuts, "Cuts", "FitMethod=GA:EffMethod=EffSel");
TMVA::MethodCuts* methodCuts = dynamic_cast<TMVA::MethodCuts* >(methodCutsBase);
//Train methods
factory->TrainAllMethods();
//Get cuts from Cuts method
std::vector<Double_t> minCuts, maxCuts;
Double_t trueEffS = methodCuts->GetCuts( 0.5, minCuts, maxCuts );
``````

Why and how to fix it? Any ideas?

I made a mistake in my previous posting, corrected code below

``````TMVA::Factory f("...", "...");
f.BookMethod(TMVA::Types::kCuts, "Cuts", "options");
f.TrainAllMethods();

TMVA::IMethod    * methodCutsBase = factory.GetMethod("dataset", "Cuts");
TMVA::MethodCuts * methodCuts     = dynamic_cast<TMVA::MethodCuts *>(methodCutsBase);

std::vector<Double_t> cutsMin;
std::vector<Double_t> cutsMax;
Double_t true_effS = methodCuts->GetCuts(effS, cutsMin, cutsMax);

// One can also use PrintCuts to get a quick overview of the values
methodCuts->PrintCuts(0.5); // arg is signal efficiency
``````

Yes. I think it would be useful for people to read this post. Thank you again.

1 Like

That is not a convenient way to get cuts info at the training stage sometimes. Better way is to get that info at the application stage. But there we have to use `TMVA::Reader`. So it can be done with

``````TMVA::Reader reader = new TMVA::Reader( "Options" );
...
...
//Get cuts
TMVA::MethodCuts* methodCuts = reader->FindCutsMVA( "Cuts" );
methodCuts->PrintCuts( effS );
``````

Note that no dynamic casts needed here (though, probably, they all are inside classes).

2 Likes

Dear p73,

Thank you for your contributive question! I searched the whole TMVA manual and could not find cut-printing until seen your post.

Visualizing and explaining the result from the black box is literally important, especially when the s/b has real physical meaning. I agree with your idea that it should have been more convenient.

If possible, could you please show me the complete code for training and printing the rectangular cut? All methods work for me: post the code here, upload it to the GitHub, or through email.

Best,
Wenbo