Using CopyTree to select events for TMVA training

Hi, experts
I am modifying codes from TMVAClassification.C. The code shown below is at the part before the invocation of TMVA::Factory, and I want to select only some numbers of entries from the S/B tree (without generating extra root file), so I did it with CopyTree.
However, if the CopyTree code is put right after getting the original tree (the commented part), (in some situation, like Nselect is large) the following error will be thrown

Error in <TSystem::ExpandFileName>: input: $HOME/.root.mimes, output: $HOME/.root.mimes
Error in <TBranch::TBranch::WriteBasketImpl>: basket's WriteBuffer failed.

On the other hand, if the ``CopyTreecode is put in its current location in the code, only the first error was thrown, and the output of the training result contains an extra Tree (I believe it was the mix of my S&B tree). But I did not code toWrite()``` the cloned tree whatsoever.

//Register the tree
    TTree *sigTreeOri = (TTree*)inputSigFile->Get("tree");
    TTree *bkgTreeOri = (TTree*)inputBkgFile->Get("tree");

//     int Nselect = 25000;
//     TTree *sigTree = sigTreeOri->CopyTree("", "", Nselect , 0);
//     TTree *bkgTree = bkgTreeOri->CopyTree("", "", Nselect , 0);

    //Create output file for the tmva training results
    TString outfileName("TMVA.root");
    TFile* outputFile = TFile::Open(outfileName, "RECREATE");

    int Nselect = 25000;
    TTree *sigTree = sigTreeOri->CopyTree("", "", Nselect , 0);
    TTree *bkgTree = bkgTreeOri->CopyTree("", "", Nselect , 0);

could you point out what could possibly went wrong ?
Also, some doubts:

  1. could I possibly prevent the cloned tree to be written by some functions ?
  2. Is CloneTree better, if i just want to select a number of events from the original tree ?
  3. I just found TTree->SetEntries(). Does that function does the same job ?

Thanks in advance.


_ROOT Version: 6.24 (PyROOT via conda)
_Platform:Centos7
_Compiler: gcc9


HI,
If you don’t want the cloned Tree’s to be written in the TMVA output file (“TMVA.root”), you should create and open the output file after having cloned the Tree (your first commented case). In this case it will be in memory, but this will work if the number of entries are not too large.
In the second case the copied trees will be attached to the TMVA output file and when the buffer is full, it will be written automatically in the output file.
If you don’t want to have it in the output file, you can open another temporary file to store the copied Trees before giving them to TMVA.
Note you can use also RDataFrame Snapshot for creating copies of the TTree (see ROOT: ROOT::RDF::RInterface< Proxied, DataSource > Class Template Reference).

Lorenzo