function [Tracks,duration] = goTracking(fname,minTrackDuration,minTrackLength,pixelsize,useSmoothing,frameinterval,maxspeed,segThreshold,minSize,maxSize,MaxLinkingDistance,MaxGapClosing)
%% segments a ilastik probability file and tracks single centrioles
%
finfo=h5info(fname);
minTrackLength=minTrackLength/pixelsize;  % we work in pixels here ;-)
datasetname=finfo.Datasets.Name;
Centrioles=permute(squeeze(h5read(fname,['/' datasetname])),[2,1,3]);
duration=size(Centrioles,3);
if isfloat(Centrioles)
    maxProb=1.0;
else
    maxProb=255;
end
Centrioles=(double(Centrioles)./maxProb)>segThreshold;

for i=1:size(Centrioles,3)
    frameProps=regionprops(squeeze(Centrioles(:,:,i)),'Centroid','Area');
    n=0;
    for j=1:length(frameProps)
        if  frameProps(j).Area>=minSize && frameProps(j).Area<=maxSize
            n=n+1;
            points{i}(n,1:2)= frameProps(j).Centroid;
        end
    end
end

[tracks,~,~] = simpletracker(points,'Debug',false,'MaxGapClosing',MaxGapClosing,'MaxLinkingDistance',MaxLinkingDistance);
n=0;
for i=1:length(tracks)
    if sum(~isnan(tracks{i}(:)))>minTrackDuration
        t=tracks{i}(:);
        for k=1:length(t)
            if isfinite(t(k))
                x(k)=points{k}(t(k),1);
                y(k)=points{k}(t(k),2);
            else
                x(k)=nan;y(k)=nan;
            end
        end
        a=find(~isnan(t));
        x(a(1):a(end))=fillmissing(x(a(1):a(end)),'linear');   % I linearly interpolate missing detections
        y(a(1):a(end))=fillmissing(y(a(1):a(end)),'linear');
        if sqrt((x(a(end))-x(a(1)))^2+(y(a(end))-y(a(1)))^2)>minTrackLength
           if useSmoothing
               x=smooth(x,'lowess');
               y=smooth(y,'lowess');
           end
           v=nan(size(x));
           alpha=v;
           for k=a(1)+1:a(end)
               v(k)=(sqrt((x(k)-x(k-1))^2+(y(k)-y(k-1))^2)/frameinterval).*pixelsize; % speed in pixels/frame
               alpha(k)=atan2d(y(k)-y(k-1),x(k)-x(k-1));  % alpha is only the x/y plane angle
           end    
           if all(v(a(1)+1:a(end))<=maxspeed)
               n=n+1;
               Tracks{n}=[x(a(1):a(end)),y(a(1):a(end)),(a(1):a(end))',v(a(1):a(end)),alpha(a(1):a(end))];
           end
        end
    end
end

% plot tracks for visual inspection
% uncomment while tuning these parameters:
% minSize,maxSize2, segThreshold, minTrackDuratio, minTrackLength=100 
% MaxLinkingDistance, MaxGapClosing, maxspeed 


% figure;
% hold('on');
% for i=1:length(Tracks)
%     plot(Tracks{i}(:,1),Tracks{i}(:,2));
% end
% title('ALL TRACKS');   
% set(gca,'Ydir','reverse');