 ROOT   Reference Guide vo004_SortAndSelect.C File Reference

## Detailed Description  In this tutorial we learn how elements of an RVec can be easily sorted and selected.

using namespace ROOT::VecOps;
{
// Because RVec implements an iterator, the class is fully compatible with
// the sorting algorithms in the standard library.
RVec<double> v1{6., 4., 5.};
std::sort(v2.begin(), v2.end());
std::cout << "Sort vector " << v1 << ": " << v2 << std::endl;
// For convenience, ROOT implements helpers, e.g., to get a sorted copy of
// an RVec ...
auto v3 = Sort(v1);
std::cout << "Sort vector " << v1 << ": " << v3 << std::endl;
// ... or a reversed copy of an RVec.
auto v4 = Reverse(v1);
std::cout << "Reverse vector " << v1 << ": " << v4 << std::endl;
// Helpers are provided to get the indices that sort the vector and to
// select these indices from an RVec.
auto i = Argsort(v1);
std::cout << "Indices that sort the vector " << v1 << ": " << i << std::endl;
RVec<double> v5{9., 7., 8.};
auto v6 = Take(v5, i);
std::cout << "Sort vector " << v5 << " respective to the previously"
<< " determined indices: " << v6 << std::endl;
// Take can also be used to get the first or last elements of an RVec.
auto v7 = Take(v1, 2);
auto v8 = Take(v1, -2);
std::cout << "Take the two first and last elements of vector " << v1
<< ": " << v7 << ", " << v8 << std::endl;
// Because the helpers return a copy of the input, you can chain the operations
// conveniently.
auto v9 = Reverse(Take(Sort(v1), -2));
std::cout << "Sort the vector " << v1 << ", take the two last elements and "
<< "reverse the selection: " << v9 << std::endl;
}
RVec< T > Reverse(const RVec< T > &v)
Return copy of reversed vector.
Definition: RVec.hxx:1116
RVec< T > Take(const RVec< T > &v, const RVec< typename RVec< T >::size_type > &i)
Return elements of a vector at given indices.
Definition: RVec.hxx:1058
RVec< typename RVec< T >::size_type > Argsort(const RVec< T > &v)
Return an RVec of indices that sort the input RVec.
Definition: RVec.hxx:1038
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Definition: TMathBase.h:362
Sort vector { 6, 4, 5 }: { 4, 5, 6 }
Sort vector { 6, 4, 5 }: { 4, 5, 6 }
Reverse vector { 6, 4, 5 }: { 5, 4, 6 }
Indices that sort the vector { 6, 4, 5 }: { 1, 2, 0 }
Sort vector { 9, 7, 8 } respective to the previously determined indices: { 7, 8, 9 }
Take the two first and last elements of vector { 6, 4, 5 }: { 6, 4 }, { 4, 5 }
Sort the vector { 6, 4, 5 }, take the two last elements and reverse the selection: { 6, 5 }
Date
August 2018

Definition in file vo004_SortAndSelect.C.