@@ -267,7 +267,7 @@ Ricom::Ricom() : stem_max(-FLT_MAX), stem_min(FLT_MAX),
267267 p_prog_mon(nullptr ),
268268 b_busy(false ),
269269 update_offset(true ),
270- b_vSTEM(false ), b_e_mag(false ), b_plot_cbed(true ), b_plot2SDL(false ),
270+ b_vSTEM(false ), b_e_mag(false ), b_plot_cbed(true ), cbed_cmap_frames( 1 ), b_plot2SDL(false ),
271271 b_recompute_detector(false ), b_recompute_kernel(false ),
272272 detector(),
273273 kernel(),
@@ -576,12 +576,12 @@ void Ricom::plot_cbed(std::vector<T> *cbed_data)
576576 }
577577
578578 float v_rng = v_max - v_min;
579- for (int ix = 0 ; ix < camera.ny_cam ; ix ++)
579+ for (int iy = 0 ; iy < camera.ny_cam ; iy ++)
580580 {
581- int iy_t = camera.v [ix ] * camera.nx_cam ;
582- for (int iy = 0 ; iy < camera.nx_cam ; iy ++)
581+ int iy_t = camera.v [iy ] * camera.nx_cam ;
582+ for (int ix = 0 ; ix < camera.nx_cam ; ix ++)
583583 {
584- float vl_f = cbed_log[iy_t + camera.u [iy ]];
584+ float vl_f = cbed_log[iy_t + camera.u [ix ]];
585585 float val = (vl_f - v_min) / v_rng;
586586 SDL_Utils::draw_pixel (srf_cbed, ix, iy, val, cbed_cmap);
587587 }
@@ -862,21 +862,28 @@ void Ricom::process_data(CAMERA::Camera<CameraInterface, CAMERA::EVENT_BASED> *c
862862
863863 while (true )
864864 {
865- // process two frames before the probe position to avoid toa problem
865+ // Process two frames behind to ensure events are accumulated
866+ // (The event reader handles out-of-order events with its own margin)
866867 int idxx = prog_mon.fr_count - nxy * img_num - 2 ;
867868
868869 ++prog_mon;
869870 fr_count = prog_mon.fr_count ;
870871
871- if (acc_cbed < 3 && b_plot_cbed )
872+ if (b_plot_cbed && acc_cbed < cbed_cmap_frames )
872873 {
874+ // Reset frame only when starting new accumulation window
875+ if (acc_cbed == 0 )
876+ {
877+ frame.assign (camera_spec->nx_cam * camera_spec->ny_cam , 0 );
878+ acc_idx = fr_count;
879+ }
873880 camera_spec->read_frame_com_cbed (prog_mon.fr_count ,
874881 dose_map, sumx_map, sumy_map,
875882 stem_data, b_vSTEM,
876883 offset, detector.radius2 ,
877- frame, acc_idx,
884+ frame, acc_idx, cbed_cmap_frames,
878885 first_frame, end_frame);
879- acc_cbed += 1 ;
886+ acc_cbed++ ;
880887 }
881888 else
882889 {
@@ -889,15 +896,17 @@ void Ricom::process_data(CAMERA::Camera<CameraInterface, CAMERA::EVENT_BASED> *c
889896
890897 if (idxx >= 0 )
891898 {
899+
892900 if (dose_map[idxx] == 0 )
893901 {
894902 com_xy[0 ] = offset[0 ];
895903 com_xy[1 ] = offset[1 ];
896904 }
897905 else
898906 {
899- com_xy[0 ] = sumx_map[idxx] / dose_map[idxx];
900- com_xy[1 ] = sumy_map[idxx] / dose_map[idxx];
907+ // Cast to float before division to preserve fractional precision
908+ com_xy[0 ] = static_cast <float >(sumx_map[idxx]) / static_cast <float >(dose_map[idxx]);
909+ com_xy[1 ] = static_cast <float >(sumy_map[idxx]) / static_cast <float >(dose_map[idxx]);
901910 }
902911 com_map_x[idxx] = com_xy[0 ];
903912 com_map_y[idxx] = com_xy[1 ];
@@ -927,9 +936,8 @@ void Ricom::process_data(CAMERA::Camera<CameraInterface, CAMERA::EVENT_BASED> *c
927936 update_surfaces (iy, p_frame);
928937 if (b_plot_cbed)
929938 {
930- frame. assign (camera_spec-> nx_cam * camera_spec-> ny_cam , 0 );
939+ // Only reset counter - frame will be reset when accumulation starts
931940 acc_cbed = 0 ;
932- acc_idx = fr_count;
933941 }
934942 fr_freq = prog_mon.fr_freq ;
935943 rescales_recomputes ();
0 commit comments