Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust JFactory::Create logic #383

Merged
merged 1 commit into from
Nov 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 52 additions & 18 deletions src/libraries/JANA/JFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,62 @@ void JFactory::Create(const std::shared_ptr<const JEvent>& event) {
DoInit();
}

auto src = event->GetJEventSource();
if (!TestFactoryFlag(REGENERATE) && src != nullptr && src->IsGetObjectsEnabled()) {
// Attempt to obtain factory data via source->GetObjects(). This will eventually be deprecated,
// but for now we want it for migration purposes. If GetObjects() is not implemented, the default
// implementation returns false with a minimal performance penalty.
bool found_data = false;

CallWithJExceptionWrapper("JEventSource::GetObjects", [&](){
found_data = src->GetObjects(event, this); });

if (found_data) {
mStatus = Status::Inserted;
mCreationStatus = CreationStatus::InsertedViaGetObjects;
return;
// How do we obtain our data? The priority is as follows:
// 1. JFactory::Process() if REGENERATE flag is set
// 2. JEvent::Insert()
// 3. JEventSource::GetObjects() if source has GetObjects() enabled
// 4. JFactory::Process()

// ---------------------------------------------------------------------
// 1. JFactory::Process() if REGENERATE flag is set
// ---------------------------------------------------------------------

if (TestFactoryFlag(REGENERATE)) {
if (mStatus == Status::Inserted) {
// Status::Inserted indicates that the data came from either src->GetObjects() or evt->Insert()
ClearData();
// ClearData() resets mStatus to Unprocessed so that the data will be regenerated exactly once.
}
// After this point, control flow falls through to "4. JFactory::Process"
}
else {

// ---------------------------------------------------------------------
// 2. JEvent::Insert()
// ---------------------------------------------------------------------

if (mStatus == Status::Inserted) {
// This may include data cached from eventsource->GetObjects().
// Either way, short-circuit here, because the data is present.
return;
}

// ---------------------------------------------------------------------
// 3. JEventSource::GetObjects() if source has GetObjects() enabled
// ---------------------------------------------------------------------

if (TestFactoryFlag(REGENERATE) && (mStatus == Status::Inserted)) {
ClearData();
// ClearData will reset mStatus to Status::Unprocessed
auto src = event->GetJEventSource();
if (src != nullptr && src->IsGetObjectsEnabled()) {
bool found_data = false;

CallWithJExceptionWrapper("JEventSource::GetObjects", [&](){
found_data = src->GetObjects(event, this); });

if (found_data) {
mStatus = Status::Inserted;
mCreationStatus = CreationStatus::InsertedViaGetObjects;
return;
}
}
// If neither "2. JEvent::Insert()" nor "3. JEventSource::GetObjects()" succeeded, fall through to "4. JFactory::Process()"
}

// ---------------------------------------------------------------------
// 4. JFactory::Process()
// ---------------------------------------------------------------------

// If the data was Processed (instead of Inserted), it will be in cache, and we can just exit.
// Otherwise we call Process() to create the data in the first place.
if (mStatus == Status::Unprocessed) {
auto run_number = event->GetRunNumber();
if (mPreviousRunNumber != run_number) {
Expand All @@ -61,7 +95,7 @@ void JFactory::Create(const std::shared_ptr<const JEvent>& event) {

void JFactory::DoInit() {
if (mStatus != Status::Uninitialized) {
throw JException("Attempted to initialize a JFactory that has already been initialized!");
return;
}
for (auto* parameter : m_parameters) {
parameter->Configure(*(m_app->GetJParameterManager()), m_prefix);
Expand Down
Loading