From c700c384ef188afdde24f92e2791025733e7f487 Mon Sep 17 00:00:00 2001 From: Eric van Genderen Date: Mon, 21 Jan 2019 10:47:20 +0100 Subject: [PATCH] WOOP WOOP --- AverageTest2_imp.m | 119 ++++++++++++++++++------------------- AverageTest_poly3.m | 132 +++++++++++++++++++++++++++++++++++++++++ README.md | 8 +-- Tryout3.m | 113 +++++++++++++++++++++++++---------- Tryout4.m | 65 ++++++++++++++++++++ Variables.m | 37 ++++++++---- WriteIO.m | 6 +- createFit_poly3.m | 39 ++++++++++++ normaliseCurve.m | 141 +++++++++++++++++++++++++++++++++----------- totFlatMain.m | 85 ++++++++++++++++++-------- 10 files changed, 569 insertions(+), 176 deletions(-) create mode 100644 AverageTest_poly3.m create mode 100644 Tryout4.m create mode 100644 createFit_poly3.m diff --git a/AverageTest2_imp.m b/AverageTest2_imp.m index 98ce335..64a64b7 100644 --- a/AverageTest2_imp.m +++ b/AverageTest2_imp.m @@ -10,24 +10,15 @@ end x = M; -ToT_FF_T_BIG2(:,3,118983)=0; % bad pixel one intercept -ToT_FF_T_BIG2(1,2,118983)=0; -ToT_FF_T_BIG2(:,3,121208)=0; % bad pixel two intercept -ToT_FF_T_BIG2(1,2,121208)=0; +%% Needs to be checked and implemntated -%% Needs to be checked and implemntated -%k = 0 -% while k < BadP -% k = k +1 -% ToT_FF_T_BIG2(:,3,BadP(k))=0; -% ToT_FF_T_BIG2(1,2,BadP(k))=0; -% end +"calc_means" meanAllCounts = mean(ToT_FF_T_BIG2(20,2,:)); medianAllCounts = median(ToT_FF_T_BIG2(20,2,:)); -stdAllCounts = std(ToT_FF_T_BIG2(20,2,:)); +stdAllCounts = std(ToT_FF_T_BIG2(20,2,:)); stdAllCountsF = 0.05 * stdAllCounts; % 0.15 = 1/3th? @@ -38,21 +29,23 @@ upperBm = medianAllCounts + stdAllCountsF; % make sure only those with certain amount of counts (MAYBE make it at % least 50% of average??? - idx = ToT_FF_T_BIG2(20,2,:) upperBm; -% idx = ToT_FF_T_BIG2(20,2,:)<10000; - counter = ToT_FF_size - sum(idx(:)); +idx = ToT_FF_T_BIG2(20,2,:) upperBm; +% idx = ToT_FF_T_BIG2(20,2,:)<10000; +counter = ToT_FF_size - sum(idx(:)); %% + +"apply means" k = 1; l = 1; TEST = 0; while k < 1025 - while l < ToT_FF_size + 1 - if idx(1,1,l) == 0 + while l < ToT_FF_size + 1 + if idx(1,1,l) == 0 TEST = TEST + ToT_FF_T_BIG2(k,3,l); if k == 1023 if TEST == 1 - l - break + l + break end end if isnan(TEST)==1 @@ -63,59 +56,59 @@ end l = l + 1; end - AverageTest2(k,1) = TEST; + AverageTest2(k,1) = TEST; TEST = 0; l = 1; k = k +1; end %% - AverageTest2(:,2) = AverageTest2(:,1)./ (counter); - - xsize = 1024; - %% - p = 1; - while p < xsize + 1 - if p == 1 - AverageTest2(p,3) = AverageTest2(p,2); - - else - P = p - 1; - AverageTest2(p,3) = AverageTest2(p,2)+AverageTest2(P,3); - end - - p = p + 1; - end - - %% - - AverageTest2(:,3) = AverageTest2(:,3); - maxy = max(AverageTest2(:,3)); - AverageTest2(:,3) = AverageTest2(:,3) - - AverageTest2(:,4) = AverageTest2(:,3)./max(AverageTest2(:,3)); - - %% get curves +AverageTest2(:,2) = AverageTest2(:,1)./ (counter); - %stepAvg = 10; % step for curve fitting - Avvan = fromToT; %start point - ij = 0; %counter - Avtot = Avvan + stepAvg; %end point for each step - intercept = 0; - - while intercept < 1 - ij = ij + 1; - Try = createFit(M,AverageTest2(:,4), Avvan, Avtot); %calls funtion createFit, - % which tries to fit a polynome to the 3th order to the Average - % Curve for each step - AverageCurve(ij,1:4) = [Avvan,Avtot,AverageTest2(Avvan,4),AverageTest2(Avtot,4)]; - coeffy = coeffvalues(Try); % places the polynomal corves in coeffy - AverageCurve(ij,5:7) = coeffy; % places the coeffy information in the AverageCurve Table - intercept = coeffy(1,3); %creates the exit for the while loop. When the fitting curve nears 1 - Avvan = Avtot; % cretes a new step start - Avtot = Avvan + stepAvg; % creates a new step stop +xsize = 1024; +%% +p = 1; +while p < xsize + 1 + if p == 1 + AverageTest2(p,3) = AverageTest2(p,2); + else + P = p - 1; + AverageTest2(p,3) = AverageTest2(p,2)+AverageTest2(P,3); end + p = p + 1; +end + +%% + +AverageTest2(:,3) = AverageTest2(:,3); +maxy = max(AverageTest2(:,3)); +AverageTest2(:,3) = AverageTest2(:,3) + +AverageTest2(:,4) = AverageTest2(:,3)./max(AverageTest2(:,3)); + +%% get curves + +%stepAvg = 10; % step for curve fitting +Avvan = fromToT; %start point +ij = 0; %counter +Avtot = Avvan + stepAvg; %end point for each step +intercept = 0; + +while intercept < 0.999999 + ij = ij + 1 + Try = createFit(M,AverageTest2(:,4), Avvan, Avtot); %calls funtion createFit, + % which tries to fit a polynome to the 3th order to the Average + % Curve for each step + AverageCurve(ij,1:4) = [Avvan,Avtot,AverageTest2(Avvan,4),AverageTest2(Avtot,4)]; + coeffy = coeffvalues(Try); % places the polynomal corves in coeffy + AverageCurve(ij,5:7) = coeffy; % places the coeffy information in the AverageCurve Table + intercept = coeffy(1,3); %creates the exit for the while loop. When the fitting curve nears 1 + Avvan = Avtot; % cretes a new step start + Avtot = Avvan + stepAvg; % creates a new step stop +end + + %% clearvars xsize x A k l p P strength TEST x p1 p2 p3 maxy coeffy ij lowerB lowerBm upperB upperBm meanAllCounts stdAllCounts stdAllCountsF medianAllCounts diff --git a/AverageTest_poly3.m b/AverageTest_poly3.m new file mode 100644 index 0000000..59fa62b --- /dev/null +++ b/AverageTest_poly3.m @@ -0,0 +1,132 @@ + +AverageTest2 = zeros(1024,3); +l = 1; +k = 1; +o = 1; + +%% Test code +%ToT_FF_size = (4 * 256 * 256); +% creates a M = 1024 by 1 matrix +% while o < 1024+1 +% M(o,1) = o; +% o = o + 1; +% end +% x = M; + + +%% Needs to be checked and implemntated +k = 0; +if BadP > 0 + while k < BadP + k = k +1 + ToT_FF_T_BIG2(:,3,BadP(k))=0; % removes only the info for the Avg curve + ToT_FF_T_BIG2(1,2,BadP(k))=0; % sets the number of counts of the pixel to 0 + end +end + +meanAllCounts = mean(ToT_FF_T_BIG2(20,2,:)); +medianAllCounts = median(ToT_FF_T_BIG2(20,2,:)); +stdAllCounts = std(ToT_FF_T_BIG2(20,2,:)); + +stdAllCountsF = outlierRemovalStrength * stdAllCounts; % 0.15 = 1/3th? + +lowerB = meanAllCounts - stdAllCountsF; +upperB = meanAllCounts + stdAllCountsF; +lowerBm = medianAllCounts - stdAllCountsF; +upperBm = medianAllCounts + stdAllCountsF; +% make sure only those with certain amount of counts (MAYBE make it at +% least 50% of average??? +idx = ToT_FF_T_BIG2(20,2,:) upperBm; +% idx = ToT_FF_T_BIG2(20,2,:)<10000; +counter = ToT_FF_size - sum(idx(:)); + +if counter < ToT_FF_size/2 + %dispPerc = uint16((counter/ToT_FF_size)*100) + text = strcat('The ammount of pixels used to calculate the average is les then 50% reconsider the outlier strength, Press a key !') + disp(text) + pause +end + +%% +k = 1; +l = 1; +TEST = 0; +while k < 1024 + while l < ToT_FF_size + 1 + if idx(1,1,l) == 0 + TEST = TEST + ToT_FF_T_BIG2(k,3,l); + end + l = l + 1; + end + AverageTest2(k,1) = TEST; + TEST = 0; + l = 1; + k = k +1; +end +%% Test case to smooth the AverageCurve to fix the jumping +AverageTest2(:,2) = movmean(AverageTest2(:,1)./ (counter), strength); + + + +%% old +%AverageTest2(:,5) = AverageTest2(:,1)./ (counter); +%xsize = 1024; +%% +p = 1; +while p < 1024%xsize + 1 + if p == 1 + AverageTest2(p,3) = AverageTest2(p,2); + + else + P = p - 1; + AverageTest2(p,3) = AverageTest2(p,2)+AverageTest2(P,3); + end + + p = p + 1; +end + +%% + +AverageTest2(:,3) = AverageTest2(:,3); +maxy = max(AverageTest2(:,3)); +AverageTest2(:,3) = AverageTest2(:,3); + +AverageTest2(:,4) = AverageTest2(:,3)./max(AverageTest2(:,3)); + + +%% get curves v2.0 +O = zeros(1024,1); + +% Still need to fix the 0 line + +%stepAvg = 10; % step for curve fitting +Avvan = fromToT; %start point +ij = 0; %counter +Avtot = Avvan + stepAvg; %end point for each step +intercept = 0; +% ZeroFixer = zeros(1024,1); +% M2 = zeros(1024,1); +% M2(2:1024,1) = M(1:1023,1) +% ZeroFixer(2:1024,1) = AverageTest2(1:1023,4) + + +while intercept < 0.99999 + ij = ij + 1; + O(Avvan:Avtot,1) = 1; + Try = createFit_poly3(M,AverageTest2(:,4), O); %calls funtion createFit, + % which tries to fit a polynome to the 3th order to the Average + % Curve for each step + %AverageCurve(ij,1:4) = [Avvan,Avtot,ZeroFixer(Avvan,1),ZeroFixer(Avtot,1)]; + AverageCurve(ij,1:4) = [Avvan,Avtot,AverageTest2(Avvan,4),AverageTest2(Avtot,4)]; + coeffy = coeffvalues(Try); % places the polynomal corves in coeffy + AverageCurve(ij,5:8) = coeffy; % places the coeffy information in the AverageCurve Table + %intercept = coeffy(1,4) %creates the exit for the while loop. When the fitting curve nears 1 + intercept = (AverageCurve(ij,3)); + O(Avvan:Avtot,1) = 0; + Avvan = Avtot; % cretes a new step start + Avtot = Avvan + stepAvg; % creates a new step stop +end +Averagecurve_succesfull = 'done' +%% +clearvars xsize x A k l p P strength TEST x p1 p2 p3 maxy coeffy ij lowerB lowerBm upperB upperBm meanAllCounts stdAllCounts stdAllCountsF medianAllCounts +clearvars ans Averagecure_Succesfull AverageTest_Finished Avtit Avvan count counter dispPerc idx intercept o O outing stepAvg text totalPixelcount \ No newline at end of file diff --git a/README.md b/README.md index 9cf399b..769ba39 100644 --- a/README.md +++ b/README.md @@ -1,15 +1,9 @@ ## ToTCorrection -Any file can be run which has a tpx3 header - -Please run first: Added a Variables file. Please set all variables there. -One can run from: +load output ToT hit_files with .h5 table -- totFlatMain.m from hitparser files -- normalise curve if you preload a saved ToT_FF_T_BIG2 file -* remember to start a WriteIO in the later case diff --git a/Tryout3.m b/Tryout3.m index 2f041bb..006f410 100644 --- a/Tryout3.m +++ b/Tryout3.m @@ -1,57 +1,108 @@ F = zeros(1024,4); starters = uint32(1); Gx = M; -l = 1; +l = 1; +syms x + %% to find the nearest point on teh averagecurve i = 0; while i < 1024 i = i + 1; - D(i,2) = find(AverageCurve(:,3) <= D(i,1), 1, 'last' ) ; - Curve = D(i,2); + if D(i,1) < AverageCurve(1,3) + D(i,2) = 1; + else + D(i,2) = find(AverageCurve(:,3) <= D(i,1), 1, 'last' ) ; + + end end %% calculates the expectedToT for every ToT % expectedToT = xavg = (sqrt(4*a*(ypixel - c) + b^2) + b) / (2 * a) -i = 0; +i = fromToT; +if UsePoly == 2 + while i < 1024 + i = i + 1; + Curve = D(i,2); + p1 = AverageCurve(Curve,5); + p2 = AverageCurve(Curve,6); + p3 = AverageCurve(Curve,7); + expectedToT = (sqrt(4*p1*(D(i,1)-p3) + p2^2) - p2) / (2 * p1); + F(i,1) = expectedToT; + end + +elseif UsePoly == 3 + memoryToT = 0; + while i < 1024 + % i = i + 1; + % Curve = D(i,2); + % a = AverageCurve(Curve,5); + % b = AverageCurve(Curve,6); + % c = AverageCurve(Curve,7); + % d = AverageCurve(Curve,8); % v2 + % y = D(i,1); + % + % expectedToT = (sqrt((-27*d*a^2 + 27*y*a^2 + 9*a*b*c - 2*b^3)^2 + 4*(3*a*c - b^2)^3) - 27*d*a^2 + 27*a^2*y + 9*a*b*c - 2*b^3)^(1/3)/(3*a*2^(1/3)) - (2^(1/3)*(3*a*c - b^2))/(3*a*(sqrt((-27*d*a^2 + 27*y*a^2 + 9*a*b*c - 2*b^3)^2 + 4*(3*a*c - b^2)^3) - 27*d*a^2 + 27*y* a^2 + 9*a*b*c - 2*b^3)^(1/3)) - b/(3*a); + % F(i,1) = expectedToT; + i = i + 1; + Curve = D(i,2); + + %myfun = @(x,a,b,c,d,findy) a*x^3 + b*x^2 + c*x + d-findy; + + a = AverageCurve(Curve,5); + b = AverageCurve(Curve,6); + c = AverageCurve(Curve,7); + d = AverageCurve(Curve,8); % v2 + y = D(i,1); +% tic +% KT = double(vpasolve(a*x^3 + b*x^2 + c*x + d-y, [memoryToT memoryToT+20])); +% toc -while i < 1024 - i = i + 1; - Curve = D(i,2); - p1 = AverageCurve(Curve,5); - p2 = AverageCurve(Curve,6); - p3 = AverageCurve(Curve,7); - expectedToT = (sqrt(4*p1*(D(i,1)-p3) + p2^2) - p2) / (2 * p1); - F(i,1) = expectedToT; +%%%% + %fun = @(x) myfun(x,a,b,c,d,findy); + %x0 = memoryToT + %KT = fzero(fun,x0) + + NT = roots([a b c d-y]); + KT = NT(imag(NT)==0); + KT = sort(KT); + + + + %%%% + ji = 0; + if length(KT) == 1 + expectedToT = KT(1); + else + while ji < length(KT) + ji = ji + 1; + Dinky = KT(ji); + if Dinky >= memoryToT + expectedToT = KT(ji); + break + else + i; + + end + end + end + + F(i,1) = expectedToT; + memoryToT = expectedToT; + end +else + error('there was no polynome use set') end %% calculate differencemap -% fdx = F(starters:end,1)-M(starters:end,1) < -1 * M(starters:end,1) + 1; -% -% if sum(fdx) > 0 -% l = starters; -% -% while l < 1024 -% if F(l,1)-M(l,1) < -1 * M(l,1) + 1 -% trick = 0; -% trick = F(l,1)-M(l,1); -% trick = trick + 1; -% F(l,4) = trick; -% else -% F(l,4) = F(l,3)-M(l,1); -% end -% l = l + 1; -% end -% counter = counter + 1 -% else F(starters:end,2) = F(starters:end,1)-M(starters:end,1); % Calculate the difference F(starters:end,3) = round(F(starters:end,2)); % Make integers ToT_FF_T_BIG3(starters:end,3,o) = F(starters:end,3); % places the Differencemap in possition 3 -% end + diff --git a/Tryout4.m b/Tryout4.m new file mode 100644 index 0000000..720342a --- /dev/null +++ b/Tryout4.m @@ -0,0 +1,65 @@ +%% TRYOUT 4 + +F = zeros(1024,4); +starters = uint32(1); + + +%% creates waitbar + +%h = waitbar(0,'Please wait...'); +h = waitbar(0, 'Please Wait'); +set(findall(h), 'Units', 'Normalized') +set(h, 'Units', 'Pixels', 'Position', [100 100 500 100]) +titleHandle = get(findobj(h,'Type','axes'),'Title'); +set(titleHandle,'FontSize',18) + +KATY = ToT_FF_T_BIG2(1,2,:) > minimumCounts; +steps = sum(KATY); + +s = clock; +tic +st = 0; + +for ik = 1:ToT_FF_size + % F(:,1) = ToT_FF_T_BIG2(:,5,ik) + + if ToT_FF_T_BIG2(1,2,ik) > minimumCounts + F = zeros(1024,4); + + + + for ikke = 1:1024 + N = AverageTable(3,find(AverageTable(1,:) <= ToT_FF_T_BIG2(ikke,5,ik), 1, 'last' )); + F(ikke,1) = N; + end + % waitbar info + st = st + 1; + if st == 1 + is = etime(clock,s); + esttime = is * steps/1; + + end + if st == 100 + is = etime(clock,s); + esttime = is * steps/100; + + end + if st == 1000 + is = etime(clock,s); + esttime = is * steps/1000; + + end + + % this is the core of the corrcetion + F(starters:end,2) = F(starters:end,1)-M(starters:end,1); % Calculate the difference + F(starters:end,3) = round(F(starters:end,2)); % Make integers + ToT_FF_T_BIG3(starters:end,3,ik) = F(starters:end,3); % places the Differencemap in possition 3 + + % waitbar info + NUMERO = num2str(ik); + h = waitbar(ik/steps, h, ['remaining time = ',num2str((esttime-etime(clock,s))/60,'%4.0f'),' min #', NUMERO, ' ' num2str((ik/ToT_FF_size)*100,'%4.1f'),'%'],'WindowStyle','modal'); + + end +end + +close(h) \ No newline at end of file diff --git a/Variables.m b/Variables.m index 48fa4bc..eeb6243 100644 --- a/Variables.m +++ b/Variables.m @@ -1,33 +1,46 @@ %% Variables file! %tester +new = 1 % means it runs the newest fitting version; Suggested KEEP 1) %% load file %Filename (witout numbering, needs underscore after file name before numbering) -MainFILE = 'TOA-TOT-norm_145_50THL_20s_hits' % ('TOA-TOT-norm_145_50THL_20s_hits') +MainFILE = 'ikkrum20_freq_tot' % ('TOA-TOT-norm_145_50THL_20s_hits') %Directory -UserDIR = '/Users/ericva/Desktop/TikTok/FF50_out/' +UserDIR = '/Users/ericva/Downloads/' FileTYPE = '.h5' % (.h5) -NumberingFormat = '000000' % ('000000') +NumberingFormat = '00000' % ('000000') +NumberofFiles = 1 -%% Preperation + +%% Preperation' %smoothing spline -strength = 13 %smoothing of curves over number of pixels (Not being used anymore) +strength = 5 %smoothing of curves over number of pixels for the Average curve. %very important removes ToT' of all datasets from being processed for %correction (13) -fromToT = 6 %start ToT %used to clear very small toT values (6) +fromToT = 3 %start ToT %used to clear very small toT values (6) toToT = 1024 % end ToT % could be used to set a final correction point (1024) +outlierRemovalStrength = 0.05 + +minimumCounts = 30000 %minimum counts for a pixel to be considered for a correction + %% bad pixels -BadP = 2 %(number of bad pixels) (10) -BadP_T = [118983 121208] % add bad pixels +BadP = 0 %2%(number of bad pixels) (10) +BadP_T = []%[118983 121208] % add bad pixels %% For Average curve -stepAvg = 10 % Curve fitting bandstep (10) -%Llp = 40 % Lower limit percentage (40) -%Ulp = 90 % upper limit percentage (90) +stepAvg = 3 % Curve fitting bandstep (3) can fail at other settings +UsePoly = 3 % polynomal curve factor (poly 2 or 3 supported) poly 2 is considered to be inferior + + +%% Chip information +Dim_x = 256 +Dim_y = 256 +Chipnr = 4 + %% Save Location and file name -resultfile = '/Users/ericva/Desktop/Pinky/tot_correct_300kV_050.h5' % full save location +resultfile = '/Users/ericva/Desktop/Maastricht/300um_Maastricht_Ikrum20.h5' % full save location diff --git a/WriteIO.m b/WriteIO.m index 28ede9e..1966e68 100644 --- a/WriteIO.m +++ b/WriteIO.m @@ -2,7 +2,7 @@ tic if exist(resultfile, 'file') == 2 - delete(resultfile); + delete(resultfile); end toc % Create dataset @@ -17,4 +17,6 @@ % Store data tic h5write(resultfile, dataset, ToT_final_M); -toc \ No newline at end of file +toc +%% +clearvars dataset DINKY k \ No newline at end of file diff --git a/createFit_poly3.m b/createFit_poly3.m new file mode 100644 index 0000000..4359cfa --- /dev/null +++ b/createFit_poly3.m @@ -0,0 +1,39 @@ +function [fitresult, gof] = createFit1(M, N, O) +%CREATEFIT1(M,N,O) +% Create a fit. +% +% Data for 'Curve fitting' fit: +% X Input : M +% Y Output: N +% Weights : O +% Output: +% fitresult : a fit object representing the fit. +% gof : structure with goodness-of fit info. +% +% See also FIT, CFIT, SFIT. + +% Auto-generated by MATLAB on 03-Aug-2018 10:51:07!!!!! + + +%% Fit: 'untitled fit 1'. +[xData, yData, weights] = prepareCurveData( M, N, O ); + +% Set up fittype and options. +ft = fittype( 'poly3' ); +opts = fitoptions( 'Method', 'LinearLeastSquares' ); +%opts.Lower = [0 -Inf -Inf -Inf]; +opts.Weights = weights; + +% Fit model to data. +[fitresult, gof] = fit( xData, yData, ft, opts ); + +% % Plot fit with data. +% figure( 'Name', 'untitled fit 1' ); +% h = plot( fitresult, xData, yData ); +% legend( h, 'N vs. M with O', 'untitled fit 1', 'Location', 'NorthEast' ); +% % Label axes +% xlabel M +% ylabel N +% grid on + + diff --git a/normaliseCurve.m b/normaliseCurve.m index 31e5602..9442b10 100644 --- a/normaliseCurve.m +++ b/normaliseCurve.m @@ -1,8 +1,6 @@ - %% normaliseCurve +%% normaliseCurve 'load variables' Variables % load Variables file -%% -%ToT_FF_T_BIG2 = ToT_FF_T_BIG; % just a way to make sure mistakes are recoverable %% set tables ToT_FF_T_BIG2(:,3,:) = 0; %can be removed in final setting @@ -10,6 +8,7 @@ ToT_FF_size = (4 * 256 * 256); % should be calculated at a sooner point %% creates a M = 1024 by 1 matrix + o = 1; while o < toToT+1 M(o,1) = o; @@ -17,76 +16,148 @@ end x = M; +clear o + %% Calculates the normalised function % goes to x:3:x -% moving mean goes to x:4:x +% moving mean goes to x:4:x % summed x:3:x goes to x:5:x o = 1; while o < ToT_FF_size - + A = ToT_FF_T_BIG2(:,1,o); % makes a single string - calc = sum(A(1:fromToT)); % calculates the ammount of count from ToT 1 till set start ToT - A(1:fromToT,1) = 0; % Sets the first ToT values to 0 to a certain value + if fromToT == 1 + calc = 0; + else + calc = sum(A(1:fromToT)); % calculates the ammount of count from ToT 1 till set start ToT + A(1:fromToT,1) = 0; % Sets the first ToT values to 0 to a certain value + end totalPixelCount = ToT_FF_T_BIG2(1,2,o) - calc;% - sum(ToT_FF_T_BIG2(1:fromToT,1,o)); %- sum(ToT_FF_T_BIG2(1:fromToT,1,o)); ToT_FF_T_BIG2(20,2,o) = totalPixelCount; % Puts the cutoff event count in the table A = A(:,1)./totalPixelCount; % normalises the events ToT_FF_T_BIG2(:,3,o) = A; % puts normalised events at position 3 - % B = movmean(A,strength); %outdated - % ToT_FF_T_BIG2(:,4,o) = B; % outdated - xsize = 1024; + xsize = 1024; p = 1; % This loop calculates the cummulative normalised occurance while p < xsize + 1 - if p == 1 - ToT_FF_T_BIG2(p,5,o) = ToT_FF_T_BIG2(p,3,o); - else - P = p - 1; - ToT_FF_T_BIG2(p,5,o) = ToT_FF_T_BIG2(p,3,o)+ToT_FF_T_BIG2(P,5,o); - end - p = p + 1; + if p == 1 + ToT_FF_T_BIG2(p,5,o) = ToT_FF_T_BIG2(p,3,o); + else + P = p - 1; + ToT_FF_T_BIG2(p,5,o) = ToT_FF_T_BIG2(p,3,o)+ToT_FF_T_BIG2(P,5,o); + end + p = p + 1; end o = o + 1; end -%% call Averagetest +clear o p xsize A totalPixelcount calc P + +%% call Averagetest % Calculates the Average ToT response as well as the the curve fits for the % summation of this line % Creates Table for Curve fit information as well % as a table with the Averages, cumulative averages tic -AverageTest2_imp -AverageTest_Finaished = toc +if UsePoly == 2 + AverageTest2_imp % Uses the Polynome to second power fitting (could be buggy) +elseif UsePoly == 3 + AverageTest_poly3 % Uses the Polynome to third power fitting (latest method) +else + error('no curefitting "poly" was set') +end + +AverageTest_Finished = toc + +clearvars AverageTest_Finished %% Per pixel difference map -% uses histogram fitting +% uses histogram fitting ToT_FF_T_BIG3 = zeros(1024,3,ToT_FF_size); % Creates extra and final table ToT_FF_T_BIG3(:,1,:) = ToT_FF_T_BIG2(:,3,:); %copies the normalised event occurance in position 1 -ToT_FF_T_BIG3(:,2,:) = ToT_FF_T_BIG2(:,2,:); % copies the extra information like total occurance counts in +ToT_FF_T_BIG3(:,2,:) = ToT_FF_T_BIG2(:,2,:); % copies the extra information like total occurance counts in F = zeros(1024,4); o = 1; -h = waitbar(0,'Please wait...'); -steps = ToT_FF_size + 1; -tic -while o < ToT_FF_size + 1 - if ToT_FF_T_BIG2(1,2,o) > 10000 + +if new == 1 + memoryToT = 0; % remembers the last Correction ToT + AverageTable = zeros(3,11000); % Needed for precision + for i = 1 : length(AverageTable) + AverageTable(1,i+1) = i/10000; % fills averageTable + + if AverageTable(1,i) < AverageCurve(1,3) + AverageTable(2,i) = 1; % picks the first curve if it is the first + else + AverageTable(2,i) = find(AverageCurve(:,3) <= AverageTable(1,i), 1, 'last' ) ; %Finds the next curve + end + Curve = AverageTable(2,i); % picks the required curve + + a = AverageCurve(Curve,5); + b = AverageCurve(Curve,6); + c = AverageCurve(Curve,7); + d = AverageCurve(Curve,8); + + %loads the required y + if AverageTable(1,i) < AverageCurve(1,3) + y = AverageCurve(1,3); + else + y = AverageTable(1,i) + end + + % calculate the roots / removes imaginary results / sort the order + NT = roots([a b c d-y]); + KT = NT(imag(NT)==0); + KT = sort(KT); + + ji = 0; + if length(KT) == 1 + expectedToT = KT(1); % easy answer if there is only 1 + else + while ji < length(KT) + ji = ji + 1; + Dinky = KT(ji); + if Dinky >= memoryToT % picks the first ToT solution after the last; ToT cannot go negative + expectedToT = KT(ji); % hard answer + break + else + i; % should not be used but is backup for strange acting pixels + end + end + end + + AverageTable(3,i) = (expectedToT); + memoryToT = expectedToT; + + end + + Tryout4 % runs Tryout 4 (newest version) + + + %clear AverageTable +else %outdated + + while o < ToT_FF_size + 1 + if ToT_FF_T_BIG2(1,2,o) > 30000 D = ToT_FF_T_BIG2(:,5,o); Tryout3 + else o; end - h = waitbar(o/steps); - o = o + 1; + o = o + 1; + end + end -close(h) +%close(h) toc - - - +%% +clearvars a b c d Curve Averagecurve_succesfull Avtot Dinky esttime expectedToT F h i ik ikke is ji KATY KT loady M memoryToT N NTNUMERO o s st starters steps titleHandle totalPixelCount Try y +clearvars NUMERO NT minimumCounts AverageTable AverageTest_Finished outlierRemovalStrength %% Fix Matrix k= 1; @@ -108,6 +179,6 @@ ToT_final_M = round(ToT_final_M); %% -clear totalPixelCount o n B ans ToTpin ix corr_fact_ToT fromToT toToT expectedToT ans Avtot Avvan calc counter Curve d D F Gx intercept kend kstart starters step steps tester - +clear totalPixelCount o n B ans ToTpin ix corr_fact_ToT fromToT toToT expectedToT ans Avtot Avvan calc counter Curve d D F Gx intercept kend kstart starters step steps tester +clearvars AverageTest_Finished k diff --git a/totFlatMain.m b/totFlatMain.m index 7a74082..f25588c 100644 --- a/totFlatMain.m +++ b/totFlatMain.m @@ -1,45 +1,78 @@ -%% Variables +%% Variables This loads the variable file, please fill in any pointers there 'load variables' Variables % load Variables file -SS = {' '}; +SS = {' '}; % Spacer %% -ToT_FF_size = (4 * 256 * 256); +ToT_FF_size = (Chipnr * Dim_x * Dim_y); % creates the table size ToT_FF_T_BIG = zeros(1024,5,ToT_FF_size); ToT_FF_T_BIG2 = zeros(1024,5,ToT_FF_size); -%% Load multiple files +% %% Load multiple files +% jString = 0; +% iString = 33; +% +% while jString < iString +% +% tic +% jString = jString +1; +% if jString == 2; +% yes = 'yes' +% else +% jString2 = strcat(NumberingFormat,num2str(jString)); +% jString2 = jString2(end-4:end); +% filename = char(strcat(UserDIR, MainFILE, '_', jString2, FileTYPE)) +% Hits = h5read(filename, '/hits'); +% loady = toc +% tic +% totFlatfield2 %Runs the totFlatfield2 script +% ff = toc +% end +% end +% ToT_FF_T_BIG2 = ToT_FF_T_BIG; +% jString = 0; + +%% Load multiple files ToT hit files + jString = 0; -iString = 30; +iString = NumberofFiles; +count = 0; - while jString < iString - tic - jString = jString +1; - if jString == 2; - yes = 'yes' +while jString < iString + + tic + jString = jString +1; + if jString == 2; + yes = 'yes' + else + jString2 = strcat(NumberingFormat,num2str(jString)); + jString2 = jString2(end-1:end); + filename = char(strcat(UserDIR, MainFILE, '_', jString2, FileTYPE)) + data = h5read(filename, '/freq_tot'); + count = count + jString; + if count == 1 + data2 = data; else - jString2 = strcat('000000',num2str(jString)); - jString2 = jString2(end-5:end); - filename = char(strcat(UserDIR, MainFILE, '_', jString2, FileTYPE)) - Hits = h5read(filename, '/hits'); - loady = toc - tic - totFlatfield2 %Runs the totFlatfield2 script - ff = toc + data2 = data2 + data; end - end - - jString = 0; - + loady = toc + end +end +%% creates data_bases and basic info +ToT_FF_T_BIG2(:,1,:) = data2(:,:); +ToT_FF_T_BIG2(1,2,:) = sum(ToT_FF_T_BIG2(:,1,:)); +ToT_FF_T_BIG2(20,2,:) = ToT_FF_T_BIG2(1,2,:); + +jString = 0; %% Calls the NormaliseCurve scripts to caclualte the ToT correctionmap normaliseCurve -WriteIO - +%% write out result +WriteIO %% -clear UserDIR FileTUPE iString jString jString2 filename SS MainFILE loady ff FileTYPE ToA_T ToT_T x_T y_T ChipID_T - \ No newline at end of file +clear ans BadP VasP_T Chipnr count Dim_X Dim_y fromToT new NumberingFormat NumberofFiles resultfile stepAvg strength ToT_FF_size toToT UsePoly Dim_x BadP_T ToT_T_BIG UserDIR FileTUPE iString data data2 Hits jString jString2 filename SS MainFILE loady ff FileTYPE ToA_T ToT_T x_T y_T ChipID_T +clearvars data data2 iString jString jString2 loady \ No newline at end of file