Write() method causing segfault on 4.00-04

I am running a script that reads a TTree from a file connected at runtime and creates histograms
from these TTrees. I then want to open another file and write these histograms to that file for use later on. It all goes well until I try to write the histograms to this new file via
[size=150] HISTOGRAM->Write();[/size]

The strange part about this is that this script worked flawlessly under ROOT Ver. 3.10-02
butI get this segault in version 4.00-04.

The salient parts of the script are as follows:
executed from command line: .x <FILE_WITH_TTREES> acceptance.C

{
:
:
TH1D *HISTOGRAM = new TH1D("…");
:
:
//LOOP OVER TTREE AND FILL HISTOGRAM
:
:
//CREATE NEW FILE AND TRY TO WRITE TO IT
TFile *acc_plots = new TFile(“acc_plots.root”,“RECREATE”);
[size=150][color=red]HISTOGRAM->Write(); //THIS IS WHERE IT SEGFAULTS[/color][/size]
:
:
}

the core dump is as follows:

[color=blue]*** Break *** segmentation violation
Generating stack trace…
0xb59d5c28 in TBuffer::WriteFastArray(void**, TClass const*, int, bool, TMemberStreamer*) + 0x98 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a98e2b in TStreamerInfo::WriteBufferAux(TBuffer&, char**, int, int, int, int) + 0x261b from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a6d3f5 in TClass::WriteBuffer(TBuffer&, void*, char const*) + 0x179 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb53ae1f7 in TAxis::Streamer(TBuffer&) + 0x2cd from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb5a6d58b in TClass::Streamer(void*, TBuffer&) + 0x12d from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a6d7f2 in TClass::Streamer(void*, TBuffer&) + 0x394 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb59d5b7a in TBuffer::WriteFastArray(void*, TClass const*, int, TMemberStreamer*) + 0x7e from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a99218 in TStreamerInfo::WriteBufferAux(TBuffer&, char**, int, int, int, int) + 0x2a08 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a6d3f5 in TClass::WriteBuffer(TBuffer&, void*, char const*) + 0x179 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb53eb019 in TH1::Streamer(TBuffer&) + 0x4d5 from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb553cf66 in TH1::StreamerNVirtual(TBuffer&) + 0x24 from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb54836e0 in from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb5729610 in G__CallFunc::Exec(void*) + 0x7c from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb5a75e3c in TMethodCall::Execute(void*) + 0x98 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a7938d in TStreamerBase::WriteBuffer(TBuffer&, char*) + 0x7d from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a99397 in TStreamerInfo::WriteBufferAux(TBuffer&, char**, int, int, int, int) + 0x2b87 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a6d3f5 in TClass::WriteBuffer(TBuffer&, void*, char const*) + 0x179 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb53fede5 in TH2::Streamer(TBuffer&) + 0x10f from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb553d396 in TH2::StreamerNVirtual(TBuffer&) + 0x24 from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb54a6400 in from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb5729610 in G__CallFunc::Exec(void*) + 0x7c from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb5a75e3c in TMethodCall::Execute(void*) + 0x98 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a7938d in TStreamerBase::WriteBuffer(TBuffer&, char*) + 0x7d from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a99397 in TStreamerInfo::WriteBufferAux(TBuffer&, char**, int, int, int, int) + 0x2b87 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a6d3f5 in TClass::WriteBuffer(TBuffer&, void*, char const*) + 0x179 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb54059aa in TH2D::Streamer(TBuffer&) + 0x1ba from /apps/root/4.00-08-gcc3.2.3/root/lib/libHist.so
0xb59efc54 in TKey::TKey[in-charge](TObject const*, char const*, int) + 0x29e from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb59e047f in TDirectory::WriteTObject(TObject const*, char const*, char const*) + 0x24d from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a014a8 in TObject::Write(char const*, int, int) + 0x1b4 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5bcdea3 in from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb56a9312 in G__call_cppfunc + 0x2c3 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb5696e7c in G__interpret_func + 0x7c8 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb56756b7 in G__getfunction + 0x160d from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb570df7e in G__getstructmem + 0xa2a from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb57077a5 in G__getvariable + 0x62b from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb566bdeb in G__getitem + 0x712 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb566a596 in G__getexpr + 0x9ab6 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb56c136b in G__exec_function + 0x1f2 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb56c89a4 in G__exec_statement + 0x252b from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb564fba7 in G__exec_tempfile_core + 0x371 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb564fe43 in G__exec_tempfile + 0x31 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb56d12a6 in G__process_cmd + 0x485d from /apps/root/4.00-08-gcc3.2.3/root/lib/libCint.so
0xb5a60224 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) + 0xb4 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb5a60363 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) + 0x4f from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb59c95d6 in TApplication::ProcessFile(char const*, int*) + 0x8ba from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb59c8c94 in TApplication::ProcessLine(char const*, bool, int*) + 0x542 from /apps/root/4.00-08-gcc3.2.3/root/lib/libCore.so
0xb495bc8a in TRint::Run(bool) + 0x1e0 from /apps/root/4.00-08-gcc3.2.3/root/lib/libRint.so
0x08048898 in main + 0x90 from /apps/root/4.00-08-gcc3.2.3/root/bin/root.exe
0xb47a179d in __libc_start_main + 0xed from /lib/tls/libc.so.6
0x08048771 in TObject::operator delete(void*) + 0x31 from /apps/root/4.00-08-gcc3.2.3/root/bin/root.exe[/color]

is this an issue of scope?
thanks so much
sorry for the detailed novella of a post
:smiley:
Joe

It looks like you are creating the histogram in the directory corresponding to the input file, the you close this file.
At this point the histogram will be deleted too.

I suggest:
-creating the output file
-creating the histogram
-opening the input file, reading it, filling the histogram
-close the input file
-cd to the output file
-write the histogram

See chapter about “Object OwnerShip” in the Users Guide.

Rene

Hi Rene,
Thanks for your quick response. That the histogram was being deleted after the call
to:

[color=red]TFile *acc_plots = new TFile(“acc_plots.root”,“RECREATE”);[/color]

had occured to me. This is why I tested to see if HISTOGRAM still existed via:

[color=red]HISTOGRAM->Print();
[/color]
sure enough it was still there:

[color=red]TH1.Print Name= HISTOGRAM, Entries= 8162, Total sum= 20.4778[/color]

so I’m not so sure that it’s a deletion problem. Like I said, this snippet of code worked
flawlessly under version 3.10. Perhaps ver 4.00-08 is more strict about object ownership.
Joe

To make progress, I need the shortest running script reproducing the problem.

Rene

Hi Rene,

I’ve attached the barebones script color=red[/color]
as well as a sample root file with the proper tree to test it color=red[/color]
Thanks very much in advance for looking at this for me.

Joe
acceptanceDEBUG.C (1.39 KB)
TEST.root (17.3 KB)

I cannot reproduce your problem with version 4.01/04

Rene

I supposed then this is solely a version[color=red] 4.00-xx[/color] phenomenon.
Since the Jlab computer center is slow in updating the to the latest version of root,
I do not have version [color=red]4.01-xx[/color] at my disposal unless I install/compile it myself.
I’ll just revert to version 3.10-xx until 4.10-xx becomes available here.
Thanks for your help.
Joe