Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
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
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 */
38int 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 string title;
47 int debug;
48
49 try {
50
51 JParser<> zap("Auxiliary program to create TGraph from input file with ASCII data.");
52
53 zap['f'] = make_field(inputFile);
54 zap['o'] = make_field(outputFile);
55 zap['M'] = make_field(multicolumn);
56 zap['H'] = make_field(header);
57 zap['T'] = make_field(title) = "";
58 zap['d'] = make_field(debug) = 1;
59
60 zap(argc, argv);
61 }
62 catch(const exception &error) {
63 FATAL(error.what() << endl);
64 }
65
66 using namespace JPP;
67
68 TFile out(outputFile.c_str(), "recreate");
69
70
71 for (vector<string>::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
72
73 const string gname = (title != "" ? title : getFilename(*file_name));
74
75 ifstream in(file_name->c_str());
76
77 while (in.peek() == '#') {
78 in.ignore(numeric_limits<streamsize>::max(), '\n');
79 }
80
81 vector<string> column;
82
83 if (header) {
84
85 if (multicolumn) {
86
87 string buffer;
88
89 if (getline(in,buffer)) {
90
91 istringstream is(buffer);
92
93 for (string key; is >> key; ) {
94 column.push_back(key);
95 }
96 }
97
98 } else {
99
100 in.ignore(numeric_limits<streamsize>::max(), '\n');
101 }
102 }
103
104
105 Double_t x, y, ex, ey;
106
107 if (!multicolumn) {
108
113
114 for (string buffer; getline(in,buffer); ) {
115
116 istringstream is(buffer);
117
118 if (is >> x) X .push_back(x);
119 if (is >> y) Y .push_back(y);
120 if (is >> ex) EX.push_back(ex);
121 if (is >> ey) EY.push_back(ey);
122 }
123
124 if (X.size() != Y.size()) {
125 FATAL("Number of points " << X.size() << ' ' << Y.size() << endl);
126 }
127
128
129 TGraph* graph = NULL;
130
131 if (EX.empty()) {
132
133 graph = new TGraph(X.size(), X.data(), Y.data());
134
135 } else {
136
137 if (X.size() != EX.size()) {
138 FATAL("Number of x points " << X.size() << ' ' << EX.size() << endl);
139 }
140
141 if (EY.empty()) {
142 EY.swap(EX);
143 EX.resize(X.size(), 0.0);
144 }
145
146 if (Y.size() != EY.size()) {
147 FATAL("Number of y points " << Y.size() << ' ' << EY.size() << endl);
148 }
149
150 graph = new TGraphErrors(X.size(), X.data(), Y.data(), EX.data(), EY.data());
151 }
152
153 if (graph != NULL) {
154
155 graph->SetName(gname.c_str());
156
157 setLimits(*graph);
158
159 DEBUG("TGraph " << graph->GetName() << endl);
160
161 graph->Write();
162 }
163
164 } else { // multicolumn option
165
168
169 for (string buffer; getline(in,buffer); ) {
170
171 istringstream is(buffer);
172
173 if (is >> x) {
174
175 X.push_back(x);
176
177 size_t i = 0;
178
179 for ( ; is >> y; ++i) {
180
181 if (i == Y.size()) {
182 Y.resize(i+1);
183 }
184
185 Y[i].push_back(y);
186 }
187
188 if (i+1 != column.size()) {
189 FATAL("Number of colums " << i+1 << ' ' << column.size() << endl);
190 }
191 }
192 }
193
194 for (size_t i = 0; i != Y.size(); ++i) {
195
196 TGraph* graph = new TGraph(X.size(), X.data(), Y[i].data());
197
198 ostringstream os;
199
200 os << gname << "[" << column[i+1] << "]";
201
202 graph->SetName(os.str().c_str());
203
204 setLimits(*graph);
205
206 DEBUG("TGraph " << graph->GetName() << endl);
207
208 graph->Write();
209 }
210 }
211
212 in.close();
213 }
214
215 out.Write();
216 out.Close();
217}
string outputFile
int main(int argc, char **argv)
Definition JGraph.cc:38
General purpose messaging.
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
Utility class to parse command line options.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
Auxiliary methods for handling file names, type names and environment.
Utility class to parse command line options.
Definition JParser.hh:1698
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).