CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
HcalQIEManager.cc
Go to the documentation of this file.
1 //
2 // Gena Kukartsev (Brown), Feb 23, 2008
3 // $Id:
4 
5 #include <fstream>
6 
10 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
11 
12 #ifdef HAVE_XDAQ
13 #include "toolbox/string.h"
14 #else
15 #include "CaloOnlineTools/HcalOnlineDb/interface/xdaq_compat.h" // Replaces toolbox::toString
16 #endif
17 
18 using namespace std;
19 using namespace oracle::occi;
20 using namespace hcal;
21 
23 
25 
26 bool HcalChannelId::operator<(const HcalChannelId& other) const {
27  long long int _res_this, _res_other;
28  int _sub_this, _sub_other;
29 
30  if (this->subdetector == "HE")
31  _sub_this = 1;
32  else if (this->subdetector == "HF")
33  _sub_this = 2;
34  else if (this->subdetector == "HO")
35  _sub_this = 3;
36  else
37  _sub_this = 4;
38 
39  if (other.subdetector == "HE")
40  _sub_other = 1;
41  else if (other.subdetector == "HF")
42  _sub_other = 2;
43  else if (other.subdetector == "HO")
44  _sub_other = 3;
45  else
46  _sub_other = 4;
47 
48  _res_this = 100 + eta + (phi + 100) * 1000 + (depth + 10) * 1000000 + _sub_this * 1000000000;
49  _res_other = 100 + other.eta + (other.phi + 100) * 1000 + (other.depth + 10) * 1000000 + _sub_other * 1000000000;
50 
51  return _res_this < _res_other;
52 }
53 
54 std::map<HcalChannelId, HcalQIECaps>& HcalQIEManager::getQIETableFromFile(std::string _filename) {
55  std::map<HcalChannelId, HcalQIECaps>* result_sup = new std::map<HcalChannelId, HcalQIECaps>;
56  std::map<HcalChannelId, HcalQIECaps>& result = (*result_sup);
57 
58  ifstream infile(_filename.c_str());
60 
61  if (infile.is_open()) {
62  std::cout << "File is open" << std::endl;
63  while (getline(infile, buf)) {
64  std::vector<std::string> _line = splitString(buf);
65 
66  HcalChannelId _id;
67  sscanf(_line[0].c_str(), "%d", &_id.eta);
68  sscanf(_line[1].c_str(), "%d", &_id.phi);
69  sscanf(_line[2].c_str(), "%d", &_id.depth);
70  _id.subdetector = _line[3];
71 
72  HcalQIECaps _adc;
73  int _columns = _line.size();
74  for (int i = 4; i != _columns; i++) {
75  sscanf(_line[i].c_str(), "%lf", &_adc.caps[i - 4]);
76  }
77 
78  /* DEBUG: double entries
79  if(result.find(_id) != result.end()){
80  cout << "TABLE DEBUG: " << _filename << " " << _id.eta << " " << _id.phi << " " << _id.depth << " " << _id.subdetector << std::endl;
81  }
82  */
83 
84  //result[_id]=_adc;
85  result.insert(std::pair<HcalChannelId, HcalQIECaps>(_id, _adc));
86 
87  //std::cout << result.size() << std::endl;
88 
89  //std::cout << _id.eta << " " << _id . subdetector << " " << _adc.caps[7] << std::endl;
90  }
91  }
92  return result;
93 }
94 
95 // courtesy of Fedor Ratnikov
96 std::vector<std::string> HcalQIEManager::splitString(const std::string& fLine) {
97  std::vector<std::string> result;
98  int start = 0;
99  bool empty = true;
100  for (unsigned i = 0; i <= fLine.size(); i++) {
101  if (fLine[i] == ' ' || fLine[i] == '\n' || fLine[i] == ' ' || i == fLine.size()) {
102  if (!empty) {
103  std::string item(fLine, start, i - start);
104  result.push_back(item);
105  empty = true;
106  }
107  start = i + 1;
108  } else {
109  if (empty)
110  empty = false;
111  }
112  }
113  return result;
114 }
115 
117  std::cout << "Creating the output file: " << output_file << "... ";
118  ofstream out_file;
119  out_file.open(output_file.c_str());
120  std::cout << " done" << std::endl;
121 
122  HCALConfigDB* db = new HCALConfigDB();
123  const std::string _accessor =
124  "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
125  db->connect(_accessor);
126 
127  oracle::occi::Connection* _connection = db->getConnection();
128 
129  std::cout << "Preparing to request the QIE table from the database..." << std::endl;
130 
131  //loop over RM slots and QIEs (to save time, otherwise the query runs forever)
132  for (int _rm = 1; _rm != 5; _rm++) {
133  for (int _qie = 1; _qie != 4; _qie++) {
134  try {
135  cout << "Preparing the query..." << std::endl;
136  Statement* stmt = _connection->createStatement();
138  ifstream inFile(query_file.c_str(), std::ios::in);
139  if (!inFile) {
140  std::cout << " Unable to open file with query!" << std::endl;
141  } else {
142  std::cout << "Query file opened successfully: " << query_file << std::endl;
143  }
144  while (getline(inFile, buf)) {
145  query.append(buf);
146  query.append("\n");
147  }
148 
149  char query_fixed[50000];
150  sprintf(query_fixed, query.c_str(), _rm, _rm, _qie, _qie);
151  inFile.close();
152  cout << "Preparing the query... done" << std::endl;
153 
154  //SELECT
155  cout << "Executing the query..." << std::endl;
156  //std::cout << query_fixed << std::endl;
157  ResultSet* rs = stmt->executeQuery(query_fixed);
158  cout << "Executing the query... done" << std::endl;
159 
160  cout << "Processing the query results..." << std::endl;
161  RooGKCounter _lines(1, 100);
162  //int count;
163  while (rs->next()) {
164  _lines.count();
165  HcalChannelId _id;
166  HcalQIECaps _caps;
167  //count = rs->getInt(1);
168  _id.eta = rs->getInt(1);
169  _id.phi = rs->getInt(2);
170  _id.depth = rs->getInt(3);
171  _id.subdetector = rs->getString(4);
172  for (int j = 0; j != 32; j++) {
173  _caps.caps[j] = rs->getDouble(j + 5);
174  }
175 
176  //==> output QIE table line
177  char buffer[1024];
178  sprintf(buffer, "%15d %15d %15d %15s", _id.eta, _id.phi, _id.depth, _id.subdetector.c_str());
179  //std::cout << buffer;
180  out_file << buffer;
181  for (int j = 0; j != 32; j++) {
182  double _x = _caps.caps[j];
183  sprintf(buffer, " %8.5f", _x);
184  //std::cout << buffer;
185  out_file << buffer;
186  }
187  //std::cout << std::endl;
188  out_file << std::endl;
189  //===
190  }
191  //Always terminate statement
192  _connection->terminateStatement(stmt);
193 
194  //std::cout << "Query count: " << count << std::endl;
195  cout << "Query line count: " << _lines.getCount() << std::endl;
196  } catch (SQLException& e) {
197  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
198  ::toolbox::toString("Oracle exception : %s", e.getMessage().c_str()));
199  }
200  }
201  }
202 
203  db->disconnect();
204  out_file.close();
205 }
206 
207 // This function generates proper QIE table ASCII file based on the result
208 // of the database query from db_file.
209 // Missing channels are filled from the old table old_file.
210 // The result is placed in output_file.
212  std::cout << "Creating the output file: " << output_file << "... ";
213  ofstream out_file;
214  out_file.open(output_file.c_str());
215  std::cout << " done" << std::endl;
216 
217  std::string badchan_file = output_file + ".badchannels";
218  std::cout << "Creating the output file for bad channels: " << badchan_file << "... ";
219  ofstream bad_file;
220  bad_file.open(badchan_file.c_str());
221  std::cout << " done" << std::endl;
222 
223  std::map<HcalChannelId, HcalQIECaps>& _old = getQIETableFromFile(old_file);
224  std::map<HcalChannelId, HcalQIECaps>& _new = getQIETableFromFile(db_file);
225  //std::map<HcalChannelId,HcalQIECaps> & _old = _manager . getQIETableFromFile( "qie_normalmode_v3.txt" );
226  //std::map<HcalChannelId,HcalQIECaps> & _new = _manager . getQIETableFromFile( "qie_adc_table_after.txt" );
227 
228  int goodChannels = 0;
229  int badChannels = 0;
230  std::cout << "old size: " << _old.size() << std::endl;
231  std::cout << "new size: " << _new.size() << std::endl;
232  for (std::map<HcalChannelId, HcalQIECaps>::const_iterator line = _old.begin(); line != _old.end(); line++) {
233  HcalQIECaps* the_caps;
234  HcalChannelId theId = line->first;
235  bool badchannel = false;
236  if (_new.find(theId) == _new.end()) {
237  badchannel = true;
238  badChannels++;
239  the_caps = &_old[theId];
240  } else {
241  goodChannels++;
242  the_caps = &_new[theId];
243  }
244  char buffer[1024];
245  int eta = theId.eta;
246  int phi = theId.phi;
247  int depth = theId.depth;
248  sprintf(buffer, "%15d %15d %15d %15s", eta, phi, depth, theId.subdetector.c_str());
249  out_file << buffer;
250  if (badchannel)
251  bad_file << buffer;
252 
253  for (int j = 0; j != 32; j++) {
254  double _x = the_caps->caps[j];
255  sprintf(buffer, " %8.5f", _x);
256  out_file << buffer;
257  if (badchannel)
258  bad_file << buffer;
259  }
260  out_file << std::endl;
261  if (badchannel)
262  bad_file << std::endl;
263  }
264 
265  std::cout << goodChannels << " " << badChannels << " " << goodChannels + badChannels << std::endl;
266 
267  out_file.close();
268  bad_file.close();
269 
270  return 0;
271 }
272 
273 // get QIE barcodes per RBX from ascii file based on Tina Vernon's spreadsheet,
274 // get the LMAP channels based on LMAP from the database (OMDS, validation)
275 // associate QIEs with channels, then append respective ADC slopes and offsets
276 // based on QIE normmode table info from the database
277 // finally, generate the QIE table ascii file for HF
279  std::cout << "Creating the output file: " << output_file << "... ";
280  std::ofstream out_file;
281  out_file.open(output_file.c_str());
282  std::cout << " done" << std::endl;
283 
284  // process the file with QIE-per-RBX info
285  std::map<std::string, std::vector<int> > _qie; // 12 QIE barcodes per RBX: _qie["HFP11"]=...
286  std::ifstream infile(input_file.c_str());
288  if (infile.is_open()) {
289  std::cout << "File is open" << std::endl;
290  getline(infile, buf);
291  std::cout << "Table legend: " << std::endl << buf << std::endl;
292  while (getline(infile, buf)) {
293  std::vector<std::string> _line = splitString(buf);
294  if (_line.size() != 17) {
295  cout << "Table line is malformed, not clear what to do... exiting." << std::endl;
296  return -1;
297  }
298  std::string _rbx = _line[0];
299  std::vector<int> _barcodes;
300  for (int i = 0; i != 12; i++) {
301  int _code;
302  sscanf(_line[i + 5].c_str(), "%d", &_code);
303  _barcodes.push_back(_code);
304  }
305  _qie.insert(std::pair<std::string, std::vector<int> >(_rbx, _barcodes));
306  }
307  }
308 
309  // database stuff
310  HCALConfigDB* db = new HCALConfigDB();
311  const std::string _accessor =
312  "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
313  db->connect(_accessor);
314  oracle::occi::Connection* _connection = db->getConnection();
315  std::cout << "Preparing to request the HF channels from LMAP in the database..." << std::endl;
316  try {
317  std::cout << "Preparing the query..." << std::endl;
318  Statement* stmt = _connection->createStatement();
319 
320  std::cout << "Preparing the query... done" << std::endl;
322  query.append("select\n");
323  query.append("lmap.side*lmap.eta, lmap.phi, lmap.depth, lmap.subdetector,\n");
324  query.append("lmap.rbx, lmap.rm_slot, lmap.qie_slot, lmap.adc\n");
325  query.append("from cms_hcl_hcal_condition_owner.hcal_hardware_logical_maps_v3 lmap\n");
326  query.append("inner join cms_hcl_core_condition_owner.cond_data_sets cds\n");
327  query.append("on cds.condition_data_set_id=lmap.condition_data_set_id\n");
328  query.append("where cds.version='30'\n");
329  query.append("and lmap.subdetector='HF'\n");
330  query.append("order by lmap.rbx, lmap.rm_slot, lmap.qie_slot, lmap.adc\n");
331 
332  //SELECT
333  std::cout << "Executing the query..." << std::endl;
334  //std::cout << query << std::endl;
335  ResultSet* rs = stmt->executeQuery(query);
336  std::cout << "Executing the query... done" << std::endl;
337 
338  std::cout << "Processing the query results..." << std::endl;
339  RooGKCounter _lines(1, 100);
340  while (rs->next()) {
341  _lines.count();
342  HcalChannelId _id;
343  std::string rbx;
344  int rm_slot, qie_slot, adc, qie_barcode;
345  //count = rs->getInt(1);
346  _id.eta = rs->getInt(1);
347  _id.phi = rs->getInt(2);
348  _id.depth = rs->getInt(3);
349  _id.subdetector = rs->getString(4);
350  rbx = rs->getString(5);
351  rm_slot = rs->getInt(6);
352  qie_slot = rs->getInt(7);
353  adc = rs->getInt(8);
354  qie_barcode = _qie[rbx][(rm_slot - 1) * 4 + qie_slot - 1];
355 
356  //==>another DB query to get the ADC caps' slopes and offsets
357  // NOTE! In HF slope range and capID seem to be exchanged
358  try {
359  //std::cout << "Preparing the query..." << std::endl;
360  Statement* stmt2 = _connection->createStatement();
361  std::string query2;
362  query2.append("select\n");
363  query2.append(
364  "vadcs.cap0_range0_offset,vadcs.cap0_range1_offset,vadcs.cap0_range2_offset,vadcs.cap0_range3_offset,\n");
365  query2.append(
366  "vadcs.cap1_range0_offset,vadcs.cap1_range1_offset,vadcs.cap1_range2_offset,vadcs.cap1_range3_offset,\n");
367  query2.append(
368  "vadcs.cap2_range0_offset,vadcs.cap2_range1_offset,vadcs.cap2_range2_offset,vadcs.cap2_range3_offset,\n");
369  query2.append(
370  "vadcs.cap3_range0_offset,vadcs.cap3_range1_offset,vadcs.cap3_range2_offset,vadcs.cap3_range3_offset,\n");
371  query2.append(
372  "vadcs.cap0_range0_slope,vadcs.cap0_range1_slope,vadcs.cap0_range2_slope,vadcs.cap0_range3_slope,\n");
373  query2.append(
374  "vadcs.cap1_range0_slope,vadcs.cap1_range1_slope,vadcs.cap1_range2_slope,vadcs.cap1_range3_slope,\n");
375  query2.append(
376  "vadcs.cap2_range0_slope,vadcs.cap2_range1_slope,vadcs.cap2_range2_slope,vadcs.cap2_range3_slope,\n");
377  query2.append(
378  "vadcs.cap3_range0_slope,vadcs.cap3_range1_slope,vadcs.cap3_range2_slope,vadcs.cap3_range3_slope\n");
379  query2.append("from CMS_HCL_HCAL_CONDITION_OWNER.V_QIECARD_ADC_NORMMODE vadcs\n");
380  query2.append("where substr(vadcs.name_label,14,6)='%d'\n");
381  query2.append("and substr(vadcs.name_label,21,1)='%d'\n");
382  query2.append("order by version desc,record_id desc, condition_data_set_id desc\n");
383  char query2_fixed[5000];
384  sprintf(query2_fixed, query2.c_str(), qie_barcode, adc);
385  //std::cout << "Preparing the query... done" << std::endl;
386  //std::cout << query2_fixed << std::endl;
387 
388  //SELECT
389  //std::cout << "Executing the query..." << std::endl;
390  //std::cout << query2 << std::endl;
391  ResultSet* rs2 = stmt2->executeQuery(query2_fixed);
392  //std::cout << "Executing the query... done" << std::endl;
393 
394  //std::cout << "Processing the query results..." << std::endl;
395  // take only the first line - sorted by version descending, latest on top
396  if (rs2->next()) {
397  HcalQIECaps _caps;
398  for (int j = 0; j != 32; j++) {
399  _caps.caps[j] = rs2->getDouble(j + 1);
400  }
401  //==> output QIE table line
402  char buffer[1024];
403  sprintf(buffer, "%15d %15d %15d %15s", _id.eta, _id.phi, _id.depth, _id.subdetector.c_str());
404  //std::cout << buffer;
405  out_file << buffer;
406  for (int j = 0; j != 32; j++) {
407  double _x = _caps.caps[j];
408  sprintf(buffer, " %8.5f", _x);
409  //std::cout << buffer;
410  out_file << buffer;
411  }
412  //std::cout << std::endl;
413  out_file << std::endl;
414  //===
415  }
416  //Always terminate statement
417  _connection->terminateStatement(stmt2);
418 
419  } catch (SQLException& e) {
420  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
421  ::toolbox::toString("Oracle exception : %s", e.getMessage().c_str()));
422  }
423  }
424  //Always terminate statement
425  _connection->terminateStatement(stmt);
426 
427  //std::cout << "Query count: " << count << std::endl;
428  std::cout << "Query line count: " << _lines.getCount() << std::endl;
429  } catch (SQLException& e) {
430  XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,
431  ::toolbox::toString("Oracle exception : %s", e.getMessage().c_str()));
432  }
433 
434  db->disconnect();
435  out_file.close();
436 
437  return 0;
438 }
void disconnect(void)
Definition: HCALConfigDB.cc:76
double caps[32]
TString subdetector
int generateQieTable(std::string db_file, std::string old_file, std::string output_file)
tuple db
Definition: EcalCondDB.py:153
unsigned long int getCount(void)
Definition: RooGKCounter.cc:95
std::vector< std::string > splitString(const std::string &fLine)
tuple result
Definition: mps_fire.py:311
oracle::occi::Connection * getConnection(void)
static std::vector< std::string > splitString(const std::string &fLine)
std::string toString(const char *format,...)
Definition: xdaq_compat.cc:4
bool operator<(const HcalChannelId &other) const
std::string subdetector
Gather config data from online DB.
Definition: HCALConfigDB.h:20
string output_file
void connect(std::string _accessor)
Definition: HCALConfigDB.cc:48
std::map< HcalChannelId, HcalQIECaps > & getQIETableFromFile(std::string _filename)
void count(void)
Definition: RooGKCounter.cc:54
void getTableFromDb(std::string query_file, std::string output_file)
int getHfQieTable(std::string input_file, std::string output_file)
tuple cout
Definition: gather_cfg.py:144
uint16_t *__restrict__ uint16_t const *__restrict__ adc