Pottu
OTObject.hpp
Go to the documentation of this file.
1 
6 #ifndef H_OTOBJECT
7 #define H_OTOBJECT
8 
9 #include <string>
10 #include <memory>
11 #include <fstream>
12 
13 #include "Histogram1D.hpp"
14 #include "Histogram2D.hpp"
15 
16 
17 namespace pottu {
18 
19  /*
20  * Simple structure controlling the export to disk.
21  * Possible values for h1dtype are:
22  * "gzip_json", "ascii", "root"
23  * Possible values for h2dtype are:
24  * "gzip_json" and "gnuplot_binary"
25  *
26  * Root support requires setting: #define POTTU_ROOT_SUPPORT 1
27  * before any include of pottu.
28  */
29 
31  std::string h1dtype{"gzip_json"};
32  std::string h2dtype{"gzip_json"};
33  std::string tmp1, tmp2;
34  };
35 
39  class OTObject {
40  public:
41  OTObject( const std::string &name_ ) : _name( name_ ) {}
42  virtual ~OTObject() {}
43 
44  OTObject( const OTObject &o ) = delete;
45  OTObject( OTObject &&o ) = delete;
46 
47  const std::string &name() const {return(_name);}
48 
49 
50 
51  virtual void exportToDisk( const std::string &prefix,
52  export_context_t &ectx ) const = 0;
53 
54 
55 
56  std::string _name;
57 
58 
59  protected:
60 
61 
62  };
63 
64 
65 
66 
68  class OTOH1D_double_64 : public OTObject {
69  public:
70  OTOH1D_double_64( const std::string &name,
71  double low, double high, double binc,
72  const std::string &title = "",
73  const std::string &xlabel = "",
74  const std::string &ylabel = "" )
75  : OTObject(name), _h( low, high, binc )
76  {
77  _h.setTitle(title); _h.setXlabel(xlabel); _h.setYlabel(ylabel);
78  }
79 
80  virtual ~OTOH1D_double_64() = default;
81 
82  virtual void exportToDisk( const std::string &prefix, export_context_t &ectx ) const {
83 
84  if( ectx.h1dtype == "gzip_json" ) {
85  nlohmann::json j = _h.getAsJSON();
86  ectx.tmp1 = std::move(j.dump());
87  gzFile f = gzopen( (prefix+name()+".h1d").c_str(), "wb" );
88  gzwrite( f, ectx.tmp1.data(), ectx.tmp1.size() );
89  gzclose( f );
90  } else if( ectx.h1dtype == "ascii" ) {
91  std::ofstream f( prefix+name() );
92  if( !f.is_open() )
93  throw std::runtime_error( std::string()+"Couldn\'t open file "+prefix+name()+"!" );
94  _h.exportAscii( f );
95  } else
96  throw std::runtime_error( std::string()+"Unsupported h1dtype: "+ectx.h1dtype );
97 
98  }
99 
100 
101 
102  Histogram1D< BinnerFloat<double>, uint64_t > _h;
103  };
104 
105 
106 
107 
110  public:
111  OTOH1D_double_double( const std::string &name,
112  double low, double high, double binc,
113  const std::string &title = "",
114  const std::string &xlabel = "",
115  const std::string &ylabel = "" )
116  : OTObject(name), _h( low, high, binc )
117  {
118  _h.setTitle(title); _h.setXlabel(xlabel); _h.setYlabel(ylabel);
119  }
120 
121  virtual ~OTOH1D_double_double() = default;
122 
123  virtual void exportToDisk( const std::string &prefix, export_context_t &ectx ) const {
124 
125  if( ectx.h1dtype == "gzip_json" ) {
126  nlohmann::json j = _h.getAsJSON();
127  ectx.tmp1 = std::move(j.dump());
128  gzFile f = gzopen( (prefix+name()+".h1d").c_str(), "wb" );
129  gzwrite( f, ectx.tmp1.data(), ectx.tmp1.size() );
130  gzclose( f );
131  } else if( ectx.h1dtype == "ascii" ) {
132  std::ofstream f( prefix+name() );
133  if( !f.is_open() )
134  throw std::runtime_error( std::string()+"Couldn\'t open file "+prefix+name()+"!" );
135  _h.exportAscii( f );
136  } else
137  throw std::runtime_error( std::string()+"Unsupported h1dtype: "+ectx.h1dtype );
138 
139  }
140 
141 
142 
144  };
145 
146 
147 
149  class OTOH1D_pow2_64 : public OTObject {
150  public:
151  OTOH1D_pow2_64( const std::string &name,
152  uint32_t high_bits, uint32_t bins_bits,
153  const std::string &title = "",
154  const std::string &xlabel = "",
155  const std::string &ylabel = "" )
156  : OTObject(name), _h( BinnerPow2<uint32_t>(high_bits,bins_bits) )
157  {
158  _h.setTitle(title); _h.setXlabel(xlabel); _h.setYlabel(ylabel);
159  }
160 
161  virtual ~OTOH1D_pow2_64() {};
162 
163  virtual void exportToDisk( const std::string &prefix, export_context_t &ectx ) const {
164  if( ectx.h1dtype == "gzip_json" ) {
165  nlohmann::json j = _h.getAsJSON();
166  ectx.tmp1 = std::move(j.dump());
167  gzFile f = gzopen( (prefix+name()+".h1d").c_str(), "wb" );
168  gzwrite( f, ectx.tmp1.data(), ectx.tmp1.size() );
169  gzclose( f );
170  } else if( ectx.h1dtype == "ascii" ) {
171  std::ofstream f( prefix+name() );
172  if( !f.is_open() )
173  throw std::runtime_error( std::string()+"Couldn\'t open file "+prefix+name()+"!" );
174  _h.exportAscii( f );
175  } else
176  throw std::runtime_error( std::string()+"Unsupported h1dtype: "+ectx.h1dtype );
177  }
178 
179 
180 
181  Histogram1D< BinnerPow2<uint32_t>, uint64_t > _h;
182  };
183 
184 
185 
187  class OTOH2D_double_64 : public OTObject {
188  public:
189  OTOH2D_double_64( const std::string &name,
190  double xlow, double xhigh, double xbinc,
191  double ylow, double yhigh, double ybinc,
192  const std::string &title = "",
193  const std::string &xlabel = "",
194  const std::string &ylabel = "",
195  const std::string &zlabel = "" )
196  : OTObject(name), _h( xlow, xhigh, xbinc, ylow, yhigh, ybinc )
197  {
198  _h.setTitle(title); _h.setXlabel(xlabel); _h.setYlabel(ylabel); _h.setZlabel(zlabel);
199  }
200 
201  virtual ~OTOH2D_double_64() {}
202 
203  virtual void exportToDisk( const std::string &prefix, export_context_t &ectx ) const {
204  if( ectx.h1dtype == "gzip_json" ) {
205  nlohmann::json j = _h.getAsJSON();
206  ectx.tmp1 = std::move(j.dump());
207  gzFile f = gzopen( (prefix+name()+".h2d").c_str(), "wb" );
208  gzwrite( f, ectx.tmp1.data(), ectx.tmp1.size() );
209  gzclose( f );
210  } else if( ectx.h1dtype == "gnuplot_binary" ) {
211  std::ofstream f( prefix+name() );
212  if( !f.is_open() )
213  throw std::runtime_error( std::string()+"Couldn\'t open file "+prefix+name()+"!" );
214  _h.exportGnuplotBinary( f );
215  } else
216  throw std::runtime_error( std::string()+"Unsupported h1dtype: "+ectx.h1dtype );
217 
218  }
219 
220 
222  };
223 
224 
225 
228  public:
229  OTOH2D_double_double( const std::string &name,
230  double xlow, double xhigh, double xbinc,
231  double ylow, double yhigh, double ybinc,
232  const std::string &title = "",
233  const std::string &xlabel = "",
234  const std::string &ylabel = "",
235  const std::string &zlabel = "" )
236  : OTObject(name), _h( xlow, xhigh, xbinc, ylow, yhigh, ybinc )
237  {
238  _h.setTitle(title); _h.setXlabel(xlabel); _h.setYlabel(ylabel); _h.setZlabel(zlabel);
239  }
240 
241  virtual ~OTOH2D_double_double() {}
242 
243  virtual void exportToDisk( const std::string &prefix, export_context_t &ectx ) const {
244  if( ectx.h1dtype == "gzip_json" ) {
245  nlohmann::json j = _h.getAsJSON();
246  ectx.tmp1 = std::move(j.dump());
247  gzFile f = gzopen( (prefix+name()+".h2d").c_str(), "wb" );
248  gzwrite( f, ectx.tmp1.data(), ectx.tmp1.size() );
249  gzclose( f );
250  } else if( ectx.h1dtype == "gnuplot_binary" ) {
251  std::ofstream f( prefix+name() );
252  if( !f.is_open() )
253  throw std::runtime_error( std::string()+"Couldn\'t open file "+prefix+name()+"!" );
254  _h.exportGnuplotBinary( f );
255  } else
256  throw std::runtime_error( std::string()+"Unsupported h1dtype: "+ectx.h1dtype );
257 
258  }
259 
260 
262  };
263 
264 }
265 
266 
267 
268 #endif
Binner with float values.
Definition: binners.hpp:19
Binner for histograms with integer values and bin counts of powers of two.
Definition: binners.hpp:104
Simple 1D histogram.
Definition: Histogram1D.hpp:26
Simple 2D histogram.
Definition: Histogram2D.hpp:22
OTObject wrapper for Histogram1D.
Definition: OTObject.hpp:68
OTObject wrapper for Histogram1D.
Definition: OTObject.hpp:109
OTObject wrapper for power of two histogram 1D.
Definition: OTObject.hpp:149
OTObject wrapper for Histogram2D.
Definition: OTObject.hpp:187
OTObject wrapper for Histogram2D.
Definition: OTObject.hpp:227
Pure virtual base class for ObjectTree objects.
Definition: OTObject.hpp:39
Definition: mainpage.dox:6
Definition: OTObject.hpp:30