Logo ROOT  
Reference Guide
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
Loading...
Searching...
No Matches
th2polyUSA.C
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/// \author Olivier Couet
17
18void th2polyUSA()
19{
20 Int_t i, bin;
21 const Int_t nx = 48;
22 const char *states[nx] = {
23 "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 Double_t pop[nx] = {4708708, 6595778, 2889450, 36961664, 5024748, 3518288, 885122, 18537969, 9829211, 1545801,
31 12910409, 6423113, 3007856, 2818747, 4314113, 4492076, 1318301, 5699478, 6593587, 9969727,
32 5266214, 2951996, 5987580, 974989, 1796619, 2643085, 1324575, 8707739, 2009671, 19541453,
33 9380884, 646844, 11542645, 3687050, 3825657, 12604767, 1053209, 4561242, 812383, 6296254,
34 24782302, 2784572, 621760, 7882590, 6664195, 1819777, 5654774, 544270};
35
36 auto usa = new TCanvas("USA", "USA");
37 usa->ToggleEventStatus();
38 Double_t lon1 = -130;
39 Double_t lon2 = -65;
40 Double_t lat1 = 24;
41 Double_t lat2 = 50;
42 auto p = new TH2Poly("Lower48", "Lower 48 Population (2009);Latitude;Longitude", lon1, lon2, lat1, lat2);
43
44 const auto fileName = "usa.root";
45 const auto fileNameUrl = "http://root.cern/files/usa.root";
46 if(gSystem->AccessPathName(fileName))
47 TFile::Cp(fileNameUrl, fileName);
48
49 auto f = TFile::Open(fileName);
50
51 if (!f) {
52 printf("Cannot access %s. Is internet working ?\n", fileName);
53 return;
54 }
55
56 // Define the TH2Poly bins.
57 TMultiGraph *mg;
58 TKey *key;
59 TIter nextkey(gDirectory->GetListOfKeys());
60 while ((key = (TKey *)nextkey())) {
61 TObject *obj = key->ReadObj();
62 if (obj->InheritsFrom("TMultiGraph")) {
63 mg = (TMultiGraph *)obj;
64 bin = p->AddBin(mg);
65 }
66 }
67
68 // Fill TH2Poly, with capital letters for the states names
69 for (i = 0; i < nx; i++)
70 p->Fill(states[i], pop[i]);
71
73 p->Draw("colz textn");
74
75 // Add the reference for the population
76 TLatex pupulationRef(-128, 27, "#scale[.55]{#splitline{Source:}{http://eadiv.state.wy.us/pop/st-09est.htm}}");
77 pupulationRef.DrawClone();
78}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
#define gDirectory
Definition TDirectory.h:384
winID h TVirtualViewer3D TVirtualGLPainter p
R__EXTERN TStyle * gStyle
Definition TStyle.h:433
R__EXTERN TSystem * gSystem
Definition TSystem.h:566
The Canvas class.
Definition TCanvas.h:23
virtual Bool_t Cp(const char *dst, Bool_t progressbar=kTRUE, UInt_t buffersize=1000000)
Allows to copy this file to the dst URL.
Definition TFile.cxx:5008
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
Create / open a file.
Definition TFile.cxx:4094
2D Histogram with Polygonal Bins
Definition TH2Poly.h:66
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition TKey.h:28
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition TKey.cxx:762
To draw Mathematical Formula.
Definition TLatex.h:18
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition TMultiGraph.h:34
Mother of all ROOT objects.
Definition TObject.h:41
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition TObject.cxx:525
void SetOptStat(Int_t stat=1)
The type of information printed in the histogram statistics box can be selected via the parameter mod...
Definition TStyle.cxx:1636
virtual Bool_t AccessPathName(const char *path, EAccessMode mode=kFileExists)
Returns FALSE if one can access a file using the specified access mode.
Definition TSystem.cxx:1296