Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TMVAClassificationCategoryApplication.C File Reference

Detailed Description

View in nbviewer Open in SWAN
This macro provides a simple example on how to use the trained classifiers (with categories) within an analysis module

  • Project : TMVA - a Root-integrated toolkit for multivariate data analysis
  • Package : TMVA
  • Executable: TMVAClassificationCategoryApplication
==> Start TMVAClassificationCategoryApplication
: Booking "FisherCat method" of type "Category" from dataset/weights/TMVAClassificationCategory_FisherCat.weights.xml.
: Reading weight file: dataset/weights/TMVAClassificationCategory_FisherCat.weights.xml
<HEADER> DataSetInfo : [Default] : Added class "Signal"
<HEADER> DataSetInfo : [Default] : Added class "Background"
: Recreating sub-classifiers from XML-file
<HEADER> DataSetInfo : [Category_Fisher_1_dsi] : Added class "Signal"
<HEADER> DataSetInfo : [Category_Fisher_1_dsi] : Added class "Background"
<HEADER> DataSetInfo : [Category_Fisher_2_dsi] : Added class "Signal"
<HEADER> DataSetInfo : [Category_Fisher_2_dsi] : Added class "Background"
: Booked classifier "FisherCat" of type: "Category"
: Booking "LikelihoodCat method" of type "Category" from dataset/weights/TMVAClassificationCategory_LikelihoodCat.weights.xml.
: Reading weight file: dataset/weights/TMVAClassificationCategory_LikelihoodCat.weights.xml
: Recreating sub-classifiers from XML-file
<HEADER> DataSetInfo : [Category_Likelihood_1_dsi] : Added class "Signal"
<HEADER> DataSetInfo : [Category_Likelihood_1_dsi] : Added class "Background"
<HEADER> DataSetInfo : [Category_Likelihood_2_dsi] : Added class "Signal"
<HEADER> DataSetInfo : [Category_Likelihood_2_dsi] : Added class "Background"
: Booked classifier "LikelihoodCat" of type: "Category"
--- TMVAClassificationApp : Accessing /home/sftnight/build/workspace/root-makedoc-v628/rootspi/rdoc/src/v6-28-00-patches/tutorials/tmva/data/toy_sigbkg_categ_offset.root!
--- Use signal sample for evaluation
: Rebuilding Dataset Default
--- End of event loop: Real time 0:00:00, CP time 0.070
--- Created root file: "TMVApp.root" containing the MVA output histograms
==> TMVAClassificationApplication is done!
#include <cstdlib>
#include <vector>
#include <iostream>
#include <map>
#include <string>
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include "TSystem.h"
#include "TROOT.h"
#include "TH1F.h"
#include "TStopwatch.h"
#if not defined(__CINT__) || defined(__MAKECINT__)
#include "TMVA/Tools.h"
#include "TMVA/Reader.h"
#endif
// two types of category methods are implemented
Bool_t UseOffsetMethod = kTRUE;
void TMVAClassificationCategoryApplication()
{
// ---------------------------------------------------------------
// default MVA methods to be trained + tested
std::map<std::string,int> Use;
//
Use["LikelihoodCat"] = 1;
Use["FisherCat"] = 1;
// ---------------------------------------------------------------
std::cout << std::endl
<< "==> Start TMVAClassificationCategoryApplication" << std::endl;
// Create the Reader object
TMVA::Reader *reader = new TMVA::Reader( "!Color:!Silent" );
// Create a set of variables and spectators and declare them to the reader
// - the variable names MUST corresponds in name and type to those given in the weight file(s) used
Float_t var1, var2, var3, var4, eta;
reader->AddVariable( "var1", &var1 );
reader->AddVariable( "var2", &var2 );
reader->AddVariable( "var3", &var3 );
reader->AddVariable( "var4", &var4 );
reader->AddSpectator( "eta", &eta );
// Book the MVA methods
for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
if (it->second) {
TString methodName = it->first + " method";
TString weightfile = "dataset/weights/TMVAClassificationCategory_" + TString(it->first) + ".weights.xml";
reader->BookMVA( methodName, weightfile );
}
}
// Book output histograms
UInt_t nbin = 100;
std::map<std::string,TH1*> hist;
hist["LikelihoodCat"] = new TH1F( "MVA_LikelihoodCat", "MVA_LikelihoodCat", nbin, -1, 0.9999 );
hist["FisherCat"] = new TH1F( "MVA_FisherCat", "MVA_FisherCat", nbin, -4, 4 );
// Prepare input tree (this must be replaced by your data source)
// in this example, there is a toy tree with signal and one with background events
// we'll later on use only the "signal" events for the test in this example.
//
TString fname = gSystem->GetDirName(__FILE__) + "/data/";
// if directory data not found try using tutorials dir
if (gSystem->AccessPathName( fname + "toy_sigbkg_categ_offset.root" )) {
fname = gROOT->GetTutorialDir() + "/tmva/data/";
}
if (UseOffsetMethod) fname += "toy_sigbkg_categ_offset.root";
else fname += "toy_sigbkg_categ_varoff.root";
std::cout << "--- TMVAClassificationApp : Accessing " << fname << "!" << std::endl;
TFile *input = TFile::Open(fname);
if (!input) {
std::cout << "ERROR: could not open data file: " << fname << std::endl;
exit(1);
}
// Event loop
// Prepare the tree
// - here the variable names have to corresponds to your tree
// - you can use the same variables as above which is slightly faster,
// but of course you can use different ones and copy the values inside the event loop
//
TTree* theTree = (TTree*)input->Get("TreeS");
std::cout << "--- Use signal sample for evaluation" << std::endl;
theTree->SetBranchAddress( "var1", &var1 );
theTree->SetBranchAddress( "var2", &var2 );
theTree->SetBranchAddress( "var3", &var3 );
theTree->SetBranchAddress( "var4", &var4 );
theTree->SetBranchAddress( "eta", &eta ); // spectator
std::cout << "--- Processing: " << theTree->GetEntries() << " events" << std::endl;
sw.Start();
for (Long64_t ievt=0; ievt<theTree->GetEntries();ievt++) {
if (ievt%1000 == 0) std::cout << "--- ... Processing event: " << ievt << std::endl;
theTree->GetEntry(ievt);
// Return the MVA outputs and fill into histograms
for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++) {
if (!it->second) continue;
TString methodName = it->first + " method";
hist[it->first]->Fill( reader->EvaluateMVA( methodName ) );
}
}
sw.Stop();
std::cout << "--- End of event loop: "; sw.Print();
// Write histograms
TFile *target = new TFile( "TMVApp.root","RECREATE" );
for (std::map<std::string,int>::iterator it = Use.begin(); it != Use.end(); it++)
if (it->second) hist[it->first]->Write();
target->Close();
std::cout << "--- Created root file: \"TMVApp.root\" containing the MVA output histograms" << std::endl;
delete reader;
std::cout << "==> TMVAClassificationApplication is done!" << std::endl << std::endl;
}
int main( int argc, char** argv )
{
TMVAClassificationCategoryApplication();
return 0;
}
int main()
Definition Prototype.cxx:12
bool Bool_t
Definition RtypesCore.h:63
unsigned int UInt_t
Definition RtypesCore.h:46
float Float_t
Definition RtypesCore.h:57
long long Long64_t
Definition RtypesCore.h:80
constexpr Bool_t kTRUE
Definition RtypesCore.h:100
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
#define gROOT
Definition TROOT.h:405
R__EXTERN TSystem * gSystem
Definition TSystem.h:560
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition TFile.h:51
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4053
1-D histogram with a float per channel (see TH1 documentation)}
Definition TH1.h:577
The Reader class serves to use the MVAs in a specific analysis context.
Definition Reader.h:64
Double_t EvaluateMVA(const std::vector< Float_t > &, const TString &methodTag, Double_t aux=0)
Evaluate a std::vector<float> of input data for a given method The parameter aux is obligatory for th...
Definition Reader.cxx:468
IMethod * BookMVA(const TString &methodTag, const TString &weightfile)
read method name from weight file
Definition Reader.cxx:368
void AddSpectator(const TString &expression, Float_t *)
Add a float spectator or expression to the reader.
Definition Reader.cxx:321
void AddVariable(const TString &expression, Float_t *)
Add a float variable or expression to the reader.
Definition Reader.cxx:303
Stopwatch class.
Definition TStopwatch.h:28
void Start(Bool_t reset=kTRUE)
Start the stopwatch.
void Stop()
Stop the stopwatch.
void Print(Option_t *option="") const override
Print the real and cpu time passed between the start and stop events.
Basic string class.
Definition TString.h:139
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1299
virtual TString GetDirName(const char *pathname)
Return the directory name in pathname.
Definition TSystem.cxx:1035
A TTree represents a columnar dataset.
Definition TTree.h:79
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
Read all branches of entry and return total number of bytes read.
Definition TTree.cxx:5629
virtual Int_t SetBranchAddress(const char *bname, void *add, TBranch **ptr=nullptr)
Change branch address, dealing with clone trees properly.
Definition TTree.cxx:8371
virtual Long64_t GetEntries() const
Definition TTree.h:460
Author
Andreas Hoecker

Definition in file TMVAClassificationCategoryApplication.C.