CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_4_5_patch3/src/CondTools/Ecal/src/EcalTPGSpikeThresholdHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGSpikeThresholdHandler.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/FEConfigSpikeInfo.h"
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigSpikeDat.h"
00007 
00008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00009 
00010 
00011 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00012 
00013 #include "CondFormats/EcalObjects/interface/EcalTPGSpike.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::EcalTPGSpikeThresholdHandler::EcalTPGSpikeThresholdHandler(const edm::ParameterSet & ps)
00043   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGSpikeThresholdHandler")) {
00044 
00045         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "EcalTPGSpike 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("EcalTPGSpikeThresholdHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00057 }
00058 
00059 popcon::EcalTPGSpikeThresholdHandler::~EcalTPGSpikeThresholdHandler()
00060 {
00061 }
00062 
00063 
00064 void popcon::EcalTPGSpikeThresholdHandler::getNewObjects()
00065 {
00066 
00067 
00068         using namespace edm;
00069         using namespace std;
00070 
00071         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Started GetNewObjects!!!";
00072 
00073         //check whats already inside of database
00074         if (tagInfo().size){
00075         //check whats already inside of database
00076           std::cout << "got offlineInfo = " << std::endl;
00077           std::cout << "tag name = " << tagInfo().name << std::endl;
00078           std::cout << "size = " << tagInfo().size <<  std::endl;
00079         } else {
00080           std::cout << " First object for this tag " << std::endl;
00081         }
00082 
00083         unsigned int max_since=0;
00084         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00085         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "max_since : "  << max_since;
00086         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "retrieved last payload ";
00087 
00088         // here we retrieve all the runs after the last from online DB 
00089         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Retrieving run list from ONLINE DB ... ";
00090 
00091         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Making connection...";
00092         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00093         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Done.";
00094         
00095         if (!econn)
00096         {
00097           std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00098           //        cerr << e.what() << std::endl;
00099           throw cms::Exception("OMDS not available");
00100         } 
00101 
00102         
00103         LocationDef my_locdef;
00104         my_locdef.setLocation(m_location); 
00105 
00106         RunTypeDef my_rundef;
00107         my_rundef.setRunType(m_runtype); 
00108 
00109         RunTag  my_runtag;
00110         my_runtag.setLocationDef( my_locdef );
00111         my_runtag.setRunTypeDef(  my_rundef );
00112         my_runtag.setGeneralTag(m_gentag); 
00113 
00114 
00115         readFromFile("last_tpg_spikeThreshold_settings.txt");
00116 
00117 
00118         unsigned int min_run=m_i_run_number+1;
00119 
00120         if(m_firstRun<m_i_run_number) {
00121           min_run=m_i_run_number+1;
00122         } else {
00123           min_run=m_firstRun;
00124         }
00125         
00126 
00127         if(min_run<max_since) {
00128           min_run=  max_since+1; // we have to add 1 to the last transferred one
00129         } 
00130         
00131         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00132 
00133         unsigned int max_run=m_lastRun;
00134         edm::LogInfo("EcalTPGSpikeThresholdHandler") << "min_run= " << min_run << " max_run= " << max_run;
00135 
00136         RunList my_list;
00137         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00138         //my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
00139       
00140         std::vector<RunIOV> run_vec=  my_list.getRuns();
00141         size_t num_runs=run_vec.size();
00142         
00143         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00144         
00145         std::string str="";
00146         
00147         unsigned int irun;
00148         if(num_runs>0){
00149             
00150           for(size_t kr=0; kr<run_vec.size(); kr++){
00151 
00152             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00153 
00154             std::cout<<" **************** "<<std::endl;
00155             std::cout<<" run= "<<irun<<std::endl;
00156 
00157             // retrieve the data :
00158             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00159             econn->fetchDataSet(&dataset, &run_vec[kr]);
00160             
00161             std::string the_config_tag="";
00162             int the_config_version=0;
00163             
00164             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00165             
00166             int nr=0;        
00167             for( it=dataset.begin(); it!=dataset.end(); it++ )
00168             {
00169               ++nr;
00170               //EcalLogicID ecalid  = it->first;
00171             
00172               RunTPGConfigDat  dat = it->second;
00173               the_config_tag=dat.getConfigTag();
00174               the_config_version=dat.getVersion();
00175             }
00176             
00177               
00178             // it is all the same for all SM... get the last one 
00179 
00180 
00181             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00182 
00183             // here we should check if it is the same as previous run.
00184 
00185 
00186             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00187               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00188 
00189               FEConfigMainInfo fe_main_info;
00190               fe_main_info.setConfigTag(the_config_tag);
00191               fe_main_info.setVersion(the_config_version);
00192 
00193               try{ 
00194                 std::cout << " before fetch config set" << std::endl;       
00195                 econn-> fetchConfigSet(&fe_main_info);
00196                 std::cout << " after fetch config set" << std::endl;    
00197                 
00198                 
00199                 // now get TPGSpikeThreshold
00200                 int spikeId=fe_main_info.getSpiId();
00201             
00202                 if( spikeId != m_i_spikeTh ) {
00203             
00204                   FEConfigSpikeInfo fe_spike_info;
00205                   fe_spike_info.setId(spikeId);
00206                   econn-> fetchConfigSet(&fe_spike_info);
00207                   std::map<EcalLogicID, FEConfigSpikeDat> dataset_TpgSpike;
00208                   econn->fetchDataSet(&dataset_TpgSpike, &fe_spike_info);
00209 
00210                   EcalTPGSpike *lut=new EcalTPGSpike();
00211                   typedef std::map<EcalLogicID, FEConfigSpikeDat>::const_iterator CIfelut;
00212                   EcalLogicID ecid_xt;
00213                   FEConfigSpikeDat  rd_spike;
00214                   int itowers=0;
00215         
00216                   for (CIfelut p = dataset_TpgSpike.begin(); p != dataset_TpgSpike.end(); p++) 
00217                   {
00218                     ecid_xt = p->first;
00219                     rd_spike  = p->second;
00220           
00221                     std::string ecid_name=ecid_xt.getName();
00222               
00223                     if(ecid_name=="EB_trigger_tower") {
00224                       // SM number
00225                       int smid=ecid_xt.getID1();
00226                       // TT number
00227                       int towerid=ecid_xt.getID2();
00228 
00229 
00230                       int tow_eta=(towerid-1)/4;
00231                       int tow_phi=((towerid-1)-tow_eta*4);
00232 
00233                       int axt=(tow_eta*5)*20 + tow_phi*5 +1 ;
00234 
00235                       EBDetId id(smid, axt, EBDetId::SMCRYSTALMODE ) ;
00236                       const EcalTrigTowerDetId towid= id.tower();
00237           
00238                       //std::cout << " TTid =  " << towid.rawId() << " SpikeTh = " <<rd_spike.getSpikeThreshold() << std::endl;
00239                        
00240                       lut->setValue(towid.rawId(), rd_spike.getSpikeThreshold());
00241                       ++itowers;
00242                     }
00243                     ++itowers;
00244                   }
00245         
00246                   std::cout <<" The number of towers is = " << itowers << std::endl;
00247                    
00248                   Time_t snc= (Time_t) irun;
00249                       
00250                   m_to_transfer.push_back(std::make_pair((EcalTPGSpike*)lut,snc));
00251         
00252                   m_i_run_number=irun;
00253                   m_i_tag=the_config_tag;
00254                   m_i_version=the_config_version;
00255                   m_i_spikeTh=spikeId;
00256                   
00257                   writeFile("last_tpg_spikeThreshold_settings.txt");
00258 
00259                 } else {
00260 
00261                   m_i_run_number=irun;
00262                   m_i_tag=the_config_tag;
00263                   m_i_version=the_config_version;
00264 
00265                   writeFile("last_tpg_spikeThreshold_settings.txt");
00266 
00267                   std::cout<< " even if the tag/version is not the same, the lutGroup id is the same -> no transfer needed "<< std::endl; 
00268 
00269                 }
00270 
00271               }       
00272               
00273               catch (std::exception &e) { 
00274                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00275                           <<" version="<<the_config_version<< std::endl;
00276                 std::cout << e.what() << std::endl;
00277                 m_i_run_number=irun;
00278 
00279               }
00280               std::cout<<" **************** "<<std::endl;
00281               
00282             } else if(nr==0) {
00283               m_i_run_number=irun;
00284               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00285               std::cout<<" **************** "<<std::endl;
00286             } else {
00287               m_i_run_number=irun;
00288               m_i_tag=the_config_tag;
00289               m_i_version=the_config_version;
00290               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00291               std::cout<<" **************** "<<std::endl;
00292               writeFile("last_tpg_spikeThreshold_settings.txt");
00293             }
00294        }
00295       }
00296           
00297       delete econn;
00298       
00299       edm::LogInfo("EcalTPGSpikeThresholdHandler") << "Ecal - > end of getNewObjects -----------";
00300 }
00301 
00302 void  popcon::EcalTPGSpikeThresholdHandler::readFromFile(const char* inputFile) {
00303   //-------------------------------------------------------------
00304   
00305   m_i_tag="";
00306   m_i_version=0;
00307   m_i_run_number=0;
00308   m_i_spikeTh=0; 
00309 
00310   FILE *inpFile; // input file
00311   inpFile = fopen(inputFile,"r");
00312   if(!inpFile) {
00313     edm::LogError("EcalTPGSpikeThresholdHandler")<<"*** Can not open file: "<<inputFile;
00314   }
00315 
00316   char line[256];
00317     
00318   std::ostringstream str;
00319 
00320   fgets(line,255,inpFile);
00321   m_i_tag=to_string(line);
00322   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00323 
00324   fgets(line,255,inpFile);
00325   m_i_version=atoi(line);
00326   str << "version= " << m_i_version << std::endl ;  
00327 
00328   fgets(line,255,inpFile);
00329   m_i_run_number=atoi(line);
00330   str << "run_number= " << m_i_run_number << std::endl ;  
00331 
00332   fgets(line,255,inpFile);
00333   m_i_spikeTh=atoi(line);
00334   str << "spikeTh_config= " << m_i_spikeTh << std::endl ;  
00335 
00336     
00337   fclose(inpFile);           // close inp. file
00338 
00339 }
00340 
00341 void  popcon::EcalTPGSpikeThresholdHandler::writeFile(const char* inputFile) {
00342   //-------------------------------------------------------------
00343   
00344   
00345   std::ofstream myfile;
00346   myfile.open (inputFile);
00347   myfile << m_i_tag <<std::endl;
00348   myfile << m_i_version <<std::endl;
00349   myfile << m_i_run_number <<std::endl;
00350   myfile << m_i_spikeTh <<std::endl;
00351 
00352   myfile.close();
00353 
00354 }