CMS 3D CMS Logo

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