function newCvd = cutSubsystem(this, targetSubsys)
%

%   Copyright 2018 The MathWorks, Inc.
try
    newCvd = [];
    rootId = this.rootID;

    %set variants for this data
    cvi.ReportData.updateDataIdx(this);
    %thia adds or finds an exisiting variant
    toCutRootVariantId = cvi.RootVariant.addRootVariant(rootId, targetSubsys);
    if isempty(toCutRootVariantId) || ...
       cv('get', toCutRootVariantId, '.state') == 0 %already cut
       return;
    end

    
    [metricNames, toMetricNames] = getEnabledMetricNames(this);
    deleteIdx = contains(metricNames, {'sigrange', 'sigsize'});
    metricNames(deleteIdx == 1) = [];
    allMetricNames = [metricNames, toMetricNames];
    
   
    topCvId = cv('get', rootId, '.topSlsf');
    descendantCvIds  = [topCvId, cv('DecendentsOf', topCvId)];
    sourceIndexMap = cvdata.getMetricIndices(this, descendantCvIds, allMetricNames);

    %set up data temporary this data with the variant turned off 
    %to get the right indices 
    toCutVariantStates = [];
    path = cv('get', toCutRootVariantId, '.path');
    foundIdx = [];
    origVariantStates = this.getRootVariantStates(); 
    if ~isempty(origVariantStates)
        foundIdx = find([origVariantStates.path] == string(path));
        %original variants state + the new variant to cut    
        if ~isempty(foundIdx)
            toCutVariantStates = origVariantStates;
            toCutVariantStates(foundIdx).state = 0;
        end
    end
    if isempty(foundIdx)
        tvs = [];
        tvs.path = path; 
        tvs.variantPath = cv('get', toCutRootVariantId, '.variantPath');
        tvs.state = 0; 
        if isempty(toCutVariantStates)
            toCutVariantStates = tvs; 
        else
            toCutVariantStates(end + 1) = tvs; 
        end
    end
    this.setRootVariantStates(toCutVariantStates); 
    
    cvi.ReportData.updateDataIdx(this);
    descendantCvIds  = [topCvId, cv('DecendentsOf', topCvId)];
    targetIndexMap = cvdata.getMetricIndices(this, descendantCvIds, allMetricNames);
    
    %restore the original
    this.setRootVariantStates(origVariantStates); 
    
       

    metricStruct = cvdata.processSubsystemMetric(rootId, [], targetIndexMap, ...
                                                     this, sourceIndexMap, ...
                                                     [],...
                                                     metricNames, toMetricNames, 'assign', true);
    newCvd = cvdata;
    newCvd.createDerivedData(this, this, metricStruct, []);
    newCvd.storeRootVariants(); 
catch MEx
    rethrow(MEx);
end
end
                                           