Jpp 20.0.0-rc.9-29-gccc23c492-D
the software that should make you happy
Loading...
Searching...
No Matches
JFremantle_t.hh
Go to the documentation of this file.
1#ifndef __JACOUSTICS__JFREMANTLE_T__
2#define __JACOUSTICS__JFREMANTLE_T__
3
4#include <string>
5#include <type_traits>
6#include <functional>
7#include <future>
8#include <mutex>
9#include <thread>
10#include <vector>
11#include <queue>
12#include <memory>
13#include <limits>
14
15#include "JLang/JObjectOutput.hh"
16
17#include "JMath/JQuantile_t.hh"
18
19#include "JAcoustics/JHit.hh"
23
24
25namespace JACOUSTICS {}
26namespace JPP { using namespace JACOUSTICS; }
27
28namespace JACOUSTICS {
29
30 /**
31 * Thread pool for global fits.
32 */
33 class JFremantle {
34 public:
35
36 typedef std::vector<JHit> input_type;
37 typedef JLANG::JObjectOutput<JSuperEvt> output_type;
38
39
40 /**
41 * Constructor.
42 *
43 * \param geometry detector geometry
44 * \param velocity sound velocity
45 * \param parameters parameters
46 * \param ns number of threads
47 * \param backlog backlog
48 */
49 JFremantle(const JGeometry& geometry,
50 const JSoundVelocity& velocity,
51 const JFitParameters& parameters,
52 const size_t ns,
53 const size_t backlog = std::numeric_limits<size_t>::max()) :
54 stop(false),
56 {
57 using namespace std;
58 using namespace JPP;
59
60 Q.reset();
61
62 for (size_t i = 0; i < ns; ++i) {
63
64 thread worker([this, geometry, velocity, parameters]() {
65
66 input_type data;
67
68 for (JGlobalfit katoomba(geometry, velocity, parameters); ; ) {
69
70 {
71 unique_lock<mutex> lock(in);
72
73 cv.wait(lock, [this]() { return stop || !input.empty(); });
74
75 if (stop && input.empty()) {
76 return;
77 }
78
79 data.swap(input.front());
80
81 input.pop();
82 }
83
84 cw.notify_one();
85
86 const auto result = katoomba(data.begin(), data.end());
87
88 if (result.chi2 / result.ndf <= katoomba.parameters.chi2perNDF) {
89
90 {
91 unique_lock<mutex> lock(out);
92
93 Q.put(result.chi2 / result.ndf);
94
95 if (JFremantle::output != NULL) {
97 result.getTimeRange(),
98 data .size(),
99 result.size(),
100 result.value.getN(),
101 result.ndf,
102 result.chi2,
103 katoomba.gandalf.numberOfIterations),
104 result.value,
105 result.begin,
106 JFremantle::squash ? result.begin : result.end));
107 }
108 }
109 }
110 }
111 });
112
113 workers.emplace_back(std::move(worker));
114 }
115 }
116
117
118 /**
119 * Destructor.
120 */
122 {
123 using namespace std;
124
125 {
126 unique_lock<mutex> lock(in);
127
128 stop = true;
129 }
130
131 cv.notify_all();
132
133 for (auto& worker : workers) {
134 worker.join();
135 }
136 }
137
138
139 /**
140 * Queue data.
141 *
142 * \param data data
143 */
144 void enqueue(input_type& data)
145 {
146 using namespace std;
147
148 {
149 unique_lock<mutex> lock(in);
150
151 cw.wait(lock, [this]() { return stop || input.size() <= backlog; });
152
153 if (stop) {
154 throw runtime_error("The thread pool has been stopped.");
155 }
156
157 input.emplace(std::move(data));
158 }
159
160 cv.notify_one();
161 }
162
163 static int detid; //!< detector identifier
164 static JMATH::JQuantile_t Q; //!< chi2/NDF
165 static bool squash; //!< squash transmissions in output
166 static output_type* output; //!< optional output
167
168 private:
169 std::vector<std::thread> workers;
170 std::queue <input_type> input;
171 std::mutex in;
172 std::mutex out;
173 std::condition_variable cv;
174 std::condition_variable cw;
175 bool stop;
176 size_t backlog;
177 };
178}
179
180#endif
Fit function of acoustic model.
Acoustic hit.
Acoustic super event fit toolkit.
Acoustic event fit.
Thread pool for global fits.
static output_type * output
optional output
std::condition_variable cw
JLANG::JObjectOutput< JSuperEvt > output_type
void enqueue(input_type &data)
Queue data.
std::vector< std::thread > workers
std::vector< JHit > input_type
static JMATH::JQuantile_t Q
chi2/NDF
std::condition_variable cv
static int detid
detector identifier
JFremantle(const JGeometry &geometry, const JSoundVelocity &velocity, const JFitParameters &parameters, const size_t ns, const size_t backlog=std::numeric_limits< size_t >::max())
Constructor.
static bool squash
squash transmissions in output
std::queue< input_type > input
Auxiliary classes and methods for acoustic position calibration.
Global fit of prameterised detector geometry to acoustics data.
Definition JGlobalfit.hh:29
Acoustic event header.
Definition JEvt.hh:157
Implementation for depth dependend velocity of sound.
Auxiliary data structure to convert model to super event.