# simtools-derive-psf-parameters ```{eval-rst} .. automodule:: simtools.applications.derive_psf_parameters :members: :exclude-members: main ``` ```{eval-rst} This includes parameters mirror_align_random_horizontal and mirror_align_random_vertical. The measured cumulative PSF should be provided by using the command line argument data. \ A file name is expected, in which the file should contain 3 columns: radial distance in mm, \ differential value of photon intensity and its integral value. The derivation is performed through gradient descent optimization that minimizes either the \ Root Mean Squared Deviation (RMSD) between measured and simulated PSF curves (default) or the \ Kolmogorov-Smirnov (KS) statistic when the --ks_statistic flag is used. The optimization workflow includes: * Loading and preprocessing PSF data from measurement files * Running gradient descent optimization to minimize RMSD * Generating cumulative PSF plots for each iteration showing optimization progression * Logging parameter evolution through gradient descent steps * Creating convergence plots showing RMSD and D80 evolution * Automatically generating D80 vs off-axis angle analysis for best parameters * Optionally exporting optimized parameters as simulation model files The assumption are: a) mirror_align_random_horizontal and mirror_align_random_vertical are the same. b) mirror_align_random_horizontal/vertical have no dependence on the zenith angle. One example of the plot generated by this applications are shown below. .. _derive_psf_parameters_plot: .. image:: images/gradient_descent.png :width: 49 % **Command line arguments** site (str, required) North or South. telescope (str, required) Telescope model name (e.g. LST-1, SST-D, ...). model_version (str, optional) Model version. parameter_version (str, optional) Parameter version for model parameter file export. source_distance (float or quantity, optional) Source distance in km. zenith_angle (float or quantity, optional) Zenith angle in deg. data (str, optional) Name of the data file with the measured cumulative PSF. plot_all (activation mode, optional) If activated, plots will be generated for all values tested during tuning. test (activation mode, optional) If activated, application will be faster by simulating fewer photons. write_psf_parameters (activation mode, optional) Write the optimized PSF parameters as simulation model parameter files. rmsd_threshold (float, optional) RMSD threshold for gradient descent convergence (default: 0.007). learning_rate (float, optional) Learning rate for gradient descent optimization (default: 0.01). max_iterations (int, optional) Maximum number of gradient descent iterations (default: 200). monte_carlo_analysis (activation mode, optional) Run Monte Carlo analysis to find statistical uncertainties. **Example** --telescope LSTN-01 --model_version 6.0.0 Run the application: .. code-block:: console simtools-derive-psf-parameters --site North --telescope LSTN-01 \\ --model_version 6.0.0 --data tests/resources/PSFcurve_data_v2.ecsv --plot_all --test Run with parameter export: .. code-block:: console simtools-derive-psf-parameters --site North --telescope LSTN-01 --model_version 7.0.0 \\ --plot_all --test --rmsd_threshold 0.01 --learning_rate 0.001 \\ --data tests/resources/PSFcurve_data_v2.ecsv \\ --write_psf_parameters Run monte carlo analysis: .. code-block:: console simtools-derive-psf-parameters --site North --telescope LSTN-01 --model_version 7.0.0 \\ --plot_all --test --monte_carlo_analysis \\ --data tests/resources/PSFcurve_data_v2.ecsv \\ --write_psf_parameters The output is saved in simtools-output/derive_psf_parameters. Output files include: * Gradient descent progression log in psf_gradient_descent_[telescope].log * Gradient descent convergence plots in gradient_descent_convergence_[telescope].png * PSF progression plots showing evolution through iterations (if --plot_all is specified) * D80 vs off-axis angle plots (d80_vs_offaxis_cm.png, d80_vs_offaxis_deg.png) * Optimized simulation model parameter files (if --write_psf_parameters is specified) ``` Independent finite-difference evaluations within each gradient calculation and Monte Carlo repetitions run concurrently on the selected [execution backend](../execution_backends.md). Gradient-descent iterations remain sequential. The controller runs the initial and candidate-step simulations and creates the final plots, so it also needs the simulation software and model source. HTCondor receives one job per independent evaluation, rather than one job for the whole workflow; scheduler latency can outweigh the benefit for short simulations. Local execution defaults to half the system CPUs, rounded down with a minimum of one worker. Set `--max_workers 1` for serial execution or choose a positive worker count explicitly. Zero or negative values use all system CPUs. This limit controls local processes; HTCondor concurrency is controlled by the scheduler. Each evaluation reconstructs its own model and writes under a unique `parallel_evaluations/` subdirectory. With `--cleanup`, all evaluation files are removed when the batch finishes, including failed evaluations and backend errors. Without it, evaluation files are retained. Final application outputs are preserved. Allow for additional memory and temporary disk use when increasing the number of workers. ## Command line arguments ```{eval-rst} .. simtools-cli-help:: :application: derive_psf_parameters :no-heading: ``` ## Example ```{eval-rst} .. simtools-integration-example:: :file: derive_psf_parameters_run.yml ```