"Filtering" TTrees


I couldn’t figure out how to implement this on Root, can anyone help me please?

I’d like to read entry by entry of a TTree and write the contents of all of it’s branches on another TTree.

The problem is: some entries should be written and others shouldn’t (depending on some criteria) and I don’t know in advance the name of all its branches.

The code should go like this:

TFile *inputFile = new TFile("abc.root");
TFile *outputFile = new TFile("xyz.root", "RECREATE");

TTree *inputTree = (TTree *) inputFile->Get("input");
TTree *outputTree = new TTree("output", "Output Tree");

std::vector<float> *criteriaBranch = new std::vector<float>;

inputTree->SetBranchAddress("criteria", &crateriaBranch);
outputTree->Branch("criteria", &criteriaBranch);

int n = inputTree->GetEntries();
for (int i = 0; i < n; i++) {
  if (criteriaBranch->at(0) > 2.0f) { // This should be some complex criteria
    // ???? Copy all other branches from inputTree to outputTree ????


delete inputFile;
delete outputTree;
delete outputFile;
delete criteriaBranch;

see example in $ROOTSYS/tutorials/tree/copytree3.C


Thank you!