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";
122 for (
size_t ipdf=0; ipdf<
samples.size(); ipdf++) {
125 if(!base_casted_sample) {
126 MACH3LOG_ERROR(
"Sample object does not derive from SampleHandlerBase. Consider overloading {} for this sample type.", __func__);
129 for (
int iSample = 0; iSample <
samples[ipdf]->GetNSamples(); ++iSample) {
130 base_casted_sample->AddData(iSample, base_casted_sample->GetMCArray(iSample));
137 for (
int p = 0; p < npars; ++p) {
138 std::string param_name =
systematics[s]->GetParFancyName(p);
139 double param_value =
systematics[s]->GetParCurr(p);
141 if (
verbose)
MACH3LOG_INFO(
"Setting prefit param {} to current value {}", param_name, param_value);
145 nlohmann::json response;
146 response[
"status"] =
"success";
150 void TH1toResponse(
const TH1* mc_hist,
const std::string& sample_title, nlohmann::json &response) {
152 auto hist1d =
dynamic_cast<const TH1D*
>(mc_hist);
157 const TAxis *ax = mc_hist->GetXaxis();
158 std::string xtitle = ax->GetTitle();
159 int nxbins = ax->GetNbins();
160 std::vector<double> xbins(nxbins+1);
161 std::vector<double> binvals(nxbins);
163 xbins[0] = ax->GetBinLowEdge(1);
164 for (
int ix = 1; ix <= nxbins; ++ix) {
165 xbins[ix] = ax->GetBinUpEdge(ix);
166 binvals[ix-1] = mc_hist->GetBinContent(ix);
168 response[
"axis_titles"][sample_title] = {xtitle};
169 response[
"bin_edges"][sample_title] = {xbins};
170 response[
"bin_values"][sample_title] = binvals;
174 void TH2toResponse(
const TH2* mc_hist,
const std::string& sample_title, nlohmann::json &response) {
176 auto hist2d =
dynamic_cast<const TH2D*
>(mc_hist);
181 const TAxis *x_axis = mc_hist->GetXaxis();
182 std::string xtitle = x_axis->GetTitle();
183 int nxbins = x_axis->GetNbins();
184 std::vector<double> xbins(nxbins+1);
186 const TAxis *y_axis = mc_hist->GetYaxis();
187 std::string ytitle = y_axis->GetTitle();
188 int nybins = y_axis->GetNbins();
189 std::vector<double> ybins(nybins+1);
191 std::vector<std::vector<double>> binvals(nxbins, std::vector<double>(nybins));
193 xbins[0] = x_axis->GetBinLowEdge(1);
194 ybins[0] = y_axis->GetBinLowEdge(1);
195 for (
int ix = 1; ix <= nxbins; ++ix) {
196 for (
int iy = 1; iy <= nybins; ++iy) {
197 xbins[ix] = x_axis->GetBinUpEdge(ix);
198 ybins[iy] = y_axis->GetBinUpEdge(iy);
199 binvals[ix-1][iy-1] = mc_hist->GetBinContent(ix, iy);
202 response[
"axis_titles"][sample_title] = {xtitle, ytitle};
203 response[
"bin_edges"][sample_title] = {xbins, ybins};
204 response[
"bin_values"][sample_title] = binvals;
212 nlohmann::json response;
214 std::vector<std::string> sample_titles = {};
216 for (
size_t ipdf=0; ipdf<
samples.size(); ipdf++) {
218 if(!base_casted_sample) {
219 MACH3LOG_ERROR(
"Sample object does not derive from SampleHandlerBase. Consider overloading {} for this sample type.", __func__);
222 for (
int iSample = 0; iSample <
samples[ipdf]->GetNSamples(); ++iSample) {
223 std::string sample_title =
samples[ipdf]->GetSampleTitle(iSample);
224 sample_titles.push_back(sample_title);
226 int dimension =
samples[ipdf]->GetNDim(iSample);
227 response[
"dimensions"][sample_title] = dimension;
229 if (dimension == 1) {
231 }
else if (dimension == 2) {
232 TH2toResponse(
static_cast<const TH2*
>(
samples[ipdf]->GetMCHist(iSample)), sample_title, response);
239 response[
"sample_names"] = sample_titles;
248 nlohmann::json response;
250 std::vector<std::string> sample_titles = {};
252 for (
size_t ipdf=0; ipdf<
samples.size(); ipdf++) {
254 if(!base_casted_sample) {
255 MACH3LOG_ERROR(
"Sample object does not derive from SampleHandlerBase. Consider overloading {} for this sample type.", __func__);
258 for (
int iSample = 0; iSample <
samples[ipdf]->GetNSamples(); ++iSample) {
259 std::string sample_title =
samples[ipdf]->GetSampleTitle(iSample);
260 sample_titles.push_back(sample_title);
262 int dimension =
samples[ipdf]->GetNDim(iSample);
263 response[
"dimensions"][sample_title] = dimension;
265 if (dimension == 1) {
267 }
else if (dimension == 2) {
268 TH2toResponse(
static_cast<const TH2*
>(
samples[ipdf]->GetDataHist(iSample)), sample_title, response);
275 response[
"sample_names"] = sample_titles;
284 std::vector<std::string> syst_par_names;
288 for (
int p = 0; p < npars; ++p) {
289 std::string param_name =
systematics[s]->GetParFancyName(p);
292 syst_par_names.push_back(param_name);
296 nlohmann::json response;
297 response[
"sys_pars"] = syst_par_names;
305 std::map<std::string, double> osc_params;
306 std::map<std::string, double> syst_params;
310 for (
int p = 0; p < npars; ++p) {
311 std::string param_name =
systematics[s]->GetParFancyName(p);
312 double param_value =
systematics[s]->GetParProp(p);
315 osc_params[param_name] = param_value;
318 syst_params[param_name] = param_value;
325 nlohmann::json response;
326 response[
"sys_pars"] = syst_params;
327 response[
"osc_pars"] = osc_params;
336 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=1)
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.