Trapping correction module and app - #184
sophieehaight wants to merge 9 commits into
Conversation
fhagemann
left a comment
There was a problem hiding this comment.
It looks like the units of depth_val (mm) and m_Depths (cm) do not match..
|
Can we also remove |
fhagemann
left a comment
There was a problem hiding this comment.
Here is my feedback from just looking at the code.
I will also try to compile and run both the module and the app to provide more feedback on that as well! :)
commented
Aug 17, 2026
commented
Aug 17, 2026
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. ℹ️ You can also turn on project coverage checks and project coverage reporting on Pull Request comment Thanks for integrating Codecov - We've got you covered ☂️ |
| m_Name = "Trapping Correction"; // - correcting energies for charge trapping (by Sophie); | ||
|
|
||
| // Set the XML tag --- has to be unique --- no spaces allowed | ||
| m_XmlTag = "TrappingCorrection"; |
There was a problem hiding this comment.
Let's be consistent with XML tag naming here (see #193)
| m_XmlTag = "TrappingCorrection"; | |
| m_XmlTag = "XmlTagTrappingCorrection"; |
There was a problem hiding this comment.
I'm actually not seeing this convention in the depth or energy calibration modules? It looks like the XML tag is just the name of the module
There was a problem hiding this comment.
Once I change the name of the xml tag, nuclearizer can no longer recognize the module. Is there somewhere else in assembly I need to update the xml tag? I was looking but I couldn't find anything
There was a problem hiding this comment.
This will affect reading in a nuclearizer config file (*.cfg). In those, you might need to update all occurences of TrappingCorrection with XmlTrappingCorrection.
There was a problem hiding this comment.
So either update the .nuclearizer.cfg in your home directory, or the respective .cfg that you load when running nuclearizer.
| cout << "\n" << m_XmlTag << " --- " << titleLabel << " ---" << endl; | ||
| cout << " Centroid (Mu) : " << mu << " keV" << endl; | ||
| cout << " Fitted Gaussian FWHM : " << fwhm << " keV" << endl; | ||
| cout << " Direct Histogram FWHM: " << directFWHM << " keV" << endl; |
There was a problem hiding this comment.
I use this output to evaluate how well the trapping correction is working. If I change the verbosity to info, the comand line becomes overloaded (I believe w info from the depth cal) and nuclearizer crashes. I agree this info doesn't need to be repeated so I can change the verbosity for the following comments but I do need it read out somewhere.
I think we probably need to clean up the outputs for the info verbosity right now because right now it's outputting so much info that I can't receive the info I'm trying to get from the trapping correction.
There was a problem hiding this comment.
@zoglauer maybe we can introduce the chatty verbosity from megalib also to nuclearizer, and move some of the info messages to chatty?
left a comment
•
There was a problem hiding this comment.
This would be good from my side. Just gathering here the open TODOs (for follow-up PRs):
- Allow for a charge trapping parameters file for multiple detectors, at not just one
- Think about flushing output to
c_Infowithout overloading the output - Maybe find an alternative to
Interpolateto avoid code duplication (maybe from an existing package or another module --> the depth calibration also used some sort of interpolation algorithm to interpolate the CTD-to-depth spline).
| // Evaluate full function FWHM over the fit window (645 keV to 675 keV) | ||
| double xMinFit = 645.0; | ||
| double xMaxFit = 675.0; |
There was a problem hiding this comment.
This seems to be tailored to Cs137. Is this what we want? What if we pass data from other isotopes through MModuleTrappingCorrection?
There was a problem hiding this comment.
I set it up in commits 8f30f9b, b95dc1e, and f191838 (took a couple tries to get my axes to update correctly here). I let the user define the energy of the photopeak they are tryting to fit on the nuclearizer expo. The trapping correction module then takes that input and uses it to define the Mu and min and max energies for the photopeak fits measuring resolutions pre and post trapping correction. I kept the default energy for Cs-137 as a benchmark.
| @@ -605,7 +600,7 @@ TF1* MModuleTrappingCorrection::GeneratePhotopeakFunction() | |||
|
|
|||
| // Provide initial sensible guesses for a Cs137 photopeak | |||
| PhotopeakFunction->SetParameter("Amplitude", 1000); | |||
| PhotopeakFunction->SetParameter("x0 (Mu)", muInitial); | |||
| PhotopeakFunction->SetParameter("x0 (Mu)", 661.7); | |||
| PhotopeakFunction->SetParameter("Sigma Gauss", 2.0); | |||
| PhotopeakFunction->SetParameter("BoverA", 0.05); | |||
| PhotopeakFunction->SetParameter("Gamma", 0.5); | |||
| @@ -615,7 +610,7 @@ TF1* MModuleTrappingCorrection::GeneratePhotopeakFunction() | |||
|
|
|||
| // Set boundary limits to stabilize convergence | |||
| PhotopeakFunction->SetParLimits(0, 1, 1e8); | |||
| PhotopeakFunction->SetParLimits(1, fitMin, fitMax); // Keeps peak centered around Mu | |||
| PhotopeakFunction->SetParLimits(1, 645, 675); // Keeps peak centered around 662 keV | |||
There was a problem hiding this comment.
Your last pushes reverted back to the fixed window around 662keV.
Is this intended?
There was a problem hiding this comment.
my mistake! I was testing the fit with and without the dynamic fitting window. This is fixed in 6321870
commented
Sep 21, 2026
|
I've lost track. Is this ready for a final review? |
There was a problem hiding this comment.
Do you also plan to add the individual detector files to resource/calibration/COSISMEX/unit?
commented
Sep 25, 2026
|
If the last changes conclude this PR, we might want to clean up the commit history and reduce the ~100 commits to a few major commits (adding the module, adding the GUI options, adding example files, ...), as this PR changes only a handful of files and adds a handful to the Happy to help here! |
33d2a05 to
4aba2dc
Compare
| double g_MinCTD = -250; | ||
| double g_MaxCTD = 250; |
There was a problem hiding this comment.
Is there ever a scenario where this range might no be enough?
For payload calibrations, we will have slightly higher temperatures than in unit-level calibrations, resulting in lower charge carrier mobilities, that might push the CTD out of the [-250,250] range (maybe?)
There was a problem hiding this comment.
Perhaps on the AC side we would see CTD >250. On the DC side most detectors can only get the required number of counts in a Cs-137 photopeak at CTD = -200 so it is not an issue. Right now the keeping CTD < +250 allows me to place an upper limit on the CTD value when map depth values to CTD values for the CCE parameterization.
Getting data at CTD >250 is something I want to take a look at in the future but right now we get plenty of counts on that side of the detector. I'll add a TO DO comment for this but for the purposes of this pull request I think we can leave these limits as is.
| MString m_TACCalFile; | ||
| MString m_TACCalibrationFile; |
There was a problem hiding this comment.
What is the difference between TACCalFile and TACCalibrationFile?
Edit: looks like m_TACCalibrationFile is used for what used to be the TAC cut file, so this can go:
| MString m_TACCalFile; | |
| MString m_TACCalibrationFile; | |
| MString m_TACCalFile; |
| Usage<<" --emax: maximum Event energy (default 700 kev)"<<endl; | ||
| Usage<<" -e: energy calibration file (.ecal)"<<endl; | ||
| Usage<<" --tcal: TAC calibration file"<<endl; | ||
| Usage<<" --tcut: TAC cut file"<<endl; |
There was a problem hiding this comment.
I don't think that we need TAC cut files anymore with the latest changes in #194
|
|
||
| // First check if each option has sufficient arguments: | ||
| // Single argument | ||
| if ((Option == "-i") || (Option == "-o") || (Option == "--emin") || (Option == "--emax") || (Option == "--tcal") || (Option == "--tcut") || (Option == "-m") || (Option == "--ctdmin") || (Option == "--ctdmax")) { |
There was a problem hiding this comment.
| if ((Option == "-i") || (Option == "-o") || (Option == "--emin") || (Option == "--emax") || (Option == "--tcal") || (Option == "--tcut") || (Option == "-m") || (Option == "--ctdmin") || (Option == "--ctdmax")) { | |
| if ((Option == "-i") || (Option == "-o") || (Option == "--emin") || (Option == "--emax") || (Option == "--tcal") || (Option == "-m") || (Option == "--ctdmin") || (Option == "--ctdmax")) { |
| if (Option == "--tcut") { | ||
| m_TACCalibrationFile = argv[++i]; | ||
| cout<<"Accepting file name: "<<m_TACCalibrationFile<<endl; | ||
| } | ||
|
|
There was a problem hiding this comment.
| if (Option == "--tcut") { | |
| m_TACCalibrationFile = argv[++i]; | |
| cout<<"Accepting file name: "<<m_TACCalibrationFile<<endl; | |
| } |
| cout << "Creating energy calibrator" << endl; | ||
| EnergyCalibrator = new MModuleEnergyCalibration(); | ||
| EnergyCalibrator->SetFileName(m_EcalFile); | ||
| S->SetModule(EnergyCalibrator, MNumber); | ||
| ++MNumber; |
There was a problem hiding this comment.
Do you require any sort of energy (slow) thresholds to be set here?
There was a problem hiding this comment.
Yes, I set a slow threshold at 30 keV. I had originally had this set locally in the ecal module but I will add it to the app.
| MStripHit* TrappingCorrectionCs137::GetDominantStrip(vector<MStripHit*>& Strips, double& EnergyFraction) | ||
| { | ||
| double MaxEnergy = -numeric_limits<double>::max(); | ||
| double TotalEnergy = 0.0; | ||
| MStripHit* MaxStrip = nullptr; | ||
|
|
||
| // Iterate through strip hits and get the strip with highest energy | ||
| for (const auto SH : Strips) { | ||
| double Energy = SH->GetEnergy(); | ||
| TotalEnergy += Energy; | ||
| if (Energy > MaxEnergy) { | ||
| MaxStrip = SH; | ||
| MaxEnergy = Energy; | ||
| } | ||
| } | ||
| if (TotalEnergy == 0) { | ||
| EnergyFraction = 0; | ||
| } else { | ||
| EnergyFraction = MaxEnergy/TotalEnergy; | ||
| } | ||
| return MaxStrip; | ||
| } |
There was a problem hiding this comment.
This seems like it's an identical copy of MModuleDepthCalibration::GetDominantStrip, which is a public function, so you can probably use that here instead:
- Load
#include "MModuleDepthCalibration.h" - In
Analyze, define (with all other modules)MModuleDepthCalibration* Depth = new MModuleDepthCalibration, maybe with a comment that you're not running the module, just using its functions here. - Call
Depth->GetDominantStripwhere you just haveGetDominantStrip. - Remove this function here (plus its declaration in the class).
| signal(SIGINT, CatchSignal); | ||
|
|
||
| // Initialize global MEGALIB variables, especially mgui, etc. | ||
| MGlobal::Initialize("Standalone", "a standalone example program"); |
There was a problem hiding this comment.
You could be more explicit here:
| MGlobal::Initialize("Standalone", "a standalone example program"); | |
| MGlobal::Initialize("TrappingCorrectionCs137", "A standalone program to determine CTD-dependent shifts of the Cs137 peak"); |
or something like that.
| @@ -0,0 +1,154 @@ | |||
| # A_HV,A_LV,B,C | |||
| 1.0009264205609762,1.00084829470918,1.288689028046797,0.3010529785474152 | |||
| # z_depth_cm,e_CCE_HV,h_CCE_HV,e_CCE_LV,h_CCE_LV | |||
There was a problem hiding this comment.
The example files here in resource/dee and in resource/calibration are a bit inconsistent when it comes to the line defining the headers z_depth (in terms of if they should start with a # or not).
I would prefer the line to always start with a #,
There was a problem hiding this comment.
I guess this file won't work because it doesn't start with ### 0 ?
| @@ -0,0 +1,154 @@ | |||
| ### 0 | |||
| 1.0008508757266585,1.0009808031358887,1.1384578503113774,0.3538332986513531 | |||
| z_depth_cm,e_CCE_HV,h_CCE_HV,e_CCE_LV,h_CCE_LV | |||
There was a problem hiding this comment.
Here and elsewhere in resource/calibration:
| z_depth_cm,e_CCE_HV,h_CCE_HV,e_CCE_LV,h_CCE_LV | |
| # z_depth_cm,e_CCE_HV,h_CCE_HV,e_CCE_LV,h_CCE_LV |
| // Skip text column headers | ||
| if (Line.BeginsWith("z_depth")) continue; |
There was a problem hiding this comment.
This could go if we force the header lines to start with #.
Or is there any reason you chose not to?
5809849 to
ba643dc
Compare
created app to return depth dep centroid energy shift for Cs-137 data
Created charge trapping correction module with GUI options
cleaned up outputs and added a check for events with calibrated timing
Created a trapping correction expo
Added trapping parameter files for unit level as well as dummy payload and single detector files
Record a local position in depth cal to access in trapping correction
changed depth cal functions to public for use in the trapping correction
Created header file missing in previous commit
Created MGUIOptionsTrappingCorrection src file
890d158 to
8a136f6
Compare
The missing MGUIOptionsTrappingCorrection files were added
left a comment
There was a problem hiding this comment.
This is a ton of good work, @sophieehaight !
I've done a review of just the Nuclearizer App for now, I'll try to get through the rest tomorrow.
It looks like there are a few things that you need to update to account for recent merged PRs as identified in Felix's comments and the ones below.
I've run the app, but it took a bit of editing to make things work. Do you usually run with or without the "-n" option for nearest neighbors?
At first I was getting strange results, namely a double peaked spectra.
The double peak goes away when I don't use the -n option, but the resulting photopeak is much broader and shifted higher in energy, similar to what Julian has shown when we add NN strip hits without an appropriate energy threshold in at higher energy).
I then tried with a SlowThresholdCut of 15 keV and the double peak structure goes away. How were you doing this without the SlowThresholdCut?
| //////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
|
|
||
| //! The user interface for the universal energy calibration |
There was a problem hiding this comment.
//! The user interface for the trapping correction
There was a problem hiding this comment.
yes, I am using the -n option to exclude nearest neighbors and I am applying a slowthreshold cut at 30 keV. I had originally just updated my Ecal module locally to apply the slow threshold cut but I am going to code the cut into the trapping app itself when I define the instance of the energy calibration. Will push that version soon!
| /* | ||
| * MGUIOptionsTrappingCorrection.h | ||
| * | ||
| * Copyright (C) by Andreas Zoglauer. |
|
|
||
| if (m_Interrupt == true) return false; | ||
|
|
||
| // [CTD, HV Energy, LV Energy] for full detector values |
There was a problem hiding this comment.
Missing the DetID map in the comment:
// [CTD][DetID][HV Energy, LV Energy] for full detector values
| MString InputFile = FileNames[0]; | ||
| cout << " input file stored as: " << InputFile << endl; | ||
| vector<MString> HDFNames; | ||
|
|
There was a problem hiding this comment.
Add comment:
"// Enable sum of weights squared for TH1 by default"
| EventFilter->SetMinimumHits(0); | ||
| EventFilter->SetMaximumHits(100); | ||
| EventFilter->SetMinimumTotalEnergy(m_MinEnergy); | ||
| EventFilter->SetMaximumTotalEnergy(m_MaxEnergy * 2); |
There was a problem hiding this comment.
Why *2 for the MaxEnergy? Maybe add a comment in the code to explain this logic?
| MStripHit* HVSH = GetDominantStrip(HVStrips, HVEnergyFraction); | ||
| MStripHit* LVSH = GetDominantStrip(LVStrips, LVEnergyFraction); | ||
|
|
||
| if ((LVSH->HasCalibratedTiming() == true) && (HVSH->HasCalibratedTiming() == true) && (LVSH != nullptr) && (HVSH != nullptr)) { |
There was a problem hiding this comment.
HasCalibratedTiming is depreciated after PR #171. See #171 (comment)
I had to change this line to get any data in the histograms.
| if ((LVSH->HasCalibratedTiming() == true) && (HVSH->HasCalibratedTiming() == true) && (LVSH != nullptr) && (HVSH != nullptr)) { | |
| if ((LVSH->HasFastTiming() == true) && (HVSH->HasFastTiming() == true) && (LVSH != nullptr) && (HVSH != nullptr)) { |
| } | ||
| } | ||
|
|
||
| // Place this outside/before your CTD bin and Detector loops! |
There was a problem hiding this comment.
Change this comment to be descriptive of the following functionality. I.e. "// Define an output file to save the photopeak fitting results"
| } | ||
| } | ||
|
|
||
| // Place this at the absolute end of your Analyze() function |
There was a problem hiding this comment.
"// Close output fit parameter file"
|
|
||
| cout << "Creating energy calibrator" << endl; | ||
| EnergyCalibrator = new MModuleEnergyCalibration(); | ||
| EnergyCalibrator->SetFileName(m_EcalFile); |
There was a problem hiding this comment.
I needed to add the following lines to include a SlowThreshold to remove the double peak spectra I was getting
| EnergyCalibrator->SetFileName(m_EcalFile); | |
| EnergyCalibrator->SetFileName(m_EcalFile); | |
| EnergyCalibrator->SetSlowThresholdCutMode(MSlowThresholdCutModes::e_Fixed); | |
| EnergyCalibrator->SetSlowThresholdCutFixedValue(15); |
| if ((InputFile.GetSubString(InputFile.Length() - 4)) == "hdf5") { | ||
| HDFNames.push_back(InputFile); | ||
| cout << "hdf names loaded correctly" << endl; | ||
| } else if ((InputFile.GetSubString(InputFile.Length() - 3)) == "txt") { |
There was a problem hiding this comment.
Are you ever expecting to combing data from different runs?
If this .txt file option is only meant to load multiple files from the same run, then the MModuleLoaderMeasurementsHDF already has the SetLoadContinuationFiles option that should handle this.
There was a problem hiding this comment.
Having the txt file as an input for loading the HDF files allows me to control how many files I'm using to characterize the data. I have more data (30+ files of Cs data) for some of the detectors so I've been using only 20 data files via the txt file option to characterize individual detectors.









Contains a new module for applying depth-based charge trapping correction to individual hit energies along with modules with GUI options and GUI expos. The branch also includes an app for characterizing trapping with Cs-137 data. The trapping correction module requires a csv parameter file as input:
detector_0_trapping_parameters.csv
This file can also be found in resource/dee