source: Sophya/trunk/SophyaLib/HiStats/basedtable.h@ 2849

Last change on this file since 2849 was 2849, checked in by ansari, 20 years ago

Ajout de la classe DataTableRow , modifs/ajout interface BaseDataTable - Reza 21/11/2005

File size: 13.8 KB
RevLine 
[2688]1// This may look like C code, but it is really -*- C++ -*-
2// Class BaseDataTable
3// R. Ansari - Avril 2005
4// (C) LAL-IN2P3/CNRS CEA-DAPNIA
5
6#ifndef BASEDTABLE_H_SEEN
7#define BASEDTABLE_H_SEEN
8
9#include "machdefs.h"
10
11#include <iostream>
12#include <string>
13#include <vector>
[2849]14#include <map>
[2688]15
[2827]16#include <complex>
17
[2688]18#include "ntupintf.h"
19#include "dvlist.h"
20#include "segdatablock.h"
[2831]21#include "tvector.h"
[2688]22
23namespace SOPHYA {
[2822]24
25// Forward class declaration for Fits handler
26template <class T> class FitsHandler;
27
[2849]28//! Class for representing and manipulating DataTable rows.
29class DataTableRow {
30public:
31//! Constructor - For use by BaseDataTable class
32 DataTableRow( vector<string>& colnames );
33//! Copy constructor
34 DataTableRow( DataTableRow const & a );
35//! Returns the value for column \b k. No bound checking performed
36 inline MuTyV operator[] (sa_size_t k) const { return mtv_[k]; }
37//! Returns a reference to the value of column \b k. No bound checking performed
38 inline MuTyV& operator[] (sa_size_t k) { return mtv_[k]; }
39//! Returns the value for column \b colname.
40 inline MuTyV operator[] (string const& colname) const { return get(colname); }
41//! Returns a reference to the value of column \b k.
42 inline MuTyV& operator[] (string const& colname) { return get(colname); }
43//! Returns the value for column \b colname.
44 inline MuTyV operator[] (const char* colname) const
45 { string sc(colname); return get(sc); }
46//! Returns a reference to the value of column \b k.
47 inline MuTyV& operator[] (const char* colname)
48 { string sc(colname); return get(sc); }
49//! Returns the value for column \b colname.
50 MuTyV get(string const& colname) const;
51//! Returns a reference to the value of column \b k.
52 MuTyV& get(string const& colname);
53//! Acces to the MuTyV pointer
54 inline sa_size_t Size() const { return size_; }
55//! Acces to the MuTyV pointer
56 inline MuTyV const * MTVPtr() const { return mtv_; }
57//! Acces to the MuTyV pointer
58 inline MuTyV* MTVPtr() { return mtv_; }
59//! prints the object content on the output stream
60 ostream& Print(ostream& os) const;
61protected:
62 MuTyV * mtv_;
63 sa_size_t size_;
64 map<string , sa_size_t> nm2idx_;
65};
66
67/*! Prints the DataTableRow object content on the output stream ( */
68inline ostream& operator << (ostream& s, DataTableRow const & row)
69 { row.Print(s); return(s); }
70
[2808]71//! Interface definition for classes handling data in a table.
[2688]72class BaseDataTable : public AnyDataObj , public NTupleInterface {
73public:
74 // ===> DO NOT CHANGE EXISTING enum type VALUES !!!
75 enum FieldType {IntegerField=1, LongField=2,
76 FloatField=3, DoubleField=4,
77 ComplexField=5, DoubleComplexField=6,
[2831]78 StringField=7, DateTimeField=8};
[2688]79 // <=====================================================>
80
[2831]81 //! Return the column type as a string (long or short depending on fgl=true/false)
[2688]82 static string ColTypeToString(FieldType ft, bool fgl=false);
[2831]83 //! Return the column type corresponding to \b sctyp
84 static FieldType StringToColType(string const & sctyp);
[2688]85
86 // constructeur , destructeur
87 BaseDataTable(sa_size_t segsz=512);
88 virtual ~BaseDataTable();
89
[2808]90 //! Adds a column holding integer, named \b cnom
[2688]91 inline sa_size_t AddIntegerColumn(const char * cnom)
92 { return AddColumn(IntegerField, cnom); }
[2808]93 //! Adds a column holding integer, named \b cnom
[2688]94 inline sa_size_t AddIntegerColumn(string const & cnom)
95 { return AddColumn(IntegerField, cnom); }
[2808]96 //! Adds a column holding long integer, named \b cnom
[2732]97 inline sa_size_t AddLongColumn(const char * cnom)
98 { return AddColumn(LongField, cnom); }
[2808]99 //! Adds a column holding long integer, named \b cnom
[2732]100 inline sa_size_t AddLongColumn(string const & cnom)
101 { return AddColumn(LongField, cnom); }
[2808]102 //! Adds a column holding floating values (r_4), named \b cnom
[2688]103 inline sa_size_t AddFloatColumn(const char * cnom)
104 { return AddColumn(FloatField, cnom); }
[2808]105 //! Adds a column holding floating values (r_4), named \b cnom
[2688]106 inline sa_size_t AddFloatColumn(string const & cnom)
107 { return AddColumn(FloatField, cnom); }
[2808]108 //! Adds a column holding double values (r_8), named \b cnom
[2688]109 inline sa_size_t AddDoubleColumn(const char * cnom)
110 { return AddColumn(DoubleField, cnom); }
[2808]111 //! Adds a column holding double values (r_8), named \b cnom
[2688]112 inline sa_size_t AddDoubleColumn(string const & cnom)
113 { return AddColumn(DoubleField, cnom); }
[2827]114 //! Adds a column holding single precision complex values (complex<r_4>), named \b cnom
115 inline sa_size_t AddComplexColumn(const char * cnom)
116 { return AddColumn(ComplexField, cnom); }
117 //! Adds a column holding single precision complex values (complex<r_4>), named \b cnom
118 inline sa_size_t AddComplexColumn(string const & cnom)
119 { return AddColumn(ComplexField, cnom); }
120 //! Adds a column holding double precision complex values (complex<r_8>), named \b cnom
121 inline sa_size_t AddDoubleComplexColumn(const char * cnom)
122 { return AddColumn(DoubleComplexField, cnom); }
123 //! Adds a column holding double precision complex values (complex<r_8>), named \b cnom
124 inline sa_size_t AddDoubleComplexColumn(string const & cnom)
125 { return AddColumn(DoubleComplexField, cnom); }
[2808]126 //! Adds a column holding character strings, named \b cnom
[2732]127 inline sa_size_t AddStringColumn(const char * cnom)
128 { return AddColumn(StringField, cnom); }
[2808]129 //! Adds a column holding character strings, named \b cnom
[2732]130 inline sa_size_t AddStringColumn(string const & cnom)
131 { return AddColumn(StringField, cnom); }
[2831]132 //! Adds a column holding Date/Time value, named \b cnom
133 inline sa_size_t AddDateTimeColumn(const char * cnom)
134 { return AddColumn(DateTimeField, cnom); }
135 //! Adds a column holding Date/Time value, named \b cnom
136 inline sa_size_t AddDateTimeColumn(string const & cnom)
137 { return AddColumn(DateTimeField, cnom); }
[2688]138
139 inline sa_size_t AddColumn(FieldType ft, const char * cnom)
140 { string nom = cnom; return AddColumn(ft, nom); }
141
142 //! Pure virtual method for adding a new column to the Data Table
143 virtual sa_size_t AddColumn(FieldType ft, string const & cnom) = 0;
144 //! Duplicate the data table structure from the source Table
145 virtual sa_size_t CopyStructure(BaseDataTable const & a);
146 //! Checks if the two table have the same column structure
147 virtual bool CompareStructure(BaseDataTable const & a);
148
149 // Verifie la validite du nom de colonne:envoie une exception
150 // si nom en double ou non valide
151 virtual bool CheckColName(string const & cnom);
152
153 // Acces to various counts and parameters
[2808]154 //! Return the number of lines (rows) in the table)
[2849]155 inline sa_size_t NRows() const { return mNEnt ; }
156 //! Return the number of lines (rows) in the table)
[2688]157 inline sa_size_t NEntry() const { return mNEnt ; }
[2808]158 //! Return the number of columns in the tables (number of cells in a row)
[2849]159 inline sa_size_t NCols() const { return mNames.size() ; }
160 //! Return the number of columns in the tables (number of cells in a row)
[2688]161 inline sa_size_t NVar() const { return mNames.size() ; }
[2808]162 //! Return the number of columns in the tables (number of cells in a row)
[2688]163 inline sa_size_t NVars() const { return mNames.size() ; }
[2808]164 //! Return the segment size (SegDBInterface objects corresponding to columns)
[2688]165 inline sa_size_t SegmentSize() const { return mSegSz ; }
[2808]166 //! Return the number of segments (SegDBInterface objects corresponding to columns)
[2688]167 inline sa_size_t NbSegments() const { return mNSeg ; }
168
[2849]169 //! Return a compatible DataTableRow object (with table column names)
170 virtual DataTableRow EmptyRow();
[2808]171
[2849]172 // Filling data structures (adding lines/rows)
173 virtual sa_size_t AddRow(const r_8* data);
174 // Filling data structures (adding lines/rows)
175 virtual sa_size_t AddRow(const MuTyV * data);
176 // Filling data structures (adding lines/rows)
177 virtual sa_size_t AddRow(DataTableRow const & data);
178
179 //! Alias for AddRow()
180 inline sa_size_t AddLine(const r_8* data)
181 { return AddRow(data); }
182 //! Alias for AddRow()
183 inline sa_size_t AddLine(const MuTyV * data)
184 { return AddRow(data); }
[2808]185 //! Alias for AddLine()
[2849]186 inline sa_size_t AddLine(DataTableRow const & data)
187 { return AddRow(data); }
188
189 //! Alias for AddRow() - Kept for backward compatibility with NTuple class interface.
[2688]190 inline sa_size_t Fill(const r_8* data)
[2849]191 { return AddRow(data); }
192
193 // Pour etendre la table -
[2688]194 virtual sa_size_t Extend();
195
[2849]196 //! Return the information stored in row \b n of the table in \b row object
197 virtual DataTableRow& GetRow(sa_size_t n, DataTableRow& row) const ;
198 //! Return the information stored in line (row) \b n of the table
199 virtual MuTyV * GetRow(sa_size_t n) const ;
200 //! Return the information stored in line \b n of the table. Alias of GetLine
201 inline MuTyV * GetLine(sa_size_t n) const
202 { return GetRow(n); }
[2808]203
[2831]204 //! Return the information stored in column \b k of the table, converted to double
205 virtual TVector<r_8> GetColumnD(sa_size_t k) const ;
206
207 //! Return the information stored in column named \b nom of the table, converted to double
208 inline TVector<r_8> GetColumnD(char const * nom) const
209 { return GetColumnD(IndexNom(nom)) ; }
210 //! Return the information stored in column named \b nom of the table, converted to double
211 inline TVector<r_8> GetColumnD(string const & nom) const
212 { return GetColumnD(IndexNom(nom)) ; }
213
[2808]214 //! Return the index for column name \b nom
[2688]215 sa_size_t IndexNom(char const* nom) const ;
[2808]216 //! Return the index for column name \b nom
[2688]217 inline sa_size_t IndexNom(string const & nom) const
218 { return IndexNom(nom.c_str()); }
[2808]219 //! Return the column name for column index \b k
[2688]220 string NomIndex(sa_size_t k) const ;
[2732]221
222 //! Return the column type for column \b k (no check on index range)
223 inline FieldType GetColumType(sa_size_t k) const
224 { return mNames[k].type; }
225 //! Return the column name for column \b k (no check on index range)
226 inline const string & GetColumName(sa_size_t k) const
227 { return mNames[k].nom; }
[2688]228
[2732]229
[2688]230 //! Copy or merges the data from \b a into the data table (cp=true -> copy)
231 virtual void CopyMerge(BaseDataTable const& a, bool cp=false) ;
[2699]232
233 //! Clear/reset the table content and structure.
234 virtual void Clear() = 0;
235
[2831]236 //! Prints the table content (ascii dump)
237 virtual ostream& Print(ostream& os, sa_size_t lstart, sa_size_t lend,
238 sa_size_t lstep=1) const ;
239 //! Prints the table content (ascii dump) on stream \b os
240 inline ostream& Print(ostream& os) const
241 { return Print(os, 0, NEntry(), 1); }
242 //! Prints the table content (ascii dump) on stream \b os
243 inline ostream& WriteASCII(ostream& os) const
244 { return Print(os, 0, NEntry(), 1); }
245
[2688]246
[2831]247 //! Prints table definition and number of entries
[2688]248 void Show(ostream& os) const ;
[2808]249 //! Prints table definition and number of entries on the standard output stream \b cout
[2688]250 inline void Show() const { Show(cout) ; }
251
[2831]252 //! Return the associated DVList (info) object.
[2688]253 DVList& Info() const ;
254
255 // Remplissage depuis fichier ASCII
[2831]256 sa_size_t FillFromASCIIFile(istream& is, char clm='#', const char* sep=" \t");
[2688]257
[2699]258 //-------------------------------------------------------------
259 //----------- Declaration de l interface NTuple --------------
260
[2688]261 virtual sa_size_t NbLines() const ;
262 virtual sa_size_t NbColumns() const ;
263 virtual r_8 * GetLineD(sa_size_t n) const ;
264 virtual r_8 GetCell(sa_size_t n, sa_size_t k) const ;
265 virtual r_8 GetCell(sa_size_t n, string const & nom) const ;
266 virtual string GetCelltoString(sa_size_t n, sa_size_t k) const ;
267 virtual void GetMinMax(sa_size_t k, double& min, double& max) const ;
268 virtual void GetMinMax(string const & nom, double& min, double& max) const ;
269 virtual sa_size_t ColumnIndex(string const & nom) const ;
270 virtual string ColumnName(sa_size_t k) const;
271 virtual string VarList_C(const char* nomx=NULL) const ;
272 virtual string LineHeaderToString() const;
273 virtual string LineToString(sa_size_t n) const;
[2699]274
[2831]275 //! Return a table row (line), formatted and converted to a string
276 virtual string TableRowToString(sa_size_t n, bool qstr, const char* sep=" ",
277 int fw=12) const;
278
[2699]279 // Pour la gestion de persistance PPF
280 friend class ObjFileIO<BaseDataTable> ;
[2822]281 // pour fichiers FITS
282 friend class FitsHandler<BaseDataTable>;
[2688]283
284protected:
285 uint_8 mNEnt ; // nb total d'entrees
286 uint_8 mSegSz ; // taille bloc/segment
287 uint_8 mNSeg; // Nb total de segments
288 mutable r_8 * mVarD; // Pour retourner une ligne de la table
289 mutable MuTyV * mVarMTV; // Pour retourner une ligne de la table en MuTyV
[2808]290
291 //! \cond Pour pas inclure ds la documentation doxygen
[2688]292 typedef struct {
293 string nom;
294 FieldType type;
295 sa_size_t ser;
296 } colst;
[2808]297 //! \endcond
[2688]298 std::vector<colst> mNames;
299
300 mutable DVList* mInfo; // Infos (variables) attachees au NTuple
301
302 // Pour calculer et garder le min/max
303 mutable std::vector<r_8> mMin;
304 mutable std::vector<r_8> mMax;
305 mutable std::vector<uint_8> mMinMaxNEnt;
306
307 // Les pointeurs des SegDataBlock ...
308 std::vector< SegDBInterface<int_4> * > mIColsP;
309 std::vector< sa_size_t > mIColIdx;
310 std::vector< SegDBInterface<int_8> * > mLColsP;
311 std::vector< sa_size_t > mLColIdx;
312 std::vector< SegDBInterface<r_4> * > mFColsP;
313 std::vector< sa_size_t > mFColIdx;
314 std::vector< SegDBInterface<r_8> * > mDColsP;
315 std::vector< sa_size_t > mDColIdx;
[2827]316 std::vector< SegDBInterface< complex<r_4> > * > mYColsP;
317 std::vector< sa_size_t > mYColIdx;
318 std::vector< SegDBInterface< complex<r_8> > * > mZColsP;
319 std::vector< sa_size_t > mZColIdx;
[2688]320 std::vector< SegDBInterface<string> * > mSColsP;
321 std::vector< sa_size_t > mSColIdx;
322
323};
324/*! Prints table information (Column name and type, min/max) on stream \b s (bd.Show(s)) */
325inline ostream& operator << (ostream& s, BaseDataTable const & bd)
326 { bd.Show(s); return(s); }
327
328} // namespace SOPHYA
329
330#endif
Note: See TracBrowser for help on using the repository browser.