From 789d8bf0ebf98c4e7478ef9cac8e4d9635fc7fe7 Mon Sep 17 00:00:00 2001 From: Gitea Date: Tue, 3 Apr 2018 22:11:52 +0200 Subject: [PATCH] Now supports creating root file for data analysis --- include/MedtechAnalysis.hh | 8 +++++++- include/MedtechRunAction.hh | 1 + medtech.cc | 2 +- src/MedtechAnalysis.cc | 18 +++++++++++++++++- src/MedtechDetectorConstruction.cc | 2 +- src/MedtechRunAction.cc | 4 +++- src/MedtechSteppingAction.cc | 2 ++ 7 files changed, 32 insertions(+), 5 deletions(-) diff --git a/include/MedtechAnalysis.hh b/include/MedtechAnalysis.hh index 3a3a8d5..8abd926 100644 --- a/include/MedtechAnalysis.hh +++ b/include/MedtechAnalysis.hh @@ -9,6 +9,8 @@ #define MedtechAnalysis_hh #include +#include "G4Threading.hh" +#include "G4AutoLock.hh" #include "TTree.h" #include "TFile.h" @@ -21,9 +23,13 @@ private: /* Private constructor to prevent instancing. */ TTree *tree; TFile *file; -public: + + G4Mutex MedtechAnalysisMutex; + /* Static access method. */ MedtechAnalysis(); + +public: ~MedtechAnalysis(); void Fill(double, double, double); diff --git a/include/MedtechRunAction.hh b/include/MedtechRunAction.hh index b59f21c..18f8418 100644 --- a/include/MedtechRunAction.hh +++ b/include/MedtechRunAction.hh @@ -22,6 +22,7 @@ #include "G4UnitsTable.hh" #include "G4SystemOfUnits.hh" #include "MedtechRun.hh" +#include "MedtechAnalysis.hh" class MedtechRunAction : public G4UserRunAction { diff --git a/medtech.cc b/medtech.cc index 557f0c9..b42e111 100644 --- a/medtech.cc +++ b/medtech.cc @@ -19,7 +19,7 @@ #include "MedtechActionInitialization.hh" #include "Parameters.hh" #include "G4SteppingManager.hh" - +#include "MedtechAnalysis.hh" int main(int argc,char** argv) { diff --git a/src/MedtechAnalysis.cc b/src/MedtechAnalysis.cc index 8dcf3e5..e7e58f6 100644 --- a/src/MedtechAnalysis.cc +++ b/src/MedtechAnalysis.cc @@ -12,6 +12,7 @@ MedtechAnalysis::MedtechAnalysis() tree = new TTree("tree", "tree"); file = new TFile("data.root","RECREATE"); instance = this; + MedtechAnalysisMutex = G4MUTEX_INITIALIZER; } MedtechAnalysis::~MedtechAnalysis() @@ -19,14 +20,26 @@ MedtechAnalysis::~MedtechAnalysis() } +MedtechAnalysis* MedtechAnalysis::getInstance() +{ + if (instance == 0) + { + instance = new MedtechAnalysis(); + } + + return instance; +} + void MedtechAnalysis::Fill(double x, double y, double e) { + G4AutoLock lock(&MedtechAnalysisMutex); tree -> Branch("x", &x, "x/D"); tree -> Branch("y", &y, "y/D"); tree -> Branch("e", &e, "e/D"); tree -> Fill(); - //tree -> ChangeFile(file); + + lock.unlock(); } void MedtechAnalysis::Close() @@ -34,3 +47,6 @@ void MedtechAnalysis::Close() tree -> Write(); file -> Close(); } + +/* Null, because instance will be initialized on demand. */ +MedtechAnalysis* MedtechAnalysis::instance = 0; diff --git a/src/MedtechDetectorConstruction.cc b/src/MedtechDetectorConstruction.cc index 9305d65..5533ede 100644 --- a/src/MedtechDetectorConstruction.cc +++ b/src/MedtechDetectorConstruction.cc @@ -79,7 +79,7 @@ G4VPhysicalVolume* MedtechDetectorConstruction::Construct() G4Tet *solidTet = new G4Tet("solidTet", G4ThreeVector(0, 0, 1*cm), G4ThreeVector(-5*cm, -5*cm, 0), G4ThreeVector(5*cm, -5*cm, 0), G4ThreeVector(0, 5*cm, 0)); G4LogicalVolume *logicTet = new G4LogicalVolume(solidTet, tetMaterial, "Tet", 0, 0, 0); G4PVPlacement *physicalTet = new G4PVPlacement(0, - G4ThreeVector(0,0,3*cm), + G4ThreeVector(0,0,1*cm), logicTet, "Tet", logicWorld, diff --git a/src/MedtechRunAction.cc b/src/MedtechRunAction.cc index c9611ce..868629f 100644 --- a/src/MedtechRunAction.cc +++ b/src/MedtechRunAction.cc @@ -19,7 +19,7 @@ MedtechRunAction::~MedtechRunAction() void MedtechRunAction::BeginOfRunAction(const G4Run*) { - + MedtechAnalysis *man = MedtechAnalysis::getInstance(); } @@ -37,6 +37,8 @@ void MedtechRunAction::EndOfRunAction(const G4Run* run) G4cout << G4endl << "--------------------End of Global Run-----------------------"; + MedtechAnalysis *man = MedtechAnalysis::getInstance(); + man -> Close(); } else { G4cout diff --git a/src/MedtechSteppingAction.cc b/src/MedtechSteppingAction.cc index 39a6dfb..971f324 100644 --- a/src/MedtechSteppingAction.cc +++ b/src/MedtechSteppingAction.cc @@ -65,6 +65,8 @@ void MedtechSteppingAction::UserSteppingAction(const G4Step* step) fTrack -> SetTrackStatus(fStopAndKill); } G4cout << "X: " << postX << " Y: " << postY << " KinE: " << postkinE << " Particle: " << fTrack -> GetDefinition() << G4endl; + MedtechAnalysis *man = MedtechAnalysis::getInstance(); + man -> Fill(postX, postY, postkinE); } // check if we are in scoring volume