Bug: Reading pickled histograms in v5.34

Dear Colleauges,

when updating to the recent version of ROOT some of my scripts reading TH1 histograms from “pickle” (docs.python.org/library/pickle.html) files started failing with error messages like:

Warning in <TClass::TClass>: no dictionary for class TH1F is available Error in <TBufferFile::CheckByteCount>: object of class TH1F read too few bytes: 2 instead of 558

This problem is not present in v5.32 and can be circumvented by “initializing” ROOT before reading the pickle also in v5.34.

To verify you can use this snipptet to create a test-file…

[code]import pickle
import ROOT

Create Histogram

h = ROOT.TH1F(“h”,“h”, 11, -0.5, 10.5)
[h.Fill(i) for i in range(11)]

Dump it into file

f_out = open(“test.dat”, “w” )
pickle.dump( h, f_out)

…and this to read it:

[code]import pickle
import ROOT

Un-comment the line below for workaround


Read histogram from file

f_in = open(“test.dat”, “r” )
h = pickle.load(f_in)
print h.GetMean()

As mentioned above the script will work in v5.32 in both version but only with the additional “SetPalette” command in v5.34. It would be great if this could be fixed again for future versions.

Below is a bash-script to test everything using cern-afs (you might actually want to put the python scripts in a directory where you have writing-rights):

[code]export MYROOTVERSION=5.34.00

cd /afs/cern.ch/sw/lcg/external/gcc/4.6.2/x86_64-slc5-gcc46-opt/
source setup.sh
cd /afs/cern.ch/sw/lcg/app/releases/ROOT/$MYROOTVERSION/x86_64-slc5-gcc46-opt/root
export PYTHONDIR=/afs/cern.ch/sw/lcg/external/Python/2.7.3/x86_64-slc5-gcc46-opt/
source bin/thisroot.sh
alias python27=/afs/cern.ch/sw/lcg/external/Python/2.7.3/x86_64-slc5-gcc46-opt/bin/python2.7
python27 /afs/cern.ch/user/g/gregor/public/pickle_test_create.py
python27 /afs/cern.ch/user/g/gregor/public/pickle_test_read.py
python27 /afs/cern.ch/user/g/gregor/public/pickle_test_read2.py[/code]

Thank You,


thanks for reporting.

You don’t need to call SetPalette: just accessing gStyle is sufficient. On initial import, ROOT is’t completely initialized, to allow certain initialization such as setting it to batch. Touching any class or variable other than gROOT will kick in the initialization.

Fix is in v5-34 patches and will follow soon in trunk as well.