CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/CondTools/Ecal/src/EcalTPGLinConstHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGLinConstHandler.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/FEConfigLUTInfo.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 
00010 #include<iostream>
00011 
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::EcalTPGLinConstHandler::EcalTPGLinConstHandler(const edm::ParameterSet & ps)
00025   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGLinConstHandler")) {
00026 
00027         edm::LogInfo("EcalTPGLinConstHandler") << "EcalTPGLinConst 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("EcalTPGLinConstHandler") << m_sid <<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;   
00039 }
00040 
00041 popcon::EcalTPGLinConstHandler::~EcalTPGLinConstHandler()
00042 {
00043 }
00044 
00045 
00046 void popcon::EcalTPGLinConstHandler::getNewObjects()
00047 {
00048 
00049         edm::LogInfo("EcalTPGLinConstHandler") << "Started getNewObjects";
00050         
00051         if (tagInfo().size){
00052         //check whats already inside of database
00053           std::cout << "got offlineInfo = " << std::endl;
00054           std::cout << "tag name = " << tagInfo().name << std::endl;
00055           std::cout << "size = " << tagInfo().size <<  std::endl;
00056         } else {
00057           std::cout << " First object for this tag " << std::endl;
00058         }
00059         
00060         unsigned int max_since =0;
00061         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00062         edm::LogInfo("EcalTPGLinConstHandler") << "max_since = " << max_since;  
00063         edm::LogInfo("EcalTPGLinConstHandler")<< "Retrieved last payload ";
00064 
00065         // here we retrieve all the runs after the last from online DB 
00066         edm::LogInfo("EcalTPGLinConstHandler")<< "Retrieving run list from ONLINE DB ... " << std::endl;
00067 
00068         edm::LogInfo("EcalTPGLinConstHandler") << "Making connection..." << std::flush;
00069         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00070         edm::LogInfo("EcalTPGLinConstHandler") << "Done." << std::endl;
00071         
00072         if (!econn)
00073           {
00074             std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00075             //      cerr << e.what() << std::endl;
00076             throw cms::Exception("OMDS not available");
00077           } 
00078 
00079         
00080         LocationDef my_locdef;
00081         my_locdef.setLocation(m_location); 
00082 
00083         RunTypeDef my_rundef;
00084         my_rundef.setRunType(m_runtype); 
00085 
00086         RunTag  my_runtag;
00087         my_runtag.setLocationDef( my_locdef );
00088         my_runtag.setRunTypeDef(  my_rundef );
00089         my_runtag.setGeneralTag(m_gentag); 
00090          
00091         readFromFile("last_tpg_lin_settings.txt");
00092 
00093 
00094         unsigned int min_run=m_i_run_number+1;
00095         
00096         if(m_firstRun<m_i_run_number) {
00097           min_run=m_i_run_number+1; // we have to add 1 to the last transferred one
00098         } else {
00099           min_run=m_firstRun;
00100         }
00101 
00102         if(min_run<max_since) {
00103           min_run= max_since+1; // we have to add 1 to the last transferred one
00104         } 
00105 
00106         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00107 
00108         unsigned int max_run=m_lastRun;
00109         edm::LogInfo("EcalTPGLinConstHandler") << "min_run=  " << min_run << "max_run = " << max_run;
00110 
00111         RunList my_list;
00112         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00113         //      my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00114       
00115         std::vector<RunIOV> run_vec=  my_list.getRuns();
00116         size_t num_runs=run_vec.size();
00117 
00118         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00119         
00120         unsigned int irun=0; 
00121         if(num_runs>0){
00122 
00123           for(size_t kr=0; kr<run_vec.size(); kr++){
00124 
00125             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00126             std::cout<<" **************** "<<std::endl;
00127             std::cout<<" **************** "<<std::endl;
00128             std::cout<<" run= "<<irun<<std::endl;
00129 
00130             // retrieve the data :
00131             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00132             econn->fetchDataSet(&dataset, &run_vec[kr]);
00133             
00134             std::string the_config_tag="";
00135             int the_config_version=0;
00136             
00137             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00138             
00139             int nr=0;
00140             for( it=dataset.begin(); it!=dataset.end(); it++ )
00141             {
00142                 ++nr;
00143                 //EcalLogicID ecalid  = it->first;
00144 
00145                 RunTPGConfigDat  dat = it->second;
00146                 the_config_tag=dat.getConfigTag();
00147                 the_config_version=dat.getVersion();
00148             }   
00149 
00150 
00151             // it is all the same for all SM... get the last one 
00152 
00153 
00154             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00155 
00156             // here we should check if it is the same as previous run.
00157 
00158 
00159             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00160               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00161 
00162               FEConfigMainInfo fe_main_info;
00163               fe_main_info.setConfigTag(the_config_tag);
00164               fe_main_info.setVersion(the_config_version);
00165 
00166               try{ 
00167                 std::cout << " before fetch config set" << std::endl;       
00168                 econn-> fetchConfigSet(&fe_main_info);
00169                 std::cout << " after fetch config set" << std::endl;    
00170          
00171          
00172                 // now get TPGLinConst
00173                 int linId=fe_main_info.getLinId();
00174               
00175                 if( linId != m_i_lin ) {
00176               
00177                   FEConfigLinInfo fe_lin_info;
00178                   fe_lin_info.setId(linId);
00179                   econn-> fetchConfigSet(&fe_lin_info);
00180                   std::map<EcalLogicID, FEConfigLinDat> dataset_TpgLin;
00181                   econn->fetchDataSet(&dataset_TpgLin, &fe_lin_info);
00182 
00183                   EcalTPGLinearizationConst *linC = new EcalTPGLinearizationConst;
00184                   typedef std::map<EcalLogicID, FEConfigLinDat>::const_iterator CIfelin;
00185                   EcalLogicID ecid_xt;
00186                   FEConfigLinDat  rd_lin;
00187                   int icells=0;
00188         
00189                   for (CIfelin p = dataset_TpgLin.begin(); p != dataset_TpgLin.end(); p++) {
00190                     ecid_xt = p->first;
00191                     rd_lin  = p->second;
00192                     std::string ecid_name=ecid_xt.getName();
00193           
00194                     //EB data
00195                     if (ecid_name=="EB_crystal_number") {
00196                       int sm_num=ecid_xt.getID1();
00197                       int xt_num=ecid_xt.getID2();
00198                       EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
00199 
00200                       EcalTPGLinearizationConst::Item item;
00201                       item.mult_x1   =rd_lin.getMultX1() ;
00202                       item.mult_x6   =rd_lin.getMultX6();
00203                       item.mult_x12  =rd_lin.getMultX12();
00204                       item.shift_x1  =rd_lin.getShift1() ;
00205                       item.shift_x6  =rd_lin.getShift6();
00206                       item.shift_x12 =rd_lin.getShift12();
00207                   
00208                       linC->insert(std::make_pair(ebdetid.rawId(),item));
00209                       ++icells;
00210                     } 
00211                     else {
00212                       //EE data
00213                       int z=ecid_xt.getID1();
00214                       int x=ecid_xt.getID2();
00215                       int y=ecid_xt.getID3();
00216                       EEDetId eedetid(x,y,z,EEDetId::XYMODE);
00217           
00218                       EcalTPGLinearizationConst::Item item;
00219           
00220                       item.mult_x1   =rd_lin.getMultX1() ;
00221                       item.mult_x6   =rd_lin.getMultX6();
00222                       item.mult_x12  =rd_lin.getMultX12();
00223                       item.shift_x1  =rd_lin.getShift1() ;
00224                       item.shift_x6  =rd_lin.getShift6();
00225                       item.shift_x12 =rd_lin.getShift12();
00226 
00227                       linC->insert(std::make_pair(eedetid.rawId(),item));
00228                       ++icells;
00229                     }
00230                   } 
00231         
00232                   Time_t snc= (Time_t) irun ;           
00233                   m_to_transfer.push_back(std::make_pair((EcalTPGLinearizationConst*)linC,snc));
00234               
00235                   m_i_run_number=irun;
00236                   m_i_tag=the_config_tag;
00237                   m_i_version=the_config_version;
00238                   m_i_lin=linId;
00239                   
00240                   writeFile("last_tpg_lin_settings.txt");
00241 
00242                 } else {    
00243                   
00244                   m_i_run_number=irun;
00245                   m_i_tag=the_config_tag;
00246                   m_i_version=the_config_version;
00247 
00248                   writeFile("last_tpg_lin_settings.txt");
00249 
00250                   std::cout<< " even if the tag/version is not the same, the linearization constants id is the same -> no transfer needed "<< std::endl; 
00251 
00252                 }
00253               }       catch (std::exception &e) { 
00254                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00255                           <<" version="<<the_config_version<< std::endl;
00256                 std::cout << e.what() << std::endl;
00257                 m_i_run_number=irun;
00258 
00259               }
00260               std::cout<<" **************** "<<std::endl;
00261               
00262             } else if(nr==0) {
00263               m_i_run_number=irun;
00264               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00265               std::cout<<" **************** "<<std::endl;
00266             } else {
00267               m_i_run_number=irun;
00268               m_i_tag=the_config_tag;
00269               m_i_version=the_config_version;
00270               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00271               std::cout<<" **************** "<<std::endl;
00272               writeFile("last_tpg_lin_settings.txt");
00273             }            
00274               
00275           }
00276         }
00277           
00278         delete econn;
00279         
00280         edm::LogInfo("EcalTPGLinConstHandler") << "Ecal - > end of getNewObjects -----------";  
00281 }
00282 
00283 void  popcon::EcalTPGLinConstHandler::readFromFile(const char* inputFile) {
00284   //-------------------------------------------------------------
00285   
00286   m_i_tag="";
00287   m_i_version=0;
00288   m_i_run_number=0;
00289   m_i_lin=0; 
00290 
00291   FILE *inpFile; // input file
00292   inpFile = fopen(inputFile,"r");
00293   if(!inpFile) {
00294     edm::LogError("EcalTPGLinConstHandler")<<"*** Can not open file: "<<inputFile;
00295   }
00296 
00297   char line[256];
00298     
00299   std::ostringstream str;
00300 
00301   fgets(line,255,inpFile);
00302   m_i_tag=to_string(line);
00303   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00304 
00305   fgets(line,255,inpFile);
00306   m_i_version=atoi(line);
00307   str << "version= " << m_i_version << std::endl ;  
00308 
00309   fgets(line,255,inpFile);
00310   m_i_run_number=atoi(line);
00311   str << "run_number= " << m_i_run_number << std::endl ;  
00312 
00313   fgets(line,255,inpFile);
00314   m_i_lin=atoi(line);
00315   str << "lin_config= " << m_i_lin << std::endl ;  
00316 
00317     
00318   fclose(inpFile);           // close inp. file
00319 
00320 }
00321 
00322 void  popcon::EcalTPGLinConstHandler::writeFile(const char* inputFile) {
00323   //-------------------------------------------------------------
00324   
00325   
00326   std::ofstream myfile;
00327   myfile.open (inputFile);
00328   myfile << m_i_tag <<std::endl;
00329   myfile << m_i_version <<std::endl;
00330   myfile << m_i_run_number <<std::endl;
00331   myfile << m_i_lin <<std::endl;
00332 
00333   myfile.close();
00334 
00335 }