CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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  int const INVALID_RUN_NUMBER = 0;
13  int 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 
23  Lumi::Lumi(int id) : id_(id) {}
24 
27  EmptyRunLumiMode emptyRunLumiMode) :
28  ep_(ep),
29  fileMode_(fileMode),
30  emptyRunLumiMode_(emptyRunLumiMode) {
31  }
32 
33  edm::IEventProcessor& Machine::ep() const { return *ep_; }
34  FileMode Machine::fileMode() const { return fileMode_; }
36 
39  }
40 
42  if(ep_->alreadyHandlingException()) return;
44  }
45 
48  ep_->rewindInput();
49  }
50 
51  Starting::Starting(my_context ctx) : my_base(ctx) { }
52 
54 
55  HandleFiles::HandleFiles(my_context ctx) :
56  my_base(ctx),
57  ep_(context<Machine>().ep()),
58  exitCalled_(false) { }
59 
61  if(ep_.alreadyHandlingException()) return;
62  exitCalled_ = true;
63  closeFiles(false);
64  }
65 
67  if(!exitCalled_) {
68  try {
69  closeFiles(true);
70  }
71  catch(...) {
72  std::string message("Another exception was caught while trying to clean up files after the primary fatal exception.");
74  }
75  }
76  }
77 
78  void HandleFiles::closeFiles(bool cleaningUpAfterException) {
80  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 
145  }
146 
148  my_base(ctx) {
149  context<HandleFiles>().goToNewInputFile();
150  }
151 
153 
155  if(context<HandleFiles>().shouldWeCloseOutput()) {
156  return transit<NewInputAndOutputFiles>();
157  } else {
158  return transit<HandleNewInputFile1>();
159  }
160  }
161 
163  my_base(ctx),
164  ep_(context<Machine>().ep()) {
166  }
167 
169 
171  if(context<HandleFiles>().shouldWeCloseOutput()) {
172  return transit<NewInputAndOutputFiles>();
173  } else {
174  return transit<HandleNewInputFile1>();
175  }
176  }
177 
180  ep_.closeInputFile(false);
181 
184 
185  ep_.readFile();
187 
190  }
191 
192  HandleRuns::HandleRuns(my_context ctx) :
193  my_base(ctx),
194  ep_(context<Machine>().ep()),
195  exitCalled_(false),
196  beginRunCalled_(false),
197  currentRun_(INVALID_RUN),
198  runException_(false) { }
199 
201  if(ep_.alreadyHandlingException()) return;
202  exitCalled_ = true;
203  finalizeRun(false);
204  }
205 
207  if(!exitCalled_) {
208  try {
209  finalizeRun(true);
210  }
211  catch(...) {
212  std::string message("Another exception was caught while trying to clean up runs after the primary fatal exception.");
213  ep_.setExceptionMessageRuns(message);
214  }
215  }
216  }
217 
219  Run const& HandleRuns::currentRun() const { return currentRun_; }
220  bool HandleRuns::runException() const { return runException_; }
221 
223 
224  runException_ = true;
226  runException_ = false;
227 
228  if(context<Machine>().emptyRunLumiMode() != doNotHandleEmptyRunsAndLumis) {
229  beginRun(currentRun());
230  }
231  }
232 
233  void HandleRuns::beginRun(Run const& run) {
234  beginRunCalled_ = true;
235 
236  runException_ = true;
237  ep_.beginRun(run);
238  runException_ = false;
239  }
240 
241  void HandleRuns::endRun(Run const& run, bool cleaningUpAfterException) {
242  beginRunCalled_ = false;
243 
244  runException_ = true;
245  ep_.endRun(run, cleaningUpAfterException);
246  runException_ = false;
247  }
248 
250  finalizeRun(false);
251  }
252 
253  void HandleRuns::finalizeRun(bool cleaningUpAfterException) {
254 
255  if(runException_) return;
256  runException_ = true;
257 
258  if(beginRunCalled_) endRun(currentRun(), cleaningUpAfterException);
261  currentRun_ = INVALID_RUN;
262  runException_ = false;
263  }
264 
267  }
268 
269  NewRun::NewRun(my_context ctx) :
270  my_base(ctx) {
271  assert(context<HandleRuns>().currentRun() == INVALID_RUN);
272  context<HandleRuns>().setupCurrentRun();
273 
274  // Here we assume that the input source or event processor
275  // will throw if we fail to get a valid run. Therefore
276  // we should not ever fail this assert.
277  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
278  }
279 
281 
283  if(run == context<HandleRuns>().currentRun()) {
284  return transit<ContinueRun1>();
285  }
286  context<HandleRuns>().finalizeRun(false);
287  return transit<NewRun>();
288  }
289 
291  if(!context<HandleFiles>().shouldWeCloseOutput()) {
292  return transit<HandleNewInputFile2>();
293  }
294  return forward_event();
295  }
296 
298  my_base(ctx) {
299  context<HandleFiles>().goToNewInputFile();
300  checkInvariant();
301  }
302 
304  checkInvariant();
305  }
306 
308  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
309  return true;
310  }
311 
313  checkInvariant();
314 
315  if(context<HandleRuns>().currentRun() != run) {
316  return transit<NewRun, HandleRuns, Run>(&HandleRuns::finalizeRun, run);
317  } else {
318  return transit<ContinueRun1>();
319  }
320  }
321 
323  checkInvariant();
324  if(!context<HandleFiles>().shouldWeCloseOutput()) {
325  return transit<HandleNewInputFile2>();
326  }
327  return forward_event();
328  }
329 
330  ContinueRun1::ContinueRun1(my_context ctx) :
331  my_base(ctx),
332  ep_(context<Machine>().ep()) {
333  ep_.readAndCacheRun(true);
334  checkInvariant();
335  }
336 
338  checkInvariant();
339  }
340 
342  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
343  return true;
344  }
345 
347  checkInvariant();
348  if(context<HandleRuns>().currentRun() != run) {
349  return transit<NewRun, HandleRuns, Run>(&HandleRuns::finalizeRun, run);
350  } else {
351  return transit<ContinueRun1>();
352  }
353  }
354 
356  checkInvariant();
357  if(!context<HandleFiles>().shouldWeCloseOutput()) {
358  return transit<HandleNewInputFile2>();
359  }
360  return forward_event();
361  }
362 
364  processHistoryID_(phid),
365  run_(run),
366  lumi_(lumi) {
367  }
368 
369  HandleLumis::HandleLumis(my_context ctx) :
370  my_base(ctx),
371  ep_(context<Machine>().ep()),
374  currentLumi_(InvalidLumiID),
376  checkInvariant();
377  }
378 
380  if(ep_.alreadyHandlingException()) return;
381  exitCalled_ = true;
382  checkInvariant();
383  if(!lumiException_ && !context<HandleRuns>().runException()) {
384  finalizeLumi(false);
385  }
386  }
387 
389  if(!exitCalled_) {
390  try {
391  checkInvariant();
392  if(!lumiException_ && !context<HandleRuns>().runException()) {
393  finalizeLumi(true);
394  }
395  }
396  catch(...) {
397  std::string message("Another exception was caught while trying to clean up lumis after the primary fatal exception.");
398  ep_.setExceptionMessageLumis(message);
399  }
400  }
401  }
402 
404  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
405  return true;
406  }
407 
409 
411 
413 
414  Run const& run = context<HandleRuns>().currentRun();
415  assert(run != INVALID_RUN);
416  lumiException_ = true;
418 
419  if(context<Machine>().emptyRunLumiMode() == handleEmptyRunsAndLumis) {
420  assert(context<HandleRuns>().beginRunCalled());
421  ep_.beginLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
422  }
423 
424  lumiException_ = false;
425 
426  currentLumiEmpty_ = true;
427  }
428 
429  void HandleLumis::finalizeLumi(bool cleaningUpAfterException) {
430 
431  lumiException_ = true;
432 
433  if(!currentLumiEmpty_ ||
434  context<Machine>().emptyRunLumiMode() == handleEmptyRunsAndLumis) {
435  ep_.endLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi(), cleaningUpAfterException);
436  }
437 
438  ep_.writeLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
439  ep_.deleteLumiFromCache(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
440  currentLumi_ = InvalidLumiID;
441 
442  lumiException_ = false;
443  }
444 
446  if(currentLumiEmpty_) {
447 
448  if(context<Machine>().emptyRunLumiMode() != handleEmptyRunsAndLumis) {
449  lumiException_ = true;
450  ep_.beginLumi(currentLumi().processHistoryID(), currentLumi().run(), currentLumi().lumi());
451  lumiException_ = false;
452  }
453  currentLumiEmpty_ = false;
454  }
455  }
456 
457  FirstLumi::FirstLumi(my_context ctx) :
458  my_base(ctx) {
459  context<HandleLumis>().setupCurrentLumi();
460  checkInvariant();
461  }
462 
464  checkInvariant();
465  }
466 
468  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
469  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
470  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
471  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
472  assert(context<HandleLumis>().currentLumiEmpty() == true);
473  return true;
474  }
475 
477  if(lumi.id() == context<HandleLumis>().currentLumi().lumi()) {
478  return transit<ContinueLumi>();
479  }
480  return transit<AnotherLumi>();
481  }
482 
484  checkInvariant();
485  if(!context<HandleFiles>().shouldWeCloseOutput()) {
486  return transit<HandleNewInputFile3>();
487  }
488  return forward_event();
489  }
490 
491  AnotherLumi::AnotherLumi(my_context ctx) :
492  my_base(ctx) {
493  context<HandleLumis>().finalizeLumi(false);
494  context<HandleLumis>().setupCurrentLumi();
495  checkInvariant();
496  }
497 
499  checkInvariant();
500  }
501 
503  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
504  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
505  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
506  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
507  assert(context<HandleLumis>().currentLumiEmpty() == true);
508  return true;
509  }
510 
512  if(lumi.id() == context<HandleLumis>().currentLumi().lumi()) {
513  return transit<ContinueLumi>();
514  }
515  return transit<AnotherLumi>();
516  }
517 
519  checkInvariant();
520  if(!context<HandleFiles>().shouldWeCloseOutput()) {
521  return transit<HandleNewInputFile3>();
522  }
523  return forward_event();
524  }
525 
526  HandleEvent::HandleEvent(my_context ctx) :
527  my_base(ctx),
528  ep_(context<Machine>().ep()) {
530  checkInvariant();
531  }
532 
534  checkInvariant();
535  }
536 
538  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
539  assert(context<HandleRuns>().beginRunCalled());
540  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
541  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
542  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
543  assert(context<HandleLumis>().currentLumiEmpty() == false);
544  return true;
545  }
546 
548  checkInvariant();
549  if(!context<HandleFiles>().shouldWeCloseOutput()) {
550  return transit<HandleNewInputFile3>();
551  }
552  return forward_event();
553  }
554 
556  markNonEmpty();
558  if(ep_.shouldWeStop()) post_event(Stop());
559  }
560 
562  context<HandleRuns>().beginRunIfNotDoneAlready();
563  context<HandleLumis>().markLumiNonEmpty();
564  }
565 
566 
568  my_base(ctx) {
569  context<HandleFiles>().goToNewInputFile();
570  checkInvariant();
571  }
572 
574  checkInvariant();
575  }
576 
578  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
579  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
580  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
581  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
582  return true;
583  }
584 
586  checkInvariant();
587 
588  if(context<HandleRuns>().currentRun() == run) {
589  return transit<ContinueRun2>();
590  }
591  return forward_event();
592  }
593 
595  checkInvariant();
596  if(!context<HandleFiles>().shouldWeCloseOutput()) {
597  return transit<HandleNewInputFile3>();
598  }
599  return forward_event();
600  }
601 
602  ContinueRun2::ContinueRun2(my_context ctx) :
603  my_base(ctx),
604  ep_(context<Machine>().ep()) {
605  ep_.readAndCacheRun(true);
606  checkInvariant();
607  }
608 
610  checkInvariant();
611  }
612 
614  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
615  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
616  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
617  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
618  return true;
619  }
620 
622  checkInvariant();
623  if(context<HandleRuns>().currentRun() != run) {
624  return forward_event();
625  } else {
626  return transit<ContinueRun2>();
627  }
628  }
629 
631  checkInvariant();
632 
633  if(context<HandleLumis>().currentLumi().lumi() != lumi.id()) {
634  return transit<AnotherLumi>();
635  } else {
636  return transit<ContinueLumi>();
637  }
638  }
639 
641  checkInvariant();
642  if(!context<HandleFiles>().shouldWeCloseOutput()) {
643  return transit<HandleNewInputFile3>();
644  }
645  return forward_event();
646  }
647 
648  ContinueLumi::ContinueLumi(my_context ctx) :
649  my_base(ctx),
650  ep_(context<Machine>().ep()) {
651  ep_.readAndCacheLumi(true);
652  checkInvariant();
653  }
654 
656  checkInvariant();
657  }
658 
660  assert(context<HandleRuns>().currentRun() != INVALID_RUN);
661  assert(context<HandleLumis>().currentLumi().processHistoryID() == context<HandleRuns>().currentRun().processHistoryID());
662  assert(context<HandleLumis>().currentLumi().run() == context<HandleRuns>().currentRun().runNumber());
663  assert(context<HandleLumis>().currentLumi().lumi() != INVALID_LUMI);
664  return true;
665  }
666 
668  checkInvariant();
669  if(context<HandleLumis>().currentLumi().lumi() != lumi.id()) {
670  return transit<AnotherLumi>();
671  } else {
672  return transit<ContinueLumi>();
673  }
674  }
675 
677  checkInvariant();
678  if(!context<HandleFiles>().shouldWeCloseOutput()) {
679  return transit<HandleNewInputFile3>();
680  }
681  return forward_event();
682  }
683 }
static const char runNumber_[]
HandleEvent(my_context ctx)
Definition: EPStates.cc:526
sc::result react(File const &file)
Definition: EPStates.cc:131
edm::IEventProcessor & ep_
Definition: EPStates.h:342
FirstFile(my_context ctx)
Definition: EPStates.cc:123
virtual void respondToCloseOutputFiles()=0
edm::IEventProcessor * ep_
Definition: EPStates.h:108
virtual void endRun(statemachine::Run const &run, bool cleaningUpAfterException)=0
void endRun(Run const &run, bool cleaningUpAfterException)
Definition: EPStates.cc:241
HandleFiles(my_context ctx)
Definition: EPStates.cc:55
Starting(my_context ctx)
Definition: EPStates.cc:51
HandleLumis(my_context ctx)
Definition: EPStates.cc:369
virtual void setExceptionMessageFiles(std::string &message)=0
virtual void writeLumi(ProcessHistoryID const &phid, int run, int lumi)=0
HandleNewInputFile3(my_context ctx)
Definition: EPStates.cc:567
virtual void endLumi(ProcessHistoryID const &phid, int run, int lumi, bool cleaningUpAfterException)=0
edm::IEventProcessor & ep_
Definition: EPStates.h:178
bool currentLumiEmpty() const
Definition: EPStates.cc:410
edm::IEventProcessor & ep_
Definition: EPStates.h:452
FileMode fileMode() const
Definition: EPStates.cc:34
tuple lumi
Definition: fjr2json.py:35
edm::IEventProcessor & ep_
Definition: EPStates.h:434
sc::result react(File const &file)
Definition: EPStates.cc:547
sc::result react(File const &file)
Definition: EPStates.cc:170
virtual int readAndCacheLumi(bool merge)=0
virtual bool shouldWeStop() const =0
sc::result react(Lumi const &lumi)
Definition: EPStates.cc:667
ContinueLumi(my_context ctx)
Definition: EPStates.cc:648
void closeFiles(bool cleaningUpAfterException)
Definition: EPStates.cc:78
edm::IEventProcessor & ep_
Definition: EPStates.h:153
virtual void openOutputFiles()=0
virtual void readAndProcessEvent()=0
sc::result react(Run const &run)
Definition: EPStates.cc:312
virtual void setExceptionMessageLumis(std::string &message)=0
virtual void rewindInput()=0
edm::IEventProcessor & ep_
Definition: EPStates.h:308
void startingNewLoop(File const &file)
Definition: EPStates.cc:37
virtual void startingNewLoop()=0
FirstLumi(my_context ctx)
Definition: EPStates.cc:457
virtual statemachine::Run readAndCacheRun(bool merge)=0
EmptyRunLumiMode emptyRunLumiMode_
Definition: EPStates.h:110
virtual void readFile()=0
edm::IEventProcessor & ep() const
Definition: EPStates.cc:33
bool beginRunCalled() const
Definition: EPStates.cc:218
HandleRuns(my_context ctx)
Definition: EPStates.cc:192
edm::IEventProcessor & ep_
Definition: EPStates.h:252
EndingLoop(my_context ctx)
Definition: EPStates.cc:98
virtual void deleteRunFromCache(statemachine::Run const &run)=0
ContinueRun2(my_context ctx)
Definition: EPStates.cc:602
int runNumber() const
Definition: EPStates.h:48
tuple result
Definition: query.py:137
LumiID const & currentLumi() const
Definition: EPStates.cc:408
HandleNewInputFile1(my_context ctx)
Definition: EPStates.cc:147
edm::IEventProcessor & ep_
Definition: EPStates.h:228
virtual bool shouldWeCloseOutput() const =0
sc::result react(Run const &run)
Definition: EPStates.cc:346
virtual void closeOutputFiles()=0
virtual void respondToOpenOutputFiles()=0
Run const & currentRun() const
Definition: EPStates.cc:219
virtual void deleteLumiFromCache(ProcessHistoryID const &phid, int run, int lumi)=0
ContinueRun1(my_context ctx)
Definition: EPStates.cc:330
edm::IEventProcessor & ep_
Definition: EPStates.h:400
edm::IEventProcessor & ep_
Definition: EPStates.h:196
AnotherLumi(my_context ctx)
Definition: EPStates.cc:491
sc::result react(Lumi const &lumi)
Definition: EPStates.cc:511
virtual void respondToOpenInputFile()=0
virtual void respondToCloseInputFile()=0
void finalizeLumi(bool cleaningUpAfterException)
Definition: EPStates.cc:429
void finalizeRun(Run const &)
Definition: EPStates.cc:249
NewInputAndOutputFiles(my_context ctx)
Definition: EPStates.cc:162
Machine(edm::IEventProcessor *ep, FileMode fileMode, EmptyRunLumiMode emptyRunLumiMode)
Definition: EPStates.cc:25
void rewindAndPrepareForNextLoop(Restart const &restart)
Definition: EPStates.cc:46
NewRun(my_context ctx)
Definition: EPStates.cc:269
sc::result react(Run const &run)
Definition: EPStates.cc:585
sc::result react(Lumi const &lumi)
Definition: EPStates.cc:476
sc::result react(Stop const &)
Definition: EPStates.cc:107
virtual bool alreadyHandlingException() const =0
HandleNewInputFile2(my_context ctx)
Definition: EPStates.cc:297
int id() const
Definition: EPStates.h:68
edm::IEventProcessor & ep_
Definition: EPStates.h:168
virtual void setExceptionMessageRuns(std::string &message)=0
void beginRun(Run const &run)
Definition: EPStates.cc:233
virtual bool endOfLoop()=0
edm::ProcessHistoryID const & processHistoryID() const
Definition: EPStates.h:47
Run(edm::ProcessHistoryID const &phid, int runNumber)
Definition: EPStates.cc:18
virtual void closeInputFile(bool cleaningUpAfterException)=0
Error(my_context ctx)
Definition: EPStates.cc:111
sc::result react(Run const &run)
Definition: EPStates.cc:282
virtual void doErrorStuff()=0
virtual void beginRun(statemachine::Run const &run)=0
sc::result react(Run const &run)
Definition: EPStates.cc:621
EmptyRunLumiMode emptyRunLumiMode() const
Definition: EPStates.cc:35
LumiID(edm::ProcessHistoryID const &phid, int run, int lumi)
Definition: EPStates.cc:363
sc::result react(File const &file)
Definition: EPStates.cc:154
bool runException() const
Definition: EPStates.cc:220
virtual void writeRun(statemachine::Run const &run)=0
virtual void beginLumi(ProcessHistoryID const &phid, int run, int lumi)=0
virtual void prepareForNextLoop()=0