CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/CondTools/Ecal/src/EcalTPGLutGroupHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGLutGroupHandler.h"
00002 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00003 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
00004 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
00005 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTInfo.h"
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTDat.h"
00007 
00008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00009 
00010 
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 
00013 #include "CondFormats/EcalObjects/interface/EcalTPGLutGroup.h"
00014 
00015 
00016 
00017 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00018 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00019 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00020 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00021 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
00022 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
00023 
00024 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00025 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00026 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00027 #include "DataFormats/EcalDetId/interface/EcalTrigTowerDetId.h"
00028 
00029 
00030 
00031 #include<iostream>
00032 #include<fstream>
00033 
00034 #include <time.h>
00035 #include <unistd.h>
00036 
00037 #include <string>
00038 #include <cstdio>
00039 #include <typeinfo>
00040 #include <sstream>
00041 
00042 popcon::EcalTPGLutGroupHandler::EcalTPGLutGroupHandler(const edm::ParameterSet & ps)
00043   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGLutGroupHandler")) {
00044 
00045         edm::LogInfo("EcalTPGLutGroupHandler") << "EcalTPGLutGroup Source handler constructor";
00046         m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
00047         m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
00048         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00049         m_user= ps.getParameter<std::string>("OnlineDBUser");
00050         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00051         m_locationsource= ps.getParameter<std::string>("LocationSource");
00052         m_location=ps.getParameter<std::string>("Location");
00053         m_gentag=ps.getParameter<std::string>("GenTag");
00054         m_runtype=ps.getParameter<std::string>("RunType");
00055 
00056         edm::LogInfo("EcalTPGLutGroupHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00057 }
00058 
00059 popcon::EcalTPGLutGroupHandler::~EcalTPGLutGroupHandler()
00060 {
00061 }
00062 
00063 
00064 void popcon::EcalTPGLutGroupHandler::getNewObjects()
00065 {
00066 
00067 
00068         using namespace edm;
00069         using namespace std;
00070 
00071         edm::LogInfo("EcalTPGLutGroupHandler") << "Started GetNewObjects!!!";
00072 
00073 
00074         /*
00075         // geometry
00076         ESHandle<CaloGeometry> theGeometry;
00077         ESHandle<CaloSubdetectorGeometry> theEndcapGeometry_handle, theBarrelGeometry_handle;
00078         evtSetup.get<CaloGeometryRecord>().get( theGeometry );
00079         evtSetup.get<EcalEndcapGeometryRecord>().get("EcalEndcap",theEndcapGeometry_handle);
00080         evtSetup.get<EcalBarrelGeometryRecord>().get("EcalBarrel",theBarrelGeometry_handle);
00081         evtSetup.get<IdealGeometryRecord>().get(eTTmap_);
00082         theEndcapGeometry_ = &(*theEndcapGeometry_handle);
00083         theBarrelGeometry_ = &(*theBarrelGeometry_handle);
00084 
00085         // electronics mapping
00086         ESHandle< EcalElectronicsMapping > ecalmapping;
00087         evtSetup.get< EcalMappingRcd >().get(ecalmapping);
00088         theMapping_ = ecalmapping.product();
00089 
00090         const std::vector<DetId> & eeCells = theEndcapGeometry_->getValidDetIds(DetId::Ecal, EcalEndcap);
00091 
00092         */
00093 
00094 
00095 
00096         //check whats already inside of database
00097         if (tagInfo().size){
00098         //check whats already inside of database
00099         std::cout << "got offlineInfo = " << std::endl;
00100         std::cout << "tag name = " << tagInfo().name << std::endl;
00101         std::cout << "size = " << tagInfo().size <<  std::endl;
00102         } else {
00103         std::cout << " First object for this tag " << std::endl;
00104         }
00105 
00106         unsigned int max_since=0;
00107         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00108         edm::LogInfo("EcalTPGLutGroupHandler") << "max_since : "  << max_since;
00109         edm::LogInfo("EcalTPGLutGroupHandler") << "retrieved last payload ";
00110 
00111         // here we retrieve all the runs after the last from online DB 
00112         edm::LogInfo("EcalTPGLutGroupHandler") << "Retrieving run list from ONLINE DB ... ";
00113 
00114         edm::LogInfo("EcalTPGLutGroupHandler") << "Making connection...";
00115         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00116         edm::LogInfo("EcalTPGLutGroupHandler") << "Done.";
00117         
00118         if (!econn)
00119         {
00120           std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00121           //        cerr << e.what() << std::endl;
00122           throw cms::Exception("OMDS not available");
00123         } 
00124 
00125         
00126         LocationDef my_locdef;
00127         my_locdef.setLocation(m_location); 
00128 
00129         RunTypeDef my_rundef;
00130         my_rundef.setRunType(m_runtype); 
00131 
00132         RunTag  my_runtag;
00133         my_runtag.setLocationDef( my_locdef );
00134         my_runtag.setRunTypeDef(  my_rundef );
00135         my_runtag.setGeneralTag(m_gentag); 
00136 
00137 
00138         readFromFile("last_tpg_lutGroup_settings.txt");
00139 
00140 
00141         unsigned int min_run=m_i_run_number+1;
00142 
00143         if(m_firstRun<m_i_run_number) {
00144           min_run=m_i_run_number+1;
00145         } else {
00146           min_run=m_firstRun;
00147         }
00148         
00149 
00150         if(min_run<max_since) {
00151           min_run=  max_since+1; // we have to add 1 to the last transferred one
00152         } 
00153         
00154         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00155 
00156         unsigned int max_run=m_lastRun;
00157         edm::LogInfo("EcalTPGLutGroupHandler") << "min_run= " << min_run << " max_run= " << max_run;
00158 
00159         RunList my_list;
00160         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00161         //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
00162       
00163         std::vector<RunIOV> run_vec=  my_list.getRuns();
00164         size_t num_runs=run_vec.size();
00165         
00166         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00167         
00168         std::string str="";
00169         
00170         unsigned int irun;
00171         if(num_runs>0){
00172 
00173 
00174           // going to query the ecal logic id 
00175             std::vector<EcalLogicID> my_TTEcalLogicId_EE;
00176             my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "EE_trigger_tower",
00177                                                     1, 200,
00178                                                     1, 70,
00179                                                     EcalLogicID::NULLID,EcalLogicID::NULLID,
00180                                                     "EE_offline_towerid",12 );
00181             std::cout <<" GOT the logic ID for the EE trigger towers "<< std::endl;
00182 
00183           for(size_t kr=0; kr<run_vec.size(); kr++){
00184 
00185             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00186 
00187             std::cout<<" **************** "<<std::endl;
00188             std::cout<<" run= "<<irun<<std::endl;
00189 
00190             // retrieve the data :
00191             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00192             econn->fetchDataSet(&dataset, &run_vec[kr]);
00193             
00194             std::string the_config_tag="";
00195             int the_config_version=0;
00196             
00197             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00198             
00199             int nr=0;        
00200             for( it=dataset.begin(); it!=dataset.end(); it++ )
00201             {
00202               ++nr;
00203               //EcalLogicID ecalid  = it->first;
00204             
00205               RunTPGConfigDat  dat = it->second;
00206               the_config_tag=dat.getConfigTag();
00207               the_config_version=dat.getVersion();
00208             }
00209             
00210               
00211             // it is all the same for all SM... get the last one 
00212 
00213 
00214             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00215 
00216             // here we should check if it is the same as previous run.
00217 
00218 
00219             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00220               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00221 
00222               FEConfigMainInfo fe_main_info;
00223               fe_main_info.setConfigTag(the_config_tag);
00224               fe_main_info.setVersion(the_config_version);
00225 
00226               try{ 
00227                 std::cout << " before fetch config set" << std::endl;       
00228                 econn-> fetchConfigSet(&fe_main_info);
00229                 std::cout << " after fetch config set" << std::endl;    
00230                 
00231                 
00232                 // now get TPGLutGroup
00233                 int lutId=fe_main_info.getLUTId();
00234             
00235                 if( lutId != m_i_lutGroup ) {
00236             
00237                   FEConfigLUTInfo fe_lut_info;
00238                   fe_lut_info.setId(lutId);
00239                   econn-> fetchConfigSet(&fe_lut_info);
00240                   std::map<EcalLogicID, FEConfigLUTDat> dataset_TpgLut;
00241                   econn->fetchDataSet(&dataset_TpgLut, &fe_lut_info);
00242 
00243                   EcalTPGLutGroup *lut=new EcalTPGLutGroup();
00244                   typedef std::map<EcalLogicID, FEConfigLUTDat>::const_iterator CIfelut;
00245                   EcalLogicID ecid_xt;
00246                   FEConfigLUTDat  rd_lut;
00247                   int itowers=0;
00248         
00249                   for (CIfelut p = dataset_TpgLut.begin(); p != dataset_TpgLut.end(); p++) 
00250                   {
00251                     ecid_xt = p->first;
00252                     rd_lut  = p->second;
00253           
00254                     std::string ecid_name=ecid_xt.getName();
00255               
00256                     if(ecid_name=="EB_trigger_tower") {
00257                       // SM number
00258                       int smid=ecid_xt.getID1();
00259                       // TT number
00260                       int towerid=ecid_xt.getID2();
00261 
00262 
00263                       int tow_eta=(towerid-1)/4;
00264                       int tow_phi=((towerid-1)-tow_eta*4);
00265 
00266                       int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
00267 
00268                       EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE ) ;
00269                       const EcalTrigTowerDetId towid= id.tower();
00270           
00271                       /*         
00272                       char ch[10];
00273                       sprintf(ch,"%d%d", smid, towerid);
00274                       std::string S="";
00275                       S.insert(0,ch);
00276           
00277                       unsigned int towerEBId = 0;
00278                       towerEBId = atoi(S.c_str());
00279                       */
00280 
00281   
00282                       lut->setValue(towid.rawId(), rd_lut.getLUTGroupId());
00283                       ++itowers;
00284                     }
00285                     else if (ecid_name=="EE_trigger_tower") {
00286                       // EE data
00287                       // TCC number
00288                       int tccid=ecid_xt.getID1();
00289                       // TT number
00290                       int towerid=ecid_xt.getID2();
00291 
00292                       bool set_the_tower=false;
00293                       int towid;
00294                       for (size_t itower=0; itower<my_TTEcalLogicId_EE.size(); itower++) {
00295 
00296                         if(!set_the_tower){
00297                           
00298                           if(my_TTEcalLogicId_EE[itower].getID1()==tccid && my_TTEcalLogicId_EE[itower].getID2()==towerid){
00299                             towid =my_TTEcalLogicId_EE[itower].getLogicID();
00300                             set_the_tower=true;
00301                             break;
00302                           }
00303                         }
00304                         
00305                       }
00306                       
00307                       if(set_the_tower){
00308                         lut->setValue(towid, rd_lut.getLUTGroupId());
00309                       } else {
00310                         std::cout <<" these may be the additional towers TCC/TT "
00311                                   << tccid<<"/"<<towerid<<std::endl;
00312                       }
00313                       
00314                       ++itowers;
00315                     }
00316                   }
00317         
00318 
00319                   Time_t snc= (Time_t) irun;
00320                       
00321                   m_to_transfer.push_back(std::make_pair((EcalTPGLutGroup*)lut,snc));
00322         
00323                   m_i_run_number=irun;
00324                   m_i_tag=the_config_tag;
00325                   m_i_version=the_config_version;
00326                   m_i_lutGroup=lutId;
00327                   
00328                   writeFile("last_tpg_lutGroup_settings.txt");
00329 
00330                 } else {
00331 
00332                   m_i_run_number=irun;
00333                   m_i_tag=the_config_tag;
00334                   m_i_version=the_config_version;
00335 
00336                   writeFile("last_tpg_lutGroup_settings.txt");
00337 
00338                   std::cout<< " even if the tag/version is not the same, the lutGroup id is the same -> no transfer needed "<< std::endl; 
00339 
00340                 }
00341 
00342               }       
00343               
00344               catch (std::exception &e) { 
00345                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00346                           <<" version="<<the_config_version<< std::endl;
00347                 std::cout << e.what() << std::endl;
00348                 m_i_run_number=irun;
00349 
00350               }
00351               std::cout<<" **************** "<<std::endl;
00352               
00353             } else if(nr==0) {
00354               m_i_run_number=irun;
00355               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00356               std::cout<<" **************** "<<std::endl;
00357             } else {
00358               m_i_run_number=irun;
00359               m_i_tag=the_config_tag;
00360               m_i_version=the_config_version;
00361               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00362               std::cout<<" **************** "<<std::endl;
00363               writeFile("last_tpg_lutGroup_settings.txt");
00364             }
00365        }
00366       }
00367           
00368       delete econn;
00369       
00370       edm::LogInfo("EcalTPGLutGroupHandler") << "Ecal - > end of getNewObjects -----------";
00371 }
00372 
00373 void  popcon::EcalTPGLutGroupHandler::readFromFile(const char* inputFile) {
00374   //-------------------------------------------------------------
00375   
00376   m_i_tag="";
00377   m_i_version=0;
00378   m_i_run_number=0;
00379   m_i_lutGroup=0; 
00380 
00381   FILE *inpFile; // input file
00382   inpFile = fopen(inputFile,"r");
00383   if(!inpFile) {
00384     edm::LogError("EcalTPGLutGroupHandler")<<"*** Can not open file: "<<inputFile;
00385   }
00386 
00387   char line[256];
00388     
00389   std::ostringstream str;
00390 
00391   fgets(line,255,inpFile);
00392   m_i_tag=to_string(line);
00393   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00394 
00395   fgets(line,255,inpFile);
00396   m_i_version=atoi(line);
00397   str << "version= " << m_i_version << std::endl ;  
00398 
00399   fgets(line,255,inpFile);
00400   m_i_run_number=atoi(line);
00401   str << "run_number= " << m_i_run_number << std::endl ;  
00402 
00403   fgets(line,255,inpFile);
00404   m_i_lutGroup=atoi(line);
00405   str << "lutGroup_config= " << m_i_lutGroup << std::endl ;  
00406 
00407     
00408   fclose(inpFile);           // close inp. file
00409 
00410 }
00411 
00412 void  popcon::EcalTPGLutGroupHandler::writeFile(const char* inputFile) {
00413   //-------------------------------------------------------------
00414   
00415   
00416   std::ofstream myfile;
00417   myfile.open (inputFile);
00418   myfile << m_i_tag <<std::endl;
00419   myfile << m_i_version <<std::endl;
00420   myfile << m_i_run_number <<std::endl;
00421   myfile << m_i_lutGroup <<std::endl;
00422 
00423   myfile.close();
00424 
00425 }