I have a TTree (Ntuple) with only about 20 branches, but 10^9 entries (each branch has equal entries). The run number (integer) is among these 20 branches, and we know that there are only several hundreds of runs. So, is there an easy way to get the list of runs, or to get all values of a TTree branch (yes, here it the run number)?
The easiest and fast way I know is that
get the maximum and minimum values of the run number
book an histogram with total bins of (maximum value - minimum vlue) and corresponding range, etc.
project “run Number” to this histogram
loop all the bins of this histogram and keep the GetBinCenter of the bin with non-zero entries.
But I think there must be a more smarter way to do so.
TH1 *hrun = (TH1*)gPad->GetPrimitive("hrun");
then procedd like in your step 4
The “problem” with this is if you have a large run range that is sparcely populated. You can get around this problem by specifying the binning:
At some point, however, your run range may be such that the number of bins becomes somewhat silly. In this case, you can also use the Draw to do the loop for you and grab the results using TTree::GetSelectedRows() and TTree::GetV1() and using the set like I proposed before.
Just want to let you know that this approach works very well. It also only takes several minutes to get the run number sets.
Of course, I would not only use vector. I tried something like:
[code] // first see if the value is in cache
map<int,size_t>::const_iterator foundCache = runsCache.find(runNumber);
if (foundCache == runsCache.end()) // not found
vector::const_iterator found = find(runs.begin(), runs.end(), runNumber);
if(found!=runs.end()) // found
// cache the value with its position
runsCache[runNumber] = found - runs.begin();
} else //not found
I guess it is not as efficient as std::set. Also, I put it in a MakeSelector->Process block, maybe this also causes inefficiency?
With your attempt using an std::vector, you are trying to implement std::set by hand. I can easily believe that since std::set has been optimized to do this, it will be much faster than trying to use the wrong tool for the job.
There is an expression “When you have a hammer, everything looks like a nail.” Well, now you have a wrench in addition to your hammer.