Lecture1 PDF
Lecture1 PDF
Anders Hjalmarsson
[email protected]
What is Geant4
A class is a data type, like int or double, that is defined by the programmer.
Mostly classes consists of a header file (.h) and a source file (.cpp)
Class Clock header file
#ifndef Clock_h
#define Clock_h 1
class Clock
{
public:
Clock();
Clock(int hour, int min, int sec);
~Clock();
void SetTime(int hour, int min ,int sec);
int ReadHour();
int ReadMin();
int ReadSec();
void WriteTime(bool wSec = true);
private:
int h;
int m;
int s;
};
#endif
Class Clock source file
#include "Clock.h" int Clock::ReadHour()
#include <iostream> {
#include <iomanip> return h;
}
Clock::Clock() : h(0), m(0), s(0) int Clock::ReadMin()
{;} {
return m;
Clock::Clock(int hour, int min, int sec); }
{ int Clock::ReadSec()
h = hour; {
m = min; return s;
s = sec; }
} void Clock::WriteTime(bool wSec)
{
Clock::~Clock() std::cout<<setw(2)<<setfill('0')<<t
{;} <<':'<<setw(2)<<setfill('0')<<m;
if(wSec)
void Clock::SetTime(int hour, int min, int sec) std:cout<<':'<<setw(2)<<setfill('0')<<s;
{ std::cout<<endl;
h = hour; m = min; s = sec }
}
What is Geant4 (cont.)
EM processes
Photon/lepton – hadron processes
Optical photon processes
Decay processes
You can add more
What is Geant4 (cont.)
• Toolkit
The Geant4 toolkit
You have to build the application
The Geant4 toolkit
You have to build the application
Define
• Geometry
G4double x = 5.0*cm;
G4double y = 5.0*cm;
G4double z = 10.0*cm;
G4Box *detectorS = new G4Box(“detectorS“, x/2, y/2, z/2);
The Geant4 toolkit
You have to build the application
Define
• Geometry
G4double x = 5.0*cm;
G4double y = 5.0*cm;
G4double z = 10.0*cm;
G4Box *detectorS = new G4Box(“detectorS“, x/2, y/2, z/2);
The Geant4 toolkit
You have to build the application
Define
• Geometry
G4double x = 5.0*cm;
G4double y = 5.0*cm;
G4double z = 10.0*cm;
G4Box *detectorS = new G4Box(“detectorS“, x/2, y/2, z/2);
• Material
G4LogicalVolume* detectorL = new G4LogicalVolume(detectorS, Pb, “detector", 0, 0, 0);
The Geant4 toolkit
You have to build the application
Define
• Geometry
G4double x = 5.0*cm;
G4double y = 5.0*cm;
G4double z = 10.0*cm;
G4Box *detectorS = new G4Box(“detectorS“, x/2, y/2, z/2);
• Material
G4LogicalVolume* detectorL = new G4LogicalVolume(detectorS, Pb, “detector", 0, 0, 0);
• Place
G4ThreeVector* positionDetector = G4ThreeVector(0,0,0);
G4VPhysicalVolume* detectorP =
new G4PVPlacement(0, positionDetector, detectorL, “detector”, worldL, false,
0, false);
The Geant4 toolkit
You have to build the application
Define
• Geometry
G4double x = 5.0*cm;
G4double y = 5.0*cm;
G4double z = 10.0*cm;
G4Box *detectorS = new G4Box(“detectorS“, x/2, y/2, z/2);
• Material
G4LogicalVolume* detectorL = new G4LogicalVolume(detectorS, Pb, “detector", 0, 0, 0);
• Place
G4ThreeVector* positionDetector = G4ThreeVector(0,0,0);
G4VPhysicalVolume* detectorP =
new G4PVPlacement(0, positionDetector, detectorL, “detector”, worldL, false,
0, false);
Mandatory class
class MyDetectorConstruction : public G4VUserDetectorConstruction
Example Detector Construction
For the DetectorConstruction class two files needed
Header file
MaSDetectorConstruction.h
Source file
MaSDetectorConstruction.cpp
MaS
• On lillekis.tsl.uu.se
• In the directory
•/home//MaS/include
•/home//MaS/src
Example DC
For the DetectorConstruction class two files needed
Header file
MaSDetectorConstruction.h #ifndef MaSDetectorConstruction_h
Source file #define MaSDetectorConstruction_h 1
class G4VPhysicalVolume;
class G4Material;
class G4VPhysicalVolume;
class G4Material;
Source file class MaSDetectorConstruction : public G4VUserDetectorConstruction
{
MaSDetectorConstruction.cpp public:
MaSDetectorConstruction();
virtual ~MaSDetectorConstruction();
virtual G4VPhysicalVolume* Construct();
#include "MaSDetectorConstruction.h" private:
void DefineMaterials();
void DefineVolumes();
MaSDetectorConstruction::MaSDetectorConstruction() : G4VPhysicalVolume* fWorldP;
G4VUserDetectorConstruction() G4Material *fWorldMaterial;
G4Material *fDetectorMaterial;
{} };
#endif
MaSDetectorConstruction::~MaSDetectorConstruction()
{}
G4VPhysicalVolume* MaSDetectorConstruction::Construct()
{}
void MaSDetectorConstruction::DefineMaterials()
{}
void MaSDetectorConstruction::DefineVolumes()
{}
#ifndef MaSDetectorConstruction_h
#define MaSDetectorConstruction_h 1
class G4VPhysicalVolume;
class G4Material;
Source file class MaSDetectorConstruction : public G4VUserDetectorConstruction
{
MaSDetectorConstruction.cpp public:
MaSDetectorConstruction();
virtual ~MaSDetectorConstruction();
virtual G4VPhysicalVolume* Construct();
#include "MaSDetectorConstruction.h" private:
void DefineMaterials();
void DefineVolumes();
MaSDetectorConstruction::MaSDetectorConstruction() : G4VPhysicalVolume* fWorldP;
G4VUserDetectorConstruction() G4Material *fWorldMaterial;
G4Material *fDetectorMaterial;
{} };
#endif
MaSDetectorConstruction::~MaSDetectorConstruction()
{}
G4VPhysicalVolume* MaSDetectorConstruction::Construct()
{}
void MaSDetectorConstruction::DefineMaterials()
{}
void MaSDetectorConstruction::DefineVolumes()
{}
#ifndef MaSDetectorConstruction_h
#define MaSDetectorConstruction_h 1
class G4VPhysicalVolume;
class G4Material;
Source file class MaSDetectorConstruction : public G4VUserDetectorConstruction
{
MaSDetectorConstruction.cpp public:
MaSDetectorConstruction();
virtual ~MaSDetectorConstruction();
virtual G4VPhysicalVolume* Construct();
G4VPhysicalVolume* MaSDetectorConstruction::Construct()
{ private:
void DefineMaterials();
G4cout<<"Construct"<<G4endl; void DefineVolumes();
DefineMaterials(); G4VPhysicalVolume* fWorldP;
G4Material *fWorldMaterial;
DefineVolumes(); G4Material *fDetectorMaterial;
return fworldP; };
#endif
}
#ifndef MaSDetectorConstruction_h
#define MaSDetectorConstruction_h 1
class G4VPhysicalVolume;
class G4Material;
Source file class MaSDetectorConstruction : public G4VUserDetectorConstruction
{
MaSDetectorConstruction.cpp public:
MaSDetectorConstruction();
virtual ~MaSDetectorConstruction();
virtual G4VPhysicalVolume* Construct();
G4VPhysicalVolume* MaSDetectorConstruction::Construct()
{ private:
void DefineMaterials();
G4cout<<"Construct"<<G4endl; void DefineVolumes();
DefineMaterials(); G4VPhysicalVolume* fWorldP;
G4Material *fWorldMaterial;
DefineVolumes(); G4Material *fDetectorMaterial;
return worldP; };
#endif
}
void MaSDetectorConstruction::DefineMaterials()
{
G4NistManager* nistManager = G4NistManager::Instance();
G4bool fromIsotopes = false;
MaSDetectorConstruction::MaSDetectorConstruction() :
Source file G4VUserDetectorConstruction()
MaSDetectorConstruction.cpp {}
MaSDetectorConstruction::~MaSDetectorConstruction()
G4VPhysicalVolume* MaSDetectorConstruction::Construct() {}
{
G4cout<<"Construct"<<G4endl; G4VPhysicalVolume* MaSDetectorConstruction::Construct()
DefineMaterials(); {}
DefineVolumes();
return worldP; void MaSDetectorConstruction::DefineMaterials()
}
{}
void MaSDetectorConstruction::DefineVolumes()
{}
void MaSDetectorConstruction::DefineMaterials()
{
G4NistManager* nistManager = G4NistManager::Instance();
G4bool fromIsotopes = false;
Optional
• state (default is solid or gas, density dependent)
• temperature (STP temperature = 271.15 K, default)
• pressure (STP pressure = 100 kPa = 1 atm, default)
Material of isotopes
G4int z; G4int a;
G4Isotope* isoO16 = new G4Isotope(“O16”, z=8, a=16 , 16.0*g/mol);
G4Isotope* isoO17 = new G4Isotope(“O17”, z=8, a=17 , 17.0*g/mol);
G4Isotope* isoO18 = new G4Isotope(“O18”, z=8, a=18 , 18.0*g/mol);
class G4VPhysicalVolume;
class G4Material;
Source file class MaSDetectorConstruction : public G4VUserDetectorConstruction
{
MaSDetectorConstruction.cpp public:
MaSDetectorConstruction();
virtual ~MaSDetectorConstruction();
virtual G4VPhysicalVolume* Construct();
G4VPhysicalVolume* MaSDetectorConstruction::Construct()
{ private:
void DefineMaterials();
G4cout<<"Construct"<<G4endl; void DefineVolumes();
DefineMaterials(); G4VPhysicalVolume* fWorldP;
G4Material *fWorldMaterial;
DefineVolumes(); G4Material *fDetectorMaterial;
return fWorldP; };
#endif
}
Example DC
void MaSDetectorConstruction::DefineVolumes()
{
G4double worldOutRadius = 0.5*m;
G4double worldInRadius = 0.0*m;
G4double worldLength = 2.0*m;
G4double worldStartAngle = 0.;
G4double worldSpanAngle = 360.*deg;
worldStartAngle, worldSpanAngle);
}
#include "MaSDetectorConstruction.h“
#include "G4Box.hh"
void MaSDetectorConstruction::DefineVolumes() #include "G4Tubs.hh"
{ #include "G4LogicalVolume.hh"
G4double worldOutRadius = 0.5*m; #include "G4PVPlacement.hh"
G4double worldInRadius = 0.0*m;
G4double worldLength = 2.0*m; MaSDetectorConstruction::MaSDetectorConstruction() :
G4double worldStartAngle = 0.; G4VUserDetectorConstruction()
G4double worldSpanAngle = 360.*deg; {}
}
#ifndef MaSDetectorConstruction_h
#define MaSDetectorConstruction_h 1
class G4VPhysicalVolume;
class G4Material;
Source file class MaSDetectorConstruction : public G4VUserDetectorConstruction
{
MaSDetectorConstruction.cpp public:
MaSDetectorConstruction();
virtual ~MaSDetectorConstruction();
#include "MaSDetectorConstruction.h“ virtual G4VPhysicalVolume* Construct();
private:
#include “G4NistManager.hh” void DefineMaterials();
#include “G4Material.hh” void DefineVolumes();
G4VPhysicalVolume* fWorldP;
G4Material *fWorldMaterial;
#include "G4Box.hh" G4Material *fDetectorMaterial;
};
#include "G4Tubs.hh" #endif
#include "G4LogicalVolume.hh"
#include "G4PVPlacement.hh"
MaSDetectorConstruction::MaSDetectorConstruction() :
G4VUserDetectorConstruction()
{}
MaSDetectorConstruction::~MaSDetectorConstruction()
{}
G4VPhysicalVolume* MaSDetectorConstruction::Construct()
{}
void MaSDetectorConstruction::DefineMaterials()
{}
void MaSDetectorConstruction::DefineVolumes()
{}
#include "MaSDetectorConstruction.h“
#include "G4Box.hh"
MaSDetectorConstruction::MaSDetectorConstruction() :
Source file G4VUserDetectorConstruction()
#include "G4Tubs.hh"
MaSDetectorConstruction.cpp {}
#include "G4LogicalVolume.hh"
#include "G4PVPlacement.hh"
MaSDetectorConstruction::~MaSDetectorConstruction()
MaSDetectorConstruction::MaSDetectorConstruction(): {}
MaSDetectorConstruction::MaSDetectorConstruction() :
G4VUserDetectorConstruction() G4VUserDetectorConstruction()
{ G4VPhysicalVolume* MaSDetectorConstruction::Construct()
{}
; {}
} MaSDetectorConstruction::~MaSDetectorConstruction()
void MaSDetectorConstruction::DefineMaterials()
{}
{}
G4VPhysicalVolume* MaSDetectorConstruction::Construct()
void MaSDetectorConstruction::DefineVolumes()
{}
{}
MaSDetectorConstruction::~MaSDetectorConstruction() void MaSDetectorConstruction::DefineMaterials()
{ {}
;
} void MaSDetectorConstruction::DefineVolumes()
{}
First geometry and mandatory class
DONE!!!
Example Primary Generator Action
Mandatory user class to control the generation of primary particles (source, beam).
Constructor
GeneratePrimaries() method
#include "G4VUserPrimaryGeneratorAction.hh"
#include "globals.hh"
#include "Randomize.hh"
class G4ParticleGun;
class G4Event;
Initialization of primary generator;
class MaSPrimaryGeneratorAction : public
G4ParticleGun or G4VUserPrimaryGeneratorAction
G4GeneralParticleSource {
public:
MaSPrimaryGeneratorAction();
virtual ~MaSPrimaryGeneratorAction();
virtual void GeneratePrimaries(G4Event* );
private:
G4ParticleGun* fParticleGun;
};
Invoke GeneratePrimaryVertex() method #endif
Example PGAction
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "Randomize.hh"
MaSPrimaryGeneratorAction::MaSPrimaryGeneratorAction()
: G4VUserPrimaryGeneratorAction()
{
…;
}
MaSPrimaryGeneratorAction::~MaSPrimaryGeneratorAction()
{
…
}
#include "G4VUserPrimaryGeneratorAction.hh"
#include "globals.hh"
#include "Randomize.hh"
class G4ParticleGun;
class G4Event;
Initialization of primary generator;
class MaSPrimaryGeneratorAction : public
G4ParticleGun or G4VUserPrimaryGeneratorAction
G4GeneralParticleSource {
public:
MaSPrimaryGeneratorAction();
virtual ~MaSPrimaryGeneratorAction();
virtual void GeneratePrimaries(G4Event* );
private:
G4ParticleGun* fParticleGun;
};
Invoke GeneratePrimaryVertex() method #endif
Example PGAction
#include "G4Event.hh"
#include "G4ParticleGun.hh"
#include "G4ParticleTable.hh"
#include "G4ParticleDefinition.hh"
#include "Randomize.hh"
MaSPrimaryGeneratorAction::MaSPrimaryGeneratorAction()
: G4VUserPrimaryGeneratorAction()
{
…;
}
MaSPrimaryGeneratorAction::~MaSPrimaryGeneratorAction()
MaSPrimaryGeneratorAction::~MaSPrimaryGeneratorAction() {
{ …
delete fParticleGun; }
}
void MaSPrimaryGeneratorAction::GeneratePrimaries(G4Event *anEvent)
{
…;
}
G4ParticleGun G4GeneralParticleSource
Simple Powerful
Easy to handle Controlled by UI commands
Use set methods to alternate event A number of different source geometries
values Built in randomizing of source
G4ParticleGun G4GeneralParticleSource
Simple Powerful
Easy to handle Controlled by UI commands
Use set methods to alternate event A number of different source geometries
values Built in randomizing of source
#include "G4RunManager.hh"
#include "G4UImanager.hh"
#include "QGSP_BERT_HP.hh"
#ifdef G4VIS_USE
#include "G4VisExecutive.hh"
#endif
#ifdef G4UI_USE
#include "G4UIExecutive.hh"
#endif
#include "Randomize.hh“
int main(int argc, char** argv)
{
G4RunManager * runManager = new G4RunManager;
G4PhysListFactory factory;
G4VModularPhysicsList* physicsList = factory.GetReferencePhysList("QGSP_BERT_HP");
physicsList -> SetVerboseLevel(1);
physicsList -> SetDefaultCutValue(.01*mm);
runManager -> SetUserInitialization(physicsList);
#ifdef G4VIS_USE
G4VisManager* visManager = new G4VisExecutive;
visManager->Initialize();
#endif
#ifdef G4VIS_USE
delete visManager;
#endif
delete runManager;
return 0;
}