Getting sorted index from a branch

I have a tree with few branches. One of those branches is named “index” and it has Float_t values.

I have seen few examples on how to Sort a Tree, but I am having difficulties.

Here is a sample a code I use.

tree->BuildIndex( "index" );
TTreeIndex *indexedTree = (TTreeIndex *) tree->GetTreeIndex();
for( int n = 0; n < 20; n++ )
{
    tree->GetEntry( indexedTree->GetIndex()[n] );
    cout << "Entry : " << n << " IndexedEntry : " << indexedTree->GetIndex()[n] << " Index : " << index << endl;
}

And this is the output I get, not sorted, but some weird ordering has happened.

Entry : 0 IndexedEntry : 225 Index : 0.804401
Entry : 1 IndexedEntry : 104 Index : 0.313073
Entry : 2 IndexedEntry : 147 Index : 0.412398
Entry : 3 IndexedEntry : 137 Index : 0.857905
Entry : 4 IndexedEntry : 148 Index : 0.307246
Entry : 5 IndexedEntry : 106 Index : 0.478615
Entry : 6 IndexedEntry : 107 Index : 0.355452
Entry : 7 IndexedEntry : 220 Index : 0.0662168
Entry : 8 IndexedEntry : 221 Index : 0.659784
Entry : 9 IndexedEntry : 223 Index : 0.418755
Entry : 10 IndexedEntry : 181 Index : 0.918294
Entry : 11 IndexedEntry : 224 Index : 0.915381
Entry : 12 IndexedEntry : 183 Index : 0.355452
Entry : 13 IndexedEntry : 226 Index : 0.436766

How I could use directly TMath::Sort function to get index branch indexed in growing or descending order?

Thanks!

It seems to work with the following version

Int_t nentries = (Int_t)tree->GetEntries();
tree->Draw("index","","goff");
Int_t *indexed = new Int_t[nentries];
TMath::Sort(nentries,tree->GetV1(),indexed);
for( int n = 0; n < 20; n++ )
{
    tree->GetEntry( indexed[n] );
    cout << "Entry : " << n << " IndexedEntry : " << indexed[n] << " Index : " << index << endl;
}

getting as result

Entry : 0 IndexedEntry : 189 Index : 162.164
Entry : 1 IndexedEntry : 29 Index : 137.115
Entry : 2 IndexedEntry : 188 Index : 93.7095
Entry : 3 IndexedEntry : 50 Index : 87.2856
Entry : 4 IndexedEntry : 16 Index : 83.3402
Entry : 5 IndexedEntry : 98 Index : 80.7055
Entry : 6 IndexedEntry : 85 Index : 62.9181
Entry : 7 IndexedEntry : 44 Index : 62.4104
Entry : 8 IndexedEntry : 175 Index : 59.291
Entry : 9 IndexedEntry : 7 Index : 52.8879
Entry : 10 IndexedEntry : 176 Index : 51.8634
Entry : 11 IndexedEntry : 126 Index : 45.661
Entry : 12 IndexedEntry : 61 Index : 44.1155
Entry : 13 IndexedEntry : 8 Index : 34.9622
Entry : 14 IndexedEntry : 31 Index : 34.3106
Entry : 15 IndexedEntry : 262 Index : 33.5661
Entry : 16 IndexedEntry : 89 Index : 33.5121
Entry : 17 IndexedEntry : 227 Index : 32.7209
Entry : 18 IndexedEntry : 48 Index : 31.658
Entry : 19 IndexedEntry : 62 Index : 30.3612

but still do not understand why the first option does not work properly …

See https://root.cern.ch/doc/v608/classTTreeIndex.html#a08aac749ab22fd5c8ab792a0061a4b0f which states

Build an index table using the leaves of Tree T with major & minor names The index is built with the expressions given in "majorname" and "minorname".

a Long64_t array fIndexValues is built with:

    major = the value of majorname converted to an integer
    minor = the value of minorname converted to an integer
    fIndexValues[i] = major<<31 + minor

I.e. in your case all the values used for the index are the same (i.e. 0).

To work-around this, you would need to use, for example for 3 significant digits:

tree->BuildIndex( "index * 1000" );

Cheers,
Philippe.

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