Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include <iostream>
0002 
0003 #include <fstream>
0004 #include <string>
0005 #include <vector>
0006 #include <time.h>
0007 #include <cstdlib>
0008 
0009 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0010 #include "OnlineDB/EcalCondDB/interface/all_cali_types.h"
0011 
0012 
0013 using namespace std;
0014 
0015 class CondDBApp {
0016 public:
0017 
0018   /**
0019    *   App constructor; Makes the database connection
0020    */
0021   CondDBApp(string sid, string user, string pass)
0022   {
0023     try {
0024       cout << "Making connection..." << flush;
0025       econn = new EcalCondDBInterface( sid, user, pass );
0026       cout << "Done." << endl;
0027     } catch (runtime_error &e) {
0028       cerr << e.what() << endl;
0029       exit(-1);
0030     }
0031 
0032     
0033   }
0034 
0035 
0036 
0037   /**
0038    *  App destructor;  Cleans up database connection
0039    */
0040   ~CondDBApp() 
0041   {
0042     delete econn;
0043   }
0044 
0045 
0046 
0047   CaliIOV makeCaliIOV()
0048   {
0049     LocationDef locdef;
0050     locdef.setLocation("LAB");
0051     CaliTag calitag;
0052     calitag.setLocationDef(locdef);
0053     calitag.setDataType("TEMP_SENSOR");
0054     calitag.setMethod("COSMICS");
0055     calitag.setVersion("OFFSET");
0056     calitag.setGeneralTag("EB_with_offset_in_situ");
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     //-------------------------------------------------------------
0080     int convertFromConstructionSMToSlot(int sm_constr,int sm_slot){
0081       // input either cosntruction number or slot number and returns the other
0082       // the slots are numbered first EB+ slot 1 ...18 then EB- 1... 18
0083       // the slots start at 1 and the SM start at 0
0084       //-------------------------------------------------------------
0085       int slot_to_constr[37]={-1,12,17,10,1,8,4,27,20,23,25,6,34,35,15,18,30,21,9
0086                   ,24,22,13,31,26,16,2,11,5,0,29,28,14,33,32,3,7,19};
0087       int constr_to_slot[36]={28,4,25,34,6,27,11,35,5,18,3,26,1,21,31,14,24,2,15,
0088                   36,8,17,20,9,19,10,23,7,30,29,16,22,33,32,12,13 };
0089 
0090       int result=0;
0091       if(sm_constr!=-1) {
0092     result=constr_to_slot[sm_constr];
0093       } else if(sm_slot!=-1) {
0094     result=slot_to_constr[sm_slot];
0095       }
0096       return result;
0097     }
0098 
0099 
0100   /**
0101    *  Write objects with associated CaliIOVs
0102    *  IOVs are written using automatic updating of the 'till', as if
0103    *  the time of the end of the run was not known at the time of writing.
0104    */
0105   void testWrite(std::string filename)
0106   {
0107 
0108     cout << "read calibration file " << filename << endl;
0109     ifstream fin;
0110     fin.open(filename.c_str());
0111 
0112     std::string ch_line;
0113     // fin>> ch_line;
0114 
0115     float temp_vec[61200];
0116     float beta_vec[61200];
0117     float r25_vec[61200];
0118     float nic_vec[61200];
0119     for(int ic=0; ic<61200; ic++){
0120       beta_vec[ic]=0;
0121       r25_vec[ic]=0;
0122       temp_vec[ic]=0;
0123       nic_vec[ic]=0;
0124     }
0125 
0126 
0127     int sm,i;
0128     float beta, r25, nic;
0129     // int slot_num=0;
0130     while( fin.peek() != EOF )
0131       {
0132 
0133     //  fin >> sm>>i>>j>>ih4>>temp>>beta>>r25>>nic;
0134     fin >> sm>>i>>beta>>r25>>nic;
0135       
0136     
0137     if(i<10) cout << "sm/i/beta/r25/offset="<<sm<<"/"<<i<<"/"<<beta<<"/"<<r25<<"/"<<nic<< endl;
0138     //  slot_num=convertFromConstructionSMToSlot(sm,-1);
0139     
0140     int ic=(sm-1)*170+i-1;
0141 
0142     //  int ic=(slot_num-1)*1700+(ih4-1);
0143     
0144     beta_vec[ic]=beta;
0145     r25_vec[ic]=r25;
0146     temp_vec[ic]=1.0;
0147     nic_vec[ic]=nic;
0148       }
0149 
0150     fin.close();
0151 
0152     cout << "Writing CaliCrystalIntercalDat objects to database..." << endl;
0153     cout << "Making a CaliIOV..." << flush;
0154     CaliIOV caliiov = this->makeCaliIOV();
0155     cout << "Done." << endl;
0156 
0157     this->printIOV(&caliiov);
0158 
0159     Tm eventTm = caliiov.getSince();
0160     CaliTag calitag = caliiov.getCaliTag();
0161 
0162 
0163     vector<EcalLogicID> ecid_vec;
0164     //    ecid_vec = econn->getEcalLogicIDSetOrdered("EB_crystal_number", 1,36, 1, 1700,EcalLogicID::NULLID,
0165     //                     EcalLogicID::NULLID, "EB_T_capsule",12);
0166 
0167 
0168     ecid_vec = econn->getEcalLogicIDSetOrdered("EB_T_capsule", 1,36, 1, 170,EcalLogicID::NULLID,
0169                            EcalLogicID::NULLID, "EB_T_capsule",12);
0170 
0171 
0172    map<EcalLogicID, CaliTempDat> dataset;
0173    int count=0;
0174    //   for (int ii=0; ii<61200; ii++){
0175    for (int ii=0; ii<6120; ii++){
0176        CaliTempDat capTemp;
0177        if(temp_vec[ii]!=0 ){
0178        
0179      float val1 = beta_vec[ii] ;
0180      float val2 = r25_vec[ii];
0181        
0182      capTemp.setBeta(val1);
0183      capTemp.setR25(val2);
0184      capTemp.setOffset(nic_vec[ii]);
0185      capTemp.setTaskStatus(1) ;
0186        
0187        // Fill the dataset
0188      
0189      dataset[ecid_vec[ ii] ] = capTemp;
0190      if(ii/100==0) cout<< "filling DB for "<<ii<<" count="<<count<<endl; 
0191      count++;
0192        }
0193    }
0194 
0195 
0196    // Insert the dataset, identifying by iov
0197    cout << "Inserting dataset..." << flush;
0198    econn->insertDataArraySet(&dataset, &caliiov);
0199    cout << "Done." << endl;
0200 
0201 
0202   };
0203 
0204 
0205 
0206   /**
0207    *  Write a data set
0208    */
0209   template<class DATT, class IOVT>
0210   void testTable(DATT* data, IOVT* iov, EcalLogicID* ecid)
0211   {
0212     tablesTried++;
0213     try {
0214       cout << "Table " << tablesTried << "..." << flush;
0215       map<EcalLogicID, DATT> dataset;
0216       dataset[*ecid] = *data;
0217       econn->insertDataSet(&dataset, iov);
0218       cout << "write OK..." << flush;
0219       dataset.clear();
0220       econn->fetchDataSet(&dataset, iov);
0221       if (!dataset.size()) {
0222     throw(runtime_error("Zero rows read back"));
0223       }
0224       cout << "read OK" << endl;
0225       tablesOK++;
0226     } catch (runtime_error &e) {
0227       cout << "testTable FAILED:  " << e.what() << endl;
0228     } catch (...) {
0229       cout << "testTable FAILED:  unknown exception" << endl;
0230     }
0231   }
0232 
0233 
0234 
0235   /**
0236    *  Write to each of the data tables
0237    */
0238   void testAllTables()
0239   {
0240     cout << "Testing writing to all tables..." << endl;
0241     tablesTried = 0;
0242     tablesOK = 0;
0243     
0244     // get a dummy logic ID
0245     EcalLogicID logicID = econn->getEcalLogicID(-1);
0246 
0247     // CaliIOV tables
0248     CaliIOV caliiov = this->makeCaliIOV();
0249 
0250     CaliGeneralDat table01;
0251     testTable(&table01, &caliiov, &logicID);
0252 
0253     CaliCrystalIntercalDat table02;
0254     testTable(&table02, &caliiov, &logicID);
0255 
0256     CaliHVScanRatioDat table03;
0257     testTable(&table03, &caliiov, &logicID);
0258 
0259     cout << "Test of writing to all tables complete" << endl;
0260     cout << tablesOK << " of " << tablesTried << " written to successfully" << endl << endl << endl;
0261   };
0262 
0263 
0264 
0265 private:
0266   CondDBApp();  // hidden default constructor
0267   EcalCondDBInterface* econn;
0268   uint64_t startmicros;
0269   uint64_t endmicros;
0270   run_t startrun;
0271   run_t endrun;
0272 
0273   int tablesTried;
0274   int tablesOK;
0275 
0276   /**
0277    *   Iterate through the dataset and print some data
0278    */
0279   void printDataSet( const map<EcalLogicID, CaliCrystalIntercalDat>* dataset, int limit = 0 ) const
0280   {
0281     cout << "==========printDataSet()" << endl;
0282     if (dataset->size() == 0) {
0283       cout << "No data in map!" << endl;
0284     }
0285     EcalLogicID ecid;
0286     CaliCrystalIntercalDat crystalCal;;
0287 
0288     int count = 0;
0289     typedef map< EcalLogicID, CaliCrystalIntercalDat >::const_iterator CI;
0290     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0291       count++;
0292       if (limit && count > limit) { return; }
0293       ecid = p->first;
0294       crystalCal  = p->second;
0295      
0296       cout << "SM:                     " << ecid.getID1() << endl;
0297       cout << "Xtal:                   " << ecid.getID2() << endl;
0298       cout << "calibration:            " << crystalCal.getCali() << endl;
0299       cout << "calibration rms:        " << crystalCal.getCaliRMS() << endl;
0300       cout << "num events:             " << crystalCal.getNumEvents() << endl;
0301       cout << "task status:            " << crystalCal.getTaskStatus() << endl;
0302       cout << "========================" << endl;
0303     }
0304     cout << endl;
0305   }
0306 
0307 
0308   /**
0309    *   Print out a CaliTag
0310    */
0311   void printTag( const CaliTag* tag) const
0312   {
0313     cout << endl;
0314     cout << "=============CaliTag:" << endl;
0315     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0316     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0317     cout << "Method:             " << tag->getMethod() << endl;
0318     cout << "Version:            " << tag->getVersion() << endl;
0319     cout << "Data Type:          " << tag->getDataType() << endl;
0320     cout << "====================" << endl;
0321   }
0322 
0323   void printIOV( const CaliIOV* iov) const
0324   {
0325     cout << endl;
0326     cout << "=============CaliIOV:" << endl;
0327     CaliTag tag = iov->getCaliTag();
0328     printTag(&tag);
0329     cout << "since:              " << iov->getSince().str() << endl;
0330     cout << "till:               " << iov->getTill().str() << endl;
0331     cout << "====================" << endl;
0332   }
0333 };
0334 
0335 
0336 
0337 int main (int argc, char* argv[])
0338 {
0339   string host;
0340   string sid;
0341   string user;
0342   string pass;
0343   string filename;
0344 
0345   if (argc != 6) {
0346     cout << "Usage:" << endl;
0347     cout << "  " << argv[0] << " <host> <SID> <user> <pass> <filename>" << endl;
0348     exit(-1);
0349   }
0350 
0351   host = argv[1];
0352   sid = argv[2];
0353   user = argv[3];
0354   pass = argv[4];
0355   filename = argv[5];
0356 
0357   try {
0358     CondDBApp app(sid, user, pass);
0359 
0360     app.testWrite(filename);
0361     //   app.testAllTables();
0362   } catch (exception &e) {
0363     cout << "ERROR:  " << e.what() << endl;
0364   } catch (...) {
0365     cout << "Unknown error caught" << endl;
0366   }
0367 
0368   cout << "All Done." << endl;
0369 
0370   return 0;
0371 }