ATLAS Offline Software
Loading...
Searching...
No Matches
H5Utils::Merger Class Reference

#include <Merger.h>

Collaboration diagram for H5Utils::Merger:

Public Member Functions

 Merger (hsize_t mergeAxis=0, int chunkSize=-1, bool requireSameFormat=true, std::size_t bufferSize=-1, bool bufferInRows=false)
 Create the merger.
 ~Merger ()
void merge (H5::Group &target, const H5::Group &source)
 Merge a source group into a target group.
void merge (H5::DataSet &target, const H5::DataSet &source)
 Merge a source dataset into a target dataset.
void merge (auto &target, const auto &source)
 Merge a source file/group into a target file/group.
void flush (H5::Group &dst)
 Write all accumulated histogram data to the output.

Protected Attributes

hsize_t m_mergeAxis
 The axis to merge along.
int m_chunkSize
 The chunk size to apply.
bool m_requireSameFormat
 Whether to require the same group structure.
std::size_t m_bufferSize
 The size of the buffer.
bool m_measureBufferInRows
 Whether to measure the buffer in bytes or rows.
std::unique_ptr< H5Utils::hist::HistogramMergerm_histMerger
 Accumulator for UHI histogram groups.

Static Private Member Functions

static H5::Group convert (const H5::Group &group)
 convert() is a no-op for a Group, used by the merge() template
static H5::Group convert (const H5::H5File &file)
 convert() opens the root group of a File, used by the merge() template

Detailed Description

Definition at line 24 of file Merger.h.

Constructor & Destructor Documentation

◆ Merger()

H5Utils::Merger::Merger ( hsize_t mergeAxis = 0,
int chunkSize = -1,
bool requireSameFormat = true,
std::size_t bufferSize = -1,
bool bufferInRows = false )

Create the merger.

Parameters
mergeAxisThe axis to merge along
chunkSizeThe chunk size to apply. If negative then the value found in the input datasets will be used.
requireSameFormatRequire all input files to have the same groups and datasets.
bufferSizeThe maximum size of the buffer to use while merging datasets
bufferInRowsWhether the buffer size is specified in rows or bytes

Definition at line 14 of file Merger.cxx.

19 :
20 m_mergeAxis(mergeAxis),
21 m_chunkSize(chunkSize),
22 m_requireSameFormat(requireSameFormat),
23 m_bufferSize(bufferSize),
24 m_measureBufferInRows(bufferInRows),
25 m_histMerger(std::make_unique<H5Utils::hist::HistogramMerger>()) {}
int m_chunkSize
The chunk size to apply.
Definition Merger.h:95
bool m_measureBufferInRows
Whether to measure the buffer in bytes or rows.
Definition Merger.h:101
std::unique_ptr< H5Utils::hist::HistogramMerger > m_histMerger
Accumulator for UHI histogram groups.
Definition Merger.h:103
hsize_t m_mergeAxis
The axis to merge along.
Definition Merger.h:93
std::size_t m_bufferSize
The size of the buffer.
Definition Merger.h:99
bool m_requireSameFormat
Whether to require the same group structure.
Definition Merger.h:97

◆ ~Merger()

H5Utils::Merger::~Merger ( )

Definition at line 27 of file Merger.cxx.

27{}

Member Function Documentation

◆ convert() [1/2]

H5::Group H5Utils::Merger::convert ( const H5::Group & group)
staticprivate

convert() is a no-op for a Group, used by the merge() template

Definition at line 122 of file Merger.cxx.

123 {
124 return group;
125 }

◆ convert() [2/2]

H5::Group H5Utils::Merger::convert ( const H5::H5File & file)
staticprivate

convert() opens the root group of a File, used by the merge() template

Definition at line 127 of file Merger.cxx.

128 {
129 return file.openGroup("/");
130 }
TFile * file

◆ flush()

void H5Utils::Merger::flush ( H5::Group & dst)

Write all accumulated histogram data to the output.

Parameters
dstThe root group of the output file.

Definition at line 132 of file Merger.cxx.

133 {
134 m_histMerger->write(dst);
135 }

◆ merge() [1/3]

void H5Utils::Merger::merge ( auto & target,
const auto & source )
inline

Merge a source file/group into a target file/group.

Parameters
targetThe file or group to merge into
sourceThe file or group to merge from

Accepts any combination of H5::File and H5::Group. Each argument is converted to the H5::Group to actually operate on via convert() (a no-op for a Group, the root group for a File) before forwarding to the merge(Group&, const Group&) overload above.

Definition at line 71 of file Merger.h.

72 {
73 H5::Group targetGroup = convert(target);
74 H5::Group sourceGroup = convert(source);
75 merge(targetGroup, sourceGroup);
76 }
void merge(H5::Group &target, const H5::Group &source)
Merge a source group into a target group.
Definition Merger.cxx:29
static H5::Group convert(const H5::Group &group)
convert() is a no-op for a Group, used by the merge() template
Definition Merger.cxx:122

◆ merge() [2/3]

void H5Utils::Merger::merge ( H5::DataSet & target,
const H5::DataSet & source )

Merge a source dataset into a target dataset.

Parameters
targetThe dataset to merge into
sourceThe dataset to merge from

Definition at line 107 of file Merger.cxx.

110 {
111 std::size_t bufferSize = m_bufferSize;
113 // Need to calculate the actual buffer size
114 std::size_t rowSize = getRowSize(source, m_mergeAxis);
115 if (std::size_t(-1) / m_bufferSize < rowSize)
116 std::overflow_error("Requested buffer would overflow the register!");
117 bufferSize = rowSize * m_bufferSize;
118 }
119 mergeDatasets(target, source, m_mergeAxis, bufferSize);
120 }
std::size_t getRowSize(const H5::DataSet &ds, hsize_t axis)
Calculate the size of a row of a dataset in bytes.
void mergeDatasets(H5::DataSet &target, const H5::DataSet &source, hsize_t mergeAxis, std::size_t bufferSize=-1)
Merge two datasets.

◆ merge() [3/3]

void H5Utils::Merger::merge ( H5::Group & target,
const H5::Group & source )

Merge a source group into a target group.

Parameters
targetThe group to merge into
sourceThe group to merge from

Definition at line 29 of file Merger.cxx.

32 {
33 // Check if this group was empty before we started
34 bool isEmpty = target.getNumObjs() == 0;
35
36 // Iterate through each child of the source group
37 for (hsize_t ii = 0; ii < source.getNumObjs(); ++ii) {
38 H5G_obj_t childType = source.getObjTypeByIdx(ii);
39 std::string childName = source.getObjnameByIdx(ii);
40 // Find the correct index in the target
41 hsize_t targetIdx = 0;
42 for (; targetIdx < target.getNumObjs(); ++targetIdx)
43 if (target.getObjnameByIdx(targetIdx) == childName)
44 break;
45 bool found = targetIdx != target.getNumObjs();
46 if (found) {
47 // Make sure these are the same type!
48 if (target.getObjTypeByIdx(targetIdx) != childType)
49 throw std::invalid_argument(
50 "Both target and source contain " + childName +
51 " but they have different types!");
52 }
53 else if (m_requireSameFormat && !isEmpty) {
54 throw std::invalid_argument(
55 "Target and source have different formats!");
56 }
57 switch (childType) {
58 case H5G_GROUP:
59 {
60 H5::Group sg = source.openGroup(childName);
61 // UHI histogram groups are accumulated separately and written
62 // once all source files have been processed.
63 if (sg.attrExists("uhi_schema")) {
64 m_histMerger->add(sg.getObjName(), sg);
65 continue;
66 }
67 H5::Group tg = found ?
68 target.openGroup(childName) :
69 target.createGroup(sg.getObjName());
70 try {
71 merge(tg, sg);
72 }
73 catch (...) {
74 std::cerr << "Encountered an error merging child " << childName << std::endl;
75 throw;
76 }
77 }
78 break;
79 case H5G_DATASET:
80 {
81 H5::DataSet sd = source.openDataSet(childName);
82 if (sd.getSpace().getSimpleExtentNdims() == 0) {
83 std::cerr << "WARNING: skipping scalar '"
84 << childName << "'" << std::endl;
85 break;
86 }
87 H5::DataSet td = found ?
88 target.openDataSet(childName) :
90 try {
91 merge(td, sd);
92 }
93 catch (...) {
94 std::cerr << "Encountered an error merging child " << childName << std::endl;
95 throw;
96 }
97 }
98 break;
99 default:
100 break;
101 }
102 } //> end loop over children
103 // TODO - this did no check to see if target contained something source
104 // didn't, this is probably fine though.
105 } //> end function merge(group)
H5::DataSet createDataSet(H5::H5Location &targetLocation, const H5::DataSet &source, hsize_t mergeAxis, int chunkSize=-1, int mergeExtent=-1)
Make a new dataset using the properties of another.

Member Data Documentation

◆ m_bufferSize

std::size_t H5Utils::Merger::m_bufferSize
protected

The size of the buffer.

Definition at line 99 of file Merger.h.

◆ m_chunkSize

int H5Utils::Merger::m_chunkSize
protected

The chunk size to apply.

Definition at line 95 of file Merger.h.

◆ m_histMerger

std::unique_ptr<H5Utils::hist::HistogramMerger> H5Utils::Merger::m_histMerger
protected

Accumulator for UHI histogram groups.

Definition at line 103 of file Merger.h.

◆ m_measureBufferInRows

bool H5Utils::Merger::m_measureBufferInRows
protected

Whether to measure the buffer in bytes or rows.

Definition at line 101 of file Merger.h.

◆ m_mergeAxis

hsize_t H5Utils::Merger::m_mergeAxis
protected

The axis to merge along.

Definition at line 93 of file Merger.h.

◆ m_requireSameFormat

bool H5Utils::Merger::m_requireSameFormat
protected

Whether to require the same group structure.

Definition at line 97 of file Merger.h.


The documentation for this class was generated from the following files: