Belle II Software  release-05-01-25
TMVA.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2016 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Keck *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #pragma once
12 #ifndef INCLUDE_GUARD_BELLE2_MVA_TMVA_HEADER
13 #define INCLUDE_GUARD_BELLE2_MVA_TMVA_HEADER
14 
15 #include <mva/interface/Options.h>
16 #include <mva/interface/Teacher.h>
17 #include <mva/interface/Expert.h>
18 
19 #include <TMVA/Factory.h>
20 #include <TMVA/Tools.h>
21 #include <TMVA/Reader.h>
22 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,8,0)
23 #include <TMVA/DataLoader.h>
24 #endif
25 
26 #include <memory>
27 
28 namespace Belle2 {
33  namespace MVA {
34 
38  class TMVAOptions : public SpecificOptions {
39 
40  public:
45  virtual void load(const boost::property_tree::ptree& pt) override;
46 
51  virtual void save(boost::property_tree::ptree& pt) const override;
52 
56  virtual po::options_description getDescription() override;
57 
61  virtual std::string getMethod() const override { return "TMVA"; }
62 
63  public:
64  std::string m_method = "BDT";
65  std::string m_type = "BDT";
70  std::string m_config =
71  "!H:!V:CreateMVAPdfs:NTrees=400:BoostType=Grad:Shrinkage=0.1:UseBaggedBoost:BaggedSampleFraction=0.5:nCuts=1024:MaxDepth=3:IgnoreNegWeightsInTraining";
72  //std::string method = "FastBDT";
73  //std::string type = "Plugins";
74  //std::string config = "!H:!V:CreateMVAPdfs:NTrees=400:Shrinkage=0.10:RandRatio=0.5:NCutLevel=8:NTreeLayers=3";
75  std::string m_factoryOption = "!V:!Silent:Color:DrawProgressBar";
76  std::string m_prepareOption = "SplitMode=random:!V";
77  std::string m_workingDirectory = "";
78  std::string m_prefix = "TMVA";
79  };
80 
85 
86  public:
92  {
93  m_factoryOption += ":AnalysisType=Classification";
94  }
95 
100  virtual void load(const boost::property_tree::ptree& pt) override;
101 
106  virtual void save(boost::property_tree::ptree& pt) const override;
107 
111  virtual po::options_description getDescription() override;
112 
116  virtual std::string getMethod() const override { return "TMVAClassification"; }
117 
118  public:
119  bool transform2probability = true;
120  };
121 
122 
127 
128  public:
134  {
135  m_factoryOption += ":AnalysisType=Multiclass";
136  }
137 
142  virtual void load(const boost::property_tree::ptree& pt) override;
143 
148  virtual void save(boost::property_tree::ptree& pt) const override;
149 
153  virtual po::options_description getDescription() override;
154 
158  virtual std::string getMethod() const override { return "TMVAMulticlass"; }
159 
160  public:
161 
162  std::vector<std::string> m_classes;
164  };
165 
166 
171 
172  public:
178  {
179  m_factoryOption += ":AnalysisType=Regression";
180  }
181 
185  virtual std::string getMethod() const override { return "TMVARegression"; }
186  };
187 
188 
192  class TMVATeacher : public Teacher {
193 
194  public:
200  TMVATeacher(const GeneralOptions& general_options, const TMVAOptions& _specific_options);
201 
202 #if ROOT_VERSION_CODE >= ROOT_VERSION(6,8,0)
203 
209  Weightfile trainFactory(TMVA::Factory& factory, TMVA::DataLoader& data_loader, std::string& jobName) const;
210 #else
211 
216  Weightfile trainFactory(TMVA::Factory& factory, std::string& jobName) const;
217 #endif
218 
219  private:
222  };
223 
228 
229  public:
235  TMVATeacherClassification(const GeneralOptions& general_options, const TMVAOptionsClassification& _specific_options);
236 
241  virtual Weightfile train(Dataset& training_data) const override;
242 
243  protected:
245  };
246 
251 
252  public:
258  TMVATeacherMulticlass(const GeneralOptions& general_options, const TMVAOptionsMulticlass& _specific_options);
259 
264  virtual Weightfile train(Dataset& training_data) const override;
265 
266  protected:
268  };
269 
274 
275  public:
281  TMVATeacherRegression(const GeneralOptions& general_options, const TMVAOptionsRegression& _specific_options);
282 
287  virtual Weightfile train(Dataset& training_data) const override;
288 
289  protected:
291  };
292 
293 
297  class TMVAExpert : public MVA::Expert {
298 
299  public:
304  virtual void load(Weightfile& weightfile) override;
305 
306  protected:
307  std::unique_ptr<TMVA::Reader> m_expert;
308  mutable std::vector<float>
310  mutable std::vector<float>
312  };
313 
318 
319  public:
324  virtual void load(Weightfile& weightfile) override;
325 
330  virtual std::vector<float> apply(Dataset& test_data) const override;
331 
332  protected:
336  };
337 
342 
343  public:
348  virtual void load(Weightfile& weightfile) override;
349 
354  virtual std::vector<float> apply(Dataset& test_data) const override
355  {
356  (void) test_data;
357  return std::vector<float>();
358  };
359 
366  virtual std::vector<float> apply(Dataset& test_data, const unsigned int classID) const override;
367 
368  protected:
371  };
372 
377 
378  public:
383  virtual void load(Weightfile& weightfile) override;
384 
389  virtual std::vector<float> apply(Dataset& test_data) const override;
390 
391  protected:
394  };
395 
396  }
398 }
399 #endif
Belle2::MVA::TMVAExpertClassification::apply
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this m_expert onto a dataset.
Definition: TMVA.cc:568
Belle2::MVA::TMVAOptions::m_config
std::string m_config
TMVA config string for the chosen method.
Definition: TMVA.h:70
Belle2::MVA::TMVAOptions::m_prepareOption
std::string m_prepareOption
Prepare options passed to prepareTrainingAndTestTree method.
Definition: TMVA.h:76
Belle2::MVA::TMVAOptionsClassification::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: TMVA.cc:79
Belle2::MVA::TMVATeacherClassification::train
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition: TMVA.cc:229
Belle2::MVA::TMVAExpertClassification
Expert for the TMVA Classification MVA method.
Definition: TMVA.h:317
Belle2::MVA::TMVATeacherClassification
Teacher for the TMVA Classification MVA method.
Definition: TMVA.h:227
Belle2::MVA::TMVAOptionsClassification::save
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: TMVA.cc:85
Belle2::MVA::TMVATeacherRegression::TMVATeacherRegression
TMVATeacherRegression(const GeneralOptions &general_options, const TMVAOptionsRegression &_specific_options)
Constructs a new teacher using the GeneralOptions and specific options of this training.
Definition: TMVA.cc:346
Belle2::MVA::TMVAOptionsMulticlass::getDescription
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: TMVA.cc:120
Belle2::MVA::TMVAOptionsMulticlass::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: TMVA.cc:99
Belle2::MVA::TMVAOptionsMulticlass::m_classes
std::vector< std::string > m_classes
Class name identifiers.
Definition: TMVA.h:162
Belle2::MVA::Dataset
Abstract base class of all Datasets given to the MVA interface The current event can always be access...
Definition: Dataset.h:34
Belle2::MVA::TMVAOptionsClassification
Options for the TMVA Classification MVA method.
Definition: TMVA.h:84
Belle2::MVA::TMVATeacherMulticlass
Teacher for the TMVA Multiclass MVA method.
Definition: TMVA.h:250
Belle2::MVA::Weightfile
The Weightfile class serializes all information about a training into an xml tree.
Definition: Weightfile.h:40
Belle2::MVA::TMVAOptionsRegression::getMethod
virtual std::string getMethod() const override
Return method name.
Definition: TMVA.h:185
Belle2::MVA::TMVATeacher::specific_options
TMVAOptions specific_options
Method specific options.
Definition: TMVA.h:220
Belle2::MVA::TMVAOptionsClassification::transform2probability
bool transform2probability
Transform output of method to a probability.
Definition: TMVA.h:119
Belle2::MVA::TMVAOptions::getDescription
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: TMVA.cc:65
Belle2::MVA::TMVAExpertClassification::load
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: TMVA.cc:472
Belle2::MVA::TMVAExpertMulticlass::specific_options
TMVAOptionsMulticlass specific_options
Method specific options.
Definition: TMVA.h:369
Belle2::MVA::TMVAOptionsClassification::getMethod
virtual std::string getMethod() const override
Return method name.
Definition: TMVA.h:116
Belle2::MVA::TMVAExpert::m_spectators_cache
std::vector< float > m_spectators_cache
Spectators Cache for TMVA::Reader: Otherwise we would have to set the branch addresses in each apply ...
Definition: TMVA.h:311
Belle2::MVA::TMVAOptionsMulticlass::TMVAOptionsMulticlass
TMVAOptionsMulticlass()
Constructor Adds Multiclass as AnalysisType to the factoryOptions.
Definition: TMVA.h:133
Belle2::MVA::SpecificOptions
Specific Options, all mehtod Options have to inherit from this class.
Definition: Options.h:99
Belle2::MVA::TMVATeacherRegression::train
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition: TMVA.cc:350
Belle2::MVA::TMVATeacher::TMVATeacher
TMVATeacher(const GeneralOptions &general_options, const TMVAOptions &_specific_options)
Constructs a new teacher using the GeneralOptions and specific options of this training.
Definition: TMVA.cc:129
Belle2::MVA::TMVAOptions::m_prefix
std::string m_prefix
Prefix used for all files generated by TMVA.
Definition: TMVA.h:78
Belle2::MVA::TMVATeacherRegression
Teacher for the TMVA Regression MVA method.
Definition: TMVA.h:273
Belle2::MVA::TMVATeacherMulticlass::specific_options
TMVAOptionsMulticlass specific_options
Method specific options.
Definition: TMVA.h:267
Belle2::MVA::TMVAOptionsRegression::TMVAOptionsRegression
TMVAOptionsRegression()
Constructor Adds REgression as AnalysisType to the factoryOptions.
Definition: TMVA.h:177
Belle2::MVA::TMVAOptions::save
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: TMVA.cc:53
Belle2::MVA::TMVAExpert
Expert for the TMVA MVA method.
Definition: TMVA.h:297
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::MVA::TMVAExpertMulticlass::apply
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this m_expert onto a dataset.
Definition: TMVA.h:354
Belle2::MVA::Teacher
Abstract base class of all Teachers Each MVA library has its own implementation of this class,...
Definition: Teacher.h:31
Belle2::MVA::TMVATeacherRegression::specific_options
TMVAOptionsRegression specific_options
Method specific options.
Definition: TMVA.h:290
Belle2::MVA::TMVAExpertClassification::expert_signalFraction
float expert_signalFraction
Signal fraction used to calculate the probability.
Definition: TMVA.h:334
Belle2::MVA::TMVAOptionsMulticlass
Options for the TMVA Multiclass MVA method.
Definition: TMVA.h:126
Belle2::MVA::TMVAExpert::load
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: TMVA.cc:446
Belle2::MVA::Expert
Abstract base class of all Expert Each MVA library has its own implementation of this class,...
Definition: Expert.h:33
Belle2::MVA::GeneralOptions
General options which are shared by all MVA trainings.
Definition: Options.h:64
Belle2::MVA::TMVAExpertRegression::specific_options
TMVAOptionsRegression specific_options
Method specific options.
Definition: TMVA.h:392
Belle2::MVA::TMVAOptions::m_method
std::string m_method
tmva method name
Definition: TMVA.h:64
Belle2::MVA::TMVATeacherClassification::specific_options
TMVAOptionsClassification specific_options
Method specific options.
Definition: TMVA.h:244
Belle2::MVA::TMVAOptions::getMethod
virtual std::string getMethod() const override
Return method name.
Definition: TMVA.h:61
Belle2::MVA::TMVAOptions::m_type
std::string m_type
tmva method type
Definition: TMVA.h:65
Belle2::MVA::TMVAExpertMulticlass
Expert for the TMVA Multiclass MVA method.
Definition: TMVA.h:341
Belle2::MVA::TMVATeacherMulticlass::TMVATeacherMulticlass
TMVATeacherMulticlass(const GeneralOptions &general_options, const TMVAOptionsMulticlass &_specific_options)
Constructs a new teacher using the GeneralOptions and specific options of this training.
Definition: TMVA.cc:335
Belle2::MVA::TMVATeacher::trainFactory
Weightfile trainFactory(TMVA::Factory &factory, TMVA::DataLoader &data_loader, std::string &jobName) const
Train a mva method using the given data loader returning a Weightfile.
Definition: TMVA.cc:133
Belle2::MVA::TMVAOptionsClassification::TMVAOptionsClassification
TMVAOptionsClassification()
Constructor Adds Classification as AnalysisType to the factoryOptions.
Definition: TMVA.h:91
Belle2::MVA::TMVAOptionsClassification::getDescription
virtual po::options_description getDescription() override
Returns a program options description for all available options.
Definition: TMVA.cc:91
Belle2::MVA::TMVATeacherClassification::TMVATeacherClassification
TMVATeacherClassification(const GeneralOptions &general_options, const TMVAOptionsClassification &_specific_options)
Constructs a new teacher using the GeneralOptions and specific options of this training.
Definition: TMVA.cc:225
Belle2::MVA::TMVAOptionsMulticlass::save
virtual void save(boost::property_tree::ptree &pt) const override
Save mechanism to store Options in a xml tree.
Definition: TMVA.cc:110
Belle2::MVA::TMVAExpertRegression
Expert for the TMVA Regression MVA method.
Definition: TMVA.h:376
Belle2::MVA::TMVAExpertClassification::specific_options
TMVAOptionsClassification specific_options
Method specific options.
Definition: TMVA.h:333
Belle2::MVA::TMVAOptions::m_factoryOption
std::string m_factoryOption
Factory options passed to tmva factory.
Definition: TMVA.h:75
Belle2::MVA::TMVAOptionsRegression
Options for the TMVA Regression MVA method.
Definition: TMVA.h:170
Belle2::MVA::TMVAExpert::m_input_cache
std::vector< float > m_input_cache
Input Cache for TMVA::Reader: Otherwise we would have to set the branch addresses in each apply call.
Definition: TMVA.h:309
Belle2::MVA::TMVAExpert::m_expert
std::unique_ptr< TMVA::Reader > m_expert
TMVA::Reader pointer.
Definition: TMVA.h:307
Belle2::MVA::TMVAOptionsMulticlass::getMethod
virtual std::string getMethod() const override
Return method name.
Definition: TMVA.h:158
Belle2::MVA::TMVAExpertRegression::load
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: TMVA.cc:537
Belle2::MVA::TMVAExpertMulticlass::load
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: TMVA.cc:506
Belle2::MVA::TMVAOptions::m_workingDirectory
std::string m_workingDirectory
Working directory of TMVA, if empty a temporary directory is used.
Definition: TMVA.h:77
Belle2::MVA::TMVAExpertRegression::apply
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this m_expert onto a dataset.
Definition: TMVA.cc:602
Belle2::MVA::TMVATeacherMulticlass::train
virtual Weightfile train(Dataset &training_data) const override
Train a mva method using the given dataset returning a Weightfile.
Definition: TMVA.cc:340
Belle2::MVA::TMVATeacher
Teacher for the TMVA MVA method.
Definition: TMVA.h:192
Belle2::MVA::TMVAOptions
Options for the TMVA MVA method.
Definition: TMVA.h:38
Belle2::MVA::TMVAOptions::load
virtual void load(const boost::property_tree::ptree &pt) override
Load mechanism to load Options from a xml tree.
Definition: TMVA.cc:37