CMS 3D CMS Logo

OutputModule.h
Go to the documentation of this file.
1 #ifndef FWCore_Framework_OutputModule_h
2 #define FWCore_Framework_OutputModule_h
3 
4 /*----------------------------------------------------------------------
5 
6 OutputModule: The base class of all "modules" that write Events to an
7 output stream.
8 
9 ----------------------------------------------------------------------*/
10 
15 
23 
25 
28 
29 #include <array>
30 #include <memory>
31 #include <string>
32 #include <vector>
33 #include <map>
34 #include <atomic>
35 
36 namespace edm {
37 
38  class ModuleCallingContext;
39  class PreallocationConfiguration;
40  class ActivityRegistry;
41  class ProductRegistry;
42  class ThinnedAssociationsHelper;
43  class WaitingTask;
44 
45  namespace maker {
46  template<typename T> class ModuleHolderT;
47  }
48 
50 
51  class OutputModule : public EDConsumerBase {
52  public:
53  template <typename T> friend class maker::ModuleHolderT;
54  template <typename T> friend class WorkerT;
55  template <typename T> friend class OutputModuleCommunicatorT;
57 
58  explicit OutputModule(ParameterSet const& pset);
59  virtual ~OutputModule();
60 
61  OutputModule(OutputModule const&) = delete; // Disallow copying and moving
62  OutputModule& operator=(OutputModule const&) = delete; // Disallow copying and moving
63 
66  int maxEvents() const {return maxEvents_;}
67 
70  int remainingEvents() const {return remainingEvents_;}
71 
72  bool selected(BranchDescription const& desc) const;
73 
74  void selectProducts(ProductRegistry const& preg, ThinnedAssociationsHelper const&);
75  std::string const& processName() const {return process_name_;}
76  SelectedProductsForBranchType const& keptProducts() const {return keptProducts_;}
77  std::array<bool, NumBranchTypes> const& hasNewlyDroppedBranch() const {return hasNewlyDroppedBranch_;}
78 
79  static void fillDescription(ParameterSetDescription & desc, std::vector<std::string> const& iDefaultOutputCommands = ProductSelectorRules::defaultSelectionStrings());
80  static void fillDescriptions(ConfigurationDescriptions& descriptions);
81  static const std::string& baseType();
82  static void prevalidate(ConfigurationDescriptions& );
83 
84  bool wantAllEvents() const {return wantAllEvents_;}
85 
86  BranchIDLists const* branchIDLists();
87 
88  ThinnedAssociationsHelper const* thinnedAssociationsHelper() const;
89 
90  protected:
91 
92  // This function is needed for compatibility with older code. We
93  // need to clean up the use of EventForOutputand EventPrincipal, to avoid
94  // creation of multiple EventForOutputobjects when handling a single
95  // event.
96  Trig getTriggerResults(EDGetTokenT<TriggerResults> const& token, EventForOutput const& e) const;
97 
98  ModuleDescription const& description() const;
99  ModuleDescription const& moduleDescription() const { return moduleDescription_;
100  }
101 
102  ParameterSetID selectorConfig() const { return selector_config_id_; }
103 
104  void doPreallocate(PreallocationConfiguration const&);
105 
106  void doBeginJob();
107  void doEndJob();
108  bool doEvent(EventPrincipal const& ep, EventSetup const& c,
109  ActivityRegistry* act,
110  ModuleCallingContext const* mcc);
111  //Needed by WorkerT but not supported
112  void preActionBeforeRunEventAsync(WaitingTask* iTask, ModuleCallingContext const& iModuleCallingContext, Principal const& iPrincipal) const {}
113 
114  bool doBeginRun(RunPrincipal const& rp, EventSetup const& c,
115  ModuleCallingContext const* mcc);
116  bool doEndRun(RunPrincipal const& rp, EventSetup const& c,
117  ModuleCallingContext const* mcc);
118  bool doBeginLuminosityBlock(LuminosityBlockPrincipal const& lbp, EventSetup const& c,
119  ModuleCallingContext const* mcc);
120  bool doEndLuminosityBlock(LuminosityBlockPrincipal const& lbp, EventSetup const& c,
121  ModuleCallingContext const* mcc);
122 
123  void setEventSelectionInfo(std::map<std::string, std::vector<std::pair<std::string, int> > > const& outputModulePathPositions,
124  bool anyProductProduced);
125 
126  void configure(OutputModuleDescription const& desc);
127 
128  std::map<BranchID::value_type, BranchID::value_type> const& droppedBranchIDToKeptBranchID() {
129  return droppedBranchIDToKeptBranchID_;
130  }
131 
132  private:
133 
135  std::atomic<int> remainingEvents_;
136 
137  // TODO: Give OutputModule
138  // an interface (protected?) that supplies client code with the
139  // needed functionality *without* giving away implementation
140  // details ... don't just return a reference to keptProducts_, because
141  // we are looking to have the flexibility to change the
142  // implementation of keptProducts_ without modifying clients. When this
143  // change is made, we'll have a one-time-only task of modifying
144  // clients (classes derived from OutputModule) to use the
145  // newly-introduced interface.
146  // TODO: Consider using shared pointers here?
147 
148  // keptProducts_ are pointers to the BranchDescription objects describing
149  // the branches we are to write.
150  //
151  // We do not own the BranchDescriptions to which we point.
153  std::array<bool, NumBranchTypes> hasNewlyDroppedBranch_;
154 
159 
161  std::vector<detail::TriggerResultsBasedEventSelector> selectors_;
162  // ID of the ParameterSet that configured the event selector
163  // subsystem.
166 
167  // needed because of possible EDAliases.
168  // filled in only if key and value are different.
169  std::map<BranchID::value_type, BranchID::value_type> droppedBranchIDToKeptBranchID_;
172 
174  std::map<BranchID, bool> keepAssociation_;
175 
177 
178  //------------------------------------------------------------------
179  // private member functions
180  //------------------------------------------------------------------
181  void doWriteRun(RunPrincipal const& rp, ModuleCallingContext const* mcc);
182  void doWriteLuminosityBlock(LuminosityBlockPrincipal const& lbp, ModuleCallingContext const* mcc);
183  void doOpenFile(FileBlock const& fb);
184  void doRespondToOpenInputFile(FileBlock const& fb);
185  void doRespondToCloseInputFile(FileBlock const& fb);
186  void doPreForkReleaseResources();
187  void doPostForkReacquireResources(unsigned int iChildIndex, unsigned int iNumberOfChildren);
190 
191  std::string workerType() const {return "WorkerT<OutputModule>";}
192 
194  return resourceAcquirer_;
195  }
196 
198  void doCloseFile();
199 
202  void maybeOpenFile();
203 
204 
205  // Do the end-of-file tasks; this is only called internally, after
206  // the appropriate tests have been done.
207  virtual void reallyCloseFile();
208 
210 
211  bool prePrefetchSelection(StreamID id, EventPrincipal const&, ModuleCallingContext const*);
212 
214  virtual bool shouldWeCloseFile() const {return false;}
215 
216  virtual void write(EventForOutput const&) = 0;
217  virtual void beginJob(){}
218  virtual void endJob(){}
219  virtual void beginRun(RunForOutput const&){}
220  virtual void endRun(RunForOutput const&){}
221  virtual void writeRun(RunForOutput const&) = 0;
224  virtual void writeLuminosityBlock(LuminosityBlockForOutput const&) = 0;
225  virtual void openFile(FileBlock const&) {}
226  virtual void respondToOpenInputFile(FileBlock const&) {}
227  virtual void respondToCloseInputFile(FileBlock const&) {}
228  virtual void preForkReleaseResources() {}
229  virtual void postForkReacquireResources(unsigned int /*iChildIndex*/, unsigned int /*iNumberOfChildren*/) {}
230 
231  virtual bool isFileOpen() const { return true; }
232 
233  virtual void reallyOpenFile() {}
234 
235  void keepThisBranch(BranchDescription const& desc,
236  std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc,
237  std::set<BranchID>& keptProductsInEvent);
238 
240  moduleDescription_ = md;
241  }
242 
243  bool limitReached() const {return remainingEvents_ == 0;}
244  };
245 }
246 #endif
void setModuleDescription(ModuleDescription const &md)
Definition: OutputModule.h:239
std::array< bool, NumBranchTypes > hasNewlyDroppedBranch_
Definition: OutputModule.h:153
std::map< BranchID::value_type, BranchID::value_type > droppedBranchIDToKeptBranchID_
Definition: OutputModule.h:169
virtual void endRun(RunForOutput const &)
Definition: OutputModule.h:220
std::vector< BranchIDList > BranchIDLists
Definition: BranchIDList.h:19
ParameterSet selectEvents_
Definition: OutputModule.h:164
static const std::vector< std::string > & defaultSelectionStrings()
ModuleDescription moduleDescription_
Definition: OutputModule.h:158
std::map< BranchID, bool > keepAssociation_
Definition: OutputModule.h:174
std::array< SelectedProducts, NumBranchTypes > SelectedProductsForBranchType
SharedResourcesAcquirer & sharedResourcesAcquirer()
Definition: OutputModule.h:193
int remainingEvents() const
Definition: OutputModule.h:70
SharedResourcesAcquirer resourceAcquirer_
Definition: OutputModule.h:176
virtual void beginRun(RunForOutput const &)
Definition: OutputModule.h:219
edm::propagate_const< std::unique_ptr< BranchIDLists > > branchIDLists_
Definition: OutputModule.h:170
std::string const & processName() const
Definition: OutputModule.h:75
virtual void reallyOpenFile()
Definition: OutputModule.h:233
edm::propagate_const< std::unique_ptr< ThinnedAssociationsHelper > > thinnedAssociationsHelper_
Definition: OutputModule.h:173
BranchIDLists const * origBranchIDLists_
Definition: OutputModule.h:171
detail::TriggerResultsBasedEventSelector::handle_t Trig
Definition: OutputModule.h:49
SelectedProductsForBranchType const & keptProducts() const
Definition: OutputModule.h:76
bool wantAllEvents() const
Definition: OutputModule.h:84
std::string process_name_
Definition: OutputModule.h:155
void preActionBeforeRunEventAsync(WaitingTask *iTask, ModuleCallingContext const &iModuleCallingContext, Principal const &iPrincipal) const
Definition: OutputModule.h:112
std::map< BranchID::value_type, BranchID::value_type > const & droppedBranchIDToKeptBranchID()
Definition: OutputModule.h:128
virtual bool isFileOpen() const
Definition: OutputModule.h:231
virtual bool shouldWeCloseFile() const
Ask the OutputModule if we should end the current file.
Definition: OutputModule.h:214
virtual void postForkReacquireResources(unsigned int, unsigned int)
Definition: OutputModule.h:229
virtual void endLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:223
virtual void beginLuminosityBlock(LuminosityBlockForOutput const &)
Definition: OutputModule.h:222
int maxEvents() const
Definition: OutputModule.h:66
std::array< bool, NumBranchTypes > const & hasNewlyDroppedBranch() const
Definition: OutputModule.h:77
virtual void respondToOpenInputFile(FileBlock const &)
Definition: OutputModule.h:226
bool limitReached() const
Definition: OutputModule.h:243
OutputModule ModuleType
Definition: OutputModule.h:56
std::vector< detail::TriggerResultsBasedEventSelector > selectors_
Definition: OutputModule.h:161
SelectedProductsForBranchType keptProducts_
Definition: OutputModule.h:152
HLT enums.
ProductSelector productSelector_
Definition: OutputModule.h:157
virtual void endJob()
Definition: OutputModule.h:218
ProductSelectorRules productSelectorRules_
Definition: OutputModule.h:156
virtual void beginJob()
Definition: OutputModule.h:217
std::string workerType() const
Definition: OutputModule.h:191
virtual void respondToCloseInputFile(FileBlock const &)
Definition: OutputModule.h:227
virtual void openFile(FileBlock const &)
Definition: OutputModule.h:225
def write(self, setup)
void doRegisterThinnedAssociations(ProductRegistry const &, ThinnedAssociationsHelper &)
Definition: OutputModule.h:188
ModuleDescription const & moduleDescription() const
Definition: OutputModule.h:99
std::atomic< int > remainingEvents_
Definition: OutputModule.h:135
virtual void preForkReleaseResources()
Definition: OutputModule.h:228
ParameterSetID selectorConfig() const
Definition: OutputModule.h:102
void registerProductsAndCallbacks(OutputModule const *, ProductRegistry const *)
Definition: OutputModule.h:209
ParameterSetID selector_config_id_
Definition: OutputModule.h:165