void
nest::sinusoidal_gamma_generator::event_hook( DSSpikeEvent& e )
{
  // get port number --- see #737
  const port tgt_idx = e.get_port();
  assert( 0 <= tgt_idx && static_cast< size_t >( tgt_idx ) < B_.t0_ms_.size() );

  if ( V_.rng_->drand() < hazard_( tgt_idx ) )
  {
    e.get_receiver().handle( e );
    B_.t0_ms_[ tgt_idx ] = V_.t_ms_;
    B_.Lambda_t0_[ tgt_idx ] = 0;
  }
}
void
nest::ppd_sup_generator::event_hook( DSSpikeEvent& e )
{
  // get port number
  const port prt = e.get_port();

  // we handle only one port here, get reference to vector element
  assert( 0 <= prt && static_cast< size_t >( prt ) < B_.age_distributions_.size() );

  // age_distribution object propagates one time step and returns number of spikes
  ulong_t n_spikes =
    B_.age_distributions_[ prt ].update( V_.hazard_step_t_, net_->get_rng( get_thread() ) );

  if ( n_spikes > 0 ) // we must not send events with multiplicity 0
  {
    e.set_multiplicity( n_spikes );
    e.get_receiver().handle( e );
  }
}
void
nest::poisson_generator_ps::event_hook( DSSpikeEvent& e )
{
  // get port number
  const port prt = e.get_port();

  // we handle only one port here, get reference to vector elem
  assert( 0 <= prt && static_cast< size_t >( prt ) < B_.next_spike_.size() );

  // obtain rng
  librandom::RngPtr rng = net_->get_rng( get_thread() );

  // introduce nextspk as a shorthand
  Buffers_::SpikeTime& nextspk = B_.next_spike_[ prt ];

  if ( nextspk.first.is_neg_inf() )
  {
    // need to initialize relative to t_min_active_
    // first spike is drawn from backward recurrence time to initialize the process in equilibrium.
    // In the case of the Poisson process with dead time, this has two domains:
    // one with uniform probability (t<dead_time) and one
    // with exponential probability (t>=dead_time).
    // First we draw a uniform number to choose the case according to the associated probability
    // mass.
    // If dead_time==0 we do not want to draw addtional random numbers (keeps old functionality).

    double spike_offset = 0;

    if ( P_.dead_time_ > 0 and rng->drand() < P_.dead_time_ * P_.rate_ / 1000.0 )
    {
      // uniform case: spike occurs with uniform probability in [0, dead_time].
      spike_offset = rng->drand() * P_.dead_time_;
    }
    else
    {
      // exponential case: spike occurs with exponential probability in [dead_time, infinity]
      spike_offset = V_.inv_rate_ms_ * V_.exp_dev_( rng ) + P_.dead_time_;
    }

    // spike_offset is now time from t_min_active_ til first spike.
    // Split into stamp+offset, then add t_min_active.
    nextspk.first = Time::ms_stamp( spike_offset );
    nextspk.second = nextspk.first.get_ms() - spike_offset;
    nextspk.first += V_.t_min_active_;
  }

  // as long as there are spikes in active period, emit and redraw
  while ( nextspk.first <= V_.t_max_active_ )
  {
    // std::cerr << nextspk.first << '\t' << nextspk.second << '\n';
    e.set_stamp( nextspk.first );
    e.set_offset( nextspk.second );
    e.get_receiver().handle( e );

    // Draw time of next spike
    // Time of spike relative to current nextspk.first stamp
    const double new_offset =
      -nextspk.second + V_.inv_rate_ms_ * V_.exp_dev_( rng ) + P_.dead_time_;

    if ( new_offset < 0 )           // still in same stamp
      nextspk.second = -new_offset; // stamps always 0 < stamp <= h
    else
    {
      // split into stamp and offset, then add to old stamp
      const Time delta_stamp = Time::ms_stamp( new_offset );
      nextspk.first += delta_stamp;
      nextspk.second = delta_stamp.get_ms() - new_offset;
    }
  }
  // std::cerr << "********************************\n";
}