CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/CondTools/Ecal/src/EcalTPGFineGrainEBGroupHandler.cc

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