Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
th2polyUSA.C File Reference

Detailed Description

View in nbviewer Open in SWAN
This tutorial illustrates how to create an histogram with polygonal bins (TH2Poly), fill it and draw it using the col option.

The initial data are stored in TMultiGraphs. They represent the USA map. Such histograms can be rendered in 3D using the option legogl.

The initial data have been downloaded from: http://www.maproom.psu.edu/dcw/ This database was developed in 1991/1992 and national boundaries reflect political reality as of that time.

void th2polyUSA()
{
Int_t i, bin;
const Int_t nx = 48;
const char *states [nx] = {
"alabama", "arizona", "arkansas", "california",
"colorado", "connecticut", "delaware", "florida",
"georgia", "idaho", "illinois", "indiana",
"iowa", "kansas", "kentucky", "louisiana",
"maine", "maryland", "massachusetts", "michigan",
"minnesota", "mississippi", "missouri", "montana",
"nebraska", "nevada", "new_hampshire", "new_jersey",
"new_mexico", "new_york", "north_carolina", "north_dakota",
"ohio", "oklahoma", "oregon", "pennsylvania",
"rhode_island", "south_carolina", "south_dakota", "tennessee",
"texas", "utah", "vermont", "virginia",
"washington", "west_virginia", "wisconsin", "wyoming"
};
Double_t pop[nx] = {
4708708, 6595778, 2889450, 36961664, 5024748, 3518288, 885122, 18537969,
9829211, 1545801, 12910409, 6423113, 3007856, 2818747, 4314113, 4492076,
1318301, 5699478, 6593587, 9969727, 5266214, 2951996, 5987580, 974989,
1796619, 2643085, 1324575, 8707739, 2009671, 19541453, 9380884, 646844,
11542645, 3687050, 3825657, 12604767, 1053209, 4561242, 812383, 6296254,
24782302, 2784572, 621760, 7882590, 6664195, 1819777, 5654774, 544270
};
TCanvas *usa = new TCanvas("USA", "USA");
usa->ToggleEventStatus();
Double_t lon1 = -130;
Double_t lon2 = -65;
Double_t lat1 = 24;
Double_t lat2 = 50;
TH2Poly *p = new TH2Poly("Lower48","Lower 48 Population (2009);Latitude;Longitude",lon1,lon2,lat1,lat2);
TFile *f = TFile::Open("http://root.cern/files/usa.root", "CACHEREAD");
if (!f) {
printf("Cannot access usa.root. Is internet working ?\n");
return;
}
// Define the TH2Poly bins.
TKey *key;
TIter nextkey(gDirectory->GetListOfKeys());
while ((key = (TKey*)nextkey())) {
TObject *obj = key->ReadObj();
if (obj->InheritsFrom("TMultiGraph")) {
mg = (TMultiGraph*)obj;
bin = p->AddBin(mg);
}
}
// Fill TH2Poly, with capital letters for the states names
for (i=0; i<nx; i++) p->Fill(states[i], pop[i]);
p->Draw("colz textn");
// Add the reference for the population
TLatex pupulationRef(-128, 27, "#scale[.55]{#splitline{Source:}{http://eadiv.state.wy.us/pop/st-09est.htm}}");
pupulationRef.DrawClone();
}
#define f(i)
Definition RSha256.hxx:104
int Int_t
Definition RtypesCore.h:45
double Double_t
Definition RtypesCore.h:59
#define gDirectory
Definition TDirectory.h:384
winID h TVirtualViewer3D TVirtualGLPainter p
R__EXTERN TStyle * gStyle
Definition TStyle.h:436
The Canvas class.
Definition TCanvas.h:23
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
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:4086
static Bool_t SetCacheFileDir(std::string_view cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
Sets the directory where to locally stage/cache remote files.
Definition TFile.cxx:4623
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:758
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:542
virtual void Draw(Option_t *option="")
Default Draw method for all objects.
Definition TObject.cxx:292
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:1640
Author
Olivier Couet

Definition in file th2polyUSA.C.