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