{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Building a Graph from an ASCII File\n", "
\n", "In this example we will read the points from a file and produce a simple graph." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the canvas on which we'll draw the graph." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "TCanvas c;\n", "c.SetGrid();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the graph of expected points by reading from an ASCII file." ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ " % Total % Received % Xferd Average Speed Time Time Time Current\n", " Dload Upload Total Spent Left Speed\n", "100 221 100 221 0 0 774 0 --:--:-- --:--:-- --:--:-- 775\n" ] } ], "source": [ ".! curl https://raw.githubusercontent.com/root-mirror/training/master/2016/macro2_input_expected.txt -o macro2_input_expected.txt" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [], "source": [ "TGraphErrors graph_expected(\"./macro2_input_expected.txt\", \"%lg %lg %lg\");\n", "\n", "graph_expected.SetTitle(\n", " \"Measurement XYZ and Expectation;\"\n", " \"lenght [cm];\"\n", " \"Arb.Units\");\n", "graph_expected.SetFillColor(kYellow);\n", "graph_expected.Draw(\"E3AL\"); // E3 draws the band" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create the graph of measured points, also reading from a file." ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [], "source": [ "TGraphErrors graph(\"../macro2_input.txt\", \"%lg %lg %lg\");\n", "\n", "graph.SetMarkerStyle(kCircle);\n", "graph.SetFillColor(0);\n", "graph.Draw(\"PESame\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Draw the legend." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], "source": [ "TLegend leg(.1, .7, .3, .9, \"Lab. Lesson 2\");\n", "leg.SetFillColor(0);\n", "leg.AddEntry(&graph_expected, \"Expected Points\");\n", "leg.AddEntry(&graph, \"Measured Points\");\n", "leg.Draw(\"Same\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Display in the notebook what is in our canvas as interactive javascript graphics." ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "%jsroot on\n", "c.Draw();" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Print graph and error values." ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "x[0]=1, y[0]=6, ex[0]=0, ey[0]=5\n", "x[1]=2, y[1]=12, ex[1]=0, ey[1]=5\n", "x[2]=3, y[2]=14, ex[2]=0, ey[2]=4.7\n", "x[3]=4, y[3]=20, ex[3]=0, ey[3]=4.5\n", "x[4]=5, y[4]=22, ex[4]=0, ey[4]=4.2\n", "x[5]=6, y[5]=24, ex[5]=0, ey[5]=5.1\n", "x[6]=7, y[6]=35, ex[6]=0, ey[6]=2.9\n", "x[7]=8, y[7]=45, ex[7]=0, ey[7]=4.1\n", "x[8]=9, y[8]=44, ex[8]=0, ey[8]=4.8\n", "x[9]=10, y[9]=53, ex[9]=0, ey[9]=5.43\n" ] } ], "source": [ "graph.Print();" ] } ], "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 }