CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/DQM/SiStripMonitorClient/src/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/interface/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 FEDNumbering numbering;
00105   const int siStripFedIdMin = numbering.MINSiStripFEDID;
00106   const int siStripFedIdMax = numbering.MAXSiStripFEDID; 
00107 
00108   // Count Tracker FEDs from RunInfo
00109   edm::eventsetup::EventSetupRecordKey recordKey(edm::eventsetup::EventSetupRecordKey::TypeTag::findType("RunInfoRcd"));
00110   if( eSetup.find( recordKey ) != 0) {
00111     
00112     edm::ESHandle<RunInfo> sumFED;
00113     eSetup.get<RunInfoRcd>().get(sumFED);    
00114     
00115     if ( sumFED.isValid() ) {
00116       std::vector<int> FedsInIds= sumFED->m_fed_in;   
00117       for(unsigned int it = 0; it < FedsInIds.size(); ++it) {
00118         int fedID = FedsInIds[it];     
00119         if(fedID>=siStripFedIdMin &&  fedID<=siStripFedIdMax)  ++nFEDConnected_;
00120       }
00121       LogDebug ("SiStripDcsInfo") << " SiStripDcsInfo :: Connected FEDs " << nFEDConnected_;
00122     }
00123   } 
00124 
00125   bookStatus();
00126   fillDummyStatus();
00127   if (nFEDConnected_ > 0) readCabling(eSetup);
00128 }
00129 //
00130 // -- Analyze
00131 //
00132 void SiStripDcsInfo::analyze(edm::Event const& event, edm::EventSetup const& eSetup) {
00133 }
00134 //
00135 // -- Begin Luminosity Block
00136 //
00137 void SiStripDcsInfo::beginLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) {
00138   LogDebug( "SiStripDcsInfo") << "SiStripDcsInfo::beginLuminosityBlock";
00139   
00140   if (nFEDConnected_ == 0) return;
00141 
00142   // initialise BadModule list 
00143   for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00144     it->second.FaultyDetectors.clear();
00145   }
00146   readStatus(eSetup);
00147   nLumiAnalysed_++;   
00148 }
00149 
00150 //
00151 // -- End Luminosity Block
00152 //
00153 void SiStripDcsInfo::endLuminosityBlock(edm::LuminosityBlock const& lumiSeg, edm::EventSetup const& eSetup) {
00154   LogDebug( "SiStripDcsInfo") << "SiStripDcsInfo::endLuminosityBlock";
00155 
00156   if (nFEDConnected_ == 0) return;
00157   readStatus(eSetup);
00158   fillStatus();
00159 }
00160 //
00161 // -- End Run
00162 //
00163 void SiStripDcsInfo::endRun(edm::Run const& run, edm::EventSetup const& eSetup){
00164   LogDebug ("SiStripDcsInfo") <<"SiStripDcsInfo::EndRun";
00165 
00166   if (nFEDConnected_ == 0) return;
00167 
00168   for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00169     it->second.FaultyDetectors.clear();
00170   }
00171   readStatus(eSetup); 
00172   fillStatus();
00173   addBadModules();
00174 } 
00175 //
00176 // -- Book MEs for SiStrip Dcs Fraction
00177 //
00178 void SiStripDcsInfo::bookStatus() {
00179   if (!bookedStatus_) {
00180     std::string strip_dir = "";
00181     SiStripUtility::getTopFolderPath(dqmStore_, "SiStrip", strip_dir); 
00182     if (strip_dir.size() > 0) dqmStore_->setCurrentFolder(strip_dir+"/EventInfo");
00183     else dqmStore_->setCurrentFolder("SiStrip/EventInfo");
00184        
00185     DcsFraction_= dqmStore_->bookFloat("DCSSummary");  
00186     
00187     DcsFraction_->setLumiFlag();
00188     
00189     dqmStore_->cd();
00190     if (strip_dir.size() > 0)  dqmStore_->setCurrentFolder(strip_dir+"/EventInfo/DCSContents");
00191     else dqmStore_->setCurrentFolder("SiStrip/EventInfo/DCSContents"); 
00192     for (std::map<std::string,SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00193       SubDetMEs local_mes;      
00194       std::string me_name;
00195       me_name = "SiStrip_" + it->first;
00196       it->second.DcsFractionME = dqmStore_->bookFloat(me_name);  
00197       it->second.DcsFractionME->setLumiFlag();  
00198     } 
00199     bookedStatus_ = true;
00200     dqmStore_->cd();
00201   }
00202 }
00203 //
00204 // -- Read Cabling
00205 // 
00206 void SiStripDcsInfo::readCabling(edm::EventSetup const& eSetup) {
00207   unsigned long long cacheID = eSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00208   if (m_cacheIDCabling_ != cacheID) {
00209     m_cacheIDCabling_ = cacheID;
00210     LogDebug("SiStripDcsInfo") <<"SiStripDcsInfo::readCabling : "
00211                                    << " Change in Cache";
00212     eSetup.get<SiStripDetCablingRcd>().get(detCabling_);
00213 
00214     std::vector<uint32_t> SelectedDetIds;
00215     detCabling_->addActiveDetectorsRawIds(SelectedDetIds);
00216     LogDebug( "SiStripDcsInfo") << " SiStripDcsInfo::readCabling : "  
00217                                     << " Total Detectors " << SelectedDetIds.size();
00218     
00219 
00220     // initialise total # of detectors first
00221     for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00222       it->second.TotalDetectors = 0;
00223     }
00224     
00225     for (std::vector<uint32_t>::const_iterator idetid=SelectedDetIds.begin(); idetid != SelectedDetIds.end(); ++idetid){    
00226       uint32_t detId = *idetid;
00227       if (detId == 0 || detId == 0xFFFFFFFF) continue;
00228       std::string subdet_tag;
00229       SiStripUtility::getSubDetectorTag(detId,subdet_tag);         
00230       
00231       std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
00232       if (iPos != SubDetMEsMap.end()){    
00233         iPos->second.TotalDetectors++;
00234       }
00235     }
00236   }
00237 }
00238 //
00239 // -- Get Faulty Detectors
00240 //
00241 void SiStripDcsInfo::readStatus(edm::EventSetup const& eSetup) {
00242 
00243   eSetup.get<SiStripDetVOffRcd>().get(siStripDetVOff_);
00244   std::vector <uint32_t> FaultyDetIds;
00245   siStripDetVOff_->getDetIds(FaultyDetIds);
00246   LogDebug( "SiStripDcsInfo") << " SiStripDcsInfo::readStatus : "
00247                                   << " Faulty Detectors " << FaultyDetIds.size();
00248   // Read and fille bad modules
00249   for (std::vector<uint32_t>::const_iterator ihvoff=FaultyDetIds.begin(); ihvoff!=FaultyDetIds.end();++ihvoff){
00250     uint32_t detId_hvoff = (*ihvoff);
00251     if (!detCabling_->IsConnected(detId_hvoff)) continue;    
00252     std::string subdet_tag;
00253     SiStripUtility::getSubDetectorTag(detId_hvoff,subdet_tag);         
00254     
00255     std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
00256     if (iPos != SubDetMEsMap.end()){  
00257       std::vector<uint32_t>::iterator ibad = std::find(iPos->second.FaultyDetectors.begin(), iPos->second.FaultyDetectors.end(), detId_hvoff);
00258       if (ibad ==  iPos->second.FaultyDetectors.end()) iPos->second.FaultyDetectors.push_back( detId_hvoff);
00259     }
00260   }
00261 }
00262 //
00263 // -- Fill Status
00264 //
00265 void SiStripDcsInfo::fillStatus(){
00266   if (!bookedStatus_) bookStatus();
00267   if (bookedStatus_) {
00268     float total_det = 0.0;
00269     float faulty_det = 0.0;
00270     float fraction;
00271     for (std::map<std::string,SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00272       int total_subdet  = it->second.TotalDetectors;
00273       int faulty_subdet = it->second.FaultyDetectors.size(); 
00274       if  (nFEDConnected_ == 0  || total_subdet == 0) fraction = -1;
00275       else fraction = 1.0  - faulty_subdet*1.0/total_subdet;   
00276       it->second.DcsFractionME->Reset();
00277       it->second.DcsFractionME->Fill(fraction);
00278       edm::LogInfo( "SiStripDcsInfo") << " SiStripDcsInfo::fillStatus : Sub Detector "
00279         << it->first << " Total Number " << total_subdet  
00280         << " Faulty ones " << faulty_subdet;
00281       total_det += total_subdet;
00282       faulty_det += faulty_subdet;
00283     }
00284     if (nFEDConnected_ == 0 || total_det == 0) fraction = -1.0;
00285     else fraction = 1 - faulty_det/total_det;
00286     DcsFraction_->Reset();
00287     DcsFraction_->Fill(fraction);
00288   } 
00289 }
00290 //
00291 // -- Fill with Dummy values
00292 //
00293 void SiStripDcsInfo::fillDummyStatus() {
00294   if (!bookedStatus_) bookStatus();
00295   if (bookedStatus_) {
00296     for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00297       it->second.DcsFractionME->Reset();
00298       it->second.DcsFractionME->Fill(-1.0);
00299     }
00300     DcsFraction_->Reset();
00301     DcsFraction_->Fill(-1.0);
00302   }
00303 }
00304 //
00305 // -- Add Bad Modules
00306 //
00307 void SiStripDcsInfo::addBadModules() {
00308     
00309   dqmStore_->cd();
00310   std::string mdir = "MechanicalView";
00311   if (!SiStripUtility::goToDir(dqmStore_, mdir)) {
00312     dqmStore_->setCurrentFolder("SiStrip/"+mdir);
00313   }
00314   std::string mechanical_dir = dqmStore_->pwd();
00315   std::string tag = "DCSError";
00316 
00317   for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00318     std::vector<uint32_t> badModules = it->second.FaultyDetectors; 
00319     for (std::vector<uint32_t>::iterator ibad = badModules.begin(); 
00320          ibad != badModules.end(); ibad++) {
00321 
00322       std::string bad_module_folder = mechanical_dir + "/" +
00323                                       it->second.folder_name + "/"     
00324                                       "BadModuleList";      
00325       dqmStore_->setCurrentFolder(bad_module_folder);
00326 
00327       std::ostringstream detid_str;
00328       detid_str << (*ibad);
00329       std::string full_path = bad_module_folder + "/" + detid_str.str();
00330       MonitorElement* me = dqmStore_->get(full_path);
00331       uint16_t flag = 0; 
00332       if (me) {
00333         flag = me->getIntValue();
00334         me->Reset();
00335       } else me = dqmStore_->bookInt(detid_str.str());
00336       SiStripUtility::setBadModuleFlag(tag, flag);
00337       me->Fill(flag);
00338     }
00339   }   
00340   dqmStore_->cd();
00341 }
00342 
00343 #include "FWCore/Framework/interface/MakerMacros.h"
00344 DEFINE_FWK_MODULE(SiStripDcsInfo);