Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /** \class edm::NavigateEventsLooper
0002 
0003 Allows interactive navigation from event to event
0004 in cmsRun jobs.  Just add the looper to the
0005 python configuration and then respond to the
0006 questions that show up on the display by typing
0007 a number followed by return.
0008 
0009 This was originally written to test the looper interface
0010 used by the Fireworks event display.  It might be useful
0011 by itself.
0012 
0013 If you use this either do not use a PoolOutputModule or
0014 turn off fast cloning its configuration.
0015 
0016 \author W. David Dagenhart, created 27 August, 2010
0017 
0018 */
0019 
0020 #include "FWCore/Framework/interface/EDLooperBase.h"
0021 #include "FWCore/Framework/interface/LooperFactory.h"
0022 #include "FWCore/Framework/interface/ProcessingController.h"
0023 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0024 
0025 #include <iostream>
0026 
0027 namespace edm {
0028 
0029   class NavigateEventsLooper : public EDLooperBase {
0030   public:
0031     NavigateEventsLooper(ParameterSet const& pset);
0032     NavigateEventsLooper(NavigateEventsLooper const&) = delete;                   // stop default
0033     NavigateEventsLooper const& operator=(NavigateEventsLooper const&) = delete;  // stop default
0034     ~NavigateEventsLooper() override;
0035 
0036     void startingNewLoop(unsigned int iIteration) override;
0037     Status duringLoop(Event const& ev, EventSetup const& es, ProcessingController& pc) override;
0038     Status endOfLoop(EventSetup const& es, unsigned int iCounter) override;
0039 
0040   private:
0041     int maxLoops_;
0042     int countLoops_;
0043     bool shouldStopLoop_;
0044     bool shouldStopProcess_;
0045   };
0046 
0047   NavigateEventsLooper::NavigateEventsLooper(ParameterSet const& pset)
0048       : maxLoops_(pset.getUntrackedParameter<int>("maxLoops", -1)),
0049         countLoops_(0),
0050         shouldStopLoop_(false),
0051         shouldStopProcess_(false) {}
0052 
0053   NavigateEventsLooper::~NavigateEventsLooper() {}
0054 
0055   void NavigateEventsLooper::startingNewLoop(unsigned int /*iIteration*/) {}
0056 
0057   EDLooperBase::Status NavigateEventsLooper::duringLoop(Event const&, EventSetup const&, ProcessingController& pc) {
0058     if (!pc.lastOperationSucceeded()) {
0059       std::cout << "Event could not be found. Nothing done. Try again.\n";
0060     }
0061 
0062     std::cout << "\nWhat should we do next?\n";
0063 
0064     if (pc.forwardState() == ProcessingController::kEventsAheadInFile) {
0065       std::cout << "(0) process the next event\n";
0066     } else if (pc.forwardState() == ProcessingController::kNextFileExists) {
0067       std::cout << "(0) process the next event if it exists (at last event in the open file. there are more files)\n";
0068     } else if (pc.forwardState() == ProcessingController::kAtLastEvent) {
0069       std::cout << "(0) will stop the loop because this is the last event\n";
0070     } else if (pc.forwardState() == ProcessingController::kUnknownForward) {
0071       std::cout << "(0) process the next event (if it exists)\n";
0072     }
0073 
0074     if (pc.canRandomAccess()) {
0075       if (pc.reverseState() == ProcessingController::kEventsBackwardsInFile) {
0076         std::cout << "(1) process the previous event\n";
0077       } else if (pc.reverseState() == ProcessingController::kPreviousFileExists) {
0078         std::cout << "(1) process the previous event if there are any (at first event in the open file. there are "
0079                      "previous files)\n";
0080       } else if (pc.reverseState() == ProcessingController::kAtFirstEvent) {
0081         std::cout << "(1) will stop the loop because this is the first event\n";
0082       }
0083 
0084       std::cout << "(2) process a specific event\n";
0085     }
0086 
0087     std::cout << "(3) stop loop\n";
0088     std::cout << "(4) stop process" << std::endl;
0089     long long int x;
0090 
0091     bool inputFailed = false;
0092     do {
0093       inputFailed = false;
0094       if (!(std::cin >> x) || x < 0 || x > 4) {
0095         inputFailed = true;
0096         std::cin.clear();
0097         std::cin.ignore(10000, '\n');
0098         std::cout << "Please enter numeric characters only. The value must be in the range 0 to 4 (inclusive). Please "
0099                      "try again."
0100                   << std::endl;
0101       }
0102       if (!pc.canRandomAccess() && (x == 1 || x == 2)) {
0103         inputFailed = true;
0104         std::cout << "The source cannot do random access. 1 and 2 are illegal values. Please try again." << std::endl;
0105       }
0106     } while (inputFailed);
0107 
0108     shouldStopLoop_ = false;
0109     shouldStopProcess_ = false;
0110     if (x == 0) {
0111       pc.setTransitionToNextEvent();
0112     } else if (x == 1) {
0113       pc.setTransitionToPreviousEvent();
0114     } else if (x == 2) {
0115       std::cout << "Which run?" << std::endl;
0116       do {
0117         inputFailed = false;
0118         if (!(std::cin >> x)) {
0119           inputFailed = true;
0120           std::cin.clear();
0121           std::cin.ignore(10000, '\n');
0122           std::cout << "Please enter numeric characters only. Please try again." << std::endl;
0123         }
0124       } while (inputFailed);
0125       RunNumber_t run = x;
0126       std::cout << "Which luminosity block?" << std::endl;
0127       do {
0128         inputFailed = false;
0129         if (!(std::cin >> x)) {
0130           inputFailed = true;
0131           std::cin.clear();
0132           std::cin.ignore(10000, '\n');
0133           std::cout << "Please enter numeric characters only. Please try again." << std::endl;
0134         }
0135       } while (inputFailed);
0136       LuminosityBlockNumber_t lumi = x;
0137       std::cout << "Which event?" << std::endl;
0138       do {
0139         inputFailed = false;
0140         if (!(std::cin >> x)) {
0141           inputFailed = true;
0142           std::cin.clear();
0143           std::cin.ignore(10000, '\n');
0144           std::cout << "Please enter numeric characters only. Please try again." << std::endl;
0145         }
0146       } while (inputFailed);
0147       EventNumber_t ev = x;
0148       pc.setTransitionToEvent(EventID(run, lumi, ev));
0149     } else if (x == 3) {
0150       pc.setTransitionToNextEvent();
0151       shouldStopLoop_ = true;
0152     } else if (x == 4) {
0153       pc.setTransitionToNextEvent();
0154       shouldStopLoop_ = true;
0155       shouldStopProcess_ = true;
0156     }
0157     return shouldStopLoop_ ? kStop : kContinue;
0158   }
0159 
0160   EDLooperBase::Status NavigateEventsLooper::endOfLoop(EventSetup const&, unsigned int /*iCounter*/) {
0161     std::cout << "Ending loop" << std::endl;
0162     if (shouldStopProcess_)
0163       return kStop;
0164     ++countLoops_;
0165     return (maxLoops_ < 0 || countLoops_ < maxLoops_) ? kContinue : kStop;
0166   }
0167 }  // namespace edm
0168 
0169 using edm::NavigateEventsLooper;
0170 DEFINE_FWK_LOOPER(NavigateEventsLooper);