Jpp - 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 <map>
7 
9 
10 #include "JSystem/JStat.hh"
11 #include "JLang/JStringStream.hh"
12 #include "JLang/JException.hh"
13 
14 
15 /**
16  * \author mdejong
17  */
18 
19 namespace JCALIBRATE {}
20 namespace JPP { using namespace JCALIBRATE; }
21 
22 /**
23  * Auxiliary classes and methods for PMT calibration.
24  */
25 namespace JCALIBRATE {
26 
29 
30 
31  /**
32  * Auxiliary class for TDC constraints.
33  */
34  struct JTDC_t :
35  public std::multimap<int, int> // module identifier -> TDC
36  {
38 
39  /**
40  * Type definition for range of TDC constraints of a given module identfier.
41  */
43 
44 
45  /**
46  * Wild card for module identifier and TDC.
47  */
48  enum { WILD_CARD = -1 };
49 
50 
51  /**
52  * Insert constraint.
53  *
54  * Note that if TDC equals JTDC_t::WILD_CARD,
55  * all possible TDCs in the given module are inserted.
56  *
57  * \param value module identifier and TDC
58  */
59  void insert(const value_type& value)
60  {
61  if (value.second == WILD_CARD) {
62 
63  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
64  multimap_type::insert(value_type(value.first, pmt));
65  }
66 
67  } else {
68 
69  multimap_type::insert(value);
70  }
71  }
72 
73 
74  /**
75  * Insert constraint.
76  *
77  * Note that if TDC equals JTDC_t::WILD_CARD,
78  * all possible TDCs in the given module are inserted.
79  *
80  * \param id module identifier
81  * \param tdc TDC
82  */
83  void insert(const int id, const int tdc)
84  {
85  this->insert(value_type(id, tdc));
86  }
87 
88 
89  /**
90  * Get range of constraints for given module.
91  *
92  * Note that if no data are available for given module identifier,
93  * the data corresponding to JTDC_t::WILD_CARD are returned.
94  *
95  * \param id module identifier
96  * \return range of constraints
97  */
98  range_type equal_range(const int id)
99  {
100  range_type range = multimap_type::equal_range(id);
101 
102  if (range.first == range.second) {
103  range = multimap_type::equal_range(WILD_CARD);
104  }
105 
106  return range;
107  }
108 
109 
110  /**
111  * Reverse constraints.
112  */
113  void reverse()
114  {
115  JTDC_t buffer;
116 
117  for (JTDC_t::const_iterator p = this->begin(); p != this->end(); ) {
118 
119  JTDC_t::const_iterator q = p;
120 
121  for ( ; q != this->end() && q->first == p->first; ++q) {}
122 
123  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
124 
125  JTDC_t::const_iterator i = p;
126 
127  for ( ; i != q && i->second != pmt; ++i) {}
128 
129  if (i == q) {
130  buffer.insert(value_type(p->first, pmt));
131  }
132  }
133 
134  p = q;
135  }
136 
137  this->swap(buffer);
138  }
139 
140 
141  /**
142  * Check validity of TDC constrants.
143  *
144  * \param option option (if true, throw exception if not valid)
145  * \return true if valid; else false
146  */
147  bool is_valid(const bool option = false) const
148  {
149  for (const_iterator i = this->begin(); i != this->end(); ++i) {
150 
151  if (i->first < WILD_CARD) {
152 
153  if (option) {
154  THROW(JValueOutOfRange, "Invalid module identifier: " << i->first << " < " << WILD_CARD);
155  }
156 
157  return false;
158  }
159 
160  if (i->second < 0 || i->second >= NUMBER_OF_PMTS) {
161 
162  if (option) {
163  THROW(JValueOutOfRange, "Invalid TDC: " << i->second << " {0, .., " << NUMBER_OF_PMTS - 1 << "}");
164  }
165 
166  return false;
167  }
168  }
169 
170  return true;
171  }
172 
173 
174  /**
175  * Read TDC constraints from input.
176  *
177  * \param in input stream
178  * \param tdc TDC constraints
179  * \return input stream
180  */
181  friend inline std::istream& operator>>(std::istream& in, JTDC_t& tdc)
182  {
183  using namespace std;
184  using namespace JPP;
185 
186  JStringStream is(in);
187 
188  if (getFileStatus(is.str().c_str())) {
189  is.load();
190  }
191 
192  for (int id, pmt; is >> id >> pmt; ) {
193  tdc.insert(JTDC_t::value_type(id, pmt));
194  }
195 
196  return in;
197  }
198 
199 
200  /**
201  * Write TDC constraints to output.
202  *
203  * \param out output stream
204  * \param tdc TDC constraints
205  * \return output stream
206  */
207  friend inline std::ostream& operator<<(std::ostream& out, const JTDC_t& tdc)
208  {
209  for (JTDC_t::const_iterator i = tdc.begin(); i != tdc.end(); ++i) {
210  out << ' ' << i->first
211  << ' ' << i->second;
212  }
213 
214  return out;
215  }
216  };
217 }
218 
219 #endif
Exceptions.
std::multimap< int, int > multimap_type
Definition: JTDC_t.hh:37
friend std::istream & operator>>(std::istream &in, JTDC_t &tdc)
Read TDC constraints from input.
Definition: JTDC_t.hh:181
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
Auxiliary class for TDC constraints.
Definition: JTDC_t.hh:34
is
Definition: JDAQCHSM.chsm:167
friend std::ostream & operator<<(std::ostream &out, const JTDC_t &tdc)
Write TDC constraints to output.
Definition: JTDC_t.hh:207
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
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:59
std::pair< const_iterator, const_iterator > range_type
Type definition for range of TDC constraints of a given module identfier.
Definition: JTDC_t.hh:42
void reverse()
Reverse constraints.
Definition: JTDC_t.hh:113
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:162
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:147
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
range_type equal_range(const int id)
Get range of constraints for given module.
Definition: JTDC_t.hh:98
void insert(const int id, const int tdc)
Insert constraint.
Definition: JTDC_t.hh:83
File status.