NTupleSorter.cxx
Go to the documentation of this file.
1 
12 #include "NTupleSorter.h"
13 
14 #include "NTuple.h"
15 
16 #include <algorithm>
17 #include <functional>
18 #include <iterator>
19 
20 using std::distance;
21 using std::greater;
22 using std::sort;
23 using std::vector;
24 using std::lower_bound;
25 
26 namespace hippodraw {
27 
29 NTupleSorter ( NTuple * ntuple )
30  : m_column ( 0 ),
31  m_increasing ( true ),
32  m_ntuple ( ntuple )
33 {
34 }
35 
37 NTupleSorter ( const NTupleSorter & sorter )
38  : m_column ( sorter.m_column ),
39  m_increasing ( sorter.m_increasing ),
40  m_ntuple ( 0 )
41 {
42 }
43 
44 NTuple *
46 getNTuple () const
47 {
48  return m_ntuple;
49 }
50 
51 void
54 {
55  if ( column == m_column ) {
57  return;
58  }
59  m_column = column;
60  m_increasing = true;
61 }
62 
63 void
65 rowwiseCopy ( std::vector < std::vector < double > * > & row_copy )
66 {
67  row_copy.clear();
68  unsigned int rows = m_ntuple -> rows ();
69 
70  for ( unsigned int row = 0; row < rows; row++ ) {
71  const vector < double > & v = m_ntuple -> getRow ( row );
72  vector < double > * p = new vector < double > ( v );
73  row_copy.push_back ( p );
74  }
75 }
76 
77 bool
79 operator () ( std::vector < double > * x,
80  std::vector < double > * y ) const
81 {
82  double left = x -> operator[] ( m_column );
83  double right = y -> operator[] ( m_column );
84 
85  return m_increasing ? left < right : right < left;
86 }
87 
88 void
90 sort ( )
91 {
92  vector < vector < double > * > table;
93  rowwiseCopy ( table );
94 
95  std::sort ( table.begin(), table.end(), *this );
96 
97  m_ntuple -> clear ();
98 
99  vector < vector < double > * > :: iterator first = table.begin ();
100  while ( first != table.end () ) {
101  vector < double > * p = *first++;
102  m_ntuple -> addRow ( *p );
103  delete p;
104  }
106 }
107 
108 void
111 {
112  m_ntuple->clear();
113 }
114 
115 void
117 addRow ( const std::vector < double > & row )
118 {
119  double sort_value = row[m_column];
120 
121  const vector < double > & column = m_ntuple ->getColumn ( m_column );
122  vector < double > ::const_iterator first;
123 
124  if ( m_increasing ) {
125  first = lower_bound ( column.begin(), column.end(), sort_value );
126  }
127  else {
128  first = lower_bound ( column.begin(), column.end(),
129  sort_value, greater< double >() );
130  }
131  unsigned int index = distance ( column.begin(), first );
132 
133  m_ntuple->insertRow ( index, row );
134 }
135 
136 void
138 eraseRow ( unsigned int index )
139 {
140  m_ntuple -> eraseRow ( index );
141 }
142 
143 unsigned int
145 columns ( ) const
146 {
147  return m_ntuple->columns ();
148 }
149 
150 unsigned int
152 rows ( ) const
153 {
154  return m_ntuple->rows ();
155 }
156 
157 const vector < double > &
159 getRow ( unsigned int index ) const
160 {
161  return m_ntuple->getRow ( index );
162 }
163 
164 } // namespace hippodraw
165 
virtual unsigned int rows() const
Returns the number of rows.
Definition: NTuple.cxx:230
void setSorting(int column)
Sets the sorting column.
bool operator()(std::vector< double > *x, std::vector< double > *y) const
A predicate function to be used for comparing two rows.
void sort()
Sorts the NTuple.
virtual const std::vector< double > & getColumn(unsigned int index) const
Returns the data in the column with index column.
Definition: NTuple.cxx:493
virtual void insertRow(unsigned int index, const std::vector< double > &v)
Inserts a Row.
Definition: NTuple.cxx:280
unsigned int rows() const
Returns the number of rows of the NTuple.
NTupleSorter class interface.
unsigned int columns() const
Returns the number of columns or data arrays available from this DataSource.
Definition: DataSource.h:458
bool m_increasing
The direction of the sort.
Definition: NTupleSorter.h:47
Namespace for HippoDraw.
Definition: AxesType.cxx:21
hippodraw::NTuple class interface.
void clear()
Clears the NTuple.
virtual const std::vector< double > & getRow(unsigned int index) const
Returns a temporary vector of data elements in one row.
Definition: NTuple.cxx:327
int m_column
The column used for sorting, or -1 if sorting is disabled.
Definition: NTupleSorter.h:43
NTuple * getNTuple() const
Returns the NTuple being managed by the sorter.
void rowwiseCopy(std::vector< std::vector< double > * > &row_copy)
Makes a row-wise copy of the NTuple object.
A DataSource class implemented with std::vector<double> to store the column data. ...
Definition: NTuple.h:33
virtual void clear()
Clears the NTuple.
Definition: NTuple.cxx:211
virtual void eraseRow(unsigned int index)
Erases a row from the NTuple.
unsigned int columns() const
Returns the number of columns of the NTuple.
void addRow(const std::vector< double > &row)
Adds a row to the NTuple.
virtual void notifyObservers() const
Notifies Observer objects of a change.
Definition: NTuple.cxx:574
NTupleSorter(const NTupleSorter &)
The copy constructor.
A helper class to sort and keep sorted an NTuple.
Definition: NTupleSorter.h:32
const std::vector< double > & getRow(unsigned int index) const
Returns a reference to the index row of the sorted NTuple.
NTuple * m_ntuple
The NTuple object that will be used.
Definition: NTupleSorter.h:51
column
The column indices for 2 dimension data point tuple.

Generated for HippoDraw Class Library by doxygen