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
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
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;
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
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
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
00156
00157 RunTPGConfigDat dat = it->second;
00158 the_config_tag=dat.getConfigTag();
00159 the_config_version=dat.getVersion();
00160 }
00161
00162
00163
00164
00165 std::cout<<" run= "<<irun<<" tag "<<the_config_tag<<" version="<<the_config_version <<std::endl;
00166
00167
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
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
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
00212 int tt_id=rd_badXt.getTTId();
00213 int xt_id=rd_badXt.getXTId();
00214
00215
00216 if (fed_id>=610 && fed_id<=645) {
00217
00218
00219
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
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
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 EBDetId ebdetid(sm_num,xt_num,EBDetId::SMCRYSTALMODE);
00241
00242 badXt->setValue(ebdetid.rawId(),rd_badXt.getStatus());
00243 ++icells;
00244 } else {
00245
00246
00247 long x=0;
00248 long y=0;
00249 long z=0;
00250
00251 for(size_t ixt=0; ixt<my_EcalLogicId_EE.size(); ixt++){
00252
00253 if(my_EcalLogicId_EE[ixt].getID1()==fed_id && my_EcalLogicId_EE[ixt].getID2()==tt_id
00254 && my_EcalLogicId_EE[ixt].getID3()==xt_id ) {
00255
00256 long ecid=(long) my_EcalLogicId_EE[ixt].getLogicID();
00257
00258 y=ecid-( (long)(ecid/1000) ) *1000;
00259 x= ( ecid- y) /1000 ;
00260 x= x -( (long)(x/1000) ) *1000;
00261 z= (ecid-y-x*1000 )/1000000 -2010;
00262 if(z==0) z=-1;
00263 if(z==2) z= 1;
00264 }
00265 }
00266
00267 EEDetId eedetid(x,y,z);
00268 badXt->setValue(eedetid.rawId(),rd_badXt.getStatus());
00269 ++icells;
00270 }
00271 }
00272
00273 edm::LogInfo("EcalTPGBadXTHandler") << "Finished badXT reading";
00274
00275 Time_t snc= (Time_t) irun ;
00276 m_to_transfer.push_back(std::make_pair((EcalTPGCrystalStatus*)badXt,snc));
00277
00278 m_i_run_number=irun;
00279 m_i_tag=the_config_tag;
00280 m_i_version=the_config_version;
00281 m_i_badXT=badxtId;
00282
00283 writeFile("last_tpg_badXT_settings.txt");
00284
00285 } else {
00286
00287 m_i_run_number=irun;
00288 m_i_tag=the_config_tag;
00289 m_i_version=the_config_version;
00290
00291 writeFile("last_tpg_badXT_settings.txt");
00292
00293 std::cout<< " even if the tag/version is not the same, the badXT id is the same -> no transfer needed "<< std::endl;
00294
00295 }
00296
00297 }
00298
00299 catch (std::exception &e) {
00300 std::cout << "ERROR: THIS CONFIG DOES NOT EXIST: tag=" <<the_config_tag
00301 <<" version="<<the_config_version<< std::endl;
00302 std::cout << e.what() << std::endl;
00303 m_i_run_number=irun;
00304
00305 }
00306 std::cout<<" **************** "<<std::endl;
00307
00308 } else if(nr==0) {
00309 m_i_run_number=irun;
00310 std::cout<< " no tag saved to RUN_TPGCONFIG_DAT by EcalSupervisor -> no transfer needed "<< std::endl;
00311 std::cout<<" **************** "<<std::endl;
00312 } else {
00313 m_i_run_number=irun;
00314 m_i_tag=the_config_tag;
00315 m_i_version=the_config_version;
00316 std::cout<< " the tag/version is the same -> no transfer needed "<< std::endl;
00317 std::cout<<" **************** "<<std::endl;
00318 writeFile("last_tpg_badXT_settings.txt");
00319 }
00320
00321
00322 }
00323 }
00324
00325 delete econn;
00326
00327 edm::LogInfo("EcalTPGBadXTHandler")<< "Ecal - > end of getNewObjects -----------";
00328 }
00329
00330 void popcon::EcalTPGBadXTHandler::readFromFile(const char* inputFile) {
00331
00332
00333 m_i_tag="";
00334 m_i_version=0;
00335 m_i_run_number=0;
00336 m_i_badXT=0;
00337
00338 FILE *inpFile;
00339 inpFile = fopen(inputFile,"r");
00340 if(!inpFile) {
00341 edm::LogError("EcalTPGBadXTHandler")<<"*** 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 ;
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_badXT=atoi(line);
00362 str << "badXT_config= " << m_i_badXT << std::endl ;
00363
00364
00365 fclose(inpFile);
00366
00367 }
00368
00369 void popcon::EcalTPGBadXTHandler::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_badXT <<std::endl;
00379
00380 myfile.close();
00381
00382 }
00383
00384 EcalTPGCrystalStatus* popcon::EcalTPGBadXTHandler::produceEcalTrgChannelStatus()
00385 {
00386
00387 EcalTPGCrystalStatus* ical = new EcalTPGCrystalStatus() ;
00388
00389 for(int ieta=-EBDetId::MAX_IETA; ieta<=EBDetId::MAX_IETA; ++ieta) {
00390 if(ieta==0) continue;
00391 for(int iphi=EBDetId::MIN_IPHI; iphi<=EBDetId::MAX_IPHI; ++iphi) {
00392 if (EBDetId::validDetId(ieta,iphi)) {
00393 EBDetId ebid(ieta,iphi);
00394 ical->setValue( ebid, 0 );
00395 }
00396 }
00397 }
00398
00399 for(int iX=EEDetId::IX_MIN; iX<=EEDetId::IX_MAX ;++iX) {
00400 for(int iY=EEDetId::IY_MIN; iY<=EEDetId::IY_MAX; ++iY) {
00401
00402 if (EEDetId::validDetId(iX,iY,1)) {
00403 EEDetId eedetidpos(iX,iY,1);
00404 ical->setValue( eedetidpos, 0 );
00405 }
00406 if (EEDetId::validDetId(iX,iY,-1)) {
00407 EEDetId eedetidneg(iX,iY,-1);
00408 ical->setValue( eedetidneg, 0 );
00409 }
00410 }
00411 }
00412 return ical;
00413 }
00414