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 = std::numeric_limits<std::size_t>::max();
10 constexpr float MeVtoGeV = 1.e-3;
11 static const SG::ConstAccessor<float> acc_charge{"charge"};
12}
13
14
15namespace MuonVal{
17 const std::string& particle):
18 AthMessaging{std::format("IParticle {:}", particle)},
20 m_name{particle}{}
21
25 std::span<const xAOD::IParticle* const> IParticleFourMomBranch::getCached(const EventContext& ctx) {
26 /* Reset the particles from the previous event*/
27 if (!m_updated && ctx.eventID().event_number() != m_lastFilled) {
28 m_cached_particles.clear();
29 }
30 return m_cached_particles;
31 }
32
33 size_t IParticleFourMomBranch::size() const { return m_updated ? m_cached_particles.size() : 0; }
34 std::string IParticleFourMomBranch::name() const { return m_name; }
35 const TTree* IParticleFourMomBranch::tree() const { return m_pt.tree(); }
36 TTree* IParticleFourMomBranch::tree() { return m_pt.tree(); }
41 if (!m_updated) {
42 m_cached_particles.clear();
43 m_updated = true;
44 }
46 if (!p || find(p) < size()){
47 if (p) {
48 ATH_MSG_VERBOSE("Rejected particle ("<<p<<") "<<p->pt()<<", "<<p->eta()
49 <<", "<<p->phi()<<". Size: "<<size());
50 }
51 return;
52 }
53
54 m_cached_particles.push_back(p);
55 m_pt.push_back(p->pt() * MeVtoGeV);
56 m_eta.push_back(p->eta());
57 m_phi.push_back(p->phi());
58 m_e.push_back(p->e() * MeVtoGeV);
59 int q{0};
60 if (acc_charge.isAvailable(*p)) {
61 q = acc_charge(*p);
62 } else if (p->type() == xAOD::Type::ObjectType::TrackParticle){
63 q = static_cast<const xAOD::TrackParticle*>(p)->charge();
64 } else if (p->type() == xAOD::Type::ObjectType::TruthParticle) {
65 q = static_cast<const xAOD::TruthParticle*>(p)->charge();
66 }
67 ATH_MSG_VERBOSE("New particle ("<<p<<") "<<p->pt()<<", "<<p->eta()<<", "<<p->phi()<<", q: "<<q<<". Size: "<<size());
68
69 m_q.push_back(q);
70 for (const auto& var : m_variables) {
71 var->push_back(p);
72 }
73 }
74
76 return find(&p);
77 }
79 if (!p) {
80 return dummyIdx;
81 }
82 return find([p](const xAOD::IParticle* cached) {
83 return p == cached;
84 });
85 }
86 size_t IParticleFourMomBranch::find(std::function<bool(const xAOD::IParticle*)> func) const {
87 if (!m_updated) {
88 return dummyIdx;
89 }
90 size_t j{0};
91 for (const xAOD::IParticle* p : m_cached_particles) {
92 if (func(p)) {
93 return j;
94 }
95 ++j;
96 }
97 return dummyIdx;
98 }
99 bool IParticleFourMomBranch::fill(const EventContext& ctx) {
100 if (!m_updated) {
101 m_cached_particles.clear();
102 }
103 if (!m_pt.fill(ctx) || !m_eta.fill(ctx) || !m_phi.fill(ctx) || !m_e.fill(ctx) || !m_q.fill(ctx)){
104 return false;
105 }
106 for (const auto& var : m_variables) {
107 if (!var->fill(ctx)) {
108 return false;
109 }
110 }
111 m_init = true;
112 m_updated = false;
113 m_lastFilled = ctx.eventID().event_number();
114 return true;
115 }
117 return m_init;
118 }
120 if (initialized()) {
121 return true;
122 }
123
124 if (!m_pt.init() || !m_eta.init() || !m_phi.init() || !m_e.init() || !m_q.init()) {
125 return false;
126 }
127 std::vector<IMuonTesterBranch*> branchesToInit{};
128 for (auto& v : m_variables) {
129 branchesToInit.push_back(v.get());
130 }
131 std::sort(branchesToInit.begin(), branchesToInit.end(),
133 return a->name() < b->name();
134 });
135 for (auto& br : branchesToInit) {
136 if (!br->init()) {
137 ATH_MSG_ERROR("Failed to initialize "<<br->name());
138 return false;
139 }
140 }
141 return true;
142 }
143
144 bool IParticleFourMomBranch::addVariable(std::shared_ptr<IParticleDecorationBranch> branch) {
145 if (initialized()) {
146 ATH_MSG_ERROR("The branch is already initialized. Cannot add more branches");
147 return false;
148 } else if (!branch) {
149 ATH_MSG_ERROR("Nullptr is given.");
150 return false;
151 }
152
153 auto itr = std::find_if(m_variables.begin(), m_variables.end(),
154 [&branch](const std::shared_ptr<IParticleDecorationBranch>& known) {
155 return known == branch || known->name() == branch->name();
156 });
157 if (itr != m_variables.end()) {
158 if (typeid((*itr).get()) != typeid(branch.get())) {
159 ATH_MSG_ERROR("Different branches have been added here under "<<branch->name()<<".");
160 return false;
161 }
162 return true;
163 }
164 m_variables.push_back(branch);
165 return m_parent.registerBranch(branch);
166 }
167 std::vector<IParticleFourMomBranch::DataDependency> IParticleFourMomBranch::data_dependencies() {
168 std::vector<DataDependency> to_ret{};
169 for (const std::shared_ptr<IParticleDecorationBranch>& br : m_variables) {
170 std::vector<DataDependency> childDep{br->data_dependencies()};
171 to_ret.insert(to_ret.end(),childDep.begin(), childDep.end());
172 }
173 return to_ret;
174 }
175}
#define ATH_MSG_ERROR(x,...)
#define ATH_MSG_VERBOSE(x,...)
double charge(const T &p)
Definition AtlasPID.h:999
static Double_t a
AthMessaging(IMessageSvc *msgSvc, const std::string &name)
Constructor.
Most basic interface class used by the MuonTester tree.
void operator+=(const xAOD::IParticle *p) override
std::vector< DataDependency > data_dependencies() override
Returns a list of all Read(Coond)HandleKeys needed by the branch.
bool addVariable(const std::string &variable, std::string_view accName="")
Writes a variable of type <T> which can be directly accessed via an SG::AuxElement e....
size_t size() const
How many particles have been pushed back already.
std::span< const xAOD::IParticle *const > getCached(const EventContext &ctx)
Returns the list of currently cached particles.
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.
Helper class to provide constant type-safe access to aux data.
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
STL namespace.
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.