CMS 3D CMS Logo

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