SpyIdentifyRunsModule

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
// Original Author:  Anne-Marie Magnan
//         Created:  2010/02/25
//

#include <sstream>
#include <fstream>
#include <iostream>
#include <memory>
#include <list>
#include <algorithm>
#include <cassert>

#include "FWCore/Utilities/interface/EDGetToken.h"
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/Framework/interface/ESHandle.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ServiceRegistry/interface/Service.h"
#include "FWCore/Utilities/interface/Exception.h"

#include "DataFormats/FEDRawData/interface/FEDNumbering.h"
#include "DataFormats/FEDRawData/interface/FEDRawDataCollection.h"
#include "DataFormats/FEDRawData/interface/FEDRawData.h"
#include "DataFormats/SiStripCommon/interface/ConstantsForHardwareSystems.h"

#include "DQM/SiStripMonitorHardware/interface/SiStripFEDSpyBuffer.h"

//
// Class declaration
//
namespace sistrip {

  class SpyIdentifyRunsModule : public edm::one::EDAnalyzer<> {
  public:
    explicit SpyIdentifyRunsModule(const edm::ParameterSet&);
    ~SpyIdentifyRunsModule() override;

  private:
    void beginJob() override;
    void analyze(const edm::Event&, const edm::EventSetup&) override;
    void endJob() override;

    void writeRunInFile(const unsigned int aRunNumber);

    //name of the output file containing the run numbers
    //of spy runs
    std::string fileName_;
    std::ofstream outFile_;

    //tag of spydata source collection
    edm::InputTag srcTag_;
    edm::EDGetTokenT<FEDRawDataCollection> srcToken_;
    uint32_t prevRun_;
  };
}  // namespace sistrip

using edm::LogError;
using edm::LogInfo;
using edm::LogWarning;
//
// Constructors and destructor
//
namespace sistrip {

  SpyIdentifyRunsModule::SpyIdentifyRunsModule(const edm::ParameterSet& iConfig)
      : fileName_(iConfig.getParameter<std::string>("OutputTextFile")),
        srcTag_(iConfig.getParameter<edm::InputTag>("InputProductLabel")),
        prevRun_(0) {
    srcToken_ = consumes<FEDRawDataCollection>(srcTag_);
  }

  SpyIdentifyRunsModule::~SpyIdentifyRunsModule() {}

  void SpyIdentifyRunsModule::beginJob() {
    outFile_.open(fileName_.c_str(), std::ios::out);
    if (!outFile_.is_open()) {
      edm::LogError("SiStripSpyIdentifyRuns")
          << " -- Cannot open file : " << fileName_ << " for writting." << std::endl;
      edm::LogInfo("SiStripSpyIdentifyRuns") << " *** SPY RUNS *** " << std::endl;

    } else {
      outFile_ << " *** SPY RUNS *** " << std::endl;
    }
  }

  void SpyIdentifyRunsModule::analyze(const edm::Event& aEvt, const edm::EventSetup& aSetup) {
    //static bool lFirstEvent = true;
    //if (!lFirstEvent) return;
    uint32_t lRunNum = aEvt.id().run();
    if (lRunNum == prevRun_)
      return;

    edm::Handle<FEDRawDataCollection> lHandle;
    aEvt.getByToken(srcToken_, lHandle);
    const FEDRawDataCollection& buffers = *lHandle;

    for (unsigned int iFed(FEDNumbering::MINSiStripFEDID); iFed <= FEDNumbering::MAXSiStripFEDID; iFed++) {
      //retrieve FED raw data for given FED
      const FEDRawData& input = buffers.FEDData(static_cast<int>(iFed));
      //check on FEDRawData pointer and size
      if (!input.data() || !input.size())
        continue;
      //construct FEDBuffer
      const auto st_buffer = preconstructCheckFEDSpyBuffer(input);
      if (sistrip::FEDBufferStatusCode::SUCCESS != st_buffer) {
        edm::LogWarning("SiStripSpyIdentifyRuns")
            << "Exception caught when creating FEDSpyBuffer object for FED " << iFed << ": "
            << "An exception of category 'FEDBuffer' occurred.\n"
            << st_buffer;
        if (sistrip::FEDBufferStatusCode::EXPECT_SPY == st_buffer)
          break;
      }
      const sistrip::FEDSpyBuffer buffer{input};
      edm::LogWarning("SiStripSpyIdentifyRuns") << " -- this is a spy file, run " << lRunNum << std::endl;
      writeRunInFile(lRunNum);
      break;
    }
    //lFirstEvent = false;
    prevRun_ = lRunNum;
  }

  void SpyIdentifyRunsModule::writeRunInFile(const unsigned int aRunNumber) {
    if (!outFile_.is_open()) {
      edm::LogInfo("SiStripSpyIdentifyRuns") << aRunNumber << std::endl;
    } else {
      outFile_ << aRunNumber << std::endl;
    }
  }

  void SpyIdentifyRunsModule::endJob() {
    //save global run number in text file in local directory
    //output loginfo with number of errors
    //or throw exception ?
    if (outFile_.is_open())
      outFile_.close();
  }

}  // namespace sistrip

#include "FWCore/Framework/interface/MakerMacros.h"
typedef sistrip::SpyIdentifyRunsModule SiStripSpyIdentifyRuns;
DEFINE_FWK_MODULE(SiStripSpyIdentifyRuns);