CMS 3D CMS Logo

/data/doxygen/doxygen-1.7.3/gen/CMSSW_4_2_8/src/CondTools/Ecal/src/EcalDCSHandler.cc

Go to the documentation of this file.
00001 #include "CondTools/Ecal/interface/EcalDCSHandler.h"
00002 #include "CondFormats/EcalObjects/interface/EcalDCSTowerStatusHelper.h"
00003 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00004 
00005 
00006 
00007 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00008 
00009 #include<iostream>
00010 
00011 popcon::EcalDCSHandler::EcalDCSHandler(const edm::ParameterSet & ps)
00012   :    m_name(ps.getUntrackedParameter<std::string>("name","EcalDCSHandler")) {
00013 
00014         std::cout << "EcalDCS Source handler constructor\n" << std::endl;
00015         m_firstRun=(unsigned long)atoi( ps.getParameter<std::string>("firstRun").c_str());
00016         m_lastRun=(unsigned long)atoi( ps.getParameter<std::string>("lastRun").c_str());
00017         m_sid= ps.getParameter<std::string>("OnlineDBSID");
00018         m_user= ps.getParameter<std::string>("OnlineDBUser");
00019         m_pass= ps.getParameter<std::string>("OnlineDBPassword");
00020 
00021         std::cout << m_sid<<"/"<<m_user<<std::endl;
00022 
00023 
00024 }
00025 
00026 popcon::EcalDCSHandler::~EcalDCSHandler()
00027 {
00028 }
00029 
00030 void popcon::EcalDCSHandler::printHVDataSet( const std::map<EcalLogicID, RunDCSHVDat>* dataset, 
00031                                              int limit = 0 ) const
00032 {
00033   std::cout << "==========printDataSet()" << std::endl;
00034   if (dataset->size() == 0) {
00035     std::cout << "No data in map!" << std::endl;
00036   }
00037   EcalLogicID ecid;
00038   RunDCSHVDat hv;
00039   
00040   int count = 0;
00041   typedef std::map< EcalLogicID, RunDCSHVDat >::const_iterator CI;
00042   for (CI p = dataset->begin(); p != dataset->end(); ++p) {
00043     count++;
00044     if (limit && count > limit) { return; }
00045     ecid = p->first;
00046     hv  = p->second;
00047     
00048     std::cout << "SM:                     " << ecid.getID1() << std::endl;
00049     std::cout << "Channel:                " << ecid.getID2() << std::endl;
00050     std::cout << "HV:                     " << hv.getHV() << std::endl;
00051     std::cout << "HV nominal:             " << hv.getHVNominal() << std::endl;
00052     std::cout << "HV status:              " << hv.getStatus() << std::endl;
00053     std::cout << "========================" << std::endl;
00054   }
00055   std::cout << std::endl;
00056 }
00057 
00058 void popcon::EcalDCSHandler::printLVDataSet( const std::map<EcalLogicID, RunDCSLVDat>* dataset, 
00059                                              int limit = 0 ) const
00060 {
00061   std::cout << "==========printDataSet()" << std::endl;
00062   if (dataset->size() == 0) {
00063     std::cout << "No data in map!" << std::endl;
00064   }
00065   EcalLogicID ecid;
00066   RunDCSLVDat lv;
00067   
00068   int count = 0;
00069   typedef std::map< EcalLogicID, RunDCSLVDat >::const_iterator CI;
00070   for (CI p = dataset->begin(); p != dataset->end(); ++p) {
00071     count++;
00072     if (limit && count > limit) { return; }
00073     ecid = p->first;
00074     lv  = p->second;
00075     
00076     std::cout << "SM:                     " << ecid.getID1() << std::endl;
00077     std::cout << "Channel:                " << ecid.getID2() << std::endl;
00078     std::cout << "LV:                     " << lv.getLV() << std::endl;
00079     std::cout << "LV nominal:             " << lv.getLVNominal() << std::endl;
00080     std::cout << "LV status:              " << lv.getStatus() << std::endl;
00081     std::cout << "========================" << std::endl;
00082     }
00083     std::cout << std::endl;
00084   }
00085 
00086 uint16_t popcon::EcalDCSHandler::OffDBStatus( uint16_t dbStatus , int pos ) {
00087   uint16_t hv_off_dbstatus = ( dbStatus & (1 << pos ) ) ;
00088   if(hv_off_dbstatus>0) hv_off_dbstatus=1;
00089   return hv_off_dbstatus;
00090 }  
00091 
00092 uint16_t  popcon::EcalDCSHandler::updateHV( RunDCSHVDat* hv, uint16_t dbStatus, int mode) const {
00093   // mode ==0 EB ,  mode==1 EE Anode , mode==2 EE Dynode 
00094 
00095   uint16_t result=0; 
00096   uint16_t hv_on_dbstatus=0;
00097   uint16_t hv_nomi_on_dbstatus=0;
00098 
00099   if(  hv->getStatus()==RunDCSHVDat::HVNOTNOMINAL ) hv_nomi_on_dbstatus=1; 
00100   if(  hv->getStatus()==RunDCSHVDat::HVOFF ) hv_on_dbstatus=1; 
00101 
00102 
00103   
00104   uint16_t temp=0;
00105 
00106   if(mode == 0 || mode == 1) {
00107     for (int i=0; i<16; i++) {
00108       if( i!= EcalDCSTowerStatusHelper::HVSTATUS &&  i!=EcalDCSTowerStatusHelper::HVNOMINALSTATUS  ) {
00109         temp = temp | (1<<i) ;  
00110       } else {
00111         temp = temp | (0<<i);
00112       } 
00113     }
00114     result=  dbStatus & temp  ;
00115     result=  ( result | (  hv_on_dbstatus << EcalDCSTowerStatusHelper::HVSTATUS ) )   | ( hv_nomi_on_dbstatus << EcalDCSTowerStatusHelper::HVNOMINALSTATUS )    ;
00116   } else {
00117     for (int i=0; i<16; i++) {
00118       if( i!=EcalDCSTowerStatusHelper::HVEEDNOMINALSTATUS &&  i!= EcalDCSTowerStatusHelper::HVEEDSTATUS ) {
00119         temp = temp | (1<<i) ;  
00120       } else {
00121         temp = temp | (0<<i);
00122       } 
00123     }
00124     result=  dbStatus & temp  ;
00125     result= ( result | ( hv_on_dbstatus << EcalDCSTowerStatusHelper::HVEEDSTATUS )) |  (  hv_nomi_on_dbstatus << EcalDCSTowerStatusHelper::HVEEDNOMINALSTATUS )   ;
00126   }
00127 
00128   return result; 
00129 }
00130 
00131 
00132 uint16_t  popcon::EcalDCSHandler::updateLV( RunDCSLVDat* lv, uint16_t dbStatus) const {
00133   uint16_t result=0; 
00134   uint16_t lv_on_dbstatus=0;
00135   uint16_t lv_nomi_on_dbstatus=0;
00136   if(  lv->getStatus()==RunDCSLVDat::LVNOTNOMINAL ) lv_nomi_on_dbstatus=1; 
00137   if(  lv->getStatus()==RunDCSLVDat::LVOFF ) lv_on_dbstatus=1; 
00138 
00139   uint16_t lv_off_dbstatus = ( dbStatus & (1 << EcalDCSTowerStatusHelper::LVSTATUS ) ) ;
00140   uint16_t lv_nomi_off_dbstatus = ( dbStatus & (1 << EcalDCSTowerStatusHelper::LVNOMINALSTATUS ) ) ;
00141   if(lv_off_dbstatus>0) lv_off_dbstatus=1;
00142   if(lv_nomi_off_dbstatus>0) lv_nomi_off_dbstatus=1;
00143 
00144   
00145   uint16_t temp=0;
00146   for (int i=0; i<16; i++) {
00147     if( i!= EcalDCSTowerStatusHelper::LVSTATUS &&  i!=EcalDCSTowerStatusHelper::LVNOMINALSTATUS ) {
00148       temp = temp | (1<<i) ;  
00149     } else {
00150       temp = temp | ( 0 << i );
00151     } 
00152   }
00153 
00154 
00155    result=  dbStatus & temp  ;
00156    result=  ( result | (  lv_on_dbstatus << EcalDCSTowerStatusHelper::LVSTATUS ) )   | ( lv_nomi_on_dbstatus << EcalDCSTowerStatusHelper::LVNOMINALSTATUS ) ;
00157 
00158   
00159   return result; 
00160 }
00161 
00162 bool popcon::EcalDCSHandler::insertHVDataSetToOffline( const std::map<EcalLogicID, RunDCSHVDat>* dataset, EcalDCSTowerStatus* dcs_temp ) const
00163 {
00164   bool result=false; 
00165   if (dataset->size() == 0) {
00166     std::cout << "No data in std::map!" << std::endl;
00167   }
00168   EcalLogicID ecid;
00169   RunDCSHVDat hv;
00170 
00171 
00172   typedef std::map< EcalLogicID, RunDCSHVDat >::const_iterator CI ;
00173 
00174   for (CI p = dataset->begin(); p != dataset->end(); ++p) {
00175 
00176 
00177     ecid = p->first;
00178     hv  = p->second;
00179 
00180     if(ecid.getName()=="EB_HV_channel"){
00181       int sm= ecid.getID1() ;
00182       int chan= ecid.getID2();
00183       
00184       int* limits=0;
00185       limits=  HVLogicIDToDetID(sm,chan);
00186       int iz=limits[0];
00187       int i1=limits[1];
00188       int i2=limits[2];
00189       int j=limits[3];
00190 
00191       for(int ik=i1; ik<=i2; ik++){ 
00192         if (EcalTrigTowerDetId::validDetId(iz,EcalBarrel,j,ik )){
00193           EcalTrigTowerDetId ebid(iz,EcalBarrel,j,ik);
00194           EcalDCSTowerStatus::const_iterator it =dcs_temp->find(ebid.rawId());
00195           
00196           uint16_t dbStatus = 0;
00197           if ( it != dcs_temp->end() ) {
00198             dbStatus = it->getStatusCode();
00199           }
00200           int modo=0;
00201           uint16_t new_dbStatus= updateHV(&hv, dbStatus, modo); 
00202           if(new_dbStatus != dbStatus ) result=true; 
00203 
00204           dcs_temp->setValue( ebid, new_dbStatus );
00205 
00206           if(new_dbStatus != dbStatus) {
00207             std::cout <<"SM/chan:"<<sm<<"/"<<chan <<" new db status ="<< new_dbStatus << " old  "<<dbStatus<<" HV: "<< hv.getHV()<<"/"<<hv.getHVNominal()<<std::endl;
00208             
00209           } 
00210         }
00211       }
00212       delete [] limits; 
00213     } else {
00214       // endcaps 
00215       int dee= ecid.getID1() ;
00216       int chan= ecid.getID2();
00217       
00218       int* limits=0;
00219       limits=  HVEELogicIDToDetID(dee,chan);
00220       int iz=limits[0];
00221       int i1=limits[1];
00222       int i2=limits[2];
00223       int j1=limits[3];
00224       int j2=limits[4];
00225 
00226       int ex_x[6];
00227       int ex_y[6];
00228       if(dee==1 ) {
00229         ex_x[0]=4;      ex_y[0]=8;
00230         ex_x[1]=4;      ex_y[1]=9;
00231         ex_x[2]=4;      ex_y[2]=10;
00232         ex_x[3]=5;      ex_y[3]=9;
00233         ex_x[4]=5;      ex_y[4]=10;
00234         ex_x[5]=6;      ex_y[5]=10;
00235       } else if(dee==2) {
00236         ex_x[0]=17;     ex_y[0]=11;
00237         ex_x[1]=17;     ex_y[1]=12;
00238         ex_x[2]=17;     ex_y[2]=13;
00239         ex_x[3]=16;     ex_y[3]=11;
00240         ex_x[4]=16;     ex_y[4]=12;
00241         ex_x[5]=15;     ex_y[5]=11;
00242       } else if(dee==3) {
00243         ex_x[0]=17;     ex_y[0]=8;
00244         ex_x[1]=17;     ex_y[1]=9;
00245         ex_x[2]=17;     ex_y[2]=10;
00246         ex_x[3]=16;     ex_y[3]=9;
00247         ex_x[4]=16;     ex_y[4]=10;
00248         ex_x[5]=15;     ex_y[5]=10;
00249       } else if(dee==4) {
00250         ex_x[0]=4;      ex_y[0]=11;
00251         ex_x[1]=4;      ex_y[1]=12;
00252         ex_x[2]=4;      ex_y[2]=13;
00253         ex_x[3]=5;      ex_y[3]=11;
00254         ex_x[4]=5;      ex_y[4]=12;
00255         ex_x[5]=6;      ex_y[5]=11;
00256       }
00257 
00258       int modo=1;
00259       if(ecid.getName()=="EE_HVD_channel") modo=2;
00260 
00261       for(int ik=i1; ik<=i2; ik++){ 
00262         for(int ip=j1; ip<=j2; ip++){
00263           bool not_excluded=true;
00264           if(chan==2 ) { // channel 2 has half a dee minus 6 towers
00265             for (int l=0; l<6; l++){
00266               if(ik== ex_x[l] && ip== ex_y[l] ) not_excluded=false;
00267             }
00268           }
00269           if(not_excluded){
00270             if (EcalScDetId::validDetId(ik,ip,iz)){
00271               EcalScDetId eeid(ik,ip,iz);
00272               EcalDCSTowerStatus::const_iterator it =dcs_temp->find(eeid.rawId());
00273           
00274               uint16_t dbStatus = 0;
00275               if ( it != dcs_temp->end() ) {
00276                 dbStatus = it->getStatusCode();
00277               }
00278               // FIXME - UPDATE HV A and D
00279               uint16_t new_dbStatus= updateHV(&hv, dbStatus, modo); 
00280               if(new_dbStatus != dbStatus ) result=true; 
00281               
00282               dcs_temp->setValue( eeid, new_dbStatus );
00283               
00284               if(new_dbStatus != dbStatus) {
00285                 std::cout <<"Dee/chan:"<<dee<<"/"<<chan <<" new db status ="<< new_dbStatus << " old  "<<dbStatus<<" HV: "<< hv.getHV()<<"/"<<hv.getHVNominal()<<std::endl;
00286                 
00287               } 
00288             }
00289           }
00290         }
00291       }
00292       if(chan==1){ // channel 1 has half a dee plus 6 more towers 
00293         for (int l=0; l<6; l++){
00294           int ik=ex_x[l];
00295           int ip=ex_y[l];
00296           if (EcalScDetId::validDetId(ik,ip,iz)){
00297             EcalScDetId eeid(ik,ip,iz);
00298             EcalDCSTowerStatus::const_iterator it =dcs_temp->find(eeid.rawId());
00299             
00300             uint16_t dbStatus = 0;
00301             if ( it != dcs_temp->end() ) {
00302               dbStatus = it->getStatusCode();
00303             }
00304             uint16_t new_dbStatus= updateHV(&hv, dbStatus,modo); 
00305             if(new_dbStatus != dbStatus ) result=true; 
00306             
00307             dcs_temp->setValue( eeid, new_dbStatus );
00308             
00309             if(new_dbStatus != dbStatus) {
00310               std::cout <<"Dee/chan:"<<dee<<"/"<<chan <<" new db status ="<< new_dbStatus << " old  "<<dbStatus<<" HV: "<< hv.getHV()<<"/"<<hv.getHVNominal()<<std::endl;
00311               
00312             } 
00313           }
00314         }
00315       }
00316       
00317       delete [] limits; 
00318 
00319     }
00320   }
00321   return result; 
00322 }
00323 
00324 int popcon::EcalDCSHandler::detIDToLogicID(int iz, int i, int j) {
00325   // returns the number from 0 to 1223 from SM1 to 36 from ch 1 to 34 
00326 
00327   int sm=0;
00328   int hv_chan=0;
00329 
00330 
00331   sm = (i-1)/4;
00332   if(iz<0) sm=sm+18;
00333   
00334   int ilocal=(i-1)-sm*4;
00335   if(iz<0){
00336     if(ilocal==0 || ilocal==1) hv_chan=1;
00337     if(ilocal==2 || ilocal==3) hv_chan=2;
00338   } else {
00339     if(ilocal==0 || ilocal==1) hv_chan=2;
00340     if(ilocal==2 || ilocal==3) hv_chan=1;
00341   }
00342 
00343   sm=sm+1; 
00344 
00345   hv_chan=(j-1)*2+hv_chan;
00346   
00347   hv_chan=(sm-1)*34+hv_chan -1  ;
00348 
00349   return hv_chan;
00350 
00351 }
00352 
00353 
00354 
00355 int * popcon::EcalDCSHandler::HVEELogicIDToDetID(int dee, int chan) const {
00356   int iz=-1;
00357   if(dee==1 || dee==2) iz=1;
00358   int ix1=1;
00359   int ix2=1;
00360   int iy1=1;
00361   int iy2=1;
00362 
00363   if(dee==1 && chan==1) {
00364     ix1=1; ix2=10;
00365     iy1=11; iy2=20;
00366   } else if(dee==2 && chan==1) {
00367     ix1=11; ix2=20;
00368     iy1=1; iy2=10;
00369   } else if(dee==3 && chan==1) {
00370     ix1=11; ix2=20;
00371     iy1=11; iy2=20;
00372   } else if(dee==4 && chan==1) {
00373     ix1=1; ix2=10;
00374     iy1=1; iy2=10;
00375   } else if(dee==1 && chan==2) {
00376     ix1=1; ix2=10;
00377     iy1=1; iy2=10;
00378   } else if(dee==2 && chan==2) {
00379     ix1=11; ix2=20;
00380     iy1=11; iy2=20;
00381   } else if(dee==3 && chan==2) {
00382     ix1=11; ix2=20;
00383     iy1=1; iy2=10;
00384   } else if(dee==4 && chan==2) {
00385     ix1=1; ix2=10;
00386     iy1=11; iy2=20;
00387   }
00388 
00389   int *result = new int[5];
00390   
00391   result[0]=iz;
00392   result[1]=ix1;
00393   result[2]=ix2;
00394   result[3]=iy1;
00395   result[4]=iy2;
00396   return result; 
00397   
00398 }
00399 
00400 int * popcon::EcalDCSHandler::HVLogicIDToDetID(int sm, int chan) const {
00401   // returns the numbers iz, i1, i2 and j1, j2 on which to loop for the towers
00402 
00403       int iz=-1;
00404       if(sm>0 && sm <= 18) iz=1;
00405       int j = (chan-1)/2 +1;
00406       int i_local_hv = (chan-1) - (j-1)*2 + 1; // this gives 1 for odd channels and 2 for even channels 
00407       int i1 =0;
00408       int i2 =0;
00409       if( iz>0 ) { // EB plus phi turns opposite to HV numbering
00410         if(i_local_hv==1) { 
00411           i1=3 ; 
00412           i2=4 ; 
00413         } else { 
00414           i1=1 ; 
00415           i2=2 ; 
00416         }
00417       } else { // EB minus phi turns as HV numbering 
00418         if(i_local_hv==1) { 
00419           i1=1 ; 
00420           i2=2 ; 
00421         } else { 
00422           i1=3 ; 
00423           i2=4 ; 
00424         }
00425       }
00426       int ioffset=0;
00427       if(iz==1) ioffset=(sm-1)*4; 
00428       if(iz==-1) ioffset=(sm-18-1)*4; 
00429       i1=i1+ioffset;
00430       i2=i2+ioffset;
00431 
00432       int *result = new int[5];
00433       
00434       result[0]=iz;
00435       result[1]=i1;
00436       result[2]=i2;
00437       result[3]=j;
00438       result[4]=j;
00439 
00440       return result; 
00441 
00442 }
00443 
00444 int * popcon::EcalDCSHandler::LVLogicIDToDetID(int sm, int chan) const {
00445   // returns the numbers iz, i1, i2 and j1, j2 on which to loop for the towers
00446 
00447       int iz=-1;
00448       if(sm>0 && sm <= 18) iz=1;
00449 
00450       int j1=0;
00451       int j2=0;
00452       int i1=0;
00453       int i2=0;
00454 
00455       if(chan==1) {
00456         i1=1;
00457         i2=4;
00458         j1=1;
00459         j2=1;
00460       } else {
00461         int ch2= (chan/2)*2;
00462         if(ch2==chan) {
00463           j1=chan;
00464         } else {
00465           j1=chan-1;
00466         }
00467         j2 = j1+1; 
00468         if( iz>0 ) { // EB plus phi turns opposite to LV numbering
00469           if(ch2==chan) { 
00470             i1=3 ; 
00471             i2=4 ; 
00472           } else { 
00473             i1=1 ; 
00474             i2=2 ; 
00475           }
00476         } else { // EB minus phi turns as LV numbering 
00477           if(ch2==chan) { 
00478             i1=1 ; 
00479             i2=2 ; 
00480           } else { 
00481             i1=3 ; 
00482             i2=4 ; 
00483           }
00484         }
00485       }
00486       int ioffset=0;
00487       if(iz==1) ioffset=(sm-1)*4; 
00488       if(iz==-1) ioffset=(sm-18-1)*4; 
00489       i1=i1+ioffset;
00490       i2=i2+ioffset;
00491 
00492 
00493       int *result = new int[5];
00494       result[0]=iz;
00495       result[1]=i1;
00496       result[2]=i2;
00497       result[3]=j1;
00498       result[4]=j2;
00499 
00500       return result; 
00501 }
00502 
00503 
00504 bool popcon::EcalDCSHandler::insertLVDataSetToOffline( const std::map<EcalLogicID, RunDCSLVDat>* dataset, EcalDCSTowerStatus* dcs_temp , std::vector<EcalLogicID> my_EELVchan ) const
00505 {
00506   bool result= false; 
00507   if (dataset->size() == 0) {
00508     std::cout << "No data in map!" << std::endl;
00509   }
00510   EcalLogicID ecid;
00511   RunDCSLVDat lv;
00512 
00513 
00514   typedef std::map< EcalLogicID, RunDCSLVDat >::const_iterator CI;
00515   for (CI p = dataset->begin(); p != dataset->end(); ++p) {
00516 
00517     ecid = p->first;
00518     lv  = p->second;
00519 
00520     if(ecid.getName()=="EB_LV_channel"){
00521 
00522       int sm= ecid.getID1() ;
00523       int chan= ecid.getID2();
00524 
00525       int* limits=0;
00526       limits=   LVLogicIDToDetID(sm,chan);
00527       int iz=limits[0];
00528       int i1=limits[1];
00529       int i2=limits[2];
00530       int j1=limits[3];
00531       int j2=limits[4];
00532 
00533       for(int ik=i1; ik<=i2; ik++){ 
00534         for(int j=j1; j<=j2; j++){ 
00535           if (EcalTrigTowerDetId::validDetId(iz,EcalBarrel,j,ik )){
00536             EcalTrigTowerDetId ebid(iz,EcalBarrel,j,ik);
00537             EcalDCSTowerStatus::const_iterator it =dcs_temp->find(ebid.rawId());
00538             uint16_t dbStatus = 0;
00539             if ( it != dcs_temp->end() ) {
00540               dbStatus = it->getStatusCode();
00541             }
00542             uint16_t new_dbStatus= updateLV(&lv, dbStatus); 
00543             if(new_dbStatus != dbStatus ) result=true; 
00544             dcs_temp->setValue( ebid, new_dbStatus );
00545 
00546           if(new_dbStatus != dbStatus) {
00547             std::cout <<"SM/chan:"<<sm<<"/"<<chan <<" new db status ="<< new_dbStatus << " old  "<<dbStatus<<" LV: "<< lv.getLV()<<"/"<<lv.getLVNominal()<<std::endl;
00548             
00549           } 
00550 
00551 
00552           }
00553         }
00554       }
00555     delete [] limits; 
00556 
00557       
00558     } else {
00559         
00560         // endcaps 
00561       int dee= ecid.getID1() ;
00562       int chan= ecid.getID2();
00563       int n=my_EELVchan.size();
00564      
00565         for (int ixt=0; ixt<n; ixt++) {
00566           if(my_EELVchan[ixt].getID1()==dee && my_EELVchan[ixt].getID2()==chan){
00567 
00568             int ilogic=my_EELVchan[ixt].getLogicID();
00569             
00570             if(ilogic == 2012058060 || ilogic == 2010060058 
00571                ||  ilogic == 2012043041 || ilogic == 2010041043) {
00572               std::cout<< "crystal " << ilogic << " in the corner ignored" << std::endl; 
00573             } else {
00574               
00575             int iz= (ilogic/1000000)-2010;
00576             if(iz==0) iz=-1;
00577             if(iz==2) iz=1;
00578             if(iz != 1 && iz!= -1) std::cout<< "BAD z"<< std::endl; 
00579             
00580             int iy=ilogic- int(ilogic/1000)*1000;
00581             
00582             int ix=(ilogic- int(ilogic/1000000)*1000000 -iy)/1000;
00583             
00584             int ixtower=  ((ix-1)/5) +1;
00585             int iytower=  ((iy-1)/5) +1;
00586             
00587             if(ixtower<1 || ixtower>20 || iytower <1 || iytower >20) 
00588               std::cout<< "BAD x/y"<<ilogic<<"/"<< ixtower<<"/"<<iytower<< std::endl;
00589             
00590             if (EcalScDetId::validDetId(ixtower,iytower,iz )){
00591               EcalScDetId eeid(ixtower,iytower,iz );
00592               EcalDCSTowerStatus::const_iterator it =dcs_temp->find(eeid.rawId());
00593               uint16_t dbStatus = 0;
00594               if ( it != dcs_temp->end() ) {
00595                 dbStatus = it->getStatusCode();
00596               }
00597               
00598               uint16_t new_dbStatus= updateLV(&lv, dbStatus);
00599               if(new_dbStatus != dbStatus ) result=true;
00600               dcs_temp->setValue( eeid, new_dbStatus );
00601               
00602               //  std::cout <<"Dee/chan:"<<dee<<"/"<<chan <<" new db status ="<< new_dbStatus << " old  "<<dbStatus<<" LV: "<< lv.getLV()<<"/"<<lv.getLVNominal()<<" ilogic/x/y " <<ilogic<<"/"<< ixtower<<"/"<<iytower<<std::endl;
00603               
00604               if(new_dbStatus != dbStatus) {
00605                 std::cout <<"Dee/chan:"<<dee<<"/"<<chan <<" new db status ="<< new_dbStatus << " old  "<<dbStatus<<" LV: "<< lv.getLV()<<"/"<<lv.getLVNominal()<<std::endl;
00606                 
00607               } 
00608               
00609             }
00610             
00611             }
00612           
00613             
00614           } 
00615         
00616         }
00617         
00618     }// end of endcaps 
00619         
00620 
00621 
00622   }
00623   return result; 
00624 }
00625 
00626 void popcon::EcalDCSHandler::getNewObjects()
00627 {
00628   bool lot_of_printout=false; 
00629         std::cout << "------- Ecal DCS - > getNewObjects\n";
00630 
00631         std::ostringstream ss; 
00632         ss<<"ECAL ";
00633 
00634         unsigned long long max_since= 1;
00635 
00636         max_since=tagInfo().lastInterval.first;
00637         std::cout << "max_since : "  << max_since << std::endl;
00638         Ref dcs_db = lastPayload();
00639         std::cout << "retrieved last payload "  << std::endl;
00640         
00641         // we copy the last valid record to a temporary object 
00642         EcalDCSTowerStatus* dcs_temp = new EcalDCSTowerStatus();
00643 
00644         // barrel
00645         int iz=0;
00646         for(int k=0 ; k<2; k++ ) {
00647           if(k==0) iz=-1;
00648           if(k==1) iz= 1;
00649           for(int i=1 ; i<73; i++) {
00650             for(int j=1 ; j<18; j++) {
00651               if (EcalTrigTowerDetId::validDetId(iz,EcalBarrel,j,i )){
00652                 EcalTrigTowerDetId ebid(iz,EcalBarrel,j,i);
00653 
00654                 uint16_t dbStatus = 0;
00655                 dbStatus =(dcs_db->barrel( ebid.hashedIndex())).getStatusCode();
00656 
00657 
00658                 EcalDCSTowerStatus::const_iterator it =dcs_db->find(ebid.rawId());
00659                 if ( it != dcs_db->end() ) {
00660                 } else {
00661                   std::cout<<"*** error channel not found: j/i="<<j<<"/"<<i << std::endl;
00662                 }
00663                 
00664                 dcs_temp->setValue( ebid, dbStatus );
00665               }
00666             }
00667           }
00668         }
00669 
00670         // endcap
00671         for(int k=0 ; k<2; k++ ) {
00672           if(k==0) iz=-1;
00673           if(k==1) iz=+1;
00674           for(int i=1 ; i<21; i++) {
00675             for(int j=1 ; j<21; j++) {
00676               if (EcalScDetId::validDetId(i,j,iz )){
00677                 EcalScDetId eeid(i,j,iz);
00678 
00679                 EcalDCSTowerStatus::const_iterator it =dcs_db->find(eeid.rawId());
00680 
00681                 uint16_t dbStatus = 0;
00682                 if ( it != dcs_db->end() ) {
00683                   dbStatus = it->getStatusCode();
00684                 } 
00685                 dcs_temp->setValue( eeid, dbStatus );
00686               }
00687             }
00688           }
00689         }
00690 
00691 
00692         // now read the actual status from the online DB
00693 
00694 
00695         econn = new EcalCondDBInterface( m_sid, m_user, m_pass );
00696         std::cout << "Connection done" << std::endl;
00697         
00698         if (!econn)
00699           {
00700             std::cout << " Problem with OMDS: connection parameters " <<m_sid <<"/"<<m_user<<std::endl;
00701             throw cms::Exception("OMDS not available");
00702           } 
00703 
00704 
00705 
00706         std::cout << "Retrieving last run from ONLINE DB ... " << std::endl;
00707         std::map<EcalLogicID, RunDat> rundat;
00708         RunIOV rp ;
00709         run_t runmax=10000000;
00710         std::string location_p5="P5_Co";
00711         econn->fetchValidDataSet(&rundat , &rp, location_p5 ,runmax);
00712         
00713         unsigned long long  irun=(unsigned long long) rp.getRunNumber();
00714 
00715         // just for testing purposes
00716         //      irun= max_since+1; 
00717         
00718         if(max_since< irun) { 
00719 
00720 
00721           // get the map of the EE LV channels to EE crystals 
00722 
00723           std::cout << "Retrieving endcap channel list from ONLINE DB ... " << std::endl;
00724           
00725           std::vector<EcalLogicID> my_EELVchan= econn->getEcalLogicIDSetOrdered( "EE_crystal_number", 1,4,
00726                                                  1, 200, EcalLogicID::NULLID, EcalLogicID::NULLID,
00727                                                  "EE_LV_channel", 12 ) ;
00728 
00729           std::cout << "done endcap channel list  ... " << std::endl;
00730 
00731           // retrieve from last value data record       
00732           // always call this method at first run
00733 
00734           std::map<EcalLogicID, RunDCSHVDat> dataset;
00735           RunIOV *r = NULL;
00736           econn->fetchDataSet(&dataset, r);
00737           
00738           if (!dataset.size()) {
00739             throw(std::runtime_error("Zero rows read back"));
00740           }
00741           
00742           
00743           if(lot_of_printout) std::cout << "read OK" << std::endl;
00744           if(lot_of_printout) printHVDataSet(&dataset,10);
00745           
00746           std::map<EcalLogicID, RunDCSLVDat> dataset_lv;
00747           econn->fetchDataSet(&dataset_lv, r);
00748           
00749           if (!dataset_lv.size()) {
00750             throw(std::runtime_error("Zero rows read back"));
00751           }
00752           if(lot_of_printout) std::cout << "read OK" << std::endl;
00753           if(lot_of_printout) printLVDataSet(&dataset_lv);
00754           
00755           bool somediff_hv= insertHVDataSetToOffline(&dataset, dcs_temp );
00756           bool somediff_lv= insertLVDataSetToOffline(&dataset_lv, dcs_temp, my_EELVchan );
00757           
00758           if(somediff_hv || somediff_lv) {
00759             
00760 
00761             /*      Tm t_now_gmt;
00762                     t_now_gmt.setToCurrentGMTime();
00763                     uint64_t tsincetemp= t_now_gmt.microsTime()/1000000 ;
00764                     uint64_t tsince = tsincetemp<< 32; 
00765                     std::cout << "Generating popcon record for time " << tsincetemp << "..." << std::flush;
00766             
00767             */
00768 
00769             std::cout << "Generating popcon record for run " << irun << "..." << std::flush;
00770     
00771             // this is for timestamp
00772             //      m_to_transfer.push_back(std::make_pair((EcalDCSTowerStatus*)dcs_temp,tsince));
00773             //      ss << "Time=" << t_now_gmt.str() << "_DCSchanged_"<<std::endl; 
00774 
00775             // this is for run number 
00776             m_to_transfer.push_back(std::make_pair((EcalDCSTowerStatus*)dcs_temp,irun));
00777             ss << "Run=" << irun << "_DCSchanged_"<<std::endl; 
00778 
00779             m_userTextLog = ss.str()+";";
00780 
00781           } else {
00782 
00783             // Tm t_now_gmt;
00784             // t_now_gmt.setToCurrentGMTime();
00785 
00786             std::cout<< "Run DCS record was the same as previous run " << std::endl;
00787             ss << "Run=" << irun << "_DCSchanged_"<<std::endl; 
00788             m_userTextLog = ss.str()+";";
00789             
00790             delete dcs_temp; 
00791             
00792           }
00793 
00794           /*      
00795           
00796         } else {
00797           
00798           // here we fetch historical data 
00799 
00800           uint64_t t_max_val= ((max_since >> 32 ) +2)*1000000 ; // time in microseconds  (2 seconds more than old data)
00801         
00802           Tm t_test(t_max_val);
00803   
00804           std::list< std::pair< Tm, std::map<  EcalLogicID, RunDCSHVDat > > > dataset;
00805           econn->fetchDCSDataSet(&dataset, t_test);
00806           
00807           if (!dataset.size()) {
00808             std::cout<< " DCS query retrieved zero lines  "<< std::endl;
00809           } else {
00810 
00811             int num_dcs=0; 
00812             std::list< std::pair< Tm, std::map<  EcalLogicID, RunDCSHVDat > > >::iterator it;
00813             for (it=dataset.begin(); it!=dataset.end(); ++it){
00814               std::pair< Tm, std::map<  EcalLogicID, RunDCSHVDat > > a_pair =(*it);
00815               Tm t_pair=a_pair.first;
00816               std::map<  EcalLogicID, RunDCSHVDat > a_map = a_pair.second;
00817               num_dcs=num_dcs+a_map.size();
00818 
00819               bool somediff_hv= insertHVDataSetToOffline(&a_map, dcs_temp );
00820 
00821 
00822               if(somediff_hv ) {
00823                 std::cout << "some diff" << std::endl;
00824                 // we have to copy this record to offline 
00825                 // we copy dcs_temp to dcs_pop
00826                 EcalDCSTowerStatus* dcs_pop = new EcalDCSTowerStatus();
00827                 
00828                 // barrel
00829                 int iz=0;
00830                 for(int k=0 ; k<2; k++ ) {
00831                   if(k==0) iz=-1;
00832                   if(k==1) iz= 1;
00833                   for(int i=1 ; i<73; i++) {
00834                     for(int j=1 ; j<18; j++) {
00835                       if (EcalTrigTowerDetId::validDetId(iz,EcalBarrel,j,i )){
00836                         EcalTrigTowerDetId ebid(iz,EcalBarrel,j,i);
00837                         
00838                         uint16_t dbStatus = 0;
00839                         dbStatus =(dcs_temp->barrel( ebid.hashedIndex())).getStatusCode();
00840                         
00841                         EcalDCSTowerStatus::const_iterator it =dcs_temp->find(ebid.rawId());
00842                         if ( it != dcs_temp->end() ) {
00843                         } else {
00844                           std::cout<<"*** error channel not found: j/i="<<j<<"/"<<i << std::endl;
00845                         }
00846                         
00847                         dcs_pop->setValue( ebid, dbStatus );
00848                       }
00849                     }
00850                   }
00851                 }
00852                 
00853                 // endcap
00854                 for(int k=0 ; k<2; k++ ) {
00855                   if(k==0) iz=-1;
00856                   if(k==1) iz=+1;
00857                   for(int i=1 ; i<21; i++) {
00858                     for(int j=1 ; j<21; j++) {
00859                       if (EcalScDetId::validDetId(i,j,iz )){
00860                         EcalScDetId eeid(i,j,iz);
00861                         
00862                         EcalDCSTowerStatus::const_iterator it =dcs_temp->find(eeid.rawId());
00863                         
00864                         uint16_t dbStatus = 0;
00865                         if ( it != dcs_temp->end() ) {
00866                           dbStatus = it->getStatusCode();
00867                         } 
00868                         dcs_pop->setValue( eeid, dbStatus );
00869                       }
00870                     }
00871                   }
00872                 }
00873                 
00874                 uint64_t tsincetemp= t_pair.microsTime()/1000000 ;
00875                 
00876                 uint64_t tsince = tsincetemp<< 32; 
00877                 Tm tnew(t_pair.microsTime());
00878                 
00879                 std::cout << "Generating popcon record for time " << tsince << "HRF time " << tnew.str() << "..." << std::flush;
00880                 
00881                 m_to_transfer.push_back(std::make_pair((EcalDCSTowerStatus*)dcs_pop,tsince));
00882                 
00883                 ss << "Time=" << tnew.str() << "_DCSchanged_"<<std::endl; 
00884                 m_userTextLog = ss.str()+";";
00885                 
00886                 
00887 
00888 
00889             }
00890             }
00891 
00892 
00893         
00894           std::cout << " num DCS = "<< num_dcs << std::endl; 
00895         }
00896 
00897 
00898 
00899           delete dcs_temp;
00900           */
00901           
00902         }
00903 
00904         delete econn;
00905         std::cout << "Ecal - > end of getNewObjects -----------\n";
00906 
00907 }
00908 
00909