CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/GeneratorInterface/LHEInterface/plugins/ExternalLHEAsciiDumper.cc

Go to the documentation of this file.
00001 // F. Cossutti
00002 // $Date: 2013/03/01 21:45:29 $
00003 // $Revision: 1.4 $//
00004 
00005 // Dump in standard ascii format the LHE file stored as string lumi product
00006 
00007 
00008 // system include files
00009 #include <memory>
00010 #include <string>
00011 #include <sstream>
00012 #include <fstream>
00013 #include <boost/algorithm/string.hpp>
00014 
00015 // user include files
00016 #include "FWCore/Framework/interface/Frameworkfwd.h"
00017 #include "FWCore/Framework/interface/EDAnalyzer.h"
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 
00020 #include "FWCore/Framework/interface/Event.h"
00021 #include "FWCore/Framework/interface/MakerMacros.h"
00022 #include "FWCore/Framework/interface/EventSetup.h"
00023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00024 #include "FWCore/Framework/interface/Run.h"
00025 
00026 #include "SimDataFormats/GeneratorProducts/interface/LHEXMLStringProduct.h"
00027 
00028 #include "FWCore/Utilities/interface/InputTag.h"
00029 
00030 //
00031 // class declaration
00032 //
00033 
00034 class ExternalLHEAsciiDumper : public edm::EDAnalyzer {
00035 public:
00036   explicit ExternalLHEAsciiDumper(const edm::ParameterSet&);
00037   ~ExternalLHEAsciiDumper();
00038   
00039   
00040 private:
00041   virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
00042   virtual void endRun(edm::Run const&, edm::EventSetup const&) override;
00043 
00044   edm::InputTag lheProduct_;
00045   std::string   lheFileName_;
00046 
00047   // ----------member data ---------------------------
00048   
00049 };
00050 
00051 ExternalLHEAsciiDumper::ExternalLHEAsciiDumper(const edm::ParameterSet& ps):
00052   lheProduct_( ps.getParameter<edm::InputTag>("lheProduct") ),
00053   lheFileName_( ps.getParameter<std::string>("lheFileName") )
00054 {
00055   
00056   return;
00057   
00058 }
00059 
00060 ExternalLHEAsciiDumper::~ExternalLHEAsciiDumper()
00061 {
00062 }
00063 
00064 void
00065 ExternalLHEAsciiDumper::analyze(const edm::Event&, const edm::EventSetup&)
00066 {
00067 }
00068 
00069 // ------------ method called once each job just after ending the event loop  ------------
00070 
00071 void
00072 ExternalLHEAsciiDumper::endRun(edm::Run const& iRun, edm::EventSetup const&) {
00073 
00074   edm::Handle< LHEXMLStringProduct > LHEAscii;
00075   iRun.getByLabel(lheProduct_,LHEAscii);
00076   
00077   const std::vector<std::string>& lheOutputs = LHEAscii->getStrings();
00078 
00079   size_t lastdot = lheFileName_.find_last_of(".");
00080   std::string basename = lheFileName_.substr(0, lastdot);
00081   std::string extension = lastdot != std::string::npos ?  lheFileName_.substr(lastdot+1, std::string::npos) : "";
00082 
00083   for (unsigned int i = 0; i < lheOutputs.size(); ++i){
00084     std::ofstream outfile;
00085     if (i == 0)
00086       outfile.open (lheFileName_.c_str(), std::ofstream::out | std::ofstream::app);
00087     else {
00088       std::stringstream fname;
00089       fname << basename << "_" << i ;
00090       if (extension != "")
00091         fname << "." << extension;
00092       outfile.open (fname.str().c_str(), std::ofstream::out | std::ofstream::app);
00093     }
00094     outfile << lheOutputs[i];
00095     outfile.close();
00096   }
00097 
00098 }
00099 
00100 DEFINE_FWK_MODULE(ExternalLHEAsciiDumper);