CMS 3D CMS Logo

CMSSW_4_4_3_patch1/src/FWCore/Framework/interface/OutputModule.h

Go to the documentation of this file.
00001 #ifndef FWCore_Framework_OutputModule_h
00002 #define FWCore_Framework_OutputModule_h
00003 
00004 /*----------------------------------------------------------------------
00005 
00006 OutputModule: The base class of all "modules" that write Events to an
00007 output stream.
00008 
00009 ----------------------------------------------------------------------*/
00010 
00011 #include "DataFormats/Provenance/interface/BranchChildren.h"
00012 #include "DataFormats/Provenance/interface/ParentageID.h"
00013 #include "DataFormats/Provenance/interface/ModuleDescription.h"
00014 #include "DataFormats/Provenance/interface/Selections.h"
00015 
00016 #include "FWCore/Framework/interface/CachedProducts.h"
00017 #include "FWCore/Framework/interface/Frameworkfwd.h"
00018 #include "FWCore/Framework/interface/GroupSelectorRules.h"
00019 #include "FWCore/Framework/interface/GroupSelector.h"
00020 #include "FWCore/ParameterSet/interface/ParameterSetfwd.h"
00021 
00022 #include "boost/array.hpp"
00023 #include "boost/utility.hpp"
00024 
00025 #include <string>
00026 #include <vector>
00027 
00028 namespace edm {
00029 
00030   typedef detail::CachedProducts::handle_t Trig;
00031 
00032   std::vector<std::string> const& getAllTriggerNames();
00033 
00034   class OutputModule : private boost::noncopyable {
00035   public:
00036     template <typename T> friend class WorkerT;
00037     friend class OutputWorker;
00038     typedef OutputModule ModuleType;
00039     typedef OutputWorker WorkerType;
00040 
00041     explicit OutputModule(ParameterSet const& pset);
00042     virtual ~OutputModule();
00045     int maxEvents() const {return maxEvents_;}
00046 
00049     int remainingEvents() const {return remainingEvents_;}
00050 
00051     bool selected(BranchDescription const& desc) const;
00052 
00053     void selectProducts();
00054     std::string const& processName() const {return process_name_;}
00055     SelectionsArray const& keptProducts() const {return keptProducts_;}
00056     boost::array<bool, NumBranchTypes> const& hasNewlyDroppedBranch() const {return hasNewlyDroppedBranch_;}
00057 
00058     static void fillDescription(ParameterSetDescription & desc);
00059     static void fillDescriptions(ConfigurationDescriptions& descriptions);
00060     static const std::string& baseType();
00061 
00062     BranchChildren const& branchChildren() const {return branchChildren_;}
00063 
00064     bool wantAllEvents() const {return wantAllEvents_;}
00065 
00066   protected:
00067     //Trig const& getTriggerResults(Event const& ep) const;
00068     Trig getTriggerResults(Event const& ep) const;
00069 
00070     // This function is needed for compatibility with older code. We
00071     // need to clean up the use of Event and EventPrincipal, to avoid
00072     // creation of multiple Event objects when handling a single
00073     // event.
00074     Trig getTriggerResults(EventPrincipal const& ep) const;
00075 
00076     // The returned pointer will be null unless the this is currently
00077     // executing its event loop function ('write').
00078     CurrentProcessingContext const* currentContext() const;
00079 
00080     ModuleDescription const& description() const;
00081 
00082     ParameterSetID selectorConfig() const { return selector_config_id_; }
00083 
00084     void doBeginJob();
00085     void doEndJob();
00086     bool doEvent(EventPrincipal const& ep, EventSetup const& c,
00087                  CurrentProcessingContext const* cpc);
00088     bool doBeginRun(RunPrincipal const& rp, EventSetup const& c,
00089                  CurrentProcessingContext const* cpc);
00090     bool doEndRun(RunPrincipal const& rp, EventSetup const& c,
00091                  CurrentProcessingContext const* cpc);
00092     bool doBeginLuminosityBlock(LuminosityBlockPrincipal const& lbp, EventSetup const& c,
00093                  CurrentProcessingContext const* cpc);
00094     bool doEndLuminosityBlock(LuminosityBlockPrincipal const& lbp, EventSetup const& c,
00095                  CurrentProcessingContext const* cpc);
00096 
00097   private:
00098 
00099     int maxEvents_;
00100     int remainingEvents_;
00101 
00102     // TODO: Give OutputModule
00103     // an interface (protected?) that supplies client code with the
00104     // needed functionality *without* giving away implementation
00105     // details ... don't just return a reference to keptProducts_, because
00106     // we are looking to have the flexibility to change the
00107     // implementation of keptProducts_ without modifying clients. When this
00108     // change is made, we'll have a one-time-only task of modifying
00109     // clients (classes derived from OutputModule) to use the
00110     // newly-introduced interface.
00111     // TODO: Consider using shared pointers here?
00112 
00113     // keptProducts_ are pointers to the BranchDescription objects describing
00114     // the branches we are to write.
00115     //
00116     // We do not own the BranchDescriptions to which we point.
00117     SelectionsArray keptProducts_;
00118     boost::array<bool, NumBranchTypes> hasNewlyDroppedBranch_;
00119 
00120     std::string process_name_;
00121     GroupSelectorRules groupSelectorRules_;
00122     GroupSelector groupSelector_;
00123     ModuleDescription moduleDescription_;
00124 
00125     // We do not own the pointed-to CurrentProcessingContext.
00126     CurrentProcessingContext const* current_context_;
00127 
00128     //This will store TriggerResults objects for the current event.
00129     // mutable std::vector<Trig> prods_;
00130     mutable bool prodsValid_;
00131 
00132     bool wantAllEvents_;
00133     mutable detail::CachedProducts selectors_;
00134     // ID of the ParameterSet that configured the event selector
00135     // subsystem.
00136     ParameterSetID selector_config_id_;
00137 
00138     typedef std::map<BranchID, std::set<ParentageID> > BranchParents;
00139     BranchParents branchParents_;
00140 
00141     BranchChildren branchChildren_;
00142 
00143     //------------------------------------------------------------------
00144     // private member functions
00145     //------------------------------------------------------------------
00146     void configure(OutputModuleDescription const& desc);
00147     void doWriteRun(RunPrincipal const& rp);
00148     void doWriteLuminosityBlock(LuminosityBlockPrincipal const& lbp);
00149     void doOpenFile(FileBlock const& fb);
00150     void doRespondToOpenInputFile(FileBlock const& fb);
00151     void doRespondToCloseInputFile(FileBlock const& fb);
00152     void doRespondToOpenOutputFiles(FileBlock const& fb);
00153     void doRespondToCloseOutputFiles(FileBlock const& fb);
00154     void doPreForkReleaseResources();
00155     void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren);
00156 
00157     std::string workerType() const {return "OutputWorker";}
00158 
00160     void doCloseFile();
00161 
00164     void maybeOpenFile();
00165 
00166 
00167     // Do the end-of-file tasks; this is only called internally, after
00168     // the appropriate tests have been done.
00169     void reallyCloseFile();
00170 
00171     void registerAnyProducts(OutputModule const*, ProductRegistry const*) {}
00172 
00174     virtual bool shouldWeCloseFile() const {return false;}
00175 
00176     virtual void write(EventPrincipal const& e) = 0;
00177     virtual void beginJob(){}
00178     virtual void endJob(){}
00179     virtual void beginRun(RunPrincipal const&){}
00180     virtual void endRun(RunPrincipal const&){}
00181     virtual void writeRun(RunPrincipal const&) = 0;
00182     virtual void beginLuminosityBlock(LuminosityBlockPrincipal const&){}
00183     virtual void endLuminosityBlock(LuminosityBlockPrincipal const&){}
00184     virtual void writeLuminosityBlock(LuminosityBlockPrincipal const&) = 0;
00185     virtual void openFile(FileBlock const&) {}
00186     virtual void respondToOpenInputFile(FileBlock const&) {}
00187     virtual void respondToCloseInputFile(FileBlock const&) {}
00188     virtual void respondToOpenOutputFiles(FileBlock const&) {}
00189     virtual void respondToCloseOutputFiles(FileBlock const&) {}
00190     virtual void preForkReleaseResources() {}
00191     virtual void postForkReacquireResources(unsigned int /*iChildIndex*/, unsigned int /*iNumberOfChildren*/) {}
00192 
00193     virtual bool isFileOpen() const { return true; }
00194 
00195     virtual void doOpenFile() { }
00196 
00197     void setModuleDescription(ModuleDescription const& md) {
00198       moduleDescription_ = md;
00199     }
00200 
00201     void updateBranchParents(EventPrincipal const& ep);
00202     void fillDependencyGraph();
00203 
00204     bool limitReached() const {return remainingEvents_ == 0;}
00205 
00206     // The following member functions are part of the Template Method
00207     // pattern, used for implementing doCloseFile() and maybeEndFile().
00208 
00209     virtual void startEndFile() {}
00210     virtual void writeFileFormatVersion() {}
00211     virtual void writeFileIdentifier() {}
00212     virtual void writeIndexIntoFile() {}
00213     virtual void writeProcessConfigurationRegistry() {}
00214     virtual void writeProcessHistoryRegistry() {}
00215     virtual void writeParameterSetRegistry() {}
00216     virtual void writeBranchIDListRegistry() {}
00217     virtual void writeParentageRegistry() {}
00218     virtual void writeProductDescriptionRegistry() {}
00219     virtual void writeProductDependencies() {}
00220     virtual void writeBranchMapper() {}
00221     virtual void finishEndFile() {}
00222   };
00223 }
00224 
00225 //this is included after the class definition since this header also needs to know about OutputModule
00226 // we put this here since all OutputModules need this header to create their plugin
00227 #include "FWCore/Framework/src/OutputWorker.h"
00228 
00229 #endif