Pottu
ObjectTree.hpp
Go to the documentation of this file.
1 
7 #ifndef H_POTTU_OBJECTTREE
8 #define H_POTTU_OBJECTTREE
9 
10 
11 #include "Histogram1D.hpp"
12 #include "Histogram2D.hpp"
13 #include "binners.hpp"
14 #include "OTObject.hpp"
15 
16 #include <sys/stat.h>
17 #include <sys/types.h>
18 
19 #include <stdexcept>
20 #include <memory>
21 #include <set>
22 #include <string>
23 #include <cstring>
24 // Keeping still c++14, filesystem requires c++17
25 //#include <filesystem>
26 
27 
28 namespace pottu {
29 
30 
31  static inline bool operator<( const std::unique_ptr<OTObject> &o1, const std::unique_ptr<OTObject> &o2 ) {
32  return( o1->name() < o2->name() );
33  }
34  static inline bool operator<( const std::string &name, const std::unique_ptr<OTObject> &oto ) {
35  return( name < oto->name() );
36  }
37  static inline bool operator<( const std::unique_ptr<OTObject> &oto, const std::string &name ) {
38  return( oto->name() < name );
39  }
40 
41 
44  class ObjectTree : public OTObject {
45  public:
49 
52 
53  ObjectTree( const std::string &name ) : OTObject(name) {}
54  virtual ~ObjectTree() {}
55 
56  ObjectTree *createSubTree( const std::string &name ) {
57  auto it = _objects.find<std::string>( name );
58  if( it != _objects.end() )
59  throw std::runtime_error( std::string()+"Object with name " + name + " already in the tree." );
60 
61  auto p = new ObjectTree( name );
62 
63  _objects.emplace( p );
64  return( p );
65  }
66 
67 
69  createHistogram1D( const std::string &name,
70  double low, double high, unsigned int binc,
71  const std::string &title = "",
72  const std::string &xlabel = "",
73  const std::string &ylabel = "" ) {
74  auto it = _objects.find<std::string>( name );
75  if( it != _objects.end() )
76  throw std::runtime_error( std::string()+"Object with name " + name + " already in the tree." );
77 
78  auto p = new OTOH1D_double_64( name, low, high, binc, title, xlabel, ylabel );
79 
80  _objects.emplace( p );
81  return( &(p->_h) );
82  }
83 
85  createHistogram1D_doublecell( const std::string &name,
86  double low, double high, unsigned int binc,
87  const std::string &title = "",
88  const std::string &xlabel = "",
89  const std::string &ylabel = "" ) {
90  auto it = _objects.find<std::string>( name );
91  if( it != _objects.end() )
92  throw std::runtime_error( std::string()+"Object with name " + name + " already in the tree." );
93 
94  auto p = new OTOH1D_double_double( name, low, high, binc, title, xlabel, ylabel );
95 
96  _objects.emplace( p );
97  return( &(p->_h) );
98  }
99 
100  pow2_h1_type *
101  createHistogram1D_pow2( const std::string &name,
102  uint32_t high_bits, uint32_t bins_bits,
103  const std::string &title = "",
104  const std::string &xlabel = "",
105  const std::string &ylabel = "" ) {
106  auto it = _objects.find<std::string>( name );
107  if( it != _objects.end() )
108  throw std::runtime_error( std::string()+"Object with name " + name + " already in the tree." );
109 
110  auto p = new OTOH1D_pow2_64( name, high_bits, bins_bits, title, xlabel, ylabel );
111 
112  _objects.emplace( p );
113  return( &(p->_h) );
114  }
115 
116 
117 
119  createHistogram2D( const std::string &name,
120  double xlow, double xhigh, unsigned int xbinc,
121  double ylow, double yhigh, unsigned int ybinc,
122  const std::string &title = "",
123  const std::string &xlabel = "",
124  const std::string &ylabel = "",
125  const std::string &zlabel = "" ) {
126  auto it = _objects.find<std::string>( name );
127  if( it != _objects.end() )
128  throw std::runtime_error( std::string()+"Object with name " + name + " already in the tree." );
129 
130  auto p = new OTOH2D_double_64( name,
131  xlow, xhigh, xbinc,
132  ylow, yhigh, ybinc,
133  title, xlabel, ylabel, zlabel );
134 
135  _objects.emplace( p );
136  return( &(p->_h) );
137  }
138 
139 
141  createHistogram2D_doublecell( const std::string &name,
142  double xlow, double xhigh, unsigned int xbinc,
143  double ylow, double yhigh, unsigned int ybinc,
144  const std::string &title = "",
145  const std::string &xlabel = "",
146  const std::string &ylabel = "",
147  const std::string &zlabel = "" ) {
148  auto it = _objects.find<std::string>( name );
149  if( it != _objects.end() )
150  throw std::runtime_error( std::string()+"Object with name " + name + " already in the tree." );
151 
152  auto p = new OTOH2D_double_double( name,
153  xlow, xhigh, xbinc,
154  ylow, yhigh, ybinc,
155  title, xlabel, ylabel, zlabel );
156 
157  _objects.emplace( p );
158  return( &(p->_h) );
159  }
160 
161 
162 
163  virtual void exportToDisk( const std::string &prefix, export_context_t &ectx ) const {
164 
165  // First trying to make a directory "prefixname/" where name is
166  // the name of this tree.
167  std::string newprefix=prefix;
168  newprefix += name();
169  newprefix += "/";
170 
171  std::cout << "Trying to create \'" << newprefix << "\'\n";
172 
173  int ret = mkdir( newprefix.c_str(), 0777 );
174  if( ret == -1 && errno != EEXIST ) {
175  throw std::runtime_error( strerror(errno) );
176  }
177 
178  /*
179  try {
180  std::filesystem::create_directory( newprefix );
181  } catch( std::exception &e ) {
182  std::cerr << "Failed to create \'" << newprefix << "\'\n";
183  }
184  */
185 
186  for( auto &obj : _objects ) {
187  //cout << "Calling export for subobject " << obj->name() << "\n";
188  obj->exportToDisk( newprefix, ectx );
189  }
190  }
191 
192 
193 
194 
195 
196  std::set< std::unique_ptr<OTObject>, std::less<> > _objects;
197  };
198 
199 }
200 
201 #endif
Binner with float values.
Definition: binners.hpp:19
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
Class holding tree of histograms and subtrees.
Definition: ObjectTree.hpp:44
Definition: mainpage.dox:6
Definition: OTObject.hpp:30