Jpp  pmt_effective_area_update
the software that should make you happy
 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  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 }
Utility class to parse command line options.
Definition: JParser.hh:1500
int main(int argc, char *argv[])
Definition: Main.cc:15
void setLimits(TGraph &g1)
Set limits of TGraph.
then fatal Wrong number of arguments fi set_variable STRING $argv[1] set_variable DETECTORXY_TXT $WORKDIR $DETECTORXY_TXT tail read X Y CHI2 RMS printf optimum n $X $Y $CHI2 $RMS awk v Y
string outputFile
is
Definition: JDAQCHSM.chsm:167
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
Auxiliary methods for handling file names, type names and environment.
then break fi done getCenter read X Y Z let X
std::istream & getline(std::istream &in, JString &object)
Read string from input stream until end of line.
Definition: JString.hh:478
int debug
debug level
Definition: JSirene.cc:63
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Utility class to parse command line options.
then usage $script< input_file >< detector_file > fi set_variable OUTPUT_DIR set_variable SELECTOR JDAQTimesliceL1 set_variable DEBUG case set_variable DEBUG
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:88
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:40