CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_3/src/DQM/SiStripMonitorClient/plugins/SiStripDcsInfo.cc

Go to the documentation of this file.
00001 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00002 #include "FWCore/ServiceRegistry/interface/Service.h"
00003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00004 
00005 #include "DQMServices/Core/interface/DQMStore.h"
00006 #include "DQMServices/Core/interface/MonitorElement.h"
00007 
00008 #include "DQM/SiStripCommon/interface/SiStripFolderOrganizer.h"
00009 #include "DQM/SiStripMonitorClient/plugins/SiStripDcsInfo.h"
00010 #include "DQM/SiStripMonitorClient/interface/SiStripUtility.h"
00011 
00012 #include "CondFormats/SiStripObjects/interface/SiStripDetVOff.h"
00013 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
00014 #include "CalibTracker/Records/interface/SiStripDetCablingRcd.h"
00015 #include "CalibFormats/SiStripObjects/interface/SiStripDetCabling.h"
00016 
00017 //Run Info
00018 #include "CondFormats/DataRecord/interface/RunSummaryRcd.h"
00019 #include "CondFormats/RunInfo/interface/RunSummary.h"
00020 #include "CondFormats/RunInfo/interface/RunInfo.h"
00021 
00022 #include <iostream>
00023 #include <iomanip>
00024 #include <stdio.h>
00025 #include <string>
00026 #include <sstream>
00027 #include <math.h>
00028 
00029 //
00030 // -- Contructor
00031 //
00032 SiStripDcsInfo::SiStripDcsInfo(edm::ParameterSet const& pSet) : 
00033     dqmStore_(edm::Service<DQMStore>().operator->()), 
00034     m_cacheIDCabling_(0),
00035     m_cacheIDDcs_(0),
00036     bookedStatus_(false),
00037     nLumiAnalysed_(0)
00038 { 
00039   // Create MessageSender
00040   LogDebug( "SiStripDcsInfo") << "SiStripDcsInfo::Deleting SiStripDcsInfo ";
00041 }
00042 //
00043 // -- Destructor
00044 //
00045 SiStripDcsInfo::~SiStripDcsInfo() {
00046   LogDebug("SiStripDcsInfo") << "SiStripDcsInfo::Deleting SiStripDcsInfo ";
00047 
00048 }
00049 //
00050 // -- Begin Job
00051 //
00052 void SiStripDcsInfo::beginJob() {
00053   std::string tag;
00054   SubDetMEs local_mes;
00055   
00056   tag = "TIB";   
00057   local_mes.folder_name = "TIB";
00058   local_mes.DcsFractionME  = 0;
00059   local_mes.TotalDetectors = 0; 
00060   local_mes.FaultyDetectors.clear();
00061   SubDetMEsMap.insert(std::pair<std::string, SubDetMEs >(tag, local_mes));
00062       
00063   tag = "TOB";
00064   local_mes.folder_name = "TOB";
00065   local_mes.DcsFractionME  = 0;
00066   local_mes.TotalDetectors = 0; 
00067   local_mes.FaultyDetectors.clear();
00068   SubDetMEsMap.insert(std::pair<std::string, SubDetMEs >(tag, local_mes));
00069 
00070   tag = "TECB";
00071   local_mes.folder_name = "TEC/side_1";
00072   local_mes.DcsFractionME  = 0;
00073   local_mes.TotalDetectors = 0; 
00074   local_mes.FaultyDetectors.clear();
00075   SubDetMEsMap.insert(std::pair<std::string, SubDetMEs >(tag, local_mes));
00076 
00077   tag = "TECF";
00078   local_mes.folder_name = "TEC/side_2";
00079   local_mes.DcsFractionME  = 0;
00080   local_mes.TotalDetectors = 0; 
00081   local_mes.FaultyDetectors.clear();
00082   SubDetMEsMap.insert(std::pair<std::string, SubDetMEs >(tag, local_mes));
00083 
00084   tag = "TIDB";
00085   local_mes.folder_name = "TID/side_1";
00086   local_mes.DcsFractionME  = 0;
00087   local_mes.TotalDetectors = 0; 
00088   local_mes.FaultyDetectors.clear();
00089   SubDetMEsMap.insert(std::pair<std::string, SubDetMEs >(tag, local_mes));
00090 
00091   tag = "TIDF";
00092   local_mes.folder_name = "TID/side_2";
00093   local_mes.DcsFractionME  = 0;
00094   local_mes.TotalDetectors = 0; 
00095   local_mes.FaultyDetectors.clear();
00096   SubDetMEsMap.insert(std::pair<std::string, SubDetMEs >(tag, local_mes));
00097 }
00098 //
00099 // -- Begin Run
00100 //
00101 void SiStripDcsInfo::beginRun(edm::Run const& run, edm::EventSetup const& eSetup) {
00102   LogDebug ("SiStripDcsInfo") <<"SiStripDcsInfo:: Begining of Run";
00103   nFEDConnected_ = 0;
00104   const int siStripFedIdMin = FEDNumbering::MINSiStripFEDID;
00105   const int siStripFedIdMax = FEDNumbering::MAXSiStripFEDID; 
00106 
00107   // Count Tracker FEDs from RunInfo
00108   edm::eventsetup::EventSetupRecordKey recordKey(edm::eventsetup::EventSetupRecordKey::TypeTag::findType("RunInfoRcd"));
00109   if( eSetup.find( recordKey ) != 0) {
00110     
00111     edm::ESHandle<RunInfo> sumFED;
00112     eSetup.get<RunInfoRcd>().get(sumFED);    
00113     
00114     if ( sumFED.isValid() ) {
00115       std::vector<int> FedsInIds= sumFED->m_fed_in;   
00116       for(unsigned int it = 0; it < FedsInIds.size(); ++it) {
00117         int fedID = FedsInIds[it];     
00118         if(fedID>=siStripFedIdMin &&  fedID<=siStripFedIdMax)  ++nFEDConnected_;
00119       }
00120       LogDebug ("SiStripDcsInfo") << " SiStripDcsInfo :: Connected FEDs " << nFEDConnected_;
00121     }
00122   } 
00123 
00124   bookStatus();
00125   fillDummyStatus();
00126   if (nFEDConnected_ > 0) readCabling(eSetup);
00127 }
00128 //
00129 // -- Analyze
00130 //
00131 void SiStripDcsInfo::analyze(edm::Event const& event, edm::EventSetup const& eSetup) {
00132 }
00133 //
00134 // -- Begin Luminosity Block
00135 //
00136 void SiStripDcsInfo::beginLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) {
00137   LogDebug( "SiStripDcsInfo") << "SiStripDcsInfo::beginLuminosityBlock";
00138   
00139   if (nFEDConnected_ == 0) return;
00140 
00141   // initialise BadModule list 
00142   for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00143     it->second.FaultyDetectors.clear();
00144   }
00145   readStatus(eSetup);
00146   nLumiAnalysed_++;   
00147 }
00148 
00149 //
00150 // -- End Luminosity Block
00151 //
00152 void SiStripDcsInfo::endLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) {
00153   LogDebug( "SiStripDcsInfo") << "SiStripDcsInfo::endLuminosityBlock";
00154 
00155   if (nFEDConnected_ == 0) return;
00156   readStatus(eSetup);
00157   fillStatus();
00158 }
00159 //
00160 // -- End Run
00161 //
00162 void SiStripDcsInfo::endRun(edm::Run const& run, edm::EventSetup const& eSetup){
00163   LogDebug ("SiStripDcsInfo") <<"SiStripDcsInfo::EndRun";
00164 
00165   if (nFEDConnected_ == 0) return;
00166 
00167   for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00168     it->second.FaultyDetectors.clear();
00169   }
00170   readStatus(eSetup); 
00171   fillStatus();
00172   addBadModules();
00173 } 
00174 //
00175 // -- Book MEs for SiStrip Dcs Fraction
00176 //
00177 void SiStripDcsInfo::bookStatus() {
00178   if (!bookedStatus_) {
00179     std::string strip_dir = "";
00180     SiStripUtility::getTopFolderPath(dqmStore_, "SiStrip", strip_dir); 
00181     if (strip_dir.size() > 0) dqmStore_->setCurrentFolder(strip_dir+"/EventInfo");
00182     else dqmStore_->setCurrentFolder("SiStrip/EventInfo");
00183        
00184     DcsFraction_= dqmStore_->bookFloat("DCSSummary");  
00185     
00186     DcsFraction_->setLumiFlag();
00187     
00188     dqmStore_->cd();
00189     if (strip_dir.size() > 0)  dqmStore_->setCurrentFolder(strip_dir+"/EventInfo/DCSContents");
00190     else dqmStore_->setCurrentFolder("SiStrip/EventInfo/DCSContents"); 
00191     for (std::map<std::string,SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00192       SubDetMEs local_mes;      
00193       std::string me_name;
00194       me_name = "SiStrip_" + it->first;
00195       it->second.DcsFractionME = dqmStore_->bookFloat(me_name);  
00196       it->second.DcsFractionME->setLumiFlag();  
00197     } 
00198     bookedStatus_ = true;
00199     dqmStore_->cd();
00200   }
00201 }
00202 //
00203 // -- Read Cabling
00204 // 
00205 void SiStripDcsInfo::readCabling(edm::EventSetup const& eSetup) {
00206   unsigned long long cacheID = eSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00207   if (m_cacheIDCabling_ != cacheID) {
00208     m_cacheIDCabling_ = cacheID;
00209     LogDebug("SiStripDcsInfo") <<"SiStripDcsInfo::readCabling : "
00210                                    << " Change in Cache";
00211     eSetup.get<SiStripDetCablingRcd>().get(detCabling_);
00212 
00213     std::vector<uint32_t> SelectedDetIds;
00214     detCabling_->addActiveDetectorsRawIds(SelectedDetIds);
00215     LogDebug( "SiStripDcsInfo") << " SiStripDcsInfo::readCabling : "  
00216                                     << " Total Detectors " << SelectedDetIds.size();
00217     
00218 
00219     // initialise total # of detectors first
00220     for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00221       it->second.TotalDetectors = 0;
00222     }
00223     
00224     for (std::vector<uint32_t>::const_iterator idetid=SelectedDetIds.begin(); idetid != SelectedDetIds.end(); ++idetid){    
00225       uint32_t detId = *idetid;
00226       if (detId == 0 || detId == 0xFFFFFFFF) continue;
00227       std::string subdet_tag;
00228       SiStripUtility::getSubDetectorTag(detId,subdet_tag);         
00229       
00230       std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
00231       if (iPos != SubDetMEsMap.end()){    
00232         iPos->second.TotalDetectors++;
00233       }
00234     }
00235   }
00236 }
00237 //
00238 // -- Get Faulty Detectors
00239 //
00240 void SiStripDcsInfo::readStatus(edm::EventSetup const& eSetup) {
00241 
00242   eSetup.get<SiStripDetVOffRcd>().get(siStripDetVOff_);
00243   std::vector <uint32_t> FaultyDetIds;
00244   siStripDetVOff_->getDetIds(FaultyDetIds);
00245   LogDebug( "SiStripDcsInfo") << " SiStripDcsInfo::readStatus : "
00246                                   << " Faulty Detectors " << FaultyDetIds.size();
00247   // Read and fille bad modules
00248   for (std::vector<uint32_t>::const_iterator ihvoff=FaultyDetIds.begin(); ihvoff!=FaultyDetIds.end();++ihvoff){
00249     uint32_t detId_hvoff = (*ihvoff);
00250     if (!detCabling_->IsConnected(detId_hvoff)) continue;    
00251     std::string subdet_tag;
00252     SiStripUtility::getSubDetectorTag(detId_hvoff,subdet_tag);         
00253     
00254     std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
00255     if (iPos != SubDetMEsMap.end()){  
00256       std::vector<uint32_t>::iterator ibad = std::find(iPos->second.FaultyDetectors.begin(), iPos->second.FaultyDetectors.end(), detId_hvoff);
00257       if (ibad ==  iPos->second.FaultyDetectors.end()) iPos->second.FaultyDetectors.push_back( detId_hvoff);
00258     }
00259   }
00260 }
00261 //
00262 // -- Fill Status
00263 //
00264 void SiStripDcsInfo::fillStatus(){
00265   if (!bookedStatus_) bookStatus();
00266   if (bookedStatus_) {
00267     float total_det = 0.0;
00268     float faulty_det = 0.0;
00269     float fraction;
00270     for (std::map<std::string,SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00271       int total_subdet  = it->second.TotalDetectors;
00272       int faulty_subdet = it->second.FaultyDetectors.size(); 
00273       if  (nFEDConnected_ == 0  || total_subdet == 0) fraction = -1;
00274       else fraction = 1.0  - faulty_subdet*1.0/total_subdet;   
00275       it->second.DcsFractionME->Reset();
00276       it->second.DcsFractionME->Fill(fraction);
00277       edm::LogInfo( "SiStripDcsInfo") << " SiStripDcsInfo::fillStatus : Sub Detector "
00278         << it->first << " Total Number " << total_subdet  
00279         << " Faulty ones " << faulty_subdet;
00280       total_det += total_subdet;
00281       faulty_det += faulty_subdet;
00282     }
00283     if (nFEDConnected_ == 0 || total_det == 0) fraction = -1.0;
00284     else fraction = 1 - faulty_det/total_det;
00285     DcsFraction_->Reset();
00286     DcsFraction_->Fill(fraction);
00287   } 
00288 }
00289 //
00290 // -- Fill with Dummy values
00291 //
00292 void SiStripDcsInfo::fillDummyStatus() {
00293   if (!bookedStatus_) bookStatus();
00294   if (bookedStatus_) {
00295     for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00296       it->second.DcsFractionME->Reset();
00297       it->second.DcsFractionME->Fill(-1.0);
00298     }
00299     DcsFraction_->Reset();
00300     DcsFraction_->Fill(-1.0);
00301   }
00302 }
00303 //
00304 // -- Add Bad Modules
00305 //
00306 void SiStripDcsInfo::addBadModules() {
00307     
00308   dqmStore_->cd();
00309   std::string mdir = "MechanicalView";
00310   if (!SiStripUtility::goToDir(dqmStore_, mdir)) {
00311     dqmStore_->setCurrentFolder("SiStrip/"+mdir);
00312   }
00313   std::string mechanical_dir = dqmStore_->pwd();
00314   std::string tag = "DCSError";
00315 
00316   for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00317     std::vector<uint32_t> badModules = it->second.FaultyDetectors; 
00318     for (std::vector<uint32_t>::iterator ibad = badModules.begin(); 
00319          ibad != badModules.end(); ibad++) {
00320 
00321       std::string bad_module_folder = mechanical_dir + "/" +
00322                                       it->second.folder_name + "/"     
00323                                       "BadModuleList";      
00324       dqmStore_->setCurrentFolder(bad_module_folder);
00325 
00326       std::ostringstream detid_str;
00327       detid_str << (*ibad);
00328       std::string full_path = bad_module_folder + "/" + detid_str.str();
00329       MonitorElement* me = dqmStore_->get(full_path);
00330       uint16_t flag = 0; 
00331       if (me) {
00332         flag = me->getIntValue();
00333         me->Reset();
00334       } else me = dqmStore_->bookInt(detid_str.str());
00335       SiStripUtility::setBadModuleFlag(tag, flag);
00336       me->Fill(flag);
00337     }
00338   }   
00339   dqmStore_->cd();
00340 }
00341 
00342 #include "FWCore/Framework/interface/MakerMacros.h"
00343 DEFINE_FWK_MODULE(SiStripDcsInfo);