remage
Simulation framework for HPGe-based experiments
 
Loading...
Searching...
No Matches
RMGAnalysisReader.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#ifndef _RMG_ANALYSIS_READER_HH_
17#define _RMG_ANALYSIS_READER_HH_
18
19#include <map>
20#include <string>
21
22#include "G4AutoLock.hh"
23#include "G4VAnalysisReader.hh"
24
25#include "RMGConfig.hh"
26
41class RMGAnalysisReader final {
42
43 public:
44
48 class Access final {
49 friend class RMGAnalysisReader;
50
51 public:
52
53 ~Access() { unlock(); }
54
55 Access(Access const&) = delete;
56 Access& operator=(Access&) = delete;
57 Access& operator=(Access const&) = delete;
58 Access& operator=(Access&&) = delete;
59
62 void unlock() {
63 fReader = nullptr;
64 fNtupleId = -1;
65 fUnits = nullptr;
66 if (fLock) { fLock.unlock(); }
67 }
68
71 void Seek(size_t offset) {
72 AssertSetup(false);
73 for (size_t i = 0; i < offset; i++) fReader->GetNtupleRow(fNtupleId);
74 }
75
78 [[nodiscard]] auto GetNtupleRow() {
79 AssertSetup(false);
80 return fReader->GetNtupleRow(fNtupleId);
81 }
82
85 const std::string& name,
86 G4double& value,
87 const std::vector<std::string>& allowed_units = {}
88 ) {
89 AssertSetup(true);
90 AssertUnit(name, allowed_units);
91 return fReader->SetNtupleDColumn(fNtupleId, name, value);
92 }
93
96 const std::string& name,
97 G4float& value,
98 const std::vector<std::string>& allowed_units = {}
99 ) {
100 AssertSetup(true);
101 AssertUnit(name, allowed_units);
102 return fReader->SetNtupleFColumn(fNtupleId, name, value);
103 }
104
107 const std::string& name,
108 G4int& value,
109 const std::vector<std::string>& allowed_units = {}
110 ) {
111 AssertSetup(true);
112 AssertUnit(name, allowed_units);
113 return fReader->SetNtupleIColumn(fNtupleId, name, value);
114 }
115
119 [[nodiscard]] std::string GetUnit(const std::string& name) const;
120
123 operator bool() const { return fReader != nullptr && fNtupleId >= 0 && fLock; }
124
125 private:
126
127 // only allow creation or moving in parent.
128 Access(
129 G4AutoLock lock,
130 G4VAnalysisReader* reader,
131 int nt,
132 const std::map<std::string, std::string>* u,
133 bool setup
134 )
135 : fReader(reader), fNtupleId(nt), fUnits(u), fLock(std::move(lock)), fCanSetup(setup) {};
136 Access(Access&&) = default;
137
138 void AssertUnit(const std::string& name, const std::vector<std::string>& allowed_units) const;
139 void AssertSetup(bool setup) const;
140
141 G4VAnalysisReader* fReader = nullptr;
142 int fNtupleId = -1;
143 const std::map<std::string, std::string>* fUnits;
144 G4AutoLock fLock;
145 bool fCanSetup = false;
146 };
147
148 RMGAnalysisReader() = default;
149 ~RMGAnalysisReader() = default;
150
151 RMGAnalysisReader(RMGAnalysisReader const&) = delete;
152 RMGAnalysisReader& operator=(RMGAnalysisReader const&) = delete;
153 RMGAnalysisReader(RMGAnalysisReader&&) = delete;
154 RMGAnalysisReader& operator=(RMGAnalysisReader&&) = delete;
155
172 [[nodiscard]] Access OpenFile(
173 const std::string& file_name,
174 std::string ntuple_dir_name,
175 std::string ntuple_name,
176 G4AutoLock lock,
177 std::string force_ext = ""
178 );
181 [[nodiscard]] Access OpenFile(
182 const std::string& file_name,
183 std::string ntuple_dir_name,
184 std::string ntuple_name,
185 std::string force_ext = ""
186 );
187
193 void CloseFile();
194
200 [[nodiscard]] Access GetLockedReader() const;
201
204 [[nodiscard]] G4AutoLock GetLock() const;
205
208 [[nodiscard]] auto& GetFileName() const { return fFileName; }
209
210 private:
211
212 static G4Mutex fMutex;
213
214 G4VAnalysisReader* fReader = nullptr;
215 int fNtupleId = -1;
216
217 std::map<std::string, std::string> fUnits;
218 bool fHasUnits = false;
219
220 std::string fFileName;
221 bool fFileIsTemp = false;
222};
223
224#endif
225
226// vim: tabstop=2 shiftwidth=2 expandtab
thread-safe access handle to the underlying reader. This handle can be used to set-up ntuple reading ...
Definition RMGAnalysisReader.hh:48
auto SetNtupleIColumn(const std::string &name, G4int &value, const std::vector< std::string > &allowed_units={})
wraps SetNtupleIColumn() of G4VAnalysisReader.
Definition RMGAnalysisReader.hh:106
std::string GetUnit(const std::string &name) const
get unit information for the column. an empty string means either no unit attached or no support by t...
Definition RMGAnalysisReader.cc:212
auto SetNtupleFColumn(const std::string &name, G4float &value, const std::vector< std::string > &allowed_units={})
wraps SetNtupleFColumn() of G4VAnalysisReader.
Definition RMGAnalysisReader.hh:95
auto SetNtupleDColumn(const std::string &name, G4double &value, const std::vector< std::string > &allowed_units={})
wraps SetNtupleDColumn() of G4VAnalysisReader.
Definition RMGAnalysisReader.hh:84
void Seek(size_t offset)
read and discard a number of rows at the current position.
Definition RMGAnalysisReader.hh:71
void unlock()
unlock this access handle before it exits the scope.
Definition RMGAnalysisReader.hh:62
auto GetNtupleRow()
wraps GetNtupleRow() of G4VAnalysisReader.
Definition RMGAnalysisReader.hh:78
auto & GetFileName() const
get the file name of the current open file, or an empty string.
Definition RMGAnalysisReader.hh:208
G4AutoLock GetLock() const
acquires a global lock to the analysis reader mutex.
Definition RMGAnalysisReader.cc:197
void CloseFile()
if any file is open for reading, close the reader. Also clean-up temporary files.
Definition RMGAnalysisReader.cc:166
Access GetLockedReader() const
get an access handle to the current underlying G4VAnalysisReader.
Definition RMGAnalysisReader.cc:190
Access OpenFile(const std::string &file_name, std::string ntuple_dir_name, std::string ntuple_name, G4AutoLock lock, std::string force_ext="")
open an input file for reading of one specific ntuple. The return access handle can be used to connec...
Definition RMGAnalysisReader.cc:58