MaCh3  2.5.1
Reference Guide
SampleHandlerNuDockBase.cpp
Go to the documentation of this file.
2 #include <unordered_map>
3 #include <NuDock/NuDockFactory.h>
4 
5 // ***************************************************************************
7 // ***************************************************************************
10  MACH3LOG_INFO("Creating SampleHandlerNuDock object..");
11  MACH3LOG_INFO("- Using NuDock sample config in this file {}", configFile);
12  ParHandler = xsec_cov;
13  SampleManager = std::make_unique<Manager>(configFile.c_str());
14  verbose = GetFromManager(SampleManager->raw()["NuDockClient"]["Verbose"], false);
15 
16  // ReadConfig();
17  // SetupReweightArrays();
18  Init();
19 }
20 
21 // ***************************************************************************
23 // ***************************************************************************
25 }
26 
27 // ***************************************************************************
29 // ***************************************************************************
32 
33  // Gather the indices of NuDock parameters from the ParameterHandler, so that we can easily retrieve their values in Reweight()
34  auto nudockParamInds_func = ParHandler->GetParsIndexFromSampleName("NuDock", SystType::kFunc);
35  // HH: We should only be using kFunc for NuDock, but for convenience of testing (e.g. when validating
36  // M3 client with M3 server we'd want to test the LLH of all the params) we include all param types here.
37  auto nudockParamInds_norm = ParHandler->GetParsIndexFromSampleName("NuDock", SystType::kNorm);
38  auto nudockParamInds_spline = ParHandler->GetParsIndexFromSampleName("NuDock", SystType::kSpline);
39  nudockParamInds = nudockParamInds_func;
40  nudockParamInds.insert(nudockParamInds.end(), nudockParamInds_norm.begin(), nudockParamInds_norm.end());
41  nudockParamInds.insert(nudockParamInds.end(), nudockParamInds_spline.begin(), nudockParamInds_spline.end());
42 
43  nudock_ptr->start_client();
44 }
45 
46 // ***************************************************************************
48 // ***************************************************************************
50  nlohmann::json request;
51  std::unordered_map<std::string, double> osc_params;
52  std::unordered_map<std::string, double> xsec_params;
53 
54  // Loop for systs
55  for (const auto& iParam : nudockParamInds) {
56  std::string paramName = ParHandler->GetParFancyName(iParam);
57  double paramValue = ParHandler->GetParProp(iParam);
58  xsec_params[paramName] = paramValue;
59  }
60 
61  // Loop over NuDockOscNameMap_r to get osc params
62  for (auto const& pair : NuDockOscNameMap_r) {
63  auto const& paramNameM3 = pair.first;
64  auto const& paramNameNuDock = pair.second;
65 
66  int iParam = ParHandler->GetParIndex(paramNameM3);
67  double paramValue = ParHandler->GetParProp(iParam);
68  // Convert sin2_theta to theta
69  FormatOscParsForNuDock(paramNameNuDock, paramValue);
70  osc_params[paramNameNuDock] = paramValue;
71  }
72 
73  request["osc_pars"] = osc_params;
74  request["sys_pars"] = xsec_params;
75 
76  auto response = nudock_ptr->send_request("/set_parameters", request);
77  if (verbose) {
78  try {
79  MACH3LOG_INFO("NuDock response: {}", response.dump());
80  } catch (const std::exception &e) {
81  MACH3LOG_ERROR("Error dumping NuDock response: {}", e.what());
82  throw MaCh3Exception(__FILE__, __LINE__);
83  }
84  }
85 }
86 
87 // ***************************************************************************
89 // ***************************************************************************
91  nlohmann::json request = "";
92  double llh_value = 0.0;
93  auto response = nudock_ptr->send_request("/log_likelihood", request);
94  try {
95  llh_value = response["log_likelihood"].get<double>();
96  llh_value /= 2; // NuDock returns 2NLL, so we divide by 2 to be consistent with M3's definition of LLH.
97  return llh_value;
98  } catch (const std::exception &e) {
99  MACH3LOG_ERROR("Error retrieving log-likelihood from NuDock response: {}", e.what());
100  throw MaCh3Exception(__FILE__, __LINE__);
101  }
102 }
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
const std::unordered_map< std::string, std::string > NuDockOscNameMap_r
Mapping from MaCh3 oscillation parameter names to NuDock names.
void FormatOscParsForNuDock(const std::string &param_name, double &param_value)
Convert an oscillation parameter value from MaCh3 convention to NuDock convention.
void InitialiseNuDockObj(Manager *man, std::unique_ptr< NuDock > &nudock_ptr)
Initialise a NuDock communication object from a manager configuration.
Factory utilities for creating and configuring NuDock communication objects.
@ kNorm
For normalisation parameters.
@ kSpline
For splined parameters (1D)
@ kFunc
For functional parameters.
Client-side sample handler that delegates reweighting and likelihood evaluation to a remote NuDock se...
Type GetFromManager(const YAML::Node &node, const Type defval, const std::string File="", const int Line=1)
Get content of config file if node is not found take default value specified.
Definition: YamlHelper.h:329
Custom exception class used throughout MaCh3.
int GetParIndex(const std::string &name) const
Get index based on name.
std::string GetParFancyName(const int i) const
Get fancy name of the Parameter.
M3::float_t GetParProp(const int i) const
Get proposed parameter value.
Class responsible for handling of systematic error parameters with different types defined in the con...
const std::vector< int > GetParsIndexFromSampleName(const std::string &SampleName, const SystType Type) const
DB Grab the parameter indices for the relevant SampleName.
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...
virtual ~SampleHandlerNuDockBase()
Destructor.
double GetLikelihood() const override
Retrieve the log-likelihood from the NuDock server.
void Reweight() override
Send current parameter values to the NuDock server.
std::vector< int > nudockParamInds
Cached indices into the ParameterHandler for parameters sent to the server.
std::unique_ptr< Manager > SampleManager
Manager owning the NuDockClient configuration.
bool verbose
Verbose logging flag, read from the NuDockClient config block.
ParameterHandlerGeneric * ParHandler
Non-owning pointer to the cross-section ParameterHandler.
std::unique_ptr< NuDock > nudock_ptr
Pointer to the NuDock client communication object.
void Init()
Initialise the NuDock client connection and cache parameter indices.
SampleHandlerNuDockBase(std::string configFile, ParameterHandlerGeneric *xsec_cov)
Construct the NuDock client sample handler.