ROOT  6.06/09
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 #ifndef ROOT_TString
48 #include "TString.h"
49 #endif
50 
51 
52 namespace TMVA {
53 
54  class IMethod;
55  class DataSetInfo;
56 
58 
59  public:
60 
61  // typedef for functor that creates object of class IMethod
62  typedef IMethod* (*Creator)(const TString& job, const TString& title,
63  DataSetInfo& dsi, const TString& option );
64 
65  public:
66 
67  static ClassifierFactory& Instance();
68  static void DestroyInstance();
69 
70  Bool_t Register ( const std::string &name, Creator creator );
71  Bool_t Unregister( const std::string &name );
72 
73  IMethod* Create ( const std::string &name,
74  const TString& job,
75  const TString& title,
76  DataSetInfo& dsi,
77  const TString& option );
78  IMethod* Create ( const std::string &name,
79  DataSetInfo& dsi,
80  const TString& weightfile ="" );
81 
82  const std::vector<std::string> List() const;
83 
84  void Print() const;
85 
86  private:
87 
88  // must use Instance() method to access/create ClassifierFactory
91 
92  // ClassifierFactory is singleton and can not be copied
93  // These two methods are private and not defined by design
96 
97  private:
98 
100  typedef std::map<std::string, Creator> CallMap;
101 
102  CallMap fCalls;
103  };
104 }
105 
106 /////////////////////////////////////////////////////////////////
107 ///
108 /// for example
109 ///
110 /// REGISTER_METHOD(Fisher)
111 ///
112 /// expands to this code:
113 ///
114 /// namespace
115 /// {
116 /// TMVA::IMethod* CreateMethod()
117 /// {
118 /// return (TMVA::IMethod*) new TMVA::MethodFisher;
119 /// }
120 /// Bool_t RegisteredMethod = TMVA::ClassifierFactory<TMVA::MethodBase>::Instance().
121 /// Register("Method", CreateMethodFisher);
122 /// }
123 ///
124 /////////////////////////////////////////////////////////////////
125 
126 #define REGISTER_METHOD(CLASS) \
127  namespace \
128  { \
129  struct RegisterTMVAMethod { \
130  static TMVA::IMethod* CreateMethod##CLASS(const TString& job, const TString& title, TMVA::DataSetInfo& dsi, const TString& option) \
131  { \
132  if(job=="" && title=="") { \
133  return (TMVA::IMethod*) new TMVA::Method##CLASS(dsi, option); \
134  } else { \
135  return (TMVA::IMethod*) new TMVA::Method##CLASS(job, title, dsi, option); \
136  } \
137  } \
138  RegisterTMVAMethod() { \
139  TMVA::ClassifierFactory::Instance(). Register(#CLASS, CreateMethod##CLASS); \
140  TMVA::Types::Instance().AddTypeMapping(TMVA::Types::k##CLASS, #CLASS); \
141  } \
142  }; \
143  static RegisterTMVAMethod RegisterTMVAMethod_instance; \
144  }
145 
146 
147 #endif
148 
149 
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...
static ClassifierFactory & Instance()
access to the ClassifierFactory singleton creates the instance if needed
std::map< std::string, Creator > CallMap
static ClassifierFactory * fgInstance
Initialize static singleton pointer.
Basic string class.
Definition: TString.h:137
bool Bool_t
Definition: RtypesCore.h:59
IMethod *(* Creator)(const TString &job, const TString &title, DataSetInfo &dsi, const TString &option)
const ClassifierFactory & operator=(const ClassifierFactory &)
static void DestroyInstance()
destroy the singleton instance
Bool_t Unregister(const std::string &name)
unregisters a classifier type name
void Print() const
prints the registered method type names
Bool_t Register(const std::string &name, Creator creator)
registers a classifier creator function under the method type name
#define name(a, b)
Definition: linkTestLib0.cpp:5
Abstract ClassifierFactory template that handles arbitrary types.
const std::vector< std::string > List() const
returns a vector of the method type names of registered methods