yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.0.0
WriterYODA.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-2022 The YODA collaboration (see AUTHORS for details)
5//
6#include "YODA/WriterYODA.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 WriterYODA _instance;
23 _instance.setPrecision(6);
24 return _instance;
25 }
26
27
33 static const int YODA_FORMAT_VERSION = 3;
34
35
36 // Version-formatting helper function
37 inline string _iotypestr(const string& baseiotype) {
38 ostringstream os;
39 os << "YODA_" << Utils::toUpper(baseiotype) << "_V" << YODA_FORMAT_VERSION;
40 return os.str();
41 }
42
43
44 void WriterYODA::_writeAnnotations(std::ostream& os, const AnalysisObject& ao) {
45 os << scientific << setprecision(_aoprecision);
46 for (const string& a : ao.annotations()) {
47 if (a.empty()) continue;
49 string ann = ao.annotation(a);
50 // remove spurious line returns at the end of a string so that we don't
51 // end up with two line returns.
52 ann.erase(std::remove(ann.begin(), ann.end(), '\n'), ann.end());
53 os << a << ": " << ann << "\n";
54 }
55 os << "---\n";
56 }
57
58
59 void WriterYODA::writeAO(std::ostream& os, const AnalysisObject& ao) {
60 ios_base::fmtflags oldflags = os.flags();
61 os << scientific << showpoint << setprecision(_aoprecision);
62 os << "BEGIN " << _iotypestr(ao.type()) << " " << ao.path() << "\n";
63 _writeAnnotations(os, ao);
64 ao._renderYODA(os, _aoprecision+7); // = "-1." + _aoprecision + "e+23"
65 os << "END " << _iotypestr(ao.type()) << "\n\n";
66 os << flush;
67 os.flags(oldflags);
68 }
69
70}
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.
Persistency writer for YODA flat text format.
Definition WriterYODA.h:15
void writeAO(std::ostream &stream, const AnalysisObject &c)
Definition WriterYODA.cc:59
static Writer & create()
Singleton creation function.
Definition WriterYODA.cc:21
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.
static const int YODA_FORMAT_VERSION
Definition WriterYODA.cc:33