Skip to content

Commit 3980848

Browse files
committed
Fix problems with JHasInputs
1 parent e6f109e commit 3980848

File tree

3 files changed

+52
-8
lines changed

3 files changed

+52
-8
lines changed

src/libraries/JANA/Components/JHasInputs.cc

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,30 @@ namespace jana::components {
1010

1111
JFactorySet* GetFactorySetAtLevel(const JEvent& event, JEventLevel desired_level) {
1212

13-
// FIXME: event.GetParent() will except if parent is missing, disregarding 'is_optional'
1413
if (desired_level == JEventLevel::None || desired_level == event.GetLevel()) {
1514
return event.GetFactorySet();
1615
}
17-
return event.GetParent(desired_level).GetFactorySet();
16+
if (event.HasParent(desired_level)) {
17+
return event.GetParent(desired_level).GetFactorySet();
18+
}
19+
return nullptr;
20+
}
21+
22+
void FactoryCreate(const JEvent& event, JFactory* factory) {
23+
factory->Create(event);
1824
}
1925

26+
2027
JHasInputs::InputBase::~InputBase() {};
2128

2229
JHasInputs::VariadicInputBase::~VariadicInputBase() {};
2330

2431
void JHasInputs::InputBase::TriggerFactoryCreate(const JEvent& event) {
2532
auto facset = GetFactorySetAtLevel(event, m_level);
26-
if (facset == nullptr && !m_is_optional) {
33+
if (facset == nullptr) {
34+
if (m_is_optional) {
35+
return;
36+
}
2737
throw JException("Could not find parent at level=" + toString(m_level));
2838
}
2939
auto databundle = facset->GetDatabundle(m_type_index, m_databundle_name);
@@ -41,7 +51,10 @@ void JHasInputs::InputBase::TriggerFactoryCreate(const JEvent& event) {
4151

4252
void JHasInputs::VariadicInputBase::TriggerFactoryCreate(const JEvent& event) {
4353
auto facset = GetFactorySetAtLevel(event, m_level);
44-
if (facset == nullptr && !m_is_optional) {
54+
if (facset == nullptr) {
55+
if (m_is_optional) {
56+
return;
57+
}
4558
throw JException("Could not find parent at level=" + toString(m_level));
4659
}
4760
if (!m_realized_databundle_names.empty()) {

src/libraries/JANA/Components/JHasInputs.h

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace jana::components {
2020

2121
// Free function in order to break circular dependence on JEvent
2222
JFactorySet* GetFactorySetAtLevel(const JEvent& event, JEventLevel desired_level);
23+
void FactoryCreate(const JEvent& event, JFactory* factory);
2324

2425
struct JHasInputs {
2526
protected:
@@ -209,6 +210,12 @@ struct JHasInputs {
209210
// JFactory in the same JFactorySet, though.
210211

211212
auto facset = GetFactorySetAtLevel(event, m_level);
213+
if (facset == nullptr) {
214+
if (m_is_optional) {
215+
return;
216+
}
217+
throw JException("Could not find parent at level=" + toString(m_level));
218+
}
212219
auto databundle = facset->GetDatabundle(std::type_index(typeid(T)), m_tag);
213220
if (databundle == nullptr) {
214221
if (!m_is_optional) {
@@ -273,6 +280,12 @@ struct JHasInputs {
273280

274281
void Populate(const JEvent& event) {
275282
auto facset = GetFactorySetAtLevel(event, m_level);
283+
if (facset == nullptr) {
284+
if (m_is_optional) {
285+
return;
286+
}
287+
throw JException("Could not find parent at level=" + toString(m_level));
288+
}
276289
auto databundle = facset->GetDatabundle(std::type_index(typeid(PodioT)), m_databundle_name);
277290
if (databundle == nullptr) {
278291
if (!m_is_optional) {
@@ -283,6 +296,9 @@ struct JHasInputs {
283296
m_data = nullptr;
284297
return;
285298
};
299+
if (databundle->GetFactory() != nullptr) {
300+
FactoryCreate(event, databundle->GetFactory());
301+
}
286302
auto* typed_databundle = dynamic_cast<JPodioDatabundle*>(databundle);
287303
if (typed_databundle == nullptr) {
288304
facset->Print();
@@ -335,7 +351,10 @@ struct JHasInputs {
335351
void Populate(const JEvent& event) {
336352
m_datas.clear();
337353
auto facset = GetFactorySetAtLevel(event, m_level);
338-
if (facset == nullptr && !m_is_optional) {
354+
if (facset == nullptr) {
355+
if (m_is_optional) {
356+
return;
357+
}
339358
throw JException("Could not find parent at level=" + toString(m_level));
340359
}
341360
if (!m_requested_databundle_names.empty()) {
@@ -350,6 +369,9 @@ struct JHasInputs {
350369
m_datas.push_back({}); // If a databundle is optional and missing, we still insert an empty vector for it
351370
continue;
352371
};
372+
if (databundle->GetFactory() != nullptr) {
373+
FactoryCreate(event, databundle->GetFactory());
374+
}
353375
auto* typed_databundle = dynamic_cast<JLightweightDatabundleT<T>*>(databundle);
354376
if (typed_databundle == nullptr) {
355377
facset->Print();
@@ -424,7 +446,10 @@ struct JHasInputs {
424446

425447
void Populate(const JEvent& event) {
426448
auto facset = GetFactorySetAtLevel(event, m_level);
427-
if (facset == nullptr && !m_is_optional) {
449+
if (facset == nullptr) {
450+
if (m_is_optional) {
451+
return;
452+
}
428453
throw JException("Could not find parent at level=" + toString(m_level));
429454
}
430455
bool need_dynamic_realized_databundle_names = (m_requested_databundle_names.empty()) && (m_empty_input_policy != EmptyInputPolicy::IncludeNothing);
@@ -443,6 +468,9 @@ struct JHasInputs {
443468
m_datas.push_back(nullptr);
444469
continue;
445470
}
471+
if (databundle->GetFactory() != nullptr) {
472+
FactoryCreate(event, databundle->GetFactory());
473+
}
446474
auto* typed_databundle = dynamic_cast<JPodioDatabundle*>(databundle);
447475
if (typed_databundle == nullptr) {
448476
facset->Print();
@@ -459,6 +487,9 @@ struct JHasInputs {
459487
else if (m_empty_input_policy == EmptyInputPolicy::IncludeEverything) {
460488
auto databundles = facset->GetDatabundles(std::type_index(typeid(PodioT)));
461489
for (auto* databundle : databundles) {
490+
if (databundle->GetFactory() != nullptr) {
491+
FactoryCreate(event, databundle->GetFactory());
492+
}
462493
auto typed_databundle = dynamic_cast<JPodioDatabundle*>(databundle);
463494
if (typed_databundle == nullptr) {
464495
throw JException("Not a JPodioDatabundle: type_name=%s, unique_name=%s", databundle->GetTypeName().c_str(), databundle->GetUniqueName().c_str());

src/libraries/JANA/JEvent.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ void JEvent::InsertCollection(typename T::collection_type&& collection, std::str
536536
auto* bundle = mFactorySet.GetDatabundle("podio::Frame");
537537

538538
if (bundle == nullptr) {
539-
LOG << "No frame databundle found, inserting new dummy JFactoryT.";
539+
//LOG << "No frame databundle found, inserting new dummy JFactoryT.";
540540
frame = new podio::Frame();
541541
Insert(frame, "");
542542
// Eventually we'll insert a databundle directly without the dummy JFactoryT
@@ -550,7 +550,7 @@ void JEvent::InsertCollection(typename T::collection_type&& collection, std::str
550550
throw JException("Databundle with unique_name 'podio::Frame' is not a JLightweightDatabundleT");
551551
}
552552
if (typed_bundle->GetSize() == 0) {
553-
LOG << "Found typed bundle with no frame. Creating new frame.";
553+
//LOG << "Found typed bundle with no frame. Creating new frame.";
554554
typed_bundle->GetData().push_back(new podio::Frame);
555555
typed_bundle->SetStatus(JDatabundle::Status::Inserted);
556556
}

0 commit comments

Comments
 (0)