trout
Phlex-based reconstruction framework for the SHiP experiment.
Loading...
Searching...
No Matches
HistoFileService.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 CERN for the benefit of the SHiP Collaboration
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
5// HistoFileService.hpp — shared ROOT output file for validation histograms.
6// Opened once; every subdetector histogrammer keeps it alive via
7// shared_ptr and Put()s its own histograms into it at its own destruction,
8// so it doesn't matter which histogrammer is torn down last.
9
10#pragma once
11
12#include <ROOT/Hist/ConvertToTH1.hxx>
13#include <ROOT/RFile.hxx>
14#include <ROOT/RHist.hxx>
15#include <TH1D.h>
16
17#include <cstdint>
18#include <exception>
19#include <memory>
20#include <mutex>
21#include <spdlog/spdlog.h>
22#include <string>
23#include <utility>
24
25namespace SHiP::trout {
26
28 public:
29 explicit HistoFileService(std::string filename)
30 : filename_{std::move(filename)}, file_{ROOT::Experimental::RFile::Recreate(filename_)} {}
31
32 void put(char const* name, char const* title, ROOT::Experimental::RHist<double> const& h) {
33 try {
34 std::scoped_lock lock{mutex_};
35 auto th1 = ROOT::Experimental::Hist::ConvertToTH1D(h);
36 th1->SetNameTitle(name, title);
37 file_->Put(name, *th1);
38 } catch (std::exception const& e) {
39 // RException, filesystem errors etc. — must not escape a destructor.
40 try {
41 spdlog::error("HistoFileService: failed to write histogram '{}' to '{}': {}", name,
42 filename_, e.what());
43 } catch (...) {
44 }
45 }
46 }
47
48 private:
49 std::string filename_;
50 std::unique_ptr<ROOT::Experimental::RFile> file_;
51 std::mutex mutex_;
52};
53
54// Shared shorthand for the 1D double-binned histograms every histogrammer
55// in this project books.
56inline std::shared_ptr<ROOT::Experimental::RHist<double>> make_hist(std::uint64_t nbins, double low,
57 double high) {
58 return std::make_shared<ROOT::Experimental::RHist<double>>(nbins, std::make_pair(low, high));
59}
60
61} // namespace SHiP::trout
void put(char const *name, char const *title, ROOT::Experimental::RHist< double > const &h)
Definition HistoFileService.hpp:32
HistoFileService(std::string filename)
Definition HistoFileService.hpp:29
Definition fit_seed.cpp:16
std::shared_ptr< ROOT::Experimental::RHist< double > > make_hist(std::uint64_t nbins, double low, double high)
Definition HistoFileService.hpp:56