SegFault calling GetN() method of a TGraph object

Hello,

I’ve been having trouble running the following script successfully.
SignalToNoiseRatio.cpp (4.1 KB)

What occurs is that there is a segfault on line 94 where the class method GetN() for a TGraph object is called. I don’t quite get how a segfault is occurring in this step of the code.

To compile this code just run: g++ `root-config --cflags --glibs` -g -Wall SignalToNoiseRatio.cpp -o SignalToNoiseRatio.exe

Also, the script uses data from the following root files:
test1.root (25.4 KB)
test2.root (122.5 KB)

You’ll need to change the directory file path in lines 53 and 54 to wherever you have test1.root and test2.root stored. In addition, you may want to change the path in line 116 so that it outputs a plot to your desired location.

Thank you for any help you can give.


Please read tips for efficient and successful posting and posting code

ROOT Version: 5.34
Platform: Ubuntu 18.04 LTS
Compiler: g++


I do not see anything obviously wrong in your code.
GetN() is called on line 87 not 94.
Are you sure GetN() is the culprit ?
Do you get some warnings when you compile your code ?
Also the ROOT version you are using is quite old.

You improperly define / use your tree branches / leaves.

You are correct its line 87 what I meant to say. I am getting segfault in line 87 and when I view the stack frame it indicates to me that the problem is in GetN(). The script executes as follows:

[jusa@ccw07 signal-to-noise-ratio]$ ./SignalToNoiseRatio.exe 
Reading KLpipipi0 chain
Reading KLpi0nunu chain

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f3e51fe941c in waitpid () from /usr/lib64/libc.so.6
#1  0x00007f3e51f66f12 in do_system () from /usr/lib64/libc.so.6
#2  0x00007f3e56c5e609 in TUnixSystem::StackTrace() () from /sw/packages/root/5.34.38/lib/root/libCore.so.5.34
#3  0x00007f3e56c6022c in TUnixSystem::DispatchSignals(ESignals) () from /sw/packages/root/5.34.38/lib/root/libCore.so.5.34
#4  <signal handler called>
#5  0x000000000040257c in TGraph::GetN (this=0x4068f641) at /sw/packages/root/5.34.38/include/root/TGraph.h:132
#6  0x0000000000402062 in SignalToNoiseRatio () at SignalToNoiseRatio.cpp:87
#7  0x00000000004019f6 in main () at SignalToNoiseRatio.cpp:20
===========================================================


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.
===========================================================
#5  0x000000000040257c in TGraph::GetN (this=0x4068f641) at /sw/packages/root/5.34.38/include/root/TGraph.h:132
#6  0x0000000000402062 in SignalToNoiseRatio () at SignalToNoiseRatio.cpp:87
#7  0x00000000004019f6 in main () at SignalToNoiseRatio.cpp:20
===========================================================

If GetN() is not the culprit then my guess would be that I initialized the TGraph object wrong on line 51, but I don’t think that is the case.

There are some warnings of unused variables at compile time but I believe they don’t have to do with the segfault.

I agree its an old version but its the only version that I am able to use at the moment.

I believe you are correct I should use TGraph.h instead of TGraphError.h. But I am still getting the same segfault error from above that is spawning from line 87

Reading KLpipipi0 chain
Reading KLpi0nunu chain

 *** Break *** segmentation violation



===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
#0  0x00007f56ce91041c in waitpid () from /usr/lib64/libc.so.6
#1  0x00007f56ce88df12 in do_system () from /usr/lib64/libc.so.6
#2  0x00007f56d3585609 in TUnixSystem::StackTrace() () from /sw/packages/root/5.34.38/lib/root/libCore.so.5.34
#3  0x00007f56d358722c in TUnixSystem::DispatchSignals(ESignals) () from /sw/packages/root/5.34.38/lib/root/libCore.so.5.34
#4  <signal handler called>
#5  0x000000000040257c in TGraph::GetN (this=0x4068f641) at /sw/packages/root/5.34.38/include/root/TGraph.h:132
#6  0x0000000000402062 in SignalToNoiseRatio () at SignalToNoiseRatio.cpp:87
#7  0x00000000004019f6 in main () at SignalToNoiseRatio.cpp:20
===========================================================


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.
===========================================================
#5  0x000000000040257c in TGraph::GetN (this=0x4068f641) at /sw/packages/root/5.34.38/include/root/TGraph.h:132
#6  0x0000000000402062 in SignalToNoiseRatio () at SignalToNoiseRatio.cpp:87
#7  0x00000000004019f6 in main () at SignalToNoiseRatio.cpp:20
===========================================================

Running directly on the ROOT prompt (not compiling), on root 6.22/02 your code runs without a segfault; check the resulting PDF to see if that’s what you expect.
ROOT does complain though that Pi0Pt and Pi0RecZ should be of type double in your code (as they are in the trees), not float.
aa.pdf (13.8 KB)

Thank you it works now. I just fixed the warnings you mentioned of the variables to Double_t and they work now.