ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
multicolor.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_hist
3 /// Use a THStack to show a 2-D hist with cells with different colors.
4 /// ~~~ {.cpp}
5 /// root > .x multicolor.C
6 /// root > .x multicolor.C(1)
7 /// ~~~
8 /// \macro_image
9 /// \macro_code
10 ///
11 /// \author Rene Brun
12 
13 #include "TCanvas.h"
14 #include "TH2.h"
15 #include "THStack.h"
16 #include "TRandom.h"
17 
18 void multicolor(Int_t stack=0) {
19  TCanvas *c1 = new TCanvas;
20  Int_t nbins = 20;
21  TH2F *h1 = new TH2F("h1","h1",nbins,-4,4,nbins,-4,4);
22  h1->SetFillColor(kBlue);
23  TH2F *h2 = new TH2F("h2","h2",nbins,-4,4,nbins,-4,4);
24  h2->SetFillColor(kRed);
25  TH2F *h3 = new TH2F("h3","h3",nbins,-4,4,nbins,-4,4);
26  h3->SetFillColor(kYellow);
27  THStack *hs = new THStack("hs","three plots");
28  hs->Add(h1);
29  hs->Add(h2);
30  hs->Add(h3);
31  TRandom r;
32  Int_t i;
33  for (i=0;i<20000;i++) h1->Fill(r.Gaus(),r.Gaus());
34  for (i=0;i<200;i++) {
35  Int_t ix = (Int_t)r.Uniform(0,nbins);
36  Int_t iy = (Int_t)r.Uniform(0,nbins);
37  Int_t bin = h1->GetBin(ix,iy);
38  Double_t val = h1->GetBinContent(bin);
39  if (val <= 0) continue;
40  if (!stack) h1->SetBinContent(bin,0);
41  if (r.Rndm() > 0.5) {
42  if (!stack) h2->SetBinContent(bin,0);
43  h3->SetBinContent(bin,val);
44  } else {
45  if (!stack) h3->SetBinContent(bin,0);
46  h2->SetBinContent(bin,val);
47  }
48  }
49  hs->Draw("lego1");
50 }
51 
52 
53 
The Histogram stack class.
Definition: THStack.h:35
virtual Double_t Rndm(Int_t i=0)
Machine independent random number generator.
Definition: TRandom.cxx:512
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
Definition: Rtypes.h:61
int Int_t
Definition: RtypesCore.h:41
Definition: Rtypes.h:61
virtual void Draw(Option_t *chopt="")
Draw this multihist with its current attributes.
Definition: THStack.cxx:422
This is the base class for the ROOT Random number generators.
Definition: TRandom.h:29
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH2.h:90
ROOT::R::TRInterface & r
Definition: Object.C:4
virtual void SetFillColor(Color_t fcolor)
Definition: TAttFill.h:50
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:256
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:961
The Canvas class.
Definition: TCanvas.h:48
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:336
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:2699
Int_t Fill(Double_t)
Invalid Fill method.
Definition: TH2.cxx:287