00001
00002
00003
00004
00005 #include <fstream>
00006
00007 #include "CaloOnlineTools/HcalOnlineDb/interface/HcalQIEManager.h"
00008 #include "CaloOnlineTools/HcalOnlineDb/interface/RooGKCounter.h"
00009 #include "xgi/Utils.h"
00010 #include "toolbox/string.h"
00011 #include "OnlineDB/Oracle/interface/Oracle.h"
00012 #include "CaloOnlineTools/HcalOnlineDb/interface/ConfigurationItemNotFoundException.hh"
00013
00014 using namespace std;
00015 using namespace oracle::occi;
00016 using namespace hcal;
00017
00018 HcalQIEManager::HcalQIEManager( void )
00019 {
00020
00021 }
00022
00023
00024
00025 HcalQIEManager::~HcalQIEManager( void )
00026 {
00027
00028 }
00029
00030
00031
00032 bool HcalChannelId::operator<( const HcalChannelId & other) const
00033 {
00034 long long int _res_this, _res_other;
00035 int _sub_this, _sub_other;
00036
00037 if (this->subdetector == "HE") _sub_this=1;
00038 else if (this->subdetector == "HF") _sub_this=2;
00039 else if (this->subdetector == "HO") _sub_this=3;
00040 else _sub_this=4;
00041
00042 if (other.subdetector == "HE") _sub_other=1;
00043 else if (other.subdetector == "HF") _sub_other=2;
00044 else if (other.subdetector == "HO") _sub_other=3;
00045 else _sub_other=4;
00046
00047
00048 _res_this = 100+eta + (phi+100)*1000 + (depth+10)*1000000 + _sub_this*1000000000;
00049 _res_other = 100+other.eta + (other.phi+100)*1000 + (other.depth+10)*1000000 + _sub_other*1000000000;
00050
00051 return _res_this < _res_other;
00052 }
00053
00054 std::map<HcalChannelId,HcalQIECaps> & HcalQIEManager::getQIETableFromFile( std::string _filename )
00055 {
00056 std::map<HcalChannelId,HcalQIECaps> * result_sup = new std::map<HcalChannelId,HcalQIECaps>;
00057 std::map<HcalChannelId,HcalQIECaps> & result = (*result_sup);
00058
00059 ifstream infile( _filename . c_str() );
00060 std::string buf;
00061
00062 if ( infile . is_open() ){
00063 std::cout << "File is open" << std::endl;
00064 while ( getline( infile, buf ) > 0 ){
00065 std::vector<std::string> _line = splitString( buf );
00066
00067 HcalChannelId _id;
00068 sscanf(_line[0].c_str(), "%d", &_id . eta);
00069 sscanf(_line[1].c_str(), "%d", &_id . phi);
00070 sscanf(_line[2].c_str(), "%d", &_id . depth);
00071 _id . subdetector = _line[3];
00072
00073 HcalQIECaps _adc;
00074 int _columns = _line . size();
00075 for(int i = 4; i != _columns; i++){
00076 sscanf(_line[i].c_str(), "%lf", &_adc . caps[i-4]);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086 result.insert( std::pair<HcalChannelId,HcalQIECaps>(_id, _adc ) );
00087
00088
00089
00090
00091 }
00092 }
00093 return result;
00094 }
00095
00096
00097
00098
00099 std::vector <std::string> HcalQIEManager::splitString (const std::string& fLine) {
00100 std::vector <std::string> result;
00101 int start = 0;
00102 bool empty = true;
00103 for (unsigned i = 0; i <= fLine.size (); i++) {
00104 if (fLine [i] == ' ' || fLine [i] == '\n' || fLine [i] == ' ' || i == fLine.size ()) {
00105 if (!empty) {
00106 std::string item (fLine, start, i-start);
00107 result.push_back (item);
00108 empty = true;
00109 }
00110 start = i+1;
00111 }
00112 else {
00113 if (empty) empty = false;
00114 }
00115 }
00116 return result;
00117 }
00118
00119
00120
00121 void HcalQIEManager::getTableFromDb( std::string query_file, std::string output_file)
00122 {
00123 std::cout << "Creating the output file: " << output_file << "... ";
00124 ofstream out_file;
00125 out_file . open( output_file.c_str() );
00126 std::cout << " done" << std::endl;
00127
00128 HCALConfigDB * db = new HCALConfigDB();
00129 const std::string _accessor = "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
00130 db -> connect( _accessor );
00131
00132 oracle::occi::Connection * _connection = db -> getConnection();
00133
00134 std::cout << "Preparing to request the QIE table from the database..." << std::endl;
00135
00136
00137 for (int _rm=1; _rm!=5; _rm++){
00138 for (int _qie=1; _qie!=4; _qie++){
00139 try {
00140 cout << "Preparing the query..." << std::endl;
00141 Statement* stmt = _connection -> createStatement();
00142 std::string query, buf;
00143 ifstream inFile( query_file . c_str(), std::ios::in );
00144 if (!inFile){
00145 std::cout << " Unable to open file with query!" << std::endl;
00146 }
00147 else{
00148 std::cout << "Query file opened successfully: " << query_file << std::endl;
00149 }
00150 while ( getline( inFile, buf ) > 0 ){
00151 query . append(buf);
00152 query . append("\n");
00153 }
00154
00155 char query_fixed[50000];
00156 sprintf(query_fixed,query.c_str(),_rm,_rm,_qie,_qie);
00157 inFile.close();
00158 cout << "Preparing the query... done" << std::endl;
00159
00160
00161 cout << "Executing the query..." << std::endl;
00162
00163 ResultSet *rs = stmt->executeQuery(query_fixed);
00164 cout << "Executing the query... done" << std::endl;
00165
00166 cout << "Processing the query results..." << std::endl;
00167 RooGKCounter _lines(1,100);
00168
00169 while (rs->next()) {
00170 _lines . count();
00171 HcalChannelId _id;
00172 HcalQIECaps _caps;
00173
00174 _id.eta = rs->getInt(1);
00175 _id.phi = rs->getInt(2);
00176 _id.depth = rs->getInt(3);
00177 _id.subdetector = rs -> getString(4);
00178 for (int j=0; j!=32; j++){
00179 _caps.caps[j] = rs -> getDouble(j+5);
00180 }
00181
00182
00183 char buffer[1024];
00184 sprintf(buffer, "%15d %15d %15d %15s", _id.eta, _id.phi, _id.depth, _id.subdetector.c_str());
00185
00186 out_file << buffer;
00187 for (int j = 0; j != 32; j++){
00188 double _x = _caps . caps[j];
00189 sprintf(buffer, " %8.5f", _x);
00190
00191 out_file << buffer;
00192 }
00193
00194 out_file << std::endl;
00195
00196
00197 }
00198
00199 _connection -> terminateStatement(stmt);
00200
00201
00202 cout << "Query line count: " << _lines.getCount() << std::endl;
00203 } catch (SQLException& e) {
00204 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
00205 }
00206 }
00207 }
00208
00209 db -> disconnect();
00210 out_file.close();
00211 }
00212
00213
00214
00215
00216
00217
00218
00219 int HcalQIEManager::generateQieTable( std::string db_file, std::string old_file, std::string output_file )
00220 {
00221 std::cout << "Creating the output file: " << output_file << "... ";
00222 ofstream out_file;
00223 out_file . open( output_file.c_str() );
00224 std::cout << " done" << std::endl;
00225
00226 std::string badchan_file = output_file + ".badchannels";
00227 std::cout << "Creating the output file for bad channels: " << badchan_file << "... ";
00228 ofstream bad_file;
00229 bad_file . open( badchan_file.c_str() );
00230 std::cout << " done" << std::endl;
00231
00232 std::map<HcalChannelId,HcalQIECaps> & _old = getQIETableFromFile( old_file . c_str() );
00233 std::map<HcalChannelId,HcalQIECaps> & _new = getQIETableFromFile( db_file . c_str() );
00234
00235
00236
00237 int goodChannels = 0;
00238 int badChannels = 0;
00239 std::cout << "old size: " << _old.size() << std::endl;
00240 std::cout << "new size: " << _new.size() << std::endl;
00241 for (std::map<HcalChannelId,HcalQIECaps>::const_iterator line=_old.begin(); line!=_old.end(); line++ ){
00242 HcalQIECaps * the_caps;
00243 HcalChannelId theId = line -> first;
00244 bool badchannel = false;
00245 if (_new.find(theId)==_new.end()){
00246 badchannel=true;
00247 badChannels++;
00248 the_caps = &_old[theId];
00249 }
00250 else{
00251 goodChannels++;
00252 the_caps = &_new[theId];
00253 }
00254 char buffer[1024];
00255 int eta = theId.eta;
00256 int phi = theId.phi;
00257 int depth = theId.depth;
00258 sprintf(buffer, "%15d %15d %15d %15s", eta, phi, depth, theId.subdetector.c_str());
00259 out_file << buffer;
00260 if (badchannel) bad_file << buffer;
00261
00262 for (int j = 0; j != 32; j++){
00263 double _x = the_caps->caps[j];
00264 sprintf(buffer, " %8.5f", _x);
00265 out_file << buffer;
00266 if (badchannel) bad_file << buffer;
00267 }
00268 out_file << std::endl;
00269 if (badchannel) bad_file << std::endl;
00270 }
00271
00272 std::cout<< goodChannels<< " " << badChannels << " " << goodChannels+badChannels << std::endl;
00273
00274 out_file . close();
00275 bad_file . close();
00276
00277 return 0;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286 int HcalQIEManager::getHfQieTable( std::string input_file, std::string output_file )
00287 {
00288 std::cout << "Creating the output file: " << output_file << "... ";
00289 std::ofstream out_file;
00290 out_file . open( output_file.c_str() );
00291 std::cout << " done" << std::endl;
00292
00293
00294 std::map<std::string,std::vector<int> > _qie;
00295 std::ifstream infile( input_file . c_str() );
00296 std::string buf;
00297 if ( infile . is_open() ){
00298 std::cout << "File is open" << std::endl;
00299 getline( infile, buf );
00300 std::cout << "Table legend: " << std::endl << buf << std::endl;
00301 while ( getline( infile, buf ) > 0 ){
00302 std::vector<std::string> _line = splitString( buf );
00303 if ( _line . size() != 17){
00304 cout << "Table line is malformed, not clear what to do... exiting." << std::endl;
00305 return -1;
00306 }
00307 std::string _rbx = _line[0];
00308 std::vector<int> _barcodes;
00309 for (int i=0; i!=12; i++){
00310 int _code;
00311 sscanf(_line[i+5].c_str(),"%d", &_code);
00312 _barcodes . push_back(_code);
00313 }
00314 _qie . insert( std::pair<std::string,std::vector<int> >(_rbx,_barcodes) );
00315 }
00316 }
00317
00318
00319 HCALConfigDB * db = new HCALConfigDB();
00320 const std::string _accessor = "occi://CMS_HCL_PRTTYPE_HCAL_READER@anyhost/int2r?PASSWORD=HCAL_Reader_88,LHWM_VERSION=22";
00321 db -> connect( _accessor );
00322 oracle::occi::Connection * _connection = db -> getConnection();
00323 std::cout << "Preparing to request the HF channels from LMAP in the database..." << std::endl;
00324 try {
00325 std::cout << "Preparing the query..." << std::endl;
00326 Statement* stmt = _connection -> createStatement();
00327
00328 std::cout << "Preparing the query... done" << std::endl;
00329 std::string query;
00330 query . append("select\n");
00331 query . append("lmap.side*lmap.eta, lmap.phi, lmap.depth, lmap.subdetector,\n");
00332 query . append("lmap.rbx, lmap.rm_slot, lmap.qie_slot, lmap.adc\n");
00333 query . append("from cms_hcl_hcal_condition_owner.hcal_hardware_logical_maps_v3 lmap\n");
00334 query . append("inner join cms_hcl_core_condition_owner.cond_data_sets cds\n");
00335 query . append("on cds.condition_data_set_id=lmap.condition_data_set_id\n");
00336 query . append("where cds.version='30'\n");
00337 query . append("and lmap.subdetector='HF'\n");
00338 query . append("order by lmap.rbx, lmap.rm_slot, lmap.qie_slot, lmap.adc\n");
00339
00340
00341 std::cout << "Executing the query..." << std::endl;
00342
00343 ResultSet *rs = stmt->executeQuery(query.c_str());
00344 std::cout << "Executing the query... done" << std::endl;
00345
00346 std::cout << "Processing the query results..." << std::endl;
00347 RooGKCounter _lines(1,100);
00348 while (rs->next()) {
00349 _lines . count();
00350 HcalChannelId _id;
00351 std::string rbx;
00352 int rm_slot, qie_slot, adc, qie_barcode;
00353
00354 _id.eta = rs->getInt(1);
00355 _id.phi = rs->getInt(2);
00356 _id.depth = rs->getInt(3);
00357 _id.subdetector = rs -> getString(4);
00358 rbx = rs->getString(5);
00359 rm_slot = rs->getInt(6);
00360 qie_slot = rs->getInt(7);
00361 adc = rs->getInt(8);
00362 qie_barcode = _qie[rbx][(rm_slot-1)*4+qie_slot-1];
00363
00364
00365
00366 try {
00367
00368 Statement* stmt2 = _connection -> createStatement();
00369 std::string query2;
00370 query2 . append("select\n");
00371 query2 . append("vadcs.cap0_range0_offset,vadcs.cap0_range1_offset,vadcs.cap0_range2_offset,vadcs.cap0_range3_offset,\n");
00372 query2 . append("vadcs.cap1_range0_offset,vadcs.cap1_range1_offset,vadcs.cap1_range2_offset,vadcs.cap1_range3_offset,\n");
00373 query2 . append("vadcs.cap2_range0_offset,vadcs.cap2_range1_offset,vadcs.cap2_range2_offset,vadcs.cap2_range3_offset,\n");
00374 query2 . append("vadcs.cap3_range0_offset,vadcs.cap3_range1_offset,vadcs.cap3_range2_offset,vadcs.cap3_range3_offset,\n");
00375 query2 . append("vadcs.cap0_range0_slope,vadcs.cap0_range1_slope,vadcs.cap0_range2_slope,vadcs.cap0_range3_slope,\n");
00376 query2 . append("vadcs.cap1_range0_slope,vadcs.cap1_range1_slope,vadcs.cap1_range2_slope,vadcs.cap1_range3_slope,\n");
00377 query2 . append("vadcs.cap2_range0_slope,vadcs.cap2_range1_slope,vadcs.cap2_range2_slope,vadcs.cap2_range3_slope,\n");
00378 query2 . append("vadcs.cap3_range0_slope,vadcs.cap3_range1_slope,vadcs.cap3_range2_slope,vadcs.cap3_range3_slope\n");
00379 query2 . append("from CMS_HCL_HCAL_CONDITION_OWNER.V_QIECARD_ADC_NORMMODE vadcs\n");
00380 query2 . append("where substr(vadcs.name_label,14,6)='%d'\n");
00381 query2 . append("and substr(vadcs.name_label,21,1)='%d'\n");
00382 query2 . append("order by version desc,record_id desc, condition_data_set_id desc\n");
00383 char query2_fixed[5000];
00384 sprintf(query2_fixed,query2.c_str(),qie_barcode,adc);
00385
00386
00387
00388
00389
00390
00391 ResultSet *rs2 = stmt2->executeQuery(query2_fixed);
00392
00393
00394
00395
00396 if (rs2->next()) {
00397 HcalQIECaps _caps;
00398 for (int j=0; j!=32; j++){
00399 _caps.caps[j] = rs2 -> getDouble(j+1);
00400 }
00401
00402 char buffer[1024];
00403 sprintf(buffer, "%15d %15d %15d %15s", _id.eta, _id.phi, _id.depth, _id.subdetector.c_str());
00404
00405 out_file << buffer;
00406 for (int j = 0; j != 32; j++){
00407 double _x = _caps . caps[j];
00408 sprintf(buffer, " %8.5f", _x);
00409
00410 out_file << buffer;
00411 }
00412
00413 out_file << std::endl;
00414
00415 }
00416
00417 _connection -> terminateStatement(stmt2);
00418
00419 } catch (SQLException& e) {
00420 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
00421 }
00422 }
00423
00424 _connection -> terminateStatement(stmt);
00425
00426
00427 std::cout << "Query line count: " << _lines.getCount() << std::endl;
00428 } catch (SQLException& e) {
00429 XCEPT_RAISE(hcal::exception::ConfigurationDatabaseException,::toolbox::toString("Oracle exception : %s",e.getMessage().c_str()));
00430 }
00431
00432 db -> disconnect();
00433 out_file.close();
00434
00435 return 0;
00436 }