CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_14/src/CondTools/Ecal/src/EcalTPGFineGrainTowerEEHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGFineGrainTowerEEHandler.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/FEConfigFgrInfo.h"
00006 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00007 #include "Geometry/EcalMapping/interface/EcalElectronicsMapping.h"
00008 #include "Geometry/EcalMapping/interface/EcalMappingRcd.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 #include<iostream>
00012 
00013 popcon::EcalTPGFineGrainTowerEEHandler::EcalTPGFineGrainTowerEEHandler(const edm::ParameterSet & ps)
00014   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGFineGrainTowerEEHandler")) {
00015 
00016         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "EcalTPGFineGrainTowerEE Source handler constructor.";
00017         m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
00018         m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
00019         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00020         m_user= ps.getParameter<std::string>("OnlineDBUser");
00021         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00022         m_locationsource= ps.getParameter<std::string>("LocationSource");
00023         m_location=ps.getParameter<std::string>("Location");
00024         m_gentag=ps.getParameter<std::string>("GenTag");
00025         m_runtype=ps.getParameter<std::string>("RunType");
00026 
00027         edm::LogInfo("EcalTPGFineGrainTowerEEHandler")<< m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00028 
00029 }
00030 
00031 popcon::EcalTPGFineGrainTowerEEHandler::~EcalTPGFineGrainTowerEEHandler()
00032 {
00033 }
00034 
00035 void popcon::EcalTPGFineGrainTowerEEHandler::getNewObjects()
00036 {
00037 
00038         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "Started GetNewObjects!!!";
00039 
00040         //check whats already inside of database
00041         if (tagInfo().size){
00042         //check whats already inside of database
00043         std::cout << "got offlineInfo = " << std::endl;
00044         std::cout << "tag name = " << tagInfo().name << std::endl;
00045         std::cout << "size = " << tagInfo().size <<  std::endl;
00046         } else {
00047         std::cout << " First object for this tag " << std::endl;
00048         }
00049 
00050         unsigned int max_since=0;
00051         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00052         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "max_since : "  << max_since;
00053         Ref fgrTTEE_db = lastPayload();
00054         
00055         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "retrieved last payload ";
00056 
00057         // here we retrieve all the runs after the last from online DB 
00058         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "Retrieving run list from ONLINE DB ... ";
00059 
00060         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "Making connection...";
00061         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00062         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "Done.";
00063         
00064         if (!econn)
00065           {
00066             std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00067             //      cerr << e.what() << std::endl;
00068             throw cms::Exception("OMDS not available");
00069           } 
00070 
00071         
00072         LocationDef my_locdef;
00073         my_locdef.setLocation(m_location); 
00074 
00075         RunTypeDef my_rundef;
00076         my_rundef.setRunType(m_runtype); 
00077 
00078         RunTag  my_runtag;
00079         my_runtag.setLocationDef( my_locdef );
00080         my_runtag.setRunTypeDef(  my_rundef );
00081         my_runtag.setGeneralTag(m_gentag); 
00082 
00083         readFromFile("last_tpg_fgrTTEE_settings.txt");
00084 
00085 
00086         unsigned int min_run=m_i_run_number+1;
00087 
00088         if(m_firstRun<m_i_run_number) {
00089           min_run=m_i_run_number+1;
00090         } else {
00091           min_run=m_firstRun;
00092         }
00093         if(min_run<max_since) {
00094           min_run=max_since+1; // we have to add 1 to the last transferred one
00095         } 
00096 
00097         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00098 
00099         unsigned int max_run=m_lastRun;
00100         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "min_run= " << min_run << " max_run= " << max_run;
00101 
00102         RunList my_list;
00103         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00104         //        my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
00105       
00106         std::vector<RunIOV> run_vec=  my_list.getRuns();
00107         size_t num_runs=run_vec.size();
00108         
00109         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00110 
00111         unsigned int irun;
00112         if(num_runs>0){
00113           // going to query the ecal logic id 
00114             std::vector<EcalLogicID> my_TTEcalLogicId_EE;
00115             my_TTEcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "EE_trigger_tower",
00116                                                     1, 200,
00117                                                     1, 70,
00118                                                     EcalLogicID::NULLID,EcalLogicID::NULLID,
00119                                                     "EE_offline_towerid",12 );
00120             std::cout <<" GOT the logic ID for the EE trigger towers "<< std::endl;
00121 
00122           for(size_t kr=0; kr<run_vec.size(); kr++){
00123 
00124             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());   
00125 
00126             std::cout<<" **************** "<<std::endl;
00127             std::cout<<" **************** "<<std::endl;
00128             std::cout<<" run= "<<irun<<std::endl;
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                 RunTPGConfigDat  dat = it->second;
00145                 the_config_tag=dat.getConfigTag();
00146                 the_config_version=dat.getVersion();
00147             }
00148 
00149             // it is all the same for all SM... get the last one 
00150 
00151             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
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<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00158 
00159               FEConfigMainInfo fe_main_info;
00160               fe_main_info.setConfigTag(the_config_tag);
00161               fe_main_info.setVersion(the_config_version);
00162 
00163               try{ 
00164                 std::cout << " before fetch config set" << std::endl;       
00165                 econn-> fetchConfigSet(&fe_main_info);
00166                 std::cout << " after fetch config set" << std::endl;        
00167 
00168                 // now get TPGFineGrainTowerEE
00169                 int fgrId=fe_main_info.getFgrId();
00170             
00171                 if( fgrId != m_i_fgrTTEE ) {
00172             
00173                   FEConfigFgrInfo fe_fgr_info;
00174                   fe_fgr_info.setId(fgrId);
00175                   econn-> fetchConfigSet(&fe_fgr_info);
00176                   std::map<EcalLogicID, FEConfigFgrEETowerDat> dataset_TpgFineGrainEE;
00177                   econn->fetchDataSet(&dataset_TpgFineGrainEE, &fe_fgr_info);
00178                   EcalTPGFineGrainTowerEE *fgrMap = new EcalTPGFineGrainTowerEE;
00179             
00180                   typedef std::map<EcalLogicID, FEConfigFgrEETowerDat>::const_iterator CIfefgr;
00181                   EcalLogicID ecid_xt;
00182                   FEConfigFgrEETowerDat  rd_fgr;
00183                   int itowers=0;
00184             
00185                   for (CIfefgr p = dataset_TpgFineGrainEE.begin(); p != dataset_TpgFineGrainEE.end(); p++) {
00186                     ecid_xt = p->first;
00187                     rd_fgr  = p->second;
00188               
00189                     std::string ecid_name=ecid_xt.getName();
00190               
00191                     if (ecid_name=="EE_trigger_tower") {
00192 
00193                       int tccid=ecid_xt.getID1();
00194                       // TT number
00195                       int towerid=ecid_xt.getID2();
00196 
00197                       bool set_the_tower=false;
00198                       int towid;
00199                       for (size_t itower=0; itower<my_TTEcalLogicId_EE.size(); itower++) {
00200 
00201                         if(!set_the_tower){
00202                           
00203                           if(my_TTEcalLogicId_EE[itower].getID1()==tccid && my_TTEcalLogicId_EE[itower].getID2()==towerid){
00204                             towid =my_TTEcalLogicId_EE[itower].getLogicID();
00205                             set_the_tower=true;
00206                             break;
00207                           }
00208                         }
00209                         
00210                       }
00211                       
00212                       if(set_the_tower){
00213                         fgrMap->setValue(towid, rd_fgr.getLUTValue());
00214 
00215                       } else {
00216                         std::cout <<" these may be the additional towers TCC/TT "
00217                                   << tccid<<"/"<<towerid<<std::endl;
00218                       }
00219                       
00220                       ++itowers;
00221                     }
00222                   }
00223                 
00224                   Time_t snc= (Time_t) irun ;
00225                       
00226                   m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainTowerEE *)fgrMap,snc));
00227 
00228                   m_i_run_number=irun;
00229                   m_i_tag=the_config_tag;
00230                   m_i_version=the_config_version;
00231                   m_i_fgrTTEE=fgrId;
00232                   
00233                   writeFile("last_tpg_fgrTTEE_settings.txt");
00234 
00235                 } else {
00236 
00237                   m_i_run_number=irun;
00238                   m_i_tag=the_config_tag;
00239                   m_i_version=the_config_version;
00240 
00241                   writeFile("last_tpg_fgrTTEE_settings.txt");
00242 
00243                   std::cout<< " even if the tag/version is not the same, the fgrTTEE id is the same -> no transfer needed "<< std::endl; 
00244 
00245                 }
00246 
00247               }    catch (std::exception &e) { 
00248                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00249                           <<" version="<<the_config_version<< std::endl;
00250                 std::cout << e.what() << std::endl;
00251                 m_i_run_number=irun;
00252 
00253               }
00254               std::cout<<" **************** "<<std::endl;
00255               
00256             } else if(nr==0) {
00257               m_i_run_number=irun;
00258               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00259               std::cout<<" **************** "<<std::endl;
00260             } else {
00261               m_i_run_number=irun;
00262               m_i_tag=the_config_tag;
00263               m_i_version=the_config_version;
00264               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00265               std::cout<<" **************** "<<std::endl;
00266               writeFile("last_tpg_fgrTTEE_settings.txt");
00267             }
00268 
00269           }
00270         }
00271           
00272         delete econn;
00273         edm::LogInfo("EcalTPGFineGrainTowerEEHandler") << "Ecal - > end of getNewObjects -----------";
00274 }
00275 
00276 
00277 void  popcon::EcalTPGFineGrainTowerEEHandler::readFromFile(const char* inputFile) {
00278   //-------------------------------------------------------------
00279   
00280   m_i_tag="";
00281   m_i_version=0;
00282   m_i_run_number=0;
00283   m_i_fgrTTEE=0; 
00284 
00285   FILE *inpFile; // input file
00286   inpFile = fopen(inputFile,"r");
00287   if(!inpFile) {
00288     edm::LogError("EcalTPGFineGrainTowerEEHandler")<<"*** Can not open file: "<<inputFile;
00289   }
00290 
00291   char line[256];
00292     
00293   std::ostringstream str;
00294 
00295   fgets(line,255,inpFile);
00296   m_i_tag=to_string(line);
00297   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00298 
00299   fgets(line,255,inpFile);
00300   m_i_version=atoi(line);
00301   str << "version= " << m_i_version << std::endl ;  
00302 
00303   fgets(line,255,inpFile);
00304   m_i_run_number=atoi(line);
00305   str << "run_number= " << m_i_run_number << std::endl ;  
00306 
00307   fgets(line,255,inpFile);
00308   m_i_fgrTTEE=atoi(line);
00309   str << "fgrTTEE_config= " << m_i_fgrTTEE << std::endl ;  
00310 
00311     
00312   fclose(inpFile);           // close inp. file
00313 
00314 }
00315 
00316 void  popcon::EcalTPGFineGrainTowerEEHandler::writeFile(const char* inputFile) {
00317   //-------------------------------------------------------------
00318   
00319   
00320   std::ofstream myfile;
00321   myfile.open (inputFile);
00322   myfile << m_i_tag <<std::endl;
00323   myfile << m_i_version <<std::endl;
00324   myfile << m_i_run_number <<std::endl;
00325   myfile << m_i_fgrTTEE <<std::endl;
00326 
00327   myfile.close();
00328 
00329 }