Jpp 20.0.0-rc.2
the software that should make you happy
Loading...
Searching...
No Matches
JBallarat.cc File Reference

Auxiliary program to process AHRS data. More...

#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <utility>
#include <vector>
#include "km3net-dataformat/definitions/module_status.hh"
#include "JDB/JSupport.hh"
#include "JDB/JAHRS.hh"
#include "JDB/JAHRSCalibration_t.hh"
#include "JDB/JAHRSToolkit.hh"
#include "JSupport/JMultipleFileScanner.hh"
#include "JSupport/JFileRecorder.hh"
#include "JSupport/JMeta.hh"
#include "JDetector/JDetector.hh"
#include "JDetector/JDetectorToolkit.hh"
#include "JDetector/JModuleRouter.hh"
#include "JDetector/JCompass.hh"
#include "JGeometry3D/JQuaternion3D.hh"
#include "JGeometry3D/JEigen3D.hh"
#include "JTools/JHashMap.hh"
#include "JLang/JVectorize.hh"
#include "JLang/JComparator.hh"
#include "JMath/JLegendre.hh"
#include "JCompass/JEvt.hh"
#include "JCompass/JEvtToolkit.hh"
#include "JCompass/JSupport.hh"
#include "JCompass/JNOAA.hh"
#include "JCompass/JPolicy.hh"
#include "JCompass/JCompassSupportkit.hh"
#include "Jeep/JPrint.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Detailed Description

Auxiliary program to process AHRS data.

The AHRS calibration as well as the quaternion calibration are applied to the data.
Possible outliers are removed according the specified maximal angle (option -S <angle_deg>.
For this, interpolations of the data are made based on Legendre polynomials with the number of points and the number of degrees
set to NUMBER_OF_POINTS and NUMBER_OF_DEGREES, respectively.
A policy for missing compass data is applied in which data from neighbouring compasses are used.
The number of floors that will be covered can be specified with option -N <npy>.
Finally, data within a given time window can be averaged (option -T <T_s>.
)

Author
mdejong

Definition in file software/JCompass/JBallarat.cc.

Function Documentation

◆ main()

int main ( int argc,
char ** argv )

Default constructor.

Constructor.

Parameters
t1time [s]
Qquaternion
nsnumber of points
policytrue if based on policy

Get common policy.

Parameters
__beginbegin of elements
__endend of elements
Returns
true if one or more elements are based on policy; else false

Get common policy.

Parameters
__beginbegin of elements
__endend of elements
Returns
true if all elements are based on policy; else false

Definition at line 66 of file software/JCompass/JBallarat.cc.

67{
68 using namespace std;
69 using namespace JPP;
70
71 JMultipleFileScanner_t inputFile;
72 counter_type numberOfEvents;
74 string detectorFile;
75 string ahrsFile;
76 double angle_deg;
77 double T_s;
78 size_t npy;
79 int debug;
80
81 try {
82
83 JParser<> zap("Auxiliary program to process AHRS data.");
84
85 zap['f'] = make_field(inputFile, "output of JConvertDB -q ahrs");
86 zap['n'] = make_field(numberOfEvents) = JLimit::max();
87 zap['a'] = make_field(detectorFile);
88 zap['c'] = make_field(ahrsFile, "output of JAHRSCalibration");
89 zap['o'] = make_field(outputFile, "ROOT formatted orientations file");
90 zap['S'] = make_field(angle_deg, "maximal angle w.r.t. fit") = 0.0;
91 zap['T'] = make_field(T_s, "time window for averaging") = 0.0;
92 zap['N'] = make_field(npy, "number of floors for missing data policy") = 1;
93 zap['d'] = make_field(debug) = 1;
94
95 zap(argc, argv);
96 }
97 catch(const exception &error) {
98 FATAL(error.what() << endl);
99 }
100
101
103
104 try {
105 load(detectorFile, detector);
106 }
107 catch(const JException& error) {
108 FATAL(error);
109 }
110
111 const JModuleRouter router(detector);
112 const JNOAAFunction1D_t& getMagneticDeclination = (isARCADetector(detector) ? static_cast<const JNOAAFunction1D_t&>(getARCAMagneticDeclination) :
114 static_cast<const JNOAAFunction1D_t&>(getZEROMagneticDeclination));
117 0.0);
118
119 NOTICE("Magnetic declination " << getMagneticDeclination << endl);
120 NOTICE("Meridian angle [rad] " << FIXED(5,3) << meridian << endl);
121
122 const JAHRSCalibration_t calibration(ahrsFile.c_str());
124
125
126 struct element_type;
128 typedef JHashMap<int, buffer_type> map_type; // container for temporary storage of data
129
130
131 struct element_type {
132 /**
133 * Default constructor.
134 */
135 element_type() :
136 first(0.0),
137 second(),
138 ns(0),
139 policy(false)
140 {}
141
142 /**
143 * Constructor.
144 *
145 * \param t1 time [s]
146 * \param Q quaternion
147 * \param ns number of points
148 * \param policy true if based on policy
149 */
150 element_type(const double t1,
151 const JQuaternion3D& Q,
152 const size_t ns,
153 const bool policy) :
154 first(t1),
155 second(Q),
156 ns(ns),
157 policy(policy)
158 {}
159
160 /**
161 * Get common policy.
162 *
163 * \param __begin begin of elements
164 * \param __end end of elements
165 * \return true if one or more elements are based on policy; else false
166 */
167 static inline bool getOR(buffer_type::const_iterator __begin,
168 buffer_type::const_iterator __end)
169 {
170 for (buffer_type::const_iterator i = __begin; i != __end; ++i) {
171 if (i->policy) {
172 return true;
173 }
174 }
175
176 return false;
177 }
178
179 /**
180 * Get common policy.
181 *
182 * \param __begin begin of elements
183 * \param __end end of elements
184 * \return true if all elements are based on policy; else false
185 */
186 static inline bool getAND(buffer_type::const_iterator __begin,
187 buffer_type::const_iterator __end)
188 {
189 for (buffer_type::const_iterator i = __begin; i != __end; ++i) {
190 if (!i->policy) {
191 return false;
192 }
193 }
194
195 return true;
196 }
197
198 double first; // time [s]
199 JQuaternion3D second; // quaternion
200 size_t ns; // number of points
201 bool policy; // true if based on policy; else false
202 };
203
204
205 JLegendre<JQuaternion3D, NUMBER_OF_DEGREES> f1; // interpolator for outlier removal
206
207
208 outputFile.open();
209
210 outputFile.put(JMeta(argc, argv));
211
212 counter_type counter = 0;
213
214 for (JMultipleFileScanner_t::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
215
216 STATUS("processing file " << *file_name << "... " << flush);
217
218 // collect data
219
220 map_type data;
221
222 for (JMultipleFileScanner<JAHRS> in(*file_name); in.hasNext() && counter != numberOfEvents; ++counter) {
223
224 const JAHRS* parameters = in.next();
225 const int id = parameters->DOMID;
226
227 if (is_valid(*parameters) && calibration.has(id) && router.hasModule(id)) {
228
229 const JModule& module = router.getModule(id);
230
231 if (module.getFloor() != 0 && !module.has(COMPASS_DISABLE)) {
232
233 JCompass compass(*parameters, calibration.get(id));
234
235 compass.correct(getMagneticDeclination(parameters->UNIXTIME * 1.0e-3), meridian);
236
237 const JQuaternion3D Q = compass.getQuaternion();
238
239 data[id].push_back(element_type(parameters->UNIXTIME * 1.0e-3, Q, 1, false));
240 }
241 }
242 }
243
244 // remove outliers
245
246 if (angle_deg > 0.0) {
247
248 for (map_type::iterator module = data.begin(); module != data.end(); ++module) {
249
250 if (!module->second.empty()) {
251
252 sort(module->second.begin(), module->second.end(), make_comparator(&element_type::first));
253
254 buffer_type::iterator out = module->second.begin(); // output
255 buffer_type::const_iterator in = module->second.begin(); // input
256 buffer_type::const_iterator p = module->second.begin(); // begin interpolation data
257 buffer_type::const_iterator q = module->second.begin(); // end interpolation data
258
259 for (int i = 0; i != NUMBER_OF_POINTS && q != module->second.end(); ++i, ++q) {}
260
261 for (int i = 0; i != NUMBER_OF_POINTS/2 && in != q; ++i, ++in) {
262
263 f1.set(p, in, q);
264
265 if (getAngle(in->second, f1(in->first)) <= angle_deg) {
266 *out = *in;
267 ++out;
268 }
269 }
270
271 for (++p; q++ != module->second.end(); ++p, ++in) {
272
273 f1.set(p, in, q);
274
275 if (getAngle(in->second, f1(in->first)) <= angle_deg) {
276 *out = *in;
277 ++out;
278 }
279 }
280
281 for ( ; in != module->second.end(); ++in) {
282
283 f1.set(p, in, q);
284
285 if (getAngle(in->second, f1(in->first)) <= angle_deg) {
286 *out = *in;
287 ++out;
288 }
289 }
290
291 module->second.erase(out, module->second.end()); // remove leftovers
292 }
293 }
294 }
295
296
297 // apply policy for missing modules and write output
298
299 const array_type<map_type::key_type>& buffer = make_array(data.begin(), data.end(), &map_type::value_type::first);
300
301 const JPolicy policy(router, buffer.begin(), buffer.end(), npy);
302
303 for (JPolicy::const_iterator module = policy.begin(); module != policy.end(); ++module) {
304
305 buffer_type buffer;
306
307 for (JPolicy::mapped_type::const_iterator in = module->second.begin(); in != module->second.end(); ++in) {
308 for (map_type::mapped_type::const_iterator i = data[*in].begin(); i != data[*in].end(); ++i) {
309 buffer.push_back(element_type(i->first, i->second, 1, true));
310 }
311 }
312
313 // average
314
315 if (T_s > 0.0) {
316
317 sort(buffer.begin(), buffer.end(), make_comparator(&element_type::first));
318
319 buffer_type::iterator out = buffer.begin(); // output
320 buffer_type::const_iterator p = buffer.begin(); // begin averaging data
321 buffer_type::const_iterator q = buffer.begin(); // end averaging data
322
323 for (p = buffer.begin(); p != buffer.end(); p = q) {
324
325 for (q = p; q != buffer.end() && q->first - p->first < T_s; ++q) {}
326
327 if (distance(p,q) > 1) {
328
329 const array_type<double>& t1 = make_array(p, q, &element_type::first);
330 const array_type<JQuaternion3D>& q1 = make_array(p, q, &element_type::second);
331
332 out->first = getAverage(t1.begin(), t1.end());
333 out->second = getAverage(q1.begin(), q1.end());
334 out->ns = distance(p,q);
335 out->policy = element_type::getOR(p,q);
336
337 } else {
338
339 *out = *p;
340 }
341
342 ++out;
343 }
344
345 buffer.erase(out, buffer.end()); // remove leftovers
346 }
347
348 for (buffer_type::const_iterator i = buffer.begin(); i != buffer.end(); ++i) {
349 outputFile.put(JOrientation(module->first, i->first, getQuaternion(i->second), i->ns, i->policy));
350 }
351 }
352
353 STATUS(endl);
354 }
355
356 JMultipleFileScanner<JMeta> io(inputFile);
357
358 io >> outputFile;
359
360 outputFile.close();
361}
string outputFile
#define STATUS(A)
Definition JMessage.hh:63
#define NOTICE(A)
Definition JMessage.hh:64
#define FATAL(A)
Definition JMessage.hh:67
int debug
debug level
Definition JSirene.cc:72
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition JParser.hh:2142
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Data structure for compass in three dimensions.
Definition JCompass.hh:51
Detector data structure.
Definition JDetector.hh:96
int getFloor() const
Get floor number.
Definition JLocation.hh:146
Router for direct addressing of module data in detector data structure.
Data structure for a composite optical module.
Definition JModule.hh:75
bool has(const int bit) const
Test PMT status.
Definition JStatus.hh:120
Data structure for unit quaternion in three dimensions.
const JQuaternion3D & getQuaternion() const
Get quaternion.
General exception.
Definition JException.hh:24
Utility class to parse command line options.
Definition JParser.hh:1698
Object writing to file.
General purpose class for object reading from a list of file names.
virtual bool hasNext() override
Check availability of next element.
static const int COMPASS_DISABLE
Enable (disable) use of compass if this status bit is 0 (1);.
JQuaternion3D getQuaternion(const JQuaternion &Q)
Get quaternion.
static JARCAMagneticDeclination getARCAMagneticDeclination
Function object for magnetic declination at ARCA site.
Definition JNOAA.hh:850
static JORCAMagneticDeclination getORCAMagneticDeclination
Function object for magnetic declination at ORCA site.
Definition JNOAA.hh:851
static const double ORCA_MERIDIAN_CONVERGENCE_ANGLE_RAD
ORCA meridian convergence angle [rad].
static const double ARCA_MERIDIAN_CONVERGENCE_ANGLE_RAD
ARCA meridian convergence angle [rad].
static JZEROMagneticDeclination getZEROMagneticDeclination
Function object for zero magnetic declination.
Definition JNOAA.hh:852
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
bool isORCADetector(const JDetectorHeader &header)
Check if given detector header is compatible with that of ORCA.
bool isARCADetector(const JDetectorHeader &header)
Check if given detector header is compatible with tat of ARCA.
double getAngle(const JQuaternion3D &first, const JQuaternion3D &second)
Get space angle between quanternions.
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition JVectorize.hh:54
std::iterator_traits< T >::value_type getAverage(T __begin, T __end)
Get average.
Definition JMath.hh:494
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
std::vector< JHitW0 > buffer_type
hits
Definition JPerth.cc:70
Long64_t counter_type
Type definition for counter.
bool is_valid(const json &js)
Check validity of JSon data.
std::map< int, range_type > map_type
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
Calibration.
Definition JHead.hh:330
Detector file.
Definition JHead.hh:227
Auxiliary base class for interpolation of magnetic declination data obtained from website of NOAA.
Definition JNOAA.hh:28
Orientation of module.
Auxiliary class to define policy for invalid modules.
Definition JPolicy.hh:34
Auxiliary class to map module identifier to AHRS calibration.
Auxiliary data structure to check validity of AHRS data.
long long int UNIXTIME
[ms]
Definition JAHRS.hh:26
Auxiliary data structure for return type of make methods.
Definition JVectorize.hh:28
Template definition for function evaluation of Legendre polynome.
Definition JLegendre.hh:269
void set(T __begin, T __end)
Set Legendre polynome.
Definition JLegendre.hh:327
static counter_type max()
Get maximum counter value.
Definition JLimit.hh:128
Auxiliary class for ROOT I/O of application specific meta data.
Definition JMeta.hh:72
Auxiliary base class for list of file names.
General purpose class for hash map of unique keys.
Definition JHashMap.hh:75