ATLAS Offline Software
ArrayBranch.icc
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 #ifndef MUONTESTER_ARRAYBRANCH_IXX
5 #define MUONTESTER_ARRAYBRANCH_IXX
6 
7 #include <MuonTesterTree/MuonTesterTree.h>
8 #include <MuonTesterTree/throwExcept.h>
9 #include <format>
10 namespace MuonVal {
11 template <class T>
12 ArrayBranch<T>::ArrayBranch(TTree* t, const std::string& b_name, size_t size) :
13  MuonTesterBranch{t,b_name},
14  m_size{size} {
15  m_data.resize(size);
16  m_updated.resize(size);
17  reset();
18 }
19 template <class T>
20 ArrayBranch<T>::ArrayBranch(MuonTesterTree& t, const std::string& b_name, size_t size) :
21  MuonTesterBranch{t, b_name},
22  m_size{size} {
23  m_data.resize(size);
24  m_updated.resize(size);
25  reset();
26 }
27 
28 template <class T>
29 ArrayBranch<T>::ArrayBranch(TTree* tree, const std::string& name, size_t size, const T& def_val) :
30  ArrayBranch(tree, name, size) {
31  setDefault(def_val);
32 }
33 template <class T>
34 ArrayBranch<T>::ArrayBranch(MuonTesterTree& tree, const std::string& name, size_t size, const T& def_val) :
35  ArrayBranch(tree, name, size) {
36  setDefault(def_val);
37 }
38 template <class T> void ArrayBranch<T>::reset() {
39  for (size_t i = 0; i < size(); ++i) m_updated[i] = false;
40 }
41 template <class T> const T& ArrayBranch<T>::operator[](size_t s) const { return get(s); }
42 template <class T> void ArrayBranch<T>::set(size_t s, const T& val) {
43  if (s >= size()) {
44  THROW_EXCEPTION("Index "<<s<<" is out of range for "<<name());
45  }
46  m_updated[s] = true;
47  m_data[s] = val;
48 }
49 template <class T> const T& ArrayBranch<T>::get(size_t s) const {
50  if (s >= size()) {
51  THROW_EXCEPTION("Index "<<s<<" is out of range for "<<name());
52  }
53  return m_data[s];
54 }
55 template <class T> T& ArrayBranch<T>::operator[](size_t s) {
56  if (s >= size()) {
57  THROW_EXCEPTION("Index "<<s<<" is out of range for "<<name());
58  }
59  m_updated[s] = true;
60  return m_data[s];
61 }
62 template <class T> size_t ArrayBranch<T>::size() const { return m_size; }
63 template <class T> bool ArrayBranch<T>::init() {
64  if (initialized()) {
65  ATH_MSG_WARNING("init() -- The branch " << name() << " is already initialized. ");
66  return true;
67  }
68  const std::string br_name{std::format("{:}[{:d}]{:}", name(), size(), tree_data_type())};
69  if (name().empty() || !tree()) {
70  ATH_MSG_ERROR("init() -- Empty names are forbidden. ");
71  return false;
72  } else if (tree()->FindBranch(name().c_str())) {
73  ATH_MSG_ERROR("init() -- The branch " << name() << " already exists in TTree " << tree()->GetName() << ".");
74  return false;
75  } else if (!tree()->Branch(br_name.c_str(), m_data.data())) {
76  ATH_MSG_ERROR("init() -- Could not create branch " << name() << " in TTree " << tree()->GetName());
77  return false;
78  }
79  m_init = true;
80  return true;
81 }
82 template <class T> bool ArrayBranch<T>::initialized() const { return m_init; }
83 template <class T> bool ArrayBranch<T>::fill(const EventContext&) {
84  if (!initialized()) {
85  ATH_MSG_ERROR("init() -- The branch " << name() << " is not initialized yet.");
86  return false;
87  }
88  for (size_t i = 0; i < size(); ++i) {
89  if (!m_updated[i]) {
90  if (m_failIfNotUpdated) {
91  ATH_MSG_ERROR("init() -- The " << i << "-th value is has not been updated. ");
92  return false;
93  } else {
94  m_data[i] = m_default;
95  }
96  }
97  }
98  reset();
99  return true;
100 }
101 template <class T> const T& ArrayBranch<T>::getDefault() const { return m_default; }
102 template <class T> void ArrayBranch<T>::setDefault(const T& val) {
103  m_default = val;
104  m_failIfNotUpdated = false;
105 }
106 }
107 #endif