7 #ifndef H_POTTU_RESULT_COLLECTION_SUPPORT
8 #define H_POTTU_RESULT_COLLECTION_SUPPORT
11 #ifdef POTTU_ROOT_SUPPORT
13 #include <TDirectory.h>
41 #ifdef POTTU_ROOT_SUPPORT
47 static inline TDirectory *createSubTree( TDirectory *tree,
const std::string &name ) {
48 return tree->mkdir( name.c_str() );
52 createHistogram1DPow2( TDirectory *tree,
const char *name, uint32_t highbits, uint32_t binbits ) {
54 auto h =
new TH1I( name, name, 1<<binbits, 0, 1<<highbits );
59 createHistogram1D( TDirectory *tree,
const char *name,
double low,
double high, uint32_t bincount ) {
61 auto h =
new TH1I( name, name, bincount, low, high );
66 createHistogram2D( TDirectory *tree,
const char *name,
67 double xlow,
double xhigh, uint32_t xbincount,
68 double ylow,
double yhigh, uint32_t ybincount ) {
70 auto h =
new TH2I( name, name, xbincount, xlow, xhigh, ybincount, ylow, yhigh );
74 static inline void fillInteger( TH1I *h,
int value ) {
78 static inline void fill( TH1I *h,
double value ) {
82 static inline void fillAmount( TH1I *h,
double value,
int amount ) {
83 h->Fill( value, amount );
86 static inline void fill( TH2I *h,
double xvalue,
double yvalue ) {
87 h->Fill( xvalue, yvalue );
90 static inline void fillAmount( TH2I *h,
double xvalue,
double yvalue,
int amount ) {
91 h->Fill( xvalue, yvalue, amount );
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;
100 static inline ObjectTree *createSubTree( ObjectTree *tree,
const char *name ) {
101 return tree->createSubTree( name );
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 );
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 );
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 );
121 static inline void fillInteger( ObjectTree::pow2_h1_type *h,
int value ) {
125 static inline void fill( ObjectTree::default_h1_type *h,
double value ) {
129 static inline void fillAmount( ObjectTree::default_h1_type *h,
double value,
int amount ) {
130 h->fill( value, amount );
133 static inline void fill( ObjectTree::default_h2_type *h,
double xvalue,
double yvalue ) {
134 h->fill( xvalue, yvalue );
137 static inline void fillAmount( ObjectTree::default_h2_type *h,
double xvalue,
double yvalue,
int amount ) {
138 h->fill( xvalue, yvalue, amount );
150 const std::string &name,
152 return pottu::createHistogram1D( tree,
154 binning.low, binning.high, binning.bins );
162 const std::string &name,
165 return pottu::createHistogram2D( tree,
167 xbinning.low, xbinning.high, xbinning.bins,
168 ybinning.low, ybinning.high, ybinning.bins );
173 #ifdef POTTU_ROOT_SUPPORT
187 std::string name = fmt::format(
"{}{}.dat", prefix, h->GetName() );
188 std::ofstream f( name );
190 throw std::runtime_error( fmt::format(
"Couldn't open file \'{}\' to export histogram.\n", name ) );
192 uint32_t n = h->GetNbinsX();
194 for( uint32_t i=1; i<=n; i++ ) {
195 f << h->GetBinLowEdge(i)+h->GetBinWidth(i)/2 <<
"\t"
196 << h->GetBinContent(i) <<
"\n";
201 throw std::runtime_error(
"exportHistogramToAscii() is not implemented for built-in histograms" );
217 if( name.size() == 0 )
218 throw std::runtime_error(
"Name cannot be empty for an object." );
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 ) );
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