Hello,
My goal is to make a skimming of a TChain and deal with it afterwards, without having to store in a root file the skimmed obtained result.
Apparently, the only way is to skim the TChain is to clone it into a TTree.
The problem is that I really need a TChain for my program, so I need to transform the TTree in a TChain.
And apparently, adding the TTree to the TChain is not working, so that the information is lost.
Here is a minimum program illustrating that.
Would there a be an idea ?
Thank you
int ProblemTChain()
{
TChain *chain=new TChain("tree_sel_HH_Resonant_mX260");
chain->Add("/sps/atlas/e/escalier/ATLAS_HGam/Outputs_trees_selection_HH_baseline/tree_h025_mc16d_PowhegH7_HHbbyy_cHHH01d0_nominal.root");
cout << "phase 1, chain->GetEntries()=" << chain->GetEntries() << endl;
//-----------------------------------------------------------------
TTree *tree_restriction=chain->GetTree()->CloneTree(0);
unsigned long long eventNumber;
TBranch *b_eventNumber;
chain->SetBranchAddress("eventNumber", &eventNumber, &b_eventNumber);
chain->SetBranchStatus("*", 1);
// https://root-forum.cern.ch/t/skim-events-from-a-tchain-into-a-new-file/9353/2
//next line
chain->LoadTree(0);
int nb_entries=chain->GetEntries();
cout << "nb_entries=" << nb_entries << endl;
for (int index_entry=0;index_entry<nb_entries;index_entry++) {
if (index_entry%2==1) //example of a restriction
continue;
// cout << "index_entry=" << index_entry << " / " << nb_entries << endl;
Long64_t centry=chain->LoadTree(index_entry); //mandatory to move from index_entry to centry with LoadTree, due to presence of several chains, else problems
if (centry<0)
continue;
chain->GetEntry(index_entry); //index_entry=absolute : mandatory : != branch
tree_restriction->Fill();
} //end loop on entries
cout << "phase 2, tree_restriction->GetEntries()=" << tree_restriction->GetEntries() << endl;
//-----------------------------------------------------------------
chain->AddClone(tree_restriction);
//remark : the trick to cast the TTree to a TChain, and then to do : chain->Add(TheCastedTreeIntoATChain) is crashing, because the TChain and TTree are not same format
cout << "phase 3, chain->GetEntries() after having added the tree by chain->AddClone(tree_restriction)=" << chain->GetEntries() << endl;
return 0;
--> this gives :
phase 1, chain->GetEntries()=358
nb_entries=358
phase 2, tree_restriction->GetEntries()=179
phase 3, chain->GetEntries() after having added the tree by chain->AddClone(tree_restriction)=358
-->the last number is 358, and not 358+179
ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided