58 fTuneParameters(tuneParameters),
60 fOptimizationFitType(optimizationFitType),
67 std::string
name =
"OptimizeConfigParameters_";
70 if (fMethod->DoRegression()){
71 Log() <<
kFATAL <<
" ERROR: Sorry, Regression is not yet implement for automatic parameter optimization"
72 <<
" --> exit" <<
Endl;
75 Log() <<
kINFO <<
"Automatic optimisation of tuning parameters in "
78 std::map<TString,TMVA::Interval*>::iterator it;
79 for (it=fTuneParameters.begin(); it!=fTuneParameters.end();it++) {
81 <<
" in range from: " << it->second->GetMin()
82 <<
" to: " << it->second->GetMax()
83 <<
" in : " << it->second->GetNbins() <<
" steps"
86 Log() <<
kINFO <<
" using the options: " << fFOMType <<
" and " << fOptimizationFitType <<
Endl;
107 if (ymin>y[i]) ymin=y[i];
108 if (ymax<y[i]) ymax=y[i];
127 if (fOptimizationFitType ==
"Scan" ) this->optimizeScan();
128 else if (fOptimizationFitType ==
"FitGA" || fOptimizationFitType ==
"Minuit" ) this->optimizeFit();
130 Log() <<
kFATAL <<
"You have chosen as optimization type " << fOptimizationFitType
131 <<
" that is not (yet) coded --> exit()" <<
Endl;
135 std::map<TString,Double_t>::iterator it;
136 for(it=fTunedParameters.begin(); it!= fTunedParameters.end(); it++){
137 Log() <<
kINFO << it->first <<
" = " << it->second <<
Endl;
139 return fTunedParameters;
148 std::vector < int > indices;
149 for (
UInt_t i=0; i< base.size(); i++){
150 indices.push_back(val % base[i] );
151 val = int(
floor(
float(val) /
float(base[i]) ) );
165 Double_t bestFOM=-1000000, currentFOM;
167 std::map<TString,Double_t> currentParameters;
168 std::map<TString,TMVA::Interval*>::iterator it;
172 currentParameters.clear();
173 fTunedParameters.clear();
175 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
176 currentParameters.insert(std::pair<TString,Double_t>(it->first,it->second->GetMin()));
177 fTunedParameters.insert(std::pair<TString,Double_t>(it->first,it->second->GetMin()));
184 std::vector< std::vector <Double_t> >
v;
185 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
186 std::vector< Double_t > tmp;
187 for (
Int_t k=0; k<it->second->GetNbins(); k++){
188 tmp.push_back(it->second->GetElement(k));
193 std::vector< int > Nindividual;
194 for (
UInt_t i=0; i<v.size(); i++) {
196 Nindividual.push_back(v[i].size());
200 for (
int i=0; i<Ntot; i++){
202 std::vector<int> indices = GetScanIndices(i, Nindividual );
203 for (it=fTuneParameters.begin(), index=0; index< indices.size(); index++, it++){
204 currentParameters[it->first] = v[index][indices[index]];
208 for (std::map<TString,Double_t>::iterator it_print=currentParameters.begin();
209 it_print!=currentParameters.end(); it_print++){
210 Log() <<
kINFO <<
" " << it_print->first <<
" = " << it_print->second <<
Endl;
214 GetMethod()->SetTuneParameters(currentParameters);
217 if (i==0)
GetMethod()->GetTransformationHandler().CalcTransformations(
222 currentFOM = GetFOM();
223 Log() <<
kINFO <<
"FOM was found : " << currentFOM <<
"; current best is " << bestFOM <<
Endl;
225 if (currentFOM > bestFOM) {
226 bestFOM = currentFOM;
227 for (std::map<TString,Double_t>::iterator iter=currentParameters.begin();
228 iter != currentParameters.end(); iter++){
229 fTunedParameters[iter->first]=iter->second;
235 GetMethod()->SetTuneParameters(fTunedParameters);
241 std::vector<TMVA::Interval*> ranges;
242 std::map<TString, TMVA::Interval*>::iterator it;
243 std::vector<Double_t> pars;
245 for (it=fTuneParameters.begin(); it != fTuneParameters.end(); it++){
247 pars.push_back( (it->second)->GetMean() );
259 if ( fOptimizationFitType ==
"Minuit" ) {
262 "FitterMinuit_BDTOptimize",
264 }
else if ( fOptimizationFitType ==
"FitGA" ) {
265 TString opt=
"PopSize=20:Steps=30:Cycles=3:ConvCrit=0.01:SaveBestCycle=5";
267 "FitterGA_BDTOptimize",
270 Log() <<
kWARNING <<
" you did not specify a valid OptimizationFitType "
271 <<
" will use the default (FitGA) " <<
Endl;
272 TString opt=
"PopSize=20:Steps=30:Cycles=3:ConvCrit=0.01:SaveBestCycle=5";
274 "FitterGA_BDTOptimize",
284 for (
UInt_t ipar=0; ipar<ranges.size(); ipar++)
delete ranges[ipar];
289 fTunedParameters.clear();
291 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
292 fTunedParameters.insert(std::pair<TString,Double_t>(it->first,pars[jcount++]));
295 GetMethod()->SetTuneParameters(fTunedParameters);
304 std::map< std::vector<Double_t> ,
Double_t>::const_iterator iter;
305 iter = fAlreadyTrainedParCombination.find(pars);
307 if (iter != fAlreadyTrainedParCombination.end()) {
313 std::map<TString,Double_t> currentParameters;
316 std::map<TString, TMVA::Interval*>::iterator it;
317 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); it++){
318 currentParameters[it->first] = pars[icount++];
321 GetMethod()->SetTuneParameters(currentParameters);
326 CalcTransformations(
GetMethod()->
Data()->GetEventCollection());
336 fAlreadyTrainedParCombination.insert(std::make_pair(pars,-currentFOM));
348 if (fMethod->DoRegression()){
349 std::cout <<
" ERROR: Sorry, Regression is not yet implement for automatic parameter optimisation"
350 <<
" --> exit" << std::endl;
353 if (fFOMType ==
"Separation") fom = GetSeparation();
354 else if (fFOMType ==
"ROCIntegral") fom = GetROCIntegral();
355 else if (fFOMType ==
"SigEffAtBkgEff01") fom = GetSigEffAtBkgEff(0.1);
356 else if (fFOMType ==
"SigEffAtBkgEff001") fom = GetSigEffAtBkgEff(0.01);
357 else if (fFOMType ==
"SigEffAtBkgEff002") fom = GetSigEffAtBkgEff(0.02);
358 else if (fFOMType ==
"BkgRejAtSigEff05") fom = GetBkgRejAtSigEff(0.5);
359 else if (fFOMType ==
"BkgEffAtSigEff05") fom = GetBkgEffAtSigEff(0.5);
361 Log()<<
kFATAL <<
" ERROR, you've specified as Figure of Merit in the "
362 <<
" parameter optimisation " << fFOMType <<
" which has not"
363 <<
" been implemented yet!! ---> exit " <<
Endl;
366 fFOMvsIter.push_back(fom);
376 if (fMvaSig) fMvaSig->Delete();
377 if (fMvaBkg) fMvaBkg->Delete();
378 if (fMvaSigFineBin) fMvaSigFineBin->Delete();
379 if (fMvaBkgFineBin) fMvaBkgFineBin->Delete();
387 fMvaSig =
new TH1D(
"fMvaSig",
"",100,-1.5,1.5);
388 fMvaBkg =
new TH1D(
"fMvaBkg",
"",100,-1.5,1.5);
389 fMvaSigFineBin =
new TH1D(
"fMvaSigFineBin",
"",100000,-1.5,1.5);
390 fMvaBkgFineBin =
new TH1D(
"fMvaBkgFineBin",
"",100000,-1.5,1.5);
392 const std::vector< Event*> events=fMethod->Data()->GetEventCollection(
Types::kTesting);
394 UInt_t signalClassNr = fMethod->DataInfo().GetClassInfo(
"Signal")->GetNumber();
398 for (
UInt_t iev=0; iev < events.size() ; iev++){
402 if (events[iev]->
GetClass() == signalClassNr) {
403 fMvaSig->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
404 fMvaSigFineBin->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
406 fMvaBkg->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
407 fMvaBkgFineBin->Fill(fMethod->GetMvaValue(events[iev]),events[iev]->GetWeight());
423 std::cout <<
"Separation caclulcaton via histograms (not PDFs) seems to give still strange results!! Don't do that, check!!"<<std::endl;
455 for (
UInt_t i=0; i<nsteps; i++){
462 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
463 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
464 std::cout <<
" Error in OptimizeConfigParameters GetROCIntegral, unequal histograms for sig and bkg.." << std::endl;
468 Double_t *cumulator = fMvaBkgFineBin->GetIntegral();
475 sigIntegral += fMvaSigFineBin->GetBinContent(ibin) * fMvaSigFineBin->GetBinWidth(ibin);
480 integral += (cumulator[ibin]) * fMvaSigFineBin->GetBinContent(ibin)/sigIntegral * fMvaSigFineBin->GetBinWidth(ibin) ;
498 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
499 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
500 std::cout <<
" Error in OptimizeConfigParameters GetSigEffAt, unequal histograms for sig and bkg.." << std::endl;
503 Double_t *bkgCumulator = fMvaBkgFineBin->GetIntegral();
504 Double_t *sigCumulator = fMvaSigFineBin->GetIntegral();
515 while (bkgCumulator[nbins-ibin] > (1-bkgEff)) {
516 sigEff = sigCumulator[
nbins]-sigCumulator[nbins-ibin];
534 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
535 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
536 std::cout <<
" Error in OptimizeConfigParameters GetBkgEffAt, unequal histograms for sig and bkg.." << std::endl;
540 Double_t *bkgCumulator = fMvaBkgFineBin->GetIntegral();
541 Double_t *sigCumulator = fMvaSigFineBin->GetIntegral();
552 while ( sigCumulator[nbins]-sigCumulator[nbins-ibin] < sigEff) {
553 bkgEff = bkgCumulator[
nbins]-bkgCumulator[nbins-ibin];
570 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
571 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
572 std::cout <<
" Error in OptimizeConfigParameters GetBkgEffAt, unequal histograms for sig and bkg.." << std::endl;
576 Double_t *bkgCumulator = fMvaBkgFineBin->GetIntegral();
577 Double_t *sigCumulator = fMvaSigFineBin->GetIntegral();
588 while ( sigCumulator[nbins]-sigCumulator[nbins-ibin] < sigEff) {
589 bkgRej = bkgCumulator[nbins-ibin];
virtual Int_t Write(const char *name=0, Int_t option=0, Int_t bufsize=0)
Write this object to the current directory.
std::string GetName(const std::string &scope_name)
MsgLogger & Endl(MsgLogger &ml)
void CheckForUnusedOptions() const
checks for unused options in option string
virtual void SetName(const char *name)
Set the name of the TNamed.
Double_t GetSeparation()
return the searation between the signal and background MVA ouput distribution
void optimizeScan()
do the actual optimization using a simple scan method, i.e.
Short_t Min(Short_t a, Short_t b)
virtual void SetYTitle(const char *title)
Double_t GetFOM()
Return the Figure of Merit (FOM) used in the parameter optimization process.
static void SetIsTraining(Bool_t)
when this static function is called, it sets the flag whether events with negative event weight shoul...
void GetMVADists()
fill the private histograms with the mva distributinos for sig/bkg
Double_t Run()
estimator function interface for fitting
std::vector< Float_t > fFOMvsIter
std::vector< std::vector< double > > Data
TCppMethod_t GetMethod(TCppScope_t scope, TCppIndex_t imeth)
Double_t GetROCIntegral()
calculate the area (integral) under the ROC curve as a overall quality measure of the classification ...
std::map< TString, Double_t > optimize()
virtual ~OptimizeConfigParameters()
the destructor (delete the OptimizeConfigParameters, store the graph and .. delete it) ...
Double_t GetSigEffAtBkgEff(Double_t bkgEff=0.1)
calculate the signal efficiency for a given background efficiency
Double_t GetVal(Double_t x) const
returns value PDF(x)
TDirectory * BaseDir() const
returns the ROOT directory where info/histograms etc of the corresponding MVA method instance are sto...
Double_t GetBkgEffAtSigEff(Double_t sigEff=0.5)
calculate the background efficiency for a given signal efficiency
THist< 2, double, THistStatContent, THistStatUncertainty > TH2D
virtual void SetXTitle(const char *title)
virtual Bool_t cd(const char *path=0)
Change current directory to "this" directory.
std::vector< int > GetScanIndices(int val, std::vector< int > base)
helper function to scan through the all the combinations in the parameter space
Double_t GetBkgRejAtSigEff(Double_t sigEff=0.5)
calculate the background rejection for a given signal efficiency
Short_t Max(Short_t a, Short_t b)
A Graph is a graphics object made of two arrays X and Y with npoints each.
Double_t EstimatorFunction(std::vector< Double_t > &)
return the estimator (from current FOM) for the fitting interface
THist< 1, double, THistStatContent, THistStatUncertainty > TH1D
Double_t GetIntegral(Double_t xmin, Double_t xmax)
computes PDF integral within given ranges
tomato 2-D histogram with a double per channel (see TH1 documentation)}
TString fOptimizationFitType