function [atime,FWHM,risetime,meanWave,thetime,Vfrac,AmpFact] = TimeAna(fn,numfiles,thresh)


load(fn);

who

theWaves=0;
Vmaxs=0;

if(numfiles==1)
    theWaves = theWaves1;
    Vmaxs=Vmaxs1;
else
    theWaves=[theWaves1 theWaves2];
    Vmaxs=[Vmaxs1; Vmaxs2];
end

isMaxCh2=(Vmaxs(:,2)>Vmaxs(:,1)).*(Vmaxs(:,2)>Vmaxs(:,3));
wavesCH2MaxonCH2 = theWaves(:,logical(isMaxCh2),2);
wavesCH1MaxonCH2 = theWaves(:,logical(isMaxCh2),1);
wavesCH3MaxonCH2 = theWaves(:,logical(isMaxCh2),3);
wavesCH4MaxonCH2 = theWaves(:,logical(isMaxCh2),4);
VmaxsMaxonCH2a=Vmaxs(logical(isMaxCh2),:);
isBigEnough=(VmaxsMaxonCH2a(:,2)>thresh);
GoodCH2waves=wavesCH2MaxonCH2(:,logical(isBigEnough));
GoodCH1waves=wavesCH1MaxonCH2(:,logical(isBigEnough));
GoodCH3waves=wavesCH3MaxonCH2(:,logical(isBigEnough));
GoodCH4waves=wavesCH4MaxonCH2(:,logical(isBigEnough));
VmaxsMaxonCH2=VmaxsMaxonCH2a(logical(isBigEnough),:);

yoho=size(VmaxsMaxonCH2)


%%%%%%%% Calculate Arrival Times %%%%%%%%%%%

thetime=time1;

[asize bsize]=size(GoodCH2waves);

atime=zeros(bsize,1);
FWHM=zeros(bsize,1);
risetime=zeros(bsize,1);

meanWave=zeros(asize,1);

for i=1:bsize
    [at,fw,rt]=findCFThreshold(GoodCH2waves(:,i),time1,0.3);
    aatime(i,1)=at;
    FWHM(i,1)=fw;
    arisetime(i,1)=rt;
    
    normC=Vmaxs(2,2);
    onorm=0;
    if(i>1) 
        onorm=Vmaxs(i,2);
    else
        onorm=1;
    end
        
    meanWave=meanWave+((normC/onorm)*(GoodCH2waves(:,i)));
end

meanWave=meanWave/bsize;



centroid= (VmaxsMaxonCH2(:,1)+2*VmaxsMaxonCH2(:,2)+3*VmaxsMaxonCH2(:,3))./((VmaxsMaxonCH2(:,1)+VmaxsMaxonCH2(:,2)+VmaxsMaxonCH2(:,3)));
isCentered= (centroid<2.4).*(centroid>1.6);

VmaxsCentered = VmaxsMaxonCH2(logical(isCentered),:);

Vfrac= (VmaxsCentered(:,2))./((VmaxsCentered(:,1))+(VmaxsCentered(:,2))+(VmaxsCentered(:,3)));

TotWave = GoodCH2waves(:,logical(isCentered)) + GoodCH4waves(:,logical(isCentered)) + 2*GoodCH1waves(:,logical(isCentered)) + 2*GoodCH3waves(:,logical(isCentered));
TotWaveB = 2*GoodCH2waves(:,logical(isCentered)) + 2*GoodCH1waves(:,logical(isCentered)) + 2*GoodCH3waves(:,logical(isCentered));

atime = aatime(logical(isCentered));
[asize bsize]=size(TotWave);

AmpFact=zeros(bsize,1);
meanTotWave=zeros(asize,1);

for i=1:bsize
    amp=IntegrateCharge(TotWave(:,i),time1,1,asize,12.);
    AmpFact(i,1)=(amp)/(1.60217646e-19);

    meanTotWave = meanTotWave + TotWaveB(:,i);
end

% comment this out if you want to get the mean pulse just on CH2...
meanWave = meanTotWave/bsize;
meanAmp=IntegrateCharge(meanWave,time1,1,asize,12.)/(1.60217646e-19)
meanWave=meanWave/(10^(12/20));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


end