// File: /workspace/github_cpp_makefile_star2_O2/spatialfruitsalad[P]volume2position/./selectedAreaHistogram.hpp
// Function: void selectedAreaHistogram::Process<unsigned short>(tomogram3d<unsigned short>&, std::vector<double, std::allocator<double>>&, std::vector<double, std::allocator<double>>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>, unsigned int)
void Process (tomogram3d<VALUE>& t, std::vector<double>& binvec, std::vector<double>& histvec, std::string foldername, unsigned int bins = 150)
    {
        std::cout << "start processing tomogram" << std::endl;

        unsigned int nx, ny, nz;
        nx = t.get_sx();
        ny = t.get_sy();
        nz = t.get_sz();

        std::vector<VALUE> greyvalues;
        long double mean = 0.0;
        unsigned int pixelsCounted = 0;
        // get all selected areas and create a vector of all grey values
        for (unsigned long k = 0; k != nz; ++k)
        for (unsigned long j = 0; j != ny; ++j)
        for (unsigned long i = 0; i != nx; ++i)
        {
            // check if the current voxel is in one of the areas
            for(auto it = m_areas.begin(); it != m_areas.end(); ++it)
            {
                if((*it).contains(i,j,k))
                {
                    pixelsCounted++;           
                    VALUE v = t.get(i,j,k);
                    greyvalues.push_back(v);
                    mean += v;
                }
            }
        }
        std::cout << "voxels counted: " << pixelsCounted << " which is a fraction of " << static_cast<double>(pixelsCounted)/static_cast<double>((nx*ny*nz)) << " of all voxels" << std::endl;
        mean /= static_cast<double>(pixelsCounted);
        std::cout << "mean value of: " << mean << std::endl;

        auto min =  (*std::min_element(greyvalues.begin(), greyvalues.end()));
        auto max =  (*std::max_element(greyvalues.begin(), greyvalues.end()));
        std::cout << "min: " << static_cast<int>(min) << std::endl << "max: " << static_cast<int>(max) << std::endl;

        auto diff = max - min;
        // create a histogram of all selected grey values
        long double binsize = static_cast<long double>(diff)/static_cast<long double>(bins);

        binvec.resize(bins + 1, 0);
        for (size_t i = 0; i != binvec.size(); ++i)
        {
           double gv = min + i * binsize;
           binvec.at(i) = gv;
        }
        histvec.resize(bins + 1, 0);
        for(auto n : greyvalues)
        {
            long double u = static_cast<long double>(n - min);
            long double v = static_cast<long double>(binsize);
            size_t idx = static_cast<size_t>(u/v);
            histvec.at(idx) += 1;
        }

        {
        std::cout << "write selected Area Histogram" << std::endl;
        std::ofstream out(foldername + "/selectedAreaHist.dat");
        for (size_t i = 0; i != histvec.size(); ++i)
        {
            long double gv = binvec.at(i);
            auto f = histvec.at(i);
            out << gv << " " << f << std::endl;      
        }
        out.close();
        }
    }

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f8, %rsp            # imm = 0x2F8
movq %r9, 0x70(%rsp)
movq %r8, 0x68(%rsp)
movq %rcx, 0x78(%rsp)
movq %rdx, 0x28(%rsp)
movq %rsi, %rbx
movq %rdi, %rbp
movq 0x11716(%rip), %rdi     # 0x17fc8
leaq 0xb39b(%rip), %rsi      # 0x11c54
callq 0x3260
movq %rax, %rdi
callq 0x3110
movq 0x18(%rbx), %rcx
movq 0x20(%rbx), %rax
movq %rcx, 0x88(%rsp)
movl %ecx, %ecx
movq %rcx, 0xa8(%rsp)
movq %rax, 0x90(%rsp)
movl %eax, %eax
movq %rax, 0xa0(%rsp)
movq %rbx, 0x58(%rsp)
movq 0x28(%rbx), %rax
movq %rax, 0x80(%rsp)
movl %eax, %eax
movq %rax, 0x98(%rsp)
xorpd %xmm0, %xmm0
leaq 0x40(%rsp), %rax
movapd %xmm0, (%rax)
andq $0x0, 0x10(%rax)
fldz
fstpt 0x18(%rsp)
xorl %r12d, %r12d
xorl %r13d, %r13d
cmpq 0x98(%rsp), %r12
je 0x69d9
xorl %r14d, %r14d
cmpq 0xa0(%rsp), %r14
je 0x69d1
xorl %r15d, %r15d
cmpq 0xa8(%rsp), %r15
je 0x69c9
movq (%rbp), %rbx
cmpq 0x8(%rbp), %rbx
je 0x69c4
movq %rbx, %rdi
movl %r15d, %esi
movl %r14d, %edx
movl %r12d, %ecx
callq 0xf912
testb %al, %al
je 0x69be
movq 0x58(%rsp), %rdi
movq %r15, %rsi
movq %r14, %rdx
movq %r12, %rcx
callq 0x70ba
movw %ax, 0xf8(%rsp)
leaq 0x40(%rsp), %rdi
leaq 0xf8(%rsp), %rsi
callq 0xf934
incl %r13d
movzwl 0xf8(%rsp), %eax
movl %eax, 0x3c(%rsp)
fldt 0x18(%rsp)
fiaddl 0x3c(%rsp)
fstpt 0x18(%rsp)
addq $0x14, %rbx
jmp 0x695b
incq %r15
jmp 0x694d
incq %r14
jmp 0x693c
incq %r12
jmp 0x692b
movq 0x115e8(%rip), %rdi     # 0x17fc8
leaq 0xb287(%rip), %rsi      # 0x11c6e
callq 0x3260
movl %r13d, %ebx
movq %rax, %rdi
movq %rbx, %rsi
callq 0x31a0
leaq 0xb27e(%rip), %rsi      # 0x11c7f
movq %rax, %rdi
callq 0x3260
cvtsi2sd %rbx, %xmm0
movq 0x90(%rsp), %rcx
imull 0x88(%rsp), %ecx
imull 0x80(%rsp), %ecx
cvtsi2sd %rcx, %xmm1
divsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x33a0
leaq 0xb25a(%rip), %rsi      # 0x11c98
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
callq 0x3110
movq 0x11573(%rip), %rdi     # 0x17fc8
leaq 0xb24b(%rip), %rsi      # 0x11ca7
callq 0x3260
movq %rbx, 0xd0(%rsp)
fildll 0xd0(%rsp)
fldt 0x18(%rsp)
fdivp %st, %st(1)
fstpt (%rsp)
movq %rax, %rdi
callq 0x3140
movq %rax, %rdi
callq 0x3110
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0xfa4b
movq 0x78(%rsp), %r12
movzwl (%rax), %ebp
movq 0x40(%rsp), %rdi
movq 0x48(%rsp), %rsi
callq 0xfa74
movzwl (%rax), %ebx
movq 0x1150f(%rip), %rdi     # 0x17fc8
leaq 0xb1f7(%rip), %rsi      # 0x11cb7
callq 0x3260
movq %rax, %rdi
movl %ebp, %esi
callq 0x3410
movq %rax, %rdi
callq 0x3110
leaq 0xb1df(%rip), %rsi      # 0x11cbd
movq %rax, %rdi
callq 0x3260
movq %rax, %rdi
movl %ebx, %esi
callq 0x3410
movq %rax, %rdi
callq 0x3110
subl %ebp, %ebx
movl %ebx, 0x38(%rsp)
movq 0x70(%rsp), %rsi
movl %esi, %eax
movq %rax, 0xc8(%rsp)
fildll 0xc8(%rsp)
fidivrl 0x38(%rsp)
fstpt 0x18(%rsp)
incl %esi
leaq 0xf8(%rsp), %rdx
andq $0x0, (%rdx)
movq 0x28(%rsp), %rdi
movq %rsi, %r15
callq 0xf954
movl %ebp, 0x34(%rsp)
xorl %ebx, %ebx
fildl 0x34(%rsp)
fstpt 0x58(%rsp)
leaq 0xa50c(%rip), %r14      # 0x11058
movq 0x28(%rsp), %rdi
movq 0x8(%rdi), %rax
subq (%rdi), %rax
sarq $0x3, %rax
cmpq %rax, %rbx
je 0x6b97
movq %rbx, %rsi
callq 0x6fb8
movq %rbx, 0xc0(%rsp)
xorl %ecx, %ecx
testq %rbx, %rbx
sets %cl
fildll 0xc0(%rsp)
fadds (%r14,%rcx,4)
fldt 0x18(%rsp)
fmulp %st, %st(1)
fldt 0x58(%rsp)
faddp %st, %st(1)
fstpl (%rax)
incq %rbx
jmp 0x6b4c
leaq 0xf8(%rsp), %rdx
andq $0x0, (%rdx)
movq %r12, %rdi
movq %r15, %rsi
callq 0xf954
movq 0x40(%rsp), %rbx
movq 0x48(%rsp), %r14
cmpq %r14, %rbx
je 0x6c32
movzwl (%rbx), %eax
subl %ebp, %eax
movl %eax, 0x30(%rsp)
fldt 0x18(%rsp)
fidivrl 0x30(%rsp)
flds 0xa56c(%rip)            # 0x11140
xorl %esi, %esi
fxch %st(1)
fucomi %st(1), %st
setae %sil
fldz
fcmovnb %st(2), %st
fstp %st(2)
fsubp %st, %st(1)
fnstcw 0x16(%rsp)
movzwl 0x16(%rsp), %eax
orl $0xc00, %eax            # imm = 0xC00
movw %ax, 0x26(%rsp)
fldcw 0x26(%rsp)
fistpll 0xb8(%rsp)
fldcw 0x16(%rsp)
shlq $0x3f, %rsi
xorq 0xb8(%rsp), %rsi
movq %r12, %rdi
callq 0x6fb8
movsd (%rax), %xmm0
addsd 0xa420(%rip), %xmm0     # 0x11048
movsd %xmm0, (%rax)
addq $0x2, %rbx
jmp 0x6bb8
movq 0x1138f(%rip), %rdi     # 0x17fc8
leaq 0xb083(%rip), %rsi      # 0x11cc3
callq 0x3260
movq %rax, %rdi
callq 0x3110
leaq 0xb08d(%rip), %rdx      # 0x11ce1
leaq 0xd8(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x4f34
movq 0x28(%rsp), %r13
leaq 0xf8(%rsp), %rdi
leaq 0xd8(%rsp), %rsi
pushq $0x10
popq %rdx
callq 0x3460
leaq 0xd8(%rsp), %rdi
callq 0x3488
xorl %ebx, %ebx
leaq 0xf8(%rsp), %r14
leaq 0xafb1(%rip), %r15      # 0x11c52
movq 0x8(%r12), %rax
subq (%r12), %rax
sarq $0x3, %rax
cmpq %rax, %rbx
je 0x6d27
movq %r13, %rdi
movq %rbx, %rsi
callq 0x6fb8
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
movq %r12, %rdi
movq %rbx, %rsi
callq 0x6fb8
movsd 0x18(%rsp), %xmm0
movsd %xmm0, 0xb0(%rsp)
fldl 0xb0(%rsp)
movsd (%rax), %xmm0
movsd %xmm0, 0x18(%rsp)
fstpt (%rsp)
movq %r14, %rdi
callq 0x3140
movq %rax, %rdi
movq %r15, %rsi
callq 0x3260
movq %rax, %rdi
movsd 0x18(%rsp), %xmm0
callq 0x33a0
movq %rax, %rdi
callq 0x3110
incq %rbx
jmp 0x6ca1
leaq 0xf8(%rsp), %rdi
callq 0x3330
leaq 0xf8(%rsp), %rdi
callq 0x3350
leaq 0x40(%rsp), %rdi
callq 0xa518
addq $0x2f8, %rsp            # imm = 0x2F8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x6d85
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x3488
jmp 0x6d9a
jmp 0x6d97
jmp 0x6d97
jmp 0x6d97
jmp 0x6d97
jmp 0x6d97
jmp 0x6d97
jmp 0x6d97
jmp 0x6d85
jmp 0x6d97
jmp 0x6d97
movq %rax, %rbx
leaq 0xf8(%rsp), %rdi
callq 0x3350
jmp 0x6d9a
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0xa518
movq %rbx, %rdi
callq 0x3420