yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.3
WriterYODA1.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-2024 The YODA collaboration (see AUTHORS for details)
5//
6#include "YODA/WriterYODA1.h"
7
8#include "yaml-cpp/yaml.h"
9#ifdef YAML_NAMESPACE
10#define YAML YAML_NAMESPACE
11#endif
12
13#include <iostream>
14#include <iomanip>
15using namespace std;
16
17namespace YODA {
18
19
22 static WriterYODA1 _instance;
23 _instance.setPrecision(6);
24 return _instance;
25 }
26
27
28 void WriterYODA1::_writeAnnotations(std::ostream& os, const AnalysisObject& ao) {
29 os << scientific << setprecision(_aoprecision);
30 for (const string& a : ao.annotations()) {
31 if (a.empty()) continue;
33 string ann = ao.annotation(a);
34 // remove stpurious line returns at the end of a string so that we don't
35 // end up with two line returns.
36 ann.erase(std::remove(ann.begin(), ann.end(), '\n'), ann.end());
37 os << a << ": " << ann << "\n";
38 }
39 os << "---\n";
40 }
41
42 void WriterYODA1::writeAO(std::ostream& stream, const AnalysisObject& ao) {
43 const string aotype = ao.type();
44 if (aotype == "Counter") {
45 writeCounter(stream, dynamic_cast<const Counter&>(ao));
46 } else if (aotype == "Histo1D") {
47 writeHisto1D(stream, dynamic_cast<const Histo1D&>(ao));
48 } else if (aotype == "Histo2D") {
49 writeHisto2D(stream, dynamic_cast<const Histo2D&>(ao));
50 } else if (aotype == "Profile1D") {
51 writeProfile1D(stream, dynamic_cast<const Profile1D&>(ao));
52 } else if (aotype == "Profile2D") {
53 writeProfile2D(stream, dynamic_cast<const Profile2D&>(ao));
54 } else if (aotype == "Scatter1D") {
55 writeScatter1D(stream, dynamic_cast<const Scatter1D&>(ao));
56 } else if (aotype == "Scatter2D") {
57 writeScatter2D(stream, dynamic_cast<const Scatter2D&>(ao));
58 } else if (aotype == "Scatter3D") {
59 writeScatter3D(stream, dynamic_cast<const Scatter3D&>(ao));
60 } else {
61 stream << "# Type " << aotype << " not supported in the legacy writer.\n";
62 // Skip writing other AO types since they were not supported in YODA1.
63 }
64 }
65
66
67 void WriterYODA1::writeCounter(std::ostream& os, const Counter& c) {
68 ios_base::fmtflags oldflags = os.flags();
69 os << scientific << showpoint << setprecision(_aoprecision);
70
71 os << "BEGIN YODA_" << Utils::toUpper("COUNTER") << "_V2 " << c.path() << "\n";
72 _writeAnnotations(os, c);
73 os << "# sumW\t sumW2\t numEntries\n";
74 os << c.sumW() << "\t" << c.sumW2() << "\t" << c.numEntries() << "\n";
75 os << "END YODA_" << Utils::toUpper("COUNTER") << "_V2\n\n";
76
77 os.flags(oldflags);
78 }
79
80
81 void WriterYODA1::writeHisto1D(std::ostream& os, const Histo1D& h) {
82 ios_base::fmtflags oldflags = os.flags();
83 os << scientific << showpoint << setprecision(_aoprecision);
84
85 os << "BEGIN YODA_" << Utils::toUpper("HISTO1D") << "_V2 " << h.path() << "\n";
86 _writeAnnotations(os, h);
87 os << "# Mean: " << h.xMean() << "\n";
88 os << "# Area: " << h.integral() << "\n";
89 os << "# ID\t ID\t sumw\t sumw2\t sumwx\t sumwx2\t numEntries\n";
90 os << "Total \tTotal \t";
91 os << h.sumW() << "\t" << h.sumW2() << "\t";
92 os << h.sumWA(0) << "\t" << h.sumWA2(0) << "\t";
93 os << h.numEntries() << "\n";
94 os << "Underflow\tUnderflow\t";
95 os << h.bin(0).sumW() << "\t" << h.bin(0).sumW2() << "\t";
96 os << h.bin(0).sumWX() << "\t" << h.bin(0).sumWX2() << "\t";
97 os << h.bin(0).numEntries() << "\n";
98 os << "Overflow\tOverflow\t";
99 os << h.bin(h.numBins()+1).sumW() << "\t" << h.bin(h.numBins()+1).sumW2() << "\t";
100 os << h.bin(h.numBins()+1).sumWX() << "\t" << h.bin(h.numBins()+1).sumWX2() << "\t";
101 os << h.bin(h.numBins()+1).numEntries() << "\n";
102 os << "# xlow\t xhigh\t sumw\t sumw2\t sumwx\t sumwx2\t numEntries\n";
103 for (const auto& b : h.bins()) {
104 os << b.xMin() << "\t" << b.xMax() << "\t";
105 os << b.sumW() << "\t" << b.sumW2() << "\t";
106 os << b.sumWX() << "\t" << b.sumWX2() << "\t";
107 os << b.numEntries() << "\n";
108 }
109 os << "END YODA_" << Utils::toUpper("HISTO1D") << "_V2\n\n";
110
111 os.flags(oldflags);
112 }
113
114
115 void WriterYODA1::writeHisto2D(std::ostream& os, const Histo2D& h) {
116 ios_base::fmtflags oldflags = os.flags();
117 os << scientific << showpoint << setprecision(_aoprecision);
118 os << "BEGIN YODA_" << Utils::toUpper("HISTO2D") << "_V2 " << h.path() << "\n";
119 _writeAnnotations(os, h);
120 //if ( h.totalDbn().numEntries() > 0 )
121 os << "# Mean: (" << h.xMean() << ", " << h.yMean() << ")\n";
122 os << "# Volume: " << h.integral() << "\n";
123 os << "# ID\t ID\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t sumwxy\t numEntries\n";
124 // Total distribution
125 os << "Total \tTotal \t";
126 os << h.sumW() << "\t" << h.sumW2() << "\t";
127 os << h.sumWA(0) << "\t" << h.sumWA2(0) << "\t";
128 os << h.sumWA(1) << "\t" << h.sumWA2(1) << "\t";
129 os << h.crossTerm(0,1) << "\t";
130 os << h.numEntries() << "\n";
131 // Outflows
133 os << "# 2D outflow persistency not currently supported until API is stable\n";
134 // Bins
135 os << "# xlow\t xhigh\t ylow\t yhigh\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t sumwxy\t numEntries\n";
136 for (const auto& b : h.bins()) {
137 os << b.xMin() << "\t" << b.xMax() << "\t";
138 os << b.yMin() << "\t" << b.yMax() << "\t";
139 os << b.sumW() << "\t" << b.sumW2() << "\t";
140 os << b.sumWX() << "\t" << b.sumWX2() << "\t";
141 os << b.sumWY() << "\t" << b.sumWY2() << "\t";
142 os << b.sumWXY() << "\t";
143 os << b.numEntries() << "\n";
144 }
145 os << "END YODA_" << Utils::toUpper("HISTO2D") << "_V2\n\n";
146
147 os.flags(oldflags);
148 }
149
150
151 void WriterYODA1::writeProfile1D(std::ostream& os, const Profile1D& p) {
152 ios_base::fmtflags oldflags = os.flags();
153 os << scientific << showpoint << setprecision(_aoprecision);
154
155 os << "BEGIN YODA_" << Utils::toUpper("PROFILE1D") << "_V2 " << p.path() << "\n";
156 _writeAnnotations(os, p);
157 os << "# ID\t ID\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t numEntries\n";
158 os << "Total \tTotal \t";
159 os << p.sumW() << "\t" << p.sumW2() << "\t";
160 os << p.sumWA(0) << "\t" << p.sumWA2(0) << "\t";
161 os << p.sumWA(1) << "\t" << p.sumWA2(1) << "\t";
162 os << p.numEntries() << "\n";
163 os << "Underflow\tUnderflow\t";
164 os << p.bin(0).sumW() << "\t" << p.bin(0).sumW2() << "\t";
165 os << p.bin(0).sumWX() << "\t" << p.bin(0).sumWX2() << "\t";
166 os << p.bin(0).sumWY() << "\t" << p.bin(0).sumWY2() << "\t";
167 os << p.bin(0).numEntries() << "\n";
168 os << "Overflow\tOverflow\t";
169 os << p.bin(p.numBins()+1).sumW() << "\t" << p.bin(p.numBins()+1).sumW2() << "\t";
170 os << p.bin(p.numBins()+1).sumWX() << "\t" << p.bin(p.numBins()+1).sumWX2() << "\t";
171 os << p.bin(p.numBins()+1).sumWY() << "\t" << p.bin(p.numBins()+1).sumWY2() << "\t";
172 os << p.bin(p.numBins()+1).numEntries() << "\n";
173 os << "# xlow\t xhigh\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t numEntries\n";
174 for (const auto& b : p.bins()) {
175 os << b.xMin() << "\t" << b.xMax() << "\t";
176 os << b.sumW() << "\t" << b.sumW2() << "\t";
177 os << b.sumWX() << "\t" << b.sumWX2() << "\t";
178 os << b.sumWY() << "\t" << b.sumWY2() << "\t";
179 os << b.numEntries() << "\n";
180 }
181 os << "END YODA_" << Utils::toUpper("PROFILE1D") << "_V2\n\n";
182
183 os.flags(oldflags);
184 }
185
186
187 void WriterYODA1::writeProfile2D(std::ostream& os, const Profile2D& p) {
188 ios_base::fmtflags oldflags = os.flags();
189 os << scientific << showpoint << setprecision(_aoprecision);
190
191 os << "BEGIN YODA_" << Utils::toUpper("PROFILE2D") << "_V2 " << p.path() << "\n";
192 _writeAnnotations(os, p);
193 os << "# sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t sumwz\t sumwz2\t sumwxy\t numEntries\n";
194 // Total distribution
195 os << "Total \tTotal \t";
196 os << p.sumW() << "\t" << p.sumW2() << "\t";
197 os << p.sumWA(0) << "\t" << p.sumWA2(0) << "\t";
198 os << p.sumWA(1) << "\t" << p.sumWA2(1) << "\t";
199 os << p.sumWA(2) << "\t" << p.sumWA2(2) << "\t";
200 os << p.crossTerm(0,1) << "\t"; // << td.sumWXZ() << "\t" << td.sumWYZ() << "\t";
201 os << p.numEntries() << "\n";
202 // Outflows
204 os << "# 2D outflow persistency not currently supported until API is stable\n";
205 // Bins
206 os << "# xlow\t xhigh\t ylow\t yhigh\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t sumwz\t sumwz2\t sumwxy\t numEntries\n";
207 for (const auto& b : p.bins()) {
208 os << b.xMin() << "\t" << b.xMax() << "\t";
209 os << b.yMin() << "\t" << b.yMax() << "\t";
210 os << b.sumW() << "\t" << b.sumW2() << "\t";
211 os << b.sumWX() << "\t" << b.sumWX2() << "\t";
212 os << b.sumWY() << "\t" << b.sumWY2() << "\t";
213 os << b.sumWZ() << "\t" << b.sumWZ2() << "\t";
214 os << b.sumWXY() << "\t"; // << b.sumWXZ() << "\t" << b.sumWYZ() << "\t";
215 os << b.numEntries() << "\n";
216 }
217 os << "END YODA_" << Utils::toUpper("PROFILE2D") << "_V2\n\n";
218
219 os.flags(oldflags);
220 }
221
222
223 void WriterYODA1::writeScatter1D(std::ostream& os, const Scatter1D& s) {
224 ios_base::fmtflags oldflags = os.flags();
225 os << scientific << showpoint << setprecision(_aoprecision);
226
227 // we promised not to modify const s, but we want to add an annotation
228 // we did not promise to not modify the *clone* of s...
229 auto sclone = s.clone();
230
231 os << "BEGIN YODA_" << Utils::toUpper("SCATTER1D") << "_V2 " << s.path() << "\n";
232 _writeAnnotations(os, sclone);
233
234 //write headers
235 std::string headers="# xval\t xerr-\t xerr+\t";
236 os << headers << "\n";
237
238 //write points
239 for (const Point1D& pt : s.points()) {
240 // fill central value
241 os << pt.x() << "\t" << pt.xErrMinus() << "\t" << pt.xErrPlus() ;
242 os << "\n";
243 }
244 os << "END YODA_" << Utils::toUpper("SCATTER1D") << "_V2\n\n";
245
246 os << flush;
247 os.flags(oldflags);
248 }
249
250
251 void WriterYODA1::writeScatter2D(std::ostream& os, const Scatter2D& s) {
252 ios_base::fmtflags oldflags = os.flags();
253 os << scientific << showpoint << setprecision(_aoprecision);
254 os << "BEGIN YODA_" << Utils::toUpper("SCATTER2D") << "_V2 " << s.path() << "\n";
255
256 // Write annotations.
257 // We promised not to modify const s, but we want to add an annotation;
258 // We did not promise to not modify the *clone* of s;
259 // Judge not, lest ye be judged
260 auto sclone = s.clone();
261 _writeAnnotations(os, sclone);
262
263 //write headers
265 std::string headers="# xval\t xerr-\t xerr+\t yval\t yerr-\t yerr+\t";
266 os << headers << "\n";
267
268 //write points
269 for (const Point2D& pt : s.points()) {
271 // fill central value
272 os << pt.x() << "\t" << pt.xErrMinus() << "\t" << pt.xErrPlus() << "\t";
273 os << pt.y() << "\t" << pt.yErrMinus() << "\t" << pt.yErrPlus() ;
274 os << "\n";
275 }
276 os << "END YODA_" << Utils::toUpper("SCATTER2D") << "_V2\n\n";
277
278 os << flush;
279 os.flags(oldflags);
280 }
281
282
283 void WriterYODA1::writeScatter3D(std::ostream& os, const Scatter3D& s) {
284 ios_base::fmtflags oldflags = os.flags();
285 os << scientific << showpoint << setprecision(_aoprecision);
286 os << "BEGIN YODA_" << Utils::toUpper("SCATTER3D") << "_V2 " << s.path() << "\n";
287
288 // write annotations
289 // we promised not to modify const s, but we want to add an annotation
290 // we did not promise to not modify the *clone* of s...
291 auto sclone = s.clone();
292 _writeAnnotations(os, sclone);
293
294 //write headers
296 std::string headers="# xval\t xerr-\t xerr+\t yval\t yerr-\t yerr+\t zval\t zerr-\t zerr+\t";
297 os << headers << "\n";
298
299 //write points
300 for (const Point3D& pt : s.points()) {
302 // fill central value
303 os << pt.x() << "\t" << pt.xErrMinus() << "\t" << pt.xErrPlus() << "\t";
304 os << pt.y() << "\t" << pt.yErrMinus() << "\t" << pt.yErrPlus() << "\t";
305 os << pt.z() << "\t" << pt.zErrMinus() << "\t" << pt.zErrPlus() ;
306 os << "\n";
307 }
308 os << "END YODA_" << Utils::toUpper("SCATTER3D") << "_V2\n\n";
309
310 os << flush;
311 os.flags(oldflags);
312 }
313
314
315}
AnalysisObject is the base class for histograms and scatters.
virtual std::string type() const
Get name of the analysis object type.
const std::string path() const
Get the AO path.
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
User-facing BinnedDbn class in arbitrary dimension.
Definition BinnedDbn.h:50
BinT & bin(size_t idx) noexcept
Returns reference to the bin at idx.
size_t numBins(const bool includeOverflows=false, const bool includeMaskedBins=false) const noexcept
Number of bins in the BinnedStorage.
BinsVecWrapper< BinsVecT > bins(const bool includeOverflows=false, const bool includeMaskedBins=false) noexcept
Returns bins vector wrapper, which skips masked elements when iterated over.
A weighted counter.
Definition Counter.h:26
double sumW2(bool=false) const
Get the sum of squared weights.
Definition Counter.h:186
double numEntries(bool=false) const
Get the number of fills.
Definition Counter.h:177
double sumW(bool=false) const
Get the sum of weights.
Definition Counter.h:183
double sumWA(const size_t dim, const bool includeOverflows=true) const
Calculates first moment along axis dim in histo.
Definition BinnedDbn.h:538
double sumWA2(const size_t dim, const bool includeOverflows=true) const
Calculates second moment along axis dim in histo.
Definition BinnedDbn.h:547
double numEntries(const bool includeOverflows=true) const noexcept
Get the number of fills (fractional fills are possible).
Definition BinnedDbn.h:506
double crossTerm(const size_t A1, const size_t A2, const bool includeOverflows=true) const
Calculates cross-term along axes A1 and A2 in histo.
Definition BinnedDbn.h:557
double sumW(const bool includeOverflows=true) const noexcept
Calculates sum of weights in histo.
Definition BinnedDbn.h:522
double sumW2(const bool includeOverflows=true) const noexcept
Calculates sum of squared weights in histo.
Definition BinnedDbn.h:530
double integral(const bool includeOverflows=true) const noexcept
Get the total volume of the histogram.
Definition BinnedDbn.h:463
A 1D data point to be contained in a Scatter1D.
Definition Point.h:555
A 2D data point to be contained in a Scatter2D.
Definition Point.h:607
A 3D data point to be contained in a Scatter3D.
Definition Point.h:662
A generic data type which is just a collection of n-dim data points with errors.
Definition Scatter.h:154
ScatterND< N > clone() const
Make a copy on the stack.
Definition Scatter.h:284
Points & points()
Get the collection of points.
Definition Scatter.h:350
Persistency writer for YODA flat text format.
Definition WriterYODA1.h:20
void writeScatter1D(std::ostream &stream, const Scatter1D &s)
void writeAO(std::ostream &stream, const AnalysisObject &c)
void writeCounter(std::ostream &stream, const Counter &c)
void writeScatter3D(std::ostream &stream, const Scatter3D &s)
void writeScatter2D(std::ostream &stream, const Scatter2D &s)
void writeHisto1D(std::ostream &stream, const Histo1D &h)
void writeProfile1D(std::ostream &stream, const Profile1D &p)
static Writer & create()
Singleton creation function.
void writeProfile2D(std::ostream &stream, const Profile2D &p)
void writeHisto2D(std::ostream &stream, const Histo2D &h)
Pure virtual base class for various output writers.
Definition Writer.h:19
void setPrecision(int precision)
Set precision of numerical quantities in this writer's output.
Definition Writer.h:143
Anonymous namespace to limit visibility.