ATLAS Offline Software
Loading...
Searching...
No Matches
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>
25#include <EventLoop/IWorker.h>
26#include <MultiDraw/Formula.h>
30
31//
32// method implementations
33//
34
36
37namespace 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
62 AlgCFlow ::
63 AlgCFlow ()
64 : m_hist (0), m_hist2 (0), m_formSvc (0)
65 {
66 RCU_NEW_INVARIANT (this);
67 }
68
69
70
71 AlgCFlow ::
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
120 EL::StatusCode AlgCFlow ::
121 setupJob (EL::Job& job)
122 {
124 useFormulas (job);
125 return EL::StatusCode::SUCCESS;
126 }
127
128
129
130 EL::StatusCode AlgCFlow ::
131 initialize ()
132 {
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
158 EL::StatusCode AlgCFlow ::
159 execute ()
160 {
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}
ClassImp(MD::AlgCFlow) namespace MD
Definition AlgCFlow.cxx:35
#define RCU_INVARIANT(x)
Definition Assert.h:201
#define RCU_ASSERT(x)
Definition Assert.h:222
#define RCU_CHANGE_INVARIANT(x)
Definition Assert.h:231
#define RCU_NEW_INVARIANT(x)
Definition Assert.h:233
#define RCU_REQUIRE_SOFT(x)
Definition Assert.h:153
#define RCU_THROW_MSG(message)
Definition PrintMsg.h:58
static const Attributes_t empty
Definition Job.h:51
std::vector< std::string > split(const std::string &s, const std::string &t=":")
Definition hcg.cxx:177
std::string label(const std::string &format, int i)
Definition label.h:19
::StatusCode StatusCode
StatusCode definition for legacy code.
This module provides a lot of global definitions, forward declarations and includes that are used by ...
Definition AlgCFlow.h:31
FormulaSvc * formulas(EL::IWorker *worker)
returns: the formula service for this worker guarantee: strong failures: formula service not configur...
void useFormulas(EL::Job &job)
effects: register the formula service for this job guarantee: strong failures: out of memory I