{
"cells": [
{
"cell_type": "markdown",
"id": "35323959",
"metadata": {},
"source": [
"# FITS_tutorial5\n",
"Open a FITS file whose primary array represents\n",
"a spectrum (flux vs wavelength)\n",
"\n",
"\n",
"\n",
"\n",
"**Author:** Claudi Martinez \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:13 PM."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "830e6088",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:01.823379Z",
"iopub.status.busy": "2026-05-19T20:14:01.823269Z",
"iopub.status.idle": "2026-05-19T20:14:02.138076Z",
"shell.execute_reply": "2026-05-19T20:14:02.137344Z"
}
},
"outputs": [],
"source": [
"using Upvd_t = std::unique_ptr;"
]
},
{
"cell_type": "markdown",
"id": "8c622d4f",
"metadata": {},
"source": [
"We open a FITS file that contains a table with 9 rows and 8 columns. Column 4 has name\n",
"'mag' and contains a vector of 6 numeric components. The values of vectors in rows 1 and 2 (column 4) are:\n",
"Row1: (99.0, 24.768, 23.215, 21.68, 21.076, 20.857)\n",
"Row2: (99.0, 21.689, 20.206, 18.86, 18.32 , 18.128 )\n",
"WARNING: when coding, row and column indices start from 0"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "4faaef34",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:02.139937Z",
"iopub.status.busy": "2026-05-19T20:14:02.139819Z",
"iopub.status.idle": "2026-05-19T20:14:02.344341Z",
"shell.execute_reply": "2026-05-19T20:14:02.343690Z"
}
},
"outputs": [],
"source": [
"TString dir = gROOT->GetTutorialDir();"
]
},
{
"cell_type": "markdown",
"id": "bb04bf65",
"metadata": {},
"source": [
"Open the table"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "4da4625c",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:02.346319Z",
"iopub.status.busy": "2026-05-19T20:14:02.346205Z",
"iopub.status.idle": "2026-05-19T20:14:02.550123Z",
"shell.execute_reply": "2026-05-19T20:14:02.549414Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Info in : The selected HDU contains a Table Extension\n"
]
}
],
"source": [
"TFITSHDU hdu(dir + \"/io/fitsio/sample4.fits[1]\");"
]
},
{
"cell_type": "markdown",
"id": "02327c6c",
"metadata": {},
"source": [
"Read vectors at rows 1 and 2 (indices 0 and 1)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "3e22370d",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:02.551818Z",
"iopub.status.busy": "2026-05-19T20:14:02.551699Z",
"iopub.status.idle": "2026-05-19T20:14:02.754324Z",
"shell.execute_reply": "2026-05-19T20:14:02.753801Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"99, 24.768, 23.215, 21.68, 21.076, 20.857)\n",
"99, 21.689, 20.206, 18.86, 18.32, 18.128)\n",
"99, 24.768, 23.215, 21.68, 21.076, 20.857)\n",
"99, 21.689, 20.206, 18.86, 18.32, 18.128)\n",
"99, 23.993, 22.575, 21.51, 21.07, 20.915)\n",
"99, 22.26, 20.717, 19.404, 18.888, 18.706)\n",
"99, 21.957, 20.41, 18.874, 18.271, 18.054)\n",
"99, 22.113, 20.535, 18.872, 18.229, 17.995)\n",
"99, 22.177, 20.678, 19.275, 18.714, 18.513)\n",
"99, 23.394, 21.846, 20.27, 19.649, 19.424)\n",
"99, 23.094, 21.452, 19.482, 18.723, 18.434)\n"
]
}
],
"source": [
"std::array vs{Upvd_t(hdu.GetTabRealVectorCell(0, \"mag\")), Upvd_t(hdu.GetTabRealVectorCell(1, \"mag\"))};\n",
"for (auto &&v : vs) {\n",
" for(auto i : ROOT::TSeqI(v->GetNoElements())) {\n",
" if (i > 0)\n",
" printf(\", \");\n",
" printf(\"%lg\", (*v)[i]); // NOTE: the asterisk is for using the overloaded [] operator of the TVectorD object\n",
" }\n",
" printf(\")\\n\");\n",
"}\n",
"\n",
"// We now dump all rows using the function GetTabRealVectorCells()\n",
"std::unique_ptr vectorCollection(hdu.GetTabRealVectorCells(\"mag\"));\n",
"for (auto vObj : *vectorCollection) {\n",
" auto &v = *static_cast(vObj);\n",
" for (auto i : ROOT::TSeqI(v.GetNoElements())) {\n",
" if (i > 0) printf(\", \");\n",
" printf(\"%lg\", (v[i]));\n",
" }\n",
" printf(\")\\n\");\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "edab2b53",
"metadata": {},
"source": [
"Draw all canvases "
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "f777a341",
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2026-05-19T20:14:02.755740Z",
"iopub.status.busy": "2026-05-19T20:14:02.755587Z",
"iopub.status.idle": "2026-05-19T20:14:02.959991Z",
"shell.execute_reply": "2026-05-19T20:14:02.959521Z"
}
},
"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
}