CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/CondTools/Ecal/src/EcalTPGFineGrainEBIdMapHandler.cc

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