14 #include "msdevstudio/MSconfig.h"
36 using std::runtime_error;
40 using namespace hippodraw;
65 findWhite (
const std::string & line,
unsigned int left,
bool tabs_only )
67 string::size_type right = line.find(
'\t', left );
69 if( line.find(
'\n', left ) < right ) right = line.find(
'\n', left );
71 if ( tabs_only ==
false ) {
72 if( line.find(
' ', left ) < right ) right = line.find(
' ', left );
81 std::vector < std::string > & values )
83 static string white (
" \n\r\t" );
86 string::size_type start = line.find_first_not_of ( white, 0 );
87 while ( start != string::npos ) {
88 string::size_type end = line.find_first_of ( white, start );
89 if ( end != string::npos ) {
90 values.push_back (
string ( line, start, end - start ) );
91 start = line.find_first_not_of ( white, end );
94 values.push_back (
string ( line, start ) );
117 bool hasTitle =
true;
118 bool hasLabel =
true;
120 string dir_name = filename;
122 string::size_type pos = dir_name.find_last_of(
'/' );
123 if ( pos == string::npos ) {
126 dir_name.erase( pos );
130 std::ifstream infile( filename.c_str() );
131 if( !infile.is_open() ) {
132 string what (
"NTupleController: Could not open file\n`" );
133 what += filename.c_str();
135 what +=
"Files doesn't exist or is not readable.";
136 throw runtime_error ( what );
139 if( !std::getline( infile, line ) ) {
144 while(
findWhite( line, 0,
false ) == 0 ) line = line.substr( 1 );
145 string::size_type firstTab = line.find(
'\t', 0 );
148 if ( firstTab != line.npos ) {
151 string firstword = line.substr ( 0, firstTab );
154 if ( std::atof ( firstword.c_str() ) != 0.0 ) {
160 if ( std::atof ( line.c_str() ) != 0.0 ) {
171 if ( !std::getline( infile, line ) )
175 string::size_type
size = line.size ();
176 if ( line[size-1] ==
'\r' ) {
177 line.erase ( size-1 );
180 string::size_type right =
findWhite( line, 0,
true );
181 string firstlabel = line.substr( 0, right );
183 if ( std::atof ( firstlabel.c_str() ) != 0.0 ) hasLabel=
false;
185 vector< string > labels;
186 string::size_type left = 0;
190 while( right != line.npos ){
204 else label = line.substr( left, right - left );
205 labels.push_back ( label );
209 if ( labels.size () == 0 ) {
215 unsigned int columns = labels.size ();
216 vector < double > vals ( columns );
219 std::getline ( infile, line );
224 vector < string > values;
227 if ( values.size() != columns ) {
230 for (
unsigned int i = 0; i < columns; i++ ) {
231 vals[
i] = atof ( values[i].c_str() );
233 ntuple -> addRow ( vals );
235 }
while ( std::getline ( infile, line ) );
237 vector < unsigned int >
shape ( 2 );
238 shape[0] = ntuple -> rows();
239 shape[1] = ntuple -> columns ();
240 ntuple -> setShape ( shape );
249 unsigned int columns = 0;
254 catch (
const runtime_error & e ) {
269 std::string what (
"NTupleController: File:\n `" );
272 if ( retval == -1 ) {
273 what +=
"could not be found.";
276 what +=
"had bad format.";
278 throw std::runtime_error ( what );
282 DataSource * ds = controller -> getDataSource ( filename );
286 nt -> setName ( filename );
288 controller -> registerDataSourceFile ( nt );
370 ntuple -> setName ( new_name );
376 const std::string & filename )
382 if ( ntuple == 0 )
return -1;
391 const std::string & filename )
393 ofstream file ( filename.c_str() );
394 if ( file.is_open () == false ) {
397 file << ntuple->
title() << endl;
399 const vector < string > & labels = ntuple->
getLabels ();
400 #ifdef ITERATOR_MEMBER_DEFECT
403 vector < string > ::const_iterator first = labels.begin ();
404 string label = *first++;
406 while ( first != labels.end() ) {
408 file <<
"\t" << label;
412 unsigned int rows = ntuple->
rows ();
413 for (
unsigned int i = 0;
i < rows;
i++ ) {
414 const vector < double > & row = ntuple->
getRow (
i );
416 #ifdef ITERATOR_MEMBER_DEFECT
419 vector < double > ::const_iterator first = row.begin();
420 while ( first != row.end() ) {
421 file <<
"\t" << *first++;
427 controller -> registerDataSourceFile ( ntuple );
437 saveNTuples (
const std::string & fileprefix,
const std::string & filesuffix )
439 string::size_type pos = fileprefix.find_last_of (
'/' );
440 const string path = fileprefix.substr ( 0, pos + 1 );
441 const string basename = fileprefix.substr ( pos + 1 );
444 vector < DataSource * > tuples;
446 unsigned int size = tuples.size();
448 for (
unsigned int i = 0;
i <
size;
i++ ) {
450 const string & tuple_name = ntuple->
getName ();
452 string filename ( basename );
454 filename += filesuffix;
456 string tuple_file ( path );
457 tuple_file += filename;
460 controller ->
changeName ( tuple_name, filename );
471 return ds -> getName ();
486 const std::vector < const TupleCut * > & cut_list,
489 if ( column_list.empty() )
return NULL;
491 unsigned int columnNumber = column_list.size();
492 unsigned int cutNumber = cut_list.size();
498 for (
unsigned int i = 0;
i <
size;
i++ )
504 for (
unsigned int j = 0; j < cutNumber; j++ )
507 accept = tc -> acceptRow ( ds,
i );
514 for (
unsigned int k = 0; k < columnNumber; k++ )
529 const std::vector < const TupleCut * > & cut_list,
531 const std::string & filename,
532 const std::string & dsname)
534 if ( column_list.empty() )
return 1;
536 ofstream file ( filename.c_str() );
537 if ( file.is_open () == false ) {
541 unsigned int columnNumber = column_list.size();
542 vector < int > col_indices ( columnNumber );
544 for (
unsigned int i = 0;
i < columnNumber;
i++ ) {
545 const string & label = column_list [
i ];
546 int index = ds -> indexOf ( label );
548 ds -> throwIfInvalidLabel ( label );
553 file << dsname << endl;
555 #ifdef ITERATOR_MEMBER_DEFECT
558 vector < string > ::const_iterator first = column_list.begin ();
559 string label = *first++;
561 while ( first != column_list.end() ) {
563 file <<
"\t" << label;
567 unsigned int cutNumber = cut_list.size();
571 for (
unsigned int i = 0;
i <
size;
i++ )
577 for (
unsigned int j = 0; j < cutNumber; j++ )
580 accept = tc -> acceptRow ( ds,
i );
587 for (
unsigned int k = 0; k < columnNumber; k++ )
589 int index = col_indices [ k ];
590 file <<
"\t" << ds -> valueAtNoCache (
i, index );
virtual void setTitle(const std::string &title)
Sets the title of the data source to title.
void splitAndFill(const std::string &line, std::vector< std::string > &values)
Splits the line at white space and fills values with strings found.
std::string::size_type findWhite(const std::string &line, unsigned int left=0, bool tabs_only=false)
Find and return position of white space.
NTupleController()
A default constructor for avoiding creation except by itself.
std::string registerNTuple(DataSource *nt)
Register a NTuple.
int writeNTupleToFile(DataSource *source, const std::string &filename)
Writes the DataSource source to a file given by filename.
void changeName(DataSource *source, const std::string &newname)
Changes the name of the DataSource object.
int createNTupleToFile(const std::vector< std::string > &column_list, const std::vector< const TupleCut * > &cut_list, DataSource *ds, const std::string &filename, const std::string &dsname)
Create NTuple with column list and cut list and save to a file.
int readAsciiNTuple(NTuple *ntuple, const std::string &filename)
Reads ASCII formatted file and fills the NTuple.
void setLabels(const std::vector< std::string > &v)
Assigns the label to each column from the vector of strings.
const std::string & title() const
Returns a const reference to the title of the data source.
std::vector< intptr_t > shape(numeric::array arr)
The class expresses a cut on a DataSource, i.e.
virtual void addRow(const std::vector< double > &v)
Adds a row to the end of the ntuple.
The namespace for conversion to string.
CircularBuffer * createCircularBuffer()
Creates and registers an empty CircularBuffer.
void fillFromFile(const std::string &file, NTuple *source)
Fills the empty NTuple source from reading data from the file.
void saveNTuples(const std::string &prefix, const std::string &suffix)
Saves all the NTuple.
virtual const std::vector< std::string > & getLabels() const
Returns the list of available labels.
A singleton class that is the interface between GUI and the NTuple objects.
intp size(numeric::array arr)
string convert(int i)
Converts an integer to a string.
CircularBuffer class interface.
A singleton class that is the interface between GUI and the DataSource objects.
virtual double valueAtNoCache(unsigned int row, unsigned int column) const
Returns the value in the table in position given by the row and column indexes without storing it int...
static NTupleController * instance()
Returns the pointer to the singleton instance.
NTuple * createNTuple()
Creates and registers an empty NTuple.
void getDataSources(std::vector< DataSource * > &, bool all=true) const
Clears and fills the vector with the registered DataSource objects.
A DataSource class implemented with std::vector<double> to store the column data. ...
hippodraw::TupleCut class interface
virtual unsigned int rows() const =0
Returns the number of rows.
virtual int indexOf(const std::string &label) const
Returns true if the specified column labeled label has been filled.
hippodraw::NTupleController class interface
DataSourceController class interface.
virtual const std::vector< double > & getRow(unsigned int) const =0
Returns a const reference to slice along the axis known as a row.
DataSource * findDataSource(const std::string &name) const
Returns the DataSource object with name name.
const std::string & getName() const
Returns the name of the data source.
static NTupleController * s_instance
The pointer to the singleton object.
static DataSourceController * instance()
Returns the pointer to the singleton instance.
Base class for DataSource.