code: def handle_events(sol, events, active_events, is_terminal, t_old, t):
      """Helper function to handle events.
  
      Parameters
      ----------
      sol : DenseOutput
          Function ``sol(t)`` which evaluates an ODE solution between `t_old`
          and  `t`.
      events : list of callables, length n_events
          Event functions with signatures ``event(t, y)``.
      active_events : ndarray
          Indices of events which occurred.
      is_terminal : ndarray, shape (n_events,)
          Which events are terminal.
      t_old, t : float
          Previous and new values of time.
  
      Returns
      -------
      root_indices : ndarray
          Indices of events which take zero between `t_old` and `t` and before
          a possible termination.
      roots : ndarray
          Values of t at which events occurred.
      terminate : bool
          Whether a terminal event occurred.
      """
      roots = []
      for event_index in active_events:
          roots.append(
              solve_event_equation(events[event_index], sol, t_old, t)
          )
  
      roots = np.asarray(roots)
  
      if np.any(is_terminal[active_events]):
          if t > t_old:
              order = np.argsort(roots)
          else:
              order = np.argsort(-roots)
          active_events = active_events[order]
          roots = roots[order]
          t = np.nonzero(is_terminal[active_events])[0][0]
          active_events = active_events[: t + 1]
          roots = roots[: t + 1]
          terminate = True
      else:
          terminate = False
  
      return active_events, roots, terminate
  
file_path: catboost/contrib/python/scipy/py2/scipy/integrate/_ivp/ivp.py
imports: from __future__ import division, print_function, absolute_import
  import inspect
  import numpy
  from .bdf import BDF
  from .radau import Radau
  from .rk import RK23, RK45
  from .lsoda import LSODA
  from scipy.optimize import OptimizeResult
  from .common import EPS, OdeSolution
  from .base import OdeSolver
label_desc: Use image node_id 3 for calling a global function with example usage: handle_events(sol, events, active_events, is_terminal, t_old, t) and returns: active_events,  roots,  terminate