ATLAS Offline Software
Loading...
Searching...
No Matches
IParticleFourMomBranch.cxx
Go to the documentation of this file.
1
2/*
3 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
4*/
8namespace {
9 constexpr size_t dummyIdx = -1;
10 constexpr float MeVtoGeV = 1.e-3;
11 static const SG::AuxElement::ConstAccessor<float> acc_charge{"charge"};
12}
13
14
15namespace MuonVal{
17 const std::string& particle):
18 AthMessaging{"IParticle "+particle},
20 m_name{particle}{}
21
25 const std::vector<const xAOD::IParticle*>& IParticleFourMomBranch::getCached()const {
26 return m_cached_particles;
27 }
28
29 size_t IParticleFourMomBranch::size() const { return m_updated ? m_cached_particles.size() : 0; }
30 std::string IParticleFourMomBranch::name() const { return m_name; }
31 const TTree* IParticleFourMomBranch::tree() const { return m_pt.tree(); }
32 TTree* IParticleFourMomBranch::tree() { return m_pt.tree(); }
37 if (!m_updated) {
38 m_cached_particles.clear();
39 m_updated = true;
40 }
42 if (!p || find(p) < size()){
43 if (p) ATH_MSG_VERBOSE("Rejected particle ("<<p<<") "<<p->pt()<<", "<<p->eta()<<", "<<p->phi()<<". Size: "<<size());
44 return;
45 }
46
47 m_cached_particles.push_back(p);
48 m_pt.push_back(p->pt() * MeVtoGeV);
49 m_eta.push_back(p->eta());
50 m_phi.push_back(p->phi());
51 m_e.push_back(p->e() * MeVtoGeV);
52 int q{0};
53 if (acc_charge.isAvailable(*p)) {
54 q = acc_charge(*p);
55 } else if (p->type() == xAOD::Type::ObjectType::TrackParticle){
56 q = static_cast<const xAOD::TrackParticle*>(p)->charge();
57 } else if (p->type() == xAOD::Type::ObjectType::TruthParticle) {
58 q = static_cast<const xAOD::TruthParticle*>(p)->charge();
59 }
60 ATH_MSG_VERBOSE("New particle ("<<p<<") "<<p->pt()<<", "<<p->eta()<<", "<<p->phi()<<", q: "<<q<<". Size: "<<size());
61
62 m_q.push_back(q);
63 for (const auto& var : m_variables) {
64 var->push_back(p);
65 }
66 }
67
69 return find(&p);
70 }
72 if (!p) {
73 return dummyIdx;
74 }
75 return find([p](const xAOD::IParticle* cached) {
76 return p == cached;
77 });
78 }
79 size_t IParticleFourMomBranch::find(std::function<bool(const xAOD::IParticle*)> func) const {
80 if (!m_updated) {
81 return dummyIdx;
82 }
83 size_t j{0};
84 for (const xAOD::IParticle* p : m_cached_particles) {
85 if (func(p)) {
86 return j;
87 }
88 ++j;
89 }
90 return dummyIdx;
91 }
92 bool IParticleFourMomBranch::fill(const EventContext& ctx) {
93 if (!m_updated) {
94 m_cached_particles.clear();
95 }
96 if (!m_pt.fill(ctx) || !m_eta.fill(ctx) || !m_phi.fill(ctx) || !m_e.fill(ctx) || !m_q.fill(ctx)){
97 return false;
98 }
99 for (const auto& var : m_variables) {
100 if (!var->fill(ctx)) {
101 return false;
102 }
103 }
104 m_init = true;
105 m_updated = false;
106 return true;
107 }
109 return m_init;
110 }
112 if (initialized()) {
113 return true;
114 }
115
116 if (!m_pt.init() || !m_eta.init() || !m_phi.init() || !m_e.init() || !m_q.init()) {
117 return false;
118 }
119 std::vector<IMuonTesterBranch*> branchesToInit{};
120 for (auto& v : m_variables) {
121 branchesToInit.push_back(v.get());
122 }
123 std::sort(branchesToInit.begin(), branchesToInit.end(),
125 return a->name() < b->name();
126 });
127 for (auto& br : branchesToInit) {
128 if (!br->init()) {
129 ATH_MSG_ERROR("Failed to initialize "<<br->name());
130 return false;
131 }
132 }
133 return true;
134 }
135
136 bool IParticleFourMomBranch::addVariable(std::shared_ptr<IParticleDecorationBranch> branch) {
137 if (initialized()) {
138 ATH_MSG_ERROR("The branch is already initialized. Cannot add more branches");
139 return false;
140 } else if (!branch) {
141 ATH_MSG_ERROR("Nullptr is given.");
142 return false;
143 }
144
145 auto itr = std::find_if(m_variables.begin(), m_variables.end(),
146 [&branch](const std::shared_ptr<IParticleDecorationBranch>& known) {
147 return known == branch || known->name() == branch->name();
148 });
149 if (itr != m_variables.end()) {
150 if (typeid((*itr).get()) != typeid(branch.get())) {
151 ATH_MSG_ERROR("Different branches have been added here under "<<branch->name()<<".");
152 return false;
153 }
154 return true;
155 }
156 m_variables.push_back(branch);
157 return m_parent.registerBranch(branch);
158 }
159 std::vector<IParticleFourMomBranch::DataDependency> IParticleFourMomBranch::data_dependencies() {
160 std::vector<DataDependency> to_ret{};
161 for (const std::shared_ptr<IParticleDecorationBranch>& br : m_variables) {
162 std::vector<DataDependency> childDep{br->data_dependencies()};
163 to_ret.insert(to_ret.end(),childDep.begin(), childDep.end());
164 }
165 return to_ret;
166 }
167}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
double charge(const T &p)
Definition AtlasPID.h:997
static Double_t a
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
Most basic interface class used by the MuonTester tree.
const std::vector< const xAOD::IParticle * > & getCached() const
Returns the list of currently cached particles.
void operator+=(const xAOD::IParticle *p) override
bool addVariable(const std::string &variable, const std::string &accName="")
Writes a variable of type <T> which can be directly accessed via an SG::AuxElement e....
std::vector< DataDependency > data_dependencies() override
Returns a list of all Read(Coond)HandleKeys needed by the branch.
size_t size() const
How many particles have been pushed back already.
IParticleFourMomBranch(MuonTesterTree &tree, const std::string &particle)
Construct an IParticleFourMomBranch.
bool fill(const EventContext &ctx) override
Fill the branch.
MuonTesterTree & getTree()
Access to the MuonTrestTree parent.
std::vector< const xAOD::IParticle * > m_cached_particles
size_t find(const xAOD::IParticle &p) const
Returns the position at which the particle has already be inserted in the chain – If the particle is ...
bool initialized() const
Has the init method been called.
void push_back(const xAOD::IParticle *p) override
Similar to the IAuxElementDecoration branch but only accepting IParticles.
std::vector< std::shared_ptr< IParticleDecorationBranch > > m_variables
std::string name() const override
Name of the four momentum branch.
const TTree * tree() const override
Return the underyling TTree pointer.
bool init() override
Initialize the branches and all children.
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
Class providing the definition of the 4-vector interface.
constexpr float MeVtoGeV
Class to store array like branches into the n-tuples.
Definition HitValAlg.cxx:19
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.
@ TrackParticle
The object is a charged track particle.
Definition ObjectType.h:43
@ TruthParticle
The object is a truth particle.
Definition ObjectType.h:67
TrackParticle_v1 TrackParticle
Reference the current persistent version:
TruthParticle_v1 TruthParticle
Typedef to implementation.