CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/CondTools/Ecal/src/EcalTPGWeightGroupHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalLaserHandler.h"
00002 
00003 #include "CondTools/Ecal/interface/EcalTPGWeightGroupHandler.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/FEConfigSlidingInfo.h"
00008 #include "OnlineDB/EcalCondDB/interface/FEConfigSlidingDat.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 
00012 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00013 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
00014 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
00015 #include "FWCore/Framework/interface/eventsetuprecord_registration_macro.h"
00016 
00017 #include<iostream>
00018 #include<fstream>
00019 
00020 
00021 #include <time.h>
00022 #include <unistd.h>
00023 
00024 #include <string>
00025 #include <cstdio>
00026 #include <typeinfo>
00027 #include <sstream>
00028 
00029 
00030 popcon::EcalTPGWeightGroupHandler::EcalTPGWeightGroupHandler(const edm::ParameterSet & ps)
00031   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGWeightGroupHandler")) {
00032 
00033         edm::LogInfo("EcalTPGWeightGroupHandler") << "EcalTPGWeightGroup Source handler constructor.";
00034         m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
00035         m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
00036         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00037         m_user= ps.getParameter<std::string>("OnlineDBUser");
00038         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00039         m_locationsource= ps.getParameter<std::string>("LocationSource");
00040         m_location=ps.getParameter<std::string>("Location");
00041         m_gentag=ps.getParameter<std::string>("GenTag");
00042         m_runtype=ps.getParameter<std::string>("RunType");
00043 
00044         edm::LogInfo("EcalTPGWeightGroupHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00045 }
00046 
00047 popcon::EcalTPGWeightGroupHandler::~EcalTPGWeightGroupHandler()
00048 {
00049 }
00050 
00051 void popcon::EcalTPGWeightGroupHandler::getNewObjects()
00052 {
00053 
00054         edm::LogInfo("EcalTPGWeightGroupHandler") << "Started GetNewObjects!!!";
00055 
00056         //check whats already inside of database
00057         if (tagInfo().size){
00058         //check whats already inside of database
00059         std::cout << "got offlineInfo = " << std::endl;
00060         std::cout << "tag name = " << tagInfo().name << std::endl;
00061         std::cout << "size = " << tagInfo().size <<  std::endl;
00062         } else {
00063         std::cout << " First object for this tag " << std::endl;
00064         }
00065 
00066         unsigned int max_since=0;
00067         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00068         edm::LogInfo("EcalTPGWeightGroupHandler") << "max_since : "  << max_since;
00069         
00070         edm::LogInfo("EcalTPGWeightGroupHandler") << "retrieved last payload ";
00071 
00072         // here we retrieve all the runs after the last from online DB 
00073 
00074         edm::LogInfo("EcalTPGWeightGroupHandler") << "Retrieving run list from ONLINE DB ... ";
00075 
00076         edm::LogInfo("EcalTPGWeightGroupHandler") << "Making connection...";
00077         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00078         edm::LogInfo("EcalTPGWeightGroupHandler") << "Done.";
00079         
00080         if (!econn)
00081           {
00082             std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00083             //      cerr << e.what() << std::endl;
00084             throw cms::Exception("OMDS not available");
00085           } 
00086 
00087         
00088         LocationDef my_locdef;
00089         my_locdef.setLocation(m_location); 
00090 
00091         RunTypeDef my_rundef;
00092         my_rundef.setRunType(m_runtype); 
00093 
00094         RunTag  my_runtag;
00095         my_runtag.setLocationDef( my_locdef );
00096         my_runtag.setRunTypeDef(  my_rundef );
00097         my_runtag.setGeneralTag(m_gentag); 
00098 
00099         readFromFile("last_tpg_weightGroup_settings.txt");
00100 
00101         unsigned int min_run=m_i_run_number+1;
00102 
00103         if(m_firstRun<m_i_run_number) {
00104           min_run=m_i_run_number+1;
00105         } else {
00106           min_run=m_firstRun;
00107         }
00108         
00109         if(min_run<max_since) {
00110           min_run=max_since+1; // we have to add 1 to the last transferred one
00111         }
00112 
00113         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00114 
00115         unsigned int max_run=m_lastRun;
00116         edm::LogInfo("EcalTPGWeightGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
00117 
00118         RunList my_list;
00119         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00120         //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
00121       
00122         std::vector<RunIOV> run_vec=  my_list.getRuns();
00123         size_t num_runs=run_vec.size();
00124 
00125         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00126         
00127         unsigned int irun=0;
00128         if(num_runs>0){
00129         
00130           // going to query the ecal logic id 
00131           std::vector<EcalLogicID> my_StripEcalLogicId_EE;
00132           my_StripEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "ECAL_readout_strip",
00133                                                     1, 2000,
00134                                                     1, 70,
00135                                                     0,5,
00136                                                     "EE_offline_stripid",123 );
00137           
00138           std::cout <<" GOT the logic ID for the EE trigger strips "<< std::endl;
00139 
00140           for(size_t kr=0; kr<run_vec.size(); kr++){
00141 
00142             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00143 
00144             std::cout<<" **************** "<<std::endl;
00145             std::cout<<" **************** "<<std::endl;
00146             std::cout<<" run= "<<irun<<std::endl;
00147 
00148             // retrieve the data :
00149             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00150             econn->fetchDataSet(&dataset, &run_vec[kr]);
00151             
00152             std::string the_config_tag="";
00153             int the_config_version=0;
00154             
00155             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00156             
00157             int nr=0;
00158             for( it=dataset.begin(); it!=dataset.end(); it++ )
00159               {
00160                 ++nr;
00161                 //EcalLogicID ecalid  = it->first;
00162 
00163                 RunTPGConfigDat  dat = it->second;
00164                 the_config_tag=dat.getConfigTag();
00165                 the_config_version=dat.getVersion();
00166             }
00167             
00168             // it is all the same for all SM... get the last one 
00169 
00170 
00171             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00172 
00173             // here we should check if it is the same as previous run.
00174 
00175 
00176             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00177               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00178 
00179               FEConfigMainInfo fe_main_info;
00180               fe_main_info.setConfigTag(the_config_tag);
00181               fe_main_info.setVersion(the_config_version);
00182 
00183               try{ 
00184                 std::cout << " before fetch config set" << std::endl;       
00185                 econn-> fetchConfigSet(&fe_main_info);
00186                 std::cout << " after fetch config set" << std::endl;        
00187 
00188 
00189                 // now get TPGWeightGroup
00190                 int wId=fe_main_info.getWeiId();
00191             
00192                 if( wId != m_i_weightGroup ) {
00193             
00194                   FEConfigWeightInfo fe_w_info;
00195                   fe_w_info.setId(wId);
00196                   econn-> fetchConfigSet(&fe_w_info);
00197                   std::map<EcalLogicID, FEConfigWeightDat> dataset_TpgW;
00198                   econn->fetchDataSet(&dataset_TpgW, &fe_w_info);
00199 
00200 
00201                   EcalTPGWeightGroup* weightG = new EcalTPGWeightGroup;
00202                   typedef std::map<EcalLogicID, FEConfigWeightDat>::const_iterator CIfesli;
00203                   EcalLogicID ecid_xt;
00204                   int weightGroup;
00205                   int icells=0;
00206             
00207                   std::map<std::string,int> map;
00208                   std::string str;
00209 
00210                   for (CIfesli p = dataset_TpgW.begin(); p != dataset_TpgW.end(); p++) {
00211                     ecid_xt = p->first;
00212                     weightGroup  = p->second.getWeightGroupId();
00213               
00214                     std::string ecid_name=ecid_xt.getName();
00215               
00216                     // EB data
00217                     if (ecid_name=="EB_VFE") {
00218                       int sm=ecid_xt.getID1();
00219                       int tt=ecid_xt.getID2();
00220                       int strip=ecid_xt.getID3();
00221                       int tcc= sm+54;
00222                       if(sm>18) tcc=sm+18 ;
00223 
00224                       // simple formula to calculate the Srip EB identifier 
00225                                 
00226                       unsigned int stripEBId = 303176+(tt-1)*64+(strip-1)*8+(tcc-37)*8192;
00227                                 
00228                       weightG->setValue(stripEBId,weightGroup);
00229                       ++icells;
00230                     }
00231                     else if (ecid_name=="ECAL_readout_strip"){
00232                       // EE data to add
00233                       int id1=ecid_xt.getID1();
00234                       int id2=ecid_xt.getID2();
00235                       int id3=ecid_xt.getID3(); 
00236                       
00237                       bool set_the_strip=false;
00238                       int stripEEId;
00239                       for (size_t istrip=0; istrip<my_StripEcalLogicId_EE.size(); istrip++) {
00240 
00241                         if(!set_the_strip){
00242                           
00243                           if(my_StripEcalLogicId_EE[istrip].getID1()==id1 
00244                              && my_StripEcalLogicId_EE[istrip].getID2()==id2
00245                              && my_StripEcalLogicId_EE[istrip].getID3()==id3 
00246                              ){
00247                             stripEEId =my_StripEcalLogicId_EE[istrip].getLogicID();
00248                             set_the_strip=true;
00249                             break;
00250                           }
00251                         }
00252                         
00253                       }
00254                       
00255                       if(set_the_strip){
00256                       
00257                         weightG->setValue(stripEEId,weightGroup);
00258                       
00259                       } else {
00260                         std::cout <<" these may be the additional towers TCC/TT "
00261                                   << id1<<"/"<<id2<<std::endl;
00262                       }
00263                         
00264                       ++icells;    
00265                     }
00266                   } 
00267             
00268                   Time_t snc= (Time_t) irun ;
00269                       
00270                   m_to_transfer.push_back(std::make_pair((EcalTPGWeightGroup*)weightG,snc));
00271             
00272                   m_i_run_number=irun;
00273                   m_i_tag=the_config_tag;
00274                   m_i_version=the_config_version;
00275                   m_i_weightGroup=wId;
00276                   
00277                   writeFile("last_tpg_weightGroup_settings.txt");
00278 
00279                 } else {
00280 
00281                   m_i_run_number=irun;
00282                   m_i_tag=the_config_tag;
00283                   m_i_version=the_config_version;
00284 
00285                   writeFile("last_tpg_weightGroup_settings.txt");
00286 
00287                   std::cout<< " even if the tag/version is not the same, the weight group id is the same -> no transfer needed "<< std::endl; 
00288 
00289                 }
00290 
00291               }       
00292               
00293               catch (std::exception &e) { 
00294                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00295                           <<" version="<<the_config_version<< std::endl;
00296                 std::cout << e.what() << std::endl;
00297                 m_i_run_number=irun;
00298 
00299               }
00300               std::cout<<" **************** "<<std::endl;
00301               
00302             } else if(nr==0) {
00303               m_i_run_number=irun;
00304               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00305               std::cout<<" **************** "<<std::endl;
00306             } else {
00307               m_i_run_number=irun;
00308               m_i_tag=the_config_tag;
00309               m_i_version=the_config_version;
00310               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00311               std::cout<<" **************** "<<std::endl;
00312               writeFile("last_tpg_weightGroup_settings.txt");
00313             }
00314 
00315           }
00316         }
00317           
00318         delete econn;
00319 
00320         edm::LogInfo("EcalTPGWeightGroupHandler") << "Ecal - > end of getNewObjects -----------";
00321         
00322 }
00323 
00324 void  popcon::EcalTPGWeightGroupHandler::readFromFile(const char* inputFile) {
00325   //-------------------------------------------------------------
00326   
00327   m_i_tag="";
00328   m_i_version=0;
00329   m_i_run_number=0;
00330   m_i_weightGroup=0; 
00331 
00332   FILE *inpFile; // input file
00333   inpFile = fopen(inputFile,"r");
00334   if(!inpFile) {
00335     edm::LogError("EcalTPGWeightGroupHandler")<<"*** Can not open file: "<<inputFile;
00336   }
00337 
00338   char line[256];
00339     
00340   std::ostringstream str;
00341 
00342   fgets(line,255,inpFile);
00343   m_i_tag=to_string(line);
00344   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00345 
00346   fgets(line,255,inpFile);
00347   m_i_version=atoi(line);
00348   str << "version= " << m_i_version << std::endl ;  
00349 
00350   fgets(line,255,inpFile);
00351   m_i_run_number=atoi(line);
00352   str << "run_number= " << m_i_run_number << std::endl ;  
00353 
00354   fgets(line,255,inpFile);
00355   m_i_weightGroup=atoi(line);
00356   str << "weightGroup_config= " << m_i_weightGroup << std::endl ;  
00357 
00358     
00359   fclose(inpFile);           // close inp. file
00360 
00361 }
00362 
00363 void  popcon::EcalTPGWeightGroupHandler::writeFile(const char* inputFile) {
00364   //-------------------------------------------------------------
00365   
00366   
00367   std::ofstream myfile;
00368   myfile.open (inputFile);
00369   myfile << m_i_tag <<std::endl;
00370   myfile << m_i_version <<std::endl;
00371   myfile << m_i_run_number <<std::endl;
00372   myfile << m_i_weightGroup <<std::endl;
00373 
00374   myfile.close();
00375 
00376 }