Skip to content

Commit

Permalink
Merge pull request #11 from DUNE/feature_examples
Browse files Browse the repository at this point in the history
Initial versions of (py)root examples
  • Loading branch information
krwood authored Jul 13, 2022
2 parents 8f63184 + 4f370b5 commit fb3c400
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 16 deletions.
72 changes: 65 additions & 7 deletions examples/root/cpp/example.C
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
///

#include "TCanvas.h"
#include "TColor.h"
#include "TH1D.h"

#include "DUNEStyle.h"
Expand All @@ -13,9 +14,9 @@ void example()
TCanvas c;

// 1D histogram example
TH1D h1D("example1d", ";x label;y label", 100, -5, 5);
h1D.FillRandom("gaus",10000);
TLegend leg(0.6,0.7,0.8,0.85);
TH1D h1D("example1d", ";x label;y label", 50, -5, 5);
h1D.FillRandom("gaus",1000);
TLegend leg(0.6,0.65,0.8,0.8);
leg.AddEntry("example1d","1D histogram","l");
h1D.Draw();
leg.Draw();
Expand All @@ -24,6 +25,50 @@ void example()
dunestyle::SimulationSide();
c.Print("example.root.pdf(");

// 1D data/mc comparison type plot
c.Clear();
TH1D* h1D_ratio = (TH1D*)h1D.Clone("h1D_ratio");
TPad p1("p1","p1",0.,0.35,1.,1.);
TPad p2("p2","p2",0.,0.,1.,0.35);
p1.SetBottomMargin(0.04);
p1.SetTopMargin(0.15);
p2.SetBottomMargin(0.3);
p2.SetTopMargin(0.04);
c.cd(); p1.Draw(); p1.cd();
h1D.GetXaxis()->SetLabelSize(0.);
h1D_ratio->GetXaxis()->SetTitleOffset(1.25);
h1D.GetXaxis()->SetTickLength(1./0.65*h1D.GetXaxis()->GetTickLength());
h1D.GetXaxis()->SetLabelSize(1./0.65*h1D.GetXaxis()->GetLabelSize());
h1D.GetYaxis()->SetLabelSize(1./0.65*h1D.GetYaxis()->GetLabelSize());
h1D.GetXaxis()->SetTitleSize(1./0.65*h1D.GetXaxis()->GetTitleSize());
h1D.GetYaxis()->SetTitleSize(1./0.65*h1D.GetYaxis()->GetTitleSize());
h1D.GetYaxis()->SetTitleOffset(0.65*h1D.GetYaxis()->GetTitleOffset());
h1D.GetXaxis()->SetTitleOffset(0.65*h1D.GetXaxis()->GetTitleOffset());
h1D_ratio->GetXaxis()->SetTickLength(1./0.65*h1D_ratio->GetXaxis()->GetTickLength()*6.5/3.5);
h1D_ratio->GetXaxis()->SetLabelSize(1./0.65*h1D_ratio->GetXaxis()->GetLabelSize()*6.5/3.5);
h1D_ratio->GetYaxis()->SetLabelSize(1./0.65*h1D_ratio->GetYaxis()->GetLabelSize()*6.5/3.5);
h1D_ratio->GetXaxis()->SetTitleSize(1./0.65*h1D_ratio->GetXaxis()->GetTitleSize()*6.5/3.5);
h1D_ratio->GetYaxis()->SetTitleSize(1./0.65*h1D_ratio->GetYaxis()->GetTitleSize()*6.5/3.5);
h1D_ratio->GetYaxis()->SetTitleOffset(0.65*h1D_ratio->GetYaxis()->GetTitleOffset()*3.5/6.5);
h1D_ratio->GetXaxis()->SetTitleOffset(h1D_ratio->GetXaxis()->GetTitleOffset()*3.5/6.5);
h1D_ratio->GetYaxis()->SetTitle("ratio to fit");
leg.Clear();
h1D.Fit("gaus");
h1D.Draw("E");
TF1* fit = h1D.GetFunction("gaus");
leg.AddEntry(&h1D,"data","lep");
leg.AddEntry(fit,"fit","l");
leg.Draw();
h1D_ratio->Sumw2();
h1D_ratio->Divide(fit);
TF1 one("one","1.",-5,5);
dunestyle::CornerLabel("MC/Data Comparison Example");
c.cd(); p2.Draw(); p2.cd();
h1D_ratio->GetYaxis()->SetRangeUser(0.,2.);
h1D_ratio->Draw("E");
one.Draw("same");
c.Print("example.root.pdf");

// 2D histogram example
c.Clear();
TH2D h2D("example2d", ";x label;y label", 100, -5, 5, 100, -5, 5);
Expand All @@ -32,18 +77,31 @@ void example()
h2D.Draw("colz");
dunestyle::CenterTitles(&h2D);
dunestyle::Simulation();
dunestyle::CornerLabel("Neutrino beam");
dunestyle::CornerLabel("2D Histogram Example");
c.Print("example.root.pdf");

// 2D contour example
c.Clear();
double levels[3] = {500,5000,25000};
leg.Clear();
double level1 = 500.;
double level2 = 5000.;
double level3 = 25000.;
double levels[3] = {level1,level2,level3};
h2D.SetContour(3,levels);
TPaletteAxis *palette = (TPaletteAxis*)h2D.GetListOfFunctions()->FindObject("palette");
TH1I l1_h("l1_h","l1_h",1,0,1); l1_h.SetFillColor(palette->GetValueColor(h2D.GetContourLevel(0)));
//TH1I l1_h("l1_h","l1_h",1,0,1); l1_h.SetFillColor(palette->GetValueColor(h2D.GetContourLevel(1))); // doesn't work?
TH1I l2_h("l2_h","l2_h",1,0,1); l2_h.SetFillColor(palette->GetValueColor((h2D.GetContourLevel(2)-h2D.GetContourLevel(0))/2.));
TH1I l3_h("l3_h","l3_h",1,0,1); l3_h.SetFillColor(palette->GetValueColor(h2D.GetContourLevel(2)));
leg.AddEntry(&l1_h,"level 1 contour","f");
leg.AddEntry(&l2_h,"level 2 contour","f");
leg.AddEntry(&l3_h,"level 3 contour","f");
h2D.Draw("cont1");
leg.Draw();
dunestyle::CenterTitles(&h2D);
dunestyle::Simulation();
dunestyle::SimulationSide();
dunestyle::CornerLabel("Neutrino beam");
dunestyle::CornerLabel("2D Contour Example");
c.Print("example.root.pdf");

// stacked histogram
Expand All @@ -65,7 +123,7 @@ void example()
leg.AddEntry("hs2","two hist","f");
leg.AddEntry("hs3","three hist","f");
leg.Draw();
dunestyle::CornerLabel("Stacked histograms");
dunestyle::CornerLabel("Stacked Histograms Example");
c.Print("example.root.pdf)");

}
134 changes: 125 additions & 9 deletions examples/root/python/example.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,131 @@
"""
'''
PyROOT placeholder example. we can do way better than this.
"""

'''
import ROOT
import numpy as np

import dunestyle.root as dunestyle
#import dunestyle.root as dunestyle
import dunestyle as dunestyle

c = ROOT.TCanvas()
h = ROOT.TH1D("example", ";x label;y label", 500, -5, 5)
h.FillRandom("gaus")
h.Draw()
dunestyle.CenterTitles(h)

# 1D histogram example
h1D = ROOT.TH1D('example1d', ';x label;y label', 50, -5, 5)
h1D.FillRandom('gaus',1000)
leg = ROOT.TLegend(0.6,0.65,0.8,0.8)
leg.AddEntry('example1d','1D histogram','l')
h1D.Draw()
leg.Draw()
dunestyle.CenterTitles(h1D)
dunestyle.WIP()
c.SaveAs("example.root.png")
dunestyle.SimulationSide()
c.Print('example.pyroot.pdf(')

# 1D data/mc comparison type plot
c.Clear()
h1D_ratio = h1D.Clone('h1D_ratio')
p1 = ROOT.TPad('p1','p1',0.,0.35,1.,1.)
p2 = ROOT.TPad('p2','p2',0.,0.,1.,0.35)
p1.SetBottomMargin(0.04)
p1.SetTopMargin(0.15)
p2.SetBottomMargin(0.3)
p2.SetTopMargin(0.04)
c.cd(); p1.Draw(); p1.cd()
h1D.GetXaxis().SetLabelSize(0.)
h1D_ratio.GetXaxis().SetTitleOffset(1.25)
h1D.GetXaxis().SetTickLength(1./0.65*h1D.GetXaxis().GetTickLength())
h1D.GetXaxis().SetLabelSize(1./0.65*h1D.GetXaxis().GetLabelSize())
h1D.GetYaxis().SetLabelSize(1./0.65*h1D.GetYaxis().GetLabelSize())
h1D.GetXaxis().SetTitleSize(1./0.65*h1D.GetXaxis().GetTitleSize())
h1D.GetYaxis().SetTitleSize(1./0.65*h1D.GetYaxis().GetTitleSize())
h1D.GetYaxis().SetTitleOffset(0.65*h1D.GetYaxis().GetTitleOffset())
h1D.GetXaxis().SetTitleOffset(0.65*h1D.GetXaxis().GetTitleOffset())
h1D_ratio.GetXaxis().SetTickLength(1./0.65*h1D_ratio.GetXaxis().GetTickLength()*6.5/3.5)
h1D_ratio.GetXaxis().SetLabelSize(1./0.65*h1D_ratio.GetXaxis().GetLabelSize()*6.5/3.5)
h1D_ratio.GetYaxis().SetLabelSize(1./0.65*h1D_ratio.GetYaxis().GetLabelSize()*6.5/3.5)
h1D_ratio.GetXaxis().SetTitleSize(1./0.65*h1D_ratio.GetXaxis().GetTitleSize()*6.5/3.5)
h1D_ratio.GetYaxis().SetTitleSize(1./0.65*h1D_ratio.GetYaxis().GetTitleSize()*6.5/3.5)
h1D_ratio.GetYaxis().SetTitleOffset(0.65*h1D_ratio.GetYaxis().GetTitleOffset()*3.5/6.5)
h1D_ratio.GetXaxis().SetTitleOffset(h1D_ratio.GetXaxis().GetTitleOffset()*3.5/6.5)
h1D_ratio.GetYaxis().SetTitle('ratio to fit')
leg.Clear()
h1D.Fit('gaus')
h1D.Draw('E')
fit = h1D.GetFunction('gaus')
leg.AddEntry(h1D,'data','lep')
leg.AddEntry(fit,'fit','l')
leg.Draw()
h1D_ratio.Sumw2()
h1D_ratio.Divide(fit)
one = ROOT.TF1('one','1.',-5,5)
dunestyle.CornerLabel('MC/Data Comparison Example')
c.cd(); p2.Draw(); p2.cd()
h1D_ratio.GetYaxis().SetRangeUser(0.,2.)
h1D_ratio.Draw('E')
one.Draw('same')
c.Print('example.pyroot.pdf')

# 2D histogram example
c.Clear()
h2D = ROOT.TH2D('example2d', ';x label;y label', 100, -5, 5, 100, -5, 5)
mean = (0,0)
cov = [[0.5,-0.5],[-0.5,1]]
throws = np.random.multivariate_normal(mean, cov, 10000000)
for throw in throws: h2D.Fill(throw[0],throw[1])
h2D.Draw('colz')
dunestyle.CenterTitles(h2D)
dunestyle.Simulation()
dunestyle.CornerLabel('2D Histogram Example')
c.Print('example.pyroot.pdf')

# 2D contour example
c.Clear()
leg.Clear()
level1 = 500.
level2 = 5000.
level3 = 25000.
levels = np.array([level1,level2,level3])
h2D.SetContour(3,levels)
palette = h2D.GetListOfFunctions().FindObject('palette')
l1_h = ROOT.TH1I('l1_h','l1_h',1,0,1)
l1_h.SetFillColor(palette.GetValueColor(h2D.GetContourLevel(0)))
# doesn't work?
#l1_h = ROOT.TH1I('l1_h','l1_h',1,0,1)
#l1_h.SetFillColor(palette.GetValueColor(h2D.GetContourLevel(1)))
l2_h = ROOT.TH1I('l2_h','l2_h',1,0,1)
l2_h.SetFillColor(palette.GetValueColor((h2D.GetContourLevel(2)-h2D.GetContourLevel(0))/2.))
l3_h = ROOT.TH1I('l3_h','l3_h',1,0,1)
l3_h.SetFillColor(palette.GetValueColor(h2D.GetContourLevel(2)))
leg.AddEntry(l1_h,'level 1 contour','f')
leg.AddEntry(l2_h,'level 2 contour','f')
leg.AddEntry(l3_h,'level 3 contour','f')
h2D.Draw('cont1')
leg.Draw()
dunestyle.CenterTitles(h2D)
dunestyle.Simulation()
dunestyle.SimulationSide()
dunestyle.CornerLabel('2D Contour Example')
c.Print('example.pyroot.pdf')

# stacked histogram
c.Clear()
leg.Clear()
hstack = ROOT.THStack('examplestack', ';x label;y label')
hs1 = ROOT.TH1D('hs1', ';x label;y label', 100, -5, 5)
hs2 = ROOT.TH1D('hs2', ';x label;y label', 100, -5, 5)
hs3 = ROOT.TH1D('hs3', ';x label;y label', 100, -5, 5)
hs1.FillRandom('gaus',10000)
hs2.FillRandom('gaus',5000)
hs3.FillRandom('gaus',1000)
hstack.Add(hs1)
hstack.Add(hs2)
hstack.Add(hs3)
hstack.Draw('pfc')
leg.SetHeader('Stacked Histograms')
leg.AddEntry('hs1','one hist','f')
leg.AddEntry('hs2','two hist','f')
leg.AddEntry('hs3','three hist','f')
leg.Draw()
dunestyle.CornerLabel('Stacked Histograms Example')
c.Print('example.pyroot.pdf)')

0 comments on commit fb3c400

Please sign in to comment.