RunAndWait vs ApplyAndWait Traces



Currently I have a program in MATLAB ZOS-API that involves a light source that is shining on an array of detector rectangles. The purpose of the for loop is to tilt the X and Y component of the light source then read out the total power from each detector. The problem I am having is how to extract the data efficiently without opening a new detectorviewer each iteration. Currently I am having the for loop run a new ray trace as well as a newanalysis command which I believe is responsible for opening a new detectorviewer each iteration. Ideally the code would just update one detectorviewer or not open one at all to save me computing time. I will attach a snip of my current for loop where I believe the problem resides.


I have tried to follow along to this link: Methods for extracting detector data through the API | Zemax Community… But not much success is being had.


Any and all help would be greatly appreciated!


Best answer by MichaelH 11 July 2024, 17:33

View original

3 replies

Userlevel 7
Badge +2



What about using the Merit Function instead?

Then, in Python, you can do:

# Do whatever changes to the system
TheSystem.NCE.GetObjectAt(1).YPosition = 1.0

# Calculate the Merit function (includes the raytrace through NSTR)

# Get total power of each detector

Total power is specified with the Data column. From the Help File: Total flux in position space for all pixels when Data = 0 and Pixel = 0.

Note that CalculateMeritFunction is synchronous. As the raytrace is run through NSTR, the code execution will wait until it is done. Therefore, it can look like your code is freezing if you trace a lot of ray or have many CAD files. That being said, it is about the same time as if you were to run your raytrace with the tool.

Take care,



Userlevel 6
Badge +2

Hey Travis,

In addition to David’s suggestion, I would also look at Sample Code 8, segments 6-9.  You can directly get the detector data from the NCE without having to open a Detector Viewer analysis.  There are 12 methods in the NCE which can be used for different detector types (the “All” and “AllSafe” return the exact same information, it’s just the “AllSafe” is easier to use with COM languages but can also be used with Matlab):

  • GetCoherentData (same as MFE)
  • GetDetectorData (same as MFE)
  • GetPolarDetectorData (same as MFE)
  • GetColorDetectorData (same as MFE)
  • GetAllCoherentData
  • GetAllCoherentDataSafe
  • GetAllDetectorData
  • GetAllDetectorDataSafe
  • GetAllPolarDetectorData
  • GetAllPolarDetectorDataSafe
  • GetAllColorDetectorData
  • GetAllColorDetectorDataSafe

The only detector this doesn't work with is the Detector Volume...I believe you will need to use the Detector Viewer for volume flux data.



I got both methods working!


Thank you both for your suggestions.