8#include "yaml-cpp/yaml.h"
10#define YAML YAML_NAMESPACE
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;
36 ann.erase(std::remove(ann.begin(), ann.end(),
'\n'), ann.end());
37 os << a <<
": " << ann <<
"\n";
43 const string aotype = ao.
type();
44 if (aotype ==
"Counter") {
46 }
else if (aotype ==
"Histo1D") {
48 }
else if (aotype ==
"Histo2D") {
50 }
else if (aotype ==
"Profile1D") {
52 }
else if (aotype ==
"Profile2D") {
54 }
else if (aotype ==
"Scatter1D") {
56 }
else if (aotype ==
"Scatter2D") {
58 }
else if (aotype ==
"Scatter3D") {
61 stream <<
"# Type " << aotype <<
" not supported in the legacy writer.\n";
68 ios_base::fmtflags oldflags = os.flags();
69 os << scientific << showpoint << setprecision(_aoprecision);
71 os <<
"BEGIN YODA_" << Utils::toUpper(
"COUNTER") <<
"_V2 " << c.
path() <<
"\n";
72 _writeAnnotations(os, c);
73 os <<
"# sumW\t sumW2\t numEntries\n";
75 os <<
"END YODA_" << Utils::toUpper(
"COUNTER") <<
"_V2\n\n";
82 ios_base::fmtflags oldflags = os.flags();
83 os << scientific << showpoint << setprecision(_aoprecision);
85 os <<
"BEGIN YODA_" << Utils::toUpper(
"HISTO1D") <<
"_V2 " << h.
path() <<
"\n";
86 _writeAnnotations(os, h);
88 os <<
"# Mean: " << h.xMean() <<
"\n";
89 os <<
"# Area: " << h.
integral() <<
"\n";
93 os <<
"# ID\t ID\t sumw\t sumw2\t sumwx\t sumwx2\t numEntries\n";
94 os <<
"Total \tTotal \t";
95 os << h.
sumW() <<
"\t" << h.
sumW2() <<
"\t";
98 os <<
"Underflow\tUnderflow\t";
99 os << h.
bin(0).sumW() <<
"\t" << h.
bin(0).sumW2() <<
"\t";
100 os << h.
bin(0).sumWX() <<
"\t" << h.
bin(0).sumWX2() <<
"\t";
101 os << h.
bin(0).numEntries() <<
"\n";
102 os <<
"Overflow\tOverflow\t";
105 os << h.
bin(h.
numBins()+1).numEntries() <<
"\n";
106 os <<
"# xlow\t xhigh\t sumw\t sumw2\t sumwx\t sumwx2\t numEntries\n";
107 for (
const auto& b : h.
bins()) {
108 os << b.xMin() <<
"\t" << b.xMax() <<
"\t";
109 os << b.sumW() <<
"\t" << b.sumW2() <<
"\t";
110 os << b.sumWX() <<
"\t" << b.sumWX2() <<
"\t";
111 os << b.numEntries() <<
"\n";
113 os <<
"END YODA_" << Utils::toUpper(
"HISTO1D") <<
"_V2\n\n";
120 ios_base::fmtflags oldflags = os.flags();
121 os << scientific << showpoint << setprecision(_aoprecision);
122 os <<
"BEGIN YODA_" << Utils::toUpper(
"HISTO2D") <<
"_V2 " << h.
path() <<
"\n";
123 _writeAnnotations(os, h);
126 os <<
"# Mean: (" << h.xMean() <<
", " << h.yMean() <<
")\n";
127 os <<
"# Volume: " << h.
integral() <<
"\n";
131 os <<
"# ID\t ID\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t sumwxy\t numEntries\n";
133 os <<
"Total \tTotal \t";
134 os << h.
sumW() <<
"\t" << h.
sumW2() <<
"\t";
141 os <<
"# 2D outflow persistency not currently supported until API is stable\n";
143 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";
144 for (
const auto& b : h.
bins()) {
145 os << b.xMin() <<
"\t" << b.xMax() <<
"\t";
146 os << b.yMin() <<
"\t" << b.yMax() <<
"\t";
147 os << b.sumW() <<
"\t" << b.sumW2() <<
"\t";
148 os << b.sumWX() <<
"\t" << b.sumWX2() <<
"\t";
149 os << b.sumWY() <<
"\t" << b.sumWY2() <<
"\t";
150 os << b.sumWXY() <<
"\t";
151 os << b.numEntries() <<
"\n";
153 os <<
"END YODA_" << Utils::toUpper(
"HISTO2D") <<
"_V2\n\n";
160 ios_base::fmtflags oldflags = os.flags();
161 os << scientific << showpoint << setprecision(_aoprecision);
163 os <<
"BEGIN YODA_" << Utils::toUpper(
"PROFILE1D") <<
"_V2 " << p.
path() <<
"\n";
164 _writeAnnotations(os, p);
165 os <<
"# ID\t ID\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t numEntries\n";
166 os <<
"Total \tTotal \t";
167 os << p.
sumW() <<
"\t" << p.
sumW2() <<
"\t";
171 os <<
"Underflow\tUnderflow\t";
172 os << p.
bin(0).sumW() <<
"\t" << p.
bin(0).sumW2() <<
"\t";
173 os << p.
bin(0).sumWX() <<
"\t" << p.
bin(0).sumWX2() <<
"\t";
174 os << p.
bin(0).sumWY() <<
"\t" << p.
bin(0).sumWY2() <<
"\t";
175 os << p.
bin(0).numEntries() <<
"\n";
176 os <<
"Overflow\tOverflow\t";
180 os << p.
bin(p.
numBins()+1).numEntries() <<
"\n";
181 os <<
"# xlow\t xhigh\t sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t numEntries\n";
182 for (
const auto& b : p.
bins()) {
183 os << b.xMin() <<
"\t" << b.xMax() <<
"\t";
184 os << b.sumW() <<
"\t" << b.sumW2() <<
"\t";
185 os << b.sumWX() <<
"\t" << b.sumWX2() <<
"\t";
186 os << b.sumWY() <<
"\t" << b.sumWY2() <<
"\t";
187 os << b.numEntries() <<
"\n";
189 os <<
"END YODA_" << Utils::toUpper(
"PROFILE1D") <<
"_V2\n\n";
196 ios_base::fmtflags oldflags = os.flags();
197 os << scientific << showpoint << setprecision(_aoprecision);
199 os <<
"BEGIN YODA_" << Utils::toUpper(
"PROFILE2D") <<
"_V2 " << p.
path() <<
"\n";
200 _writeAnnotations(os, p);
201 os <<
"# sumw\t sumw2\t sumwx\t sumwx2\t sumwy\t sumwy2\t sumwz\t sumwz2\t sumwxy\t numEntries\n";
203 os <<
"Total \tTotal \t";
204 os << p.
sumW() <<
"\t" << p.
sumW2() <<
"\t";
212 os <<
"# 2D outflow persistency not currently supported until API is stable\n";
214 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";
215 for (
const auto& b : p.
bins()) {
216 os << b.xMin() <<
"\t" << b.xMax() <<
"\t";
217 os << b.yMin() <<
"\t" << b.yMax() <<
"\t";
218 os << b.sumW() <<
"\t" << b.sumW2() <<
"\t";
219 os << b.sumWX() <<
"\t" << b.sumWX2() <<
"\t";
220 os << b.sumWY() <<
"\t" << b.sumWY2() <<
"\t";
221 os << b.sumWZ() <<
"\t" << b.sumWZ2() <<
"\t";
222 os << b.sumWXY() <<
"\t";
223 os << b.numEntries() <<
"\n";
225 os <<
"END YODA_" << Utils::toUpper(
"PROFILE2D") <<
"_V2\n\n";
232 ios_base::fmtflags oldflags = os.flags();
233 os << scientific << showpoint << setprecision(_aoprecision);
237 auto sclone = s.
clone();
239 os <<
"BEGIN YODA_" << Utils::toUpper(
"SCATTER1D") <<
"_V2 " << s.
path() <<
"\n";
240 _writeAnnotations(os, sclone);
243 std::string headers=
"# xval\t xerr-\t xerr+\t";
244 os << headers <<
"\n";
249 os << pt.x() <<
"\t" << pt.xErrMinus() <<
"\t" << pt.xErrPlus() ;
252 os <<
"END YODA_" << Utils::toUpper(
"SCATTER1D") <<
"_V2\n\n";
260 ios_base::fmtflags oldflags = os.flags();
261 os << scientific << showpoint << setprecision(_aoprecision);
262 os <<
"BEGIN YODA_" << Utils::toUpper(
"SCATTER2D") <<
"_V2 " << s.
path() <<
"\n";
268 auto sclone = s.
clone();
269 _writeAnnotations(os, sclone);
273 std::string headers=
"# xval\t xerr-\t xerr+\t yval\t yerr-\t yerr+\t";
274 os << headers <<
"\n";
280 os << pt.x() <<
"\t" << pt.xErrMinus() <<
"\t" << pt.xErrPlus() <<
"\t";
281 os << pt.y() <<
"\t" << pt.yErrMinus() <<
"\t" << pt.yErrPlus() ;
284 os <<
"END YODA_" << Utils::toUpper(
"SCATTER2D") <<
"_V2\n\n";
292 ios_base::fmtflags oldflags = os.flags();
293 os << scientific << showpoint << setprecision(_aoprecision);
294 os <<
"BEGIN YODA_" << Utils::toUpper(
"SCATTER3D") <<
"_V2 " << s.
path() <<
"\n";
299 auto sclone = s.
clone();
300 _writeAnnotations(os, sclone);
304 std::string headers=
"# xval\t xerr-\t xerr+\t yval\t yerr-\t yerr+\t zval\t zerr-\t zerr+\t";
305 os << headers <<
"\n";
311 os << pt.x() <<
"\t" << pt.xErrMinus() <<
"\t" << pt.xErrPlus() <<
"\t";
312 os << pt.y() <<
"\t" << pt.yErrMinus() <<
"\t" << pt.yErrPlus() <<
"\t";
313 os << pt.z() <<
"\t" << pt.zErrMinus() <<
"\t" << pt.zErrPlus() ;
316 os <<
"END YODA_" << Utils::toUpper(
"SCATTER3D") <<
"_V2\n\n";
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.
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.
double sumW2(bool=false) const
Get the sum of squared weights.
double numEntries(bool=false) const
Get the number of fills.
double sumW(bool=false) const
Get the sum of weights.
double sumWA(const size_t dim, const bool includeOverflows=true) const
Calculates first moment along axis dim in histo.
double sumWA2(const size_t dim, const bool includeOverflows=true) const
Calculates second moment along axis dim in histo.
double numEntries(const bool includeOverflows=true) const noexcept
Get the number of fills (fractional fills are possible).
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.
double sumW(const bool includeOverflows=true) const noexcept
Calculates sum of weights in histo.
double sumW2(const bool includeOverflows=true) const noexcept
Calculates sum of squared weights in histo.
double integral(const bool includeOverflows=true) const noexcept
Get the total volume of the histogram.
Errors relating to insufficient (effective) statistics.
A 1D data point to be contained in a Scatter1D.
A 2D data point to be contained in a Scatter2D.
A 3D data point to be contained in a Scatter3D.
A generic data type which is just a collection of n-dim data points with errors.
ScatterND< N > clone() const
Make a copy on the stack.
Points & points()
Get the collection of points.
Persistency writer for YODA flat text format.
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.
void setPrecision(int precision)
Set precision of numerical quantities in this writer's output.
Anonymous namespace to limit visibility.