Problems with TTree::GetEntry() method

Dear rooters,

I have a very embarrassing problem with TTree::GetEntry() method. I have developed a very simple code:

[code]
void simple_dump(){

TFile *file = new TFile (“P1G.root”);

TTree tree = (TTree)file->Get(“USR1”);

if(tree==0) return;

tree->SetMakeClass(1);

Int_t eRun;
Int_t eSpill;
Int_t eEvinspill;

tree->SetBranchStatus("*", 0);

tree->SetBranchAddress(“eRun”,&eRun);
tree->SetBranchAddress(“eSpill”,&eSpill);
tree->SetBranchAddress(“eEvinspill”,&eEvinspill);

Long64_t events = tree->GetEntries();

cout<<"****"<<events<<endl;

cout<<“Evt\t”<<“Spill\t”<<“Run\t”<<endl;

for(Long64_t i = 0; i<events; i++)
{
tree->GetEntry(i);

  cout<<eEvinspill<<'\t'<<eSpill<<'\t'<<eRun<<endl;
}

}[/code]

Which aims to make a simple dump of three variables from a tree inside “P1G.root” file (attached). I expect to have dumped down all data but I got “zeros” :confused:

Why after tree->GetEntry(i); the variables are not filled? :blush:

I guess I’m doing some very basic mistake. :question:

Could someone please point me my mistake?

Many thanks in advance.

Cheers,
Luís.
P1G.root (70.3 KB)

This message has been seen 26 times and I got not even a single comment / hint / answer !?!?!? :open_mouth:

Regards,
Luís.

After seen one reply from pcanal in a very old post of mine ([url]Problem while reading from tree using SetBranchAddress() I’ll try to use TFile::MakeProject to get the original library. I guess it is same problem.

Cheers,
L.

I actually don’t see any problems with your code. Your tree contains many branches and many of them contain constant values, such as eRun which contains only 30772. I guess that is intentional. One thing that might be, is that you have not chosen the proper type at the time of the creation of the tree, like:

can you check that?

Ok it is solved :smiley:

[code]void simple_dump(){

gSystem->Load(“HptEventOld.so”); // <— Create and load the library using TFile::MakeProject class

TFile *file = new TFile (“P1G.root”);

TTree tree = (TTree)file->Get(“USR1”);

if(tree==0) return;

//tree->SetMakeClass(1); // no need to SetMakeClass()

Int_t Run;
Int_t Spill;
Int_t Evinspill;

//tree->SetBranchStatus("*", 0); // no need to “turn off” all branches

HptEventOld *ptr = 0; // <— create a pointer for the structure

tree->SetBranchAddress(“Event”,&ptr); // <— and use to hold the whole tree

// no need of these too
// tree->SetBranchAddress(“eRun”,&Run);
// tree->SetBranchAddress(“eSpill”,&Spill);
// tree->SetBranchAddress(“eEvinspill”,&Evinspill);

Long64_t events = tree->GetEntries();

cout<<"****"<<events<<endl;

cout<<“Evt\t”<<“Spill\t”<<“Run\t”<<endl;

for(Long64_t i = 0; i<events; i++)
{
tree->GetEntry(i);

  cout<<ptr->eEvinspill<<'\t'<<ptr->eSpill<<'\t'<<ptr->eRun<<endl; // <--- the way to access the leafs 
}

}[/code]

Long time passed since PCanal reply and I’ve hadn’t the time to say thank you. This is also an embarrasing situation. :blush:

But now I had the opportunity, and this time I won’t wasted: Thank PCanal for your reply ([url]Problem while reading from tree using SetBranchAddress() =D>

Cheers,
Luís.