Skip to content

Commit

Permalink
Merge pull request #19 from cl-fontana/master
Browse files Browse the repository at this point in the history
Adding the TSpectrum one-dimensional background estimation with SNIP algorithm
  • Loading branch information
wojdyr authored Sep 18, 2021
2 parents b45ba47 + 3a75228 commit f662747
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 138 deletions.
13 changes: 13 additions & 0 deletions doc/data.rst
Original file line number Diff line number Diff line change
Expand Up @@ -637,11 +637,24 @@ and functions:
Calculates Shirley background
(useful in X-ray photoelectron spectroscopy).

``snip_bg(@n, window_width, direction, filter_order, estimate_compton)``
Calculates the spectrum background applying a Sensitive Nonlinear Iterative Peak clipping algorithm (SNIP) (useful in gamma-ray spectroscopy).
This SNIP algorithm is taken from the `TSpectrum <https://root.cern.ch/doc/v608/classTSpectrum.html>`_ class of `ROOT <http://root.cern.ch>`_.
It requires the parameters:
1. ``window_width``: maximal width of clipping window;
2. ``direction``: direction of change of clipping window, if it is a positive number the window will be increasing;
3. ``filter_order``: order of clipping filter, possible values are 2, 4, 6 or 8;
4. ``estimate_compton``: if a positive number the algorithm will try to estimate the Compton edge.
Active points are modified according to the SNIP algorithm.
Inactive points are left as they are, assuming that they are already considered background.
Subtracting the calculated background from the initial spectrum gives zero in all inactive points.

Examples::

@+ = @0 # duplicate the dataset
@+ = @0 and @1 # create a new dataset from @0 and @1
@0 = @0 - shirley_bg(@0) # remove Shirley background
@0 = @0 - snip_bg(@0, 30, 1, 2, 0) # remove gamma background
@0 = @0 - @1 # subtract @1 from @0
@0 = @0 - 0.28*@1 # subtract scaled dataset @1 from @0

Expand Down
2 changes: 2 additions & 0 deletions fityk/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ libfityk_la_SOURCES = logic.cpp view.cpp lexer.cpp eparser.cpp cparser.cpp \
runner.cpp info.cpp common.cpp data.cpp var.cpp mgr.cpp \
tplate.cpp func.cpp udf.cpp bfunc.cpp f_fcjasym.cpp ast.cpp \
vm.cpp transform.cpp settings.cpp ui.cpp ui_api.cpp \
root/background.cpp \
luabridge.cpp GAfit.cpp LMfit.cpp guess.cpp NMfit.cpp \
model.cpp fit.cpp voigt.cpp numfuncs.cpp fityk.cpp \
\
logic.h view.h lexer.h eparser.h cparser.h \
runner.h info.h common.h data.h var.h mgr.h \
tplate.h func.h udf.h bfunc.h f_fcjasym.h ast.h \
vm.h transform.h settings.h ui.h luabridge.h \
root/background.hpp \
GAfit.h LMfit.h guess.h NMfit.h \
model.h fit.h voigt.h numfuncs.h \
swig/fityk_lua.cpp swig/luarun.h \
Expand Down
5 changes: 5 additions & 0 deletions fityk/eparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const char* function_name(int op)
case OP_DT_SUM_SAME_X: return "sum_same_x";
case OP_DT_AVG_SAME_X: return "avg_same_x";
case OP_DT_SHIRLEY_BG: return "shirley_bg";
case OP_DT_SNIP_BG: return "snip_bg";
// 2-args functions
case OP_MOD: return "mod";
case OP_MIN2: return "min2";
Expand Down Expand Up @@ -138,6 +139,8 @@ int get_function_narg(int op)
case OP_RANDNORM:
case OP_RANDU:
return 2;
case OP_DT_SNIP_BG:
return 5;
// Fityk functions
case OP_FUNC:
case OP_SUM_F:
Expand Down Expand Up @@ -819,6 +822,8 @@ void ExpressionParser::parse_expr(Lexer& lex, int default_ds,
put_function(OP_DT_AVG_SAME_X);
else if (mode == kDatasetTrMode && word == "shirley_bg")
put_function(OP_DT_SHIRLEY_BG);
else if (mode == kDatasetTrMode && word == "snip_bg")
put_function(OP_DT_SNIP_BG);

else
lex.throw_syntax_error("unknown function: " + word);
Expand Down
Loading

0 comments on commit f662747

Please sign in to comment.