THStack is not shown

Hi experts,

I divided the canvas into 4 small canvas, for each smaller canvas, they are divided into top and bottom pad. But when I try to draw the signal and background THStack on the top pad, it’s not shown and also the legend. I have done the test, the signal hist of the THStack can be drawn. In the picture, the black dots stand for data hist. The Blue hist can be ignored. And the signal and background hists are not blank.
The related code part is shown and the whole code is attached.

Could you please give me some advice? Thank you very much .

  1     stack_signal = ROOT.THStack("stack_signal","stack_signal mode")
  0     stack_background = ROOT.THStack("stack_background","stack_background mode")                                                                                                                                                                                                                                     
  1     
  2     stack_signal.Add(hist_ggH)
  3     stack_signal.Add(hist_VBFH)
  4     stack_signal.Add(hist_VH)
  5     stack_signal.Add(hist_ttH)
  6     
  7     stack_background.Add(hist_Zll_jet)
  8     stack_background.Add(hist_ll_gamma)
  9     
 10     
 11     #  print(hist_ggH.GetName())
 12     
 13     stack_background.Draw("h")
 14     #  hist_ggH.Draw("h, same")
 15     #  stack_background.SetMinimum(0)
 16     #  stack_background.SetMaximum(1000)
 17     #  stack_signal.Draw("hist,same,nostack")
 18     stack_signal.Draw("h,NOCLEAR,same")
 19     #  stack_signal.Draw("h,same")
 20     #  hist_data.Draw("PE same")
 21     if new_iso_tight is not None:
 22         new_iso_tight.Draw("P,same")
 23         new_iso_tight.SetMarkerColor(ROOT.kBlue)
 24     
 25     if i==0:
 26         stack_background.GetYaxis().SetTitle("Events / 4 GeV")
 27         stack_background.GetYaxis().SetTitleOffset(0.7)
 28         stack_background.GetYaxis().SetTitleSize(0.07)
 29     if i==19:
 30         stack_background.GetYaxis().SetTitle("Events / 0.32 ")
 31         stack_background.GetYaxis().SetTitleOffset(0.7)
 32         stack_background.GetYaxis().SetTitleSize(0.07)
 33     if i==21 or i==22:
 34         stack_background.GetYaxis().SetTitle("Events / 1.00 GeV")
 35         stack_background.GetYaxis().SetTitleOffset(0.7)
 36         stack_background.GetYaxis().SetTitleSize(0.07)
 37     if i==25:
 38         stack_background.GetYaxis().SetTitle("Events / 0.16 ")
 39         stack_background.GetYaxis().SetTitleOffset(0.7)
 40         stack_background.GetYaxis().SetTitleSize(0.07)
 41     if i==27 or i==28:
 42         stack_background.GetYaxis().SetTitle("Events / 4 GeV")
 43         stack_background.GetYaxis().SetTitleOffset(0.7)
 44         stack_background.GetYaxis().SetTitleSize(0.07)
 45     if i==30:
 46         stack_background.GetYaxis().SetTitle("Events / 2 GeV")
 47         stack_background.GetYaxis().SetTitleOffset(0.7)
 48         stack_background.GetYaxis().SetTitleSize(0.07)
 49     
 50     #  #  stack_background.GetXaxis().SetLabelSize(0)
 51     #  #  stack_background.GetXaxis().SetTitleSize(0)
 52     
 53     
 54     sig_content1 = hist_ggH.GetBinContent(hist_ggH.GetMaximumBin())
 55     sig_content2 = hist_VBFH.GetBinContent(hist_VBFH.GetMaximumBin())
 56     sig_content3 = hist_VH.GetBinContent(hist_VH.GetMaximumBin())
 57     sig_content4 = hist_ttH.GetBinContent(hist_ttH.GetMaximumBin())
 58     sig_content = sig_content1 + sig_content2 + sig_content3 + sig_content4
 59     
 60     bkg_content1 = hist_Zll_jet.GetBinContent(hist_Zll_jet.GetMaximumBin())
 61     bkg_content2 = hist_ll_gamma.GetBinContent(hist_ll_gamma.GetMaximumBin())
 62     bkg_content = bkg_content1 + bkg_content2
 63     
 64     
 65     hist_data.Draw("PE same")



get_and_draw_jets_to_gamma_fake_factor.py (21.5 KB)

Hi,

Thanks for the complete post.
We’d need a minimal reproducer of the behaviour: do you manage to reproduce the problem with a simpler script, w/o private input files (e.g. ATLAS private data that cannot be shared), and one single canvas?

Without that, helping out will be hard.

One thing is that the THStacks instances seem to be created in the drawing function draw_hist and we might be in presence of some memory ownership issue.

Cheers,
D

Hello @xiangjun,

I agree with Danilo above that it’s hard to find out without being able to run it, but I have one idea that you could check:
Is there maybe a histogram that has one bin at Infinity or NaN? This probably cannot be drawn, so you could check the bin contents of every histogram in a for-loop to check if they are finite numbers.

Hi Danilo and StephanH,

Thank you very much! I have done the test, the signal and background hists do have the normal content. The strange thing is that if I don’t use function to draw just copying the code from the function to the positon of executing draw_hist function. it’s fine.

I have simplified it, you can test it by

python3 test_3_temp.py

test_3_temp.py (15.5 KB)

Run2_data_blind_iso_tight.root (1.9 MB)
Run2_data_blind_iso_loose.root (1.8 MB)
Run2_data_blind_noniso_loose.root (1.8 MB)
Run2_data_blind_noniso_tight.root (1.8 MB)

Hi,

But on this plot legend is displayed.
Or there should be something else.

Which ROOT version you are using?

Regards,
Sergey

Hi ,

I’m using ROOT 6.32.06

With ROOT 6.32 I see correct image with legend inside.

Can you provide macro demonstrating your problem?

This strongly points to a lifetime issue between Python and C++. Could it be that you create the objects in Python, and you ask these to be added to the stack? In this case, Python might think that the objects are no longer in use, and will garbage-collect them.

A fix would be to put them e.g. in a list that survives the end of the function, like so:

histosThatNeedToSurvive = []

def plottingFunc:
  hist = ...
  stack.Add(hist)
  histosThatNeedToSurvive.Append(hist)
  ...

PS: You might have to do the same for the THStack object itself, if you create it in the function, and you want it to be visible after the function ends.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.