ROOT  6.06/09
Reference Guide
ClassifierFactory.cxx
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  * Implementation (see header for description) *
12  * *
13  * Authors (alphabetical): *
14  * Joerg Stelzer <stelzer@cern.ch> - DESY, Germany *
15  * *
16  * Copyright (c) 2008: *
17  * DESY, Germany *
18  * *
19  * Redistribution and use in source and binary forms, with or without *
20  * modification, are permitted according to the terms listed in LICENSE *
21  * (http://tmva.sourceforge.net/LICENSE) *
22  **********************************************************************************/
23 
24 //_______________________________________________________________________
25 //
26 // This is the MVA factory
27 //_______________________________________________________________________
28 
29 
30 #include "TMVA/ClassifierFactory.h"
31 #include <assert.h>
32 #include <iostream>
33 
34 ///
35 /// Initialize static singleton pointer
36 ///
37 TMVA::ClassifierFactory* TMVA::ClassifierFactory::fgInstance = 0;
38 
39 ////////////////////////////////////////////////////////////////////////////////
40 /// access to the ClassifierFactory singleton
41 /// creates the instance if needed
42 
44 {
46 
47  return *fgInstance;
48 }
49 
50 ////////////////////////////////////////////////////////////////////////////////
51 /// destroy the singleton instance
52 
54 {
55  if (fgInstance!=0) delete fgInstance;
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// registers a classifier creator function under the method type name
60 
61 Bool_t TMVA::ClassifierFactory::Register( const std::string &name, Creator creator )
62 {
63  if(fCalls.find(name) != fCalls.end())
64  {
65  std::cerr << "ClassifierFactory<>::Register - " << name << " already exists" << std::endl;
66  return false;
67  }
68 
69  return fCalls.insert(CallMap::value_type(name, creator)).second;
70 }
71 
72 ////////////////////////////////////////////////////////////////////////////////
73 /// unregisters a classifier type name
74 
76 {
77  return fCalls.erase(name) == 1;
78 }
79 
80 ////////////////////////////////////////////////////////////////////////////////
81 /// creates the method if needed based on the method name using the
82 /// creator function the factory has stored
83 
85  const TString& job,
86  const TString& title,
87  DataSetInfo& dsi,
88  const TString& option )
89 {
90  // additional options are passed to the creator function (the
91  // method constructor)
92 
93  CallMap::const_iterator it = fCalls.find(name);
94 
95  // handle unknown algorithm request
96  if (it == fCalls.end()) {
97  std::cerr << "ClassifierFactory<>::Create - don't know anything about " << name << std::endl;
98  assert(0);
99  }
100 
101  return (it->second)(job, title, dsi, option);
102 }
103 
104 ////////////////////////////////////////////////////////////////////////////////
105 /// creates the method if needed based on the method name using the
106 /// creator function the factory has stored
107 
109  DataSetInfo& dsi,
110  const TString& weightfile )
111 {
112  // additional options are passed to the creator function (the
113  // second method constructor)
114 
115  CallMap::const_iterator it = fCalls.find(name);
116 
117  // handle unknown algorithm request
118  if (it == fCalls.end()) {
119  std::cerr << "ClassifierFactory<>::Create - don't know anything about " << name << std::endl;
120  assert(0);
121  }
122 
123  return (it->second)("", "", dsi, weightfile);
124 }
125 
126 ////////////////////////////////////////////////////////////////////////////////
127 /// returns a vector of the method type names of registered methods
128 
129 const std::vector<std::string> TMVA::ClassifierFactory::List() const
130 {
131  std::vector<std::string> svec;
132 
133  CallMap::const_iterator it = fCalls.begin();
134  for (; it != fCalls.end(); ++it) svec.push_back(it -> first);
135 
136  return svec;
137 }
138 
139 ////////////////////////////////////////////////////////////////////////////////
140 /// prints the registered method type names
141 
143 {
144  std::cout << "Print: ClassifierFactory<> knows about " << fCalls.size() << " objects" << std::endl;
145 
146  CallMap::const_iterator it = fCalls.begin();
147  for (; it != fCalls.end(); ++it) std::cout << "Registered object name " << it -> first << std::endl;
148 }
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
#define assert(cond)
Definition: unittest.h:542
static ClassifierFactory * fgInstance
Initialize static singleton pointer.
Basic string class.
Definition: TString.h:137
bool Bool_t
Definition: RtypesCore.h:59
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
const std::vector< std::string > List() const
returns a vector of the method type names of registered methods