Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:17:32

0001 
0002 // Copyright 2020-2025, Jefferson Science Associates, LLC.
0003 // Subject to the terms in the LICENSE file found in the top-level directory.
0004 
0005 #pragma once
0006 
0007 // This entire file was copied from the CCDB 0.06 source
0008 // (janaccdb directory). It has been modified based on
0009 // changes that were later made to DCalibrationCCDB.h in
0010 // the sim-recon code.
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  *  Descendant of JCalibration class which allow to use CCDB as JANA calibration source
0027  */
0028 class JCalibrationCCDB : public JCalibration {
0029 public:
0030 
0031     /** @brief    Constructor
0032      *
0033      * @parameter [in] url - connection string. like mysql://...
0034      * @parameter [in] run - run number
0035      * @parameter [in] context - variation
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     /** @brief   destructor
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     /** @brief gets a className
0061      */
0062     virtual const char* className(void) {
0063         return static_className();
0064     }
0065 
0066 
0067     /** @brief gets a className static version of function
0068      */
0069     static const char* static_className(void) {
0070         return "JCalibrationCCDB";
0071     }
0072 
0073     /** @brief gets pointer to underlying ccdb::Calibration object
0074      */
0075     ccdb::Calibration* GetCCDBCalibObj(void) {
0076         return mCalibration;
0077     }
0078 
0079     /** @brief lock mutex used when accessing CCDB
0080      */
0081     void Lock(void) {
0082         pthread_mutex_lock(&mutex);
0083     }
0084 
0085     /** @brief unlock mutex used when accessing CCDB
0086      */
0087     void Unlock(void) {
0088         pthread_mutex_unlock(&mutex);
0089     }
0090 
0091 
0092     /** @brief    get calibration constants
0093      *
0094      * @parameter [in]  namepath - full resource string
0095      * @parameter [out] svals - data to be returned
0096      * @parameter [in]  event_number - optional parameter of event number
0097      * @return true if constants were read
0098      */
0099     bool GetCalib(string namepath, map<string, string>& svals, uint64_t event_number = 0) {
0100         // Lock mutex for exclusive use of underlying Calibration object
0101         pthread_mutex_lock(&mutex);
0102 
0103         //
0104         try {
0105             //>oO CCDB debug output
0106 #ifdef CCDB_DEBUG_OUTPUT
0107             cout<<"CCDB::janaccdb"<<endl;
0108             cout<<"CCDB::janaccdb REQUEST map<string, string> request = '"<<namepath<<"'"<<endl;
0109 #endif  //>end of  CCDB debug output
0110 
0111             bool result = mCalibration->GetCalib(svals, namepath);
0112 
0113             //>oO CCDB debug output
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  //>end of  CCDB debug output
0127 
0128             pthread_mutex_unlock(&mutex);
0129             return !result; //JANA has false - if success and true if error
0130         }
0131         catch (std::exception& ex) {
0132             //>oO CCDB debug output
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 //end of CCDB debug output
0137 
0138             pthread_mutex_unlock(&mutex);
0139             return true; //JANA has false - if success and true if error
0140         }
0141     }
0142 
0143 
0144     /** @brief    get calibration constants
0145     *
0146     * @parameter [in]  namepath - full resource string
0147     * @parameter [out] svals - data to be returned
0148     * @parameter [in]  event_number - optional parameter of event number
0149     * @return true if constants were read
0150     */
0151     bool GetCalib(string namepath, vector<string>& svals, uint64_t event_number = 0) {
0152         /// The CCDB method corresponding to this one treats constants stored as
0153         /// a single column but many rows as an error and throws an exception if
0154         /// it is detected. The message in the exception suggests using a
0155         /// vector<vector<T> > instead. Thus, we do that here and convert if
0156         /// necessary into a 1-D vector.
0157         ///
0158         /// It is worth noting that back in May 2014 a similar issue was
0159         /// addressed in the CCDB code itself when the method filling a
0160         /// map<string, string> was used. Fixing this one in CCDB also will
0161         /// require coordinated releases of CCDB and JANA. We choose to
0162         /// handle it here in order to avoid that.
0163 
0164         // Lock mutex for exclusive use of underlying Calibration object
0165         pthread_mutex_lock(&mutex);
0166 
0167         try {
0168             //>oO CCDB debug output
0169 #ifdef CCDB_DEBUG_OUTPUT
0170             cout<<"CCDB::janaccdb"<<endl;
0171             cout<<"CCDB::janaccdb REQUEST vector<string> request = '"<<namepath<<"'"<<endl;
0172 #endif  //>end of  CCDB debug output
0173 
0174             vector<vector<string> > ssvals;
0175             bool result = mCalibration->GetCalib(ssvals, namepath);
0176 
0177             if (ssvals.size() == 1) {
0178                 // ---- COLUMN-WISE ----
0179 
0180                 svals = ssvals[0];
0181 
0182             } else {
0183                 // ---- ROW-WISE ----
0184 
0185                 // add first element of each row (should only be one!)
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             //>oO CCDB debug output
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  //>end of  CCDB debug output
0205 
0206             pthread_mutex_unlock(&mutex);
0207             return !result; //JANA has false - if success and true if error
0208         }
0209         catch (std::exception& ex) {
0210             //>oO CCDB debug output
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 //end of CCDB debug output
0215 
0216             pthread_mutex_unlock(&mutex);
0217             return true; //JANA has false - if success and true if error
0218         }
0219     }
0220 
0221 
0222     /** @brief    get calibration constants
0223      *
0224      * @parameter [in]  namepath - full resource string
0225      * @parameter [out] vsvals - data to be returned
0226      * @parameter [in]  event_number - optional parameter of event number
0227      * @return true if constants were read
0228      */
0229     bool GetCalib(string namepath, vector<map<string, string> >& vsvals, uint64_t event_number = 0) {
0230         // Lock mutex for exclusive use of underlying Calibration object
0231         pthread_mutex_lock(&mutex);
0232 
0233         //
0234         try {
0235             //>oO CCDB debug output
0236 #ifdef CCDB_DEBUG_OUTPUT
0237             cout<<"CCDB::janaccdb"<<endl;
0238             cout<<"CCDB::janaccdb REQUEST vector<map<string, string>> request = '"<<namepath<<"'"<<endl;
0239 #endif  //end of CCDB debug output
0240 
0241             bool result = mCalibration->GetCalib(vsvals, namepath);
0242 
0243             //>oO CCDB debug output
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  //end of CCDB debug output
0259 
0260 
0261             pthread_mutex_unlock(&mutex);
0262             return !result; //JANA has false - if success and true if error, CCDB otherwise
0263         }
0264         catch (std::exception& ex) {
0265             //>oO CCDB debug output
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; //JANA has false - if success and true if error, CCDB otherwise
0273         }
0274     }
0275 
0276 
0277     /** @brief    get calibration constants
0278     *
0279     * @parameter [in]  namepath - full resource string
0280     * @parameter [out] vsvals - data to be returned
0281     * @parameter [in]  event_number - optional parameter of event number
0282     * @return true if constants were read
0283     */
0284     bool GetCalib(string namepath, vector<vector<string> >& vsvals, uint64_t event_number = 0) {
0285         // Lock mutex for exclusive use of underlying Calibration object
0286         pthread_mutex_lock(&mutex);
0287 
0288         //
0289         try {
0290             //>oO CCDB debug output
0291 #ifdef CCDB_DEBUG_OUTPUT
0292             cout<<"CCDB::janaccdb"<<endl;
0293             cout<<"CCDB::janaccdb REQUEST vector<vector<string> > request = '"<<namepath<<"'"<<endl;
0294 #endif  //end of CCDB debug output
0295 
0296             bool result = mCalibration->GetCalib(vsvals, namepath);
0297 
0298             //>oO CCDB debug output
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  //end of CCDB debug output
0314 
0315             pthread_mutex_unlock(&mutex);
0316             return !result; //JANA has false - if success and true if error, CCDB otherwise
0317         }
0318         catch (std::exception& ex) {
0319             //>oO CCDB debug output
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; //JANA has false - if success and true if error, CCDB otherwise
0327         }
0328     }
0329 
0330 
0331     /** @brief    GetListOfNamepaths
0332       *
0333       * @parameter [in] vector<string> & namepaths
0334       * @return   void
0335       */
0336     void GetListOfNamepaths(vector<string>& namepaths) {
0337         // Lock mutex for exclusive use of underlying Calibration object
0338         pthread_mutex_lock(&mutex);
0339 
0340         try {
0341             //some ccdb debug output
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             //some ccdb debug output
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();                    // prevent use of default constructor
0360     ccdb::Calibration* mCalibration;    ///Underlaying CCDB user api class
0361     pthread_mutex_t mutex;
0362 
0363 };
0364 
0365