Apply a selection on an existent TTree without copying it

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 !

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;
}

Hi,

maybe TEntryList is the way to go?

Best,
Alvaro

1 Like

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