-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from lopezzot/lp-addleakagecounter
Add (leakage) particle spectrum analyzer
- Loading branch information
Showing
7 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
//************************************************** | ||
// \file SpectrumAnalyzer.hh | ||
// \brief: Declaration of SpectrumAnalyzer class | ||
// \author: Lorenzo Pezzotti (CERN EP-SFT-sim) | ||
// @lopezzot | ||
// \start date: 28 August 2023 | ||
//************************************************** | ||
|
||
// A portable Geant4-based particle spectrum analyzer | ||
// to be used within a Geant4 simulation without affecting it. | ||
// Instead of coding it in the simulation, create a singleton | ||
// and manage its usage with (#ifdef) compiler definition. | ||
|
||
#ifndef SpectrumAnalyzer_h | ||
# define SpectrumAnalyzer_h | ||
|
||
// Includers from Geant4 | ||
// | ||
# include "G4Step.hh" | ||
# include "G4ThreadLocalSingleton.hh" | ||
|
||
// Includers from C++ | ||
// | ||
# include <functional> | ||
|
||
class SpectrumAnalyzer | ||
{ | ||
friend class G4ThreadLocalSingleton<SpectrumAnalyzer>; | ||
|
||
public: | ||
// Return pointer to class instance | ||
static SpectrumAnalyzer* GetInstance() | ||
{ | ||
static G4ThreadLocalSingleton<SpectrumAnalyzer> instance{}; | ||
return instance.Instance(); | ||
} | ||
|
||
// Methods | ||
// | ||
// Run-wise methods | ||
void CreateNtupleAndScorer(const G4String scName = "te"); | ||
inline void ClearNtupleID() { ntupleID = 99; } | ||
// Event-wise methods | ||
inline void ClearEventFields() | ||
{ | ||
neutronScore = 0.; | ||
protonScore = 0., pionScore = 0., gammaScore = 0., electronScore = 0.; | ||
} | ||
void FillEventFields() const; | ||
// Step-wise methods | ||
void Analyze(const G4Step* step); | ||
|
||
private: | ||
// Members | ||
// | ||
// Run-wise members | ||
G4int ntupleID; | ||
std::function<G4double(const G4Step* step)> scorer; | ||
G4String scorerName{}; | ||
// Event-wise members | ||
G4double neutronScore; | ||
G4double protonScore; | ||
G4double pionScore; | ||
G4double gammaScore; | ||
G4double electronScore; | ||
|
||
// Scoring quantities | ||
inline static G4double GetMomentum(const G4Step* step) | ||
{ | ||
return step->GetTrack()->GetMomentum().mag(); | ||
}; | ||
inline static G4double GetKE(const G4Step* step) | ||
{ | ||
return step->GetTrack()->GetKineticEnergy(); | ||
}; | ||
inline static G4double GetTE(const G4Step* step) { return step->GetTrack()->GetTotalEnergy(); }; | ||
|
||
private: | ||
// Private constructor | ||
SpectrumAnalyzer() = default; | ||
|
||
public: | ||
SpectrumAnalyzer(SpectrumAnalyzer const&) = delete; | ||
void operator=(SpectrumAnalyzer const&) = delete; | ||
}; | ||
|
||
#endif // SpectrumAnalyzer_h | ||
|
||
//************************************************** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//************************************************** | ||
// \file SpectrumAnalyzer.cc | ||
// \brief: Definition of SpectrumAnalyzer class | ||
// \author: Lorenzo Pezzotti (CERN EP-SFT-sim) | ||
// @lopezzot | ||
// \start date: 28 August 2023 | ||
//************************************************** | ||
|
||
// Includers from project files | ||
// | ||
#include "SpectrumAnalyzer.hh" | ||
|
||
// Includers from Geant4 | ||
// | ||
#include "G4Version.hh" | ||
#if G4VERSION_NUMBER < 1100 | ||
# include "g4root.hh" | ||
#else | ||
# include "G4AnalysisManager.hh" | ||
#endif | ||
#include "G4Step.hh" | ||
|
||
// #define DEBUG | ||
|
||
void SpectrumAnalyzer::CreateNtupleAndScorer(const G4String scName) | ||
{ | ||
auto AM = G4AnalysisManager::Instance(); | ||
|
||
ntupleID = AM->CreateNtuple("Spectrum", "Spectrum"); | ||
AM->CreateNtupleDColumn("neutronScore"); | ||
AM->CreateNtupleDColumn("protonScore"); | ||
AM->CreateNtupleDColumn("pionScore"); | ||
AM->CreateNtupleDColumn("gammaScore"); | ||
AM->CreateNtupleDColumn("electronScore"); | ||
AM->FinishNtuple(); | ||
|
||
// Define scorer type | ||
scorerName = scName; | ||
if (scorerName == "te") { | ||
scorer = GetTE; | ||
} | ||
if (scorerName == "momentum") { | ||
scorer = GetMomentum; | ||
} | ||
else if (scorerName == "ke") { | ||
scorer = GetKE; | ||
} | ||
else { | ||
scorer = GetTE; | ||
} // default case | ||
} | ||
|
||
void SpectrumAnalyzer::FillEventFields() const | ||
{ | ||
auto AM = G4AnalysisManager::Instance(); | ||
AM->FillNtupleDColumn(ntupleID, 0, neutronScore); | ||
AM->FillNtupleDColumn(ntupleID, 1, protonScore); | ||
AM->FillNtupleDColumn(ntupleID, 2, pionScore); | ||
AM->FillNtupleDColumn(ntupleID, 3, gammaScore); | ||
AM->FillNtupleDColumn(ntupleID, 4, electronScore); | ||
AM->AddNtupleRow(ntupleID); | ||
} | ||
|
||
void SpectrumAnalyzer::Analyze(const G4Step* step) | ||
{ | ||
auto PDGID = step->GetTrack()->GetParticleDefinition()->GetPDGEncoding(); | ||
auto val = scorer(step); | ||
if (PDGID == 2112 || PDGID == -2112) { | ||
neutronScore += val; | ||
} | ||
else if (PDGID == 2212 || PDGID == 2212) { | ||
protonScore += val; | ||
} | ||
else if (PDGID == 211 || PDGID == 211) { | ||
pionScore += val; | ||
} | ||
else if (PDGID == 22) { | ||
gammaScore += val; | ||
} | ||
else if (PDGID == -11 || PDGID == 11) { | ||
electronScore += val; | ||
} | ||
else { | ||
} | ||
|
||
#ifdef DEBUG | ||
G4cout << "-->SpectrumAnalyzer::Analyze, scorer name " << scorerName << " " << PDGID << " " | ||
<< step->GetTrack()->GetParticleDefinition()->GetParticleName() << " Total Energy " | ||
<< GetTE(step) << " Momentum " << GetMomentum(step) << " Kinetic Energy " << GetKE(step) | ||
<< G4endl; | ||
#endif | ||
} | ||
|
||
//************************************************** |