CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HCALConfigDB.cc
Go to the documentation of this file.
1 
2 //
3 // Gena Kukartsev (Brown), Feb 1, 2008
4 // $Id:
5 //
6 
7 #include <iostream>
8 #include <cstring>
9 
10 #ifdef HAVE_XDAQ
11 #include <toolbox/string.h>
12 #else
13 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h" // Replaces toolbox::toString
14 #endif
15 
17 
20 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabase.hh"
21 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplOracle.hh"
22 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationDatabaseImplXMLFile.hh"
23 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
25 
26 using namespace std;
27 using namespace oracle::occi;
28 using namespace hcal;
29 
31  database = nullptr;
32  database2 = nullptr;
33 }
34 
36  database = nullptr;
37  database2 = nullptr;
38  accessor = _accessor;
39 }
40 
42  delete database;
43  delete database2;
44 }
45 
46 void HCALConfigDB::setAccessor(std::string _accessor) { accessor = _accessor; }
47 
49  accessor = _accessor;
50 
51  std::string::size_type i = accessor.find("occi://");
52  if (i != std::string::npos) {
53  database = new ConfigurationDatabaseImplOracle();
54  database->connect(accessor);
55  } else {
56  database = new ConfigurationDatabaseImplXMLFile();
57  database->connect(accessor);
58  }
59 }
60 
61 void HCALConfigDB::connect(std::string _accessor1, std::string _accessor2) {
62  connect(_accessor1);
63 
64  accessor2 = _accessor2;
65 
66  std::string::size_type i = accessor2.find("occi://");
67  if (i != std::string::npos) {
68  database2 = new ConfigurationDatabaseImplOracle();
69  database2->connect(accessor2);
70  } else {
71  database2 = new ConfigurationDatabaseImplXMLFile();
72  database2->connect(accessor2);
73  }
74 }
75 
77  if (database != nullptr)
78  database->disconnect();
79  if (database2 != nullptr)
80  database2->disconnect();
81 }
82 
83 std::vector<unsigned int> HCALConfigDB::getOnlineLUT(
84  std::string tag, int crate, int slot, int topbottom, int fiber, int channel, int luttype) {
85  //connect( accessor );
86 
87  std::vector<unsigned int> result;
88 
89  hcal::ConfigurationDatabase::FPGASelection _fpga;
90  if (topbottom == 0)
91  _fpga = hcal::ConfigurationDatabase::Bottom;
92  else if (topbottom == 1)
94  else {
95  std::cout << "topbottom out of range" << std::endl;
96  exit(-1);
97  }
98 
99  hcal::ConfigurationDatabase::LUTType _lt;
100  if (luttype == 1)
101  _lt = hcal::ConfigurationDatabase::LinearizerLUT;
102  else if (luttype == 2)
103  _lt = hcal::ConfigurationDatabase::CompressionLUT;
104  else {
105  std::cout << "LUT type out of range" << std::endl;
106  exit(-1);
107  }
108 
109  hcal::ConfigurationDatabase::LUTId _lutid(crate, slot, _fpga, fiber, channel, _lt);
110  std::map<hcal::ConfigurationDatabase::LUTId, hcal::ConfigurationDatabase::LUT> testLUTs;
111 
113 
114  try {
115  database->getLUTs(tag, crate, slot, testLUTs);
116  } catch (hcal::exception::ConfigurationItemNotFoundException& e) {
117  std::cout << "Found nothing!" << std::endl;
118  } catch (hcal::exception::Exception& e2) {
119  std::cout << "Exception: " << e2.what() << std::endl;
120  }
121 
122  result = testLUTs[_lutid];
123 
124  //database -> disconnect();
125 
126  return result;
127 }
128 
129 std::vector<unsigned int> HCALConfigDB::getOnlineLUT(std::string tag,
130  uint32_t _rawid,
131  hcal::ConfigurationDatabase::LUTType _lt) {
132  std::vector<unsigned int> result;
133  HcalDetId _id(_rawid);
134 
135  double _condition_data_set_id;
136  unsigned int _crate, _slot, _fiber, _channel;
137  hcal::ConfigurationDatabase::FPGASelection _fpga;
138 
139  int side = _id.zside();
140  int etaAbs = _id.ietaAbs();
141  int phi = _id.iphi();
142  int depth = _id.depth();
144  if (_id.subdet() == HcalBarrel)
145  subdetector = "HB";
146  else if (_id.subdet() == HcalEndcap)
147  subdetector = "HE";
148  else if (_id.subdet() == HcalOuter)
149  subdetector = "HO";
150  else if (_id.subdet() == HcalForward)
151  subdetector = "HF";
152 
153  oracle::occi::Connection* _connection = database->getConnection();
154 
155  try {
156  Statement* stmt = _connection->createStatement();
157  std::string query = ("SELECT RECORD_ID, CRATE, HTR_SLOT, HTR_FPGA, HTR_FIBER, FIBER_CHANNEL ");
158  query += " FROM CMS_HCL_HCAL_CONDITION_OWNER.HCAL_HARDWARE_LOGICAL_MAPS_V3 ";
159  query += toolbox::toString(" WHERE SIDE=%d AND ETA=%d AND PHI=%d AND DEPTH=%d AND SUBDETECTOR='%s'",
160  side,
161  etaAbs,
162  phi,
163  depth,
164  subdetector.c_str());
165 
166  //SELECT
167  ResultSet* rs = stmt->executeQuery(query);
168 
169  _condition_data_set_id = 0.0;
170 
171  while (rs->next()) {
172  double _cdsi = rs->getDouble(1);
173  if (_condition_data_set_id < _cdsi) {
174  _condition_data_set_id = _cdsi;
175  _crate = rs->getInt(2);
176  _slot = rs->getInt(3);
177  std::string fpga_ = rs->getString(4);
178  if (fpga_ == "top")
180  else
181  _fpga = hcal::ConfigurationDatabase::Bottom;
182  _fiber = rs->getInt(5);
183  _channel = rs->getInt(6);
184 
185  int topbottom, luttype;
187  topbottom = 1;
188  else
189  topbottom = 0;
190  if (_lt == hcal::ConfigurationDatabase::LinearizerLUT)
191  luttype = 1;
192  else
193  luttype = 2;
194 
195  result = getOnlineLUT(tag, _crate, _slot, topbottom, _fiber, _channel, luttype);
196  }
197  }
198  //Always terminate statement
199  _connection->terminateStatement(stmt);
200  } catch (SQLException& e) {
201  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
202  ::toolbox::toString("Oracle exception : %s", e.getMessage().c_str()));
203  }
204  return result;
205 }
206 
208  uint32_t _rawid,
209  hcal::ConfigurationDatabase::LUTType _lt) {
210  std::vector<unsigned int> result;
211 
212  if (database && database2) {
213  HcalDetId _id(_rawid);
214 
215  double _condition_data_set_id;
216  unsigned int _crate, _slot, _fiber, _channel;
217  hcal::ConfigurationDatabase::FPGASelection _fpga;
218 
219  int side = _id.zside();
220  int etaAbs = _id.ietaAbs();
221  int phi = _id.iphi();
222  int depth = _id.depth();
224  if (_id.subdet() == HcalBarrel)
225  subdetector = "HB";
226  else if (_id.subdet() == HcalEndcap)
227  subdetector = "HE";
228  else if (_id.subdet() == HcalOuter)
229  subdetector = "HO";
230  else if (_id.subdet() == HcalForward)
231  subdetector = "HF";
232 
233  oracle::occi::Connection* _connection = database2->getConnection();
234 
235  try {
236  Statement* stmt = _connection->createStatement();
237  std::string query = ("SELECT RECORD_ID, CRATE, HTR_SLOT, HTR_FPGA, HTR_FIBER, FIBER_CHANNEL ");
238  query += " FROM CMS_HCL_HCAL_CONDITION_OWNER.HCAL_HARDWARE_LOGICAL_MAPS_V3 ";
239  query += toolbox::toString(" WHERE SIDE=%d AND ETA=%d AND PHI=%d AND DEPTH=%d AND SUBDETECTOR='%s'",
240  side,
241  etaAbs,
242  phi,
243  depth,
244  subdetector.c_str());
245 
246  //SELECT
247  ResultSet* rs = stmt->executeQuery(query);
248 
249  _condition_data_set_id = 0.0;
250 
251  while (rs->next()) {
252  double _cdsi = rs->getDouble(1);
253  if (_condition_data_set_id < _cdsi) {
254  _condition_data_set_id = _cdsi;
255  _crate = rs->getInt(2);
256  _slot = rs->getInt(3);
257  std::string fpga_ = rs->getString(4);
258  if (fpga_ == "top")
260  else
261  _fpga = hcal::ConfigurationDatabase::Bottom;
262  _fiber = rs->getInt(5);
263  _channel = rs->getInt(6);
264 
265  //
266  int topbottom, luttype;
268  topbottom = 1;
269  else
270  topbottom = 0;
271  if (_lt == hcal::ConfigurationDatabase::LinearizerLUT)
272  luttype = 1;
273  else
274  luttype = 2;
275  result = getOnlineLUT(tag, _crate, _slot, topbottom, _fiber, _channel, luttype);
276  }
277  }
278  //Always terminate statement
279  _connection->terminateStatement(stmt);
280 
281  } catch (SQLException& e) {
282  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
283  ::toolbox::toString("Oracle exception : %s", e.getMessage().c_str()));
284  }
285  } else {
286  std::cout << "Either the XML file with LUTs or the database with LMap are not defined" << std::endl;
287  }
288 
289  return result;
290 }
291 
292 oracle::occi::Connection* HCALConfigDB::getConnection(void) { return database->getConnection(); }
293 
294 oracle::occi::Environment* HCALConfigDB::getEnvironment(void) { return database->getEnvironment(); }
295 
296 //Utility function that cnverts oracle::occi::Clob to std::string
297 string HCALConfigDB::clobToString(const oracle::occi::Clob& _clob) {
298  oracle::occi::Clob clob = _clob;
299  Stream* instream = clob.getStream(1, 0);
300  unsigned int size = clob.length();
301  char* cbuffer = new char[size];
302  memset(cbuffer, 0, size);
303  instream->readBuffer(cbuffer, size);
304  std::string str(cbuffer, size);
305  return str;
306 }
void disconnect(void)
Definition: HCALConfigDB.cc:76
constexpr int ietaAbs() const
get the absolute value of the cell ieta
Definition: HcalDetId.h:148
constexpr int zside() const
get the z-side of the cell (1/-1)
Definition: HcalDetId.h:141
TString subdetector
uint16_t size_type
tuple result
Definition: mps_fire.py:311
constexpr HcalSubdetector subdet() const
get the subdetector
Definition: HcalDetId.h:138
static const std::string Top
oracle::occi::Environment * getEnvironment(void)
oracle::occi::Connection * getConnection(void)
constexpr int iphi() const
get the cell iphi
Definition: HcalDetId.h:157
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
std::vector< unsigned int > getOnlineLUT(std::string tag, int crate, int slot, int topbottom, int fiber, int channel, int luttype)
Definition: HCALConfigDB.cc:83
void connect(std::string _accessor)
Definition: HCALConfigDB.cc:48
void setAccessor(std::string _accessor)
Definition: HCALConfigDB.cc:46
std::vector< unsigned int > getOnlineLUTFromXML(std::string tag, uint32_t _rawid, hcal::ConfigurationDatabase::LUTType _lt=hcal::ConfigurationDatabase::LinearizerLUT)
tuple cout
Definition: gather_cfg.py:144
constexpr int depth() const
get the tower depth
Definition: HcalDetId.h:164
std::string clobToString(const oracle::occi::Clob &)
#define str(s)
tuple size
Write out results.
static XMLProcessor * getInstance()
Definition: XMLProcessor.h:134