Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-24 09:24:27

0001 // @(#)root/tmva $Id$
0002 // Author: Andreas Hoecker, Matt Jachowski, Peter Speckmayer, Helge Voss, Kai Voss
0003 
0004 /**********************************************************************************
0005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
0006  * Package: TMVA                                                                  *
0007  * Class  : MethodCuts                                                            *
0008  *                                             *
0009  *                                                                                *
0010  * Description:                                                                   *
0011  *      Multivariate optimisation of signal efficiency for given background       *
0012  *      efficiency, using rectangular minimum and maximum requirements on         *
0013  *      input variables                                                           *
0014  *                                                                                *
0015  * Authors (alphabetical):                                                        *
0016  *      Andreas Hoecker  <Andreas.Hocker@cern.ch> - CERN, Switzerland             *
0017  *      Matt Jachowski   <jachowski@stanford.edu> - Stanford University, USA      *
0018  *      Peter Speckmayer <speckmay@mail.cern.ch>  - CERN, Switzerland             *
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:                                                            *
0023  *      CERN, Switzerland                                                         *
0024  *      U. of Victoria, Canada                                                    *
0025  *      MPI-K Heidelberg, Germany                                                 *
0026  *      LAPP, Annecy, France                                                      *
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_MethodCuts
0034 #define ROOT_TMVA_MethodCuts
0035 
0036 //////////////////////////////////////////////////////////////////////////
0037 //                                                                      //
0038 // MethodCuts                                                           //
0039 //                                                                      //
0040 // Multivariate optimisation of signal efficiency for given background  //
0041 // efficiency, using rectangular minimum and maximum requirements on    //
0042 // input variables                                                      //
0043 //                                                                      //
0044 //////////////////////////////////////////////////////////////////////////
0045 
0046 #include <vector>
0047 
0048 
0049 #include "TMVA/MethodBase.h"
0050 #include "TMVA/BinarySearchTree.h"
0051 #include "TMVA/PDF.h"
0052 #include "TMatrixDfwd.h"
0053 #include "IFitterTarget.h"
0054 
0055 class TRandom;
0056 
0057 namespace TMVA {
0058 
0059    class Interval;
0060 
0061    class MethodCuts : public MethodBase, public IFitterTarget {
0062 
0063    public:
0064 
0065       MethodCuts( const TString& jobName,
0066                   const TString& methodTitle,
0067                   DataSetInfo& theData,
0068                   const TString& theOption = "MC:150:10000:");
0069 
0070       MethodCuts( DataSetInfo& theData,
0071                   const TString& theWeightFile);
0072 
0073       virtual ~MethodCuts( void );
0074 
0075       Bool_t HasAnalysisType( Types::EAnalysisType type, UInt_t numberClasses, UInt_t numberTargets ) override;
0076 
0077       // training method
0078       void Train( void ) override;
0079 
0080       using MethodBase::ReadWeightsFromStream;
0081 
0082       void AddWeightsXMLTo      ( void* parent ) const override;
0083 
0084       void ReadWeightsFromStream( std::istream & i ) override;
0085       void ReadWeightsFromXML   ( void* wghtnode ) override;
0086 
0087       // calculate the MVA value (for CUTs this is just a dummy)
0088       Double_t GetMvaValue( Double_t* err = nullptr, Double_t* errUpper = nullptr ) override;
0089 
0090       // write method specific histos to target file
0091       void WriteMonitoringHistosToFile( void ) const override;
0092 
0093       // test the method
0094       void TestClassification() override;
0095 
0096       // also overwrite --> not computed for cuts
0097       Double_t GetSeparation  ( TH1*, TH1* ) const override { return -1; }
0098       Double_t GetSeparation  ( PDF* = nullptr, PDF* = nullptr ) const override { return -1; }
0099       Double_t GetSignificance( void )       const override { return -1; }
0100       Double_t GetmuTransform ( TTree *)           { return -1; }
0101       Double_t GetEfficiency  ( const TString&, Types::ETreeType, Double_t& ) override;
0102       Double_t GetTrainingEfficiency(const TString& ) override;
0103 
0104       // rarity distributions (signal or background (default) is uniform in [0,1])
0105       Double_t GetRarity( Double_t, Types::ESBType ) const override { return 0; }
0106 
0107       // accessors for Minuit
0108       Double_t ComputeEstimator( std::vector<Double_t> & );
0109 
0110       Double_t EstimatorFunction( std::vector<Double_t> & ) override;
0111       Double_t EstimatorFunction( Int_t ievt1, Int_t ievt2 );
0112 
0113       void     SetTestSignalEfficiency( Double_t effS ) { fTestSignalEff = effS; }
0114 
0115       // retrieve cut values for given signal efficiency
0116       void     PrintCuts( Double_t effS ) const;
0117       Double_t GetCuts  ( Double_t effS, std::vector<Double_t>& cutMin, std::vector<Double_t>& cutMax ) const;
0118       Double_t GetCuts  ( Double_t effS, Double_t* cutMin, Double_t* cutMax ) const;
0119 
0120       // ranking of input variables (not available for cuts)
0121       const Ranking* CreateRanking() override { return nullptr; }
0122 
0123       void DeclareOptions() override;
0124       void ProcessOptions() override;
0125 
0126       // maximum |cut| value
0127       static const Double_t fgMaxAbsCutVal;
0128 
0129       // no check of options at this place
0130       void CheckSetup() override {}
0131 
0132    protected:
0133 
0134       // make ROOT-independent C++ class for classifier response (classifier-specific implementation)
0135       void MakeClassSpecific( std::ostream&, const TString& ) const override;
0136 
0137       // get help message text
0138       void GetHelpMessage() const override;
0139 
0140    private:
0141 
0142       // optimisation method
0143       enum EFitMethodType { kUseMonteCarlo = 0,
0144                             kUseGeneticAlgorithm,
0145                             kUseSimulatedAnnealing,
0146                             kUseMinuit,
0147                             kUseEventScan,
0148                             kUseMonteCarloEvents };
0149 
0150       // efficiency calculation method
0151       // - kUseEventSelection: computes efficiencies from given data sample
0152       // - kUsePDFs          : creates smoothed PDFs from data samples, and
0153       //                       uses this to compute efficiencies
0154       enum EEffMethod     { kUseEventSelection = 0,
0155                             kUsePDFs };
0156 
0157       // improve the Monte Carlo by providing some additional information
0158       enum EFitParameters { kNotEnforced = 0,
0159                             kForceMin,
0160                             kForceMax,
0161                             kForceSmart };
0162 
0163       // general
0164       TString                 fFitMethodS;         ///< chosen fit method (string)
0165       EFitMethodType          fFitMethod;          ///< chosen fit method
0166       TString                 fEffMethodS;         ///< chosen efficiency calculation method (string)
0167       EEffMethod              fEffMethod;          ///< chosen efficiency calculation method
0168       std::vector<EFitParameters>* fFitParams;     ///< vector for series of fit methods
0169       Double_t                fTestSignalEff;      ///< used to test optimized signal efficiency
0170       Double_t                fEffSMin;            ///< used to test optimized signal efficiency
0171       Double_t                fEffSMax;            ///< used to test optimized signal efficiency
0172       Double_t*               fCutRangeMin;        ///< minimum of allowed cut range
0173       Double_t*               fCutRangeMax;        ///< maximum of allowed cut range
0174       std::vector<Interval*>  fCutRange;           ///< allowed ranges for cut optimisation
0175 
0176       // for the use of the binary tree method
0177       BinarySearchTree*       fBinaryTreeS;
0178       BinarySearchTree*       fBinaryTreeB;
0179 
0180       // MC method
0181       Double_t**              fCutMin;             ///< minimum requirement
0182       Double_t**              fCutMax;             ///< maximum requirement
0183       Double_t*               fTmpCutMin;          ///< temporary minimum requirement
0184       Double_t*               fTmpCutMax;          ///< temporary maximum requirement
0185       TString*                fAllVarsI;           ///< what to do with variables
0186 
0187       // relevant for all methods
0188       Int_t                   fNpar;               ///< number of parameters in fit (default: 2*Nvar)
0189       Double_t                fEffRef;             ///< reference efficiency
0190       std::vector<Int_t>*     fRangeSign;          ///< used to match cuts to fit parameters (and vice versa)
0191       TRandom*                fRandom;             ///< random generator for MC optimisation method
0192 
0193       // basic statistics
0194       std::vector<Double_t>*  fMeanS;              ///< means of variables (signal)
0195       std::vector<Double_t>*  fMeanB;              ///< means of variables (background)
0196       std::vector<Double_t>*  fRmsS;               ///< RMSs of variables (signal)
0197       std::vector<Double_t>*  fRmsB;               ///< RMSs of variables (background)
0198 
0199       TH1*                    fEffBvsSLocal;       ///< intermediate eff. background versus eff signal histo
0200 
0201       // PDF section
0202       std::vector<TH1*>*      fVarHistS;           ///< reference histograms (signal)
0203       std::vector<TH1*>*      fVarHistB;           ///< reference histograms (background)
0204       std::vector<TH1*>*      fVarHistS_smooth;    ///< smoothed reference histograms (signal)
0205       std::vector<TH1*>*      fVarHistB_smooth;    ///< smoothed reference histograms (background)
0206       std::vector<PDF*>*      fVarPdfS;            ///< reference PDFs (signal)
0207       std::vector<PDF*>*      fVarPdfB;            ///< reference PDFs (background)
0208 
0209       // negative efficiencies
0210       Bool_t                  fNegEffWarning;      ///< flag risen in case of negative efficiency warning
0211 
0212 
0213       // the definition of fit parameters can be different from the actual
0214       // cut requirements; these functions provide the matching
0215       void     MatchParsToCuts( const std::vector<Double_t>&, Double_t*, Double_t* );
0216       void     MatchParsToCuts( Double_t*, Double_t*, Double_t* );
0217 
0218       void     MatchCutsToPars( std::vector<Double_t>&, Double_t*, Double_t* );
0219       void     MatchCutsToPars( std::vector<Double_t>&, Double_t**, Double_t**, Int_t ibin );
0220 
0221       // creates PDFs in case these are used to compute efficiencies
0222       // (corresponds to: EffMethod == kUsePDFs)
0223       void     CreateVariablePDFs( void );
0224 
0225       // returns signal and background efficiencies for given cuts - using event counting
0226       void     GetEffsfromSelection( Double_t* cutMin, Double_t* cutMax,
0227                                      Double_t& effS, Double_t& effB );
0228       // returns signal and background efficiencies for given cuts - using PDFs
0229       void     GetEffsfromPDFs( Double_t* cutMin, Double_t* cutMax,
0230                                 Double_t& effS, Double_t& effB );
0231 
0232       // default initialisation method called by all constructors
0233       void     Init( void ) override;
0234 
0235       ClassDefOverride(MethodCuts,0);  // Multivariate optimisation of signal efficiency
0236    };
0237 
0238 } // namespace TMVA
0239 
0240 #endif