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