Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist028_THStack_multicolor.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_hist
3/// \notebook
4/// \preview 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/// \date August 2016
13/// \author Rene Brun
14
15#include "TCanvas.h"
16#include "TH2.h"
17#include "THStack.h"
18#include "TRandom.h"
19
20void hist028_THStack_multicolor(Int_t isStack = 0)
21{
22 TCanvas *c1 = new TCanvas;
23 Int_t nbins = 20;
24 TH2F *h1 = new TH2F("h1", "h1", nbins, -4, 4, nbins, -4, 4);
25 h1->SetFillColor(kBlue);
26 TH2F *h2 = new TH2F("h2", "h2", nbins, -4, 4, nbins, -4, 4);
27 h2->SetFillColor(kRed);
28 TH2F *h3 = new TH2F("h3", "h3", nbins, -4, 4, nbins, -4, 4);
30 THStack *hs = new THStack("hs", "three plots");
31 hs->Add(h1);
32 hs->Add(h2);
33 hs->Add(h3);
34 TRandom r;
35 Int_t i;
36 for (i = 0; i < 20000; i++)
37 h1->Fill(r.Gaus(), r.Gaus());
38 for (i = 0; i < 200; i++) {
39 Int_t ix = (Int_t)r.Uniform(0, nbins);
40 Int_t iy = (Int_t)r.Uniform(0, nbins);
41 Int_t bin = h1->GetBin(ix, iy);
42 Double_t val = h1->GetBinContent(bin);
43 if (val <= 0)
44 continue;
45 if (!isStack)
46 h1->SetBinContent(bin, 0);
47 if (r.Rndm() > 0.5) {
48 if (!isStack)
49 h2->SetBinContent(bin, 0);
50 h3->SetBinContent(bin, val);
51 } else {
52 if (!isStack)
53 h3->SetBinContent(bin, 0);
54 h2->SetBinContent(bin, val);
55 }
56 }
57 hs->Draw("lego1");
58}
ROOT::R::TRInterface & r
Definition Object.C:4
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
@ kRed
Definition Rtypes.h:67
@ kBlue
Definition Rtypes.h:67
@ kYellow
Definition Rtypes.h:67
virtual void SetFillColor(Color_t fcolor)
Set the fill area color.
Definition TAttFill.h:40
The Canvas class.
Definition TCanvas.h:23
2-D histogram with a float per channel (see TH1 documentation)
Definition TH2.h:345
void SetBinContent(Int_t bin, Double_t content) override
Set bin content.
Definition TH2.cxx:2603
void Draw(Option_t *chopt="") override
Default Draw method for all objects.
virtual void Add(TH1 *h, Option_t *option="")
This is the base class for the ROOT Random number generators.
Definition TRandom.h:27
return c1
Definition legend1.C:41
TH1F * h1
Definition legend1.C:5