Giter Site home page Giter Site logo

mprebinspectra.jl's Introduction

Example of how to use the MPRebinSpectra module.

A discrete 2D spectrum of electron energies (for $2\nu\beta\beta$ decay) is given as an ascii text file where:

column1 = E1, column2 = E2, column3 = multiplicity(dGdE)

The premise of MPRebinSpectra module is the use of segmential linear approximation of the discrete spectral points in order to transform the spectrum to a semi-continuous (subsequently continuous), normalized (to volume = 1) spectrum. Such spectrum can be saved as a new ascii file to use in further analysis (i.e. for sampling energies).

using MPRebinSpectra, StatsPlots, FHist, CSV, DataFrames, ColorSchemes
Base.displaysize() = (8, 80) # this is just to show "only" 5 rows of dataframes
gr()
Plots.GRBackend()
inFile = "spectrumG0.dat"
"spectrumG0.dat"

The MPRebinSpectra works with dataframes. The headers must be ["E1", "E2", "dGdE"] (at some point I plan to change this, so that headers could be user-defined.

Each column must contain only Float64 variables.

df_raw = CSV.File(inFile, delim = "    ", header = ["E1", "E2", "dGdE"]) |> DataFrame
df_raw.E1 = parse.(Float64, df_raw.E1)
df_raw.E2 = parse.(Float64, df_raw.E2);
p_raw = scatter(df_raw[1:1000:end, 1], df_raw[1:1000:end,2], df_raw[1:1000:end,3],
                zlims = (minimum(df_raw.dGdE), maximum(df_raw.dGdE)), legend = :false,
                xlabel = "E1 [MeV]", ylabel = "E2 [MeV]", zlabel = "dGdE", 
                title = "Raw spectrum, every 1000 pts", c = :blue, markeralpha = 1, ms = 2.5,
                markerstrokewidth = 0.2, camera = (55,45))
p_raw_side = scatter(df_raw[1:1000:end, 1], df_raw[1:1000:end,2], df_raw[1:1000:end,3],
                zlims = (minimum(df_raw.dGdE), 1.1*maximum(df_raw.dGdE)), legend = :false,
                 ylabel = "E2 [MeV]", zlabel = "dGdE", 
                title = "Projection", c = :blue, markeralpha = 0.6, ms = 4,
                markerstrokewidth = 0.2, camera = (90,0), zticks = :none)
plot(p_raw, p_raw_side, size = (900, 400), bottom_margin = 6Plots.mm, left_margin = 6Plots.mm)

svg


Rebinned dataframe contains columns:

E1 - defines constant E1 for which the linear approximation of the spectral points was made
minE - minimum energy E2 for the segment of the linear approximation 
maxE - maximum energy E2 for the segment of the linear approximation (E1 of next row is E2 of previous - continuity)
minG - dGdE value corresponding to minE 
maxG - dGdE value corresponding to maxE (this will probably be deleted in future)
a - approximation parameter from y = ax + b
b - approximation parameter from y = ax + b

The argument _prec of rebin2D(df, _prec, _dx) is the precision with which the approximation is to be made. For example _prec = 0.001 means that the approximation is made with an uncertainty of 1 promile. The argument _dx is the energy step of the spectrum (by default set to 0.001MeV).


df = rebin2D(df_raw, 0.001)

614,474 rows × 7 columns

E1minEmaxEminGmaxGab
Float64Float64Float64Float64Float64Float64Float64
10.000930.00.000930.01.34513e-471.44638e-440.0
20.000930.000930.014931.34513e-471.37867e-472.39571e-471.3429e-47
30.000930.014930.026931.37867e-471.40984e-472.5975e-471.33989e-47
40.000930.026930.040931.40984e-471.45085e-472.92929e-471.33095e-47
50.000930.040930.058931.45085e-471.50717e-473.12889e-471.32278e-47
60.000930.058930.109931.50717e-471.66716e-473.13706e-471.3223e-47
70.000930.109930.133931.66716e-471.73789e-472.94708e-471.34319e-47
80.000930.133930.156931.73789e-471.80181e-472.77913e-471.36568e-47

normalize2D!(df) normalizes the dataframe to the total volume of 1.


E1 = 0.10093
x = df_raw[df_raw.E1 .== E1 , 2]
y = df_raw[df_raw.E1 .== E1 , 3]
p_approx = scatter(x[1:50:end], y[1:50:end],
                    ylims = (0, 1.1*maximum(df_raw[df_raw.E1 .== E1 , 3])),
                    xlims = (0,3),
                    xlabel = "E2 [MeV]", ylabel = "dG/dE1dE2", title = "E1 = $E1 MeV; every 50 points",
                    label = "SpectrumG0 points"
                    )

for row in eachrow(df[df.E1 .== E1, :])
    plot!( [row.minE, row.maxE], 
           [get_line_point(row.minE, row.a, row.b), get_line_point(row.maxE, row.a, row.b)], 
           label = ifelse(row[:maxE] == 0.00093, "linear approximation", ""), 
           lw = 4, a=0.1, minorgrid =:true, framestyle = :box )
end
p_approx

png

df = normalize2D!(df)

614,474 rows × 7 columns

E1minEmaxEminGmaxGab
Float64Float64Float64Float64Float64Float64Float64
10.000930.00.000930.00.402019432.2780.0
20.000930.000930.014930.4020190.4120430.7160060.401353
30.000930.014930.026930.4120430.4213580.7763140.400452
40.000930.026930.040930.4213580.4336150.8754750.397782
50.000930.040930.058930.4336150.4504470.935130.39534
60.000930.058930.109930.4504470.4982640.9375720.395196
70.000930.109930.133930.4982640.5194030.8807940.401438
80.000930.133930.156930.5194030.5385060.8305980.408161

Last step is to add a column of CDF values - probabilities.

df = get_cdf!(df)

614,474 rows × 8 columns

E1minEmaxEminGmaxGabcdf
Float64Float64Float64Float64Float64Float64Float64Float64
10.000930.00.000930.00.402019432.2780.01.86939e-7
20.000930.000930.014930.4020190.4120430.7160060.4013535.88537e-6
30.000930.014930.026930.4120430.4213580.7763140.4004521.08858e-5
40.000930.026930.040930.4213580.4336150.8754750.3977821.68706e-5
50.000930.040930.058930.4336150.4504470.935130.395342.48272e-5
60.000930.058930.109930.4504470.4982640.9375720.3951964.90193e-5
70.000930.109930.133930.4982640.5194030.8807940.4014386.12313e-5
80.000930.133930.156930.5194030.5385060.8305980.4081617.33972e-5
cdfs = df[:, :cdf]

scatter(cdfs[1:10000:end], title= "Cumulative Distribution function CDF; every 1e3",
        xlabel = "energy segments (E1 + E2)", ylabel = "CDF", minorgrid = :true, 
        xlims=(0, 60), ylims = (0, 1.1), legend = :false, framestyle = :box)

svg

Approximated spectrum shown as lines build out of linear segments. Each line represents one constant E1

c = palette(:thermal)

p = plot_lines(50, df, c)
p = plot!(ylims = (minimum(df.minG), 1.1*maximum(df.maxG)), ylabel = "dG/dE1dE2")

svg

Output can be saved as a csv file which can be read off into new dataframe with ease

CSV.write("spectrumG0_Rebinned_prec0001.csv", df)
"spectrumG0_Rebinned_prec0001.csv"

mprebinspectra.jl's People

Contributors

shoram444 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.