Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:48:03

0001 // system include files
0002 #include <memory>
0003 #include <iostream>
0004 #include <fstream>
0005 
0006 // user include files
0007 #include "FWCore/Framework/interface/Frameworkfwd.h"
0008 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0009 
0010 #include "FWCore/Framework/interface/Event.h"
0011 #include "FWCore/Framework/interface/Run.h"
0012 #include "FWCore/Framework/interface/MakerMacros.h"
0013 
0014 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0015 #include "SimDataFormats/GeneratorProducts/interface/LHERunInfoProduct.h"
0016 //
0017 // class decleration
0018 //
0019 
0020 class AlpgenExtractor : public edm::one::EDAnalyzer<edm::one::WatchRuns> {
0021 public:
0022   explicit AlpgenExtractor(const edm::ParameterSet&);
0023   ~AlpgenExtractor() override = default;
0024 
0025 private:
0026   void beginRun(const edm::Run&, const edm::EventSetup&) override;
0027   void endRun(const edm::Run&, const edm::EventSetup&) override {}
0028   void analyze(const edm::Event&, const edm::EventSetup&) override {}
0029   void writeHeader(const std::vector<LHERunInfoProduct::Header>::const_iterator, const std::string);
0030   // ----------member data ---------------------------
0031   const std::string unwParFile_;
0032   const std::string wgtFile_;
0033   const std::string parFile_;
0034   const edm::EDGetTokenT<LHERunInfoProduct> tokenLHERun_;
0035 };
0036 
0037 //
0038 // constants, enums and typedefs
0039 //
0040 
0041 //
0042 // static data member definitions
0043 //
0044 
0045 //
0046 // constructors and destructor
0047 //
0048 AlpgenExtractor::AlpgenExtractor(const edm::ParameterSet& iConfig)
0049     : unwParFile_(iConfig.getUntrackedParameter<std::string>("unwParFile")),
0050       wgtFile_(iConfig.getUntrackedParameter<std::string>("wgtFile")),
0051       parFile_(iConfig.getUntrackedParameter<std::string>("parFile")),
0052       tokenLHERun_(consumes<LHERunInfoProduct, edm::InRun>(edm::InputTag("source"))) {
0053   //now do what ever initialization is needed
0054 }
0055 
0056 //
0057 // member functions
0058 //
0059 
0060 // ------------ method called to for each run    ------------
0061 void AlpgenExtractor::beginRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {
0062   const edm::Handle<LHERunInfoProduct>& runInfo = iRun.getHandle(tokenLHERun_);
0063   std::cout << "Found " << runInfo->headers_size() << " headers." << std::endl;
0064 
0065   std::vector<LHERunInfoProduct::Header>::const_iterator headers = runInfo->headers_begin();
0066   // Skip the first header -initial comments.
0067   // BOTH the order and the increment of the headers variable are crucial here -
0068   // check the AlpgenSource code for more information.
0069   // Write the _unw.par file.
0070   headers++;
0071   writeHeader(headers, unwParFile_);
0072   // Write the .wgt file.
0073   headers++;
0074   writeHeader(headers, wgtFile_);
0075   // Write the .par file.
0076   headers++;
0077   writeHeader(headers, parFile_);
0078 }
0079 
0080 void AlpgenExtractor::writeHeader(std::vector<LHERunInfoProduct::Header>::const_iterator header,
0081                                   const std::string filename) {
0082   std::ofstream outfile;
0083   outfile.open(filename.c_str());
0084   for (LHERunInfoProduct::Header::const_iterator i = header->begin(); i != header->end(); ++i) {
0085     outfile << *i;
0086   }
0087   outfile.close();
0088 }
0089 
0090 //define this as a plug-in
0091 DEFINE_FWK_MODULE(AlpgenExtractor);