Problem of spliting a tree

Hello,
I have a tree called tree_orig, I want to split it to several ones due to some selections. So I do in the way:

TTree *tree_new[3][5];
//clone tree structure
  for ( int itype=0; itype<3; itype++) {
    for ( int ijet=0; ijet<5; ijet++ ) {
      outfile[itype][ijet]->cd();
      tree_new[itype][ijet] = (TTree*)tree_orig->CloneTree(0);
    }
  }

  //loop all events to split...
  for ( int ientry=0; ientry<Nallevents; ientry++ ) {
    tree_orig->GetEntry(ientry);
    if ( SomeCondtionHere )
      tree_new[itype][ijet]->Fill();
}
  //write to files
  for (int itype =0; itype<3; itype++) {
    for (int ijet=0; ijet<5; ijet++) {
      outfile[itype][ijet]->cd();
      tree_new[itype][ijet]->Write();
      outfile[itype][ijet]->Write();
    }
  }

A weird thing here is when I open a new file with a new tree, the tree inside is bad with the error like:

Error in <TFile::TFile>: file original.root does not exist

original.root is the original root file on which all splited trees are based. Why new files have locations of the original file? If I don’t use array of pointers of tree, just pointers of tree, every thing is here. Anybody gives me any hint about this problem?

Thanks,
Zhiyi.

Unfortunately you do not show the most important part of the code where you create the directories. Anyhow, I suggest replacing you statements like

tree_new[itype][ijet]->Write(); by

tree_new[itype][ijet]->AutoSave();
Rene

Thanks.

Actually, I am trying to store those trees in different files instead of different directories in one new file. I think the problem perphaps is I create tree without “cd” to their files, the following codes are not still working:

  char sbuf[256];

  //open input file
  TFile *inputfile = new TFile(inputfilename, "READ");
  //get tree
  TTree *tree_top    = (TTree*)gDirectory->Get("TopologicalVariables");

  //create new files
  TString outputfilename = inputfilename;
  outputfilename.ReplaceAll(".root", "");
  //create new files:outfile[type][njet]  and trees
  TFile *outfile[3][5];
  TTree *tree_new[3][5];
  //clone tree structure
  TString str_njet[5] = {"EqOneJet", "EqTwoJet", "EqThreeJet", "EqFourJet", "GeFiveJet"};
  for ( int itype=0; itype<3; itype++) {
    for ( int ijet=0; ijet<5; ijet++ ) {
      sprintf(sbuf, "%s_type%i_%s.root", outputfilename.Data(), itype+1, str_njet[ijet].Data() );
      printf("   %-25s created...\n", sbuf);
      outfile[itype][ijet] =  new TFile(sbuf, "RECREATE");
      outfile[itype][ijet]->cd();
      tree_new[itype][ijet] = (TTree*)tree_top->CloneTree(0);
    }
  }

  //loop all events to split...
  for ( int ientry=0; ientry<Nallevents; ientry++ ) {
    tree_orig->GetEntry(ientry);
    if ( SomeCondtionHere )
      tree_new[itype][ijet]->Fill();
}
  //write to files
  for (int itype =0; itype<3; itype++) {
    for (int ijet=0; ijet<5; ijet++) {
      outfile[itype][ijet]->cd();
      tree_new[itype][ijet]->Write();
      outfile[itype][ijet]->Write();
    }
  } 

If I didn’t use array to define files and tree, say, define a file and then define a tree with it, that is working well. So as I said above, the problem may be from the fact that trees are not properly stored in their files. But the tricky point is I want to use array to define files and tree since it can reduce some codes.

Zhiyi

[quote=“brun”]Unfortunately you do not show the most important part of the code where you create the directories. Anyhow, I suggest replacing you statements like

tree_new[itype][ijet]->Write(); by

tree_new[itype][ijet]->AutoSave();
Rene[/quote]

Hi,

There is nothing obvious that could lead to this error message in the code you provided. One thing that you could look at is to see what is the stack trace when TObject::Error is ran (use gdb or your favorite debugger). Otherwise we would need a complete running example to understand the issue.

Cheers,
Philippe

Thanks.

I am sorry to forget to mention ROOT version I am using. Actually the problem is happened for D0 software framework with ROOT version 4.04, I just transfer to ROOT 5.08 on my local computer, the problem is gone.

[quote=“pcanal”]Hi,

There is nothing obvious that could lead to this error message in the code you provided. One thing that you could look at is to see what is the stack trace when TObject::Error is ran (use gdb or your favorite debugger). Otherwise we would need a complete running example to understand the issue.

Cheers,
Philippe[/quote]

Hi,

So it works with a newer version of ROOT (5.08) so unless there is an imperative strong reason why you must do this work using the older version of ROOT (and unless you provided more information :slight_smile: ) I would declare this problem fixed.

Cheers,
Philippe

I agree with you that the problem is fixed. As you know, for D0, default root version within framework is old one, anyway, I can use newer version.

[quote=“pcanal”]Hi,

So it works with a newer version of ROOT (5.08) so unless there is an imperative strong reason why you must do this work using the older version of ROOT (and unless you provided more information :slight_smile: ) I would declare this problem fixed.

Cheers,
Philippe[/quote]

I got a solution from Philippe, and the problem now is solved essentially. I copy his reply here for keeping record: