Delete histogram from rootfile

Hello altogether

And a second problem, which I can’t resolve. I want to delete a number of histogramms in a root file:

TFile *rootfile = new TFile(rootFilePath.c_str(), "update"); cout << "Removing old result-Histogramms from rootfile " << endl; vector<TH1*> histo = rootutils::searchHistos(rootfile); for(unsigned int i =0; i < histo.size();i++){ delete histo[i]; } cout << "Everything removed, closing rootfile " << endl; rootfile->Close();

The function rootutils::searchHistos(rootfile) returns me a list of TH1* pointers to all the histogramms I want to delete. However as soon as I try to run a macro with this code I get:

b1@devubuntu:~/coding/fitpdhs/src$ root -l testReconstruction.cc root [0] Processing testReconstruction.cc... Removing old result-Histogramms from rootfile *** glibc detected *** /home/b1/root/bin/root.exe: invalid fastbin entry (free): 0x0000000002c78830 *** ======= Backtrace: ========= /lib/libc.so.6(+0x774b6)[0x7ff1c56024b6] /lib/libc.so.6(cfree+0x73)[0x7ff1c5608c83] /home/b1/root/lib/libCint.so(G__exec_statement+0x147a)[0x7ff1c6bf7bea] /home/b1/root/lib/libCint.so(+0x118164)[0x7ff1c6c02164] /home/b1/root/lib/libCint.so(G__exec_statement+0x5fff)[0x7ff1c6bfc76f] /home/b1/root/lib/libCint.so(G__interpret_func+0x2602)[0x7ff1c6bb03f2] /home/b1/root/lib/libCint.so(G__getfunction+0x19e9)[0x7ff1c6b9f279] /home/b1/root/lib/libCint.so(G__getitem+0x7ea)[0x7ff1c6b7e77a] /home/b1/root/lib/libCint.so(G__getexpr+0x556a)[0x7ff1c6b84e0a] /home/b1/root/lib/libCint.so(G__getexpr+0x8cd6)[0x7ff1c6b88576] /home/b1/root/lib/libCint.so(G__exec_statement+0x26b8)[0x7ff1c6bf8e28] /home/b1/root/lib/libCint.so(G__interpret_func+0x2602)[0x7ff1c6bb03f2] /home/b1/root/lib/libCint.so(G__getfunction+0x1908)[0x7ff1c6b9f198] /home/b1/root/lib/libCint.so(G__interpret_func+0x3340)[0x7ff1c6bb1130] /home/b1/root/lib/libCint.so(G__getfunction+0x1908)[0x7ff1c6b9f198] /home/b1/root/lib/libCint.so(G__getitem+0x7ea)[0x7ff1c6b7e77a] /home/b1/root/lib/libCint.so(G__getexpr+0x556a)[0x7ff1c6b84e0a] /home/b1/root/lib/libCint.so(G__define_var+0x32d4)[0x7ff1c6b714a4] /home/b1/root/lib/libCint.so(G__exec_statement+0x47f8)[0x7ff1c6bfaf68] /home/b1/root/lib/libCint.so(G__interpret_func+0x2602)[0x7ff1c6bb03f2] /home/b1/root/lib/libCint.so(G__getfunction+0x19e9)[0x7ff1c6b9f279] /home/b1/root/lib/libCint.so(G__getitem+0x7ea)[0x7ff1c6b7e77a] /home/b1/root/lib/libCint.so(G__getexpr+0x556a)[0x7ff1c6b84e0a] /home/b1/root/lib/libCint.so(G__exec_statement+0xa8d9)[0x7ff1c6c01049] /home/b1/root/lib/libCint.so(G__interpret_func+0x2602)[0x7ff1c6bb03f2] /home/b1/root/lib/libCint.so(G__getfunction+0x19e9)[0x7ff1c6b9f279] /home/b1/root/lib/libCint.so(G__getitem+0x7ea)[0x7ff1c6b7e77a] /home/b1/root/lib/libCint.so(G__getexpr+0x556a)[0x7ff1c6b84e0a] /home/b1/root/lib/libCint.so(G__exec_statement+0xa8d9)[0x7ff1c6c01049] /home/b1/root/lib/libCint.so(G__interpret_func+0x2602)[0x7ff1c6bb03f2] /home/b1/root/lib/libCint.so(G__getfunction+0x19e9)[0x7ff1c6b9f279] /home/b1/root/lib/libCint.so(G__getitem+0x7ea)[0x7ff1c6b7e77a] /home/b1/root/lib/libCint.so(G__getexpr+0x556a)[0x7ff1c6b84e0a] /home/b1/root/lib/libCint.so(G__calc_internal+0x298)[0x7ff1c6b8cb68] /home/b1/root/lib/libCint.so(G__process_cmd+0x2f16)[0x7ff1c6c08626] /home/b1/root/lib/libCore.so(_ZN5TCint11ProcessLineEPKcPN12TInterpreter10EErrorCodeE+0x406)[0x7ff1c79507c6] /home/b1/root/lib/libCore.so(_ZN5TCint16ProcessLineSynchEPKcPN12TInterpreter10EErrorCodeE+0xf3)[0x7ff1c794cab3] /home/b1/root/lib/libCore.so(_ZN12TApplication11ExecuteFileEPKcPib+0x6e8)[0x7ff1c7888e18] /home/b1/root/lib/libCore.so(_ZN12TApplication11ProcessLineEPKcbPi+0x6e8)[0x7ff1c78876a8] /home/b1/root/lib/libRint.so(_ZN5TRint3RunEb+0x42b)[0x7ff1c64e023b] /home/b1/root/bin/root.exe(main+0x4c)[0x40106c] /lib/libc.so.6(__libc_start_main+0xfe)[0x7ff1c55a9d8e] /home/b1/root/bin/root.exe[0x400f39] ======= Memory map: ======== 00400000-00402000 r-xp 00000000 08:01 1580340 /home/b1/root/bin/root.exe 00601000-00602000 r--p 00001000 08:01 1580340 /home/b1/root/bin/root.exe 00602000-00603000 rw-p 00002000 08:01 1580340 /home/b1/root/bin/root.exe 01def000-02d12000 rw-p 00000000 00:00 0 [heap] 7ff1a8000000-7ff1a8021000 rw-p 00000000 00:00 0 7ff1a8021000-7ff1ac000000 ---p 00000000 00:00 0 7ff1adceb000-7ff1b980f000 rw-p 00000000 00:00 0 7ff1bf5a1000-7ff1bf5b1000 r-xp 00000000 08:01 2101766 /home/b1/root/cint/cint/include/stdfunc.dll 7ff1bf5b1000-7ff1bf7b1000 ---p 00010000 08:01 2101766 /home/b1/root/cint/cint/include/stdfunc.dll 7ff1bf7b1000-7ff1bf7b2000 r--p 00010000 08:01 2101766 /home/b1/root/cint/cint/include/stdfunc.dll 7ff1bf7b2000-7ff1bf7b3000 rw-p 00011000 08:01 2101766 /home/b1/root/cint/cint/include/stdfunc.dll 7ff1bf7b3000-7ff1bf7c1000 r-xp 00000000 08:01 2101750 /home/b1/root/cint/cint/stl/vectorbool.dll 7ff1bf7c1000-7ff1bf9c1000 ---p 0000e000 08:01 2101750 /home/b1/root/cint/cint/stl/vectorbool.dll 7ff1bf9c1000-7ff1bf9c2000 r--p 0000e000 08:01 2101750 /home/b1/root/cint/cint/stl/vectorbool.dll 7ff1bf9c2000-7ff1bf9c3000 rw-p 0000f000 08:01 2101750 /home/b1/root/cint/cint/stl/vectorbool.dll 7ff1bf9c3000-7ff1bf9e6000 r-xp 00000000 08:01 1580260 /home/b1/root/lib/libvectorDict.so 7ff1bf9e6000-7ff1bfbe6000 ---p 00023000 08:01 1580260 /home/b1/root/lib/libvectorDict.so 7ff1bfbe6000-7ff1bfbe8000 r--p 00023000 08:01 1580260 /home/b1/root/lib/libvectorDict.so 7ff1bfbe8000-7ff1bfbe9000 rw-p 00025000 08:01 1580260 /home/b1/root/lib/libvectorDict.so 7ff1bfbe9000-7ff1bfbea000 rw-p 00000000 00:00 0 7ff1bfbea000-7ff1bfda1000 r-xp 00000000 08:01 2101749 /home/b1/root/cint/cint/stl/vector.dll 7ff1bfda1000-7ff1bffa0000 ---p 001b7000 08:01 2101749 /home/b1/root/cint/cint/stl/vector.dll 7ff1bffa0000-7ff1bffa1000 r--p 001b6000 08:01 2101749 /home/b1/root/cint/cint/stl/vector.dll 7ff1bffa1000-7ff1bffa8000 rw-p 001b7000 08:01 2101749 /home/b1/root/cint/cint/stl/vector.dll 7ff1bffa8000-7ff1bffad000 r-xp 00000000 08:01 800281 /usr/lib/libXfixes.so.3.1.0 7ff1bffad000-7ff1c01ac000 ---p 00005000 08:01 800281 /usr/lib/libXfixes.so.3.1.0 7ff1c01ac000-7ff1c01ad000 r--p 00004000 08:01 800281 /usr/lib/libXfixes.so.3.1.0 7ff1c01ad000-7ff1c01ae000 rw-p 00005000 08:01 800281 /usr/lib/libXfixes.so.3.1.0 7ff1c01ae000-7ff1c01b7000 r-xp 00000000 08:01 794282 /usr/lib/libXcursor.so.1.0.2 7ff1c01b7000-7ff1c03b6000 ---p 00009000 08:01 794282 /usr/lib/libXcursor.so.1.0.2 7ff1c03b6000-7ff1c03b7000 r--p 00008000 08:01 794282 /usr/lib/libXcursor.so.1.0.2 7ff1c03b7000-7ff1c03b8000 rw-p 00009000 08:01 794282 /usr/lib/libXcursor.so.1.0.2 7ff1c03b8000-7ff1c03dc000 r-xp 00000000 08:01 1580316 /home/b1/root/lib/libGX11TTF.so 7ff1c03dc000-7ff1c05db000 ---p 00024000 08:01 1580316 /home/b1/root/lib/libGX11TTF.so 7ff1c05db000-7ff1c05dd000 r--p 00023000 08:01 1580316 /home/b1/root/lib/libGX11TTF.so 7ff1c05dd000-7ff1c05de000 rw-p 00025000 08:01 1580316 /home/b1/root/lib/libGX11TTF.so 7ff1c05de000-7ff1c0d02000 r-xp 00000000 08:01 1580280 /home/b1/root/lib/libGui.so 7ff1c0d02000-7ff1c0f02000 ---p 00724000 08:01 1580280 /home/b1/root/lib/libGui.so 7ff1c0f02000-7ff1c0f51000 r--p 00724000 08:01 1580280 /home/b1/root/lib/libGui.so

Unfortunatelly, this stack-trace isn’t helping me, I cant figure out what I am doing wrong. I would appreciate any hints.

Thanks in advance

ftiaronsem

Root-Version: v5-24-00b

[quote]I want to delete a number of histogramms in a root file:[/quote]Where do you want to delete them from?delete histo[i];will ‘only’ remove them from memory. To remove them from the file you would need to call TDirectory::Delete(const char *cyclename). (note that ‘deleting’ from a file, usually does not reduce the size of the file).

Cheers,
Philippe.

Thanks Philippe, TDirectory::Delete(const char *cyclename) was the right way to go and it works smoothly now.

Why does deleting from a file usually not reduce file size? Luckily filesize isn’t something I have to care about in this case, but it would be very interesting to know why.

Thanks again

ftiaronsem

Search for “delete” in http://root.cern.ch/download/doc/11InputOutput.pdf for details.
In short - if you delete an object in a file, the records that it physically used are not removed from the file (there is no “cleanup” in ROOT), they are just marked as “deleted records”, and the space is made free for recycling in the next writing (so a ROOT file will not shrink, it can only become bigger; well, at least until any ROOT expert disagrees, of course).