ATLAS Offline Software
AlgCFlow.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/AlgCFlow.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 AlgCFlow ::
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_hist->GetDimension() == 1);
47  RCU_INVARIANT (m_formulas.size() == std::size_t (m_hist->GetNbinsX()));
48  RCU_INVARIANT (m_formulas.size() == m_values.size());
49  RCU_INVARIANT (m_formulas.size() == m_back.size());
50  RCU_INVARIANT (m_formulas.size() == m_axis.size());
51  }
52  if (m_hist2)
53  {
54  RCU_INVARIANT (m_index.size() == m_formulas.size());
55  RCU_INVARIANT (m_formSvc != 0);
56  } else
57  RCU_INVARIANT (m_index.empty());
58  }
59 
60 
61 
63  AlgCFlow ()
64  : m_hist (0), m_hist2 (0), m_formSvc (0)
65  {
66  RCU_NEW_INVARIANT (this);
67  }
68 
69 
70 
72  AlgCFlow (TH1 *val_hist_swallow)
73  : m_hist (0), m_hist2 (0)
74  {
75  std::unique_ptr<TH1> hist (val_hist_swallow);
76 
77  RCU_REQUIRE_SOFT (val_hist_swallow != 0);
78  RCU_REQUIRE_SOFT (val_hist_swallow->GetDimension() == 1);
79 
80  for (std::size_t form = 0, end = hist->GetNbinsX();
81  form != end; ++ form)
82  {
83  const std::string label = hist->GetXaxis()->GetBinLabel(form+1);
84  const std::size_t split = label.find (':');
85 
86  m_axis.push_back (hist->GetXaxis()->GetBinCenter (form+1));
87  if (split == std::string::npos)
88  {
89  m_formulas.push_back (label);
90  m_back.push_back (form - 1);
91  } else
92  {
93  m_formulas.push_back (label.substr (0, split));
94  const std::string back = label.substr (split+1);
95  m_back.push_back (form);
96 
97  if (!back.empty())
98  {
99  for (std::size_t form2 = 0; form2 != form; ++ form2)
100  {
101  if (m_formulas[form2] == back)
102  m_back.back() = form2;
103  }
104  if (m_back.back() == form)
105  RCU_THROW_MSG ("unknown back formula: " + back);
106  }
107  }
108  }
109 
110  m_hist = hist.release();
111  m_hist->SetDirectory (0);
112 
113  m_values.resize (m_formulas.size());
114 
115  RCU_NEW_INVARIANT (this);
116  }
117 
118 
119 
122  {
123  RCU_CHANGE_INVARIANT (this);
124  useFormulas (job);
125  return EL::StatusCode::SUCCESS;
126  }
127 
128 
129 
131  initialize ()
132  {
133  RCU_CHANGE_INVARIANT (this);
134 
135  try
136  {
137  m_formSvc = formulas (wk());
138  for (std::size_t form = 0, end = m_formulas.size(); form != end; ++ form)
139  {
140  if (!m_formulas[form].empty())
141  m_index.push_back (m_formSvc->addForm (m_formulas[form]));
142  else
143  m_index.push_back (0);
144  }
145  m_hist2 = dynamic_cast<TH1*>(m_hist->Clone ());
146  RCU_ASSERT (m_hist2 != 0);
147  wk()->addOutput (m_hist2);
148  } catch (...)
149  {
150  m_index.clear ();
151  throw;
152  }
153  return EL::StatusCode::SUCCESS;
154  }
155 
156 
157 
159  execute ()
160  {
161  RCU_CHANGE_INVARIANT (this);
162 
163  RCU_ASSERT (m_hist2 != 0);
164 
165  int ndim = 0;
166  std::size_t size = std::size_t (-1);
167  for (std::size_t form = 0; form != m_formulas.size(); ++ form)
168  {
169  if (m_index[form]) switch (m_index[form]->ndim())
170  {
171  case -1:
172  RCU_THROW_MSG ("formula not valid: " + m_formulas[form]);
173  break;
174  case 0:
175  if (m_index[form]->ndata() > 0)
176  m_values[form] = m_index[form]->value (0);
177  else
178  size = 0;
179  break;
180  case 1:
181  ndim = 1;
182  if (size > m_index[form]->ndata())
183  size = m_index[form]->ndata();
184  break;
185  default:
186  RCU_THROW_MSG ("unknown formula dimension: " + m_formulas[form]);
187  }
188  }
189 
190  if (ndim == 0 && size > 1)
191  size = 1;
192  for (std::size_t iter = 0; iter != size; ++ iter)
193  {
194  for (std::size_t form = 0, end = m_formulas.size(); form != end; ++ form)
195  {
196  double weight = 1;
197 
198  if (m_back[form] < form)
199  weight = m_values[m_back[form]];
200 
201  if (weight != 0 && !m_formulas[form].empty())
202  {
203  if (m_index[form]->ndim() == 1)
204  weight *= m_index[form]->value (iter);
205  else
206  weight *= m_index[form]->value (0);
207  }
208 
209  m_values[form] = weight;
210  m_hist2->Fill (m_axis[form], m_values[form]);
211  }
212  }
213  return EL::StatusCode::SUCCESS;
214  }
215 }
MD::AlgCFlow::m_values
std::vector< Double_t > m_values
description: the value of the formulas
Definition: AlgCFlow.h:115
MD::AlgCFlow::AlgCFlow
AlgCFlow()
effects: default constructor guarantee: no-fail rationale: this should only be called by serializatio...
MD::AlgCFlow::initialize
virtual EL::StatusCode initialize()
effects: do everything that needs to be done before running the algorithm, e.g.
ClassImp
ClassImp(MD::AlgCFlow) namespace MD
Definition: AlgCFlow.cxx:35
MD::AlgCFlow::m_hist2
TH1 * m_hist2
description: the histogram we are filling
Definition: AlgCFlow.h:131
MD
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition: AlgCFlow.h:31
AlgCFlow.h
PlotCalibFromCool.label
label
Definition: PlotCalibFromCool.py:78
plotmaker.hist
hist
Definition: plotmaker.py:148
Formula.h
EL::IHistogramWorker::addOutput
virtual void addOutput(TObject *output_swallow)=0
effects: add an object to the output.
MD::AlgCFlow::setupJob
virtual EL::StatusCode setupJob(EL::Job &job)
effects: give the algorithm a chance to intialize the job with anything this algorithm needs.
Assert.h
empty
bool empty(TH1 *h)
Definition: computils.cxx:294
MD::AlgCFlow::m_formulas
std::vector< std::string > m_formulas
description: the list of formulas used
Definition: AlgCFlow.h:111
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...
MD::AlgCFlow::m_formSvc
FormulaSvc * m_formSvc
description: the formula service we are using
Definition: AlgCFlow.h:135
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
RCU_REQUIRE_SOFT
#define RCU_REQUIRE_SOFT(x)
Definition: Assert.h:153
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
MD::AlgCFlow::m_hist
TH1 * m_hist
description: the histogram we use as a template
Definition: AlgCFlow.h:107
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
MD::AlgCFlow::execute
virtual EL::StatusCode execute()
effects: process the next event guarantee: basic failures: algorithm dependent rationale: the virtual...
MD::AlgCFlow::testInvariant
void testInvariant() const
effects: test the invariant of this object guarantee: no-fail
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::AlgCFlow::m_axis
std::vector< Double_t > m_axis
description: the axis values of the bins
Definition: AlgCFlow.h:127
MD::AlgCFlow
Definition: AlgCFlow.h:33
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::AlgCFlow::m_index
std::vector< const Formula * > m_index
description: the indices to the formulas
Definition: AlgCFlow.h:119
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
IWorker.h
RCU_ASSERT
#define RCU_ASSERT(x)
Definition: Assert.h:222
MD::AlgCFlow::m_back
std::vector< std::size_t > m_back
description: the back-indices to the formulas
Definition: AlgCFlow.h:123
Trk::split
@ split
Definition: LayerMaterialProperties.h:38
RCU_NEW_INVARIANT
#define RCU_NEW_INVARIANT(x)
Definition: Assert.h:233