Skip to content

Commit

Permalink
Add is_optional flag for Omni Inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Apr 24, 2024
1 parent 86a4676 commit 32c299c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/examples/TimesliceExample/MyFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ struct MyFileWriter : public JEventProcessor {
Input<podio::Frame> m_evt_frame_in {this, {.name = "",
.level = JEventLevel::PhysicsEvent}};

// TODO: Support optional inputs
// Input<podio::Frame> m_ts_frame_in {this, "", JEventLevel::Timeslice};
Input<podio::Frame> m_ts_frame_in {this, {.name = "",
.level = JEventLevel::Timeslice,
.is_optional = true }};

std::unique_ptr<podio::ROOTFrameWriter> m_writer = nullptr;
std::mutex m_mutex;
Expand All @@ -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<podio::Frame>();
m_writer->writeFrame(*(ts_frame_in.at(0)), "timeslices");
m_writer->writeFrame(*(m_ts_frame_in().at(0)), "timeslices");
}

LOG_DEBUG(GetLogger())
Expand Down
16 changes: 9 additions & 7 deletions src/libraries/JANA/JEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ class JEvent : public std::enable_shared_from_this<JEvent>
//OBJECTS
// C style getters
template<class T> JFactoryT<T>* Get(const T** item, const std::string& tag="") const;
template<class T> JFactoryT<T>* Get(std::vector<const T*> &vec, const std::string& tag = "") const;
template<class T> JFactoryT<T>* Get(std::vector<const T*> &vec, const std::string& tag = "", bool strict=true) const;
template<class T> void GetAll(std::vector<const T*> &vec) const;

// C++ style getters
template<class T> const T* GetSingle(const std::string& tag = "") const;
template<class T> const T* GetSingleStrict(const std::string& tag = "") const;
template<class T> std::vector<const T*> Get(const std::string& tag = "") const;
template<class T> std::vector<const T*> Get(const std::string& tag = "", bool strict=true) const;
template<class T> typename JFactoryT<T>::PairType GetIterators(const std::string& aTag = "") const;
template<class T> std::vector<const T*> GetAll() const;
template<class T> std::map<std::pair<std::string,std::string>,std::vector<T*>> GetAllChildren() const;
Expand Down Expand Up @@ -336,9 +336,10 @@ JFactoryT<T>* JEvent::Get(const T** destination, const std::string& tag) const


template<class T>
JFactoryT<T>* JEvent::Get(std::vector<const T*>& destination, const std::string& tag) const
JFactoryT<T>* JEvent::Get(std::vector<const T*>& destination, const std::string& tag, bool strict) const
{
auto factory = GetFactory<T>(tag, true);
auto factory = GetFactory<T>(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++) {
Expand Down Expand Up @@ -392,12 +393,13 @@ template<class T> const T* JEvent::GetSingleStrict(const std::string& tag) const


template<class T>
std::vector<const T*> JEvent::Get(const std::string& tag) const {
std::vector<const T*> JEvent::Get(const std::string& tag, bool strict) const {

auto factory = GetFactory<T>(tag, true);
auto factory = GetFactory<T>(tag, strict);
std::vector<const T*> 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<const T*> vec;
for (auto it=iters.first; it!=iters.second; ++it) {
vec.push_back(*it);
}
Expand Down
30 changes: 18 additions & 12 deletions src/libraries/JANA/Omni/JHasInputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(this->names[0]);
m_data = event.Get<T>(this->names[0], !this->is_optional);
}
else {
m_data = event.GetParent(level).template Get<T>(this->names[0]);
if (this->is_optional && !event.HasParent(level)) return;
m_data = event.GetParent(level).template Get<T>(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<T>(name);
event.Get<T>(name, !this->is_optional);
}
else {
event.GetParent(level).template Get<T>(name);
if (this->is_optional && !event.HasParent(level)) return;
event.GetParent(level).template Get<T>(name, !this->is_optional);
}
}
};
Expand All @@ -142,21 +144,23 @@ struct JHasInputs {
auto& level = this->levels[0];
auto& name = this->names[0];
if (level == event.GetLevel() || level == JEventLevel::None) {
m_data = event.GetCollection<PodioT>(name);
m_data = event.GetCollection<PodioT>(name, !this->is_optional);
}
else {
m_data = event.GetParent(level).template GetCollection<PodioT>(name);
if (this->is_optional && !event.HasParent(level)) return;
m_data = event.GetParent(level).template GetCollection<PodioT>(name, !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.GetCollection<PodioT>(name);
event.GetCollection<PodioT>(name, !this->is_optional);
}
else {
event.GetParent(level).template GetCollection<PodioT>(name);
if (this->is_optional && !event.HasParent(level)) return;
event.GetParent(level).template GetCollection<PodioT>(name, !this->is_optional);
}
}
};
Expand Down Expand Up @@ -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<PodioT>(coll_name));
m_data.push_back(event.GetCollection<PodioT>(coll_name, !this->is_optional));
}
else {
m_data.push_back(event.GetParent(level).GetCollection<PodioT>(coll_name));
if (this->is_optional && !event.HasParent(level)) return;
m_data.push_back(event.GetParent(level).GetCollection<PodioT>(coll_name, !this->is_optional));
}
}
}
Expand All @@ -205,10 +210,11 @@ struct JHasInputs {
auto& coll_name = names[i];
auto& level = levels[i];
if (level == event.GetLevel() || level == JEventLevel::None) {
event.GetCollection<PodioT>(coll_name);
event.GetCollection<PodioT>(coll_name, !this->is_optional);
}
else {
event.GetParent(level).GetCollection<PodioT>(coll_name);
if (this->is_optional && !event.HasParent(level)) return;
event.GetParent(level).GetCollection<PodioT>(coll_name, !this->is_optional);
}
}
}
Expand Down

0 comments on commit 32c299c

Please sign in to comment.