SpyExtractRunModule

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
// 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/SiStripCommon/interface/ConstantsForHardwareSystems.h"

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

//
// Class declaration
//
namespace sistrip {

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

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

    //check when the current run changes
    const bool updateRun(const uint32_t aRun);

    //name of the output file containing the run number
    //get it from the input file
    std::string fileName_;

    //tag of spydata run number collection
    edm::InputTag runTag_;
    edm::EDGetTokenT<uint32_t> runToken_;

    //cache of the current and previous run number
    uint32_t currentRun_;
    uint32_t previousRun_;

    //error counter for number of times the run number changes
    uint32_t errCounter_;
  };
}  // namespace sistrip

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

  SpyExtractRunModule::SpyExtractRunModule(const edm::ParameterSet& iConfig)
      : fileName_(iConfig.getParameter<std::string>("OutputTextFile")),
        runTag_(iConfig.getParameter<edm::InputTag>("RunNumberTag")),
        currentRun_(0),
        previousRun_(0),
        errCounter_(0) {
    runToken_ = consumes<uint32_t>(runTag_);
  }

  SpyExtractRunModule::~SpyExtractRunModule() {}

  void SpyExtractRunModule::beginJob() {
    currentRun_ = 0;
    previousRun_ = 0;
    errCounter_ = 0;
  }

  void SpyExtractRunModule::analyze(const edm::Event& aEvt, const edm::EventSetup& aSetup) {
    static bool lFirstEvent = true;
    edm::Handle<uint32_t> lRun;
    aEvt.getByToken(runToken_, lRun);

    const bool isUpdated = updateRun(*lRun);

    if (isUpdated && !lFirstEvent) {
      edm::LogError("SpyExtractRunModule")
          << " -- Run number changed for event : " << aEvt.id().event() << " (id().run() = " << aEvt.id().run()
          << ") from " << previousRun_ << " to " << currentRun_ << std::endl;
    }

    lFirstEvent = false;
  }

  void SpyExtractRunModule::endJob() {
    //save global run number in text file in local directory
    //output loginfo with number of errors
    //or throw exception ?

    if (errCounter_ == 1) {
      edm::LogInfo("SiStripSpyExtractRun")
          << " -- Writting run number " << currentRun_ << " into file " << fileName_ << std::endl;
      std::ofstream lOutFile;
      lOutFile.open(fileName_.c_str(), std::ios::out);
      if (!lOutFile.is_open()) {
        edm::LogError("SiStripSpyExtractRun")
            << " -- Cannot open file : " << fileName_ << " for writting run number " << currentRun_ << std::endl;
      } else {
        lOutFile << currentRun_ << std::endl;
        lOutFile.close();
      }

    } else {
      edm::LogError("SiStripSpyExtractRun")
          << " -- Number of times the run number changed in this job = " << errCounter_
          << ", currentRun = " << currentRun_ << ", previousRun = " << previousRun_ << std::endl;
    }
  }

  const bool SpyExtractRunModule::updateRun(const uint32_t aRun) {
    if (aRun != currentRun_) {
      previousRun_ = currentRun_;
      currentRun_ = aRun;
      errCounter_++;
      return true;
    }
    return false;
  }

}  // namespace sistrip

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