{ "cells": [ { "cell_type": "markdown", "id": "f4b08c8d", "metadata": {}, "source": [ "# hist039_TH2Poly_usa\n", "bins (TH2Poly), fill it and draw it using the `col` option. The initial data\n", "are stored in TMultiGraphs. They represent the USA map. Such histograms can\n", "be rendered in 3D using the option `legogl`.\n", "\n", "The initial data have been downloaded from: http://www.maproom.psu.edu/dcw/\n", "This database was developed in 1991/1992 and national boundaries reflect\n", "political reality as of that time.\n", "\n", "\n", "\n", "\n", "**Author:** Olivier Couet \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:12 PM." ] }, { "cell_type": "code", "execution_count": 1, "id": "157a9f88", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:53.784264Z", "iopub.status.busy": "2026-05-19T20:12:53.784140Z", "iopub.status.idle": "2026-05-19T20:12:54.348527Z", "shell.execute_reply": "2026-05-19T20:12:54.348233Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "[TFile::Cp] Total 0.95 MB\t|>...................| 0.00 % [0.0 MB/s]\r", "[TFile::Cp] Total 0.95 MB\t|====================| 100.00 % [59.1 MB/s]\r\n" ] } ], "source": [ "Int_t i, bin;\n", "const Int_t nx = 48;\n", "const char *states[nx] = {\n", " \"alabama\", \"arizona\", \"arkansas\", \"california\", \"colorado\", \"connecticut\", \"delaware\",\n", " \"florida\", \"georgia\", \"idaho\", \"illinois\", \"indiana\", \"iowa\", \"kansas\",\n", " \"kentucky\", \"louisiana\", \"maine\", \"maryland\", \"massachusetts\", \"michigan\", \"minnesota\",\n", " \"mississippi\", \"missouri\", \"montana\", \"nebraska\", \"nevada\", \"new_hampshire\", \"new_jersey\",\n", " \"new_mexico\", \"new_york\", \"north_carolina\", \"north_dakota\", \"ohio\", \"oklahoma\", \"oregon\",\n", " \"pennsylvania\", \"rhode_island\", \"south_carolina\", \"south_dakota\", \"tennessee\", \"texas\", \"utah\",\n", " \"vermont\", \"virginia\", \"washington\", \"west_virginia\", \"wisconsin\", \"wyoming\"};\n", "Double_t pop[nx] = {4708708, 6595778, 2889450, 36961664, 5024748, 3518288, 885122, 18537969, 9829211, 1545801,\n", " 12910409, 6423113, 3007856, 2818747, 4314113, 4492076, 1318301, 5699478, 6593587, 9969727,\n", " 5266214, 2951996, 5987580, 974989, 1796619, 2643085, 1324575, 8707739, 2009671, 19541453,\n", " 9380884, 646844, 11542645, 3687050, 3825657, 12604767, 1053209, 4561242, 812383, 6296254,\n", " 24782302, 2784572, 621760, 7882590, 6664195, 1819777, 5654774, 544270};\n", "\n", "auto usa = new TCanvas(\"USA\", \"USA\");\n", "usa->ToggleEventStatus();\n", "Double_t lon1 = -130;\n", "Double_t lon2 = -65;\n", "Double_t lat1 = 24;\n", "Double_t lat2 = 50;\n", "auto p = new TH2Poly(\"Lower48\", \"Lower 48 Population (2009);Latitude;Longitude\", lon1, lon2, lat1, lat2);\n", "\n", "const auto fileName = \"usa.root\";\n", "const auto fileNameUrl = \"http://root.cern/files/usa.root\";\n", "if(gSystem->AccessPathName(fileName))\n", " TFile::Cp(fileNameUrl, fileName);\n", "\n", "auto f = TFile::Open(fileName);\n", "\n", "if (!f) {\n", " printf(\"Cannot access %s. Is internet working ?\\n\", fileName);\n", " return;\n", "}" ] }, { "cell_type": "markdown", "id": "de944621", "metadata": {}, "source": [ "Define the TH2Poly bins." ] }, { "cell_type": "code", "execution_count": 2, "id": "99c56a8e", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:54.360913Z", "iopub.status.busy": "2026-05-19T20:12:54.360789Z", "iopub.status.idle": "2026-05-19T20:12:54.562739Z", "shell.execute_reply": "2026-05-19T20:12:54.562268Z" } }, "outputs": [], "source": [ "TMultiGraph *mg;\n", "TKey *key;\n", "TIter nextkey(gDirectory->GetListOfKeys());\n", "while ((key = (TKey *)nextkey())) {\n", " TObject *obj = key->ReadObj();\n", " if (obj->InheritsFrom(\"TMultiGraph\")) {\n", " mg = (TMultiGraph *)obj;\n", " bin = p->AddBin(mg);\n", " }\n", "}" ] }, { "cell_type": "markdown", "id": "b875838a", "metadata": {}, "source": [ "Fill TH2Poly, with capital letters for the states names" ] }, { "cell_type": "code", "execution_count": 3, "id": "3d6d3afb", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:54.564265Z", "iopub.status.busy": "2026-05-19T20:12:54.564155Z", "iopub.status.idle": "2026-05-19T20:12:54.934783Z", "shell.execute_reply": "2026-05-19T20:12:54.934327Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "for (i = 0; i < nx; i++)\n", " p->Fill(states[i], pop[i]);\n", "\n", "gStyle->SetOptStat(0);\n", "p->Draw(\"colz textn\");" ] }, { "cell_type": "markdown", "id": "8bce7994", "metadata": {}, "source": [ "Add the reference for the population" ] }, { "cell_type": "code", "execution_count": 4, "id": "8258536f", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:54.936176Z", "iopub.status.busy": "2026-05-19T20:12:54.936064Z", "iopub.status.idle": "2026-05-19T20:12:55.139979Z", "shell.execute_reply": "2026-05-19T20:12:55.139505Z" } }, "outputs": [], "source": [ "TLatex pupulationRef(-128, 27, \"#scale[.55]{#splitline{Source:}{http://eadiv.state.wy.us/pop/st-09est.htm}}\");\n", "pupulationRef.DrawClone();" ] }, { "cell_type": "markdown", "id": "69ebdc08", "metadata": {}, "source": [ "Draw all canvases " ] }, { "cell_type": "code", "execution_count": 5, "id": "09e2ce9a", "metadata": { "collapsed": false, "execution": { "iopub.execute_input": "2026-05-19T20:12:55.141470Z", "iopub.status.busy": "2026-05-19T20:12:55.141356Z", "iopub.status.idle": "2026-05-19T20:12:55.347356Z", "shell.execute_reply": "2026-05-19T20:12:55.346929Z" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "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 }