Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:51

0001 // Original Author:  Anne-Marie Magnan
0002 //         Created:  2010/02/25
0003 //
0004 
0005 #include <sstream>
0006 #include <fstream>
0007 #include <iostream>
0008 #include <memory>
0009 #include <list>
0010 #include <algorithm>
0011 #include <cassert>
0012 
0013 #include "FWCore/Utilities/interface/EDGetToken.h"
0014 #include "FWCore/Framework/interface/Frameworkfwd.h"
0015 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/EventSetup.h"
0018 #include "FWCore/Framework/interface/ESHandle.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 #include "FWCore/Utilities/interface/InputTag.h"
0021 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0022 #include "FWCore/ServiceRegistry/interface/Service.h"
0023 #include "FWCore/Utilities/interface/Exception.h"
0024 
0025 #include "DataFormats/FEDRawData/interface/FEDNumbering.h"
0026 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
0027 
0028 #include "DQM/SiStripMonitorHardware/interface/SiStripFEDSpyBuffer.h"
0029 
0030 //
0031 // Class declaration
0032 //
0033 namespace sistrip {
0034 
0035   class SpyExtractRunModule : public edm::one::EDAnalyzer<> {
0036   public:
0037     explicit SpyExtractRunModule(const edm::ParameterSet&);
0038     ~SpyExtractRunModule() override;
0039 
0040   private:
0041     void beginJob() override;
0042     void analyze(const edm::Event&, const edm::EventSetup&) override;
0043     void endJob() override;
0044 
0045     //check when the current run changes
0046     const bool updateRun(const uint32_t aRun);
0047 
0048     //name of the output file containing the run number
0049     //get it from the input file
0050     std::string fileName_;
0051 
0052     //tag of spydata run number collection
0053     edm::InputTag runTag_;
0054     edm::EDGetTokenT<uint32_t> runToken_;
0055 
0056     //cache of the current and previous run number
0057     uint32_t currentRun_;
0058     uint32_t previousRun_;
0059 
0060     //error counter for number of times the run number changes
0061     uint32_t errCounter_;
0062   };
0063 }  // namespace sistrip
0064 
0065 using edm::LogError;
0066 using edm::LogInfo;
0067 using edm::LogWarning;
0068 //
0069 // Constructors and destructor
0070 //
0071 namespace sistrip {
0072 
0073   SpyExtractRunModule::SpyExtractRunModule(const edm::ParameterSet& iConfig)
0074       : fileName_(iConfig.getParameter<std::string>("OutputTextFile")),
0075         runTag_(iConfig.getParameter<edm::InputTag>("RunNumberTag")),
0076         currentRun_(0),
0077         previousRun_(0),
0078         errCounter_(0) {
0079     runToken_ = consumes<uint32_t>(runTag_);
0080   }
0081 
0082   SpyExtractRunModule::~SpyExtractRunModule() {}
0083 
0084   void SpyExtractRunModule::beginJob() {
0085     currentRun_ = 0;
0086     previousRun_ = 0;
0087     errCounter_ = 0;
0088   }
0089 
0090   void SpyExtractRunModule::analyze(const edm::Event& aEvt, const edm::EventSetup& aSetup) {
0091     static bool lFirstEvent = true;
0092     edm::Handle<uint32_t> lRun;
0093     aEvt.getByToken(runToken_, lRun);
0094 
0095     const bool isUpdated = updateRun(*lRun);
0096 
0097     if (isUpdated && !lFirstEvent) {
0098       edm::LogError("SpyExtractRunModule")
0099           << " -- Run number changed for event : " << aEvt.id().event() << " (id().run() = " << aEvt.id().run()
0100           << ") from " << previousRun_ << " to " << currentRun_ << std::endl;
0101     }
0102 
0103     lFirstEvent = false;
0104   }
0105 
0106   void SpyExtractRunModule::endJob() {
0107     //save global run number in text file in local directory
0108     //output loginfo with number of errors
0109     //or throw exception ?
0110 
0111     if (errCounter_ == 1) {
0112       edm::LogInfo("SiStripSpyExtractRun")
0113           << " -- Writting run number " << currentRun_ << " into file " << fileName_ << std::endl;
0114       std::ofstream lOutFile;
0115       lOutFile.open(fileName_.c_str(), std::ios::out);
0116       if (!lOutFile.is_open()) {
0117         edm::LogError("SiStripSpyExtractRun")
0118             << " -- Cannot open file : " << fileName_ << " for writting run number " << currentRun_ << std::endl;
0119       } else {
0120         lOutFile << currentRun_ << std::endl;
0121         lOutFile.close();
0122       }
0123 
0124     } else {
0125       edm::LogError("SiStripSpyExtractRun")
0126           << " -- Number of times the run number changed in this job = " << errCounter_
0127           << ", currentRun = " << currentRun_ << ", previousRun = " << previousRun_ << std::endl;
0128     }
0129   }
0130 
0131   const bool SpyExtractRunModule::updateRun(const uint32_t aRun) {
0132     if (aRun != currentRun_) {
0133       previousRun_ = currentRun_;
0134       currentRun_ = aRun;
0135       errCounter_++;
0136       return true;
0137     }
0138     return false;
0139   }
0140 
0141 }  // namespace sistrip
0142 
0143 #include "FWCore/Framework/interface/MakerMacros.h"
0144 typedef sistrip::SpyExtractRunModule SiStripSpyExtractRunModule;
0145 DEFINE_FWK_MODULE(SiStripSpyExtractRunModule);