CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
EventProcessor.h
Go to the documentation of this file.
1 #ifndef FWCore_Framework_EventProcessor_h
2 #define FWCore_Framework_EventProcessor_h
3 
4 /*----------------------------------------------------------------------
5 
6 EventProcessor: This defines the 'framework application' object. It is
7 configured in the user's main() function, and is set running.
8 
9 ----------------------------------------------------------------------*/
10 
12 
17 
19 
23 
24 #include "boost/shared_ptr.hpp"
25 #include "boost/thread/condition.hpp"
26 #include "boost/utility.hpp"
27 
28 #include <map>
29 #include <memory>
30 #include <set>
31 #include <string>
32 #include <vector>
33 
34 namespace statemachine {
35  class Machine;
36  class Run;
37 }
38 
39 namespace edm {
40 
41  class ActionTable;
42  class EDLooperBase;
43  class ProcessDesc;
44  class SubProcess;
45  namespace eventsetup {
46  class EventSetupProvider;
47  }
48 
49  namespace event_processor {
50  /*
51  Several of these state are likely to be transitory in
52  the offline because they are completly driven by the
53  data coming from the input source.
54  */
57 
62 
63  class StateSentry;
64  }
65 
66  class EventProcessor : public IEventProcessor, private boost::noncopyable {
67  public:
68 
69  // The input string 'config' contains the entire contents of a configuration file.
70  // Also allows the attachement of pre-existing services specified by 'token', and
71  // the specification of services by name only (defaultServices and forcedServices).
72  // 'defaultServices' are overridden by 'config'.
73  // 'forcedServices' override the 'config'.
74  explicit EventProcessor(std::string const& config,
75  ServiceToken const& token = ServiceToken(),
77  std::vector<std::string> const& defaultServices = std::vector<std::string>(),
78  std::vector<std::string> const& forcedServices = std::vector<std::string>());
79 
80  // Same as previous constructor, but without a 'token'. Token will be defaulted.
81 
82  EventProcessor(std::string const& config,
83  std::vector<std::string> const& defaultServices,
84  std::vector<std::string> const& forcedServices = std::vector<std::string>());
85 
86  EventProcessor(boost::shared_ptr<ProcessDesc>& processDesc,
87  ServiceToken const& token,
89 
91  EventProcessor(std::string const& config, bool isPython);
92 
94 
99  void beginJob();
100 
104  void endJob();
105 
109  char const* currentStateName() const;
110  char const* stateName(event_processor::State s) const;
111  char const* msgName(event_processor::Msg m) const;
113  void runAsync();
114  StatusCode statusAsync() const;
115 
116  // Concerning the async control functions:
117  // The event processor is left with the running thread.
118  // The async thread is stuck at this point and the process
119  // is likely not going to be able to continue.
120  // The reason for this timeout could be either an infinite loop
121  // or I/O blocking forever.
122  // The only thing to do is end the process.
123  // If you call endJob, you will likely get an exception from the
124  // state checks telling you that it is not valid to call this function.
125  // All these function force the event processor state into an
126  // error state.
127 
128  // tell the event loop to stop and wait for its completion
129  StatusCode stopAsync(unsigned int timeout_secs = 60 * 2);
130 
131  // tell the event loop to shutdown and wait for the completion
132  StatusCode shutdownAsync(unsigned int timeout_secs = 60 * 2);
133 
134  // wait until async event loop thread completes
135  // or timeout occurs (See StatusCode for return values)
136  StatusCode waitTillDoneAsync(unsigned int timeout_seconds = 0);
137 
138  // Both of these calls move the EP to the ready to run state but only
139  // the first actually sets the run number, the other one just stores
140  // the run number set externally in order to later compare to the one
141  // read from the input source for verification
143  void declareRunNumber(RunNumber_t runNumber);
144 
145  // -------------
146 
147  // These next two functions are deprecated. Please use
148  // RunToCompletion or RunEventCount instead. These will
149  // be deleted as soon as we have time to clean up the code
150  // in packages outside the Framework that uses them already.
151  StatusCode run(int numberEventsToProcess, bool repeatable = true);
152  StatusCode run();
153 
154  // Skip the specified number of events.
155  // If numberToSkip is negative, we will back up.
156  StatusCode skip(int numberToSkip);
157 
158  // Rewind to the first event
159  void rewind();
160 
163 
167 
168  std::vector<ModuleDescription const*>
169  getAllModuleDescriptions() const;
170 
174  int totalEvents() const;
175 
178  int totalEventsPassed() const;
179 
182  int totalEventsFailed() const;
183 
186  void enableEndPaths(bool active);
187 
190  bool endPathsEnabled() const;
191 
194  void getTriggerReport(TriggerReport& rep) const;
195 
197  void clearCounters();
198 
199  // Really should not be public,
200  // but the EventFilter needs it for now.
202 
207 
212 
213  //------------------------------------------------------------------
214  //
215  // Nested classes and structs below.
216 
217  // The function "runToCompletion" will run until the job is "complete",
218  // which means:
219  // 1 - no more input data
220  // 2 - input maxEvents parameter limit reached
221  // 3 - output maxEvents parameter limit reached
222  // 4 - input maxLuminosityBlocks parameter limit reached
223  // 5 - looper directs processing to end
224  // The function "runEventCount" will pause after processing the
225  // number of input events specified by the argument. One can
226  // call it again to resume processing at the same point. This
227  // function will also stop at the same point as "runToCompletion"
228  // if the job is complete before the requested number of events
229  // are processed. If the requested number of events is less than
230  // 1, "runEventCount" interprets this as infinity and does not
231  // pause until the job is complete.
232  //
233  // The return values from these functions are as follows:
234  // epSignal - processing terminated early, SIGUSR2 encountered
235  // epCountComplete - "runEventCount" processed the number of events
236  // requested by the argument
237  // epSuccess - all other cases
238  //
239  // We expect that in most cases, processes will call
240  // "runToCompletion" once per job and not use "runEventCount".
241  //
242  // If a process used "runEventCount", then it would need to
243  // check the value returned by "runEventCount" to determine
244  // if it processed the requested number of events. It would
245  // only make sense to call it again if it returned epCountComplete
246  // on the preceding call.
247 
248  // The online is an exceptional case. Online uses the DaqSource
249  // and the StreamerOutputModule, which are specially written to
250  // handle multiple calls of "runToCompletion" in the same job.
251  // The call to setRunNumber resets the DaqSource between those calls.
252  // With most sources and output modules, this does not work.
253  // If and only if called by the online, the argument to runToCompletion
254  // is set to true and this affects the state initial and final state
255  // transitions that are managed directly in EventProcessor.cc. (I am
256  // not sure if there is a reason for this or it is just a historical
257  // peculiarity that could be cleaned up and removed).
258 
259  virtual StatusCode runToCompletion(bool onlineStateTransitions);
260  virtual StatusCode runEventCount(int numberOfEventsToProcess);
261 
262  // The following functions are used by the code implementing our
263  // boost statemachine
264 
265  virtual void readFile();
266  virtual void closeInputFile();
267  virtual void openOutputFiles();
268  virtual void closeOutputFiles();
269 
270  virtual void respondToOpenInputFile();
271  virtual void respondToCloseInputFile();
272  virtual void respondToOpenOutputFiles();
273  virtual void respondToCloseOutputFiles();
274 
275  virtual void startingNewLoop();
276  virtual bool endOfLoop();
277  virtual void rewindInput();
278  virtual void prepareForNextLoop();
279  virtual bool shouldWeCloseOutput() const;
280 
281  virtual void doErrorStuff();
282 
283  virtual void beginRun(statemachine::Run const& run);
284  virtual void endRun(statemachine::Run const& run);
285 
286  virtual void beginLumi(ProcessHistoryID const& phid, int run, int lumi);
287  virtual void endLumi(ProcessHistoryID const& phid, int run, int lumi);
288 
290  virtual int readAndCacheLumi();
291  virtual void writeRun(statemachine::Run const& run);
292  virtual void deleteRunFromCache(statemachine::Run const& run);
293  virtual void writeLumi(ProcessHistoryID const& phid, int run, int lumi);
294  virtual void deleteLumiFromCache(ProcessHistoryID const& phid, int run, int lumi);
295 
296  virtual void readAndProcessEvent();
297  virtual bool shouldWeStop() const;
298 
299  virtual void setExceptionMessageFiles(std::string& message);
300  virtual void setExceptionMessageRuns(std::string& message);
301  virtual void setExceptionMessageLumis(std::string& message);
302 
303  virtual bool alreadyHandlingException() const;
304 
305  //returns 'true' if this was a child and we should continue processing
306  bool forkProcess(std::string const& jobReportFile);
307 
308  private:
309  //------------------------------------------------------------------
310  //
311  // Now private functions.
312  // init() is used by only by constructors
313  void init(boost::shared_ptr<ProcessDesc>& processDesc,
314  ServiceToken const& token,
316 
317  StatusCode runCommon(bool onlineStateTransitions, int numberOfEventsToProcess);
318  void terminateMachine();
319 
321 
322  StatusCode waitForAsyncCompletion(unsigned int timeout_seconds);
323 
324  void connectSigs(EventProcessor* ep);
325 
327  void errorState();
328  void setupSignal();
329 
330  static void asyncRun(EventProcessor*);
331 
332  bool hasSubProcess() const {
333  return subProcess_.get() != 0;
334  }
335 
336  //------------------------------------------------------------------
337  //
338  // Data members below.
339  // Are all these data members really needed? Some of them are used
340  // only during construction, and never again. If they aren't
341  // really needed, we should remove them.
342 
345  boost::shared_ptr<ActivityRegistry> actReg_;
346  boost::shared_ptr<SignallingProductRegistry> preg_;
348  boost::shared_ptr<InputSource> input_;
349  boost::shared_ptr<eventsetup::EventSetupProvider> esp_;
350  boost::shared_ptr<ActionTable const> act_table_;
351  boost::shared_ptr<ProcessConfiguration> processConfiguration_;
352  std::auto_ptr<Schedule> schedule_;
353  std::auto_ptr<SubProcess> subProcess_;
354 
356  boost::shared_ptr<boost::thread> event_loop_;
357 
360  boost::condition stopper_;
361  boost::condition starter_;
362  volatile int stop_count_;
363  volatile Status last_rc_;
364  std::string last_error_text_;
365  volatile bool id_set_;
366  volatile pthread_t event_loop_id_;
368  boost::shared_ptr<FileBlock> fb_;
369  boost::shared_ptr<EDLooperBase> looper_;
370 
371  std::auto_ptr<statemachine::Machine> machine_;
375  std::string fileMode_;
376  std::string emptyRunLumiMode_;
384 
388  typedef std::set<std::pair<std::string, std::string> > ExcludedData;
389  typedef std::map<std::string, ExcludedData> ExcludedDataMap;
392  }; // class EventProcessor
393 
394  //--------------------------------------------------------------------
395 
396  inline
399  return run(-1, false);
400  }
401 }
402 #endif
std::string emptyRunLumiMode_
unsigned int numberOfSequentialEventsPerChild_
volatile int stop_count_
boost::shared_ptr< SignallingProductRegistry > preg_
boost::shared_ptr< InputSource > input_
event_processor::State getState() const
virtual void beginLumi(ProcessHistoryID const &phid, int run, int lumi)
volatile bool id_set_
virtual void closeOutputFiles()
StatusCode skip(int numberToSkip)
static boost::mutex mutex
Definition: LHEProxy.cc:11
StatusCode waitForAsyncCompletion(unsigned int timeout_seconds)
boost::shared_ptr< boost::thread > event_loop_
ActivityRegistry::PostProcessEvent postProcessEventSignal_
boost::shared_ptr< EDLooperBase > looper_
virtual void setExceptionMessageFiles(std::string &message)
tuple lumi
Definition: fjr2json.py:41
void clearCounters()
Clears counters used by trigger report.
virtual void closeInputFile()
StatusCode doneAsync(event_processor::Msg m)
virtual StatusCode runToCompletion(bool onlineStateTransitions)
boost::shared_ptr< ActivityRegistry > actReg_
StatusCode shutdownAsync(unsigned int timeout_secs=60 *2)
virtual StatusCode runEventCount(int numberOfEventsToProcess)
boost::mutex state_lock_
void setRunNumber(RunNumber_t runNumber)
virtual void deleteRunFromCache(statemachine::Run const &run)
virtual void rewindInput()
std::set< std::pair< std::string, std::string > > ExcludedData
std::string exceptionMessageRuns_
char const * msgName(event_processor::Msg m) const
EventProcessor(std::string const &config, ServiceToken const &token=ServiceToken(), serviceregistry::ServiceLegacy=serviceregistry::kOverlapIsError, std::vector< std::string > const &defaultServices=std::vector< std::string >(), std::vector< std::string > const &forcedServices=std::vector< std::string >())
bool forkProcess(std::string const &jobReportFile)
virtual void beginRun(statemachine::Run const &run)
boost::shared_ptr< eventsetup::EventSetupProvider > esp_
StatusCode waitTillDoneAsync(unsigned int timeout_seconds=0)
virtual void readFile()
boost::condition stopper_
std::string last_error_text_
virtual int readAndCacheLumi()
ServiceToken serviceToken_
bool endPathsEnabled() const
boost::mutex stop_lock_
virtual bool alreadyHandlingException() const
std::string exceptionMessageLumis_
sigc::signal< void, Event const &, EventSetup const & > PostProcessEvent
virtual void setExceptionMessageLumis(std::string &message)
sigc::signal< void, EventID const &, Timestamp const & > PreProcessEvent
virtual void setExceptionMessageRuns(std::string &message)
ExcludedDataMap eventSetupDataToExcludeFromPrefetching_
void declareRunNumber(RunNumber_t runNumber)
virtual void readAndProcessEvent()
volatile event_processor::State state_
virtual void writeLumi(ProcessHistoryID const &phid, int run, int lumi)
boost::shared_ptr< ProcessConfiguration > processConfiguration_
void connectSigs(EventProcessor *ep)
static void asyncRun(EventProcessor *)
int totalEvents() const
std::auto_ptr< statemachine::Machine > machine_
virtual void endRun(statemachine::Run const &run)
std::auto_ptr< Schedule > schedule_
ActivityRegistry::PostProcessEvent & postProcessEventSignal()
char const * stateName(event_processor::State s) const
std::string exceptionMessageFiles_
StatusCode runCommon(bool onlineStateTransitions, int numberOfEventsToProcess)
char const * currentStateName() const
virtual void respondToCloseInputFile()
volatile pthread_t event_loop_id_
ActivityRegistry::PreProcessEvent & preProcessEventSignal()
volatile Status last_rc_
virtual void writeRun(statemachine::Run const &run)
void changeState(event_processor::Msg)
virtual statemachine::Run readAndCacheRun()
virtual void respondToOpenInputFile()
virtual bool shouldWeCloseOutput() const
virtual void doErrorStuff()
StatusCode statusAsync() const
virtual void respondToOpenOutputFiles()
std::map< std::string, ExcludedData > ExcludedDataMap
string message
Definition: argparse.py:126
ServiceToken getToken()
virtual void openOutputFiles()
void init(boost::shared_ptr< ProcessDesc > &processDesc, ServiceToken const &token, serviceregistry::ServiceLegacy)
boost::condition starter_
virtual void endLumi(ProcessHistoryID const &phid, int run, int lumi)
boost::shared_ptr< ActionTable const > act_table_
int totalEventsFailed() const
virtual bool endOfLoop()
virtual void respondToCloseOutputFiles()
virtual void prepareForNextLoop()
std::auto_ptr< SubProcess > subProcess_
virtual void startingNewLoop()
unsigned int RunNumber_t
Definition: EventRange.h:32
string s
Definition: asciidump.py:422
ActivityRegistry::PreProcessEvent preProcessEventSignal_
StatusCode stopAsync(unsigned int timeout_secs=60 *2)
virtual void deleteLumiFromCache(ProcessHistoryID const &phid, int run, int lumi)
std::vector< ModuleDescription const * > getAllModuleDescriptions() const
int totalEventsPassed() const
boost::shared_ptr< FileBlock > fb_
PrincipalCache principalCache_
bool hasSubProcess() const
virtual bool shouldWeStop() const
void enableEndPaths(bool active)
void getTriggerReport(TriggerReport &rep) const