{ "cells": [ { "cell_type": "markdown", "id": "686e3eeb", "metadata": {}, "source": [ "# FITS_tutorial1\n", "Open a FITS file and retrieve the first plane of the image array\n", "as a TImage object\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": "markdown", "id": "6cea9a05", "metadata": {}, "source": [ "Here we open a FITS file that contains only the primary HDU, consisting on an image.\n", "The object you will see is a snapshot of the NGC7662 nebula,\n", "which was taken by the author on November 2009 in Barcelona." ] }, { "cell_type": "code", "execution_count": null, "id": "22220430", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TString dir = gROOT->GetTutorialDir();" ] }, { "cell_type": "markdown", "id": "5f01a70e", "metadata": {}, "source": [ "Open primary HDU from file" ] }, { "cell_type": "code", "execution_count": null, "id": "3a60f45b", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TFITSHDU hdu(dir + \"/io/fitsio/sample1.fits\");" ] }, { "cell_type": "markdown", "id": "5bc8d020", "metadata": {}, "source": [ "Dump the HDUs within the FITS file\n", "and also their metadata\n", "printf(\"Press ENTER to see summary of all data stored in the file:\"); getchar();" ] }, { "cell_type": "code", "execution_count": null, "id": "3f64c954", "metadata": { "collapsed": false }, "outputs": [], "source": [ "hdu.Print(\"F+\");" ] }, { "cell_type": "markdown", "id": "0c2423ec", "metadata": {}, "source": [ "Here we get the exposure time." ] }, { "cell_type": "code", "execution_count": null, "id": "278de740", "metadata": { "collapsed": false }, "outputs": [], "source": [ "printf(\"Exposure time = %s\\n\", hdu.GetKeywordValue(\"EXPTIME\").Data());" ] }, { "cell_type": "markdown", "id": "a8b90d81", "metadata": {}, "source": [ "Read the primary array as a matrix, selecting only layer 0.\n", "This function may be useful to do image processing, e.g. custom filtering" ] }, { "cell_type": "code", "execution_count": null, "id": "658166d0", "metadata": { "collapsed": false }, "outputs": [], "source": [ "std::unique_ptr mat(hdu.ReadAsMatrix(0));\n", "mat->Print();" ] }, { "cell_type": "markdown", "id": "9926667f", "metadata": {}, "source": [ "Read the primary array as an image, selecting only layer 0." ] }, { "cell_type": "code", "execution_count": null, "id": "4ddd8e1b", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TImage * im = (TImage *)hdu.ReadAsImage(0);" ] }, { "cell_type": "markdown", "id": "79ff2984", "metadata": {}, "source": [ "Read the primary array as a histogram. Depending on array dimensions, the returned\n", "histogram will be 1D, 2D or 3D." ] }, { "cell_type": "code", "execution_count": null, "id": "740637f9", "metadata": { "collapsed": false }, "outputs": [], "source": [ "TH1 * hist = (TH1 *)hdu.ReadAsHistogram();\n", "\n", "auto c = new TCanvas(\"c1\", \"FITS tutorial #1\", 1400, 800);\n", "c->Divide(2, 1);\n", "c->cd(1);\n", "im->Draw();\n", "c->cd(2);\n", "hist->Draw(\"COL\");" ] }, { "cell_type": "markdown", "id": "423b3dda", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": null, "id": "e3aacd8f", "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 }