Changeset 2849 in Sophya for trunk/SophyaLib/HiStats
- Timestamp:
- Nov 21, 2005, 12:08:55 PM (20 years ago)
- Location:
- trunk/SophyaLib/HiStats
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/SophyaLib/HiStats/basedtable.cc
r2831 r2849 3 3 #include "sopnamsp.h" 4 4 #include "pexceptions.h" 5 6 /*! 7 \class SOPHYA::DataTableRow 8 \ingroup HiStats 9 This class is intented to be used with datatable classes 10 (inheriting from BaseDataTable) for representing a row (line) 11 of the table. 12 */ 13 DataTableRow::DataTableRow( vector<string>& colnames ) 14 { 15 if (colnames.size() < 1) 16 throw ParmError("DataTableRow::DataTableRow(vector<string>& cn) cn.size()==0 "); 17 size_ = colnames.size(); 18 mtv_ = new MuTyV[ size_ ]; 19 for(sa_size_t k=0; k<size_; k++) 20 nm2idx_[colnames[k]] = k; 21 } 22 23 DataTableRow::DataTableRow(DataTableRow const & a ) 24 { 25 size_ = a.size_; 26 mtv_ = new MuTyV[ size_ ]; 27 nm2idx_ = a.nm2idx_; 28 } 29 30 MuTyV DataTableRow::get(string const& colname) const 31 { 32 map<string, sa_size_t>::const_iterator it = nm2idx_.find(colname); 33 if (it == nm2idx_.end()) { 34 string msg = "DataTableRow::get( " ; 35 msg += colname; msg += " ) Not found column name"; 36 throw NotFoundExc(msg); 37 } 38 return mtv_[(*it).second]; 39 } 40 41 MuTyV& DataTableRow::get(string const& colname) 42 { 43 map<string, sa_size_t>::const_iterator it = nm2idx_.find(colname); 44 if (it == nm2idx_.end()) { 45 string msg = "DataTableRow::get( " ; 46 msg += colname; msg += " ) const - Not found column name"; 47 throw NotFoundExc(msg); 48 } 49 return mtv_[(*it).second]; 50 } 51 52 ostream& DataTableRow::Print(ostream& os) const 53 { 54 for(sa_size_t k=0; k<size_; k++) 55 os << (string)mtv_[k] << " "; 56 return os; 57 } 5 58 6 59 /*! … … 143 196 } 144 197 198 // Retourne une structure 199 /*! 200 The returned BaseDataTable object can be used for subsequent call to 201 AddRow() or GetRow() methods. 202 Generate an exception if called for a table with no columns 203 */ 204 DataTableRow BaseDataTable::EmptyRow() 205 { 206 if (NCols == 0) 207 throw ParmError("BaseDataTable::EmptyRow() Table has no column !"); 208 vector<string> nms; 209 for(sa_size_t k=0; k<NVar(); k++) nms.push_back(mNames[k].nom); 210 return DataTableRow(nms); 211 } 212 145 213 // 146 214 // A quel index correspond mon nom ? … … 162 230 } 163 231 164 //! Adds a line (or row to the table)with r_8* inout data232 //! Adds a row (or line) to the table with r_8* inout data 165 233 /*! 166 234 The data to be added is provided as an array (vector) of double (r_8). … … 171 239 (data[k] k=0..NbColumns()) 172 240 */ 173 sa_size_t BaseDataTable::Add Line(const r_8* data)241 sa_size_t BaseDataTable::AddRow(const r_8* data) 174 242 { 175 243 if (NVar() == 0) 176 throw ParmError("BaseDataTable::Add Line(const r_8*) Table has no column !");244 throw ParmError("BaseDataTable::AddRow(const r_8*) Table has no column !"); 177 245 if (NEntry() == SegmentSize()*NbSegments()) Extend(); 178 246 sa_size_t n = NEntry(); … … 198 266 } 199 267 200 //! Adds a line (or row to the table)with input data as an array of MuTyV268 //! Adds a row (or line) to the table with input data as an array of MuTyV 201 269 /*! 202 270 The data to be added is provided as an array (vector) of MuTyV. … … 207 275 (data[k] k=0..NbColumns()) 208 276 */ 209 sa_size_t BaseDataTable::Add Line(const MuTyV* data)277 sa_size_t BaseDataTable::AddRow(const MuTyV* data) 210 278 { 211 279 if (NVar() == 0) 212 throw ParmError("BaseDataTable::Add Line(const MuTyV*) Table has no column !");280 throw ParmError("BaseDataTable::AddRow(const MuTyV*) Table has no column !"); 213 281 if (NEntry() == SegmentSize()*NbSegments()) Extend(); 214 282 sa_size_t n = NEntry(); … … 235 303 return mNEnt; 236 304 } 237 305 //! Adds a row (or line) to the table with input data as DataTableRow object 306 /*! 307 The internal MuTyV array of the object contains the date and the 308 MuTyV class conversion operators are used to match against each 309 cell data type. 310 Only the size of the input data object is checked. 311 Return the new number of table rows (lines / entries) 312 \param data : Data for each cell of the row to be appended 313 (data[k] k=0..NbColumns()) 314 */ 315 sa_size_t BaseDataTable::AddRow(DataTableRow const& data) 316 { 317 if ( data.Size() != NCols() ) 318 throw SzMismatchError(" BaseDataTable::AddRow() - data.Size() != NCols() "); 319 return AddRow(data.MTVPtr()); 320 } 321 322 /*! 323 Extends the table (in the row direction). This method is called automatically when needed. 324 */ 238 325 sa_size_t BaseDataTable::Extend() 239 326 { … … 256 343 } 257 344 258 259 MuTyV* BaseDataTable::GetLine(sa_size_t n) const 345 /*! 346 Fills the input \b row object with the content of row \b n. 347 Return a reference to the input \b row object. 348 Generate an exception if the input \b row object has the wrong size. 349 This method is slower(less efficient) than the GetRow(n) method. 350 */ 351 DataTableRow& BaseDataTable::GetRow(sa_size_t n, DataTableRow& row) const 352 { 353 if ( row.Size() != NCols() ) 354 throw SzMismatchError(" BaseDataTable::GetRow(n, row) - row.Size() != NCols() "); 355 MuTyV* rmtv = GetRow(n); 356 for(sa_size_t k=0; k<NCols(); k++) 357 row[k] = rmtv[k]; 358 return row; 359 } 360 361 MuTyV* BaseDataTable::GetRow(sa_size_t n) const 260 362 { 261 363 if ((n < 0) || (n >= NEntry())) 262 throw RangeCheckError("BaseDataTable::Get Line() out of range line index n");364 throw RangeCheckError("BaseDataTable::GetRow() out of range line index n"); 263 365 if (mVarMTV == NULL) mVarMTV = new MuTyV[NVar()]; 264 366 … … 345 447 } 346 448 for(sa_size_t kk=0; kk<a.NEntry(); kk++) 347 Add Line(a.GetLine(kk));449 AddRow(a.GetLine(kk)); 348 450 } 349 451 … … 456 558 } 457 559 } 458 Add Line(mVarMTV);560 AddRow(mVarMTV); 459 561 nl++; 460 562 } -
trunk/SophyaLib/HiStats/basedtable.h
r2831 r2849 12 12 #include <string> 13 13 #include <vector> 14 #include <map> 14 15 15 16 #include <complex> … … 24 25 // Forward class declaration for Fits handler 25 26 template <class T> class FitsHandler; 27 28 //! Class for representing and manipulating DataTable rows. 29 class DataTableRow { 30 public: 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; 61 protected: 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 ( */ 68 inline ostream& operator << (ostream& s, DataTableRow const & row) 69 { row.Print(s); return(s); } 26 70 27 71 //! Interface definition for classes handling data in a table. … … 109 153 // Acces to various counts and parameters 110 154 //! Return the number of lines (rows) in the table) 155 inline sa_size_t NRows() const { return mNEnt ; } 156 //! Return the number of lines (rows) in the table) 111 157 inline sa_size_t NEntry() const { return mNEnt ; } 158 //! Return the number of columns in the tables (number of cells in a row) 159 inline sa_size_t NCols() const { return mNames.size() ; } 112 160 //! Return the number of columns in the tables (number of cells in a row) 113 161 inline sa_size_t NVar() const { return mNames.size() ; } … … 119 167 inline sa_size_t NbSegments() const { return mNSeg ; } 120 168 121 // Filling data structures (adding lines) 122 virtual sa_size_t AddLine(const r_8* data); 123 virtual sa_size_t AddLine(const MuTyV * data); 124 169 //! Return a compatible DataTableRow object (with table column names) 170 virtual DataTableRow EmptyRow(); 171 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); } 125 185 //! Alias for AddLine() 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. 126 190 inline sa_size_t Fill(const r_8* data) 127 { return AddLine(data); } 128 //! Alias for AddLine() 129 inline sa_size_t Fill(const MuTyV * data); 130 191 { return AddRow(data); } 192 193 // Pour etendre la table - 131 194 virtual sa_size_t Extend(); 132 195 133 //! Return the information stored in line \b n of the table 134 virtual MuTyV * GetLine(sa_size_t n) const ; 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); } 135 203 136 204 //! Return the information stored in column \b k of the table, converted to double -
trunk/SophyaLib/HiStats/datatable.cc
r2847 r2849 15 15 The whole data set is kept in memory. 16 16 \sa SOPHYA::MuTyV 17 \sa SOPHYA::DataTableRow 17 18 \sa SOPHYA::BaseDataTable 18 19 \sa SOPHYA::SegDataBlock … … 28 29 for(int i=0; i<63; i++) { 29 30 x[0] = (i%9)-4.; x[1] = (i/9)-3.; x[2] = x[0]*x[0]+x[1]*x[1]; 30 dt.Add Line(x);31 dt.AddRow(x); 31 32 } 32 33 // Printing table info … … 36 37 po << dt ; 37 38 \endcode 39 40 DataTableRow objects can also be used to fill the table, or access the data, as 41 shown in the example below: 42 \code 43 // We use the same table as in the example above: 44 DataTableRow row = dt.EmptyRow(); 45 row[0] = 83.3; row[1] = 20.; row[2] = 83.3*83.3+20.*20.; 46 dt.AddRow(row); 47 row["X0_f"] = 7.5; row["X1_f"] = 6.; row["X0X0pX1X1_d"] = 7.5*7.5+6.*6.; 48 dt.AddRow(row); 49 \endcode 38 50 */ 39 51 -
trunk/SophyaLib/HiStats/swppfdtable.cc
r2831 r2849 25 25 26 26 \sa SOPHYA::MuTyV 27 \sa SOPHYA::DataTableRow 27 28 \sa SOPHYA::DataTable 28 29 \sa SOPHYA::SwSegDataBlock SOPHYA::PPFDataSwapper
Note:
See TracChangeset
for help on using the changeset viewer.