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