Écrire un code de simulation GEANT4
Transcription
Écrire un code de simulation GEANT4
Écrire un code de simulation GEANT4 Documentation sur le code Geant4 http://www-geant4.kek.jp/Reference 2 Documentation sur le code Geant4 Geant4 version 9.5: p01 - mars 2012 p02 - octobre 2012 3 Le programme principal GEANT4 est une boîte à outils; il faut créer l'application principale (main program). int main( int argc, char* argv[] ) { ... } run = détecteur + processus physique + une boucle sur un certain nombre d'événements event = l'unité de base de la simulation track = le parcours d'une particule, son histoire d'interactions avec les materiaux passifs et les détecteurs (actifs) G4RunManager = classe au plus haut niveau, gérante d'un run (présence obligatoire) #include “G4RunManager.hh” int main( int argc, char* argv[] ) { G4RunManager *runManager = new G4RunManager(); ... delete runManager; } 4 La compilation du code - la structure du code: exemple.cc contient le block de code main() ./ GNUMakefile exemple.cc - le fichier de compilation: GNUMakefile - compiler le code avec la commande: make - l'exécutable est dans: $HOME/geant4_workdir/bin/ Linux-g++/exemple ./include header1.hh header2.hh header3.hh ... ./src source1.cc source2.cc source3.cc ... name := exemple G4TARGET := $(name) G4EXLIB := true ifndef G4INSTALL G4INSTALL = ../../.. endif .PHONY: all all: lib bin include $(G4INSTALL)/config/binmake.gmk - la bibliothèque partagée $HOME/geant4_workdir/tmp/ Linux-g++/exemple/libexemple.so 5 Note: l'exécutable peut être changer d'emplacement, mais pas la bibliothèque partagée! Compilation du code avec “cmake” (GEANT4 version 9.5) dans le répertoire avec le sources, créer un nouveau répertoire: build ./ CMakeLists.txt exemple.cc ./include header1.hh header2.hh header3.hh ./src source1.cc source2.cc source3.cc ./build cd build cmake -DGeant4_DIR=/repertoire/vers/le/fichier/Geant4Config.cmake .. make l'exécutable sera crée dans le répertoire build - après une modification dans un des fichiers sources: make - après une modification de structure (rajout/élimination de fichiers) cmake -DGeant4_DIR=/repertoire/vers/le/fichier/Geant4Config.cmake .. 6 make Un minimum de code #include “G4RunManager.hh” #include "MyDetector.hh" #include "MyPhysicsList.hh" int main( int argc, char* argv[] ) { G4RunManager *runManager = new G4RunManager(); G4VUserDetectorConstruction* detector = new MyDetector; runManager->SetUserInitialization(detector); G4VUserPhysicsList* physics = new MyPhysicsList; runManager->SetUserInitialization(physics); runManager->Initialize(); delete runManager; } Note: le reste nécessaire peut être communiqué au kernel par l'interface utilisateur, voir la suite ... 7 Le mode d'exécution interface utilisateur (UI) #include “G4RunManager.hh” ... #include “G4UIExecutive.hh” int main( int argc, char* argv[] ) { G4RunManager *runManager = new G4RunManager(); ... G4UIExecutive* ui = new G4UIExecutive(argc, argv); ui->SessionStart(); delete ui; delete runManager; } ************************************************************* Geant4 version Name: geant4-09-04-patch-02 (24-June-2011) Copyright : Geant4 Collaboration Reference : NIM A 506 (2003), 250-303 WWW : http://cern.ch/geant4 ************************************************************* Create G4UIExecutive, start session Idle> ici les commandes utilisateur; exit pour sortir 8 Commandes GEANT4 UI syntaxe: Idle> /multi/level/command/directory/command [parameters] Idle> help Command directory path : / Sub-directories : 1) /control/ UI control commands. 2) /units/ Available units. 3) /persistency/ Control commands for Persistency package 4) /geometry/ Geometry control commands. 5) /tracking/ TrackingManager and SteppingManager control commands. 6) /event/ EventManager control commands. 7) /cuts/ Commands for G4VUserPhysicsList. 8) /run/ Run control commands. 9) /random/ Random number status control commands. 10) /particle/ Particle control commands. 11) /process/ Process Table control commands. 12) /gun/ Particle Gun control commands. 13) /material/ Commands for materials Commands : Type the number ( 0:end, -n:n level back ) : Note: la disponibilité d'une commande dépend en général de l'état de l'application. 9 Soumettre des commandes UI “en dur” dans le code compilé G4UImanager* UI = G4UImanager::GetUIpointer(); UI->ApplyCommand("/run/verbose 1"); UI->ApplyCommand("/event/verbose 1"); UI->ApplyCommand("/tracking/verbose 1"); Note: ne pas utiliser dans une boucle sur les événements! Macro-commandes run.mac /control/verbose 2 # comment lines will be echoed # commands in a macro /run/verbose 1 /event/verbose 1 /tracking/verbose 1 code compilé G4UImanager* UI = G4UImanager::GetUIpointer(); UI->ApplyCommand("/control/execute/run.mac”); UI interactive Idle>/control/execute run.mac 10 Navigation dans un terminal de type Linux (facilité) #include "G4UIterminal.hh" #include "G4UIsession.hh" G4UIsession *session = new G4UIterminal(new G4UItcsh); session->SessionStart(); delete session; Command directory path : / Sub-directories : /control/ UI control commands. /units/ Available units. /persistency/ Control commands for Persistency package /geometry/ Geometry control commands. /tracking/ TrackingManager and SteppingManager control commands. /event/ EventManager control commands. /cuts/ Commands for G4VUserPhysicsList. /run/ Run control commands. /random/ Random number status control commands. /particle/ Particle control commands. /process/ Process Table control commands. /gun/ Particle Gun control commands. /material/ Commands for materials Commands : 11 Idle> ls, cd, pwd (ne pas utiliser dans une macro-commande) Code avec visualisation activée #include "G4VisExecutive.hh" G4VisManager* visManager = new G4VisExecutive; visManager->Initialize(); ... /gun/ Particle Gun control commands. /material/ Commands for materials /vis/ Visualization commands. Commands : Idle> cd vis Idle> ls /vis/ASCIITree/ Commands for ASCIITree control. /vis/heprep/ HepRep commands. /vis/rayTracer/ RayTracer commands. /vis/gMocren/ gMocren commands. /vis/ogl/ G4OpenGLViewer commands. /vis/modeling/ Modeling commands. ... les différentes systèmes graphiques enrégistrés dans l'installation G4 12 La création de nouvelles commandes UI; le mechanisme messenger - communiquer par le biais de l'interface utilisateur (UI) avec les classes utilisateur (détecteur, contrôle événement, etc.) - pour quelles raisons? modifier pendant l'exécution (UI où macro-commande) les différents paramètres include/DetectorMessenger.hh class DetectorMessenger: public G4UImessenger { public: DetectorMessenger(DetectorConstruction* detectorConstruction); virtual void SetNewValue(G4UIcommand*, G4String); private: DetectorConstruction* fDetectorConstruction; G4UIcmdWithADoubleAndUnit* fSetMagFieldCmd; src/DetectorMessenger.cc fSetMagFieldCmd = new G4UIcmdWithADoubleAndUnit("/B4/det/setMagField",this); fSetMagFieldCmd->SetGuidance("Define magnetic field."); ... fDetectorConstruction ->SetMagField(fSetMagFieldCmd->GetNewDoubleValue(newValue)); 13 Le nouveau répertoire de commandes (exemple basic/B4/B4a) Idle> ls Command directory path : / Sub-directories : /control/ UI control commands. ... /B4/ UI commands of this example ... Idle> ls B4/det Guidance : Detector construction control Sub-directories : Commands : setMagField * Define magnetic field. macro-commande # run1.mac ... /B4/det/setMagField 0.2 tesla ... 14 La persistance des objets en GEANT4 Un objet crée pendant l'exécution de l'application cesse d'exister à la fin de l'application. Afficher/enrégistrer les résultats de la simulation pour un usage ultérieur. Interface de communication simple: G4cout << "Enter value for parameter:" << G4endl; G4int par = 0; G4cin >> par; G4cout << "par = " << par << G4endl; ... et les objets? struct nom_structure { int par1; float par2; float par3; bool par4; int par5[3]; }; G4cout G4cout G4cout ... G4cout G4cout ... << “nom_structure” << endl; << “par1:” << par1 << endl; << “par2:“ << par2 << endl; << “par5[3]:” << endl; << par5[0] << endl; 15 Exemple: extended/persistency/P01 root [1] .ls TFile** hits.root TFile* hits.root KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> KEY: vector<ExP01TrackerHit*> { Point *p = new Point(6,2); vector <Point*> v; v.reserve(10); v.push_back(p); Point *q = v[0]; } Event_1;1 Event_2;1 Event_3;1 Event_4;1 Event_5;1 Event_6;1 Event_7;1 Event_8;1 Event_9;1 Event_10;1 object object object object object object object object object object title title title title title title title title title title - utiliser un std::vector pour collectioner les objets de type Point - les écrire dans une enveloppe ROOT - la relecture nécessite un dictionnaire pour traduire dans le langage de classe Point 16 Persistance avec le mechanisme Reflex interfacé avec l'interpréteur C++ de ROOT (= Cintex) gSystem->Load(“libClassesDict.so”); contient le dictionnaire pour la classe ObjectClass ROOT::Cintex::Cintex::Enable(); activer le mechanisme Reflex TFile fo(“fichier.root”,”RECREATE”); ouvrir un fichier ROOT ObjectClass *object = new ObjectClass(...); fo.WriteObject(object,”name”); écrire l'objet dans le fichier fo.Close(); readHits.C lecture du fichier: gSystem->Load(“libCintex.so”); gSystem->Load(“/repertoire/vers/libClassesDict.so ROOT::Cintex::Cintex::Enable(); ... /> root root[0] .x readHits.C 17 Persistance avec des objets ROOT (G4RootAnalysisManager) exemple “novice/N02” modifié src/ExN02RunAction.cc #include "G4RootAnalysisManager.hh" void ExN02RunAction::BeginOfRunAction(const G4Run* aRun) { G4RootAnalysisManager *man = G4RootAnalysisManager::Instance(); man->OpenFile("RootExN02.root")) { G4int idH1_1 = man->CreateH1("Edep","Edep",100,0.,0.2); man->CreateNtuple("Hits","Hits"); G4int idN_1 = man->CreateNtupleIColumn("TrackID"); ... man->FinishNtuple(); } void ExN02RunAction::EndOfRunAction(const G4Run*) { G4RootAnalysisManager *man = G4RootAnalysisManager::Instance(); man->Write(); man->CloseFile(); delete G4RootAnalysisManager::Instance(); } 18 G4RootAnalysisManager (cont.) src/ExN02TrackerSD.cc #include "G4RootAnalysisManager.hh" G4bool ExN02TrackerSD::ProcessHits(G4Step* aStep, ...) { G4double edep = aStep->GetTotalEnergyDeposit(); ExN02TrackerHit* newHit = new ExN02TrackerHit(); newHit->SetEdep(edep); ... G4RootAnalysisManager *man = G4RootAnalysisManager::Instance(); man->FillH1(0,edep); man->FillNtupleIColumn(0,newHit->GetTrackID()); man->AddNtupleRow(); } - le remplissage se fait dans le processeur de hits du détecteur sensible (sensitive detector, SD) - possibilités: histogramme 1D (double), histogramme 2D (double), ntuple (int, float, double) - histogrammes et ntuples identifiable par un index attribué à la création 19 G4RootAnalysisManager (cont.) root [1] .ls TFile** RootExN02.root TFile* RootExN02.root KEY: TDirectoryFile histo;1 histo KEY: TDirectoryFile ntuple;1 ntuple root [2] ntuple->cd() root [3] .ls TDirectoryFile* ntuple ntuple KEY: TTree Hits;1 Hits root [3] Hits->Print() ****************************************************************************** *Tree :Hits : Hits * *Entries : 50 : Total = 7289 bytes File Size = 6659 * * : : Tree compression factor = 1.00 * ****************************************************************************** *Br 0 :TrackID : Int_t Hits * *Entries : 50 : Total Size= 1029 bytes One basket in memory * *Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 * *............................................................................* ... ... *Br 5 :Pos_z : Double_t Hits * *Entries : 50 : Total Size= 1227 bytes One basket in memory * *Baskets : 0 : Basket Size= 32000 bytes Compression= 1.00 * *............................................................................* 20 G4RootAnalysisManager (cont.) root [1] .ls TFile** RootExN02.root TFile* RootExN02.root KEY: TDirectoryFile histo;1 histo KEY: TDirectoryFile ntuple;1 ntuple root [2] histo->cd() root [3] .ls TDirectoryFile* histo histo KEY: TH1D Edep;1 Edep root [4]Edep->Draw(“H”) 21