CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/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                   int icells=0;
00210                     
00211                     
00212                   for (CIfesli p = dataset_TpgSli.begin(); p != dataset_TpgSli.end(); p++) {
00213                     ecid_xt = p->first;
00214                     rd_sli  = p->second;
00215               
00216                     std::string ecid_name=ecid_xt.getName();
00217               
00218                     // EB data
00219                     if (ecid_name=="EB_VFE") {
00220                       int sm=ecid_xt.getID1();
00221                       int tt=ecid_xt.getID2();
00222                       int strip=ecid_xt.getID3();
00223                       int tcc= sm+54;
00224                       if(sm>18) tcc=sm+18 ;
00225 
00226                       // simple formula to calculate the Srip EB identifier 
00227                                 
00228                       unsigned int stripEBId = 303176+(tt-1)*64+(strip-1)*8+(tcc-37)*8192;
00229                                 
00230                       sliW->setValue(stripEBId, (unsigned int)rd_sli.getSliding());
00231                       ++icells;
00232 
00233                     }
00234                     else if (ecid_name=="ECAL_readout_strip"){
00235                 
00236                       // EE data
00237                       int id1=ecid_xt.getID1(); // dcc
00238                       int id2=ecid_xt.getID2(); // ccu
00239                       int id3=ecid_xt.getID3(); // strip
00240                 
00241                       bool set_the_strip=false;
00242                       int stripid;
00243                       for (size_t istrip=0; istrip<my_StripEcalLogicId_EE.size(); istrip++) {
00244 
00245                         if(!set_the_strip){
00246                           
00247                           if(my_StripEcalLogicId_EE[istrip].getID1()==id1 
00248                              && my_StripEcalLogicId_EE[istrip].getID2()==id2
00249                              && my_StripEcalLogicId_EE[istrip].getID3()==id3 
00250                              ){
00251                             stripid =my_StripEcalLogicId_EE[istrip].getLogicID();
00252                             set_the_strip=true;
00253                             break;
00254                           }
00255                         }
00256                         
00257                       }
00258                       
00259                       if(set_the_strip){
00260 
00261                         sliW->setValue(stripid, (unsigned int)rd_sli.getSliding());
00262 
00263                       } else {
00264                         std::cout <<" these may be the additional towers TCC/TT "
00265                                   << id1<<"/"<<id2<<std::endl;
00266                       }
00267                       
00268 
00269                       ++icells;
00270                     } 
00271                   }
00272             
00273                
00274                   Time_t snc= (Time_t) irun;          
00275                   m_to_transfer.push_back(std::make_pair((EcalTPGSlidingWindow*)sliW,snc));
00276 
00277                   m_i_run_number=irun;
00278                   m_i_tag=the_config_tag;
00279                   m_i_version=the_config_version;
00280                   m_i_sliding=slidingId;
00281                   
00282                   writeFile("last_tpg_sliding_settings.txt");
00283                 
00284                 } else {
00285           
00286                   m_i_run_number=irun;
00287                   m_i_tag=the_config_tag;
00288                   m_i_version=the_config_version;
00289 
00290                   writeFile("last_tpg_sliding_settings.txt");
00291 
00292                   std::cout<< " even if the tag/version is not the same, the sliding windows id is the same -> no transfer needed "<< std::endl; 
00293 
00294                 }
00295               }         catch (std::exception &e) { 
00296                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00297                           <<" version="<<the_config_version<< std::endl;
00298                 std::cout << e.what() << std::endl;
00299           
00300                 m_i_run_number=irun;
00301 
00302               }
00303         
00304               std::cout<<" **************** "<<std::endl;
00305 
00306               
00307             } else if(nr==0) {
00308               m_i_run_number=irun;
00309               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00310               std::cout<<" **************** "<<std::endl;
00311             } else {
00312               m_i_run_number=irun;
00313               m_i_tag=the_config_tag;
00314               m_i_version=the_config_version;
00315               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00316               std::cout<<" **************** "<<std::endl;
00317               writeFile("last_tpg_sliding_settings.txt");
00318             }
00319 
00320           }
00321         }
00322           
00323         delete econn;
00324 
00325         edm::LogInfo("EcalTPGSlidingWindowHandler") << "Ecal - > end of getNewObjects -----------";
00326         
00327 }
00328 
00329 void  popcon::EcalTPGSlidingWindowHandler::readFromFile(const char* inputFile) {
00330   //-------------------------------------------------------------
00331   
00332   m_i_tag="";
00333   m_i_version=0;
00334   m_i_run_number=0;
00335   m_i_sliding=0; 
00336 
00337   FILE *inpFile; // input file
00338   inpFile = fopen(inputFile,"r");
00339   if(!inpFile) {
00340     edm::LogError("EcalTPGSlidingWindowHandler")<<"*** Can not open file: "<<inputFile;
00341   }
00342 
00343   char line[256];
00344     
00345   std::ostringstream str;
00346 
00347   fgets(line,255,inpFile);
00348   m_i_tag=to_string(line);
00349   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00350 
00351   fgets(line,255,inpFile);
00352   m_i_version=atoi(line);
00353   str << "version= " << m_i_version << std::endl ;  
00354 
00355   fgets(line,255,inpFile);
00356   m_i_run_number=atoi(line);
00357   str << "run_number= " << m_i_run_number << std::endl ;  
00358 
00359   fgets(line,255,inpFile);
00360   m_i_sliding=atoi(line);
00361   str << "sliding_config= " << m_i_sliding << std::endl ;  
00362 
00363     
00364   fclose(inpFile);           // close inp. file
00365 
00366 }
00367 
00368 void  popcon::EcalTPGSlidingWindowHandler::writeFile(const char* inputFile) {
00369   //-------------------------------------------------------------
00370   
00371   
00372   std::ofstream myfile;
00373   myfile.open (inputFile);
00374   myfile << m_i_tag <<std::endl;
00375   myfile << m_i_version <<std::endl;
00376   myfile << m_i_run_number <<std::endl;
00377   myfile << m_i_sliding <<std::endl;
00378 
00379   myfile.close();
00380 
00381 }
00382