Jpp - the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JAHRSCalibration_t.hh
Go to the documentation of this file.
1 #ifndef __JDB_JAHRSCALIBRATION_T__
2 #define __JDB_JAHRSCALIBRATION_T__
3 
4 #include <istream>
5 #include <ostream>
6 #include <fstream>
7 #include <map>
8 #include <vector>
9 
10 #include "JLang/JException.hh"
11 #include "JLang/JObjectStreamIO.hh"
12 
13 #include "JDB/JDB.hh"
14 #include "JDB/JDBToolkit.hh"
15 #include "JDB/JPBS_t.hh"
16 #include "JDB/JAHRSCalibration.hh"
19 
20 
21 /**
22  * \author mdejong
23  */
24 namespace JDATABASE {}
25 namespace JPP { using namespace JDATABASE; }
26 
27 namespace JDATABASE {
28 
31 
32 
33  /**
34  * Auxiliary class to map module identifier to AHRS calibration.
35  */
37  public std::map<int, JAHRSCalibration>,
38  public JObjectStreamIO<JAHRSCalibration_t>
39  {
40  /**
41  * Default constructor.
42  */
44  {}
45 
46 
47  /**
48  * Constructor.
49  *
50  * \param id detector identifier
51  * \param option option
52  */
53  JAHRSCalibration_t(const int id, const int option)
54  {
55  using namespace std;
56  using namespace JPP;
57 
59 
60  ResultSet& rs = getResultSet(getTable<JDetectorIntegration_t>());
61 
62  rs >> detector;
63 
64  detector.configure(getDetector(id));
65 
66  this->configure(detector, option);
67  }
68 
69 
70  /**
71  * Constructor.
72  *
73  * \param detector detector integration
74  * \param option option
75  */
77  {
78  this->configure(detector, option);
79  }
80 
81 
82  /**
83  * Constructor.
84  *
85  * \param file_name input file
86  */
87  JAHRSCalibration_t(const char* file_name)
88  {
89  load(file_name);
90  }
91 
92 
93  /**
94  * Configure.
95  *
96  * The latest <em>valid</em> AHRS calibration data to the identifier of the parent module.\n
97  * In this, the time order and validity of the AHRS calibration data are defined by AHRSComparator and JAHRSValidity, respectively.\n
98  * The option corresponds to the maximal difference between latest version and latest valid version.
99  *
100  * \param detector detector integration
101  * \param option option
102  */
103  void configure(const JDetectorIntegration_t& detector, const int option)
104  {
105  using namespace std;
106  using namespace JPP;
107 
109 
112 
113  this->clear();
114 
115  vector<JAHRSCalibration> calibration;
116 
117  ResultSet& rs = getResultSet(getTable<JAHRSCalibration>());
118 
119  rs >> calibration;
120 
121  rs.Close();
122 
123 
124  sort(calibration.begin(), calibration.end(), binary_negate<JAHRSCalibrationComparator>(JAHRSCalibrationComparator()));
125 
126 
127  for (vector<JAHRSCalibration>::const_iterator p = calibration.begin(); p != calibration.end(); ) {
128 
130 
131  for (++q; q != calibration.end() && q->SERIALNUMBER == p->SERIALNUMBER; ++q) {}
132 
133  const JAHRSCalibration& calibration = *p;
134  const JUPI_t upi = getUPI(PBS::AHRS, calibration.SERIALNUMBER);
135 
136  const JDetectorIntegration_t::range_type r1 = detector.find(upi);
137 
138  if (distance(r1.first, r1.second) == 1) {
139 
140  const int id = getCLBID(detector[r1.first->second].container.getUPI());
141 
142  for (vector<JAHRSCalibration>::const_iterator i = p; i != q; ++i) {
143 
144  if (is_valid(*i) && distance(p,i) <= option) {
145 
146  this->insert(make_pair(id, calibration));
147 
148  break;
149  }
150  }
151  }
152 
153  p = q;
154  }
155  }
156 
157 
158  /**
159  * Check availability of AHRS calibration for given module identifier.
160  *
161  * \param id module identifier
162  * \return true if AHRS calibration available; else false
163  */
164  bool has(int id) const
165  {
166  return (this->find(id) != this->end());
167  }
168 
169 
170  /**
171  * Get AHRS calibration for given module identifier.
172  *
173  * \param id module identifier
174  * \return AHRS calibration
175  */
176  const JAHRSCalibration& get(int id) const
177  {
178  const_iterator i = this->find(id);
179 
180  if (i != this->end())
181  return i->second;
182  else
183  THROW(JValueOutOfRange, "Invalid module identifier " << id);
184  }
185 
186 
187  /**
188  * Read AHRS calibration from input stream.
189  *
190  * \param in input stream
191  * \param calibration AHRS calibration
192  * \return input stream
193  */
194  friend inline std::istream& operator>>(std::istream& in, JAHRSCalibration_t& calibration)
195  {
196  int id;
197  JAHRSCalibration buffer;
198 
199  while (in >> id >> buffer) {
200  calibration[id] = buffer;
201  }
202 
203  return in;
204  }
205 
206 
207  /**
208  * Write AHRS calibration to output stream.
209  *
210  * \param out output stream
211  * \param calibration AHRS calibration
212  * \return output stream
213  */
214  friend inline std::ostream& operator<<(std::ostream& out, const JAHRSCalibration_t& calibration)
215  {
216  using namespace std;
217 
218  for (JAHRSCalibration_t::const_iterator i = calibration.begin(); i != calibration.end(); ++i) {
219  out << i->first << ' ' << i->second << endl;
220  }
221 
222  return out;
223  }
224  };
225 }
226 
227 #endif
228 
void configure(const JDetectorIntegration_t &detector, const int option)
Configure.
Exceptions.
JAHRSCalibration_t(const JDetectorIntegration_t &detector, const int option)
Constructor.
friend std::istream & operator>>(std::istream &in, JAHRSCalibration_t &calibration)
Read AHRS calibration from input stream.
static void initialise(JHelper_t &helper, const Args &...args)
Initialise.
Definition: JDBToolkit.hh:255
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
friend std::ostream & operator<<(std::ostream &out, const JAHRSCalibration_t &calibration)
Write AHRS calibration to output stream.
JAHRSCalibration_t()
Default constructor.
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
bool has(int id) const
Check availability of AHRS calibration for given module identifier.
Auxiliary data structure for sorting of AHRS calibrations.
JDetectorsHelper getDetector
Function object for mapping serial number to object identifier of detector and vice versa...
Definition: JDBToolkit.cc:5
JAHRSCalibration_t(const int id, const int option)
Constructor.
bool is_valid(const json &js)
Check validity of JSon data.
range_type find(const JUPI_t &upi) const
Find range of products with given UPI.
void load(const char *file_name)
Load from input file.
Auxiliary data structure to check validity of AHRS calibration data.
ResultSet & getResultSet(const std::string &query)
Get result set.
Definition: JDB.hh:269
Auxiliary class to map module identifier to AHRS calibration.
JUPIHelper getUPI
Function object for mapping PBS and serial number to UPI.
Definition: JDBToolkit.cc:7
JCLBIDHelper getCLBID
Function object for mapping UPI of central-logic board to module identifier.
Definition: JDBToolkit.cc:6
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
Auxiliary base class for storing and loading a single object to and from an ASCII file...
do set_variable DETECTOR_TXT $WORKDIR detector
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 CHECK_EXIT_CODE typeset A TRIPODS get_tripods $WORKDIR tripod txt TRIPODS for EMITTER in
Definition: JCanberra.sh:38
JAHRSCalibration_t(const char *file_name)
Constructor.
static const JPBS_t AHRS(3, 4, 3, 4)
PBS of compass