ExternalLHEAsciiDumper

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
// F. Cossutti

// Dump in standard ascii format the LHE file stored as string lumi product

// system include files
#include <memory>
#include <string>
#include <sstream>
#include <fstream>

// user include files
#include "FWCore/Framework/interface/Frameworkfwd.h"
#include "FWCore/Framework/interface/one/EDAnalyzer.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

#include "FWCore/Framework/interface/Event.h"
#include "FWCore/Framework/interface/MakerMacros.h"
#include "FWCore/Framework/interface/EventSetup.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Framework/interface/Run.h"

#include "SimDataFormats/GeneratorProducts/interface/LHEXMLStringProduct.h"

#include "FWCore/Utilities/interface/InputTag.h"

//
// class declaration
//

class ExternalLHEAsciiDumper : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
public:
  explicit ExternalLHEAsciiDumper(const edm::ParameterSet&);
  ~ExternalLHEAsciiDumper() override = default;

private:
  void analyze(const edm::Event&, const edm::EventSetup&) override {}
  void beginRun(edm::Run const&, edm::EventSetup const&) override {}
  void endRun(edm::Run const&, edm::EventSetup const&) override;

  const edm::InputTag lheProduct_;
  const std::string lheFileName_;

  const edm::EDGetTokenT<LHEXMLStringProduct> LHEAsciiToken_;

  // ----------member data ---------------------------
};

ExternalLHEAsciiDumper::ExternalLHEAsciiDumper(const edm::ParameterSet& ps)
    : lheProduct_(ps.getParameter<edm::InputTag>("lheProduct")),
      lheFileName_(ps.getParameter<std::string>("lheFileName")),
      LHEAsciiToken_(consumes<LHEXMLStringProduct, edm::InRun>(edm::InputTag(lheProduct_))) {}

// ------------ method called once each job just after ending the event loop  ------------

void ExternalLHEAsciiDumper::endRun(edm::Run const& iRun, edm::EventSetup const&) {
  const edm::Handle<LHEXMLStringProduct>& LHEAscii = iRun.getHandle(LHEAsciiToken_);

  const std::vector<std::string>& lheOutputs = LHEAscii->getStrings();

  unsigned int iout = 0;

  size_t lastdot = lheFileName_.find_last_of('.');
  std::string basename = lheFileName_.substr(0, lastdot);
  std::string extension = lastdot != std::string::npos ? lheFileName_.substr(lastdot + 1, std::string::npos) : "";

  for (unsigned int i = 0; i < lheOutputs.size(); ++i) {
    std::ofstream outfile;
    if (iout == 0)
      outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
    else {
      std::stringstream fname;
      fname << basename << "_" << iout;
      if (!extension.empty())
        fname << "." << extension;
      outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
    }
    outfile << lheOutputs[i];
    outfile.close();
    ++iout;
  }

  for (unsigned int i = 0; i < LHEAscii->getCompressed().size(); ++i) {
    std::ofstream outfile;
    if (iout == 0)
      outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
    else {
      std::stringstream fname;
      fname << basename << "_" << iout;
      if (!extension.empty())
        fname << "." << extension;
      outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
    }
    LHEAscii->writeCompressedContent(outfile, i);
    outfile.close();
    ++iout;
  }
}

DEFINE_FWK_MODULE(ExternalLHEAsciiDumper);