Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JBinsMap.hh
Go to the documentation of this file.
1#ifndef __JROOT__JBINSMAP__
2#define __JROOT__JBINSMAP__
3
4#include <map>
5#include <vector>
6#include <string>
7#include <memory>
8#include <iostream>
9#include <algorithm>
10
11#include "TH1D.h"
12#include "TH2D.h"
13#include "TH3D.h"
14
15#include "JLang/JException.hh"
17
18
19/**
20 * \author bjung
21 */
22
23namespace JROOT {}
24namespace JPP { using namespace JROOT; }
25
26namespace JROOT {
27
28 /**
29 * Template declaration of auxiliary class for retrieving histograms corresponding to a given histogram name.
30 */
31 template<class T>
33
34
35 /**
36 * Class for I/O of variable-width binning of multiple histograms.
37 */
38 class JBinsMap :
39 public std::map<std::string, std::map<char, std::vector<double> > >
40 {
41 public:
42
44
45 typedef typename map_type::mapped_type mapped_type;
46 typedef typename map_type::value_type value_type;
47
48 typedef typename map_type::iterator iterator;
49 typedef typename map_type::reverse_iterator reverse_iterator;
50
51 typedef typename map_type::const_iterator const_iterator;
52 typedef typename map_type::const_reverse_iterator const_reverse_iterator;
53
54
55 /**
56 * Get histogram with binning corresponding to a given histogram name.
57 *
58 * \param name histogram name
59 * \return pointer to 1D histogram
60 */
61 template<class T>
62 std::unique_ptr<T> getHistogram(const std::string& name) const
63 {
64 static const JBinsMapHelper<T> getHistogram(*this);
65
66 return getHistogram(name);
67 }
68
69
70 /**
71 * Read binning from stream.
72 *
73 * \param in input stream
74 * \return input stream
75 */
76 friend inline std::istream& operator>>(std::istream& in, JBinsMap& binning)
77 {
78 using namespace std;
79 using namespace JPP;
80
81 JStringStream is(in);
82
83 for (string name; is >> name; ) {
84
85 if (name.back() == SEPARATOR) { name.pop_back(); }
86
87 mapped_type binsMap;
88
89 for (char axis; is >> axis; ) {
90
91 if (axis != 'x' &&
92 axis != 'X' &&
93 axis != 'y' &&
94 axis != 'Y' &&
95 axis != 'z' &&
96 axis != 'Z') {
97 is.clear();
98 is.seekg(-1, ios::cur);
99 break;
100 }
101
102 axis = char(tolower(axis));
103
104 if (is.peek() == (int) SEPARATOR) { is.ignore(); }
105
106 vector<double> bins;
107
108 for (double value; is >> value; ) {
109
110 vector<double>::const_iterator i = std::lower_bound(bins.cbegin(), bins.cend(), value);
111
112 if (i == bins.cend() || *i != value) {
113 bins.insert(i, value);
114 }
115 }
116
117 binsMap.insert(make_pair(axis, bins));
118
119 is.clear();
120 }
121
122 binning.insert(make_pair(name, binsMap));
123 }
124
125 return in;
126 }
127
128
129 /**
130 * Write binning to output
131 *
132 * \param out output stream
133 * \return output stream
134 */
135 friend inline std::ostream& operator<<(std::ostream& out, const JBinsMap& binning)
136 {
137 using namespace std;
138
139 for (const_iterator i = binning.begin(); i != binning.end(); ++i) {
140
141 out << i->first << SEPARATOR << endl;
142
143 for (typename mapped_type::const_iterator j = i->second.begin(); j != i->second.end(); ++j) {
144
145 out << j->first << SEPARATOR;
146
147 for (vector<double>::const_iterator k = j->second.begin(); k != j->second.end(); ++k) {
148 out << ' ' << *k;
149 }
150
151 out << endl;
152 }
153
154 out << endl;
155 }
156
157 return out;
158 }
159
160
161 private:
162 static const char SEPARATOR = ':'; //!< Separator for axis label
163 };
164
165
166 /**
167 * Auxiliary class for retrieving 1D histograms corresponding to a given histogram name.
168 */
169 template<>
170 struct JBinsMapHelper<TH1D>
171 {
172 /**
173 * Constructor.
174 *
175 * \param binsMap bins map
176 */
177 JBinsMapHelper(const JBinsMap& binsMap) :
178 binsMap(binsMap)
179 {}
180
181
182 /**
183 * Get 1D histogram with binning corresponding to a given histogram name.
184 *
185 * \param name histogram name
186 * \return pointer to 1D histogram
187 */
188 std::unique_ptr<TH1D> operator()(const std::string& name) const
189 {
190 using namespace std;
191 using namespace JPP;
192
193 typename JBinsMap::const_iterator i = binsMap.find(name);
194
195 if (i == binsMap.cend()) {
196 THROW(JNoValue, "JBinsMapHelper<TH1D>::operator(): No bin configuration found for " << name);
197 }
198
199 const typename JBinsMap::mapped_type& bins = i->second;
200
201 typename JBinsMap::mapped_type::const_iterator p = bins.find('x');
202
203 if (p != bins.cend()) {
204
205 const vector<double>& Xbins = p->second;
206
207 return std::make_unique<TH1D>(name.c_str(), "",
208 Xbins.size()-1, Xbins.data());
209 }
210
211 THROW(JNoValue, "JBinsMapHelper<TH1D>::operator(): Missing x-axis binning for " << name);
212 }
213
214
215 private:
216 const JBinsMap& binsMap; //!< Bins map
217 };
218
219
220 /**
221 * Auxiliary class for retrieving 2D histograms corresponding to a given histogram name.
222 */
223 template<>
224 struct JBinsMapHelper<TH2D>
225 {
226 /**
227 * Constructor.
228 *
229 * \param binsMap bins map
230 */
231 JBinsMapHelper(const JBinsMap& binsMap) :
232 binsMap(binsMap)
233 {}
234
235
236 /**
237 * Get 2D histogram with binning corresponding to a given histogram name.
238 *
239 * \param name histogram name
240 * \return pointer to 2D histogram
241 */
242 std::unique_ptr<TH2D> operator()(const std::string& name) const
243 {
244 using namespace std;
245 using namespace JPP;
246
247 typename JBinsMap::const_iterator i = binsMap.find(name);
248
249 if (i == binsMap.cend()) {
250 THROW(JNoValue, "JBinsMapHelper<TH2D>::operator(): No bin configuration found for " << name);
251 }
252
253 const typename JBinsMap::mapped_type& bins = i->second;
254
255 typename JBinsMap::mapped_type::const_iterator p = bins.find('x');
256
257 if (p != bins.cend()) {
258
259 const vector<double>& Xbins = p->second;
260
261 typename JBinsMap::mapped_type::const_iterator q = bins.find('y');
262
263 if (q != bins.cend()) {
264
265 const vector<double>& Ybins = q->second;
266
267 return std::make_unique<TH2D>(name.c_str(), "",
268 Xbins.size()-1, Xbins.data(),
269 Ybins.size()-1, Ybins.data());
270 }
271
272 THROW(JNoValue, "JBinsMapHelper<TH2D>::operator(): Missing y-axis binning for " << name);
273 }
274
275 THROW(JNoValue, "JBinsMapHelper<TH2D>::operator(): Missing x-axis binning for " << name);
276 }
277
278
279 private:
280 const JBinsMap& binsMap; //!< Bins map
281 };
282
283
284 /**
285 * Auxiliary class for retrieving 3D histograms corresponding to a given histogram name.
286 */
287 template<>
288 struct JBinsMapHelper<TH3D>
289 {
290 /**
291 * Constructor.
292 *
293 * \param binsMap bins map
294 */
295 JBinsMapHelper(const JBinsMap& binsMap) :
296 binsMap(binsMap)
297 {}
298
299
300 /**
301 * Get 3D histogram with binning corresponding to a given histogram name.
302 *
303 * \param name histogram name
304 * \return pointer to 3D histogram
305 */
306 std::unique_ptr<TH3D> operator()(const std::string& name) const
307 {
308 using namespace std;
309 using namespace JPP;
310
311 typename JBinsMap::const_iterator i = binsMap.find(name);
312
313 if (i == binsMap.cend()) {
314 THROW(JNoValue, "JBinsMapHelper<TH3D>::operator(): No bin configuration found for " << name);
315 }
316
317 const typename JBinsMap::mapped_type& bins = i->second;
318
319 typename JBinsMap::mapped_type::const_iterator p = bins.find('x');
320
321 if (p != bins.cend()) {
322
323 const vector<double>& Xbins = p->second;
324
325 typename JBinsMap::mapped_type::const_iterator q = bins.find('y');
326
327 if (q != bins.cend()) {
328
329 const vector<double>& Ybins = q->second;
330
331 typename JBinsMap::mapped_type::const_iterator r = bins.find('z');
332
333 if (r != bins.cend()) {
334
335 const vector<double>& Zbins = r->second;
336
337 return std::make_unique<TH3D>(name.c_str(), "",
338 Xbins.size()-1, Xbins.data(),
339 Ybins.size()-1, Ybins.data(),
340 Zbins.size()-1, Zbins.data());
341 }
342
343 THROW(JNoValue, "JBinsMapHelper<TH3D>::operator(): Missing z-axis binning for " << name);
344 }
345
346 THROW(JNoValue, "JBinsMapHelper<TH3D>::operator(): Missing y-axis binning for " << name);
347 }
348
349 THROW(JNoValue, "JBinsMapHelper<TH3D>::operator(): Missing x-axis binning for " << name);
350 }
351
352
353 private:
354 const JBinsMap& binsMap; //!< Bins map
355 };
356}
357
358#endif
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Exception for missing value.
Wrapper class around STL stringstream class to facilitate optional loading of data from file.
Template declaration of auxiliary class for retrieving histograms corresponding to a given histogram ...
Definition JBinsMap.hh:32
Class for I/O of variable-width binning of multiple histograms.
Definition JBinsMap.hh:40
friend std::ostream & operator<<(std::ostream &out, const JBinsMap &binning)
Write binning to output.
Definition JBinsMap.hh:135
map_type::value_type value_type
Definition JBinsMap.hh:46
map_type::const_iterator const_iterator
Definition JBinsMap.hh:51
static const char SEPARATOR
Separator for axis label.
Definition JBinsMap.hh:162
map_type::reverse_iterator reverse_iterator
Definition JBinsMap.hh:49
std::map< std::string, std::map< char, std::vector< double > > > map_type
Definition JBinsMap.hh:43
map_type::iterator iterator
Definition JBinsMap.hh:48
map_type::mapped_type mapped_type
Definition JBinsMap.hh:45
std::unique_ptr< T > getHistogram(const std::string &name) const
Get histogram with binning corresponding to a given histogram name.
Definition JBinsMap.hh:62
friend std::istream & operator>>(std::istream &in, JBinsMap &binning)
Read binning from stream.
Definition JBinsMap.hh:76
map_type::const_reverse_iterator const_reverse_iterator
Definition JBinsMap.hh:52
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Auxiliary classes and methods for ROOT I/O.
std::unique_ptr< TH1D > operator()(const std::string &name) const
Get 1D histogram with binning corresponding to a given histogram name.
Definition JBinsMap.hh:188
const JBinsMap & binsMap
Bins map.
Definition JBinsMap.hh:216
JBinsMapHelper(const JBinsMap &binsMap)
Constructor.
Definition JBinsMap.hh:177
const JBinsMap & binsMap
Bins map.
Definition JBinsMap.hh:280
std::unique_ptr< TH2D > operator()(const std::string &name) const
Get 2D histogram with binning corresponding to a given histogram name.
Definition JBinsMap.hh:242
JBinsMapHelper(const JBinsMap &binsMap)
Constructor.
Definition JBinsMap.hh:231
JBinsMapHelper(const JBinsMap &binsMap)
Constructor.
Definition JBinsMap.hh:295
std::unique_ptr< TH3D > operator()(const std::string &name) const
Get 3D histogram with binning corresponding to a given histogram name.
Definition JBinsMap.hh:306
const JBinsMap & binsMap
Bins map.
Definition JBinsMap.hh:354