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  namespace eventsetup {
45  class EventSetupProvider;
46  }
47 
48  namespace event_processor {
49  /*
50  Several of these state are likely to be transitory in
51  the offline because they are completly driven by the
52  data coming from the input source.
53  */
56 
61 
62  class StateSentry;
63  }
64 
65  class EventProcessor : public IEventProcessor, private boost::noncopyable {
66  public:
67 
68  // The input string 'config' contains the entire contents of a configuration file.
69  // Also allows the attachement of pre-existing services specified by 'token', and
70  // the specification of services by name only (defaultServices and forcedServices).
71  // 'defaultServices' are overridden by 'config'.
72  // 'forcedServices' cause an exception if the same service is specified in 'config'.
73  explicit EventProcessor(std::string const& config,
74  ServiceToken const& token = ServiceToken(),
76  std::vector<std::string> const& defaultServices = std::vector<std::string>(),
77  std::vector<std::string> const& forcedServices = std::vector<std::string>());
78 
79  // Same as previous constructor, but without a 'token'. Token will be defaulted.
80 
81  EventProcessor(std::string const& config,
82  std::vector<std::string> const& defaultServices,
83  std::vector<std::string> const& forcedServices = std::vector<std::string>());
84 
85  EventProcessor(boost::shared_ptr<ProcessDesc>& processDesc,
86  ServiceToken const& token,
88 
90  EventProcessor(std::string const& config, bool isPython);
91 
93 
98  void beginJob();
99 
103  void endJob();
104 
108  char const* currentStateName() const;
109  char const* stateName(event_processor::State s) const;
110  char const* msgName(event_processor::Msg m) const;
112  void runAsync();
113  StatusCode statusAsync() const;
114 
115  // Concerning the async control functions:
116  // The event processor is left with the running thread.
117  // The async thread is stuck at this point and the process
118  // is likely not going to be able to continue.
119  // The reason for this timeout could be either an infinite loop
120  // or I/O blocking forever.
121  // The only thing to do is end the process.
122  // If you call endJob, you will likely get an exception from the
123  // state checks telling you that it is not valid to call this function.
124  // All these function force the event processor state into an
125  // error state.
126 
127  // tell the event loop to stop and wait for its completion
128  StatusCode stopAsync(unsigned int timeout_secs = 60 * 2);
129 
130  // tell the event loop to shutdown and wait for the completion
131  StatusCode shutdownAsync(unsigned int timeout_secs = 60 * 2);
132 
133  // wait until async event loop thread completes
134  // or timeout occurs (See StatusCode for return values)
135  StatusCode waitTillDoneAsync(unsigned int timeout_seconds = 0);
136 
137  // Both of these calls move the EP to the ready to run state but only
138  // the first actually sets the run number, the other one just stores
139  // the run number set externally in order to later compare to the one
140  // read from the input source for verification
142  void declareRunNumber(RunNumber_t runNumber);
143 
144  // -------------
145 
146  // These next two functions are deprecated. Please use
147  // RunToCompletion or RunEventCount instead. These will
148  // be deleted as soon as we have time to clean up the code
149  // in packages outside the Framework that uses them already.
150  StatusCode run(int numberEventsToProcess, bool repeatable = true);
151  StatusCode run();
152 
153  // Skip the specified number of events.
154  // If numberToSkip is negative, we will back up.
155  StatusCode skip(int numberToSkip);
156 
157  // Rewind to the first event
158  void rewind();
159 
162 
166 
167  std::vector<ModuleDescription const*>
168  getAllModuleDescriptions() const;
169 
173  int totalEvents() const;
174 
177  int totalEventsPassed() const;
178 
181  int totalEventsFailed() const;
182 
185  void enableEndPaths(bool active);
186 
189  bool endPathsEnabled() const;
190 
193  void getTriggerReport(TriggerReport& rep) const;
194 
196  void clearCounters();
197 
198  // Really should not be public,
199  // but the EventFilter needs it for now.
201 
206 
211 
212  //------------------------------------------------------------------
213  //
214  // Nested classes and structs below.
215 
216  // The function "runToCompletion" will run until the job is "complete",
217  // which means:
218  // 1 - no more input data
219  // 2 - input maxEvents parameter limit reached
220  // 3 - output maxEvents parameter limit reached
221  // 4 - input maxLuminosityBlocks parameter limit reached
222  // 5 - looper directs processing to end
223  // The function "runEventCount" will pause after processing the
224  // number of input events specified by the argument. One can
225  // call it again to resume processing at the same point. This
226  // function will also stop at the same point as "runToCompletion"
227  // if the job is complete before the requested number of events
228  // are processed. If the requested number of events is less than
229  // 1, "runEventCount" interprets this as infinity and does not
230  // pause until the job is complete.
231  //
232  // The return values from these functions are as follows:
233  // epSignal - processing terminated early, SIGUSR2 encountered
234  // epCountComplete - "runEventCount" processed the number of events
235  // requested by the argument
236  // epSuccess - all other cases
237  //
238  // We expect that in most cases, processes will call
239  // "runToCompletion" once per job and not use "runEventCount".
240  //
241  // If a process used "runEventCount", then it would need to
242  // check the value returned by "runEventCount" to determine
243  // if it processed the requested number of events. It would
244  // only make sense to call it again if it returned epCountComplete
245  // on the preceding call.
246 
247  // The online is an exceptional case. Online uses the DaqSource
248  // and the StreamerOutputModule, which are specially written to
249  // handle multiple calls of "runToCompletion" in the same job.
250  // The call to setRunNumber resets the DaqSource between those calls.
251  // With most sources and output modules, this does not work.
252  // If and only if called by the online, the argument to runToCompletion
253  // is set to true and this affects the state initial and final state
254  // transitions that are managed directly in EventProcessor.cc. (I am
255  // not sure if there is a reason for this or it is just a historical
256  // peculiarity that could be cleaned up and removed).
257 
258  virtual StatusCode runToCompletion(bool onlineStateTransitions);
259  virtual StatusCode runEventCount(int numberOfEventsToProcess);
260 
261  // The following functions are used by the code implementing our
262  // boost statemachine
263 
264  virtual void readFile();
265  virtual void closeInputFile();
266  virtual void openOutputFiles();
267  virtual void closeOutputFiles();
268 
269  virtual void respondToOpenInputFile();
270  virtual void respondToCloseInputFile();
271  virtual void respondToOpenOutputFiles();
272  virtual void respondToCloseOutputFiles();
273 
274  virtual void startingNewLoop();
275  virtual bool endOfLoop();
276  virtual void rewindInput();
277  virtual void prepareForNextLoop();
278  virtual bool shouldWeCloseOutput() const;
279 
280  virtual void doErrorStuff();
281 
282  virtual void beginRun(statemachine::Run const& run);
283  virtual void endRun(statemachine::Run const& run);
284 
285  virtual void beginLumi(ProcessHistoryID const& phid, int run, int lumi);
286  virtual void endLumi(ProcessHistoryID const& phid, int run, int lumi);
287 
289  virtual int readAndCacheLumi();
290  virtual void writeRun(statemachine::Run const& run);
291  virtual void deleteRunFromCache(statemachine::Run const& run);
292  virtual void writeLumi(ProcessHistoryID const& phid, int run, int lumi);
293  virtual void deleteLumiFromCache(ProcessHistoryID const& phid, int run, int lumi);
294 
295  virtual void readAndProcessEvent();
296  virtual bool shouldWeStop() const;
297 
298  virtual void setExceptionMessageFiles(std::string& message);
299  virtual void setExceptionMessageRuns(std::string& message);
300  virtual void setExceptionMessageLumis(std::string& message);
301 
302  virtual bool alreadyHandlingException() const;
303 
304  //returns 'true' if this was a child and we should continue processing
305  bool forkProcess(std::string const& jobReportFile);
306 
307  private:
308  //------------------------------------------------------------------
309  //
310  // Now private functions.
311  // init() is used by only by constructors
312  void init(boost::shared_ptr<ProcessDesc>& processDesc,
313  ServiceToken const& token,
315 
316  StatusCode runCommon(bool onlineStateTransitions, int numberOfEventsToProcess);
317  void terminateMachine();
318 
320 
321  StatusCode waitForAsyncCompletion(unsigned int timeout_seconds);
322 
323  void connectSigs(EventProcessor* ep);
324 
326  void errorState();
327  void setupSignal();
328 
329  static void asyncRun(EventProcessor*);
330 
331  //------------------------------------------------------------------
332  //
333  // Data members below.
334  // Are all these data members really needed? Some of them are used
335  // only during construction, and never again. If they aren't
336  // really needed, we should remove them.
337 
340  boost::shared_ptr<ActivityRegistry> actReg_;
341  boost::shared_ptr<SignallingProductRegistry> preg_;
343  boost::shared_ptr<InputSource> input_;
344  boost::shared_ptr<eventsetup::EventSetupProvider> esp_;
345  boost::shared_ptr<ActionTable const> act_table_;
346  boost::shared_ptr<ProcessConfiguration> processConfiguration_;
347  std::auto_ptr<Schedule> schedule_;
348 
350  boost::shared_ptr<boost::thread> event_loop_;
351 
354  boost::condition stopper_;
355  boost::condition starter_;
356  volatile int stop_count_;
357  volatile Status last_rc_;
358  std::string last_error_text_;
359  volatile bool id_set_;
360  volatile pthread_t event_loop_id_;
362  boost::shared_ptr<FileBlock> fb_;
363  boost::shared_ptr<EDLooperBase> looper_;
364 
365  std::auto_ptr<statemachine::Machine> machine_;
369  std::string fileMode_;
370  std::string emptyRunLumiMode_;
378 
382  typedef std::set<std::pair<std::string, std::string> > ExcludedData;
383  typedef std::map<std::string, ExcludedData> ExcludedDataMap;
386  }; // class EventProcessor
387 
388  //--------------------------------------------------------------------
389 
390  inline
393  return run(-1, false);
394  }
395 }
396 #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_
tuple config
Definition: cmsDriver.py:17
int totalEventsFailed() const
virtual bool endOfLoop()
virtual void respondToCloseOutputFiles()
virtual void prepareForNextLoop()
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_
virtual bool shouldWeStop() const
void enableEndPaths(bool active)
void getTriggerReport(TriggerReport &rep) const