I have a list of names whose number and content depend on input file, and want to add TBranches with these names to a TTree, fill the tree in a loop, and write it the output file.
How can I fill the address parameter when adding the branches?
I tried to declare double array or array of pointers got with malloc to hold the addresses, but none of them work. It causes segmentation violation to build an array with vectors inside, so I cannot use the vectors.
I have a root file with a TTree called Events inside, on which are some variables of different names including nElectron and Electron_*.
I want to build a TTree called Selected that saved all the variables named Electron_* in Events, but only the entries where nElectron are not zero.
How should I do that? Given that the variables with a name begin with "Electron_" differs between input files, I cannot define each of them manually (as below).
inputtree->SetBranchStatus("*", false);
inputtree->SetBranchStatus("nElectron", true);
inputtree->SetBranchStatus("Electron_*", true);
int nElectron;
intputtree->SetBranchAddress("nElectron", &nElectron);
// No need to set the other address unless there are used for
// the selection.
TTree *outputtree = intputtree->CloneTree(0);
outputtree->SetDirectory( outputfile );
for(long long e = 0; e < inputtree->GetEntriesFast(); ++e) {
inputtree->GetEntry(e);
if (nElectron)
outputtree->Fill();
}
Alternatively you can also use RDataFrame’s Snapshot feature.
Cheers,
Philippe.