Skip to content

ADD: First set of nuclearizer unit tests#150

Merged
zoglauer merged 11 commits into
cositools:develop/emfrom
zoglauer:feature/unittests
Jun 1, 2026
Merged

ADD: First set of nuclearizer unit tests#150
zoglauer merged 11 commits into
cositools:develop/emfrom
zoglauer:feature/unittests

Conversation

@zoglauer

@zoglauer zoglauer commented May 19, 2026

Copy link
Copy Markdown
Collaborator

This PR adds the first structured nuclearizer unit-test suite and the required test data (in resource/unittestdata). It covers core readout classes (MStripHit, MHit, MReadOutAssembly) plus one module for testing, MModuleLoaderMeasurementsHDF. It also adds end-to-end tests for the 542-1 and 406-1 datasets.

First, you need to update MEGAlib to the latest version, since I offloaded some added functionality into the best suited MEGAlib base classes.

Then, you compile nuclearzier and the unit tests via

make unittests

This creates a bunch of files which start via "UTN*", e.g. UTNStripHit. Those are the unit tests.
You can run them individually, e.g.,

UTNStripHit

or all together via:

UTNRunner

Try it out!

There are 4 focus areas for the review:
(1) I did a bug fix passes on MReadOutAssembly, MStripHit, and MHit. I did not change the style, and ignored that our StreamDat functionality is a mess. I will fix that next after this initial review. There are lots of changes, in those classes - but their where mostly minor issues, and the output of the unit tests is unchanged
(2)
The test data is in resource/unittests. Please feel free to take a look what we have so far - we need lots more in the future.
(3)
The unit tests live in the directory unittests. All the tests are AI generated. I did some course review, and had the AI correct some stuff. You can take a quick look for completeness (and nonsense - which gets generated too - but I hope I found all).
(4)
The unit-test directory has a file called TestingConventions.md. The first part tells you how to prompt an AI agent to make more unit tests, and the second part are instructions from an AI agent for an AI agent on how to make the tests.

@zoglauer zoglauer requested review from ckierans and fhagemann May 19, 2026 22:53
@fhagemann

fhagemann commented May 21, 2026

Copy link
Copy Markdown

EDIT: Never mind, this was resolved by updating megalib.. Should've read your initial message more carefully..

I will look at this now.
Maybe first comment: when I run make unittests, I'm already getting warnings and errors:

image

@zoglauer

Copy link
Copy Markdown
Collaborator Author

Did you update MEAGlib?

@fhagemann

fhagemann commented May 21, 2026

Copy link
Copy Markdown

I did now, and it works... would have helped if I had fully read your initial message 😅 Sorry for the fuzz..

@fhagemann fhagemann left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A had a quick look over all files, and especially for the .cxx files in unittests, I only browsed through them instead of reading them line by line.

Regarding your comments:
(1) The bug fixes look fine --> see my comments in those files if there are any
(2) Test data also looks fine, except for some .cfg files that have paths that link to some files other than this test data. I believe that shouldn't be an issue though
(3) These unit level tests are VERY detailed ^^ good thing we have AI helping us out here. I think I had one comment on adding tests for reading TI from ROA files and making sure that we get the time assignment to m_EventTimeUTC correct. Also I was wondering why we use sometimes 0u and sometimes (unsigned int) 0
(4) I actually read through the TestingConventions.md in a bit more detail and I think I also learned quite a number of new things from the C++ testing cosmos and good practice for writing unit tests. I recommend this for others to also have a look if they have time ;)

Comment thread resource/unittestdata/406-1/hp52406-1.revan.cfg Outdated
Comment thread resource/unittestdata/406-1/hp52406-1.simplified.geo.setup Outdated
Comment thread resource/unittestdata/406-1/hp52406-1.stripmap.map
Comment thread resource/unittestdata/406-1/hp52406-1.taccut.csv
Comment thread resource/unittestdata/542-1/hdf5_to_roa.nuclearizer.cfg Outdated
Comment thread unittests/UTNReadOutAssembly.cxx
Comment thread unittests/UTNReadOutAssembly.cxx
Comment thread unittests/UTNRunner.cxx
Comment thread unittests/UTNStripHit.cxx
Comment on lines +157 to +162
// IsXStrip is an alias for IsLowVoltageStrip
H.IsXStrip(true);
Passed = EvaluateTrue("IsXStrip(bool)/IsXStrip()", "alias true", "IsXStrip() returns true after IsXStrip(true)", H.IsXStrip()) && Passed;
Passed = EvaluateTrue("IsXStrip(bool)/IsLowVoltageStrip()", "alias true", "IsLowVoltageStrip() returns true after IsXStrip(true)", H.IsLowVoltageStrip()) && Passed;
H.IsXStrip(false);
Passed = EvaluateFalse("IsXStrip(bool)/IsXStrip()", "alias false", "IsXStrip() returns false after IsXStrip(false)", H.IsXStrip()) && Passed;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Will IsXStrip be discontinued at some point?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

yes... we have to do more cleanup in this class

Comment thread unittests/UTNReadOutAssembly.cxx
@fhagemann

Copy link
Copy Markdown

I also ran some of the tests. Do you expect some of them to fail?
image

@zoglauer

Copy link
Copy Markdown
Collaborator Author

There was an issue with the tests on Linux which did not show up on macOS. Fixed. They all pass now.

@zoglauer

Copy link
Copy Markdown
Collaborator Author

I think I fixed all your suggestion except the style fixes, which are the next pass over the classes.
Can you check again?

@fhagemann

fhagemann commented May 26, 2026

Copy link
Copy Markdown

For me, the UTNReadOutAssembly tests fail and seem to trigger an massert in megalib:
https://github.com/zoglauer/megalib/blob/31298310d572e23a348c731eda0b8637abebde7a/src/global/misc/src/MPhysicalEvent.cxx#L304

image

@fhagemann

Copy link
Copy Markdown

I also resolved all of my comments (except for the clean-up ones for reference)

Comment on lines +1212 to +1216
MReadOutAssembly R;
R.SetID(10);
MPhysicalEvent* PE = new MPhysicalEvent();
R.SetPhysicalEvent(PE);
delete PE; // SetPhysicalEvent duplicated it, so the caller still owns PE

@fhagemann fhagemann May 26, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This MPhysicalEvent has m_EventType == -1 (Unknown), causing my unit tests to fail.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed it in MEGAlib

Comment on lines +1272 to +1273
Passed = EvaluateTrue("StreamRoa()", "UH line present with strip hit", "StreamRoa() output contains 'UH' when there is a strip hit",
S.Contains("UH")) && Passed;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Related to #156: should only MStripHits start their line with UH?

Comment on lines +1361 to +1372
R.SetID(6);
MCrystalHit* CH = new MCrystalHit();
CH->SetCrystalID(2);
CH->AddOrigins({77});
R.AddCrystalHit(CH);

// WithOrigins=false: the crystal hit origin 77 must not appear
ostringstream SS_no;
R.StreamRoa(SS_no, true, true, false, false, false, false, false, false);
MString S_no = SS_no.str();
Passed = EvaluateTrue("StreamRoa()", "crystal WithOrigins=false omits origins", "StreamRoa() with WithOrigins=false does not emit the crystal hit origin 77",
S_no.Contains("77") == false) && Passed;

@fhagemann fhagemann May 27, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can we also check here for MCrystalHit if the line starts with "UH" (or "UC" in case that #156 is the way it was intended)?

@zoglauer

Copy link
Copy Markdown
Collaborator Author

I fixed the crash issue and on other issue in MEGAlib.
Thus please update MEGAlib, and then hopefully it works now.

@fhagemann

Copy link
Copy Markdown

With the newest version of MEGAlib, I don't get that error anymore, and all tests pass now:
image

@zoglauer zoglauer merged commit b26ca66 into cositools:develop/em Jun 1, 2026
@zoglauer

zoglauer commented Jun 1, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks, Felix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants