Logo ROOT  
Reference Guide
LikelihoodJob.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * PB, Patrick Bos, Netherlands eScience Center, p.bos@esciencecenter.nl
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
13#include "LikelihoodJob.h"
14
26#include "RooRealVar.h"
27
28namespace RooFit {
29namespace TestStatistics {
30
32 std::shared_ptr<RooAbsL> likelihood,
33 std::shared_ptr<WrapperCalculationCleanFlags> calculation_is_clean)
34 : LikelihoodWrapper(std::move(likelihood), std::move(calculation_is_clean)),
35 n_event_tasks_(MultiProcess::Config::LikelihoodJob::defaultNEventTasks),
36 n_component_tasks_(MultiProcess::Config::LikelihoodJob::defaultNComponentTasks)
37{
38 init_vars();
39 // determine likelihood type
40 if (dynamic_cast<RooUnbinnedL *>(likelihood_.get()) != nullptr) {
42 } else if (dynamic_cast<RooBinnedL *>(likelihood_.get()) != nullptr) {
44 } else if (dynamic_cast<RooSumL *>(likelihood_.get()) != nullptr) {
46 } else if (dynamic_cast<RooSubsidiaryL *>(likelihood_.get()) != nullptr) {
48 } else {
49 throw std::logic_error("in LikelihoodJob constructor: likelihood is not of a valid subclass!");
50 }
51 // Note to future maintainers: take care when storing the minimizer_fcn pointer. The
52 // RooAbsMinimizerFcn subclasses may get cloned inside MINUIT, which means the pointer
53 // should also somehow be updated in this class.
54}
55
57{
58 return new LikelihoodJob(*this);
59}
60
61// This is a separate function (instead of just in ctor) for historical reasons.
62// Its predecessor RooRealMPFE::initVars() was used from multiple ctors, but also
63// from RooRealMPFE::constOptimizeTestStatistic at the end, which makes sense,
64// because it might change the set of variables. We may at some point want to do
65// this here as well.
67{
68 // Empty current lists
71
72 // Retrieve non-constant parameters
73 auto vars = std::make_unique<RooArgSet>(
74 *likelihood_->getParameters()); // TODO: make sure this is the right list of parameters, compare to original
75 // implementation in RooRealMPFE.cxx
76 RooArgList varList(*vars);
77
78 // Save in lists
79 vars_.add(varList);
80 save_vars_.addClone(varList);
81}
82
84{
85 if (get_manager()->process_manager().is_worker()) {
87 switch (mode) {
90 auto message = get_manager()->messenger().receive_from_master_on_worker<zmq::message_t>();
91 auto message_begin = message.data<update_state_t>();
92 auto message_end = message_begin + message.size() / sizeof(update_state_t);
93 std::vector<update_state_t> to_update(message_begin, message_end);
94 for (auto const &item : to_update) {
95 RooRealVar *rvar = (RooRealVar *)vars_.at(item.var_index);
96 rvar->setVal(static_cast<double>(item.value));
97 if (rvar->isConstant() != item.is_constant) {
98 rvar->setConstant(static_cast<bool>(item.is_constant));
99 }
100 }
101 break;
102 }
104 LikelihoodWrapper::enableOffsetting(get_manager()->messenger().receive_from_master_on_worker<bool>());
105 break;
106 }
107 }
108 }
109}
110
111/// \warning In automatic mode, this function can start MultiProcess (forks, starts workers, etc)!
113{
114 std::size_t val = n_event_tasks_;
117 }
118 if (val > likelihood_->getNEvents()) {
119 val = likelihood_->getNEvents();
120 }
121 return val;
122}
123
124
126{
127 std::size_t val = n_component_tasks_;
129 val = 1;
130 }
131 if (val > likelihood_->getNComponents()) {
132 val = likelihood_->getNComponents();
133 }
134 return val;
135}
136
138{
139 if (get_manager()->process_manager().is_master()) {
140 bool valChanged = false;
141 bool constChanged = false;
142 std::vector<update_state_t> to_update;
143 for (std::size_t ix = 0u; ix < static_cast<std::size_t>(vars_.getSize()); ++ix) {
144 valChanged = !vars_[ix].isIdentical(save_vars_[ix], true);
145 constChanged = (vars_[ix].isConstant() != save_vars_[ix].isConstant());
146
147 if (valChanged || constChanged) {
148 if (constChanged) {
149 ((RooRealVar *)&save_vars_[ix])->setConstant(vars_[ix].isConstant());
150 }
151 // TODO: Check with Wouter why he uses copyCache in MPFE; makes it very difficult to extend, because
152 // copyCache is protected (so must be friend). Moved setting value to if-block below.
153 // _saveVars[ix].copyCache(&_vars[ix]);
154
155 // send message to queue (which will relay to workers)
156 RooAbsReal *rar_val = dynamic_cast<RooAbsReal *>(&vars_[ix]);
157 if (rar_val) {
158 double val = rar_val->getVal();
159 dynamic_cast<RooRealVar *>(&save_vars_[ix])->setVal(val);
160 bool isC = vars_[ix].isConstant();
161 to_update.push_back(update_state_t{ix, val, isC});
162 }
163 }
164 }
165 if (!to_update.empty()) {
166 ++state_id_;
167 zmq::message_t message(to_update.begin(), to_update.end());
168 // always send Job id first! This is used in worker_loop to route the
169 // update_state call to the correct Job.
171 }
172 }
173}
174
176{
178}
179
181{
182 if (get_manager()->process_manager().is_master()) {
183 // update parameters that changed since last calculation (or creation if first time)
185
186 // master fills queue with tasks
187 auto N_tasks = getNEventTasks() * getNComponentTasks();
188 for (std::size_t ix = 0; ix < N_tasks; ++ix) {
189 get_manager()->queue().add({id_, state_id_, ix});
190 }
191 n_tasks_at_workers_ = N_tasks;
192
193 // wait for task results back from workers to master
195
196 result_ = 0;
197 for (auto const &item : results_) {
198 result_ += item;
199 }
201 results_.clear();
202 }
203}
204
205// --- RESULT LOGISTICS ---
206
208{
209 task_result_t task_result{id_, result_.Result(), result_.Carry()};
210 zmq::message_t message(sizeof(task_result_t));
211 memcpy(message.data(), &task_result, sizeof(task_result_t));
212 get_manager()->messenger().send_from_worker_to_master(std::move(message));
213}
214
215bool LikelihoodJob::receive_task_result_on_master(const zmq::message_t &message)
216{
217 auto task_result = message.data<task_result_t>();
218 results_.emplace_back(task_result->value, task_result->carry);
220 bool job_completed = (n_tasks_at_workers_ == 0);
221 return job_completed;
222}
223
224// --- END OF RESULT LOGISTICS ---
225
226void LikelihoodJob::evaluate_task(std::size_t task)
227{
228 assert(get_manager()->process_manager().is_worker());
229
230 double section_first = 0;
231 double section_last = 1;
232 if (getNEventTasks() > 1) {
233 std::size_t event_task = task % getNEventTasks();
234 std::size_t N_events = likelihood_->numDataEntries();
235 if (event_task > 0) {
236 std::size_t first = N_events * event_task / getNEventTasks();
237 section_first = static_cast<double>(first) / N_events;
238 }
239 if (event_task < getNEventTasks() - 1) {
240 std::size_t last = N_events * (event_task + 1) / getNEventTasks();
241 section_last = static_cast<double>(last) / N_events;
242 }
243 }
244
245 switch (likelihood_type_) {
248 result_ = likelihood_->evaluatePartition({section_first, section_last}, 0, 0);
249 break;
250 }
251 case LikelihoodType::sum: {
252 std::size_t components_first = 0;
253 std::size_t components_last = likelihood_->getNComponents();
254 if (getNComponentTasks() > 1) {
255 std::size_t component_task = task / getNEventTasks();
256 components_first = likelihood_->getNComponents() * component_task / getNComponentTasks();
257 if (component_task == getNComponentTasks() - 1) {
258 components_last = likelihood_->getNComponents();
259 } else {
260 components_last = likelihood_->getNComponents() * (component_task + 1) / getNComponentTasks();
261 }
262 }
263 result_ = likelihood_->evaluatePartition({section_first, section_last}, components_first, components_last);
264 break;
265 }
266
267 default: {
268 throw std::logic_error(
269 "in LikelihoodJob::evaluate_task: likelihood types other than binned and unbinned not yet implemented!");
270 break;
271 }
272 }
273}
274
276{
279}
280
281#define PROCESS_VAL(p) \
282 case (p): s = #p; break;
283
284std::ostream &operator<<(std::ostream &out, const LikelihoodJob::update_state_mode value)
285{
286 std::string s;
287 switch (value) {
290 default: s = std::to_string(static_cast<int>(value));
291 }
292 return out << s;
293}
294
295#undef PROCESS_VAL
296
297} // namespace TestStatistics
298} // namespace RooFit
#define PROCESS_VAL(p)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
Option_t Option_t TPoint TPoint const char mode
T Result() const
Definition: Util.h:245
T Carry() const
Definition: Util.h:255
bool isConstant() const
Check if the "Constant" attribute is set.
Definition: RooAbsArg.h:377
virtual void removeAll()
Remove all arguments from our set, deleting them if we own them.
Int_t getSize() const
Return the number of elements in the collection.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
virtual RooAbsArg * addClone(const RooAbsArg &var, bool silent=false)
Add a clone of the specified argument to list.
void setConstant(bool value=true)
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:62
double getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:91
RooArgList is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgList.h:22
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition: RooArgList.h:110
Messenger & messenger() const
Definition: JobManager.cxx:140
ProcessManager & process_manager() const
Definition: JobManager.cxx:135
std::size_t id_
Definition: Job.h:45
std::size_t state_id_
Definition: Job.h:46
JobManager * get_manager()
Get JobManager instance; create and activate if necessary.
Definition: Job.cxx:116
void gather_worker_results()
Wait for all tasks to be retrieved for the current Job.
Definition: Job.cxx:130
value_t receive_from_master_on_worker(bool *more=nullptr)
Definition: Messenger.h:176
void publish_from_master_to_workers(T &&item)
specialization that sends the final message
Definition: Messenger.h:150
void add(JobTask job_task)
Enqueue a task.
Definition: Queue.cxx:61
void enableOffsetting(bool flag) override
bool receive_task_result_on_master(const zmq::message_t &message) override
void evaluate_task(std::size_t task) override
std::vector< ROOT::Math::KahanSum< double > > results_
Definition: LikelihoodJob.h:67
void send_back_task_result_from_worker(std::size_t task) override
void update_state() override
Virtual function to update any necessary state on workers.
void evaluate() override
Triggers (possibly asynchronous) evaluation of the likelihood.
LikelihoodJob(std::shared_ptr< RooAbsL > _likelihood, std::shared_ptr< WrapperCalculationCleanFlags > calculation_is_clean)
ROOT::Math::KahanSum< double > result_
Definition: LikelihoodJob.h:66
LikelihoodJob * clone() const override
Virtual base class for implementation of likelihood calculation strategies.
ROOT::Math::KahanSum< double > applyOffsetting(ROOT::Math::KahanSum< double > current_value)
Likelihood class that sums over multiple -log components.
Definition: RooSumL.h:26
RooRealVar represents a variable that can be changed from the outside.
Definition: RooRealVar.h:40
void setVal(double value) override
Set value of variable to 'value'.
Definition: RooRealVar.cxx:254
std::size_t State
Definition: types.h:23
std::ostream & operator<<(std::ostream &out, const LikelihoodJob::update_state_mode value)
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition: Common.h:18
static constexpr double s
Definition: first.py:1
static constexpr std::size_t automaticNEventTasks
Definition: Config.h:28
static constexpr std::size_t automaticNComponentTasks
Definition: Config.h:29