CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_6/src/CondTools/Ecal/src/EcalTPGBadXTHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalTPGBadXTHandler.h"
00002 
00003 #include "OnlineDB/EcalCondDB/interface/EcalLogicID.h"
00004 #include "OnlineDB/EcalCondDB/interface/RunTPGConfigDat.h"
00005 #include "OnlineDB/EcalCondDB/interface/FEConfigMainInfo.h"
00006 #include "OnlineDB/EcalCondDB/interface/FEConfigBadXTInfo.h"
00007 #include "OnlineDB/EcalCondDB/interface/RunList.h"
00008 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 #include "CondFormats/EcalObjects/interface/EcalTPGCrystalStatus.h"
00012 
00013 #include<iostream>
00014 #include<fstream>
00015 
00016 
00017 #include <time.h>
00018 #include <unistd.h>
00019 
00020 #include <string>
00021 #include <cstdio>
00022 #include <typeinfo>
00023 #include <sstream>
00024 
00025 popcon::EcalTPGBadXTHandler::EcalTPGBadXTHandler(const edm::ParameterSet & ps)
00026   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalTPGBadXTHandler")) {
00027 
00028         edm::LogInfo("EcalTPGBadXTHandler") << "EcalTPGBadXT Source handler constructor.";
00029         m_firstRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("firstRun").c_str()));
00030         m_lastRun=static_cast<unsigned int>(atoi( ps.getParameter<std::string>("lastRun").c_str()));
00031         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00032         m_user= ps.getParameter<std::string>("OnlineDBUser");
00033         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00034         m_locationsource= ps.getParameter<std::string>("LocationSource");
00035         m_location=ps.getParameter<std::string>("Location");
00036         m_gentag=ps.getParameter<std::string>("GenTag");
00037         m_runtype=ps.getParameter<std::string>("RunType");
00038 
00039         edm::LogInfo("EcalTPGBadXTHandler") << m_sid<<"/"<<m_user<<"/"<<m_location<<"/"<<m_gentag;
00040 
00041 
00042 }
00043 
00044 popcon::EcalTPGBadXTHandler::~EcalTPGBadXTHandler()
00045 {
00046 }
00047 
00048 
00049 void popcon::EcalTPGBadXTHandler::getNewObjects()
00050 {
00051         edm::LogInfo("EcalTPGBadXTHandler") << "Started GetNewObjects!!!";
00052 
00053         
00054         unsigned int max_since=0;
00055         max_since=static_cast<unsigned int>(tagInfo().lastInterval.first);
00056         edm::LogInfo("EcalTPGBadXTHandler") << "max_since : "  << max_since;
00057         edm::LogInfo("EcalTPGBadXTHandler")  << "retrieved last payload ";
00058 
00059         // here we retrieve all the runs after the last from online DB 
00060         edm::LogInfo("EcalTPGBadXTHandler") << "Retrieving run list from ONLINE DB ... ";
00061 
00062         edm::LogInfo("EcalTPGBadXTHandler") << "Making connection...";
00063         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00064         edm::LogInfo("EcalTPGBadXTHandler")<< "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         LocationDef my_locdef;
00074         my_locdef.setLocation(m_location); 
00075 
00076         RunTypeDef my_rundef;
00077         my_rundef.setRunType(m_runtype); 
00078 
00079         RunTag  my_runtag;
00080         my_runtag.setLocationDef( my_locdef );
00081         my_runtag.setRunTypeDef(  my_rundef );
00082         my_runtag.setGeneralTag(m_gentag); 
00083 
00084         readFromFile("last_tpg_badXT_settings.txt");
00085 
00086 
00087         unsigned int min_run=m_i_run_number+1;
00088 
00089         if(m_firstRun<m_i_run_number) {
00090           min_run=m_i_run_number+1;
00091         } else {
00092           min_run=m_firstRun;
00093         }
00094         if(min_run<max_since) {
00095           min_run=  max_since+1; // we have to add 1 to the last transferred one
00096         } 
00097 
00098         std::cout<<"m_i_run_number"<< m_i_run_number <<"m_firstRun "<<m_firstRun<< "max_since " <<max_since<< std::endl;
00099 
00100 
00101         unsigned int max_run=m_lastRun;
00102         edm::LogInfo("EcalTPGBadXTHandler") << "min_run= " << min_run << "max_run= " << max_run;
00103 
00104         RunList my_list; 
00105         my_list=econn->fetchGlobalRunListByLocation(my_runtag, min_run, max_run, my_locdef);
00106         //      my_list=econn->fetchRunListByLocation(my_runtag, min_run, max_run, my_locdef); 
00107       
00108         std::vector<RunIOV> run_vec=  my_list.getRuns();
00109         size_t num_runs=run_vec.size();
00110 
00111         std::cout <<"number of runs is : "<< num_runs<< std::endl;
00112 
00113         std::vector<EcalLogicID> my_EcalLogicId;
00114         std::vector<EcalLogicID> my_EcalLogicId_EE;
00115 
00116         unsigned int irun=0;
00117         if(num_runs>0){
00118 
00119 
00120           my_EcalLogicId = econn->getEcalLogicIDSetOrdered( "ECAL_crystal_number_fedccuxt",
00121                                                           610, 650,
00122                                                           1, 100,
00123                                                           0, 100, 
00124                                                           "EB_crystal_number",123 );
00125 
00126           my_EcalLogicId_EE = econn->getEcalLogicIDSetOrdered( "ECAL_crystal_number_fedccuxt",
00127                                                           600, 700,
00128                                                           1, 100,
00129                                                           0, 100, 
00130                                                           "EE_crystal_number",123 );
00131 
00132 
00133 
00134           for(size_t kr=0; kr<run_vec.size(); kr++){
00135             std::cout << "here we are in run "<<kr<<std::endl;
00136             irun=static_cast<unsigned int>(run_vec[kr].getRunNumber());
00137             
00138             std::cout<<" **************** "<<std::endl;
00139             std::cout<<" **************** "<<std::endl;
00140             std::cout<<" run= "<<irun<<std::endl;
00141 
00142             // retrieve the data :
00143             std::map<EcalLogicID, RunTPGConfigDat> dataset;
00144             econn->fetchDataSet(&dataset, &run_vec[kr]);
00145             
00146             std::string the_config_tag="";
00147             int the_config_version=0;
00148             
00149             std::map< EcalLogicID,  RunTPGConfigDat>::const_iterator it;
00150         
00151             int nr=0;
00152             for( it=dataset.begin(); it!=dataset.end(); it++ )
00153             {
00154               ++nr;
00155               //EcalLogicID ecalid  = it->first;
00156             
00157               RunTPGConfigDat  dat = it->second;
00158               the_config_tag=dat.getConfigTag();
00159               the_config_version=dat.getVersion();
00160             }
00161 
00162             // it is all the same for all SM... get the last one 
00163 
00164 
00165             std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00166 
00167             // here we should check if it is the same as previous run.
00168 
00169 
00170             if((the_config_tag != m_i_tag || the_config_version != m_i_version ) && nr>0 ) {
00171               std::cout<<"the tag is different from last transferred run ... retrieving last config set from DB"<<std::endl;
00172 
00173               FEConfigMainInfo fe_main_info;
00174               fe_main_info.setConfigTag(the_config_tag);
00175               fe_main_info.setVersion(the_config_version);
00176               std::cout << " version=" <<fe_main_info.getVersion()<< std::endl;
00177 
00178               
00179               try{ 
00180                 std::cout << " before fetch config set" << std::endl;       
00181                 econn-> fetchConfigSet(&fe_main_info);
00182                 std::cout << " after fetch config set" << std::endl;        
00183 
00184         
00185                 // now get TPGBadXT
00186                 int badxtId=fe_main_info.getBxtId();
00187             
00188                 if( badxtId != m_i_badXT && badxtId!=0) {
00189             
00190                   FEConfigBadXTInfo fe_badXt_info;
00191                   fe_badXt_info.setId(badxtId);
00192                   econn-> fetchConfigSet(&fe_badXt_info);
00193                   std::vector<FEConfigBadXTDat> dataset_TpgBadXT;
00194                   econn->fetchConfigDataSet(&dataset_TpgBadXT, &fe_badXt_info);
00195 
00196                   // NB new 
00197 
00198                   EcalTPGCrystalStatus* badXt;
00199                   badXt =  produceEcalTrgChannelStatus();
00200 
00201 
00202                   typedef std::vector<FEConfigBadXTDat>::const_iterator CIfeped;
00203                   EcalLogicID ecid_xt;
00204                   FEConfigBadXTDat  rd_badXt;
00205                   int icells=0;
00206 
00207                   for (CIfeped p = dataset_TpgBadXT.begin(); p != dataset_TpgBadXT.end(); p++) {
00208                     rd_badXt = *p;
00209                 
00210                     int fed_id=rd_badXt.getFedId();
00211                     //int tcc_id=rd_badXt.getTCCId();
00212                     int tt_id=rd_badXt.getTTId();
00213                     int xt_id=rd_badXt.getXTId();
00214 
00215                     // EB data      
00216                     if (fed_id>=610 && fed_id<=645) {
00217 
00218                       // logic id is 1011ssxxxx
00219                       // get SM id
00220                       int sm_num=0;
00221                       if(fed_id<=627 ) sm_num=fed_id-609+18; 
00222                       if(fed_id>627  ) sm_num=fed_id-627; 
00223               
00224                       // get crystal id
00225                       int xt_num=0;
00226               
00227                       for(size_t ixt=0; ixt<my_EcalLogicId.size(); ixt++){
00228                 
00229                         if(my_EcalLogicId[ixt].getID1()==fed_id && my_EcalLogicId[ixt].getID2()==tt_id 
00230                         && my_EcalLogicId[ixt].getID3()==xt_id ) {
00231                           //1011060504
00232                           int ecid= my_EcalLogicId[ixt].getLogicID();
00233                           xt_num=(ecid)-(101100+sm_num)*10000;
00234                   
00235                         }
00236                       }
00237 
00238                       std::cout<< " masking crystal "<<sm_num<<"/"<<xt_num<<" from fed/tt/xt"<<
00239                         fed_id<<"/"<<tt_id<<"/"<<xt_id<< std::endl;
00240                       if( sm_num==0 && xt_num==0 ) {
00241                         std::cout<< " ERROR FOR crystal from fed/tt/xt"<<
00242                         fed_id<<"/"<<tt_id<<"/"<<xt_id<< std::endl;
00243                       }
00244                       EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
00245                       
00246                       badXt->setValue(ebdetid.rawId(),rd_badXt.getStatus());    
00247                       ++icells;
00248                     } else {
00249                       // EE data
00250 
00251                       long x=0;
00252                       long y=0;
00253                       long z=0; 
00254 
00255                       for(size_t ixt=0; ixt<my_EcalLogicId_EE.size(); ixt++){
00256                 
00257                         if(my_EcalLogicId_EE[ixt].getID1()==fed_id && my_EcalLogicId_EE[ixt].getID2()==tt_id 
00258                           && my_EcalLogicId_EE[ixt].getID3()==xt_id ) {
00259                   
00260                           long ecid=(long) my_EcalLogicId_EE[ixt].getLogicID();
00261                           // logic_id 201Zxxxyyy Z=0 / 2 -> z= -1 / 1 , x -> 1 100,  y -> 1 100 
00262                           y=ecid-( (long)(ecid/1000) ) *1000;
00263                           x= ( ecid- y) /1000 ;
00264                           x= x -( (long)(x/1000) ) *1000;
00265                           z= (ecid-y-x*1000 )/1000000 -2010;
00266                           if(z==0) z=-1;
00267                           if(z==2) z= 1; 
00268                         }
00269                       }
00270 
00271                       if( x==0 && y==0 && z==0 ) {
00272                         std::cout<< " ERROR FOR crystal from fed/tt/xt"<<
00273                         fed_id<<"/"<<tt_id<<"/"<<xt_id<< std::endl;
00274                       }
00275                       EEDetId eedetid(x,y,z);
00276                       badXt->setValue(eedetid.rawId(),rd_badXt.getStatus());    
00277                       ++icells;
00278                     }
00279                   }//end for over data
00280           
00281                   edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
00282             
00283                   Time_t snc= (Time_t) irun ;                      
00284                   m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt,snc));
00285             
00286                   m_i_run_number=irun;
00287                   m_i_tag=the_config_tag;
00288                   m_i_version=the_config_version;
00289                   m_i_badXT=badxtId;
00290             
00291                   writeFile("last_tpg_badXT_settings.txt");
00292             
00293                 } else {
00294               
00295                 m_i_run_number=irun;
00296                 m_i_tag=the_config_tag;
00297                 m_i_version=the_config_version;
00298               
00299                 writeFile("last_tpg_badXT_settings.txt");
00300               
00301                 std::cout<< " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "<< std::endl; 
00302               
00303                 }
00304 
00305               }       
00306               
00307               catch (std::exception &e) { 
00308                 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00309                           <<" version="<<the_config_version<< std::endl;
00310                 std::cout << e.what() << std::endl;
00311                 m_i_run_number=irun;
00312 
00313               }
00314               std::cout<<" **************** "<<std::endl;
00315               
00316             } else if(nr==0) {
00317               m_i_run_number=irun;
00318               std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl; 
00319               std::cout<<" **************** "<<std::endl;
00320             } else {
00321               m_i_run_number=irun;
00322               m_i_tag=the_config_tag;
00323               m_i_version=the_config_version;
00324               std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl; 
00325               std::cout<<" **************** "<<std::endl;
00326               writeFile("last_tpg_badXT_settings.txt");
00327             }
00328 
00329                 
00330           }//end for over kr (nr of runs)
00331         }//end if
00332         
00333         delete econn;
00334 
00335         edm::LogInfo("EcalTPGBadXTHandler")<< "Ecal - > end of getNewObjects -----------";        
00336 }
00337 
00338 void  popcon::EcalTPGBadXTHandler::readFromFile(const char* inputFile) {
00339   //-------------------------------------------------------------
00340   
00341   m_i_tag="";
00342   m_i_version=0;
00343   m_i_run_number=0;
00344   m_i_badXT=0; 
00345 
00346   FILE *inpFile; // input file
00347   inpFile = fopen(inputFile,"r");
00348   if(!inpFile) {
00349     edm::LogError("EcalTPGBadXTHandler")<<"*** Can not open file: "<<inputFile;
00350   }
00351 
00352   char line[256];
00353     
00354   std::ostringstream str;
00355 
00356   fgets(line,255,inpFile);
00357   m_i_tag=to_string(line);
00358   str << "gen tag " << m_i_tag << std::endl ;  // should I use this? 
00359 
00360   fgets(line,255,inpFile);
00361   m_i_version=atoi(line);
00362   str << "version= " << m_i_version << std::endl ;  
00363 
00364   fgets(line,255,inpFile);
00365   m_i_run_number=atoi(line);
00366   str << "run_number= " << m_i_run_number << std::endl ;  
00367 
00368   fgets(line,255,inpFile);
00369   m_i_badXT=atoi(line);
00370   str << "badXT_config= " << m_i_badXT << std::endl ;  
00371 
00372     
00373   fclose(inpFile);           // close inp. file
00374 
00375 }
00376 
00377 void  popcon::EcalTPGBadXTHandler::writeFile(const char* inputFile) {
00378   //-------------------------------------------------------------
00379   
00380   
00381   std::ofstream myfile;
00382   myfile.open (inputFile);
00383   myfile << m_i_tag <<std::endl;
00384   myfile << m_i_version <<std::endl;
00385   myfile << m_i_run_number <<std::endl;
00386   myfile << m_i_badXT <<std::endl;
00387 
00388   myfile.close();
00389 
00390 }
00391 
00392 EcalTPGCrystalStatus* popcon::EcalTPGBadXTHandler::produceEcalTrgChannelStatus()
00393 {
00394 
00395   EcalTPGCrystalStatus*  ical =  new EcalTPGCrystalStatus() ;
00396   // barrel
00397   for(int ieta=-EBDetId::MAX_IETA; ieta<=EBDetId::MAX_IETA; ++ieta) {
00398     if(ieta==0) continue;
00399     for(int iphi=EBDetId::MIN_IPHI; iphi<=EBDetId::MAX_IPHI; ++iphi) {
00400       if (EBDetId::validDetId(ieta,iphi)) {
00401         EBDetId ebid(ieta,iphi);
00402         ical->setValue( ebid, 0 );
00403       }
00404     }
00405   }
00406   // endcap
00407   for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX) {
00408     for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY) {
00409       // make an EEDetId since we need EEDetId::rawId() to be used as the key for the pedestals
00410       if (EEDetId::validDetId(iX,iY,1)) {
00411         EEDetId eedetidpos(iX,iY,1);
00412         ical->setValue( eedetidpos, 0 );
00413       }
00414       if (EEDetId::validDetId(iX,iY,-1)) {
00415         EEDetId eedetidneg(iX,iY,-1);
00416         ical->setValue( eedetidneg, 0 );
00417       }
00418     }
00419   }
00420   return ical;
00421 }
00422