Using Global Variables while reading a tree

Hi everyone,

I’m trying to write a macro using global variables, and i have some trouble.

I’m trying to read a tree from a file, and store what i’ve read in some global variables. I’m using this part of code:

Int_t           npv;
Float_t         met;
Float_t         evtWeight;

int main () {

TTree *tree(nullptr); 

tree->SetBranchAddress("npv", &npv);
tree->SetBranchAddress("met", &met);
tree->SetBranchAddress("evtWeight", &evtWeight);

TFile *f = TFile::Open("mc.root");	//mc.root contains the tree that I want to read

f->GetObject("H4l",tree);			//H4l is the name of the tree that i wnat to read
Long64_t nentries = tree->GetEntries();

for (Long64_t jentry=0; jentry<nentries; jentry++) {        
        [...]  */

… And the code goes on in function main. If I declare “npv, met, evtWeight” in function main, it works. If i declare this variables (like in my example) like global variables (and that’s my goal), when i compile and execute this macro, root 6 crashes.

Someone can help me? Thank you

This is the report of the crash, if you want to read it:

"#5 0x00007fec3e516445 in int TTree::SetBranchAddress<int>(char const*, int*, TBranch**) () #6 0x00007fec3e510478 in Risoluzione_pt_funzioni() () #7 0x00007fec3e51602b in __cling_Un1Qu30(void*) () #8 0x00007fec39f9ee84 in cling::Interpreter::RunFunction(clang::FunctionDecl const*, cling::Value*) () from /home/lorenzo//lib/root/ #9 0x00007fec39fa40fe in cling::Interpreter::EvaluateInternal(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::CompilationOptions, cling::Value*, cling::Transaction**) () from /home/lorenzo//lib/root/ #10 0x00007fec39fa42af in cling::Interpreter::echo(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cling::Value*) () from /home/lorenzo//lib/root/ #11 0x00007fec3a009cc2 in cling::MetaSema::actOnxCommand(llvm::StringRef, llvm::StringRef, cling::Value*) () from /home/lorenzo//lib/root/ #12 0x00007fec3a0184d6 in cling::MetaParser::isXCommand(cling::MetaSema::ActionResult&, cling::Value*) () from /home/lorenzo//lib/root/ #13 0x00007fec3a0185de in cling::MetaParser::isCommand(cling::MetaSema::ActionResult&, cling::Value*) () from /home/lorenzo//lib/root/ #14 0x00007fec3a019199 in cling::MetaProcessor::process(char const*, cling::Interpreter::CompilationResult&, cling::Value*) () from /home/lorenzo//lib/root/ "

thank you

TFile *f = TFile::Open("mc.root"); if ((!f) || (f->IsZombie())) { delete f; return -1; } // file access problems TTree *tree; f->GetObject("H4l", tree); if (!tree) { delete f; return -2; } // "H4l" not found // tree->SetBranchAddress ...

I already use two checks, but they are probably different from yours. Am I right? I write them here:

if( not f->IsOpen()){                     
        cout<<"file not found"<<endl;
        return -1;                     
    if(tree==nullptr) {                 
        cout << "tree not found" <<endl;
        return -1;

None of this check works. So i imagine that the problem isn’t a file access or the tree not found. Indeed, if I write my variables in the main function the code works… If i write them like global variables the macro crashes…


What code are you actually running. The snippet above contains:[code]TTree *tree(nullptr);

tree->SetBranchAddress(“npv”, &npv);
tree->SetBranchAddress(“met”, &met);
tree->SetBranchAddress(“evtWeight”, &evtWeight);[/code]which clearly lead to a null pointer dereference (and a crash) since SetBranchAddress is called before tree is setting to point to an actual object …


Thank you, Philippe, you’re right.

Obviusly in the code that works GetObject command and SetBranchAddress are inverted.
Now it works.