48 for (
size_t i = 0; i <
samples.size(); ++i) {
53 for (
int iSample = 0; iSample <
samples[i]->GetNSamples(); ++iSample) {
54 double sample_llh_ind =
samples[i]->GetSampleLikelihood(iSample);
69 MACH3LOG_INFO(
"Setting parameters from NuDock request: {}", request.dump());
71 std::map<std::string, double> syst_params = request[
"sys_pars"].get<std::map<std::string, double>>();
72 std::map<std::string, double> osc_params = request[
"osc_pars"].get<std::map<std::string, double>>();
77 for (
int p = 0; p < npars; ++p) {
78 std::string param_name =
systematics[s]->GetParFancyName(p);
83 if (osc_params.find(param_name_nudock) != osc_params.end()) {
84 double param_value = osc_params[param_name_nudock];
89 MACH3LOG_WARN(
"Oscillation parameter {} not found in request", param_name);
95 if (syst_params.find(param_name) != syst_params.end()) {
96 double param_value = syst_params[param_name];
100 MACH3LOG_WARN(
"Systematic parameter {} not found in request", param_name);
107 for (
size_t i = 0; i <
samples.size(); ++i) {
111 nlohmann::json response;
112 response[
"status"] =
"success";
121 for (
size_t ipdf=0; ipdf<
samples.size(); ipdf++) {
124 if(!base_casted_sample) {
125 MACH3LOG_ERROR(
"Sample object does not derive from SampleHandlerBase. Consider overloading {} for this sample type.", __func__);
128 for (
int iSample = 0; iSample <
samples[ipdf]->GetNSamples(); ++iSample) {
129 base_casted_sample->AddData(iSample, base_casted_sample->GetMCArray(iSample));
136 for (
int p = 0; p < npars; ++p) {
137 std::string param_name =
systematics[s]->GetParFancyName(p);
138 double param_value =
systematics[s]->GetParCurr(p);
140 if (
verbose)
MACH3LOG_INFO(
"Setting prefit param {} to current value {}", param_name, param_value);
144 nlohmann::json response;
145 response[
"status"] =
"success";
149 void TH1toResponse(
const TH1* mc_hist,
const std::string& sample_title, nlohmann::json &response) {
151 auto hist1d =
dynamic_cast<const TH1D*
>(mc_hist);
156 const TAxis *ax = mc_hist->GetXaxis();
157 std::string xtitle = ax->GetTitle();
158 int nxbins = ax->GetNbins();
159 std::vector<double> xbins(nxbins+1);
160 std::vector<double> binvals(nxbins);
162 xbins[0] = ax->GetBinLowEdge(1);
163 for (
int ix = 1; ix <= nxbins; ++ix) {
164 xbins[ix] = ax->GetBinUpEdge(ix);
165 binvals[ix-1] = mc_hist->GetBinContent(ix);
167 response[
"axis_titles"][sample_title] = {xtitle};
168 response[
"bin_edges"][sample_title] = {xbins};
169 response[
"bin_values"][sample_title] = binvals;
173 void TH2toResponse(
const TH2* mc_hist,
const std::string& sample_title, nlohmann::json &response) {
175 auto hist2d =
dynamic_cast<const TH2D*
>(mc_hist);
180 const TAxis *x_axis = mc_hist->GetXaxis();
181 std::string xtitle = x_axis->GetTitle();
182 int nxbins = x_axis->GetNbins();
183 std::vector<double> xbins(nxbins+1);
185 const TAxis *y_axis = mc_hist->GetYaxis();
186 std::string ytitle = y_axis->GetTitle();
187 int nybins = y_axis->GetNbins();
188 std::vector<double> ybins(nybins+1);
190 std::vector<std::vector<double>> binvals(nxbins, std::vector<double>(nybins));
192 xbins[0] = x_axis->GetBinLowEdge(1);
193 ybins[0] = y_axis->GetBinLowEdge(1);
194 for (
int ix = 1; ix <= nxbins; ++ix) {
195 for (
int iy = 1; iy <= nybins; ++iy) {
196 xbins[ix] = x_axis->GetBinUpEdge(ix);
197 ybins[iy] = y_axis->GetBinUpEdge(iy);
198 binvals[ix-1][iy-1] = mc_hist->GetBinContent(ix, iy);
201 response[
"axis_titles"][sample_title] = {xtitle, ytitle};
202 response[
"bin_edges"][sample_title] = {xbins, ybins};
203 response[
"bin_values"][sample_title] = binvals;
210 nlohmann::json response;
212 std::vector<std::string> sample_titles = {};
214 for (
size_t ipdf=0; ipdf<
samples.size(); ipdf++) {
216 if(!base_casted_sample) {
217 MACH3LOG_ERROR(
"Sample object does not derive from SampleHandlerBase. Consider overloading {} for this sample type.", __func__);
220 for (
int iSample = 0; iSample <
samples[ipdf]->GetNSamples(); ++iSample) {
221 std::string sample_title =
samples[ipdf]->GetSampleTitle(iSample);
222 sample_titles.push_back(sample_title);
224 int dimension =
samples[ipdf]->GetNDim(iSample);
225 response[
"dimensions"][sample_title] = dimension;
227 if (dimension == 1) {
229 }
else if (dimension == 2) {
230 TH2toResponse(
static_cast<const TH2*
>(
samples[ipdf]->GetMCHist(iSample)), sample_title, response);
237 response[
"sample_names"] = sample_titles;
245 nlohmann::json response;
247 std::vector<std::string> sample_titles = {};
249 for (
size_t ipdf=0; ipdf<
samples.size(); ipdf++) {
251 if(!base_casted_sample) {
252 MACH3LOG_ERROR(
"Sample object does not derive from SampleHandlerBase. Consider overloading {} for this sample type.", __func__);
255 for (
int iSample = 0; iSample <
samples[ipdf]->GetNSamples(); ++iSample) {
256 std::string sample_title =
samples[ipdf]->GetSampleTitle(iSample);
257 sample_titles.push_back(sample_title);
259 int dimension =
samples[ipdf]->GetNDim(iSample);
260 response[
"dimensions"][sample_title] = dimension;
262 if (dimension == 1) {
264 }
else if (dimension == 2) {
265 TH2toResponse(
static_cast<const TH2*
>(
samples[ipdf]->GetDataHist(iSample)), sample_title, response);
272 response[
"sample_names"] = sample_titles;
280 std::vector<std::string> syst_par_names;
284 for (
int p = 0; p < npars; ++p) {
285 std::string param_name =
systematics[s]->GetParFancyName(p);
288 syst_par_names.push_back(param_name);
292 nlohmann::json response;
293 response[
"sys_pars"] = syst_par_names;
301 std::map<std::string, double> osc_params;
302 std::map<std::string, double> syst_params;
306 for (
int p = 0; p < npars; ++p) {
307 std::string param_name =
systematics[s]->GetParFancyName(p);
308 double param_value =
systematics[s]->GetParProp(p);
311 osc_params[param_name] = param_value;
314 syst_params[param_name] = param_value;
320 nlohmann::json response;
321 response[
"sys_pars"] = syst_params;
322 response[
"osc_pars"] = osc_params;
330 nlohmann::json response;
const std::unordered_map< std::string, std::string > NuDockOscNameMap_r
Mapping from MaCh3 oscillation parameter names to NuDock names.
void FormatOscParsForMaCh3(const std::string ¶m_name, double ¶m_value)
Convert an oscillation parameter value from NuDock convention to MaCh3 convention.
void TH2toResponse(const TH2 *mc_hist, const std::string &sample_title, nlohmann::json &response)
void TH1toResponse(const TH1 *mc_hist, const std::string &sample_title, nlohmann::json &response)
Base class for the MaCh3 NuDock server, exposing fit internals via a JSON-based request/response prot...
Type GetFromManager(const YAML::Node &node, const Type defval, const std::string &File, const int Line)
Get content of config file if node is not found take default value specified.
Base class for implementing fitting algorithms.
std::vector< double > sample_llh
store the llh breakdowns
std::vector< SampleHandlerInterface * > samples
Sample holder.
Manager * fitMan
The manager for configuration handling.
std::vector< double > syst_llh
systematic llh breakdowns
std::vector< ParameterHandlerBase * > systematics
Systematic holder.
Custom exception class used throughout MaCh3.
The manager class is responsible for managing configurations and settings.
YAML::Node const & raw() const
Return config.
virtual nlohmann::json getDataSpectrum(const nlohmann::json &request)
Handle a "get_data_spectrum" request.
virtual nlohmann::json setAsimovPoint(const nlohmann::json &request)
Handle a "set_asimov" request.
void setup()
Perform one-time server setup.
virtual nlohmann::json get2NLL(const nlohmann::json &request)
Handle a "log_likelihood" request.
virtual ~NuDockServerBase()
Destructor.
virtual double getLogLikelihood()
Compute the total log-likelihood across all systematics and samples.
NuDockServerBase(Manager *const fitMan)
Construct the NuDock server.
std::string GetName() const
Return a human-readable identifier for this fitter.
virtual nlohmann::json getParameters(const nlohmann::json &request)
Handle a "get_parameters" request.
virtual nlohmann::json getMCSpectrum(const nlohmann::json &request)
Handle a "get_mc_spectrum" request.
virtual nlohmann::json getParametersNames(const nlohmann::json &request)
Handle a "get_parameter_names" request.
virtual nlohmann::json setParameters(const nlohmann::json &request)
Handle a "set_parameters" request.
bool verbose
Flag controlling verbose logging of server operations.
bool add_prior_llh
If true, systematic prior likelihoods are added to the total LLH.
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...
constexpr static const double _BAD_DOUBLE_
Default value used for double initialisation.