CMS 3D CMS Logo

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