{ "cells": [ { "cell_type": "markdown", "id": "f8662758", "metadata": {}, "source": [ "# classification\n", "\n", "\n", "**Author:** \n", "This notebook tutorial was automatically generated with ROOTBOOK-izer from the macro found in the ROOT repository on Tuesday, May 19, 2026 at 08:20 PM." ] }, { "cell_type": "markdown", "id": "be2ac681", "metadata": {}, "source": [ " Arguments are defined. " ] }, { "cell_type": "code", "execution_count": 1, "id": "3b2198be", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:43.355851Z", "iopub.status.busy": "2026-05-19T20:20:43.355744Z", "iopub.status.idle": "2026-05-19T20:20:43.672009Z", "shell.execute_reply": "2026-05-19T20:20:43.671349Z" } }, "outputs": [], "source": [ "UInt_t jobs = 4;" ] }, { "cell_type": "code", "execution_count": 2, "id": "cbb80eec", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:43.674214Z", "iopub.status.busy": "2026-05-19T20:20:43.674095Z", "iopub.status.idle": "2026-05-19T20:20:43.876429Z", "shell.execute_reply": "2026-05-19T20:20:43.875722Z" } }, "outputs": [], "source": [ "TMVA::Tools::Instance();\n", "\n", "TFile *input(nullptr);\n", "TString fname = gROOT->GetTutorialDir() + \"/machine_learning/data/tmva_class_example.root\";\n", "if (!gSystem->AccessPathName(fname)) {\n", " input = TFile::Open(fname); // check if file in local directory exists\n", "}\n", "if (!input) {\n", " std::cout << \"ERROR: could not open data file\" << fname << std::endl;\n", " exit(1);\n", "}" ] }, { "cell_type": "markdown", "id": "cdc26808", "metadata": {}, "source": [ "Register the training and test trees" ] }, { "cell_type": "code", "execution_count": 3, "id": "0d1349b1", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:43.878520Z", "iopub.status.busy": "2026-05-19T20:20:43.878407Z", "iopub.status.idle": "2026-05-19T20:20:44.082448Z", "shell.execute_reply": "2026-05-19T20:20:44.081784Z" } }, "outputs": [], "source": [ "TTree *signalTree = (TTree *)input->Get(\"TreeS\");\n", "TTree *background = (TTree *)input->Get(\"TreeB\");\n", "\n", "TMVA::DataLoader *dataloader = new TMVA::DataLoader(\"dataset\");" ] }, { "cell_type": "markdown", "id": "fc72a359", "metadata": {}, "source": [ "If you wish to modify default settings\n", "(please check \"src/Config.h\" to see all available global options)\n", "\n", "(TMVA::gConfig().GetVariablePlotting()).fTimesRMS = 8.0;\n", "(TMVA::gConfig().GetIONames()).fWeightFileDir = \"myWeightDirectory\";" ] }, { "cell_type": "markdown", "id": "2adc4e69", "metadata": {}, "source": [ "Define the input variables that shall be used for the MVA training\n", "note that you may also use variable expressions, such as: \"3*var1/var2*abs(var3)\"\n", "[all types of expressions that can also be parsed by TTree::Draw( \"expression\" )]" ] }, { "cell_type": "code", "execution_count": 4, "id": "f3a12b59", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:44.084477Z", "iopub.status.busy": "2026-05-19T20:20:44.084361Z", "iopub.status.idle": "2026-05-19T20:20:44.288534Z", "shell.execute_reply": "2026-05-19T20:20:44.287907Z" } }, "outputs": [], "source": [ "dataloader->AddVariable(\"myvar1 := var1+var2\", 'F');\n", "dataloader->AddVariable(\"myvar2 := var1-var2\", \"Expression 2\", \"\", 'F');\n", "dataloader->AddVariable(\"var3\", \"Variable 3\", \"units\", 'F');\n", "dataloader->AddVariable(\"var4\", \"Variable 4\", \"units\", 'F');" ] }, { "cell_type": "markdown", "id": "3d7af021", "metadata": {}, "source": [ "You can add so-called \"Spectator variables\", which are not used in the MVA training,\n", "but will appear in the final \"TestTree\" produced by TMVA. This TestTree will contain the\n", "input variables, the response values of all trained MVAs, and the spectator variables" ] }, { "cell_type": "code", "execution_count": 5, "id": "04ed5ca5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:44.290753Z", "iopub.status.busy": "2026-05-19T20:20:44.290636Z", "iopub.status.idle": "2026-05-19T20:20:44.494691Z", "shell.execute_reply": "2026-05-19T20:20:44.494041Z" } }, "outputs": [], "source": [ "dataloader->AddSpectator(\"spec1 := var1*2\", \"Spectator 1\", \"units\", 'F');\n", "dataloader->AddSpectator(\"spec2 := var1*3\", \"Spectator 2\", \"units\", 'F');" ] }, { "cell_type": "markdown", "id": "20e4d4c1", "metadata": {}, "source": [ "global event weights per tree (see below for setting event-wise weights)" ] }, { "cell_type": "code", "execution_count": 6, "id": "88b53ed5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:44.496908Z", "iopub.status.busy": "2026-05-19T20:20:44.496790Z", "iopub.status.idle": "2026-05-19T20:20:44.702270Z", "shell.execute_reply": "2026-05-19T20:20:44.701602Z" } }, "outputs": [], "source": [ "Double_t signalWeight = 1.0;\n", "Double_t backgroundWeight = 1.0;" ] }, { "cell_type": "markdown", "id": "77309002", "metadata": {}, "source": [ "You can add an arbitrary number of signal or background trees" ] }, { "cell_type": "code", "execution_count": 7, "id": "8bac93d5", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:44.704316Z", "iopub.status.busy": "2026-05-19T20:20:44.704199Z", "iopub.status.idle": "2026-05-19T20:20:44.910021Z", "shell.execute_reply": "2026-05-19T20:20:44.909462Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "DataSetInfo : [dataset] : Added class \"Signal\"\n", " : Add Tree TreeS of type Signal with 6000 events\n", "DataSetInfo : [dataset] : Added class \"Background\"\n", " : Add Tree TreeB of type Background with 6000 events\n" ] } ], "source": [ "dataloader->AddSignalTree(signalTree, signalWeight);\n", "dataloader->AddBackgroundTree(background, backgroundWeight);" ] }, { "cell_type": "markdown", "id": "c65913e6", "metadata": {}, "source": [ "Set individual event weights (the variables must exist in the original TTree)\n", "- for signal : `dataloader->SetSignalWeightExpression (\"weight1*weight2\");`\n", "- for background: `dataloader->SetBackgroundWeightExpression(\"weight1*weight2\");`" ] }, { "cell_type": "code", "execution_count": 8, "id": "f4eaad7c", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:20:44.911764Z", "iopub.status.busy": "2026-05-19T20:20:44.911634Z", "iopub.status.idle": "2026-05-19T20:20:47.041511Z", "shell.execute_reply": "2026-05-19T20:20:47.040886Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "