CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/CondTools/Ecal/src/EcalTPGBadTTHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGBadTTHandler.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/FEConfigBadTTInfo.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/EcalTPGTowerStatus.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::EcalTPGBadTTHandler::EcalTPGBadTTHandler(const edm::ParameterSet & ps)
00026   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGBadTTHandler")) {
00027 
00028         edm::LogInfo("EcalTPGBadTTHandler") << "EcalTPGTowerStatus 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("EcalTPGBadTTHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00040 
00041 }
00042 
00043 popcon::EcalTPGBadTTHandler::~EcalTPGBadTTHandler()
00044 {
00045 }
00046 
00047 
00048 void popcon::EcalTPGBadTTHandler::getNewObjects()
00049 {
00050         edm::LogInfo("EcalTPGBadTTHandler") << "Started GetNewObjects!!!";
00051 
00052         unsigned int max_since=0;
00053         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00054         edm::LogInfo("EcalTPGBadTTHandler") << "max_since : "  << max_since;
00055         edm::LogInfo("EcalTPGBadTTHandler") << "retrieved last payload ";
00056 
00057         // here we retrieve all the runs after the last from online DB 
00058         edm::LogInfo("EcalTPGBadTTHandler") << "Retrieving run list from ONLINE DB ... ";
00059 
00060         edm::LogInfo("EcalTPGBadTTHandler") << "Making connection...";
00061         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00062         edm::LogInfo("EcalTPGBadTTHandler") << "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_badTT_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("EcalTPGBadTTHandler") << "min_run= " << min_run << "max_run= " << max_run;
00099 
00100         RunList my_list; 
00101         //      my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef); 
00102         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00103     
00104         std::vector<RunIOV> run_vec=  my_list.getRuns();
00105         size_t num_runs=run_vec.size();
00106 
00107         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00108        
00109         std::string str="";
00110     
00111         unsigned int irun=0;
00112         if(num_runs>0){
00113 
00114 
00115           // going to query the ecal logic id 
00116             std::vector<EcalLogicID> my_TTEcalLogicId_EE;
00117             my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "EE_trigger_tower",
00118                                                     1, 200,
00119                                                     1, 70,
00120                                                     EcalLogicID::NULLID,EcalLogicID::NULLID,
00121                                                     "EE_offline_towerid",12 );
00122             std::cout <<" GOT the logic ID for the EE trigger towers "<< std::endl;
00123 
00124 
00125 
00126          for(size_t kr=0; kr<run_vec.size(); kr++){
00127           
00128             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
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             // it is all the same for all SM... get the last one 
00151 
00152 
00153             // here we should check if it is the same as previous run.
00154 
00155 
00156             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00157               std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00158               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00159 
00160               FEConfigMainInfo fe_main_info;
00161               fe_main_info.setConfigTag(the_config_tag);
00162               fe_main_info.setVersion(the_config_version);
00163 
00164               try{ 
00165 
00166                 econn-> fetchConfigSet(&fe_main_info);
00167         
00168                 // now get TPGTowerStatus
00169                 int badttId=fe_main_info.getBttId();
00170         
00171                 if( badttId != m_i_badTT ) {
00172         
00173                   FEConfigBadTTInfo fe_badTT_info;
00174                   fe_badTT_info.setId(badttId);
00175              
00176                   econn-> fetchConfigSet(&fe_badTT_info);
00177              
00178                   std::vector< FEConfigBadTTDat > dataset_TpgBadTT;
00179 
00180                   econn->fetchConfigDataSet(&dataset_TpgBadTT, &fe_badTT_info);
00181              
00182                   EcalTPGTowerStatus* towerStatus = new EcalTPGTowerStatus;
00183                   typedef std::vector<FEConfigBadTTDat>::const_iterator CIfeped;
00184                   EcalLogicID ecid_xt;
00185                   FEConfigBadTTDat  rd_badTT;
00186 
00187                   // reset the map 
00188                   // EB
00189                   for(int ism=1; ism<=36; ism++) {
00190                     for(int ito=1; ito<=68; ito++) {
00191                       int tow_eta=(ito-1)/4;
00192                       int tow_phi=((ito-1)-tow_eta*4);
00193                       int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
00194                       EBDetId id(ism, axt, EBDetId::SMCRYSTALMODE ) ;
00195                       const EcalTrigTowerDetId towid= id.tower();
00196                       int tower_status=0;
00197                       towerStatus->setValue(towid.rawId(),tower_status);
00198                     }
00199                   }
00200                   //EE
00201                   for (size_t itower=0; itower<my_TTEcalLogicId_EE.size(); itower++) {
00202                     int towid =my_TTEcalLogicId_EE[itower].getLogicID();
00203                     int tower_status=0;
00204                     towerStatus->setValue(towid,tower_status);
00205                   }
00206 
00207                   // now put at 1 those that are bad 
00208                   int icells=0;
00209                   for (CIfeped p = dataset_TpgBadTT.begin(); p != dataset_TpgBadTT.end(); p++) {
00210                     rd_badTT  = *p;
00211           
00212                     int tcc_num=rd_badTT.getTCCId();
00213                     int tt_num=rd_badTT.getTTId();
00214                  
00215                     std::cout<< " tcc/tt"<< tcc_num<<"/"<<tt_num<< std::endl;
00216 
00217                     if(tcc_num>36 && tcc_num<=72) {
00218                       // SM number
00219                       int smid=tcc_num-54;
00220                       if(tcc_num<55) smid=tcc_num-18;
00221                       // TT number
00222                       int towerid=tt_num;
00223 
00224                       int tow_eta=(towerid-1)/4;
00225                       int tow_phi=((towerid-1)-tow_eta*4);
00226                       int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
00227 
00228                       EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE ) ;
00229                       const EcalTrigTowerDetId towid= id.tower();
00230                       towerStatus->setValue(towid.rawId(),rd_badTT.getStatus());
00231   
00232                       ++icells;
00233                     }  else  {
00234                       // EE data
00235 
00236                       // TCC number
00237                       int tccid=tcc_num;
00238                       // TT number
00239                       int towerid=tt_num;
00240 
00241                       bool set_the_tower=false;
00242                       int towid;
00243                       for (size_t itower=0; itower<my_TTEcalLogicId_EE.size(); itower++) {
00244 
00245                         if(!set_the_tower){
00246                           
00247                           if(my_TTEcalLogicId_EE[itower].getID1()==tccid && my_TTEcalLogicId_EE[itower].getID2()==towerid){
00248                             towid =my_TTEcalLogicId_EE[itower].getLogicID();
00249                             set_the_tower=true;
00250                             break;
00251                           }
00252                         }
00253                         
00254                       }
00255                       
00256                       if(set_the_tower){
00257 
00258                         towerStatus->setValue(towid,rd_badTT.getStatus());
00259   
00260 
00261                       } else {
00262                         std::cout <<" these may be the additional towers TCC/TT "
00263                                   << tccid<<"/"<<towerid<<std::endl;
00264                       }
00265                       
00266                       ++icells;
00267 
00268                     }
00269                   }
00270 
00271 
00272                  
00273                 
00274              
00275 
00276                   edm::LogInfo("EcalTPGBadTTHandler") << "Finished badTT reading.";
00277              
00278                   Time_t snc= (Time_t) irun ;                      
00279              
00280                   m_to_transfer.push_back(std::make_pair((EcalTPGTowerStatus*)towerStatus,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_badTT=badttId;
00286              
00287                   writeFile("last_tpg_badTT_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_badTT_settings.txt");
00296              
00297                   //  std::cout<< " even if the tag/version is not the same, the badTT id is the same -> no transfer needed "<< std::endl; 
00298              
00299                 }
00300            
00301               }       
00302               
00303               catch (std::exception &e) { 
00304                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00305                           <<" version="<<the_config_version<< std::endl;
00306                 std::cout << e.what() << std::endl;
00307                 m_i_run_number=irun;
00308                 
00309               }
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             } else {
00316               m_i_run_number=irun;
00317               m_i_tag=the_config_tag;
00318               m_i_version=the_config_version;
00319 
00320               writeFile("last_tpg_badTT_settings.txt");
00321             }   
00322          }
00323         }
00324         
00325         delete econn;
00326 
00327         edm::LogInfo("EcalTPGBadTTHandler") << "Ecal - > end of getNewObjects -----------";        
00328 }
00329 
00330 
00331 void  popcon::EcalTPGBadTTHandler::readFromFile(const char* inputFile) {
00332   //-------------------------------------------------------------
00333   
00334   m_i_tag="";
00335   m_i_version=0;
00336   m_i_run_number=0;
00337   m_i_badTT=0; 
00338 
00339   FILE *inpFile; // input file
00340   inpFile = fopen(inputFile,"r");
00341   if(!inpFile) {
00342     edm::LogError("EcalTPGBadTTHandler")<<"*** Can not open file: "<<inputFile;
00343   }
00344 
00345   char line[256];
00346     
00347   std::ostringstream str;
00348 
00349   fgets(line,255,inpFile);
00350   m_i_tag=to_string(line);
00351   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00352 
00353   fgets(line,255,inpFile);
00354   m_i_version=atoi(line);
00355   str << "version= " << m_i_version << std::endl ;  
00356 
00357   fgets(line,255,inpFile);
00358   m_i_run_number=atoi(line);
00359   str << "run_number= " << m_i_run_number << std::endl ;  
00360 
00361   fgets(line,255,inpFile);
00362   m_i_badTT=atoi(line);
00363   str << "badTT_config= " << m_i_badTT << std::endl ;  
00364 
00365     
00366   fclose(inpFile);           // close inp. file
00367 
00368 }
00369 
00370 void  popcon::EcalTPGBadTTHandler::writeFile(const char* inputFile) {
00371   //-------------------------------------------------------------
00372   
00373   
00374   std::ofstream myfile;
00375   myfile.open (inputFile);
00376   myfile << m_i_tag <<std::endl;
00377   myfile << m_i_version <<std::endl;
00378   myfile << m_i_run_number <<std::endl;
00379   myfile << m_i_badTT <<std::endl;
00380 
00381   myfile.close();
00382 
00383 }