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/FEDRawData/interface/FEDRawDataCollection.h"
0027 #include "DataFormats/FEDRawData/interface/FEDRawData.h"
0028 #include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"
0029 
0030 #include "DQM/SiStripMonitorHardware/interface/SiStripFEDSpyBuffer.h"
0031 
0032 //
0033 // Class declaration
0034 //
0035 namespace sistrip {
0036 
0037   class SpyIdentifyRunsModule : public edm::one::EDAnalyzer<> {
0038   public:
0039     explicit SpyIdentifyRunsModule(const edm::ParameterSet&);
0040     ~SpyIdentifyRunsModule() override;
0041 
0042   private:
0043     void beginJob() override;
0044     void analyze(const edm::Event&, const edm::EventSetup&) override;
0045     void endJob() override;
0046 
0047     void writeRunInFile(const unsigned int aRunNumber);
0048 
0049     //name of the output file containing the run numbers
0050     //of spy runs
0051     std::string fileName_;
0052     std::ofstream outFile_;
0053 
0054     //tag of spydata source collection
0055     edm::InputTag srcTag_;
0056     edm::EDGetTokenT<FEDRawDataCollection> srcToken_;
0057     uint32_t prevRun_;
0058   };
0059 }  // namespace sistrip
0060 
0061 using edm::LogError;
0062 using edm::LogInfo;
0063 using edm::LogWarning;
0064 //
0065 // Constructors and destructor
0066 //
0067 namespace sistrip {
0068 
0069   SpyIdentifyRunsModule::SpyIdentifyRunsModule(const edm::ParameterSet& iConfig)
0070       : fileName_(iConfig.getParameter<std::string>("OutputTextFile")),
0071         srcTag_(iConfig.getParameter<edm::InputTag>("InputProductLabel")),
0072         prevRun_(0) {
0073     srcToken_ = consumes<FEDRawDataCollection>(srcTag_);
0074   }
0075 
0076   SpyIdentifyRunsModule::~SpyIdentifyRunsModule() {}
0077 
0078   void SpyIdentifyRunsModule::beginJob() {
0079     outFile_.open(fileName_.c_str(), std::ios::out);
0080     if (!outFile_.is_open()) {
0081       edm::LogError("SiStripSpyIdentifyRuns")
0082           << " -- Cannot open file : " << fileName_ << " for writting." << std::endl;
0083       edm::LogInfo("SiStripSpyIdentifyRuns") << " *** SPY RUNS *** " << std::endl;
0084 
0085     } else {
0086       outFile_ << " *** SPY RUNS *** " << std::endl;
0087     }
0088   }
0089 
0090   void SpyIdentifyRunsModule::analyze(const edm::Event& aEvt, const edm::EventSetup& aSetup) {
0091     //static bool lFirstEvent = true;
0092     //if (!lFirstEvent) return;
0093     uint32_t lRunNum = aEvt.id().run();
0094     if (lRunNum == prevRun_)
0095       return;
0096 
0097     edm::Handle<FEDRawDataCollection> lHandle;
0098     aEvt.getByToken(srcToken_, lHandle);
0099     const FEDRawDataCollection& buffers = *lHandle;
0100 
0101     for (unsigned int iFed(FEDNumbering::MINSiStripFEDID); iFed <= FEDNumbering::MAXSiStripFEDID; iFed++) {
0102       //retrieve FED raw data for given FED
0103       const FEDRawData& input = buffers.FEDData(static_cast<int>(iFed));
0104       //check on FEDRawData pointer and size
0105       if (!input.data() || !input.size())
0106         continue;
0107       //construct FEDBuffer
0108       const auto st_buffer = preconstructCheckFEDSpyBuffer(input);
0109       if (sistrip::FEDBufferStatusCode::SUCCESS != st_buffer) {
0110         edm::LogWarning("SiStripSpyIdentifyRuns")
0111             << "Exception caught when creating FEDSpyBuffer object for FED " << iFed << ": "
0112             << "An exception of category 'FEDBuffer' occurred.\n"
0113             << st_buffer;
0114         if (sistrip::FEDBufferStatusCode::EXPECT_SPY == st_buffer)
0115           break;
0116       }
0117       const sistrip::FEDSpyBuffer buffer{input};
0118       edm::LogWarning("SiStripSpyIdentifyRuns") << " -- this is a spy file, run " << lRunNum << std::endl;
0119       writeRunInFile(lRunNum);
0120       break;
0121     }
0122     //lFirstEvent = false;
0123     prevRun_ = lRunNum;
0124   }
0125 
0126   void SpyIdentifyRunsModule::writeRunInFile(const unsigned int aRunNumber) {
0127     if (!outFile_.is_open()) {
0128       edm::LogInfo("SiStripSpyIdentifyRuns") << aRunNumber << std::endl;
0129     } else {
0130       outFile_ << aRunNumber << std::endl;
0131     }
0132   }
0133 
0134   void SpyIdentifyRunsModule::endJob() {
0135     //save global run number in text file in local directory
0136     //output loginfo with number of errors
0137     //or throw exception ?
0138     if (outFile_.is_open())
0139       outFile_.close();
0140   }
0141 
0142 }  // namespace sistrip
0143 
0144 #include "FWCore/Framework/interface/MakerMacros.h"
0145 typedef sistrip::SpyIdentifyRunsModule SiStripSpyIdentifyRuns;
0146 DEFINE_FWK_MODULE(SiStripSpyIdentifyRuns);