Home | News | Documentation | Download

Root file doesn't exist- break segmentation violation

Hi,
I just started using root and I some problems.
I generated a class with MakeClass (.h and .C files) from a root file with a tree not made by me. Than I wrote a small application .cc in order to call the Loop() function of the .C file, but when I execute it I get this error:

Error in TFile::TFile: file ntuple_tetraquarks_pseudoscalar_14Gev.root does not exist

*** Break *** segmentation violation

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

gdb.printing.register_pretty_printer(gdb.current_objfile(),
gdb.printing.register_pretty_printer(gdb.current_objfile(),

#0 0x00007ff42b23e46c in waitpid () from /lib64/libc.so.6
#1 0x00007ff42b1bbf62 in do_system () from /lib64/libc.so.6
#2 0x00007ff42fbcd5dc in TUnixSystem::StackTrace() () from /usr/lib64/root/libCore.so.6.22
#3 0x00007ff42fbd006a in TUnixSystem::DispatchSignals(ESignals) () from /usr/lib64/root/libCore.so.6.22
#4
#5 0x0000000000401ef8 in main ()

The lines below might hint at the cause of the crash.
You may get help by asking at the ROOT forum http://root.cern.ch/forum
Only if you are really convinced it is a bug in ROOT then please submit a
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 0x0000000000401ef8 in main ()

Can someone help me? Thanks

The error explicitly says that the ROOT file with your tree is missing. You need something like this:

TFile *f = TFile::Open("/path/to/your/file.root");
TTree *t; f->GetObject("YourTTree", t);
YourClass *c = new YourClass(t); // "YourClass" generated by "MakeClass"
c->Loop();
delete c; // automatically deletes "f" and "t", too

Of course, you can also modify “YourClass” constructor (in the “.h” file) so that it automatically opens the correct ROOT file.

I wrote this:

#include “TTree.h”
#include “TreeAnalysis.h”
#include
using namespace std;

int main(){

//open root file

TString fname("/home/student/Tesi/ntuple_tetraquarks_pseudoscalar_14Gev.root");
TFile* rootfile = TFile::Open(fname);
if(!rootfile->IsOpen()) {
cout <<“Problems reading root file”<< endl;
exit(-1);
};
cout <<“Reading data from” <<fname<< endl;

//pointer to TTree
TDirectoryFile* dir = (TDirectoryFile*)rootfile->Get(“GenAnalysis”);
TTree* tree = (TTree*) dir->Get(“tree”);
if(!tree) {
cout <<“Null pointer to tree”<< endl;
exit(-1);
}

//costructor
TreeAnalysis treeanalysis(tree);
// loop
treeanalysis.Loop();

delete tree;

return 0;
}
(inside the root file the tree is in the GenAnalysis directory)
Am I still missing something? Cause it doesn’t work

“doesn’t work” means what?

Try:

#include "TreeAnalysis.C"

#include "TString.h"
#include "TFile.h"
#include "TTree.h"

#include <iostream>

int main(int /*argc*/, char ** /*argv*/) {
  TString fname("/home/student/Tesi/ntuple_tetraquarks_pseudoscalar_14Gev.root");
  TFile *rootfile = TFile::Open(fname);
  if ((!rootfile) || rootfile->IsZombie()) {
    std::cout << "Problems reading file " << fname << std::endl;
    exit(1);
  };
  std::cout << "Reading data from " << fname << std::endl;
  
  // pointer to TTree
  TTree *tree;
  rootfile->GetObject("GenAnalysis/tree", tree);
  if (!tree) {
    std::cout << "Null pointer to tree" << std::endl;
    exit(1);
  }
  
  // constructor
  TreeAnalysis treeanalysis(tree);
  // loop
  treeanalysis.Loop();
  
  return 0;
}
1 Like

It means that it gives the same error as before, not existing file.
With your code I have a compilation error:

AppTreeAnalysis1.cc: In function ‘int main(int, char**)’:
AppTreeAnalysis1.cc:20:41: error: no matching function for call to ‘TFile::GetObject(const char [17])’
rootfile->GetObject(“GenAnalysis/tree”);
^
AppTreeAnalysis1.cc:20:41: note: candidate is:
In file included from /usr/include/root/TROOT.h:28:0,
from TreeAnalysis.h:11,
from TreeAnalysis.C:2,
from AppTreeAnalysis1.cc:1:
/usr/include/root/TDirectory.h:155:35: note: template void TDirectory::GetObject(const char*, T*&)
template inline void GetObject(const char* namecycle, T*& ptr) // See TDirectory::Get for information
^
/usr/include/root/TDirectory.h:155:35: note: template argument deduction/substitution failed:
AppTreeAnalysis1.cc:20:41: note: candidate expects 2 arguments, 1 provided
rootfile->GetObject(“GenAnalysis/tree”);

Sorry, there was a bug in that line, now corrected.

Now I get this in the compilation:

/tmp/ccjv37uK.o: In function TreeAnalysis::TreeAnalysis(TTree*)': TreeAnalysis.C:(.text+0x0): multiple definition of TreeAnalysis::TreeAnalysis(TTree*)’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x0): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::Init(TTree*)': TreeAnalysis.C:(.text+0x2e0): multiple definition of TreeAnalysis::Init(TTree*)’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x2e0): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::TreeAnalysis(TTree*)': TreeAnalysis.C:(.text+0x0): multiple definition of TreeAnalysis::TreeAnalysis(TTree*)’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x0): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::~TreeAnalysis()': TreeAnalysis.C:(.text+0x130): multiple definition of TreeAnalysis::~TreeAnalysis()’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x130): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::~TreeAnalysis()': TreeAnalysis.C:(.text+0x130): multiple definition of TreeAnalysis::~TreeAnalysis()’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x130): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::~TreeAnalysis()': TreeAnalysis.C:(.text+0x192): multiple definition of TreeAnalysis::~TreeAnalysis()’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x192): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::GetEntry(long long)': TreeAnalysis.C:(.text+0x1b8): multiple definition of TreeAnalysis::GetEntry(long long)’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x1b8): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::LoadTree(long long)': TreeAnalysis.C:(.text+0x208): multiple definition of TreeAnalysis::LoadTree(long long)’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x208): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::Notify()': TreeAnalysis.C:(.text+0x117e): multiple definition of TreeAnalysis::Notify()’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x117e): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::Show(long long)': TreeAnalysis.C:(.text+0x118e): multiple definition of TreeAnalysis::Show(long long)’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x118e): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::Cut(long long)': TreeAnalysis.C:(.text+0x11da): multiple definition of TreeAnalysis::Cut(long long)’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x11da): first defined here
/tmp/ccjv37uK.o: In function TreeAnalysis::Loop()': TreeAnalysis.C:(.text+0x11ee): multiple definition of TreeAnalysis::Loop()’
/tmp/ccNIHKZe.o:AppTreeAnalysis1.cc:(.text+0x11ee): first defined here
collect2: error: ld returned 1 exit status

Note that, I #include "TreeAnalysis.C" so you should not compile this source code file separately.

I compile the two files together with:
g++ -o AppTreeAnalysis1 root-config --libs --cflags AppTreeAnalysis1.cc TreeAnalysis.C

`root-config --cxx --cflags` -o AppTreeAnalysis1 AppTreeAnalysis1.cc `root-config --libs`

Thank you, now it compiles without errors. In the execution there is still an error with the file, but now is:

Error in TFile::TFile: file /home/student/Tesi/ntuple_tetraquarks_pseudoscalar_14Gev.root does not exist
Problems reading file /home/student/Tesi/ntuple_tetraquarks_pseudoscalar_14Gev.root

without the segmentation violation

Well …

ls -al /home/student/Tesi/ntuple_tetraquarks_pseudoscalar_14Gev.root
ls -al /home/student/Tesi/

Now it works, this last error was because of a my typing error, sorry. Thank you very much!!