function applyReqMask(this, isMasked)
% cvdata.applyReqMask
% Reaggregate coverage from stored traces, applying mask constructed based
% on linked requirements (if isMasked=true).
% When isMasked=false, revert this operation, reaggregating coverage from
% the full stored traces (no mask applied).

%   Copyright 2019 The MathWorks, Inc.

    this.buildTraceMask();
    if (~this.traceOn) || isempty(this.trace)
        return;
    end
    
    [metricNames, toMetricNames] = getEnabledMetricNames(this);
    deleteIdx = contains(metricNames, {'sigrange', 'sigsize'}); % Unsupported metrics
    metricNames(deleteIdx == 1) = [];
    allMetricNames = [metricNames, toMetricNames];
    isTOmetric = [zeros(1,numel(metricNames)), ones(1,numel(toMetricNames))];
 
    for metricIdx = 1:length(allMetricNames)
        metricName = allMetricNames{metricIdx};
        
        if isMasked && ~isempty(this.traceMask) && isfield(this.traceMask, metricName)
            locTraceMask = this.traceMask.(metricName);
        else
            locTraceMask = [];
        end
        
        this.reaggregateFromTrace(metricName, isTOmetric(metricIdx), locTraceMask)
    end
end
