Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
th2polyUSA.py
Go to the documentation of this file.
1# \file
2# \ingroup tutorial_hist
3# \notebook -js
4# \preview This tutorial illustrates how to create an histogram with polygonal
5# bins (TH2Poly), fill it and draw it using the `col` option. The initial data
6# are stored in TMultiGraphs. They represent the USA map. Such histograms can
7# be rendered in 3D using the option `legogl`.
8#
9# The initial data have been downloaded from: http://www.maproom.psu.edu/dcw/
10# This database was developed in 1991/1992 and national boundaries reflect
11# political reality as of that time.
12#
13# \macro_code
14# \macro_image
15#
16# \date February 2024
17# \author Olivier Couet
18
19import ROOT
20import os.path
21import urllib.request
22
23states = ["alabama", "arizona", "arkansas", "california", "colorado", "connecticut", "delaware",
24 "florida", "georgia", "idaho", "illinois", "indiana", "iowa", "kansas",
25 "kentucky", "louisiana", "maine", "maryland", "massachusetts", "michigan", "minnesota",
26 "mississippi", "missouri", "montana", "nebraska", "nevada", "new_hampshire", "new_jersey",
27 "new_mexico", "new_york", "north_carolina", "north_dakota", "ohio", "oklahoma", "oregon",
28 "pennsylvania", "rhode_island", "south_carolina", "south_dakota", "tennessee", "texas", "utah",
29 "vermont", "virginia", "washington", "west_virginia", "wisconsin", "wyoming"]
30
31pops = [4708708, 6595778, 2889450, 36961664, 5024748, 3518288, 885122, 18537969, 9829211, 1545801,
32 12910409, 6423113, 3007856, 2818747, 4314113, 4492076, 1318301, 5699478, 6593587, 9969727,
33 5266214, 2951996, 5987580, 974989, 1796619, 2643085, 1324575, 8707739, 2009671, 19541453,
34 9380884, 646844, 11542645, 3687050, 3825657, 12604767, 1053209, 4561242, 812383, 6296254,
35 24782302, 2784572, 621760, 7882590, 6664195, 1819777, 5654774, 544270]
36
37usa = ROOT.TCanvas("USA", "USA")
39lon1 = -130
40lon2 = -65
41lat1 = 24
42lat2 = 50
43p = ROOT.TH2Poly("Lower48", "Lower 48 Population (2009);Latitude;Longitude", lon1, lon2, lat1, lat2)
44
45fileName = "usa.root"
46fileNameUrl = "http://root.cern/files/usa.root"
47if not os.path.isfile(fileName):
48 urllib.request.urlretrieve(fileNameUrl, fileName)
49f = ROOT.TFile.Open(fileName)
50
51# Define the TH2Poly bins.
53for key in f.GetListOfKeys():
54 obj = key.ReadObj()
55 if (obj.InheritsFrom("TMultiGraph")):
56 p.AddBin(obj)
57
58# Fill TH2Poly, with capital letters for the states names
59for state, pop in zip (states, pops):
60 p.Fill(state, pop)
61
63p.Draw("colz textn")
64
65# Add the reference for the population
66pupulationRef = ROOT.TLatex(-128, 27, "#scale[.55]{#splitline{Source:}{http://eadiv.state.wy.us/pop/st-09est.htm}}")
68
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.