CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/CondTools/Ecal/src/EcalTPGPhysicsConstHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGPhysicsConstHandler.h"
00002 
00003 #include "DataFormats/DetId/interface/DetId.h"
00004 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00005 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
00007 #include "OnlineDB/EcalCondDB/interface/FEConfigParamDat.h"
00008 #include "OnlineDB/EcalCondDB/interface/FEConfigLUTInfo.h"
00009 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00011 
00012 #include<iostream>
00013 
00014 popcon::EcalTPGPhysicsConstHandler::EcalTPGPhysicsConstHandler(const edm::ParameterSet & ps)
00015   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGPhysicsConstHandler")) {
00016 
00017         edm::LogInfo("EcalTPGPhysicsConstHandler") << "EcalTPGPhysicsConst Source handler constructor.";
00018         m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
00019         m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
00020         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00021         m_user= ps.getParameter<std::string>("OnlineDBUser");
00022         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00023         m_locationsource= ps.getParameter<std::string>("LocationSource");
00024         m_location=ps.getParameter<std::string>("Location");
00025         m_gentag=ps.getParameter<std::string>("GenTag");
00026         m_runtype=ps.getParameter<std::string>("RunType");
00027 
00028         edm::LogInfo("EcalTPGPhysicsConstHandler")<< m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00029 
00030 }
00031 
00032 popcon::EcalTPGPhysicsConstHandler::~EcalTPGPhysicsConstHandler()
00033 {
00034 }
00035 
00036 
00037 void popcon::EcalTPGPhysicsConstHandler::getNewObjects()
00038 {
00039 
00040         edm::LogInfo("EcalTPGPhysicsConstHandler") << "Started GetNewObjects!!!";
00041 
00042         //check whats already inside of database
00043         if (tagInfo().size){
00044         //check whats already inside of database
00045         std::cout << "got offlineInfo = " << std::endl;
00046         std::cout << "tag name = " << tagInfo().name << std::endl;
00047         std::cout << "size = " << tagInfo().size <<  std::endl;
00048         } else {
00049         std::cout << " First object for this tag " << std::endl;
00050         }
00051 
00052         unsigned int max_since=0;
00053         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00054         edm::LogInfo("EcalTPGPhysicsConstHandler") << "max_since : "  << max_since;
00055         Ref physC_db = lastPayload();
00056         
00057         edm::LogInfo("EcalTPGPhysicsConstHandler") << "retrieved last payload ";
00058 
00059         // here we retrieve all the runs after the last from online DB 
00060         edm::LogInfo("EcalTPGPhysicsConstHandler") << "Retrieving run list from ONLINE DB ... ";
00061 
00062         edm::LogInfo("EcalTPGPhysicsConstHandler") << "Making connection...";
00063         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00064         edm::LogInfo("EcalTPGPhysicsConstHandler") << "Done.";
00065         
00066         if (!econn)
00067           {
00068             std::cout << " connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00069             //      cerr << e.what() << std::endl;
00070             throw cms::Exception("OMDS not available");
00071           } 
00072 
00073         
00074         LocationDef my_locdef;
00075         my_locdef.setLocation(m_location); 
00076 
00077         RunTypeDef my_rundef;
00078         my_rundef.setRunType(m_runtype); 
00079 
00080         RunTag  my_runtag;
00081         my_runtag.setLocationDef( my_locdef );
00082         my_runtag.setRunTypeDef(  my_rundef );
00083         my_runtag.setGeneralTag(m_gentag); 
00084 
00085 
00086         readFromFile("last_tpg_physC_settings.txt");
00087 
00088         unsigned int min_run=m_i_run_number+1;
00089 
00090         if(m_firstRun<m_i_run_number) {
00091           min_run=m_i_run_number+1;
00092         } else {
00093           min_run=m_firstRun;
00094         }
00095         
00096         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00097 
00098         if(min_run<max_since) {
00099           min_run=  max_since+1; // we have to add 1 to the last transferred one
00100         } 
00101 
00102         unsigned int max_run=m_lastRun;
00103         edm::LogInfo("EcalTPGPhysicsConstHandler") << "min_run= " << min_run << "max_run= " << max_run;
00104         
00105         RunList my_list;
00106         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00107         //      my_list=econn->fetchRunListByLocation(my_runtag,min_run,max_run,my_locdef);
00108 
00109       
00110         std::vector<RunIOV> run_vec=  my_list.getRuns();
00111         size_t num_runs=run_vec.size();
00112 
00113         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00114 
00115         unsigned int irun;
00116         if(num_runs>0){
00117         
00118           for(size_t kr=0; kr<run_vec.size(); kr++){
00119 
00120             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00121 
00122             std::cout<<" **************** "<<std::endl;
00123             std::cout<<" **************** "<<std::endl;
00124             std::cout<<" run= "<<irun<<std::endl;
00125             
00126             // retrieve the data :
00127             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00128             econn->fetchDataSet(&dataset, &run_vec[kr]);
00129             
00130             std::string the_config_tag="";
00131             int the_config_version=0;
00132             
00133             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00134             
00135             int nr=0;
00136             for( it=dataset.begin(); it!=dataset.end(); it++ )
00137             {
00138               ++nr;
00139               //EcalLogicID ecalid  = it->first;
00140               RunTPGConfigDat  dat = it->second;
00141               the_config_tag=dat.getConfigTag();
00142               the_config_version=dat.getVersion();
00143             } 
00144               
00145               
00146             // it is all the same for all SM... get the last one 
00147 
00148 
00149             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00150 
00151             // here we should check if it is the same as previous run.
00152 
00153 
00154             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00155               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00156 
00157               FEConfigMainInfo fe_main_info;
00158               fe_main_info.setConfigTag(the_config_tag);
00159               fe_main_info.setVersion(the_config_version);
00160 
00161               try{ 
00162                 std::cout << " before fetch config set" << std::endl;       
00163                 econn-> fetchConfigSet(&fe_main_info);
00164                 std::cout << " after fetch config set" << std::endl;       
00165            
00166            
00167                 // now get TPGPhysicsConst
00168                 int linId=fe_main_info.getLinId();
00169                 int fgrId=fe_main_info.getFgrId();
00170                 int lutId=fe_main_info.getLUTId();
00171                 
00172                 if ((linId != m_i_physClin) || (fgrId != m_i_physCfgr) || (lutId != m_i_physClut) ) {
00173                   
00174                   std::cout<<"one of the parameters: linId, LutId or fgrId is different from" <<std::endl;
00175                   std::cout<<"last transferred run ..."<<std::endl;
00176  
00177                   FEConfigLinInfo fe_physLin_info;
00178                   FEConfigFgrInfo fe_physFgr_info;
00179                   FEConfigLUTInfo fe_physLut_info;
00180                   fe_physLin_info.setId(linId);
00181                   fe_physFgr_info.setId(fgrId);
00182                   fe_physLut_info.setId(lutId);
00183                   
00184                   econn-> fetchConfigSet(&fe_physLin_info);
00185                   econn-> fetchConfigSet(&fe_physFgr_info);
00186                   econn-> fetchConfigSet(&fe_physLut_info);
00187                   std::map<EcalLogicID, FEConfigLinParamDat> dataset_TpgPhysicsLin;
00188                   std::map<EcalLogicID, FEConfigLUTParamDat> dataset_TpgPhysicsLut;
00189                   std::map<EcalLogicID, FEConfigFgrParamDat> dataset_TpgPhysicsFgr;
00190                   
00191                   econn->fetchDataSet(&dataset_TpgPhysicsLin, &fe_physLin_info);
00192                   econn->fetchDataSet(&dataset_TpgPhysicsLut, &fe_physLut_info);
00193                   econn->fetchDataSet(&dataset_TpgPhysicsFgr, &fe_physFgr_info);
00194 
00195                   EcalTPGPhysicsConst* physC = new EcalTPGPhysicsConst;
00196                   typedef std::map<EcalLogicID, FEConfigLinParamDat>::const_iterator CIfeLin;
00197                   typedef std::map<EcalLogicID, FEConfigLUTParamDat>::const_iterator CIfeLUT;
00198                   typedef std::map<EcalLogicID, FEConfigFgrParamDat>::const_iterator CIfeFgr;
00199 
00200                   EcalLogicID ecidLin_xt;
00201                   EcalLogicID ecidLut_xt;
00202                   EcalLogicID ecidFgr_xt;
00203                   FEConfigLinParamDat rd_physLin;
00204                   FEConfigLUTParamDat rd_physLut;
00205                   FEConfigFgrParamDat rd_physFgr;
00206                   
00207                   std::map<int,float> EtSatLinEB;
00208                   std::map<int,float> EtSatLinEE;
00209                   typedef std::map<int,float>::const_iterator itEtSat;
00210                   
00211                   std::map<int,EcalTPGPhysicsConst::Item> temporaryMapEB;
00212                   std::map<int,EcalTPGPhysicsConst::Item> temporaryMapEE;
00213                   typedef std::map<int,EcalTPGPhysicsConst::Item>::iterator iterEB;
00214                   typedef std::map<int,EcalTPGPhysicsConst::Item>::iterator iterEE;
00215                   
00216                  
00217                   for (CIfeLin p0 = dataset_TpgPhysicsLin.begin(); p0 != dataset_TpgPhysicsLin.end(); p0++) 
00218                   { 
00219                     ecidLin_xt = p0->first;
00220                     rd_physLin = p0->second;
00221           
00222                     std::string ecid_nameLin=ecidLin_xt.getName();
00223 
00224                     if(ecid_nameLin=="EB") {
00225                       DetId eb(DetId::Ecal, EcalBarrel);              
00226                       EtSatLinEB.insert(std::make_pair(eb.rawId(),rd_physLin.getETSat()));  
00227                     }
00228                     else if (ecid_nameLin=="EE"){
00229                     DetId ee(DetId::Ecal, EcalEndcap);                
00230                       EtSatLinEE.insert(std::make_pair(ee.rawId(),rd_physLin.getETSat()));
00231                     }
00232                   }   
00233 
00234                                   
00235                   int icells=0;
00236                   for (CIfeLUT p1 = dataset_TpgPhysicsLut.begin(); p1 != dataset_TpgPhysicsLut.end(); p1++) 
00237                   { 
00238                     ecidLut_xt = p1->first;
00239                     rd_physLut = p1->second;
00240           
00241                     std::string ecid_nameLut=ecidLut_xt.getName(); 
00242 
00243                     // Ecal barrel detector
00244                     if(ecid_nameLut=="EB") {
00245                     
00246                       DetId eb(DetId::Ecal, EcalBarrel);
00247                       
00248                       for (itEtSat it1 = EtSatLinEB.begin() ; it1 != EtSatLinEB.end(); it1++){
00249 
00250                         if (it1->first == (int)eb.rawId()){ 
00251                           float ETSatLin = it1->second;
00252                           
00253                           if (rd_physLut.getETSat() == ETSatLin) {
00254                             EcalTPGPhysicsConst::Item item;
00255                             item.EtSat=rd_physLut.getETSat();
00256                             item.ttf_threshold_Low=rd_physLut.getTTThreshlow();
00257                             item.ttf_threshold_High=rd_physLut.getTTThreshhigh();
00258                             item.FG_lowThreshold=0; 
00259                             item.FG_highThreshold=0; 
00260                             item.FG_lowRatio=0; 
00261                             item.FG_highRatio=0; 
00262                             temporaryMapEB.insert(std::make_pair(eb.rawId(),item));
00263                           }
00264                           else throw cms::Exception("The values of the ETSatLin and ETSatLut are different.");
00265                         }
00266                         
00267                       }
00268                                                       
00269                         
00270                       ++icells;
00271                     }
00272                     else if (ecid_nameLut=="EE") {
00273                       // Ecal endcap detector     
00274                       
00275                       DetId ee(DetId::Ecal, EcalEndcap);
00276           
00277                       for (itEtSat it2 = EtSatLinEE.begin(); it2 != EtSatLinEE.end(); it2++){
00278 
00279                         if (it2->first == (int)ee.rawId()){ 
00280                           float ETSatLin = it2->second;
00281                        
00282                           if (rd_physLut.getETSat() == ETSatLin) {
00283                             EcalTPGPhysicsConst::Item item;
00284                             item.EtSat=rd_physLut.getETSat();
00285                             item.ttf_threshold_Low=rd_physLut.getTTThreshlow();
00286                             item.ttf_threshold_High=rd_physLut.getTTThreshhigh();
00287                             item.FG_lowThreshold=0; 
00288                             item.FG_highThreshold=0; 
00289                             item.FG_lowRatio=0; 
00290                             item.FG_highRatio=0;
00291                             temporaryMapEE.insert( std::make_pair(ee.rawId(),item) );
00292                           }
00293                           else throw cms::Exception("The values of the ETSatLin and ETSatLut are different.");
00294                         }
00295                         
00296                       }
00297                                            
00298                       ++icells;   
00299                     }
00300                   }
00301                   
00302                   int icellsEB=0;
00303                   int icellsEE=0;
00304                   for (CIfeFgr p2 = dataset_TpgPhysicsFgr.begin(); p2 != dataset_TpgPhysicsFgr.end(); p2++) 
00305                   { 
00306                     ecidFgr_xt = p2->first;
00307                     rd_physFgr  = p2->second;
00308           
00309                     std::string ecid_nameFgr=ecidFgr_xt.getName();
00310 
00311                     // Ecal barrel detector
00312                     if(ecid_nameFgr=="EB") {
00313                       
00314                       DetId eb(DetId::Ecal, EcalBarrel);
00315                       
00316                       int count;
00317                       for ( iterEB itt=temporaryMapEB.begin() ; itt != temporaryMapEB.end() ; itt++ ){
00318                                         
00319                         if (itt->first == (int)eb.rawId()){ 
00320 
00321                           (itt->second).FG_lowThreshold=rd_physFgr.getFGlowthresh();
00322                           (itt->second).FG_highThreshold=rd_physFgr.getFGhighthresh();
00323                           (itt->second).FG_lowRatio=rd_physFgr.getFGlowratio();
00324                           (itt->second).FG_highRatio= rd_physFgr.getFGhighratio();
00325                                                    
00326                            physC->setValue(eb.rawId(),itt->second);
00327                         
00328                         }
00329                         
00330                         count++; 
00331                       }   
00332           
00333                       ++icellsEB;
00334                       
00335                     }
00336                     else if (ecid_nameFgr=="EE") {
00337                       // Ecal endcap detector
00338 
00339                       DetId ee(DetId::Ecal, EcalEndcap);
00340                 
00341                       int countEE = 0;
00342                       for ( iterEE itEE=temporaryMapEE.begin() ; itEE != temporaryMapEE.end() ; itEE++ ){                       
00343 
00344                         if (itEE->first == (int)ee.rawId()){ 
00345                           
00346                           (itEE->second).FG_lowThreshold=rd_physFgr.getFGlowthresh();
00347                           (itEE->second).FG_highThreshold=rd_physFgr.getFGhighthresh();
00348                           // the last two is empty for the EE
00349                           (itEE->second).FG_lowRatio=rd_physFgr.getFGlowratio();
00350                           (itEE->second).FG_highRatio= rd_physFgr.getFGhighratio();
00351                            
00352                            physC->setValue(ee.rawId(),itEE->second); 
00353                         }
00354                         
00355                         countEE++; 
00356                       }
00357            
00358                       ++icellsEE;         
00359                     }
00360                   }
00361                   
00362 
00363                   Time_t snc= (Time_t) irun ;
00364                       
00365                   m_to_transfer.push_back(std::make_pair((EcalTPGPhysicsConst*)physC,snc));
00366               
00367               
00368                   m_i_run_number=irun;
00369                   m_i_tag=the_config_tag;
00370                   m_i_version=the_config_version;
00371                   m_i_physClin=linId;
00372                   m_i_physClut=lutId;
00373                   m_i_physCfgr=fgrId;
00374                   
00375                   writeFile("last_tpg_physC_settings.txt");
00376 
00377                 } else {
00378 
00379                   m_i_run_number=irun;
00380                   m_i_tag=the_config_tag;
00381                   m_i_version=the_config_version;
00382 
00383                   writeFile("last_tpg_physC_settings.txt");
00384 
00385                   std::cout<< " even if the tag/version is not the same, the physics constants id is the same -> no transfer needed "<< std::endl; 
00386 
00387                 }
00388 
00389               }       
00390               
00391               
00392               
00393               catch (std::exception &e) { 
00394                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00395                           <<" version="<<the_config_version<< std::endl;
00396                 std::cout << e.what() << std::endl;
00397                 m_i_run_number=irun;
00398 
00399               }
00400               std::cout<<" **************** "<<std::endl;
00401               
00402             } else if(nr==0) {
00403               m_i_run_number=irun;
00404               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00405               std::cout<<" **************** "<<std::endl;
00406             } else {
00407               m_i_run_number=irun;
00408               m_i_tag=the_config_tag;
00409               m_i_version=the_config_version;
00410               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00411               std::cout<<" **************** "<<std::endl;
00412               writeFile("last_tpg_physC_settings.txt");
00413             }
00414     }
00415   }
00416           
00417         delete econn;
00418         edm::LogInfo("EcalTPGPhysicsConstHandler") << "Ecal - > end of getNewObjects -----------";
00419         
00420 }
00421 
00422 
00423 void  popcon::EcalTPGPhysicsConstHandler::readFromFile(const char* inputFile) {
00424   //-------------------------------------------------------------
00425   
00426   m_i_tag="";
00427   m_i_version=0;
00428   m_i_run_number=0;
00429   m_i_physClin=0;
00430   m_i_physClut=0;
00431   m_i_physCfgr=0;
00432                   
00433   FILE *inpFile; // input file
00434   inpFile = fopen(inputFile,"r");
00435   if(!inpFile) {
00436     edm::LogError("EcalTPGPhysicsConstHandler")<<"*** Can not open file: "<<inputFile;
00437   }
00438 
00439   char line[256];
00440     
00441   std::ostringstream str;
00442 
00443   fgets(line,255,inpFile);
00444   m_i_tag=to_string(line);
00445   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00446 
00447   fgets(line,255,inpFile);
00448   m_i_version=atoi(line);
00449   str << "version= " << m_i_version << std::endl ;  
00450 
00451   fgets(line,255,inpFile);
00452   m_i_run_number=atoi(line);
00453   str << "run_number= " << m_i_run_number << std::endl ;  
00454 
00455   fgets(line,255,inpFile);
00456   m_i_physClin=atoi(line);
00457   str << "physClin_config= " << m_i_physClin << std::endl ;  
00458 
00459   fgets(line,255,inpFile);
00460   m_i_physClut=atoi(line);
00461   str << "physClut_config= " << m_i_physClut << std::endl ;  
00462   
00463   fgets(line,255,inpFile);
00464   m_i_physCfgr=atoi(line);
00465   str << "physCfgr_config= " << m_i_physCfgr << std::endl ;  
00466     
00467   fclose(inpFile);           // close inp. file
00468 
00469 }
00470 
00471 void  popcon::EcalTPGPhysicsConstHandler::writeFile(const char* inputFile) {
00472   //-------------------------------------------------------------
00473   
00474   
00475   std::ofstream myfile;
00476   myfile.open (inputFile);
00477   myfile << m_i_tag <<std::endl;
00478   myfile << m_i_version <<std::endl;
00479   myfile << m_i_run_number <<std::endl;
00480   myfile << m_i_physClin <<std::endl;
00481   myfile << m_i_physClut <<std::endl;
00482   myfile << m_i_physCfgr <<std::endl;
00483 
00484   myfile.close();
00485 
00486 }
00487