ATLAS Offline Software
Loading...
Searching...
No Matches
Formula.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7//
8// includes
9//
10
11#include <MultiDraw/Formula.h>
12
13#include <memory>
14#include <sstream>
15#include <TTree.h>
16#include <TTreeFormula.h>
17#include <TTreeFormulaManager.h>
18#include <EventLoop/Job.h>
20#include <EventLoop/IWorker.h>
23
24//
25// method implementations
26//
27
28namespace MD
29{
30 void Formula ::
31 testInvariant () const
32 {
33 RCU_INVARIANT (this != 0);
34 if (!m_formula.empty())
35 {
36 RCU_INVARIANT (!m_name.empty());
37
38 if (m_tree != 0)
39 {
41 if (m_ndata > 0)
42 {
43 RCU_INVARIANT (m_cache.size() >= std::size_t (m_ndata));
44 RCU_INVARIANT (m_read.size() >= std::size_t (m_ndata));
45 }
46 } else
47 {
48 RCU_INVARIANT (m_form == 0);
50 }
51 } else
52 {
53 RCU_INVARIANT (m_name.empty());
54 RCU_INVARIANT (m_tree == 0);
55 RCU_INVARIANT (m_form == 0);
57 }
58 }
59
60
61
62 Formula ::
63 Formula ()
64 : m_tree (0), m_form (0), m_manager (0), m_ndim (-1),
65 m_entry (-1), m_ndata (0)
66 {
67 RCU_NEW_INVARIANT (this);
68 }
69
70
71
72 Formula ::
73 Formula (const std::string& name, const std::string& formula, TTree *tree)
74 : m_formula (formula), m_name (name),
75 m_tree (0), m_form (0), m_manager (0), m_ndim (-1),
76 m_entry (-1), m_ndata (0)
77 {
78 RCU_NEW_INVARIANT (this);
79
80 reset (tree);
81 }
82
83
84
85 Formula ::
86 ~Formula ()
87 {
89
90 delete m_form;
91 m_form = 0;
92 }
93
94
95
96 void Formula ::
97 reset (TTree *tree)
98 {
101
102 delete m_form;
103 m_form = 0;
104 m_manager = 0;
105 m_ndim = -1;
106 m_entry = -1;
107 m_ndata = 0;
108 m_tree = 0;
109
110 m_tree = tree;
111 m_form = new TTreeFormula (m_name.c_str(), m_formula.c_str(), tree);
112
113 m_form->SetQuickLoad (kTRUE);
114 m_manager = new TTreeFormulaManager;
115 m_manager->Add (m_form);
116 m_manager->Sync ();
117 if (m_manager->GetMultiplicity () == -1)
118 tree->SetBit (TTree::kForceRead);
119 if (m_form->GetNdim() <= 0)
120 m_ndim = -1;
121 else if (m_form->GetMultiplicity() == 0)
122 m_ndim = 0;
123 else if (m_manager->GetMultiplicity() == 1 && m_form->GetMultiplicity() == 1)
124 m_ndim = 1;
125 else if (m_manager->GetMultiplicity() == -1 && m_form->GetMultiplicity() == 1)
126 m_ndim = 0;
127
128
129 }
130
131
132
133 const std::string& Formula ::
134 formula () const
135 {
136 RCU_READ_INVARIANT (this);
137 return m_formula;
138 }
139
140
141
142 bool Formula ::
143 valid () const
144 {
145 RCU_READ_INVARIANT (this);
146 return !m_formula.empty() && m_ndim >= 0;
147 }
148
149
150
151 int Formula ::
152 ndim () const
153 {
154 RCU_READ_INVARIANT (this);
156 return m_ndim;
157 }
158
159
160
161 std::size_t Formula ::
162 ndata () const
163 {
164 RCU_READ_INVARIANT (this);
166
167 if (m_ndim < 0)
168 RCU_THROW_MSG ("invalid formula: " + m_formula);
169 if (m_tree->GetReadEntry() != m_entry)
170 {
171 m_entry = -1;
172 m_ndata = m_manager->GetNdata();
173 if (m_ndata > 0)
174 {
175 if (m_read.size() < std::size_t (m_ndata))
176 m_read.resize (m_ndata);
177 for (std::size_t iter = 0, end = m_ndata; iter != end; ++ iter)
178 m_read[iter] = false;
179
180 if (m_cache.size() < std::size_t (m_ndata))
181 m_cache.resize (m_ndata);
182 m_cache[0] = m_form->EvalInstance (0);
183 m_read[0] = true;
184 m_entry = m_tree->GetReadEntry();
185 }
186 }
187 if (m_ndata < 0)
188 RCU_THROW_MSG ("failed to read formula: " + m_formula);
189 return m_ndata;
190 }
191
192
193
194 Double_t Formula ::
195 value (std::size_t data) const
196 {
197 RCU_READ_INVARIANT (this);
199 RCU_REQUIRE_SOFT (data < ndata ());
200
201 if (!m_read[data])
202 {
203 m_cache[data] = m_form->EvalInstance (data);
204 m_read[data] = true;
205 }
206 return m_cache[data];
207 }
208}
#define RCU_INVARIANT(x)
Definition Assert.h:196
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:230
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:226
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:228
#define RCU_REQUIRE(x)
Definition Assert.h:203
#define RCU_REQUIRE_SOFT(x)
Definition Assert.h:148
#define RCU_READ_INVARIANT(x)
Definition Assert.h:224
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:53
static const Attributes_t empty
const std::string & formula() const
description: the formula used guarantee: no-fail
Definition Formula.cxx:134
Long64_t m_entry
description: the last entry we read
Definition Formula.h:137
TTreeFormulaManager * m_manager
description: the manager used
Definition Formula.h:128
TTreeFormula * m_form
description the formula used
Definition Formula.h:124
std::string m_name
description: the name we use for the formula
Definition Formula.h:116
TTree * m_tree
description: the tree we are connected to
Definition Formula.h:120
int m_ndim
description: the number of array dimensions we need to loop over, or -1 if we are in error
Definition Formula.h:133
std::vector< Double_t > m_cache
description: the cache of data entries
Definition Formula.h:145
bool valid() const
returns: whether the formula is valid guarantee: no-fail
Definition Formula.cxx:143
std::size_t ndata() const
returns: the number of data entries for the formula with the given index guarantee: strong failures: ...
Definition Formula.cxx:162
std::string m_formula
description: members directly corresponding to accessors
Definition Formula.h:112
void reset(TTree *tree)
effects: reset this formula to a new tree guarantee: strong failures: out of memory II failures: TTre...
Definition Formula.cxx:97
Int_t m_ndata
description: the number of data entries
Definition Formula.h:141
std::vector< bool > m_read
description: whether we read the given data entry
Definition Formula.h:149
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition AlgCFlow.h:22
TChain * tree