_ROOT Version: 6.30
_Platform: Windows 11 Compiler: Not Provided
__
Hello,
I have a TCut variable (TCut cut_all) defined in my script and the script is automatically loaded when starting root session using .rootlogon.C, making my cut_all to be global now
What I want to do is to use my TCut cut_all when reading a tree using loop “for”. I have found a solution involving TTreeFormula:
//my definitions
TTreeFormula* formula = new TTreeFormula("formula", cut_all.GetTitle(), tree);
Long64_t nEntries = tree->GetEntries();
for (Long64_t entryNum = 0; entryNum < nEntries; ++entryNum) {
tree->GetEntry(entryNum);
if (formula->EvalInstance() != 0) {
hist->Fill(mf0[1]);
}
}
//returning the histogram
The problem is that my histogram is EMPTY after the loop: formula->EvalInstance() equals to 0 for all entries.
Why is this happening
And generally, how to use globally defined TCut variable when reading a tree using loop “for”
This is very important for me, because I often interactively work with trees using tree->Draw(“mf0[1]”, cut_all), but sometimes I need to control every entry, but I don’t want to manually type out the whole cut in if(…) condition, because my TCut variable is very lengthy.
Are there any other solutions available? I greatly appreciate any assistance provided!
What you wrote ‘should’ have worked unless tree is a TChain (in which case you need to add more code to handle the transition from one tree to the other) or if cut_all contains references to collections (in which case you need to not just look at one value for loop through the multiple values for each entries of the tree)
Note in your example the loop needs to be the same:
// Calling this for a TChain is very ineffecient, it request to open and then immediately
// close all the file.
// Long64_t nEntries = tree->GetEntries();
Long_t treenumber = tree->GetTreeNumber();
for (Long64_t entryNum = 0; entryNum < tree->GetEntriesFast(); ++entryNum) {
if ( tree->LoadEntry(entryNum) < 0 ) break;
if (tree->GetTreeNumber() != treenumber) {
formula->UpdateFormulaLeaves();
treenumber = tree->GetTreeNumber();
}
tree->GetEntry(entryNum);
if (formula->EvalInstance(0) != 0) {
hist->Fill(mf0[1]);
}
}