Logo ROOT  
Reference Guide
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 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);
h2->SetFillColor(kRed);
TH2F *h3 = new TH2F("h3","h3",nbins,-4,4,nbins,-4,4);
h3->SetFillColor(kYellow);
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");
}
Author
Rene Brun

Definition in file multicolor.C.

TH2F
2-D histogram with a float per channel (see TH1 documentation)}
Definition: TH2.h:251
THStack
Definition: THStack.h:38
r
ROOT::R::TRInterface & r
Definition: Object.C:4
THStack::Add
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:361
TRandom.h
Int_t
int Int_t
Definition: RtypesCore.h:45
TH1::SetBinContent
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content see convention for numbering bins in TH1::GetBin In case the bin number is greater th...
Definition: TH1.cxx:8677
TCanvas.h
h1
TH1F * h1
Definition: legend1.C:5
TH1::GetBinContent
virtual Double_t GetBinContent(Int_t bin) const
Return content of bin number bin.
Definition: TH1.cxx:4906
TRandom
Definition: TRandom.h:27
TH1::GetBin
virtual Int_t GetBin(Int_t binx, Int_t biny=0, Int_t binz=0) const
Return Global bin number corresponding to binx,y,z.
Definition: TH1.cxx:4808
THStack.h
TH1::Fill
virtual Int_t Fill(Double_t x)
Increment bin with abscissa X by 1.
Definition: TH1.cxx:3274
kRed
@ kRed
Definition: Rtypes.h:66
TH2.h
Double_t
double Double_t
Definition: RtypesCore.h:59
TCanvas
Definition: TCanvas.h:23
kBlue
@ kBlue
Definition: Rtypes.h:66
THStack::Draw
virtual void Draw(Option_t *chopt="")
Draw this multihist with its current attributes.
Definition: THStack.cxx:447
kYellow
@ kYellow
Definition: Rtypes.h:66
TH2::SetBinContent
virtual void SetBinContent(Int_t bin, Double_t content)
Set bin content.
Definition: TH2.cxx:2480
c1
return c1
Definition: legend1.C:41