LHEWriter

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
#include <algorithm>
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <memory>

#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/Run.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
#include "SimDataFormats/GeneratorProducts/interface/LHEEventProduct.h"

using namespace lhef;

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

protected:
  void beginRun(const edm::Run &run, const edm::EventSetup &es) override;
  void endRun(const edm::Run &run, const edm::EventSetup &es) override;
  void analyze(const edm::Event &event, const edm::EventSetup &es) override;

private:
  std::ofstream file;
  std::ofstream file1;

  edm::EDGetTokenT<LHERunInfoProduct> tokenLHERunInfo_;
  edm::EDGetTokenT<LHEEventProduct> tokenLHEEvent_;
};

LHEWriter::LHEWriter(const edm::ParameterSet &params)
    : tokenLHERunInfo_(consumes<LHERunInfoProduct, edm::InRun>(
          params.getUntrackedParameter<edm::InputTag>("moduleLabel", std::string("source")))),
      tokenLHEEvent_(consumes<LHEEventProduct>(
          params.getUntrackedParameter<edm::InputTag>("moduleLabel", std::string("source")))) {}

LHEWriter::~LHEWriter() {}

void LHEWriter::beginRun(const edm::Run &run, const edm::EventSetup &es) {
  file.open("writer.lhe", std::fstream::out | std::fstream::trunc);
  file1.open("writer1.lhe", std::fstream::out | std::fstream::trunc);
}

void LHEWriter::endRun(const edm::Run &run, const edm::EventSetup &es) {
  const edm::Handle<LHERunInfoProduct> &product = run.getHandle(tokenLHERunInfo_);
  //run.getByLabel("source", product);
  //run.getByToken(tokenLHERunInfo_, product);

  std::copy(product->begin(), product->end(), std::ostream_iterator<std::string>(file));

  file1 << LHERunInfoProduct::endOfFile();
  file.close();
  file1.close();

  system("cat writer1.lhe >> writer.lhe");
  system("rm -rf writer1.lhe");
}

void LHEWriter::analyze(const edm::Event &event, const edm::EventSetup &es) {
  const edm::Handle<LHEEventProduct> &product = event.getHandle(tokenLHEEvent_);
  //event.getByLabel("source", product);
  //event.getByToken(tokenLHEEvent_, product);

  std::copy(product->begin(), product->end(), std::ostream_iterator<std::string>(file1));
}

DEFINE_FWK_MODULE(LHEWriter);