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