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