Filtering a TTree - need help with variation

Hi - I’m using ROOT 4.00/08.

I have a TTree in one file with many branches.

I’m filtering this TTree into a smaller TTree in a different file with a subset of branches and entries using code similar to this:

root.cern.ch/root/html/examples/copytree2.C.html

Except that I use

newtree->AutoSave()
newtree->Fill()

instead of newtree->CopyEntries()

Now - I’d like to load a branch from the old tree, so that I can access its information, but I do not want to save this branch in the new tree.

I tried (a simplified example):

oldtree->SetBranchStatus(“momentum”,0);
// Some other branches have status set to 1
TTree *newtree = oldtree->CloneTree(0);
oldtree->SetBranchStatus(“momentum”,1);

for (Int_t i=0;i<nentries; i++) {
if (oldtree->LoadTree(i) < 0) {
//Fail
}
oldtree->GetEntry(i);
// Print value of momentum
// Other stuff
}

but it does not look like the information in the momentum branch is being accessed correctly: I get weird behavior where the first n printouts of
momentum come out as 0, even though they should not be.

Is there a different way to do this? Or is the above supposed to work?Thanks.

-Ed

Hi Ed,

Try the following:

[code]oldtree->SetBranchStatus(“momentum”,0);
TBranch *bmomentum = oldtree->GetBranch(“momentum”);
// Some other branches have status set to 1
TTree *newtree = oldtree->CloneTree(0);

for (Int_t i=0;i<nentries; i++) {
if (oldtree->LoadTree(i) < 0) {
bmomentum->GetEntry(i,1);
}[/code]
Rene

Hi Rene - It doesn’t work. I get zero when I printout:

Double_t p;

oldtree->SetBranchStatus("momentum",0); 
TBranch *bmomentum = oldtree->GetBranch("momentum"); 
bmomentum->SetAddress(&p);
// Some other branches have status set to 1 
TTree *newtree = oldtree->CloneTree(0); 

for (Int_t i=0;i<nentries; i++) { 
  if (oldtree->LoadTree(i) < 0) { 
  bmomentum->GetEntry(i,1); 
  //break
  }
  cout << p << endl;

I also tried:

[code]oldtree->SetBranchStatus(“momentum”,0);
TBranch *bmomentum = oldtree->GetBranch(“momentum”);
// Some other branches have status set to 1
TTree *newtree = oldtree->CloneTree(0);

for (Int_t i=0;i<nentries; i++) {
if (oldtree->LoadTree(i) < 0) {
//break
}
bmomentum->GetEntry(i,1);
[/code]

But that gives a seg violation.

Thanks.

-Ed

Ed,

You should not set the address of branch "momentum"
Could you try the following variant

[code]//oldtree->SetBranchStatus(“momentum”,0);
//TBranch *bmomentum = oldtree->GetBranch(“momentum”);
// Some other branches have status set to 1
TTree *newtree = oldtree->CloneTree(0);
newtree->SetBranchStatus(“momentum”,0);

for (Int_t i=0;i<nentries; i++) {
if (oldtree->LoadTree(i) < 0) {
//break
}
//bmomentum->GetEntry(i,1); [/code]
Rene

Hi Rene- that does work. Unfortunately - the problem is that I’d like to replace (essentially correct) the values of “momentum” with my own values in the new tree, but at the same time, keep the same branch name.

The reason for this is that a lot of my code uses the branch name “momentum”, so I’d rather not come up with a new name, and also, in most of my ntuples, the “momentum” branch has already been written out, and those values were correct.

Thanks.

-Ed

Hi Ed,

In this case the recipee is simple. Proceed as indicated above and
after having CloneTree, create a new branch "momentum"
newtree->Branch(“momentum”,&momentum,“momentum/F”);

Rene