Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RHistConcurrentFiller.hxx
Go to the documentation of this file.
1/// \file
2/// \warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
3/// Feedback is welcome!
4
5#ifndef ROOT_RHistConcurrentFiller
6#define ROOT_RHistConcurrentFiller
7
8#include "RHist.hxx"
10
11#include <exception>
12#include <memory>
13#include <mutex>
14#include <stdexcept>
15#include <vector>
16
17namespace ROOT {
18namespace Experimental {
19
20/**
21A histogram filler to concurrently fill an RHist.
22
23\code
24auto hist = std::make_shared<ROOT::Experimental::RHist<int>>(10, std::make_pair(5, 15));
25{
26 ROOT::Experimental::RHistConcurrentFiller filler(hist);
27 auto context = filler.CreateFillContext();
28 context.Fill(8.5);
29}
30// hist->GetBinContent(ROOT::Experimental::RBinIndex(3)) will return 1
31\endcode
32
33\warning This is part of the %ROOT 7 prototype! It will change without notice. It might trigger earthquakes.
34Feedback is welcome!
35*/
36template <typename BinContentType>
38 /// A pointer to the filled histogram
39 std::shared_ptr<RHist<BinContentType>> fHist;
40
41 /// Mutex to protect access to the list of fill contexts (not for filling itself!)
42 std::mutex fMutex;
43 /// The list of fill contexts, for checks during destruction
44 std::vector<std::weak_ptr<RHistFillContext<BinContentType>>> fFillContexts;
45
46public:
47 /// Create a filler object.
48 ///
49 /// \param[in] hist a pointer to the histogram
50 explicit RHistConcurrentFiller(std::shared_ptr<RHist<BinContentType>> hist) : fHist(hist)
51 {
52 if (!hist) {
53 throw std::invalid_argument("hist must not be nullptr");
54 }
55 }
56
61
63 {
64 for (const auto &context : fFillContexts) {
65 if (!context.expired()) {
66 // According to C++ Core Guideline C.36 "A destructor must not fail" and (C.37) "If a destructor tries to
67 // exit with an exception, it’s a bad design error and the program had better terminate".
68 std::terminate(); // GCOVR_EXCL_LINE
69 }
70 }
71 }
72
73 const std::shared_ptr<RHist<BinContentType>> &GetHist() const { return fHist; }
74
75 /// Create a new context for concurrent filling.
76 std::shared_ptr<RHistFillContext<BinContentType>> CreateFillContext()
77 {
78 // Cannot use std::make_shared because the constructor of RHistFillContext is private. Also it would mean that the
79 // (direct) memory of all contexts stays around until the vector of weak_ptr's is cleared.
80 std::shared_ptr<RHistFillContext<BinContentType>> context(new RHistFillContext<BinContentType>(*fHist));
81
82 {
83 std::lock_guard g(fMutex);
84 fFillContexts.push_back(context);
85 }
86
87 return context;
88 }
89};
90
91} // namespace Experimental
92} // namespace ROOT
93
94#endif
#define g(i)
Definition RSha256.hxx:105
A histogram filler to concurrently fill an RHist.
RHistConcurrentFiller(std::shared_ptr< RHist< BinContentType > > hist)
Create a filler object.
RHistConcurrentFiller & operator=(const RHistConcurrentFiller &)=delete
std::shared_ptr< RHist< BinContentType > > fHist
A pointer to the filled histogram.
RHistConcurrentFiller(const RHistConcurrentFiller &)=delete
std::mutex fMutex
Mutex to protect access to the list of fill contexts (not for filling itself!)
std::vector< std::weak_ptr< RHistFillContext< BinContentType > > > fFillContexts
The list of fill contexts, for checks during destruction.
RHistConcurrentFiller(RHistConcurrentFiller &&)=delete
std::shared_ptr< RHistFillContext< BinContentType > > CreateFillContext()
Create a new context for concurrent filling.
const std::shared_ptr< RHist< BinContentType > > & GetHist() const
RHistConcurrentFiller & operator=(RHistConcurrentFiller &&)=delete