Segmentation violation, trying to read TFiles with a script

Hello,

I am trying to iterate over a directory (it only contains .root files), each of them contains only one ntuple.
This is my script:

TSystemDirectory dir(“ntuples”, “ntuples”);
TList *files = dir.GetListOfFiles();
if (files) {
TFile file;
TString fileName;
TIter next(files);
while ((file=(TFile
)next())) {
fileName = file->GetName();
cout << fileName.Data() << endl;
TNtuple *ntp;
if (fileName.Contains(“ee”)){
ntp = file->Get(“ee”); // problem is here
}
if (fileName.Contains(“mumu”)) {
ntp = file->Get(“mumu”); // problem is here
}
[…]

When I try to run this, I get a segmentation violation (detailed below).
The problem definitely occurs when the lines i marked as problematic are reached, I tried all sorts of commenting out. If I create a TFile on a root session and get a TNtuple out of it using Get(“ee”), it works.

What am I doing wrong?

Thank you!

Here is the full output of the error:
*** Break *** segmentation violation

===========================================================
There was a crash (#7 0x0063598d in SigHandler(ESignals) ()).
This is the entire stack trace of all threads:

#0 0x002c4402 in __kernel_vsyscall ()
#1 0x05a1a833 in __waitpid_nocancel () from /lib/libc.so.6
#2 0x059bf19b in do_system () from /lib/libc.so.6
#3 0x0026eead in system () from /lib/libpthread.so.0
#4 0x0063125d in TUnixSystem::Exec(char const*) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#5 0x00638364 in TUnixSystem::StackTrace() ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#6 0x006358be in TUnixSystem::DispatchSignals(ESignals) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#7 0x0063598d in SigHandler(ESignals) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#8 0x0062eb54 in sighandler(int) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#9
#10 0x00000000 in ?? ()
#11 0x00700eac in G__G__Base1_8_0_32(G__value*, char const*, G__param*, int) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#12 0x00bf45ea in Cint::G__ExceptionWrapper(int ()(G__value, char const*, G__param*, int), G__value*, char*, G__param*, int) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#13 0x00cb03d6 in G__execute_call ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#14 0x00cb2af0 in G__call_cppfunc ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#15 0x00c8b30f in G__interpret_func ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#16 0x00c77334 in G__getfunction ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#17 0x00d6e5cc in G__getstructmem ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#18 0x00d6448a in G__getvariable ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#19 0x00c4ddd1 in G__getitem ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#20 0x00c58781 in G__getexpr ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#21 0x00c3e47e in G__define_var ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#22 0x00ce0b5f in G__exec_statement ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#23 0x00ce9740 in G__exec_loop(char const*, char*, std::list<G__FastAllocString, std::allocator<G__FastAllocString> > const&) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#24 0x00cdf0c0 in G__exec_statement ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#25 0x00ce3d63 in G__exec_statement ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#26 0x00c8d411 in G__interpret_func ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#27 0x00c774bb in G__getfunction ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#28 0x00c4dec1 in G__getitem ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#29 0x00c58781 in G__getexpr ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#30 0x00c5f4c0 in G__calc_internal ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#31 0x00cf190f in G__process_cmd ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCint.so
#32 0x0061efc5 in TCint::ProcessLine(char const*, TInterpreter::EErrorCode*) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#33 0x0062427f in TCint::ProcessLineSynch(char const*, TInterpreter::EErrorCode*) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#34 0x00545c86 in TApplication::ExecuteFile(char const*, int*, bool) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#35 0x005462cc in TApplication::ProcessFile(char const*, int*, bool) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#36 0x00543765 in TApplication::ProcessLine(char const*, bool, int*) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libCore.so
#37 0x0011d8f9 in TRint::Run(bool) ()
from /BFROOT/package/root/5.26-00/Linux26SL5_i386_gcc412/lib/libRint.so
#38 0x08048d83 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
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.

#10 0x00000000 in ?? ()

Hi,

Could you provide a minimal running piece of code reproducing the problem?

Cheers, Bertrand.

Hi Bertnard,

I don’t understand what you are asking. This is my code, all I do is run the script:

TSystemDirectory dir(“ntuples”, “ntuples”);
TList *files = dir.GetListOfFiles();
if (files) {
TFile file;
TString fileName;
TIter next(files);
while ((file=(TFile
)next())) {
fileName = file->GetName();
cout << fileName.Data() << endl;
TNtuple *ntp;
if (fileName.Contains(“ee”)){
ntp = file->Get(“ee”); // problem is here
}
if (fileName.Contains(“mumu”)) {
ntp = file->Get(“mumu”); // problem is here
}
}
(more code here, it’s currently commented out but I still get the problem)

[quote=“noafeld”]I don’t understand what you are asking. This is my code, all I do is run the script:[/quote]OK, fine, and how do I test it without the root files (with the ntuples)?

{ TSystemDirectory dir("ntuples", "ntuples"); TList *files = dir.GetListOfFiles(); if (files) { TIter next(files); TObject *o; while (o = next()) { if(o->InheritsFrom("TSystemDirectory")) continue; // skip directories TString fileName(o->GetName()); std::cout << fileName << std::endl; TFile *file = TFile::Open(fileName); if (!file) continue; // file could not be opened TNtuple *ntp = ((TNtuple *)0); if (fileName.Contains("ee")) { file->GetObject("ee", ntp); if (!ntp) { delete file; continue; } // "ee" TNtuple not found // // ... use "ee" ntp here ... // } else if (fileName.Contains("mumu")) { file->GetObject("mumu", ntp); if (!ntp) { delete file; continue; } // "mumu" TNtuple not found // // ... use "mumu" ntp here ... // } if (!ntp) { delete file; continue; } // just a precaution // // ... use "any found above" ntp here ... // delete file; // automatically deletes ntp, too } } delete files; // cleanup }

Oh yes, obviously! Thanks Wile! (I suppose I was too tired yesterday night… :blush: )

Cheers, Bertrand.

Hi Wile,
I’m sorry, I don’t understand. Was your message directed at me or Bertnard? Because if it was for me, then I’m not sure what to do with it… Is that a change I should put in my code? Why?

(If it really was for me, maybe I can write some specific questions instead of just “why?”)

Also, If you want to try it with my files, you can find them at bbrltda.slac.stanford.edu:/awg/tauqed/noafeld/releases/analysis52/workdir/root/ntuples
If you want me to change the permissions of the files, I’d love to.

Thank you!

You can stay with your source code if you like receiving “segmentation violation”. :mrgreen:
[shrugs] Suit yourself. I’m easy. <img src="/uploads/default/original/2X/8/84c2fe9464a4066c00e1bd5978e913e7869cbb07.gif" width=“22” height=“16” alt=":-"" title=“Whistle”/>

OK.
So first of all thank you.
Second:
Why did I get the segmentation violation?
Is it because I used TFile::Get() and not GetObject? and if so, what’s the difference? Get() works outside of the script.
Or is it because I didn’t delete the file? It seems like the problem occurred before the part I was supposed to delete the file, ie it didn’t even get to the part in which I do things with the ntuple and crashed just because of the Get().

Thank you,
Noa

(file=(TFile*)next()) = CCCP