Drawing histograms from different root files to a single canvase

Hey guys,

I’m pretty new to using root and having some trouble with a script in python.

I’m creating a canvas then loop to { load a file, get a histogram from that file, draw the histogram to the canvas} then exit the loop and update and save the canvas as a pdf

Here’s the relevant portion of code sans histogram formatting:

  for histType, xaxis_label in zip(histTypeList, xaxis_labelList):
    plotnumber = 0
    c = ROOT.TCanvas()
    leg = ROOT.TLegend(0.8,0.8,0.9,0.9)

    for filename in filelist:
      plotnumber += 1
      f = ROOT.TFile(filename)
      hist = f.Get(os.path.join(selection, objType, histType))
	
      if plotnumber == 1:
        hist.Draw('hist')
      else:
        hist.Draw('hist same')
    leg.Draw()

    c.Modified()
    c.Update()
    c.SaveAs('kinematic_{0:s}_{1:s}.pdf'.format(objType, histType))
    c.Clear()

Here’s the error I’m getting:

Traceback (most recent call last):
File “kinematic_plot_Dijets_Wprime.py”, line 80, in
c.Update()
SystemError: void TCanvas::Update() =>
problem in C++; program state has been reset

If I run the code with only a single file it runs fine.

Any help would be appreciated – thanks!!

Could you post a script we can run showing the issue? And do you have a crash/stack trace when this happens?

Here is the crash trace – let me just check that I am okay to post the entire code, I’m not the only contributor – thanks!

[@lxplus006 Wprime_Dijets]$ python kinematic_plot_Dijets_Wprime.py

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================

Thread 2 (Thread 0x7f3dcd070700 (LWP 16283)):
#0  0x00007f3de5744a00 in sem_wait () from /lib64/libpthread.so.0
#1  0x00007f3de5a9c118 in PyThread_acquire_lock () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#2  0x00007f3de5a59676 in PyEval_RestoreThread () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:359
#3  0x00007f3dced93228 in time_sleep () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/lib/python2.7/lib-dynload/time.so
#4  0x00007f3de5a6009c in PyEval_EvalFrameEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:4352
#5  0x00007f3de5a63fc0 in PyEval_EvalCodeEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:3584
#6  0x00007f3de59da695 in function_call () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#7  0x00007f3de59a8723 in PyObject_Call () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#8  0x00007f3de5a5cbe3 in PyEval_EvalFrameEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:4666
#9  0x00007f3de5a60017 in PyEval_EvalFrameEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:4437
#10 0x00007f3de5a60017 in PyEval_EvalFrameEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:4437
#11 0x00007f3de5a63fc0 in PyEval_EvalCodeEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:3584
#12 0x00007f3de59da5cc in function_call () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#13 0x00007f3de59a8723 in PyObject_Call () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#14 0x00007f3de59b731d in instancemethod_call () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Objects/classobject.c:2602
#15 0x00007f3de59a8723 in PyObject_Call () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#16 0x00007f3de5a59d57 in PyEval_CallObjectWithKeywords () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:4221
#17 0x00007f3de5aa1b52 in t_bootstrap () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#18 0x00007f3de573eaa1 in start_thread () from /lib64/libpthread.so.0
#19 0x00007f3de4e00bcd in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f3de5f62700 (LWP 16243)):
#0  0x00007f3de4dc486d in waitpid () from /lib64/libc.so.6
#1  0x00007f3de4d56479 in do_system () from /lib64/libc.so.6
#2  0x00007f3de4d567b0 in system () from /lib64/libc.so.6
#3  0x00007f3ddd188c5a in TUnixSystem::StackTrace() () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libCore.so
#4  0x00007f3ddd18ac2c in TUnixSystem::DispatchSignals(ESignals) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libCore.so
#5  <signal handler called>
#6  0x00007f3ddd0b3f50 in TAttFill::Copy(TAttFill&) const () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libCore.so
#7  0x00007f3dc7d86a7d in TLegend::PaintPrimitives() () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libGraf.so
#8  0x00007f3dcc224cc9 in TPad::PaintModified() () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libGpad.so
#9  0x00007f3dcc1e73a6 in TCanvas::Update() () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libGpad.so
#10 0x00007f3dcc0e5024 in __cf_33 ()
#11 0x00007f3dd7fa0cef in TClingCallFunc::exec(void*, void*) const () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libCling.so
#12 0x00007f3dde7723cf in PyROOT::TVoidExecutor::Execute(long, void*, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#13 0x00007f3dde76a9bc in PyROOT::TMethodHolder::CallSafe(void*, long, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#14 0x00007f3dde7696aa in PyROOT::TMethodHolder::Execute(void*, long, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#15 0x00007f3dde768315 in PyROOT::TMethodHolder::Call(PyROOT::ObjectProxy*, _object*, _object*, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#16 0x00007f3dde7575f5 in PyROOT::(anonymous namespace)::mp_call(PyROOT::MethodProxy*, _object*, _object*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#17 0x00007f3de59a8723 in PyObject_Call () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#18 0x00007f3de5a5fc39 in PyEval_EvalFrameEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:4569
#19 0x00007f3de5a63fc0 in PyEval_EvalCodeEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:3584
#20 0x00007f3de5a640e9 in PyEval_EvalCode () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:669
#21 0x00007f3de5a887ba in PyRun_FileExFlags () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#22 0x00007f3de5a89ba7 in PyRun_SimpleFileExFlags () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#23 0x00007f3de5a9fd05 in Py_Main () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#24 0x00007f3de4d36d1d in __libc_start_main () from /lib64/libc.so.6
#25 0x00000000004006d9 in _start ()
===========================================================


The lines below might hint at the cause of the crash.
If they do not help you then please submit a bug report at
http://root.cern.ch/bugs. Please post the ENTIRE stack trace
from above as an attachment in addition to anything else
that might help us fixing this issue.
===========================================================
#6  0x00007f3ddd0b3f50 in TAttFill::Copy(TAttFill&) const () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libCore.so
#7  0x00007f3dc7d86a7d in TLegend::PaintPrimitives() () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libGraf.so
#8  0x00007f3dcc224cc9 in TPad::PaintModified() () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libGpad.so
#9  0x00007f3dcc1e73a6 in TCanvas::Update() () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libGpad.so
#10 0x00007f3dcc0e5024 in __cf_33 ()
#11 0x00007f3dd7fa0cef in TClingCallFunc::exec(void*, void*) const () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libCling.so
#12 0x00007f3dde7723cf in PyROOT::TVoidExecutor::Execute(long, void*, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#13 0x00007f3dde76a9bc in PyROOT::TMethodHolder::CallSafe(void*, long, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#14 0x00007f3dde7696aa in PyROOT::TMethodHolder::Execute(void*, long, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#15 0x00007f3dde768315 in PyROOT::TMethodHolder::Call(PyROOT::ObjectProxy*, _object*, _object*, PyROOT::TCallContext*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#16 0x00007f3dde7575f5 in PyROOT::(anonymous namespace)::mp_call(PyROOT::MethodProxy*, _object*, _object*) () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/root/6.04.14-x86_64-slc6-gcc49-opt/lib/libPyROOT.so
#17 0x00007f3de59a8723 in PyObject_Call () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#18 0x00007f3de5a5fc39 in PyEval_EvalFrameEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:4569
#19 0x00007f3de5a63fc0 in PyEval_EvalCodeEx () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:3584
#20 0x00007f3de5a640e9 in PyEval_EvalCode () at /var/build/jenkins/workspace/lcg_release_tar/BUILDTYPE/Release/COMPILER/gcc49/LABEL/slc6-physical/build/externals/Python-2.7.13/src/Python/2.7.13/Python/ceval.c:669
#21 0x00007f3de5a887ba in PyRun_FileExFlags () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#22 0x00007f3de5a89ba7 in PyRun_SimpleFileExFlags () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#23 0x00007f3de5a9fd05 in Py_Main () from /cvmfs/atlas.cern.ch/repo/ATLASLocalRootBase/x86_64/python/2.7.13-x86_64-slc6-gcc49/2.7.13-597a5/x86_64-slc6-gcc49-opt/bin/../lib/libpython2.7.so.1.0
#24 0x00007f3de4d36d1d in __libc_start_main () from /lib64/libc.so.6
#25 0x00000000004006d9 in _start ()
===========================================================


Traceback (most recent call last):
  File "kinematic_plot_Dijets_Wprime.py", line 80, in <module>
    c.Update()
SystemError: void TCanvas::Update() =>
    problem in C++; program state has been reset

Thanks. And no need the entire code if you can post a simple reproducer…

So, while stripping down the so that I could provide something here I just happened to catch the line of code that was causing the error ( it was leg.Draw() ).

However, now I’m having an issue where the histograms are not plotting to the same canvas. As the code runs, I can see each get plotted but in the only the last one plotted remains instead of three. Here’s the code:

import ROOT
import os

wmasses = [400, 1500]
wprime_filelist = ['hist-user.cylin.L1CaloSimu.Wprime_WZqqqq_m{0:d}.tag-00-00-17_OUTPUT.root'.format(i) for i in wmasses]
dijet_filelist = ['hist-user.cylin.L1CaloSimu.JZW.tag-00-00-17.root']
filelist = wprime_filelist + dijet_filelist
selection = 'postsel'
objTypeList = ['gTowers', 'gTowers_central', 'gBlocks_central']
histTypeList = ['Pt_0', 'Pt', 'Energy', 'Energy_0', 'Mass', 'Eta', 'Phi']
xaxis_labelList = ['p_{T} [GeV]', 'p_{T} [GeV]', 'Energy [GeV]', 'Energy [GeV]', 'Mass [GeV]', '#eta', '#phi']

for objType in objTypeList:

  for histType, xaxis_label in zip(histTypeList, xaxis_labelList):
    plotnumber = 0
    c = ROOT.TCanvas()

    for filename, drawstyle in zip(filelist, drawlist):
      plotnumber += 1
      f = ROOT.TFile(filename)
      hist = f.Get(os.path.join(selection, objType, histType))
      if plotnumber == 1:
        hist.Draw('hist')
      else:
        hist.Draw('hist same')

    c.Modified()
    c.Update()
    c.SaveAs('kinematic_{0:s}_{1:s}.pdf'.format(objType, histType))
    c.Clear()

Well, if you create a TCanvas c = ROOT.TCanvas() inside a loop, a new one will be created at each iteration…

RIght but there should be a new canvas/pdf for each member of objType that contains the overlaid plots from each member of filename

The script doesn’t work:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "test.py", line 19, in <module>
    for filename, drawstyle in zip(filelist, drawlist):
NameError: name 'drawlist' is not defined

Ah, I’m sorry – drawlist was leftover from when I tried to see if the if/else statement was to blame and should have been removed so only filename is being looped over

I was actually just posting at the time of your reply:

I’m not sure that I understand why but I created an initial, separate loop to open all of the files into a list rather than opening them repeatedly in the nested-most loop and it solved all of my issues… maybe it’s a memory leak issue?

Anyway, thank you so much for your help and speedy responses!

OK, fine, but I still can’t run it, I don’t have the files…

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