remage
Simulation framework for HPGe-based experiments
 
Loading...
Searching...
No Matches
RMGConvertLH5.hh
1// Copyright (C) 2024 Manuel Huber <https://orcid.org/0009-0000-5212-2999>
2//
3// This program is free software: you can redistribute it and/or modify it under
4// the terms of the GNU Lesser General Public License as published by the Free
5// Software Foundation, either version 3 of the License, or (at your option) any
6// later version.
7//
8// This program is distributed in the hope that it will be useful, but WITHOUT
9// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11// details.
12//
13// You should have received a copy of the GNU Lesser General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16
17#ifndef _RMG_CONVERT_LH5_HH
18#define _RMG_CONVERT_LH5_HH
19
20#include <filesystem>
21#include <map>
22#include <memory>
23#include <optional>
24#include <regex>
25#include <set>
26#include <string>
27#include <vector>
28
29#include "RMGLog.hh"
30
31#include "H5Cpp.h"
32
39class RMGConvertLH5 {
40
41 public:
42
59 static bool ConvertToLH5(
60 std::string,
61 std::string,
62 std::set<std::string>,
63 const std::map<int, std::pair<int, std::string>>&,
64 bool,
65 bool part_of_batch = false,
66 int n_ev = -1
67 );
82 static bool ConvertFromLH5(
83 std::string,
84 std::string,
85 bool,
86 bool part_of_batch,
87 std::map<std::string, std::map<std::string, std::string>>&
88 );
89
90 inline static bool fIsStandalone = false;
91
92 private:
93
94 RMGConvertLH5(
95 std::string filename,
96 std::string ntuple_group,
97 std::set<std::string> aux_ntuples,
98 const std::map<int, std::pair<int, std::string>>& ntuple_meta,
99 bool dry_run,
100 bool part_of_batch,
101 int n_ev
102 )
103 : fHdf5FileName(filename), fNtupleGroupName(ntuple_group), fAuxNtuples(aux_ntuples),
104 fNtupleMeta(ntuple_meta), fDryRun(dry_run), fIsPartOfBatch(part_of_batch),
105 fEventCount(n_ev) {};
106
108
109 static int iter_children(hid_t, const char*, const H5L_info_t*, void*);
110 static std::vector<std::string> GetChildren(H5::Group&);
111
112 bool ExistsByType(H5::H5Location&, std::string, H5O_type_t);
113
114 void SetStringAttribute(H5::H5Object&, std::string, std::string);
115 std::optional<std::string> GetStringAttribute(H5::H5Object&, std::string);
116
117 void CreateUIntDataset(H5::Group&, std::string, uint64_t);
118 void CreateStringDataset(H5::Group&, std::string, std::string);
119
121 // HDF5 -> LH5 (output files):
122
123 bool ConvertToLH5Internal();
124
125 std::pair<std::string, std::vector<std::string>> ReadNullSepDataset(
126 H5::Group&,
127 std::string,
128 std::string
129 );
130
131 static inline const std::regex names_it_re = std::regex("(_in_.+?)?\\0|(_in_.+?)$");
132
133 std::unique_ptr<H5::DataType> FormToHDFDataType(std::string);
134 std::string DataTypeToLGDO(H5::DataType);
135 bool ConvertNTupleToTable(H5::Group&);
136
137 bool CheckGeantHeader(H5::Group&);
138
140 // LH5 -> HDF5 (input files):
141
142 bool ConvertFromLH5Internal(std::map<std::string, std::map<std::string, std::string>>&);
143
144 static inline const std::regex table_dtype_re = std::regex("^table\\{.*\\}$");
145
146 std::string HDFDataTypeToForm(H5::DataType);
147 bool ConvertTableToNTuple(H5::Group&, std::map<std::string, std::string>&);
148
150
151 template<typename... Args> void LH5Log(RMGLog::LogLevel loglevel, const Args&... args) {
152 if (fDryRun && loglevel < RMGLog::error) return;
153 std::string fn_prefix = fIsPartOfBatch ? " (" + fHdf5FileName + ")" : "";
155 loglevel,
156 "",
157 fIsStandalone ? "" : "ConvertLH5",
158 fDryRun ? "[dry-run]" : "",
159 fn_prefix,
160 ": ",
161 args...
162 );
163 }
164
165 std::string fHdf5FileName;
166 std::string fNtupleGroupName;
167 std::set<std::string> fAuxNtuples;
168 std::map<int, std::pair<int, std::string>> fNtupleMeta;
169 std::string fUIDKeyFormatString = "det{:03}";
170 bool fDryRun;
171 bool fIsPartOfBatch;
172 int fEventCount = -1;
173};
174
175#endif
176
177// vim: tabstop=2 shiftwidth=2 expandtab
static bool ConvertFromLH5(std::string, std::string, bool, bool part_of_batch, std::map< std::string, std::map< std::string, std::string > > &)
Convert an LH5 input file to HDF5 format.
Definition RMGConvertLH5.cc:601
static bool ConvertToLH5(std::string, std::string, std::set< std::string >, const std::map< int, std::pair< int, std::string > > &, bool, bool part_of_batch=false, int n_ev=-1)
Convert a Geant4 HDF5 output file to LH5 format.
Definition RMGConvertLH5.cc:423
LogLevel
Definition RMGLog.hh:66
@ error
Print only errors.
Definition RMGLog.hh:72
static void Out(RMGLog::LogLevel loglevel, const T &message)