CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/CondTools/Ecal/src/EcalTPGPedestalsHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGPedestalsHandler.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/RunList.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 
00010 #include "CondFormats/EcalObjects/interface/EcalTPGPedestals.h"
00011 
00012 #include<iostream>
00013 #include<fstream>
00014 
00015 
00016 #include <time.h>
00017 #include <unistd.h>
00018 
00019 #include <string>
00020 #include <cstdio>
00021 #include <typeinfo>
00022 #include <sstream>
00023 
00024 
00025 
00026 popcon::EcalTPGPedestalsHandler::EcalTPGPedestalsHandler(const edm::ParameterSet & ps)
00027   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGPedestalsHandler")) {
00028 
00029         edm::LogInfo("EcalTPGPedestalsHandler") << "EcalTPGPedestals Source handler constructor";
00030         m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
00031         m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
00032         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00033         m_user= ps.getParameter<std::string>("OnlineDBUser");
00034         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00035         m_locationsource= ps.getParameter<std::string>("LocationSource");
00036         m_location=ps.getParameter<std::string>("Location");
00037         m_gentag=ps.getParameter<std::string>("GenTag");
00038         m_runtype=ps.getParameter<std::string>("RunType");
00039 
00040         edm::LogInfo("EcalTPGPedestalsHandler")<< m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00041 
00042 }
00043 
00044 popcon::EcalTPGPedestalsHandler::~EcalTPGPedestalsHandler()
00045 {
00046 }
00047 
00048 
00049 void popcon::EcalTPGPedestalsHandler::getNewObjects()
00050 {
00051 
00052         edm::LogInfo("EcalTPGPedestalsHandler") << "Started getNewObjects";
00053         
00054         //check whats already inside of database
00055         if (tagInfo().size){
00056         //check whats already inside of database
00057         std::cout << "got offlineInfo = " << std::endl;
00058         std::cout << "tag name = " << tagInfo().name << std::endl;
00059         std::cout << "size = " << tagInfo().size <<  std::endl;
00060         } else {
00061         std::cout << " First object for this tag " << std::endl;
00062         }
00063                         
00064         unsigned int max_since =0;
00065         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first); 
00066         edm::LogInfo("EcalTPGPedestalsHandler") << "max_since = " << max_since;    
00067         edm::LogInfo("EcalTPGPedestalsHandler")<< "Retrieved last payload ";
00068 
00069         // here we retrieve all the runs after the last from online DB 
00070         edm::LogInfo("EcalTPGPedestalsHandler")<< "Retrieving run list from ONLINE DB ... " << std::endl;
00071 
00072         edm::LogInfo("EcalTPGPedestalsHandler") << "Making connection..." << std::flush;
00073         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00074         edm::LogInfo("EcalTPGPedestalsHandler") << "Done." << std::endl;
00075         
00076         if (!econn)
00077         {
00078           std::cout << " Connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00079           throw cms::Exception("OMDS not available");
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 
00094         readFromFile("last_tpg_ped_settings.txt");
00095 
00096 
00097         unsigned int min_run=m_i_run_number+1;
00098 
00099         if(m_firstRun < m_i_run_number) {
00100           min_run=m_i_run_number+1;
00101         } else {
00102           min_run=m_firstRun;
00103         }
00104         if(min_run<max_since) {
00105           min_run=  max_since+1; // we have to add 1 to the last transferred one
00106         } 
00107 
00108         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00109 
00110         unsigned int max_run=m_lastRun;
00111         edm::LogInfo("EcalTPGPedestalsHandler") <<"min_run= " << min_run << " max_run = " << max_run;
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        
00116         std::vector<RunIOV> run_vec=  my_list.getRuns();
00117         size_t num_runs=run_vec.size();
00118         
00119         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00120         
00121         unsigned int irun=0;
00122                 
00123         if(num_runs>0){ 
00124         
00125           for(size_t kr=0; kr<run_vec.size(); kr++){
00126             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00127 
00128             std::cout<<" **************** "<<std::endl;
00129             std::cout<<" **************** "<<std::endl;
00130             std::cout<<" run= "<<irun<<std::endl;
00131 
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            
00148               RunTPGConfigDat  dat = it->second;
00149               the_config_tag=dat.getConfigTag();
00150               the_config_version=dat.getVersion();
00151             }
00152 
00153             // it is all the same for all SM... get the last one 
00154 
00155 
00156             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00157 
00158             // here we should check if it is the same as previous run.
00159 
00160 
00161             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00162               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00163 
00164               FEConfigMainInfo fe_main_info;
00165               fe_main_info.setConfigTag(the_config_tag);
00166               fe_main_info.setVersion(the_config_version);
00167 
00168               try{ 
00169                 std::cout << " before fetch config set" << std::endl;       
00170                 econn-> fetchConfigSet(&fe_main_info);
00171                 std::cout << " after fetch config set" << std::endl;        
00172 
00173               // now get TPGPedestals
00174                 int pedId=fe_main_info.getPedId();
00175                 
00176                 if( pedId != m_i_ped ) {
00177                   
00178                   FEConfigPedInfo fe_ped_info;
00179                   fe_ped_info.setId(pedId);
00180                   econn-> fetchConfigSet(&fe_ped_info);
00181                   std::map<EcalLogicID, FEConfigPedDat> dataset_TpgPed;
00182                   econn->fetchDataSet(&dataset_TpgPed, &fe_ped_info);
00183                   
00184                   // NB new 
00185                   EcalTPGPedestals* peds = new EcalTPGPedestals;
00186                   typedef std::map<EcalLogicID, FEConfigPedDat>::const_iterator CIfeped;
00187                   EcalLogicID ecid_xt;
00188                   FEConfigPedDat  rd_ped;
00189                   int icells=0;
00190                   for (CIfeped p = dataset_TpgPed.begin(); p != dataset_TpgPed.end(); p++) 
00191                     {
00192                       ecid_xt = p->first;
00193                       rd_ped  = p->second;
00194                     
00195                       std::string ecid_name=ecid_xt.getName();
00196                     
00197                     // EB data
00198                     if (ecid_name=="EB_crystal_number") {
00199                       if(icells<10) std::cout << " copy the EB data " << " icells = " << icells << std::endl;
00200                       int sm_num=ecid_xt.getID1();
00201                       int xt_num=ecid_xt.getID2();
00202                       
00203                       EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
00204                       EcalTPGPedestals::Item item;
00205                       item.mean_x1  =(unsigned int)rd_ped.getPedMeanG1() ;
00206                       item.mean_x6  =(unsigned int)rd_ped.getPedMeanG6();
00207                       item.mean_x12 =(unsigned int)rd_ped.getPedMeanG12();
00208                       
00209                       peds->insert(std::make_pair(ebdetid.rawId(),item));
00210                       ++icells;
00211                     }else if (ecid_name=="EE_crystal_number"){
00212                       
00213                       // EE data
00214                       int z=ecid_xt.getID1();
00215                       int x=ecid_xt.getID2();
00216                       int y=ecid_xt.getID3();
00217                       EEDetId eedetid(x,y,z,EEDetId::XYMODE);
00218                       EcalTPGPedestals::Item item;
00219                       item.mean_x1  =(unsigned int)rd_ped.getPedMeanG1();
00220                       item.mean_x6  =(unsigned int)rd_ped.getPedMeanG6();
00221                       item.mean_x12 =(unsigned int)rd_ped.getPedMeanG12();
00222                       
00223                       peds->insert(std::make_pair(eedetid.rawId(),item));
00224                       ++icells;
00225                     }
00226                   }
00227                 
00228                   
00229                   Time_t snc= (Time_t) irun; 
00230                   m_to_transfer.push_back(std::make_pair((EcalTPGPedestals*)peds,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_ped=pedId;
00236                   
00237                   writeFile("last_tpg_ped_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_ped_settings.txt");
00246 
00247                   std::cout<< " even if the tag/version is not the same, the pedestals 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_ped_settings.txt");
00273             }
00274 
00275           }
00276         }
00277         
00278 
00279         delete econn;
00280         
00281         edm::LogInfo("EcalTPGPedestalsHandler")  << "Ecal - > end of getNewObjects -----------";        
00282 
00283 }
00284 
00285 
00286 
00287 void  popcon::EcalTPGPedestalsHandler::readFromFile(const char* inputFile) {
00288   //-------------------------------------------------------------
00289   
00290   m_i_tag="";
00291   m_i_version=0;
00292   m_i_run_number=0;
00293   m_i_ped=0; 
00294 
00295   FILE *inpFile; // input file
00296   inpFile = fopen(inputFile,"r");
00297   if(!inpFile) {
00298     edm::LogError("EcalTPGPedestalsHandler")<<"*** Can not open file: "<<inputFile;
00299   }
00300 
00301   char line[256];
00302     
00303   std::ostringstream str;
00304 
00305   fgets(line,255,inpFile);
00306   m_i_tag=to_string(line);
00307   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00308 
00309   fgets(line,255,inpFile);
00310   m_i_version=atoi(line);
00311   str << "version= " << m_i_version << std::endl ;  
00312 
00313   fgets(line,255,inpFile);
00314   m_i_run_number=atoi(line);
00315   str << "run_number= " << m_i_run_number << std::endl ;  
00316 
00317   fgets(line,255,inpFile);
00318   m_i_ped=atoi(line);
00319   str << "ped_config= " << m_i_ped << std::endl ;  
00320 
00321     
00322   fclose(inpFile);           // close inp. file
00323 
00324 }
00325 
00326 void  popcon::EcalTPGPedestalsHandler::writeFile(const char* inputFile) {
00327   //-------------------------------------------------------------
00328   
00329   
00330   std::ofstream myfile;
00331   myfile.open (inputFile);
00332   myfile << m_i_tag <<std::endl;
00333   myfile << m_i_version <<std::endl;
00334   myfile << m_i_run_number <<std::endl;
00335   myfile << m_i_ped <<std::endl;
00336 
00337   myfile.close();
00338 
00339 }
00340 
00341