ATLAS Offline Software
IParticleFourMomBranch.cxx
Go to the documentation of this file.
1 
2 /*
3  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 */
7 namespace {
8  constexpr size_t dummyIdx = -1;
9  constexpr float MeVtoGeV = 1.e-3;
10  static const SG::AuxElement::ConstAccessor<float> acc_charge{"charge"};
11 }
12 
13 
14 namespace MuonVal{
16  const std::string& particle):
17  AthMessaging{"IParticle "+particle},
18  m_parent{tree},
19  m_name{particle}{}
20 
22  return m_parent;
23  }
24  const std::vector<const xAOD::IParticle*>& IParticleFourMomBranch::getCached()const {
25  return m_cached_particles;
26  }
27 
28  size_t IParticleFourMomBranch::size() const { return m_pt.size(); }
29  std::string IParticleFourMomBranch::name() const { return m_name; }
30  const TTree* IParticleFourMomBranch::tree() const { return m_pt.tree(); }
31  TTree* IParticleFourMomBranch::tree() { return m_pt.tree(); }
37  if (!p || find(p) < size())
38  return;
39  m_cached_particles.push_back(p);
40  m_pt.push_back(p->pt() * MeVtoGeV);
41  m_eta.push_back(p->eta());
42  m_phi.push_back(p->phi());
43  m_e.push_back(p->e() * MeVtoGeV);
44  int q{0};
45  if (acc_charge.isAvailable(*p)) {
46  q = acc_charge(*p);
47  } else if (p->type() == xAOD::Type::ObjectType::TrackParticle){
48  q = static_cast<const xAOD::TrackParticle*>(p)->charge();
49  }
50  m_q.push_back(q);
51  for (const auto& var : m_variables) {
52  var->push_back(p);
53  }
54  }
55 
57  return find(&p);
58  }
60  if (!p) {
61  return dummyIdx;
62  }
63  return find([p](const xAOD::IParticle* cached) { return p == cached; });
64  }
65  size_t IParticleFourMomBranch::find(std::function<bool(const xAOD::IParticle*)> func) const {
66  size_t j = 0;
67  for (const xAOD::IParticle* p : m_cached_particles) {
68  if (func(p)) {
69  return j;
70  }
71  ++j;
72  }
73  return dummyIdx;
74  }
75  bool IParticleFourMomBranch::fill(const EventContext& ctx) {
76  if (!m_pt.fill(ctx) || !m_eta.fill(ctx) || !m_phi.fill(ctx) || !m_e.fill(ctx) || !m_q.fill(ctx)){
77  return false;
78  }
79  for (const auto& var : m_variables) {
80  if (!var->fill(ctx)) {
81  return false;
82  }
83  }
84  m_init = true;
85  m_cached_particles.clear();
86  return true;
87  }
89  return m_init;
90  }
92  if (initialized()) {
93  return true;
94  }
95 
96  if (!m_pt.init() || !m_eta.init() || !m_phi.init() || !m_e.init() || !m_q.init()) {
97  return false;
98  }
99  std::vector<IMuonTesterBranch*> branchesToInit{};
100  for (auto& v : m_variables) {
101  branchesToInit.push_back(v.get());
102  }
103  std::sort(branchesToInit.begin(), branchesToInit.end(),
105  return a->name() < b->name();
106  });
107  for (auto& br : branchesToInit) {
108  if (!br->init()) {
109  ATH_MSG_ERROR("Failed to initialize "<<br->name());
110  return false;
111  }
112  }
113  return true;
114  }
115 
116  bool IParticleFourMomBranch::addVariable(std::shared_ptr<IParticleDecorationBranch> branch) {
117  if (initialized()) {
118  ATH_MSG_ERROR("The branch is already initialized. Cannot add more branches");
119  return false;
120  } else if (!branch) {
121  ATH_MSG_ERROR("Nullptr is given.");
122  return false;
123  }
124 
125  auto itr = std::find_if(m_variables.begin(), m_variables.end(),
126  [&branch](const std::shared_ptr<IParticleDecorationBranch>& known) {
127  return known == branch || known->name() == branch->name();
128  });
129  if (itr != m_variables.end()) {
130  if (typeid((*itr).get()) != typeid(branch.get())) {
131  ATH_MSG_ERROR("Different branches have been added here under "<<branch->name()<<".");
132  return false;
133  }
134  return true;
135  }
136  m_variables.push_back(branch);
138  }
139  std::vector<IParticleFourMomBranch::DataDependency> IParticleFourMomBranch::data_dependencies() {
140  std::vector<DataDependency> to_ret{};
141  for (const std::shared_ptr<IParticleDecorationBranch>& br : m_variables) {
142  std::vector<DataDependency> childDep{br->data_dependencies()};
143  to_ret.insert(to_ret.end(),childDep.begin(), childDep.end());
144  }
145  return to_ret;
146  }
147 }
MuonVal::IParticleFourMomBranch::tree
const TTree * tree() const override
Return the underyling TTree pointer.
Definition: IParticleFourMomBranch.cxx:30
beamspotnt.var
var
Definition: bin/beamspotnt.py:1394
Trk::ParticleSwitcher::particle
constexpr ParticleHypothesis particle[PARTICLEHYPOTHESES]
the array of masses
Definition: ParticleHypothesis.h:76
MuonVal::IParticleFourMomBranch::fill
bool fill(const EventContext &ctx) override
Fill the branch.
Definition: IParticleFourMomBranch.cxx:75
IParticleFourMomBranch.h
tree
TChain * tree
Definition: tile_monitor.h:30
MuonVal::IParticleFourMomBranch::getCached
const std::vector< const xAOD::IParticle * > & getCached() const
Returns the list of currently cached particles.
Definition: IParticleFourMomBranch.cxx:24
CP::MeVtoGeV
constexpr float MeVtoGeV
Definition: IsolationCloseByCorrectionTool.cxx:33
MuonVal::MuonTesterTree::registerBranch
bool registerBranch(std::shared_ptr< IMuonTesterBranch > branch)
This method adds the branch to the tree and hands over the ownership to the MuonAnalysisTree instance...
Definition: MuonTesterTree.cxx:28
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
MuonVal::MuonTesterBranch::tree
TTree * tree() override final
Returns the underlying TTree object.
Definition: MuonTesterBranch.cxx:53
MuonVal::IParticleFourMomBranch::m_e
VectorBranch< float > m_e
Definition: IParticleFourMomBranch.h:132
MuonVal::IParticleFourMomBranch::init
bool init() override
Initialize the branches and all children.
Definition: IParticleFourMomBranch.cxx:91
xAOD::TrackParticle
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Definition: Event/xAOD/xAODTracking/xAODTracking/TrackParticle.h:13
MuonVal::IParticleFourMomBranch::getTree
MuonTesterTree & getTree()
Access to the MuonTrestTree parent.
Definition: IParticleFourMomBranch.cxx:21
MuonVal::IParticleFourMomBranch::m_cached_particles
std::vector< const xAOD::IParticle * > m_cached_particles
Definition: IParticleFourMomBranch.h:136
MuonVal::IParticleFourMomBranch::IParticleFourMomBranch
IParticleFourMomBranch(MuonTesterTree &tree, const std::string &particle)
Construct an IParticleFourMomBranch.
Definition: IParticleFourMomBranch.cxx:15
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
MuonVal::IParticleFourMomBranch::addVariable
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....
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
MuonVal::MuonTesterTree
Definition: MuonTesterTree.h:30
MuonVal::IParticleFourMomBranch::m_init
bool m_init
Definition: IParticleFourMomBranch.h:128
MuonVal::IParticleFourMomBranch::initialized
bool initialized() const
Has the init method been called.
Definition: IParticleFourMomBranch.cxx:88
MuonVal::VectorBranch::fill
bool fill(const EventContext &ctx) override
Clears vector in cases that it has not been updated in this event Retursn falls if the vector has not...
MuonVal::IParticleFourMomBranch::m_phi
VectorBranch< float > m_phi
Definition: IParticleFourMomBranch.h:131
AthMessaging
Class to provide easy MsgStream access and capabilities.
Definition: AthMessaging.h:55
MuonVal::IParticleFourMomBranch::size
size_t size() const
How many particles have been pushed back already.
Definition: IParticleFourMomBranch.cxx:28
MuonVal::VectorBranch::push_back
void push_back(const T &value)
Adds a new element at the end of the vector.
MuonVal
Class to store array like branches into the n-tuples.
Definition: MuonTPMetaDataAlg.cxx:25
MuonVal::IParticleFourMomBranch::push_back
void push_back(const xAOD::IParticle *p) override
Similar to the IAuxElementDecoration branch but only accepting IParticles.
Definition: IParticleFourMomBranch.cxx:35
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
charge
double charge(const T &p)
Definition: AtlasPID.h:756
python.decorators.cached.cached
def cached(func)
Decorator to cache function return value.
Definition: cached.py:6
MuonVal::IParticleFourMomBranch::m_eta
VectorBranch< float > m_eta
Definition: IParticleFourMomBranch.h:130
MuonVal::IParticleFourMomBranch::operator+=
void operator+=(const xAOD::IParticle *p) override
Definition: IParticleFourMomBranch.cxx:32
TrackParticle.h
python.PyAthena.v
v
Definition: PyAthena.py:154
RTTAlgmain.branch
branch
Definition: RTTAlgmain.py:61
a
TList * a
Definition: liststreamerinfos.cxx:10
MuonVal::IParticleFourMomBranch::m_pt
VectorBranch< float > m_pt
Definition: IParticleFourMomBranch.h:129
known
Definition: TrigBStoxAODTool.cxx:107
MuonVal::IParticleFourMomBranch::name
std::string name() const override
Name of the four momentum branch.
Definition: IParticleFourMomBranch.cxx:29
MuonVal::VectorBranch::init
bool init() override
Initialized the Branch.
MuonVal::IParticleFourMomBranch::data_dependencies
std::vector< DataDependency > data_dependencies() override
Returns a list of all Read(Coond)HandleKeys needed by the branch.
Definition: IParticleFourMomBranch.cxx:139
MuonVal::IParticleFourMomBranch::m_variables
std::vector< std::shared_ptr< IParticleDecorationBranch > > m_variables
Definition: IParticleFourMomBranch.h:135
extractSporadic.q
list q
Definition: extractSporadic.py:98
MuonVal::IParticleFourMomBranch::m_q
VectorBranch< int > m_q
Definition: IParticleFourMomBranch.h:133
MuonVal::VectorBranch::size
size_t size() const
Returns the number of actual saved elements.
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
MuonVal::IParticleFourMomBranch::m_parent
MuonTesterTree & m_parent
Definition: IParticleFourMomBranch.h:126
MuonVal::IMuonTesterBranch
Most basic interface class used by the MuonTester tree.
Definition: IMuonTesterBranch.h:20
MuonVal::IParticleFourMomBranch::m_name
std::string m_name
Definition: IParticleFourMomBranch.h:127
MuonVal::IParticleFourMomBranch::find
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 ...
Definition: IParticleFourMomBranch.cxx:56
PlotCalibFromCool.br
br
Definition: PlotCalibFromCool.py:355