Jpp  18.2.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JTDC_t.hh
Go to the documentation of this file.
1 #ifndef __JCALIBRATE_JTDC_t__
2 #define __JCALIBRATE_JTDC_t__
3 
4 #include <istream>
5 #include <ostream>
6 #include <iomanip>
7 #include <map>
8 
10 
11 #include "JSystem/JStat.hh"
12 #include "JLang/JStringStream.hh"
13 #include "JLang/JException.hh"
14 #include "Jeep/JComment.hh"
15 
16 
17 /**
18  * \author mdejong
19  */
20 
21 namespace JCALIBRATE {}
22 namespace JPP { using namespace JCALIBRATE; }
23 
24 /**
25  * Auxiliary classes and methods for PMT calibration.
26  */
27 namespace JCALIBRATE {
28 
31  using JEEP::JComment;
32 
33 
34  /**
35  * Auxiliary class for TDC constraints.
36  */
37  struct JTDC_t :
38  public std::multimap<int, int> // module identifier -> TDC
39  {
41 
42  /**
43  * Type definition for range of TDC constraints of a given module identfier.
44  */
46 
47 
48  /**
49  * Wild card for module identifier and TDC.
50  */
51  enum { WILD_CARD = -1 };
52 
53 
54  /**
55  * Insert constraint.
56  *
57  * Note that if TDC equals JTDC_t::WILD_CARD,
58  * all possible TDCs in the given module are inserted.
59  *
60  * \param value module identifier and TDC
61  */
62  void insert(const value_type& value)
63  {
64  if (value.second == WILD_CARD) {
65 
66  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
67  multimap_type::insert(value_type(value.first, pmt));
68  }
69 
70  } else {
71 
72  multimap_type::insert(value);
73  }
74  }
75 
76 
77  /**
78  * Insert constraint.
79  *
80  * Note that if TDC equals JTDC_t::WILD_CARD,
81  * all possible TDCs in the given module are inserted.
82  *
83  * \param id module identifier
84  * \param tdc TDC
85  */
86  void insert(const int id, const int tdc)
87  {
88  this->insert(value_type(id, tdc));
89  }
90 
91 
92  /**
93  * Get range of constraints for given module.
94  *
95  * Note that if no data are available for given module identifier,
96  * the data corresponding to JTDC_t::WILD_CARD are returned.
97  *
98  * \param id module identifier
99  * \return range of constraints
100  */
101  range_type equal_range(const int id)
102  {
103  range_type range = multimap_type::equal_range(id);
104 
105  if (range.first == range.second) {
106  range = multimap_type::equal_range(WILD_CARD);
107  }
108 
109  return range;
110  }
111 
112 
113  /**
114  * Check if TDC is constraint.
115  *
116  * \param id module identifier
117  * \param tdc TDC channel
118  * \return true if given TDC is constraint; else false
119  */
120  bool has(const int id, const int tdc)
121  {
122  const range_type range = equal_range(id);
123 
124  for (JTDC_t::const_iterator i = range.first; i != range.second; ++i) {
125  if (tdc == i->second) {
126  return true;
127  }
128  }
129 
130  return false;
131  }
132 
133 
134  /**
135  * Reverse constraints.
136  */
137  void reverse()
138  {
139  JTDC_t buffer;
140 
141  for (JTDC_t::const_iterator p = this->begin(); p != this->end(); ) {
142 
143  JTDC_t::const_iterator q = p;
144 
145  for ( ; q != this->end() && q->first == p->first; ++q) {}
146 
147  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
148 
149  JTDC_t::const_iterator i = p;
150 
151  for ( ; i != q && i->second != pmt; ++i) {}
152 
153  if (i == q) {
154  buffer.insert(value_type(p->first, pmt));
155  }
156  }
157 
158  p = q;
159  }
160 
161  this->swap(buffer);
162  }
163 
164 
165  /**
166  * Check validity of TDC constrants.
167  *
168  * \param option option (if true, throw exception if not valid)
169  * \return true if valid; else false
170  */
171  bool is_valid(const bool option = false) const
172  {
173  for (const_iterator i = this->begin(); i != this->end(); ++i) {
174 
175  if (i->first < WILD_CARD) {
176 
177  if (option) {
178  THROW(JValueOutOfRange, "Invalid module identifier: " << i->first << " < " << WILD_CARD);
179  }
180 
181  return false;
182  }
183 
184  if (i->second < 0 || i->second >= NUMBER_OF_PMTS) {
185 
186  if (option) {
187  THROW(JValueOutOfRange, "Invalid TDC: " << i->second << " {0, .., " << NUMBER_OF_PMTS - 1 << "}");
188  }
189 
190  return false;
191  }
192  }
193 
194  return true;
195  }
196 
197 
198  /**
199  * Read TDC constraints from input.
200  *
201  * \param in input stream
202  * \param tdc TDC constraints
203  * \return input stream
204  */
205  friend inline std::istream& operator>>(std::istream& in, JTDC_t& tdc)
206  {
207  using namespace std;
208  using namespace JPP;
209 
210  JStringStream is(in);
211 
212  if (getFileStatus(is.str().c_str())) {
213  is.load();
214  }
215 
216  is >> tdc.comment;
217 
218  for (int id, pmt; is >> id >> pmt; ) {
219  tdc.insert(JTDC_t::value_type(id, pmt));
220  }
221 
222  return in;
223  }
224 
225 
226  /**
227  * Write TDC constraints to output.
228  *
229  * \param out output stream
230  * \param tdc TDC constraints
231  * \return output stream
232  */
233  friend inline std::ostream& operator<<(std::ostream& out, const JTDC_t& tdc)
234  {
235  using namespace std;
236 
237  out << tdc.comment;
238 
239  for (JTDC_t::const_iterator i = tdc.begin(); i != tdc.end(); ++i) {
240  out << setw(10) << i->first << ' ' << setw(2) << i->second << endl;
241  }
242 
243  return out;
244  }
245 
247  };
248 }
249 
250 #endif
Exceptions.
std::multimap< int, int > multimap_type
Definition: JTDC_t.hh:40
friend std::istream & operator>>(std::istream &in, JTDC_t &tdc)
Read TDC constraints from input.
Definition: JTDC_t.hh:205
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
Auxiliary class for TDC constraints.
Definition: JTDC_t.hh:37
is
Definition: JDAQCHSM.chsm:167
bool has(const int id, const int tdc)
Check if TDC is constraint.
Definition: JTDC_t.hh:120
friend std::ostream & operator<<(std::ostream &out, const JTDC_t &tdc)
Write TDC constraints to output.
Definition: JTDC_t.hh:233
JComment comment
Definition: JTDC_t.hh:246
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
Auxiliary class for comment.
Definition: JComment.hh:41
z range($ZMAX-$ZMIN)< $MINIMAL_DZ." fi fi typeset -Z 4 STRING typeset -Z 2 FLOOR JPlot1D -f $
void insert(const value_type &value)
Insert constraint.
Definition: JTDC_t.hh:62
std::pair< const_iterator, const_iterator > range_type
Type definition for range of TDC constraints of a given module identfier.
Definition: JTDC_t.hh:45
void reverse()
Reverse constraints.
Definition: JTDC_t.hh:137
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:178
KM3NeT DAQ constants, bit handling, etc.
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
bool is_valid(const bool option=false) const
Check validity of TDC constrants.
Definition: JTDC_t.hh:171
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 JAcoustics sh $DETECTOR_ID source JAcousticsToolkit sh CHECK_EXIT_CODE typeset A EMITTERS get_tripods $WORKDIR tripod txt EMITTERS get_transmitters $WORKDIR transmitter txt EMITTERS for EMITTER in
Definition: JCanberra.sh:46
range_type equal_range(const int id)
Get range of constraints for given module.
Definition: JTDC_t.hh:101
void insert(const int id, const int tdc)
Insert constraint.
Definition: JTDC_t.hh:86
File status.