Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTuna2Graph.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <sstream>
4 #include <iomanip>
5 #include <iterator>
6 #include <set>
7 #include <map>
8 
9 #include "TROOT.h"
10 #include "TFile.h"
11 #include "TGraph.h"
12 #include "TTimeStamp.h"
13 #include "TNamed.h"
14 
15 #include "JDB/JDatalog.hh"
16 #include "JDB/JSupport.hh"
18 #include "JROOT/JRootToolkit.hh"
19 #include "JTools/JRange.hh"
20 
21 #include "Jeep/JPrint.hh"
22 #include "Jeep/JParser.hh"
23 #include "Jeep/JMessage.hh"
24 
25 
26 namespace {
27 
28  /**
29  * Auxiliary data structure for book keeping of histograms.
30  */
31  struct JKey_t {
32  /**
33  * Constuctor.
34  *
35  * \param string string
36  * \param floor floor
37  * \param parameter parameter
38  */
39  JKey_t(const int string,
40  const int floor,
41  const std::string& parameter) :
42  string (string),
43  floor (floor),
44  parameter(parameter)
45  {}
46 
47 
48  /**
49  * Less-than operator.
50  *
51  * \param first first key
52  * \param second second key
53  * \return true if first key before second key; else false
54  */
55  friend inline bool operator<(const JKey_t& first, const JKey_t& second)
56  {
57  if (first.string == second.string) {
58 
59  if (first.floor == second.floor)
60  return first.parameter < second.parameter;
61  else
62  return first.floor < second.floor;
63 
64  } else {
65 
66  return first.string < second.string;
67  }
68  }
69 
70 
71  /**
72  * Convert to string.
73  *
74  * \return string
75  */
76  std::string toString() const
77  {
78  std::ostringstream os;
79 
80  os << *this;
81 
82  return os.str();
83  }
84 
85 
86  /**
87  * Write key to output stream.
88  *
89  * \param output output stream
90  * \param object key
91  * \return output stream
92  */
93  friend inline std::ostream& operator<<(std::ostream& out, const JKey_t& object)
94  {
95  using namespace std;
96 
97  return out << setw(3) << setfill('0') << object.string << '.'
98  << setw(2) << setfill('0') << object.floor << '.'
99  << setfill(' ') << object.parameter;
100  }
101 
102  int string;
103  int floor;
104  std::string parameter;
105  };
106 }
107 
108 
109 /**
110  * \file
111  *
112  * Auxiliary program to convert ROOT TTree with slow control data to ROOT TGraph's.
113  * \author mdejong
114  */
115 int main(int argc, char **argv)
116 {
117  using namespace std;
118  using namespace JPP;
119 
121  JLimit_t& numberOfEvents = inputFile.getLimit();
122  string outputFile;
123  set<string> filter;
124  int debug;
125 
126  try {
127 
128  JParser<> zap("Auxiliary program to convert ROOT TTree with slow control data to ROOT TGraph's.");
129 
130  zap['f'] = make_field(inputFile, "ROOT input file (output file of JTuna).");
131  zap['n'] = make_field(numberOfEvents) = JLimit::max();
132  zap['o'] = make_field(outputFile, "ROOT output file");
133  zap['F'] = make_field(filter, "filter") = JPARSER::initialised();
134  zap['d'] = make_field(debug) = 2;
135 
136  zap(argc, argv);
137  }
138  catch(const exception &error) {
139  FATAL(error.what() << endl);
140  }
141 
142 
143  struct JGraph_t {
144 
145  void put(const Double_t x, const Double_t y)
146  {
147  X.push_back(x);
148  Y.push_back(y);
149  }
150 
151  vector<double> X;
152  vector<double> Y;
153  };
154 
155 
157 
158  set<int> strings;
159  set<int> floors;
160 
161  long long int counter = 0;
162 
163  for (inputFile.rewind(); inputFile.hasNext(); ++counter) {
164 
165  STATUS(setw(10) << counter << '\r'); DEBUG(endl);
166 
167  JDatalog* p = inputFile.next();
168 
169  if (filter.count(p->parameter) == 0) {
170 
171  JGraph_t& g1 = data[JKey_t(p->string, p->floor, p->parameter)];
172 
173  g1.put(p->getTime(), p->value);
174 
175  strings.insert(p->string);
176  floors .insert(p->floor);
177  }
178  }
179  STATUS(endl);
180 
181 
182  TFile out(outputFile.c_str(), "recreate");
183 
184  for (map<JKey_t, JGraph_t>::iterator i = data.begin(); i != data.end(); ++i) {
185 
186  TGraph g1(i->second.X.size(), i->second.X.data(), i->second.Y.data());
187 
188  g1.SetName(i->first.toString().c_str());
189 
190  const JRange<double> range(i->second.Y.begin(), i->second.Y.end());
191 
192  g1.SetMinimum(range.getLowerLimit());
193  g1.SetMaximum(range.getUpperLimit());
194 
195  out << g1;
196  }
197 
198  ostringstream os;
199 
200  os << "set_variable NUMBER_OF_STRINGS " << setw(4) << strings.size() << ";" << endl;
201  os << "set_variable NUMBER_OF_FLOORS " << setw(4) << floors. size() << ";" << endl;
202  if (!strings.empty()) {
203  os << "set_variable FIRST_STRING " << setw(4) << *strings. begin() << ";" << endl;
204  os << "set_variable LAST_STRING " << setw(4) << *strings.rbegin() << ";" << endl;
205  }
206  if (!floors.empty()) {
207  os << "set_variable FIRST_FLOOR " << setw(4) << *floors. begin() << ";" << endl;
208  os << "set_variable LAST_FLOOR " << setw(4) << *floors. rbegin() << ";" << endl;
209  }
210  os << "set_array STRINGS ";
211  copy(strings.begin(), strings.end(), ostream_iterator<int>(os, " "));
212  os << endl;
213 
214  TNamed meta("TUNA", os.str().c_str());
215 
216  out << meta;
217 
218  out.Write();
219  out.Close();
220 }
Utility class to parse command line options.
Definition: JParser.hh:1493
double getTime() const
Get time.
Definition: JDatalog.hh:65
bool operator<(const Head &first, const Head &second)
Less than operator.
Definition: JHead.hh:1263
#define STATUS(A)
Definition: JMessage.hh:63
Empty structure for specification of parser element that is initialised (i.e. does not require input)...
Definition: JParser.hh:63
string outputFile
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
Auxiliary class for defining the range of iterations of objects.
Definition: JLimit.hh:41
I/O formatting auxiliaries.
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
ROOT TTree parameter settings.
int debug
debug level
Definition: JSirene.cc:61
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Scanning of objects from multiple files according a format that follows from the extension of each fi...
std::string parameter
Definition: JDatalog.hh:74
Auxiliary class to define a range between two values.
General purpose class for object reading from a list of file names.
Utility class to parse command line options.
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:153
std::ostream & operator<<(std::ostream &stream, const CLBCommonHeader &header)
const JLimit & getLimit() const
Get limit.
Definition: JLimit.hh:73
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
Double_t g1(const Double_t x)
Function.
Definition: JQuantiles.cc:25
int main(int argc, char *argv[])
Definition: Main.cpp:15