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>
22
23#include <stdexcept>
24
25//
26// method implementations
27//
28
29namespace MD
30{
31 void Formula ::
32 testInvariant () const
33 {
34 RCU_INVARIANT (this != 0);
35 if (!m_formula.empty())
36 {
37 RCU_INVARIANT (!m_name.empty());
38
39 if (m_tree != 0)
40 {
42 if (m_ndata > 0)
43 {
44 RCU_INVARIANT (m_cache.size() >= std::size_t (m_ndata));
45 RCU_INVARIANT (m_read.size() >= std::size_t (m_ndata));
46 }
47 } else
48 {
49 RCU_INVARIANT (m_form == 0);
51 }
52 } else
53 {
54 RCU_INVARIANT (m_name.empty());
55 RCU_INVARIANT (m_tree == 0);
56 RCU_INVARIANT (m_form == 0);
58 }
59 }
60
61
62
63 Formula ::
64 Formula ()
65 : m_tree (0), m_form (0), m_manager (0), m_ndim (-1),
66 m_entry (-1), m_ndata (0)
67 {
68 RCU_NEW_INVARIANT (this);
69 }
70
71
72
73 Formula ::
74 Formula (const std::string& name, const std::string& formula, TTree *tree)
75 : m_formula (formula), m_name (name),
76 m_tree (0), m_form (0), m_manager (0), m_ndim (-1),
77 m_entry (-1), m_ndata (0)
78 {
79 RCU_NEW_INVARIANT (this);
80
81 reset (tree);
82 }
83
84
85
86 Formula ::
87 ~Formula ()
88 {
90
91 delete m_form;
92 m_form = 0;
93 }
94
95
96
97 void Formula ::
98 reset (TTree *tree)
99 {
102
103 delete m_form;
104 m_form = 0;
105 m_manager = 0;
106 m_ndim = -1;
107 m_entry = -1;
108 m_ndata = 0;
109 m_tree = 0;
110
111 m_tree = tree;
112 m_form = new TTreeFormula (m_name.c_str(), m_formula.c_str(), tree);
113
114 m_form->SetQuickLoad (kTRUE);
115 m_manager = new TTreeFormulaManager;
116 m_manager->Add (m_form);
117 m_manager->Sync ();
118 if (m_manager->GetMultiplicity () == -1)
119 tree->SetBit (TTree::kForceRead);
120 if (m_form->GetNdim() <= 0)
121 m_ndim = -1;
122 else if (m_form->GetMultiplicity() == 0)
123 m_ndim = 0;
124 else if (m_manager->GetMultiplicity() == 1 && m_form->GetMultiplicity() == 1)
125 m_ndim = 1;
126 else if (m_manager->GetMultiplicity() == -1 && m_form->GetMultiplicity() == 1)
127 m_ndim = 0;
128
129
130 }
131
132
133
134 const std::string& Formula ::
135 formula () const
136 {
137 RCU_READ_INVARIANT (this);
138 return m_formula;
139 }
140
141
142
143 bool Formula ::
144 valid () const
145 {
146 RCU_READ_INVARIANT (this);
147 return !m_formula.empty() && m_ndim >= 0;
148 }
149
150
151
152 int Formula ::
153 ndim () const
154 {
155 RCU_READ_INVARIANT (this);
157 return m_ndim;
158 }
159
160
161
162 std::size_t Formula ::
163 ndata () const
164 {
165 RCU_READ_INVARIANT (this);
167
168 if (m_ndim < 0)
169 throw std::runtime_error ("invalid formula: " + m_formula);
170 if (m_tree->GetReadEntry() != m_entry)
171 {
172 m_entry = -1;
173 m_ndata = m_manager->GetNdata();
174 if (m_ndata > 0)
175 {
176 if (m_read.size() < std::size_t (m_ndata))
177 m_read.resize (m_ndata);
178 for (std::size_t iter = 0, end = m_ndata; iter != end; ++ iter)
179 m_read[iter] = false;
180
181 if (m_cache.size() < std::size_t (m_ndata))
182 m_cache.resize (m_ndata);
183 m_cache[0] = m_form->EvalInstance (0);
184 m_read[0] = true;
185 m_entry = m_tree->GetReadEntry();
186 }
187 }
188 if (m_ndata < 0)
189 throw std::runtime_error ("failed to read formula: " + m_formula);
190 return m_ndata;
191 }
192
193
194
195 Double_t Formula ::
196 value (std::size_t data) const
197 {
198 RCU_READ_INVARIANT (this);
200 RCU_REQUIRE_SOFT (data < ndata ());
201
202 if (!m_read[data])
203 {
204 m_cache[data] = m_form->EvalInstance (data);
205 m_read[data] = true;
206 }
207 return m_cache[data];
208 }
209}
#define RCU_INVARIANT(x)
Definition Assert.h:189
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:223
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:219
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:221
#define RCU_REQUIRE(x)
Definition Assert.h:196
#define RCU_REQUIRE_SOFT(x)
Definition Assert.h:141
#define RCU_READ_INVARIANT(x)
Definition Assert.h:217
static const Attributes_t empty
const std::string & formula() const
description: the formula used guarantee: no-fail
Definition Formula.cxx:135
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:144
std::size_t ndata() const
returns: the number of data entries for the formula with the given index guarantee: strong failures: ...
Definition Formula.cxx:163
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:98
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