test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
HcalDbPoolOCCI.cc
Go to the documentation of this file.
1 
2 //
3 // F.Ratnikov (UMd), Dec 14, 2005
4 // $Id: HcalDbPoolOCCI.cc,v 1.6 2009/12/04 03:42:53 elmer Exp $
5 //
6 #include <string>
7 #include <iostream>
8 #include <cstdio>
9 
11 
13 
14 const bool debug = false;
15 
16 namespace {
17  long getObjectId (const std::string& fToken) {
18  size_t ipos = fToken.find ("OID=");
19  if (ipos != std::string::npos) {
20  ipos = fToken.find ('-', ipos);
21  if (ipos != std::string::npos) {
22  size_t ipos2 = fToken.find (']', ipos);
23  if (ipos2 != std::string::npos) {
24  while (fToken [++ipos] != '0');
25  std::string id (fToken, ipos, ipos2-ipos);
26  char* endptr = 0;
27  unsigned long result = strtoul (id.c_str (), &endptr, 16);
28  if (endptr && !*endptr) return long (result);
29  }
30  }
31  }
32  return -1;
33  }
34 
35  const char* getTable (const HcalPedestals* fObject) {return "HCALPEDESTAL";}
36  const char* getTable (const HcalGains* fObject) {return "HCALGAIN";}
37 }
38 
40  : mConnect (0)
41 {
42  mEnvironment = oracle::occi::Environment::createEnvironment (oracle::occi::Environment::OBJECT);
43  // decode connect string
44  size_t ipass = fDb.find ('/');
45  size_t ihost = fDb.find ('@');
46 
47  if (ipass == std::string::npos || ihost == std::string::npos) {
48  std::cerr << "HcalDbPoolOCCI::HcalDbPoolOCCI-> Error in connection string format: " << fDb
49  << " Expect user/password@db" << std::endl;
50  }
51  else {
52  std::string user (fDb, 0, ipass);
53  std::string pass (fDb, ipass+1, ihost-ipass-1);
54  std::string host (fDb, ihost+1);
55  // if (debug) std::cout << "HcalDbPoolOCCI::HcalDbPoolOCCI-> Connecting " << user << '/' << pass << '@' << host << std::endl;
56  try {
57  mConnect = mEnvironment->createConnection(user, pass, host);
58  mStatement = mConnect->createStatement ();
59  }
60  catch (oracle::occi::SQLException& sqlExcp) {
61  std::cerr << "HcalDbPoolOCCI::HcalDbPoolOCCI exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
62  }
63  }
64 }
65 
67  delete mStatement;
68  mEnvironment->terminateConnection (mConnect);
69  oracle::occi::Environment::terminateEnvironment (mEnvironment);
70 }
71 
72 bool HcalDbPoolOCCI::getObject (HcalPedestals* fObject, const std::string& fTag, unsigned long fRun) {
73  HcalPedestal* myped(0);
74  return getObjectGeneric (fObject, myped, fTag, fRun);
75 }
76 
77 bool HcalDbPoolOCCI::getObject (HcalGains* fObject, const std::string& fTag, unsigned long fRun) {
78  HcalGain* mygain(0);
79  return getObjectGeneric (fObject, mygain, fTag, fRun);
80 }
81 
82 bool HcalDbPoolOCCI::getObject (HcalElectronicsMap* fObject, const std::string& fTag, unsigned long fRun) {
83  return false;
84 }
85 
86 
87 
89  std::string result = "";
90  std::string sql_query = "select * from metadata";
91  try {
92  if (debug) std::cout << "executing query: \n" << sql_query << std::endl;
93  mStatement->setPrefetchRowCount (100);
94  mStatement->setSQL (sql_query);
95  oracle::occi::ResultSet* rset = mStatement->executeQuery ();
96  while (rset->next ()) {
97  std::string name = rset->getString (1);
98  std::string token = rset->getString (2);
99  if (name == fTag) {
100  result = token;
101  break;
102  }
103  }
104  delete rset;
105  }
106  catch (oracle::occi::SQLException& sqlExcp) {
107  std::cerr << "HcalDbPoolOCCI::getMetadataToken exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
108  }
109  return result;
110 }
111 
112 std::string HcalDbPoolOCCI::getDataToken (const std::string& fIov, unsigned long fRun) {
113  std::string result = "";
114  long iovId = getObjectId (fIov);
115  if (iovId >= 0) {
116  char sql_query [1024];
117  sprintf (sql_query, "select IOV_IOV_UNSIGNED_LONG, IOV_IOV_STRING from COND__IOV_IOV where ID_ID = %ld ORDER BY IOV_IOV_UNSIGNED_LONG DESC", iovId);
118  try {
119  if (debug) std::cout << "executing query: \n" << sql_query << std::endl;
120  mStatement->setPrefetchRowCount (100);
121  mStatement->setSQL (std::string (sql_query));
122  oracle::occi::ResultSet* rset = mStatement->executeQuery ();
123  while (rset->next ()) {
124  unsigned long runMax = rset->getUInt (1);
125  std::string token = rset->getString (2);
126  if (fRun <= runMax) {
127  result = token;
128  break;
129  }
130  }
131  delete rset;
132  }
133  catch (oracle::occi::SQLException& sqlExcp) {
134  std::cerr << "HcalDbPoolOCCI::getDataToken exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
135  }
136  }
137  return result;
138 }
139 
140 template <class T, class S>
141 bool HcalDbPoolOCCI::getObjectGeneric (T* fObject, S* fCondObject, const std::string& fTag, unsigned long fRun) {
142  std::string mdToken = getMetadataToken (fTag);
143  if (debug) std::cout << "HcalDbPoolOCCI::getObjectGeneric-> tag/token: " << fTag << '/' << mdToken << std::endl;
144  if (mdToken.empty ()) return false;
145  std::string objToken = getDataToken (mdToken, fRun);
146  if (debug) std::cout << "HcalDbPoolOCCI::getObjectGeneric-> Run/token: " << fRun << '/' << objToken << std::endl;
147  if (objToken.empty ()) return false;
148  long id = getObjectId (objToken);
149  if (id >= 0) {
150  char sql_query [1024];
151  const char* name = getTable (fObject);
152  sprintf (sql_query, "select MITEMS_%s_MID, MITEMS_%s_MVALUE1, MITEMS_%s_MVALUE2, MITEMS_%s_MVALUE3, MITEMS_%s_MVALUE4 from %sS_MITEMS where ID_ID = %ld ORDER BY MITEMS_%s_MID",
153  name, name, name, name, name, name, id, name);
154  try {
155  if (debug) std::cout << "executing query: \n" << sql_query << std::endl;
156  mStatement->setPrefetchRowCount (100);
157  mStatement->setSQL (sql_query);
158  oracle::occi::ResultSet* rset = mStatement->executeQuery ();
159  while (rset->next ()) {
160  unsigned long hcalId = rset->getUInt (1);
161  float values [4];
162  for (int i = 0; i < 4; i++) values [i] = rset->getFloat (i+2);
163 
164  fCondObject = new S(DetId (hcalId), values[0], values[1], values[2], values[3]);
165  fObject->addValues (*fCondObject);
166  delete fCondObject;
167  // if (debug) std::cout << "new entry: " << hcalId << '/' << values [0] << '/' << values [1] << '/'
168  // << values [2] << '/' << values [3] << std::endl;
169  }
170  delete rset;
171  return true;
172  }
173  catch (oracle::occi::SQLException& sqlExcp) {
174  std::cerr << "HcalDbPoolOCCI::getObjectn exception-> " << sqlExcp.getErrorCode () << ": " << sqlExcp.what () << std::endl;
175  }
176  }
177  return false;
178 }
int i
Definition: DBlmapReader.cc:9
std::string getDataToken(const std::string &fIov, unsigned long fRun)
bool getObject(HcalPedestals *fObject, const std::string &fTag, unsigned long fRun)
oracle::occi::SQLException SQLException
Definition: HcalDbOmds.cc:27
tuple result
Definition: mps_fire.py:83
oracle::occi::Connection * mConnect
bool getObjectGeneric(T *fObject, S *fCondObject, const std::string &fTag, unsigned long fRun)
oracle::occi::Environment * mEnvironment
std::string getMetadataToken(const std::string &fTag)
string host
Definition: query.py:114
Definition: DetId.h:18
#define debug
Definition: HDRShower.cc:19
oracle::occi::Statement * mStatement
double S(const TLorentzVector &, const TLorentzVector &)
Definition: Particle.cc:99
oracle::occi::ResultSet ResultSet
Definition: HcalDbOmds.cc:26
tuple cout
Definition: gather_cfg.py:145
long double T
HcalDbPoolOCCI(const std::string &fDb)