CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/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 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 
00207   //Retrieve tracker topology from geometry
00208   edm::ESHandle<TrackerTopology> tTopoHandle;
00209   eSetup.get<IdealGeometryRecord>().get(tTopoHandle);
00210   const TrackerTopology* const tTopo = tTopoHandle.product();
00211 
00212   unsigned long long cacheID = eSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00213   if (m_cacheIDCabling_ != cacheID) {
00214     m_cacheIDCabling_ = cacheID;
00215     LogDebug("SiStripDcsInfo") <<"SiStripDcsInfo::readCabling : "
00216                                    << " Change in Cache";
00217     eSetup.get<SiStripDetCablingRcd>().get(detCabling_);
00218 
00219     std::vector<uint32_t> SelectedDetIds;
00220     detCabling_->addActiveDetectorsRawIds(SelectedDetIds);
00221     LogDebug( "SiStripDcsInfo") << " SiStripDcsInfo::readCabling : "  
00222                                     << " Total Detectors " << SelectedDetIds.size();
00223     
00224 
00225     // initialise total # of detectors first
00226     for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00227       it->second.TotalDetectors = 0;
00228     }
00229     
00230     for (std::vector<uint32_t>::const_iterator idetid=SelectedDetIds.begin(); idetid != SelectedDetIds.end(); ++idetid){    
00231       uint32_t detId = *idetid;
00232       if (detId == 0 || detId == 0xFFFFFFFF) continue;
00233       std::string subdet_tag;
00234       SiStripUtility::getSubDetectorTag(detId,subdet_tag,tTopo);         
00235       
00236       std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
00237       if (iPos != SubDetMEsMap.end()){    
00238         iPos->second.TotalDetectors++;
00239       }
00240     }
00241   }
00242 }
00243 //
00244 // -- Get Faulty Detectors
00245 //
00246 void SiStripDcsInfo::readStatus(edm::EventSetup const& eSetup) {
00247 
00248   //Retrieve tracker topology from geometry
00249   edm::ESHandle<TrackerTopology> tTopoHandle;
00250   eSetup.get<IdealGeometryRecord>().get(tTopoHandle);
00251   const TrackerTopology* const tTopo = tTopoHandle.product();
00252 
00253   eSetup.get<SiStripDetVOffRcd>().get(siStripDetVOff_);
00254   std::vector <uint32_t> FaultyDetIds;
00255   siStripDetVOff_->getDetIds(FaultyDetIds);
00256   LogDebug( "SiStripDcsInfo") << " SiStripDcsInfo::readStatus : "
00257                                   << " Faulty Detectors " << FaultyDetIds.size();
00258   // Read and fille bad modules
00259   for (std::vector<uint32_t>::const_iterator ihvoff=FaultyDetIds.begin(); ihvoff!=FaultyDetIds.end();++ihvoff){
00260     uint32_t detId_hvoff = (*ihvoff);
00261     if (!detCabling_->IsConnected(detId_hvoff)) continue;    
00262     std::string subdet_tag;
00263     SiStripUtility::getSubDetectorTag(detId_hvoff,subdet_tag,tTopo);         
00264     
00265     std::map<std::string, SubDetMEs>::iterator iPos = SubDetMEsMap.find(subdet_tag);
00266     if (iPos != SubDetMEsMap.end()){  
00267       std::vector<uint32_t>::iterator ibad = std::find(iPos->second.FaultyDetectors.begin(), iPos->second.FaultyDetectors.end(), detId_hvoff);
00268       if (ibad ==  iPos->second.FaultyDetectors.end()) iPos->second.FaultyDetectors.push_back( detId_hvoff);
00269     }
00270   }
00271 }
00272 //
00273 // -- Fill Status
00274 //
00275 void SiStripDcsInfo::fillStatus(){
00276   if (!bookedStatus_) bookStatus();
00277   if (bookedStatus_) {
00278     float total_det = 0.0;
00279     float faulty_det = 0.0;
00280     float fraction;
00281     for (std::map<std::string,SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00282       int total_subdet  = it->second.TotalDetectors;
00283       int faulty_subdet = it->second.FaultyDetectors.size(); 
00284       if  (nFEDConnected_ == 0  || total_subdet == 0) fraction = -1;
00285       else fraction = 1.0  - faulty_subdet*1.0/total_subdet;   
00286       it->second.DcsFractionME->Reset();
00287       it->second.DcsFractionME->Fill(fraction);
00288       edm::LogInfo( "SiStripDcsInfo") << " SiStripDcsInfo::fillStatus : Sub Detector "
00289         << it->first << " Total Number " << total_subdet  
00290         << " Faulty ones " << faulty_subdet;
00291       total_det += total_subdet;
00292       faulty_det += faulty_subdet;
00293     }
00294     if (nFEDConnected_ == 0 || total_det == 0) fraction = -1.0;
00295     else fraction = 1 - faulty_det/total_det;
00296     DcsFraction_->Reset();
00297     DcsFraction_->Fill(fraction);
00298   } 
00299 }
00300 //
00301 // -- Fill with Dummy values
00302 //
00303 void SiStripDcsInfo::fillDummyStatus() {
00304   if (!bookedStatus_) bookStatus();
00305   if (bookedStatus_) {
00306     for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00307       it->second.DcsFractionME->Reset();
00308       it->second.DcsFractionME->Fill(-1.0);
00309     }
00310     DcsFraction_->Reset();
00311     DcsFraction_->Fill(-1.0);
00312   }
00313 }
00314 //
00315 // -- Add Bad Modules
00316 //
00317 void SiStripDcsInfo::addBadModules() {
00318     
00319   dqmStore_->cd();
00320   std::string mdir = "MechanicalView";
00321   if (!SiStripUtility::goToDir(dqmStore_, mdir)) {
00322     dqmStore_->setCurrentFolder("SiStrip/"+mdir);
00323   }
00324   std::string mechanical_dir = dqmStore_->pwd();
00325   std::string tag = "DCSError";
00326 
00327   for (std::map<std::string, SubDetMEs>::iterator it = SubDetMEsMap.begin(); it != SubDetMEsMap.end(); it++) {
00328     std::vector<uint32_t> badModules = it->second.FaultyDetectors; 
00329     for (std::vector<uint32_t>::iterator ibad = badModules.begin(); 
00330          ibad != badModules.end(); ibad++) {
00331 
00332       std::string bad_module_folder = mechanical_dir + "/" +
00333                                       it->second.folder_name + "/"     
00334                                       "BadModuleList";      
00335       dqmStore_->setCurrentFolder(bad_module_folder);
00336 
00337       std::ostringstream detid_str;
00338       detid_str << (*ibad);
00339       std::string full_path = bad_module_folder + "/" + detid_str.str();
00340       MonitorElement* me = dqmStore_->get(full_path);
00341       uint16_t flag = 0; 
00342       if (me) {
00343         flag = me->getIntValue();
00344         me->Reset();
00345       } else me = dqmStore_->bookInt(detid_str.str());
00346       SiStripUtility::setBadModuleFlag(tag, flag);
00347       me->Fill(flag);
00348     }
00349   }   
00350   dqmStore_->cd();
00351 }
00352 
00353 #include "FWCore/Framework/interface/MakerMacros.h"
00354 DEFINE_FWK_MODULE(SiStripDcsInfo);