Fill one branch at a time problem. Memory problem

Hello again, I have some questions regarding the code below

a) What I get in the end is an empty tree. Meaning 0 entries, what am I doing wrong?
b) When I uncomment the line that deletes the pointers to the branches (see end of script) cint throws a segmentation error and crashes. Again can someone explain it to me? Is it necessary to delete the pointers to the branches?
c) With that line commented the script seems to run fine, although the tree has no entries. After I exit cint with .q i get an output which I post in the end of this thread. I have seen these happening many times and I know it is probably my memory handling, I want to start doing things right, so any help much appreciated.

[code]{
TFile siFile(“2802.root”);
TTree * siTree=(TTree*) siFile.Get(“siTree”);

vector * clPos1x=0;
vector * clPos3x=0;
vector * clPos6x=0;

TBranch * b_1x;
TBranch * b_3x;
TBranch * b_6x;

siTree->SetBranchAddress(“clustPos1x”,&clPos1x,&b_1x);
siTree->SetBranchAddress(“clustPos3x”,&clPos3x ,&b_3x);
siTree->SetBranchAddress(“clustPos6x”,&clPos6x ,&b_6x);

Float_t a;
Float_t b;
Int_t ev;

TFile extrFile(“extr2802.root”,“RECREATE”);
TTree extrTree(“extrTree”,“extrTree”);

TBranch * b_a = extrTree.Branch(“ax”, &a,“a/F” );
TBranch * b_b = extrTree.Branch(“bx”, &b,“b/F” );
TBranch * b_ev = extrTree.Branch(“ev”, &ev,“ev/I” );

Float_t c3=176;Float_t c1=15496;Float_t c2=30976;
Float_t z6=900;Float_t z1=860;Float_t z3=0;
Float_t p1,p2;

for(Int_t i=0;iGetEntries();i++){if (i%100==1) cout<<i<<"\n";
clPos1x.clear();clPos3x.clear();clPos6x.clear();
siTree->GetEntry(i);
ev=i;

if (i%10==1) cout<<clPos3x.size()<<"\n";
if( clPos3x.size()==1 && clPos1x.size()==1 && clPos6x.size()==1){	
p1=z3*(*clPos3x)[0]+z6*(*clPos6x)[0]+z1*(*clPos1x)[0];
p2=(*clPos3x)[0]+(*clPos6x)[0]+(*clPos1x)[0];

a=(p2-c3*p1/c1)/(3.-c2/c1);
b=(3*p1-c3*p2)/(3*c1-c2);

//b_a->Fill();	
//b_b->Fill();
}
else {a=NULL;b=NULL;}

//b_ev->Fill();
extrTree->Fill();
}

//extrTree->Write("", TObject::kOverwrite);
extrFile->Write();
delete clPos1x;delete clPos3x;delete clPos6x;
delete siTree;
//delete b_a;delete b_b;delete b_ev;delete b_1x;delete b_3x;delete b_6x;
extrFile->Close();
siFile->Close();
}[/code]

After I quit cint:

*** glibc detected *** /home/psifunk/root/bin/root.exe: corrupted double-linked list: 0x08ca4c28 ***
======= Backtrace: =========
/lib/tls/i686/cmov/libc.so.6[0xb6c72f09]
/lib/tls/i686/cmov/libc.so.6[0xb6c74a8d]
/lib/tls/i686/cmov/libc.so.6(__libc_malloc+0x95)[0xb6c768c5]
/usr/lib/libstdc++.so.6(_Znwj+0x27)[0xb6e6adc7]
/usr/lib/libstdc++.so.6(_Znaj+0x1d)[0xb6e6af0d]
/home/psifunk/root/lib/libCore.so(ErrorHandler+0xf6)[0xb78fece6]
/home/psifunk/root/lib/libCore.so(_Z5BreakPKcS0_z+0x33)[0xb78fefa3]
/home/psifunk/root/lib/libCore.so(_ZN11TUnixSystem15DispatchSignalsE8ESignals+0xc2)[0xb79d9042]
/home/psifunk/root/lib/libCore.so[0xb79d914d]
/home/psifunk/root/lib/libCore.so[0xb79cf9f2]
[0xb7f50400]
======= Memory map: ========
08048000-0804a000 r-xp 00000000 08:11 98327 /home/psifunk/root/bin/root.exe
0804a000-0804b000 rw-p 00001000 08:11 98327 /home/psifunk/root/bin/root.exe
08715000-08e95000 rw-p 08715000 00:00 0 [heap]
b6400000-b6421000 rw-p b6400000 00:00 0
b6421000-b6500000 —p b6421000 00:00 0
b652a000-b652b000 rw-p b652a000 00:00 0
…and goes on

Hi,

From your example I do not see a reason to Fill each branch individually; Note that in almost all cases it is necessary to have all branches being evenly filled (in particular in order to know which entry of the branches goes together).

[quote]a) What I get in the end is an empty tree. Meaning 0 entries, what am I doing wrong? [/quote]It means that either you did not properly fill the TTree or that you are using the very advanced case of filling branches individually and did not call TTree::SetEntries.

[quote]b) When I uncomment the line that deletes the pointers to the branches (see end of script) cint throws a segmentation error and crashes. Again can someone explain it to me? Is it necessary to delete the pointers to the branches? [/quote]Never delete the branch pointers :slight_smile: it is fatal. The TBranch objects belong to the TTree object and will be deleted by the TTree (which itself will be deleted by the TFile).

Cheers,
Philippe.

Hi Philippe,
I will explain why I wanted to fill the branches individually . Probably there is a better way of achievnig what I wanted.
I have events from a detector system , 3 detectors in a row. In this stage of the analysis I have the clustering information and I want to do a simple straight line reconstruction using only events with one cluster in all three detectors. So I need a branch with parameters a and b but this information is relevant only for the events(entries) tha satisfy the if statement

So I also added a branch which holds the event number. That way eventes that dont satisfy the reguirements would get an entry number but no a and b.I hoped like that I can AddFriend this tree to the tree with the original data and I have a and b coresponding to the right event number so I can use eg Draw . Could you suggest other ways to do this?

Thanks,
Marios

[quote] That way eventes that dont satisfy the reguirements would get an entry number but no a and b.[/quote]This is not what you would get by filling the branch unevenly. Instead the a,b will be ‘assigned’ to the wrong event number (i.e if the event skip is ‘event #200’ for entry ‘200’, then (if I understood correctly), you would not fill ‘a and b’ for this entry; then upon filling the next you would fill the number 201 in the ‘event number column’ and then you would enter stuff in ‘a and b’ … however since they are 200th value entered in that branch they would be associated with the tree entry 200 and hence the event number 200!.

Instead all you need is to have your event number column and the a and b columns and fill all three only for the event you want to keep.

Then if you can TTree::BuildIndex on this tree and AddFriend this tree to the tree with the original data and you will have a and b coresponding to the right event number so you use eg Draw.

Cheers,
Philippe.