CMS 3D CMS Logo

ExternalLHEAsciiDumper.cc
Go to the documentation of this file.
1 // F. Cossutti
2 
3 // Dump in standard ascii format the LHE file stored as string lumi product
4 
5 // system include files
6 #include <memory>
7 #include <string>
8 #include <sstream>
9 #include <fstream>
10 
11 // user include files
15 
21 
23 
25 
26 //
27 // class declaration
28 //
29 
30 class ExternalLHEAsciiDumper : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
31 public:
33  ~ExternalLHEAsciiDumper() override = default;
34 
35 private:
36  void analyze(const edm::Event&, const edm::EventSetup&) override {}
37  void beginRun(edm::Run const&, edm::EventSetup const&) override {}
38  void endRun(edm::Run const&, edm::EventSetup const&) override;
39 
42 
44 
45  // ----------member data ---------------------------
46 };
47 
49  : lheProduct_(ps.getParameter<edm::InputTag>("lheProduct")),
50  lheFileName_(ps.getParameter<std::string>("lheFileName")),
51  LHEAsciiToken_(consumes<LHEXMLStringProduct, edm::InRun>(edm::InputTag(lheProduct_))) {}
52 
53 // ------------ method called once each job just after ending the event loop ------------
54 
57 
58  const std::vector<std::string>& lheOutputs = LHEAscii->getStrings();
59 
60  unsigned int iout = 0;
61 
62  size_t lastdot = lheFileName_.find_last_of('.');
63  std::string basename = lheFileName_.substr(0, lastdot);
64  std::string extension = lastdot != std::string::npos ? lheFileName_.substr(lastdot + 1, std::string::npos) : "";
65 
66  for (unsigned int i = 0; i < lheOutputs.size(); ++i) {
67  std::ofstream outfile;
68  if (iout == 0)
69  outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
70  else {
71  std::stringstream fname;
72  fname << basename << "_" << iout;
73  if (!extension.empty())
74  fname << "." << extension;
75  outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
76  }
77  outfile << lheOutputs[i];
78  outfile.close();
79  ++iout;
80  }
81 
82  for (unsigned int i = 0; i < LHEAscii->getCompressed().size(); ++i) {
83  std::ofstream outfile;
84  if (iout == 0)
85  outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
86  else {
87  std::stringstream fname;
88  fname << basename << "_" << iout;
89  if (!extension.empty())
90  fname << "." << extension;
91  outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
92  }
93  LHEAscii->writeCompressedContent(outfile, i);
94  outfile.close();
95  ++iout;
96  }
97 }
98 
void beginRun(edm::Run const &, edm::EventSetup const &) override
ExternalLHEAsciiDumper(const edm::ParameterSet &)
void analyze(const edm::Event &, const edm::EventSetup &) override
void writeCompressedContent(std::ostream &output, unsigned int i) const
const edm::InputTag lheProduct_
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const std::vector< std::vector< uint8_t > > & getCompressed() const
const std::vector< std::string > & getStrings() const
~ExternalLHEAsciiDumper() override=default
string fname
main script
HLT enums.
void endRun(edm::Run const &, edm::EventSetup const &) override
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Run.h:346
Definition: Run.h:45
const edm::EDGetTokenT< LHEXMLStringProduct > LHEAsciiToken_