diff --git a/src/examples/TimesliceExample/MyFileWriter.h b/src/examples/TimesliceExample/MyFileWriter.h index be13c5f27..c7f1301e8 100644 --- a/src/examples/TimesliceExample/MyFileWriter.h +++ b/src/examples/TimesliceExample/MyFileWriter.h @@ -22,8 +22,9 @@ struct MyFileWriter : public JEventProcessor { Input m_evt_frame_in {this, {.name = "", .level = JEventLevel::PhysicsEvent}}; - // TODO: Support optional inputs - // Input m_ts_frame_in {this, "", JEventLevel::Timeslice}; + Input m_ts_frame_in {this, {.name = "", + .level = JEventLevel::Timeslice, + .is_optional = true }}; std::unique_ptr m_writer = nullptr; std::mutex m_mutex; @@ -45,9 +46,7 @@ struct MyFileWriter : public JEventProcessor { auto ts_nr = ts.GetEventNumber(); if (event->GetEventIndex() == 0) { - // m_writer->writeFrame(*(m_ts_frame_in().at(0)), "timeslices"); - auto ts_frame_in = ts.Get(); - m_writer->writeFrame(*(ts_frame_in.at(0)), "timeslices"); + m_writer->writeFrame(*(m_ts_frame_in().at(0)), "timeslices"); } LOG_DEBUG(GetLogger()) diff --git a/src/libraries/JANA/JEvent.h b/src/libraries/JANA/JEvent.h index 29e601047..2fa573c4f 100644 --- a/src/libraries/JANA/JEvent.h +++ b/src/libraries/JANA/JEvent.h @@ -80,13 +80,13 @@ class JEvent : public std::enable_shared_from_this //OBJECTS // C style getters template JFactoryT* Get(const T** item, const std::string& tag="") const; - template JFactoryT* Get(std::vector &vec, const std::string& tag = "") const; + template JFactoryT* Get(std::vector &vec, const std::string& tag = "", bool strict=true) const; template void GetAll(std::vector &vec) const; // C++ style getters template const T* GetSingle(const std::string& tag = "") const; template const T* GetSingleStrict(const std::string& tag = "") const; - template std::vector Get(const std::string& tag = "") const; + template std::vector Get(const std::string& tag = "", bool strict=true) const; template typename JFactoryT::PairType GetIterators(const std::string& aTag = "") const; template std::vector GetAll() const; template std::map,std::vector> GetAllChildren() const; @@ -336,9 +336,10 @@ JFactoryT* JEvent::Get(const T** destination, const std::string& tag) const template -JFactoryT* JEvent::Get(std::vector& destination, const std::string& tag) const +JFactoryT* JEvent::Get(std::vector& destination, const std::string& tag, bool strict) const { - auto factory = GetFactory(tag, true); + auto factory = GetFactory(tag, strict); + if (factory == nullptr) return nullptr; // Will have thrown already if strict==true JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope auto iterators = factory->CreateAndGetData(this->shared_from_this()); for (auto it=iterators.first; it!=iterators.second; it++) { @@ -392,12 +393,13 @@ template const T* JEvent::GetSingleStrict(const std::string& tag) const template -std::vector JEvent::Get(const std::string& tag) const { +std::vector JEvent::Get(const std::string& tag, bool strict) const { - auto factory = GetFactory(tag, true); + auto factory = GetFactory(tag, strict); + std::vector vec; + if (factory == nullptr) return vec; // Will have thrown already if strict==true JCallGraphEntryMaker cg_entry(mCallGraph, factory); // times execution until this goes out of scope auto iters = factory->CreateAndGetData(this->shared_from_this()); - std::vector vec; for (auto it=iters.first; it!=iters.second; ++it) { vec.push_back(*it); } diff --git a/src/libraries/JANA/Omni/JHasInputs.h b/src/libraries/JANA/Omni/JHasInputs.h index 84e4dcedb..10211cc7d 100644 --- a/src/libraries/JANA/Omni/JHasInputs.h +++ b/src/libraries/JANA/Omni/JHasInputs.h @@ -102,20 +102,22 @@ struct JHasInputs { void GetCollection(const JEvent& event) { auto& level = this->levels[0]; if (level == event.GetLevel() || level == JEventLevel::None) { - m_data = event.Get(this->names[0]); + m_data = event.Get(this->names[0], !this->is_optional); } else { - m_data = event.GetParent(level).template Get(this->names[0]); + if (this->is_optional && !event.HasParent(level)) return; + m_data = event.GetParent(level).template Get(this->names[0], !this->is_optional); } } void PrefetchCollection(const JEvent& event) { auto& level = this->levels[0]; auto& name = this->names[0]; if (level == event.GetLevel() || level == JEventLevel::None) { - event.Get(name); + event.Get(name, !this->is_optional); } else { - event.GetParent(level).template Get(name); + if (this->is_optional && !event.HasParent(level)) return; + event.GetParent(level).template Get(name, !this->is_optional); } } }; @@ -142,10 +144,11 @@ struct JHasInputs { auto& level = this->levels[0]; auto& name = this->names[0]; if (level == event.GetLevel() || level == JEventLevel::None) { - m_data = event.GetCollection(name); + m_data = event.GetCollection(name, !this->is_optional); } else { - m_data = event.GetParent(level).template GetCollection(name); + if (this->is_optional && !event.HasParent(level)) return; + m_data = event.GetParent(level).template GetCollection(name, !this->is_optional); } } @@ -153,10 +156,11 @@ struct JHasInputs { auto& level = this->levels[0]; auto& name = this->names[0]; if (level == event.GetLevel() || level == JEventLevel::None) { - event.GetCollection(name); + event.GetCollection(name, !this->is_optional); } else { - event.GetParent(level).template GetCollection(name); + if (this->is_optional && !event.HasParent(level)) return; + event.GetParent(level).template GetCollection(name, !this->is_optional); } } }; @@ -189,10 +193,11 @@ struct JHasInputs { auto& coll_name = names[i]; auto& level = levels[i]; if (level == event.GetLevel() || level == JEventLevel::None) { - m_data.push_back(event.GetCollection(coll_name)); + m_data.push_back(event.GetCollection(coll_name, !this->is_optional)); } else { - m_data.push_back(event.GetParent(level).GetCollection(coll_name)); + if (this->is_optional && !event.HasParent(level)) return; + m_data.push_back(event.GetParent(level).GetCollection(coll_name, !this->is_optional)); } } } @@ -205,10 +210,11 @@ struct JHasInputs { auto& coll_name = names[i]; auto& level = levels[i]; if (level == event.GetLevel() || level == JEventLevel::None) { - event.GetCollection(coll_name); + event.GetCollection(coll_name, !this->is_optional); } else { - event.GetParent(level).GetCollection(coll_name); + if (this->is_optional && !event.HasParent(level)) return; + event.GetParent(level).GetCollection(coll_name, !this->is_optional); } } }