Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:57

0001 // @(#)Root/tmva $Id$   
0002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss 
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : Factory                                                               *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  * This template creates ClassifierFactory stores creator functors                *
0012  * to template parameter class. ClassifierFactory is a singelton class            *
0013  * which is explicitly deleted.                                                   *
0014  *                                                                                *
0015  * Authors (alphabetical):                                                        *
0016  *      Joerg Stelzer   <stelzer@cern.ch>        - DESY, Germany                  *
0017  *                                                                                *
0018  * Copyright (c) 2008:                                                            *
0019  *      DESY, Germany                                                             * 
0020  *                                                                                *
0021  * Redistribution and use in source and binary forms, with or without             *
0022  * modification, are permitted according to the terms listed in LICENSE           *
0023  * (see tmva/doc/LICENSE)                                          *
0024  **********************************************************************************/
0025 
0026 #ifndef ROOT_TMVA_ClassifierFactory
0027 #define ROOT_TMVA_ClassifierFactory
0028 
0029 /////////////////////////////////////////////////////////////////
0030 //
0031 // Abstract ClassifierFactory template that handles arbitrary types
0032 // 
0033 // This template creates ClassifierFactory stores creator functors
0034 // to template parameter class. ClassifierFactory is a singelton class
0035 // which is explicitly deleted.
0036 //
0037 // Source: Andrei Alexandrescu, Modern C++ Design
0038 //
0039 /////////////////////////////////////////////////////////////////
0040 
0041 // C++
0042 #include <map> 
0043 #include <string>
0044 #include <vector>
0045 
0046 // Local
0047 #include "TString.h"
0048 
0049 
0050 namespace TMVA {
0051 
0052    class IMethod;
0053    class DataSetInfo;
0054    
0055    class ClassifierFactory {       
0056 
0057    public:
0058       
0059       // typedef for functor that creates object of class IMethod
0060       typedef IMethod* (*Creator)(const TString& job, const TString& title,
0061                                   DataSetInfo& dsi, const TString& option ); 
0062       
0063    public:
0064       
0065       static ClassifierFactory& Instance();
0066       static void               DestroyInstance();
0067       
0068       Bool_t   Register  ( const std::string &name, Creator creator ); 
0069       Bool_t   Unregister( const std::string &name );
0070 
0071       IMethod* Create    ( const std::string &name,
0072                            const TString& job,
0073                            const TString& title,
0074                            DataSetInfo& dsi,
0075                            const TString& option ); 
0076       IMethod* Create    ( const std::string &name,
0077                            DataSetInfo& dsi,
0078                            const TString& weightfile ="" ); 
0079       
0080       const std::vector<std::string> List() const;
0081  
0082       void Print() const;
0083 
0084    private:
0085 
0086       // must use Instance() method to access/create ClassifierFactory
0087       ClassifierFactory() {}
0088       ~ClassifierFactory() {}
0089 
0090       // ClassifierFactory is singleton and can not be copied
0091       // These two methods are private and not defined by design
0092       ClassifierFactory( const ClassifierFactory& );
0093       const ClassifierFactory& operator =(const ClassifierFactory& );
0094       
0095    private: 
0096       
0097       static ClassifierFactory *fgInstance; 
0098       typedef std::map<std::string, Creator> CallMap; 
0099       
0100       CallMap fCalls;       
0101    };
0102 }
0103 
0104 /////////////////////////////////////////////////////////////////
0105 ///
0106 /// for example
0107 ///
0108 /// REGISTER_METHOD(Fisher)
0109 /// 
0110 /// expands to this code:
0111 ///
0112 /// namespace
0113 /// {
0114 ///    TMVA::IMethod* CreateMethod()
0115 ///    {
0116 ///       return (TMVA::IMethod*) new TMVA::MethodFisher;
0117 ///    }
0118 ///    Bool_t RegisteredMethod = TMVA::ClassifierFactory<TMVA::MethodBase>::Instance(). 
0119 ///       Register("Method", CreateMethodFisher);
0120 /// }
0121 ///
0122 /////////////////////////////////////////////////////////////////
0123 
0124 #define REGISTER_METHOD(CLASS)                                          \
0125    namespace                                                            \
0126    {                                                                    \
0127       struct RegisterTMVAMethod {                                       \
0128          static TMVA::IMethod* CreateMethod##CLASS(const TString& job, const TString& title, TMVA::DataSetInfo& dsi, const TString& option) \
0129          {                                                              \
0130             if(job=="" && title=="") {                                  \
0131                return (TMVA::IMethod*) new TMVA::Method##CLASS(dsi, option); \
0132             } else {                                                    \
0133                return (TMVA::IMethod*) new TMVA::Method##CLASS(job, title, dsi, option); \
0134             }                                                           \
0135          }                                                              \
0136          RegisterTMVAMethod() {                                         \
0137             TMVA::ClassifierFactory::Instance(). Register(#CLASS, CreateMethod##CLASS); \
0138             TMVA::Types::Instance().AddTypeMapping(TMVA::Types::k##CLASS, #CLASS); \
0139          }                                                              \
0140       };                                                                \
0141       static RegisterTMVAMethod RegisterTMVAMethod_instance;            \
0142    }
0143 
0144 
0145 #endif
0146 
0147