Is there a way to check for existing branches in TChain?

Dear all,

I am currently writing some learning unit-tests for the ROOT classes I use for analysis.

Since I want my variable-input class to be segmentation-violation safe I have to test what happens if I
try to access a variable which is not contained in the TChain:

However, this causes a segmentation violation.

I had a look at the available functions in TChain, but I could find one which allows me to check
if the variable exists (something like bool containsBranch(const char* name)).

I also tried to use GetBranch and check for null-pointer, but I also get a segmentation fault.

Any suggestion is appreciated.

Thank you in advance.

I managed to find a workaround using an own handler for the SegFault signal.
The example is from:
stackoverflow.com/questions/7700 … pp-crashes

For me it is only a poor man’s solution since I would like to prevent segfaults.

Hi,

yikes. An alternative is

static TString invalidBranch("this is not in the chain");
TBranch* br = input->GetListOfBranches()->FindObject(invalidBranch);
if (br) input->SetBranchStatus(invalidBranch, 1);

Axel.

Hi Axel,

thanks for the reply.

Unfortunately your suggestion produces a segmentation fault as well.

I had to add the cast, since it didn’t compile otherwise.

Just some details:
Using ROOT 5.26.00b (debug) on Snow Leopard

1 Like

I just tried the code on my destop (Ubuntu 10.04, 64bit) with the same root version (before on Mac OS X 10.6.3).

I don’t get a segmentation fault any more. Instead the program simply ends.
I guess the initial problem is still the same, but no signal (SIGSEGV) is passed on the Linux system.

If I try the code in CINT, it works. The old code

returns a meaningful error in CINT:

So I guess I have to explain what I do:
I am using CUTE (Eclipse plugin for C++ unit testing) and compiling (g++) the code into a executable.

What does CINT do what is missed in the compilation? Is ROOT limited to be used with CINT only?

Hi,

if this

static TString invalidBranch("this is not in the chain");
TBranch* br = input->GetListOfBranches()->FindObject(invalidBranch);
if (br) input->SetBranchStatus(invalidBranch, 1);

crashes then you have a completely different problem. This has nothing to do with CINT. (And yes, of course ROOT works also when not accessed through CINT!) I assume that you simply have either input being NULL or a library version mismatch or you didn’t build your binary right (i.e. something wrong with linking).

I need at least the backtrace of the crash; ideally even the whole thing (code, Makefile, ROOT file) so I can reproduce it.

Cheers, Axel.

Hi Alex,

I guess I have to apologize.

When I was rewriting the test in order to provide you with a make file for the ROOT test only, I realized one thing:
At the stage I check for the TBranch the TChain was not initialised!

That is what lead to the unexpected behaviour (freeze on Linux, segfault on OS X).

I now even get the same error message as when I used CINT.

Thanks for the help!

Cheers,
Luke