TTree::GetEntries() give wrong numbers?

Hello,

I’m trying to select some signal events from a TTree by applying different cuts in its branches, actually, everything works, and apparently, the cuts done are well applied; Even so, the number of entries change depending on the method i use:

So, I start from a TTree with a known number of entries (28739) which, after applying a cut and using TTree::GetEntries() contains 27315, then i copy the reduced tree to another file. However, if i inspect the new tree using a TBrowser, it shows me a different number of entries (25516). why is this happening? am I doing something wrong? which number is correct?
it will be probably a dumb question, but is running me crazy.

there’s the code i use to apply the cut, and to save the new reduced tree:

    TString finput = "Lb2L0Gamma_Offline_mcmatch_L0DD.root";
    TString foutput = "data_background_MC.root";
    
    TFile *input =new TFile(finput, "READ");
    input->cd("Lb2L0GammaTuple");

    TTree *datainput = (TTree*) gDirectory->Get("DecayTree");

    datainput->SetBranchStatus("*", 0);
    datainput->SetBranchStatus("Lambda_b0_M", 1);
    datainput->SetBranchStatus("Lambda_b0_PT",1);
    datainput->SetBranchStatus("Lambda_b0_FD_OWNPV",1);
    datainput->SetBranchStatus("Lambda_b0_FDCHI2_OWNPV",1);
    datainput->SetBranchStatus("Lambda_b0_MTDOCA",1);
    datainput->SetBranchStatus("Lambda_b0_MTDOCACHI2",1);
    datainput->SetBranchStatus("Lambda_b0_DOCA_12", 1);
    datainput->SetBranchStatus("Lambda0_M", 1);
    

    TFile *f = new TFile (foutput, "RECREATE");
    f->mkdir("MC");
    f->cd("MC")

    TTree *dataoutput = datainput->CopyTree("Lambda_b0_M>5270.5 && Lambda_b0_M<5922.3");
    

    cout <<"MC: "<<datainput->GetEntries()<<endl;
    cout <<"Lb0 mass cut: "<<dataoutput->GetEntries()<<endl;

when it ends, it prints me the initial number of entries, and the final one, but as i said, if I use a TBrowser to inspect the final tree, the number changes.

You are possibly missing in the end:
dataoutput->Write();
or you can also try:
f->Write();

thanks for the reply,

I didn’t include TTree::Write() because the code already writes it after doing the CopyTree, if I include Write() the tree is written twice in the file.

Even so, i tried to add the Write() and know, one of the written trees have the same entries as GetEntries(), but the other one has a few less, what is happening?? what is this TTree, and how i can avoid its writting

Search for “cycle” and/or “namecycle”, for example, in the TDirectoryFile class description.

Thanks!

I’ve found how to delete the wrong TTree and know the program works as it should, but i still don’t know why is this TTree created. previously I thought that, when you use CopyTree, it is also written in the file, but in fact, only is written with a part of the entries, which is kinda weird… do you know why and if it exists a better way to perform my program without deleting a TTree after each cut.


“ROOT User’s Guide” -> “Input/Output”