npstat is hosted by Hepforge, IPPP Durham
NPStat  5.10.0
ArchivedNtuple.hh
Go to the documentation of this file.
1 #ifndef NPSTAT_ARCHIVEDNTUPLE_HH_
2 #define NPSTAT_ARCHIVEDNTUPLE_HH_
3 
4 /*!
5 // \file ArchivedNtuple.hh
6 //
7 // \brief Large, archive-based homogeneous ntuple
8 //
9 // Author: I. Volobouev
10 //
11 // November 2010
12 */
13 
14 #include "npstat/stat/AbsNtuple.hh"
15 #include "npstat/stat/NtupleBuffer.hh"
16 #include "npstat/stat/NtupleRecordTypesFwd.hh"
17 
18 #include "geners/AbsArchive.hh"
19 
20 namespace npstat {
21  template <typename T>
22  class NtupleReference;
23 
24  /**
25  // Ntuple which does not have to fit in memory, all rows at the same time.
26  // It can grow very large -- the size is limited only by the capacity
27  // of the underlying archive which usually means by the size of available
28  // disk space.
29  */
30  template <typename T>
31  class ArchivedNtuple : public AbsNtuple<T>
32  {
33  public:
34  typedef T value_type;
35 
36  /**
37  // Constructor arguments are as follows:
38  //
39  // columnNames -- naturally, the names of the ntuple columns
40  //
41  // title -- some title for the ntuple (arbitrary string)
42  //
43  // archive -- Archive in which the ntuple data will be stored.
44  // This archive must exist while the ntuple is
45  // in use.
46  //
47  // name -- ntuple name label in the archive
48  //
49  // category -- ntuple category labels in the archive
50  //
51  // rowsPerBuffer -- Number of rows to keep in memory simultaneously.
52  // This number essentially defines how large are
53  // going to be the chunks of data which are written
54  // to the archive or read back every single time
55  // the underlying code talks to the archive. The
56  // optimal buffer size will depend on the data
57  // access pattern. For example, if rows will be
58  // accessed in random order, it makes little sense
59  // to have large buffers. On the other hand,
60  // a large buffer will minimize the number of times
61  // we have to go to the archive in case the rows
62  // are accessed sequentially.
63  //
64  // writeColumnWise -- If this argument is "true", the data in the
65  // buffers will be written column after column.
66  // This mode is useful (results in faster data
67  // access) in case the ntuple data will be used
68  // predominatly via the "columnContents" method.
69  */
70  ArchivedNtuple(const std::vector<std::string>& columnNames,
71  const char* title, gs::AbsArchive& archive,
72  const char* name, const char* category,
73  unsigned long rowsPerBuffer,
74  bool writeColumnWise=false);
75 
76  inline virtual ~ArchivedNtuple() {write();}
77 
78  //@{
79  /** Basic inspector of ntuple properties */
80  inline gs::AbsArchive& archive() const {return ar_;}
81  inline const std::string& name() const {return name_;}
82  inline const std::string& category() const {return category_;}
83  inline unsigned long rowsPerBuffer() const
84  {return fillBuffer_.maxrows();}
85  inline bool writesByColumn() const
86  {return fillBuffer_.writeByColumn();}
87  inline bool isReadable() const {return readable_;}
88  inline bool isWritable() const {return writable_;}
89  //@}
90 
91  /**
92  // Each object of this type created in one particular program run
93  // will have its own unique number. This number in not persistent.
94  */
95  inline unsigned long objectNumber() const {return objectNumber_;}
96 
97  /**
98  // Add data to the ntuple. Will throw std::runtime_error in case
99  // the underlying archive is not writable. lenValues must be
100  // divisible by the number of columns.
101  */
102  void fill(const T* values, unsigned long lenValues);
103 
104  //@{
105  /**
106  // Convenience method which works if the number of arguments equals
107  // the number if colums (otherwise an exception will be thrown)
108  */
109  void fill(const T& v0);
110  void fill(const T& v0, const T& v1);
111  void fill(const T& v0, const T& v1, const T& v2);
112  void fill(const T& v0, const T& v1, const T& v2, const T& v3);
113  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
114  const T& v4);
115  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
116  const T& v4, const T& v5);
117  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
118  const T& v4, const T& v5, const T& v6);
119  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
120  const T& v4, const T& v5, const T& v6, const T& v7);
121  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
122  const T& v4, const T& v5, const T& v6, const T& v7,
123  const T& v8);
124  void fill(const T& v0, const T& v1, const T& v2, const T& v3,
125  const T& v4, const T& v5, const T& v6, const T& v7,
126  const T& v8, const T& v9);
127  //@}
128 
129  /**
130  // Number of rows. If the ntuple is always filled one row
131  // at a time, this is also the number of fills.
132  */
133  inline unsigned long nRows() const {return fillBuffer_.nRows();}
134 
135  /** Access individual elements without bounds checking */
136  inline T operator()(const unsigned long row,
137  const unsigned long column) const;
138 
139  /** Access individual elements with bounds checking */
140  inline T at(const unsigned long row,
141  const unsigned long column) const;
142 
143  /**
144  // Access one row at a time. The provided buffer should be
145  // sufficiently large to contain the complete row.
146  */
147  void rowContents(const unsigned long row,
148  T* buffer, unsigned long lenBuffer) const;
149 
150  /**
151  // Access one column at a time. The provided buffer should be
152  // sufficiently large to contain the complete column.
153  */
154  void columnContents(const Column& c,
155  T* buffer, unsigned long lenBuffer) const;
156 
157  /**
158  // Can't really clear the data, it is in the archive already.
159  // So this is just a NOOP.
160  */
161  virtual void clear() {}
162 
163  //@{
164  /** Method needed for "geners" I/O */
165  virtual bool write();
166  virtual gs::ClassId classId() const {return gs::ClassId(*this);}
167  //@}
168 
169  static const char* classname();
170  static inline unsigned version() {return 1;}
171 
172  protected:
173  virtual bool isEqual(const AbsNtuple<T>& r) const;
174 
175  private:
176  typedef ArchivedNtuple<T> MyNtuple;
177 
183  friend class NtupleReference<MyNtuple>;
184 
185  ArchivedNtuple();
187  ArchivedNtuple& operator=(const ArchivedNtuple&);
188 
189  // The following function is used by NtupleReference
190  static ArchivedNtuple* read(gs::AbsArchive& ar,
191  std::istream& is,
192  unsigned long long headId);
193 
194  static unsigned long nextObjectNumber();
195 
196  bool loadRowData(unsigned long rowNumber) const;
197  bool loadColumnSection(unsigned long firstRow, unsigned long col,
198  T* buf, unsigned long lenBuf) const;
199  void saveHeader();
200  void saveFillBuffer();
201 
202  gs::AbsArchive& ar_;
203 
204  std::string name_;
205  std::string category_;
206 
207  NtupleBuffer<T> fillBuffer_;
208  mutable NtupleBuffer<T> readBuffer_;
209  gs::ClassId bufferClass_;
210 
211  std::vector<unsigned long long> idlist_;
212  std::vector<long long> columnOffsets_;
213  unsigned long long headerSaved_;
214  unsigned long ncols_;
215  const unsigned long objectNumber_;
216  bool readable_;
217  bool writable_;
218  };
219 }
220 
221 #include "npstat/stat/ArchivedNtuple.icc"
222 
223 #endif // NPSTAT_ARCHIVEDNTUPLE_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: ArchivedNtuple.hh:32
void columnContents(const Column &c, T *buffer, unsigned long lenBuffer) const
virtual bool write()
virtual void clear()
Definition: ArchivedNtuple.hh:161
ArchivedNtuple(const std::vector< std::string > &columnNames, const char *title, gs::AbsArchive &archive, const char *name, const char *category, unsigned long rowsPerBuffer, bool writeColumnWise=false)
void fill(const T &v0)
virtual gs::ClassId classId() const
Definition: ArchivedNtuple.hh:166
virtual bool isEqual(const AbsNtuple< T > &r) const
T operator()(const unsigned long row, const unsigned long column) const
unsigned long nRows() const
Definition: ArchivedNtuple.hh:133
void rowContents(const unsigned long row, T *buffer, unsigned long lenBuffer) const
T at(const unsigned long row, const unsigned long column) const
void fill(const T *values, unsigned long lenValues)
unsigned long objectNumber() const
Definition: ArchivedNtuple.hh:95
gs::AbsArchive & archive() const
Definition: ArchivedNtuple.hh:80
Definition: Column.hh:30
Definition: NtupleBuffer.hh:25
Definition: NtupleReference.hh:28
Definition: NtupleRecordTypes.hh:88
Definition: NtupleRecordTypes.hh:108
Definition: NtupleRecordTypes.hh:136
Definition: NtupleRecordTypes.hh:57
Definition: NtupleRecordTypes.hh:27
Definition: AbsArrayProjector.hh:14