Trouble getting a histogram from a root file

I am facing a very puzzling issue…
I open a root file, that contains a few histograms.
The histograms in the initial file are ok, in the sence that for all bins isnan() and isinf() return false.
Off course I can print them, without any problem.

However when I try to print it after getting from the initial root file I get a segmentation error…

My code is

[code]void WAVG(char* filename, int cycle){

TFile *file1 = new TFile(TString::Format("%s_%i.root", filename, cycle),“READ”);
if (file1->IsOpen()) cout << “File " << file1->GetName() << " opened successfully” << endl;

TH1F *havgflux_234U_front  = (TH1F*)file1->Get("hflux_234U_front");  for (int i=0; i<havgflux_234U_front->GetNbinsX(); i++) {if (isinf(havgflux_234U_front->GetBinContent(i+1))) cout << i+1 << endl;}

havgflux_234U_front->Draw("histo");

}[/code]

The initial file is attached.

To execute while in a root session I do the following

.L WAVG.C++ WAVG("MGAS_En_650keV", 10)

Any idea on why this is happening?

Thanks a lot in advance!

The error output is

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

Thread 9 (Thread 0x7f4ba164f700 (LWP 29070)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 8 (Thread 0x7f4ba0c4e700 (LWP 29071)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 7 (Thread 0x7f4ba024d700 (LWP 29072)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 6 (Thread 0x7f4b9f84c700 (LWP 29073)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 5 (Thread 0x7f4b9ee4b700 (LWP 29074)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 4 (Thread 0x7f4b9e44a700 (LWP 29075)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 3 (Thread 0x7f4b9da49700 (LWP 29076)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 2 (Thread 0x7f4b9d048700 (LWP 29077)):
#0 0x00007f4bae24968c in pthread_cond_wait

GLIBC_2.3.2 () from /lib64/libpthread.so.0
#1 0x00007f4ba38a09c3 in ?? () from /usr/lib64/dri/swrast_dri.so
#2 0x00007f4ba38a0537 in ?? () from /usr/lib64/dri/swrast_dri.so
#3 0x00007f4bae245aa1 in start_thread () from /lib64/libpthread.so.0
#4 0x00007f4baf9afaad in clone () from /lib64/libc.so.6

Thread 1 (Thread 0x7f4bb0f280e0 (LWP 22706)):
#0 0x00007f4baf9737fd in waitpid () from /lib64/libc.so.6
#1 0x00007f4baf9055c9 in do_system () from /lib64/libc.so.6
#2 0x00007f4baf905900 in system () from /lib64/libc.so.6
#3 0x00007f4bb0616b04 in TUnixSystem::StackTrace() () from /usr/lib64/root/libCore.so.5.34
#4 0x00007f4bb0615f23 in TUnixSystem::DispatchSignals(ESignals) () from /usr/lib64/root/libCore.so.5.34
#5
#6 0x00007f4baadc4540 in FT_Done_Glyph () from /usr/lib64/libfreetype.so.6
#7 0x00007f4bab113c65 in TTF::LayoutGlyphs() () from /usr/lib64/root/libGraf.so
#8 0x00007f4bab1151dd in TTF::GetTextExtent(unsigned int&, unsigned int&, char*) () from /usr/lib64/root/libGraf.so
#9 0x00007f4bab0ebb73 in TLatex::Analyse(double, double, TextSpec_t, char const*, int) () from /usr/lib64/root/libGraf.so
#10 0x00007f4bab0ebc32 in TLatex::Anal1(TextSpec_t, char const*, int) () from /usr/lib64/root/libGraf.so
#11 0x00007f4bab0ebd8b in TLatex::FirstParse(double, double, char const*) () from /usr/lib64/root/libGraf.so
#12 0x00007f4bab0edafa in TLatex::PaintLatex1(double, double, double, double, char const*) () from /usr/lib64/root/libGraf.so
#13 0x00007f4bab0eecb2 in TLatex::PaintLatex(double, double, double, double, char const*) () from /usr/lib64/root/libGraf.so
#14 0x00007f4bab0d6067 in TGaxis::PaintAxis(double, double, double, double, double&, double&, int&, char const*, double, bool) () from /usr/lib64/root/libGraf.so
#15 0x00007f4b9c346f93 in THistPainter::PaintAxis(bool) () from /usr/lib64/root/libHistPainter.so
#16 0x00007f4b9c335f71 in THistPainter::Paint(char const*) () from /usr/lib64/root/libHistPainter.so
#17 0x00007f4baa6afe32 in TPad::PaintModified() () from /usr/lib64/root/libGpad.so
#18 0x00007f4baa697c09 in TCanvas::Update() () from /usr/lib64/root/libGpad.so
#19 0x00007f4bb05d3a17 in TCint::UpdateAllCanvases() () from /usr/lib64/root/libCore.so.5.34
#20 0x00007f4bb05d680a in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) () from /usr/lib64/root/libCore.so.5.34
#21 0x00007f4bb05d43b3 in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) () from /usr/lib64/root/libCore.so.5.34
#22 0x00007f4bb01890e0 in TRint::HandleTermInput() () from /usr/lib64/root/libRint.so.5.34
#23 0x00007f4bb0613f8e in TUnixSystem::CheckDescriptors() () from /usr/lib64/root/libCore.so.5.34
#24 0x00007f4bb0614273 in TUnixSystem::DispatchOneEvent(bool) () from /usr/lib64/root/libCore.so.5.34
#25 0x00007f4bb0594bd6 in TSystem::InnerLoop() () from /usr/lib64/root/libCore.so.5.34
#26 0x00007f4bb059670b in TSystem::Run() () from /usr/lib64/root/libCore.so.5.34
#27 0x00007f4bb05372df in TApplication::Run(bool) () from /usr/lib64/root/libCore.so.5.34
#28 0x00007f4bb018a5b4 in TRint::Run(bool) () from /usr/lib64/root/libRint.so.5.34
#29 0x000000000040103c in main ()

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 0x00007f4baadc4540 in FT_Done_Glyph () from /usr/lib64/libfreetype.so.6
#7 0x00007f4bab113c65 in TTF::LayoutGlyphs() () from /usr/lib64/root/libGraf.so
#8 0x00007f4bab1151dd in TTF::GetTextExtent(unsigned int&, unsigned int&, char*) () from /usr/lib64/root/libGraf.so
#9 0x00007f4bab0ebb73 in TLatex::Analyse(double, double, TextSpec_t, char const*, int) () from /usr/lib64/root/libGraf.so
#10 0x00007f4bab0ebc32 in TLatex::Anal1(TextSpec_t, char const*, int) () from /usr/lib64/root/libGraf.so
#11 0x00007f4bab0ebd8b in TLatex::FirstParse(double, double, char const*) () from /usr/lib64/root/libGraf.so
#12 0x00007f4bab0edafa in TLatex::PaintLatex1(double, double, double, double, char const*) () from /usr/lib64/root/libGraf.so
#13 0x00007f4bab0eecb2 in TLatex::PaintLatex(double, double, double, double, char const*) () from /usr/lib64/root/libGraf.so
#14 0x00007f4bab0d6067 in TGaxis::PaintAxis(double, double, double, double, double&, double&, int&, char const*, double, bool) () from /usr/lib64/root/libGraf.so
#15 0x00007f4b9c346f93 in THistPainter::PaintAxis(bool) () from /usr/lib64/root/libHistPainter.so
#16 0x00007f4b9c335f71 in THistPainter::Paint(char const*) () from /usr/lib64/root/libHistPainter.so
#17 0x00007f4baa6afe32 in TPad::PaintModified() () from /usr/lib64/root/libGpad.so
#18 0x00007f4baa697c09 in TCanvas::Update() () from /usr/lib64/root/libGpad.so
#19 0x00007f4bb05d3a17 in TCint::UpdateAllCanvases() () from /usr/lib64/root/libCore.so.5.34

[/code][/quote]
MGAS_En_650keV_10.root (522 KB)

Hi,

Is this an ownership issue? Is the file closed before using the histograms? In this case, have you tried to call the SetDirectory(0) on them in order to “detach” them from the file?
See root.cern.ch/root/htmldoc/guide … -ownership

Cheers,
Danilo

[quote=“dpiparo”]Hi,

Is this an ownership issue? Is the file closed before using the histograms? In this case, have you tried to call the SetDirectory(0) on them in order to “detach” them from the file?
See root.cern.ch/root/htmldoc/guide … -ownership

Cheers,
Danilo[/quote]

Thank you so much for your post!!!

How can I tell whether there is an ownership issue or not?

Try the attached macro.
WAVG.C (1.06 KB)

Now it seems to work!!!

Thanks a lot!!!

One question though…

What does this part do ? I am not familiar with the syntax…

#if 1 /* 0 or 1 */ havgflux_234U_front->SetDirectory(gROOT); // (gROOT) or (0) delete file1; #endif /* 0 or 1 */

cplusplus.com -> Preprocessor directives

Now there is another problem.
I want to set all bins to 0. The initial histogram has 9000 entries. When i set all bin contents to 0, the entries become 18000!!! And I can’t understand why!

The code is

[code]void test(const char* filename, int cycle) {
TFile *file1 = new TFile(TString::Format("%s_%i.root", filename, cycle),“READ”);
if ((!file1) || file1->IsZombie()) { delete file1; return; } // just a precaution
std::cout << “File " << file1->GetName() << " opened successfully.” << std::endl;

TH1F *havgflux_234U_front;
file1->GetObject(“hflux_234U_front”, havgflux_234U_front);
if (!havgflux_234U_front) { delete file1; return; } // just a precaution
std::cout << “Histogram " << havgflux_234U_front->GetName() << " found.” << std::endl;

for (int i=0; i<havgflux_234U_front->GetNbinsX(); i++) {
havgflux_234U_front->SetBinContent(i+1, 0.);
}

havgflux_234U_front->Draw(“histo”);

#if 1 /* 0 or 1 /
havgflux_234U_front->SetDirectory(gROOT); // (gROOT) or (0)
delete file1;
#endif /
0 or 1 */
}[/code]

And here are the two plots

Before

after setting al contents to 0

havgflux_234U_front->Reset(“M”); // root.cern.ch/root/html534/TH1.html#TH1:Reset