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

Last change on this file since 3089 was 2962, checked in by ansari, 19 years ago

Ajout methode BaseDataTable::CheckCorrectColName() pour correction des noms de colonnes (pour lecture FITS en particulier), Reza 2/6/2006

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