# Change in OptimizeTuningParameters

**URL:** <https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528>\
**Category:** TMVA\
**Created:** [November 4, 2021, 10:16am UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528 "2021-11-04T10:16:04Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ali\_dokhani](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ali_dokhani/32/22676_2.png) [@ali\_dokhani](https://root-forum.cern.ch/u/ali_dokhani)\
**Post date:** [November 4, 2021, 10:16am UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/1 "2021-11-04T10:16:04Z")

</div>

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;
   method->OptimizeTuningParameters();

```

Please help me how to fix this.

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 4, 2021, 10:29am UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/2 "2021-11-04T10:29:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ali\_dokhani](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ali_dokhani/32/22676_2.png) [@ali\_dokhani](https://root-forum.cern.ch/u/ali_dokhani)\
**Post date:** [November 4, 2021, 8:07pm UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/3 "2021-11-04T20:07:32Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![couet](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/couet/32/12037_2.png) [@couet](https://root-forum.cern.ch/u/couet)\
**Post date:** [November 5, 2021, 8:16am UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/4 "2021-11-05T08:16:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ali\_dokhani](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ali_dokhani/32/22676_2.png) [@ali\_dokhani](https://root-forum.cern.ch/u/ali_dokhani)\
**Post date:** [November 6, 2021, 7:50am UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/5 "2021-11-06T07:50:23Z")

</div>

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
```

---

<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:** [November 9, 2021, 5:20pm UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/6 "2021-11-09T17:20:14Z")

</div>

Hi,

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

Thanks

Lorenzo

---

<div class="post-metadata">

**Author:** ![ali\_dokhani](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ali_dokhani/32/22676_2.png) [@ali\_dokhani](https://root-forum.cern.ch/u/ali_dokhani)\
**Post date:** [November 11, 2021, 7:01am UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/7 "2021-11-11T07:01:22Z")

</div>

Hi Lorenzo,  
What I did :

```
       	#ifndef MYMethodBDT_derived_H_
        #define MYMethodBDT_derived_H_
class MethodBDT_derived : public TMVA::MethodBDT {
	
	public:
	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;
      (it->second)->Print(oss);
      Log()<<oss.str();
      Log()<<TMVA::Endl;
   }

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

}
#endif

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;
		exit(1);
	}
	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;
		exit(1);
	}

	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.SetBackgroundWeightExpression("Weight_SR1");
	dataloader.AddSignalTree( signalTree);

	dataloader.AddBackgroundTree(backgroundTree);

	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;
	
	method->OptimizeTuningParameters(/*"ROCIntegral","FitGA"*/);
	
}

```

Thanks,  
Ali

---

<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:** [November 11, 2021, 4:52pm UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/8 "2021-11-11T16:52:48Z")

</div>

HI,  
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

Lorenzo

---

<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:** [November 11, 2021, 9:52pm UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/9 "2021-11-11T21:52:20Z")

</div>

Hi,  
After creating your Method class (MethodBDT\_derived) you need to call this function to correctly initialize and setup the BDT with the correct options.

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

// setup user BDT class 
method->SetAnalysisType(TMVA::Types::kClassification);
method->SetupMethod();
method->ParseOptions();
method->ProcessSetup();
// if using output file call SetFile otherwise call SetSilentFile(true) 
//method->SetFile(outputFile);
method->SetSilentFile(true);
method->CheckSetup();

```

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

Cheers  
Lorenzo

---

<div class="post-metadata">

**Author:** ![ali\_dokhani](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/ali_dokhani/32/22676_2.png) [@ali\_dokhani](https://root-forum.cern.ch/u/ali_dokhani)\
**Post date:** [November 12, 2021, 7:20am UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/10 "2021-11-12T07:20:14Z")

</div>

it works.Thank You so much for your help.

---

<div class="post-metadata">

**Author:** ![Michael7988](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/m/71e660/32.png) [@Michael7988](https://root-forum.cern.ch/u/Michael7988)\
**Post date:** [December 2, 2021, 12:13pm UTC](https://root-forum.cern.ch/t/change-in-optimizetuningparameters/47528/11 "2021-12-02T12:13:39Z")

</div>

Thanks for the update. It was very useful.
