Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
hist028_THStack_multicolor.C File Reference

Detailed Description

View in nbviewer Open in SWAN
Use a THStack to show a 2-D hist with cells with different colors.

root > .x multicolor.C
root > .x multicolor.C(1)
#include "TCanvas.h"
#include "TH2.h"
#include "THStack.h"
#include "TRandom.h"
void hist028_THStack_multicolor(Int_t isStack = 0)
{
TCanvas *c1 = new TCanvas;
Int_t nbins = 20;
TH2F *h1 = new TH2F("h1", "h1", nbins, -4, 4, nbins, -4, 4);
h1->SetFillColor(kBlue);
TH2F *h2 = new TH2F("h2", "h2", nbins, -4, 4, nbins, -4, 4);
TH2F *h3 = new TH2F("h3", "h3", nbins, -4, 4, nbins, -4, 4);
THStack *hs = new THStack("hs", "three plots");
hs->Add(h1);
hs->Add(h2);
hs->Add(h3);
Int_t i;
for (i = 0; i < 20000; i++)
h1->Fill(r.Gaus(), r.Gaus());
for (i = 0; i < 200; i++) {
Int_t ix = (Int_t)r.Uniform(0, nbins);
Int_t iy = (Int_t)r.Uniform(0, nbins);
Int_t bin = h1->GetBin(ix, iy);
Double_t val = h1->GetBinContent(bin);
if (val <= 0)
continue;
if (!isStack)
h1->SetBinContent(bin, 0);
if (r.Rndm() > 0.5) {
if (!isStack)
h2->SetBinContent(bin, 0);
h3->SetBinContent(bin, val);
} else {
if (!isStack)
h3->SetBinContent(bin, 0);
h2->SetBinContent(bin, val);
}
}
hs->Draw("lego1");
}
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
Date
August 2016
Author
Rene Brun

Definition in file hist028_THStack_multicolor.C.