Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JGraph.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <fstream>
4 #include <vector>
5 #include <limits>
6 
7 #include "TROOT.h"
8 #include "TFile.h"
9 #include "TGraph.h"
10 #include "TGraphErrors.h"
11 
12 #include "JGizmo/JGizmoToolkit.hh"
13 
14 #include "Jeep/JeepToolkit.hh"
15 #include "Jeep/JParser.hh"
16 #include "Jeep/JMessage.hh"
17 
18 
19 /**
20  * \file
21  * Auxiliary program to create TGraph from input file with ASCII data.
22  *
23  * Supported input file formats:
24  * <pre>
25  * x y
26  * x y ey
27  * x y ex ey
28  * </pre>
29  * or (option -M)
30  * <pre>
31  * x y [y [y]]
32  * </pre>
33  *
34  * Lines starting with a '#' are skipped.
35  * Optionally, a single header line is interpreted as column names (option -HM).
36  * \author mdejong
37  */
38 int main(int argc, char **argv)
39 {
40  using namespace std;
41 
42  vector<string> inputFile;
43  string outputFile;
44  bool multicolumn;
45  bool header;
46  int debug;
47 
48  try {
49 
50  JParser<> zap("Auxiliary program to create TGraph from input file with ASCII data.");
51 
52  zap['f'] = make_field(inputFile);
53  zap['o'] = make_field(outputFile);
54  zap['M'] = make_field(multicolumn);
55  zap['H'] = make_field(header);
56  zap['d'] = make_field(debug) = 1;
57 
58  zap(argc, argv);
59  }
60  catch(const exception &error) {
61  FATAL(error.what() << endl);
62  }
63 
64  using namespace JPP;
65 
66  TFile out(outputFile.c_str(), "recreate");
67 
68 
69  for (vector<string>::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
70 
71  const string title = getFilename(*file_name);
72 
73  ifstream in(file_name->c_str());
74 
75  while (in.peek() == '#') {
76  in.ignore(numeric_limits<streamsize>::max(), '\n');
77  }
78 
79  vector<string> column;
80 
81  if (header) {
82 
83  if (multicolumn) {
84 
85  string buffer;
86 
87  if (getline(in,buffer)) {
88 
89  istringstream is(buffer);
90 
91  for (string key; is >> key; ) {
92  column.push_back(key);
93  }
94  }
95 
96  } else {
97 
98  in.ignore(numeric_limits<streamsize>::max(), '\n');
99  }
100  }
101 
102 
103  Double_t x, y, ex, ey;
104 
105  if (!multicolumn) {
106 
109  vector<Double_t> EX;
110  vector<Double_t> EY;
111 
112  for (string buffer; getline(in,buffer); ) {
113 
114  istringstream is(buffer);
115 
116  if (is >> x) X .push_back(x);
117  if (is >> y) Y .push_back(y);
118  if (is >> ex) EX.push_back(ex);
119  if (is >> ey) EY.push_back(ey);
120  }
121 
122  if (X.size() != Y.size()) {
123  FATAL("Number of points " << X.size() << ' ' << Y.size() << endl);
124  }
125 
126 
127  TGraph* graph = NULL;
128 
129  if (EX.empty()) {
130 
131  graph = new TGraph(X.size(), X.data(), Y.data());
132 
133  } else {
134 
135  if (X.size() != EX.size()) {
136  FATAL("Number of x points " << X.size() << ' ' << EX.size() << endl);
137  }
138 
139  if (EY.empty()) {
140  EY.swap(EX);
141  EX.resize(X.size(), 0.0);
142  }
143 
144  if (Y.size() != EY.size()) {
145  FATAL("Number of y points " << Y.size() << ' ' << EY.size() << endl);
146  }
147 
148  graph = new TGraphErrors(X.size(), X.data(), Y.data(), EX.data(), EY.data());
149  }
150 
151  if (graph != NULL) {
152 
153  graph->SetName(title.c_str());
154 
155  setLimits(*graph);
156 
157  DEBUG("TGraph " << graph->GetName() << endl);
158 
159  graph->Write();
160  }
161 
162  } else { // multicolumn option
163 
166 
167  for (string buffer; getline(in,buffer); ) {
168 
169  istringstream is(buffer);
170 
171  if (is >> x) {
172 
173  X.push_back(x);
174 
175  size_t i = 0;
176 
177  for ( ; is >> y; ++i) {
178 
179  if (i == Y.size()) {
180  Y.resize(i+1);
181  }
182 
183  Y[i].push_back(y);
184  }
185 
186  if (i+1 != column.size()) {
187  FATAL("Number of colums " << i+1 << ' ' << column.size() << endl);
188  }
189  }
190  }
191 
192  for (size_t i = 0; i != Y.size(); ++i) {
193 
194  TGraph* graph = new TGraph(X.size(), X.data(), Y[i].data());
195 
196  ostringstream os;
197 
198  os << title << "[" << column[i+1] << "]";
199 
200  graph->SetName(os.str().c_str());
201 
202  setLimits(*graph);
203 
204  DEBUG("TGraph " << graph->GetName() << endl);
205 
206  graph->Write();
207  }
208  }
209 
210  in.close();
211  }
212 
213  out.Write();
214  out.Close();
215 }
Utility class to parse command line options.
Definition: JParser.hh:1410
void setLimits(TGraph &g1)
Set limits of TGraph.
string outputFile
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1836
Auxiliary methods for handling file names, type names and environment.
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:468
int debug
debug level
Definition: JSirene.cc:59
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:65
Utility class to parse command line options.
std::string getFilename(const std::string &file_name)
Get file name part, i.e.
Definition: JeepToolkit.hh:85
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:60
int main(int argc, char *argv[])
Definition: Main.cpp:15