Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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_monitoring_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 host, 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     locations[0] = "H4";
0030     locations[1] = "867-1";
0031     locations[2] = "867-2";
0032     locations[3] = "867-3";
0033   }
0034 
0035 
0036 
0037   /**
0038    *  App destructor;  Cleans up database connection
0039    */
0040   ~CondDBApp() 
0041   {
0042     delete econn;
0043   }
0044 
0045 
0046   RunIOV makeRunIOV()
0047   {
0048     // The objects necessary to identify a dataset
0049     LocationDef locdef;
0050     RunTypeDef rundef;
0051     RunTag runtag;
0052 
0053     locdef.setLocation(locations[3]);
0054 
0055     rundef.setRunType("TEST");
0056     
0057     runtag.setLocationDef(locdef);
0058     runtag.setRunTypeDef(rundef);
0059 
0060     // Our beginning time will be the current GMT time
0061     // This is the time zone that should be written to the DB!
0062     // (Actually UTC)
0063     Tm startTm;
0064     startTm.setToCurrentGMTime();
0065 
0066     // Our beginning run number will be the seconds representation
0067     // of that time, and we will increment our IoV based on 
0068     // a microsecond representation
0069     uint64_t microseconds = startTm.microsTime();
0070     startmicros = microseconds;
0071     run_t run = (int)(microseconds/1000000);
0072     startrun = run;
0073 
0074     cout << "Starting Time:    " << startTm.str() << endl;
0075     cout << "Starting Micros:  " << startmicros << endl;
0076     cout << "Starting Run:     " << startrun << endl;
0077 
0078     // Set the properties of the iov
0079     RunIOV runiov;
0080 
0081     startTm.setToMicrosTime(microseconds);
0082     cout << "Setting run " << run << " run_start " << startTm.str() << endl;
0083     runiov.setRunNumber(run);
0084     runiov.setRunStart(startTm);
0085     runiov.setRunTag(runtag);
0086     
0087     return runiov;
0088   }
0089 
0090   
0091   MonRunIOV makeMonRunIOV(RunIOV* runiov)
0092   {
0093     // Monitoring Tag and IOV
0094     MonVersionDef monverdef;
0095     monverdef.setMonitoringVersion("test01");
0096 
0097     MonRunTag montag;
0098     montag.setMonVersionDef(monverdef);
0099 
0100     MonRunIOV moniov;
0101     moniov.setMonRunTag(montag);
0102     moniov.setRunIOV(*runiov);
0103     moniov.setSubRunNumber(0);
0104     moniov.setSubRunStart(runiov->getRunStart());
0105 
0106     return moniov;
0107   }
0108 
0109 
0110 
0111   /**
0112    *  Write MonPedestalsDat objects with associated RunTag and RunIOVs
0113    *  IOVs are written using automatic updating of the 'RunEnd', as if
0114    *  the time of the end of the run was not known at the time of writing.
0115    */
0116   void testWrite()
0117   {
0118     cout << "Writing MonPedestalsDatObjects to database..." << endl;
0119     RunIOV runiov = this->makeRunIOV();
0120     RunTag runtag = runiov.getRunTag();
0121     run_t run = runiov.getRunNumber();
0122 
0123     // write to the DB
0124     cout << "Inserting run..." << flush;
0125     econn->insertRunIOV(&runiov);
0126     cout << "Done." << endl;
0127     printIOV(&runiov);
0128 
0129 //     // write another run with the same run number to generate exception later
0130 //     runtag.setGeneralTag("different");
0131 //     runiov.setRunTag(runtag);
0132 //     econn->insertRunIOV(&runiov);
0133 
0134 
0135     // fetch it back
0136     cout << "Fetching run by tag just used..." << flush;
0137     RunIOV runiov_prime = econn->fetchRunIOV(&runtag, run);
0138     cout << "Done." << endl;
0139     printIOV(&runiov_prime);
0140     cout << "Fetching run by location..." << flush;
0141     RunIOV runiov_prime2 = econn->fetchRunIOV(runtag.getLocationDef().getLocation(), run);
0142     cout << "Done." << endl;
0143     printIOV(&runiov_prime2);    
0144 
0145     // Monitoring Tag and IOV
0146     MonRunIOV moniov = this->makeMonRunIOV(&runiov);
0147 
0148     // Get channel ID for SM 10, crystal c      
0149     int c = 1;
0150     EcalLogicID ecid;
0151     ecid = econn->getEcalLogicID("EB_crystal_number", 10, c);
0152 
0153     // Set the data
0154     MonPedestalsDat ped;
0155     map<EcalLogicID, MonPedestalsDat> dataset;
0156 
0157     int i = 1;
0158     float val = 0.11111 + i;
0159     ped.setPedMeanG1(val);
0160     ped.setPedMeanG6(val);
0161     ped.setPedMeanG12(val);
0162     ped.setPedRMSG1(val);
0163     ped.setPedRMSG6(val);
0164     ped.setPedRMSG12(val);
0165     ped.setTaskStatus(1);
0166     
0167     // Fill the dataset
0168     dataset[ecid] = ped;
0169     
0170     // Insert the dataset, identifying by iov
0171     cout << "Inserting dataset..." << flush;
0172     econn->insertDataSet(&dataset, &moniov);
0173     cout << "Done." << endl;
0174 
0175     // Fetch it back
0176     cout << "Fetching dataset..." << flush;
0177     dataset.clear();
0178     econn->fetchDataSet(&dataset, &moniov);
0179     cout << "retrieved " << dataset.size() << " channel-value pairs" << endl;
0180     printDataSet(&dataset);
0181 
0182     // Fetch back MonRunIOV just written
0183     MonRunTag montag = moniov.getMonRunTag();
0184     subrun_t subrun = moniov.getSubRunNumber();
0185     cout << "Fetching subrun..." << flush;
0186     MonRunIOV monruniov_prime = econn->fetchMonRunIOV(&runtag, &montag, run, subrun);
0187     cout << "Done." << endl << endl << endl;
0188   };
0189 
0190 
0191 
0192   /**
0193    *  Write a data set
0194    */
0195   template<class DATT, class IOVT>
0196   void testTable(DATT* data, IOVT* iov, EcalLogicID* ecid)
0197   {
0198     tablesTried++;
0199     try {
0200       cout << "Table " << tablesTried << "..." << flush;
0201       map<EcalLogicID, DATT> dataset;
0202       dataset[*ecid] = *data;
0203       econn->insertDataSet(&dataset, iov);
0204       cout << "write OK..." << flush;
0205       dataset.clear();
0206       econn->fetchDataSet(&dataset, iov);
0207       if (!dataset.size()) {
0208     throw(runtime_error("Zero rows read back"));
0209       }
0210       cout << "read OK" << endl;
0211       tablesOK++;
0212     } catch (runtime_error &e) {
0213       cout << "testTable FAILED:  " << e.what() << endl;
0214     } catch (...) {
0215       cout << "testTable FAILED:  unknown exception" << endl;
0216     }
0217   }
0218 
0219 
0220 
0221   /**
0222    *  Write to each of the data tables
0223    */
0224   void testAllTables()
0225   {
0226     cout << "Testing writing to all tables..." << endl;
0227     tablesTried = 0;
0228     tablesOK = 0;
0229     
0230     // get a dummy logic ID
0231     EcalLogicID logicID = econn->getEcalLogicID(-1);
0232 
0233     // RunIOV tables
0234     RunIOV runiov = this->makeRunIOV();
0235 
0236     // tables using RunIOV
0237     RunDat table01;
0238     testTable(&table01, &runiov, &logicID);
0239 
0240     RunConfigDat table01a;
0241     testTable(&table01a, &runiov, &logicID);
0242 
0243     RunH4TablePositionDat table01b;
0244     testTable(&table01b, &runiov, &logicID);
0245 
0246     // MonRunIOV tables
0247     MonRunIOV moniov = this->makeMonRunIOV(&runiov);
0248 
0249     MonPedestalsDat table02;
0250     testTable(&table02, &moniov, &logicID);
0251 
0252     MonRunOutcomeDef monRunOutcomeDef;
0253     monRunOutcomeDef.setShortDesc("success");
0254     MonRunDat table03;
0255     table03.setMonRunOutcomeDef(monRunOutcomeDef);
0256     testTable(&table03, &moniov, &logicID);
0257 
0258     MonTestPulseDat table04;
0259     testTable(&table04, &moniov, &logicID);
0260 
0261     MonPulseShapeDat table05;
0262     testTable(&table05, &moniov, &logicID);
0263 
0264     MonDelaysTTDat table06;
0265     testTable(&table06, &moniov, &logicID);
0266 
0267     MonShapeQualityDat table07;
0268     testTable(&table07, &moniov, &logicID);
0269 
0270     MonPedestalsOnlineDat table08;
0271     testTable(&table08, &moniov, &logicID);
0272 
0273     MonPedestalOffsetsDat table09;
0274     testTable(&table09, &moniov, &logicID);
0275 
0276     MonCrystalConsistencyDat table10;
0277     testTable(&table10, &moniov, &logicID);
0278 
0279     MonTTConsistencyDat table11;
0280     testTable(&table11, &moniov, &logicID);
0281 
0282     MonOccupancyDat table12;
0283     testTable(&table12, &moniov, &logicID);
0284 
0285     MonLaserBlueDat table14;
0286     testTable(&table14, &moniov, &logicID);
0287 
0288     MonLaserGreenDat table15;
0289     testTable(&table15, &moniov, &logicID);
0290 
0291     MonLaserRedDat table16;
0292     testTable(&table16, &moniov, &logicID);
0293 
0294     MonLaserIRedDat table17;
0295     testTable(&table17, &moniov, &logicID);
0296 
0297     MonPNBlueDat table18;
0298     testTable(&table18, &moniov, &logicID);
0299 
0300     MonPNGreenDat table19;
0301     testTable(&table19, &moniov, &logicID);
0302     
0303     MonPNRedDat table20;
0304     testTable(&table20, &moniov, &logicID);
0305     
0306     MonPNIRedDat table21;
0307     testTable(&table21, &moniov, &logicID);
0308 
0309     MonPNMGPADat table22;
0310     testTable(&table22, &moniov, &logicID);
0311 
0312     MonPNPedDat table23;
0313     testTable(&table23, &moniov, &logicID);
0314 
0315     MonMemChConsistencyDat table25;
0316     testTable(&table25, &moniov, &logicID);
0317 
0318     MonMemTTConsistencyDat table26;
0319     testTable(&table26, &moniov, &logicID);
0320 
0321     MonH4TablePositionDat table27;
0322     testTable(&table27, &moniov, &logicID);
0323 
0324     MonLaserPulseDat table29;
0325     testTable(&table29, &moniov, &logicID);
0326 
0327     cout << "Test of writing to all tables complete" << endl;
0328     cout << tablesOK << " of " << tablesTried << " written to successfully" << endl << endl << endl;
0329   };
0330 
0331 
0332 
0333 private:
0334   CondDBApp();  // hidden default constructor
0335   EcalCondDBInterface* econn;
0336   string locations[4];
0337   uint64_t startmicros;
0338   uint64_t endmicros;
0339   run_t startrun;
0340   run_t endrun;
0341 
0342   int tablesTried;
0343   int tablesOK;
0344 
0345   /**
0346    *   Iterate through the dataset and print some data
0347    */
0348   void printDataSet( const map<EcalLogicID, MonPedestalsDat>* dataset, int limit = 0 ) const
0349   {
0350     cout << "==========printDataSet()" << endl;
0351     if (dataset->size() == 0) {
0352       cout << "No data in map!" << endl;
0353     }
0354     EcalLogicID ecid;
0355     MonPedestalsDat ped;
0356 
0357     int count = 0;
0358     typedef map< EcalLogicID, MonPedestalsDat >::const_iterator CI;
0359     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0360       count++;
0361       if (limit && count > limit) { return; }
0362       ecid = p->first;
0363       ped  = p->second;
0364      
0365       cout << "SM:                     " << ecid.getID1() << endl;
0366       cout << "Xtal:                   " << ecid.getID2() << endl;
0367       cout << "Mean G1:                " << ped.getPedMeanG1() << endl;
0368       cout << "========================" << endl;
0369     }
0370     cout << endl;
0371   }
0372 
0373 
0374   /**
0375    *   Print out a RunTag
0376    */
0377   void printTag( const RunTag* tag) const
0378   {
0379     cout << endl;
0380     cout << "=============RunTag:" << endl;
0381     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0382     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0383     cout << "Run Type:           " << tag->getRunTypeDef().getRunType() << endl;
0384     cout << "====================" << endl;
0385   }
0386 
0387   void printIOV( const RunIOV* iov) const
0388   {
0389     cout << endl;
0390     cout << "=============RunIOV:" << endl;
0391     RunTag tag = iov->getRunTag();
0392     printTag(&tag);
0393     cout << "Run Number:         " << iov->getRunNumber() << endl;
0394     cout << "Run Start:          " << iov->getRunStart().str() << endl;
0395     cout << "Run End:            " << iov->getRunEnd().str() << endl;
0396     cout << "====================" << endl;
0397   }
0398 };
0399 
0400 
0401 
0402 int main (int argc, char* argv[])
0403 {
0404   string host;
0405   string sid;
0406   string user;
0407   string pass;
0408 
0409   if (argc != 5) {
0410     cout << "Usage:" << endl;
0411     cout << "  " << argv[0] << " <host> <SID> <user> <pass>" << endl;
0412     exit(-1);
0413   }
0414 
0415   host = argv[1];
0416   sid = argv[2];
0417   user = argv[3];
0418   pass = argv[4];
0419 
0420   try {
0421     CondDBApp app(host, sid, user, pass);
0422 
0423     app.testWrite();
0424     app.testAllTables();
0425   } catch (exception &e) {
0426     cout << "ERROR:  " << e.what() << endl;
0427   } catch (...) {
0428     cout << "Unknown error caught" << endl;
0429   }
0430 
0431   cout << "All Done." << endl;
0432 
0433   return 0;
0434 }