File indexing completed on 2025-01-18 10:17:32
0001
0002
0003
0004
0005 #pragma once
0006
0007
0008
0009
0010
0011 #include <JANA/Calibrations/JCalibration.h>
0012 #include <JANA/JLogger.h>
0013 #include <CCDB/Calibration.h>
0014
0015 #include <exception>
0016 #include <string>
0017 #include <vector>
0018 #include <map>
0019 #include <iostream>
0020
0021
0022 using namespace std;
0023
0024
0025
0026
0027
0028 class JCalibrationCCDB : public JCalibration {
0029 public:
0030
0031
0032
0033
0034
0035
0036
0037 JCalibrationCCDB(ccdb::Calibration* calib, string url, int32_t run, string context = "default") :
0038 JCalibration(calib->GetConnectionString(), run, context) {
0039
0040 mCalibration = calib;
0041 pthread_mutex_init(&mutex, NULL);
0042
0043 #ifdef CCDB_DEBUG_OUTPUT
0044 jout<<"CCDB::janaccdb created JCalibrationCCDB with connection string:" << calib->GetConnectionString()<< " run:"<<run<< " context:"<<context<<endl;
0045 #endif
0046 }
0047
0048
0049
0050
0051 virtual ~JCalibrationCCDB() {
0052 if (mCalibration != NULL) {
0053 pthread_mutex_lock(&mutex);
0054 delete mCalibration;
0055 pthread_mutex_unlock(&mutex);
0056 }
0057 }
0058
0059
0060
0061
0062 virtual const char* className(void) {
0063 return static_className();
0064 }
0065
0066
0067
0068
0069 static const char* static_className(void) {
0070 return "JCalibrationCCDB";
0071 }
0072
0073
0074
0075 ccdb::Calibration* GetCCDBCalibObj(void) {
0076 return mCalibration;
0077 }
0078
0079
0080
0081 void Lock(void) {
0082 pthread_mutex_lock(&mutex);
0083 }
0084
0085
0086
0087 void Unlock(void) {
0088 pthread_mutex_unlock(&mutex);
0089 }
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099 bool GetCalib(string namepath, map<string, string>& svals, uint64_t event_number = 0) {
0100
0101 pthread_mutex_lock(&mutex);
0102
0103
0104 try {
0105
0106 #ifdef CCDB_DEBUG_OUTPUT
0107 cout<<"CCDB::janaccdb"<<endl;
0108 cout<<"CCDB::janaccdb REQUEST map<string, string> request = '"<<namepath<<"'"<<endl;
0109 #endif
0110
0111 bool result = mCalibration->GetCalib(svals, namepath);
0112
0113
0114 #ifdef CCDB_DEBUG_OUTPUT
0115 string result_str((result)?string("loaded"):string("failure"));
0116 if(result)
0117 {
0118 string first_value(" --NAN-- ");
0119 if(svals.size()>0)
0120 {
0121 map<string, string>::const_iterator iter = svals.begin();
0122 first_value.assign(iter->second);
0123 }
0124 cout<<"CCDB::janaccdb selected name-values count = '"<<svals.size()<<"' first_value '"<<first_value<<"'"<<endl;
0125 }
0126 #endif
0127
0128 pthread_mutex_unlock(&mutex);
0129 return !result;
0130 }
0131 catch (std::exception& ex) {
0132
0133 #ifdef CCDB_DEBUG_OUTPUT
0134 cout <<"CCDB::janaccdb Exception caught at GetCalib(string namepath, map<string, string> &svals, int event_number=0)"<<endl;
0135 cout <<"CCDB::janaccdb what = "<<ex.what()<<endl;
0136 #endif
0137
0138 pthread_mutex_unlock(&mutex);
0139 return true;
0140 }
0141 }
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151 bool GetCalib(string namepath, vector<string>& svals, uint64_t event_number = 0) {
0152
0153
0154
0155
0156
0157
0158
0159
0160
0161
0162
0163
0164
0165 pthread_mutex_lock(&mutex);
0166
0167 try {
0168
0169 #ifdef CCDB_DEBUG_OUTPUT
0170 cout<<"CCDB::janaccdb"<<endl;
0171 cout<<"CCDB::janaccdb REQUEST vector<string> request = '"<<namepath<<"'"<<endl;
0172 #endif
0173
0174 vector<vector<string> > ssvals;
0175 bool result = mCalibration->GetCalib(ssvals, namepath);
0176
0177 if (ssvals.size() == 1) {
0178
0179
0180 svals = ssvals[0];
0181
0182 } else {
0183
0184
0185
0186 for (uint32_t i = 0; i < ssvals.size(); i++) {
0187 if (ssvals[i].size() > 0) svals.push_back(ssvals[i][0]);
0188 }
0189 }
0190
0191
0192 #ifdef CCDB_DEBUG_OUTPUT
0193 string result_str((result)?string("loaded"):string("failure"));
0194 if(result)
0195 {
0196 string first_value(" --NAN-- ");
0197 if(svals.size()>0)
0198 {
0199 vector<string>::const_iterator iter = svals.begin();
0200 first_value.assign(*iter);
0201 }
0202 cout<<"CCDB::janaccdb selected name-values count = '"<<svals.size()<<"' first_value '"<<first_value<<"'"<<endl;
0203 }
0204 #endif
0205
0206 pthread_mutex_unlock(&mutex);
0207 return !result;
0208 }
0209 catch (std::exception& ex) {
0210
0211 #ifdef CCDB_DEBUG_OUTPUT
0212 cout <<"CCDB::janaccdb Exception caught at GetCalib(string namepath, map<string, string> &svals, int event_number=0)"<<endl;
0213 cout <<"CCDB::janaccdb what = "<<ex.what()<<endl;
0214 #endif
0215
0216 pthread_mutex_unlock(&mutex);
0217 return true;
0218 }
0219 }
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229 bool GetCalib(string namepath, vector<map<string, string> >& vsvals, uint64_t event_number = 0) {
0230
0231 pthread_mutex_lock(&mutex);
0232
0233
0234 try {
0235
0236 #ifdef CCDB_DEBUG_OUTPUT
0237 cout<<"CCDB::janaccdb"<<endl;
0238 cout<<"CCDB::janaccdb REQUEST vector<map<string, string>> request = '"<<namepath<<"'"<<endl;
0239 #endif
0240
0241 bool result = mCalibration->GetCalib(vsvals, namepath);
0242
0243
0244 #ifdef CCDB_DEBUG_OUTPUT
0245 cout<<"CCDB::janaccdb result = "<<string ((result)?string("loaded"):string("failure"))<<endl;
0246 if(result)
0247 {
0248 string first_value(" --NAN-- ");
0249 if(vsvals.size()>0 && vsvals[0].size()>0)
0250 {
0251 map<string, string>::const_iterator iter = vsvals[0].begin();
0252 first_value.assign(iter->second);
0253 }
0254
0255 cout<<"CCDB::janaccdb selected rows = '"<<vsvals.size() <<"' selected columns = '"<<(int)((vsvals.size()>0)? vsvals[0].size() :0)
0256 <<"' first value = '"<<first_value<<"'"<<endl;
0257 }
0258 #endif
0259
0260
0261 pthread_mutex_unlock(&mutex);
0262 return !result;
0263 }
0264 catch (std::exception& ex) {
0265
0266 #ifdef CCDB_DEBUG_OUTPUT
0267 cout <<"CCDB::janaccdb Exception caught at GetCalib(string namepath, map<string, string> &svals, int event_number=0)"<<endl;
0268 cout <<"CCDB::janaccdb what = "<<ex.what()<<endl;
0269 #endif
0270
0271 pthread_mutex_unlock(&mutex);
0272 return true;
0273 }
0274 }
0275
0276
0277
0278
0279
0280
0281
0282
0283
0284 bool GetCalib(string namepath, vector<vector<string> >& vsvals, uint64_t event_number = 0) {
0285
0286 pthread_mutex_lock(&mutex);
0287
0288
0289 try {
0290
0291 #ifdef CCDB_DEBUG_OUTPUT
0292 cout<<"CCDB::janaccdb"<<endl;
0293 cout<<"CCDB::janaccdb REQUEST vector<vector<string> > request = '"<<namepath<<"'"<<endl;
0294 #endif
0295
0296 bool result = mCalibration->GetCalib(vsvals, namepath);
0297
0298
0299 #ifdef CCDB_DEBUG_OUTPUT
0300 cout<<"CCDB::janaccdb result = "<<string ((result)?string("loaded"):string("failure"))<<endl;
0301 if(result)
0302 {
0303 string first_value(" --NAN-- ");
0304 if(vsvals.size()>0 && vsvals[0].size()>0)
0305 {
0306 vector<string>::const_iterator iter = vsvals[0].begin();
0307 first_value.assign(*iter);
0308 }
0309
0310 cout<<"CCDB::janaccdb selected rows = '"<<vsvals.size() <<"' selected columns = '"<<(int)((vsvals.size()>0)? vsvals[0].size() :0)
0311 <<"' first value = '"<<first_value<<"'"<<endl;
0312 }
0313 #endif
0314
0315 pthread_mutex_unlock(&mutex);
0316 return !result;
0317 }
0318 catch (std::exception& ex) {
0319
0320 #ifdef CCDB_DEBUG_OUTPUT
0321 cout <<"CCDB::janaccdb Exception caught at GetCalib(string namepath, map<string, string> &svals, int event_number=0)"<<endl;
0322 cout <<"CCDB::janaccdb what = "<<ex.what()<<endl;
0323 #endif
0324
0325 pthread_mutex_unlock(&mutex);
0326 return true;
0327 }
0328 }
0329
0330
0331
0332
0333
0334
0335
0336 void GetListOfNamepaths(vector<string>& namepaths) {
0337
0338 pthread_mutex_lock(&mutex);
0339
0340 try {
0341
0342 #ifdef CCDB_DEBUG_OUTPUT
0343 cout<<"CCDB::janaccdb Getting list of namepaths. "<<endl;
0344 #endif
0345
0346 mCalibration->GetListOfNamepaths(namepaths);
0347 }
0348 catch (std::exception& ex) {
0349
0350
0351 #ifdef CCDB_DEBUG_OUTPUT
0352 cout<<"CCDB::janaccdb Exception cought at GetListOfNamepaths(vector<string> &namepaths). What = "<< ex.what()<<endl;
0353 #endif
0354 }
0355 pthread_mutex_unlock(&mutex);
0356 }
0357
0358 private:
0359 JCalibrationCCDB();
0360 ccdb::Calibration* mCalibration;
0361 pthread_mutex_t mutex;
0362
0363 };
0364
0365