ATLAS Offline Software
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>
25 #include <EventLoop/StatusCode.h>
26 #include <EventLoop/IWorker.h>
27 #include <RootCoreUtils/Assert.h>
28 #include <RootCoreUtils/ThrowMsg.h>
29 
30 //
31 // method implementations
32 //
33 
34 namespace MD
35 {
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  {
46  RCU_INVARIANT (m_manager != 0);
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);
55  RCU_INVARIANT (m_manager == 0);
56  }
57  } else
58  {
59  RCU_INVARIANT (m_name.empty());
60  RCU_INVARIANT (m_tree == 0);
61  RCU_INVARIANT (m_form == 0);
62  RCU_INVARIANT (m_manager == 0);
63  }
64  }
65 
66 
67 
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 
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 
92  ~Formula ()
93  {
94  RCU_DESTROY_INVARIANT (this);
95 
96  delete m_form;
97  m_form = 0;
98  }
99 
100 
101 
103  reset (TTree *tree)
104  {
105  RCU_CHANGE_INVARIANT (this);
106  RCU_REQUIRE (!formula().empty());
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 
149  valid () const
150  {
151  RCU_READ_INVARIANT (this);
152  return !m_formula.empty() && m_ndim >= 0;
153  }
154 
155 
156 
158  ndim () const
159  {
160  RCU_READ_INVARIANT (this);
161  RCU_REQUIRE_SOFT (valid ());
162  return m_ndim;
163  }
164 
165 
166 
167  std::size_t Formula ::
168  ndata () const
169  {
170  RCU_READ_INVARIANT (this);
171  RCU_REQUIRE_SOFT (valid ());
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);
204  RCU_REQUIRE_SOFT (valid ());
205  RCU_REQUIRE_SOFT (data < ndata ());
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 }
MD::Formula::formula
const std::string & formula() const
description: the formula used guarantee: no-fail
Definition: Formula.cxx:140
data
char data[hepevt_bytes_allocation_ATLAS]
Definition: HepEvt.cxx:11
MD::Formula::m_form
TTreeFormula * m_form
description the formula used
Definition: Formula.h:133
MD
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition: AlgCFlow.h:31
MD::Formula::m_name
std::string m_name
description: the name we use for the formula
Definition: Formula.h:125
Job.h
MD::Formula::m_ndim
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
RCU_REQUIRE
#define RCU_REQUIRE(x)
Definition: Assert.h:208
Formula.h
tree
TChain * tree
Definition: tile_monitor.h:30
MD::Formula::valid
bool valid() const
returns: whether the formula is valid guarantee: no-fail
Definition: Formula.cxx:149
MD::Formula::value
Double_t value(std::size_t data) const
returns: the result of evaluating the formula with the given index for the given data entry guarantee...
Definition: Formula.cxx:201
MD::Formula::ndata
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
Assert.h
MD::Formula::~Formula
~Formula()
effects: standard destructor guarantee: no-fail
Definition: Formula.cxx:92
empty
bool empty(TH1 *h)
Definition: computils.cxx:295
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
MD::Formula::m_cache
std::vector< Double_t > m_cache
description: the cache of data entries
Definition: Formula.h:154
MD::Formula::m_formula
std::string m_formula
description: members directly corresponding to accessors
Definition: Formula.h:121
MD::Formula::ndim
int ndim() const
returns: the number of unspecified array dimensions for the given formula guarantee: no-fail requires...
Definition: Formula.cxx:158
RCU_REQUIRE_SOFT
#define RCU_REQUIRE_SOFT(x)
Definition: Assert.h:153
MD::Formula::m_entry
Long64_t m_entry
description: the last entry we read
Definition: Formula.h:146
MD::Formula::m_manager
TTreeFormulaManager * m_manager
description: the manager used
Definition: Formula.h:137
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
MD::Formula::m_read
std::vector< bool > m_read
description: whether we read the given data entry
Definition: Formula.h:158
MD::Formula::Formula
Formula()
effects: standard default constructor guarantee: no-fail
Definition: Formula.cxx:69
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ThrowMsg.h
MD::Formula::testInvariant
void testInvariant() const
effects: test the invariant of this object guarantee: no-fail
Definition: Formula.cxx:37
MD::Formula::m_tree
TTree * m_tree
description: the tree we are connected to
Definition: Formula.h:129
RCU_DESTROY_INVARIANT
#define RCU_DESTROY_INVARIANT(x)
Definition: Assert.h:235
StatusCode.h
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
MD::Formula::reset
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
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
IWorker.h
RCU_READ_INVARIANT
#define RCU_READ_INVARIANT(x)
Definition: Assert.h:229
MD::Formula::m_ndata
Int_t m_ndata
description: the number of data entries
Definition: Formula.h:150
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233