CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CondTools/Ecal/src/EcalTPGLutIdMapHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
00002 
00003 #include "CondTools/Ecal/interface/EcalTPGLutIdMapHandler.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/FEConfigLUTInfo.h"
00008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00009 
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 
00012 #include<iostream>
00013 #include<fstream>
00014 
00015 
00016 #include <time.h>
00017 #include <unistd.h>
00018 
00019 #include <string>
00020 #include <cstdio>
00021 #include <typeinfo>
00022 #include <sstream>
00023 
00024 popcon::EcalTPGLutIdMapHandler::EcalTPGLutIdMapHandler(const edm::ParameterSet & ps)
00025   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGLutIdMapHandler")) {
00026 
00027         edm::LogInfo("EcalTPGLutIdMapHandler") << "EcalTPGLutIdMap Source handler constructor";
00028         m_firstRun=(unsigned int)atoi( ps.getParameter<std::string>("firstRun").c_str());
00029         m_lastRun=(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("EcalTPGLutIdMapHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00039 
00040 
00041 }
00042 
00043 popcon::EcalTPGLutIdMapHandler::~EcalTPGLutIdMapHandler()
00044 {
00045 }
00046 
00047 
00048 void popcon::EcalTPGLutIdMapHandler::getNewObjects()
00049 {
00050         edm::LogInfo("EcalTPGLutIdMapHandler") << "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("EcalTPGLutIdMapHandler") << "max_since : "  << max_since;
00065         edm::LogInfo("EcalTPGLutIdMapHandler") << "retrieved last payload ";
00066 
00067         // here we retrieve all the runs after the last from online DB 
00068         edm::LogInfo("EcalTPGLutIdMapHandler") << "Retrieving run list from ONLINE DB ... ";
00069 
00070         edm::LogInfo("EcalTPGLutIdMapHandler") << "Making connection...";
00071         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00072         edm::LogInfo("EcalTPGLutIdMapHandler") << "Done.";
00073         
00074         if (!econn)
00075           {
00076             std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00077             //      cerr << e.what() << std::endl;
00078             throw cms::Exception("OMDS not available");
00079           } 
00080 
00081         
00082         LocationDef my_locdef;
00083         my_locdef.setLocation(m_location); 
00084 
00085         RunTypeDef my_rundef;
00086         my_rundef.setRunType(m_runtype); 
00087 
00088         RunTag  my_runtag;
00089         my_runtag.setLocationDef( my_locdef );
00090         my_runtag.setRunTypeDef(  my_rundef );
00091         my_runtag.setGeneralTag(m_gentag); 
00092 
00093 
00094         readFromFile("last_tpg_lutIdMap_settings.txt");
00095 
00096 
00097         unsigned int min_run=m_i_run_number+1;
00098 
00099         if(m_firstRun<m_i_run_number) {
00100           min_run=m_i_run_number+1;
00101         } else {
00102           min_run=m_firstRun;
00103         }
00104         
00105         if(min_run<max_since) {
00106           min_run=max_since+1; // we have to add 1 to the last transferred one
00107         }
00108         
00109         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00110 
00111 
00112         unsigned int max_run=m_lastRun;
00113         edm::LogInfo("EcalTPGLutIdMapHandler") << "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 
00122         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00123         
00124         unsigned int irun;
00125         if(num_runs>0){
00126 
00127           for(size_t kr=0; kr<run_vec.size(); kr++){
00128 
00129             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00130 
00131             std::cout<<" **************** "<<std::endl;
00132             std::cout<<" **************** "<<std::endl;
00133             std::cout<<" run= "<<irun<<std::endl;
00134 
00135             // retrieve the data :
00136             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00137             econn->fetchDataSet(&dataset, &run_vec[kr]);
00138                 
00139             std::string the_config_tag="";
00140             int the_config_version=0;
00141                 
00142             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00143         
00144             int nr=0;
00145             for( it=dataset.begin(); it!=dataset.end(); it++ )
00146             {
00147               ++nr;
00148               EcalLogicID ecalid  = it->first;
00149               RunTPGConfigDat  dat = it->second;
00150               the_config_tag=dat.getConfigTag();
00151               the_config_version=dat.getVersion();
00152             }
00153                 
00154                   
00155             // it is all the same for all SM... get the last one 
00156 
00157 
00158             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00159 
00160             // here we should check if it is the same as previous run.
00161 
00162 
00163                 if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00164                   std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00165 
00166                   FEConfigMainInfo fe_main_info;
00167                   fe_main_info.setConfigTag(the_config_tag);
00168                   fe_main_info.setVersion(the_config_version);
00169 
00170                   try{ 
00171                     std::cout << " before fetch config set" << std::endl;           
00172                     econn-> fetchConfigSet(&fe_main_info);
00173                     std::cout << " after fetch config set" << std::endl;            
00174 
00175 
00176                     // now get TPGLutIdMap
00177                     int lutId=fe_main_info.getLUTId();
00178         
00179                     if( lutId != m_i_lutIdMap ) {
00180                 
00181                       FEConfigLUTInfo fe_lut_info;
00182                       fe_lut_info.setId(lutId);
00183                       econn-> fetchConfigSet(&fe_lut_info);
00184                       std::map<EcalLogicID, FEConfigLUTGroupDat> dataset_TpgLut;
00185         
00186                       econn->fetchDataSet(&dataset_TpgLut, &fe_lut_info);
00187                       edm::LogInfo("EcalTPGLutIdMapHandler") << "Got object!";
00188         
00189                       EcalTPGLutIdMap* lutMap = new EcalTPGLutIdMap;
00190         
00191                       typedef std::map<EcalLogicID, FEConfigLUTGroupDat>::const_iterator CIfelut;
00192                       EcalLogicID ecid_xt;
00193                       FEConfigLUTGroupDat  rd_lut;
00194                       int igroups=0;
00195         
00196                       for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) 
00197                       {
00198                         ecid_xt = p->first;
00199                         rd_lut  = p->second;
00200 
00201                         //std::string ecid_name=ecid_xt.getName();
00202           
00203                         unsigned int lutArray[1024] ;
00204                         for (int ilut=0;ilut<1024;++ilut) {
00205                           lutArray[ilut]=rd_lut.getLUTValue(ilut);
00206                           std::cout << ilut << "  " << rd_lut.getLUTValue(ilut)<< std::endl;
00207                         }
00208           
00209                         EcalTPGLut mylut;
00210                         mylut.setLut(lutArray); 
00211                         lutMap->setValue(rd_lut.getLUTGroupId(),mylut);
00212                         ++igroups;
00213           
00214                       }
00215         
00216                       Time_t snc= (Time_t) irun ;
00217                       m_to_transfer.push_back(std::make_pair((EcalTPGLutIdMap*)lutMap,snc));
00218           
00219                       m_i_run_number=irun;
00220                       m_i_tag=the_config_tag;
00221                       m_i_version=the_config_version;
00222                       m_i_lutIdMap=lutId;
00223                   
00224                       writeFile("last_tpg_lutIdMap_settings.txt");
00225 
00226                     } else {
00227 
00228                       m_i_run_number=irun;
00229                       m_i_tag=the_config_tag;
00230                       m_i_version=the_config_version;
00231 
00232                       writeFile("last_tpg_lutIdMap_settings.txt");
00233 
00234                       std::cout<< " even if the tag/version is not the same, the lutIdMap id is the same -> no transfer needed "<< std::endl; 
00235 
00236                     }
00237 
00238               }       
00239               
00240               catch (std::exception &e) { 
00241                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00242                           <<" version="<<the_config_version<< std::endl;
00243                 std::cout << e.what() << std::endl;
00244                 m_i_run_number=irun;
00245 
00246               }
00247               std::cout<<" **************** "<<std::endl;
00248               
00249             } else if(nr==0) {
00250               m_i_run_number=irun;
00251               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00252               std::cout<<" **************** "<<std::endl;
00253             } else {
00254               m_i_run_number=irun;
00255               m_i_tag=the_config_tag;
00256               m_i_version=the_config_version;
00257               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00258               std::cout<<" **************** "<<std::endl;
00259               writeFile("last_tpg_lutIdMap_settings.txt");
00260             }
00261           
00262           }
00263         }
00264           
00265         delete econn;
00266 
00267         edm::LogInfo("EcalTPGLutIdMapHandler") << "Ecal - > end of getNewObjects -----------";
00268         
00269 }
00270 
00271 
00272 void  popcon::EcalTPGLutIdMapHandler::readFromFile(const char* inputFile) {
00273   //-------------------------------------------------------------
00274   
00275   m_i_tag="";
00276   m_i_version=0;
00277   m_i_run_number=0;
00278   m_i_lutIdMap=0; 
00279 
00280   FILE *inpFile; // input file
00281   inpFile = fopen(inputFile,"r");
00282   if(!inpFile) {
00283     edm::LogError("EcalTPGLutIdMapHandler")<<"*** Can not open file: "<<inputFile;
00284   }
00285 
00286   char line[256];
00287     
00288   std::ostringstream str;
00289 
00290   fgets(line,255,inpFile);
00291   m_i_tag=to_string(line);
00292   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00293 
00294   fgets(line,255,inpFile);
00295   m_i_version=atoi(line);
00296   str << "version= " << m_i_version << std::endl ;  
00297 
00298   fgets(line,255,inpFile);
00299   m_i_run_number=atoi(line);
00300   str << "run_number= " << m_i_run_number << std::endl ;  
00301 
00302   fgets(line,255,inpFile);
00303   m_i_lutIdMap=atoi(line);
00304   str << "lutIdMap_config= " << m_i_lutIdMap << std::endl ;  
00305 
00306     
00307   fclose(inpFile);           // close inp. file
00308 
00309 }
00310 
00311 void  popcon::EcalTPGLutIdMapHandler::writeFile(const char* inputFile) {
00312   //-------------------------------------------------------------
00313   
00314   
00315   std::ofstream myfile;
00316   myfile.open (inputFile);
00317   myfile << m_i_tag <<std::endl;
00318   myfile << m_i_version <<std::endl;
00319   myfile << m_i_run_number <<std::endl;
00320   myfile << m_i_lutIdMap <<std::endl;
00321 
00322   myfile.close();
00323 
00324 }