Jpp  18.1.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JPlatypus_t.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JPLATYPUS_T__
2 #define __JACOUSTICS__JPLATYPUS_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 
13 #include "JTools/JHashMap.hh"
14 #include "JTools/JQuantile.hh"
15 
16 #include "JAcoustics/JHit.hh"
17 #include "JAcoustics/JEmitter.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 JPlatypus {
35  public:
36  /**
37  * Constructor.
38  *
39  * \param geometry detector geometry
40  * \param emitters emitters geometry
41  * \param velocity sound velocity
42  * \param parameters parameters
43  * \param ns number of threads
44  */
45  JPlatypus(const JGeometry& geometry,
46  const JEmitters& emitters,
47  const JSoundVelocity& velocity,
49  const size_t ns) :
50  stop(false)
51  {
52  using namespace std;
53  using namespace JPP;
54 
55  Q.reset();
56 
57  for (size_t i = 0; i < ns; ++i) {
58 
59  thread worker([this, geometry, emitters, velocity, parameters]() {
60 
62 
63  for (JKatoomba<JGandalf> katoomba(geometry, velocity, parameters.option); ; ) {
64 
65  katoomba.estimator.reset(getMEstimator(parameters.mestimator));
66 
67  {
68  unique_lock<mutex> lock(in);
69 
70  cv.wait(lock, [this]() { return stop || !input.empty(); });
71 
72  if (stop && input.empty()) {
73  return;
74  }
75 
76  const JSuperEvt& evt = input.front();
77 
78  data.clear();
79 
80  for (JSuperEvt::rx_type::const_iterator hit = evt.rx.begin(); hit != evt.rx.end(); ++hit) {
81 
82  data.push_back(JHit(emitters[hit->id],
83  hit->counter,
84  JLocation(hit->string, hit->floor),
85  hit->toa,
86  parameters.sigma_s,
87  hit->weight));
88  }
89 
90  katoomba.value = getModel(evt);
91 
92  input.pop();
93  }
94 
95  const double chi2 = katoomba (data.begin(), data.end()) / katoomba.estimator->getRho(1.0);
96  const double ndf = getWeight(data.begin(), data.end()) - katoomba.value.getN();
97 
98  {
99  unique_lock<mutex> lock(out);
100 
101  Q.put(chi2 / ndf);
102  }
103  }
104  });
105 
106  workers.emplace_back(move(worker));
107  }
108  }
109 
110 
111  /**
112  * Destructor.
113  */
115  {
116  using namespace std;
117 
118  {
119  unique_lock<mutex> lock(in);
120 
121  stop = true;
122  }
123 
124  cv.notify_all();
125 
126  for (auto& worker : workers) {
127  worker.join();
128  }
129  }
130 
131 
132  /**
133  * Get number of pending data.
134  *
135  * \return number of pending data
136  */
137  size_t backlog()
138  {
139  using namespace std;
140 
141  {
142  unique_lock<mutex> lock(in);
143 
144  return input.size();
145  }
146  }
147 
148 
149  /**
150  * Queue super event.
151  *
152  * \param evt super event
153  */
154  void enqueue(const JSuperEvt& evt)
155  {
156  using namespace std;
157 
158  {
159  unique_lock<mutex> lock(in);
160 
161  if (stop) {
162  throw runtime_error("The thread pool has been stopped.");
163  }
164 
165  input.emplace(evt);
166  }
167 
168  cv.notify_one();
169  }
170 
171  static JTOOLS::JQuantile Q; //!< chi2/NDF
172 
173  private:
175  std::queue <JSuperEvt> input;
176  std::mutex in;
177  std::mutex out;
178  std::condition_variable cv;
179  bool stop;
180  };
181 }
182 
183 #endif
JModel getModel(const JEvt &evt)
Get model.
Acoustic hit.
double getWeight(T __begin, T __end)
Get total weight of data points.
Definition: JKatoomba_t.hh:59
void reset()
Reset.
Definition: JQuantile.hh:87
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:72
Auxiliary data structure for running average, standard deviation and quantiles.
Definition: JQuantile.hh:43
General purpose class for hash map of unique elements.
size_t backlog()
Get number of pending data.
Definition: JPlatypus_t.hh:137
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
Acoustic emitter.
Acoustics hit.
Acoustic super event fit toolkit.
Acoustic fit parameters.
Template specialisation of fit function of acoustic model based on JGandalf minimiser.
Definition: JKatoomba_t.hh:629
Logical location of module.
Definition: JLocation.hh:37
Acoustic event fit.
void put(const double x, const double w=1.0)
Put value.
Definition: JQuantile.hh:133
static JTOOLS::JQuantile Q
chi2/NDF
Definition: JPlatypus_t.hh:171
std::queue< JSuperEvt > input
Definition: JPlatypus_t.hh:175
std::vector< std::thread > workers
Definition: JPlatypus_t.hh:174
JPlatypus(const JGeometry &geometry, const JEmitters &emitters, const JSoundVelocity &velocity, const JFitParameters &parameters, const size_t ns)
Constructor.
Definition: JPlatypus_t.hh:45
Implementation for depth dependend velocity of sound.
Acoustic super event fit.
Definition: JSuperEvt.hh:36
~JPlatypus()
Destructor.
Definition: JPlatypus_t.hh:114
Thread pool for global fits.
Definition: JPlatypus_t.hh:34
Fit functions of acoustic model.
then if[[!-f $DETECTOR]] then JDetector sh $DETECTOR fi cat $WORKDIR trigger_parameters txt<< EOFtrigger3DMuon.enabled=1;trigger3DMuon.numberOfHits=5;trigger3DMuon.gridAngle_deg=1;ctMin=0.0;TMaxLocal_ns=15.0;EOF set_variable TRIGGEREFFICIENCY_TRIGGERED_EVENTS_ONLY INPUT_FILES=() for((i=1;$i<=$NUMBER_OF_RUNS;++i));do JSirene.sh $DETECTOR $JPP_DATA/genhen.km3net_wpd_V2_0.evt.gz $WORKDIR/sirene_ ${i}.root JTriggerEfficiency.sh $DETECTOR $DETECTOR $WORKDIR/sirene_ ${i}.root $WORKDIR/trigger_efficiency_ ${i}.root $WORKDIR/trigger_parameters.txt $JPP_DATA/PMT_parameters.txt INPUT_FILES+=($WORKDIR/trigger_efficiency_ ${i}.root) done for ANGLE_DEG in $ANGLES_DEG[*];do set_variable SIGMA_NS 3.0 set_variable OUTLIERS 3 set_variable OUTPUT_FILE $WORKDIR/matrix\[${ANGLE_DEG}\deg\].root $JPP_DIR/examples/JReconstruction-f"$INPUT_FILES[*]"-o $OUTPUT_FILE-S ${SIGMA_NS}-A ${ANGLE_DEG}-O ${OUTLIERS}-d ${DEBUG}--!fiif[[$OPTION=="plot"]];then if((0));then for H1 in h0 h1;do JPlot1D-f"$WORKDIR/matrix["${^ANGLES_DEG}" deg].root:${H1}"-y"1 2e3"-Y-L TR-T""-\^"number of events [a.u.]"-> o chi2
Definition: JMatrixNZ.sh:106
double sigma_s
time-of-arrival resolution [s]
int mestimator
M-estimator.
JMEstimator * getMEstimator(const int type)
Get M-Estimator.
Definition: JMEstimator.hh:203
void enqueue(const JSuperEvt &evt)
Queue super event.
Definition: JPlatypus_t.hh:154
std::condition_variable cv
Definition: JPlatypus_t.hh:178
JTOOLS::JHashMap< int, JEmitter > JEmitters
Definition: JPlatypus_t.hh:29