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 <sstream>
0004 #include <vector>
0005 #include <time.h>
0006 #include <cstdlib>
0007 
0008 #include "OnlineDB/EcalCondDB/interface/EcalCondDBInterface.h"
0009 #include "OnlineDB/EcalCondDB/interface/all_monitoring_types.h"
0010 #include "OnlineDB/EcalCondDB/interface/all_fe_config_types.h"
0011 #include "OnlineDB/EcalCondDB/interface/RunDat.h"
0012 #include "OnlineDB/EcalCondDB/interface/RunList.h"
0013 #include "OnlineDB/EcalCondDB/interface/MonPedestalsDat.h"
0014 #include "TROOT.h"
0015 #include "TFile.h"
0016 #include "TDirectory.h"
0017 #include "TH2F.h"
0018 #include "TF1.h"
0019 
0020 
0021 using namespace std;
0022 
0023 class CondDBApp {
0024 public:
0025 
0026   /**
0027    *   App constructor; Makes the database connection
0028    */
0029   CondDBApp(string host, string sid, string user, string pass, int port)
0030   {
0031     try {
0032       cout << "Making connection...to " << port << flush;
0033       econn = new EcalCondDBInterface( host, sid, user, pass, port );
0034       cout << "Done." << endl;
0035     } catch (runtime_error &e) {
0036       cerr << e.what() << endl;
0037       exit(-1);
0038     }
0039 
0040  
0041   }
0042   CondDBApp(string sid, string user, string pass)
0043   {
0044     try {
0045       cout << "Making connection...to " << sid << endl;
0046       econn = new EcalCondDBInterface(  sid, user, pass );
0047       cout << "Done." << endl;
0048     } catch (runtime_error &e) {
0049       cerr << e.what() << endl;
0050       exit(-1);
0051     }
0052 
0053  
0054   }
0055 
0056 
0057   /**
0058    *  App destructor;  Cleans up database connection
0059    */
0060   ~CondDBApp() 
0061   {
0062     delete econn;
0063   }
0064 
0065   inline std::string to_string( char value[])
0066   {
0067     std::ostringstream streamOut;
0068     streamOut << value;
0069     return streamOut.str();    
0070   }
0071   
0072 
0073   int test(int runa, int runb ) {
0074 
0075 
0076     cout << "*****************************************" << endl;
0077     cout << "************Inserting Peds from OMDS*****" << endl;
0078     cout << "*****************************************" << endl;
0079     
0080     int result=0;
0081 
0082     cout << "Retrieving run list from DB ... " << endl;
0083    
0084 
0085 
0086     RunTag  my_runtag;
0087     LocationDef my_locdef;
0088     RunTypeDef my_rundef;
0089     
0090     my_locdef.setLocation("H4B_07");
0091     my_rundef.setRunType("PEDESTAL");
0092     my_runtag.setLocationDef(my_locdef);
0093     my_runtag.setRunTypeDef(my_rundef);
0094     my_runtag.setGeneralTag("LOCAL");
0095 
0096     // here we retrieve the Monitoring results
0097 
0098     MonVersionDef monverdef;
0099     monverdef.setMonitoringVersion("test01");
0100     MonRunTag montag;
0101     montag.setMonVersionDef(monverdef);
0102     montag.setGeneralTag("CMSSW");
0103 
0104    
0105     MonRunList mon_list;
0106     mon_list.setMonRunTag(montag);
0107     mon_list.setRunTag(my_runtag);
0108     int min_run=runa;
0109     int n_runs=runb;
0110     cout<< "min run is:"<<min_run<<endl;
0111     mon_list=econn->fetchMonRunListLastNRuns(my_runtag, montag, min_run ,n_runs  );
0112 
0113     std::vector<MonRunIOV> mon_run_vec=  mon_list.getRuns();
0114 
0115     cout <<"number of ped runs is : "<< mon_run_vec.size()<< endl;
0116     int mon_runs=(int) mon_run_vec.size();
0117     if(mon_runs>0){
0118       for(int ii=0; ii<mon_runs; ii++){
0119     cout << "here is the run number: "<< mon_run_vec[ii].getRunIOV().getRunNumber() << endl;
0120       }
0121     }
0122 
0123     int sm_num=0;  
0124 
0125     if(mon_runs>0){
0126 
0127       // for the first run of the list we retrieve the pedestals
0128       int run=0;
0129       cout <<" retrieve the data for a given run"<< endl;
0130       cout << "here is the run number: "<< mon_run_vec[run].getRunIOV().getRunNumber() << endl;
0131       
0132       RunIOV runiov_prime = mon_run_vec[run].getRunIOV();
0133 
0134       map<EcalLogicID, MonPedestalsDat> dataset_ped;
0135       econn->fetchDataSet(&dataset_ped, &mon_run_vec[run]);
0136 
0137 
0138 
0139       typedef map<EcalLogicID, MonPedestalsDat>::const_iterator CImped;
0140       EcalLogicID ecid_xt;
0141       MonPedestalsDat  rd_ped;
0142       
0143       sm_num=0;
0144       float ped_m12[61200];
0145       float ped_m6[61200];
0146       float ped_m1[61200];
0147       for (int i=0; i<61200; i++){
0148     ped_m12[i]=0;
0149     ped_m6[i]=0;
0150     ped_m1[i]=0;
0151       }
0152 
0153 
0154       for (CImped p = dataset_ped.begin(); p != dataset_ped.end(); p++) {
0155     ecid_xt = p->first;
0156     rd_ped  = p->second;
0157     sm_num=ecid_xt.getID1();
0158     int xt_id=ecid_xt.getID2();
0159     int xt_num=ecid_xt.getID2()+(sm_num-1)*1700-1;
0160     ped_m12[xt_num]=rd_ped.getPedMeanG12();
0161     ped_m6[xt_num]=rd_ped.getPedMeanG6();
0162     ped_m1[xt_num]=rd_ped.getPedMeanG1();
0163     if(xt_id==123) cout <<"here is one value for XT: "<< xt_id<<" SM: " << sm_num<< " "<< ped_m12[xt_num] << endl;
0164       }
0165       //  here we can do something to the pedestals like checking
0166       //  that they are correct and that there are no missing channels
0167 
0168       // .....
0169 
0170 
0171       // then we ship them to the config DB
0172 
0173       int mon_iov_id=mon_run_vec[run].getID();
0174 
0175       cout << "now create fe record " <<endl;
0176       FEConfigPedInfo fe_ped_info ;
0177       fe_ped_info.setIOVId(mon_iov_id);
0178       fe_ped_info.setConfigTag("from_CondDB");
0179       econn->insertConfigSet(&fe_ped_info);
0180       result =fe_ped_info.getID();
0181 
0182       // this is to insert in FE Config DB      
0183       vector<EcalLogicID> ecid_vec;
0184       ecid_vec = econn->getEcalLogicIDSet("EB_crystal_number", 1,36,1,1700);
0185       cout << "*********got channel numbers   *********" << endl;
0186 
0187 
0188       map<EcalLogicID, FEConfigPedDat> dataset;
0189       for (int ich=0; ich<61200; ich++){
0190     FEConfigPedDat ped;
0191     ped.setPedMeanG1(ped_m1[ich]);
0192     ped.setPedMeanG6(ped_m6[ich]);
0193     ped.setPedMeanG12(ped_m12[ich]);
0194     // Fill the dataset
0195     dataset[ecid_vec[ich]] = ped;
0196       }
0197     // Insert the dataset, identifying by iov
0198       cout << "*********about to insert peds *********" << endl;
0199       econn->insertDataArraySet(&dataset, &fe_ped_info);
0200       cout << "*********Done peds            *********" << endl;
0201     
0202     }
0203     
0204     return result;
0205   }
0206 
0207 
0208 
0209 
0210 
0211   void testRead(int iconf_req ) {
0212     // now we do something else 
0213     // this is an example for reading the pedestals 
0214     // for a given config iconf_req 
0215  
0216     cout << "*****************************************" << endl;
0217     cout << "test readinf fe_ped with id="<<iconf_req  << endl;
0218     cout << "*****************************************" << endl;
0219  
0220     FEConfigPedInfo fe_ped_info;
0221     fe_ped_info.setId(iconf_req);
0222     econn->fetchConfigSet(&fe_ped_info);
0223     map<EcalLogicID, FEConfigPedDat> dataset_ped;
0224     econn->fetchDataSet(&dataset_ped, &fe_ped_info);
0225 
0226     typedef map<EcalLogicID, FEConfigPedDat>::const_iterator CIfeped;
0227     EcalLogicID ecid_xt;
0228     FEConfigPedDat  rd_ped;
0229 
0230       float ped_m12[61200];
0231       float ped_m6[61200];
0232       float ped_m1[61200];
0233       for (int i=0; i<61200; i++){
0234     ped_m12[i]=0;
0235     ped_m6[i]=0;
0236     ped_m1[i]=0;
0237       }
0238 
0239       for (CIfeped p = dataset_ped.begin(); p != dataset_ped.end(); p++) {
0240     ecid_xt = p->first;
0241     rd_ped  = p->second;
0242     int sm_num=ecid_xt.getID1();
0243     int xt_num=ecid_xt.getID2();
0244     ped_m12[xt_num]=rd_ped.getPedMeanG12();
0245     ped_m6[xt_num]=rd_ped.getPedMeanG6();
0246     ped_m1[xt_num]=rd_ped.getPedMeanG1();
0247     if(xt_num==123&&sm_num==10) cout <<"here is one value for XT: "<< xt_num<<" " << ped_m12[xt_num] << endl;
0248 
0249       }
0250 
0251     cout << "*****************************************" << endl;
0252     cout << "test read done"<<iconf_req  << endl;
0253     cout << "*****************************************" << endl;
0254 
0255   }
0256 
0257 
0258 
0259   void testWriteLUT() {
0260     // now we do something else 
0261     // this is an example for writing the lut
0262  
0263 
0264       cout << "*****************************************" << endl;
0265       cout << "************Inserting LUT************" << endl;
0266       cout << "*****************************************" << endl;
0267 
0268       FEConfigLUTInfo fe_lut_info ;
0269       fe_lut_info.setNumberOfGroups(1); // this eventually refers to some other table 
0270       fe_lut_info.setConfigTag("test");
0271       econn->insertConfigSet(&fe_lut_info);
0272             
0273       Tm tdb = fe_lut_info.getDBTime();
0274       //tdb.dumpTm();
0275 
0276       vector<EcalLogicID> ecid_vec;
0277       ecid_vec = econn->getEcalLogicIDSet("EB_crystal_number", 1,36,1,1700);
0278      
0279       map<EcalLogicID, FEConfigLUTGroupDat> dataset;
0280       // we create 5 LUT groups 
0281       for (int ich=0; ich<5; ich++){
0282     FEConfigLUTGroupDat lut;
0283     lut.setLUTGroupId(ich);
0284     for (int i=0; i<1024; i++){
0285       lut.setLUTValue(i, (i*2) );
0286     }
0287     // Fill the dataset
0288     dataset[ecid_vec[ich]] = lut; // we use any logic id, because it is in any case ignored... 
0289        }
0290 
0291       // Insert the dataset
0292       econn->insertDataArraySet(&dataset, &fe_lut_info);
0293 
0294 
0295 
0296       // now we store in the DB the correspondence btw channels and LUT groups 
0297       map<EcalLogicID, FEConfigLUTDat> dataset2;
0298       // in this case I decide in a stupid way which channel belongs to which group 
0299       for (int ich=0; ich<(int)ecid_vec.size() ; ich++){
0300     FEConfigLUTDat lut;
0301     int igroup=ich/(ecid_vec.size()/5);
0302     lut.setLUTGroupId(igroup);
0303     // Fill the dataset
0304     dataset2[ecid_vec[ich]] = lut;
0305       }
0306 
0307       // Insert the dataset
0308 
0309       econn->insertDataArraySet(&dataset2, &fe_lut_info);
0310       cout << "*****************************************" << endl;
0311       cout << "************LUT done*********************" << endl;
0312       cout << "*****************************************" << endl;
0313 
0314     
0315 
0316   }
0317 
0318   void testWriteWeights() {
0319     // now we do something else 
0320     // this is an example for writing the weights
0321  
0322       cout << "*****************************************" << endl;
0323       cout << "************Inserting weights************" << endl;
0324       cout << "*****************************************" << endl;
0325 
0326       FEConfigWeightInfo fe_wei_info ;
0327       fe_wei_info.setNumberOfGroups(5); // this eventually refers to some other table 
0328       fe_wei_info.setConfigTag("my preferred weights");
0329       econn->insertConfigSet(&fe_wei_info);
0330       
0331       Tm tdb = fe_wei_info.getDBTime();
0332       //      tdb.dumpTm();
0333 
0334       vector<EcalLogicID> ecid_vec;
0335       ecid_vec = econn->getEcalLogicIDSet("EB_crystal_number", 1,36,1,1700);
0336 
0337       map<EcalLogicID, FEConfigWeightGroupDat> dataset;
0338       // we create 5 groups 
0339       for (int ich=0; ich<5; ich++){
0340     FEConfigWeightGroupDat wei;
0341     wei.setWeightGroupId(ich);
0342     wei.setWeight0(0);
0343     wei.setWeight1(1);
0344     wei.setWeight2(3);
0345     wei.setWeight3(3);
0346     wei.setWeight4(3);
0347     // Fill the dataset
0348     dataset[ecid_vec[ich]] = wei; // we use any logic id, because it is in any case ignored... 
0349        }
0350 
0351       // Insert the dataset
0352       econn->insertDataArraySet(&dataset, &fe_wei_info);
0353 
0354 
0355 
0356       // now we store in the DB the correspondence btw channels and LUT groups 
0357       map<EcalLogicID, FEConfigWeightDat> dataset2;
0358       // in this case I decide in a stupid way which channel belongs to which group 
0359       for (int ich=0; ich<(int)ecid_vec.size() ; ich++){
0360     FEConfigWeightDat weid;
0361     int igroup=ich/(ecid_vec.size()/5);
0362     weid.setWeightGroupId(igroup);
0363     // Fill the dataset
0364     dataset2[ecid_vec[ich]] = weid;
0365       }
0366 
0367       // Insert the dataset
0368 
0369       econn->insertDataArraySet(&dataset2, &fe_wei_info);
0370       cout << "*****************************************" << endl;
0371       cout << "************weights done*****************" << endl;
0372       cout << "*****************************************" << endl;
0373 
0374     
0375 
0376   }
0377 
0378 
0379 
0380 private:
0381   CondDBApp();  // hidden default constructor
0382   EcalCondDBInterface* econn;
0383   
0384   uint64_t startmicros;
0385   uint64_t endmicros;
0386   run_t startrun;
0387   run_t endrun;
0388 
0389   TFile* f;
0390   TH2F* mataq_vs_run;
0391   TH2F* apd_pn_mean_vs_run;
0392 
0393   void printTag( const RunTag* tag) const
0394   {
0395     cout << endl;
0396     cout << "=============RunTag:" << endl;
0397     cout << "GeneralTag:         " << tag->getGeneralTag() << endl;
0398     cout << "Location:           " << tag->getLocationDef().getLocation() << endl;
0399     cout << "Run Type:           " << tag->getRunTypeDef().getRunType() << endl;
0400     cout << "====================" << endl;
0401   }
0402 
0403   void printIOV( const RunIOV* iov) const
0404   {
0405     cout << endl;
0406     cout << "=============RunIOV:" << endl;
0407     RunTag tag = iov->getRunTag();
0408     printTag(&tag);
0409     cout << "Run Number:         " << iov->getRunNumber() << endl;
0410     cout << "Run Start:          " << iov->getRunStart().str() << endl;
0411     cout << "Run End:            " << iov->getRunEnd().str() << endl;
0412     cout << "====================" << endl;
0413   }
0414 };
0415 
0416 int main (int argc, char* argv[])
0417 {
0418 
0419   string sid;
0420   string user;
0421   string pass;
0422   string smin_run;
0423   string sn_run;
0424 
0425   if (argc != 6) {
0426     cout << "Usage:" << endl;
0427     cout << "  " << argv[0] << " <SID> <user> <pass> <min_run> <n_run>" << endl;
0428     exit(-1);
0429   }
0430 
0431 
0432   sid = argv[1];
0433   user = argv[2];
0434   pass = argv[3];
0435   smin_run = argv[4];
0436   int min_run=atoi(smin_run.c_str());
0437   sn_run = argv[5];
0438   int n_run=atoi(sn_run.c_str());
0439 
0440 
0441   try {
0442     CondDBApp app( sid, user, pass);
0443 
0444     int i= app.test(min_run, n_run);
0445     
0446     app.testRead(i);
0447 
0448     app.testWriteLUT();
0449     app.testWriteWeights();
0450   } catch (exception &e) {
0451     cout << "ERROR:  " << e.what() << endl;
0452   } catch (...) {
0453     cout << "Unknown error caught" << endl;
0454   }
0455 
0456   cout << "All Done." << endl;
0457 
0458   return 0;
0459 }