Cannot call method of TMVA::MethodCuts after casting from TMVA::MethodBase

I am using TMVA methods for classification. Particularly, the Rectangular Cuts method is used. And I want to get cuts’ values after training:

    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;
    //and here I get segmentation violation
    Double_t trueEffS = methodCuts->GetCuts( 0.5, minCuts, maxCuts );

But I get the segmentation violation error after trying to call MethodCuts methods. Note that it seems that casting is successful. It had been checked by:

if (TMVA::MethodCuts* methodCuts = dynamic_cast<TMVA::MethodCuts*(methodCutsBase) ) { /*print smth*/ }

And also I can call MethodBase methods. For example,

std::cout << methodCuts->GetMethodName() << "\n";

prints Cuts.
How to fix it? Why so?

Full code here: TMVAClassification.C (2.5 KB)

1 Like

Hi,

Good question! It turns out that TMVA internally recreates the methods when certain conditions hold. If you save a pointer to the method before the training phase it is sometimes invalidated during training.

The safe way of getting a method is by retrieving it after training has been completed through

factory->TrainAllMethods();
// ... some more code ...
TMVA::IMethod * method = factory->GetMethod("dataset_name", "method_name");

The methods are internally recreated when xml weight-files are saved.

Cheers,
Kim

I am wondering why your code even compiles, your example as well as the attached file. You are missing a closing > for the dynamic_cast. But I guess that your real code is different?

OK. Your solution works. Thank you.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.