Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RProfileConcurrentFiller.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_RProfileConcurrentFiller
6#define ROOT_RProfileConcurrentFiller
7
8#include "RProfile.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 RProfile.
22
23\code
24auto profile = std::make_shared<ROOT::Experimental::RProfile>(10, std::make_pair(5, 15));
25{
26 ROOT::Experimental::RProfileConcurrentFiller filler(profile);
27 auto context = filler.CreateFillContext();
28 context->Fill(8.5, 23.0);
29}
30// profile->GetBinContent(ROOT::Experimental::RBinIndex(3)) will return the filled bin content
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*/
37 /// A pointer to the filled profile histogram
38 std::shared_ptr<RProfile> fProfile;
39
40 /// Mutex to protect access to the list of fill contexts (not for filling itself!)
41 std::mutex fMutex;
42 /// The list of fill contexts, for checks during destruction
43 std::vector<std::weak_ptr<RProfileFillContext>> fFillContexts;
44
45public:
46 /// Create a filler object.
47 ///
48 /// \param[in] hist a pointer to the histogram
49 explicit RProfileConcurrentFiller(std::shared_ptr<RProfile> profile) : fProfile(profile)
50 {
51 if (!profile) {
52 throw std::invalid_argument("profile must not be nullptr");
53 }
54 }
55
60
62 {
63 for (const auto &context : fFillContexts) {
64 if (!context.expired()) {
65 // According to C++ Core Guideline C.36 "A destructor must not fail" and (C.37) "If a destructor tries to
66 // exit with an exception, it’s a bad design error and the program had better terminate".
67 std::terminate(); // GCOVR_EXCL_LINE
68 }
69 }
70 }
71
72 const std::shared_ptr<RProfile> &GetProfile() const { return fProfile; }
73
74 /// Create a new context for concurrent filling.
75 std::shared_ptr<RProfileFillContext> CreateFillContext()
76 {
77 // Cannot use std::make_shared because the constructor of RProfileFillContext is private. Also it would mean that
78 // the (direct) memory of all contexts stays around until the vector of weak_ptr's is cleared.
79 std::shared_ptr<RProfileFillContext> context(new RProfileFillContext(*fProfile));
80
81 {
82 std::lock_guard g(fMutex);
83 fFillContexts.push_back(context);
84 }
85
86 return context;
87 }
88};
89
90} // namespace Experimental
91} // namespace ROOT
92
93#endif
#define g(i)
Definition RSha256.hxx:105
A histogram filler to concurrently fill an RProfile.
RProfileConcurrentFiller(RProfileConcurrentFiller &&)=delete
RProfileConcurrentFiller(const RProfileConcurrentFiller &)=delete
std::shared_ptr< RProfile > fProfile
A pointer to the filled profile histogram.
std::shared_ptr< RProfileFillContext > CreateFillContext()
Create a new context for concurrent filling.
RProfileConcurrentFiller & operator=(RProfileConcurrentFiller &&)=delete
RProfileConcurrentFiller(std::shared_ptr< RProfile > profile)
Create a filler object.
std::mutex fMutex
Mutex to protect access to the list of fill contexts (not for filling itself!)
RProfileConcurrentFiller & operator=(const RProfileConcurrentFiller &)=delete
const std::shared_ptr< RProfile > & GetProfile() const
std::vector< std::weak_ptr< RProfileFillContext > > fFillContexts
The list of fill contexts, for checks during destruction.
A context to concurrently fill an RProfile.