Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <iostream>
0002 #include <iomanip>
0003 #include <string>
0004 #include <vector>
0005 #include <time.h>
0006 
0007 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0008 #include "OnlineDB/EcalCondDB/interface/RunTag.h"
0009 #include "OnlineDB/EcalCondDB/interface/RunIOV.h"
0010 #include "OnlineDB/EcalCondDB/interface/RunCrystalErrorsDat.h"
0011 #include "OnlineDB/EcalCondDB/interface/RunTTErrorsDat.h"
0012 #include "OnlineDB/EcalCondDB/interface/RunPNErrorsDat.h"
0013 #include "OnlineDB/EcalCondDB/interface/RunMemChErrorsDat.h"
0014 #include "OnlineDB/EcalCondDB/interface/RunMemTTErrorsDat.h"
0015 #include "CondTools/Ecal/interface/EcalErrorDictionary.h"
0016 
0017 
0018 using namespace std;
0019 
0020 class CondDBApp {
0021 public:
0022 
0023   /**
0024    *   App constructor; Makes the database connection
0025    */
0026   CondDBApp(string host, string sid, string user, string pass)
0027   {
0028     try {
0029       cout << "Making connection..." << flush;
0030       econn = new EcalCondDBInterface(  sid, user, pass );
0031       cout << "Done." << endl;
0032     } catch (runtime_error &e) {
0033       cerr << e.what() << endl;
0034       exit(-1);
0035     }
0036 
0037     locations[0] = "H4";
0038     locations[1] = "867-1";
0039     locations[2] = "867-2";
0040     locations[3] = "867-3";
0041   }
0042 
0043 
0044 
0045   /**
0046    *  App destructor;  Cleans up database connection
0047    */
0048   ~CondDBApp() 
0049   {
0050     delete econn;
0051   }
0052 
0053 
0054   RunIOV makeRunIOV()
0055   {
0056     // The objects necessary to identify a dataset
0057     LocationDef locdef;
0058     RunTypeDef rundef;
0059     RunTag runtag;
0060 
0061     locdef.setLocation(locations[3]);
0062 
0063     rundef.setRunType("TEST");
0064     
0065     runtag.setLocationDef(locdef);
0066     runtag.setRunTypeDef(rundef);
0067 
0068     // Our beginning time will be the current GMT time
0069     // This is the time zone that should be written to the DB!
0070     // (Actually UTC)
0071     Tm startTm;
0072     startTm.setToCurrentGMTime();
0073 
0074     // Our beginning run number will be the seconds representation
0075     // of that time, and we will increment our IoV based on 
0076     // a microsecond representation
0077     uint64_t microseconds = startTm.microsTime();
0078     startmicros = microseconds;
0079     run_t run = (int)(microseconds/1000000);
0080     startrun = run;
0081 
0082     cout << "Starting Time:    " << startTm.str() << endl;
0083     cout << "Starting Micros:  " << startmicros << endl;
0084     cout << "Starting Run:     " << startrun << endl;
0085 
0086     // Set the properties of the iov
0087     RunIOV runiov;
0088 
0089     startTm.setToMicrosTime(microseconds);
0090     cout << "Setting run " << run << " run_start " << startTm.str() << endl;
0091     runiov.setRunNumber(run);
0092     runiov.setRunStart(startTm);
0093     runiov.setRunTag(runtag);
0094     
0095     return runiov;
0096   }
0097 
0098 
0099 
0100   void testWrite()
0101   {
0102     cout << "Writing RunCrystalErrorsDat object to database..." << endl;
0103     RunIOV runiov = this->makeRunIOV();
0104     RunTag runtag = runiov.getRunTag();
0105     run_t run = runiov.getRunNumber();
0106 
0107     // write to the DB
0108     cout << "Inserting run..." << flush;
0109     econn->insertRunIOV(&runiov);
0110     cout << "Done." << endl;
0111     printIOV(&runiov);
0112 
0113     // fetch it back
0114     cout << "Fetching run by tag just used..." << flush;
0115     RunIOV runiov_prime = econn->fetchRunIOV(&runtag, run);
0116     cout << "Done." << endl;
0117     printIOV(&runiov_prime);
0118     cout << "Fetching run by location..." << flush;
0119     RunIOV runiov_prime2 = econn->fetchRunIOV(runtag.getLocationDef().getLocation(), run);
0120     cout << "Done." << endl;
0121     printIOV(&runiov_prime2);    
0122 
0123     // Get channel ID for SM 10, crystal c      
0124     int c = 1;
0125     EcalLogicID ecid;
0126     ecid = econn->getEcalLogicID("EB_crystal_number", 10, c);
0127 
0128     // Set the data
0129     RunCrystalErrorsDat cryerr;
0130     map<EcalLogicID, RunCrystalErrorsDat> dataset;
0131 
0132     uint64_t bits = 0;
0133     bits = 0;
0134     bits |= EcalErrorDictionary::getMask("PEDESTAL_LOW_GAIN_MEAN_ERROR");
0135     bits |= EcalErrorDictionary::getMask("CH_ID_ERROR");
0136     bits |= EcalErrorDictionary::getMask("LASER_MEAN_WARNING");
0137     cryerr.setErrorBits(bits);
0138 
0139     // Fill the dataset
0140     dataset[ecid] = cryerr;
0141     
0142     // Insert the dataset, identifying by iov
0143     cout << "Inserting dataset..." << flush;
0144     econn->insertDataSet(&dataset, &runiov);
0145     cout << "Done." << endl;
0146 
0147     // Fetch it back
0148     cout << "Fetching dataset..." << flush;
0149     dataset.clear();
0150     econn->fetchDataSet(&dataset, &runiov);
0151     cout << "retrieved " << dataset.size() << " channel-value pairs" << endl;
0152     printDataSet(&dataset);
0153   };
0154 
0155 
0156 
0157   void testLookup()
0158   {
0159     cout << "Testing Lookup of RunCrystalErrorsDat" << endl;
0160     RunIOV runiov = this->makeRunIOV();
0161     RunTag runtag = runiov.getRunTag();
0162     runtag.setGeneralTag("testLookup " + runiov.getRunStart().str() );  // XXX hack for testing
0163     runiov.setRunTag(runtag);
0164 
0165     run_t run = runiov.getRunNumber();
0166     uint64_t micros = runiov.getRunStart().microsTime();
0167 
0168     // Get channel ID for SM 10, crystal c      
0169     int c = 1;
0170     EcalLogicID ecid;
0171     ecid = econn->getEcalLogicID("EB_crystal_number", 10, c);
0172     
0173     // Prepare data objects
0174     RunCrystalErrorsDat cryerr;
0175     map<EcalLogicID, RunCrystalErrorsDat> dataset;
0176     srand(time(0));
0177 
0178     // Write 12 runs to the DB, and write data every 4
0179     for (int i = 0;  i < 12; i++) {
0180       cout << "i:  " << i << "  Inserting run " << run << "..." << flush;
0181       econn->insertRunIOV(&runiov);
0182 
0183       if (i%4 == 0) {
0184     // Fill the dataset
0185     cryerr.setErrorBits((uint64_t)rand() * (uint64_t)rand());
0186     dataset[ecid] = cryerr;
0187     
0188     // Insert the dataset, identifying by iov
0189     cout << "Inserting dataset..." << flush;
0190     econn->insertDataSet(&dataset, &runiov);
0191       }
0192       cout << "Done." << endl;
0193 
0194       // Increment RunIOV
0195       run++;
0196       micros += 100000 * 60 * 60;  // 1 hour
0197       runiov.setRunNumber(run);
0198       runiov.setRunStart(Tm(micros));
0199     }
0200 
0201     // Now pretend we don't know what runs data was inserted for
0202     // Lookup data based on some runs (every 3 in this case)
0203     RunIOV validIOV;
0204     run_t somerun = startrun;
0205 
0206     for (int i = 0; i < 4; i++) {
0207       cout << "Fetching dataset for run: " << somerun << "..." << endl;
0208       econn->fetchValidDataSet(&dataset, &validIOV, &runtag, somerun);
0209       cout << "Attached to run: " << validIOV.getRunNumber() << endl;
0210       printDataSet(&dataset);
0211       somerun += 3;
0212     }
0213   }
0214 
0215   /**
0216    *  Write a data set
0217    */
0218   template<class DATT, class IOVT>
0219   void testTable(DATT* data, IOVT* iov, EcalLogicID* ecid)
0220   {
0221     tablesTried++;
0222     try {
0223       cout << "Table " << tablesTried << "..." << flush;
0224       map<EcalLogicID, DATT> dataset;
0225       dataset[*ecid] = *data;
0226       econn->insertDataSet(&dataset, iov);
0227       cout << "write OK..." << flush;
0228       dataset.clear();
0229       econn->fetchDataSet(&dataset, iov);
0230       if (!dataset.size()) {
0231     throw(runtime_error("Zero rows read back"));
0232       }
0233       cout << "read OK" << endl;
0234       tablesOK++;
0235     } catch (runtime_error &e) {
0236       cout << "testTable FAILED:  " << e.what() << endl;
0237     } catch (...) {
0238       cout << "testTable FAILED:  unknown exception" << endl;
0239     }
0240   }
0241 
0242 
0243 
0244   /**
0245    *  Write to each of the data tables
0246    */
0247   void testAllTables()
0248   {
0249     cout << "Testing writing to all tables..." << endl;
0250     tablesTried = 0;
0251     tablesOK = 0;
0252     
0253     // get a dummy logic ID
0254     EcalLogicID logicID = econn->getEcalLogicID(-1);
0255 
0256     // RunIOV tables
0257     RunIOV runiov = this->makeRunIOV();
0258 
0259     // tables using RunIOV
0260     RunCrystalErrorsDat table01;
0261     testTable(&table01, &runiov, &logicID);
0262 
0263     RunTTErrorsDat table02;
0264     testTable(&table02, &runiov, &logicID);
0265 
0266     RunPNErrorsDat table03;
0267     testTable(&table03, &runiov, &logicID);
0268 
0269     RunMemChErrorsDat table04;
0270     testTable(&table04, &runiov, &logicID);
0271 
0272     RunMemTTErrorsDat table05;
0273     testTable(&table05, &runiov, &logicID);
0274 
0275   };
0276 
0277 
0278 
0279 private:
0280   CondDBApp();  // hidden default constructor
0281   EcalCondDBInterface* econn;
0282   string locations[4];
0283   uint64_t startmicros;
0284   uint64_t endmicros;
0285   run_t startrun;
0286   run_t endrun;
0287 
0288   int tablesTried;
0289   int tablesOK;
0290 
0291   /**
0292    *   Iterate through the dataset and print some data
0293    */
0294   void printDataSet( const map<EcalLogicID, RunCrystalErrorsDat>* dataset, int limit = 0 ) const
0295   {
0296     cout << "==========printDataSet()" << endl;
0297     if (dataset->size() == 0) {
0298       cout << "No data in map!" << endl;
0299     }
0300     EcalLogicID ecid;
0301     RunCrystalErrorsDat cryerr;
0302 
0303     int count = 0;
0304     typedef map< EcalLogicID, RunCrystalErrorsDat >::const_iterator CI;
0305     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0306       count++;
0307       if (limit && count > limit) { return; }
0308       ecid = p->first;
0309       cryerr  = p->second;
0310      
0311       cout << "SM:                     " << ecid.getID1() << endl;
0312       cout << "Xtal:                   " << ecid.getID2() << endl;
0313       cout << "Error Bits:             " 
0314        << "0x" << hex << setfill('0') << setw(16) << cryerr.getErrorBits() 
0315        << " (" << dec << setfill(' ') << setw(20) << cryerr.getErrorBits() << ")" << endl;
0316       cout << "Errors:" << endl;
0317       EcalErrorDictionary::printErrors(cryerr.getErrorBits());
0318       cout << "========================" << endl;
0319     }
0320     cout << endl;
0321   }
0322 
0323 
0324   /**
0325    *   Print out a RunTag
0326    */
0327   void printTag( const RunTag* tag) const
0328   {
0329     cout << endl;
0330     cout << "=============RunTag:" << endl;
0331     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0332     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0333     cout << "Run Type:           " << tag->getRunTypeDef().getRunType() << endl;
0334     cout << "====================" << endl;
0335   }
0336 
0337   void printIOV( const RunIOV* iov) const
0338   {
0339     cout << endl;
0340     cout << "=============RunIOV:" << endl;
0341     RunTag tag = iov->getRunTag();
0342     printTag(&tag);
0343     cout << "Run Number:         " << iov->getRunNumber() << endl;
0344     cout << "Run Start:          " << iov->getRunStart().str() << endl;
0345     cout << "Run End:            " << iov->getRunEnd().str() << endl;
0346     cout << "====================" << endl;
0347   }
0348 };
0349 
0350 
0351 
0352 int main (int argc, char* argv[])
0353 {
0354   string host;
0355   string sid;
0356   string user;
0357   string pass;
0358 
0359   if (argc != 5) {
0360     cout << "Usage:" << endl;
0361     cout << "  " << argv[0] << " <host> <SID> <user> <pass>" << endl;
0362     exit(-1);
0363   }
0364 
0365   host = argv[1];
0366   sid = argv[2];
0367   user = argv[3];
0368   pass = argv[4];
0369 
0370   try {
0371     CondDBApp app(host, sid, user, pass);
0372 
0373     app.testWrite();
0374     app.testAllTables();
0375     app.testLookup();
0376 
0377   } catch (exception &e) {
0378     cout << "ERROR:  " << e.what() << endl;
0379   } catch (...) {
0380     cout << "Unknown error caught" << endl;
0381   }
0382 
0383   cout << "All Done." << endl;
0384 
0385   return 0;
0386 }