{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "
\n", " \n", "
\n", "
\n", " \n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# JsMVA: Input variable plots\n", "
" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [ { "data": { "application/javascript": [ "\n", "require(['notebook'],\n", " function() {\n", " IPython.CodeCell.config_defaults.highlight_modes['magic_text/x-c++src'] = {'reg':[/^%%cpp/]};\n", " console.log(\"JupyROOT - %%cpp magic configured\");\n", " }\n", ");\n" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Welcome to JupyROOT 6.07/07\n" ] } ], "source": [ "import ROOT\n", "from ROOT import TFile, TMVA, TCut" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Enable JS visualization" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsmva on" ] }, { "cell_type": "markdown", "metadata": { "collapsed": false }, "source": [ "## Declarations, building training and testing trees " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "For more details please see this notebook." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
DataSetInfo
Dataset: tmva_class_exampleAdded class \"Signal\"
Add Tree TreeS of type Signal with 6000 events
DataSetInfo
Dataset: tmva_class_exampleAdded class \"Background\"
Add Tree TreeB of type Background with 6000 events
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "SysError in : could not delete TMVA.root (errno: 26) (Text file busy)\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "outputFile = TFile( \"TMVA.root\", 'RECREATE' )\n", "\n", "TMVA.Tools.Instance()\n", "\n", "factory = TMVA.Factory(JobName=\"TMVAClassification\", TargetFile=outputFile,\n", " V=False, Color=True, DrawProgressBar=True, Transformations=[\"I\", \"D\", \"P\", \"G\",\"D\"],\n", " AnalysisType=\"Classification\")\n", "\n", "dataset = \"tmva_class_example\"\n", "loader = TMVA.DataLoader(dataset)\n", "\n", "loader.AddVariable( \"myvar1 := var1+var2\", 'F' )\n", "loader.AddVariable( \"myvar2 := var1-var2\", \"Expression 2\", 'F' )\n", "loader.AddVariable( \"var3\", \"Variable 3\", 'F' )\n", "loader.AddVariable( \"var4\", \"Variable 4\", 'F' )\n", "\n", "loader.AddSpectator( \"spec1:=var1*2\", \"Spectator 1\", 'F' )\n", "loader.AddSpectator( \"spec2:=var1*3\", \"Spectator 2\", 'F' )\n", "\n", "if ROOT.gSystem.AccessPathName( \"./tmva_class_example.root\" ) != 0: \n", " ROOT.gSystem.Exec( \"wget https://root.cern.ch/files/tmva_class_example.root\")\n", " \n", "input = TFile.Open( \"./tmva_class_example.root\" )\n", "\n", "# Get the signal and background trees for training\n", "signal = input.Get( \"TreeS\" )\n", "background = input.Get( \"TreeB\" )\n", " \n", "# Global event weights (see below for setting event-wise weights)\n", "signalWeight = 1.0\n", "backgroundWeight = 1.0\n", "\n", "mycuts = TCut(\"\")\n", "mycutb = TCut(\"\")\n", "\n", "loader.AddSignalTree(signal, signalWeight)\n", "loader.AddBackgroundTree(background, backgroundWeight)\n", "loader.fSignalWeight = signalWeight\n", "loader.fBackgroundWeight = backgroundWeight\n", "loader.fTreeS = signal\n", "loader.fTreeB = background\n", "\n", "loader.PrepareTrainingAndTestTree(SigCut=mycuts, BkgCut=mycutb,\n", " nTrain_Signal=0, nTrain_Background=0, SplitMode=\"Random\", NormMode=\"NumEvents\", V=False)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Visualizing input variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we want to see a histogram of one of the variables readed from root file and passed to DataLoader, we have to invoke DataLoader.DrawInputVariable method. This method is available just in notebook, it's inseeted to DataLoader by using %jsmva on magic. The arguments of this function:\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
KeywordCan be used as positional argumentDefaultDescription
variableNameyes, 1.-The name of variable we want to visualize
numBinyes, 2.100Number of bins used for creating the histogram
processTrfsyes, 3.\"\"List of transformations on input variables. Possible transformations: I (identity), N (normalize), D (decorrelation), P (PCA), U (uniform), G (Gaussian)
" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
DataSetFactory
Dataset: tmva_class_exampleNumber of events in input trees
Number of training and testing events
Signaltraining events3000
testing events3000
training and testing events6000
Backgroundtraining events3000
testing events3000
training and testing events6000
DataSetInfo Correlation matrix (Signal)
DataSetInfo Correlation matrix (Background)
DataSetFactory
Dataset: tmva_class_example
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "loader.DrawInputVariable(\"myvar1\")" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
[tmva_class_example]Create Transformation \"G\" with events from all classes.
Transformation, Variable selection :
Input : variable 'myvar1' <---> Output : variable 'myvar1'
Input : variable 'myvar2' <---> Output : variable 'myvar2'
Input : variable 'var3' <---> Output : variable 'var3'
Input : variable 'var4' <---> Output : variable 'var4'
[tmva_class_example]Create Transformation \"D\" with events from all classes.
Transformation, Variable selection :
Input : variable 'myvar1' <---> Output : variable 'myvar1'
Input : variable 'myvar2' <---> Output : variable 'myvar2'
Input : variable 'var3' <---> Output : variable 'var3'
Input : variable 'var4' <---> Output : variable 'var4'
Preparing the Gaussian transformation...
TFHandler_DataLoader
VariableMeanRMSMinMax
myvar10.00983110.99805-3.03395.7307
myvar20.00914570.99571-3.03385.7307
var30.0104741.0017-3.03375.7307
var40.0100320.99858-3.03385.7307
Preparing the Decorrelation transformation...
TFHandler_DataLoader
VariableMeanRMSMinMax
myvar10.00535821.0000-4.435211.946
myvar20.00883371.0000-3.05565.8934
var30.00856891.0000-3.779010.607
var40.00349711.0000-4.188513.372
[tmva_class_example]Create Transformation \"G\" with events from all classes.
Transformation, Variable selection :
Input : variable 'myvar1' <---> Output : variable 'myvar1'
Input : variable 'myvar2' <---> Output : variable 'myvar2'
Input : variable 'var3' <---> Output : variable 'var3'
Input : variable 'var4' <---> Output : variable 'var4'
[tmva_class_example]Create Transformation \"D\" with events from all classes.
Transformation, Variable selection :
Input : variable 'myvar1' <---> Output : variable 'myvar1'
Input : variable 'myvar2' <---> Output : variable 'myvar2'
Input : variable 'var3' <---> Output : variable 'var3'
Input : variable 'var4' <---> Output : variable 'var4'
Preparing the Gaussian transformation...
TFHandler_DataLoader
VariableMeanRMSMinMax
myvar10.00983110.99805-3.03395.7307
myvar20.00914570.99571-3.03385.7307
var30.0104741.0017-3.03375.7307
var40.0100320.99858-3.03385.7307
Preparing the Decorrelation transformation...
TFHandler_DataLoader
VariableMeanRMSMinMax
myvar10.00535821.0000-4.435211.946
myvar20.00883371.0000-3.05565.8934
var30.00856891.0000-3.779010.607
var40.00349711.0000-4.188513.372
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "loader.DrawInputVariable(\"myvar1\", processTrfs=[\"G\", \"D\"])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Correlation matrix of input variables" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To visualize the correlation between input variables we can draw a 2D histogram of correlation matrix. This can be done by using DataLoader.DrawCorrelationMatrix function (inserted to DataLoader by %jsmva on):\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
KeywordCan be used as positional argumentDefaultDescription
classNameyes, 1.-Selects in which tree we want to draw the correlation matrix (Signal/Background)
" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "scrolled": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "loader.DrawCorrelationMatrix(\"Signal\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Close the factory's output file" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [], "source": [ "outputFile.Close()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python [Root]", "language": "python", "name": "Python [Root]" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 2 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython2", "version": "2.7.11" } }, "nbformat": 4, "nbformat_minor": 0 }