CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
31 public:
33  ~ExternalLHEAsciiDumper() override;
34 
35 private:
36  void analyze(const edm::Event&, const edm::EventSetup&) override;
37  void endRun(edm::Run const&, edm::EventSetup const&) override;
38 
41 
43 
44  // ----------member data ---------------------------
45 };
46 
48  : lheProduct_(ps.getParameter<edm::InputTag>("lheProduct")),
49  lheFileName_(ps.getParameter<std::string>("lheFileName")) {
50  LHEAsciiToken_ = consumes<LHEXMLStringProduct, edm::InRun>(edm::InputTag(lheProduct_));
51 
52  return;
53 }
54 
56 
58 
59 // ------------ method called once each job just after ending the event loop ------------
60 
63  iRun.getByToken(LHEAsciiToken_, LHEAscii);
64 
65  const std::vector<std::string>& lheOutputs = LHEAscii->getStrings();
66 
67  unsigned int iout = 0;
68 
69  size_t lastdot = lheFileName_.find_last_of('.');
70  std::string basename = lheFileName_.substr(0, lastdot);
71  std::string extension = lastdot != std::string::npos ? lheFileName_.substr(lastdot + 1, std::string::npos) : "";
72 
73  for (unsigned int i = 0; i < lheOutputs.size(); ++i) {
74  std::ofstream outfile;
75  if (iout == 0)
76  outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
77  else {
78  std::stringstream fname;
79  fname << basename << "_" << iout;
80  if (!extension.empty())
81  fname << "." << extension;
82  outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
83  }
84  outfile << lheOutputs[i];
85  outfile.close();
86  ++iout;
87  }
88 
89  for (unsigned int i = 0; i < LHEAscii->getCompressed().size(); ++i) {
90  std::ofstream outfile;
91  if (iout == 0)
92  outfile.open(lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
93  else {
94  std::stringstream fname;
95  fname << basename << "_" << iout;
96  if (!extension.empty())
97  fname << "." << extension;
98  outfile.open(fname.str().c_str(), std::ofstream::out | std::ofstream::app);
99  }
100  LHEAscii->writeCompressedContent(outfile, i);
101  outfile.close();
102  ++iout;
103  }
104 }
105 
ExternalLHEAsciiDumper(const edm::ParameterSet &)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void analyze(const edm::Event &, const edm::EventSetup &) override
edm::EDGetTokenT< LHEXMLStringProduct > LHEAsciiToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Run.h:318
string fname
main script
void endRun(edm::Run const &, edm::EventSetup const &) override
Definition: Run.h:45