Pottu
result_collection_support.hpp
Go to the documentation of this file.
1 
7 #ifndef H_POTTU_RESULT_COLLECTION_SUPPORT
8 #define H_POTTU_RESULT_COLLECTION_SUPPORT
9 
10 
11 #ifdef POTTU_ROOT_SUPPORT
12 // #include <TFile.h>
13  #include <TDirectory.h>
14  #include <TH1I.h>
15  #include <TH2I.h>
16 #else
17 // Including legacy ObjectTree only if no root support requested
18  #include "ObjectTree.hpp"
19 #endif
20 
21 #include <fmt/base.h>
22 
23 #include <fstream>
24 #include <exception>
25 #include <cstdint>
26 
27 
28 namespace pottu {
29 
35  double low{0};
36  double high{16384};
37  uint32_t bins{256};
38  };
39 
40 
41 #ifdef POTTU_ROOT_SUPPORT
42  typedef TDirectory * _result_tree_type;
43  typedef TH1I _histogram_1d_pow2_type;
44  typedef TH1I _histogram_1d_type;
45  typedef TH2I _histogram_2d_type;
46 
47  static inline TDirectory *createSubTree( TDirectory *tree, const std::string &name ) {
48  return tree->mkdir( name.c_str() );
49  }
50 
51  static inline TH1I *
52  createHistogram1DPow2( TDirectory *tree, const char *name, uint32_t highbits, uint32_t binbits ) {
53  tree->cd();
54  auto h = new TH1I( name, name, 1<<binbits, 0, 1<<highbits );
55  return h;
56  }
57 
58  static inline TH1I *
59  createHistogram1D( TDirectory *tree, const char *name, double low, double high, uint32_t bincount ) {
60  tree->cd();
61  auto h = new TH1I( name, name, bincount, low, high );
62  return h;
63  }
64 
65  static inline TH2I *
66  createHistogram2D( TDirectory *tree, const char *name,
67  double xlow, double xhigh, uint32_t xbincount,
68  double ylow, double yhigh, uint32_t ybincount ) {
69  tree->cd();
70  auto h = new TH2I( name, name, xbincount, xlow, xhigh, ybincount, ylow, yhigh );
71  return h;
72  }
73 
74  static inline void fillInteger( TH1I *h, int value ) {
75  h->Fill( value );
76  }
77 
78  static inline void fill( TH1I *h, double value ) {
79  h->Fill( value );
80  }
81 
82  static inline void fillAmount( TH1I *h, double value, int amount ) {
83  h->Fill( value, amount );
84  }
85 
86  static inline void fill( TH2I *h, double xvalue, double yvalue ) {
87  h->Fill( xvalue, yvalue );
88  }
89 
90  static inline void fillAmount( TH2I *h, double xvalue, double yvalue, int amount ) {
91  h->Fill( xvalue, yvalue, amount );
92  }
93 
94 #else
95  typedef ObjectTree * _result_tree_type;
96  typedef ObjectTree::pow2_h1_type _histogram_1d_pow2_type;
97  typedef ObjectTree::default_h1_type _histogram_1d_type;
98  typedef ObjectTree::default_h2_type _histogram_2d_type;
99 
100  static inline ObjectTree *createSubTree( ObjectTree *tree, const char *name ) {
101  return tree->createSubTree( name );
102  }
103 
104  static inline ObjectTree::pow2_h1_type *
105  createHistogram1DPow2( ObjectTree *tree, const char *name, uint32_t highbits, uint32_t binbits ) {
106  return tree->createHistogram1D_pow2( name, highbits, binbits );
107  }
108 
109  static inline ObjectTree::default_h1_type *
110  createHistogram1D( ObjectTree *tree, const char *name, double low, double high, uint32_t bincount ) {
111  return tree->createHistogram1D( name, low, high, bincount );
112  }
113 
114  static inline ObjectTree::default_h2_type *
115  createHistogram2D( ObjectTree *tree, const char *name,
116  double xlow, double xhigh, uint32_t xbincount,
117  double ylow, double yhigh, uint32_t ybincount ) {
118  return tree->createHistogram2D( name, xlow, xhigh, xbincount, ylow, yhigh, ybincount );
119  }
120 
121  static inline void fillInteger( ObjectTree::pow2_h1_type *h, int value ) {
122  h->fill( value );
123  }
124 
125  static inline void fill( ObjectTree::default_h1_type *h, double value ) {
126  h->fill( value );
127  }
128 
129  static inline void fillAmount( ObjectTree::default_h1_type *h, double value, int amount ) {
130  h->fill( value, amount );
131  }
132 
133  static inline void fill( ObjectTree::default_h2_type *h, double xvalue, double yvalue ) {
134  h->fill( xvalue, yvalue );
135  }
136 
137  static inline void fillAmount( ObjectTree::default_h2_type *h, double xvalue, double yvalue, int amount ) {
138  h->fill( xvalue, yvalue, amount );
139  }
140 
141 
142 
143 #endif
144 
145 
149  static inline pottu::_histogram_1d_type *createHistogram1D( pottu::_result_tree_type tree,
150  const std::string &name,
151  const histogram_param_binning_t &binning ) {
152  return pottu::createHistogram1D( tree,
153  name.c_str(),
154  binning.low, binning.high, binning.bins );
155  }
156 
157 
161  static inline pottu::_histogram_2d_type *createHistogram2D( pottu::_result_tree_type tree,
162  const std::string &name,
163  const histogram_param_binning_t &xbinning,
164  const histogram_param_binning_t &ybinning ) {
165  return pottu::createHistogram2D( tree,
166  name.c_str(),
167  xbinning.low, xbinning.high, xbinning.bins,
168  ybinning.low, ybinning.high, ybinning.bins );
169  }
170 
171 
172 
173 #ifdef POTTU_ROOT_SUPPORT
186  static inline void exportHistogramToAscii( const pottu::_histogram_1d_type *h, const std::string &prefix ) {
187  std::string name = fmt::format( "{}{}.dat", prefix, h->GetName() );
188  std::ofstream f( name );
189  if( !f.is_open() ) {
190  throw std::runtime_error( fmt::format( "Couldn't open file \'{}\' to export histogram.\n", name ) );
191  }
192  uint32_t n = h->GetNbinsX();
193 
194  for( uint32_t i=1; i<=n; i++ ) {
195  f << h->GetBinLowEdge(i)+h->GetBinWidth(i)/2 << "\t"
196  << h->GetBinContent(i) << "\n";
197  }
198  }
199 #else
200  static inline void exportHistogramToAscii( const pottu::_histogram_1d_type *h, const std::string &prefix ) {
201  throw std::runtime_error( "exportHistogramToAscii() is not implemented for built-in histograms" );
202  // TODO
203  }
204 #endif
205 
206 
216  static inline void throwIfNotValidName( const std::string &name ) {
217  if( name.size() == 0 )
218  throw std::runtime_error( "Name cannot be empty for an object." );
219 
220  for( const auto c : name ) {
221  if( !( isalnum(c) || c=='_' || c=='-' || c=='.' ) )
222  throw std::runtime_error( fmt::format( "Name \'{}\' is not valid name for an object.", name ) );
223  }
224  }
225 
226 
227 }
228 
229 
230 
231 
232 
233 
234 #endif
Simple 1D histogram.
Definition: Histogram1D.hpp:26
Simple 2D histogram.
Definition: Histogram2D.hpp:22
Class holding tree of histograms and subtrees.
Definition: ObjectTree.hpp:44
Definition: mainpage.dox:6
static void throwIfNotValidName(const std::string &name)
Checks that the given string can be used as a filename.
Definition: result_collection_support.hpp:216
Helper struct to contain information about histogram binning.
Definition: result_collection_support.hpp:34