70 std::string
name =
"OptimizeConfigParameters_";
74 Log() << kFATAL <<
" ERROR: Sorry, Regression is not yet implement for automatic parameter optimization"
75 <<
" --> exit" <<
Endl;
78 Log() << kINFO <<
"Automatic optimisation of tuning parameters in "
81 std::map<TString,TMVA::Interval*>::iterator it;
83 Log() << kINFO << it->first
84 <<
" in range from: " << it->second->GetMin()
85 <<
" to: " << it->second->GetMax()
86 <<
" in : " << it->second->GetNbins() <<
" steps"
97 if(!GetMethod()->IsSilentFile()) GetMethod()->BaseDir()->cd();
106 y[i] = fFOMvsIter[i];
112 h->SetXTitle(
"#iteration "+fOptimizationFitType);
113 h->SetYTitle(fFOMType);
116 if(!GetMethod()->IsSilentFile())
gFOMvsIter->Write();
117 if(!GetMethod()->IsSilentFile())
h->Write();
128 if (fOptimizationFitType ==
"Scan" ) this->optimizeScan();
129 else if (fOptimizationFitType ==
"FitGA" || fOptimizationFitType ==
"Minuit" ) this->optimizeFit();
131 Log() << kFATAL <<
"You have chosen as optimization type " << fOptimizationFitType
132 <<
" that is not (yet) coded --> exit()" <<
Endl;
135 Log() << kINFO <<
"For " << GetMethod()->GetName() <<
" the optimized Parameters are: " <<
Endl;
136 std::map<TString,Double_t>::iterator it;
137 for(it=fTunedParameters.begin(); it!= fTunedParameters.end(); ++it){
138 Log() << kINFO << it->first <<
" = " << it->second <<
Endl;
140 return fTunedParameters;
149 std::vector < int > indices;
150 for (
UInt_t i=0; i< base.size(); i++){
151 indices.push_back(val % base[i] );
152 val =
int( floor(
float(val) /
float(base[i]) ) );
169 std::map<TString,TMVA::Interval*>::iterator it;
174 fTunedParameters.clear();
176 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); ++it){
177 currentParameters.insert(std::pair<TString,Double_t>(it->first,it->second->GetMin()));
178 fTunedParameters.insert(std::pair<TString,Double_t>(it->first,it->second->GetMin()));
185 std::vector< std::vector <Double_t> >
v;
186 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); ++it){
187 std::vector< Double_t > tmp;
188 for (
Int_t k=0; k<it->second->GetNbins(); k++){
189 tmp.push_back(it->second->GetElement(k));
195 for (
UInt_t i=0; i<
v.size(); i++) {
201 for (
int i=0; i<
Ntot; i++){
203 std::vector<int> indices = GetScanIndices(i,
Nindividual );
204 for (it=fTuneParameters.begin(),
index=0;
index< indices.size(); ++
index, ++it){
207 Log() << kINFO <<
"--------------------------" <<
Endl;
208 Log() << kINFO <<
"Settings being evaluated:" <<
Endl;
214 GetMethod()->Reset();
217 if(!GetMethod()->IsSilentFile()) GetMethod()->BaseDir()->cd();
218 if (i==0) GetMethod()->GetTransformationHandler().CalcTransformations(
219 GetMethod()->Data()->GetEventCollection());
221 GetMethod()->Train();
230 fTunedParameters[iter->first]=iter->second;
235 GetMethod()->Reset();
236 GetMethod()->SetTuneParameters(fTunedParameters);
244 std::vector<TMVA::Interval*> ranges;
245 std::map<TString, TMVA::Interval*>::iterator it;
246 std::vector<Double_t> pars;
248 for (it=fTuneParameters.begin(); it != fTuneParameters.end(); ++it){
250 pars.push_back( (it->second)->GetMean() );
256 GetMethod()->GetTransformationHandler().CalcTransformations(GetMethod()->Data()->GetEventCollection());
262 if ( fOptimizationFitType ==
"Minuit" ) {
263 TString opt=
"FitStrategy=0:UseImprove=False:UseMinos=False:Tolerance=100";
267 "FitterMinuit_BDTOptimize",
269 }
else if ( fOptimizationFitType ==
"FitGA" ) {
270 TString opt=
"PopSize=20:Steps=30:Cycles=3:ConvCrit=0.01:SaveBestCycle=5";
272 "FitterGA_BDTOptimize",
275 Log() << kWARNING <<
" you did not specify a valid OptimizationFitType "
276 <<
" will use the default (FitGA) " <<
Endl;
277 TString opt=
"PopSize=20:Steps=30:Cycles=3:ConvCrit=0.01:SaveBestCycle=5";
279 "FitterGA_BDTOptimize",
289 for (
UInt_t ipar=0; ipar<ranges.size(); ipar++)
delete ranges[ipar];
291 GetMethod()->Reset();
293 fTunedParameters.clear();
295 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); ++it){
296 fTunedParameters.insert(std::pair<TString,Double_t>(it->first,pars[
jcount++]));
299 GetMethod()->SetTuneParameters(fTunedParameters);
308 std::map< std::vector<Double_t> ,
Double_t>::const_iterator iter;
309 iter = fAlreadyTrainedParCombination.find(pars);
311 if (iter != fAlreadyTrainedParCombination.end()) {
320 std::map<TString, TMVA::Interval*>::iterator it;
321 for (it=fTuneParameters.begin(); it!=fTuneParameters.end(); ++it){
324 GetMethod()->Reset();
326 if(!GetMethod()->IsSilentFile()) GetMethod()->BaseDir()->cd();
329 GetMethod()->GetTransformationHandler().
330 CalcTransformations(GetMethod()->Data()->GetEventCollection());
334 GetMethod()->Train();
340 fAlreadyTrainedParCombination.insert(std::make_pair(pars,-
currentFOM));
360 Log() << kFATAL <<
" ERROR, " <<
percent <<
" in " << fFOMType
361 <<
" is not a valid floating point number" <<
Endl;
367 if (fMethod->DoRegression()){
368 std::cout <<
" ERROR: Sorry, Regression is not yet implement for automatic parameter optimisation"
369 <<
" --> exit" << std::endl;
372 if (fFOMType ==
"Separation")
fom = GetSeparation();
373 else if (fFOMType ==
"ROCIntegral")
fom = GetROCIntegral();
374 else if (fFOMType.BeginsWith(
"SigEffAtBkgEff0"))
fom = GetSigEffAtBkgEff(
parsePercent(fFOMType));
375 else if (fFOMType.BeginsWith(
"BkgRejAtSigEff0"))
fom = GetBkgRejAtSigEff(
parsePercent(fFOMType));
376 else if (fFOMType.BeginsWith(
"BkgEffAtSigEff0"))
fom = GetBkgEffAtSigEff(
parsePercent(fFOMType));
378 Log()<< kFATAL <<
" ERROR, you've specified as Figure of Merit in the "
379 <<
" parameter optimisation " << fFOMType <<
" which has not"
380 <<
" been implemented yet!! ---> exit " <<
Endl;
384 fFOMvsIter.push_back(
fom);
394 if (fMvaSig) fMvaSig->Delete();
395 if (fMvaBkg) fMvaBkg->Delete();
396 if (fMvaSigFineBin) fMvaSigFineBin->Delete();
397 if (fMvaBkgFineBin) fMvaBkgFineBin->Delete();
405 fMvaSig =
new TH1D(
"fMvaSig",
"",100,-1.5,1.5);
406 fMvaBkg =
new TH1D(
"fMvaBkg",
"",100,-1.5,1.5);
407 fMvaSigFineBin =
new TH1D(
"fMvaSigFineBin",
"",100000,-1.5,1.5);
408 fMvaBkgFineBin =
new TH1D(
"fMvaBkgFineBin",
"",100000,-1.5,1.5);
410 const std::vector< Event*> events=fMethod->Data()->GetEventCollection(
Types::kTesting);
421 fMvaSig->Fill(fMethod->GetMvaValue(events[
iev]),events[
iev]->GetWeight());
422 fMvaSigFineBin->Fill(fMethod->GetMvaValue(events[
iev]),events[
iev]->GetWeight());
424 fMvaBkg->Fill(fMethod->GetMvaValue(events[
iev]),events[
iev]->GetWeight());
425 fMvaBkgFineBin->Fill(fMethod->GetMvaValue(events[
iev]),events[
iev]->GetWeight());
441 std::cout <<
"Separation calculation via histograms (not PDFs) seems to give still strange results!! Don't do that, check!!"<<std::endl;
473 integral += (1-
pdfB->GetIntegral(cut,
xmax)) *
pdfS->GetVal(cut);
479 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
480 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
481 std::cout <<
" Error in OptimizeConfigParameters GetROCIntegral, unequal histograms for sig and bkg.." << std::endl;
486 Int_t nbins = fMvaSigFineBin->GetNbinsX();
514 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
515 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
516 std::cout <<
" Error in OptimizeConfigParameters GetSigEffAt, unequal histograms for sig and bkg.." << std::endl;
522 Int_t nbins=fMvaBkgFineBin->GetNbinsX();
551 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
552 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
553 std::cout <<
" Error in OptimizeConfigParameters GetBkgEffAt, unequal histograms for sig and bkg.." << std::endl;
560 Int_t nbins=fMvaBkgFineBin->GetNbinsX();
588 if ( (fMvaSigFineBin->GetXaxis()->GetXmin() != fMvaBkgFineBin->GetXaxis()->GetXmin()) ||
589 (fMvaSigFineBin->GetNbinsX() != fMvaBkgFineBin->GetNbinsX()) ){
590 std::cout <<
" Error in OptimizeConfigParameters GetBkgEffAt, unequal histograms for sig and bkg.." << std::endl;
597 Int_t nbins=fMvaBkgFineBin->GetNbinsX();
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Signed integer 4 bytes (int)
float Float_t
Float 4 bytes (float)
double Double_t
Double 8 bytes.
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
Option_t Option_t TPoint TPoint percent
const_iterator begin() const
const_iterator end() const
A TGraph is an object made of two arrays X and Y with npoints each.
1-D histogram with a double per channel (see TH1 documentation)
2-D histogram with a double per channel (see TH1 documentation)
void CheckForUnusedOptions() const
checks for unused options in option string
static void SetIsTraining(Bool_t)
when this static function is called, it sets the flag whether events with negative event weight shoul...
Base class for TMVA fitters.
Double_t Run()
estimator function interface for fitting
Fitter using a Genetic Algorithm.
The TMVA::Interval Class.
Virtual base Class for all MVA method.
const char * GetName() const override
Bool_t DoRegression() const
ostringstream derivative to redirect and format output
std::vector< int > GetScanIndices(int val, std::vector< int > base)
helper function to scan through the all the combinations in the parameter space
MsgLogger * fLogger
! message logger
Double_t GetBkgRejAtSigEff(Double_t sigEff=0.5)
calculate the background rejection for a given signal efficiency
virtual ~OptimizeConfigParameters()
the destructor (delete the OptimizeConfigParameters, store the graph and .. delete it)
Double_t GetBkgEffAtSigEff(Double_t sigEff=0.5)
calculate the background efficiency for a given signal efficiency
void optimizeScan()
do the actual optimization using a simple scan method, i.e.
OptimizeConfigParameters(MethodBase *const method, std::map< TString, TMVA::Interval * > tuneParameters, TString fomType="Separation", TString optimizationType="GA")
Constructor which sets either "Classification or Regression".
Double_t EstimatorFunction(std::vector< Double_t > &) override
return the estimator (from current FOM) for the fitting interface
std::map< TString, TMVA::Interval * > fTuneParameters
parameters included in the tuning
MethodBase *const fMethod
The MVA method to be evaluated.
TString fOptimizationFitType
which type of optimisation procedure to be used
std::map< TString, Double_t > optimize()
Double_t GetSeparation()
return the separation between the signal and background MVA ouput distribution
TString fFOMType
the FOM type (Separation, ROC integra.. whatever you implemented..
Double_t GetFOM()
Return the Figure of Merit (FOM) used in the parameter optimization process.
Double_t GetSigEffAtBkgEff(Double_t bkgEff=0.1)
calculate the signal efficiency for a given background efficiency
Double_t GetROCIntegral()
calculate the area (integral) under the ROC curve as a overall quality measure of the classification
void GetMVADists()
fill the private histograms with the mva distributions for sig/bkg
PDF wrapper for histograms; uses user-defined spline interpolation.
MsgLogger & Endl(MsgLogger &ml)
Short_t Max(Short_t a, Short_t b)
Returns the largest of a and b.
Short_t Min(Short_t a, Short_t b)
Returns the smallest of a and b.