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 namespace MuonVal {
10 template <class T>
11 ArrayBranch<T>::ArrayBranch(TTree* t, const std::string& b_name, size_t size) :
12  MuonTesterBranch{t,b_name},
13  m_size{size} {
14  m_data.resize(size);
15  m_updated.resize(size);
16  reset();
17 }
18 template <class T>
19 ArrayBranch<T>::ArrayBranch(MuonTesterTree& t, const std::string& b_name, size_t size) :
20  MuonTesterBranch{t, b_name},
21  m_size{size} {
22  m_data.resize(size);
23  m_updated.resize(size);
24  reset();
25 }
26 
27 template <class T>
28 ArrayBranch<T>::ArrayBranch(TTree* tree, const std::string& name, size_t size, const T& def_val) :
29  ArrayBranch(tree, name, size) {
30  setDefault(def_val);
31 }
32 template <class T>
33 ArrayBranch<T>::ArrayBranch(MuonTesterTree& tree, const std::string& name, size_t size, const T& def_val) :
34  ArrayBranch(tree, name, size) {
35  setDefault(def_val);
36 }
37 template <class T> void ArrayBranch<T>::reset() {
38  for (size_t i = 0; i < size(); ++i) m_updated[i] = false;
39 }
40 template <class T> const T& ArrayBranch<T>::operator[](size_t s) const { return get(s); }
41 template <class T> void ArrayBranch<T>::set(size_t s, const T& val) {
42  if (s >= size()) {
43  THROW_EXCEPTION("Index "<<s<<" is out of range for "<<name());
44  }
45  m_updated[s] = true;
46  m_data[s] = val;
47 }
48 template <class T> const T& ArrayBranch<T>::get(size_t s) const {
49  if (s >= size()) {
50  THROW_EXCEPTION("Index "<<s<<" is out of range for "<<name());
51  }
52  return m_data[s];
53 }
54 template <class T> T& ArrayBranch<T>::operator[](size_t s) {
55  if (s >= size()) {
56  THROW_EXCEPTION("Index "<<s<<" is out of range for "<<name());
57  }
58  m_updated[s] = true;
59  return m_data[s];
60 }
61 template <class T> size_t ArrayBranch<T>::size() const { return m_size; }
62 template <class T> bool ArrayBranch<T>::init() {
63  if (initialized()) {
64  ATH_MSG_WARNING("init() -- The branch " << name() << " is already initialized. ");
65  return true;
66  }
67  std::stringstream br_name{};
68  br_name<<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(name().c_str(), m_data.data(), br_name.str().c_str())) {
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