CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/CondTools/Ecal/src/EcalTPGSlidingWindowHandler.cc

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