Change in OptimizeTuningParameters

I wanted to change OptimizeTuningParameters.I tried to change with Runtime Polymorphism. I created a new class inheriting from TMVA::MethodBDT and redefined the OptimizeTuningParameters function. I got *** Break *** segmentation violation error.

  TMVA::Factory factory{"<name of factory>", "AnalysisType=Classification" };
  TMVA::DataLoader dataloader{"dataset"};

     // Set up dataloader ...

   TMVA::MethodBase* method = factory->BookMethod(&dataloader,   TMVA::Types::kBDT,"BDT","BoostType=AdaBoost");

    MethodBDT_derived_class* Mymethod = new   MethodBDT_derived_class("TMVA","BDT",dataloader.GetDataSetInfo(),"BoostType=AdaBoost");

    method = Mymethod;

Please help me how to fix this.

Where to you get this break ? in your new version of OptimizeTuningParameters ?

yes I got this break in my new version of OptimizeTuningParameters even when I did not make any changes in content of primary OptimizeTuningParameters function

That’s TMVA so @moneta may know, But can you post the trace back you get ? it might help to understand where this break comes from.

I used TMVA from ROOT 6.14/02 and got *** Break *** segmentation violation error. when I use ROOT 6.22/06 I get this error

  <FATAL>                         : Dataset[dataset] : MethodBase::BaseDir() -    MethodBaseDir() return a NULL pointer!
  ***> abort program execution
  terminate called after throwing an instance of 'std::runtime_error'
  what():  FATAL error


I think adding a new method class is not so trivial. Can you please post your code and I will investigate your issue



Hi Lorenzo,
What I did :

       	#ifndef MYMethodBDT_derived_H_
        #define MYMethodBDT_derived_H_
class  MethodBDT_derived : public TMVA::MethodBDT {
	MethodBDT_derived( const TString& jobName, const TString& methodTitle, TMVA::DataSetInfo& theData, const TString& theOption = ""):MethodBDT(jobName,methodTitle,theData, ""){}; 		
	std::map<TString,Double_t>  OptimizeTuningParameters(TString fomType, TString fitType) ;

std::map<TString,Double_t>  MethodBDT_derived::OptimizeTuningParameters(TString fomType, TString fitType)

    // fill all the tuning parameters that should be optimized into a map:
   std::map<TString,TMVA::Interval*> tuneParameters;
   std::map<TString,Double_t> tunedParameters;
   // note: the 3rd parameter in the interval is the "number of bins", NOT the stepsize !!
   //       the actual VALUES at (at least for the scan, guess also in GA) are always
   //       read from the middle of the bins. Hence.. the choice of Intervals e.g. for the
   //       MaxDepth, in order to make nice integer values!!!
   // find some reasonable ranges for the optimisation of MinNodeEvents:
   tuneParameters.insert(std::pair<TString,TMVA::Interval*>("NTrees",         new TMVA::Interval(50,1000,20))); //  stepsize 50
   tuneParameters.insert(std::pair<TString,TMVA::Interval*>("MaxDepth",       new TMVA::Interval(2,4,3)));    // stepsize 1
   tuneParameters.insert(std::pair<TString,TMVA::Interval*>("MinNodeSize",    new TMVA::LogInterval(1,30,30)));    //
   //tuneParameters.insert(std::pair<TString,TMVA::Interval*>("NodePurityLimit",new TMVA::Interval(.4,.6,3)));   // stepsize .1
   //tuneParameters.insert(std::pair<TString,TMVA::Interval*>("BaggedSampleFraction",new TMVA::Interval(.4,.9,6)));   // stepsize .1
	tuneParameters.insert(std::pair<TString,TMVA::Interval*>("AdaBoostBeta",   new TMVA::Interval(.2,1.,5))); 

   Log()<<TMVA::kINFO << " the following BDT parameters will be tuned on the respective *grid*\n"<<TMVA::Endl;
   std::map<TString,TMVA::Interval*>::iterator it;
   for(it=tuneParameters.begin(); it!= tuneParameters.end(); ++it){
      Log() << TMVA::kWARNING << it->first << TMVA::Endl;
      std::ostringstream oss;

   TMVA::OptimizeConfigParameters optimize(this, tuneParameters, fomType, fitType);
return tunedParameters;


void test()

	TMVA::Factory *factory = new TMVA::Factory( "TMVAClassification", "AnalysisType=Classification" );

	TFile *input_Signal_ACT(0);
	TString fname_Signal_ACT = "Signal_ACT.root";
	if(!gSystem->AccessPathName( fname_Signal_ACT ))
		input_Signal_ACT = TFile::Open( fname_Signal_ACT ); 
	if (!input_Signal_ACT)
		std::cout << "ERROR: could not open Signal_ACT file" << std::endl;
	std::cout << "--- TMVA       : Using input file for signal: " << input_Signal_ACT->GetName() << std::endl;

	TFile *input_Background(0);
	TString fname_Background = "Background.root";
	if(!gSystem->AccessPathName( fname_Background ))
		input_Background = TFile::Open( fname_Background ); 
	if (!input_Background)
		std::cout << "ERROR: could not open Background file" << std::endl;

	TTree *signalTree     = (TTree*)input_Signal_ACT->Get("SR1");

	TTree *backgroundTree  = (TTree*)input_Background->Get("SR1");

	TMVA::DataLoader dataloader{"dataset"};

	dataloader.AddVariable( "GammaPt_SR1", "P_{T,#gamma}[GeV]", 'F' );
	dataloader.AddVariable( "EleCharge_SR1","e_{charge}", 'F' );
	dataloader.AddVariable( "TopMass_SR1", "m_{e #nu b}[GeV]", 'F' );
	dataloader.AddVariable( "Dr_e_photon_SR1", "#Delta R_{e,#gamma}", 'F' );
	dataloader.AddVariable( "DrTopG_SR1", "#Delta R_{top,#gamma}", 'F' );
	dataloader.AddVariable( "Dr_e_Bjet_SR1", "#Delta R_{e,b-jet}", 'F' );
	dataloader.AddVariable( "Met_SR1", "Met[GeV]", 'F' );
	dataloader.AddVariable( "EleEta_SR1", "#eta_{e}", 'F' );
	dataloader.AddVariable( "GammaEta_SR1", "#eta_{#gamma}", 'F' );

	dataloader.AddSignalTree( signalTree);


	TCut mycuts = ""; 
	TCut mycutb = ""; 

	dataloader.PrepareTrainingAndTestTree( mycuts, mycutb,"nTrain_Signal=0:nTrain_Background=0:SplitMode=Random:NormMode=NumEvents:!V" );

	TMVA::MethodBase* method = factory->BookMethod(&dataloader, TMVA::Types::kBDT,"BDT", "BoostType=AdaBoost"); 
	MethodBDT_derived* Mymethod = new MethodBDT_derived("TMVA","BDT",dataloader.GetDataSetInfo(),"BoostType=AdaBoost");
	method = Mymethod;


Thank you for posting your code. I think the problem is caused because the method is create outside the Factory and this is not something supported in TMVA for the time being.
I will investigate and try to find a workaround for you. Probably you need to create your method as the other TMVA methods, using the Factory::BookMethod function, but you need to register it


After creating your Method class (MethodBDT_derived) you need to call this function to correctly initialize and setup the BDT with the correct options.

MethodBDT_derived* Mymethod = new MethodBDT_derived("TMVA","BDT",dataloader.GetDataSetInfo(),"BoostType=AdaBoost");
method = Mymethod;

// setup user BDT class 
// if using output file call SetFile otherwise call SetSilentFile(true) 

After calling these functions, the code seems to work for me


it works.Thank You so much for your help.

Thanks for the update. It was very useful.