// tableToCWN.cc
//
// This file presents and illustrates the use of a utility tableToCWNblock()
// which takes an istream of ascii characters which has on its first line
// column headings and in its remainder data (ws separated) and forms a
// Root Ntuple with that data.
//
// Usage of the main that comes with this file:
//
// tableToCWN []
#include
#include
#include
#include
#include
#include
#include
#include
int getrow (std::istream & in, std::vector & row);
bool tableToCWN ( std::istream & inputStream,
HepFileManager* manager,
const std::string & ntupleName,
bool reportProgress = false ) {
// Strategy:
//
// 1 - Establish the Ntuple
// 2 - Parse the first line of the input to declare the N columns
// 3 - Read the remaining table data to fill the ntuple entries
// 1 - Establish the (column-wise) Ntuple
HepNtuple& ntup = manager->ntuple(ntupleName);
ntup.setColumnWise();
// 2 - Parse the first line of the input to count and declare the N columns
std::string headingLine;
getline( inputStream, headingLine );
std::istringstream headings (headingLine);
if ( reportProgress ) {
std::cout << "Column names are: \n";
}
typedef std::vector svec;
svec nameList;
std::string columnName;
while (headings >> columnName) {
nameList.push_back(columnName);
}
std::vector rowData(nameList.size());
int numData = 0;
for(svec::const_iterator it(nameList.begin());
it != nameList.end();
it++ ) {
columnName = *it;
ntup.columnAt (columnName, &(rowData[numData]));
numData++;
if ( reportProgress ) {
std::cout << columnName << " ";
}
}
if ( reportProgress ) {
std::cout << "\n\nThis table has " << rowData.size() << " columns\n\n";
}
// 3 - Read the remaining table data to fill the ntuple entries
int nrows;
for ( nrows = 0; ; ++nrows ) {
int nc = getrow( inputStream, rowData);
if (nc == 0) break;
if (nc != (int)rowData.size()) {
std::cout << "The table data entries are not a multiple of nColumns\n";
std::cout << "The odd entries will not be in the Ntuple\n";
return false;
}
if (reportProgress && (nrows%5000 == 0)) {
std::cout << "rowData for row " << nrows << "\n";
for (int rj = 0; rj < (int)rowData.size(); ++rj ) {
std::cout << rowData[rj] << " ";
}
std::cout << "\n";
}
ntup.capture();
ntup.storeCapturedData();
ntup.clearData();
if (reportProgress && (nrows%100 == 0)) {
std::cout << "row " << nrows << "\n";
}
}
if ( reportProgress ) {
std::cout << "\nThis table has " << nrows << " rows of data\n\n";
}
return true;;
}
int getrow (std::istream & in, std::vector & row) {
int n = 0;
double x;
while(n<(int)row.size()) {
if (!(in >> x)) break;
// std::cout << " n = " << n << " data = " << x << "\n";
row[n] = x;
n++;
}
return n;
}
#include
int main (int argc, char* argv[]) {
if (argc < 3 || argc > 4) {
std::cout << "Usage: \n" <<
"tableToCWN []\n\n";
return -1;
}
std::string inputFile (argv[1]);
std::string rootFile (argv[2]);
std::string ntupleName ( argc == 4 ? argv[3] : inputFile );
std::cout << "Taking input from " << argv[1] << "\n";
std::cout << "Will produce file " << argv[2] << "\n";
std::cout << "Will name ntuple " << ntupleName << "\n";
std::ifstream input (inputFile.c_str());
if (!input) {
std::cout << "Failed to open input file!\n";
return -2;
}
HepFileManager* manager = new HepRootFileManager(rootFile,
HepFileManager::HEP_RENAME);
if (!manager) {
std::cout << "Failed to create file manager!\n";
return -3;
}
bool result = tableToCWN ( input, manager, ntupleName, true );
if (!result) {
std::cout << "tableToCWN reports failure to parse the data\n";
return -4;
}
std::cout << "tableToCWN s completed the job \n";
delete manager;
return 0;
}