CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/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                     // EB data                                                                                                            
00212                     if (ecid_name=="EB_VFE") {
00213                       int sm=ecid_xt.getID1();
00214                       int tt=ecid_xt.getID2();
00215                       int strip=ecid_xt.getID3();
00216                       int tcc= sm+54;
00217                       if(sm>18) tcc=sm+18 ;
00218 
00219                       // simple formula to calculate the Srip EB identifier                                                               
00220 
00221                       unsigned int stripEBId = 303176+(tt-1)*64+(strip-1)*8+(tcc-37)*8192;
00222 
00223                       EcalTPGFineGrainStripEE::Item item;
00224                       item.threshold = (unsigned int)rd_fgr.getThreshold();
00225                       item.lut = (unsigned int)rd_fgr.getLUTFgr();
00226 
00227                       fgrStripEE->setValue(stripEBId,item);
00228  
00229 
00230                       ++icells;
00231                     } else if (ecid_name=="ECAL_readout_strip"){
00232                       // EE data
00233                       // fed
00234                       int id1=ecid_xt.getID1();
00235                       // ccu
00236                       int id2=ecid_xt.getID2();
00237                       // Strip
00238                       int id3=ecid_xt.getID3(); 
00239                   
00240                       bool set_the_strip=false;
00241                       int stripEEId;
00242                       for (size_t istrip=0; istrip<my_StripEcalLogicId_EE.size(); istrip++) {
00243 
00244                         if(!set_the_strip){
00245                           
00246                           if(my_StripEcalLogicId_EE[istrip].getID1()==id1 
00247                              && my_StripEcalLogicId_EE[istrip].getID2()==id2
00248                              && my_StripEcalLogicId_EE[istrip].getID3()==id3 
00249                              ){
00250                             stripEEId =my_StripEcalLogicId_EE[istrip].getLogicID();
00251                             set_the_strip=true;
00252                             break;
00253                           }
00254                         }
00255                         
00256                       }
00257                        
00258                       EcalTPGFineGrainStripEE::Item item;
00259                       item.threshold = (unsigned int)rd_fgr.getThreshold();
00260                       item.lut = (unsigned int)rd_fgr.getLUTFgr();
00261                       
00262                       if(set_the_strip){
00263                         fgrStripEE->setValue(stripEEId,item);
00264                       } else {
00265                         std::cout <<" these may be the additional towers TCC/TT "
00266                                   << id1<<"/"<<id2<<std::endl;
00267                       }
00268                       
00269                       ++icells;
00270                     }
00271                   }
00272             
00273 
00274                   Time_t snc= (Time_t) irun;          
00275                   m_to_transfer.push_back(std::make_pair((EcalTPGFineGrainStripEE*)fgrStripEE,snc));
00276 
00277                   m_i_run_number=irun;
00278                   m_i_tag=the_config_tag;
00279                   m_i_version=the_config_version;
00280                   m_i_fgrStripEE=fgrId;
00281                   
00282                   writeFile("last_tpg_fgrStripEE_settings.txt");
00283 
00284                 } else {
00285 
00286                   m_i_run_number=irun;
00287                   m_i_tag=the_config_tag;
00288                   m_i_version=the_config_version;
00289 
00290                   writeFile("last_tpg_fgrStripEE_settings.txt");
00291 
00292                   std::cout<< " even if the tag/version is not the same, the fgrStripEEestals id is the same -> no transfer needed "<< std::endl; 
00293 
00294                 }
00295 
00296               }       
00297               
00298               catch (std::exception &e) { 
00299                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00300                           <<" version="<<the_config_version<< std::endl;
00301                 std::cout << e.what() << std::endl;
00302                 m_i_run_number=irun;
00303 
00304               }
00305               std::cout<<" **************** "<<std::endl;
00306               
00307             } else if(nr==0) {
00308               m_i_run_number=irun;
00309               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00310               std::cout<<" **************** "<<std::endl;
00311             } else {
00312               m_i_run_number=irun;
00313               m_i_tag=the_config_tag;
00314               m_i_version=the_config_version;
00315               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00316               std::cout<<" **************** "<<std::endl;
00317               writeFile("last_tpg_fgrStripEE_settings.txt");
00318             }
00319             
00320           }
00321         }
00322           
00323         delete econn;
00324 
00325         edm::LogInfo("EcalTPGFineGrainStripEEHandler") << "Ecal - > end of getNewObjects -----------";
00326         
00327 }
00328 
00329 
00330 void  popcon::EcalTPGFineGrainStripEEHandler::readFromFile(const char* inputFile) {
00331   //-------------------------------------------------------------
00332   
00333   m_i_tag="";
00334   m_i_version=0;
00335   m_i_run_number=0;
00336   m_i_fgrStripEE=0; 
00337 
00338   FILE *inpFile; // input file
00339   inpFile = fopen(inputFile,"r");
00340   if(!inpFile) {
00341     edm::LogError("EcalTPGFineGrainStripEEHandler")<<"*** Can not open file: "<<inputFile;
00342   }
00343 
00344   char line[256];
00345     
00346   std::ostringstream str;
00347 
00348   fgets(line,255,inpFile);
00349   m_i_tag=to_string(line);
00350   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00351 
00352   fgets(line,255,inpFile);
00353   m_i_version=atoi(line);
00354   str << "version= " << m_i_version << std::endl ;  
00355 
00356   fgets(line,255,inpFile);
00357   m_i_run_number=atoi(line);
00358   str << "run_number= " << m_i_run_number << std::endl ;  
00359 
00360   fgets(line,255,inpFile);
00361   m_i_fgrStripEE=atoi(line);
00362   str << "fgrStripEE_config= " << m_i_fgrStripEE << std::endl ;  
00363 
00364     
00365   fclose(inpFile);           // close inp. file
00366 
00367 }
00368 
00369 void  popcon::EcalTPGFineGrainStripEEHandler::writeFile(const char* inputFile) {
00370   //-------------------------------------------------------------
00371   
00372   
00373   std::ofstream myfile;
00374   myfile.open (inputFile);
00375   myfile << m_i_tag <<std::endl;
00376   myfile << m_i_version <<std::endl;
00377   myfile << m_i_run_number <<std::endl;
00378   myfile << m_i_fgrStripEE <<std::endl;
00379 
00380   myfile.close();
00381 
00382 }