Skip to content

Commit

Permalink
allow call Particle:: SetMass() and SetCharge() only if user knows wh…
Browse files Browse the repository at this point in the history
…at one does
  • Loading branch information
lubynets committed Aug 5, 2024
1 parent 49ba4ab commit ae24214
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
12 changes: 12 additions & 0 deletions core/Particle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ void Particle::SetPid(PdgCode_t pid) {
charge_ = GetChargeByPdgId(pid);
}
}

void Particle::CheckIsAllowedSetMassAndChargeExplicitly() const {
if(!is_allowed_set_charge_and_mass_explicitly_) {
std::string message = "Particle::CheckIsAllowedSetMassAndChargeExplicitly(): ";
message += "mass and charge of the particle are set automatically with SetPid() call ";
message += "(unless they were already assigned with some values, incl. when copied content from Track to Particle). ";
message += "Use SetMass() and SetCharge() only if you want to set them different from PDG-true values. ";
message += "To unblock this possibility use SetIsAllowedSetMassAndChargeExplicitly() function.";
throw std::runtime_error(message);
}
}

}// namespace AnalysisTree
11 changes: 11 additions & 0 deletions core/Particle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,21 @@ class Particle : public Track {
ANALYSISTREE_ATTR_NODISCARD Floating_t GetMass() const { return mass_; }

void SetMass(Floating_t mass) {
CheckIsAllowedSetMassAndChargeExplicitly();
mass_ = mass;
}

void SetCharge(Int_t charge) {
CheckIsAllowedSetMassAndChargeExplicitly();
Track::SetCharge(charge);
}

void SetPid(PdgCode_t pid);

void SetIsAllowedSetMassAndChargeExplicitly(bool is=true) { is_allowed_set_charge_and_mass_explicitly_ = is; }

void CheckIsAllowedSetMassAndChargeExplicitly() const;

ANALYSISTREE_ATTR_NODISCARD Floating_t GetEnergy() const { return sqrt(mass_ * mass_ + GetP() * GetP()); }
ANALYSISTREE_ATTR_NODISCARD Floating_t GetKineticEnergy() const { return GetEnergy() - mass_; }

Expand Down Expand Up @@ -90,6 +100,7 @@ class Particle : public Track {
protected:
Floating_t mass_{-1000.f};
PdgCode_t pid_{0};
bool is_allowed_set_charge_and_mass_explicitly_{false};//!

ClassDefOverride(Particle, 2);
};
Expand Down

0 comments on commit ae24214

Please sign in to comment.