Writing to a TNtuple 2 times

Hi,

I have a problem. Here is the code (below). If I call the test::fill() function twice the TNtuple will be filled with crazy numbers the second time.

[code]//////////////////////////////////////////////////////
//testntuple.C

#include <TNtuple.h>
#include

using namespace std;

class test{
public:
test();
~test();
void fill();
public:
TNtuple * n;
};

test::test(){
n = new TNtuple(“n”,“n”, “x:y”);
}

test::~test(){
delete n;
}

void test::fill(){
for(int i = 0; i < 1000; i++){
n->Fill(i, i);
}
cout << n->GetEntries() << endl;
float x,y;
n->SetBranchAddress(“x”, &x);
n->SetBranchAddress(“y”, &y);
for(int j = 0; j < n->GetEntries(); j++){
n->GetEntry(j);
cout << x << ", " << y << endl;
}
}
void testntuple(){
test * t = new test();
t->fill();
t->fill();
}

//////////////////////////////////////////////////////[/code]

The problem is - I guess - that I call the SetBranchAddress and after that I have to set it “back” somehow…

Can someone help me?

Balint

Hi,

Okay, I got the solution. After the calling the SetBranchAddress functions I have to call the ResetBranchAddresses() function to set them back.

Balint

float x,y; n->SetBranchAddress("x", &x); n->SetBranchAddress("y", &y); Since you are setting the addresses to memory that is local you should call ResetBranchAddresses as soon as the memory is being released. I.e. in your case you need to call ResetBranchAddresses at the end of test::fill().

Cheers,
Philippe.