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