npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
InMemoryNtuple.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_INMEMORYNTUPLE_HH_
2 #define NPSTAT_INMEMORYNTUPLE_HH_
3 
4 /*!
5 // \file InMemoryNtuple.hh
6 //
7 // \brief Homogeneous ntuple which must fit in computer memory
8 //
9 // Fast access to any ntuple row.
10 //
11 // Author: I. Volobouev
12 //
13 // November 2010
14 */
15 
16 #include <stdexcept>
17 #include "npstat/stat/AbsNtuple.hh"
18 
19 namespace npstat {
20  /**
21  // Homogeneous ntuple which must fit in memory (so its size is limited).
22  // See ArchivedNtuple class if you need to create really large ntuples.
23  */
24  template <typename T>
25  class InMemoryNtuple : public AbsNtuple<T>
26  {
27  public:
28  /**
29  // Constructor arguments are as follows:
30  //
31  // colNames -- naturally, the names of the ntuple columns
32  //
33  // ntTitle -- some title for the ntuple (arbitrary string)
34  */
35  inline explicit InMemoryNtuple(const std::vector<std::string>& colNames,
36  const char* ntTitle = 0)
37  : AbsNtuple<T>(colNames, ntTitle), ncols_(colNames.size()) {}
38 
39  inline virtual ~InMemoryNtuple() {}
40 
41  /**
42  // Number of rows. If the ntuple is always filled one row
43  // at a time, this is also the number of fills.
44  */
45  inline unsigned long nRows() const {return data_.size()/ncols_;}
46 
47  /**
48  // Add data to the ntuple. Will throw std::invalid_argument
49  // in case lenValues is not divisible by the number of columns.
50  */
51  void fill(const T* values, unsigned long lenValues);
52 
53  //@{
54  /**
55  // Convenience method which works if the number of arguments equals
56  // the number if colums (otherwise an exception will be thrown)
57  */
58  void fill(const T& v0);
59  void fill(const T& v0, const T& v1);
60  void fill(const T& v0, const T& v1, const T& v2);
61  void fill(const T& v0, const T& v1, const T& v2, const T& v3);
62  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
63  const T& v4);
64  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
65  const T& v4, const T& v5);
66  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
67  const T& v4, const T& v5, const T& v6);
68  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
69  const T& v4, const T& v5, const T& v6, const T& v7);
70  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
71  const T& v4, const T& v5, const T& v6, const T& v7,
72  const T& v8);
73  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
74  const T& v4, const T& v5, const T& v6, const T& v7,
75  const T& v8, const T& v9);
76  //@}
77 
78  /** Access individual elements without bounds checking */
79  inline T operator()(const unsigned long r, const unsigned long c) const
80  {return data_[r*ncols_ + c];}
81 
82  /** Access individual elements with bounds checking */
83  inline T at(const unsigned long r, const unsigned long c) const
84  {
85  if (c >= ncols_)
86  throw std::out_of_range("In npstat::InMemoryNtuple::at: "
87  "column number is out of range");
88  return data_.at(r*ncols_ + c);
89  }
90 
91  /** Clear all ntuple contents */
92  inline void clear() {data_.clear();}
93 
94  /**
95  // Access one row at a time. The provided buffer should be
96  // sufficiently large to contain the complete row.
97  */
98  void rowContents(unsigned long row,
99  T* buf, unsigned long lenBuf) const;
100 
101  /**
102  // Access one column at a time. The provided buffer should be
103  // sufficiently large to contain the complete column.
104  */
105  void columnContents(const Column& c,
106  T* buf, unsigned long lenBuf) const;
107 
108  //@{
109  /** Method needed for "geners" I/O */
110  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
111  bool write(std::ostream&) const;
112  //@}
113 
114  static const char* classname();
115  static inline unsigned version() {return 1;}
116  static InMemoryNtuple* read(const gs::ClassId& id, std::istream& in);
117 
118  protected:
119  inline virtual bool isEqual(const AbsNtuple<T>& other) const
120  {
121  return this->columnNames() == other.columnNames() &&
122  this->title() == other.title() &&
123  data_ == static_cast<const InMemoryNtuple&>(other).data_;
124  }
125 
126  private:
127  std::vector<T> data_;
128  unsigned long ncols_;
129  };
130 }
131 
132 #include "npstat/stat/InMemoryNtuple.icc"
133 
134 #endif // NPSTAT_INMEMORYNTUPLE_HH_
Interface definition for homogeneous ntuples (point clouds)
Definition: AbsNtuple.hh:39
const std::vector< std::string > & columnNames() const
Definition: AbsNtuple.hh:77
const std::string & title() const
Definition: AbsNtuple.hh:62
Definition: Column.hh:30
Definition: InMemoryNtuple.hh:26
void clear()
Definition: InMemoryNtuple.hh:92
virtual gs::ClassId classId() const
Definition: InMemoryNtuple.hh:110
T operator()(const unsigned long r, const unsigned long c) const
Definition: InMemoryNtuple.hh:79
unsigned long nRows() const
Definition: InMemoryNtuple.hh:45
void fill(const T *values, unsigned long lenValues)
void columnContents(const Column &c, T *buf, unsigned long lenBuf) const
void rowContents(unsigned long row, T *buf, unsigned long lenBuf) const
void fill(const T &v0)
virtual bool isEqual(const AbsNtuple< T > &other) const
Definition: InMemoryNtuple.hh:119
T at(const unsigned long r, const unsigned long c) const
Definition: InMemoryNtuple.hh:83
InMemoryNtuple(const std::vector< std::string > &colNames, const char *ntTitle=0)
Definition: InMemoryNtuple.hh:35
Definition: AbsArrayProjector.hh:14