File indexing completed on 2023-03-17 11:04:46
0001
0002
0003
0004
0005
0006 #include <memory>
0007 #include <string>
0008 #include <sstream>
0009 #include <fstream>
0010
0011
0012 #include "FWCore/Framework/interface/Frameworkfwd.h"
0013 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015
0016 #include "FWCore/Framework/interface/Event.h"
0017 #include "FWCore/Framework/interface/MakerMacros.h"
0018 #include "FWCore/Framework/interface/EventSetup.h"
0019 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0020 #include "FWCore/Framework/interface/Run.h"
0021
0022 #include "SimDataFormats/GeneratorProducts/interface/LHEXMLStringProduct.h"
0023
0024 #include "FWCore/Utilities/interface/InputTag.h"
0025
0026
0027
0028
0029
0030 class ExternalLHEAsciiDumper : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0031 public:
0032 explicit ExternalLHEAsciiDumper(const edm::ParameterSet&);
0033 ~ExternalLHEAsciiDumper() override = default;
0034
0035 private:
0036 void analyze(const edm::Event&, const edm::EventSetup&) override {}
0037 void beginRun(edm::Run const&, edm::EventSetup const&) override {}
0038 void endRun(edm::Run const&, edm::EventSetup const&) override;
0039
0040 const edm::InputTag lheProduct_;
0041 const std::string lheFileName_;
0042
0043 const edm::EDGetTokenT<LHEXMLStringProduct> LHEAsciiToken_;
0044
0045
0046 };
0047
0048 ExternalLHEAsciiDumper::ExternalLHEAsciiDumper(const edm::ParameterSet& ps)
0049 : lheProduct_(ps.getParameter<edm::InputTag>("lheProduct")),
0050 lheFileName_(ps.getParameter<std::string>("lheFileName")),
0051 LHEAsciiToken_(consumes<LHEXMLStringProduct, edm::InRun>(edm::InputTag(lheProduct_))) {}
0052
0053
0054
0055 void ExternalLHEAsciiDumper::endRun(edm::Run const& iRun, edm::EventSetup const&) {
0056 const edm::Handle<LHEXMLStringProduct>& LHEAscii = iRun.getHandle(LHEAsciiToken_);
0057
0058 const std::vector<std::string>& lheOutputs = LHEAscii->getStrings();
0059
0060 unsigned int iout = 0;
0061
0062 size_t lastdot = lheFileName_.find_last_of('.');
0063 std::string basename = lheFileName_.substr(0, lastdot);
0064 std::string extension = lastdot != std::string::npos ? lheFileName_.substr(lastdot + 1, std::string::npos) : "";
0065
0066 for (unsigned int i = 0; i < lheOutputs.size(); ++i) {
0067 std::ofstream outfile;
0068 if (iout == 0)
0069 outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
0070 else {
0071 std::stringstream fname;
0072 fname << basename << "_" << iout;
0073 if (!extension.empty())
0074 fname << "." << extension;
0075 outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
0076 }
0077 outfile << lheOutputs[i];
0078 outfile.close();
0079 ++iout;
0080 }
0081
0082 for (unsigned int i = 0; i < LHEAscii->getCompressed().size(); ++i) {
0083 std::ofstream outfile;
0084 if (iout == 0)
0085 outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
0086 else {
0087 std::stringstream fname;
0088 fname << basename << "_" << iout;
0089 if (!extension.empty())
0090 fname << "." << extension;
0091 outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
0092 }
0093 LHEAscii->writeCompressedContent(outfile, i);
0094 outfile.close();
0095 ++iout;
0096 }
0097 }
0098
0099 DEFINE_FWK_MODULE(ExternalLHEAsciiDumper);