#include #include #include #include #include #include "TROOT.h" #include "TFile.h" #include "TGraph.h" #include "TGraphErrors.h" #include "JGizmo/JGizmoToolkit.hh" #include "Jeep/JeepToolkit.hh" #include "Jeep/JParser.hh" #include "Jeep/JMessage.hh" /** * \file * Auxiliary program to create TGraph from input file with ASCII data. * * Supported input file formats: *
 *     x y
 *     x y ey
 *     x y ex ey
 * 
* or (option -M) *
 *     x y [y [y]]
 * 
* * Lines starting with a '#' are skipped. * Optionally, a single header line is interpreted as column names (option -HM). * \author mdejong */ int main(int argc, char **argv) { using namespace std; vector inputFile; string outputFile; bool multicolumn; bool header; string title; int debug; try { JParser<> zap("Auxiliary program to create TGraph from input file with ASCII data."); zap['f'] = make_field(inputFile); zap['o'] = make_field(outputFile); zap['M'] = make_field(multicolumn); zap['H'] = make_field(header); zap['T'] = make_field(title) = ""; zap['d'] = make_field(debug) = 1; zap(argc, argv); } catch(const exception &error) { FATAL(error.what() << endl); } using namespace JPP; TFile out(outputFile.c_str(), "recreate"); for (vector::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) { const string gname = (title != "" ? title : getFilename(*file_name)); ifstream in(file_name->c_str()); while (in.peek() == '#') { in.ignore(numeric_limits::max(), '\n'); } vector column; if (header) { if (multicolumn) { string buffer; if (getline(in,buffer)) { istringstream is(buffer); for (string key; is >> key; ) { column.push_back(key); } } } else { in.ignore(numeric_limits::max(), '\n'); } } Double_t x, y, ex, ey; if (!multicolumn) { vector X; vector Y; vector EX; vector EY; for (string buffer; getline(in,buffer); ) { istringstream is(buffer); if (is >> x) X .push_back(x); if (is >> y) Y .push_back(y); if (is >> ex) EX.push_back(ex); if (is >> ey) EY.push_back(ey); } if (X.size() != Y.size()) { FATAL("Number of points " << X.size() << ' ' << Y.size() << endl); } TGraph* graph = NULL; if (EX.empty()) { graph = new TGraph(X.size(), X.data(), Y.data()); } else { if (X.size() != EX.size()) { FATAL("Number of x points " << X.size() << ' ' << EX.size() << endl); } if (EY.empty()) { EY.swap(EX); EX.resize(X.size(), 0.0); } if (Y.size() != EY.size()) { FATAL("Number of y points " << Y.size() << ' ' << EY.size() << endl); } graph = new TGraphErrors(X.size(), X.data(), Y.data(), EX.data(), EY.data()); } if (graph != NULL) { graph->SetName(gname.c_str()); setLimits(*graph); DEBUG("TGraph " << graph->GetName() << endl); graph->Write(); } } else { // multicolumn option vector< Double_t > X; vector< vector > Y; for (string buffer; getline(in,buffer); ) { istringstream is(buffer); if (is >> x) { X.push_back(x); size_t i = 0; for ( ; is >> y; ++i) { if (i == Y.size()) { Y.resize(i+1); } Y[i].push_back(y); } if (i+1 != column.size()) { FATAL("Number of colums " << i+1 << ' ' << column.size() << endl); } } } for (size_t i = 0; i != Y.size(); ++i) { TGraph* graph = new TGraph(X.size(), X.data(), Y[i].data()); ostringstream os; os << gname << "[" << column[i+1] << "]"; graph->SetName(os.str().c_str()); setLimits(*graph); DEBUG("TGraph " << graph->GetName() << endl); graph->Write(); } } in.close(); } out.Write(); out.Close(); }