CMS 3D CMS Logo

EPStates.cc
Go to the documentation of this file.
1 
4 
5 #include <cassert>
6 #include <exception>
7 #include <sstream>
8 #include <string>
9 
10 namespace statemachine {
11  namespace {
12  edm::RunNumber_t const INVALID_RUN_NUMBER = 0;
13  edm::LuminosityBlockNumber_t const INVALID_LUMI = 0;
14  Run const INVALID_RUN(edm::ProcessHistoryID(), INVALID_RUN_NUMBER);
15  HandleLumis::LumiID const InvalidLumiID = HandleLumis::LumiID(edm::ProcessHistoryID(), INVALID_RUN_NUMBER, INVALID_LUMI);
16  }
17 
19  processHistoryID_(phid),
20  runNumber_(runNumber) {
21  }
22 
24 
26  FileMode fileMode,
27  EmptyRunLumiMode emptyRunLumiMode) :
28  ep_(ep),
29  fileMode_(fileMode),
30  emptyRunLumiMode_(emptyRunLumiMode) {
31  }
32 
33  edm::IEventProcessor const& Machine::ep() const { return *ep_; }
35  FileMode Machine::fileMode() const { return fileMode_; }
37 
39  ep_->startingNewLoop();
40  }
41 
43  if(ep_->alreadyHandlingException()) return;
44  ep_->startingNewLoop();
45  }
46 
48  ep_->prepareForNextLoop();
49  ep_->rewindInput();
50  }
51 
52  Starting::Starting(my_context ctx) : my_base(ctx) { }
53 
55 
56  HandleFiles::HandleFiles(my_context ctx) :
57  my_base(ctx),
58  ep_(context<Machine>().ep()),
59  exitCalled_(false) { }
60 
62  if(ep_.alreadyHandlingException()) return;
63  exitCalled_ = true;
64  closeFiles(false);
65  }
66 
68  if(!exitCalled_) {
69  try {
70  closeFiles(true);
71  }
72  catch(...) {
73  std::string message("Another exception was caught while trying to clean up files after the primary fatal exception.");
75  }
76  }
77  }
78 
79  void HandleFiles::closeFiles(bool cleaningUpAfterException) {
81  ep_.closeInputFile(cleaningUpAfterException);
83  }
84 
87  ep_.closeInputFile(false);
88 
89  ep_.readFile();
91  }
92 
94  if(context<Machine>().fileMode() == NOMERGE) return true;
95  return ep_.shouldWeCloseOutput();
96  }
97 
98  EndingLoop::EndingLoop(my_context ctx) :
99  my_base(ctx),
100  ep_(context<Machine>().ep()) {
101  if(ep_.alreadyHandlingException() || ep_.endOfLoop()) post_event(Stop());
102  else post_event(Restart());
103  }
104 
106 
108  return terminate();
109  }
110 
111  Error::Error(my_context ctx) :
112  my_base(ctx),
113  ep_(context<Machine>().ep()) {
114  post_event(Stop());
115  ep_.doErrorStuff();
116  }
117 
119 
120  class HandleNewInputFile1;
122 
123  FirstFile::FirstFile(my_context ctx) :
124  my_base(ctx),
125  ep_(context<Machine>().ep()) {
126  openFiles();
127  }
128 
130 
132  if(context<HandleFiles>().shouldWeCloseOutput()) {
133  return transit<NewInputAndOutputFiles>();
134  } else {
135  return transit<HandleNewInputFile1>();
136  }
137  }
138 
140  ep_.readFile();
142 
144  }
145 
147  my_base(ctx) {
148  context<HandleFiles>().goToNewInputFile();
149  }
150 
152 
154  if(context<HandleFiles>().shouldWeCloseOutput()) {
155  return transit<NewInputAndOutputFiles>();
156  } else {
157  return transit<HandleNewInputFile1>();
158  }
159  }
160 
162  my_base(ctx),
163  ep_(context<Machine>().ep()) {
165  }
166 
168 
170  if(context<HandleFiles>().shouldWeCloseOutput()) {
171  return transit<NewInputAndOutputFiles>();
172  } else {
173  return transit<HandleNewInputFile1>();
174  }
175  }
176 
179  ep_.closeInputFile(false);
180 
182 
183  ep_.readFile();
185 
187  }
188 
189  HandleRuns::HandleRuns(my_context ctx) :
190  my_base(ctx),
191  ep_(context<Machine>().ep()),
192  exitCalled_(false),
193  beginRunCalled_(false),
194  currentRun_(INVALID_RUN),
195  runException_(false) { }
196 
198  if(ep_.alreadyHandlingException()) return;
199  exitCalled_ = true;
200  finalizeRun(false);
201  }
202 
204  if(!exitCalled_) {
205  try {
206  finalizeRun(true);
207  }
208  catch(...) {
209  std::string message("Another exception was caught while trying to clean up runs after the primary fatal exception.");
210  ep_.setExceptionMessageRuns(message);
211  }
212  }
213  }
214 
216  Run const& HandleRuns::currentRun() const { return currentRun_; }
217  bool HandleRuns::runException() const { return runException_; }
218 
220 
221  runException_ = true;
222  currentRun_ = ep_.readRun();
223  runException_ = false;
224 
225  if(context<Machine>().emptyRunLumiMode() != doNotHandleEmptyRunsAndLumis) {
226  beginRun(currentRun());
227  }
228  }
229 
230  void HandleRuns::beginRun(Run const& run) {
231  beginRunCalled_ = true;
232 
233  runException_ = true;
234  ep_.beginRun(run);
235  runException_ = false;
236  }
237 
238  void HandleRuns::endRun(Run const& run, bool cleaningUpAfterException) {
239  beginRunCalled_ = false;
240 
241  runException_ = true;
242  ep_.endRun(run, cleaningUpAfterException);
243  runException_ = false;
244  }
245 
247  finalizeRun(false);
248  }
249 
250  void HandleRuns::finalizeRun(bool cleaningUpAfterException) {
251 
252  if(runException_) return;
253  runException_ = true;
254 
255  if(beginRunCalled_) endRun(currentRun(), cleaningUpAfterException);
258  currentRun_ = INVALID_RUN;
259  runException_ = false;
260  }
261 
264  }
265 
266  NewRun::NewRun(my_context ctx) :
267  my_base(ctx) {
268  assert(context<HandleRuns>().currentRun() == INVALID_RUN);
269  context<HandleRuns>().setupCurrentRun();
270 
271  // Here we assume that the input source or event processor
272  // will throw if we fail to get a valid run. Therefore
273  // we should not ever fail this assert.
274  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
275  }
276 
278 
280  if(run == context<HandleRuns>().currentRun()) {
281  return transit<ContinueRun1>();
282  }
283  context<HandleRuns>().finalizeRun(false);
284  return transit<NewRun>();
285  }
286 
288  if(!context<HandleFiles>().shouldWeCloseOutput()) {
289  return transit<HandleNewInputFile2>();
290  }
291  return forward_event();
292  }
293 
295  my_base(ctx) {
296  context<HandleFiles>().goToNewInputFile();
297  checkInvariant();
298  }
299 
301  checkInvariant();
302  }
303 
305  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
306  return true;
307  }
308 
310  checkInvariant();
311 
312  if(context<HandleRuns>().currentRun() != run) {
313  return transit<NewRun, HandleRuns, Run>(&HandleRuns::finalizeRun, run);
314  } else {
315  return transit<ContinueRun1>();
316  }
317  }
318 
320  checkInvariant();
321  if(!context<HandleFiles>().shouldWeCloseOutput()) {
322  return transit<HandleNewInputFile2>();
323  }
324  return forward_event();
325  }
326 
327  ContinueRun1::ContinueRun1(my_context ctx) :
328  my_base(ctx),
329  ep_(context<Machine>().ep()) {
331  checkInvariant();
332  }
333 
335  checkInvariant();
336  }
337 
339  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
340  return true;
341  }
342 
344  checkInvariant();
345  if(context<HandleRuns>().currentRun() != run) {
346  return transit<NewRun, HandleRuns, Run>(&HandleRuns::finalizeRun, run);
347  } else {
348  return transit<ContinueRun1>();
349  }
350  }
351 
353  checkInvariant();
354  if(!context<HandleFiles>().shouldWeCloseOutput()) {
355  return transit<HandleNewInputFile2>();
356  }
357  return forward_event();
358  }
359 
361  processHistoryID_(phid),
362  run_(run),
363  lumi_(lumi) {
364  }
365 
366  HandleLumis::HandleLumis(my_context ctx) :
367  my_base(ctx),
368  ep_(context<Machine>().ep()),
371  currentLumi_(InvalidLumiID),
373  checkInvariant();
374  }
375 
377  if(ep_.alreadyHandlingException()) return;
378  exitCalled_ = true;
379  checkInvariant();
380  if(!lumiException_ && !context<HandleRuns>().runException()) {
381  finalizeLumi(false);
382  }
383  }
384 
386  if(!exitCalled_) {
387  try {
388  checkInvariant();
389  if(!lumiException_ && !context<HandleRuns>().runException()) {
390  finalizeLumi(true);
391  }
392  }
393  catch(...) {
394  std::string message("Another exception was caught while trying to clean up lumis after the primary fatal exception.");
395  ep_.setExceptionMessageLumis(message);
396  }
397  }
398  }
399 
401  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
402  return true;
403  }
404 
406 
408 
410 
411  Run const& run = context<HandleRuns>().currentRun();
412  assert(run != INVALID_RUN);
413  lumiException_ = true;
415 
416  if(context<Machine>().emptyRunLumiMode() == handleEmptyRunsAndLumis) {
417  assert(context<HandleRuns>().beginRunCalled());
418  ep_.beginLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
419  }
420 
421  lumiException_ = false;
422 
423  currentLumiEmpty_ = true;
424  }
425 
426  void HandleLumis::finalizeLumi(bool cleaningUpAfterException) {
427 
428  lumiException_ = true;
429 
430  if(!currentLumiEmpty_ ||
431  context<Machine>().emptyRunLumiMode() == handleEmptyRunsAndLumis) {
432  ep_.endLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi(), cleaningUpAfterException);
433  }
434 
435  ep_.writeLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
436  ep_.deleteLumiFromCache(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
437  currentLumi_ = InvalidLumiID;
438 
439  lumiException_ = false;
440  }
441 
443  if(currentLumiEmpty_) {
444 
445  if(context<Machine>().emptyRunLumiMode() != handleEmptyRunsAndLumis) {
446  lumiException_ = true;
447  ep_.beginLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
448  lumiException_ = false;
449  }
450  currentLumiEmpty_ = false;
451  }
452  }
453 
454  FirstLumi::FirstLumi(my_context ctx) :
455  my_base(ctx) {
456  context<HandleLumis>().setupCurrentLumi();
457  checkInvariant();
458  }
459 
461  checkInvariant();
462  }
463 
465  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
466  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
467  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
468  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
469  assert(context<HandleLumis>().currentLumiEmpty() == true);
470  return true;
471  }
472 
474  if(lumi.id() == context<HandleLumis>().currentLumi().lumi()) {
475  return transit<ContinueLumi>();
476  }
477  return transit<AnotherLumi>();
478  }
479 
481  checkInvariant();
482  if(!context<HandleFiles>().shouldWeCloseOutput()) {
483  return transit<HandleNewInputFile3>();
484  }
485  return forward_event();
486  }
487 
488  AnotherLumi::AnotherLumi(my_context ctx) :
489  my_base(ctx) {
490  context<HandleLumis>().finalizeLumi(false);
491  context<HandleLumis>().setupCurrentLumi();
492  checkInvariant();
493  }
494 
496  checkInvariant();
497  }
498 
500  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
501  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
502  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
503  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
504  assert(context<HandleLumis>().currentLumiEmpty() == true);
505  return true;
506  }
507 
509  if(lumi.id() == context<HandleLumis>().currentLumi().lumi()) {
510  return transit<ContinueLumi>();
511  }
512  return transit<AnotherLumi>();
513  }
514 
516  checkInvariant();
517  if(!context<HandleFiles>().shouldWeCloseOutput()) {
518  return transit<HandleNewInputFile3>();
519  }
520  return forward_event();
521  }
522 
523  HandleEvent::HandleEvent(my_context ctx) :
524  my_base(ctx),
525  ep_(context<Machine>().ep()) {
527  checkInvariant();
528  }
529 
531  checkInvariant();
532  }
533 
535  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
536  assert(context<HandleRuns>().beginRunCalled());
537  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
538  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
539  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
540  assert(context<HandleLumis>().currentLumiEmpty() == false);
541  return true;
542  }
543 
545  checkInvariant();
546  if(!context<HandleFiles>().shouldWeCloseOutput()) {
547  return transit<HandleNewInputFile3>();
548  }
549  return forward_event();
550  }
551 
553  markNonEmpty();
555  if(ep_.shouldWeStop()) post_event(Stop());
556  }
557 
559  context<HandleRuns>().beginRunIfNotDoneAlready();
560  context<HandleLumis>().markLumiNonEmpty();
561  }
562 
563 
565  my_base(ctx) {
566  context<HandleFiles>().goToNewInputFile();
567  checkInvariant();
568  }
569 
571  checkInvariant();
572  }
573 
575  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
576  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
577  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
578  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
579  return true;
580  }
581 
583  checkInvariant();
584 
585  if(context<HandleRuns>().currentRun() == run) {
586  return transit<ContinueRun2>();
587  }
588  return forward_event();
589  }
590 
592  checkInvariant();
593  if(!context<HandleFiles>().shouldWeCloseOutput()) {
594  return transit<HandleNewInputFile3>();
595  }
596  return forward_event();
597  }
598 
599  ContinueRun2::ContinueRun2(my_context ctx) :
600  my_base(ctx),
601  ep_(context<Machine>().ep()) {
603  checkInvariant();
604  }
605 
607  checkInvariant();
608  }
609 
611  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
612  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
613  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
614  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
615  return true;
616  }
617 
619  checkInvariant();
620  if(context<HandleRuns>().currentRun() != run) {
621  return forward_event();
622  } else {
623  return transit<ContinueRun2>();
624  }
625  }
626 
628  checkInvariant();
629 
630  if(context<HandleLumis>().currentLumi().lumi() != lumi.id()) {
631  return transit<AnotherLumi>();
632  } else {
633  return transit<ContinueLumi>();
634  }
635  }
636 
638  checkInvariant();
639  if(!context<HandleFiles>().shouldWeCloseOutput()) {
640  return transit<HandleNewInputFile3>();
641  }
642  return forward_event();
643  }
644 
645  ContinueLumi::ContinueLumi(my_context ctx) :
646  my_base(ctx),
647  ep_(context<Machine>().ep()) {
649  checkInvariant();
650  }
651 
653  checkInvariant();
654  }
655 
657  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
658  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
659  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
660  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
661  return true;
662  }
663 
665  checkInvariant();
666  if(context<HandleLumis>().currentLumi().lumi() != lumi.id()) {
667  return transit<AnotherLumi>();
668  } else {
669  return transit<ContinueLumi>();
670  }
671  }
672 
674  checkInvariant();
675  if(!context<HandleFiles>().shouldWeCloseOutput()) {
676  return transit<HandleNewInputFile3>();
677  }
678  return forward_event();
679  }
680 }
static const char runNumber_[]
HandleEvent(my_context ctx)
Definition: EPStates.cc:523
edm::IEventProcessor const & ep() const
Definition: EPStates.cc:33
sc::result react(File const &file)
Definition: EPStates.cc:131
edm::IEventProcessor & ep_
Definition: EPStates.h:345
FirstFile(my_context ctx)
Definition: EPStates.cc:123
virtual void endRun(statemachine::Run const &run, bool cleaningUpAfterException)=0
void endRun(Run const &run, bool cleaningUpAfterException)
Definition: EPStates.cc:238
HandleFiles(my_context ctx)
Definition: EPStates.cc:56
Starting(my_context ctx)
Definition: EPStates.cc:52
HandleLumis(my_context ctx)
Definition: EPStates.cc:366
virtual void setExceptionMessageFiles(std::string &message)=0
virtual bool shouldWeStop() const =0
HandleNewInputFile3(my_context ctx)
Definition: EPStates.cc:564
edm::IEventProcessor & ep_
Definition: EPStates.h:181
bool currentLumiEmpty() const
Definition: EPStates.cc:407
edm::IEventProcessor & ep_
Definition: EPStates.h:455
virtual void beginLumi(ProcessHistoryID const &phid, RunNumber_t run, LuminosityBlockNumber_t lumi)=0
FileMode fileMode() const
Definition: EPStates.cc:35
edm::IEventProcessor & ep_
Definition: EPStates.h:437
sc::result react(File const &file)
Definition: EPStates.cc:544
sc::result react(File const &file)
Definition: EPStates.cc:169
sc::result react(Lumi const &lumi)
Definition: EPStates.cc:664
ContinueLumi(my_context ctx)
Definition: EPStates.cc:645
void closeFiles(bool cleaningUpAfterException)
Definition: EPStates.cc:79
edm::IEventProcessor & ep_
Definition: EPStates.h:156
virtual void writeLumi(ProcessHistoryID const &phid, RunNumber_t run, LuminosityBlockNumber_t lumi)=0
virtual void openOutputFiles()=0
virtual void readAndProcessEvent()=0
sc::result react(Run const &run)
Definition: EPStates.cc:309
unsigned int LuminosityBlockNumber_t
virtual void setExceptionMessageLumis(std::string &message)=0
virtual int readLuminosityBlock()=0
edm::IEventProcessor & ep_
Definition: EPStates.h:311
void startingNewLoop(File const &file)
Definition: EPStates.cc:38
FirstLumi(my_context ctx)
Definition: EPStates.cc:454
EmptyRunLumiMode emptyRunLumiMode_
Definition: EPStates.h:113
virtual statemachine::Run readAndMergeRun()=0
edm::LuminosityBlockNumber_t id() const
Definition: EPStates.h:70
virtual void readFile()=0
bool beginRunCalled() const
Definition: EPStates.cc:215
HandleRuns(my_context ctx)
Definition: EPStates.cc:189
edm::IEventProcessor & ep_
Definition: EPStates.h:255
EndingLoop(my_context ctx)
Definition: EPStates.cc:98
virtual void endLumi(ProcessHistoryID const &phid, RunNumber_t run, LuminosityBlockNumber_t lumi, bool cleaningUpAfterException)=0
virtual void deleteRunFromCache(statemachine::Run const &run)=0
ContinueRun2(my_context ctx)
Definition: EPStates.cc:599
LumiID const & currentLumi() const
Definition: EPStates.cc:405
HandleNewInputFile1(my_context ctx)
Definition: EPStates.cc:146
edm::IEventProcessor & ep_
Definition: EPStates.h:231
virtual bool shouldWeCloseOutput() const =0
sc::result react(Run const &run)
Definition: EPStates.cc:343
virtual void closeOutputFiles()=0
Run const & currentRun() const
Definition: EPStates.cc:216
ContinueRun1(my_context ctx)
Definition: EPStates.cc:327
edm::IEventProcessor & ep_
Definition: EPStates.h:403
edm::IEventProcessor & ep_
Definition: EPStates.h:199
AnotherLumi(my_context ctx)
Definition: EPStates.cc:488
sc::result react(Lumi const &lumi)
Definition: EPStates.cc:508
virtual void respondToOpenInputFile()=0
virtual void respondToCloseInputFile()=0
void finalizeLumi(bool cleaningUpAfterException)
Definition: EPStates.cc:426
void finalizeRun(Run const &)
Definition: EPStates.cc:246
NewInputAndOutputFiles(my_context ctx)
Definition: EPStates.cc:161
Machine(edm::IEventProcessor *ep, FileMode fileMode, EmptyRunLumiMode emptyRunLumiMode)
Definition: EPStates.cc:25
void rewindAndPrepareForNextLoop(Restart const &restart)
Definition: EPStates.cc:47
NewRun(my_context ctx)
Definition: EPStates.cc:266
sc::result react(Run const &run)
Definition: EPStates.cc:582
Run(edm::ProcessHistoryID const &phid, edm::RunNumber_t runNumber)
Definition: EPStates.cc:18
sc::result react(Lumi const &lumi)
Definition: EPStates.cc:473
virtual bool alreadyHandlingException() const =0
sc::result react(Stop const &)
Definition: EPStates.cc:107
virtual statemachine::Run readRun()=0
HandleNewInputFile2(my_context ctx)
Definition: EPStates.cc:294
edm::IEventProcessor & ep_
Definition: EPStates.h:171
virtual void setExceptionMessageRuns(std::string &message)=0
virtual int readAndMergeLumi()=0
edm::RunNumber_t runNumber() const
Definition: EPStates.h:50
void beginRun(Run const &run)
Definition: EPStates.cc:230
Lumi(edm::LuminosityBlockNumber_t id)
Definition: EPStates.cc:23
virtual bool endOfLoop()=0
edm::ProcessHistoryID const & processHistoryID() const
Definition: EPStates.h:49
virtual void closeInputFile(bool cleaningUpAfterException)=0
Error(my_context ctx)
Definition: EPStates.cc:111
unsigned int RunNumber_t
sc::result react(Run const &run)
Definition: EPStates.cc:279
virtual void doErrorStuff()=0
LumiID(edm::ProcessHistoryID const &phid, edm::RunNumber_t run, edm::LuminosityBlockNumber_t lumi)
Definition: EPStates.cc:360
virtual void beginRun(statemachine::Run const &run)=0
sc::result react(Run const &run)
Definition: EPStates.cc:618
EmptyRunLumiMode emptyRunLumiMode() const
Definition: EPStates.cc:36
edm::propagate_const< edm::IEventProcessor * > ep_
Definition: EPStates.h:111
sc::result react(File const &file)
Definition: EPStates.cc:153
bool runException() const
Definition: EPStates.cc:217
virtual void deleteLumiFromCache(ProcessHistoryID const &phid, RunNumber_t run, LuminosityBlockNumber_t lumi)=0
virtual void writeRun(statemachine::Run const &run)=0