Jpp
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JDBToolkit.hh
Go to the documentation of this file.
1 #ifndef __JDB_JDBTOOLKIT__
2 #define __JDB_JDBTOOLKIT__
3 
4 #include <string>
5 #include <map>
6 
7 #include "JLang/JException.hh"
8 
9 #include "JDB/JDB.hh"
10 #include "JDB/JDetectors.hh"
11 #include "JDB/JCLBID.hh"
12 #include "JDB/JUPI.hh"
13 #include "JDB/JUPI_t.hh"
14 #include "JDB/JSelector.hh"
16 
17 
18 /**
19  * \author mdejong
20  */
21 
22 namespace JDATABASE {};
23 namespace JPP { using namespace JDATABASE; }
24 
25 /**
26  * Auxiliary classes and methods for database I/O.
27  */
28 namespace JDATABASE {
29 
31 
32 
33  /**
34  * Auxiliary class for mapping serial number and object identifier of detectors.
35  */
37  /**
38  * Initialise.
39  */
40  void initialise()
41  {
42  ResultSet& rs = JDB::get()->StreamDS(getTable<JDetectors>(), JSelector());
43 
44  for (JDetectors parameters; rs >> parameters; ) {
45  det2id[parameters.OID] = parameters.SERIALNUMBER;
46  id2det[parameters.SERIALNUMBER] = parameters.OID;
47  }
48 
49  rs.Close();
50  }
51 
52 
53  /**
54  * Get detector serial number.
55  *
56  * \param detid object identifier
57  * \return serial number
58  */
59  int operator()(const std::string& detid)
60  {
62 
63  if (p == det2id.end()) {
64 
65  initialise();
66 
67  p = det2id.find(detid);
68 
69  if (p == det2id.end()) {
70  THROW(JDatabaseException, "Invalid detector identifier " << detid);
71  }
72  }
73 
74  return p->second;
75  }
76 
77 
78  /**
79  * Get detector identifier.
80  *
81  * \param id serial number
82  * \return object identifier
83  */
84  inline std::string operator()(const int id)
85  {
87 
88  if (p == id2det.end()) {
89 
90  initialise();
91 
92  p = id2det.find(id);
93 
94  if (p == id2det.end()) {
95  THROW(JDatabaseException, "Invalid detector identifier " << id);
96  }
97  }
98 
99  return p->second;
100  }
101 
102  protected:
105  };
106 
107 
108  /**
109  * Auxiliary class for mapping UPI of central-logic board to module identifier.
110  */
111  struct JCLBIDHelper {
112  /**
113  * Initialise.
114  */
115  void initialise()
116  {
117  ResultSet& rs = JDB::get()->StreamDS(getTable<JCLBID>(), getSelector<JCLBID>());
118 
119  for (JCLBID parameters; rs >> parameters; ) {
120  upi2id[parameters.CLBUPI] = parameters.CLBID;
121  id2upi[parameters.CLBID] = parameters.CLBUPI;
122  }
123 
124  rs.Close();
125  }
126 
127 
128  /**
129  * Get module identifier.
130  *
131  * \param upi %UPI
132  * \return module identifier
133  */
134  int operator()(const JUPI_t& upi)
135  {
137 
138  if (p == upi2id.end()) {
139 
140  initialise();
141 
142  p = upi2id.find(upi);
143 
144  if (p == upi2id.end()) {
145  THROW(JDatabaseException, "Invalid UPI " << upi);
146  }
147  }
148 
149  return p->second;
150  }
151 
152 
153  /**
154  * Get %UPI.
155  *
156  * \param id module identifier
157  * \return %UPI
158  */
159  JUPI_t operator()(const int id)
160  {
162 
163  if (p == id2upi.end()) {
164 
165  initialise();
166 
167  p = id2upi.find(id);
168 
169  if (p == id2upi.end()) {
170  THROW(JDatabaseException, "Invalid module identifier " << id);
171  }
172  }
173 
174  return p->second;
175  }
176 
177  protected:
180  };
181 
182 
183  /**
184  * Auxiliary class for mapping %UPI of product to serial number.
185  */
186  struct JUPIHelper {
187  /**
188  * Initialise.
189  *
190  * \param pbs %PBS
191  */
192  void initialise(const JPBS_t& pbs)
193  {
194  ResultSet& rs = JDB::get()->StreamDS(getTable<JUPI>(), getSelector<JUPI>(pbs));
195 
196  for (JUPI parameters; rs >> parameters; ) {
197 
198  const JUPI_t upi(parameters.PBS,
199  parameters.VARIANT,
200  parameters.VERSION,
201  parameters.SERIALNUMBER);
202 
203  number2upi[pbs][parameters.SERIALNUMBER] = upi;
204  }
205 
206  rs.Close();
207  }
208 
209 
210  /**
211  * Get %UPI.
212  *
213  * \param pbs %PBS
214  * \param number serial number
215  * \return %UPI
216  */
217  JUPI_t operator()(const JPBS_t& pbs, const int number)
218  {
220 
221  if (p == number2upi[pbs].end()) {
222 
223  initialise(pbs);
224 
225  p = number2upi[pbs].find(number);
226 
227  if (p == number2upi[pbs].end()) {
228  THROW(JDatabaseException, "Invalid PBS / serial number " << pbs << " / " << number);
229  }
230  }
231 
232  return p->second;
233  }
234 
235  protected:
237  };
238 
239 
240  /**
241  * Wrapper data structure for initialisation of fuction objects.
242  *
243  * The fuction objects will expand on the fly making a corresponding query to the database.\n
244  * To avoid making a query within another query, a given function object can <em>a priori</em>
245  * be initialised using the static method JDBToolkit::initialise.
246  */
247  struct JDBToolkit {
248  /**
249  * Initialise.
250  *
251  * \param helper database helper
252  * \param args values
253  */
254  template<class JHelper_t, class ...Args>
255  static void initialise(JHelper_t& helper, const Args& ...args)
256  {
257  helper.initialise(args...);
258  }
259  };
260 
261 
262  extern JDetectorsHelper getDetector; //!< Function object for mapping serial number to object identifier of detector and vice versa.
263  extern JCLBIDHelper getCLBID; //!< Function object for mapping %UPI of central-logic board to module identifier.
264  static JCLBIDHelper& getCLBUPI = getCLBID; //!< Function object for mapping module identifier to %UPI of central-logic board.
265  extern JUPIHelper getUPI; //!< Function object for mapping %PBS and serial number to %UPI.
266 }
267 
268 #endif
Exceptions.
static JCLBIDHelper & getCLBUPI
Function object for mapping module identifier to UPI of central-logic board.
Definition: JDBToolkit.hh:264
static void initialise(JHelper_t &helper, const Args &...args)
Initialise.
Definition: JDBToolkit.hh:255
Database exception.
Definition: JException.hh:648
#define THROW(JException_t, A)
Marco for throwing exception with std::ostream compatible message.
Definition: JException.hh:670
*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
Universal product identifier (UPI).
Definition: JUPI_t.hh:29
Auxiliary class for specifying selection of database data.
Auxiliary class for mapping UPI of central-logic board to module identifier.
Definition: JDBToolkit.hh:111
JDetectorsHelper getDetector
Function object for mapping serial number to object identifier of detector and vice versa...
Definition: JDBToolkit.cc:5
std::map< std::string, int > det2id
Definition: JDBToolkit.hh:104
std::map< JUPI_t, int > upi2id
Definition: JDBToolkit.hh:178
int operator()(const JUPI_t &upi)
Get module identifier.
Definition: JDBToolkit.hh:134
void initialise(const JPBS_t &pbs)
Initialise.
Definition: JDBToolkit.hh:192
Auxiliary class for mapping UPI of product to serial number.
Definition: JDBToolkit.hh:186
std::map< int, JUPI_t > id2upi
Definition: JDBToolkit.hh:179
JUPI_t operator()(const int id)
Get UPI.
Definition: JDBToolkit.hh:159
std::map< JPBS_t, std::map< int, JUPI_t > > number2upi
Definition: JDBToolkit.hh:236
Auxiliary class for mapping serial number and object identifier of detectors.
Definition: JDBToolkit.hh:36
void initialise()
Initialise.
Definition: JDBToolkit.hh:115
then $JPP_DIR software JDB JAsciiDB q upi pbs
Definition: JTuneHV.sh:149
Wrapper data structure for initialisation of fuction objects.
Definition: JDBToolkit.hh:247
void initialise()
Initialise.
Definition: JDBToolkit.hh:40
Product breakdown structure (PBS).
Definition: JPBS_t.hh:25
std::string operator()(const int id)
Get detector identifier.
Definition: JDBToolkit.hh:84
JUPI_t operator()(const JPBS_t &pbs, const int number)
Get UPI.
Definition: JDBToolkit.hh:217
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
int operator()(const std::string &detid)
Get detector serial number.
Definition: JDBToolkit.hh:59
std::map< int, std::string > id2det
Definition: JDBToolkit.hh:103
Template definition for getting table specific selector.
static JDB & get()
Get connection to database.
Definition: JDB.hh:81