Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
hist040_TH2Poly_europe.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. The initial data are stored
6/// in TMultiGraphs. They represent the european countries.
7/// The histogram filling is done according to a Mercator projection,
8/// therefore the bin contains should be proportional to the real surface
9/// of the countries.
10///
11/// The initial data have been downloaded from: http://www.maproom.psu.edu/dcw/
12/// This database was developed in 1991/1992 and national boundaries reflect
13/// political reality as of that time.
14///
15/// The script is shooting npoints (script argument) randomly over the Europe area.
16/// The number of points inside the countries should be proportional to the country surface
17/// The estimated surface is compared to the surfaces taken from wikipedia.
18///
19/// \macro_image
20/// \macro_output
21/// \macro_code
22///
23/// \date August 2020
24/// \author Olivier Couet
25
27{
28 Int_t i, j;
29 Double_t lon1 = -25;
30 Double_t lon2 = 35;
31 Double_t lat1 = 34;
32 Double_t lat2 = 72;
33 Double_t R = (lat2 - lat1) / (lon2 - lon1);
34 Int_t W = 800;
35 Int_t H = (Int_t)(R * 800);
36 gStyle->SetStatX(0.28);
37 gStyle->SetStatY(0.45);
38 gStyle->SetStatW(0.15);
39
40 // Canvas used to draw TH2Poly (the map)
41 TCanvas *ce = new TCanvas("ce", "ce", 0, 0, W, H);
42 ce->ToggleEventStatus();
43 ce->SetGridx();
44 ce->SetGridy();
45
46 // Real surfaces taken from Wikipedia.
47 const Int_t nx = 36;
48 // see http://en.wikipedia.org/wiki/Area_and_population_of_European_countries
49 const char *countries[nx] = {
50 "france", "spain", "sweden", "germany", "finland", "norway", "poland", "italy",
51 "yugoslavia", "united_kingdom", "romania", "belarus", "greece", "czechoslovakia", "bulgaria", "iceland",
52 "hungary", "portugal", "austria", "ireland", "lithuania", "latvia", "estonia", "denmark",
53 "netherlands", "switzerland", "moldova", "belgium", "albania", "cyprus", "luxembourg", "andorra",
54 "malta", "liechtenstein", "san_marino", "monaco"};
55 Float_t surfaces[nx] = {547030, 505580, 449964, 357021, 338145, 324220, 312685, 301230, 255438,
56 244820, 237500, 207600, 131940, 127711, 110910, 103000, 93030, 89242,
57 83870, 70280, 65200, 64589, 45226, 43094, 41526, 41290, 33843,
58 30528, 28748, 9250, 2586, 468, 316, 160, 61, 2};
59
60 TH1F *h = new TH1F("h", "Countries surfaces (in km^{2})", 3, 0, 3);
61 for (i = 0; i < nx; i++)
62 h->Fill(countries[i], surfaces[i]);
63 h->LabelsDeflate();
64
66 TFile *f;
67 f = TFile::Open("http://root.cern/files/europe.root", "cacheread");
68
69 if (!f) {
70 printf("Cannot access europe.root. Is internet working ?\n");
71 return;
72 }
73
74 TH2Poly *p =
75 new TH2Poly("Europe", "Europe (bin contents are normalized to the surfaces in km^{2})", lon1, lon2, lat1, lat2);
76 p->GetXaxis()->SetNdivisions(520);
77 p->GetXaxis()->SetTitle("longitude");
78 p->GetYaxis()->SetTitle("latitude");
79
80 p->SetContour(100);
81
82 TMultiGraph *mg;
83 TKey *key;
84 TIter nextkey(gDirectory->GetListOfKeys());
85 while ((key = (TKey *)nextkey())) {
86 TObject *obj = key->ReadObj();
87 if (obj->InheritsFrom("TMultiGraph")) {
88 mg = (TMultiGraph *)obj;
89 p->AddBin(mg);
90 }
91 }
92
93 TRandom r;
95 Double_t x, y, pi4 = TMath::Pi() / 4, alpha = TMath::Pi() / 360;
96
97 gBenchmark->Start("Partitioning");
98 p->ChangePartition(100, 100);
99 gBenchmark->Show("Partitioning");
100
101 // Fill TH2Poly according to a Mercator projection.
102 gBenchmark->Start("Filling");
103 for (i = 0; i < npoints; i++) {
104 longitude = r.Uniform(lon1, lon2);
105 latitude = r.Uniform(lat1, lat2);
106 x = longitude;
107 y = 38 * TMath::Log(TMath::Tan(pi4 + alpha * latitude));
108 p->Fill(x, y);
109 }
110 gBenchmark->Show("Filling");
111
112 Int_t nbins = p->GetNumberOfBins();
113 Double_t maximum = p->GetMaximum();
114
115 // h2 contains the surfaces computed from TH2Poly.
116 TH1F *h2 = (TH1F *)h->Clone("h2");
117 h2->Reset();
118 for (j = 0; j < nx; j++) {
119 for (i = 0; i < nbins; i++) {
120 if (strstr(countries[j], p->GetBinName(i + 1))) {
121 h2->Fill(countries[j], p->GetBinContent(i + 1));
122 h2->SetBinError(j, p->GetBinError(i + 1));
123 }
124 }
125 }
126
127 // Normalize the TH2Poly bin contents to the real surfaces.
129 for (i = 0; i < nbins; i++)
130 p->SetBinContent(i + 1, scale * p->GetBinContent(i + 1));
131
132 gStyle->SetOptStat(1111);
133 p->Draw("COLZ");
134
135 TCanvas *c1 = new TCanvas("c1", "c1", W + 10, 0, W - 20, H);
136 c1->SetRightMargin(0.047);
137
138 scale = h->GetMaximum() / h2->GetMaximum();
139
140 h->SetStats(0);
141 h->SetLineColor(kRed - 3);
142 h->SetLineWidth(2);
143 h->SetMarkerStyle(20);
144 h->SetMarkerColor(kBlue);
145 h->SetMarkerSize(0.8);
146 h->Draw("LP");
147 h->GetXaxis()->SetLabelFont(42);
148 h->GetXaxis()->SetLabelSize(0.03);
149 h->GetYaxis()->SetLabelFont(42);
150
151 h2->Scale(scale);
153 for (i = 0; i < nx; i++)
154 h2->SetBinError(i + 1, scale2 * h2->GetBinError(i + 1));
155 h2->Draw("E SAME");
156 h2->SetMarkerStyle(20);
157 h2->SetMarkerSize(0.8);
158
159 TLegend *leg = new TLegend(0.5, 0.67, 0.92, 0.8, nullptr, "NDC");
160 leg->SetTextFont(42);
161 leg->SetTextSize(0.025);
162 leg->AddEntry(h, "Real countries surfaces from Wikipedia (in km^{2})", "lp");
163 leg->AddEntry(h2, "Countries surfaces from TH2Poly (with errors)", "lp");
164 leg->Draw();
165 leg->Draw();
166
167 Double_t wikiSum = h->Integral();
168 Double_t polySum = h2->Integral();
170 printf("THPoly Europe surface estimation error wrt wikipedia = %f per cent when using %d points\n", 100 * error,
171 npoints);
172}
#define f(i)
Definition RSha256.hxx:104
#define h(i)
Definition RSha256.hxx:106
int Int_t
Definition RtypesCore.h:45
float Float_t
Definition RtypesCore.h:57
double Double_t
Definition RtypesCore.h:59
@ kRed
Definition Rtypes.h:66
@ kBlue
Definition Rtypes.h:66
R__EXTERN TBenchmark * gBenchmark
Definition TBenchmark.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
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
R__EXTERN TStyle * gStyle
Definition TStyle.h:442
virtual void Start(const char *name)
Starts Benchmark with the specified name.
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
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:131
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:4130
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:4673
1-D histogram with a float per channel (see TH1 documentation)
Definition TH1.h:645
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
This class displays a legend box (TPaveText) containing several legend entries.
Definition TLegend.h:23
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
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
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:1642
void SetStatX(Float_t x=0)
Definition TStyle.h:401
void SetStatW(Float_t w=0.19)
Definition TStyle.h:403
void SetStatY(Float_t y=0)
Definition TStyle.h:402
Double_t y[n]
Definition legend1.C:17
return c1
Definition legend1.C:41
Double_t x[n]
Definition legend1.C:17
leg
Definition legend1.C:34
#define H(x, y, z)
Double_t Log(Double_t x)
Returns the natural logarithm of x.
Definition TMath.h:760
Double_t Sqrt(Double_t x)
Returns the square root of x.
Definition TMath.h:666
constexpr Double_t Pi()
Definition TMath.h:37
Double_t Tan(Double_t)
Returns the tangent of an angle of x radians.
Definition TMath.h:604
Short_t Abs(Short_t d)
Returns the absolute value of parameter Short_t d.
Definition TMathBase.h:123