Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:12:28

0001 #include "FWCore/Framework/test/MockEventProcessor.h"
0002 
0003 namespace {
0004   // As each data item is read from the mock data it is
0005   // stored in one of these:
0006   struct token {
0007     int value;
0008     char id;
0009   };
0010 
0011   std::istream& operator>>(std::istream& is, token& t) {
0012     if (is >> t.id)
0013       is >> t.value;
0014     return is;
0015   }
0016 
0017   //The TransitionProcessors.icc uses the class name
0018   // EventProcessor (with no namespace) as the type
0019   // to which it interacts.
0020   using EventProcessor = edm::MockEventProcessor;
0021 
0022   class WaitingTaskHolder;
0023 }  // namespace
0024 
0025 #define TEST_NO_FWD_DECL
0026 
0027 namespace {
0028 #include "FWCore/Framework/src/TransitionProcessors.icc"
0029 }
0030 
0031 namespace edm {
0032 
0033   MockEventProcessor::MockEventProcessor(std::string const& mockData, std::ostream& output, bool iDoNotMerge)
0034       : mockData_(mockData),
0035         output_(output),
0036         input_(mockData_),
0037         nextRun_(0),
0038         nextLumi_(0),
0039         doNotMerge_(iDoNotMerge),
0040         shouldWeCloseOutput_(true),
0041         shouldWeEndLoop_(true),
0042         shouldWeStop_(false),
0043         eventProcessed_(false),
0044         reachedEndOfInput_(false),
0045         shouldThrow_(false) {}
0046 
0047   InputSource::ItemType MockEventProcessor::nextTransitionType() {
0048     token t;
0049     if (not(input_ >> t)) {
0050       reachedEndOfInput_ = true;
0051       return lastTransition_ = InputSource::ItemType::IsStop;
0052     }
0053 
0054     char ch = t.id;
0055 
0056     eventProcessed_ = false;
0057     if (ch == 'r') {
0058       output_ << "    *** nextItemType: Run " << t.value << " ***\n";
0059       nextRun_ = static_cast<RunNumber_t>(t.value);
0060       return lastTransition_ = InputSource::ItemType::IsRun;
0061     } else if (ch == 'l') {
0062       output_ << "    *** nextItemType: Lumi " << t.value << " ***\n";
0063       nextLumi_ = static_cast<LuminosityBlockNumber_t>(t.value);
0064       return lastTransition_ = InputSource::ItemType::IsLumi;
0065     } else if (ch == 'e') {
0066       output_ << "    *** nextItemType: Event ***\n";
0067       // a special value for test purposes only
0068       if (t.value == 7) {
0069         shouldWeStop_ = true;
0070         output_ << "    *** shouldWeStop will return true this event ***\n";
0071       } else {
0072         shouldWeStop_ = false;
0073       }
0074       return lastTransition_ = InputSource::ItemType::IsEvent;
0075     } else if (ch == 'f') {
0076       output_ << "    *** nextItemType: File " << t.value << " ***\n";
0077       // a special value for test purposes only
0078       if (t.value == 0)
0079         shouldWeCloseOutput_ = false;
0080       else
0081         shouldWeCloseOutput_ = true;
0082       return lastTransition_ = InputSource::ItemType::IsFile;
0083     } else if (ch == 's') {
0084       output_ << "    *** nextItemType: Stop " << t.value << " ***\n";
0085       // a special value for test purposes only
0086       if (t.value == 0)
0087         shouldWeEndLoop_ = false;
0088       else
0089         shouldWeEndLoop_ = true;
0090       return lastTransition_ = InputSource::ItemType::IsStop;
0091     } else if (ch == 'x') {
0092       output_ << "    *** nextItemType: Restart " << t.value << " ***\n";
0093       shouldWeEndLoop_ = t.value;
0094       return lastTransition_ = InputSource::ItemType::IsStop;
0095     } else if (ch == 't') {
0096       output_ << "    *** nextItemType: Throw " << t.value << " ***\n";
0097       shouldThrow_ = true;
0098       return nextTransitionType();
0099     }
0100     return lastTransition_ = InputSource::ItemType::IsInvalid;
0101   }
0102 
0103   InputSource::ItemType MockEventProcessor::lastTransitionType() const { return lastTransition_; }
0104 
0105   InputSource::ItemType MockEventProcessor::readAndProcessEvents() {
0106     bool first = true;
0107     do {
0108       if (first) {
0109         first = false;
0110       } else {
0111         shouldWeStop();
0112       }
0113       readAndProcessEvent();
0114       if (shouldWeStop()) {
0115         return InputSource::ItemType::IsEvent;
0116       }
0117     } while (nextTransitionType() == InputSource::ItemType::IsEvent);
0118 
0119     return lastTransitionType();
0120   }
0121 
0122   void MockEventProcessor::runToCompletion() {
0123     do {
0124       FilesProcessor fp(doNotMerge_);
0125 
0126       bool firstTime = true;
0127       do {
0128         if (not firstTime) {
0129           prepareForNextLoop();
0130           rewindInput();
0131         } else {
0132           firstTime = false;
0133         }
0134         startingNewLoop();
0135 
0136         auto trans = fp.processFiles(*this);
0137 
0138         fp.normalEnd();
0139 
0140         if (trans != InputSource::ItemType::IsStop) {
0141           //problem with the source
0142           doErrorStuff();
0143           break;
0144         }
0145       } while (not endOfLoop());
0146       output_ << "Left processing loop.\n";
0147     } while (not reachedEndOfInput_ and not input_.eof());
0148 
0149     return;
0150   }
0151 
0152   void MockEventProcessor::readFile() {
0153     output_ << " \treadFile\n";
0154     throwIfNeeded();
0155   }
0156 
0157   void MockEventProcessor::closeInputFile(bool /*cleaningUpAfterException*/) { output_ << "\tcloseInputFile\n"; }
0158 
0159   void MockEventProcessor::openOutputFiles() { output_ << "\topenOutputFiles\n"; }
0160 
0161   void MockEventProcessor::closeOutputFiles() { output_ << "\tcloseOutputFiles\n"; }
0162 
0163   void MockEventProcessor::respondToOpenInputFile() { output_ << "\trespondToOpenInputFile\n"; }
0164 
0165   void MockEventProcessor::respondToCloseInputFile() { output_ << "\trespondToCloseInputFile\n"; }
0166 
0167   void MockEventProcessor::startingNewLoop() { output_ << "\tstartingNewLoop\n"; }
0168 
0169   bool MockEventProcessor::endOfLoop() {
0170     output_ << "\tendOfLoop\n";
0171     return shouldWeEndLoop_;
0172   }
0173 
0174   void MockEventProcessor::rewindInput() { output_ << "\trewind\n"; }
0175 
0176   void MockEventProcessor::prepareForNextLoop() { output_ << "\tprepareForNextLoop\n"; }
0177 
0178   bool MockEventProcessor::shouldWeCloseOutput() const {
0179     output_ << "\tshouldWeCloseOutput\n";
0180     return shouldWeCloseOutput_;
0181   }
0182 
0183   void MockEventProcessor::doErrorStuff() { output_ << "\tdoErrorStuff\n"; }
0184 
0185   void MockEventProcessor::beginProcessBlock(bool& beginProcessBlockSucceeded) {}
0186   void MockEventProcessor::inputProcessBlocks() {}
0187   void MockEventProcessor::endProcessBlock(bool cleaningUpAfterException, bool beginProcessBlockSucceeded) {}
0188 
0189   InputSource::ItemType MockEventProcessor::processRuns() {
0190     bool finished = false;
0191     auto nextTransition = edm::InputSource::ItemType::IsRun;
0192     do {
0193       switch (nextTransition) {
0194         case edm::InputSource::ItemType::IsRun: {
0195           processRun();
0196           nextTransition = nextTransitionType();
0197           break;
0198         }
0199         case edm::InputSource::ItemType::IsLumi: {
0200           nextTransition = processLumis();
0201           break;
0202         }
0203         default:
0204           finished = true;
0205       }
0206     } while (not finished);
0207     return nextTransition;
0208   }
0209 
0210   void MockEventProcessor::processRun() {
0211     if ((not currentRun_) or (currentRunNumber_ != nextRun_)) {
0212       if (currentRun_) {
0213         endUnfinishedLumi(true);
0214         endUnfinishedRun(true);
0215       }
0216       currentRun_ = true;
0217       readRun();
0218       beginRun(currentRunNumber_);
0219     } else {
0220       //merge
0221       readAndMergeRun();
0222     }
0223   }
0224 
0225   InputSource::ItemType MockEventProcessor::processLumis() {
0226     if (lumiStatus_ and currentLumiNumber_ == nextLumi_) {
0227       readAndMergeLumi();
0228       if (nextTransitionType() == InputSource::ItemType::IsEvent) {
0229         readAndProcessEvents();
0230         if (shouldWeStop()) {
0231           return edm::InputSource::ItemType::IsStop;
0232         }
0233       }
0234     } else {
0235       endUnfinishedLumi(true);
0236       lumiStatus_ = true;
0237       auto lumi = readLuminosityBlock();
0238       output_ << "\tbeginLumi " << currentRunNumber_ << "/" << lumi << "\n";
0239       throwIfNeeded();
0240       didGlobalBeginLumiSucceed_ = true;
0241       //Need to do event processing here
0242       if (nextTransitionType() == InputSource::ItemType::IsEvent) {
0243         readAndProcessEvents();
0244         if (shouldWeStop()) {
0245           return edm::InputSource::ItemType::IsStop;
0246         }
0247       }
0248     }
0249     return lastTransitionType();
0250   }
0251 
0252   void MockEventProcessor::beginRun(RunNumber_t run) {
0253     output_ << "\tbeginRun " << run << "\n";
0254     throwIfNeeded();
0255     didGlobalBeginRunSucceed_ = true;
0256   }
0257 
0258   void MockEventProcessor::endRun() {
0259     auto postfix = didGlobalBeginRunSucceed_ ? "\n" : " global failed\n";
0260     output_ << "\tendRun " << currentRunNumber_ << postfix;
0261     currentRun_ = false;
0262   }
0263 
0264   void MockEventProcessor::endUnfinishedRun(bool) {
0265     endRun();
0266     if (didGlobalBeginRunSucceed_) {
0267       writeRun();
0268     }
0269     clearRunPrincipal();
0270   }
0271 
0272   void MockEventProcessor::endUnfinishedLumi(bool) {
0273     if (lumiStatus_) {
0274       endLumi();
0275       if (didGlobalBeginLumiSucceed_) {
0276         writeLumi();
0277       }
0278       clearLumiPrincipal();
0279     }
0280   }
0281 
0282   void MockEventProcessor::endLumi() {
0283     auto postfix = didGlobalBeginLumiSucceed_ ? "\n" : " global failed\n";
0284     output_ << "\tendLumi " << currentRunNumber_ << "/" << currentLumiNumber_ << postfix;
0285   }
0286 
0287   void MockEventProcessor::readRun() {
0288     currentRunNumber_ = nextRun_;
0289     output_ << "\treadRun " << currentRunNumber_ << "\n";
0290   }
0291 
0292   void MockEventProcessor::readAndMergeRun() { output_ << "\treadAndMergeRun " << currentRunNumber_ << "\n"; }
0293 
0294   LuminosityBlockNumber_t MockEventProcessor::readLuminosityBlock() {
0295     output_ << "\treadLuminosityBlock " << nextLumi_ << "\n";
0296     currentLumiNumber_ = nextLumi_;
0297     return currentLumiNumber_;
0298   }
0299 
0300   LuminosityBlockNumber_t MockEventProcessor::readAndMergeLumi() {
0301     output_ << "\treadAndMergeLumi " << currentLumiNumber_ << "\n";
0302     return currentLumiNumber_;
0303   }
0304 
0305   void MockEventProcessor::writeRun() { output_ << "\twriteRun " << currentRunNumber_ << "\n"; }
0306 
0307   void MockEventProcessor::clearRunPrincipal() {
0308     output_ << "\tclearRunPrincipal " << currentRunNumber_ << "\n";
0309     didGlobalBeginRunSucceed_ = false;
0310   }
0311 
0312   void MockEventProcessor::writeLumi() {
0313     output_ << "\twriteLumi " << currentRunNumber_ << "/" << currentLumiNumber_ << "\n";
0314   }
0315 
0316   void MockEventProcessor::clearLumiPrincipal() {
0317     output_ << "\tclearLumiPrincipal " << currentRunNumber_ << "/" << currentLumiNumber_ << "\n";
0318     lumiStatus_ = false;
0319     didGlobalBeginLumiSucceed_ = false;
0320   }
0321 
0322   void MockEventProcessor::readAndProcessEvent() {
0323     output_ << "\treadEvent\n";
0324     output_ << "\tprocessEvent\n";
0325     eventProcessed_ = true;
0326     throwIfNeeded();
0327   }
0328 
0329   bool MockEventProcessor::shouldWeStop() const {
0330     output_ << "\tshouldWeStop\n";
0331     return eventProcessed_ and shouldWeStop_;
0332   }
0333 
0334   void MockEventProcessor::throwIfNeeded() {
0335     if (shouldThrow_) {
0336       shouldThrow_ = false;
0337       output_ << "\tthrowing\n";
0338       throw TestException();
0339     }
0340   }
0341 
0342   void MockEventProcessor::setExceptionMessageFiles(std::string&) {}
0343   void MockEventProcessor::setExceptionMessageRuns() {}
0344   void MockEventProcessor::setExceptionMessageLumis() {}
0345 
0346   bool MockEventProcessor::setDeferredException(std::exception_ptr) { return true; }
0347 
0348 }  // namespace edm