# 'std::bad\_alloc' after using TMVA::Factory ::BookMethod

**URL:** <https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618>\
**Category:** TMVA\
**Created:** [July 19, 2017, 7:35pm UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618 "2017-07-19T19:35:49Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [July 19, 2017, 7:35pm UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/1 "2017-07-19T19:35:49Z")

</div>

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!

---

<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:** [July 19, 2017, 8:13pm UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/2 "2017-07-19T20:13:31Z")

</div>

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 🙂

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [July 20, 2017, 6:47am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/3 "2017-07-20T06:47:01Z")

</div>

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)

---

<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:** [July 20, 2017, 8:26am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/4 "2017-07-20T08:26:53Z")

</div>

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

Cheers,  
Kim

---

<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:** [July 20, 2017, 8:59am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/5 "2017-07-20T08:59:00Z")

</div>

HI,

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

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [July 20, 2017, 9:04am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/6 "2017-07-20T09:04:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![willemv](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/w/b19c9b/32.png) [@willemv](https://root-forum.cern.ch/u/willemv)\
**Post date:** [July 27, 2017, 8:29am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/7 "2017-07-27T08:29:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![omazapa](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/omazapa/32/12711_2.png) [@omazapa](https://root-forum.cern.ch/u/omazapa)\
**Post date:** [July 27, 2017, 11:58am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/8 "2017-07-27T11:58:55Z")

</div>

Hi Willem

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

Cheers  
Omar.

---

<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:** [August 2, 2017, 10:08am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/9 "2017-08-02T10:08:02Z")

</div>

Hi,

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

Cheers,  
Kim

---

<div class="post-metadata">

**Author:** ![fiorini](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/f/e47c2d/32.png) [@fiorini](https://root-forum.cern.ch/u/fiorini)\
**Post date:** [September 23, 2022, 9:44am UTC](https://root-forum.cern.ch/t/std-bad-alloc-after-using-tmva-factory-bookmethod/25618/10 "2022-09-23T09:44:20Z")

</div>

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).
