Skip to content

Commit d4a5ca7

Browse files
committed
GeneratorHybrid: improve unit treatment
So far, units are treated solely in the Generator::addTrack function. This works well for fundamental generators. However, the hybrid generator is a meta generator potentially consisting of a collection of underlying generators that may have completely different units. This may currently lead to wrong generator output, in certain cases. This commit fixes these bugs and introduces unit handling within GeneratorHybrid.
1 parent 8f6726b commit d4a5ca7

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

Generators/include/Generators/Generator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,13 @@ class Generator : public FairGenerator
7878

7979
/** setters **/
8080
void setMomentumUnit(double val) { mMomentumUnit = val; };
81+
double getMomentumUnit() const { return mMomentumUnit; }
8182
void setEnergyUnit(double val) { mEnergyUnit = val; };
83+
double getEnergyUnit() const { return mEnergyUnit; }
8284
void setPositionUnit(double val) { mPositionUnit = val; };
85+
double getPositionUnit() const { return mPositionUnit; }
8386
void setTimeUnit(double val) { mTimeUnit = val; };
87+
double getTimeUnit() const { return mTimeUnit; }
8488
void setBoost(Double_t val) { mBoost = val; };
8589
void setTriggerMode(ETriggerMode_t val) { mTriggerMode = val; };
8690
void addTrigger(Trigger trigger) { mTriggers.push_back(trigger); };

Generators/include/Generators/GeneratorHybrid.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ class GeneratorHybrid : public Generator
5454
{
5555

5656
public:
57-
GeneratorHybrid() = default;
5857
GeneratorHybrid(const std::string& inputgens);
5958
~GeneratorHybrid();
6059

Generators/src/GeneratorHybrid.cxx

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ namespace eventgen
2525

2626
GeneratorHybrid::GeneratorHybrid(const std::string& inputgens)
2727
{
28+
// This generator has trivial unit conversions
29+
setTimeUnit(1.);
30+
setPositionUnit(1.);
31+
setMomentumUnit(1.);
32+
setEnergyUnit(1.);
33+
2834
if (!parseJSON(inputgens)) {
2935
LOG(fatal) << "Failed to parse JSON configuration from input generators";
3036
exit(1);
@@ -382,6 +388,27 @@ bool GeneratorHybrid::importParticles()
382388
}
383389
}
384390
}
391+
392+
auto unit_transformer = [](auto& p, auto pos_unit, auto time_unit, auto en_unit, auto mom_unit) {
393+
p.SetMomentum(p.Px() * mom_unit, p.Py() * mom_unit, p.Pz() * mom_unit, p.Energy() * en_unit);
394+
p.SetProductionVertex(p.Vx() * pos_unit, p.Vy() * pos_unit, p.Vz() * pos_unit, p.T() * time_unit);
395+
};
396+
397+
auto index_transformer = [](auto& p, int offset) {
398+
for (int i = 0; i < 2; ++i) {
399+
if (p.GetMother(i) != -1) {
400+
const auto newindex = p.GetMother(i) + offset;
401+
p.SetMother(i, newindex);
402+
}
403+
}
404+
if (p.GetNDaughters() > 0) {
405+
for (int i = 0; i < 2; ++i) {
406+
const auto newindex = p.GetDaughter(i) + offset;
407+
p.SetDaughter(i, newindex);
408+
}
409+
}
410+
};
411+
385412
// Clear particles and event header
386413
mParticles.clear();
387414
mMCEventHeader.clearInfo();
@@ -391,23 +418,20 @@ bool GeneratorHybrid::importParticles()
391418
LOG(info) << "Importing particles for task " << subIndex;
392419
auto subParticles = gens[subIndex]->getParticles();
393420

421+
auto time_unit = gens[subIndex]->getTimeUnit();
422+
auto pos_unit = gens[subIndex]->getPositionUnit();
423+
auto mom_unit = gens[subIndex]->getMomentumUnit();
424+
auto energy_unit = gens[subIndex]->getEnergyUnit();
425+
394426
// The particles carry mother and daughter indices, which are relative
395427
// to the sub-generator. We need to adjust these indices to reflect that particles
396428
// are now embedded into a cocktail.
397429
auto offset = mParticles.size();
398430
for (auto& p : subParticles) {
399-
for (int i = 0; i < 2; ++i) {
400-
if (p.GetMother(i) != -1) {
401-
const auto newindex = p.GetMother(i) + offset;
402-
p.SetMother(i, newindex);
403-
}
404-
}
405-
if (p.GetNDaughters() > 0) {
406-
for (int i = 0; i < 2; ++i) {
407-
const auto newindex = p.GetDaughter(i) + offset;
408-
p.SetDaughter(i, newindex);
409-
}
410-
}
431+
// apply the mother-daugher index transformation
432+
index_transformer(p, offset);
433+
// apply unit transformation of sub-generator
434+
unit_transformer(p, pos_unit, time_unit, energy_unit, mom_unit);
411435
}
412436

413437
mParticles.insert(mParticles.end(), subParticles.begin(), subParticles.end());
@@ -420,6 +444,18 @@ bool GeneratorHybrid::importParticles()
420444
LOG(info) << "Importing particles for task " << genIndex;
421445
// at this moment the mIndex-th generator is ready to be used
422446
mParticles = gens[genIndex]->getParticles();
447+
448+
auto time_unit = gens[genIndex]->getTimeUnit();
449+
auto pos_unit = gens[genIndex]->getPositionUnit();
450+
auto mom_unit = gens[genIndex]->getMomentumUnit();
451+
auto energy_unit = gens[genIndex]->getEnergyUnit();
452+
453+
// transform units to units of the hybrid generator
454+
for (auto& p : mParticles) {
455+
// apply unit transformation
456+
unit_transformer(p, pos_unit, time_unit, energy_unit, mom_unit);
457+
}
458+
423459
// fetch the event Header information from the underlying generator
424460
gens[genIndex]->updateHeader(&mMCEventHeader);
425461
mInputTaskQueue.push(genIndex);

0 commit comments

Comments
 (0)