CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CondTools/Ecal/src/EcalTPGWeightIdMapHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
00002 
00003 #include "CondTools/Ecal/interface/EcalTPGWeightIdMapHandler.h"
00004 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00005 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
00007 #include "OnlineDB/EcalCondDB/interface/FEConfigWeightInfo.h"
00008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 #include<iostream>
00012 #include<fstream>
00013 
00014 
00015 #include <time.h>
00016 #include <unistd.h>
00017 
00018 #include <string>
00019 #include <cstdio>
00020 #include <typeinfo>
00021 #include <sstream>
00022 
00023 
00024 popcon::EcalTPGWeightIdMapHandler::EcalTPGWeightIdMapHandler(const edm::ParameterSet & ps)
00025   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGWeightIdMapHandler")) {
00026 
00027         edm::LogInfo("EcalTPGWeightIdMapHandler") << "EcalTPGWeightIdMap Source handler constructor";
00028         m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
00029         m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
00030         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00031         m_user= ps.getParameter<std::string>("OnlineDBUser");
00032         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00033         m_locationsource= ps.getParameter<std::string>("LocationSource");
00034         m_location=ps.getParameter<std::string>("Location");
00035         m_gentag=ps.getParameter<std::string>("GenTag");
00036         m_runtype=ps.getParameter<std::string>("RunType");
00037 
00038         edm::LogInfo("EcalTPGWeightIdMapHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00039 
00040 }
00041 
00042 popcon::EcalTPGWeightIdMapHandler::~EcalTPGWeightIdMapHandler()
00043 {
00044 }
00045 
00046 
00047 void popcon::EcalTPGWeightIdMapHandler::getNewObjects()
00048 {
00049 
00050         edm::LogInfo("EcalTPGWeightIdMapHandler") << "Started GetNewObjects!!!";
00051 
00052         //check whats already inside of database
00053         if (tagInfo().size){
00054         //check whats already inside of database
00055         std::cout << "got offlineInfo = " << std::endl;
00056         std::cout << "tag name = " << tagInfo().name << std::endl;
00057         std::cout << "size = " << tagInfo().size <<  std::endl;
00058         } else {
00059         std::cout << " First object for this tag " << std::endl;
00060         }
00061 
00062         unsigned int max_since=0;
00063         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00064         edm::LogInfo("EcalTPGWeightIdMapHandler") << "max_since : "  << max_since;
00065         Ref weightIdMap_db = lastPayload();
00066         
00067         edm::LogInfo("EcalTPGWeightIdMapHandler") << "retrieved last payload ";
00068 
00069         // here we retrieve all the runs after the last from online DB 
00070         edm::LogInfo("EcalTPGWeightIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
00071 
00072         edm::LogInfo("EcalTPGWeightIdMapHandler") << "Making connection...";
00073         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00074         edm::LogInfo("EcalTPGWeightIdMapHandler") << "Done.";
00075         
00076         if (!econn)
00077           {
00078             std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00079             //      cerr << e.what() << std::endl;
00080             throw cms::Exception("OMDS not available");
00081           } 
00082 
00083         
00084         LocationDef my_locdef;
00085         my_locdef.setLocation(m_location); 
00086 
00087         RunTypeDef my_rundef;
00088         my_rundef.setRunType(m_runtype); 
00089 
00090         RunTag  my_runtag;
00091         my_runtag.setLocationDef( my_locdef );
00092         my_runtag.setRunTypeDef(  my_rundef );
00093         my_runtag.setGeneralTag(m_gentag); 
00094 
00095         readFromFile("last_tpg_weightIdMap_settings.txt");
00096 
00097 
00098         unsigned int min_run=m_i_run_number+1;
00099 
00100         if(m_firstRun<m_i_run_number) {
00101           min_run=m_i_run_number+1;
00102         } else {
00103           min_run=m_firstRun;
00104         }
00105         
00106         if(min_run<max_since) {
00107           min_run=max_since+1; // we have to add 1 to the last transferred one
00108         } 
00109 
00110         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00111 
00112         unsigned int max_run=m_lastRun;
00113         edm::LogInfo("EcalTPGWeightIdMapHandler") << "min_run= " << min_run << "max_run= " << max_run;
00114 
00115         RunList my_list;
00116         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00117         //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
00118       
00119         std::vector<RunIOV> run_vec=  my_list.getRuns();
00120         size_t num_runs=run_vec.size();
00121         edm::LogInfo("EcalTPGWeightIdMapHandler") << "number of Mon runs is : "<< num_runs;
00122 
00123         unsigned int irun;
00124         if(num_runs>0){
00125           for(size_t kr=0; kr<run_vec.size(); kr++){
00126 
00127             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00128 
00129             std::cout<<" **************** "<<std::endl;
00130             std::cout<<" **************** "<<std::endl;
00131             std::cout<<" run= "<<irun<<std::endl;
00132           
00133             // retrieve the data :
00134             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00135             econn->fetchDataSet(&dataset, &run_vec[kr]);
00136                 
00137             std::string the_config_tag="";
00138             int the_config_version=0;
00139                 
00140             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00141                 
00142             int nr=0;
00143             for( it=dataset.begin(); it!=dataset.end(); it++ )
00144             {
00145               ++nr;
00146               //EcalLogicID ecalid  = it->first;
00147               RunTPGConfigDat  dat = it->second;
00148               the_config_tag=dat.getConfigTag();
00149               the_config_version=dat.getVersion();
00150             }
00151                   
00152                   
00153             // it is all the same for all SM... get the last one 
00154 
00155 
00156             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00157 
00158             // here we should check if it is the same as previous run.
00159 
00160 
00161             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00162               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00163 
00164               FEConfigMainInfo fe_main_info;
00165               fe_main_info.setConfigTag(the_config_tag);
00166               fe_main_info.setVersion(the_config_version);
00167 
00168               try{ 
00169                 std::cout << " before fetch config set" << std::endl;       
00170                 econn-> fetchConfigSet(&fe_main_info);
00171                 std::cout << " after fetch config set" << std::endl;        
00172 
00173 
00174                 // now get TPGWeightIdMap
00175                 int weightId=fe_main_info.getWeiId();
00176                 
00177                 if( weightId != m_i_weightIdMap ) {
00178                 
00179                   FEConfigWeightInfo fe_weight_info;
00180                   fe_weight_info.setId(weightId);
00181                   econn-> fetchConfigSet(&fe_weight_info);
00182                   std::map<EcalLogicID, FEConfigWeightGroupDat> dataset_TpgWeight;
00183                   econn->fetchDataSet(&dataset_TpgWeight, &fe_weight_info);
00184                   edm::LogInfo("EcalTPGWeightIdMapHandler") << "Got object!";
00185                   EcalTPGWeightIdMap* weightMap = new EcalTPGWeightIdMap;
00186                   typedef std::map<EcalLogicID, FEConfigWeightGroupDat>::const_iterator CIfeweight;
00187                   EcalLogicID ecid_xt;
00188                   FEConfigWeightGroupDat  rd_w;
00189 
00190                   int igroups=0;
00191                   for (CIfeweight p = dataset_TpgWeight.begin(); p != dataset_TpgWeight.end(); p++) {
00192                 
00193                   rd_w =  p->second;
00194                   // EB and EE data 
00195                   EcalTPGWeights w;             
00196                   unsigned int weight0 = static_cast<unsigned int>(rd_w.getWeight4());
00197                   unsigned int weight1 = static_cast<unsigned int>(rd_w.getWeight3());
00198                   unsigned int weight2 = static_cast<unsigned int>(rd_w.getWeight2());
00199                   unsigned int weight3 = static_cast<unsigned int>(rd_w.getWeight1()- 0x80);
00200                   unsigned int weight4 = static_cast<unsigned int>(rd_w.getWeight0());
00201           
00202                   w.setValues(weight0,weight1,weight2,weight3,weight4);
00203                   weightMap->setValue(rd_w.getWeightGroupId(),w);
00204           
00205                   ++igroups;
00206                 }
00207         
00208                 edm::LogInfo("EcalTPGWeightIdMapHandler") << "found " << igroups << "Weight groups";
00209 
00210                 Time_t snc= (Time_t) irun;            
00211                 m_to_transfer.push_back(std::make_pair((EcalTPGWeightIdMap*)weightMap,snc));
00212                           
00213                 m_i_run_number=irun;
00214                 m_i_tag=the_config_tag;
00215                 m_i_version=the_config_version;
00216                 m_i_weightIdMap=weightId;
00217                   
00218                 writeFile("last_tpg_weightIdMap_settings.txt");
00219 
00220                 } else {
00221 
00222                   m_i_run_number=irun;
00223                   m_i_tag=the_config_tag;
00224                   m_i_version=the_config_version;
00225 
00226                   writeFile("last_tpg_weightIdMap_settings.txt");
00227 
00228                   std::cout<< " even if the tag/version is not the same, the weightIdMap id is the same -> no transfer needed "<< std::endl; 
00229 
00230                 }
00231 
00232               }       catch (std::exception &e) { 
00233                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00234                           <<" version="<<the_config_version<< std::endl;
00235                 std::cout << e.what() << std::endl;
00236                 m_i_run_number=irun;
00237 
00238               }
00239               std::cout<<" **************** "<<std::endl;
00240               
00241             } else if(nr==0) {
00242               m_i_run_number=irun;
00243               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00244               std::cout<<" **************** "<<std::endl;
00245             } else {
00246               m_i_run_number=irun;
00247               m_i_tag=the_config_tag;
00248               m_i_version=the_config_version;
00249               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00250               std::cout<<" **************** "<<std::endl;
00251               writeFile("last_tpg_weightIdMap_settings.txt");
00252             }
00253             
00254           }
00255         }
00256           
00257         delete econn;
00258 
00259         edm::LogInfo("EcalTPGWeightIdMapHandler") << "Ecal - > end of getNewObjects -----------";
00260         
00261 }
00262 
00263 void  popcon::EcalTPGWeightIdMapHandler::readFromFile(const char* inputFile) {
00264   //-------------------------------------------------------------
00265   
00266   m_i_tag="";
00267   m_i_version=0;
00268   m_i_run_number=0;
00269   m_i_weightIdMap=0; 
00270 
00271   FILE *inpFile; // input file
00272   inpFile = fopen(inputFile,"r");
00273   if(!inpFile) {
00274     edm::LogError("EcalTPGWeightIdMapHandler")<<"*** Can not open file: "<<inputFile;
00275   }
00276 
00277   char line[256];
00278     
00279   std::ostringstream str;
00280 
00281   fgets(line,255,inpFile);
00282   m_i_tag=to_string(line);
00283   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00284 
00285   fgets(line,255,inpFile);
00286   m_i_version=atoi(line);
00287   str << "version= " << m_i_version << std::endl ;  
00288 
00289   fgets(line,255,inpFile);
00290   m_i_run_number=atoi(line);
00291   str << "run_number= " << m_i_run_number << std::endl ;  
00292 
00293   fgets(line,255,inpFile);
00294   m_i_weightIdMap=atoi(line);
00295   str << "weightIdMap_config= " << m_i_weightIdMap << std::endl ;  
00296 
00297     
00298   fclose(inpFile);           // close inp. file
00299 
00300 }
00301 
00302 void  popcon::EcalTPGWeightIdMapHandler::writeFile(const char* inputFile) {
00303   //-------------------------------------------------------------
00304   
00305   
00306   std::ofstream myfile;
00307   myfile.open (inputFile);
00308   myfile << m_i_tag <<std::endl;
00309   myfile << m_i_version <<std::endl;
00310   myfile << m_i_run_number <<std::endl;
00311   myfile << m_i_weightIdMap <<std::endl;
00312 
00313   myfile.close();
00314 
00315 }