Jpp 20.0.0-195-g190c9e876
the software that should make you happy
Loading...
Searching...
No Matches
JShowerDirectionPrefit.hh
Go to the documentation of this file.
1#ifndef JSHOWERDIRECTIONPREFIT_INCLUDE
2#define JSHOWERDIRECTIONPREFIT_INCLUDE
3
4#include <string>
5#include <iostream>
6#include <set>
7#include <vector>
8#include <algorithm>
9#include <memory>
10
13
16
17#include "JTrigger/JHitR0.hh"
18#include "JTrigger/JBuildL0.hh"
19
21
22#include "JFit/JPMTW0.hh"
24#include "JFit/JFitToolkit.hh"
25#include "JFit/JPoint4D.hh"
26#include "JFit/JModel.hh"
27
32
35
37
38#include "JMath/JConstants.hh"
39
45
46/**
47 * \author adomi, vcarretero
48 */
49namespace JRECONSTRUCTION {}
50namespace JPP { using namespace JRECONSTRUCTION; }
51
52namespace JRECONSTRUCTION {
53
60 using JFIT::JRegressor;
61 using JFIT::JEnergy;
62 using JFIT::JShower3EZ;
65
66
67 /**
68 * class to handle the direction fit of the shower reconstruction, mainly dedicated for ORCA
69 */
72 public JRegressor<JShower3EZ, JAbstractMinimiser>
73 {
74 public:
78
79 using JRegressor_t::operator();
80 using JRegressor_t::getH1;
81 using JRegressor_t::getH0;
82
84
85 /**
86 * Input data type.
87 */
88 struct input_type :
89 public JDAQEventHeader
90 {
91 /**
92 * Default constructor.
93 */
95 {}
96
97
98 /**
99 * Constructor.
100 *
101 * \param header header
102 * \param in start values
103 * \param coverage coverage
104 */
106 const JEvt& in,
107 const coverage_type& coverage) :
108 JDAQEventHeader(header),
109 in(in),
111 {}
112
116 };
117
118 /**
119 * Compute chi2 for a set of energies
120 *
121 * \param Ev energy vector
122 * \param data PMTs
123 * \param R Rotation
124 * \param sh shower
125 */
127 const std::vector<double>& Ev,
128 const std::vector<JFIT::JPMTW0>& data,
130 const JShower3EZ& sh
131 ) {
132 using namespace JPP;
133
134 std::vector<double> chi2(Ev.size(), 0.0);
135
136 for (auto i = data.begin(); i != data.end(); ++i) {
137
138 JPMTW0 pmt = *i;
139 pmt.rotate(R);
140
141 auto H1 = getH1(sh, pmt);
142 auto H0 = getH0(pmt.getR());
143
144 const bool hit = pmt.getN() != 0;
145 const double chi2_bg = getChi2(get_value(H0), hit);
146
147 for (size_t j = 0; j < Ev.size(); ++j) {
148 chi2[j] += getChi2(get_value(Ev[j] * H1 + H0), hit) - chi2_bg; ; // H1 is linear with E, -log-lik ratio
149 }
150 }
151
152 return chi2;
153 }
154
155 /**
156 * Find best energy iteratively increasing the upper threshold if there is suspicion the best-fit is outside
157 *
158 * \param Ev energy vector
159 * \param data PMTs
160 * \param R Rotation
161 * \param sh shower
162 */
165 const std::vector<JFIT::JPMTW0>& data,
167 const JShower3EZ& sh
168 ) {
169 using namespace JPP;
170
171 const int max_extensions = 5;
172 int extension_count = 0;
173
174 while (true) {
175
176 std::vector<double> chi2 = computeChi2(Ev, data, R, sh);
177
178 auto p = std::min_element(chi2.begin(), chi2.end());
179 size_t idx = p - chi2.begin();
180
181 bool at_upper_edge = (idx == Ev.size() - 1);
182
183 bool monotonic_tail = false;
184 if (Ev.size() >= 3) {
185 size_t n = Ev.size();
186 monotonic_tail = (chi2[n-3] > chi2[n-2]) &&
187 (chi2[n-2] > chi2[n-1]);
188 }
189
190 if (!(at_upper_edge && monotonic_tail)) {
191 return {Ev[idx], *p};
192 }
193
194 if (extension_count >= max_extensions) {
195 return {Ev[idx], *p};
196 }
197
198 extension_count++;
199
200 std::vector<double> Ev_ext;
201 double E_start = Ev.back();
202 double E_end = 100.0 * E_start;
203
204 size_t N = Ev.size();
205
206 for (size_t k = 0; k < N; ++k) {
207 double t = double (k) / (N - 1);
208 Ev_ext.push_back(E_start * std::pow(E_end / E_start, t));
209 }
210
211 Ev = std::move(Ev_ext);
212 }
213 }
214 /**
215 * Parameterized constructor
216 *
217 * \param parameters parameters
218 * \param storage storage
219 * \param pmtParameters PMT parameters
220 * \param debug debug
221 */
223 const storage_type& storage,
225 const int debug = 0):
227 JRegressor_t(storage),
229 {
230 using namespace JPP;
231
232 JRegressor_t::debug = debug;
233 JRegressor_t::T_ns.setRange(parameters.TMin_ns, parameters.TMax_ns);
234 JRegressor_t::Vmax_npe = VMax_npe;
235
236 if (Emin_GeV > Emax_GeV || En <= 1) {
237 THROW(JException, "Invalid energy input " << Emin_GeV << ' ' << Emax_GeV << ' ' << En);
238 }
239
240 const double base = std::pow((Emax_GeV / Emin_GeV), 1.0 / (En - 1));
241
242 for (int i = 0; i != En; ++i) {
243 Ev.push_back(Emin_GeV * std::pow(base, i));
244 }
245 }
246
247 /**
248 * Get input data.
249 *
250 * \param router module router
251 * \param summary summary data
252 * \param event event
253 * \param in start values
254 * \param coverage coverage
255 * \return input data
256 */
258 const JSummaryRouter& summary,
259 const JDAQEvent& event,
260 const JEvt& in,
261 const coverage_type& coverage) const
262 {
263 using namespace std;
264 using namespace JTRIGGER;
265
266 input_type input(event.getDAQEventHeader(), in, coverage);
267
268 const JBuildL0 <JHitR0> buildL0;
270
271 const JDAQTimeslice timeslice(event, true);
272
273 JSuperFrame2D<JHit> buffer;
274
275 for (JDAQTimeslice::const_iterator i = timeslice.begin(); i != timeslice.end(); ++i) {
276
277 if (router.hasModule(i->getModuleID())) {
278
279 buffer(*i, router.getModule(i->getModuleID()));
280
281 buildL0(buffer, back_inserter(data[i->getModuleID()]));
282 }
283 }
284
285 for (const auto& module : router.getReference()) {
286 if (!module.empty()) {
287 input.data.push_back(module_type(module, summary.getSummaryFrame(module.getID(), R_Hz), data[module.getID()]));
288 }
289 }
290
291 return input;
292 }
293
294 /**
295 * Fit function.
296 *
297 * \param input input data
298 * \return fit results
299 */
301 {
302 using namespace std;
303 using namespace JPP;
304
306
307 JEvt out;
308
309 JEvt in = input.in;
310
312
313 if (!in.empty()) {
314 in.select(JHistory::is_event(in.begin()->getHistory()));
315 }
316
317 for (JEvt::const_iterator shower = in.begin(); shower != in.end(); ++shower) {
318
319 vector<JPMTW0> data;
320
321 const JPosition3D vertex(getPosition(*shower));
322 const double time = shower->getT();
323
324 for (const auto& module : input.data) {
325
326 JPosition3D pos(module->getPosition());
327
328 pos.sub(vertex);
329
330 if (pos.getLength() <= DMax_m) {
331
332 const double t1 = time + pos.getLength() * getInverseSpeedOfLight() * getIndexOfRefraction();
333
334 for (size_t i = 0; i != module->size(); ++i) {
335
336 if (module.getStatus(i)) {
337
338 struct {
339
340 bool operator()(const JHitR0& hit) const
341 {
342 return (hit.getPMT() == pmt && T_ns(hit.getT()));
343 }
344
345 const JTimeRange T_ns;
346 const size_t pmt;
347
348 } match = { JRegressor_t::T_ns + t1, i };
349
350 const JPMTIdentifier id(module->getID(), i);
351
353
354 const size_t ns = count_if(module.begin(), module.end(), match);
355 const double QE = wip.QE;
356
357 JPMT pmt = module->getPMT(i);
358
359 pmt.sub(vertex);
360
361 data.push_back(JPMTW0(pmt, QE, module.frame.getRate(i), ns));
362
363 }
364 }
365 }
366 }
367
368 JVector3D start_dir(0,0,0);
369
370 for (vector<JPMTW0>::const_iterator i = data.begin(); i != data.end(); ++i) {
371 if (i->getN() != 0) start_dir.add(i->getPosition());
372 }
373
374 JOmega3D scan_directions(JDirection3D(start_dir),
375 JOmega3D::range_type(0.0,PI),
376 scanAngle_deg * PI / 180.0);
377
378 const JShower3EZ sh(JVertex3D(JVector3D(0,0,0), shower->getT()), JVersor3Z(), 1);
379
380 for (JOmega3D_t::const_iterator dir = scan_directions.begin(); dir != scan_directions.end(); ++dir) {
381
382 const JGEOMETRY3D::JRotation3D R(*dir);
383
385
386 out.push_back(getFit(JHistory(shower->getHistory(), event()),
387 JShower3D(JVertex3D(vertex,time), JDirection3D(*dir)),
388 getQuality(result.second),
389 data.size(),
390 result.first));
391
392 // set additional values
393 out.rbegin()->setW(JPP_COVERAGE_ORIENTATION, input.coverage.orientation);
394 out.rbegin()->setW(JPP_COVERAGE_POSITION, input.coverage.position);
395
396 }
397 }
398
399 // apply default sorter
400
401 sort(out.begin(), out.end(), qualitySorter);
402
403 copy(input.in.begin(), input.in.end(), back_inserter(out));
404
405 return out;
406
407 }
408
410 };
411}
412
413#endif
Coverage of dynamical detector calibration.
Auxiliary class to extract a subset of optical modules from a detector.
Data structure for detector geometry and calibration.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Auxiliary methods to evaluate Poisson probabilities and chi2.
Basic data structure for L0 hit.
Mathematical constants.
int debug
debug level
Definition JSirene.cc:74
Direct access to module in detector data structure.
Data regression method for JFIT::JShower3EZ.
Router for direct addressing of module data in detector data structure.
bool hasModule(const JObjectID &id) const
Has module.
const JModule & getModule(const JObjectID &id) const
Get module parameters.
Data structure for a composite optical module.
Definition JModule.hh:76
Auxiliary class for map of PMT parameters.
const JPMTParameters & getPMTParameters(const JPMTIdentifier &id) const
Get PMT parameters.
Data structure for PMT parameters.
double QE
relative quantum efficiency
Data structure for PMT geometry, calibration and status.
Definition JPMT.hh:49
Abstract minimiser.
Definition JRegressor.hh:27
Data structure for fit of energy.
Data structure for set of track fit results.
void select(const JSelector_t &selector)
Select fits.
Data structure for fit of straight line in positive z-direction with energy.
Definition JShower3EZ.hh:30
JAxis3D & rotate(const JRotation3D &R)
Rotate axis.
Definition JAxis3D.hh:225
Data structure for direction in three dimensions.
Direction set covering (part of) solid angle.
Definition JOmega3D.hh:68
Data structure for position in three dimensions.
Data structure for vector in three dimensions.
Definition JVector3D.hh:36
JVector3D & add(const JVector3D &vector)
Add vector.
Definition JVector3D.hh:142
double getLength() const
Get length.
Definition JVector3D.hh:246
JVector3D & sub(const JVector3D &vector)
Subtract vector.
Definition JVector3D.hh:158
Data structure for normalised vector in positive z-direction.
Definition JVersor3Z.hh:41
General exception.
Definition JException.hh:24
const JClass_t & getReference() const
Get reference to object.
Definition JReference.hh:38
class to handle the direction fit of the shower reconstruction, mainly dedicated for ORCA
std::vector< double > computeChi2(const std::vector< double > &Ev, const std::vector< JFIT::JPMTW0 > &data, const JGEOMETRY3D::JRotation3D &R, const JShower3EZ &sh)
Compute chi2 for a set of energies.
std::pair< double, double > findBestEnergy(std::vector< double > Ev, const std::vector< JFIT::JPMTW0 > &data, const JGEOMETRY3D::JRotation3D &R, const JShower3EZ &sh)
Find best energy iteratively increasing the upper threshold if there is suspicion the best-fit is out...
input_type getInput(const JModuleRouter &router, const JSummaryRouter &summary, const JDAQEvent &event, const JEvt &in, const coverage_type &coverage) const
Get input data.
JShowerDirectionPrefit(const JShowerDirectionPrefitParameters_t &parameters, const storage_type &storage, const JPMTParametersMap &pmtParameters, const int debug=0)
Parameterized constructor.
JEvt operator()(const input_type &input)
Fit function.
JRegressor< JShower3EZ, JAbstractMinimiser > JRegressor_t
Router for fast addressing of summary data in KM3NETDAQ::JDAQSummaryslice data structure as a functio...
const JDAQSummaryFrame & getSummaryFrame(const JDAQModuleIdentifier &module) const
Get summary frame.
Reduced data structure for L0 hit.
Definition JHitR0.hh:27
JPMT_t getPMT() const
Get PMT.
Definition JHitR0.hh:60
double getT() const
Get calibrated time of hit.
2-dimensional frame with time calibrated data from one optical module.
const JDAQEventHeader & getDAQEventHeader() const
Get DAQ event header.
Data storage class for rate measurements of all PMTs in one module.
static const int JPP_COVERAGE_POSITION
coverage of dynamic position calibration of this event
static const int JPP_COVERAGE_ORIENTATION
coverage of dynamic orientation calibration of this event
double getChi2(const double P)
Get chi2 corresponding to given probability.
double getIndexOfRefraction()
Get average index of refraction of water corresponding to group velocity.
const double getInverseSpeedOfLight()
Get inverse speed of light.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
double getQuality(const double chi2, const int N, const int NDF)
Get quality of fit.
JPosition3D getPosition(const JFit &fit)
Get position.
JFIT::JHistory JHistory
Definition JHistory.hh:455
void copy(const JFIT::JEvt::const_iterator __begin, const JFIT::JEvt::const_iterator __end, Evt &out)
Copy tracks.
bool qualitySorter(const JFit &first, const JFit &second)
Comparison of fit results.
JFit getFit(const JHistory &history, const JTrack3D &track, const double Q, const int NDF, const double energy=0.0, const int status=SINGLE_STAGE)
Get fit.
return result
Definition JPolint.hh:862
const int n
Definition JPolint.hh:791
if((p==this->begin() &&this->getDistance(x,(p++) ->getX()) > distance_type::precision)||(p==this->end() &&this->getDistance((--p) ->getX(), x) > distance_type::precision))
Template base class for polynomial interpolations with polynomial result.
Definition JPolint.hh:775
int j
Definition JPolint.hh:801
JResultEvaluator< JResult_t >::result_type get_value(const JResult_t &value)
Helper method to recursively evaluate a to function value.
Definition JResult.hh:998
Auxiliary classes and methods for triggering.
Data structure for coverage of detector by dynamical calibrations.
Definition JCoverage.hh:19
double position
coverage of detector by available position calibration [0,1]
Definition JCoverage.hh:42
double orientation
coverage of detector by available orientation calibration [0,1]
Definition JCoverage.hh:41
Auxiliary class for historical event.
Definition JHistory.hh:40
Auxiliary class to test history.
Definition JHistory.hh:157
Auxiliary class for handling PMT geometry, rate and response.
Definition JPMTW0.hh:24
int getN() const
Get number of hits.
Definition JPMTW0.hh:82
double getR() const
Get rate.
Definition JPMTW0.hh:71
Template definition of a data regressor of given model.
Definition JRegressor.hh:70
Auxiliary class for handling module response.
Definition JModuleL0.hh:45
input_type(const JDAQEventHeader &header, const JEvt &in, const coverage_type &coverage)
Constructor.