Hello everyone,
Thank you both for your answers.
I’ll keep in mind the possibility to use Python if I do not succeed to do it as @Wile_E_Coyote proposed.
In order to save boost::histogram into a ROOT file, I created a simple example whose goal was to write a Boost and a ROOT histograms into a ROOT file.
The main file is
#include <iostream>
#include <fstream>
#include <sstream>
#include "TH1F.h"
#include "TFile.h"
#include "TTree.h"
#include "TRandom.h"
#include <boost/histogram.hpp>
#include <boost/format.hpp>
#include "BDSBH4D.h"
int main() {
BDSBH4D* h1 = new BDSBH4D();
h1->h.operator()(0.7,0.7,0.7,120);
// Print of the 4D - histogram.
std::ostringstream os4;
for (auto&& x : indexed(h1->h)) {
os4 << boost::format("(%i, %i, %i, %i) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) [%.3f, %.3f) %i\n")
% x.index(0) % x.index(1) % x.index(2) % x.index(3)
% x.bin(0).lower() % x.bin(0).upper()
% x.bin(1).lower() % x.bin(1).upper()
% x.bin(2).lower() % x.bin(2).upper()
% x.bin(3).lower() % x.bin(3).upper()
% *x;
}
std::cout << os4.str() << std::flush;
TH1F* histogram = new TH1F("stored_histogram","histogram;X;# of entries",100,-5,5);
histogram->FillRandom("gaus");
TFile *hfile = new TFile("my_rootfile.root","RECREATE","Example");
TTree *tree = new TTree("Event","A Root Tree");
tree->Branch("Histos",&histogram,32000,0);
tree->Branch("BOOST_histogram","BDSBH4D",&h1,32000,0);
tree->Fill();
hfile->Write();
hfile->ls();
hfile->Close();
return 0;
}
with BDSBH4D.cpp
#include "BDSBH4D.h"
ClassImp(BDSBH4D)
BDSBH4D::BDSBH4D()
{
h = boost::histogram::make_histogram(boost::histogram::axis::regular<double> {3, 0.0, 1.0, "x"},
boost::histogram::axis::regular<double> {3, 0.0, 1.0, "y"},
boost::histogram::axis::regular<double> {3, 0.0, 1.0, "z"},
boost::histogram::axis::regular<double, boost::histogram::axis::transform::log> {3, 1.0, 230.0, "Energy_log"});
}
BDSBH4D.h
#ifndef ROOT_TEST_BDSBH4D_HH
#define ROOT_TEST_BDSBH4D_HH
#include <boost/histogram.hpp>
#include "Rtypes.h"
#include "TObject.h"
typedef boost::histogram::histogram<std::__1::tuple<boost::histogram::axis::regular<double, boost::use_default, boost::use_default, boost::use_default>, boost::histogram::axis::regular<double, boost::use_default, boost::use_default, boost::use_default>, boost::histogram::axis::regular<double, boost::use_default, boost::use_default, boost::use_default>, boost::histogram::axis::regular<double, boost::histogram::axis::transform::log, boost::use_default, boost::use_default> >, boost::histogram::unlimited_storage<std::__1::allocator<char> > > boost_histogram;
class BDSBH4D : public TObject {
public:
BDSBH4D();
virtual ~BDSBH4D() {;}
boost_histogram h;
ClassDef(BDSBH4D,1);
};
#endif //ROOT_TEST_BDSBH4D_HH
and BDSBH4DLinkDef.h
#ifndef ROOT_TEST_BDSBH4DLINKDEF_H
#define ROOT_TEST_BDSBH4DLINKDEF_H
#pragma link C++ class boost::histogram::histogram<std::__1::tuple<boost::histogram::axis::regular<double, boost::use_default, boost::use_default, boost::use_default>, boost::histogram::axis::regular<double, boost::use_default, boost::use_default, boost::use_default>, boost::histogram::axis::regular<double, boost::use_default, boost::use_default, boost::use_default>, boost::histogram::axis::regular<double, boost::histogram::axis::transform::log, boost::use_default, boost::use_default> >, boost::histogram::unlimited_storage<std::__1::allocator<char> > >+;
#pragma link C++ class BDSBH4D+;
#pragma link C++ class boost::histogram::detail::mutex_base<tuple<boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default>,boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default>,boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default>,boost::histogram::axis::regular<double,boost::histogram::axis::transform::log,boost::use_default,boost::use_default> >,boost::histogram::unlimited_storage<allocator<char> >,boost::histogram::detail::null_mutex>+;
#pragma link C++ class boost::empty_::empty_value<boost::histogram::detail::null_mutex,0,true>+;
#pragma link C++ class boost::histogram::detail::null_mutex+;
#pragma link C++ class tuple<boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default>,boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default>,boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default>,boost::histogram::axis::regular<double,boost::histogram::axis::transform::log,boost::use_default,boost::use_default> >+;
#pragma link C++ class boost::histogram::unlimited_storage<allocator<char> >+;
#pragma link C++ class boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default>+;
#pragma link C++ class boost::histogram::unlimited_storage<allocator<char> >::buffer_type+;
#pragma link C++ class boost::histogram::axis::regular<double,boost::histogram::axis::transform::log,boost::use_default,boost::use_default>+;
#pragma link C++ class boost::histogram::axis::iterator_mixin<boost::histogram::axis::regular<double,boost::use_default,boost::use_default,boost::use_default> >+;
#pragma link C++ class boost::histogram::axis::transform::id+;
#pragma link C++ class boost::histogram::axis::metadata_base<boost::use_default,string>+;
#pragma link C++ class allocator<char>+;
#pragma link C++ class boost::histogram::axis::iterator_mixin<boost::histogram::axis::regular<double,boost::histogram::axis::transform::log,boost::use_default,boost::use_default> >+;
#pragma link C++ class boost::histogram::axis::transform::log+;
#pragma link C++ class boost::empty_::empty_value<string,0,false>+;
#endif //ROOT_TEST_BDSBH4DLINKDEF_H
Unfortunately, when I run this simple example, I get this error:
Error in <TStreamerInfo::Build>: boost::histogram::unlimited_storage<allocator<char> >::buffer_type, unknown type: void* ptr
coming from the boost::histogram::unlimited_storage.hpp file at the line 256
mutable void* ptr = nullptr;
I have quite some difficulty to correct this error as I do not understand why ROOT detect void* ptr
as a type.
Have you already encountered such a situation? I am quite new with c++ and dictionaries so maybe I forgot a step.
I already thank you for your help,
Here is a dropbox link to the small example if it can help: https://www.dropbox.com/sh/c9jjnuonkinumy5/AACs9uz4-s9Cg8l5bfjnjPdHa?dl=0
Have a nice day,
Eliott Ramoisiaux