'std::bad_alloc' after using TMVA::Factory ::BookMethod

I am trying to use TMVA for the first time, hoping to train a BDT discriminator. At the moment I was trying to train a simple discriminator using 2 variables, and to add signal and background training and test events “by hand”. The relevant parts of my code could be summarized as:

TString outfileName("TMVA.root");
TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile, "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );
TMVA::DataLoader *dataloader=new TMVA::DataLoader("dataset");
dataloader->AddVariable( "a", 'F' );
dataloader->AddVariable( "b", 'F' );

for(Long64_t it = 0; it < treeSignal->GetEntries();; ++it){
	treeSignal->GetEntry(it);
	std::vector<Double_t> vars(2);
	vars[0] = a;
	vars[1] = b;
	dataloader->AddSignalTrainingEvent( vars, 1. );
}

for(Long64_t it = 0; it < treeBkg->GetEntries();; ++it){
	treeBkg->GetEntry(it);
	std::vector<Double_t> vars(2);
	vars[0] = a;
	vars[1] = b;
	dataloader->AddBackgroundTrainingEvent( vars, 1. );
}
factory->BookMethod( dataloader, TMVA::Types::kBDT, "BDT", "!H:!V:NTrees=850:MinNodeSize=2.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20");
factory->TrainAllMethods();

However, the moment I try “factory->BookMethod(…)” the code crashes with the error message “terminate called after throwing an instance of ‘std::bad_alloc’ what(): std::bad_alloc Aborted (core dumped)”, and I can not seem to figure out what the problem is. What am I missing here that makes the code crash? (extra note : For the moment my code and trees are organized in such a way that I can only add training events by hand, and can not use “->AddSignalTree” etc.) Thanks in advance for the help!

Strange. It’s hard to say what the problem is with only this information, but you should definitely at least get a sensible error message in this situation.

If you could

  • verify that a and b have the values that you expect when adding the event.
  • try another method, e.g. Fisher just to see if this is specific to the BDT.
  • Provide a minimal reproducer (a file containing as little code as possible while still reproducing the error). Perhaps using TMVA::Classification as base to get some example trees.

And get back to us, that would help us debug the problem :slight_smile:

Cheers,
Kim

1 Like

Hello Kim,

Thanks for the reply! I checked the variables a and b, and they were always sensible. Upon trying a Fisher discriminant however, the error chages. Now the code manages to book the method in fact, but crashes on the training stage, giving the error

Factory                  : Booking method: Fisher                                                    ]
                     : 
Factory                  : Train all methods
<FATAL>                         : No input data for the training provided!
***> abort program execution
terminate called after throwing an instance of 'std::runtime_error'
what():  FATAL error
Aborted (core dumped)

Does this help you in localizing what the issue might be? So the difference is basically that for a BDT I can not “book” the method while for a Fisher discriminant I can book the method, but not train it. I do not understand where the “No input data for the training provided” comes from as my code clearly calls “>AddSignalTrainingEvent(…)” and the same for the background, and the variables given to this function were all sensible.

As requested I also made a small example that gives me the error messages in question:

    #include "TMVA/Factory.h"
#include "TMVA/DataLoader.h"
#include "TMVA/Tools.h"


void Loop(){
	const TString fileName = "test.root";
	//Read Trees from ROOT files
	TFile* file =  new TFile("../data_april17/" + fileName);
	file->cd("FakeElectrons");
	TTree* tree = (TTree*) (file->Get("FakeElectrons/fakeTree"));
	Double_t _lPt[20];
	Double_t _lEta[20];
	TBranch *b__lPt;
	TBranch *b__lEta;
	tree->SetBranchAddress("_lPt", _lPt, &b__lPt);
	tree->SetBranchAddress("_lEta", _lEta, &b__lEta);


	TMVA::Tools::Instance();

	TString outfileName("TMVA.root");
   	TFile* outputFile = TFile::Open(outfileName, "RECREATE" );
	TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", outputFile,
		                                           "!V:!Silent:Color:DrawProgressBar:Transformations=I;D;P;G,D:AnalysisType=Classification" );
	TMVA::DataLoader *dataloader=new TMVA::DataLoader("dataset");
	dataloader->AddVariable( "a", 'F' );
   	dataloader->AddVariable( "b", 'F' );

	//Loop over sample
	for(Long64_t it = 0; it < tree->GetEntries(); ++it){
		tree->GetEntry(it);
		std::vector<Double_t> vars = {_lPt[0], _lEta[0]};
		dataloader->AddSignalTrainingEvent( vars, 1. );
		vars[1] = _lPt[0];
		vars[0] = _lEta[0];
		dataloader->AddBackgroundTrainingEvent( vars, 1. );
	}
	factory->BookMethod( dataloader, TMVA::Types::kBDT, "BDT");//, "!H:!V:NTrees=850:MinNodeSize=2.5%:MaxDepth=3:BoostType=AdaBoost:AdaBoostBeta=0.5:UseBaggedBoost:BaggedSampleFraction=0.5:SeparationType=GiniIndex:nCuts=20");
	//factory->BookMethod( dataloader, TMVA::Types::kFisher, "Fisher", "H:!V:Fisher:VarTransform=None:CreateMVAPdfs:PDFInterpolMVAPdf=Spline2:NbinsMVAPdf=50:NsmoothMVAPdf=10" );
	factory->TrainAllMethods();
	outputFile->Close();
	delete factory;
	delete dataloader;
}

I read two variables out of my tree “test.root” , being “_lPt”, and “_lEta”, which are arrays of 1 to 20 elements, and atleast the first entry was always initialized. Running this small example code, crashes on “bookmethod” when using a BDT discriminator and during the training stage for a Fisher discriminant.

(The ROOT version I am using is 6.08.06)

Thanks, that is very helpful. I’ll look into it as soon as possible.

Cheers,
Kim

HI,

It looks to me you had a memory problem. How many events are you using and how many variables ?

Cheers

Lorenzo

Hello Lorenzo,

I used 2 variables as a first try. The sample I have contains a few million events, but regardless whether I run over a tiny fraction of it or the whole sample I get the same result. The small snippet of code I provided produces all the errors (atleast in my case), and shows what variables I use etc.

regards,

Willem

Is there anyone still looking into this problem? It still has not been solved.

Hi Willem

please try calling the method dataloader->PrepareTrainingAndTestTree
If not, can you provide the root file to reproduce the problem?

Cheers
Omar.

Hi,

Any update on giving access to the file “test.root” or trying out adding the dataloder->PrepareTrainingAndTestTree?

Cheers,
Kim

Hi,
I stumbled on a similar problem myself and I would like to report the solution my case for future reference.
In my case, the issue causing the error was that the
“dataloder->PrepareTrainingAndTestTree” command was before the “AddXXXEvent” part.

The error is trivial to solve, but the error message doesn’t help much to solve it (especially in the case of the BDT).