Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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