YODA is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.3.1
ReaderFLAT.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of YODA -- Yet more Objects for Data Analysis
4 // Copyright (C) 2008-2015 The YODA collaboration (see AUTHORS for details)
5 //
6 #ifndef YODA_READERFLAT_H
7 #define YODA_READERFLAT_H
8 
9 #include "YODA/AnalysisObject.h"
10 #include "YODA/Reader.h"
11 #include <YODA/Counter.h>
12 #include <YODA/Scatter1D.h>
13 #include <YODA/Scatter2D.h>
14 #include <YODA/Scatter3D.h>
15 #include <boost/spirit/include/qi.hpp>
16 #include <boost/spirit/include/phoenix_operator.hpp>
17 #include <boost/fusion/include/adapt_struct.hpp>
18 
19 namespace YODA {
20 
21  using namespace boost::spirit;
22  using namespace boost::phoenix;
23 
24 
26  class ReaderFLAT : public Reader {
27  public:
28 
30  static Reader& create() {
31  static ReaderFLAT _instance;
32  return _instance;
33  }
34 
35  void read(std::istream& stream, std::vector<AnalysisObject*>& aos) {
36  _readDoc(stream, aos);
37  }
38 
39 
40  // Hide from Doxygen until endcond
42 
43  protected:
44 
45  void _readDoc(std::istream& stream, std::vector<AnalysisObject*>& aos);
46 
47  private:
48 
49  void cleanup() {
50  _scatter1d.points.clear();
51  _scatter2d.points.clear();
52  _scatter3d.points.clear();
53  _annotations.clear();
54  }
55 
56  public:
57 
60  ReaderFLAT() { }
61 
62 
63  // Here comes everything we need for the parser
64 
66  struct histogrampointsymmetric0d {
67  double x;
68  double ex;
69  };
70 
71  struct histogrampointasymmetric0d {
72  double x;
73  double exminus;
74  double explus;
75  };
76 
77  struct histogrampointsymmetric1d {
78  double xmin;
79  double xmax;
80  double y;
81  double ey;
82  };
83 
84  struct histogrampointasymmetric1d {
85  double xmin;
86  double xmax;
87  double y;
88  double eyminus;
89  double eyplus;
90  };
91 
92  struct histogrampointsymmetric2d {
93  double xmin;
94  double xmax;
95  double ymin;
96  double ymax;
97  double z;
98  double ez;
99  };
100 
101  struct histogrampointasymmetric2d {
102  double xmin;
103  double xmax;
104  double ymin;
105  double ymax;
106  double z;
107  double ezminus;
108  double ezplus;
109  };
110 
112  struct keyval {
113  std::string key;
114  std::string val;
115  };
116 
117 
119  static std::map<std::string, std::string> _annotations;
120 
121 
123  struct scatter1d {
124  std::vector<YODA::Point1D> points;
125  };
126  static scatter1d _scatter1d;
127 
128 
130  struct scatter2d {
131  std::vector<YODA::Point2D> points;
132  };
133  static scatter2d _scatter2d;
134 
135 
137  struct scatter3d {
138  std::vector<YODA::Point3D> points;
139  };
140  static scatter3d _scatter3d;
141 
142 
144 
145 
147  struct fillpoint {
148  // 0D
149  void operator()(const histogrampointsymmetric0d p, qi::unused_type, qi::unused_type) const {
150  //std::cout << "0d symm" << std::endl;
151  YODA::Point1D point(p.x, p.ex, p.ex);
152  _scatter1d.points.push_back(point);
153  }
154  void operator()(const histogrampointasymmetric0d p, qi::unused_type, qi::unused_type) const {
155  //std::cout << "0d asymm" << std::endl;
156  YODA::Point1D point(p.x, p.exminus, p.explus);
157  _scatter1d.points.push_back(point);
158  }
159  // 1D
160  void operator()(const histogrampointsymmetric1d p, qi::unused_type, qi::unused_type) const {
161  //std::cout << "1d symm" << std::endl;
162  double x = 0.5*(p.xmin+p.xmax);
163  double ex = 0.5*(p.xmax-p.xmin);
164  YODA::Point2D point(x, p.y, ex, ex, p.ey, p.ey);
165  _scatter2d.points.push_back(point);
166  }
167  void operator()(const histogrampointasymmetric1d p, qi::unused_type, qi::unused_type) const {
168  //std::cout << "1d asymm" << std::endl;
169  double x = 0.5*(p.xmin+p.xmax);
170  double ex = 0.5*(p.xmax-p.xmin);
171  YODA::Point2D point(x, p.y, ex, ex, p.eyminus, p.eyplus);
172  _scatter2d.points.push_back(point);
173  }
174  // 2D
175  void operator()(const histogrampointsymmetric2d p, qi::unused_type, qi::unused_type) const {
176  //std::cout << "2d symm" << std::endl;
177  double x = 0.5*(p.xmin+p.xmax);
178  double ex = 0.5*(p.xmax-p.xmin);
179  double y = 0.5*(p.ymin+p.ymax);
180  double ey = 0.5*(p.ymax-p.ymin);
181  YODA::Point3D point(x, y, p.z, ex, ex, ey, ey, p.ez, p.ez);
182  _scatter3d.points.push_back(point);
183  }
184  void operator()(const histogrampointasymmetric2d p, qi::unused_type, qi::unused_type) const {
185  //std::cout << "2d asymm" << std::endl;
186  double x = 0.5*(p.xmin+p.xmax);
187  double ex = 0.5*(p.xmax-p.xmin);
188  double y = 0.5*(p.ymin+p.ymax);
189  double ey = 0.5*(p.ymax-p.ymin);
190  YODA::Point3D point(x, y, p.z, ex, ex, ey, ey, p.ezminus, p.ezplus);
191  _scatter3d.points.push_back(point);
192  }
193  };
194 
195 
197  struct fillkeyval {
198  void operator()(const keyval m, qi::unused_type, qi::unused_type) const {
199  _annotations[m.key] = m.val;
200  }
201  };
202 
203 
208  static qi::symbols<char, int> bgroup;
209  static qi::symbols<char, int> egroup;
210  template <typename Iterator>
211  struct group_grammar : qi::grammar<Iterator, int()>
212  {
213  group_grammar() : group_grammar::base_type(start) {
214  start = begin | end;
215  begin = qi::eps [_val = 0] >>
216  qi::lit("# BEGIN ") >>
217  bgroup [_val += _1];
218  end = qi::eps [_val = 0] >>
219  qi::lit("# END ") >>
220  egroup [_val += _1];
221  }
222  qi::rule<Iterator, int()> start, begin, end;
223  };
224 
225 
227  template <typename Iterator, typename Skipper>
228  struct data_grammar : qi::grammar<Iterator, Skipper>
229  {
230 
231  data_grammar() : data_grammar::base_type(line) {
232 
238  line = HistogramPointAsymmetric2D [fillpoint()] |
239  HistogramPointSymmetric2D [fillpoint()] |
240  HistogramPointAsymmetric1D [fillpoint()] |
241  HistogramPointSymmetric1D [fillpoint()] |
242  HistogramPointAsymmetric0D [fillpoint()] |
243  HistogramPointSymmetric0D [fillpoint()] |
244  keyvaluepair[fillkeyval()] |
245  comment;
246 
247  // Histogram
248  HistogramPointAsymmetric2D %= double_ >> double_ >> double_ >> double_ >> double_ >> double_ >> double_;
249  HistogramPointSymmetric2D %= double_ >> double_ >> double_ >> double_ >> double_ >> double_;
250  HistogramPointAsymmetric1D %= double_ >> double_ >> double_ >> double_ >> double_;
251  HistogramPointSymmetric1D %= double_ >> double_ >> double_ >> double_;
252  HistogramPointAsymmetric0D %= double_ >> double_ >> double_;
253  HistogramPointSymmetric0D %= double_ >> double_;
254 
255 
260  key = !qi::char_("0-9-") >> *~qi::char_("=");
261  value = *~qi::char_("\n");
262  keyvaluepair %= key >> "=" >> value;
263 
265  comment = qi::lit("#") >> *~qi::char_("\n");
266  }
267 
273  qi::rule<Iterator, Skipper> line, comment;
274  qi::rule<Iterator, std::string()> key, value;
275  qi::rule<Iterator, keyval(), Skipper> keyvaluepair;
276 
277  qi::rule<Iterator, histogrampointsymmetric2d(), Skipper> HistogramPointSymmetric2D;
278  qi::rule<Iterator, histogrampointasymmetric2d(), Skipper> HistogramPointAsymmetric2D;
279  qi::rule<Iterator, histogrampointsymmetric1d(), Skipper> HistogramPointSymmetric1D;
280  qi::rule<Iterator, histogrampointasymmetric1d(), Skipper> HistogramPointAsymmetric1D;
281  qi::rule<Iterator, histogrampointsymmetric0d(), Skipper> HistogramPointSymmetric0D;
282  qi::rule<Iterator, histogrampointasymmetric0d(), Skipper> HistogramPointAsymmetric0D;
283  };
284 
285 
287 
288  };
289 
290 } // end of YODA namespace
291 
292 
293 
294 // Now we need to make boost aware of the structs we want to
295 // fill directly from the parser. Boost wants this magic in
296 // the global scope, that's why we have it outside the namespace.
297 
299 
301 
302 BOOST_FUSION_ADAPT_STRUCT(
303  YODA::ReaderFLAT::histogrampointsymmetric0d,
304  (double, x)
305  (double, ex)
306 )
307 
308 BOOST_FUSION_ADAPT_STRUCT(
309  YODA::ReaderFLAT::histogrampointasymmetric0d,
310  (double, x)
311  (double, exminus)
312  (double, explus)
313 )
314 
315 BOOST_FUSION_ADAPT_STRUCT(
316  YODA::ReaderFLAT::histogrampointsymmetric1d,
317  (double, xmin)
318  (double, xmax)
319  (double, y)
320  (double, ey)
321 )
322 
323 BOOST_FUSION_ADAPT_STRUCT(
324  YODA::ReaderFLAT::histogrampointasymmetric1d,
325  (double, xmin)
326  (double, xmax)
327  (double, y)
328  (double, eyminus)
329  (double, eyplus)
330 )
331 
332 BOOST_FUSION_ADAPT_STRUCT(
333  YODA::ReaderFLAT::histogrampointsymmetric2d,
334  (double, xmin)
335  (double, xmax)
336  (double, ymin)
337  (double, ymax)
338  (double, z)
339  (double, ez)
340 )
341 
342 BOOST_FUSION_ADAPT_STRUCT(
343  YODA::ReaderFLAT::histogrampointasymmetric2d,
344  (double, xmin)
345  (double, xmax)
346  (double, ymin)
347  (double, ymax)
348  (double, z)
349  (double, ezminus)
350  (double, ezplus)
351 )
352 
353 BOOST_FUSION_ADAPT_STRUCT(
354  YODA::ReaderFLAT::keyval,
355  (std::string, key)
356  (std::string, val)
357 )
358 
360 
361 #endif
void read(std::istream &stream, std::vector< AnalysisObject * > &aos)
Read in a collection of objects objs from output stream stream.
Definition: ReaderFLAT.h:35
static Reader & create()
Singleton creation function.
Definition: ReaderFLAT.h:30
A 3D data point to be contained in a Scatter3D.
Definition: Point3D.h:17
Persistency reader from YODA flat text data format.
Definition: ReaderFLAT.h:26
A 1D data point to be contained in a Scatter1D.
Definition: Point1D.h:17
Pure virtual base class for various output writers.
Definition: Reader.h:18
A 2D data point to be contained in a Scatter2D.
Definition: Point2D.h:17