A problem with TTree::Fill()

Hi, all

I have came up with a strange problem with TTree::Fill(). I have a tree with some data and now I wat to pick out some of them to form a new tree. Here is my code:

void split()
{
TFile *input = new TFile(" input.root", “READ”);
TTree *original = (TTree *)input->Get(“Train”);

Double_t data1;
Double_t data2;
Double_t data3;
Int_t data4;

original->SetBranchAddress("data1", &data1);
original->SetBranchAddress("data2", &data2);
original->SetBranchAddress("data3", &data3);
original->SetBranchAddress("data4", &data4);

TTree *newtree = new TTree("Train", "My New Tree");
newtree->Branch("data1", &data1, "data1/D");
newtree->Branch("data2", &data2, "data2/D");
newtree->Branch("data3", &data3, "data3/D");
newtree->Branch("data4", &data4, "data4/I");

for(Int_t i = 0; i < original->GetEntries(); i++)
  {
      original->GetEntry(i);

      if(data1 > 0.5)  newtree->Fill();

  }

}

When I load this macro in ROOT v5.14, ROOT gives out following error messages while i equals certain values:

Error in TTree::Fill: Failed filling branch: Train.data1, nbytes=-1
This error is symptomatic of a Tree created as a memory-resident Tree
Instead of doing:
TTree *T = new TTree(…)
TFile *f = new TFile(…)
you should do:
TFile *f = new TFile(…)
TTree *T = new TTree(…)
Error in TTree::Fill: Failed filling branch:Train.data2, nbytes=-1
This error is symptomatic of a Tree created as a memory-resident Tree
Instead of doing:
TTree *T = new TTree(…)
TFile *f = new TFile(…)
you should do:
TFile *f = new TFile(…)
TTree *T = new TTree(…)
Error in TTree::Fill: Failed filling branch:Train.data3, nbytes=-1
This error is symptomatic of a Tree created as a memory-resident Tree
Instead of doing:
TTree *T = new TTree(…)
TFile *f = new TFile(…)
you should do:
TFile *f = new TFile(…)
TTree *T = new TTree(…)
Error in TTree::Fill: Failed filling branch:Train.data4, nbytes=-1
This error is symptomatic of a Tree created as a memory-resident Tree
Instead of doing:
TTree *T = new TTree(…)
TFile *f = new TFile(…)
you should do:
TFile *f = new TFile(…)
TTree *T = new TTree(…)

I checked the value of the branches of the entries which induce the error, however they appears quite normal.
Thanks in advance for any help!

You did not open any file in write mode where to store your new Tree.

Rene

[quote=“brun”]You did not open any file in write mode where to store your new Tree.

Rene[/quote]

Thanks very much for responding.

But in my script. I need not to store the new tree. I just want to use it in the following process. Do I have to open a file to write before I create a new tree in memory?

After I submit the message above, I change the first a few lines of the macro to this:

TChain original(“Train”);
original.Add(“input.root”);

original.SetBranchAddress(“data1”, &data1)

Then the problem disappeared. Is there any differences between these two ways to retrieve a tree from a file?

Thanks a lot.

[quote]Is there any differences between these two ways to retrieve a tree from a file? [/quote]Yes. In your second example, no file has been open yet when you create the output tree and hence the current directory is still gROOT.

The solution in your first case (since you want a in-memory tree rather than a on file tree) is to precede the new TTree with:gROOT->cd();

Cheers,
Philippe.