Jpp  master_rocky
the software that should make you happy
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 { WILDCARD = -1 };
52 
53 
54  /**
55  * Insert constraint.
56  *
57  * Note that if TDC equals JTDC_t::WILDCARD,
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 == WILDCARD) {
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::WILDCARD,
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::WILDCARD 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(WILDCARD);
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 < WILDCARD) {
176 
177  if (option) {
178  THROW(JValueOutOfRange, "Invalid module identifier: " << i->first << " < " << WILDCARD);
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
KM3NeT DAQ constants, bit handling, etc.
Exceptions.
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:712
File status.
Wrapper class around STL stringstream class to facilitate optional loading of data from file.
void load()
Load data from file with name corresponding to current contents.
Exception for accessing a value in a collection that is outside of its range.
Definition: JException.hh:180
Auxiliary classes and methods for PMT calibration.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static JStat getFileStatus
Function object for file status.
Definition: JStat.hh:173
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
Definition: JSTDTypes.hh:14
Auxiliary class for TDC constraints.
Definition: JTDC_t.hh:39
range_type equal_range(const int id)
Get range of constraints for given module.
Definition: JTDC_t.hh:101
void reverse()
Reverse constraints.
Definition: JTDC_t.hh:137
void insert(const value_type &value)
Insert constraint.
Definition: JTDC_t.hh:62
JComment comment
Definition: JTDC_t.hh:246
std::multimap< int, int > multimap_type
Definition: JTDC_t.hh:40
bool has(const int id, const int tdc)
Check if TDC is constraint.
Definition: JTDC_t.hh:120
friend std::istream & operator>>(std::istream &in, JTDC_t &tdc)
Read TDC constraints from input.
Definition: JTDC_t.hh:205
friend std::ostream & operator<<(std::ostream &out, const JTDC_t &tdc)
Write TDC constraints to output.
Definition: JTDC_t.hh:233
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
bool is_valid(const bool option=false) const
Check validity of TDC constrants.
Definition: JTDC_t.hh:171
void insert(const int id, const int tdc)
Insert constraint.
Definition: JTDC_t.hh:86
Auxiliary class for comment.
Definition: JComment.hh:43