yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.7.2
WriterFLAT.cc
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-2017 The YODA collaboration (see AUTHORS for details)
5 //
6 #include "YODA/WriterFLAT.h"
7 
8 #include "YODA/Counter.h"
9 #include "YODA/Histo1D.h"
10 #include "YODA/Histo2D.h"
11 #include "YODA/Profile1D.h"
12 #include "YODA/Profile2D.h"
13 #include "YODA/Scatter1D.h"
14 #include "YODA/Scatter2D.h"
15 #include "YODA/Scatter3D.h"
16 
17 #include <iostream>
18 #include <iomanip>
19 
20 using namespace std;
21 
22 namespace YODA {
23 
25  Writer& WriterFLAT::create() {
26  static WriterFLAT _instance;
27  _instance.setPrecision(6);
28  return _instance;
29  }
30 
31  void WriterFLAT::_writeAnnotations(std::ostream& os, const AnalysisObject& ao) {
32  os << scientific << setprecision(_precision);
33  for (const string& a : ao.annotations()) {
34  if (a.empty()) continue;
35  if (a == "Type") continue;
37  os << a << "=" << ao.annotation(a) << "\n";
38  }
39  }
40 
41 
42  void WriterFLAT::writeCounter(std::ostream& os, const Counter& c) {
43  ios_base::fmtflags oldflags = os.flags();
44  os << scientific << showpoint << setprecision(_precision);
45 
46  os << "# BEGIN COUNTER " << c.path() << "\n";
47  _writeAnnotations(os, c);
48  os << "# value\t error\n";
49  os << c.val() << "\t" << c.err() << "\n";
50  os << "# END COUNTER\n\n";
51 
52  os << flush;
53  os.flags(oldflags);
54  }
55 
56 
57  void WriterFLAT::writeHisto1D(std::ostream& os, const Histo1D& h) {
58  Scatter2D tmp = mkScatter(h);
59  tmp.setAnnotation("Type", "Histo1D");
60  writeScatter2D(os, tmp);
61  }
62 
63 
64  void WriterFLAT::writeHisto2D(std::ostream& os, const Histo2D& h) {
65  Scatter3D tmp = mkScatter(h);
66  tmp.setAnnotation("Type", "Histo2D");
67  writeScatter3D(os, tmp);
68  }
69 
70 
71  void WriterFLAT::writeProfile1D(std::ostream& os, const Profile1D& p) {
72  Scatter2D tmp = mkScatter(p);
73  tmp.setAnnotation("Type", "Profile1D");
74  writeScatter2D(os, tmp);
75  }
76 
77 
78  void WriterFLAT::writeProfile2D(std::ostream& os, const Profile2D& h) {
79  Scatter3D tmp = mkScatter(h);
80  tmp.setAnnotation("Type", "Profile2D");
81  writeScatter3D(os, tmp);
82  }
83 
84 
85 
86 
87  void WriterFLAT::writeScatter1D(std::ostream& os, const Scatter1D& s) {
88  ios_base::fmtflags oldflags = os.flags();
89  os << scientific << showpoint << setprecision(_precision);
90 
91  os << "# BEGIN VALUE " << s.path() << "\n";
92  _writeAnnotations(os, s);
93  os << "# value\t errminus\t errplus\n";
94  for (const Point1D& pt : s.points()) {
95  os << pt.x() << "\t" << pt.xErrMinus() << "\t" << pt.xErrPlus() << "\n";
96  }
97  os << "# END VALUE\n\n";
98 
99  os << flush;
100  os.flags(oldflags);
101  }
102 
103 
104  void WriterFLAT::writeScatter2D(std::ostream& os, const Scatter2D& s) {
105  ios_base::fmtflags oldflags = os.flags();
106  os << scientific << showpoint << setprecision(_precision);
107 
108  os << "# BEGIN HISTO1D " << s.path() << "\n";
109  _writeAnnotations(os, s);
110  os << "# xlow\t xhigh\t val\t errminus\t errplus\n";
111  for (const Point2D& pt : s.points()) {
112  os << pt.x()-pt.xErrMinus() << "\t" << pt.x()+pt.xErrPlus() << "\t";
113  os << pt.y() << "\t" << pt.yErrMinus() << "\t" << pt.yErrPlus() << "\n";
114  }
115  os << "# END HISTO1D\n\n";
116 
117  os << flush;
118  os.flags(oldflags);
119  }
120 
121 
122  void WriterFLAT::writeScatter3D(std::ostream& os, const Scatter3D& s) { // , bool asHist2D) {
123  ios_base::fmtflags oldflags = os.flags();
124  os << scientific << showpoint << setprecision(_precision);
125 
126  os << "# BEGIN HISTO2D " << s.path() << "\n";
127  _writeAnnotations(os, s);
128 
129  // if (asHist2D) { // Extension of what writeScatter2D does
130  os << "# xlow\t xhigh\t ylow\t yhigh\t val\t errminus\t errplus\n";
131  for (const Point3D& pt : s.points()) {
132  os << pt.x()-pt.xErrMinus() << "\t" << pt.x()+pt.xErrPlus() << "\t";
133  os << pt.y()-pt.yErrMinus() << "\t" << pt.y()+pt.yErrPlus() << "\t";
134  os << pt.z() << "\t" << pt.zErrMinus() << "\t" << pt.zErrPlus() << "\n";
135  }
136 
137  // } else { // What writerYODA should do... let's just put this in there (generalised for multiple errs).
138  // os << "# xval\t xerr-\t xerr+\t yval\t yerr-\t yerr+\t zval\t zerr-\t zerr+\n";
139  // for (const Point3D& pt : s.points()) {
140  // os << pt.x() << "\t" << pt.xErrMinus() << "\t" << pt.xErrMinus() << "\t";
141  // os << pt.y() << "\t" << pt.yErrMinus() << "\t" << pt.yErrMinus() << "\t";
142  // os << pt.z() << "\t" << pt.zErrMinus() << "\t" << pt.zErrMinus() << "\n";
143  // }
144  // }
145 
146  os << "# END HISTO2D\n\n";
147 
148  os << flush;
149  os.flags(oldflags);
150  }
151 
152 
153 }
const std::string path() const
Get the AO path.
A very generic data type which is just a collection of 2D data points with errors.
Definition: Scatter2D.h:24
STL namespace.
A weighted counter.
Definition: Counter.h:23
A one-dimensional histogram.
Definition: Histo1D.h:26
Persistency writer for flat text format.
Definition: WriterFLAT.h:16
A two-dimensional profile histogram.
Definition: Profile2D.h:31
A 3D data point to be contained in a Scatter3D.
Definition: Point3D.h:18
A 1D data point to be contained in a Scatter1D.
Definition: Point1D.h:18
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
Scatter1D mkScatter(const Counter &c)
Make a Scatter1D representation of a Histo1D.
Definition: Scatter1D.cc:9
Pure virtual base class for various output writers.
Definition: Writer.h:28
void setPrecision(int precision)
Set precision of numerical quantities in this writer&#39;s output.
Definition: Writer.h:154
double err() const
Definition: Counter.h:140
A very generic data type which is just a collection of 3D data points with errors.
Definition: Scatter3D.h:24
double val() const
Get the value.
Definition: Counter.h:135
AnalysisObject is the base class for histograms and scatters.
A very generic data type which is just a collection of 1D data points with errors.
Definition: Scatter1D.h:23
Points & points()
Get the collection of points (non-const)
Definition: Scatter1D.h:157
Points & points()
Get the collection of points (non-const)
Definition: Scatter3D.h:202
A two-dimensional histogram.
Definition: Histo2D.h:31
A one-dimensional profile histogram.
Definition: Profile1D.h:33
void setAnnotation(const std::string &name, const std::string &value)
Add or set a string-valued annotation by name.
std::vector< std::string > annotations() const
Points & points()
Get the collection of points (non-const)
Definition: Scatter2D.h:185
A 2D data point to be contained in a Scatter2D.
Definition: Point2D.h:18