// Physics List for MyPhysicsList1
//
// This file was copied from geant4/examples/extended/analysis to TOPAS
// as an example of a user-contributed physics list.
// The only addition for TOPAS was to also pass the pointer to
// the TsParameterManager so that you can use TOPAS parameters to further
// control your physics.
//
// ********************************************************************
// * License and Disclaimer                                           *
// *                                                                  *
// * The  Geant4 software  is  copyright of the Copyright Holders  of *
// * the Geant4 Collaboration.  It is provided  under  the terms  and *
// * conditions of the Geant4 Software License,  included in the file *
// * LICENSE and available at  http://cern.ch/geant4/license .  These *
// * include a list of copyright holders.                             *
// *                                                                  *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work  make  any representation or  warranty, express or implied, *
// * regarding  this  software system or assume any liability for its *
// * use.  Please see the license in the file  LICENSE  and URL above *
// * for the full disclaimer and the limitation of liability.         *
// *                                                                  *
// * This  code  implementation is the result of  the  scientific and *
// * technical work of the GEANT4 collaboration.                      *
// * By using,  copying,  modifying or  distributing the software (or *
// * any work based  on the software)  you  agree  to acknowledge its *
// * use  in  resulting  scientific  publications,  and indicate your *
// * acceptance of all terms of the Geant4 Software license.          *
// ********************************************************************

#include "MyPhysicsList1.hh"

#include "G4ProcessManager.hh"

#include "G4BosonConstructor.hh"
#include "G4LeptonConstructor.hh"
#include "G4MesonConstructor.hh"
#include "G4BosonConstructor.hh"
#include "G4BaryonConstructor.hh"
#include "G4IonConstructor.hh"
#include "G4SystemOfUnits.hh"

MyPhysicsList1::MyPhysicsList1(TsParameterManager* pM):  G4VUserPhysicsList(),
fPm(pM)
{
	defaultCutValue = 1.0*mm;
	SetVerboseLevel(1);
}


MyPhysicsList1::~MyPhysicsList1()
{}


void MyPhysicsList1::ConstructParticle()
{
	// In this method, static member functions should be called
	// for all particles which you want to use.
	// This ensures that objects of these particle types will be
	// created in the program.

	G4BosonConstructor  pBosonConstructor;
	pBosonConstructor.ConstructParticle();

	G4LeptonConstructor pLeptonConstructor;
	pLeptonConstructor.ConstructParticle();

	G4MesonConstructor pMesonConstructor;
	pMesonConstructor.ConstructParticle();

	G4BaryonConstructor pBaryonConstructor;
	pBaryonConstructor.ConstructParticle();

	G4IonConstructor pIonConstructor;
	pIonConstructor.ConstructParticle();
}


void MyPhysicsList1::ConstructProcess()
{
	AddTransportation();
	ConstructEM();
	ConstructDecay();
}


#include "G4PhysicsListHelper.hh"

#include "G4ComptonScattering.hh"
#include "G4GammaConversion.hh"
#include "G4PhotoElectricEffect.hh"

#include "G4eMultipleScattering.hh"
#include "G4eIonisation.hh"
#include "G4eBremsstrahlung.hh"
#include "G4eplusAnnihilation.hh"

#include "G4MuMultipleScattering.hh"
#include "G4MuIonisation.hh"
#include "G4MuBremsstrahlung.hh"
#include "G4MuPairProduction.hh"

#include "G4hMultipleScattering.hh"
#include "G4hIonisation.hh"
#include "G4hBremsstrahlung.hh"
#include "G4hPairProduction.hh"

#include "G4ionIonisation.hh"


void MyPhysicsList1::ConstructEM()
{
	G4PhysicsListHelper* ph = G4PhysicsListHelper::GetPhysicsListHelper();

	G4ParticleTable::G4PTblDicIterator* theParticleIterator = GetParticleIterator();
	theParticleIterator->reset();
	while( (*theParticleIterator)() ){
	G4ParticleDefinition* particle = theParticleIterator->value();
	G4String particleName = particle->GetParticleName();

	if (particleName == "gamma") {
		// gamma
		ph->RegisterProcess(new G4PhotoElectricEffect, particle);
		ph->RegisterProcess(new G4ComptonScattering,   particle);
		ph->RegisterProcess(new G4GammaConversion,     particle);

	} else if (particleName == "e-") {
		//electron
		ph->RegisterProcess(new G4eMultipleScattering, particle);
		ph->RegisterProcess(new G4eIonisation,         particle);
		ph->RegisterProcess(new G4eBremsstrahlung,     particle);

	} else if (particleName == "e+") {
		//positron
		ph->RegisterProcess(new G4eMultipleScattering, particle);
		ph->RegisterProcess(new G4eIonisation,         particle);
		ph->RegisterProcess(new G4eBremsstrahlung,     particle);
		ph->RegisterProcess(new G4eplusAnnihilation,   particle);

	} else if( particleName == "mu+" ||
						 particleName == "mu-"    ) {
		//muon
		ph->RegisterProcess(new G4MuMultipleScattering, particle);
		ph->RegisterProcess(new G4MuIonisation,         particle);
		ph->RegisterProcess(new G4MuBremsstrahlung,     particle);
		ph->RegisterProcess(new G4MuPairProduction,     particle);

	} else if( particleName == "proton" ||
						 particleName == "pi-" ||
						 particleName == "pi+"    ) {
		//proton
		ph->RegisterProcess(new G4hMultipleScattering, particle);
		ph->RegisterProcess(new G4hIonisation,         particle);
		ph->RegisterProcess(new G4hBremsstrahlung,     particle);
		ph->RegisterProcess(new G4hPairProduction,     particle);

	} else if( particleName == "alpha" ||
						 particleName == "He3" )     {
		//alpha
		ph->RegisterProcess(new G4hMultipleScattering, particle);
		ph->RegisterProcess(new G4ionIonisation,       particle);

	} else if( particleName == "GenericIon" ) {
		//Ions
		ph->RegisterProcess(new G4hMultipleScattering, particle);
		ph->RegisterProcess(new G4ionIonisation,       particle);

		} else if ((!particle->IsShortLived()) &&
						 (particle->GetPDGCharge() != 0.0) &&
						 (particle->GetParticleName() != "chargedgeantino")) {
		//all others charged particles except geantino
		ph->RegisterProcess(new G4hMultipleScattering, particle);
		ph->RegisterProcess(new G4hIonisation,         particle);
	}
	}
}


#include "G4Decay.hh"

void MyPhysicsList1::ConstructDecay()
{
	G4PhysicsListHelper* ph = G4PhysicsListHelper::GetPhysicsListHelper();

	// Add Decay Process
	G4Decay* theDecayProcess = new G4Decay();
	G4ParticleTable::G4PTblDicIterator* theParticleIterator = GetParticleIterator();
	theParticleIterator->reset();
	while( (*theParticleIterator)() ){
	G4ParticleDefinition* particle = theParticleIterator->value();
	if (theDecayProcess->IsApplicable(*particle)) {
		ph->RegisterProcess(theDecayProcess, particle);
	}
	}
}


void MyPhysicsList1::SetCuts()
{
	if (verboseLevel >0){
	G4cout << "MyPhysicsList1::SetCuts:";
	G4cout << "CutLength : " << G4BestUnit(defaultCutValue,"Length") << G4endl;
	}

	// set cut values for gamma at first and for e- second and next for e+,
	// because some processes for e+/e- need cut values for gamma
	//
	SetCutValue(defaultCutValue, "gamma");
	SetCutValue(defaultCutValue, "e-");
	SetCutValue(defaultCutValue, "e+");
	SetCutValue(defaultCutValue, "proton");

	if (verboseLevel>0) DumpCutValuesTable();
}
