Skip to content

Commit 2bac47b

Browse files
authored
Merge pull request #103 from rest-for-physics/lobis-isotropic
Add option for angular range for isotropic generator
2 parents da06979 + ed539c9 commit 2bac47b

4 files changed

Lines changed: 52 additions & 11 deletions

File tree

inc/TRestGeant4Particle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ class TRestGeant4Particle {
5757

5858
void SetParticleCharge(Int_t charge) { fCharge = charge; }
5959

60-
void SetDirection(TVector3 dir) { fDirection = dir; }
60+
void SetDirection(const TVector3& dir) { fDirection = dir.Unit(); }
6161
void SetEnergy(Double_t en) { fEnergy = en; }
62-
void SetOrigin(TVector3 pos) { fOrigin = pos; }
62+
void SetOrigin(const TVector3& pos) { fOrigin = pos; }
6363

6464
void Print() const;
6565

inc/TRestGeant4ParticleSource.h

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada
4242
size_t fAngularDistributionFormulaNPoints = 500;
4343
TF1* fAngularDistributionFunction = nullptr;
4444
TVector2 fAngularDistributionRange;
45+
double fAngularDistributionIsotropicConeHalfAngle = 0;
4546

4647
TString fEnergyDistributionType = "Mono";
4748
TString fEnergyDistributionFilename;
@@ -65,14 +66,7 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada
6566
virtual void InitFromConfigFile() override;
6667
static TRestGeant4ParticleSource* instantiate(std::string model = "");
6768

68-
inline TVector3 GetDirection() const {
69-
if (fDirection.Mag() <= 0) {
70-
std::cout << "TRestGeant4ParticleSource::GetDirection: "
71-
<< "Direction cannot be the zero vector" << std::endl;
72-
exit(1);
73-
}
74-
return fDirection;
75-
}
69+
TVector3 GetDirection() const;
7670

7771
inline TString GetEnergyDistributionType() const { return fEnergyDistributionType; }
7872
inline TVector2 GetEnergyDistributionRange() const { return fEnergyDistributionRange; }
@@ -91,6 +85,9 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada
9185
inline TString GetAngularDistributionFilename() const { return fAngularDistributionFilename; }
9286
inline TString GetAngularDistributionNameInFile() const { return fAngularDistributionNameInFile; }
9387
inline const TF1* GetAngularDistributionFunction() const { return fAngularDistributionFunction; }
88+
inline double GetAngularDistributionIsotropicConeHalfAngle() const {
89+
return fAngularDistributionIsotropicConeHalfAngle;
90+
}
9491

9592
inline const TF2* GetEnergyAndAngularDistributionFunction() const {
9693
return fEnergyAndAngularDistributionFunction;
@@ -100,6 +97,16 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada
10097

10198
inline std::vector<TRestGeant4Particle> GetParticles() const { return fParticles; }
10299

100+
inline void SetAngularDistributionIsotropicConeHalfAngle(double angle) {
101+
if (angle < 0 || angle > TMath::Pi()) {
102+
std::cerr << "TRestGeant4ParticleSource::SetAngularDistributionIsotropicConeHalfAngle: "
103+
"angle must be between 0 and Pi radians"
104+
<< std::endl;
105+
exit(1);
106+
}
107+
fAngularDistributionIsotropicConeHalfAngle = angle;
108+
}
109+
103110
inline void SetAngularDistributionType(const TString& type) {
104111
fAngularDistributionType = TRestGeant4PrimaryGeneratorTypes::AngularDistributionTypesToString(
105112
TRestGeant4PrimaryGeneratorTypes::StringToAngularDistributionTypes(type.Data()));
@@ -201,6 +208,6 @@ class TRestGeant4ParticleSource : public TRestGeant4Particle, public TRestMetada
201208
// Destructor
202209
virtual ~TRestGeant4ParticleSource();
203210

204-
ClassDefOverride(TRestGeant4ParticleSource, 5);
211+
ClassDefOverride(TRestGeant4ParticleSource, 6);
205212
};
206213
#endif

src/TRestGeant4Metadata.cxx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,11 @@
461461
/// <angular type="isotropic" />
462462
/// \endcode
463463
///
464+
/// Additionally a direction and a cone angle can be specified to generate the particles in a cone around the
465+
/// direction. The angle specified is the half opening angle of the cone. \code
466+
/// <angular type="isotropic" direction="(0,1,0)" coneHalfAngle="30deg"/>
467+
/// \endcode
468+
///
464469
/// * **backtoback**: The source momentum direction will be opposite to
465470
/// the direction of the previous source. If it is the first source the
466471
/// angular distribution type will be re-defined to isotropic.
@@ -1146,6 +1151,12 @@ void TRestGeant4Metadata::ReadParticleSource(TRestGeant4ParticleSource* source,
11461151
}
11471152
source->SetDirection(StringTo3DVector(GetParameter("direction", angularDefinition, "(1,0,0)")));
11481153

1154+
if ((StringToAngularDistributionTypes(source->GetAngularDistributionType().Data()) ==
1155+
AngularDistributionTypes::ISOTROPIC)) {
1156+
source->SetAngularDistributionRange({0, TMath::Pi()});
1157+
const double coneHalfAngle = GetDblParameterWithUnits("coneHalfAngle", angularDefinition, 0);
1158+
source->SetAngularDistributionIsotropicConeHalfAngle(coneHalfAngle);
1159+
}
11491160
// Energy distribution parameters
11501161
TiXmlElement* energyDefinition = GetElement("energy", sourceDefinition);
11511162
if (energyDefinition == nullptr) {

src/TRestGeant4ParticleSource.cxx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,17 @@ void TRestGeant4ParticleSource::PrintMetadata() {
5656
}
5757
RESTMetadata << "Angular distribution direction: (" << GetDirection().X() << "," << GetDirection().Y()
5858
<< "," << GetDirection().Z() << ")" << RESTendl;
59+
if ((StringToAngularDistributionTypes(GetAngularDistributionType().Data()) ==
60+
AngularDistributionTypes::ISOTROPIC)) {
61+
if (GetAngularDistributionIsotropicConeHalfAngle() != 0) {
62+
const double solidAngle =
63+
2 * TMath::Pi() * (1 - TMath::Cos(GetAngularDistributionIsotropicConeHalfAngle()));
64+
RESTMetadata << "Angular distribution isotropic cone half angle (deg): "
65+
<< GetAngularDistributionIsotropicConeHalfAngle() * TMath::RadToDeg()
66+
<< " - solid angle: " << solidAngle << " ("
67+
<< solidAngle / (4 * TMath::Pi()) * 100 << "%)" << RESTendl;
68+
}
69+
}
5970
if ((StringToAngularDistributionTypes(GetAngularDistributionType().Data()) ==
6071
AngularDistributionTypes::FORMULA) ||
6172
StringToAngularDistributionTypes(GetAngularDistributionType().Data()) ==
@@ -146,6 +157,18 @@ void TRestGeant4ParticleSource::Update() {
146157
}
147158
}
148159

160+
TVector3 TRestGeant4ParticleSource::GetDirection() const {
161+
// direction should be unit (normalized) vector with a tolerance of 0.001
162+
163+
const double magnitude = fDirection.Mag();
164+
constexpr double tolerance = 0.001;
165+
if (TMath::Abs(magnitude - 1) > tolerance) {
166+
cerr << "TRestGeant4ParticleSource::GetDirection: Direction must be unit vector" << endl;
167+
exit(1);
168+
}
169+
170+
return fDirection;
171+
}
149172
///////////////////////////////////////////////
150173
/// \brief Reads an input file produced by Decay0.
151174
///

0 commit comments

Comments
 (0)