Skip to content

Commit 53759d9

Browse files
authored
Charge field (#132)
* add charge field as default for track and particle * fix bug of prev. commit * check if field to be added doesn't already exist; bugfix: add setter for charge field * apply clang format
1 parent 99200ac commit 53759d9

9 files changed

Lines changed: 50 additions & 4 deletions

File tree

core/BranchConfig.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ BranchConfig::BranchConfig(std::string name, DetType type) : name_(std::move(nam
2525
VectorConfig<float>::AddField("phi", TrackFields::kPhi, "azimuthal angle");
2626
VectorConfig<float>::AddField("eta", TrackFields::kEta, "pseudorapidity");
2727
VectorConfig<float>::AddField("p", TrackFields::kP, "GeV/c");
28+
VectorConfig<int>::AddField("q", TrackFields::kQ, "Charge of the track (or its sign when absolute value unknown)");
2829
VectorConfig<int>::AddField("id", TrackFields::kId, "unique id");
2930
} else if (type_ == DetType::kParticle) {
3031
VectorConfig<float>::AddField("px", ParticleFields::kPx, "GeV/c");
@@ -38,6 +39,7 @@ BranchConfig::BranchConfig(std::string name, DetType type) : name_(std::move(nam
3839
VectorConfig<float>::AddField("E", ParticleFields::kEnergy, "full energy, GeV");
3940
VectorConfig<float>::AddField("T", ParticleFields::kKineticEnergy, "kinetic energy, GeV");
4041
VectorConfig<float>::AddField("rapidity", ParticleFields::kRapidity, "in Lab. frame");
42+
VectorConfig<int>::AddField("q", ParticleFields::kQ, "Charge of the particle");
4143
VectorConfig<int>::AddField("pid", ParticleFields::kPid, "PDG code");
4244
VectorConfig<int>::AddField("id", ParticleFields::kId, "unique id");
4345
} else if (type_ == DetType::kHit) {
@@ -148,6 +150,12 @@ std::vector<std::string> VectorConfig<T>::SplitString(const std::string& input)
148150
return result;
149151
}
150152

153+
void BranchConfig::GuaranteeFieldNameVacancy(const std::string& name) const {
154+
if (HasField(name)) {
155+
throw std::runtime_error("BranchConfig::GuaranteeFieldNameVacancy(): field " + name + " already exists");
156+
}
157+
}
158+
151159
void BranchConfig::Print() const {
152160
std::cout << "Branch " << name_ << " (id=" << id_ << ") consists of:" << std::endl;
153161
std::cout << "Floating fields:" << std::endl;

core/BranchConfig.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,19 @@ class BranchConfig : public VectorConfig<int>, public VectorConfig<float>, publi
134134
// Setters
135135
template<typename T>
136136
void AddField(const std::string& name, const std::string& title = "") {
137+
GuaranteeFieldNameVacancy(name);
137138
VectorConfig<T>::AddField(name, title);
138139
}
139140
template<typename T>
140141
void AddFields(const std::vector<std::string>& names, const std::string& title = "") {
142+
for (auto& n : names) {
143+
GuaranteeFieldNameVacancy(n);
144+
}
141145
VectorConfig<T>::AddFields(names, title);
142146
}
143147
template<typename T>
144148
void AddField(const std::string& name, ShortInt_t id, const std::string& title = "") {
149+
GuaranteeFieldNameVacancy(name);
145150
VectorConfig<T>::AddField(name, id, title);
146151
}
147152

@@ -176,6 +181,8 @@ class BranchConfig : public VectorConfig<int>, public VectorConfig<float>, publi
176181
protected:
177182
void GenerateId();
178183

184+
void GuaranteeFieldNameVacancy(const std::string& name) const;
185+
179186
std::string name_;
180187
size_t id_{0};
181188
DetType type_{DetType(UndefValueShort)};

core/Constants.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ enum TrackFields : ShortInt_t {
6767
kPy = -5,
6868
kPz = -6,
6969
kP = -7,
70-
kId = -8
70+
kQ = -8,
71+
kId = -9
7172
};
7273
}
7374

@@ -85,7 +86,8 @@ enum ParticleFields : ShortInt_t {
8586
kP = -10,
8687
kEnergy = -11,
8788
kKineticEnergy = -12,
88-
kId = -13
89+
kQ = -13,
90+
kId = -14
8991
};
9092
}
9193

core/Particle.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,9 @@ void Particle::SetPid(PdgCode_t pid) {
99
pid_ = pid;
1010
if (mass_ == -1000.f)
1111
mass_ = GetMassByPdgId(pid);
12+
13+
if (charge_ == -1000) {
14+
charge_ = GetChargeByPdgId(pid);
15+
}
1216
}
1317
}// namespace AnalysisTree

core/Particle.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Particle : public Track {
5555
case ParticleFields::kPx: return GetPx();
5656
case ParticleFields::kPy: return GetPy();
5757
case ParticleFields::kPz: return GetPz();
58+
case ParticleFields::kQ: return GetCharge();
5859
case ParticleFields::kId: return GetId();
5960
default: throw std::out_of_range("Particle::GetField - Index " + std::to_string(iField) + " is not found");
6061
}
@@ -71,6 +72,7 @@ class Particle : public Track {
7172
case ParticleFields::kPy: py_ = value; break;
7273
case ParticleFields::kPz: pz_ = value; break;
7374
case ParticleFields::kMass: mass_ = value; break;
75+
case ParticleFields::kQ: charge_ = value; break;
7476
case ParticleFields::kPid: SetPid(value); break;
7577
case ParticleFields::kId: break;
7678
case ParticleFields::kP: /*throw std::runtime_error("Cannot set transient fields");*/ break;

core/Track.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,20 @@ float Track::GetMassByPdgId(PdgCode_t pdg) {
4141
}
4242
}
4343

44+
int Track::GetChargeByPdgId(PdgCode_t pdg) {
45+
46+
if (pdg > 1000000000) {//100ZZZAAA0
47+
auto Z = (pdg % 10000000) / 10000;
48+
return Z;
49+
}
50+
auto db = TDatabasePDG::Instance();
51+
auto particle = db->GetParticle(pdg);
52+
53+
if (particle) {
54+
return int(particle->Charge() / 3);
55+
} else {
56+
throw std::runtime_error("Mass of " + std::to_string(pdg) + " is not known");
57+
}
58+
}
59+
4460
}// namespace AnalysisTree

core/Track.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,16 @@ class Track : public Container {
6363
pz_ = momentum.Pz();
6464
}
6565

66+
void SetCharge(Int_t charge) noexcept { charge_ = charge; }
67+
6668
ANALYSISTREE_ATTR_NODISCARD inline Floating_t GetPx() const noexcept { return px_; }
6769
ANALYSISTREE_ATTR_NODISCARD inline Floating_t GetPy() const noexcept { return py_; }
6870
ANALYSISTREE_ATTR_NODISCARD inline Floating_t GetPz() const noexcept { return pz_; }
6971
ANALYSISTREE_ATTR_NODISCARD inline Floating_t GetPt() const noexcept { return sqrt(px_ * px_ + py_ * py_); }
7072
ANALYSISTREE_ATTR_NODISCARD inline Floating_t GetPhi() const noexcept { return atan2(py_, px_); }
7173
ANALYSISTREE_ATTR_NODISCARD inline Floating_t GetEta() const noexcept { return 0.5f * log((GetP() + pz_) / (GetP() - pz_)); }
7274
ANALYSISTREE_ATTR_NODISCARD inline Floating_t GetP() const noexcept { return sqrt(px_ * px_ + py_ * py_ + pz_ * pz_); }
75+
ANALYSISTREE_ATTR_NODISCARD inline Integer_t GetCharge() const noexcept { return charge_; }
7376

7477
/**
7578
* @return 3d-momentum of a track
@@ -120,6 +123,7 @@ class Track : public Container {
120123
case TrackFields::kPx: return GetPx();
121124
case TrackFields::kPy: return GetPy();
122125
case TrackFields::kPz: return GetPz();
126+
case TrackFields::kQ: return GetCharge();
123127
case TrackFields::kId: return GetId();
124128
default: throw std::out_of_range("Track::GetField - Index " + std::to_string(id) + " is not found");
125129
}
@@ -135,6 +139,7 @@ class Track : public Container {
135139
case TrackFields::kPx: px_ = value; break;
136140
case TrackFields::kPy: py_ = value; break;
137141
case TrackFields::kPz: pz_ = value; break;
142+
case TrackFields::kQ: charge_ = value; break;
138143
case TrackFields::kId: break;
139144
case TrackFields::kP: /*throw std::runtime_error("Cannot set transient fields");*/ break;
140145
case TrackFields::kPt: /*throw std::runtime_error("Cannot set transient fields");*/ break;
@@ -152,10 +157,12 @@ class Track : public Container {
152157

153158
protected:
154159
static float GetMassByPdgId(PdgCode_t pdg);
160+
static int GetChargeByPdgId(PdgCode_t pdg);
155161

156162
Floating_t px_{UndefValueFloat};///< x-component of track's momentum
157163
Floating_t py_{UndefValueFloat};///< y-component of track's momentum
158164
Floating_t pz_{UndefValueFloat};///< z-component of track's momentum
165+
Integer_t charge_{-1000};
159166

160167
ClassDefOverride(Track, 2);
161168
};

infra/TaskManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ void TaskManager::Finish() {
150150
out_tree_->Write();
151151
configuration_->Write("Configuration");
152152
data_header_->Write("DataHeader");
153-
if(is_write_hash_info_) WriteCommitInfo();
153+
if (is_write_hash_info_) WriteCommitInfo();
154154
out_file_->Close();
155155
out_tree_ = nullptr;
156156
delete out_file_;

infra/TaskManager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class TaskManager {
158158
void SetWriteMode(eBranchWriteMode mode) { write_mode_ = mode; }
159159
void SetBranchesExclude(std::vector<std::string> brex) { branches_exclude_ = std::move(brex); }
160160
void SetVerbosityPeriod(int value) { verbosity_period_ = value; }
161-
void SetIsWriteHashInfo(bool is=true) { is_write_hash_info_ = is; }
161+
void SetIsWriteHashInfo(bool is = true) { is_write_hash_info_ = is; }
162162
void SetIsUpdateEntryInExec(bool is = true) { is_update_entry_in_exec_ = is; }
163163

164164
void ClearTasks() { tasks_.clear(); }

0 commit comments

Comments
 (0)