Home | News | Documentation | Download

Sort all TTreeReaderArray

Hi,

following the example of TTreeReader found here, suppose I have the following:

TTreeReaderArray<double> jetPt(reader, "jetpT");
TTreeReaderArray<double> jetEta(reader, "jetEta");
TTreeReaderArray<double> jetPhi(reader, "jetPhi");
// and so on...

Now I want to sort them all as the elements of the jetPt decrease. Clearly, I cannot do it with

int n = sizeof(jetPt)/sizeof(jetPt[0]);
sort(jetPt, jetPt+n, std::greater<double>());

, because it will sort only the jetPt array, and I won’t be able to find which index of sorted jetPt corresponds to index of the unsorted jetPhi. Is there any way to do what I want?

Thanks!

ROOT Version: 6.14/04


Because TTreeReaderArray<double> is not really an array of double (in the C++ sense), this is not the number of element. You meant;

size_t n = jetPt.GetSize();

To do the sort you can use a collection of indices, for example.

size_t n = jetPt.GetSize();
std::vector<long> index;
for(size_t i = 0; i < n; ++i) index.push_back(i);
sort(index.begin(), index.end(), [&jetPt](long a, long b) -> bool
{
    return jetPt[a] > jetPt[b];
})

[corrected the lambda capture syntax]
then you can use index to access the elements of all 3 arrays.

Hi Philippe,

I take it in your example you meant
[&jetPt] and not [jetPt&] in the third paramater of sort(...), right?

Yes, you are right, this was a typo.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.