Skip to content

Commit

Permalink
Fix bug in decaying generators (#235)
Browse files Browse the repository at this point in the history
* Refactor AtVertexPropagator

Instead of counting the event number with three
different variables, we now use a single variable
that is a boolean to distinguish between the
beam-like half of the event and the reacion-like
half of the event.

* Refactor AtVertexPropagator

Make the modification of the boolean more explicit
so the behavior is more clear.

* Add AtReactionGenerator as base for all reactions.

This class covers if the generator should be run
or not depending on which type of event we are
runnning.

It should also handle if there is a child
generator that still needs to run and will only
mark the reaction event complete if all generators
have been run.

* Remove redundant sequential decays

* Allow reaction gen to handle event type

* Fix reaction never generating

Because we are setting the event type at the end
of the generator phase (adding particles to the
primary track), we loose the information about
if it is reaction/beam in the stepping phase where
`ProcessHits` is called. To account for this we
use the trackID to determine what the event type
is. Alternatively, we could check
AtVertexPropogator we would just have to invert it

To account for hehavior in `AtTPC2Body` generator
added flag to `AtReactionGenerator` to allow for
any reaction to always run (even if it is a beam-
like event).

* Add additional tests

* Fix remaining generators

By definition, if an AtReactionGenerator is running
then it can create new particles. We should not
check for if it is a reaction event. That is
handled by the base class only and should be
hidden from users implementing generators.

* Fix formatting

* Fix clang-tidy warnings

* Refactor test system

- Move all tests to the same directory as the source files so they
are easier to locate in the project structure.

---------

Co-authored-by: anthoak13 <[email protected]>
  • Loading branch information
anthoak13 and anthoak13 committed Jun 21, 2024
1 parent 3605006 commit c926e25
Show file tree
Hide file tree
Showing 54 changed files with 398 additions and 1,030 deletions.
File renamed without changes.
1 change: 0 additions & 1 deletion AtData/AtFittedTrack.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ ClassImp(AtFittedTrack);

using XYZVector = ROOT::Math::XYZVector;

AtFittedTrack::AtFittedTrack() {}
const std::tuple<Float_t, Float_t, Float_t, Float_t, Float_t, Float_t, Float_t> AtFittedTrack::GetEnergyAngles()
{
return std::forward_as_tuple(fEnergy, fEnergyXtr, fTheta, fPhi, fEnergyPRA, fThetaPRA, fPhiPRA);
Expand Down
2 changes: 1 addition & 1 deletion AtData/AtFittedTrack.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class AtFittedTrack : public TObject {
Int_t fTrackPoints{0};

public:
AtFittedTrack();
AtFittedTrack() = default;
~AtFittedTrack() = default;

inline void SetTrackID(Int_t trackid) { fTrackID = trackid; }
Expand Down
9 changes: 8 additions & 1 deletion AtData/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ set(SRCS

)

attpcroot_add_test_dir(test)
set(TEST_SRCS
AtBaseEventTest.cxx
)

attpcroot_generate_tests(${LIBRARY_NAME}Tests
SRCS ${TEST_SRCS}
DEPS ${LIBRARY_NAME}
)

generate_target_and_root_library(${LIBRARY_NAME}
LINKDEF ${LINKDEF}
Expand Down
9 changes: 0 additions & 9 deletions AtData/test/CMakeLists.txt

This file was deleted.

8 changes: 4 additions & 4 deletions AtDetectors/AtSiArray/AtSiArrayGeo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ const char *AtSiArrayGeo::getModuleName(Int_t m)
ASCII file should start with AtSiArray otherwise they will
not be constructed
*/
sprintf(modName, "AtSiArray%i", m + 1);
return modName;
modName = TString::Format("AtSiArray%i", m + 1);
return modName.Data();
}

const char *AtSiArrayGeo::getEleName(Int_t m)
{
/** Returns the element name of Det number m */
sprintf(eleName, "AtSiArray%i", m + 1);
return eleName;
eleName = TString::Format("AtSiArray%i", m + 1);
return eleName.Data();
}
4 changes: 2 additions & 2 deletions AtDetectors/AtSiArray/AtSiArrayGeo.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class TMemberInspector;
class AtSiArrayGeo : public FairGeoSet {

protected:
char modName[20]{}; // name of module
char eleName[20]{}; // substring for elements in module
TString modName{}; // name of module
TString eleName{}; // substring for elements in module
public:
AtSiArrayGeo();
~AtSiArrayGeo() {}
Expand Down
16 changes: 7 additions & 9 deletions AtDetectors/AtTpc/AtTpc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,14 @@ void AtTpc::trackEnteringVolume()
fTrackID = gMC->GetStack()->GetCurrentTrackNumber();

// Position of the first hit of the beam in the TPC volume ( For tracking purposes in the TPC)
if (AtVertexPropagator::Instance()->GetBeamEvtCnt() % 2 != 0 && fTrackID == 0 &&
(fVolName == "drift_volume" || fVolName == "cell"))
if (fTrackID == 0 && (fVolName == "drift_volume" || fVolName == "cell"))
InPos = fPosIn;

Int_t VolumeID = 0;

if (AtVertexPropagator::Instance()->GetBeamEvtCnt() % 2 != 0)
if (fTrackID == 0)
LOG(debug) << cGREEN << " AtTPC: Beam Event ";
else if (AtVertexPropagator::Instance()->GetDecayEvtCnt() % 2 == 0)
else
LOG(debug) << cBLUE << " AtTPC: Reaction/Decay Event ";

LOG(debug) << " AtTPC: First hit in Volume " << fVolName;
Expand Down Expand Up @@ -123,8 +122,7 @@ void AtTpc::getTrackParametersWhileExiting()
// Correct fPosOut
if (gMC->IsTrackExiting()) {
correctPosOut();
if ((fVolName.Contains("drift_volume") || fVolName.Contains("cell")) &&
AtVertexPropagator::Instance()->GetBeamEvtCnt() % 2 != 0 && fTrackID == 0)
if ((fVolName.Contains("drift_volume") || fVolName.Contains("cell")) && fTrackID == 0)
resetVertex();
}
}
Expand Down Expand Up @@ -167,7 +165,7 @@ void AtTpc::correctPosOut()
bool AtTpc::reactionOccursHere()
{
bool atEnergyLoss = fELossAcc * 1000 > AtVertexPropagator::Instance()->GetRndELoss();
bool isPrimaryBeam = AtVertexPropagator::Instance()->GetBeamEvtCnt() % 2 != 0 && fTrackID == 0;
bool isPrimaryBeam = fTrackID == 0;
bool isInRightVolume = fVolName.Contains("drift_volume") || fVolName.Contains("cell");
return atEnergyLoss && isPrimaryBeam && isInRightVolume;
}
Expand Down Expand Up @@ -229,10 +227,10 @@ void AtTpc::addHit()

// We assume that the beam-like particle is fTrackID==0 since it is the first one added in the
// primary generator
if (AtVertexPropagator::Instance()->GetBeamEvtCnt() % 2 != 0 && fTrackID == 0) {
if (fTrackID == 0) {
EIni = 0;
AIni = 0;
} else if (AtVertexPropagator::Instance()->GetDecayEvtCnt() % 2 == 0) {
} else {
EIni = AtVertexPropagator::Instance()->GetTrackEnergy(fTrackID);
AIni = AtVertexPropagator::Instance()->GetTrackAngle(fTrackID);
}
Expand Down
14 changes: 14 additions & 0 deletions AtGenerators/AtReactionGenerator.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "AtReactionGenerator.h"

#include "AtVertexPropagator.h"

Bool_t AtReactionGenerator::ReadEvent(FairPrimaryGenerator *primGen)
{
bool isBeamEvent = AtVertexPropagator::Instance()->IsBeamEvent();
if (kIsFinalGen)
AtVertexPropagator::Instance()->EndEvent(); // End the event if this is the final generator

if (!isBeamEvent || kAlwaysRun)
return GenerateReaction(primGen);
return true;
}
25 changes: 25 additions & 0 deletions AtGenerators/AtReactionGenerator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#ifndef ATREACTIONGENERATOR_H
#define ATREACTIONGENERATOR_H

#include <FairGenerator.h>

/**
* This class represents a generator for reaction-like events.
* When the ReadEvent method is called, this generator can assume
* that it's parent vertex is stored in the AtVertexPropagator singleton.
*
*/
class AtReactionGenerator : public FairGenerator {
protected:
bool kIsFinalGen{true}; ///< Flag to indicate if this generator is the final one in the chain
bool kAlwaysRun{false}; ///< Flag to indicate if this generator should always run
public:
virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen) final;
void SetSequentialDecay(Bool_t var) { kIsFinalGen = !var; }
void SetAlwaysRun(Bool_t var) { kAlwaysRun = var; }

protected:
virtual bool GenerateReaction(FairPrimaryGenerator *primGen) = 0;
};

#endif // ATREACTIONGENERATOR_H
51 changes: 51 additions & 0 deletions AtGenerators/AtReactionGeneratorTest.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#include "AtReactionGenerator.h"

#include "AtVertexPropagator.h"

#include <gtest/gtest.h>

class FakeAtReactionGenerator : public AtReactionGenerator {
protected:
bool ranEvent{false};

public:
bool GenerateReaction(FairPrimaryGenerator *primGen) override
{
ranEvent = true;
return true;
}
bool RanEvent() { return ranEvent; }
};

class AtReactionGeneratorTest : public ::testing::Test {
protected:
FakeAtReactionGenerator generator;
void SetUp() override { AtVertexPropagator::Instance()->ResetForTesting(); }
void TearDown() override { AtVertexPropagator::Instance()->ResetForTesting(); }
};

TEST_F(AtReactionGeneratorTest, AtReactionGenerator_ReadEvent_BeamEvent)
{

AtVertexPropagator::Instance()->ResetForTesting();
AtVertexPropagator::Instance()->SetIsBeamEvent(true);
generator.ReadEvent(nullptr);
EXPECT_FALSE(generator.RanEvent());
}

TEST_F(AtReactionGeneratorTest, AtReactionGenerator_ReadEvent_NonBeamEvent)
{

AtVertexPropagator::Instance()->SetIsBeamEvent(false);
generator.ReadEvent(nullptr);
EXPECT_TRUE(generator.RanEvent());
}

TEST_F(AtReactionGeneratorTest, AtReactionGenerator_ReadEvent_AlwaysNonBeamEvent)
{

AtVertexPropagator::Instance()->SetIsBeamEvent(true);
generator.SetAlwaysRun(true);
generator.ReadEvent(nullptr);
EXPECT_TRUE(generator.RanEvent());
}
2 changes: 1 addition & 1 deletion AtGenerators/AtTPC20MgDecay-Rnalpha.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Bool_t AtTPC20MgDecay::Init()
}

// ----- Public method ReadEvent --------------------------------------
Bool_t AtTPC20MgDecay::ReadEvent(FairPrimaryGenerator *primGen)
Bool_t AtTPC20MgDecay::GenerateReaction(FairPrimaryGenerator *primGen)
{

if (fBoxVtxIsSet) {
Expand Down
2 changes: 1 addition & 1 deletion AtGenerators/AtTPC20MgDecay.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Bool_t AtTPC20MgDecay::Init()
}

// ----- Public method ReadEvent --------------------------------------
Bool_t AtTPC20MgDecay::ReadEvent(FairPrimaryGenerator *primGen)
Bool_t AtTPC20MgDecay::GenerateReaction(FairPrimaryGenerator *primGen)
{

if (fBoxVtxIsSet) {
Expand Down
11 changes: 6 additions & 5 deletions AtGenerators/AtTPC20MgDecay.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
#ifndef AtTPC20MGDECAY_H
#define AtTPC20MGDECAY_H

#include "AtReactionGenerator.h"

#include <Rtypes.h> // for Double32_t, Bool_t, THashConsistencyHolder

#include "FairGenerator.h"

class TBuffer;
class TClass;
class TMemberInspector;
class FairPrimaryGenerator;

class AtTPC20MgDecay : public FairGenerator {
class AtTPC20MgDecay : public AtReactionGenerator {
private:
Bool_t fOnlyAPBranch{false}; // True if only the beta-alpha-proton branch is visible
Bool_t fBoxVtxIsSet{false}; // True if box vertex is set
Expand All @@ -31,7 +32,7 @@ class AtTPC20MgDecay : public FairGenerator {
virtual ~AtTPC20MgDecay() = default;

/** Initializer **/
virtual Bool_t Init();
virtual Bool_t Init() override;

void SetXYZ(Double32_t x = 0, Double32_t y = 0, Double32_t z = 0)
{
Expand All @@ -52,13 +53,13 @@ class AtTPC20MgDecay : public FairGenerator {
fBoxVtxIsSet = kTRUE;
}

virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen);
virtual bool GenerateReaction(FairPrimaryGenerator *primGen) override;

void ShowOnlyAlphaProtonBranch() { fOnlyAPBranch = kTRUE; };
void SetNuclearDecayChain() { fNuclearDecayChainIsSet = kTRUE; };
void SetDecayChainPoint(Double32_t ParticleEnergy = 0, Double32_t ParticleBranchingRatio = 0);

ClassDef(AtTPC20MgDecay, 1)
ClassDefOverride(AtTPC20MgDecay, 2)
};

#endif
2 changes: 1 addition & 1 deletion AtGenerators/AtTPC20MgDecay_pag.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Bool_t AtTPC20MgDecay_pag::Init()
}

// ----- Public method ReadEvent --------------------------------------
Bool_t AtTPC20MgDecay_pag::ReadEvent(FairPrimaryGenerator *primGen)
Bool_t AtTPC20MgDecay_pag::GenerateReaction(FairPrimaryGenerator *primGen)
{

if (fBoxVtxIsSet) {
Expand Down
10 changes: 6 additions & 4 deletions AtGenerators/AtTPC20MgDecay_pag.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#ifndef AtTPC20MGDECAY_pag_H
#define AtTPC20MGDECAY_pag_H

#include "AtReactionGenerator.h"

#include <Rtypes.h> // for Double32_t, Bool_t, THashConsistencyHolder

#include "FairGenerator.h"
Expand All @@ -10,7 +12,7 @@ class TClass;
class TMemberInspector;
class FairPrimaryGenerator;

class AtTPC20MgDecay_pag : public FairGenerator {
class AtTPC20MgDecay_pag : public AtReactionGenerator {

public:
/** Default constructor **/
Expand All @@ -20,7 +22,7 @@ class AtTPC20MgDecay_pag : public FairGenerator {
virtual ~AtTPC20MgDecay_pag() = default;

/** Initializer **/
virtual Bool_t Init();
virtual Bool_t Init() override;

void SetXYZ(Double32_t x = 0, Double32_t y = 0, Double32_t z = 0)
{
Expand All @@ -41,7 +43,7 @@ class AtTPC20MgDecay_pag : public FairGenerator {
fBoxVtxIsSet = kTRUE;
}

virtual Bool_t ReadEvent(FairPrimaryGenerator *primGen);
virtual bool GenerateReaction(FairPrimaryGenerator *primGen) override;

void ShowOnlyAlphaProtonBranch() { fOnlyAPBranch = kTRUE; };
void SetNuclearDecayChain() { fNuclearDecayChainIsSet = kTRUE; };
Expand All @@ -57,7 +59,7 @@ class AtTPC20MgDecay_pag : public FairGenerator {
Int_t fParticlesDefinedInNuclearDecay;
Double32_t fParticleEnergies[3] = {0, 0, 0};
Double32_t fParticleBranchingRatios[3] = {0, 0, 0};
ClassDef(AtTPC20MgDecay_pag, 1)
ClassDefOverride(AtTPC20MgDecay_pag, 2)
};

#endif
Loading

0 comments on commit c926e25

Please sign in to comment.