Jpp  master_rocky-43-ge265d140c
the software that should make you happy
Functions
JGraph.cc File Reference

Auxiliary program to create TGraph from input file with ASCII data. More...

#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <limits>
#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"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

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

Definition in file JGraph.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 38 of file JGraph.cc.

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 
111  vector<Double_t> EX;
112  vector<Double_t> EY;
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
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
#define FATAL(A)
Definition: JMessage.hh:67
int debug
debug level
Definition: JSirene.cc:69
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:2142
Utility class to parse command line options.
Definition: JParser.hh:1698
std::string getFilename(const std::string &file_name)
Get file name part, i.e. part after last JEEP::PATHNAME_SEPARATOR if any.
Definition: JeepToolkit.hh:128
void setLimits(TGraph &g1)
Set limits of TGraph.
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JSTDTypes.hh:14