Logo ROOT  
Reference Guide
th2polyUSA.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook -js
4/// 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",
24 "colorado", "connecticut", "delaware", "florida",
25 "georgia", "idaho", "illinois", "indiana",
26 "iowa", "kansas", "kentucky", "louisiana",
27 "maine", "maryland", "massachusetts", "michigan",
28 "minnesota", "mississippi", "missouri", "montana",
29 "nebraska", "nevada", "new_hampshire", "new_jersey",
30 "new_mexico", "new_york", "north_carolina", "north_dakota",
31 "ohio", "oklahoma", "oregon", "pennsylvania",
32 "rhode_island", "south_carolina", "south_dakota", "tennessee",
33 "texas", "utah", "vermont", "virginia",
34 "washington", "west_virginia", "wisconsin", "wyoming"
35 };
36 Double_t pop[nx] = {
37 4708708, 6595778, 2889450, 36961664, 5024748, 3518288, 885122, 18537969,
38 9829211, 1545801, 12910409, 6423113, 3007856, 2818747, 4314113, 4492076,
39 1318301, 5699478, 6593587, 9969727, 5266214, 2951996, 5987580, 974989,
40 1796619, 2643085, 1324575, 8707739, 2009671, 19541453, 9380884, 646844,
41 11542645, 3687050, 3825657, 12604767, 1053209, 4561242, 812383, 6296254,
42 24782302, 2784572, 621760, 7882590, 6664195, 1819777, 5654774, 544270
43 };
44
45 TCanvas *usa = new TCanvas("USA", "USA");
46 usa->ToggleEventStatus();
47 Double_t lon1 = -130;
48 Double_t lon2 = -65;
49 Double_t lat1 = 24;
50 Double_t lat2 = 50;
51 TH2Poly *p = new TH2Poly("USA","USA Population",lon1,lon2,lat1,lat2);
52
54 TFile *f = TFile::Open("http://root.cern.ch/files/usa.root", "CACHEREAD");
55
56 if (!f) {
57 printf("Cannot access usa.root. Is internet working ?\n");
58 return;
59 }
60
61 // Define the TH2Poly bins.
63 TKey *key;
64 TIter nextkey(gDirectory->GetListOfKeys());
65 while ((key = (TKey*)nextkey())) {
66 TObject *obj = key->ReadObj();
67 if (obj->InheritsFrom("TMultiGraph")) {
68 mg = (TMultiGraph*)obj;
69 bin = p->AddBin(mg);
70 }
71 }
72
73 // Fill TH2Poly.
74 for (i=0; i<nx; i++) p->Fill(states[i], pop[i]);
75
76 gStyle->SetOptStat(11);
77 p->Draw("colz textn");
78}
#define f(i)
Definition: RSha256.hxx:104
int Int_t
Definition: RtypesCore.h:43
double Double_t
Definition: RtypesCore.h:57
#define gDirectory
Definition: TDirectory.h:229
R__EXTERN TStyle * gStyle
Definition: TStyle.h:410
The Canvas class.
Definition: TCanvas.h:27
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition: TCanvas.cxx:2375
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
Definition: TFile.h:53
static Bool_t SetCacheFileDir(ROOT::Internal::TStringView cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
Definition: TFile.h:323
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:3942
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2998
2D Histogram with Polygonal Bins
Definition: TH2Poly.h:66
virtual Int_t Fill(Double_t x, Double_t y)
Increment the bin containing (x,y) by 1.
Definition: TH2Poly.cxx:589
virtual Int_t AddBin(TObject *poly)
Adds a new bin to the histogram.
Definition: TH2Poly.cxx:222
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:738
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:36
Mother of all ROOT objects.
Definition: TObject.h:37
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:443
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:1590
static constexpr double mg