cjt00
March 11, 2019, 4:52pm
1
ROOT Version: 5.34/32
Platform: Not Provided
Compiler: Not Provided
eventsout.root (353.1 KB)
I want to read a the entries from a ttree. I have a very simple macro to open the file and should print the value of one of the variabes:
#include <TROOT.h>
#include <TChain.h>
#include "TObject.h"
#include "TTree.h"
#include "TBranch.h"
void test(){
TFile *f = TFile::Open("eventsout.root");
Double_t x[2];
TTree *t;
TBranch *b;
f->GetObject("treeout",t);
t->SetBranchAddress("in.t",x,&b);
Long64_t n = t->GetEntriesFast();
// t->Show(10);
t->GetEntry(10);
//Long64_t m = t->LoadTree(10);
std::cout << x[0] << std::endl;
}
The show command displays all the data so clearly the tree is being found ok, but the print statement at the bottom just displays an uninitialised value like 1e-315.
I have tried passing a reference to x in SetBranchAddress:
t->SetBranchAddress("in.t",&x,&b);
But this does not work either.
I have no idea why this is happening.
I have attached the file I’m reading in.
If you don’t know how to deal with your tree, see how various flavours of automatically generated “analysis skeletons” deal with it.
cjt00
March 11, 2019, 5:05pm
3
The code above is based on how a skeleton is already doing it which is why I don’t understand why it’s not working.
Here is the treeout class for you.treeout.C (1.4 KB)
treeout.h (45.7 KB)
I need to iterate over a lot of these files to create an array of histograms.
Well, you did your homework so, here’s a carrot for you:
{
TFile *f = TFile::Open("eventsout.root");
if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
TTree *t;
f->GetObject("treeout", t);
if (!t) { delete f; return; } // just a precaution
t->SetMakeClass(1); // tree in MakeClass mode
t->SetBranchStatus("*", 0); // disable all branches
const Int_t kMaxin = 2;
Int_t in;
Double_t x[kMaxin]; //[in] (a variable size array)
t->SetBranchStatus("in", 1); // activate branch
t->SetBranchAddress("in", &in);
t->SetBranchStatus("in.t", 1); // activate branch
t->SetBranchAddress("in.t", x);
Long64_t n = t->GetEntries();
if (n > 10) n = 10; // just the first 10 events
for (Long64_t i = 0; i < n; i++) {
t->GetEntry(i);
std::cout << i << " : " << in << " :";
if (in > kMaxin)
{ std::cout << " OUCH! WE ARE DOOMED!" << std::endl; break; }
for(Int_t j = 0; j < in; j++) {
std::cout << " " << x[j];
}
std::cout << std::endl;
}
// t->ResetBranchAddresses(); // disconnect from local variables
delete f; // automatically deletes "t", too
}
cjt00
March 12, 2019, 3:06pm
5
That worked thanks!
Have you any idea what was wrong with my code?
system
Closed
March 26, 2019, 3:06pm
6
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.