Skip to content

Commit b392d4f

Browse files
authored
Merge pull request #4 from shahor02/cosm_field
Detect magnetic field in Init()
2 parents cda0995 + 2c1f974 commit b392d4f

File tree

2 files changed

+26
-15
lines changed

2 files changed

+26
-15
lines changed

GeneratorCosmics/GeneratorCosmics.cxx

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,24 @@ GeneratorCosmics::GeneratorCosmics() : TGenerator("GeneratorCosmics", "Generator
2424
{
2525
}
2626

27+
//-----------------------------------------------------------------------------
28+
bool GeneratorCosmics::detectField()
29+
{
30+
//
31+
auto fld = TGeoGlobalMagField::Instance()->GetField();
32+
if (TVirtualMC::GetMC() && TVirtualMC::GetMC()->GetMagField()) {
33+
fld = TVirtualMC::GetMC()->GetMagField();
34+
}
35+
if (fld) {
36+
double r[3] = {0, 0, 0}, b[3] = {0, 0, 0};
37+
fld->Field(r, b);
38+
setBkG(b[2]);
39+
return true;
40+
}
41+
return false;
42+
}
43+
44+
2745
//-----------------------------------------------------------------------------
2846
void GeneratorCosmics::GenerateEvent()
2947
{
@@ -32,19 +50,8 @@ void GeneratorCosmics::GenerateEvent()
3250
constexpr int MuMinusPDG = 13, MuPlusPDG = -13;
3351
constexpr float MuMass = 0.1056583;
3452
//
35-
if (!mFieldIsSet) {
36-
auto fld = TGeoGlobalMagField::Instance()->GetField();
37-
if (TVirtualMC::GetMC() && TVirtualMC::GetMC()->GetMagField()) {
38-
fld = TVirtualMC::GetMC()->GetMagField();
39-
}
40-
if (fld) {
41-
double r[3] = {0, 0, 0}, b[3] = {0, 0, 0};
42-
fld->Field(r, b);
43-
setBkG(b[2]);
44-
}
45-
else {
46-
throw std::runtime_error("Failed to fetch magnetic field");
47-
}
53+
if (!mFieldIsSet && !detectField()) {
54+
throw std::runtime_error("Failed to fetch magnetic field");
4855
}
4956

5057
fParticles->Clear();
@@ -157,11 +164,12 @@ void GeneratorCosmics::Init()
157164
mGenFun.reset(new TF1("genFun", "x/(1.+(x/3.)*(x/3.))^1.", mPMin, mPMax));
158165
break;
159166
}
160-
161167
printf("Cosmics generator configuration:\n");
162168
printf("Parameterization type: %d with %e < p < %e\n", int(mParam), mPMin, mPMax);
163169
printf("Tracks created at R=%.2f and requested to have |X|<%.2f and |Z|<%.2f at Y=0\n", mROrigin, mXAcc, mZAcc);
164-
printf("Magnetic field %f\n", mBkG);
170+
if (detectField()) {
171+
printf("Magnetic field %f\n", mBkG);
172+
}
165173
return;
166174
}
167175

GeneratorCosmics/GeneratorCosmics.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ class GeneratorCosmics : public TGenerator
8484
bool getXZatOrigin(float& xpos, float& zpos, const float r[3], const float p[3], int q) const;
8585

8686
private:
87+
88+
bool detectField();
89+
8790
GenParamType mParam = GenParamType::ParamTPC;
8891
std::unique_ptr<TF1> mGenFun;
8992

0 commit comments

Comments
 (0)