Segmentation Violation at TCanvas.SaveAs()

ROOT Version: Not Provided
Platform: lxplus
Compiler: Not Provided


Hi everyone,
I’m trying to make a Python code to plot some histograms. In detail, I have about 15 ROOT files containing many histograms all with the same name. I pass them from terminal, then the code take from any file the histogram (with the same name) and draw the overlap of them.

For shure there is a faster way to do that (I didn’t found sadly) but at a certain point the code go in Error

*** Break *** segmentation violation



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

Thread 2 (Thread 0x7fc15c4c5700 (LWP 2403)):
#0  0x00007fc1a27b4de2 in pthread_cond_timedwait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1  0x00007fc1a2d49465 in PyCOND_TIMEDWAIT (cond=0x7fc1a3140c88 <_PyRuntime+1184>, mut=0x7fc1a3140cb8 <_PyRuntime+1232>, us=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/condvar.h:73
#2  take_gil (ceval=0x7fc1a3140a30 <_PyRuntime+584>, tstate=0x6fa7ed0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval_gil.h:206
#3  0x00007fc1a2d49a10 in PyEval_RestoreThread (tstate=0x6fa7ed0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:399
#4  0x00007fc1a2defb76 in pysleep (secs=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Modules/timemodule.c:1850
#5  time_sleep (self=<optimized out>, obj=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Modules/timemodule.c:338
#6  0x00007fc1a2cc37c4 in cfunction_vectorcall_O (func=0x7fc1a32a8220, args=0x7fc12b7ae1c0, nargsf=<optimized out>, kwnames=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/methodobject.c:482
#7  0x00007fc1a2d537b1 in _PyObject_Vectorcall (callable=0x7fc1a32a8220, args=0x7fc12b7ae1c0, nargsf=9223372036854775809, kwnames=0x0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Include/cpython/abstract.h:127
#8  call_function (tstate=<optimized out>, pp_stack=0x7fc15c4c4668, oparg=<optimized out>, kwnames=0x0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:4963
#9  0x00007fc1a2d50669 in _PyEval_EvalFrameDefault (f=0x7fc12b7ae040, throwflag=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:3469
#10 0x00007fc1a2d54863 in PyEval_EvalFrameEx (f=0x7fc12b7ae040, throwflag=0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:741
#11 _PyEval_EvalCodeWithName (_co=<optimized out>, globals=<optimized out>, locals=<optimized out>, args=<optimized out>, argcount=<optimized out>, kwnames=0x7fc12b7ae1a8, kwargs=0x7fc1a32c04e0, kwcount=0, kwstep=1, defs=0x0, defcount=0, kwdefs=0x0, closure=0x0, name=0x7fc1341437b0, qualname=0x7fc134141730) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:4298
#12 0x00007fc1a2c861c9 in _PyFunction_Vectorcall (func=<optimized out>, stack=0xffffffffffffffff, nargsf=<optimized out>, kwnames=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:435
#13 0x00007fc1a2c85a39 in PyVectorcall_Call (callable=0x7fc1a32815e0, tuple=<optimized out>, kwargs=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:199
#14 0x00007fc1a2c85b18 in PyObject_Call (callable=<optimized out>, args=<optimized out>, kwargs=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:227
#15 0x00007fc1a2d5096d in do_call_core (tstate=0x6fa7ed0, func=0x7fc1a32815e0, callargs=0x7fc1a32c04c0, kwdict=0x7fc1a32205c0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:5010
#16 _PyEval_EvalFrameDefault (f=0x7fc1a317a200, throwflag=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:3559
#17 0x00007fc1a2c8604f in function_code_fastcall (co=<optimized out>, args=0x7fc14a2aadb8, nargs=<optimized out>, globals=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:283
#18 0x00007fc1a2d537b1 in _PyObject_Vectorcall (callable=0x7fc19b1e6280, args=0x7fc14a2aadb8, nargsf=9223372036854775809, kwnames=0x0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Include/cpython/abstract.h:127
#19 call_function (tstate=<optimized out>, pp_stack=0x7fc15c4c4b18, oparg=<optimized out>, kwnames=0x0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:4963
#20 0x00007fc1a2d5064b in _PyEval_EvalFrameDefault (f=0x7fc14a2aac40, throwflag=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:3486
#21 0x00007fc1a2c8604f in function_code_fastcall (co=<optimized out>, args=0x7fc130209538, nargs=<optimized out>, globals=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:283
#22 0x00007fc1a2d537b1 in _PyObject_Vectorcall (callable=0x7fc19b1e6550, args=0x7fc130209538, nargsf=9223372036854775809, kwnames=0x0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Include/cpython/abstract.h:127
#23 call_function (tstate=<optimized out>, pp_stack=0x7fc15c4c4cf8, oparg=<optimized out>, kwnames=0x0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:4963
#24 0x00007fc1a2d5064b in _PyEval_EvalFrameDefault (f=0x7fc1302093c0, throwflag=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/ceval.c:3486
#25 0x00007fc1a2c8604f in function_code_fastcall (co=<optimized out>, args=0x7fc15c4c4e28, nargs=<optimized out>, globals=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:283
#26 0x00007fc1a2c8874c in _PyObject_Vectorcall (callable=0x7fc19b1e6310, args=0x7fc15c4c4e28, nargsf=1, kwnames=0x0) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Include/cpython/abstract.h:127
#27 method_vectorcall (method=<optimized out>, args=0x7fc1a32ec058, nargsf=<optimized out>, kwnames=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/classobject.c:67
#28 0x00007fc1a2c85a39 in PyVectorcall_Call (callable=0x7fc12b7ac100, tuple=<optimized out>, kwargs=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:199
#29 0x00007fc1a2c85b18 in PyObject_Call (callable=<optimized out>, args=<optimized out>, kwargs=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Objects/call.c:227
#30 0x00007fc1a2df21d8 in t_bootstrap (boot_raw=0x7fc12b7a7d20) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Modules/_threadmodule.c:1002
#31 0x00007fc1a2da4496 in pythread_wrapper (arg=<optimized out>) at /workspace/build/externals/Python-3.8.6/src/Python/3.8.6/Python/thread_pthread.h:232
#32 0x00007fc1a27b0ea5 in start_thread () from /lib64/libpthread.so.0
#33 0x00007fc1a1dd0b0d in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7fc1a332c740 (LWP 2298)):
#0  0x00007fc1a1d97659 in waitpid () from /lib64/libc.so.6
#1  0x00007fc1a1d14f62 in do_system () from /lib64/libc.so.6
#2  0x00007fc1a1d15311 in system () from /lib64/libc.so.6
#3  0x00007fc145ab8092 in TUnixSystem::StackTrace() () from /cvmfs/sft.cern.ch/lcg/views/LCG_99/x86_64-centos7-clang10-opt/lib/libCore.so
#4  0x00007fc145f3b321 in (anonymous namespace)::TExceptionHandlerImp::HandleException(int) () from /cvmfs/sft.cern.ch/lcg/views/LCG_99/x86_64-centos7-clang10-opt/lib/libcppyy_backend3_8.so
#5  0x00007fc145abb972 in TUnixSystem::DispatchSignals(ESignals) () from /cvmfs/sft.cern.ch/lcg/views/LCG_99/x86_64-centos7-clang10-opt/lib/libCore.so
#6  <signal handler called>
#7  0x00007fc12afc6e06 in TLegend::PaintPrimitives() () from /cvmfs/sft.cern.ch/lcg/views/LCG_99/x86_64-centos7-clang10-opt/lib/libGraf.so
#8  0x00007fc12b2c26a2 in TPad::PaintModified() () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#9  0x00007fc12b2a68d8 in TCanvas::Update() () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#10 0x00007fc12b2c6336 in TPad::Print(char const*, char const*) () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#11 0x00007fc12b2c8b8c in TPad::SaveAs(char const*, char const*) const () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#12 0x00007fc1282c6072 in ?? ()
#13 0x0000000000000000 in ?? ()
===========================================================


The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum https://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
report at https://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.
===========================================================
#7  0x00007fc12afc6e06 in TLegend::PaintPrimitives() () from /cvmfs/sft.cern.ch/lcg/views/LCG_99/x86_64-centos7-clang10-opt/lib/libGraf.so
#8  0x00007fc12b2c26a2 in TPad::PaintModified() () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#9  0x00007fc12b2a68d8 in TCanvas::Update() () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#10 0x00007fc12b2c6336 in TPad::Print(char const*, char const*) () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#11 0x00007fc12b2c8b8c in TPad::SaveAs(char const*, char const*) const () from /cvmfs/sft.cern.ch/lcg/releases/ROOT/v6.22.06-71cc6/x86_64-centos7-clang10-opt/lib/libGpad.so
#12 0x00007fc1282c6072 in ?? ()
#13 0x0000000000000000 in ?? ()
===========================================================


Traceback (most recent call last):
  File "Plot.py", line 93, in <module>
    truthEta_c.SaveAs("truthEta.png")
cppyy.ll.SegmentationViolation: void TPad::SaveAs(const char* filename = "", const char* option = "") =>
    SegmentationViolation: segfault in C++; program state was reset

This is my horrible code:

# Import necessary modules
import argparse
import sys
import numpy as np
import os
import glob
import re
import ROOT
import matplotlib.pyplot as plt
# set ATLAS plot style
#from AtlasStyle import setStyle
#setStyle()

ROOT.gROOT.Reset()
ROOT.gROOT.ForceStyle();
ROOT.gStyle.SetOptStat(0)
#ROOT.gROOT.SetStyle("ATLAS")

def getOptions(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(description="Parses command.")
    parser.add_argument("-i", "--input", help="Input files (either comma-separated .root files or .txt list of files)", type=str, nargs="+")
    options = parser.parse_args(args)
    return options

options = getOptions()



#################################################################
truthPt_c = ROOT.TCanvas("truthPt_c", "pT of truth DP", 800, 600)
truthPt_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_pT")

    #val = val + 1
    hist.SetLineColor(i+1)
    truthPt_legend.AddEntry(hist, input_file)
    #plt.hist(hist, label=input_file, color=val, histtype='step')
    # Convert the histogram into a numpy array
    #bins = np.linspace(h1.GetXaxis().GetXmin(), h1.GetXaxis().GetXmax(), h1.GetNbinsX()+1)
    #bin_content = np.array([h1.GetBinContent(j) for j in range(1, h1.GetNbinsX()+1)])



    #plt.hist(bins[:-1], bins, label=input_file, weights=bin_content,  color=val, histtype='step', density=True)
    hist.Scale(1/hist.Integral())
    #hist.Draw("HIST")
    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)
    #truthPt_c.Update()
    #truthPt_c.Modified()
    #truthPt_legend.DrawClone()

    #plt.legend()
    #plt.show()
 #plt.savefig("truthPt.png")


#truthPt_legend.Draw()
# Save the overlapped histogram as a PNG file
truthPt_c.SaveAs("truthPt.png")
truthPt_c.Close()

###################################################################
truthEta_c = ROOT.TCanvas("truthEta_c", "Eta of truth DP", 800, 600)
truthEta_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_Eta")


    hist.SetLineColor(i)
    truthEta_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthEta_legend.Draw()
# Save the overlapped histogram as a PNG file
truthEta_c.SaveAs("truthEta.png")
truthEta_c.Close()


################################################################
truthPhi_c = ROOT.TCanvas("truthPhi_c", "Phi of truth DP", 800, 600)
truthPhi_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_Phi")


    hist.SetLineColor(i)
    truthPhi_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthPhi_legend.Draw()
# Save the overlapped histogram as a PNG file
truthPhi_c.SaveAs("truthPhi.png")
truthPhi_c.Close()


#################################################################
truthdpT_c = ROOT.TCanvas("truthdpT_c", "dpT of truth DP", 800, 600)
truthdpT_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_dpT")


    hist.SetLineColor(i)
    truthdpT_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthdpT_legend.Draw()
# Save the overlapped histogram as a PNG file
truthdpT_c.SaveAs("truthdpT.png")
truthdpT_c.Close()




#################################################################
truthdEta_c = ROOT.TCanvas("truthdEta_c", "dEta of truth DP", 800, 600)
truthdEta_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_dEta")


    hist.SetLineColor(i)
    truthdEta_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthdEta_legend.Draw()
# Save the overlapped histogram as a PNG file
truthdEta_c.SaveAs("truthdEta.png")
truthdEta_c.Close()


#################################################################
truthdPhi_c = ROOT.TCanvas("truthdPhi_c", "dPhi of truth DP", 800, 600)
truthdPhi_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_dPhi")


    hist.SetLineColor(i)
    truthdPhi_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthdPhi_legend.Draw()
# Save the overlapped histogram as a PNG file
truthdPhi_c.SaveAs("truthdPhi.png")
truthdPhi_c.Close()


#################################################################
truthdR_c = ROOT.TCanvas("truthdR_c", "DR of truth DP", 800, 600)
truthdR_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_dR")


    hist.SetLineColor(i)
    truthdR_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthdR_legend.Draw()
# Save the overlapped histogram as a PNG file
truthdR_c.SaveAs("truthdR.png")
truthdR_c.Close()


#################################################################
truthpTimb_c = ROOT.TCanvas("truthpTimb_c", "pT imbalace of truth DP", 800, 600)
truthpTimb_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_pT_imbalance")


    hist.SetLineColor(i)
    truthpTimb_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthpTimb_legend.Draw()
# Save the overlapped histogram as a PNG file
truthpTimb_c.SaveAs("truthpTimb.png")
truthpTimb_c.Close()

#################################################################
truthEChildPt_c = ROOT.TCanvas("truthEChildPt_c", "Electron Child pT", 800, 600)
truthEChildPt_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_EChildPt")


    hist.SetLineColor(i)
    truthEChildPt_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthEChildPt_legend.Draw()
# Save the overlapped histogram as a PNG file
truthEChildPt_c.SaveAs("truthEChildPt.png")
truthEChildPt_c.Close()

#################################################################
truthEChildEta_c = ROOT.TCanvas("truthEChildEta_c", "Electron Child Eta", 800, 600)
truthEChildEta_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_EChildEta")


    hist.SetLineColor(i)
    truthEChildEta_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthEChildEta_legend.Draw()
# Save the overlapped histogram as a PNG file
truthEChildEta_c.SaveAs("truthEChildEta.png")
truthEChildEta_c.Close()



#################################################################
truthEChildPhi_c = ROOT.TCanvas("truthEChildPhi_c", "Electron Child Phi", 800, 600)
truthEChildPhi_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_EChildPhi")


    hist.SetLineColor(i)
    truthEChildPhi_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthEChildPhi_legend.Draw()
# Save the overlapped histogram as a PNG file
truthEChildPhi_c.SaveAs("truthEChildPhi.png")
truthEChildPhi_c.Close()




#################################################################
truthEChildDR_c = ROOT.TCanvas("truthEChildDR_c", "Electron Child DR", 800, 600)
truthEChildDR_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_EChildDR")


    hist.SetLineColor(i)
    truthEChildDR_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthEChildDR_legend.Draw()
# Save the overlapped histogram as a PNG file
truthEChildDR_c.SaveAs("truthEChildDR.png")
truthEChildDR_c.Close()

#################################################################
truthEpTimb_c = ROOT.TCanvas("truthEpTimb_c", "Electron Child pT imbalance", 800, 600)
truthEpTimb_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_EpT_imbalance")


    hist.SetLineColor(i)
    truthEpTimb_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthEpTimb_legend.Draw()
# Save the overlapped histogram as a PNG file
truthEpTimb_c.SaveAs("truthEpTimb.png")
truthEpTimb_c.Close()






#################################################################
truthMUChildPt_c = ROOT.TCanvas("truthMUChildPt_c", "Muon Child pT", 800, 600)
truthMUChildPt_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_MUChildPt")


    hist.SetLineColor(i)
    truthMUChildPt_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthMUChildPt_legend.Draw()
# Save the overlapped histogram as a PNG file
truthMUChildPt_c.SaveAs("truthMUChildPt.png")
truthMUChildPt_c.Close()

#################################################################
truthMUChildEta_c = ROOT.TCanvas("truthMUChildEta_c", "Muon Child Eta", 800, 600)
truthMUChildEta_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_MUChildEta")


    hist.SetLineColor(i)
    truthMUChildEta_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthMUChildEta_legend.Draw()
# Save the overlapped histogram as a PNG file
truthMUChildEta_c.SaveAs("truthMUChildEta.png")
truthMUChildEta_c.Close()



#################################################################
truthMUChildPhi_c = ROOT.TCanvas("truthMUChildPhi_c", "Muon Child Phi", 800, 600)
truthMUChildPhi_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_MUChildPhi")


    hist.SetLineColor(i)
    truthMUChildPhi_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthMUChildPhi_legend.Draw()
# Save the overlapped histogram as a PNG file
truthMUChildPhi_c.SaveAs("truthMUChildPhi.png")
truthMUChildPhi_c.Close()




#################################################################
truthMUChildDR_c = ROOT.TCanvas("truthMUChildDR_c", "Muon Child DR", 800, 600)
truthMUChildDR_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_MUChildDR")


    hist.SetLineColor(i)
    truthMUChildDR_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthMUChildDR_legend.Draw()
# Save the overlapped histogram as a PNG file
truthMUChildDR_c.SaveAs("truthMUChildDR.png")
truthMUChildDR_c.Close()

#################################################################
truthMUpTimb_c = ROOT.TCanvas("truthMUpTimb_c", "Muon Child pT imbalance", 800, 600)
truthMUpTimb_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_MUpT_imbalance")


    hist.SetLineColor(i)
    truthEpTimb_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthMUpTimb_legend.Draw()
# Save the overlapped histogram as a PNG file
truthMUpTimb_c.SaveAs("truthMUpTimb.png")
truthMUpTimb_c.Close()













#################################################################
truthQUARKChildPt_c = ROOT.TCanvas("truthQUARKChildPt_c", "Quark Child pT", 800, 600)
truthQUARKChildPt_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_QUARKChildPt")


    hist.SetLineColor(i)
    truthQUARKChildPt_legend.AddEntry(hist, input_file)

    if hist.GetEntries()!=0 :
        hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthQUARKChildPt_legend.Draw()
# Save the overlapped histogram as a PNG file
truthQUARKChildPt_c.SaveAs("truthQUARKChildPt.png")
truthQUARKChildPt_c.Close()

#################################################################
truthQUARKChildEta_c = ROOT.TCanvas("truthQUARKChildEta_c", "Quark Child Eta", 800, 600)
truthQUARKChildEta_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_QUARKChildEta")


    hist.SetLineColor(i)
    truthQUARKChildEta_legend.AddEntry(hist, input_file)

    if hist.GetEntries()!=0 :
        hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthQUARKChildEta_legend.Draw()
# Save the overlapped histogram as a PNG file
truthQUARKChildEta_c.SaveAs("truthQUARKChildEta.png")
truthQUARKChildEta_c.Close()



#################################################################
truthQUARKChildPhi_c = ROOT.TCanvas("truthQUARKChildPhi_c", "Quark Child Phi", 800, 600)
truthQUARKChildPhi_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_QUARKChildPhi")


    hist.SetLineColor(i)
    truthQUARKChildPhi_legend.AddEntry(hist, input_file)

    if hist.GetEntries()!=0 :
        hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthQUARKChildPhi_legend.Draw()
# Save the overlapped histogram as a PNG file
truthQUARKChildPhi_c.SaveAs("truthQUARKChildPhi.png")
truthQUARKChildPhi_c.Close()




#################################################################
truthQUARKChildDR_c = ROOT.TCanvas("truthQUARKChildDR_c", "Quark Child DR", 800, 600)
truthQUARKChildDR_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_QUARKChildDR")


    hist.SetLineColor(i)
    truthQUARKChildDR_legend.AddEntry(hist, input_file)

    if hist.GetEntries()!=0 :
        hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthQUARKChildDR_legend.Draw()
# Save the overlapped histogram as a PNG file
truthQUARKChildDR_c.SaveAs("truthQUARKChildDR.png")
truthQUARKChildDR_c.Close()

#################################################################
truthQUARKpTimb_c = ROOT.TCanvas("truthMUpTimb_c", "Quark Child pT imbalance", 800, 600)
truthQUARKpTimb_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_QUARKpT_imbalance")


    hist.SetLineColor(i)
    truthEpTimb_legend.AddEntry(hist, input_file)

    if hist.GetEntries()!=0 :
        hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthQUARKpTimb_legend.Draw()
# Save the overlapped histogram as a PNG file
truthQUARKpTimb_c.SaveAs("truthQUARKpTimb.png")
truthQUARKpTimb_c.Close()

At any Run the SegmentationViolation happen in a different point of the code, so I suppose it’s a memory problem. The root files are correctly taken, the plots before the error are saved (I have also a problem with the TLegend that desn’t match colors, but it is a secondary problem).

I don’t know how to solve

So it is not necessary at SaveAs …
We cannot run your code. as we do not have your data.
Can you try to reduce your code in order to find the minimal reproducer ?

With minimal reproduced you say to reduce the code only for one plot? In that case, here it is:

import argparse
import sys
import numpy as np
import os
import glob
import re
import ROOT
import matplotlib.pyplot as plt
# set ATLAS plot style
#from AtlasStyle import setStyle
#setStyle()

ROOT.gROOT.Reset()
ROOT.gROOT.ForceStyle();
ROOT.gStyle.SetOptStat(0)


def getOptions(args=sys.argv[1:]):
    parser = argparse.ArgumentParser(description="Parses command.")
    parser.add_argument("-i", "--input", help="Input files (either comma-separated .root files or .txt list of files)", type=str, nargs="+")
    options = parser.parse_args(args)
    return options

options = getOptions()

truthEta_c = ROOT.TCanvas("truthEta_c", "Eta of truth DP", 800, 600)
truthEta_legend = ROOT.TLegend(0.7, 0.7, 0.9, 0.9)

# Loop over the input ROOT files
for i, input_file in enumerate(options.input):

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("h_truthDP_Eta")


    hist.SetLineColor(i)
    truthEta_legend.AddEntry(hist, input_file)


    hist.Scale(1/hist.Integral())

    # Draw the histogram on the canvas
    option = "" if i == 0 else "same"
    hist.DrawCopy(option)


truthEta_legend.Draw()
# Save the overlapped histogram as a PNG file
truthEta_c.SaveAs("truthEta.png")
truthEta_c.Close()

And I use this structure for any histogram

Do you get a crash with that reduced code too?

No it works, except TLegend that plot the same colour but it is for shure a logical problem that I can fix by myself

Ah ok, that’s not a minimal reproducer of the crash in that case. You can maybe start from the crashing program and progressively reduce it until it does not crash, That way you will identify which part of your code provokes the crash.

I’ll try but if I run the same code multiple times without changing anything it goes into SegmentationViolation in different places. If I don’t solve it, I can make many codes, one for each histogram, but it would be very annoying…

Sorry again, If I divide the code in multiple codes it works, but you can easily understand that is so uncomfortable. Can someone suggest how to make it faster and secure in a single code?

You very likely have either: code overwriting, uninitialized variables, or arrays out of bounds … As we cannot run your code because of missing data, it is hard to tell more.

Yeah I know. Can you give me some hint of how to write a code for what I have to do? Because, based of what I understand, I have to open a different Canvas for each plot, same for a Legend, and that brings to have many many Canvas that probably can be avoided in some way. I’m learning these days ROOT so I have to improve

After each “draw” statement, try to add:
ROOT.gPad.Modified(); ROOT.gPad.Update()

Also, after each

    file = ROOT.TFile.Open(input_file)
    hist = file.Get("somehisto")

try adding sometihng like

    if not hist:
        print ('could not retrieve','somehisto','from',input_file)

@Wile_E_Coyote it gives me:
AttributeError: Failed to get attribute gPad from ROOT
I’m on lxplus, I don’t know if this changes something.

@dastudillo
I tried yesterday, the histograms are all correctly taken

Try, e.g.:
python -c 'import ROOT; c = ROOT.TCanvas(); ROOT.gPad.Modified(); ROOT.gPad.Update()'

Nada, but “works” if I do canvas_name.Modified() and canvas_name.Update() without ROOT.gPad. Work in the sense that run but I have the same problems before, so at a certain point goes in SegmentationViolation (this time at the second histogram, a record! The run before at the last one, it’s so strange)

If someone is interested, the problem was with reading the name of the histograms. So I made a loop where I saved all histograms name in an array and then worked with it, not directly with Get()

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