Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 08:50:16

0001 // @(#)root/tmva $Id$ 2017
0002 // Authors:  Omar Zapata, Andreas Hoecker, Peter Speckmayer, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne,
0003 // Jan Therhaag
0004 
0005 #ifndef ROOT_TMVA_Classification
0006 #define ROOT_TMVA_Classification
0007 
0008 #include <TString.h>
0009 #include <TMultiGraph.h>
0010 #include <vector>
0011 #include <map>
0012 
0013 #include <TMVA/IMethod.h>
0014 #include <TMVA/MethodBase.h>
0015 #include <TMVA/Configurable.h>
0016 #include <TMVA/Types.h>
0017 #include <TMVA/DataSet.h>
0018 #include <TMVA/Event.h>
0019 #include <TMVA/Results.h>
0020 #include <TMVA/ResultsClassification.h>
0021 #include <TMVA/ResultsMulticlass.h>
0022 #include <TMVA/Factory.h>
0023 #include <TMVA/DataLoader.h>
0024 #include <TMVA/OptionMap.h>
0025 #include <TMVA/Envelope.h>
0026 
0027 /*! \class TMVA::ClassificationResult
0028  * Class to save the results of the classifier.
0029  * Every machine learning method booked have an object for the results
0030  * in the classification process, in this class is stored the mvas,
0031  * data loader name and ml method name and title.
0032  * You can to display the results calling the method Show, get the ROC-integral with the
0033  * method GetROCIntegral or get the TMVA::ROCCurve object calling GetROC.
0034 \ingroup TMVA
0035 */
0036 
0037 /*! \class TMVA::Classification
0038  * Class to perform two class classification.
0039  * The first step before any analysis is to prepare the data,
0040  * to do that you need to create an object of TMVA::DataLoader,
0041  * in this object you need to configure the variables and the number of events
0042  * to train/test.
0043  * The class TMVA::Experimental::Classification needs a TMVA::DataLoader object,
0044  * optional a TFile object to save the results and some extra options in a string
0045  * like "V:Color:Transformations=I;D;P;U;G:Silent:DrawProgressBar:ModelPersistence:Jobs=2" where:
0046  * V                = verbose output
0047  * Color            = coloured screen output
0048  * Silent           = batch mode: boolean silent flag inhibiting any output from TMVA
0049  * Transformations  = list of transformations to test.
0050  * DrawProgressBar  = draw progress bar to display training and testing.
0051  * ModelPersistence = to save the trained model in xml or serialized files.
0052  * Jobs             = number of ml methods to test/train in parallel using MultiProc, requires to call Evaluate method.
0053  * Basic example.
0054  * \code
0055 void classification(UInt_t jobs = 2)
0056 {
0057    TMVA::Tools::Instance();
0058 
0059    TFile *input(0);
0060    TString fname = gROOT->GetTutorialDir() + "/tmva/data/tmva_class_example.root";
0061    if (!gSystem->AccessPathName(fname)) {
0062       input = TFile::Open(fname); // check if file in local directory exists
0063    }
0064    if (!input) {
0065       std::cout << "ERROR: could not open data file" << std::endl;
0066       exit(1);
0067    }
0068 
0069    // Register the training and test trees
0070 
0071    TTree *signalTree = (TTree *)input->Get("TreeS");
0072    TTree *background = (TTree *)input->Get("TreeB");
0073 
0074    TMVA::DataLoader *dataloader = new TMVA::DataLoader("dataset");
0075 
0076    dataloader->AddVariable("myvar1 := var1+var2", 'F');
0077    dataloader->AddVariable("myvar2 := var1-var2", "Expression 2", "", 'F');
0078    dataloader->AddVariable("var3", "Variable 3", "units", 'F');
0079    dataloader->AddVariable("var4", "Variable 4", "units", 'F');
0080 
0081    dataloader->AddSpectator("spec1 := var1*2", "Spectator 1", "units", 'F');
0082    dataloader->AddSpectator("spec2 := var1*3", "Spectator 2", "units", 'F');
0083 
0084    // global event weights per tree (see below for setting event-wise weights)
0085    Double_t signalWeight = 1.0;
0086    Double_t backgroundWeight = 1.0;
0087 
0088    dataloader->SetBackgroundWeightExpression("weight");
0089 
0090    TMVA::Experimental::Classification *cl = new TMVA::Experimental::Classification(dataloader, Form("Jobs=%d", jobs));
0091 
0092    cl->BookMethod(TMVA::Types::kBDT, "BDTG", "!H:!V:NTrees=2000:MinNodeSize=2.5%:BoostType=Grad:Shrinkage=0.10:"
0093                                              "UseBaggedBoost:BaggedSampleFraction=0.5:nCuts=20:MaxDepth=2");
0094    cl->BookMethod(TMVA::Types::kSVM, "SVM", "Gamma=0.25:Tol=0.001:VarTransform=Norm");
0095 
0096    cl->Evaluate(); // Train and Test all methods
0097 
0098    auto &results = cl->GetResults();
0099 
0100    TCanvas *c = new TCanvas(Form("ROC"));
0101    c->SetTitle("ROC-Integral Curve");
0102 
0103    auto mg = new TMultiGraph();
0104    for (UInt_t i = 0; i < results.size(); i++) {
0105       auto roc = results[i].GetROCGraph();
0106       roc->SetLineColorAlpha(i + 1, 0.1);
0107       mg->Add(roc);
0108    }
0109    mg->Draw("AL");
0110    mg->GetXaxis()->SetTitle(" Signal Efficiency ");
0111    mg->GetYaxis()->SetTitle(" Background Rejection ");
0112    c->BuildLegend(0.15, 0.15, 0.3, 0.3);
0113    c->Draw();
0114 
0115    delete cl;
0116 }
0117  * \endcode
0118  *
0119 \ingroup TMVA
0120 */
0121 
0122 namespace TMVA {
0123 class ResultsClassification;
0124 namespace Experimental {
0125 class ClassificationResult : public TObject {
0126    friend class Classification;
0127 
0128 private:
0129    OptionMap fMethod;
0130    TString fDataLoaderName;
0131    std::map<UInt_t, std::vector<std::tuple<Float_t, Float_t, Bool_t>>> fMvaTrain; ///< Mvas for two-class classification
0132    std::map<UInt_t, std::vector<std::tuple<Float_t, Float_t, Bool_t>>> fMvaTest;  ///< Mvas for two-class and multiclass classification
0133    std::vector<TString> fClassNames;
0134 
0135    Bool_t IsMethod(TString methodname, TString methodtitle);
0136    Bool_t fIsCuts;        ///< if it is a method cuts need special output
0137    Double_t fROCIntegral;
0138 
0139 public:
0140    ClassificationResult();
0141    ClassificationResult(const ClassificationResult &cr);
0142    ~ClassificationResult() {}
0143 
0144    const TString GetMethodName() const { return fMethod.GetValue<TString>("MethodName"); }
0145    const TString GetMethodTitle() const { return fMethod.GetValue<TString>("MethodTitle"); }
0146    ROCCurve *GetROC(UInt_t iClass = 0, TMVA::Types::ETreeType type = TMVA::Types::kTesting);
0147    Double_t GetROCIntegral(UInt_t iClass = 0, TMVA::Types::ETreeType type = TMVA::Types::kTesting);
0148    TString GetDataLoaderName() { return fDataLoaderName; }
0149    Bool_t IsCutsMethod() { return fIsCuts; }
0150 
0151    void Show();
0152 
0153    TGraph *GetROCGraph(UInt_t iClass = 0, TMVA::Types::ETreeType type = TMVA::Types::kTesting);
0154    ClassificationResult &operator=(const ClassificationResult &r);
0155 
0156    ClassDef(ClassificationResult, 3);
0157 };
0158 
0159 class Classification : public Envelope {
0160    std::vector<ClassificationResult> fResults; ///<!
0161    std::vector<IMethod *> fIMethods;           ///<! vector of objects with booked methods
0162    Types::EAnalysisType fAnalysisType;         ///<!
0163    Bool_t fCorrelations;                       ///<!
0164    Bool_t fROC;                                ///<!
0165 public:
0166    explicit Classification(DataLoader *loader, TFile *file, TString options);
0167    explicit Classification(DataLoader *loader, TString options);
0168    ~Classification();
0169 
0170    virtual void Train();
0171    virtual void TrainMethod(TString methodname, TString methodtitle);
0172    virtual void TrainMethod(Types::EMVA method, TString methodtitle);
0173 
0174    virtual void Test();
0175    virtual void TestMethod(TString methodname, TString methodtitle);
0176    virtual void TestMethod(Types::EMVA method, TString methodtitle);
0177 
0178    virtual void Evaluate();
0179 
0180    std::vector<ClassificationResult> &GetResults();
0181 
0182    MethodBase *GetMethod(TString methodname, TString methodtitle);
0183 
0184 protected:
0185    TString GetMethodOptions(TString methodname, TString methodtitle);
0186    Bool_t HasMethodObject(TString methodname, TString methodtitle, Int_t &index);
0187    Bool_t IsCutsMethod(TMVA::MethodBase *method);
0188    TMVA::ROCCurve *
0189    GetROC(TMVA::MethodBase *method, UInt_t iClass = 0, TMVA::Types::ETreeType type = TMVA::Types::kTesting);
0190    TMVA::ROCCurve *GetROC(TString methodname, TString methodtitle, UInt_t iClass = 0,
0191                           TMVA::Types::ETreeType type = TMVA::Types::kTesting);
0192 
0193    Double_t GetROCIntegral(TString methodname, TString methodtitle, UInt_t iClass = 0);
0194 
0195    ClassificationResult &GetResults(TString methodname, TString methodtitle);
0196    void CopyFrom(TDirectory *src, TFile *file);
0197    void MergeFiles();
0198 
0199    ClassDef(Classification, 0);
0200 };
0201 } // namespace Experimental
0202 } // namespace TMVA
0203 
0204 #endif // ROOT_TMVA_Classification