{
"cells": [
{
"cell_type": "markdown",
"id": "74669f32",
"metadata": {},
"source": [
"# gr010_approx_smooth\n",
"\n",
"See the [TGraphSmooth documentation](https://root.cern/doc/master/classTGraphSmooth.html)\n",
"\n",
"\n",
"\n",
"**Author:** Christian Stratowa, Vienna, Austria. \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:37 PM."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f5d293ea",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TCanvas *vC1;\n",
"TGraph *grxy, *grin, *grout;"
]
},
{
"cell_type": "markdown",
"id": "cacb6b06",
"metadata": {},
"source": [
" Definition of a helper function: "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1da27dc4",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%%cpp -d\n",
"void DrawSmooth(Int_t pad, const char *title, const char *xt, const char *yt)\n",
"{\n",
" vC1->cd(pad);\n",
" TH1F *vFrame = gPad->DrawFrame(0,0,15,150);\n",
" vFrame->SetTitle(title);\n",
" vFrame->SetTitleSize(0.2);\n",
" vFrame->SetXTitle(xt);\n",
" vFrame->SetYTitle(yt);\n",
" grxy->SetMarkerColor(kBlue);\n",
" grxy->SetMarkerStyle(21);\n",
" grxy->SetMarkerSize(0.5);\n",
" grxy->Draw(\"P\");\n",
" grin->SetMarkerColor(kRed);\n",
" grin->SetMarkerStyle(5);\n",
" grin->SetMarkerSize(0.7);\n",
" grin->Draw(\"P\");\n",
" grout->DrawClone(\"LP\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "faf563b4",
"metadata": {},
"source": [
"Test data (square)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c8a73fe8",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Int_t n = 11;\n",
"Double_t x[] = {1,2,3,4,5,6,6,6,8,9,10};\n",
"Double_t y[] = {1,4,9,16,25,25,36,49,64,81,100};\n",
"grxy = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"id": "4c2842a5",
"metadata": {},
"source": [
"X values, for which y values should be interpolated"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5cd18177",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Int_t nout = 14;\n",
"Double_t xout[] =\n",
" {1.2,1.7,2.5,3.2,4.4,5.2,5.7,6.5,7.6,8.3,9.7,10.4,11.3,13};"
]
},
{
"cell_type": "markdown",
"id": "9d3e0a07",
"metadata": {},
"source": [
"Create Canvas"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c109d66a",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"vC1 = new TCanvas(\"vC1\",\"square\",200,10,700,700);\n",
"vC1->Divide(2,2);"
]
},
{
"cell_type": "markdown",
"id": "6335ae56",
"metadata": {},
"source": [
"Initialize graph with data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "afb90eb3",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"id": "09aff921",
"metadata": {},
"source": [
"Interpolate at equidistant points (use mean for tied x-values)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d4094726",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TGraphSmooth *gs = new TGraphSmooth(\"normal\");\n",
"grout = gs->Approx(grin,\"linear\");\n",
"DrawSmooth(1,\"Approx: ties = mean\",\"X-axis\",\"Y-axis\");"
]
},
{
"cell_type": "markdown",
"id": "95cd6d0c",
"metadata": {},
"source": [
"Re-initialize graph with data\n",
"(since graph points were set to unique vales)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5532cadd",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"id": "abeb3eb2",
"metadata": {},
"source": [
"Interpolate at given points xout"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61e75645",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grout = gs->Approx(grin,\"linear\", 14, xout, 0, 130);\n",
"DrawSmooth(2,\"Approx: ties = mean\",\"\",\"\");"
]
},
{
"cell_type": "markdown",
"id": "ace7eb8a",
"metadata": {},
"source": [
"Print output variables for given values xout"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cf58bd41",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"Int_t vNout = grout->GetN();\n",
"Double_t vXout, vYout;\n",
"for (Int_t k=0;kGetPoint(k, vXout, vYout);\n",
" cout << \"k= \" << k << \" vXout[k]= \" << vXout\n",
" << \" vYout[k]= \" << vYout << endl;\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "0a6c8748",
"metadata": {},
"source": [
"Re-initialize graph with data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "818a7fd0",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"id": "0967bf5a",
"metadata": {},
"source": [
"Interpolate at equidistant points (use min for tied x-values)\n",
"_grout = gs->Approx(grin,\"linear\", 50, 0, 0, 0, 1, 0, \"min\");_"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c4b0e799",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grout = gs->Approx(grin,\"constant\", 50, 0, 0, 0, 1, 0.5, \"min\");\n",
"DrawSmooth(3,\"Approx: ties = min\",\"\",\"\");"
]
},
{
"cell_type": "markdown",
"id": "982caefa",
"metadata": {},
"source": [
"Re-initialize graph with data"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "faab82bf",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"id": "41e11410",
"metadata": {},
"source": [
"Interpolate at equidistant points (use max for tied x-values)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cfcd667a",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grout = gs->Approx(grin,\"linear\", 14, xout, 0, 0, 2, 0, \"max\");\n",
"DrawSmooth(4,\"Approx: ties = max\",\"\",\"\");"
]
},
{
"cell_type": "markdown",
"id": "e0984497",
"metadata": {},
"source": [
"Cleanup"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fe84169",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"delete gs;"
]
},
{
"cell_type": "markdown",
"id": "10331e6f",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "fa3a0d84",
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"%jsroot on\n",
"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
}