15#ifdef HAS_CPP_STANDARD_17
16#include <range/v3/view/enumerate.hpp>
17#include <range/v3/view/filter.hpp>
18namespace stdrng = ranges;
21namespace stdrng = std::ranges;
27 inline auto get_size_in_bytes()
29 return static_cast<std::streamsize
>(
sizeof(T));
33 inline auto get_size_in_bytes(
const std::vector<T>& data)
35 return static_cast<std::streamsize
>(
sizeof(T) * data.size());
42 Mille::Mille(std::string_view outFileName,
bool asBinary,
bool writeZero)
48 (asBinary ? (std::ios::binary | std::ios::out | std::ios::trunc) : std::ios::out | std::ios::trunc));
51 throw std::runtime_error(fmt::format(
"Mille::Mille: Could not open {} as output file.", outFileName));
57 if (data_point.
sigma <= 0.)
64 if (
buffer_.get_current_size() == 0)
71 for (
const auto [index, value] :
72 stdrng::views::enumerate(data_point.
locals) |
73 stdrng::views::filter([
this](
const auto& index_deriv) ->
bool
74 { return std::get<1>(index_deriv) != 0 or is_zero_written_; }))
76 buffer_.add_entry(
static_cast<int>(index + 1), value);
81 for (
const auto& [label, deriv] : data_point.
globals)
87 buffer_.add_entry(label, deriv);
91 fmt::println(stderr,
"Mille::mille: Invalid label {} <= 0 or > ", label);
99 if (special_data.empty())
105 throw std::logic_error(
"Mille::special: Special values already stored for this record.");
107 if (
buffer_.get_current_size() == 0)
113 buffer_.add_entry(0, -
static_cast<float>(special_data.size()));
114 for (
const auto& [index, value] : special_data)
116 buffer_.add_entry(index, value);
134 const auto data_size =
static_cast<int>(
buffer_.get_current_size());
135 output_file_.write(
reinterpret_cast<const char*
>(&data_size), get_size_in_bytes<
decltype(data_size)>());
137 get_size_in_bytes(
buffer_.get_values()));
139 get_size_in_bytes(
buffer_.get_indices()));
160 throw std::runtime_error(
161 fmt::format(
"Mille::checkBufferSize: Buffer too short ({}), \n need space for nLocal ({}) \nGlobal "
162 "({}) local/global derivatives, {} already stored!",
void write_to_non_binary()
bool has_special_done_
if true, special(..) already called for this record
void mille(const MilleDataPoint &data_point)
Write the MilleDataPoint structure to the internal data buffer of the type MilleBuffer.
void check_buffer_size(std::size_t nLocal, std::size_t nGlobal)
Mille(std::string_view outFileName, bool asBinary=true, bool writeZero=false)
std::ofstream output_file_
C-binary for output.
bool is_binary_
if false output as text
static constexpr unsigned int max_label_size_
std::size_t max_buffer_size_
Maximum size of the data buffer.
bool is_zero_written_
if true also write out derivatives/labels ==0
MilleBuffer< int, float > buffer_
Data buffer to store the points of the events.
void special(const std::vector< std::pair< int, float > > &special_data)
void end()
Streaming an entry data to the output file.
std::vector< std::pair< int, float > > globals
std::vector< float > locals