CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_7_hltpatch2/src/CondTools/Ecal/src/EcalTPGBadStripHandler.cc

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