Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-15 10:28:59

0001 // @(#)root/tmva $Id$
0002 // Author: Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne, Jan Therhaag
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : MethodBase                                                            *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  *      Virtual base class for all MVA method                                     *
0012  *                                                                                *
0013  * Authors (alphabetical):                                                        *
0014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
0015  *      Peter Speckmayer <peter.speckmayer@cern.ch>  - CERN, Switzerland          *
0016  *      Joerg Stelzer   <Joerg.Stelzer@cern.ch>  - CERN, Switzerland              *
0017  *      Jan Therhaag          <Jan.Therhaag@cern.ch>   - U of Bonn, Germany       *
0018  *      Eckhard v. Toerne     <evt@uni-bonn.de>        - U of Bonn, Germany       *
0019  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
0020  *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
0021  *                                                                                *
0022  * Copyright (c) 2005-2011:                                                       *
0023  *      CERN, Switzerland                                                         *
0024  *      U. of Victoria, Canada                                                    *
0025  *      MPI-K Heidelberg, Germany                                                 *
0026  *      U. of Bonn, Germany                                                       *
0027  *                                                                                *
0028  * Redistribution and use in source and binary forms, with or without             *
0029  * modification, are permitted according to the terms listed in LICENSE           *
0030  * (see tmva/doc/LICENSE)                                          *
0031  **********************************************************************************/
0032 
0033 #ifndef ROOT_TMVA_MethodBase
0034 #define ROOT_TMVA_MethodBase
0035 
0036 //////////////////////////////////////////////////////////////////////////
0037 //                                                                      //
0038 // MethodBase                                                           //
0039 //                                                                      //
0040 // Virtual base class for all TMVA method                               //
0041 //                                                                      //
0042 //////////////////////////////////////////////////////////////////////////
0043 
0044 #include <iosfwd>
0045 #include <vector>
0046 #include <map>
0047 #include "assert.h"
0048 
0049 #include "TString.h"
0050 
0051 #include "TMVA/IMethod.h"
0052 #include "TMVA/Configurable.h"
0053 #include "TMVA/Types.h"
0054 #include "TMVA/DataSet.h"
0055 #include "TMVA/Event.h"
0056 #include "TMVA/TransformationHandler.h"
0057 #include <TMVA/Results.h>
0058 #include "TMVA/TrainingHistory.h"
0059 
0060 #include <TFile.h>
0061 
0062 class TGraph;
0063 class TTree;
0064 class TDirectory;
0065 class TSpline;
0066 class TH1F;
0067 class TH1D;
0068 class TMultiGraph;
0069 
0070 /*! \class TMVA::IPythonInteractive
0071 \ingroup TMVA
0072 
0073 This class is needed by JsMVA, and it's a helper class for tracking errors during
0074 the training in Jupyter notebook. It’s only initialized in Jupyter notebook context.
0075 In initialization we specify some title, and a TGraph will be created for every title.
0076 We can add new data points easily to all TGraphs. These graphs are added to a
0077 TMultiGraph, and during an interactive training we get this TMultiGraph object
0078 and plot it with JsROOT.
0079 */
0080 
0081 namespace TMVA {
0082 
0083    class Ranking;
0084    class PDF;
0085    class TSpline1;
0086    class MethodCuts;
0087    class MethodBoost;
0088    class DataSetInfo;
0089    namespace Experimental {
0090    class Classification;
0091    }
0092    class TrainingHistory;
0093 
0094    class IPythonInteractive {
0095    public:
0096        IPythonInteractive();
0097        ~IPythonInteractive();
0098        void Init(std::vector<TString>& graphTitles);
0099        void ClearGraphs();
0100        void AddPoint(Double_t x, Double_t y1, Double_t y2);
0101        void AddPoint(std::vector<Double_t>& dat);
0102        inline TMultiGraph* Get() {return fMultiGraph;}
0103        inline bool NotInitialized(){ return fNumGraphs==0;};
0104    private:
0105        TMultiGraph* fMultiGraph;
0106        std::vector<TGraph*> fGraphs;
0107        Int_t fNumGraphs;
0108        Int_t fIndex;
0109    };
0110 
0111    class MethodBase : virtual public IMethod, public Configurable {
0112 
0113       friend class CrossValidation;
0114       friend class Factory;
0115       friend class RootFinder;
0116       friend class MethodBoost;
0117       friend class MethodCrossValidation;
0118       friend class Experimental::Classification;
0119 
0120    public:
0121 
0122       enum EWeightFileType { kROOT=0, kTEXT };
0123 
0124       // default constructor
0125       MethodBase( const TString& jobName,
0126                   Types::EMVA methodType,
0127                   const TString& methodTitle,
0128                   DataSetInfo& dsi,
0129                   const TString& theOption = "" );
0130 
0131       // constructor used for Testing + Application of the MVA, only (no training),
0132       // using given weight file
0133       MethodBase( Types::EMVA methodType,
0134                   DataSetInfo& dsi,
0135                   const TString& weightFile );
0136 
0137       // default destructor
0138       virtual ~MethodBase();
0139 
0140       // declaration, processing and checking of configuration options
0141       void             SetupMethod();
0142       void             ProcessSetup();
0143       virtual void     CheckSetup(); // may be overwritten by derived classes
0144 
0145       // ---------- main training and testing methods ------------------------------
0146 
0147       // prepare tree branch with the method's discriminating variable
0148       void             AddOutput( Types::ETreeType type, Types::EAnalysisType analysisType );
0149 
0150       // performs classifier training
0151       // calls methods Train() implemented by derived classes
0152       void             TrainMethod();
0153 
0154       // optimize tuning parameters
0155       virtual std::map<TString,Double_t> OptimizeTuningParameters(TString fomType="ROCIntegral", TString fitType="FitGA");
0156       virtual void SetTuneParameters(std::map<TString,Double_t> tuneParameters);
0157 
0158       void             Train() override = 0;
0159 
0160       // store and retrieve time used for training
0161       void             SetTrainTime( Double_t trainTime ) { fTrainTime = trainTime; }
0162       Double_t         GetTrainTime() const { return fTrainTime; }
0163 
0164       // store and retrieve time used for testing
0165       void             SetTestTime ( Double_t testTime ) { fTestTime = testTime; }
0166       Double_t         GetTestTime () const { return fTestTime; }
0167 
0168       // performs classifier testing
0169       virtual void     TestClassification();
0170       virtual Double_t GetKSTrainingVsTest(Char_t SorB, TString opt="X");
0171 
0172       // performs multiclass classifier testing
0173       virtual void     TestMulticlass();
0174 
0175       // performs regression testing
0176       virtual void     TestRegression( Double_t& bias, Double_t& biasT,
0177                                        Double_t& dev,  Double_t& devT,
0178                                        Double_t& rms,  Double_t& rmsT,
0179                                        Double_t& mInf, Double_t& mInfT, // mutual information
0180                                        Double_t& corr,
0181                                        Types::ETreeType type );
0182 
0183       // options treatment
0184       void     Init()           override = 0;
0185       void     DeclareOptions() override = 0;
0186       void     ProcessOptions() override = 0;
0187       virtual void     DeclareCompatibilityOptions(); // declaration of past options
0188 
0189       // reset the Method --> As if it was not yet trained, just instantiated
0190       //      virtual void     Reset()          = 0;
0191       //for the moment, I provide a dummy (that would not work) default, just to make
0192       // compilation/running w/o parameter optimisation still possible
0193       virtual void     Reset(){return;}
0194 
0195       // classifier response:
0196       // some methods may return a per-event error estimate
0197       // error calculation is skipped if err==0
0198       Double_t GetMvaValue( Double_t* errLower = nullptr, Double_t* errUpper = nullptr) override = 0;
0199 
0200       // signal/background classification response
0201       Double_t GetMvaValue( const TMVA::Event* const ev, Double_t* err = nullptr, Double_t* errUpper = nullptr );
0202 
0203    protected:
0204       // helper function to set errors to -1
0205       void NoErrorCalc(Double_t* const err, Double_t* const errUpper);
0206 
0207       // signal/background classification response for all current set of data
0208       virtual std::vector<Double_t> GetMvaValues(Long64_t firstEvt = 0, Long64_t lastEvt = -1, Bool_t logProgress = false);
0209       // same as above but using a provided data set (used by MethodCategory)
0210       virtual std::vector<Double_t> GetDataMvaValues(DataSet *data = nullptr, Long64_t firstEvt = 0, Long64_t lastEvt = -1, Bool_t logProgress = false);
0211 
0212    public:
0213       // regression response
0214       const std::vector<Float_t>& GetRegressionValues(const TMVA::Event* const ev){
0215          fTmpEvent = ev;
0216          const std::vector<Float_t>* ptr = &GetRegressionValues();
0217          fTmpEvent = nullptr;
0218          return (*ptr);
0219       }
0220 
0221       virtual const std::vector<Float_t>& GetRegressionValues() {
0222          std::vector<Float_t>* ptr = new std::vector<Float_t>(0);
0223          return (*ptr);
0224       }
0225 
0226       // multiclass classification response
0227       virtual const std::vector<Float_t>& GetMulticlassValues() {
0228          std::vector<Float_t>* ptr = new std::vector<Float_t>(0);
0229          return (*ptr);
0230       }
0231 
0232       // Training history
0233       virtual const std::vector<Float_t>& GetTrainingHistory(const char* /*name*/ ) {
0234          std::vector<Float_t>* ptr = new std::vector<Float_t>(0);
0235          return (*ptr);
0236       }
0237 
0238       // probability of classifier response (mvaval) to be signal (requires "CreateMvaPdf" option set)
0239       virtual Double_t GetProba( const Event *ev); // the simple one, automatically calculates the mvaVal and uses the SAME sig/bkg ratio as given in the training sample (typically 50/50 .. (NormMode=EqualNumEvents) but can be different)
0240       virtual Double_t GetProba( Double_t mvaVal, Double_t ap_sig );
0241 
0242       // Rarity of classifier response (signal or background (default) is uniform in [0,1])
0243       virtual Double_t GetRarity( Double_t mvaVal, Types::ESBType reftype = Types::kBackground ) const;
0244 
0245       // create ranking
0246       const Ranking* CreateRanking() override = 0;
0247 
0248       // make ROOT-independent C++ class
0249       void     MakeClass( const TString& classFileName = TString("") ) const override;
0250 
0251       // print help message
0252       void             PrintHelpMessage() const override;
0253 
0254       //
0255       // streamer methods for training information (creates "weight" files) --------
0256       //
0257    public:
0258       void WriteStateToFile     () const;
0259       void ReadStateFromFile    ();
0260 
0261    protected:
0262       // the actual "weights"
0263       virtual void AddWeightsXMLTo      ( void* parent ) const = 0;
0264       virtual void ReadWeightsFromXML   ( void* wghtnode ) = 0;
0265       void ReadWeightsFromStream( std::istream& ) override = 0;       // backward compatibility
0266       virtual void ReadWeightsFromStream( TFile& ) {}                // backward compatibility
0267 
0268    private:
0269       friend class MethodCategory;
0270       friend class MethodCompositeBase;
0271       void WriteStateToXML      ( void* parent ) const;
0272       void ReadStateFromXML     ( void* parent );
0273       void WriteStateToStream   ( std::ostream& tf ) const;   // needed for MakeClass
0274       void WriteVarsToStream    ( std::ostream& tf, const TString& prefix = "" ) const;  // needed for MakeClass
0275 
0276 
0277    public: // these two need to be public, they are used to read in-memory weight-files
0278       void ReadStateFromStream  ( std::istream& tf );         // backward compatibility
0279       void ReadStateFromStream  ( TFile&        rf );         // backward compatibility
0280       void ReadStateFromXMLString( const char* xmlstr );      // for reading from memory
0281 
0282    private:
0283       // the variable information
0284       void AddVarsXMLTo         ( void* parent  ) const;
0285       void AddSpectatorsXMLTo   ( void* parent  ) const;
0286       void AddTargetsXMLTo      ( void* parent  ) const;
0287       void AddClassesXMLTo      ( void* parent  ) const;
0288       void ReadVariablesFromXML ( void* varnode );
0289       void ReadSpectatorsFromXML( void* specnode);
0290       void ReadTargetsFromXML   ( void* tarnode );
0291       void ReadClassesFromXML   ( void* clsnode );
0292       void ReadVarsFromStream   ( std::istream& istr );       // backward compatibility
0293 
0294    public:
0295       // ---------------------------------------------------------------------------
0296 
0297       // write evaluation histograms into target file
0298       virtual void     WriteEvaluationHistosToFile(Types::ETreeType treetype);
0299 
0300       // write classifier-specific monitoring information to target file
0301       void     WriteMonitoringHistosToFile() const override;
0302 
0303       // ---------- public evaluation methods --------------------------------------
0304 
0305       // individual initialization for testing of each method
0306       // overload this one for individual initialisation of the testing,
0307       // it is then called automatically within the global "TestInit"
0308 
0309       // variables (and private member functions) for the Evaluation:
0310       // get the efficiency. It fills a histogram for efficiency/vs/bkg
0311       // and returns the one value fo the efficiency demanded for
0312       // in the TString argument. (Watch the string format)
0313       virtual Double_t GetEfficiency( const TString&, Types::ETreeType, Double_t& err );
0314       virtual Double_t GetTrainingEfficiency(const TString& );
0315       virtual std::vector<Float_t> GetMulticlassEfficiency( std::vector<std::vector<Float_t> >& purity );
0316       virtual std::vector<Float_t> GetMulticlassTrainingEfficiency(std::vector<std::vector<Float_t> >& purity );
0317       virtual TMatrixD GetMulticlassConfusionMatrix(Double_t effB, Types::ETreeType type);
0318       virtual Double_t GetSignificance() const;
0319       virtual Double_t GetROCIntegral(TH1D *histS, TH1D *histB) const;
0320       virtual Double_t GetROCIntegral(PDF *pdfS=nullptr, PDF *pdfB=nullptr) const;
0321       virtual Double_t GetMaximumSignificance( Double_t SignalEvents, Double_t BackgroundEvents,
0322                                                Double_t& optimal_significance_value  ) const;
0323       virtual Double_t GetSeparation( TH1*, TH1* ) const;
0324       virtual Double_t GetSeparation( PDF* pdfS = nullptr, PDF* pdfB = nullptr ) const;
0325 
0326       virtual void GetRegressionDeviation(UInt_t tgtNum, Types::ETreeType type, Double_t& stddev,Double_t& stddev90Percent ) const;
0327       // ---------- public accessors -----------------------------------------------
0328 
0329       // classifier naming (a lot of names ... aren't they ;-)
0330       const TString&   GetJobName       () const { return fJobName; }
0331       const TString&   GetMethodName    () const { return fMethodName; }
0332       TString          GetMethodTypeName() const { return Types::Instance().GetMethodName(fMethodType); }
0333       Types::EMVA      GetMethodType    () const { return fMethodType; }
0334       const char*      GetName          () const override { return fMethodName.Data(); }
0335       const TString&   GetTestvarName   () const { return fTestvar; }
0336       const TString    GetProbaName     () const { return fTestvar + "_Proba"; }
0337       TString          GetWeightFileName() const;
0338 
0339       // build classifier name in Test tree
0340       // MVA prefix (e.g., "TMVA_")
0341       void             SetTestvarName  ( const TString & v="" ) { fTestvar = (v=="") ? ("MVA_" + GetMethodName()) : v; }
0342 
0343       // number of input variable used by classifier
0344       UInt_t           GetNvar()       const { return DataInfo().GetNVariables(); }
0345       UInt_t           GetNVariables() const { return DataInfo().GetNVariables(); }
0346       UInt_t           GetNTargets()   const { return DataInfo().GetNTargets(); };
0347 
0348       // internal names and expressions of input variables
0349       const TString&   GetInputVar  ( Int_t i ) const { return DataInfo().GetVariableInfo(i).GetInternalName(); }
0350       const TString&   GetInputLabel( Int_t i ) const { return DataInfo().GetVariableInfo(i).GetLabel(); }
0351       const char *     GetInputTitle( Int_t i ) const { return DataInfo().GetVariableInfo(i).GetTitle(); }
0352 
0353       // normalisation and limit accessors
0354       Double_t         GetMean( Int_t ivar ) const { return GetTransformationHandler().GetMean(ivar); }
0355       Double_t         GetRMS ( Int_t ivar ) const { return GetTransformationHandler().GetRMS(ivar); }
0356       Double_t         GetXmin( Int_t ivar ) const { return GetTransformationHandler().GetMin(ivar); }
0357       Double_t         GetXmax( Int_t ivar ) const { return GetTransformationHandler().GetMax(ivar); }
0358 
0359       // sets the minimum requirement on the MVA output to declare an event signal-like
0360       Double_t         GetSignalReferenceCut() const { return fSignalReferenceCut; }
0361       Double_t         GetSignalReferenceCutOrientation() const { return fSignalReferenceCutOrientation; }
0362 
0363       // sets the minimum requirement on the MVA output to declare an event signal-like
0364       void             SetSignalReferenceCut( Double_t cut ) { fSignalReferenceCut = cut; }
0365       void             SetSignalReferenceCutOrientation( Double_t cutOrientation ) { fSignalReferenceCutOrientation = cutOrientation; }
0366 
0367       // pointers to ROOT directories
0368       TDirectory*      BaseDir()       const;
0369       TDirectory*      MethodBaseDir() const;
0370       TFile*           GetFile() const {return fFile;}
0371 
0372       void             SetMethodDir ( TDirectory* methodDir ) { fBaseDir = fMethodBaseDir  = methodDir; }
0373       void             SetBaseDir( TDirectory* methodDir ){ fBaseDir = methodDir; }
0374       void             SetMethodBaseDir( TDirectory* methodDir ){ fMethodBaseDir = methodDir; }
0375       void             SetFile(TFile* file){fFile=file;}
0376 
0377       //Silent file
0378       void SetSilentFile(Bool_t status) {fSilentFile=status;}
0379       Bool_t IsSilentFile() const {return fSilentFile;}
0380 
0381       //Model Persistence
0382       void SetModelPersistence(Bool_t status){fModelPersistence=status;}//added support to create/remove dir here if exits or not
0383       Bool_t IsModelPersistence() const {return fModelPersistence;}
0384 
0385       // the TMVA version can be obtained and checked using
0386       //    if (GetTrainingTMVAVersionCode()>TMVA_VERSION(3,7,2)) {...}
0387       // or
0388       //    if (GetTrainingROOTVersionCode()>ROOT_VERSION(5,15,5)) {...}
0389       UInt_t           GetTrainingTMVAVersionCode()   const { return fTMVATrainingVersion; }
0390       UInt_t           GetTrainingROOTVersionCode()   const { return fROOTTrainingVersion; }
0391       TString          GetTrainingTMVAVersionString() const;
0392       TString          GetTrainingROOTVersionString() const;
0393 
0394       TransformationHandler&        GetTransformationHandler(Bool_t takeReroutedIfAvailable=true)
0395          {
0396             if(fTransformationPointer && takeReroutedIfAvailable) return *fTransformationPointer; else return fTransformation;
0397          }
0398       const TransformationHandler&  GetTransformationHandler(Bool_t takeReroutedIfAvailable=true) const
0399       {
0400          if(fTransformationPointer && takeReroutedIfAvailable) return *fTransformationPointer; else return fTransformation;
0401       }
0402 
0403       void             RerouteTransformationHandler (TransformationHandler* fTargetTransformation) { fTransformationPointer=fTargetTransformation; }
0404 
0405       // ---------- event accessors ------------------------------------------------
0406 
0407       // returns reference to data set
0408       // NOTE: this DataSet is the "original" dataset, i.e. the one seen by ALL Classifiers WITHOUT transformation
0409       DataSet* Data() const { return (fTmpData) ? fTmpData : DataInfo().GetDataSet(); }
0410       DataSetInfo&     DataInfo() const { return fDataSetInfo; }
0411 
0412 
0413       // event reference and update
0414       // NOTE: these Event accessors make sure that you get the events transformed according to the
0415       //        particular classifiers transformation chosen
0416       UInt_t           GetNEvents      () const { return Data()->GetNEvents(); }
0417       const Event*     GetEvent        () const;
0418       const Event*     GetEvent        ( const TMVA::Event* ev ) const;
0419       const Event*     GetEvent        ( Long64_t ievt ) const;
0420       const Event*     GetEvent        ( Long64_t ievt , Types::ETreeType type ) const;
0421       const Event*     GetTrainingEvent( Long64_t ievt ) const;
0422       const Event*     GetTestingEvent ( Long64_t ievt ) const;
0423       const std::vector<TMVA::Event*>& GetEventCollection( Types::ETreeType type );
0424 
0425       TrainingHistory  fTrainHistory;
0426       // ---------- public auxiliary methods ---------------------------------------
0427 
0428       // this method is used to decide whether an event is signal- or background-like
0429       // the reference cut "xC" is taken to be where
0430       // Int_[-oo,xC] { PDF_S(x) dx } = Int_[xC,+oo] { PDF_B(x) dx }
0431       virtual Bool_t        IsSignalLike();
0432       virtual Bool_t        IsSignalLike(Double_t mvaVal);
0433 
0434 
0435       Bool_t                HasMVAPdfs() const { return fHasMVAPdfs; }
0436       virtual void          SetAnalysisType( Types::EAnalysisType type ) { fAnalysisType = type; }
0437       Types::EAnalysisType  GetAnalysisType() const { return fAnalysisType; }
0438       Bool_t                DoRegression() const { return fAnalysisType == Types::kRegression; }
0439       Bool_t                DoMulticlass() const { return fAnalysisType == Types::kMulticlass; }
0440 
0441       // setter method for suppressing writing to XML and writing of standalone classes
0442       void                  DisableWriting(Bool_t setter){ fModelPersistence = setter?kFALSE:kTRUE; }//DEPRECATED
0443 
0444     protected:
0445       mutable const Event *fTmpEvent; //! temporary event when testing on a different DataSet than the own one
0446       DataSet *fTmpData =  nullptr; //! temporary dataset used when evaluating on a different data (used by MethodCategory::GetMvaValues)
0447        // helper variables for JsMVA
0448        IPythonInteractive *fInteractive = nullptr;
0449        bool fExitFromTraining = false;
0450        UInt_t fIPyMaxIter = 0, fIPyCurrentIter = 0;
0451 
0452     public:
0453 
0454       // initializing IPythonInteractive class (for JsMVA only)
0455       inline void InitIPythonInteractive(){
0456         if (fInteractive) delete fInteractive;
0457         fInteractive = new IPythonInteractive();
0458       }
0459 
0460       // get training errors (for JsMVA only)
0461       inline TMultiGraph* GetInteractiveTrainingError(){return fInteractive->Get();}
0462 
0463       // stop's the training process (for JsMVA only)
0464       inline void ExitFromTraining(){
0465         fExitFromTraining = true;
0466       }
0467 
0468       // check's if the training ended (for JsMVA only)
0469       inline bool TrainingEnded(){
0470         if (fExitFromTraining && fInteractive){
0471           delete fInteractive;
0472           fInteractive = nullptr;
0473         }
0474         return fExitFromTraining;
0475       }
0476 
0477       // get fIPyMaxIter
0478       inline UInt_t GetMaxIter(){ return fIPyMaxIter; }
0479 
0480       // get fIPyCurrentIter
0481       inline UInt_t GetCurrentIter(){ return fIPyCurrentIter; }
0482 
0483    protected:
0484 
0485       // ---------- protected accessors -------------------------------------------
0486 
0487       //TDirectory*  LocalTDir() const { return Data().LocalRootDir(); }
0488 
0489       // weight file name and directory (given by global config variable)
0490       void             SetWeightFileName( TString );
0491 
0492       const TString&   GetWeightFileDir() const { return fFileDir; }
0493       void             SetWeightFileDir( TString fileDir );
0494 
0495       // are input variables normalised ?
0496       Bool_t           IsNormalised() const { return fNormalise; }
0497       void             SetNormalised( Bool_t norm ) { fNormalise = norm; }
0498 
0499       // set number of input variables (only used by MethodCuts, could perhaps be removed)
0500       //      void SetNvar( Int_t n ) { fNvar = n; }
0501 
0502       // verbose and help flags
0503       Bool_t           Verbose() const { return fVerbose; }
0504       Bool_t           Help   () const { return fHelp; }
0505 
0506       // ---------- protected event and tree accessors -----------------------------
0507 
0508       // names of input variables (if the original names are expressions, they are
0509       // transformed into regexps)
0510       const TString&   GetInternalVarName( Int_t ivar ) const { return (*fInputVars)[ivar]; }
0511       const TString&   GetOriginalVarName( Int_t ivar ) const { return DataInfo().GetVariableInfo(ivar).GetExpression(); }
0512 
0513       Bool_t           HasTrainingTree() const { return Data()->GetNTrainingEvents() != 0; }
0514 
0515       // ---------- protected auxiliary methods ------------------------------------
0516 
0517    protected:
0518 
0519       // make ROOT-independent C++ class for classifier response (classifier-specific implementation)
0520       void     MakeClassSpecific( std::ostream&, const TString& = "" ) const override {}
0521 
0522       // header and auxiliary classes
0523       virtual void     MakeClassSpecificHeader( std::ostream&, const TString& = "" ) const {}
0524 
0525       // static pointer to this object - required for ROOT finder (to be solved differently)(solved by Omar)
0526       //static MethodBase* GetThisBase();
0527 
0528       // some basic statistical analysis
0529       void Statistics( Types::ETreeType treeType, const TString& theVarName,
0530                        Double_t&, Double_t&, Double_t&,
0531                        Double_t&, Double_t&, Double_t& );
0532 
0533       // if TRUE, write weights only to text files
0534       Bool_t           TxtWeightsOnly() const { return kTRUE; }
0535 
0536    protected:
0537 
0538       // access to event information that needs method-specific information
0539 
0540       Bool_t           IsConstructedFromWeightFile() const { return fConstructedFromWeightFile; }
0541 
0542    private:
0543 
0544       // ---------- private definitions --------------------------------------------
0545       // Initialisation
0546       void             InitBase();
0547       void             DeclareBaseOptions();
0548       void             ProcessBaseOptions();
0549 
0550       // used in efficiency computation
0551       enum ECutOrientation { kNegative = -1, kPositive = +1 };
0552       ECutOrientation  GetCutOrientation() const { return fCutOrientation; }
0553 
0554       // ---------- private accessors ---------------------------------------------
0555 
0556       // reset required for RootFinder
0557       void             ResetThisBase();
0558 
0559       // ---------- private auxiliary methods --------------------------------------
0560 
0561       // PDFs for classifier response (required to compute signal probability and Rarity)
0562       void             CreateMVAPdfs();
0563 
0564       // for root finder
0565       //virtual method to find ROOT
0566       virtual Double_t         GetValueForRoot ( Double_t );  // implementation
0567 
0568       // used for file parsing
0569       Bool_t           GetLine( std::istream& fin, char * buf );
0570 
0571       // fill test tree with classification or regression results
0572       virtual void     AddClassifierOutput    ( Types::ETreeType type );
0573       virtual void     AddClassifierOutputProb( Types::ETreeType type );
0574       virtual void     AddRegressionOutput    ( Types::ETreeType type );
0575       virtual void     AddMulticlassOutput    ( Types::ETreeType type );
0576 
0577    private:
0578 
0579       void             AddInfoItem( void* gi, const TString& name,
0580                                     const TString& value) const;
0581 
0582       // ========== class members ==================================================
0583 
0584    protected:
0585 
0586       // direct accessors
0587       Ranking*              fRanking;              // pointer to ranking object (created by derived classifiers)
0588       std::vector<TString>* fInputVars;            // vector of input variables used in MVA
0589 
0590       // histogram binning
0591       Int_t                 fNbins;                // number of bins in input variable histograms
0592       Int_t                 fNbinsMVAoutput;       // number of bins in MVA output histograms
0593       Int_t                 fNbinsH;               // number of bins in evaluation histograms
0594 
0595       Types::EAnalysisType  fAnalysisType;         // method-mode : true --> regression, false --> classification
0596 
0597       std::vector<Float_t>* fRegressionReturnVal;  // holds the return-values for the regression
0598       std::vector<Float_t>* fMulticlassReturnVal;  // holds the return-values for the multiclass classification
0599 
0600    private:
0601 
0602       // MethodCuts redefines some of the evaluation variables and histograms -> must access private members
0603       friend class MethodCuts;
0604 
0605 
0606       // data sets
0607       DataSetInfo&     fDataSetInfo;         //! the data set information (sometimes needed)
0608 
0609       Double_t         fSignalReferenceCut;  // minimum requirement on the MVA output to declare an event signal-like
0610       Double_t         fSignalReferenceCutOrientation;  // minimum requirement on the MVA output to declare an event signal-like
0611       Types::ESBType   fVariableTransformType;  // this is the event type (sig or bgd) assumed for variable transform
0612 
0613       // naming and versioning
0614       TString          fJobName;             // name of job -> user defined, appears in weight files
0615       TString          fMethodName;          // name of the method (set in derived class)
0616       Types::EMVA      fMethodType;          // type of method (set in derived class)
0617       TString          fTestvar;             // variable used in evaluation, etc (mostly the MVA)
0618       UInt_t           fTMVATrainingVersion; // TMVA version used for training
0619       UInt_t           fROOTTrainingVersion; // ROOT version used for training
0620       Bool_t           fConstructedFromWeightFile; // is it obtained from weight file?
0621 
0622       // Directory structure: dataloader/fMethodBaseDir/fBaseDir
0623       // where the first directory name is defined by the method type
0624       // and the second is user supplied (the title given in Factory::BookMethod())
0625       TDirectory*      fBaseDir;             // base directory for the instance, needed to know where to jump back from localDir
0626       mutable TDirectory* fMethodBaseDir;    // base directory for the method
0627       //this will be the next way to save results
0628       TFile            *fFile;
0629 
0630       //SilentFile
0631       Bool_t fSilentFile;
0632       //Model Persistence
0633       Bool_t fModelPersistence;
0634 
0635       TString          fParentDir;           ///< method parent name, like booster name
0636 
0637       TString          fFileDir;             ///< unix sub-directory for weight files (default: DataLoader's Name + "weights")
0638       TString          fWeightFile;          ///< weight file name
0639 
0640    private:
0641 
0642       TH1*             fEffS;                ///< efficiency histogram for rootfinder
0643 
0644       PDF*             fDefaultPDF;          ///< default PDF definitions
0645       PDF*             fMVAPdfS;             ///< signal MVA PDF
0646       PDF*             fMVAPdfB;             ///< background MVA PDF
0647 
0648       //      TH1D*            fmvaS;        ///< PDFs of MVA distribution (signal)
0649       //      TH1D*            fmvaB;        ///< PDFs of MVA distribution (background)
0650       PDF*             fSplS;                ///< PDFs of MVA distribution (signal)
0651       PDF*             fSplB;                ///< PDFs of MVA distribution (background)
0652       TSpline*         fSpleffBvsS;          ///< splines for signal eff. versus background eff.
0653 
0654       PDF*             fSplTrainS;           ///< PDFs of training MVA distribution (signal)
0655       PDF*             fSplTrainB;           ///< PDFs of training MVA distribution (background)
0656       TSpline*         fSplTrainEffBvsS;     ///< splines for training signal eff. versus background eff.
0657 
0658    private:
0659 
0660       // basic statistics quantities of MVA
0661       Double_t         fMeanS;               ///< mean (signal)
0662       Double_t         fMeanB;               ///< mean (background)
0663       Double_t         fRmsS;                ///< RMS (signal)
0664       Double_t         fRmsB;                ///< RMS (background)
0665       Double_t         fXmin;                ///< minimum (signal and background)
0666       Double_t         fXmax;                ///< maximum (signal and background)
0667 
0668       // variable preprocessing
0669       TString          fVarTransformString;           ///< labels variable transform method
0670 
0671       TransformationHandler* fTransformationPointer;  ///< pointer to the rest of transformations
0672       TransformationHandler  fTransformation;         ///< the list of transformations
0673 
0674 
0675       // help and verbosity
0676       Bool_t           fVerbose;               ///< verbose flag
0677       TString          fVerbosityLevelString;  ///< verbosity level (user input string)
0678       EMsgType         fVerbosityLevel;        ///< verbosity level
0679       Bool_t           fHelp;                  ///< help flag
0680       Bool_t           fHasMVAPdfs;            ///< MVA Pdfs are created for this classifier
0681 
0682       Bool_t           fIgnoreNegWeightsInTraining; ///< If true, events with negative weights are not used in training
0683 
0684    protected:
0685 
0686       Bool_t           IgnoreEventsWithNegWeightsInTraining() const { return fIgnoreNegWeightsInTraining; }
0687 
0688       // for signal/background
0689       UInt_t           fSignalClass;           // index of the Signal-class
0690       UInt_t           fBackgroundClass;       // index of the Background-class
0691 
0692    private:
0693 
0694       // timing variables
0695       Double_t         fTrainTime;             // for timing measurements
0696       Double_t         fTestTime;              // for timing measurements
0697 
0698       // orientation of cut: depends on signal and background mean values
0699       ECutOrientation  fCutOrientation;      // +1 if Sig>Bkg, -1 otherwise
0700 
0701       // for root finder
0702       TSpline1*        fSplRefS;             // helper splines for RootFinder (signal)
0703       TSpline1*        fSplRefB;             // helper splines for RootFinder (background)
0704 
0705       TSpline1*        fSplTrainRefS;        // helper splines for RootFinder (signal)
0706       TSpline1*        fSplTrainRefB;        // helper splines for RootFinder (background)
0707 
0708       mutable std::vector<const std::vector<TMVA::Event*>*> fEventCollections; // if the method needs the complete event-collection, the transformed event coll. ist stored here.
0709 
0710    public:
0711       Bool_t           fSetupCompleted;      // is method setup
0712 
0713    private:
0714 
0715       // This is a workaround for OSx where static thread_local data members are
0716       // not supported. The C++ solution would indeed be the following:
0717 //       static MethodBase*& GetThisBaseThreadLocal() {TTHREAD_TLS(MethodBase*) fgThisBase(nullptr); return fgThisBase; };
0718 
0719       // ===== depreciated options, kept for backward compatibility  =====
0720    private:
0721 
0722       Bool_t           fNormalise;                   // normalise input variables
0723       Bool_t           fUseDecorr;                   // synonymous for decorrelation
0724       TString          fVariableTransformTypeString; // labels variable transform type
0725       Bool_t           fTxtWeightsOnly;              // if TRUE, write weights only to text files
0726       Int_t            fNbinsMVAPdf;                 // number of bins used in histogram that creates PDF
0727       Int_t            fNsmoothMVAPdf;               // number of times a histogram is smoothed before creating the PDF
0728 
0729    protected:
0730       Results *fResults;
0731       ClassDefOverride(MethodBase,0);  // Virtual base class for all TMVA method
0732 
0733    };
0734 } // namespace TMVA
0735 
0736 
0737 
0738 
0739 
0740 
0741 
0742 // ========== INLINE FUNCTIONS =========================================================
0743 
0744 
0745 //_______________________________________________________________________
0746 inline const TMVA::Event* TMVA::MethodBase::GetEvent( const TMVA::Event* ev ) const
0747 {
0748    return GetTransformationHandler().Transform(ev);
0749 }
0750 
0751 inline const TMVA::Event* TMVA::MethodBase::GetEvent() const
0752 {
0753    if(fTmpEvent)
0754       return GetTransformationHandler().Transform(fTmpEvent);
0755    else
0756       return GetTransformationHandler().Transform(Data()->GetEvent());
0757 }
0758 
0759 inline const TMVA::Event* TMVA::MethodBase::GetEvent( Long64_t ievt ) const
0760 {
0761    assert(fTmpEvent==nullptr);
0762    return GetTransformationHandler().Transform(Data()->GetEvent(ievt));
0763 }
0764 
0765 inline const TMVA::Event* TMVA::MethodBase::GetEvent( Long64_t ievt, Types::ETreeType type ) const
0766 {
0767    assert(fTmpEvent==nullptr);
0768    return GetTransformationHandler().Transform(Data()->GetEvent(ievt, type));
0769 }
0770 
0771 inline const TMVA::Event* TMVA::MethodBase::GetTrainingEvent( Long64_t ievt ) const
0772 {
0773    assert(fTmpEvent==nullptr);
0774    return GetEvent(ievt, Types::kTraining);
0775 }
0776 
0777 inline const TMVA::Event* TMVA::MethodBase::GetTestingEvent( Long64_t ievt ) const
0778 {
0779    assert(fTmpEvent==nullptr);
0780    return GetEvent(ievt, Types::kTesting);
0781 }
0782 
0783 #endif