function [ye,ROC,caModels,caROC] = GetShapeBasedROCPerformance_MultiWaySplit(Root,Template,variablename, objidx, options,savename);
%function [ye,ROC,caModels,caROC] = GetShapeBasedROCPerformance_MultiWaySplit(Root,Template,variablename, objidx, options,savename);
%
%computes performance on the shape based object data.

HelpTextInfo = 'GetPerformance';
AppRoot = '/cbcl/scratch01/bileschi/PrecomputedFeatures/StreetScenes/AppearanceDetector/';

d.nSplitParts = 5;

d.bRandsplit = 0; %0 means that we will take the first 2/3 for training
d.TrainingPart = 2/3;
d.sPARAMS.Nrounds = 10;
d.sPARAMS.R = 0;
d.sPARAMS.T = 0;

options = ResolveMissingOptions(options,d);

sPARAMS = options.sPARAMS;

objname{1} = 'car';
objname{2} = 'pedestrian';
objname{3} = 'bicycle';

objs2do = objidx;
for i = objs2do
  on = objname{i};
  [yfull, NewDataX] = GetNewDataX(Template, variablename, on,Root);
  [caTrainIdx, caDataXTrain, DataXTest, cayTrain, yTest,TestIdx] = DivideData(yfull, NewDataX, options);
  [caModels] = TrainModels(caDataXTrain, cayTrain, options);
  [cayel, caye, caROC] = TestModels(caModels, DataXTest, yTest, options);
  [ye, ROC] = CombineModels(caye, yTest,options); 
end

if(exist('savename'))
  save(savename,'HelpTextInfo','ye','ROC','caModels','caROC','yfull','TestIdx','Root','Template','variablename','objidx','caTrainIdx');
end

function [cayel, caye, caROC] = TestModels(caModels, DataXTest, yTest, options);
for j = 1:options.nSplitParts
    [cayel{j},caye{j}] = CLSgentleBoostC(DataXTest,caModels{j});
    [caROC{j}] = ROCetc2(caye{j}', yTest);
end 
  
function [ye, ROC] = CombineModels(caye, yTest,options);  
ye = zeros(size(yTest));
for j = 1:options.nSplitParts
  ye = ye + caye{j};
end
ROC = ROCetc2(ye',yTest);

function [caModels] = TrainModels(caDataXTrain, cayTrain, options);
for j = 1:options.nSplitParts  
  caModels{j} = CLSgentleBoost(caDataXTrain{j}, cayTrain{j}', options.sPARAMS);
end

function [caTrainIdx, caDataXTrain, DataXTest, cayTrain, yTest,TestIdx] = DivideData(yfull, NewDataX, options);
nParts = options.nSplitParts;
L = length(yfull);
partAllocation = mod(0:(L-1),nParts)+1;
if(options.bRandsplit)
  nTrain = round(options.TrainingPart * L);
  nTest = L - nTrain;
  p = randperm(L);
  Test = p(1:nTest);
  Train = p((nTest+1):end);
  partAllocation = partAllocation(randperm(length(partAllocation)));
else
  Test = 1:(floor(L * (1-options.TrainingPart)));
  Train = (floor(L * (1-options.TrainingPart))+1):L;
end
for j = 1:nParts
  caTrainIdx{j} = intersect(Train, find(partAllocation == j));
  caDataXTrain{j} = NewDataX(:,caTrainIdx{j});
  cayTrain{j} = yfull(caTrainIdx{j});
end
TestIdx = Test;
DataXTest = NewDataX(:,TestIdx);
yTest = yfull(TestIdx);
 
function [yfull, NewDataX] = GetNewDataX(Template, variablename,on,Root)
NewDataX = [];
yfull = [];
if(not(iscell(Template)))
  d = dir(fullfile(Root,sprintf('AppearanceData_128x128_%s*.mat',on)));
  d2 = dir(fullfile(Root,sprintf(Template,on)));
  for iFl = 1:min(length(d),4);
    load(fullfile(Root,d(iFl).name));%-->grayX,toyotaX,y;
    D = load(fullfile(Root,d2(iFl).name));%-->eval(variablename);
    NewDataX = [NewDataX,getfield(D,variablename)];
    yfull = [yfull,y]; 
  end
else
  sn = 1;
  d = dir(fullfile(Root,sprintf('AppearanceData_128x128_%s*.mat',on)));
  for iGroup = 1:length(Template)
    d2 = dir(fullfile(Root,sprintf(Template{iGroup},on)));
    sq = 1;
    for iFl = 1:min(length(d),4);
      D = load(fullfile(Root,d2(iFl).name));%-->eval(variablename);
      [n,q] = size(getfield(D,variablename{iGroup}));
      NewDataX(sn:(n+sn-1),sq:(sq+q-1)) = getfield(D,variablename{iGroup});
      sq = sq + q;
      if(iGroup == 1)
        load(fullfile(Root,d(iFl).name));%-->grayX,toyotaX,y;
        yfull = [yfull,y];
      end 
    end %loop over breaks in data on disk
  sn = size(NewDataX,1) + 1;
  end %loop over data types
end



     
