Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
software/JCompass/JBallarat.cc
Go to the documentation of this file.
1 #include <string>
2 #include <iostream>
3 #include <iomanip>
4 #include <algorithm>
5 #include <utility>
6 #include <vector>
7 
8 #include "JDB/JSupport.hh"
9 #include "JDB/JAHRS.hh"
11 #include "JDB/JAHRSToolkit.hh"
12 
15 #include "JSupport/JMeta.hh"
16 
17 #include "JDetector/JDetector.hh"
20 #include "JDetector/JCompass.hh"
21 
23 #include "JGeometry3D/JEigen3D.hh"
24 
25 #include "JTools/JHashMap.hh"
26 #include "JLang/JVectorize.hh"
27 #include "JLang/JComparator.hh"
28 #include "JMath/JLegendre.hh"
29 
30 #include "JCompass/JEvt.hh"
31 #include "JCompass/JEvtToolkit.hh"
32 #include "JCompass/JSupport.hh"
33 #include "JCompass/JNOAA.hh"
34 #include "JCompass/JPolicy.hh"
35 #include "JCompass/JBallarat.hh"
37 
38 #include "Jeep/JPrint.hh"
39 #include "Jeep/JParser.hh"
40 #include "Jeep/JMessage.hh"
41 
42 
43 /**
44  * \file
45  *
46  * Auxiliary program to process AHRS data.
47  *
48  * The AHRS calibration as well as the quaternion calibration are applied to the data.\n
49  * Possible outliers are removed according the specified maximal angle (option <tt>-S <angle_deg></tt>.\n
50  * For this, interpolations of the data are made based on Legendre polynomials with
51  * the number of points and the number of degrees set to
52  * JCOMPASS::JBallarat::NUMBER_OF_POINTS and
53  * JCOMPASS::JBallarat::NUMBER_OF_DEGREES, respectively.
54  * \author mdejong
55  */
56 int main(int argc, char **argv)
57 {
58  using namespace std;
59  using namespace JPP;
60 
61  JMultipleFileScanner_t inputFile;
62  counter_type numberOfEvents;
64  string detectorFile;
65  string ahrsFile;
66  double angle_deg;
67  int debug;
68 
69  try {
70 
71  JParser<> zap("Auxiliary program to process AHRS data.");
72 
73  zap['f'] = make_field(inputFile, "output of JConvertDB -q ahrs");
74  zap['n'] = make_field(numberOfEvents) = JLimit::max();
75  zap['a'] = make_field(detectorFile);
76  zap['c'] = make_field(ahrsFile, "output of JAHRSCalibration");
77  zap['o'] = make_field(outputFile, "ROOT formatted orientations file");
78  zap['S'] = make_field(angle_deg) = 0.0;
79  zap['d'] = make_field(debug) = 1;
80 
81  zap(argc, argv);
82  }
83  catch(const exception &error) {
84  FATAL(error.what() << endl);
85  }
86 
87 
89 
90  try {
91  load(detectorFile, detector);
92  }
93  catch(const JException& error) {
94  FATAL(error);
95  }
96 
97  const JModuleRouter router(detector);
98  const JNOAAFunction1D_t& getMagneticDeclination = (isARCADetector(detector) ? static_cast<const JNOAAFunction1D_t&>(getARCAMagneticDeclination) :
100  static_cast<const JNOAAFunction1D_t&>(getZEROMagneticDeclination));
101  const double meridian = (isARCADetector(detector) ? ARCA_MERIDIAN_CONVERGENCE_ANGLE_RAD :
103  0.0);
104 
105  NOTICE("Magnetic declination " << getMagneticDeclination << endl);
106 
107  const JAHRSCalibration_t calibration(ahrsFile.c_str());
108  const JAHRSValidity is_valid;
109 
110 
111  typedef pair<double, JQuaternion3D> element_type;
112  typedef vector<element_type> buffer_type;
113  typedef JHashMap<int, buffer_type> map_type; // container for temporary storage of data
114 
115  JLegendre<JQuaternion3D, JBallarat::NUMBER_OF_DEGREES> f1; // interpolator for outlier removal
116 
117 
118  outputFile.open();
119 
120  outputFile.put(JMeta(argc, argv));
121 
122  counter_type counter = 0;
123 
124  for (JMultipleFileScanner_t::const_iterator file_name = inputFile.begin(); file_name != inputFile.end(); ++file_name) {
125 
126  STATUS("processing file " << *file_name << "... " << flush);
127 
128  // collect data
129 
130  map_type data;
131 
132  for (JMultipleFileScanner<JAHRS> in(*file_name); in.hasNext() && counter != numberOfEvents; ++counter) {
133 
134  const JAHRS* parameters = in.next();
135  const int id = parameters->DOMID;
136 
137  if (is_valid(*parameters) && calibration.has(id) && router.hasModule(id)) {
138 
139  const JModule& module = router.getModule(id);
140 
141  if (module.getFloor() != 0) {
142 
143  JCompass compass(*parameters, calibration.get(id));
144 
145  compass.correct(getMagneticDeclination(parameters->UNIXTIME * 1.0e-3), meridian);
146 
147  const JQuaternion3D Q = module.getQuaternion() * compass.getQuaternion();
148 
149  data[id].push_back(element_type(parameters->UNIXTIME * 1.0e-3, Q));
150  }
151  }
152  }
153 
154  // remove outliers
155 
156  if (angle_deg > 0.0) {
157 
158  for (map_type::iterator module = data.begin(); module != data.end(); ++module) {
159 
160  if (!module->second.empty()) {
161 
162  sort(module->second.begin(), module->second.end(), make_comparator(&element_type::first));
163 
164  buffer_type::iterator out = module->second.begin(); // output
165  buffer_type::const_iterator in = module->second.begin(); // input
166  buffer_type::const_iterator p = module->second.begin(); // begin interpolation data
167  buffer_type::const_iterator q = module->second.begin(); // end interpolation data
168 
169  for (int i = 0; i != JBallarat::NUMBER_OF_POINTS && q != module->second.end(); ++i, ++q) {}
170 
171  for (int i = 0; i != JBallarat::NUMBER_OF_POINTS/2 && in != q; ++i, ++in) {
172 
173  f1.set(p, in, q);
174 
175  if (getAngle(in->second, f1(in->first)) <= angle_deg) {
176  *out = *in;
177  ++out;
178  }
179  }
180 
181  for (++p; q++ != module->second.end(); ++p, ++in) {
182 
183  f1.set(p, in, q);
184 
185  if (getAngle(in->second, f1(in->first)) <= angle_deg) {
186  *out = *in;
187  ++out;
188  }
189  }
190 
191  for ( ; in != module->second.end(); ++in) {
192 
193  f1.set(p, in, q);
194 
195  if (getAngle(in->second, f1(in->first)) <= angle_deg) {
196  *out = *in;
197  ++out;
198  }
199  }
200 
201  module->second.erase(out, module->second.end()); // remove leftovers
202  }
203  }
204  }
205 
206  // apply policy for missing modules and write output
207 
208  const array_type<map_type::key_type>& buffer = make_array(data.begin(), data.end(), &map_type::value_type::first);
209 
210  const JPolicy policy(router, buffer.begin(), buffer.end());
211 
212  for (JPolicy::const_iterator module = policy.begin(); module != policy.end(); ++module) {
213  for (JPolicy::mapped_type::const_iterator in = module->second.begin(); in != module->second.end(); ++in) {
214  for (map_type::mapped_type::const_iterator i = data[*in].begin(); i != data[*in].end(); ++i) {
215  outputFile.put(JOrientation(module->first, i->first, getQuaternion(i->second)));
216  }
217  }
218  }
219 
220  STATUS(endl);
221  }
222 
223  JMultipleFileScanner<JMeta> io(inputFile);
224 
225  io >> outputFile;
226 
227  outputFile.close();
228 }
bool isORCADetector(const JDetectorHeader &header)
Check if given detector header is compatible with that of ORCA.
Auxiliary class for ROOT I/O of application specific meta data.
Definition: JMeta.hh:70
Object writing to file.
Utility class to parse command line options.
Definition: JParser.hh:1500
double getAngle(const JQuaternion3D &first, const JQuaternion3D &second)
Get space angle between quanternions.
General exception.
Definition: JException.hh:23
static JARCAMagneticDeclination getARCAMagneticDeclination
Function object for magnetic declination at ARCA site.
Definition: JNOAA.hh:729
void correct(const double declination, const double meridian)
Correct compass for magnetic declination and meridian convergence angle.
Definition: JCompass.hh:274
int main(int argc, char *argv[])
Definition: Main.cc:15
JComparator< JResult_t T::*, JComparison::lt > make_comparator(JResult_t T::*member)
Helper method to create comparator between values of data member.
Definition: JComparator.hh:185
int getFloor() const
Get floor number.
Definition: JLocation.hh:145
const JModule & getModule(const JObjectID &id) const
Get module parameters.
Data structure for a composite optical module.
Definition: JModule.hh:57
bool isARCADetector(const JDetectorHeader &header)
Check if given detector header is compatible with tat of ARCA.
General purpose class for hash map of unique keys.
Definition: JHashMap.hh:71
JQuaternion3D getQuaternion(const JQuaternion &Q)
Get quaternion.
General purpose class for hash map of unique elements.
#define STATUS(A)
Definition: JMessage.hh:63
Detector data structure.
Definition: JDetector.hh:80
static JZEROMagneticDeclination getZEROMagneticDeclination
Function object for zero magnetic declination.
Definition: JNOAA.hh:731
Recording of objects on file according a format that follows from the file name extension.
Router for direct addressing of module data in detector data structure.
*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
Long64_t counter_type
Type definition for counter.
string outputFile
static const double ARCA_MERIDIAN_CONVERGENCE_ANGLE_RAD
ARCA meridian convergence angle [rad].
Data structure for detector geometry and calibration.
static JORCAMagneticDeclination getORCAMagneticDeclination
Function object for magnetic declination at ORCA site.
Definition: JNOAA.hh:730
ROOT TTree parameter settings.
then echo The file $DIR KM3NeT_00000001_00000000 root already please rename or remove it first
const JQuaternion3D & getQuaternion() const
Get quaternion.
I/O formatting auxiliaries.
Detector file.
Definition: JHead.hh:196
Auxiliary base class for interpolation of magnetic declination data obtained from website of NOAA...
Definition: JNOAA.hh:24
Auxiliary class to define policy for invalid modules.
Definition: JPolicy.hh:31
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
const array_type< JValue_t > & make_array(const JValue_t(&array)[N])
Method to create array of values.
Definition: JVectorize.hh:37
Template definition for function evaluation of Legendre polynome.
Definition: JLegendre.hh:213
bool is_valid(const json &js)
Check validity of JSon data.
Data structure for compass in three dimensions.
Definition: JCompass.hh:49
ROOT I/O of application specific meta data.
#define NOTICE(A)
Definition: JMessage.hh:64
Compass event fit.
ROOT TTree parameter settings.
int debug
debug level
Definition: JSirene.cc:63
Meridian convergence angles for different sites.
Auxiliary methods to convert data members or return values of member methods of a set of objects to a...
General purpose messaging.
#define FATAL(A)
Definition: JMessage.hh:67
Scanning of objects from multiple files according a format that follows from the extension of each fi...
Data structure for unit quaternion in three dimensions.
Direct access to module in detector data structure.
Auxiliary base class for list of file names.
Policy for invalid modules.
void load(const std::string &file_name, JDetector &detector)
Load detector from input file.
General purpose class for object reading from a list of file names.
Utility class to parse command line options.
bool hasModule(const JObjectID &id) const
Has module.
Auxiliary class to map module identifier to AHRS calibration.
Orientation of module.
Compass event data types.
do set_variable DETECTOR_TXT $WORKDIR detector
static const double ORCA_MERIDIAN_CONVERGENCE_ANGLE_RAD
ORCA meridian convergence angle [rad].
then fatal Wrong number of arguments fi set_variable DETECTOR $argv[1] set_variable INPUT_FILE $argv[2] eval JPrintDetector a $DETECTOR O IDENTIFIER eval JPrintDetector a $DETECTOR O SUMMARY source JAcoustics sh $DETECTOR_ID typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:36
long long int UNIXTIME
[ms]
Definition: JAHRS.hh:26
Auxiliary data structure for return type of make methods.
Definition: JVectorize.hh:25
Auxiliary data structure to check validity of AHRS data.
Definition: JAHRSToolkit.hh:20