{
"cells": [
{
"cell_type": "markdown",
"id": "291fe7af",
"metadata": {},
"source": [
"# tree120_ntuple\n",
"Simple tree analysis.\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Rene Brun \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:18 PM."
]
},
{
"cell_type": "markdown",
"id": "6356822e",
"metadata": {},
"source": [
"just in case this script is executed multiple times"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e13bbb17",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"delete gROOT->GetListOfFiles()->FindObject(\"hsimple.root\");\n",
"delete gROOT->GetListOfCanvases()->FindObject(\"c1\");\n",
"\n",
"gBenchmark->Start(\"ntuple1\");"
]
},
{
"cell_type": "markdown",
"id": "ed24a615",
"metadata": {},
"source": [
"Connect ROOT histogram/ntuple demonstration file\n",
"generated by example $ROOTSYS/tutorials/hsimple.C."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "14c9a1b5",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TString dir = gROOT->GetTutorialDir();\n",
"dir.Append(\"/hsimple.C\");\n",
"dir.ReplaceAll(\"/./\",\"/\");\n",
"if (gBenchmark->GetBench(\"hsimple\") < 0)\n",
" gInterpreter->LoadMacro(dir.Data());\n",
"TFile *f1 = (TFile*)gROOT->ProcessLineFast(\"hsimple(1)\");\n",
"if (!f1)\n",
" return;"
]
},
{
"cell_type": "markdown",
"id": "e13247f8",
"metadata": {},
"source": [
"Create a canvas, with 4 pads"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "bf52a989",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"auto c1 = new TCanvas(\"c1\",\"The Ntuple canvas\",200,10,700,780);\n",
"auto pad1 = new TPad(\"pad1\",\"This is pad1\",0.02,0.52,0.48,0.98,21);\n",
"auto pad2 = new TPad(\"pad2\",\"This is pad2\",0.52,0.52,0.98,0.98,21);\n",
"auto pad3 = new TPad(\"pad3\",\"This is pad3\",0.02,0.02,0.48,0.48,21);\n",
"auto pad4 = new TPad(\"pad4\",\"This is pad4\",0.52,0.02,0.98,0.48,1);\n",
"pad1->Draw();\n",
"pad2->Draw();\n",
"pad3->Draw();\n",
"pad4->Draw();"
]
},
{
"cell_type": "markdown",
"id": "e8e24d27",
"metadata": {},
"source": [
"Change default style for the statistics box"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d565ad6",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gStyle->SetStatW(0.30);\n",
"gStyle->SetStatH(0.20);"
]
},
{
"cell_type": "markdown",
"id": "e0acf7d1",
"metadata": {},
"source": [
"Display a function of one ntuple column imposing a condition\n",
"on another column."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c823c096",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pad1->cd();\n",
"pad1->SetGrid();\n",
"pad1->SetLogy();\n",
"auto ntuple = f1->Get(\"ntuple\");\n",
"ntuple->SetLineColor(1);\n",
"ntuple->SetFillStyle(1001);\n",
"ntuple->SetFillColor(45);\n",
"ntuple->Draw(\"3*px+2\",\"px**2+py**2>1\");\n",
"ntuple->SetFillColor(38);\n",
"ntuple->Draw(\"2*px+2\",\"pz>2\",\"same\");\n",
"ntuple->SetFillColor(5);\n",
"ntuple->Draw(\"1.3*px+2\",\"(px^2+py^2>4) && py>0\",\"same\");\n",
"pad1->RedrawAxis();"
]
},
{
"cell_type": "markdown",
"id": "7b33376e",
"metadata": {},
"source": [
"Display the profile of two columns\n",
"The profile histogram produced is saved in the current directory with\n",
"the name hprofs"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d3f612e",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pad2->cd();\n",
"pad2->SetGrid();\n",
"ntuple->Draw(\"pz:px>>hprofs\",\"\",\"goffprofs\");\n",
"TProfile *hprofs = (TProfile*)gDirectory->Get(\"hprofs\");\n",
"hprofs->SetMarkerColor(5);\n",
"hprofs->SetMarkerSize(0.7);\n",
"hprofs->SetMarkerStyle(21);\n",
"hprofs->Fit(\"pol2\");"
]
},
{
"cell_type": "markdown",
"id": "0ae34000",
"metadata": {},
"source": [
"Get pointer to fitted function and modify its attributes"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "eed92f0f",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TF1 *fpol2 = hprofs->GetFunction(\"pol2\");\n",
"fpol2->SetLineWidth(4);\n",
"fpol2->SetLineColor(2);"
]
},
{
"cell_type": "markdown",
"id": "a340bf5f",
"metadata": {},
"source": [
"Display a scatter plot of two columns with a selection.\n",
"Superimpose the result of another cut with a different marker color"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0663c630",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pad3->cd();\n",
"pad3->GetFrame()->SetBorderSize(8);\n",
"ntuple->SetMarkerColor(1);\n",
"ntuple->Draw(\"py:px\",\"pz>1\");\n",
"ntuple->SetMarkerColor(2);\n",
"ntuple->Draw(\"py:px\",\"pz<1\",\"same\");"
]
},
{
"cell_type": "markdown",
"id": "0b7cac5f",
"metadata": {},
"source": [
"Display a 3-D scatter plot of 3 columns. Superimpose a different selection."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5ee7fcb",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"pad4->cd();\n",
"ntuple->Draw(\"pz:py:px\",\"(pz<10 && pz>6)+(pz<4 && pz>3)\");\n",
"ntuple->SetMarkerColor(4);\n",
"ntuple->Draw(\"pz:py:px\",\"pz<6 && pz>4\",\"same\");\n",
"ntuple->SetMarkerColor(5);\n",
"ntuple->Draw(\"pz:py:px\",\"pz<4 && pz>3\",\"same\");\n",
"auto l4 = new TPaveText(-0.9,0.5,0.9,0.95);\n",
"l4->SetFillColor(42);\n",
"l4->SetTextAlign(12);\n",
"l4->AddText(\"You can interactively rotate this view in 2 ways:\");\n",
"l4->AddText(\" - With the RotateCube in clicking in this pad\");\n",
"l4->AddText(\" - Selecting View with x3d in the View menu\");\n",
"l4->Draw();"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ed442dc",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"c1->cd();\n",
"c1->Update();\n",
"gStyle->SetStatColor(19);\n",
"gBenchmark->Show(\"ntuple1\");"
]
},
{
"cell_type": "markdown",
"id": "b42400c2",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d9aa9686",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"gROOT->GetListOfCanvases()->Draw()"
]
}
],
"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
}