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 );
550 col_indices [i] = index;
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 );