Logo ROOT   6.07/09
Reference Guide
multicolor.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// \notebook
4 /// Use a THStack to show a 2-D hist with cells with different colors.
5 /// ~~~{.cpp}
6 /// root > .x multicolor.C
7 /// root > .x multicolor.C(1)
8 /// ~~~
9 /// \macro_image
10 /// \macro_code
11 ///
12 /// \author Rene Brun
13 
14 #include "TCanvas.h"
15 #include "TH2.h"
16 #include "THStack.h"
17 #include "TRandom.h"
18 
19 void multicolor(Int_t isStack=0) {
20  TCanvas *c1 = new TCanvas;
21  Int_t nbins = 20;
22  TH2F *h1 = new TH2F("h1","h1",nbins,-4,4,nbins,-4,4);
23  h1->SetFillColor(kBlue);
24  TH2F *h2 = new TH2F("h2","h2",nbins,-4,4,nbins,-4,4);
25  h2->SetFillColor(kRed);
26  TH2F *h3 = new TH2F("h3","h3",nbins,-4,4,nbins,-4,4);
27  h3->SetFillColor(kYellow);
28  THStack *hs = new THStack("hs","three plots");
29  hs->Add(h1);
30  hs->Add(h2);
31  hs->Add(h3);
32  TRandom r;
33  Int_t i;
34  for (i=0;i<20000;i++) h1->Fill(r.Gaus(),r.Gaus());
35  for (i=0;i<200;i++) {
36  Int_t ix = (Int_t)r.Uniform(0,nbins);
37  Int_t iy = (Int_t)r.Uniform(0,nbins);
38  Int_t bin = h1->GetBin(ix,iy);
39  Double_t val = h1->GetBinContent(bin);
40  if (val <= 0) continue;
41  if (!isStack) h1->SetBinContent(bin,0);
42  if (r.Rndm() > 0.5) {
43  if (!isStack) h2->SetBinContent(bin,0);
44  h3->SetBinContent(bin,val);
45  } else {
46  if (!isStack) h3->SetBinContent(bin,0);
47  h2->SetBinContent(bin,val);
48  }
49  }
50  hs->Draw("lego1");
51 }
52 
53 
54 
The Histogram stack class.
Definition: THStack.h:35
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition: TRandom.cxx:235
return c1
Definition: legend1.C:41
Definition: Rtypes.h:61
int Int_t
Definition: RtypesCore.h:41
int nbins[3]
Definition: Rtypes.h:61
virtual void Draw(Option_t *chopt="")
Draw this multihist with its current attributes.
Definition: THStack.cxx:431
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:31
TH1F * h1
Definition: legend1.C:5
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
virtual Double_t Rndm()
Machine independent random number generator.
Definition: TRandom.cxx:512
TRandom2 r(17)
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition: TAttFill.h:42
tomato 2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:255
virtual Int_t GetBin(Int_t binx, Int_t biny, Int_t binz=0) const
Return Global bin number corresponding to binx,y,z.
Definition: TH2.cxx:966
The Canvas class.
Definition: TCanvas.h:41
double Double_t
Definition: RtypesCore.h:55
virtual void Add(TH1 *h, Option_t *option="")
add a new histogram to the list Only 1-d and 2-d histograms currently supported.
Definition: THStack.cxx:345
virtual Double_t Uniform(Double_t x1=1)
Returns a uniform deviate on the interval (0, x1).
Definition: TRandom.cxx:606
Definition: Rtypes.h:61
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH2.cxx:2475
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:292
THist< 2, float, THistStatContent, THistStatUncertainty > TH2F
Definition: THist.hxx:308