ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
th2polyEurope.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// This tutorial illustrates how to create an histogram with polygonal
4 /// bins (TH2Poly), fill it and draw it. The initial data are stored
5 /// in TMultiGraphs. They represent the european countries.
6 /// The histogram filling is done according to a Mercator projection,
7 /// therefore the bin contains should be proportional to the real surface
8 /// of the countries.
9 ///
10 /// The initial data have been downloaded from: http://www.maproom.psu.edu/dcw/
11 /// This database was developed in 1991/1992 and national boundaries reflect
12 /// political reality as of that time.
13 ///
14 /// The script is shooting npoints (script argument) randomly over the Europe area.
15 /// The number of points inside the countries should be proportional to the country surface
16 /// The estimated surface is compared to the surfaces taken from wikipedia.
17 ///
18 /// \macro_image
19 /// \macro_code
20 ///
21 /// \author Olivier Couet
22 
23 void th2polyEurope(Int_t npoints=500000)
24 {
25  Int_t i,j;
26  Double_t lon1 = -25;
27  Double_t lon2 = 35;
28  Double_t lat1 = 34;
29  Double_t lat2 = 72;
30  Double_t R = (lat2-lat1)/(lon2-lon1);
31  Int_t W = 800;
32  Int_t H = (Int_t)(R*800);
33  gStyle->SetTitleX(0.2);
34  gStyle->SetStatX(0.28);
35  gStyle->SetStatY(0.45);
36  gStyle->SetStatW(0.15);
37 
38  // Canvas used to draw TH2Poly (the map)
39  TCanvas *ce = new TCanvas("ce", "ce",0,0,W,H);
40  ce->ToggleEventStatus();
41  ce->SetGridx();
42  ce->SetGridy();
43 
44  // Real surfaces taken from Wikipedia.
45  const Int_t nx = 36;
46  // see http://en.wikipedia.org/wiki/Area_and_population_of_European_countries
47  const char *countries[nx] = { "france", "spain", "sweden", "germany", "finland",
48  "norway", "poland", "italy", "yugoslavia", "united_kingdom",
49  "romania", "belarus","greece", "czechoslovakia","bulgaria",
50  "iceland", "hungary","portugal","austria", "ireland",
51  "lithuania", "latvia", "estonia", "denmark", "netherlands",
52  "switzerland","moldova","belgium", "albania", "cyprus",
53  "luxembourg", "andorra","malta", "liechtenstein", "san_marino",
54  "monaco" };
55  Float_t surfaces[nx] = { 547030, 505580, 449964, 357021, 338145,
56  324220, 312685, 301230, 255438, 244820,
57  237500, 207600, 131940, 127711, 110910,
58  103000, 93030, 89242, 83870, 70280,
59  65200, 64589, 45226, 43094, 41526,
60  41290, 33843, 30528, 28748, 9250,
61  2586, 468, 316, 160, 61,
62  2};
63 
64  TH1F *h = new TH1F("h","Countries surfaces (in km^{2})",3,0,3);
65  for (i=0; i<nx; i++) h->Fill(countries[i], surfaces[i]);
66  h->LabelsDeflate();
67 
69  TFile *f;
70  f = TFile::Open("http://root.cern.ch/files/europe.root","cacheread");
71 
72  if (!f) {
73  printf("Cannot access europe.root. Is internet working ?\n");
74  return;
75  }
76 
77  TH2Poly *p = new TH2Poly(
78  "Europe",
79  "Europe (bin contents are normalized to the surfaces in km^{2})",
80  lon1,lon2,lat1,lat2);
81  p->GetXaxis()->SetNdivisions(520);
82  p->GetXaxis()->SetTitle("longitude");
83  p->GetYaxis()->SetTitle("latitude");
84 
85  p->SetContour(100);
86 
87  TMultiGraph *mg;
88  TKey *key;
89  TIter nextkey(gDirectory->GetListOfKeys());
90  while ((key = (TKey*)nextkey())) {
91  TObject *obj = key->ReadObj();
92  if (obj->InheritsFrom("TMultiGraph")) {
93  mg = (TMultiGraph*)obj;
94  p->AddBin(mg);
95  }
96  }
97 
98  TRandom r;
99  Double_t longitude, latitude;
100  Double_t x, y, pi4 = TMath::Pi()/4, alpha = TMath::Pi()/360;
101 
102  gBenchmark->Start("Partitioning");
103  p->ChangePartition(100, 100);
104  gBenchmark->Show("Partitioning");
105 
106  // Fill TH2Poly according to a Mercator projection.
107  gBenchmark->Start("Filling");
108  for (i=0; i<npoints; i++) {
109  longitude = r.Uniform(lon1,lon2);
110  latitude = r.Uniform(lat1,lat2);
111  x = longitude;
112  y = 38*TMath::Log(TMath::Tan(pi4+alpha*latitude));
113  p->Fill(x,y);
114  }
115  gBenchmark->Show("Filling");
116 
117  Int_t nbins = p->GetNumberOfBins();
118  Double_t maximum = p->GetMaximum();
119 
120 
121  // h2 contains the surfaces computed from TH2Poly.
122  TH1F *h2 = (TH1F *)h->Clone("h2");
123  h2->Reset();
124  for (j=0; j<nx; j++) {
125  for (i=0; i<nbins; i++) {
126  if (strstr(countries[j],p->GetBinName(i+1))) {
127  h2->Fill(countries[j],p->GetBinContent(i+1));
128  h2->SetBinError(j, p->GetBinError(i+1));
129  }
130  }
131  }
132 
133  // Normalize the TH2Poly bin contents to the real surfaces.
134  Double_t scale = surfaces[0]/maximum;
135  for (i=0; i<nbins; i++) p->SetBinContent(i+1, scale*p->GetBinContent(i+1));
136 
137  gStyle->SetOptStat(1111);
138  gStyle->SetPalette(1);
139  p->Draw("COLZ");
140 
141  TCanvas *c1 = new TCanvas("c1", "c1",W+10,0,W-20,H);
142  c1->SetRightMargin(0.047);
143 
144  scale = h->GetMaximum()/h2->GetMaximum();
145 
146  h->SetStats(0);
147  h->SetLineColor(kRed-3);
148  h->SetLineWidth(2);
149  h->SetMarkerStyle(20);
150  h->SetMarkerColor(kBlue);
151  h->SetMarkerSize(0.8);
152  h->Draw("LP");
153  h->GetXaxis()->SetLabelFont(42);
154  h->GetXaxis()->SetLabelSize(0.03);
155  h->GetYaxis()->SetLabelFont(42);
156 
157  h2->Scale(scale);
158  Double_t scale2=TMath::Sqrt(scale);
159  for (i=0; i<nx; i++) h2->SetBinError(i+1, scale2*h2->GetBinError(i+1));
160  h2->Draw("E SAME");
161  h2->SetMarkerStyle(20);
162  h2->SetMarkerSize(0.8);
163 
164  TLegend *leg = new TLegend(0.5,0.67,0.92,0.8,NULL,"NDC");
165  leg->SetTextFont(42);
166  leg->SetTextSize(0.025);
167  leg->AddEntry(h,"Real countries surfaces from Wikipedia (in km^{2})","lp");
168  leg->AddEntry(h2,"Countries surfaces from TH2Poly (with errors)","lp");
169  leg->Draw();
170  leg->Draw();
171 
172  Double_t wikiSum = h->Integral();
173  Double_t polySum = h2->Integral();
174  Double_t error = TMath::Abs(wikiSum-polySum)/wikiSum;
175  printf("THPoly Europe surface estimation error wrt wikipedia = %f per cent when using %d points\n",100*error,npoints);
176 }
const int nx
Definition: kalman.C:16
virtual void SetGridx(Int_t value=1)
Definition: TPad.h:327
virtual void SetLineWidth(Width_t lwidth)
Definition: TAttLine.h:57
virtual void Scale(Double_t c1=1, Option_t *option="")
Multiply this histogram by a constant c1.
Definition: TH1.cxx:6174
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3159
Double_t GetBinContent(Int_t bin) const
Returns the content of the input bin For the overflow/underflow/sea bins: -1 | -2 | -3 ---+----+---- ...
Definition: TH2Poly.cxx:717
This class displays a legend box (TPaveText) containing several legend entries.
Definition: TLegend.h:35
virtual void ToggleEventStatus()
Toggle event statusbar.
Definition: TCanvas.cxx:2124
Double_t Log(Double_t x)
Definition: TMath.h:526
virtual Bool_t InheritsFrom(const char *classname) const
Returns kTRUE if object inherits from class "classname".
Definition: TObject.cxx:487
float Float_t
Definition: RtypesCore.h:53
Double_t GetBinError(Int_t bin) const
Returns the value of error associated to bin number bin.
Definition: TH2Poly.cxx:730
Definition: Rtypes.h:61
void ChangePartition(Int_t n, Int_t m)
Changes the number of partition cells in the histogram.
Definition: TH2Poly.cxx:416
virtual void SetContour(Int_t nlevels, const Double_t *levels=0)
Set the number and values of contour levels.
Definition: TH1.cxx:7863
R__EXTERN TStyle * gStyle
Definition: TStyle.h:423
virtual void Draw(Option_t *option="")
Draw this legend with its current attributes.
Definition: TLegend.cxx:373
Int_t GetNumberOfBins() const
Definition: TH2Poly.h:113
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
Definition: TFile.h:45
virtual void SetNdivisions(Int_t n=510, Bool_t optim=kTRUE)
Set the number of divisions for this axis.
Definition: TAttAxis.cxx:212
A TMultiGraph is a collection of TGraph (or derived) objects.
Definition: TMultiGraph.h:37
virtual void Show(const char *name)
Stops Benchmark name and Prints results.
Definition: TBenchmark.cxx:155
void SetStatX(Float_t x=0)
Definition: TStyle.h:391
1-D histogram with a float per channel (see TH1 documentation)}
Definition: TH1.h:570
int Int_t
Definition: RtypesCore.h:41
int nbins[3]
const char * GetBinName(Int_t bin) const
Returns the bin name.
Definition: TH2Poly.cxx:746
void SetStatY(Float_t y=0)
Definition: TStyle.h:392
Short_t Abs(Short_t d)
Definition: TMathBase.h:110
TFile * f
virtual void Reset(Option_t *option="")
Reset.
Definition: TH1.cxx:9501
TObject * Clone(const char *newname=0) const
Make a complete copy of the underlying object.
Definition: TH1.cxx:2565
virtual void SetLabelFont(Style_t font=62)
Set labels' font.
Definition: TAttAxis.cxx:166
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=1, Int_t netopt=0)
Create / open a file.
Definition: TFile.cxx:3851
virtual void SetTextFont(Font_t tfont=62)
Definition: TAttText.h:59
Double_t GetMaximum() const
Returns the maximum value of the histogram.
Definition: TH2Poly.cxx:766
virtual void Start(const char *name)
Starts Benchmark with the specified name.
Definition: TBenchmark.cxx:172
Double_t x[n]
Definition: legend1.C:17
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
void SetTitleX(Float_t x=0)
Definition: TStyle.h:407
Int_t AddBin(TObject *poly)
Adds a new bin to the histogram.
Definition: TH2Poly.cxx:202
virtual void SetMarkerColor(Color_t mcolor=1)
Definition: TAttMarker.h:51
Book space in a file, create I/O buffers, to fill them, (un)compress them.
Definition: TKey.h:30
virtual void SetBinError(Int_t bin, Double_t error)
see convention for numbering bins in TH1::GetBin
Definition: TH1.cxx:8528
virtual void SetLineColor(Color_t lcolor)
Definition: TAttLine.h:54
virtual Double_t Integral(Option_t *option="") const
Return integral of bin contents.
Definition: TH1.cxx:7378
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void Draw(Option_t *option="")
Draw this histogram with options.
Definition: TH1.cxx:2878
void SetBinContent(Int_t bin, Double_t content)
Sets the contents of the input bin to the input content Negative values between -1 and -9 are for the...
Definition: TH2Poly.cxx:1194
R__EXTERN TBenchmark * gBenchmark
Definition: TBenchmark.h:63
virtual void LabelsDeflate(Option_t *axis="X")
Reduce the number of bins for the axis passed in the option to the number of bins having a label...
Definition: TH1.cxx:4776
void SetStatW(Float_t w=0.19)
Definition: TStyle.h:393
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
TAxis * GetYaxis()
Definition: TH1.h:320
virtual void SetLabelSize(Float_t size=0.04)
Set size of axis labels The size is expressed in per cent of the pad width.
Definition: TAttAxis.cxx:187
virtual void SetMarkerSize(Size_t msize=1)
Definition: TAttMarker.h:54
Double_t Pi()
Definition: TMath.h:44
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
TLegendEntry * AddEntry(const TObject *obj, const char *label="", Option_t *option="lpf")
Add a new entry to this legend.
Definition: TLegend.cxx:280
virtual void SetGridy(Int_t value=1)
Definition: TPad.h:328
ClassImp(TMCParticle) void TMCParticle printf(": p=(%7.3f,%7.3f,%9.3f) ;", fPx, fPy, fPz)
Double_t y[n]
Definition: legend1.C:17
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
virtual void SetRightMargin(Float_t rightmargin)
Set Pad right margin in fraction of the pad width.
Definition: TAttPad.cxx:117
Mother of all ROOT objects.
Definition: TObject.h:58
virtual TObject * ReadObj()
To read a TObject* from the file.
Definition: TKey.cxx:727
static Bool_t SetCacheFileDir(const char *cacheDir, Bool_t operateDisconnected=kTRUE, Bool_t forceCacheread=kFALSE)
Sets the directory where to locally stage/cache remote files.
Definition: TFile.cxx:4371
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:1252
#define NULL
Definition: Rtypes.h:82
#define gDirectory
Definition: TDirectory.h:221
Definition: Rtypes.h:61
Double_t Sqrt(Double_t x)
Definition: TMath.h:464
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
virtual Double_t GetBinError(Int_t bin) const
Return value of error associated to bin number bin.
Definition: TH1.cxx:8395
virtual Double_t GetMaximum(Double_t maxval=FLT_MAX) const
Return maximum value smaller than maxval of bins in the range, unless the value has been overridden b...
Definition: TH1.cxx:7921
virtual void SetTitle(const char *title="")
Change (i.e. set) the title of the TNamed.
Definition: TNamed.cxx:152
void SetPalette(Int_t ncolors=kBird, Int_t *colors=0, Float_t alpha=1.)
See TColor::SetPalette.
Definition: TStyle.cxx:1445
virtual void SetStats(Bool_t stats=kTRUE)
Set statistics option on/off.
Definition: TH1.cxx:8320
Double_t Tan(Double_t)
Definition: TMath.h:427
Int_t Fill(Double_t x, Double_t y)
Increment the bin containing (x,y) by 1.
Definition: TH2Poly.cxx:564
TAxis * GetXaxis()
Definition: TH1.h:319
2D Histogram with Polygonal Bins
Definition: TH2Poly.h:70