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 #include <boost/algorithm/string.hpp>
11 
12 // user include files
16 
22 
24 
26 
27 //
28 // class declaration
29 //
30 
32 public:
34  ~ExternalLHEAsciiDumper() override;
35 
36 private:
37  void analyze(const edm::Event&, const edm::EventSetup&) 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  return;
54 }
55 
57 
59 
60 // ------------ method called once each job just after ending the event loop ------------
61 
64  iRun.getByToken(LHEAsciiToken_, LHEAscii);
65 
66  const std::vector<std::string>& lheOutputs = LHEAscii->getStrings();
67 
68  unsigned int iout = 0;
69 
70  size_t lastdot = lheFileName_.find_last_of(".");
71  std::string basename = lheFileName_.substr(0, lastdot);
72  std::string extension = lastdot != std::string::npos ? lheFileName_.substr(lastdot + 1, std::string::npos) : "";
73 
74  for (unsigned int i = 0; i < lheOutputs.size(); ++i) {
75  std::ofstream outfile;
76  if (iout == 0)
77  outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
78  else {
79  std::stringstream fname;
80  fname << basename << "_" << iout;
81  if (!extension.empty())
82  fname << "." << extension;
83  outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
84  }
85  outfile << lheOutputs[i];
86  outfile.close();
87  ++iout;
88  }
89 
90  for (unsigned int i = 0; i < LHEAscii->getCompressed().size(); ++i) {
91  std::ofstream outfile;
92  if (iout == 0)
93  outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
94  else {
95  std::stringstream fname;
96  fname << basename << "_" << iout;
97  if (!extension.empty())
98  fname << "." << extension;
99  outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
100  }
101  LHEAscii->writeCompressedContent(outfile, i);
102  outfile.close();
103  ++iout;
104  }
105 }
106 
ExternalLHEAsciiDumper(const edm::ParameterSet &)
void analyze(const edm::Event &, const edm::EventSetup &) override
const std::vector< std::string > & getStrings() const
const std::vector< std::vector< uint8_t > > & getCompressed() const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
edm::EDGetTokenT< LHEXMLStringProduct > LHEAsciiToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:315
string fname
main script
HLT enums.
void writeCompressedContent(std::ostream &output, unsigned int i) const
void endRun(edm::Run const &, edm::EventSetup const &) override
Definition: Run.h:45