ATLAS Offline Software
AlgHist.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 //
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/AlgHist.h>
18 
19 #include <memory>
20 #include <TH1.h>
21 #include <TH2.h>
22 #include <TH3.h>
23 #include <TProfile.h>
24 #include <EventLoop/StatusCode.h>
25 #include <EventLoop/IWorker.h>
26 #include <MultiDraw/Formula.h>
27 #include <MultiDraw/FormulaSvc.h>
28 #include <RootCoreUtils/Assert.h>
29 #include <RootCoreUtils/ThrowMsg.h>
30 
31 //
32 // method implementations
33 //
34 
36 
37 namespace MD
38 {
39  void AlgHist ::
40  testInvariant () const
41  {
42  RCU_INVARIANT (this != 0);
43  if (m_hist != 0)
44  {
45  RCU_INVARIANT (m_hist->GetDirectory() == 0);
46  RCU_INVARIANT (m_formulas.size() == m_valnum + m_cut);
47  RCU_INVARIANT (m_values.size() == m_valnum + 1);
48  }
49  switch (m_type)
50  {
51  case -1:
52  RCU_INVARIANT (m_hist == 0);
53  break;
54  case 0:
55  RCU_INVARIANT (m_hist != 0);
56  RCU_INVARIANT (m_valnum == 1);
57  break;
58  case 1:
59  RCU_INVARIANT (dynamic_cast<TProfile*>(m_hist) != 0);
60  RCU_INVARIANT (m_valnum == 2);
61  break;
62  case 2:
63  RCU_INVARIANT (dynamic_cast<TH2*>(m_hist) != 0);
64  RCU_INVARIANT (m_valnum == 2);
65  break;
66  case 3:
67  RCU_INVARIANT (dynamic_cast<TH3*>(m_hist) != 0);
68  RCU_INVARIANT (m_valnum == 3);
69  break;
70  default:
71  RCU_INVARIANT0 ("invalid type");
72  }
73  for (std::size_t form = 0, end = m_formulas.size(); form != end; ++ form)
74  RCU_INVARIANT (!m_formulas[form].empty());
75  if (m_hist2)
76  {
77  RCU_INVARIANT (m_index.size() == m_formulas.size());
78  RCU_INVARIANT (m_formSvc != 0);
79  } else
80  RCU_INVARIANT (m_index.empty());
81  }
82 
83 
84 
86  AlgHist ()
87  : m_hist (0), m_type (-1), m_valnum (1), m_cut (false), m_hist2 (0)
88  {
89  RCU_NEW_INVARIANT (this);
90  }
91 
92 
93 
95  AlgHist (TH1 *val_hist_swallow,
96  const std::string& val_value0,
97  const std::string& val_value1,
98  const std::string& val_value2,
99  const std::string& val_value3)
100  : m_hist (0), m_type (-1), m_valnum (1), m_cut (false), m_hist2 (0)
101  {
102  std::unique_ptr<TH1> hist (val_hist_swallow);
103 
104  RCU_REQUIRE_SOFT (val_hist_swallow != 0);
105  RCU_REQUIRE_SOFT (!val_value0.empty());
106 
107  m_formulas.push_back (val_value0);
108  if (!val_value1.empty())
109  m_formulas.push_back (val_value1);
110  if (!val_value2.empty())
111  m_formulas.push_back (val_value2);
112  if (!val_value3.empty())
113  m_formulas.push_back (val_value3);
114 
115  m_hist = hist.release();
116  m_hist->SetDirectory (0);
117  if (dynamic_cast<TProfile*>(m_hist) != 0)
118  {
119  m_type = 1;
120  m_valnum = 2;
121  } else if (dynamic_cast<TH2*>(m_hist) != 0)
122  {
123  m_type = 2;
124  m_valnum = 2;
125  } else if (dynamic_cast<TH3*>(m_hist) != 0)
126  {
127  m_type = 3;
128  m_valnum = 3;
129  } else
130  {
131  m_type = 0;
132  m_valnum = 1;
133  }
134  m_values.resize (m_valnum + 1, 1);
135  if (m_valnum == m_formulas.size())
136  m_cut = false;
137  else if (m_valnum+1 == m_formulas.size())
138  m_cut = true;
139  else
140  RCU_THROW_MSG ("invalid number of formulas");
141 
142  RCU_NEW_INVARIANT (this);
143  }
144 
145 
146  AlgHist ::
147  ~AlgHist ()
148  {
149  if (m_hist != nullptr) {
150  delete m_hist;
151  }
152  }
153 
154 
155 
158  {
159  RCU_CHANGE_INVARIANT (this);
160  useFormulas (job);
161  return EL::StatusCode::SUCCESS;
162  }
163 
164 
165 
167  initialize ()
168  {
169  RCU_CHANGE_INVARIANT (this);
170 
171  try
172  {
173  m_formSvc = formulas (wk());
174  for (std::size_t form = 0, end = m_formulas.size(); form != end; ++ form)
175  m_index.push_back (m_formSvc->addForm (m_formulas[form]));
176  m_hist2 = dynamic_cast<TH1*>(m_hist->Clone ());
177  RCU_ASSERT (m_hist2 != 0);
178  wk()->addOutput (m_hist2);
179  } catch (...)
180  {
181  m_index.clear ();
182  throw;
183  }
184  return EL::StatusCode::SUCCESS;
185  }
186 
187 
188 
190  execute ()
191  {
192  RCU_CHANGE_INVARIANT (this);
193 
194  RCU_ASSERT (m_formulas.size() == m_index.size());
195  RCU_ASSERT (m_formulas.size() <= m_values.size());
196 
197  if (m_cut && m_index.back()->ndim() == 0 &&
198  (m_values.back() = m_index.back()->value (0)) == 0)
199  return EL::StatusCode::SUCCESS;
200 
201  int ndim = 0;
202  std::size_t size = std::size_t (-1);
203  for (std::size_t form = 0; form != m_valnum; ++ form)
204  {
205  switch (m_index[form]->ndim())
206  {
207  case -1:
208  RCU_THROW_MSG ("formula not valid: " + m_formulas[form]);
209  break;
210  case 0:
211  if (m_index[form]->ndata() > 0)
212  m_values[form] = m_index[form]->value (0);
213  else
214  size = 0;
215  break;
216  case 1:
217  ndim = 1;
218  if (size > m_index[form]->ndata())
219  size = m_index[form]->ndata();
220  break;
221  default:
222  RCU_THROW_MSG ("unknown formula dimension: " + m_formulas[form]);
223  }
224  }
225 
226  if (ndim == 0 && size > 1)
227  size = 1;
228  for (std::size_t iter = 0; iter != size; ++ iter)
229  {
230  if (ndim == 1)
231  {
232  if (m_cut && m_index.back()->ndim() == 1 &&
233  (m_values.back() = m_index.back()->value (iter)) == 0)
234  continue;
235 
236  for (std::size_t form = 0; form != m_valnum; ++ form)
237  {
238  if (m_index[form]->ndim() == 1)
239  m_values[form] = m_index[form]->value (iter);
240  }
241  }
242 
243  switch (m_type)
244  {
245  case 0:
246  m_hist2->Fill (m_values[0], m_values[1]);
247  break;
248  case 1:
249  static_cast<TProfile*>(m_hist2)->Fill (m_values[0], m_values[1], m_values[2]);
250  break;
251  case 2:
252  static_cast<TH2*>(m_hist2)->Fill (m_values[0], m_values[1], m_values[2]);
253  break;
254  case 3:
255  static_cast<TH3*>(m_hist2)->Fill (m_values[0], m_values[1], m_values[2], m_values[3]);
256  break;
257  default:
258  RCU_THROW_MSG ("invalid number of values");
259  }
260  }
261  return EL::StatusCode::SUCCESS;
262  }
263 }
MD::AlgHist::m_index
std::vector< const Formula * > m_index
description: the indices to the formulas
Definition: AlgHist.h:141
MD::AlgHist
Definition: AlgHist.h:33
MD::AlgHist::m_cut
bool m_cut
description: whether we use a cut
Definition: AlgHist.h:137
MD::AlgHist::testInvariant
void testInvariant() const
effects: test the invariant of this object guarantee: no-fail
MD
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition: AlgCFlow.h:31
plotmaker.hist
hist
Definition: plotmaker.py:148
MD::AlgHist::execute
virtual EL::StatusCode execute()
effects: process the next event guarantee: basic failures: algorithm dependent rationale: the virtual...
Formula.h
EL::IHistogramWorker::addOutput
virtual void addOutput(TObject *output_swallow)=0
effects: add an object to the output.
Assert.h
MD::AlgHist::m_formSvc
FormulaSvc * m_formSvc
description: the formula service we are using
Definition: AlgHist.h:149
empty
bool empty(TH1 *h)
Definition: computils.cxx:295
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
MD::formulas
FormulaSvc * formulas(EL::IWorker *worker)
returns: the formula service for this worker guarantee: strong failures: formula service not configur...
m_type
TokenType m_type
the type
Definition: TProperty.cxx:44
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
MD::AlgHist::m_values
std::vector< Double_t > m_values
description: the value of the formulas
Definition: AlgHist.h:129
RCU_REQUIRE_SOFT
#define RCU_REQUIRE_SOFT(x)
Definition: Assert.h:153
python.TrigEgammaMonitorHelper.TProfile
def TProfile(*args, **kwargs)
Definition: TrigEgammaMonitorHelper.py:81
MD::AlgHist::AlgHist
AlgHist()
effects: default constructor guarantee: no-fail rationale: this should only be called by serializatio...
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MD::AlgHist::setupJob
virtual EL::StatusCode setupJob(EL::Job &job)
effects: give the algorithm a chance to intialize the job with anything this algorithm needs.
MD::AlgHist::m_valnum
std::size_t m_valnum
description: the number of values without the weight/cut
Definition: AlgHist.h:133
MD::AlgHist::m_hist2
TH1 * m_hist2
description: the histogram we are filling
Definition: AlgHist.h:145
RCU_INVARIANT0
#define RCU_INVARIANT0(y)
Definition: Assert.h:205
ClassImp
ClassImp(MD::AlgHist) namespace MD
Definition: AlgHist.cxx:35
MD::AlgHist::initialize
virtual EL::StatusCode initialize()
effects: do everything that needs to be done before running the algorithm, e.g.
EL::Algorithm::wk
IWorker * wk() const
description: the worker that is controlling us guarantee: no-fail
RCU_INVARIANT
#define RCU_INVARIANT(x)
Definition: Assert.h:201
ThrowMsg.h
MD::FormulaSvc::addForm
const Formula * addForm(const std::string &formula)
effects: add another formula returns: the formula guarantee: strong failures: out of memory II failur...
FormulaSvc.h
MD::AlgHist::m_hist
TH1 * m_hist
description: the histogram we use as a template
Definition: AlgHist.h:117
MD::useFormulas
void useFormulas(EL::Job &job)
effects: register the formula service for this job guarantee: strong failures: out of memory I
StatusCode.h
RCU_CHANGE_INVARIANT
#define RCU_CHANGE_INVARIANT(x)
Definition: Assert.h:231
MD::AlgHist::m_type
int m_type
description: the type of histogram used
Definition: AlgHist.h:121
RCU_THROW_MSG
#define RCU_THROW_MSG(message)
Definition: PrintMsg.h:58
EL::Job
Definition: Job.h:51
test_interactive_athena.job
job
Definition: test_interactive_athena.py:6
MD::AlgHist::m_formulas
std::vector< std::string > m_formulas
description: the list of formulas used
Definition: AlgHist.h:125
IWorker.h
RCU_ASSERT
#define RCU_ASSERT(x)
Definition: Assert.h:222
MD::AlgHist::~AlgHist
~AlgHist()
effects: destructor guarantee: no-fail rationale: this is needed to delete the template histogram
AlgHist.h
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233