////////////////////////////////////////////////////////////////////////// // // // MyVec // // // // A TVectorD with extra functions: // // ReadValues(filename) // // WriteValues(filename) // // to read and write its values from / to a flat file // // // // To use this interactively: (assume vec.asc contains 6 values): // // .L MyVec.cxx+ // // MyVec x(6); // // x.ReadValues("vec.asc") // // // ////////////////////////////////////////////////////////////////////////// #include "TVectorD.h" #include "iostream" #include "fstream" namespace std {} using namespace std; class MyVec : public TVectorD { public: MyVec(){}; MyVec(Int_t n); MyVec(Int_t n, Double_t *val); ~MyVec() {}; Int_t ReadValues(const Char_t * fname); Int_t WriteValues(const Char_t * fname); ClassDef(MyVec,0) // A TVectorD with ASCII I/O facility }; ClassImp(MyVec) //__________________________________________________________ MyVec::MyVec(Int_t n, Double_t *val) : TVectorD (n, val){}; //__________________________________________________________ MyVec::MyVec(Int_t n) : TVectorD (n){}; //___________________________________________________ Int_t MyVec::ReadValues(const Char_t * fname) { // Read values from ascii file with name fname // Return number of items read Int_t nread = 0; ifstream inf(fname); if (inf.fail()) { cout << "Cant open: " << fname << endl; } else { for (Int_t i = 0; i < GetNrows(); i++) { Float_t temp; inf >> temp; if (inf.eof())break; (*this)[i] = temp; nread++; } } if (nread != GetNrows()) cout << "Warning: Values read: " << nread << " expected: " << GetNrows()<< endl; return nread; } //__________________________________________________ Int_t MyVec::WriteValues(const Char_t * fname) { // Write values to ascii file with name fname // Return number of items written Int_t written = 0; ofstream inf(fname); if (inf.fail()) { cout << "Cant open: " << fname << endl; } else { for (Int_t i = 0; i < GetNrows(); i++) { inf << (*this)[i]; written++; } } return written; }