Skip to content

[PR199 update] DEE Shield Readout module - from energy to ADC for the shield hits - #233

Open
vfioretti wants to merge 6 commits into
cositools:develop/emfrom
vfioretti:develop/em
Open

vfioretti wants to merge 6 commits into
cositools:develop/emfrom
vfioretti:develop/em

Conversation

@vfioretti

Copy link
Copy Markdown

This is an update for PR #199, that was closed because of conflicts with the master branch.

This PR modifies the following files:

GUI configuration to read the calibration file:

  • modified: include/MGUIOptionsDEESMEX.h
  • modified: include/MModuleDEESMEX.h
  • modified: src/MGUIOptionsDEESMEX.cxx

I added the ability in MReadOutElementVoxel3D to also map the detector/crystal ID with the detector and crystal ID listed in the calibration file:

  • modified: include/MReadOutElementVoxel3D.h
  • modified: src/MReadOutElementVoxel3D.cxx

new DEE submodule (Shield Readout) to convert from energy to ADC the shield hits:

  • modified: src/MSubModuleShieldReadout.cxx
  • modified: include/MSubModuleShieldReadout.h

A new file (energy to ADC calibration parameters) is added in resource/dee/ShieldEnergyCalibration_NRL.csv

Changes from PR #199:

  • ...ShieldReadoutFilename... moved to ShieldEnergyCalibrationFilename... for clarity
  • dead code removed (see this comment)
  • using false instead of ! (see this comment)

On this comment on include/MReadOutElementVoxel3D.h:
using bool without virtual in
bool operator<(const MReadOutElementVoxel3D& R) const;
is correct, because it only needs to order between two MReadOutElementVoxel3D objects, while
virtual bool operator==(const MReadOutElement& R) const;
should be able to compare any readout element from the derived class.

@vfioretti

Copy link
Copy Markdown
Author

@ckierans this PR replies to the reviewed PR #199 (closed because of conflicts)

@vfioretti
vfioretti marked this pull request as draft November 3, 2026 10:43
@vfioretti
vfioretti marked this pull request as ready for review November 3, 2026 16:41
@vfioretti

vfioretti commented Nov 3, 2026 •

Copy link
Copy Markdown
Author

@ckierans the pull request on the shield readout was updated to include bug correction on the MReadOutElementVoxel3D class. MSubModuleShieldEnergyCorrection.cxx also updated to write more info in the verbose and verify the update.

This pull request solves issues 275 and 279.

Code analysis obtained with chatGPT and Claude in parallel.

Main bugs:

  • operator< doesn’t overrides the base class mReadOutElement because the base has unsigned int m_DetectorID, and MReadOutElementVoxel3D class adds its own MString m_DetectorID
  • operator== returns false for an element compared with itself, if voxel IDs are partly set

Additional corrections aim to simplify the code and none of them change results for valid, fully specified data.

  • Writing out and reading back always works. Default, cleared, crystal-only, partially set and fully specified elements all read back equal to the original. Before, an element with an empty detector ID wrote out a line that Parse rejected.
  • Bad input is rejected. Non-numeric, negative and too-large IDs now make Parse return false, with an error naming the field and token position. Before, they turned into wrong but valid-looking IDs.
  • A failed parse changes nothing. The element keeps its previous values instead of ending up as a mix of old and new data.
  • Detector IDs that can’t be read back are reported. IDs containing whitespace, starting with # or //, or equal to - produce an error when written, instead of shifting fields or being cut off when read back.
  • Error messages are accurate. The too-few-tokens message states the actual requirement (StartElement + 5) instead of “less than 5”.
  • ToString() only formats text. It no longer prints warnings to cout, so logging elements adds no noise.
  • ToString() output is clearer. Undefined IDs show as ?, and partially set voxels are shown, e.g. (2, ?, ?), instead of being hidden. Output for fully specified elements is unchanged.
  • No C++20 warning on operator==. Comparing two voxel elements is no longer ambiguous under C++20. Under C++17 nothing changes.
  • Simpler code. Clone() is one line and copies the whole object, including the base-class part. The dead commented-out operators are gone.

@vfioretti

Copy link
Copy Markdown
Author

@ckierans I added the following commit to the PR: [BUG+ADD] reading D0/D1 plus bug correction on event merging. I updated the MSubModuleDEEIntake.cxx file to:

  • require BRIK only for the GeD
  • add a fake voxel (0,0,0) when reading D0/D1
  • BUG correction: the code was only updating the DetectorID, not the CrystalID of the Crystal Hit class (this line of code was lost in the DEE upgrading). The effect was summing energy deposits in different crystals. Now it seems to worksfine (I verified it using prints in the verbose).

I tested the DEE using the smex v36 mass model, all ok.

NOTE: the energy correction file only contains the X0 panel, but nuclearizer applies a constant default energy correction when it does not find a value from file, so it is not blocking the DEE.

@ckierans ckierans left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice work, Valentina!

The unit tests are failing still after your change to Issue #275 - note at the bottom of the issue it says "Done when: all problems described in this issue are fixed, all ExpectedToFail() instances related to this issue are removed from the unit tests, and the unit tests pass on both macOS and Linux." So you have to update UTNReadOutElementVoxel3D accordingly:

// before
Passed = ExpectedToFail(EvaluateTrue("operator<", "different crystal ID", "...", (A < D2) != (D2 < A))) && Passed;
// after
Passed = EvaluateTrue("operator<", "different crystal ID", "...", (A < D2) != (D2 < A)) && Passed;

In a Claude review of your PR, one suggestion was to change m_DetectorID to m_DetectorName to resolve the shadowing of m_DetectorID with it's base member, as pointed out in issue #275. Meaning that the crystals with their string identifier have a DetectorName instead of a DetectorID in the derived MReadOutElementVoxel3D class. You can make this a smaller change and keep the public facing Set/GetDetectorID(), but internally change the protected members to m_DetectorName to avoid confusion:

   //! Set detector ID as string
   void SetDetectorID(const MString& DetectorID)
   {
-    m_DetectorID = DetectorID;
+    m_DetectorName = DetectorID;
   }
   //! Get detector ID as string
   MString GetDetectorID() const
   {
-    return m_DetectorID;
+    return m_DetectorName;
   }

I also would vote for changing the name "Shield" to "ACS" in almost all of these files. That's the official name of the subsystem, and is especially true considering it's not used only as a Shield! That's an annoying cosmetic change, I know.

Comment on lines 74 to 83
//! Set detector ID as string
void SetDetectorID(const MString& DetectorID)
{
m_DetectorID = DetectorID;
}
//! Get detector ID as string
MString GetDetectorID() const
{
return m_DetectorID;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
//! Set detector ID as string
void SetDetectorID(const MString& DetectorID) { m_DetectorID = DetectorID; }
//! Get detector ID as string
MString GetDetectorID() const { return m_DetectorID; }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Move to single line for easier readability following the convention in other Nuclearizer classes.

Comment on lines 85 to 94
//! Set crystal ID as int
void SetCrystalID(unsigned int CrystalID)
{
m_CrystalID = CrystalID;
}
//! Get crystal ID as int
unsigned int GetCrystalID() const
{
return m_CrystalID;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
}
//! Set crystal ID as int
void SetCrystalID(unsigned int CrystalID) { m_CrystalID = CrystalID; }
//! Get crystal ID as int
unsigned int GetCrystalID() const { return m_CrystalID; }

Comment on lines 108 to 117
//! Set Voxel X ID as int
void SetVoxelXID(unsigned int VoxelXID)
{
m_VoxelXID = VoxelXID;
}
//! Get Voxel X ID as int
unsigned int GetVoxelXID() const
{
return m_VoxelXID;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
//! Set Voxel X ID as int
void SetVoxelXID(unsigned int VoxelXID) { m_VoxelXID = VoxelXID; }
//! Get Voxel X ID as int
unsigned int GetVoxelXID() const { return m_VoxelXID; }

Comment on lines 119 to 128
//! Set Voxel Y ID as int
void SetVoxelYID(unsigned int VoxelYID)
{
m_VoxelYID = VoxelYID;
}
//! Set Voxel Y ID as int
//! Get Voxel Y ID as int
unsigned int GetVoxelYID() const
{
return m_VoxelYID;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
//! Set Voxel Y ID as int
void SetVoxelYID(unsigned int VoxelYID) { m_VoxelYID = VoxelYID; }
//! Get Voxel Y ID as int
unsigned int GetVoxelYID() const { return m_VoxelYID; }

Comment on lines 130 to 139
//! Set Voxel Z ID as int
void SetVoxelZID(unsigned int VoxelZID)
{
m_VoxelZID = VoxelZID;
}
//! Get Voxel Z ID as int
unsigned int GetVoxelZID() const
{
return m_VoxelZID;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
//! Set Voxel Z ID as int
void SetVoxelZID(unsigned int VoxelZID) { m_VoxelZID = VoxelZID; }
//! Get Voxel Z ID as int
unsigned int GetVoxelZID() const { return m_VoxelZID; }

Comment on lines +245 to +254
bool MReadOutElementVoxel3D::IsWritableDetectorID(const MString& DetectorID)
{
const char* C = DetectorID.Data();
if (C[0] == '#' || (C[0] == '/' && C[1] == '/')) return false; // would start a comment in MTokenizer
if (DetectorID == c_EmptyDetectorIDToken) return false; // would be read back as empty
for (; *C != '\0'; ++C) {
if (isspace(static_cast<unsigned char>(*C)) != 0) return false;
}
return true;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Can you make this a bit more readable and follow the coding style in other Nuclearizer module. Claude did this translation for me as an example, but this should be equivalent.

Suggested change
bool MReadOutElementVoxel3D::IsWritableDetectorID(const MString& DetectorID)
{
const char* C = DetectorID.Data();
if (C[0] == '#' || (C[0] == '/' && C[1] == '/')) return false; // would start a comment in MTokenizer
if (DetectorID == c_EmptyDetectorIDToken) return false; // would be read back as empty
for (; *C != '\0'; ++C) {
if (isspace(static_cast<unsigned char>(*C)) != 0) return false;
}
return true;
}
bool MReadOutElementVoxel3D::IsWritableDetectorID(const MString& DetectorID)
{
// MTokenizer treats everything after "#" or "//" as a comment
if (DetectorID.BeginsWith("#") == true || DetectorID.BeginsWith("//") == true) {
return false;
}
// "-" is reserved as the placeholder for an empty detector ID
if (DetectorID == c_EmptyDetectorIDToken) {
return false;
}
// Whitespace would split the ID into several tokens
for (unsigned int i = 0; i < DetectorID.Length(); ++i) {
const unsigned char Character = DetectorID.Data()[i];
if (isspace(Character) != 0) {
return false;
}
}
return true;
}

Comment on lines +260 to +275
bool MReadOutElementVoxel3D::ParseUnsignedInt(const MString& Token, unsigned int& Value)
{
const char* C = Token.Data();
if (*C == '\0') return false;

unsigned long long V = 0;
for (; *C != '\0'; ++C) {
if (*C < '0' || *C > '9') return false;
V = 10*V + static_cast<unsigned long long>(*C - '0');
if (V > numeric_limits<unsigned int>::max()) return false;
}

Value = static_cast<unsigned int>(V);
return true;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same comment about readability as above.

CH.m_ADC = 2000 + 4*CH.m_Energy;
if (CH.m_ADC > 16383) CH.m_ADC = 16383;
for (MDEECrystalHit& CrystalHit: Hits) {
CrystalHit.m_ADC = GetADC(CrystalHit.m_Energy, CrystalHit.m_DetectorID, CrystalHit.m_CrystalID);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

m_ADC is an unsigned int, but here GetADC returns a double. I think you should make a new variable double ADC = GetADC(.... and then do the max/min checks and then save CrystalHit.m_ADC as an unsigned int casted/rounded version of ADC.

////////////////////////////////////////////////////////////////////////////////


bool MSubModuleShieldReadout::ParseShieldReadoutFile()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Eventually, once we have the ACS energy calibration in the forward pipeline, we might move this chunk of code into the (currently non-existant) MModuleACSEnergyCalibration module to match what we do in the GeD pipeline, but that is okay here now.


MSubModuleShieldEnergyCorrection::MSubModuleShieldEnergyCorrection()
: MSubModule(),
m_Random(0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'm pretty sure the 4th issue identified in #279 is still present. Remove m_Random(0) here and use TRandom.h ROOT library in this file and use gRandom directly.

// before
double CorrectedEnergy = m_Random.Gaus(ShieldCorrectedCentroid, ShieldSigma);

// after
// Use ROOT's global generator gRandom, which MAssembly seeds, so that DEE runs are reproducible
double CorrectedEnergy = gRandom->Gaus(ShieldCorrectedCentroid, ShieldSigma);

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