// File: /workspace/github_cpp_makefile_star2_O2/richardposner[P]BioNetFit2/src/code/Swarm.cpp
// Function: Swarm::finishFit()
void Swarm::finishFit() {

	if (options.verbosity >=3) cout<<"Fitting finished. Summarizing the results.\n";
	string outputDir;
	if (options.bootstrap) {
		outputDir = options.outputDir + "/" + options.jobName + "_bootstrap/";

		if (!checkIfFileExists(outputDir)) {
			string command = "mkdir " + outputDir + " && cp " + configPath_ + " " + outputDir;

			int errorCounter = 0;
			while (runCommand(command) != 0 && errorCounter < 10) {
				//sleep(1);
				++errorCounter;
			}

			if (errorCounter > 10) {
				cout << "Error: Couldn't create directory: " + outputDir + " to contain final bootstrap results.";
				//outputBootstrapSummary();
			}
		}

		// Output to bootstrap file
		outputBootstrapSummary();

		// Output fit summary
		string outputFilePath = outputDir + toString(bootstrapCounter + 1) + "_all_fits.txt";
		outputRunSummary(outputFilePath);

		// Reset variables
		resetVariables();

		if ((bootstrapCounter + 1) < options.bootstrap) {
			cout << "Deleting last fitting run and beginning next fit.." << endl;

			string command = "cd " + options.jobOutputDir + " && find -mindepth 1 -maxdepth 1 -type d -exec rm -r {} \\;";
			runCommand(command);

			killAllParticles(NEW_BOOTSTRAP);
		}
		else {
			killAllParticles(FIT_FINISHED);
			exit(EXIT_SUCCESS);
		}
	}
	else {
		outputDir = options.jobOutputDir + "Results/";   //was outputDir = options.jobOutputDir + "Results/";
		string command = "mkdir " + outputDir + " && cp " + configPath_ + " " + outputDir;

		int errorCounter = 0;
		while (runCommand(command) != 0 && errorCounter < 10) {
			//sleep(1);
			++errorCounter;
		}

		if (errorCounter > 10) {
			cout << "Error: Couldn't create directory: " + outputDir + " to contain final fitting results. Outputting results to screen.";
			outputRunSummary();
		}

		string outputFilePath = outputDir + "all_fits.txt";
		outputRunSummary(outputFilePath);

		generateBestFitModel(outputDir);

		if(options.verbosity>=3){
			cout << "Killing all particles, sending FIT_FINISHED message" << endl;
		}
		killAllParticles(FIT_FINISHED);
		if(options.verbosity>=3){
			cout << "Done killing all particles." << endl;
		}
		cout << "Finished fitting in " << tmr_.elapsed() << " seconds. Results can be found in " << options.jobOutputDir << "Results" << endl;
	}
	//copyBestFitToResults(outputDir);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x168, %rsp            # imm = 0x168
movq %rdi, %rbx
cmpl $0x3, 0x1614(%rdi)
jb 0x7291e
movq 0xf3686(%rip), %rdi     # 0x165f98
leaq 0x6b068(%rip), %rsi     # 0xdd981
callq 0x289f0
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x0, 0x1638(%rbx)
jne 0x72c08
leaq 0x1520(%rbx), %r14
leaq 0x6b0bf(%rip), %rdx     # 0xdda09
leaq 0x48(%rsp), %rdi
movq %r14, %rsi
callq 0x334ed
leaq 0x68(%rsp), %r15
leaq 0x48(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x28c40
movq %r12, %rdi
callq 0x295a0
leaq 0x6351c(%rip), %rsi     # 0xd5e97
leaq 0x88(%rsp), %rdi
movq %r15, %rdx
callq 0x3338b
leaq 0x6b027(%rip), %rdx     # 0xdd9b9
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3336c
leaq 0x1800(%rbx), %rdx
leaq 0x28(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x334ce
leaq 0x6cc46(%rip), %rdx     # 0xdf60d
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3336c
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x334ce
leaq 0x8(%rsp), %rdi
callq 0x295a0
leaq 0x28(%rsp), %rdi
callq 0x295a0
leaq 0xa8(%rsp), %rdi
callq 0x295a0
leaq 0x88(%rsp), %rdi
callq 0x295a0
xorl %r13d, %r13d
leaq 0x128(%rsp), %r15
leaq 0x48(%rsp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x28dd0
movq %r15, %rdi
callq 0x9f543
movl %eax, %ebp
movq %r15, %rdi
callq 0x295a0
testl %ebp, %ebp
je 0x72a54
cmpl $0x9, %r13d
ja 0x72a54
incl %r13d
jmp 0x72a28
cmpl $0xb, %r13d
jb 0x72ab3
leaq 0x6af60(%rip), %rsi     # 0xdd9c1
leaq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x3338b
leaq 0x6af9b(%rip), %rdx     # 0xdda12
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3336c
movq 0xf350b(%rip), %rdi     # 0x165f98
leaq 0x8(%rsp), %rsi
callq 0x28910
leaq 0x8(%rsp), %rdi
callq 0x295a0
leaq 0x28(%rsp), %rdi
callq 0x295a0
movq %rbx, %rdi
callq 0x7af8c
leaq 0x6af99(%rip), %rdx     # 0xdda53
leaq 0x8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x334ed
leaq 0xe8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x28dd0
leaq 0xe8(%rsp), %rsi
movq %rbx, %rdi
callq 0x7942a
leaq 0xe8(%rsp), %rdi
callq 0x295a0
leaq 0xc8(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x28dd0
leaq 0xc8(%rsp), %rsi
movq %rbx, %rdi
callq 0x7b292
leaq 0xc8(%rsp), %rdi
callq 0x295a0
cmpl $0x3, 0x1614(%rbx)
jb 0x72b4b
movq 0xf3461(%rip), %rdi     # 0x165f98
leaq 0x6af22(%rip), %rsi     # 0xdda60
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
pushq $0x16
popq %rsi
movq %rbx, %rdi
callq 0x7aeb6
cmpl $0x3, 0x1614(%rbx)
jb 0x72b7a
movq 0xf3432(%rip), %rdi     # 0x165f98
leaq 0x6af27(%rip), %rsi     # 0xdda94
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
movq 0xf3417(%rip), %rdi     # 0x165f98
leaq 0x6af28(%rip), %rsi     # 0xddab0
callq 0x289f0
movq %rax, %r15
addq $0x1db0, %rbx           # imm = 0x1DB0
movq %rbx, %rdi
callq 0x864e6
movq %r15, %rdi
callq 0x290f0
leaq 0x6af17(%rip), %rsi     # 0xddac5
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
movq %r14, %rsi
callq 0x28910
leaq 0x6af20(%rip), %rsi     # 0xddae8
movq %rax, %rdi
callq 0x289f0
movq %rax, %rdi
callq 0x284a0
leaq 0x8(%rsp), %rdi
callq 0x295a0
leaq 0x48(%rsp), %rdi
callq 0x295a0
leaq 0x68(%rsp), %rdi
callq 0x295a0
addq $0x168, %rsp            # imm = 0x168
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1500(%rbx), %rsi
leaq 0x6ada1(%rip), %rdx     # 0xdd9b7
leaq 0x28(%rsp), %rdi
callq 0x334ed
leaq 0x14c0(%rbx), %rdx
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x334ce
leaq 0x6ad70(%rip), %rdx     # 0xdd9ad
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x3336c
leaq 0x68(%rsp), %r14
leaq 0x48(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x28c40
movq %r15, %rdi
callq 0x295a0
leaq 0x8(%rsp), %rdi
callq 0x295a0
leaq 0x28(%rsp), %rdi
callq 0x295a0
leaq 0x108(%rsp), %rdi
movq %r14, %rsi
callq 0x28dd0
leaq 0x108(%rsp), %rdi
callq 0x9ec28
movl %eax, %ebp
leaq 0x108(%rsp), %rdi
callq 0x295a0
testl %ebp, %ebp
jne 0x72df4
leaq 0x631df(%rip), %rsi     # 0xd5e97
leaq 0x88(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x3338b
leaq 0x6ace8(%rip), %rdx     # 0xdd9b9
leaq 0xa8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x3336c
leaq 0x1800(%rbx), %rdx
leaq 0x28(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x334ce
leaq 0x6c907(%rip), %rdx     # 0xdf60d
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3336c
leaq 0x48(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x68(%rsp), %rdx
callq 0x334ce
leaq 0x8(%rsp), %rdi
callq 0x295a0
leaq 0x28(%rsp), %rdi
callq 0x295a0
leaq 0xa8(%rsp), %rdi
callq 0x295a0
leaq 0x88(%rsp), %rdi
callq 0x295a0
xorl %r12d, %r12d
leaq 0x148(%rsp), %r14
leaq 0x48(%rsp), %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x28dd0
movq %r14, %rdi
callq 0x9f543
movl %eax, %ebp
movq %r14, %rdi
callq 0x295a0
testl %ebp, %ebp
je 0x72d93
cmpl $0x9, %r12d
ja 0x72d93
incl %r12d
jmp 0x72d67
cmpl $0xb, %r12d
jb 0x72dea
leaq 0x6ac21(%rip), %rsi     # 0xdd9c1
leaq 0x28(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x3338b
leaq 0x6ac2e(%rip), %rdx     # 0xdd9e4
leaq 0x8(%rsp), %rdi
leaq 0x28(%rsp), %rsi
callq 0x3336c
movq 0xf31cc(%rip), %rdi     # 0x165f98
leaq 0x8(%rsp), %rsi
callq 0x28910
leaq 0x8(%rsp), %rdi
callq 0x295a0
leaq 0x28(%rsp), %rdi
callq 0x295a0
leaq 0x48(%rsp), %rdi
callq 0x295a0
movq %rbx, %rdi
callq 0x793e6
jmp 0x72e69
jmp 0x72e78
jmp 0x72ef8
movq %rax, %rbx
leaq 0x148(%rsp), %rdi
jmp 0x72f08
jmp 0x72ef8
jmp 0x72eaa
jmp 0x72eb9
jmp 0x72ec8
jmp 0x72eda
jmp 0x72ee9
movq %rax, %rbx
leaq 0x108(%rsp), %rdi
jmp 0x72f12
jmp 0x72ee9
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x295a0
jmp 0x72e5a
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x72f12
jmp 0x72ef8
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x295a0
jmp 0x72e7b
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
jmp 0x72f08
jmp 0x72ef8
jmp 0x72ee9
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
jmp 0x72ea1
movq %rax, %rbx
leaq 0xe8(%rsp), %rdi
callq 0x295a0
jmp 0x72ef1
jmp 0x72ef8
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x295a0
jmp 0x72ebc
movq %rax, %rbx
leaq 0x28(%rsp), %rdi
callq 0x295a0
jmp 0x72ecb
movq %rax, %rbx
leaq 0xa8(%rsp), %rdi
callq 0x295a0
jmp 0x72edd
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
jmp 0x72f12
jmp 0x72ee9
movq %rax, %rbx
jmp 0x72f17
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
jmp 0x72f08
movq %rax, %rbx
jmp 0x72f0d
movq %rax, %rbx
leaq 0x128(%rsp), %rdi
callq 0x295a0
leaq 0x48(%rsp), %rdi
callq 0x295a0
leaq 0x68(%rsp), %rdi
callq 0x295a0
movq %rbx, %rdi
callq 0x292f0
nop