Reading array from TTree?

Hi there,
I am wondering what is the correct way to read an array from a TTree. I have tried to model my code after the tree1r() script on page 202 of Chapter 12 in the User’s Guide, adapted by me for arrays. It actually runs, most of the time, under CINT for Windows. However I am very suspicious that I am not cleaning up the memory correctly after reading the array, which leads to infrequent but mysterious problems. The basic steps of my code are below. I am just wondering if anyone sees any obvious problems with it. Thanks very much.
Best regards,
Penny

[code]f = new TFile(fff, “READ”);
T = (TTree *)f->FindObjectAny(“T”);

TBranch *b_voltagesI;
b_voltagesI = T->GetBranch(“voltages_chan1”); // "voltages_chan1[10000]/D"
Double_t voltagesI[10000];
b_voltagesI->SetAddress(voltagesI);
for (Int_t i=0;i<10000;i++)
{
b_voltagesI->GetEntry(i);
// Insert calculations on voltagesI array here.
}

delete voltagesI; // Is this right?
delete T;
delete f;[/code]

delete voltagesI; // Is this right? No, The correct C++ syntax is:

Cheers,
Philippe.

Hooray, thanks. Forgot to include the ‘newbie alert’ in my post. :bulb:
Regards,
Penny

Double_t voltagesI[10000]; // ... delete [] voltagesI;
… looks weird to me.

Oups. Pepe is right. My answer assumes that you meant:Double_t *voltagesI = new Double_t[10000];If you do indeed us Double_t voltagesI[10000];, there is no need to delete the voltagesI.

Cheers,
Philippe.

Hi Pepe and Philippe,
Thanks so much for the (frequent and much appreciated) help! These boards are the greatest.
Best regards,
Penny

T = (TTree *)f->FindObjectAny("T"); // ... delete T;
… is this “delete” really necessary?

Hi Pepe,

In this case the delete is ‘redundant’ because TTree object are owner by the TFile they are read from (but the code is such that the explicit deletion of the TTree is still legal … the TFile is informed when the TTree is explicitly deleted).

Cheers,
Philippe.

That’s what i thought, too.
Another strange question … would “delete b_voltagesI;” be legal, too?
And what about sequences “delete b_voltagesI; delete T;” and “delete T; delete b_voltagesI;”?

[quote]would “delete b_voltagesI;” be legal, too?[/quote]No, it would not. A TBranch is an integral part of the TTree structure and the TTree can not be partially deleted.

Cheers,
Philippe.