Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:23:14

0001 #include <iostream>
0002 #include <string>
0003 #include <vector>
0004 #include <time.h>
0005 #include <cstdlib>
0006 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0007 #include "OnlineDB/EcalCondDB/interface/all_cali_types.h"
0008 
0009 
0010 using namespace std;
0011 
0012 class CondDBApp {
0013 public:
0014 
0015   /**
0016    *   App constructor; Makes the database connection
0017    */
0018   CondDBApp(string sid, string user, string pass)
0019   {
0020     try {
0021       cout << "Making connection..." << flush;
0022       econn = new EcalCondDBInterface( sid, user, pass );
0023       cout << "Done." << endl;
0024     } catch (runtime_error &e) {
0025       cerr << e.what() << endl;
0026       exit(-1);
0027     }
0028 
0029     
0030   }
0031 
0032 
0033 
0034   /**
0035    *  App destructor;  Cleans up database connection
0036    */
0037   ~CondDBApp() 
0038   {
0039     delete econn;
0040   }
0041 
0042 
0043 
0044   CaliIOV makeCaliIOV()
0045   {
0046     LocationDef locdef;
0047     locdef.setLocation("LAB");
0048     CaliTag calitag;
0049     calitag.setLocationDef(locdef);
0050 
0051 
0052     // Our beginning time will be the current GMT time
0053     // This is the time zone that should be written to the DB!
0054     // (Actually UTC)
0055     Tm since;
0056     since.setToCurrentGMTime();
0057 
0058     // Our beginning run number will be the seconds representation
0059     // of that time, and we will increment our IoV based on 
0060     // a microsecond representation
0061     uint64_t microseconds = since.microsTime();
0062     startmicros = microseconds;
0063 
0064     // Set the properties of the iov
0065     CaliIOV caliiov;
0066 
0067     caliiov.setSince(since);
0068     caliiov.setCaliTag(calitag);
0069     
0070     return caliiov;
0071   }
0072 
0073 
0074 
0075   /**
0076    *  Write objects with associated CaliIOVs
0077    *  IOVs are written using automatic updating of the 'till', as if
0078    *  the time of the end of the run was not known at the time of writing.
0079    */
0080   void testWrite()
0081   {
0082     cout << "Writing CaliCrystalIntercalDat objects to database..." << endl;
0083     cout << "Making a CaliIOV..." << flush;
0084     CaliIOV caliiov = this->makeCaliIOV();
0085     cout << "Done." << endl;
0086 
0087     this->printIOV(&caliiov);
0088 
0089     Tm eventTm = caliiov.getSince();
0090     CaliTag calitag = caliiov.getCaliTag();
0091 
0092     // Get channel ID for SM 10, crystal c      
0093     int c = 1;
0094     EcalLogicID ecid;
0095     ecid = econn->getEcalLogicID("EB_crystal_number", 10, c);
0096 
0097     // Set the data
0098     CaliCrystalIntercalDat crystalCal;;
0099     map<EcalLogicID, CaliCrystalIntercalDat> dataset;
0100 
0101     int i = 1;
0102     float val = 0.11111 + i;
0103     crystalCal.setCali(val);
0104     crystalCal.setCaliRMS(val);
0105     crystalCal.setNumEvents(i);
0106     crystalCal.setTaskStatus(true);
0107 
0108     // Fill the dataset
0109     dataset[ecid] = crystalCal;
0110     
0111     // Insert the dataset, identifying by iov
0112     cout << "Inserting dataset..." << flush;
0113     econn->insertDataSet(&dataset, &caliiov);
0114     cout << "Done." << endl;
0115 
0116     // Fetch it back
0117     cout << "Fetching dataset..." << flush;
0118     dataset.clear();
0119     econn->fetchDataSet(&dataset, &caliiov);
0120     cout << "retrieved " << dataset.size() << " channel-value pairs" << endl;
0121     printDataSet(&dataset);
0122 
0123     // Fetch back CaliIOV just written
0124     cout << "Fetching IOV just written..." << flush;
0125     CaliIOV caliiov_prime = econn->fetchCaliIOV(&calitag, eventTm);
0126     cout << "Done." << endl << endl << endl;
0127     this->printIOV(&caliiov_prime);
0128   };
0129 
0130 
0131 
0132   /**
0133    *  Write a data set
0134    */
0135   template<class DATT, class IOVT>
0136   void testTable(DATT* data, IOVT* iov, EcalLogicID* ecid)
0137   {
0138     tablesTried++;
0139     try {
0140       cout << "Table " << tablesTried << "..." << flush;
0141       map<EcalLogicID, DATT> dataset;
0142       dataset[*ecid] = *data;
0143       econn->insertDataSet(&dataset, iov);
0144       cout << "write OK..." << flush;
0145       dataset.clear();
0146       econn->fetchDataSet(&dataset, iov);
0147       if (!dataset.size()) {
0148     throw(runtime_error("Zero rows read back"));
0149       }
0150       cout << "read OK" << endl;
0151       tablesOK++;
0152     } catch (runtime_error &e) {
0153       cout << "testTable FAILED:  " << e.what() << endl;
0154     } catch (...) {
0155       cout << "testTable FAILED:  unknown exception" << endl;
0156     }
0157   }
0158 
0159 
0160 
0161   /**
0162    *  Write to each of the data tables
0163    */
0164   void testAllTables()
0165   {
0166     cout << "Testing writing to all tables..." << endl;
0167     tablesTried = 0;
0168     tablesOK = 0;
0169     
0170     // get a dummy logic ID
0171     EcalLogicID logicID = econn->getEcalLogicID(-1);
0172 
0173     // CaliIOV tables
0174     CaliIOV caliiov = this->makeCaliIOV();
0175 
0176     CaliGeneralDat table01;
0177     testTable(&table01, &caliiov, &logicID);
0178 
0179     CaliCrystalIntercalDat table02;
0180     testTable(&table02, &caliiov, &logicID);
0181 
0182     CaliHVScanRatioDat table03;
0183     testTable(&table03, &caliiov, &logicID);
0184 
0185     cout << "Test of writing to all tables complete" << endl;
0186     cout << tablesOK << " of " << tablesTried << " written to successfully" << endl << endl << endl;
0187   };
0188 
0189 
0190 
0191 private:
0192   CondDBApp();  // hidden default constructor
0193   EcalCondDBInterface* econn;
0194   uint64_t startmicros;
0195   uint64_t endmicros;
0196   run_t startrun;
0197   run_t endrun;
0198 
0199   int tablesTried;
0200   int tablesOK;
0201 
0202   /**
0203    *   Iterate through the dataset and print some data
0204    */
0205   void printDataSet( const map<EcalLogicID, CaliCrystalIntercalDat>* dataset, int limit = 0 ) const
0206   {
0207     cout << "==========printDataSet()" << endl;
0208     if (dataset->size() == 0) {
0209       cout << "No data in map!" << endl;
0210     }
0211     EcalLogicID ecid;
0212     CaliCrystalIntercalDat crystalCal;;
0213 
0214     int count = 0;
0215     typedef map< EcalLogicID, CaliCrystalIntercalDat >::const_iterator CI;
0216     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0217       count++;
0218       if (limit && count > limit) { return; }
0219       ecid = p->first;
0220       crystalCal  = p->second;
0221      
0222       cout << "SM:                     " << ecid.getID1() << endl;
0223       cout << "Xtal:                   " << ecid.getID2() << endl;
0224       cout << "calibration:            " << crystalCal.getCali() << endl;
0225       cout << "calibration rms:        " << crystalCal.getCaliRMS() << endl;
0226       cout << "num events:             " << crystalCal.getNumEvents() << endl;
0227       cout << "task status:            " << crystalCal.getTaskStatus() << endl;
0228       cout << "========================" << endl;
0229     }
0230     cout << endl;
0231   }
0232 
0233 
0234   /**
0235    *   Print out a CaliTag
0236    */
0237   void printTag( const CaliTag* tag) const
0238   {
0239     cout << endl;
0240     cout << "=============CaliTag:" << endl;
0241     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0242     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0243     cout << "Method:             " << tag->getMethod() << endl;
0244     cout << "Version:            " << tag->getVersion() << endl;
0245     cout << "Data Type:          " << tag->getDataType() << endl;
0246     cout << "====================" << endl;
0247   }
0248 
0249   void printIOV( const CaliIOV* iov) const
0250   {
0251     cout << endl;
0252     cout << "=============CaliIOV:" << endl;
0253     CaliTag tag = iov->getCaliTag();
0254     printTag(&tag);
0255     cout << "since:              " << iov->getSince().str() << endl;
0256     cout << "till:               " << iov->getTill().str() << endl;
0257     cout << "====================" << endl;
0258   }
0259 };
0260 
0261 
0262 
0263 int main (int argc, char* argv[])
0264 {
0265   string host;
0266   string sid;
0267   string user;
0268   string pass;
0269 
0270   if (argc != 5) {
0271     cout << "Usage:" << endl;
0272     cout << "  " << argv[0] << " <host> <SID> <user> <pass>" << endl;
0273     exit(-1);
0274   }
0275 
0276   host = argv[1];
0277   sid = argv[2];
0278   user = argv[3];
0279   pass = argv[4];
0280 
0281   try {
0282     CondDBApp app(sid, user, pass);
0283 
0284     app.testWrite();
0285     app.testAllTables();
0286   } catch (exception &e) {
0287     cout << "ERROR:  " << e.what() << endl;
0288   } catch (...) {
0289     cout << "Unknown error caught" << endl;
0290   }
0291 
0292   cout << "All Done." << endl;
0293 
0294   return 0;
0295 }