Split TTree from two files in other two files

Hi all,

I have two file with a TTree inside and I would like to split them in other two files following some selection criteria.
For a single file there are not problem, I found also this topic:

[url]Splitting Tree in multiple files

and everything is ok, but if I have two files no.
The two files are: tmva_train_vh_allbkg.root and tmva_train_vbf_allbkg.root, in the first file there are 3 branches more, the other are the same. The name of tree and branches are the same.

My code is below:

{  
  TFile *read1 = TFile::Open("tmva_train_vh_allbkg.root");
  TTree *T1 = (TTree*)read1->Get("TestTree");
  Int_t nentries_1 = T1->GetEntries();
  cout << nentries_1 << endl;

  TFile *read2 = TFile::Open("tmva_train_vbf_allbkg.root");
  TTree *T2 = (TTree*)read2->Get("TestTree");
  Int_t nentries_2 = T2->GetEntries();
  cout << nentries_2 << endl;

  TFile *f1 = new TFile("new_sig.root","recreate");
  TTree *newtree1 = T1->CloneTree(0);
  
  TFile *f2 = new TFile("new_bkg.root","recreate");
  TTree *newtree2 = T1->CloneTree(0);
  
  Int_t type;
  T1->SetBranchAddress("type", &type);

  for (Int_t i=0;i<nentries_1; i++)
    {
      T1->GetEntry(i);
      if (type == 1) //Sig
        {
          newtree1->Fill();
        }
      else //Bkg
        {
          newtree2->Fill();
        }
    }
  
  T2->SetBranchAddress("type", &type);
  for (Int_t j=0;j<nentries_2; j++)
    {
      T2->GetEntry(j);
      if (type == 1) //Sig
        {
          newtree1->Fill();
        }
      else //Bkg
        {
          newtree2->Fill();
        }
    }

  f1->Write();
  f2->Write();
}

The result is in attach. There is a “background” and a peak.
The “background” is correct, if I enlarge it, it is the result from a file, the peak is the problem, it is like the program can not read the true values and use a constant to fill it.
Also the number of entries is correct because are 17000 for each files, so I have 32000 in total and 17000 for the peak.

Do you have any idea?
Thank you
Ciccio

Hi,

The problem is that the T2 tree is not getting its data from your input tree. Except for the branch type, there is no connection between T2 and the other 3. The other 3 trees are connected because the call to CloneTree implicit set the address the branch look at for data to be the same. To solve the problem, replacing: T2->SetBranchAddress("type", &type);withT1->CopyAddresses(T2);should be enough.

Cheers,
Philippe.