CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch13/src/CondTools/Ecal/src/EcalTPGFineGrainStripEEHandler.cc

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