{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Approx\n",
"Macro to test interpolation function Approx\n",
"```cpp\n",
" .x ErrorIntegral.C\n",
"```\n",
"\n",
"cally generated with lalalalalal ROOTbook-izer (beta) on stuff\n",
"\n",
"**Author:** Christian Stratowa, Vienna, Austria. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"TCanvas *vC1;\n",
"TGraph *grxy, *grin, *grout;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" A helper function is created: "
]
},
{
"cell_type": "code",
"execution_count": 2,
"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",
"metadata": {},
"source": [
" Test data (square)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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",
"metadata": {},
"source": [
" X values, for which y values should be interpolated"
]
},
{
"cell_type": "code",
"execution_count": 4,
"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",
"metadata": {},
"source": [
" Create Canvas"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"vC1 = new TCanvas(\"vC1\",\"square\",200,10,700,700);\n",
"vC1->Divide(2,2);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Initialize graph with data"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Interpolate at equidistant points (use mean for tied x-values)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"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",
"metadata": {},
"source": [
" Re-initialize graph with data\n",
" (since graph points were set to unique vales)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Interpolate at given points xout"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grout = gs->Approx(grin,\"linear\", 14, xout, 0, 130);\n",
"DrawSmooth(2,\"Approx: ties = mean\",\"\",\"\");"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Print output variables for given values xout"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"k= 0 vXout[k]= 1.2 vYout[k]= 1.6\n",
"k= 1 vXout[k]= 1.7 vYout[k]= 3.1\n",
"k= 2 vXout[k]= 2.5 vYout[k]= 6.5\n",
"k= 3 vXout[k]= 3.2 vYout[k]= 10.4\n",
"k= 4 vXout[k]= 4.4 vYout[k]= 19.6\n",
"k= 5 vXout[k]= 5.2 vYout[k]= 27.3333\n",
"k= 6 vXout[k]= 5.7 vYout[k]= 33.1667\n",
"k= 7 vXout[k]= 6.5 vYout[k]= 43.5\n",
"k= 8 vXout[k]= 7.6 vYout[k]= 58.5333\n",
"k= 9 vXout[k]= 8.3 vYout[k]= 69.1\n",
"k= 10 vXout[k]= 9.7 vYout[k]= 94.3\n",
"k= 11 vXout[k]= 10.4 vYout[k]= 130\n",
"k= 12 vXout[k]= 11.3 vYout[k]= 130\n",
"k= 13 vXout[k]= 13 vYout[k]= 130\n"
]
}
],
"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",
"metadata": {},
"source": [
" Re-initialize graph with data"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"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": 12,
"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",
"metadata": {},
"source": [
" Re-initialize graph with data"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"grin = new TGraph(n,x,y);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Interpolate at equidistant points (use max for tied x-values)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"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",
"metadata": {},
"source": [
" Cleanup"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"delete gs;"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%jsroot\n",
"gROOT->GetListOfCanvases()->Draw()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"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": 0
}