Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #include "FWCore/Framework/interface/FileBlock.h"
0002 #include "FWCore/Framework/interface/Frameworkfwd.h"
0003 #include "FWCore/Framework/interface/EventPrincipal.h"
0004 #include "FWCore/Framework/interface/InputSourceMacros.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Sources/interface/ProducerSourceBase.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008 
0009 namespace edm {
0010   class ThrowingSource : public ProducerSourceBase {
0011   public:
0012     explicit ThrowingSource(ParameterSet const&, InputSourceDescription const&);
0013     ~ThrowingSource() noexcept(false) override;
0014 
0015     void beginJob() override;
0016     void endJob() override;
0017     void beginLuminosityBlock(edm::LuminosityBlock&) override;
0018     void beginRun(edm::Run&) override;
0019     std::shared_ptr<edm::FileBlock> readFile_() override;
0020     void closeFile_() override;
0021     std::shared_ptr<edm::RunAuxiliary> readRunAuxiliary_() override;
0022     std::shared_ptr<edm::LuminosityBlockAuxiliary> readLuminosityBlockAuxiliary_() override;
0023     void readEvent_(edm::EventPrincipal&) override;
0024 
0025   private:
0026     enum {
0027       kDoNotThrow = 0,
0028       kConstructor = 1,
0029       kBeginJob = 2,
0030       kBeginRun = 3,
0031       kBeginLumi = 4,
0032       kEndLumi = 5,
0033       kEndRun = 6,
0034       kEndJob = 7,
0035       kGetNextItemType = 8,
0036       kReadEvent = 9,
0037       kReadLuminosityBlockAuxiliary = 10,
0038       kReadRunAuxiliary = 11,
0039       kReadFile = 12,
0040       kCloseFile = 13,
0041       kDestructor = 14
0042     };
0043     bool setRunAndEventInfo(EventID& id, TimeValue_t& time, edm::EventAuxiliary::ExperimentType& eType) override;
0044     void produce(Event&) override;
0045 
0046     // To test exception throws from sources
0047     int whenToThrow_;
0048   };
0049 
0050   ThrowingSource::ThrowingSource(ParameterSet const& pset, InputSourceDescription const& desc)
0051       : ProducerSourceBase(pset, desc, false),
0052         whenToThrow_(pset.getUntrackedParameter<int>("whenToThrow", kDoNotThrow)) {
0053     if (whenToThrow_ == kConstructor)
0054       throw cms::Exception("TestThrow") << "ThrowingSource constructor";
0055   }
0056 
0057   ThrowingSource::~ThrowingSource() noexcept(false) {
0058     if (whenToThrow_ == kDestructor)
0059       throw cms::Exception("TestThrow") << "ThrowingSource destructor";
0060   }
0061 
0062   bool ThrowingSource::setRunAndEventInfo(EventID&, TimeValue_t&, edm::EventAuxiliary::ExperimentType&) { return true; }
0063 
0064   void ThrowingSource::produce(edm::Event&) {}
0065 
0066   void ThrowingSource::beginJob() {
0067     if (whenToThrow_ == kBeginJob)
0068       throw cms::Exception("TestThrow") << "ThrowingSource::beginJob";
0069   }
0070 
0071   void ThrowingSource::endJob() {
0072     if (whenToThrow_ == kEndJob)
0073       throw cms::Exception("TestThrow") << "ThrowingSource::endJob";
0074   }
0075 
0076   void ThrowingSource::beginLuminosityBlock(LuminosityBlock& lb) {
0077     if (whenToThrow_ == kBeginLumi)
0078       throw cms::Exception("TestThrow") << "ThrowingSource::beginLuminosityBlock";
0079   }
0080 
0081   void ThrowingSource::beginRun(Run& run) {
0082     if (whenToThrow_ == kBeginRun)
0083       throw cms::Exception("TestThrow") << "ThrowingSource::beginRun";
0084   }
0085 
0086   std::shared_ptr<FileBlock> ThrowingSource::readFile_() {
0087     if (whenToThrow_ == kReadFile)
0088       throw cms::Exception("TestThrow") << "ThrowingSource::readFile_";
0089     return std::make_shared<FileBlock>();
0090   }
0091 
0092   void ThrowingSource::closeFile_() {
0093     if (whenToThrow_ == kCloseFile)
0094       throw cms::Exception("TestThrow") << "ThrowingSource::closeFile_";
0095   }
0096 
0097   std::shared_ptr<RunAuxiliary> ThrowingSource::readRunAuxiliary_() {
0098     if (whenToThrow_ == kReadRunAuxiliary)
0099       throw cms::Exception("TestThrow") << "ThrowingSource::readRunAuxiliary_";
0100     Timestamp ts = Timestamp(presentTime());
0101     resetNewRun();
0102     return std::make_shared<RunAuxiliary>(eventID().run(), ts, Timestamp::invalidTimestamp());
0103   }
0104 
0105   std::shared_ptr<LuminosityBlockAuxiliary> ThrowingSource::readLuminosityBlockAuxiliary_() {
0106     if (whenToThrow_ == kReadLuminosityBlockAuxiliary)
0107       throw cms::Exception("TestThrow") << "ThrowingSource::readLuminosityBlockAuxiliary_";
0108     if (processingMode() == Runs)
0109       return std::shared_ptr<LuminosityBlockAuxiliary>();
0110     Timestamp ts = Timestamp(presentTime());
0111     resetNewLumi();
0112     return std::make_shared<LuminosityBlockAuxiliary>(
0113         eventID().run(), eventID().luminosityBlock(), ts, Timestamp::invalidTimestamp());
0114   }
0115 
0116   void ThrowingSource::readEvent_(EventPrincipal& eventPrincipal) {
0117     if (whenToThrow_ == kReadEvent)
0118       throw cms::Exception("TestThrow") << "ThrowingSource::readEvent_";
0119     assert(eventCached() || processingMode() != RunsLumisAndEvents);
0120     EventAuxiliary aux(eventID(), processGUID(), Timestamp(presentTime()), false, EventAuxiliary::Undefined);
0121     auto history = processHistoryRegistry().getMapped(aux.processHistoryID());
0122     eventPrincipal.fillEventPrincipal(aux, history);
0123   }
0124 }  // namespace edm
0125 
0126 using edm::ThrowingSource;
0127 DEFINE_FWK_INPUT_SOURCE(ThrowingSource);