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

Fix bug in LoadNGEM where Min/MaxEventsPerFrame were not respected #38564

Merged
merged 5 commits into from
Jan 16, 2025

Conversation

RichardWaiteSTFC
Copy link
Contributor

@RichardWaiteSTFC RichardWaiteSTFC commented Jan 6, 2025

Description of work

Added loop to clear events in frame if the current frame is not good (i.e. number of events outside Min/Max limits set) in preparation for next frame (don't know if that is the best way to do it)..

The issue was here

if (eventCountInFrame >= minEventsReq && eventCountInFrame <= maxEventsReq) {
// Add number of event counts to workspace.
frameEventCounts.emplace_back(eventCountInFrame);
++goodFrames;
PARALLEL_FOR_NO_WSP_CHECK()
// Add events that match parameters to workspace
for (auto i = 0; i < NUM_OF_SPECTRA; ++i) {
if (eventsInFrame[i].getNumberEvents() > 0) {
events[i] += eventsInFrame[i];
eventsInFrame[i].clear();
}
}
}
}

If the statement on L74 is false then eventsInFrame doesn't get cleared - such that the sum over all eventsInFrame[i].getNumberEvents() is not equal to eventCountInFrame

Fixes #38558

Report to: draspi

To test:

(1) Follow instructions on the issue
(2) For MinEventsPerFrame=0 and MaxEventsPerFrame=0 the workspace should have no counts (ws.getNumberEvents() = 0)
(3) If MinEventsPerFrame and MaxEventsPerFrame are not set then the resulting figure should look like the one in the issue.

Reviewer

Please comment on the points listed below (full description).
Your comments will be used as part of the gatekeeper process, so please comment clearly on what you have checked during your review. If changes are made to the PR during the review process then your final comment will be the most important for gatekeepers. In this comment you should make it clear why any earlier review is still valid, or confirm that all requested changes have been addressed.

Code Review

  • Is the code of an acceptable quality?
  • Does the code conform to the coding standards?
  • Are the unit tests small and test the class in isolation?
  • If there is GUI work does it follow the GUI standards?
  • If there are changes in the release notes then do they describe the changes appropriately?
  • Do the release notes conform to the release notes guide?

Functional Tests

  • Do changes function as described? Add comments below that describe the tests performed?
  • Do the changes handle unexpected situations, e.g. bad input?
  • Has the relevant (user and developer) documentation been added/updated?

Does everything look good? Mark the review as Approve. A member of @mantidproject/gatekeepers will take care of it.

Gatekeeper

If you need to request changes to a PR then please add a comment and set the review status to "Request changes". This will stop the PR from showing up in the list for other gatekeepers.

In preparation for next frame
@RichardWaiteSTFC RichardWaiteSTFC added Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) ISIS Team: Core Issue and pull requests managed by the Core subteam at ISIS labels Jan 6, 2025
@RichardWaiteSTFC RichardWaiteSTFC added this to the Release 6.12 milestone Jan 6, 2025
@RichardWaiteSTFC RichardWaiteSTFC marked this pull request as draft January 6, 2025 16:54
The condition ++numWordsSkipped is always true, but it will only get evaluated if !file.seekg(SKIP_WORD_SIZE, std::ios_base::cur).eof() is true which will indicate a word being skipped, so this does actually have a function!
@RichardWaiteSTFC RichardWaiteSTFC marked this pull request as ready for review January 8, 2025 10:01
@cailafinn cailafinn self-assigned this Jan 8, 2025
Copy link
Contributor

@cailafinn cailafinn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Working well, the correct counts are now shown in the output. Just a quick question about one of the changes from the CppCheck suppressions.

Comment on lines 281 to 300
} while (!event.generic.check() && !file.seekg(SKIP_WORD_SIZE, std::ios_base::cur).eof() && ++numWordsSkipped);
} while (!event.generic.check() && !file.seekg(SKIP_WORD_SIZE, std::ios_base::cur).eof());
if (file.eof()) {
++numWordsSkipped;
break; // we have either not read an event, or only read part of one
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these are equivalent. The numWordsSkipped variable indicates the number of words (multiples of 4 bytes) that were skipped at the beginning of the file before the algorithm found a valid event. I think this will just mark a skipped word whenever the end of a file is reached.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep sorry, I think I'm confused by this
!file.seekg(SKIP_WORD_SIZE, std::ios_base::cur).eof()
would this terminate the loop if the current position in the file is within SKIP_WROD_SIZE of the end of the file?
i.e. would the do/while loop end before eof?

Copy link
Contributor Author

@RichardWaiteSTFC RichardWaiteSTFC Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked at the docs for seekg and am none the wiser! Not had to do much file IO in C++!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So its main check is if it was able to load an event into the EventUnion object of some number of words (4 bytes) using event.generic.check(). If it could do that, it exits the loop. If it couldn't create a valid event, it checks if it can skip 1 word forward file.seekg(SKIP_WORD_SIZE, std::ios_base::cur).eof() and exits the loop if it would be unable to progress because you reached the end of the file. Otherwise, it increments the number of skipped words and then restarts the loop to try to create an event object from the next word. It repeats this until it is able to create an valid event object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah OK I see thanks, so the counter ++numWordsSkipped represents the number of times file.seekg(SKIP_WORD_SIZE, std::ios_base::cur) has been run without reaching the end of the file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think my last commit is now equivalent - seems a bit clunky (maybe could be improved) - but also happy to revert and keep the suppression. whatever you prefer!

@RichardWaiteSTFC RichardWaiteSTFC force-pushed the 38558_LoadNGEM_not_respecting_min_max_events branch from af088c8 to 77aae45 Compare January 14, 2025 13:58
Copy link
Contributor

@cailafinn cailafinn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change looks good and this is probably easier to read than the original style, just a nitpick (sorry) on the variable naming:

Comment on lines 284 to 285
bool is_event_invalid = true;
bool is_not_eof_after_skip = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These want to be in camelCase as they're C++ variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, sorry too much time with python! Force pushed to fix this

@RichardWaiteSTFC RichardWaiteSTFC force-pushed the 38558_LoadNGEM_not_respecting_min_max_events branch from 77aae45 to 45eeeac Compare January 16, 2025 09:49
Copy link
Contributor

@cailafinn cailafinn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me now. Min/Max now properly working as expected.

@thomashampson thomashampson self-assigned this Jan 16, 2025
@thomashampson thomashampson merged commit fc0059c into main Jan 16, 2025
10 checks passed
@thomashampson thomashampson deleted the 38558_LoadNGEM_not_respecting_min_max_events branch January 16, 2025 14:06
peterfpeterson pushed a commit to peterfpeterson/mantid that referenced this pull request Jan 16, 2025
This is a squashed version of mantidproject#38564

Clear events in frame if not good frame

In preparation for next frame

Update cpp check suppression

The condition ++numWordsSkipped is always true, but it will only get evaluated if !file.seekg(SKIP_WORD_SIZE, std::ios_base::cur).eof() is true which will indicate a word being skipped, so this does actually have a function!

Update test to assert no events if min=max=0

Add release note

Fix cpp check supression
peterfpeterson pushed a commit to peterfpeterson/mantid that referenced this pull request Jan 16, 2025
This is a squashed version of mantidproject#38564

Clear events in frame if not good frame

In preparation for next frame

Update cpp check suppression

The condition ++numWordsSkipped is always true, but it will only get evaluated if !file.seekg(SKIP_WORD_SIZE, std::ios_base::cur).eof() is true which will indicate a word being skipped, so this does actually have a function!

Update test to assert no events if min=max=0

Add release note

Fix cpp check supression
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Issues and pull requests that are regressions or would be considered a bug by users (e.g. crashing) ISIS Team: Core Issue and pull requests managed by the Core subteam at ISIS
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LoadNGEM not respecting Min/MaxEventsPerFrame
3 participants