Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
vo004_SortAndSelect.C File Reference

Detailed Description

View in nbviewer Open in SWAN In this tutorial we learn how elements of an RVec can be easily sorted and selected.

{
// Because RVec implements an iterator, the class is fully compatible with
// the sorting algorithms in the standard library.
ROOT::RVecD 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;
ROOT::RVecD 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:2326
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:2242
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Definition TMathBase.h:358
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
Author
Stefan Wunsch

Definition in file vo004_SortAndSelect.C.