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 #include "JLang/JSingleton.hh"
9 
10 #include "JDB/JDB.hh"
11 #include "JDB/JSelector.hh"
13 #include "JDB/JDetectors.hh"
14 
15 
16 /**
17  * \author mdejong
18  */
19 
20 namespace JDATABASE {};
21 namespace JPP { using namespace JDATABASE; }
22 
23 /**
24  * Auxiliary classes and methods for database I/O.
25  */
26 namespace JDATABASE {
27 
29  using JLANG::JSingleton;
30 
31  /**
32  * Auxiliary class for mapping serial number and object identifier of detectors.
33  */
35  public JSingleton<JDetectorsHelper>
36  {
37  /**
38  * Initialise.
39  */
40  static void initialise()
41  {
42  ResultSet& rs = JDB::get()->StreamDS(getTable<JDetectors>(), JSelector());
43 
44  for (JDetectors parameters; rs >> parameters; ) {
45  getInstance().det2id[parameters.OID] = parameters.SERIALNUMBER;
46  getInstance().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) const
60  {
62 
63  if (p == det2id.end()) {
64 
65  ResultSet& rs = JDB::get()->StreamDS(getTable<JDetectors>(), getSelector<JDetectors>(detid));
66 
68 
69  if (rs >> parameters) {
70 
71  rs.Close();
72 
73  det2id[parameters.OID] = parameters.SERIALNUMBER;
74  id2det[parameters.SERIALNUMBER] = parameters.OID;
75 
76  return parameters.SERIALNUMBER;
77  }
78 
79  } else {
80 
81  return p->second;
82  }
83 
84  THROW(JDatabaseException, "Invalid detector identifier " << detid);
85  }
86 
87 
88  /**
89  * Get detector identifier.
90  *
91  * \param id serial number
92  * \return object identifier
93  */
94  inline std::string operator()(const int id) const
95  {
97 
98  if (p == id2det.end()) {
99 
100  ResultSet& rs = JDB::get()->StreamDS(getTable<JDetectors>(), getSelector<JDetectors>(id));
101 
103 
104  if (rs >> parameters) {
105 
106  rs.Close();
107 
108  id2det[parameters.SERIALNUMBER] = parameters.OID;
109  det2id[parameters.OID] = parameters.SERIALNUMBER;
110 
111  return parameters.OID;
112  }
113 
114  } else {
115 
116  return p->second;
117  }
118 
119  THROW(JDatabaseException, "Invalid detector identifier " << id);
120  }
121 
122  protected:
125  };
126 
127 
128  /**
129  * Function object for mapping serial number and object identifier of detectors.
130  */
132 }
133 
134 #endif
Exceptions.
Simple singleton class.
Definition: JSingleton.hh:18
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
Auxiliary class for specifying selection of database data.
std::string operator()(const int id) const
Get detector identifier.
Definition: JDBToolkit.hh:94
static void initialise()
Initialise.
Definition: JDBToolkit.hh:40
std::map< std::string, int > det2id
Definition: JDBToolkit.hh:124
static const JDetectorsHelper & getDetector
Function object for mapping serial number and object identifier of detectors.
Definition: JDBToolkit.hh:131
Auxiliary class for mapping serial number and object identifier of detectors.
Definition: JDBToolkit.hh:34
static data_type & getInstance()
Get unique instance of template class.
Definition: JSingleton.hh:27
int operator()(const std::string &detid) const
Get detector serial number.
Definition: JDBToolkit.hh:59
std::map< int, std::string > id2det
Definition: JDBToolkit.hh:123
Template definition for getting table specific selector.
static JDB & get()
Get connection to database.
Definition: JDB.hh:81