How to declare a std::vector of boost histograms ? What is the type of a boost histogram?

Viewed 198

I am currently working onto a project where I need to work with a std::vector of boost histograms.

The problem I encountered is that I am not able to find the right type of a boost histogram. I let my last attempt in the code below.

Here is an example code of the situation:

#include <boost/format.hpp>
#include <boost/histogram.hpp>
#include <boost/histogram/serialization.hpp> // includes serialization code

int main() {
    using namespace boost::histogram;

    // Creation of the histogram.
    auto h = make_histogram(axis::regular<double> {3, 0.0, 1.0, "x"},
                            axis::regular<double> {3, 0.0, 1.0, "y"},
                            axis::regular<double> {3, 0.0, 1.0, "z"},
                            axis::regular<double, axis::transform::log> {3, 1.0, 230.0, "Energy_log"});

        h(0.1,0.1,0.1,70);
        h(0.1,0.1,0.1,100);
        h(0.1,0.1,0.1,200);

        //std::vector<*Boost_histogram_TYPE*> histograms4D;

        std::vector<boost::histogram::histogram<boost::histogram::axis::variant, boost::histogram::unlimited_storage>> histograms4D;

        histograms4D.push_back(h);

} 

Does anyone has already encountered such a situation ?

I already thank you for your help,

Eliott

2 Answers

You could just name the type, once you have an object of that type:

using boost_histogram = decltype(h);

and once you have the type, you could use it like this:

std::vector<boost_histogram> histograms4D;

Here's a demo.

When using c++17, use constructor template argument deduction:

Live On Coliru

#include <boost/archive/text_oarchive.hpp>
#include <boost/histogram.hpp>
#include <iostream>

int main() {
    using namespace boost::histogram;

    auto h = make_histogram(
            axis::regular<double> {3, 0.0, 1.0, "x"},
            axis::regular<double> {3, 0.0, 1.0, "y"},
            axis::regular<double> {3, 0.0, 1.0, "z"},
            axis::regular<double, axis::transform::log> {3, 1.0, 230.0, "Energy_log"});

    h(0.1,0.1,0.1,70);
    h(0.1,0.1,0.1,100);
    h(0.1,0.1,0.1,200);

    std::vector histograms4D {h};
}
Related