Verified Commit 75aa3858 authored by Tobias WEBER's avatar Tobias WEBER
Browse files

scanbrowser: started with fitting

parent 3fe70754
......@@ -4,9 +4,10 @@
# @license see 'LICENSE' file
#
cmake_minimum_required(VERSION 3.0)
project(scanbrowser)
cmake_minimum_required(VERSION 3.0)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}" "${PROJECT_SOURCE_DIR}/../../../tlibs2/cmake")
message("Build type: ${CMAKE_BUILD_TYPE}")
......@@ -33,6 +34,11 @@ find_package(Qt5Widgets REQUIRED)
find_package(Qt5PrintSupport REQUIRED)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
# Minuit
find_package(Minuit2 REQUIRED)
find_package(Threads REQUIRED)
find_package(OpenMP REQUIRED)
# -----------------------------------------------------------------------------
......@@ -96,5 +102,6 @@ add_executable(scanbrowser
target_link_libraries(scanbrowser
${Boost_LIBRARIES}
Qt5::Core Qt5::Gui Qt5::Widgets Qt5::PrintSupport
${Minuit2_LIBRARIES} Threads::Threads OpenMP::OpenMP_CXX
-ldl
)
......@@ -30,6 +30,8 @@ real {uinteger}(\.[0-9]*)?|\.[0-9]+
")" { return yy::CliParser::make_TOK_BRACKET_CLOSE(); }
"[" { return yy::CliParser::make_TOK_SQBRACKET_OPEN(); }
"]" { return yy::CliParser::make_TOK_SQBRACKET_CLOSE(); }
"{" { return yy::CliParser::make_TOK_CURLBRACKET_OPEN(); }
"}" { return yy::CliParser::make_TOK_CURLBRACKET_CLOSE(); }
"+" { return yy::CliParser::make_TOK_PLUS(); }
"-" { return yy::CliParser::make_TOK_MINUS(); }
"*" { return yy::CliParser::make_TOK_MULT(); }
......
......@@ -112,6 +112,7 @@ enum class SymbolType
STRING, // e.g. "abc"
LIST, // e.g. 1, 2, 3
ARRAY, // e.g. [1, 2, 3]
MAP, // e.g. {"key" : 1.23}
DATASET
};
......
......@@ -22,6 +22,7 @@
%token<std::string> TOK_STRING TOK_IDENT
%token TOK_BRACKET_OPEN TOK_BRACKET_CLOSE
%token TOK_SQBRACKET_OPEN TOK_SQBRACKET_CLOSE
%token TOK_CURLBRACKET_OPEN TOK_CURLBRACKET_CLOSE
%token TOK_PLUS TOK_MINUS TOK_MULT TOK_DIV TOK_MOD TOK_POW
%token TOK_ASSIGN
%token TOK_MEMBER_ACCESS
......@@ -43,7 +44,7 @@
%right PREC_UNARY_PLUSMINUS
%nonassoc TOK_POW
%nonassoc TOK_MEMBER_ACCESS
%left TOK_SQBRACKET_OPEN
%left TOK_SQBRACKET_OPEN TOK_CURLBRACKET_OPEN
%%
......
......@@ -8,6 +8,7 @@
#include "funcs.h"
#include "../globals.h"
#include "libs/phys.h"
#include "libs/fit.h"
using t_real = t_real_cli;
......@@ -606,6 +607,77 @@ std::shared_ptr<Symbol> func_normtomon(CliParserContext & ctx, std::shared_ptr<S
const auto &dat = dynamic_cast<const SymbolDataset&>(*arg);
return std::make_shared<SymbolDataset>(dat.GetValue().norm(0));
}
/**
* fit function
*/
std::shared_ptr<Symbol> func_fit (CliParserContext & ctx, const std::vector<std::shared_ptr<Symbol>>& args)
{
if(args.size() < 2)
{
ctx.PrintError("Insufficient number of arguments given. At least a dataset and a fit function is needed.");
return nullptr;
}
// TODO: multiple data sets
if(args[0]->GetType() != SymbolType::DATASET)
{
ctx.PrintError("The first argument has to be a data set or a list of data sets.");
return nullptr;
}
if(args[1]->GetType() != SymbolType::STRING)
{
ctx.PrintError("The second argument has to be a string giving the fit expression.");
return nullptr;
}
const auto& dataset = dynamic_cast<SymbolDataset&>(*args[0]).GetValue();
const std::string& strexpr = dynamic_cast<SymbolString&>(*args[1]).GetValue();
std::vector<std::string> vars;
std::vector<t_real> initials;
std::vector<t_real> errs;
std::vector<bool> fixed;
// if no variables are given, determine them
if(vars.size() == 0)
{
tl2::ExprParser<t_real> expr;
expr.parse(strexpr);
const auto& exprvars = expr.get_vars();
for(const auto& pair : exprvars)
{
if(pair.first == "x")
continue;
vars.push_back(pair.first);
}
}
initials.resize(vars.size(), 0);
errs.resize(vars.size(), 0);
fixed.resize(vars.size(), false);
// fit all channels in the data set
for(std::size_t channel=0; channel<dataset.GetNumChannels(); ++channel)
{
const auto& dat = dataset.GetChannel(channel);
const auto& xvals = dat.GetAxis(0);
const auto& yvals = dat.GetCounter(0);
const auto& yerrs = dat.GetCounterErrors(0);
tl2::fit_expr(strexpr, xvals, yvals, yerrs, "x", vars, initials, errs, &fixed);
}
return nullptr;
}
// ----------------------------------------------------------------------------
......@@ -746,6 +818,7 @@ std::unordered_map<std::string, std::tuple<std::shared_ptr<Symbol>(*)
std::make_pair("append", std::make_tuple(&func_append, "appends two or more data sets")),
std::make_pair("append_channels", std::make_tuple(&func_append_channels, "appends two or more data set as individual channels")),
std::make_pair("add_pointwise", std::make_tuple(&func_add_pointwise, "pointwise addition of two or more data sets")),
std::make_pair("fit", std::make_tuple(&func_fit, "fitting of one or more data sets")),
};
......
/**
* Evaluates symbols
* evaluates symbols
* @author Tobias Weber <tweber@ill.fr>
* @date 15-Jun-2018
* @license see 'LICENSE' file
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment