Logo ROOT  
Reference Guide
ClassifierFactory.h
Go to the documentation of this file.
1 // @(#)Root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : Factory *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * This template creates ClassifierFactory stores creator functors *
12  * to template parameter class. ClassifierFactory is a singelton class *
13  * which is explicitly deleted. *
14  * *
15  * Authors (alphabetical): *
16  * Joerg Stelzer <stelzer@cern.ch> - DESY, Germany *
17  * *
18  * Copyright (c) 2008: *
19  * DESY, Germany *
20  * *
21  * Redistribution and use in source and binary forms, with or without *
22  * modification, are permitted according to the terms listed in LICENSE *
23  * (http://tmva.sourceforge.net/LICENSE) *
24  **********************************************************************************/
25 
26 #ifndef ROOT_TMVA_ClassifierFactory
27 #define ROOT_TMVA_ClassifierFactory
28 
29 /////////////////////////////////////////////////////////////////
30 //
31 // Abstract ClassifierFactory template that handles arbitrary types
32 //
33 // This template creates ClassifierFactory stores creator functors
34 // to template parameter class. ClassifierFactory is a singelton class
35 // which is explicitly deleted.
36 //
37 // Source: Andrei Alexandrescu, Modern C++ Design
38 //
39 /////////////////////////////////////////////////////////////////
40 
41 // C++
42 #include <map>
43 #include <string>
44 #include <vector>
45 
46 // Local
47 #include "TString.h"
48 
49 
50 namespace TMVA {
51 
52  class IMethod;
53  class DataSetInfo;
54 
56 
57  public:
58 
59  // typedef for functor that creates object of class IMethod
60  typedef IMethod* (*Creator)(const TString& job, const TString& title,
61  DataSetInfo& dsi, const TString& option );
62 
63  public:
64 
65  static ClassifierFactory& Instance();
66  static void DestroyInstance();
67 
68  Bool_t Register ( const std::string &name, Creator creator );
69  Bool_t Unregister( const std::string &name );
70 
71  IMethod* Create ( const std::string &name,
72  const TString& job,
73  const TString& title,
74  DataSetInfo& dsi,
75  const TString& option );
76  IMethod* Create ( const std::string &name,
77  DataSetInfo& dsi,
78  const TString& weightfile ="" );
79 
80  const std::vector<std::string> List() const;
81 
82  void Print() const;
83 
84  private:
85 
86  // must use Instance() method to access/create ClassifierFactory
89 
90  // ClassifierFactory is singleton and can not be copied
91  // These two methods are private and not defined by design
94 
95  private:
96 
98  typedef std::map<std::string, Creator> CallMap;
99 
101  };
102 }
103 
104 /////////////////////////////////////////////////////////////////
105 ///
106 /// for example
107 ///
108 /// REGISTER_METHOD(Fisher)
109 ///
110 /// expands to this code:
111 ///
112 /// namespace
113 /// {
114 /// TMVA::IMethod* CreateMethod()
115 /// {
116 /// return (TMVA::IMethod*) new TMVA::MethodFisher;
117 /// }
118 /// Bool_t RegisteredMethod = TMVA::ClassifierFactory<TMVA::MethodBase>::Instance().
119 /// Register("Method", CreateMethodFisher);
120 /// }
121 ///
122 /////////////////////////////////////////////////////////////////
123 
124 #define REGISTER_METHOD(CLASS) \
125  namespace \
126  { \
127  struct RegisterTMVAMethod { \
128  static TMVA::IMethod* CreateMethod##CLASS(const TString& job, const TString& title, TMVA::DataSetInfo& dsi, const TString& option) \
129  { \
130  if(job=="" && title=="") { \
131  return (TMVA::IMethod*) new TMVA::Method##CLASS(dsi, option); \
132  } else { \
133  return (TMVA::IMethod*) new TMVA::Method##CLASS(job, title, dsi, option); \
134  } \
135  } \
136  RegisterTMVAMethod() { \
137  TMVA::ClassifierFactory::Instance(). Register(#CLASS, CreateMethod##CLASS); \
138  TMVA::Types::Instance().AddTypeMapping(TMVA::Types::k##CLASS, #CLASS); \
139  } \
140  }; \
141  static RegisterTMVAMethod RegisterTMVAMethod_instance; \
142  }
143 
144 
145 #endif
146 
147 
TMVA::ClassifierFactory::operator=
const ClassifierFactory & operator=(const ClassifierFactory &)
TMVA::ClassifierFactory::CallMap
std::map< std::string, Creator > CallMap
Definition: ClassifierFactory.h:98
TMVA::ClassifierFactory::DestroyInstance
static void DestroyInstance()
destroy the singleton instance
Definition: ClassifierFactory.cxx:58
TMVA::ClassifierFactory::ClassifierFactory
ClassifierFactory(const ClassifierFactory &)
TMVA::ClassifierFactory::Print
void Print() const
prints the registered method type names
Definition: ClassifierFactory.cxx:147
TString
Basic string class.
Definition: TString.h:136
TString.h
bool
TMVA::ClassifierFactory::~ClassifierFactory
~ClassifierFactory()
Definition: ClassifierFactory.h:88
TMVA::ClassifierFactory::Instance
static ClassifierFactory & Instance()
access to the ClassifierFactory singleton creates the instance if needed
Definition: ClassifierFactory.cxx:48
TMVA::DataSetInfo
Class that contains all the data information.
Definition: DataSetInfo.h:62
TMVA::ClassifierFactory::fgInstance
static ClassifierFactory * fgInstance
Initialize static singleton pointer.
Definition: ClassifierFactory.h:97
TMVA::ClassifierFactory::Register
Bool_t Register(const std::string &name, Creator creator)
registers a classifier creator function under the method type name
Definition: ClassifierFactory.cxx:66
TMVA::ClassifierFactory::Creator
IMethod *(* Creator)(const TString &job, const TString &title, DataSetInfo &dsi, const TString &option)
Definition: ClassifierFactory.h:60
TMVA::IMethod
Interface for all concrete MVA method implementations.
Definition: IMethod.h:53
TMVA::ClassifierFactory
This is the MVA factory.
Definition: ClassifierFactory.h:55
TMVA::ClassifierFactory::Unregister
Bool_t Unregister(const std::string &name)
unregisters a classifier type name
Definition: ClassifierFactory.cxx:80
name
char name[80]
Definition: TGX11.cxx:110
TMVA::ClassifierFactory::fCalls
CallMap fCalls
Definition: ClassifierFactory.h:100
TMVA::ClassifierFactory::List
const std::vector< std::string > List() const
returns a vector of the method type names of registered methods
Definition: ClassifierFactory.cxx:134
TMVA::ClassifierFactory::Create
IMethod * Create(const std::string &name, const TString &job, const TString &title, DataSetInfo &dsi, const TString &option)
creates the method if needed based on the method name using the creator function the factory has stor...
Definition: ClassifierFactory.cxx:89
TMVA
create variable transformations
Definition: GeneticMinimizer.h:22
TMVA::ClassifierFactory::ClassifierFactory
ClassifierFactory()
Definition: ClassifierFactory.h:87