yoda is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis 2.1.0
WriterH5.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/WriterH5.h"
8
9#include <string>
10
11using namespace std;
12
13namespace YODA {
14
15
18 static WriterH5 _instance;
19 _instance.setPrecision(6); //< not used
20 return _instance;
21 }
22
23
27 static const size_t YODA_H5_FORMAT_VERSION = 1;
28
29
30 void WriterH5::writeAOS(YODA_H5::File& h5, const std::vector<const AnalysisObject*>& aos) {
31
32 // Work out the length of the concatenated serialized streams
33 size_t annolen = 0, datalen = 0;
34 vector<size_t> annosizes, datasizes, labelsizes;
35 annosizes.reserve(aos.size());
36 labelsizes.reserve(aos.size());
37 datasizes.reserve(aos.size()+1); //< +1 for # of AOs requiring error labels
38 vector<string> labels; // list of error labels
39 for (const AnalysisObject* ao : aos) {
40 annosizes.emplace_back(ao->lengthMeta() - 1); //< skip "Title" key
41 datasizes.emplace_back(ao->lengthContent());
42 annolen += annosizes.back();
43 datalen += datasizes.back();
44 // if the AO is an Estimate, extract the error labels
45 ao->_extractLabels(labels, labelsizes);
46 }
47 datasizes.emplace_back(labelsizes.size());
48 //size_t annochunk = annolen/aos.size();
49 //size_t datachunk = datalen/aos.size();
50 size_t annochunk = std::min(annolen, (size_t)1200);
51 size_t datachunk = std::min(datalen, (size_t)1500);
52
53 // create empty H5 DataSets
54 H5DataSetWriter<string> annoset(h5, "annotations", annolen, annochunk, _compress);
55 H5DataSetWriter<double> dataset(h5, "content", datalen, datachunk, _compress);
56
57 // Now that we know the sizes of all AOs,
58 // fill an uber-vector of serialized sub-vectors
59 // as well as a map of edge handlers for binned AOs
60 map<string, EdgeHandlerBasePtr> layout;
61 layout["sizeinfo"] = std::make_shared<EdgeHandler<size_t>>();
62 static_pointer_cast<EdgeHandler<size_t>>(layout["sizeinfo"])->extend(std::move(annosizes));
63 static_pointer_cast<EdgeHandler<size_t>>(layout["sizeinfo"])->extend(std::move(datasizes));
64 static_pointer_cast<EdgeHandler<size_t>>(layout["sizeinfo"])->extend(std::move(labelsizes));
65 vector<string> aoinfo; aoinfo.reserve(H5FileManager::AO_META*aos.size());
66 for (const AnalysisObject* ao : aos) {
67
68 // save metadata about this AO
69 aoinfo.emplace_back(ao->path());
70 aoinfo.emplace_back(Utils::toUpper(ao->type()));
71
72 // retrieve annotations for this AO
73 vector<string> annos = ao->serializeMeta(); //< skips Path and Title
74 annos.emplace_back(ao->title()); // title value should be at the end
75
76 // retrieve content for this AO
77 vector<double> content = ao->serializeContent();
78
79 // write to file
80 annoset.writeSlice(std::move(annos));
81 dataset.writeSlice(std::move(content));
82
83 // if the AO is a Fillable, extract its binning
84 ao->_extractEdges(layout, labels);
85 }
86
87 // write AO metadata
88 auto meta = H5DataSet(h5, "aoinfo", std::move(aoinfo), _compress);
89
90 // write layout and attribute for file-level metadata
91 meta.createAttribute("meta", vector<size_t>{YODA_H5_FORMAT_VERSION, aos.size()});
92
93 // write error source labels
94 (void)H5DataSet(h5, "labels", std::move(labels), _compress);
95
96 // write bin edges
97 for (const auto& item : layout) {
98 item.second->writeToFile(item.first, h5, _compress);
99 }
100
101 }
102
103}
AnalysisObject is the base class for histograms and scatters.
A helper class to deal with chunking of H5 datasets.
Definition H5Utils.h:158
void writeSlice(vector< T > &&data) noexcept
Method to write a slice that may span multiple rows/columns.
Definition H5Utils.h:168
static constexpr size_t AO_META
Definition H5Utils.h:276
Persistency writer for YODA H5 format.
Definition WriterH5.h:15
void writeAOS(YODA_H5::File &file, const std::vector< const AnalysisObject * > &aos)
Definition WriterH5.cc:30
static Writer & create()
Singleton creation function.
Definition WriterH5.cc:17
Pure virtual base class for various output writers.
Definition Writer.h:31
void setPrecision(int precision)
Set precision of numerical quantities in this writer's output.
Definition Writer.h:176
Anonymous namespace to limit visibility.
YODA_H5::DataSet H5DataSet(YODA_H5::File &h5file, const string &label, vector< T > &&data, bool compress)
Helper method to construct and fill a YODA_H5::DataSet.
Definition H5Utils.h:35
static const size_t YODA_H5_FORMAT_VERSION
Definition WriterH5.cc:27