Accessing variables via SetBranchAddress

Hi everyone,

I’m having problems accessing the variables for a given tree. Usually it worked for me like in the code example, but now it just says “unknown branch”:

    TFile *f = new TFile("selected.root");
    TTree *mytree=(TTree*)f->Get("recData");

    Float_t fEnergy;
    mytree->SetBranchAddress("fEnergy",&fEnergy);

    for(Int_t i = 0; i <mytree->GetEntries(); i++){
    mytree->GetEntry(i);
    cout << fEnergy<< endl;
    }

I think that I set the address incorrectly, although I tried variations like the full pathname (event.fFDEvents.fFdRecShower.fEnergy). Please see the attached screenshot of the tree-structure.
Thanks!


In general, one would need your root file in order to try it, but as a first step you could try to use the “full name” of the branch which is something like “event.fFDEvents.fFdRecShower.fEnergy” (if it doesn’t work, either attach your root file here or put it somewhere in a “public area”, e.g. “afs”).

Ok, here is the link:
dropbox.com/s/xevxtlb1cbzc6 … .root?dl=0

See:




Try:

{
  TFile *f = TFile::Open("selected.root");
  if (!f) return; // just a precaution
  
  TTree *t; f->GetObject("recData", t);
  if (!t) { delete f; return; } // just a precaution
  
  t->SetMakeClass(1);
  t->SetBranchStatus("*", 0);
  
  Int_t fFDEvents_;
  // note: you must know kMaxevent_fFDEvents from somewhere else,
  //       e.g. use TTree::MakeClass to get it
  const Int_t kMaxevent_fFDEvents = 3;
  Double_t fEnergy[kMaxevent_fFDEvents]; //[fFDEvents_]
  
  t->SetBranchStatus("event.fFDEvents", 1);
  t->SetBranchAddress("event.fFDEvents", &fFDEvents_);
  
  t->SetBranchStatus("event.fFDEvents.fFdRecShower.fEnergy", 1);
  t->SetBranchAddress("event.fFDEvents.fFdRecShower.fEnergy", fEnergy);
  
  for(Long64_t i = 0; i < t->GetEntries(); i++) {
    t->GetEntry(i);
    
    if (fFDEvents_ > kMaxevent_fFDEvents) {
      // note: the stack is corrupt, a SIGSEV crash is possible
      std::cout << i << " : " << fFDEvents_ << " :"
                << " FATAL ERROR : fFDEvents_ > kMaxevent_fFDEvents = "
                << kMaxevent_fFDEvents << std::endl;
      delete f; // automatically deletes "t", too
      return; // try to get out of here
    }
    
    std::cout << i << " : " << fFDEvents_ << " :";
    for (Int_t j = 0; j < fFDEvents_; j++) std::cout << " " << fEnergy[j];
    std::cout << std::endl;
  }
  
  // t->ResetBranchAddresses(); // detach "t" from local variables
  delete f; // automatically deletes "t", too
}

Thank you very much! :slight_smile: