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 #include <unistd.h>
0008 #include <cstdio>
0009 #include <typeinfo>
0010 #include <sstream>
0011 
0012 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0013 #include "OnlineDB/EcalCondDB/interface/all_cali_types.h"
0014 
0015 
0016 using std::string;
0017 
0018 using namespace std;
0019 
0020 class CondDBApp {
0021 public:
0022 
0023   /**
0024    *   App constructor; Makes the database connection
0025    */
0026   CondDBApp(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     
0038   }
0039 
0040 
0041 
0042   /**
0043    *  App destructor;  Cleans up database connection
0044    */
0045   ~CondDBApp() 
0046   {
0047     delete econn;
0048   }
0049 
0050 
0051 
0052     //-------------------------------------------------------------
0053     int convertFromConstructionSMToSlot(int sm_constr,int sm_slot){
0054       // input either cosntruction number or slot number and returns the other
0055       // the slots are numbered first EB+ slot 1 ...18 then EB- 1... 18
0056       // the slots start at 1 and the SM start at 0
0057       //-------------------------------------------------------------
0058       int slot_to_constr[37]={-1,12,17,10,1,8,4,27,20,23,25,6,34,35,15,18,30,21,9
0059                   ,24,22,13,31,26,16,2,11,5,0,29,28,14,33,32,3,7,19};
0060       int constr_to_slot[36]={28,4,25,34,6,27,11,35,5,18,3,26,1,21,31,14,24,2,15,
0061                   36,8,17,20,9,19,10,23,7,30,29,16,22,33,32,12,13 };
0062 
0063       int result=0;
0064       if(sm_constr!=-1) {
0065     result=constr_to_slot[sm_constr];
0066       } else if(sm_slot!=-1) {
0067     result=slot_to_constr[sm_slot];
0068       }
0069       return result;
0070     }
0071 
0072 
0073   /**
0074    *  Write objects with associated CaliIOVs
0075    *  IOVs are written using automatic updating of the 'till', as if
0076    *  the time of the end of the run was not known at the time of writing.
0077    */
0078   void testWrite(std::string filename)
0079   {
0080 
0081     cout << "read calibration file " << filename << endl;
0082     ifstream fin;
0083     fin.open(filename.c_str());
0084 
0085     std::string ch_line;
0086     // fin>> ch_line;
0087 
0088 
0089     int slot_num=0;
0090 
0091 
0092     FILE *inpFile; // input file
0093 
0094     inpFile = fopen(filename.c_str(),"r");
0095     if(!inpFile) {
0096       std::cout<< "*** Can not open file: "<<filename;
0097     
0098     }
0099 
0100     char line[256];
0101 
0102     std::ostringstream str;
0103 
0104     fgets(line,255,inpFile);
0105     string sm_or_all=to_string(line);
0106     int sm_number=0;
0107     int nchan=1700;
0108     sm_number=atoi(line);
0109     str << "sm= " << sm_number << endl ;
0110     if(sm_number!=-1){
0111       nchan=1700;
0112     } else {
0113       nchan=61200;
0114     }
0115     fgets(line,255,inpFile);
0116     //int nevents=atoi(line); // not necessary here just for online conddb
0117 
0118     fgets(line,255,inpFile);
0119     string gen_tag=to_string(line);
0120     str << "gen tag " << gen_tag << endl ;  // should I use this?
0121 
0122     fgets(line,255,inpFile);
0123     string cali_method=to_string(line);
0124     str << "cali method " << cali_method << endl ; // not important
0125 
0126     fgets(line,255,inpFile);
0127     string cali_version=to_string(line);
0128     str << "cali version " << cali_version << endl ; // not important
0129 
0130     fgets(line,255,inpFile);
0131     string cali_type=to_string(line);
0132     str << "cali type " << cali_type << endl ; // not important
0133 
0134     std::cout << "Intercalibration file " << str.str() ;
0135 
0136 
0137     LocationDef locdef;
0138     locdef.setLocation("LAB");
0139     CaliTag calitag;
0140     calitag.setLocationDef(locdef);
0141     calitag.setMethod(cali_method);
0142     calitag.setVersion(cali_version);
0143     calitag.setDataType(cali_type);
0144     calitag.setGeneralTag(gen_tag);
0145 
0146 
0147     // Our beginning time will be the current GMT time
0148     // This is the time zone that should be written to the DB!
0149     // (Actually UTC)
0150     Tm since ;
0151     since.setToGMTime(1);   
0152 
0153     // Set the properties of the iov
0154     CaliIOV caliiov;
0155 
0156     caliiov.setSince(since);
0157     caliiov.setCaliTag(calitag);
0158 
0159 
0160     int sm_num[61200]={0};
0161     int cry_num[61200]={0};
0162     float calib[61200]={0};
0163     float calib_rms[61200]={0};
0164     int calib_nevents[61200]={0};
0165     int calib_status[61200]={0};
0166 
0167 
0168     vector<EcalLogicID> ecid_vec;
0169     ecid_vec = econn->getEcalLogicIDSetOrdered("EB_crystal_number", 1, 36, 1, 1700,EcalLogicID::NULLID,
0170                            EcalLogicID::NULLID, "EB_crystal_number",12);
0171 
0172 
0173     int ii = 0;
0174     if(sm_number!=-1){
0175       while(fgets(line,255,inpFile)) {
0176         sscanf(line, "%d %f %f %d %d", &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_nevents[ii], &calib_status[ii]);
0177         sm_num[ii]=sm_number;
0178         ii++ ;
0179       }
0180     } else {
0181       // this is for the whole Barrel
0182       cout<<"mode ALL BARREL" <<endl;
0183       while(fgets(line,255,inpFile)) {
0184         sscanf(line, "%d %d %f %f %d", &sm_num[ii], &cry_num[ii], &calib[ii], &calib_rms[ii], &calib_status[ii] );
0185         if(ii==0) cout<<"crystal "<<cry_num[ii]<<" of sm "<<sm_num[ii]<<" cali= "<< calib[ii]<<endl;
0186         ii++ ;
0187       }
0188     }
0189 
0190     fclose(inpFile);           // close inp. file
0191 
0192     cout << " I read the calibrations for "<< ii<< " crystals " << endl;
0193     if(ii!=nchan) std::cout << "Some crystals missing. Missing channels will be set to 0" << endl;
0194 
0195     // Get channel ID
0196 
0197 
0198 
0199 
0200     cout << "Writing CaliCrystalIntercalDat objects to database..." << endl;
0201 
0202     this->printIOV(&caliiov);
0203 
0204     map<EcalLogicID, CaliCrystalIntercalDat> dataset;
0205 
0206     // Set the data
0207     for(int i=0; i<nchan; i++){
0208 
0209       slot_num=convertFromConstructionSMToSlot(sm_num[i],-1);
0210       
0211       if(i==0) cout<<"crystal "<<cry_num[i]<<" of sm "<<sm_num[i]<< " in slot " <<slot_num<<" calib= "<< calib[i]<<endl  ;
0212 
0213     CaliCrystalIntercalDat crystalCal;
0214     crystalCal.setCali(calib[i]);
0215     crystalCal.setCaliRMS(calib_rms[i]);
0216     crystalCal.setNumEvents(calib_nevents[i]);
0217     crystalCal.setTaskStatus(calib_status[i]);
0218 
0219     // Fill the dataset
0220 
0221     int itot=(slot_num-1)*1700+(cry_num[i]-1);
0222     dataset[ecid_vec [itot] ] = crystalCal;
0223        
0224     }
0225 
0226 
0227    // Insert the dataset, identifying by iov
0228    cout << "Inserting dataset..." << flush;
0229    econn->insertDataArraySet(&dataset, &caliiov);
0230    cout << "Done." << endl;
0231 
0232   
0233   };
0234 
0235 
0236 
0237   /**
0238    *  Write a data set
0239    */
0240   template<class DATT, class IOVT>
0241   void testTable(DATT* data, IOVT* iov, EcalLogicID* ecid)
0242   {
0243     tablesTried++;
0244     try {
0245       cout << "Table " << tablesTried << "..." << flush;
0246       map<EcalLogicID, DATT> dataset;
0247       dataset[*ecid] = *data;
0248       econn->insertDataSet(&dataset, iov);
0249       cout << "write OK..." << flush;
0250       dataset.clear();
0251       econn->fetchDataSet(&dataset, iov);
0252       if (!dataset.size()) {
0253     throw(runtime_error("Zero rows read back"));
0254       }
0255       cout << "read OK" << endl;
0256       tablesOK++;
0257     } catch (runtime_error &e) {
0258       cout << "testTable FAILED:  " << e.what() << endl;
0259     } catch (...) {
0260       cout << "testTable FAILED:  unknown exception" << endl;
0261     }
0262   }
0263 
0264 
0265 
0266 
0267 
0268 
0269 private:
0270   CondDBApp();  // hidden default constructor
0271   EcalCondDBInterface* econn;
0272   uint64_t startmicros;
0273   uint64_t endmicros;
0274   run_t startrun;
0275   run_t endrun;
0276 
0277   int tablesTried;
0278   int tablesOK;
0279 
0280   std::string to_string( char value[]) {
0281     std::ostringstream streamOut;
0282     streamOut << value;
0283     return streamOut.str();
0284   }
0285 
0286 
0287   /**
0288    *   Iterate through the dataset and print some data
0289    */
0290   void printDataSet( const map<EcalLogicID, CaliCrystalIntercalDat>* dataset, int limit = 0 ) const
0291   {
0292     cout << "==========printDataSet()" << endl;
0293     if (dataset->size() == 0) {
0294       cout << "No data in map!" << endl;
0295     }
0296     EcalLogicID ecid;
0297     CaliCrystalIntercalDat crystalCal;;
0298 
0299     int count = 0;
0300     typedef map< EcalLogicID, CaliCrystalIntercalDat >::const_iterator CI;
0301     for (CI p = dataset->begin(); p != dataset->end(); p++) {
0302       count++;
0303       if (limit && count > limit) { return; }
0304       ecid = p->first;
0305       crystalCal  = p->second;
0306      
0307       cout << "SM:                     " << ecid.getID1() << endl;
0308       cout << "Xtal:                   " << ecid.getID2() << endl;
0309       cout << "calibration:            " << crystalCal.getCali() << endl;
0310       cout << "calibration rms:        " << crystalCal.getCaliRMS() << endl;
0311       cout << "num events:             " << crystalCal.getNumEvents() << endl;
0312       cout << "task status:            " << crystalCal.getTaskStatus() << endl;
0313       cout << "========================" << endl;
0314     }
0315     cout << endl;
0316   }
0317 
0318 
0319   /**
0320    *   Print out a CaliTag
0321    */
0322   void printTag( const CaliTag* tag) const
0323   {
0324     cout << endl;
0325     cout << "=============CaliTag:" << endl;
0326     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0327     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0328     cout << "Method:             " << tag->getMethod() << endl;
0329     cout << "Version:            " << tag->getVersion() << endl;
0330     cout << "Data Type:          " << tag->getDataType() << endl;
0331     cout << "====================" << endl;
0332   }
0333 
0334   void printIOV( const CaliIOV* iov) const
0335   {
0336     cout << endl;
0337     cout << "=============CaliIOV:" << endl;
0338     CaliTag tag = iov->getCaliTag();
0339     printTag(&tag);
0340     cout << "since:              " << iov->getSince().str() << endl;
0341     cout << "till:               " << iov->getTill().str() << endl;
0342     cout << "====================" << endl;
0343   }
0344 };
0345 
0346 
0347 
0348 int main (int argc, char* argv[])
0349 {
0350   string host;
0351   string sid;
0352   string user;
0353   string pass;
0354   string filename;
0355 
0356   if (argc != 6) {
0357     cout << "Usage:" << endl;
0358     cout << "  " << argv[0] << " <host> <SID> <user> <pass> <filename>" << endl;
0359     exit(-1);
0360   }
0361 
0362   host = argv[1];
0363   sid = argv[2];
0364   user = argv[3];
0365   pass = argv[4];
0366   filename = argv[5];
0367 
0368   try {
0369     CondDBApp app(sid, user, pass);
0370 
0371     app.testWrite(filename);
0372 
0373   } catch (exception &e) {
0374     cout << "ERROR:  " << e.what() << endl;
0375   } catch (...) {
0376     cout << "Unknown error caught" << endl;
0377   }
0378 
0379   cout << "All Done." << endl;
0380 
0381   return 0;
0382 }