Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:32:14

0001 #include <iostream>
0002 #include <string>
0003 #include <vector>
0004 #include <time.h>
0005 #include <unistd.h>
0006 
0007 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0008 #include "OnlineDB/EcalCondDB/interface/all_monitoring_types.h"
0009 
0010 
0011 using namespace std;
0012 
0013 class CondDBApp {
0014 public:
0015 
0016   /**
0017    *   App constructor; Makes the database connection
0018    */
0019   CondDBApp(string host, string sid, string user, string pass)
0020   {
0021     try {
0022       cout << "Making connection..." << flush;
0023       econn = new EcalCondDBInterface( sid, user, pass );
0024       cout << "Done." << endl;
0025     } catch (runtime_error &e) {
0026       cerr << e.what() << endl;
0027       exit(-1);
0028     }
0029 
0030     locations[0] = "H4";
0031     locations[1] = "867-1";
0032     locations[2] = "867-2";
0033     locations[3] = "867-3";
0034   }
0035 
0036 
0037 
0038   /**
0039    *  App destructor;  Cleans up database connection
0040    */
0041   ~CondDBApp() 
0042   {
0043     delete econn;
0044   }
0045 
0046 
0047   RunIOV makeRunIOV(int run_num)
0048   {
0049     // The objects necessary to identify a dataset
0050     LocationDef locdef;
0051     RunTypeDef rundef;
0052     RunTag runtag;
0053 
0054     locdef.setLocation("P5_Co");
0055 
0056     rundef.setRunType("TEST");
0057     
0058     runtag.setLocationDef(locdef);
0059     runtag.setRunTypeDef(rundef);
0060 
0061     // Our beginning time will be the current GMT time
0062     // This is the time zone that should be written to the DB!
0063     // (Actually UTC)
0064     Tm startTm;
0065     startTm.setToCurrentGMTime();
0066 
0067     uint64_t microseconds = startTm.microsTime();
0068     startmicros = microseconds;
0069 
0070     int startrun = run_num;
0071 
0072     cout << "Starting Time:    " << startTm.str() << endl;
0073     cout << "Starting Micros:  " << startmicros << endl;
0074     cout << "Starting Run:     " << startrun << endl;
0075 
0076     // Set the properties of the iov
0077     RunIOV runiov;
0078 
0079     startTm.setToMicrosTime(microseconds);
0080     cout << "Setting run " << startrun << " run_start " << startTm.str() << endl;
0081     runiov.setRunNumber(startrun);
0082     runiov.setRunStart(startTm);
0083     runiov.setRunTag(runtag);
0084     
0085     return runiov;
0086   }
0087 
0088   
0089   MonRunIOV makeMonRunIOV(RunIOV* runiov)
0090   {
0091     // Monitoring Tag and IOV
0092     MonVersionDef monverdef;
0093     monverdef.setMonitoringVersion("test01");
0094 
0095     MonRunTag montag;
0096     montag.setMonVersionDef(monverdef);
0097 
0098     MonRunIOV moniov;
0099     moniov.setMonRunTag(montag);
0100     moniov.setRunIOV(*runiov);
0101     moniov.setSubRunNumber(0);
0102     moniov.setSubRunStart(runiov->getRunStart());
0103 
0104     return moniov;
0105   }
0106 
0107 
0108 
0109   /**
0110    *  Write MonPedestalsDat objects with associated RunTag and RunIOVs
0111    *  IOVs are written using automatic updating of the 'RunEnd', as if
0112    *  the time of the end of the run was not known at the time of writing.
0113    */
0114   void testWrite(int run_num, std::string config_tag)
0115   {
0116     cout << "Writing MonPedestalsDatObjects to database..." << endl;
0117     RunIOV runiov = this->makeRunIOV(run_num);
0118     RunTag runtag = runiov.getRunTag();
0119     run_t run = runiov.getRunNumber();
0120 
0121     // write to the DB
0122     cout << "Inserting run..." << flush;
0123     econn->insertRunIOV(&runiov);
0124     cout << "Done." << endl;
0125     printIOV(&runiov);
0126 
0127 //     // write another run with the same run number to generate exception later
0128 //     runtag.setGeneralTag("different");
0129 //     runiov.setRunTag(runtag);
0130 //     econn->insertRunIOV(&runiov);
0131 
0132 
0133     // fetch it back
0134     cout << "Fetching run by tag just used..." << flush;
0135     RunIOV runiov_prime = econn->fetchRunIOV(&runtag, run);
0136     cout << "Done." << endl;
0137     printIOV(&runiov_prime);
0138 
0139 
0140 
0141 
0142     int c = 1;
0143     EcalLogicID ecid;
0144     ecid = econn->getEcalLogicID("ECAL");
0145 
0146     // recuperare last version !!!
0147 
0148     FEConfigMainInfo cfg_info;
0149     cfg_info.setConfigTag(config_tag);
0150     econn->fetchConfigSet(&cfg_info);
0151 
0152     int iversion= cfg_info.getVersion();
0153 
0154 
0155     // Set the data
0156     RunTPGConfigDat cfgdat;
0157     map<EcalLogicID, RunTPGConfigDat> dataset;
0158 
0159     cfgdat.setConfigTag(config_tag);
0160     cfgdat.setVersion(iversion);
0161 
0162     // Fill the dataset
0163     dataset[ecid] = cfgdat;
0164 
0165     // Insert the dataset, identifying by iov
0166     cout << "Inserting dataset..." << flush;
0167     econn->insertDataSet(&dataset, &runiov);
0168     cout << "Done." << endl;
0169 
0170 
0171 
0172     sleep(3);
0173 
0174 
0175 
0176 
0177 
0178 
0179     cout << "updating start time..." << flush;
0180 
0181     Tm startTm;
0182     startTm.setToCurrentGMTime();
0183     uint64_t microseconds = startTm.microsTime();
0184     startTm.setToMicrosTime(microseconds);
0185     runiov.setRunStart(startTm);
0186 
0187     // write to the DB
0188     cout << "Inserting run..." << flush;
0189     econn->updateRunIOVStartTime(&runiov);
0190     cout << "Done." << endl;
0191     printIOV(&runiov);
0192 
0193 
0194     sleep(3);
0195 
0196     cout << "updating end time..." << flush;
0197 
0198     Tm endTm;
0199     endTm.setToCurrentGMTime();
0200     microseconds = endTm.microsTime();
0201     endTm.setToMicrosTime(microseconds);
0202     runiov.setRunEnd(endTm);
0203 
0204     // write to the DB
0205     cout << "Inserting run..." << flush;
0206     econn->updateRunIOVEndTime(&runiov);
0207     cout << "Done." << endl;
0208     printIOV(&runiov);
0209 
0210     cout << "Done." << endl << endl << endl;
0211   };
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 private:
0246   CondDBApp();  // hidden default constructor
0247   EcalCondDBInterface* econn;
0248   string locations[4];
0249   uint64_t startmicros;
0250   uint64_t endmicros;
0251   run_t startrun;
0252   run_t endrun;
0253 
0254   int tablesTried;
0255   int tablesOK;
0256 
0257   /**
0258    *   Iterate through the dataset and print some data
0259    */
0260   void printDataSet( const map<EcalLogicID, MonPedestalsDat>* dataset, int limit = 0 ) const
0261   {
0262     cout << "==========printDataSet()" << endl;
0263     if (dataset->size() == 0) {
0264       cout << "No data in map!" << endl;
0265     }
0266     EcalLogicID ecid;
0267     MonPedestalsDat ped;
0268 
0269     int count = 0;
0270     typedef map< EcalLogicID, MonPedestalsDat >::const_iterator CI;
0271     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0272       count++;
0273       if (limit && count > limit) { return; }
0274       ecid = p->first;
0275       ped  = p->second;
0276      
0277       cout << "SM:                     " << ecid.getID1() << endl;
0278       cout << "Xtal:                   " << ecid.getID2() << endl;
0279       cout << "Mean G1:                " << ped.getPedMeanG1() << endl;
0280       cout << "========================" << endl;
0281     }
0282     cout << endl;
0283   }
0284 
0285 
0286   /**
0287    *   Print out a RunTag
0288    */
0289   void printTag( const RunTag* tag) const
0290   {
0291     cout << endl;
0292     cout << "=============RunTag:" << endl;
0293     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0294     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0295     cout << "Run Type:           " << tag->getRunTypeDef().getRunType() << endl;
0296     cout << "====================" << endl;
0297   }
0298 
0299   void printIOV( const RunIOV* iov) const
0300   {
0301     cout << endl;
0302     cout << "=============RunIOV:" << endl;
0303     RunTag tag = iov->getRunTag();
0304     printTag(&tag);
0305     cout << "Run Number:         " << iov->getRunNumber() << endl;
0306     cout << "Run Start:          " << iov->getRunStart().str() << endl;
0307     cout << "Run End:            " << iov->getRunEnd().str() << endl;
0308     cout << "====================" << endl;
0309   }
0310 };
0311 
0312 
0313 
0314 int main (int argc, char* argv[])
0315 {
0316   string host;
0317   string sid;
0318   string user;
0319   string pass;
0320   int run_num;
0321   string config_tag;
0322 
0323 
0324   if (argc != 7) {
0325     cout << "Usage:" << endl;
0326     cout << "  " << argv[0] << " <host> <SID> <user> <pass>" << endl;
0327     exit(-1);
0328   }
0329 
0330   host = argv[1];
0331   sid = argv[2];
0332   user = argv[3];
0333   pass = argv[4];
0334   run_num = atoi(argv[5]);
0335   config_tag = argv[6];
0336 
0337 
0338   try {
0339     CondDBApp app(host, sid, user, pass);
0340 
0341     app.testWrite(run_num, config_tag);
0342 
0343   } catch (exception &e) {
0344     cout << "ERROR:  " << e.what() << endl;
0345   } catch (...) {
0346     cout << "Unknown error caught" << endl;
0347   }
0348 
0349   cout << "All Done." << endl;
0350 
0351   return 0;
0352 }