Skip to content
Snippets Groups Projects
Geant4ParticleGun.cpp 2.67 KiB
Newer Older
Markus Frank's avatar
Markus Frank committed
// $Id: $
//==========================================================================
//  AIDA Detector description implementation for LCD
//--------------------------------------------------------------------------
Markus Frank's avatar
Markus Frank committed
// Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
// For the licensing terms see $DD4hepINSTALL/LICENSE.
// For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
// Author     : M.Frank
//
//==========================================================================

// Framework include files
Markus Frank's avatar
Markus Frank committed
#include "DD4hep/Printout.h"
#include "DD4hep/InstanceCount.h"
#include "DDG4/Geant4Context.h"
#include "DDG4/Geant4Primary.h"
#include "DDG4/Geant4ParticleGun.h"
#include "DDG4/Geant4InputHandling.h"
Markus Frank's avatar
Markus Frank committed
#include "CLHEP/Units/SystemOfUnits.h"

// C/C++ include files
Markus Frank's avatar
Markus Frank committed
#include <limits>
Markus Frank's avatar
Markus Frank committed
using namespace std;
using namespace DD4hep::Simulation;

/// Standard constructor
Geant4ParticleGun::Geant4ParticleGun(Geant4Context* ctxt, const string& nam)
  : Geant4IsotropeGenerator(ctxt,nam), m_shotNo(0)
Markus Frank's avatar
Markus Frank committed
{
  InstanceCount::increment(this);
  m_needsControl = true;
  declareProperty("isotrop",      m_isotrop = false);
  declareProperty("Standalone",   m_standalone = true);
  // Backwards compatibility: Un-capitalize
  declareProperty("position",     m_position);
  declareProperty("direction",    m_direction);
  declareProperty("energy",       m_energy);
  declareProperty("particle",     m_particleName);
  declareProperty("multiplicity", m_multiplicity);
  declareProperty("print",        m_print = true);
}

/// Default destructor
Geant4ParticleGun::~Geant4ParticleGun() {
  InstanceCount::decrement(this);
}

/// Callback to generate primary particles
void Geant4ParticleGun::operator()(G4Event* event)   {
Markus Frank's avatar
Markus Frank committed
  if ( m_isotrop )   {
    double mom = 0.0;
    this->Geant4IsotropeGenerator::getParticleDirection(0,m_direction,mom);
Markus Frank's avatar
Markus Frank committed
  }
  else  {
    double r = m_direction.R(), eps = numeric_limits<float>::epsilon();
    if ( r > eps )  {
      m_direction.SetXYZ(m_direction.X()/r, m_direction.Y()/r, m_direction.Z()/r);
    }
Markus Frank's avatar
Markus Frank committed
  }

  if ( m_standalone )  {
    generationInitialization(this,context());
  }
  this->Geant4ParticleGenerator::operator()(event);
  print("Shoot [%d] %.3f GeV %s pos:(%.3f %.3f %.3f)[mm] dir:(%6.3f %6.3f %6.3f)",
Andre Sailer's avatar
Andre Sailer committed
        m_shotNo, m_energy/CLHEP::GeV, m_particleName.c_str(),
        m_position.X()/CLHEP::mm, m_position.Y()/CLHEP::mm, m_position.Z()/CLHEP::mm,
        m_direction.X(),m_direction.Y(), m_direction.Z());
  if ( m_print )   {
    this->Geant4ParticleGenerator::printInteraction(m_mask);
  }
Markus Frank's avatar
Markus Frank committed
  ++m_shotNo;
  if ( m_standalone ) {
    mergeInteractions(this,context());
    generatePrimaries(this,context(),event);
  }