CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/DQM/SiStripMonitorHardware/plugins/SiStripFEDMonitor.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    DQM/SiStripMonitorHardware
00004 // Class:      SiStripFEDMonitorPlugin
00005 // 
00010 //
00011 // Original Author:  Nicholas Cripps
00012 //         Created:  2008/09/16
00013 // $Id: SiStripFEDMonitor.cc,v 1.39 2010/04/21 10:40:19 amagnan Exp $
00014 //
00015 //Modified        :  Anne-Marie Magnan
00016 //   ---- 2009/04/21 : histogram management put in separate class
00017 //                     struct helper to simplify arguments of functions
00018 //   ---- 2009/04/22 : add TkHistoMap with % of bad channels per module
00019 //   ---- 2009/04/27 : create FEDErrors class 
00020 
00021 #include <sstream>
00022 #include <memory>
00023 #include <list>
00024 #include <algorithm>
00025 #include <cassert>
00026 
00027 #include "FWCore/Framework/interface/Frameworkfwd.h"
00028 #include "FWCore/Framework/interface/EDAnalyzer.h"
00029 #include "FWCore/Framework/interface/Event.h"
00030 #include "FWCore/Framework/interface/EventSetup.h"
00031 #include "FWCore/Framework/interface/ESHandle.h"
00032 #include "FWCore/Framework/interface/LuminosityBlock.h"
00033 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00034 #include "FWCore/Utilities/interface/InputTag.h"
00035 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00036 #include "FWCore/ServiceRegistry/interface/Service.h"
00037 #include "FWCore/Utilities/interface/Exception.h"
00038 
00039 #include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
00040 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
00041 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
00042 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
00043 
00044 #include "CondFormats/DataRecord/interface/SiStripFedCablingRcd.h"
00045 #include "CondFormats/SiStripObjects/interface/SiStripFedCabling.h"
00046 
00047 #include "DQMServices/Core/interface/DQMStore.h"
00048 
00049 #include "DQM/SiStripMonitorHardware/interface/FEDHistograms.hh"
00050 #include "DQM/SiStripMonitorHardware/interface/FEDErrors.hh"
00051 
00052 
00053 //
00054 // Class declaration
00055 //
00056 
00057 class SiStripFEDMonitorPlugin : public edm::EDAnalyzer
00058 {
00059  public:
00060   explicit SiStripFEDMonitorPlugin(const edm::ParameterSet&);
00061   ~SiStripFEDMonitorPlugin();
00062  private:
00063   virtual void beginJob();
00064   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00065   virtual void endJob();
00066   virtual void beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00067                                     const edm::EventSetup& context);
00068   virtual void endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00069                                   const edm::EventSetup& context);
00070 
00071   //update the cabling if necessary
00072   void updateCabling(const edm::EventSetup& eventSetup);
00073 
00074 
00075   //tag of FEDRawData collection
00076   edm::InputTag rawDataTag_;
00077   //histogram helper class
00078   FEDHistograms fedHists_;
00079   //folder name for histograms in DQMStore
00080   std::string folderName_;
00081   //book detailed histograms even if they will be empty (for merging)
00082   bool fillAllDetailedHistograms_;
00083   //do histos vs time with time=event number. Default time = orbit number (s)
00084   bool fillWithEvtNum_;
00085   //print debug messages when problems are found: 1=error debug, 2=light debug, 3=full debug
00086   unsigned int printDebug_;
00087   //bool printDebug_;
00088   //write the DQMStore to a root file at the end of the job
00089   bool writeDQMStore_;
00090   std::string dqmStoreFileName_;
00091   //the DQMStore
00092   DQMStore* dqm_;
00093   //FED cabling
00094   uint32_t cablingCacheId_;
00095   const SiStripFedCabling* cabling_;
00096 
00097   //add parameter to save computing time if TkHistoMap are not filled
00098   bool doTkHistoMap_;
00099   bool doMedHists_;
00100 
00101   unsigned int nEvt_;
00102 
00103   //FED errors
00104   //need class member for lumi histograms
00105   FEDErrors fedErrors_;
00106 
00107 };
00108 
00109 
00110 //
00111 // Constructors and destructor
00112 //
00113 
00114 SiStripFEDMonitorPlugin::SiStripFEDMonitorPlugin(const edm::ParameterSet& iConfig)
00115   : rawDataTag_(iConfig.getUntrackedParameter<edm::InputTag>("RawDataTag",edm::InputTag("source",""))),
00116     folderName_(iConfig.getUntrackedParameter<std::string>("HistogramFolderName","SiStrip/ReadoutView/FedMonitoringSummary")),
00117     fillAllDetailedHistograms_(iConfig.getUntrackedParameter<bool>("FillAllDetailedHistograms",false)),
00118     fillWithEvtNum_(iConfig.getUntrackedParameter<bool>("FillWithEventNumber",false)),
00119     printDebug_(iConfig.getUntrackedParameter<unsigned int>("PrintDebugMessages",1)),
00120     //printDebug_(iConfig.getUntrackedParameter<bool>("PrintDebugMessages",false)),
00121     writeDQMStore_(iConfig.getUntrackedParameter<bool>("WriteDQMStore",false)),
00122     dqmStoreFileName_(iConfig.getUntrackedParameter<std::string>("DQMStoreFileName","DQMStore.root")),
00123     dqm_(0),
00124     cablingCacheId_(0)
00125 {
00126   //print config to debug log
00127   std::ostringstream debugStream;
00128   if (printDebug_>1) {
00129     debugStream << "[SiStripFEDMonitorPlugin]Configuration for SiStripFEDMonitorPlugin: " << std::endl
00130                 << "[SiStripFEDMonitorPlugin]\tRawDataTag: " << rawDataTag_ << std::endl
00131                 << "[SiStripFEDMonitorPlugin]\tHistogramFolderName: " << folderName_ << std::endl
00132                 << "[SiStripFEDMonitorPlugin]\tFillAllDetailedHistograms? " << (fillAllDetailedHistograms_ ? "yes" : "no") << std::endl
00133                 << "[SiStripFEDMonitorPlugin]\tFillWithEventNumber?" << (fillWithEvtNum_ ? "yes" : "no") << std::endl
00134                 << "[SiStripFEDMonitorPlugin]\tPrintDebugMessages? " << (printDebug_ ? "yes" : "no") << std::endl
00135                 << "[SiStripFEDMonitorPlugin]\tWriteDQMStore? " << (writeDQMStore_ ? "yes" : "no") << std::endl;
00136     if (writeDQMStore_) debugStream << "[SiStripFEDMonitorPlugin]\tDQMStoreFileName: " << dqmStoreFileName_ << std::endl;
00137   }
00138   
00139   //don;t generate debug mesages if debug is disabled
00140   std::ostringstream* pDebugStream = (printDebug_>1 ? &debugStream : NULL);
00141   
00142   fedHists_.initialise(iConfig,pDebugStream);
00143 
00144   doTkHistoMap_ = fedHists_.tkHistoMapEnabled();
00145 
00146   doMedHists_ = fedHists_.cmHistosEnabled();
00147 
00148   if (printDebug_) {
00149     LogTrace("SiStripMonitorHardware") << debugStream.str();
00150 
00151     //debugStream.str("");
00152 
00153     //debugStream << " -- Quelle est la difference entre un canard ? " << std::endl 
00154     //  << " -- Reponse: c'est qu'il a les deux pattes de la meme longueur, surtout la gauche." << std::endl;
00155 
00156     //edm::LogError("SiStripMonitorHardware") << debugStream.str();
00157   }
00158 
00159   nEvt_ = 0;
00160 
00161 }
00162 
00163 SiStripFEDMonitorPlugin::~SiStripFEDMonitorPlugin()
00164 {
00165 }
00166 
00167 
00168 //
00169 // Member functions
00170 //
00171 
00172 // ------------ method called to for each event  ------------
00173 void
00174 SiStripFEDMonitorPlugin::analyze(const edm::Event& iEvent, 
00175                                  const edm::EventSetup& iSetup)
00176 {
00177   //update cabling
00178   updateCabling(iSetup);
00179   
00180   //get raw data
00181   edm::Handle<FEDRawDataCollection> rawDataCollectionHandle;
00182   iEvent.getByLabel(rawDataTag_,rawDataCollectionHandle);
00183   const FEDRawDataCollection& rawDataCollection = *rawDataCollectionHandle;
00184   
00185   fedErrors_.initialiseEvent();
00186 
00187   //initialise map of fedId/bad channel number
00188   std::map<unsigned int,std::pair<unsigned short,unsigned short> > badChannelFraction;
00189 
00190   unsigned int lNFEDMonitoring = 0;
00191   unsigned int lNFEDUnpacker = 0;
00192   unsigned int lNChannelMonitoring = 0;
00193   unsigned int lNChannelUnpacker = 0;
00194 
00195   unsigned int lNTotBadFeds = 0;
00196   unsigned int lNTotBadChannels = 0;
00197   unsigned int lNTotBadActiveChannels = 0;
00198 
00199   //loop over siStrip FED IDs
00200   for (unsigned int fedId = FEDNumbering::MINSiStripFEDID; 
00201        fedId <= FEDNumbering::MAXSiStripFEDID; 
00202        fedId++) {//loop over FED IDs
00203     const FEDRawData& fedData = rawDataCollection.FEDData(fedId);
00204 
00205     //create an object to fill all errors
00206     fedErrors_.initialiseFED(fedId,cabling_);
00207     bool lFullDebug = false;
00208  
00209     //Do detailed check
00210     //first check if data exists
00211     bool lDataExist = fedErrors_.checkDataPresent(fedData);
00212     if (!lDataExist) {
00213       fedHists_.fillFEDHistograms(fedErrors_,lFullDebug);
00214       continue;
00215     }
00216 
00217 
00218  
00219     //check for problems and fill detailed histograms
00220     fedErrors_.fillFEDErrors(fedData,
00221                              lFullDebug,
00222                              printDebug_,
00223                              lNChannelMonitoring,
00224                              lNChannelUnpacker,
00225                              doMedHists_,
00226                              fedHists_.cmHistPointer(false),
00227                              fedHists_.cmHistPointer(true)
00228                              );
00229 
00230     //check filled in previous method.
00231     bool lFailUnpackerFEDcheck = fedErrors_.failUnpackerFEDCheck();
00232 
00233     fedErrors_.incrementFEDCounters();
00234     fedHists_.fillFEDHistograms(fedErrors_,lFullDebug);
00235 
00236 
00237     bool lFailMonitoringFEDcheck = fedErrors_.failMonitoringFEDCheck();
00238     if (lFailMonitoringFEDcheck) lNTotBadFeds++;
00239 
00240 
00241     //sanity check: if something changed in the unpacking code 
00242     //but wasn't propagated here
00243     //print only the summary, and more info if printDebug>1
00244     if (lFailMonitoringFEDcheck != lFailUnpackerFEDcheck) {
00245       if (printDebug_>1) {
00246         std::ostringstream debugStream;
00247         debugStream << " --- WARNING: FED " << fedId << std::endl 
00248                     << " ------ Monitoring FED check " ;
00249         if (lFailMonitoringFEDcheck) debugStream << "failed." << std::endl;
00250         else debugStream << "passed." << std::endl ;
00251         debugStream << " ------ Unpacker FED check " ;
00252         if (lFailUnpackerFEDcheck) debugStream << "failed." << std::endl;
00253         else debugStream << "passed." << std::endl ;
00254         edm::LogError("SiStripMonitorHardware") << debugStream.str();
00255       }
00256 
00257       if (lFailMonitoringFEDcheck) lNFEDMonitoring++;
00258       else if (lFailUnpackerFEDcheck) lNFEDUnpacker++;
00259     }
00260 
00261 
00262     //Fill TkHistoMap:
00263     //add an entry for all channels (good = 0), 
00264     //so that tkHistoMap knows which channels should be there.
00265     if (doTkHistoMap_ && !fedHists_.tkHistoMapPointer()) {
00266       edm::LogWarning("SiStripMonitorHardware") << " -- Fedid " << fedId
00267                                                 << ", TkHistoMap enabled but pointer is null." << std::endl;
00268     }
00269 
00270     fedErrors_.fillBadChannelList(doTkHistoMap_,
00271                                   fedHists_.tkHistoMapPointer(),
00272                                   lNTotBadChannels,
00273                                   lNTotBadActiveChannels);
00274   }//loop over FED IDs
00275 
00276   if ((lNTotBadFeds> 0 || lNTotBadChannels>0) && printDebug_>1) {
00277     std::ostringstream debugStream;
00278     debugStream << "[SiStripFEDMonitorPlugin] --- Total number of bad feds = " 
00279                 << lNTotBadFeds << std::endl
00280                 << "[SiStripFEDMonitorPlugin] --- Total number of bad channels = " 
00281                 << lNTotBadChannels << std::endl
00282                 << "[SiStripFEDMonitorPlugin] --- Total number of bad active channels = " 
00283                 << lNTotBadActiveChannels << std::endl;
00284     edm::LogInfo("SiStripMonitorHardware") << debugStream.str();
00285   }
00286 
00287   if ((lNFEDMonitoring > 0 || lNFEDUnpacker > 0 || lNChannelMonitoring > 0 || lNChannelUnpacker > 0) && printDebug_) {
00288     std::ostringstream debugStream;
00289     debugStream
00290       << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl 
00291       << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl 
00292       << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at FED level : " << std::endl 
00293       << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNFEDMonitoring << std::endl 
00294       << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNFEDUnpacker << std::endl
00295       << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl 
00296       << "[SiStripFEDMonitorPlugin]-- Summary of differences between unpacker and monitoring at Channel level : " << std::endl 
00297       << "[SiStripFEDMonitorPlugin] ---- Number of times monitoring fails but not unpacking = " << lNChannelMonitoring << std::endl 
00298       << "[SiStripFEDMonitorPlugin] ---- Number of times unpacking fails but not monitoring = " << lNChannelUnpacker << std::endl
00299       << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl 
00300       << "[SiStripFEDMonitorPlugin]-------------------------------------------------------------------------" << std::endl ;
00301     edm::LogError("SiStripMonitorHardware") << debugStream.str();
00302 
00303   }
00304 
00305   FEDErrors::getFEDErrorsCounters().nTotalBadChannels = lNTotBadChannels;
00306   FEDErrors::getFEDErrorsCounters().nTotalBadActiveChannels = lNTotBadActiveChannels;
00307 
00308   //fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(), nEvt_);
00309   //time in seconds since beginning of the run or event number
00310   if (fillWithEvtNum_) fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),iEvent.id().event());
00311   else fedHists_.fillCountersHistograms(FEDErrors::getFEDErrorsCounters(),FEDErrors::getChannelErrorsCounters(),iEvent.orbitNumber()/11223.);
00312 
00313 
00314   nEvt_++;
00315 
00316 }//analyze method
00317 
00318 // ------------ method called once each job just before starting event loop  ------------
00319 void 
00320 SiStripFEDMonitorPlugin::beginJob()
00321 {
00322   //get DQM store
00323   dqm_ = &(*edm::Service<DQMStore>());
00324   dqm_->setCurrentFolder(folderName_);
00325   
00326   //this propagates dqm_ to the histoclass, must be called !
00327   fedHists_.bookTopLevelHistograms(dqm_);
00328   
00329   if (fillAllDetailedHistograms_) fedHists_.bookAllFEDHistograms();
00330 
00331   nEvt_ = 0;
00332 
00333   //const unsigned int siStripFedIdMin = FEDNumbering::MINSiStripFEDID;
00334   //const unsigned int siStripFedIdMax = FEDNumbering::MAXSiStripFEDID;
00335 
00336   //mark all channels as inactive until they have been 'locked' at least once
00337   //   activeChannels_.resize(siStripFedIdMax+1);
00338   //   for (unsigned int fedId = siStripFedIdMin; 
00339   //        fedId <= siStripFedIdMax; 
00340   //        fedId++) {
00341   //     activeChannels_[fedId].resize(sistrip::FEDCH_PER_FED,false);
00342   //   }
00343   
00344 
00345 
00346 
00347 }
00348 
00349 // ------------ method called once each job just after ending the event loop  ------------
00350 void 
00351 SiStripFEDMonitorPlugin::endJob()
00352 {
00353   if (writeDQMStore_) dqm_->save(dqmStoreFileName_);
00354 }
00355 
00356 
00357 
00358 void 
00359 SiStripFEDMonitorPlugin::beginLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00360                                               const edm::EventSetup& context)
00361 {
00362 
00363   fedErrors_.initialiseLumiBlock();
00364 
00365 }
00366 
00367 
00368 void 
00369 SiStripFEDMonitorPlugin::endLuminosityBlock(const edm::LuminosityBlock& lumiSeg, 
00370                                             const edm::EventSetup& context)
00371 {
00372   fedHists_.fillLumiHistograms(fedErrors_.getLumiErrors());
00373 }
00374 
00375 
00376 
00377 
00378 void SiStripFEDMonitorPlugin::updateCabling(const edm::EventSetup& eventSetup)
00379 {
00380   uint32_t currentCacheId = eventSetup.get<SiStripFedCablingRcd>().cacheIdentifier();
00381   if (cablingCacheId_ != currentCacheId) {
00382     edm::ESHandle<SiStripFedCabling> cablingHandle;
00383     eventSetup.get<SiStripFedCablingRcd>().get(cablingHandle);
00384     cabling_ = cablingHandle.product();
00385     cablingCacheId_ = currentCacheId;
00386   }
00387 }
00388 
00389 
00390 //
00391 // Define as a plug-in
00392 //
00393 
00394 #include "FWCore/Framework/interface/MakerMacros.h"
00395 DEFINE_FWK_MODULE(SiStripFEDMonitorPlugin);