Jpp  18.0.1-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
14 #include "JLang/JObjectOutput.hh"
15 
16 #include "JTools/JQuantile.hh"
17 
18 #include "JAcoustics/JHit.hh"
19 #include "JAcoustics/JGlobalfit.hh"
20 #include "JAcoustics/JSuperEvt.hh"
22 
23 
24 namespace JACOUSTICS {}
25 namespace JPP { using namespace JACOUSTICS; }
26 
27 namespace JACOUSTICS {
28 
30 
31  /**
32  * Thread pool for global fits.
33  */
34  class JFremantle {
35  public:
36  /**
37  * Constructor.
38  *
39  * \param geometry detector geometry
40  * \param velocity sound velocity
41  * \param parameters parameters
42  * \param ns number of threads
43  */
44  JFremantle(const JGeometry& geometry,
45  const JSoundVelocity& velocity,
47  const size_t ns) :
48  stop(false)
49  {
50  using namespace std;
51  using namespace JPP;
52 
53  Q.reset();
54 
55  for (size_t i = 0; i < ns; ++i) {
56 
57  thread worker([this, geometry, velocity, parameters]() {
58 
60 
61  for (JGlobalfit katoomba(geometry, velocity, parameters); ; ) {
62 
63  {
64  unique_lock<mutex> lock(in);
65 
66  cv.wait(lock, [this]() { return stop || !input.empty(); });
67 
68  if (stop && input.empty()) {
69  return;
70  }
71 
72  data.swap(input.front());
73 
74  input.pop();
75  }
76 
77  const auto result = katoomba(data.begin(), data.end());
78 
79  if (result.chi2 / result.ndf <= katoomba.parameters.chi2perNDF) {
80 
81  {
82  unique_lock<mutex> lock(out);
83 
84  Q.put(result.chi2 / result.ndf);
85 
86  if (JFremantle::output != NULL) {
88  result.getTimeRange(),
89  data .size(),
90  result.size(),
91  result.value.getN(),
92  result.ndf,
93  result.chi2),
94  result.value,
95  result.begin,
96  result.end));
97  }
98  }
99  }
100  }
101  });
102 
103  workers.emplace_back(move(worker));
104  }
105  }
106 
107 
108  /**
109  * Destructor.
110  */
112  {
113  using namespace std;
114 
115  {
116  unique_lock<mutex> lock(in);
117 
118  stop = true;
119  }
120 
121  cv.notify_all();
122 
123  for (auto& worker : workers) {
124  worker.join();
125  }
126  }
127 
128 
129  /**
130  * Get number of pending data.
131  *
132  * \return number of pending data
133  */
134  size_t backlog()
135  {
136  using namespace std;
137 
138  {
139  unique_lock<mutex> lock(in);
140 
141  return input.size();
142  }
143  }
144 
145 
146  /**
147  * Queue data.
148  *
149  * \param data data
150  */
152  {
153  using namespace std;
154 
155  {
156  unique_lock<mutex> lock(in);
157 
158  if (stop) {
159  throw runtime_error("The thread pool has been stopped.");
160  }
161 
162  input.emplace(move(data));
163  }
164 
165  cv.notify_one();
166  }
167 
169 
170  static std::string oid; //!< detector identifier
171  static JTOOLS::JQuantile Q; //!< chi2/NDF
172  static output_type* output; //!< optional output
173 
174  private:
176  std::queue <data_type> input;
177  std::mutex in;
178  std::mutex out;
179  std::condition_variable cv;
180  bool stop;
181  };
182 }
183 
184 #endif
Fit function of acoustic model.
Acoustic hit.
~JFremantle()
Destructor.
void reset()
Reset.
Definition: JQuantile.hh:87
Auxiliary data structure for running average, standard deviation and quantiles.
Definition: JQuantile.hh:43
JLANG::JObjectOutput< JSuperEvt > output_type
std::vector< size_t > ns
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
std::condition_variable cv
Global fit of prameterised detector geometry to acoustics data.
Definition: JGlobalfit.hh:29
Acoustic super event fit toolkit.
static std::string oid
detector identifier
size_t backlog()
Get number of pending data.
Acoustic event header.
Acoustic event fit.
then awk string
void put(const double x, const double w=1.0)
Put value.
Definition: JQuantile.hh:133
std::queue< data_type > input
JFremantle(const JGeometry &geometry, const JSoundVelocity &velocity, const JFitParameters &parameters, const size_t ns)
Constructor.
Definition: JFremantle_t.hh:44
Implementation for depth dependend velocity of sound.
void enqueue(data_type &data)
Queue data.
static output_type * output
optional output
Thread pool for global fits.
Definition: JFremantle_t.hh:34
static JTOOLS::JQuantile Q
chi2/NDF
Template interface of object output for single data type.
virtual bool put(const T &object)=0
Object output.
std::vector< JHit > data_type
Definition: JFremantle_t.hh:29
Auxiliary data structure to convert model to super event.
std::vector< std::thread > workers