Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-03-14 23:36:19

0001 /*----------------------------------------------------------------------
0002 ----------------------------------------------------------------------*/
0003 
0004 #include <algorithm>
0005 #include <iterator>
0006 #include <ostream>
0007 #include <iostream>
0008 #include <string>
0009 #include "FWCore/Framework/interface/global/OutputModule.h"
0010 #include "FWCore/Framework/interface/EventForOutput.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "DataFormats/Provenance/interface/Provenance.h"
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
0016 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0017 #include "FWCore/ParameterSet/interface/Registry.h"
0018 
0019 namespace edm {
0020 
0021   class ModuleCallingContext;
0022 
0023   class AsciiOutputModule : public global::OutputModule<> {
0024   public:
0025     // We do not take ownership of passed stream.
0026     explicit AsciiOutputModule(ParameterSet const& pset);
0027     ~AsciiOutputModule() override;
0028     static void fillDescriptions(ConfigurationDescriptions& descriptions);
0029 
0030   private:
0031     void write(EventForOutput const& e) override;
0032     void writeLuminosityBlock(LuminosityBlockForOutput const&) override {}
0033     void writeRun(RunForOutput const&) override {}
0034     int prescale_;
0035     int verbosity_;
0036     int counter_;
0037     bool allProvenance_;
0038   };
0039 
0040   AsciiOutputModule::AsciiOutputModule(ParameterSet const& pset)
0041       : global::OutputModuleBase(pset),
0042         global::OutputModule<>(pset),
0043         prescale_(pset.getUntrackedParameter<unsigned int>("prescale")),
0044         verbosity_(pset.getUntrackedParameter<unsigned int>("verbosity")),
0045         counter_(0),
0046         allProvenance_(pset.getUntrackedParameter<bool>("allProvenance")) {
0047     if (prescale_ == 0)
0048       prescale_ = 1;
0049   }
0050 
0051   AsciiOutputModule::~AsciiOutputModule() {
0052     LogAbsolute("AsciiOut") << ">>> processed " << counter_ << " events" << std::endl;
0053   }
0054 
0055   void AsciiOutputModule::write(EventForOutput const& e) {
0056     if ((++counter_ % prescale_) != 0 || verbosity_ <= 0)
0057       return;
0058 
0059     // RunForOutput const& run = evt.getRun(); // this is still unused
0060     LogAbsolute("AsciiOut") << ">>> processing event # " << e.id() << " time " << e.time().value() << std::endl;
0061 
0062     if (verbosity_ <= 1)
0063       return;
0064 
0065     // Write out non-EDProduct contents...
0066 
0067     // ... list of process-names
0068     for (auto const& process : e.processHistory()) {
0069       LogAbsolute("AsciiOut") << process.processName() << " ";
0070     }
0071 
0072     // ... collision id
0073     LogAbsolute("AsciiOut") << '\n' << e.id() << '\n';
0074 
0075     // Loop over products, and write some output for each...
0076     for (auto const& prod : e.productRegistry().productList()) {
0077       ProductDescription const& desc = prod.second;
0078       if (selected(desc)) {
0079         if (desc.isAlias()) {
0080           LogAbsolute("AsciiOut") << "ModuleLabel " << desc.moduleLabel() << " is an alias for";
0081         }
0082 
0083         auto const& prov = e.getProvenance(desc.originalBranchID());
0084         LogAbsolute("AsciiOut") << prov;
0085 
0086         if (verbosity_ > 2) {
0087           ProductDescription const& desc2 = prov.productDescription();
0088           std::string const& process = desc2.processName();
0089           std::string const& label = desc2.moduleLabel();
0090           ProcessHistory const& processHistory = e.processHistory();
0091 
0092           for (ProcessConfiguration const& pc : processHistory) {
0093             if (pc.processName() == process) {
0094               ParameterSetID const& psetID = pc.parameterSetID();
0095               pset::Registry const* psetRegistry = pset::Registry::instance();
0096               ParameterSet const* processPset = psetRegistry->getMapped(psetID);
0097               if (processPset) {
0098                 if (desc.isAlias()) {
0099                   LogAbsolute("AsciiOut") << "Alias PSet\n" << processPset->getParameterSet(desc.moduleLabel());
0100                 }
0101                 LogAbsolute("AsciiOut") << processPset->getParameterSet(label) << "\n";
0102               }
0103             }
0104           }
0105         }
0106       } else if (allProvenance_) {
0107         auto const& prov = e.getStableProvenance(desc.originalBranchID());
0108         LogAbsolute("AsciiOut") << prov;
0109         if (verbosity_ > 2) {
0110           ProductDescription const& desc2 = prov.productDescription();
0111           std::string const& process = desc2.processName();
0112           std::string const& label = desc2.moduleLabel();
0113           ProcessHistory const& processHistory = e.processHistory();
0114 
0115           for (ProcessConfiguration const& pc : processHistory) {
0116             if (pc.processName() == process) {
0117               ParameterSetID const& psetID = pc.parameterSetID();
0118               pset::Registry const* psetRegistry = pset::Registry::instance();
0119               ParameterSet const* processPset = psetRegistry->getMapped(psetID);
0120               if (processPset) {
0121                 if (desc.isAlias()) {
0122                   LogAbsolute("AsciiOut") << "Alias PSet\n" << processPset->getParameterSet(desc.moduleLabel());
0123                 }
0124                 LogAbsolute("AsciiOut") << processPset->getParameterSet(label) << "\n";
0125               }
0126             }
0127           }
0128         }
0129       }
0130     }
0131   }
0132 
0133   void AsciiOutputModule::fillDescriptions(ConfigurationDescriptions& descriptions) {
0134     ParameterSetDescription desc;
0135     desc.setComment("Outputs event information into text file.");
0136     desc.addUntracked("prescale", 1U)->setComment("prescale factor");
0137     desc.addUntracked("verbosity", 1U)
0138         ->setComment(
0139             "0: no output\n"
0140             "1: event ID and timestamp only\n"
0141             "2: provenance for each kept product\n"
0142             ">2: PSet and provenance for each kept product");
0143     desc.addUntracked("allProvenance", false)
0144         ->setComment("when printing provenance info, also print stable provenance of non-kept data products.");
0145     OutputModule::fillDescription(desc);
0146     descriptions.add("asciiOutput", desc);
0147   }
0148 }  // namespace edm
0149 
0150 using edm::AsciiOutputModule;
0151 DEFINE_FWK_MODULE(AsciiOutputModule);