{ "cells": [ { "cell_type": "markdown", "id": "f853420e", "metadata": {}, "source": [ "# rf401_importttreethx\n", "Data and categories: advanced options for importing data from ROOT TTree and THx histograms\n", "\n", "Basic import options are demonstrated in rf102_dataimport.C\n", "\n", "\n", "\n", "\n", "**Author:** Wouter Verkerke \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:31 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "b6f678d8", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:29.688864Z", "iopub.status.busy": "2026-05-19T20:31:29.688754Z", "iopub.status.idle": "2026-05-19T20:31:29.703158Z", "shell.execute_reply": "2026-05-19T20:31:29.702688Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "#include \"RooRealVar.h\"\n", "#include \"RooDataSet.h\"\n", "#include \"RooDataHist.h\"\n", "#include \"RooCategory.h\"\n", "#include \"RooGaussian.h\"\n", "#include \"TCanvas.h\"\n", "#include \"TAxis.h\"\n", "#include \"RooPlot.h\"\n", "#include \"TH1.h\"\n", "#include \"TTree.h\"\n", "#include \"TRandom.h\"\n", "#include \n", "\n", "using namespace RooFit;\n", "\n", "TH1 *makeTH1(TRandom &trnd, const char *name, double mean, double sigma);\n", "TTree *makeTTree(TRandom &trnd);" ] }, { "cell_type": "markdown", "id": "6853e890", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 2, "id": "f9a614c4", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:29.704507Z", "iopub.status.busy": "2026-05-19T20:31:29.704393Z", "iopub.status.idle": "2026-05-19T20:31:29.711448Z", "shell.execute_reply": "2026-05-19T20:31:29.711008Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "TH1 *makeTH1(TRandom &trnd, const char *name, double mean, double sigma)\n", "{\n", " // Create ROOT TH1 filled with a Gaussian distribution\n", "\n", " TH1D *hh = new TH1D(name, name, 100, -10, 10);\n", " for (int i = 0; i < 1000; i++) {\n", " hh->Fill(trnd.Gaus(mean, sigma));\n", " }\n", " return hh;\n", "}" ] }, { "cell_type": "markdown", "id": "fcebf732", "metadata": {}, "source": [ " Definition of a helper function: " ] }, { "cell_type": "code", "execution_count": 3, "id": "508b28f7", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:29.712722Z", "iopub.status.busy": "2026-05-19T20:31:29.712584Z", "iopub.status.idle": "2026-05-19T20:31:29.721424Z", "shell.execute_reply": "2026-05-19T20:31:29.720970Z" } }, "outputs": [], "source": [ "%%cpp -d\n", "TTree *makeTTree(TRandom &trnd)\n", "{\n", " // Create ROOT TTree filled with a Gaussian distribution in x and a uniform distribution in y\n", "\n", " TTree *tree = new TTree(\"tree\", \"tree\");\n", " double *px = new double;\n", " double *py = new double;\n", " double *pz = new double;\n", " Int_t *pi = new Int_t;\n", " tree->Branch(\"x\", px, \"x/D\");\n", " tree->Branch(\"y\", py, \"y/D\");\n", " tree->Branch(\"z\", pz, \"z/D\");\n", " tree->Branch(\"i\", pi, \"i/I\");\n", " for (int i = 0; i < 100; i++) {\n", " *px = trnd.Gaus(0, 3);\n", " *py = trnd.Uniform() * 30 - 15;\n", " *pz = trnd.Gaus(0, 5);\n", " *pi = i % 3;\n", " tree->Fill();\n", " }\n", " return tree;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "id": "97f96456", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:29.722847Z", "iopub.status.busy": "2026-05-19T20:31:29.722729Z", "iopub.status.idle": "2026-05-19T20:31:30.038899Z", "shell.execute_reply": "2026-05-19T20:31:30.038313Z" } }, "outputs": [], "source": [ "TRandom3 trnd{};" ] }, { "cell_type": "markdown", "id": "e232d325", "metadata": {}, "source": [ "Import multiple TH1 into a RooDataHist\n", "--------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "91d878ec", "metadata": {}, "source": [ "Create thee ROOT TH1 histograms" ] }, { "cell_type": "code", "execution_count": 5, "id": "fc153e0f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:30.040499Z", "iopub.status.busy": "2026-05-19T20:31:30.040380Z", "iopub.status.idle": "2026-05-19T20:31:30.248663Z", "shell.execute_reply": "2026-05-19T20:31:30.248013Z" } }, "outputs": [], "source": [ "TH1 *hh_1 = makeTH1(trnd, \"hh1\", 0, 3);\n", "TH1 *hh_2 = makeTH1(trnd, \"hh2\", -3, 1);\n", "TH1 *hh_3 = makeTH1(trnd, \"hh3\", +3, 4);" ] }, { "cell_type": "markdown", "id": "5d4fcf0b", "metadata": {}, "source": [ "Declare observable x" ] }, { "cell_type": "code", "execution_count": 6, "id": "4fdc9813", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:30.250408Z", "iopub.status.busy": "2026-05-19T20:31:30.250289Z", "iopub.status.idle": "2026-05-19T20:31:30.458388Z", "shell.execute_reply": "2026-05-19T20:31:30.457790Z" } }, "outputs": [], "source": [ "RooRealVar x(\"x\", \"x\", -10, 10);" ] }, { "cell_type": "markdown", "id": "4460b041", "metadata": {}, "source": [ "Create category observable c that serves as index for the ROOT histograms" ] }, { "cell_type": "code", "execution_count": 7, "id": "af037aa9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:30.460006Z", "iopub.status.busy": "2026-05-19T20:31:30.459890Z", "iopub.status.idle": "2026-05-19T20:31:30.665386Z", "shell.execute_reply": "2026-05-19T20:31:30.664771Z" } }, "outputs": [], "source": [ "RooCategory c(\"c\", \"c\", {{\"SampleA\",0}, {\"SampleB\",1}, {\"SampleC\",2}});" ] }, { "cell_type": "markdown", "id": "d3cf4502", "metadata": {}, "source": [ "Create a binned dataset that imports contents of all TH1 mapped by index category c" ] }, { "cell_type": "code", "execution_count": 8, "id": "1154f229", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:30.667002Z", "iopub.status.busy": "2026-05-19T20:31:30.666886Z", "iopub.status.idle": "2026-05-19T20:31:30.891676Z", "shell.execute_reply": "2026-05-19T20:31:30.890485Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- RooDataHist::ctor(dh) WARNING: argument ImportDataSlice is duplicated\n", "[#0] WARNING:InputArguments -- RooDataHist::ctor(dh) WARNING: argument ImportDataSlice is duplicated\n", "RooDataHist::dh[c,x] = 300 bins (2964 weights)\n" ] } ], "source": [ "RooDataHist *dh = new RooDataHist(\"dh\", \"dh\", x, Index(c), Import(\"SampleA\", *hh_1), Import(\"SampleB\", *hh_2),\n", " Import(\"SampleC\", *hh_3));\n", "dh->Print();" ] }, { "cell_type": "markdown", "id": "a1334bcc", "metadata": {}, "source": [ "Alternative constructor form for importing multiple histograms" ] }, { "cell_type": "code", "execution_count": 9, "id": "9c3b3dc9", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:30.893163Z", "iopub.status.busy": "2026-05-19T20:31:30.893019Z", "iopub.status.idle": "2026-05-19T20:31:31.208119Z", "shell.execute_reply": "2026-05-19T20:31:31.207715Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "RooDataHist::dh[c,x] = 300 bins (2964 weights)\n" ] } ], "source": [ "std::map hmap;\n", "hmap[\"SampleA\"] = hh_1;\n", "hmap[\"SampleB\"] = hh_2;\n", "hmap[\"SampleC\"] = hh_3;\n", "RooDataHist *dh2 = new RooDataHist(\"dh\", \"dh\", x, c, hmap);\n", "dh2->Print();" ] }, { "cell_type": "markdown", "id": "fc020a40", "metadata": {}, "source": [ "Importing a TTree into a RooDataSet with cuts\n", "-----------------------------------------------------------------------------------------" ] }, { "cell_type": "code", "execution_count": 10, "id": "798e9e35", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:31.209404Z", "iopub.status.busy": "2026-05-19T20:31:31.209292Z", "iopub.status.idle": "2026-05-19T20:31:31.417084Z", "shell.execute_reply": "2026-05-19T20:31:31.416562Z" } }, "outputs": [], "source": [ "TTree *tree = makeTTree(trnd);" ] }, { "cell_type": "markdown", "id": "854ecd1c", "metadata": {}, "source": [ "Define observables y,z" ] }, { "cell_type": "code", "execution_count": 11, "id": "f6d192e2", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:31.418684Z", "iopub.status.busy": "2026-05-19T20:31:31.418547Z", "iopub.status.idle": "2026-05-19T20:31:31.626294Z", "shell.execute_reply": "2026-05-19T20:31:31.625764Z" } }, "outputs": [], "source": [ "RooRealVar y(\"y\", \"y\", -10, 10);\n", "RooRealVar z(\"z\", \"z\", -10, 10);" ] }, { "cell_type": "markdown", "id": "bf231efe", "metadata": {}, "source": [ "Import only observables (y,z)" ] }, { "cell_type": "code", "execution_count": 12, "id": "75140ab6", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:31.627989Z", "iopub.status.busy": "2026-05-19T20:31:31.627878Z", "iopub.status.idle": "2026-05-19T20:31:31.837104Z", "shell.execute_reply": "2026-05-19T20:31:31.836684Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #7 because y cannot accommodate the value 13.3845\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #8 because y cannot accommodate the value 11.1861\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #12 because y cannot accommodate the value 13.7009\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping event #14 because y cannot accommodate the value -10.6852\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds) Skipping ...\n", "[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds) Ignored 35 out-of-range events\n", "RooDataSet::ds[x,y] = 65 entries\n" ] } ], "source": [ "RooDataSet ds(\"ds\", \"ds\", RooArgSet(x, y), Import(*tree));\n", "ds.Print();" ] }, { "cell_type": "markdown", "id": "f4b6d498", "metadata": {}, "source": [ "Import observables (x,y,z) but only event for which (y+z<0) is true" ] }, { "cell_type": "code", "execution_count": 13, "id": "1152bb57", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:31.838340Z", "iopub.status.busy": "2026-05-19T20:31:31.838230Z", "iopub.status.idle": "2026-05-19T20:31:32.046510Z", "shell.execute_reply": "2026-05-19T20:31:32.046112Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #7 because y cannot accommodate the value 13.3845\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #8 because y cannot accommodate the value 11.1861\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #12 because y cannot accommodate the value 13.7009\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping event #14 because y cannot accommodate the value -10.6852\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds2) Skipping ...\n", "[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds2) Ignored 36 out-of-range events\n", "RooDataSet::ds2[x,y,z] = 26 entries\n" ] } ], "source": [ "RooDataSet ds2(\"ds2\", \"ds2\", RooArgSet(x, y, z), Import(*tree), Cut(\"y+z<0\"));\n", "ds2.Print();" ] }, { "cell_type": "markdown", "id": "fa68c25c", "metadata": {}, "source": [ "Importing integer TTree branches\n", "---------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "45a7281b", "metadata": {}, "source": [ "Import integer tree branch as RooRealVar" ] }, { "cell_type": "code", "execution_count": 14, "id": "de743a08", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:32.047799Z", "iopub.status.busy": "2026-05-19T20:31:32.047692Z", "iopub.status.idle": "2026-05-19T20:31:32.255516Z", "shell.execute_reply": "2026-05-19T20:31:32.255110Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:DataHandling -- RooAbsReal::attachToTree(i) TTree Int_t branch i will be converted to double precision.\n", "RooDataSet::ds3[i,x] = 100 entries\n" ] } ], "source": [ "RooRealVar i(\"i\", \"i\", 0, 5);\n", "RooDataSet ds3(\"ds3\", \"ds3\", RooArgSet(i, x), Import(*tree));\n", "ds3.Print();" ] }, { "cell_type": "markdown", "id": "896a7475", "metadata": {}, "source": [ "Define category i" ] }, { "cell_type": "code", "execution_count": 15, "id": "960a104e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:32.256759Z", "iopub.status.busy": "2026-05-19T20:31:32.256649Z", "iopub.status.idle": "2026-05-19T20:31:32.464380Z", "shell.execute_reply": "2026-05-19T20:31:32.463870Z" } }, "outputs": [], "source": [ "RooCategory icat(\"i\", \"i\");\n", "icat.defineType(\"State0\", 0);\n", "icat.defineType(\"State1\", 1);" ] }, { "cell_type": "markdown", "id": "b160b355", "metadata": {}, "source": [ "Import integer tree branch as RooCategory (only events with i==0 and i==1\n", "will be imported as those are the only defined states)" ] }, { "cell_type": "code", "execution_count": 16, "id": "e9321924", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:32.465935Z", "iopub.status.busy": "2026-05-19T20:31:32.465826Z", "iopub.status.idle": "2026-05-19T20:31:32.674190Z", "shell.execute_reply": "2026-05-19T20:31:32.673783Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #2 because i cannot accommodate the value 0\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #5 because i cannot accommodate the value 0\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #8 because i cannot accommodate the value 0\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping event #11 because i cannot accommodate the value 0\n", "[#1] INFO:DataHandling -- RooTreeDataStore::loadValues(ds4) Skipping ...\n", "[#0] WARNING:DataHandling -- RooTreeDataStore::loadValues(ds4) Ignored 33 out-of-range events\n", "RooDataSet::ds4[i,x] = 67 entries\n" ] } ], "source": [ "RooDataSet ds4(\"ds4\", \"ds4\", RooArgSet(icat, x), Import(*tree));\n", "ds4.Print();" ] }, { "cell_type": "markdown", "id": "9904fc66", "metadata": {}, "source": [ "Import multiple RooDataSets into a RooDataSet\n", "----------------------------------------------------------------------------------------" ] }, { "cell_type": "markdown", "id": "980a1bfe", "metadata": {}, "source": [ "Create three RooDataSets in (y,z)" ] }, { "cell_type": "code", "execution_count": 17, "id": "baf78c92", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:32.675666Z", "iopub.status.busy": "2026-05-19T20:31:32.675538Z", "iopub.status.idle": "2026-05-19T20:31:32.880651Z", "shell.execute_reply": "2026-05-19T20:31:32.880139Z" } }, "outputs": [], "source": [ "std::unique_ptr dsA{ds2.reduce({x, y}, \"z<-5\")};\n", "std::unique_ptr dsB{ds2.reduce({x, y}, \"abs(z)<5\")};\n", "std::unique_ptr dsC{ds2.reduce({x, y}, \"z>5\")};" ] }, { "cell_type": "markdown", "id": "5593a6fb", "metadata": {}, "source": [ "Create a dataset that imports contents of all the above datasets mapped by index category c" ] }, { "cell_type": "code", "execution_count": 18, "id": "216e625e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:31:32.883397Z", "iopub.status.busy": "2026-05-19T20:31:32.883280Z", "iopub.status.idle": "2026-05-19T20:31:33.092002Z", "shell.execute_reply": "2026-05-19T20:31:33.091314Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[#0] WARNING:InputArguments -- RooDataSet::ctor(dsABC) WARNING: argument ImportDataSlice is duplicated\n", "[#0] WARNING:InputArguments -- RooDataSet::ctor(dsABC) WARNING: argument ImportDataSlice is duplicated\n", "RooDataSet::dsABC[x,y,c] = 26 entries\n" ] } ], "source": [ "RooDataSet dsABC{\"dsABC\", \"dsABC\", RooArgSet(x, y), Index(c), Import(\"SampleA\", *dsA),\n", " Import(\"SampleB\", *dsB), Import(\"SampleC\", *dsC)};\n", "\n", "dsABC.Print();" ] } ], "metadata": { "kernelspec": { "display_name": "ROOT C++", "language": "c++", "name": "root" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".C", "mimetype": " text/x-c++src", "name": "c++" } }, "nbformat": 4, "nbformat_minor": 5 }