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
5// - 2012.
6// Distributed under the Boost Software License, Version 1.0.
7// (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9
10// Please feel free to contact me (krumnack@iastate.edu) for bug
11// reports, feature suggestions, praise and complaints.
12
13//
14// includes
15//
16
17#include <MultiDraw/Formula.h>
18
19#include <memory>
20#include <sstream>
21#include <TTree.h>
22#include <TTreeFormula.h>
23#include <TTreeFormulaManager.h>
24#include <EventLoop/Job.h>
26#include <EventLoop/IWorker.h>
29
30//
31// method implementations
32//
33
34namespace MD
35{
36 void Formula ::
37 testInvariant () const
38 {
39 RCU_INVARIANT (this != 0);
40 if (!m_formula.empty())
41 {
42 RCU_INVARIANT (!m_name.empty());
43
44 if (m_tree != 0)
45 {
47 if (m_ndata > 0)
48 {
49 RCU_INVARIANT (m_cache.size() >= std::size_t (m_ndata));
50 RCU_INVARIANT (m_read.size() >= std::size_t (m_ndata));
51 }
52 } else
53 {
54 RCU_INVARIANT (m_form == 0);
56 }
57 } else
58 {
59 RCU_INVARIANT (m_name.empty());
60 RCU_INVARIANT (m_tree == 0);
61 RCU_INVARIANT (m_form == 0);
63 }
64 }
65
66
67
68 Formula ::
69 Formula ()
70 : m_tree (0), m_form (0), m_manager (0), m_ndim (-1),
71 m_entry (-1), m_ndata (0)
72 {
73 RCU_NEW_INVARIANT (this);
74 }
75
76
77
78 Formula ::
79 Formula (const std::string& name, const std::string& formula, TTree *tree)
80 : m_formula (formula), m_name (name),
81 m_tree (0), m_form (0), m_manager (0), m_ndim (-1),
82 m_entry (-1), m_ndata (0)
83 {
84 RCU_NEW_INVARIANT (this);
85
86 reset (tree);
87 }
88
89
90
91 Formula ::
92 ~Formula ()
93 {
95
96 delete m_form;
97 m_form = 0;
98 }
99
100
101
102 void Formula ::
103 reset (TTree *tree)
104 {
107
108 delete m_form;
109 m_form = 0;
110 m_manager = 0;
111 m_ndim = -1;
112 m_entry = -1;
113 m_ndata = 0;
114 m_tree = 0;
115
116 m_tree = tree;
117 m_form = new TTreeFormula (m_name.c_str(), m_formula.c_str(), tree);
118
119 m_form->SetQuickLoad (kTRUE);
120 m_manager = new TTreeFormulaManager;
121 m_manager->Add (m_form);
122 m_manager->Sync ();
123 if (m_manager->GetMultiplicity () == -1)
124 tree->SetBit (TTree::kForceRead);
125 if (m_form->GetNdim() <= 0)
126 m_ndim = -1;
127 else if (m_form->GetMultiplicity() == 0)
128 m_ndim = 0;
129 else if (m_manager->GetMultiplicity() == 1 && m_form->GetMultiplicity() == 1)
130 m_ndim = 1;
131 else if (m_manager->GetMultiplicity() == -1 && m_form->GetMultiplicity() == 1)
132 m_ndim = 0;
133
134
135 }
136
137
138
139 const std::string& Formula ::
140 formula () const
141 {
142 RCU_READ_INVARIANT (this);
143 return m_formula;
144 }
145
146
147
148 bool Formula ::
149 valid () const
150 {
151 RCU_READ_INVARIANT (this);
152 return !m_formula.empty() && m_ndim >= 0;
153 }
154
155
156
157 int Formula ::
158 ndim () const
159 {
160 RCU_READ_INVARIANT (this);
162 return m_ndim;
163 }
164
165
166
167 std::size_t Formula ::
168 ndata () const
169 {
170 RCU_READ_INVARIANT (this);
172
173 if (m_ndim < 0)
174 RCU_THROW_MSG ("invalid formula: " + m_formula);
175 if (m_tree->GetReadEntry() != m_entry)
176 {
177 m_entry = -1;
178 m_ndata = m_manager->GetNdata();
179 if (m_ndata > 0)
180 {
181 if (m_read.size() < std::size_t (m_ndata))
182 m_read.resize (m_ndata);
183 for (std::size_t iter = 0, end = m_ndata; iter != end; ++ iter)
184 m_read[iter] = false;
185
186 if (m_cache.size() < std::size_t (m_ndata))
187 m_cache.resize (m_ndata);
188 m_cache[0] = m_form->EvalInstance (0);
189 m_read[0] = true;
190 m_entry = m_tree->GetReadEntry();
191 }
192 }
193 if (m_ndata < 0)
194 RCU_THROW_MSG ("failed to read formula: " + m_formula);
195 return m_ndata;
196 }
197
198
199
200 Double_t Formula ::
201 value (std::size_t data) const
202 {
203 RCU_READ_INVARIANT (this);
206
207 if (!m_read[data])
208 {
209 m_cache[data] = m_form->EvalInstance (data);
210 m_read[data] = true;
211 }
212 return m_cache[data];
213 }
214}
#define RCU_INVARIANT(x)
Definition Assert.h:201
#define RCU_DESTROY_INVARIANT(x)
Definition Assert.h:235
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:231
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:233
#define RCU_REQUIRE(x)
Definition Assert.h:208
#define RCU_REQUIRE_SOFT(x)
Definition Assert.h:153
#define RCU_READ_INVARIANT(x)
Definition Assert.h:229
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
static const Attributes_t empty
const std::string & formula() const
description: the formula used guarantee: no-fail
Definition Formula.cxx:140
Long64_t m_entry
description: the last entry we read
Definition Formula.h:146
TTreeFormulaManager * m_manager
description: the manager used
Definition Formula.h:137
TTreeFormula * m_form
description the formula used
Definition Formula.h:133
std::string m_name
description: the name we use for the formula
Definition Formula.h:125
TTree * m_tree
description: the tree we are connected to
Definition Formula.h:129
int m_ndim
description: the number of array dimensions we need to loop over, or -1 if we are in error
Definition Formula.h:142
std::vector< Double_t > m_cache
description: the cache of data entries
Definition Formula.h:154
bool valid() const
returns: whether the formula is valid guarantee: no-fail
Definition Formula.cxx:149
std::size_t ndata() const
returns: the number of data entries for the formula with the given index guarantee: strong failures: ...
Definition Formula.cxx:168
std::string m_formula
description: members directly corresponding to accessors
Definition Formula.h:121
void reset(TTree *tree)
effects: reset this formula to a new tree guarantee: strong failures: out of memory II failures: TTre...
Definition Formula.cxx:103
Int_t m_ndata
description: the number of data entries
Definition Formula.h:150
std::vector< bool > m_read
description: whether we read the given data entry
Definition Formula.h:158
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition AlgCFlow.h:31
TChain * tree