Pyroot code filling THStack from json does not fill canvas properly

Hi

I have a simple pyRoot code that read a json and fills two THStack histos. However, the canvas is still empty . Any ideas?

import ROOT
import json
from   ROOT import gROOT, TCanvas, TFile, TGraphErrors, TMath,  SetOwnership, TColor, kYellow, kGreen, kWhite, kMagenta, kCyan, kBlue, kOrange, kTeal
import math, sys, optparse, array, time, copy

ROOT.gROOT.SetBatch(ROOT.kTRUE)
# Load the JSON data from file
with open('my_file.json', 'r') as f:
    data = json.load(f)

canvas = ROOT.TCanvas('canvas', 'canvas', 200, 100)

# Create a legend
legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Create a list of colors

#colors = {'qcd': ROOT.kRed, 'top': ROOT.kGreen, 'ew': ROOT.kBlue, 'ewk': ROOT.kOrange}

colors = {'dy':kYellow, 'qcd':kMagenta, 'qcdpt':kMagenta, 'top':kBlue, 'ew':kGreen+2, 'ewk':kCyan, 'ewknlo':kCyan+1, 'ewknlo61':kCyan+1, 'ewkincl':kTeal, 'ewkincl61':kTeal-4}

canvas = ROOT.TCanvas('canvas', 'canvas')

stacks=[]
for i, entry in enumerate(data):

    # Create THStack
    label = entry['selection']
    stack = ROOT.THStack('stack_{0:s}'.format(label), '')
    # Set x-axis label
    #stack.GetXaxis().SetTitle(label)

    # Loop over each key in the entry
    for key, value in entry.items():
        if key == 'selection':
            continue

        # Create histogram and fill with color based on key
        hist = ROOT.TH1F(key, '', 100, 0, 1)
        hist.SetFillColor(colors[key])
        hist.Fill(value)
        if i==0 :
            legend.AddEntry(hist, key, 'l')

        # Add histogram to THStack
        stack.Add(hist)

    # Add THStack to list of stacks
    stacks.append(stack)

# Draw first THStack and set y-axis range
canvas.cd()
legend.Draw()
stacks[0].Draw('hist')
stacks[0].SetMaximum(10**8)

# Loop over remaining THStacks and draw them on top of each other
for stack in stacks[1:]:
    stack.Draw('hist same')

canvas.Update()
# Save canvas as PDF
canvas.SaveAs('output.pdf')

edit: what I need, is to have a canvas, and in each bin one THStack histo which is read from json data

Your code works ony changing the actual data, since we don’t have your JSON file:

import ROOT
import json
from   ROOT import gROOT, TCanvas, TFile, TGraphErrors, TMath,  SetOwnership, TColor, kYellow, kGreen, kWhite, kMagenta, kCyan, kBlue, kOrange, kTeal
import math, sys, optparse, array, time, copy

ROOT.gROOT.SetBatch(ROOT.kTRUE)
canvas = ROOT.TCanvas('canvas', 'canvas', 200, 100)
# Create a legend
legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)
# Create a list of colors
colors = {'dy':kYellow, 'qcd':kMagenta, 'qcdpt':kMagenta, 'top':kBlue, 'ew':kGreen+2, 'ewk':kCyan, 'ewknlo':kCyan+1, 'ewknlo61':kCyan+1, 'ewkincl':kTeal, 'ewkincl61':kTeal-4}
canvas = ROOT.TCanvas('canvas', 'canvas')
stacks=[]
for i in range(2):
    # Create THStack
    stack = ROOT.THStack('stack_{0:d}'.format(i), '')
    if i==0:
        h1st = ROOT.TH1F("h1st","",100,-4,4)
        h1st.FillRandom("gaus",20000)
        h1st.SetFillColor(colors['dy'])
        stack.Add(h1st)
        h2st = ROOT.TH1F("h2st","",100,-4,4)
        h2st.FillRandom("gaus",15000)
        h2st.SetFillColor(colors['qcd'])
        stack.Add(h2st)
        h3st = ROOT.TH1F("h3st","",100,-4,4)
        h3st.FillRandom("gaus",10000)
        h3st.SetFillColor(colors['top'])
        stack.Add(h3st)
    else:
        h1st = ROOT.TH1F("h1st2","",100,-4,4)
        h1st.FillRandom("gaus",4000)
        h1st.SetFillColor(colors['ew'])
        stack.Add(h1st)
        h2st = ROOT.TH1F("h2st2","",100,-4,4)
        h2st.FillRandom("gaus",3000)
        h2st.SetFillColor(colors['ewk'])
        stack.Add(h2st)
        h3st = ROOT.TH1F("h3st2","",100,-4,4)
        h3st.FillRandom("gaus",2000)
        h3st.SetFillColor(colors['ewknlo'])
        stack.Add(h3st)
    # Add THStack to list of stacks
    stacks.append(stack)

# Draw first THStack and set y-axis range
canvas.cd()
legend.Draw()
stacks[0].Draw('hist')
#stacks[0].SetMaximum(10**8)

# Loop over remaining THStacks and draw them on top of each other
for stack in stacks[1:]:
    stack.Draw('hist same')
canvas.Update()
# Save canvas as PDF
canvas.SaveAs('output.pdf')

giving

So, first check that the data inside the file is correct, and that you are reading it correctly.

thanks, the json looks something like that though

[    {        "ewk": 33727009.8515625,        "selection": "winclwnjets_qcdht_nlepeq1_njetsgt0_massgt80_nbtagl_noveto_hitslt2",        "top": 266047.99072265625,        "dy": 1690008.9763183594,            "qcd": 3617608.293273926,        "ew": 362401.93505859375    },    {        "ewk": 13727009.8515625,        "selection": "winclwnjets_qcdht_nlepeq1_njetsgt0_massgt0_nbtagl_noveto_hitslt2",            "top": 366047.99072265625,        "dy": 690008.9763183594,        "qcd": 617608.293273926,        "ew": 362401.93505859375    }]