Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement modular biasing options #83

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
28bcb8a
increased npoints default values
lobis Nov 21, 2022
2eef4e3
Merge branch 'master' of https://github.com/rest-for-physics/geant4lib
lobis Nov 24, 2022
a9703dc
Merge branch 'master' of https://github.com/rest-for-physics/geant4lib
lobis Nov 30, 2022
b1a32fc
Merge branch 'master' of https://github.com/rest-for-physics/geant4lib
lobis Dec 12, 2022
72995ac
add prototype for biasing info
lobis Dec 15, 2022
dea6365
Merge branch 'master' into lobis-gamma-biasing
lobis Feb 22, 2023
f50bc67
attempt to integrate with TRestGeant4Metadata
lobis Feb 22, 2023
a786f9c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 22, 2023
cb92149
attempt to fix pipeline failure
lobis Feb 22, 2023
624ec7f
fSplittingFactor set to int
lobis Feb 23, 2023
3bc9b3d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 23, 2023
6ce4b60
support for old biasing
lobis Feb 23, 2023
fba6440
Merge remote-tracking branch 'origin/lobis-gamma-biasing' into lobis-…
lobis Feb 23, 2023
2fa4b79
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 23, 2023
c4ae5a3
add generic "cosmic" distribution (power -2.7)
lobis Feb 27, 2023
631af89
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 27, 2023
c1b8cec
add option to save kill volume energy
lobis Feb 27, 2023
ead68a1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 27, 2023
906b6ae
Merge remote-tracking branch 'origin/master' into lobis-gamma-biasing
lobis Feb 27, 2023
d291dfb
working biasing example
lobis Feb 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions inc/TRestGeant4BiasingInfo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@

#ifndef REST_TRESTGEANT4BIASINGINFO_H
#define REST_TRESTGEANT4BIASINGINFO_H

#include <TVector3.h>

#include <set>
#include <string>

class TRestGeant4BiasingInfo {
ClassDef(TRestGeant4BiasingInfo, 1);

private:
bool fEnabled = false;
int fSplittingFactor = 1;
bool fBiasOncePerTrack = true;
std::set<std::string> fBiasingVolumes;
TVector3 fBiasingCenter;

public:
inline TRestGeant4BiasingInfo() = default;

inline ~TRestGeant4BiasingInfo() = default;

inline void SetEnabled(bool enabled) { fEnabled = enabled; }

inline bool GetEnabled() const { return fEnabled; }

inline void SetSplittingFactor(unsigned int splittingFactor) { fSplittingFactor = splittingFactor; }

inline int GetSplittingFactor() const { return fSplittingFactor; }

inline void SetBiasOncePerTrack(bool biasOncePerTrack) { fBiasOncePerTrack = biasOncePerTrack; }

inline bool GetBiasOncePerTrack() const { return fBiasOncePerTrack; }

inline void AddBiasingVolume(const std::string& volumeName) { fBiasingVolumes.insert(volumeName); }

inline void ClearBiasingVolumes() { fBiasingVolumes.clear(); }

inline std::set<std::string> GetBiasingVolumes() const { return fBiasingVolumes; }

inline void SetBiasingCenter(const TVector3& biasingCenter) { fBiasingCenter = biasingCenter; }

inline TVector3 GetBiasingCenter() const { return fBiasingCenter; }
};

#endif // REST_TRESTGEANT4BIASINGINFO_H
14 changes: 14 additions & 0 deletions inc/TRestGeant4GeometryInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,17 @@ class TRestGeant4GeometryInfo {
void PopulateFromGdml(const TString&);

TString GetAlternativeNameFromGeant4PhysicalName(const TString&) const;

TString GetGeant4PhysicalNameFromAlternativeName(const TString&) const;

std::vector<TString> GetAllLogicalVolumes() const;

std::vector<TString> GetAllPhysicalVolumes() const;

std::vector<TString> GetAllAlternativePhysicalVolumes() const;

std::vector<TString> GetAllLogicalVolumesMatchingExpression(const TString&) const;

std::vector<TString> GetAllPhysicalVolumesMatchingExpression(const TString&) const;

inline bool IsValidGdmlName(const TString& volume) const {
Expand All @@ -71,16 +75,25 @@ class TRestGeant4GeometryInfo {
inline bool IsValidPhysicalVolume(const TString& volume) const {
return fPhysicalToLogicalVolumeMap.count(volume) > 0;
}

inline bool IsValidLogicalVolume(const TString& volume) const {
return fLogicalToPhysicalMap.count(volume) > 0;
}

inline std::vector<TString> GetAllPhysicalVolumesFromLogical(const TString& logicalVolume) const {
if (IsValidLogicalVolume(logicalVolume)) {
return fLogicalToPhysicalMap.at(logicalVolume);
}
return {};
}

inline TString GetLogicalVolumeFromPhysical(const TString& physicalVolume) const {
if (IsValidPhysicalVolume(physicalVolume)) {
return fPhysicalToLogicalVolumeMap.at(physicalVolume);
}
return {};
}

inline TVector3 GetPosition(const TString& volume) const {
return fPhysicalToPositionInWorldMap.at(volume);
}
Expand All @@ -90,6 +103,7 @@ class TRestGeant4GeometryInfo {
void InsertVolumeName(Int_t id, const TString& volumeName);

TString GetVolumeFromID(Int_t id) const;

Int_t GetIDFromVolume(const TString& volumeName) const;

void Print() const;
Expand Down
23 changes: 22 additions & 1 deletion inc/TRestGeant4Metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include <iostream>
#include <vector>

#include "TRestGeant4BiasingInfo.h"
#include "TRestGeant4BiasingVolume.h"
#include "TRestGeant4GeometryInfo.h"
#include "TRestGeant4ParticleSource.h"
Expand All @@ -51,9 +52,11 @@ class TRestGeant4Metadata : public TRestMetadata {
void InitFromConfigFile() override;

void ReadGenerator();

void ReadParticleSource(TRestGeant4ParticleSource* src, TiXmlElement* sourceDefinition);

void ReadDetector();

void ReadBiasing();

bool fDetectorSectionInitialized = false; //!
Expand All @@ -64,6 +67,9 @@ class TRestGeant4Metadata : public TRestMetadata {
/// Class used to store and retrieve physics info such as process names or particle names
TRestGeant4PhysicsInfo fGeant4PhysicsInfo;

/// Class used to store and retrieve biasing info
TRestGeant4BiasingInfo fGeant4BiasingInfo;

/// Class used to store and retrieve Geant4 primary generator info
TRestGeant4PrimaryGeneratorInfo fGeant4PrimaryGeneratorInfo;

Expand Down Expand Up @@ -152,6 +158,8 @@ class TRestGeant4Metadata : public TRestMetadata {

std::set<std::string> fKillVolumes;

bool fKillVolumesComputeEnergy = false;

/// If this parameter is set to 'true' it will print out on screen every time 10k events are reached.
Bool_t fPrintProgress = false; //!

Expand All @@ -175,6 +183,9 @@ class TRestGeant4Metadata : public TRestMetadata {
/// \brief Returns an immutable reference to the physics info
inline const TRestGeant4PhysicsInfo& GetGeant4PhysicsInfo() const { return fGeant4PhysicsInfo; }

/// \brief Returns an immutable reference to the biasing info
inline const TRestGeant4BiasingInfo& GetGeant4BiasingInfo() const { return fGeant4BiasingInfo; }

/// \brief Returns an immutable reference to the primary generator info
inline const TRestGeant4PrimaryGeneratorInfo& GetGeant4PrimaryGeneratorInfo() const {
return fGeant4PrimaryGeneratorInfo;
Expand Down Expand Up @@ -332,6 +343,10 @@ class TRestGeant4Metadata : public TRestMetadata {

inline bool IsKillVolume(const char* volumeName) const { return fKillVolumes.count(volumeName) > 0; }

inline bool GetKillVolumesComputeEnergy() const { return fKillVolumesComputeEnergy; }

void SetKillVolumesComputeEnergy(bool computeEnergy) { fKillVolumesComputeEnergy = computeEnergy; }

inline std::vector<std::string> GetKillVolumes() const {
std::vector<std::string> result;
for (const auto& volume : fKillVolumes) {
Expand All @@ -349,7 +364,9 @@ class TRestGeant4Metadata : public TRestMetadata {
}

Double_t GetCosmicFluxInCountsPerCm2PerSecond() const;

Double_t GetCosmicIntensityInCountsPerSecond() const;

Double_t GetEquivalentSimulatedTime() const;

/// Returns a std::string with the name of the active volume with index n
Expand All @@ -375,15 +392,19 @@ class TRestGeant4Metadata : public TRestMetadata {
void PrintMetadata() override;

TRestGeant4Metadata();

TRestGeant4Metadata(const char* configFilename, const std::string& name = "");

~TRestGeant4Metadata();

ClassDefOverride(TRestGeant4Metadata, 13);
ClassDefOverride(TRestGeant4Metadata, 14);

// Allow modification of otherwise inaccessible / immutable members that shouldn't be modified by the user
friend class SteppingAction;

friend class DetectorConstruction;

friend class TRestGeant4Hits;
};

#endif // RestCore_TRestGeant4Metadata
2 changes: 1 addition & 1 deletion inc/TRestGeant4PrimaryGeneratorInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ EnergyDistributionTypes StringToEnergyDistributionTypes(const std::string&);

enum class EnergyDistributionFormulas {
COSMIC_NEUTRONS,
COSMIC_GAMMAS,
COSMIC,
};

std::string EnergyDistributionFormulasToString(const EnergyDistributionFormulas&);
Expand Down
2 changes: 2 additions & 0 deletions src/TRestGeant4BiasingInfo.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

#include "TRestGeant4BiasingInfo.h"
63 changes: 53 additions & 10 deletions src/TRestGeant4Metadata.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,8 @@
/// to stop a track after entering the volume. The track will be immediately killed and its energy deposition
/// won't be computed. This is useful to speed up certain kinds of simulations.
///
/// If "killVolumesComputeEnergy" option is enabled,
/// the energy deposition in the volumes marked as kill will be computed towards the sensitive volume.
/// ## 4. The biasing volumes section (optional)
///
/// The REST Geant4 toolkit (*restG4*) implements a particular biasing
Expand Down Expand Up @@ -846,13 +848,16 @@ void TRestGeant4Metadata::InitFromConfigFile() {
fPrintProgress = ToUpper(GetParameter("printProgress", "true")) == "TRUE" ||
ToUpper(GetParameter("printProgress", "on")) == "ON";

fKillVolumesComputeEnergy = ToUpper(GetParameter("killVolumesComputeEnergy", "true")) == "TRUE" ||
ToUpper(GetParameter("killVolumesComputeEnergy", "on")) == "ON";

fRegisterEmptyTracks = ToUpper(GetParameter("registerEmptyTracks", "false")) == "TRUE" ||
ToUpper(GetParameter("registerEmptyTracks", "off")) == "ON";

ReadBiasing();
ReadGenerator();
// Detector (old storage) section is processed after initializing geometry info in Detector Construction
// This allows to use regular expression to match logical or physical volumes etc.
ReadBiasing();

fMaxTargetStepSize = GetDblParameterWithUnits("maxTargetStepSize", -1);
if (fMaxTargetStepSize > 0) {
Expand Down Expand Up @@ -949,20 +954,62 @@ Double_t TRestGeant4Metadata::GetEquivalentSimulatedTime() const {
return seconds;
}

/*
* OLD IMPLEMENTATION
*
void TRestGeant4Metadata::ReadBiasing() {

}
*/

void TRestGeant4Metadata::ReadBiasing() {
TiXmlElement* biasingDefinition = GetElement("biasing");
if (biasingDefinition == nullptr) {
fNBiasingVolumes = 0;
fGeant4BiasingInfo.SetEnabled(false);
return;
}
TString biasEnabled = GetFieldValue("value", biasingDefinition);
TString biasType = GetFieldValue("type", biasingDefinition);
string biasEnabled = GetFieldValue("value", biasingDefinition);
// biasEnabled to lowercase
std::transform(biasEnabled.begin(), biasEnabled.end(), biasEnabled.begin(), ::tolower);
if (biasEnabled == "off") {
fNBiasingVolumes = 0;
fGeant4BiasingInfo.SetEnabled(false);
return;
}

RESTInfo << "Biasing is enabled" << RESTendl;

RESTDebug << "Bias: " << biasEnabled << RESTendl;
string biasType = GetFieldValue("type", biasingDefinition);

if (biasEnabled == "on" || biasEnabled == "ON" || biasEnabled == "On" || biasEnabled == "oN") {
RESTInfo << "Biasing is enabled" << RESTendl;
if (biasType == "split") {
fGeant4BiasingInfo.SetEnabled(true);

const int splittingFactor = StringToInteger(GetParameter("splittingFactor", biasingDefinition, "1"));

if (splittingFactor <= 1) {
RESTError << "Parameter 'splittingFactor' in biasing section must be set to an integer > 1"
<< RESTendl;
exit(1);
}

fGeant4BiasingInfo.SetSplittingFactor(splittingFactor);

const TVector3 center = StringTo3DVector(GetParameter("center", biasingDefinition, "(0,0,0)"));
fGeant4BiasingInfo.SetBiasingCenter(center);

TiXmlElement* volumeDefinition = GetElement("volume", biasingDefinition);
while (volumeDefinition != nullptr) {
string name = GetFieldValue("name", volumeDefinition);
if (name.empty() || name == "Not defined") {
RESTError << "volume inside biasing section defined without name" << RESTendl;
exit(1);
}
fGeant4BiasingInfo.AddBiasingVolume(name);
volumeDefinition = GetNextElement(volumeDefinition);
}
} else {
// default implementation
TiXmlElement* biasVolumeDefinition = GetElement("biasingVolume", biasingDefinition);
Int_t n = 0;
while (biasVolumeDefinition) {
Expand All @@ -976,10 +1023,6 @@ void TRestGeant4Metadata::ReadBiasing() {
biasVolume.SetEnergyRange(Get2DVectorParameterWithUnits("energyRange", biasVolumeDefinition));
biasVolume.SetBiasingVolumeType(biasType); // For the moment all the volumes should be same type

/* TODO check that values are right if not printBiasingVolume with
getchar() biasVolume.PrintBiasingVolume(); getchar();
*/

fBiasingVolumes.push_back(biasVolume);
biasVolumeDefinition = GetNextElement(biasVolumeDefinition);
n++;
Expand Down
20 changes: 12 additions & 8 deletions src/TRestGeant4PrimaryGeneratorInfo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ string TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToString(
switch (type) {
case EnergyDistributionFormulas::COSMIC_NEUTRONS:
return "CosmicNeutrons";
case EnergyDistributionFormulas::COSMIC_GAMMAS:
return "CosmicGammas";
case EnergyDistributionFormulas::COSMIC:
return "Cosmic";
}
cout << "TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToString - Error - Unknown energy "
"distribution formula"
Expand All @@ -177,10 +177,9 @@ EnergyDistributionFormulas TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistr
if (TString(type).EqualTo(EnergyDistributionFormulasToString(EnergyDistributionFormulas::COSMIC_NEUTRONS),
TString::ECaseCompare::kIgnoreCase)) {
return EnergyDistributionFormulas::COSMIC_NEUTRONS;
} else if (TString(type).EqualTo(
EnergyDistributionFormulasToString(EnergyDistributionFormulas::COSMIC_GAMMAS),
TString::ECaseCompare::kIgnoreCase)) {
return EnergyDistributionFormulas::COSMIC_GAMMAS;
} else if (TString(type).EqualTo(EnergyDistributionFormulasToString(EnergyDistributionFormulas::COSMIC),
TString::ECaseCompare::kIgnoreCase)) {
return EnergyDistributionFormulas::COSMIC;
} else {
cout << "TRestGeant4PrimaryGeneratorTypes::StringToEnergyDistributionFormulas - Error - Unknown "
"energyDistributionFormulas: "
Expand All @@ -207,8 +206,13 @@ TF1 TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula(
distribution.GetXaxis()->SetTitle("Energy (keV)");
return distribution;
}
case EnergyDistributionFormulas::COSMIC_GAMMAS:
exit(1);
case EnergyDistributionFormulas::COSMIC:
const char* title = "Cosmic distribution aproximation";
auto distribution = TF1(title, "TMath::Power(x, -2.7)", 1E2, 1E9);
distribution.SetNormalized(true); // Normalized, not really necessary
distribution.SetTitle(title);
distribution.GetXaxis()->SetTitle("Energy (keV)");
return distribution;
}
cout << "TRestGeant4PrimaryGeneratorTypes::EnergyDistributionFormulasToRootFormula - Error - Unknown "
"energy distribution formula"
Expand Down