# Apply a selection on an existent TTree without copying it

**URL:** <https://root-forum.cern.ch/t/apply-a-selection-on-an-existent-ttree-without-copying-it/58242>\
**Category:** ROOT\
**Tags:** tree, tmva, root\
**Created:** [February 22, 2024, 10:36am UTC](https://root-forum.cern.ch/t/apply-a-selection-on-an-existent-ttree-without-copying-it/58242 "2024-02-22T10:36:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alo](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/alo/32/33312_2.png) [@Alo](https://root-forum.cern.ch/u/Alo)\
**Post date:** [February 22, 2024, 10:36am UTC](https://root-forum.cern.ch/t/apply-a-selection-on-an-existent-ttree-without-copying-it/58242/1 "2024-02-22T10:36:54Z")

</div>

Hi everyone,

I am trying to count the number of entries in a TTree under a MVA response cut (on the code bellow). But, before doing that, I want to apply a selection on the TTree without modifying it. However, the ROOT file is heavy. Then, I can’t use `TTree::CopyTree` to avoid memory problems. Does someone have an idea for that ?

Thanks !

```auto
Float_t Normed_S_or_B_With_Response_Cut(TString rootFileName, Float_t Norm_W, Float_t response_Cut, TString weightFile, TString treeName,
                                        vector<TString> condsVar, TString conds, vector<TString> readVarsFloat, vector<TString> readVarsDouble)
{  

    TMVA::Reader *reader = new TMVA::Reader( "!V:!Color" );

    Float_t floatVars[readVarsFloat.size()];

    Float_t floatedDoubleVars[readVarsDouble.size()];
    Double_t doubleVars[readVarsDouble.size()];

    for(Int_t p=0 ; p<readVarsFloat.size() ; p++)
    {
        reader->AddVariable(readVarsFloat[p], &floatVars[p]);
    }
    for(Int_t l=0 ; l<readVarsDouble.size() ; l++)
    {
        reader->AddVariable(readVarsDouble[l], &floatedDoubleVars[l]);
    }

    reader->BookMVA( "MVA", weightFile );

    TFile* rootFile = TFile::Open(rootFileName, "READ");
    TTree* myTree = (TTree*)rootFile->Get(treeName);

    myTree->SetBranchStatus("*", 0);

    for(Int_t i=0; i<condsVar.size(); i++)
    {
        myTree->SetBranchStatus(condsVar[i], 1);
    }
    for(Int_t j=0 ; j<readVarsFloat.size() ; j++)
    {
        myTree->SetBranchStatus(readVarsFloat[j], 1);
        myTree->SetBranchAddress(readVarsFloat[j], &floatVars[j]);
    }
    for(Int_t m=0 ; m<readVarsDouble.size() ; m++)
    {
        myTree->SetBranchStatus(readVarsDouble[m], 1);
        myTree->SetBranchAddress(readVarsDouble[m], &doubleVars[m]);
    }

    Int_t condsEntries = 0;

    for(Int_t n=0 ; n<myTree->GetEntries() ; n++)
    {
        myTree->GetEntry(n);

        for(Int_t n=0 ; n<readVarsDouble.size() ; n++)
        {
            floatedDoubleVars[n] = doubleVars[n];
        }

        if(reader->EvaluateMVA("MVA")>response_Cut)
        {
            condsEntries++;
            cout << condsEntries << endl;
        }

    }

    Float_t SB = condsEntries * Norm_W ;

    delete myTree;
    delete rootFile;

    return SB;
}

```

---

<div class="post-metadata">

**Author:** ![atd](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/atd/32/33835_2.png) [@atd](https://root-forum.cern.ch/u/atd)\
**Post date:** [February 23, 2024, 2:57pm UTC](https://root-forum.cern.ch/t/apply-a-selection-on-an-existent-ttree-without-copying-it/58242/2 "2024-02-23T14:57:30Z")

</div>

Hi,

maybe [TEntryList](https://root.cern.ch/doc/master/classTChain.html#a23261bfd5cef42408c053ad2233e35d0) is the way to go?

Best,  
Alvaro

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [March 8, 2024, 2:58pm UTC](https://root-forum.cern.ch/t/apply-a-selection-on-an-existent-ttree-without-copying-it/58242/3 "2024-03-08T14:58:19Z")

</div>

Closed on March 8, 2024, 2:58pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
