// File: NichtJens[P]lmf2txt/WriteLMF.cpp
// Function: main
int main(int argc, char* argv[], char* envp[])
//////////////////////////////////////////////////////////////////////////////////////////
{
	#ifdef _DEBUG
		printf("\n***********************\n    SLOW DEBUG VERSION USED\n***********************\n");
	#endif

	unsigned int	number_of_hits[NUM_CHANNELS];
	int				TDC[NUM_CHANNELS][NUM_IONS];


//  --------------------------------------------------------
//	read Header information of template LMF-file (ignore this part)
	LMF_IO * LMF = new LMF_IO(NUM_CHANNELS, NUM_IONS);
	char template_name[270];
	sprintf(template_name,"%s",argv[0]);
	while (strlen(template_name) > 0) {
		if (template_name[strlen(template_name)-1] == '\\' || template_name[strlen(template_name)-1] == '/') {
			break;
		}
		template_name[strlen(template_name)-1] = 0;
	}

	sprintf(template_name+strlen(template_name),"%s","data/Cobold2011R5-2_Header-Template_1TDC8HP.lmf");

	if (!LMF->OpenInputLMF(template_name)) {
		printf("error: could not open the template file\n%s\n",template_name);
		printf("It must be in the same folder as the exe.\n");
		return false;
	}
	LMF->CloseInputLMF();
//  end of read Header information ----------------------------

	FILE * in_file = 0;
	int result = 0;
	if (argc < 2)
		result = fopen_s(&in_file, "data/input_file.dat", "rt");
	else
		result = fopen_s(&in_file, argv[1], "rt");

	if (result || (!in_file)) {
		printf("input file not found.\n");
		LMF->CloseOutputLMF();
		if (LMF) delete LMF;
		return 0;
	}

//	Open and prepare output file
	LMF->number_of_channels_output = 16;
	LMF->max_number_of_hits_output = 10;
	LMF->frequency = 1.;
	LMF->tdcresolution_output				= 0.016; // can be ignored
	LMF->TDC8HP.GroupingEnable_p66_output	= 0;	 // can be ignored
	LMF->TDC8HP.GroupRangeStart_p69			= -100.; // can be ignored
	LMF->TDC8HP.GroupRangeEnd_p70			= 1000.; // can be ignored
	LMF->OpenOutputLMF((char*) "output.lmf");

	double dTimeStamp = 0.; // timestamp in seconds


//  Write Events into new LMF file:
	while (true) {
		memset(number_of_hits,   0,sizeof(number_of_hits)   );	// clear number_of_hits[] array
		memset(TDC,   0,sizeof(TDC)   );	// clear TDC[] array

		for (int channel = 0;channel < LMF->number_of_channels_output; channel++)
		{
			if (!fscanf_s(in_file,"%i",&number_of_hits[channel])) break;
			for (unsigned int hit = 0;hit < number_of_hits[channel]; hit++)
			{
				double time_in_ns = 0.;
				if (!fscanf_s(in_file, "%lf", &time_in_ns)) break;
				TDC[channel][hit] = int(time_in_ns / LMF->tdcresolution_output); // convert nanoseconds to TDC-bins
			}
		}
		
		if (feof(in_file)) break;

		dTimeStamp += 1.; // here you can set your own event timestamp (optionally)

		LMF->WriteTDCData(dTimeStamp, number_of_hits,&TDC[0][0]);
	}


	LMF->CloseOutputLMF();

	if (LMF) delete LMF;
	if (in_file) fclose(in_file);

	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x2ae0, %rsp           # imm = 0x2AE0
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movl $0x2b80, %edi           # imm = 0x2B80
callq 0x1140
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x2ac0(%rbp)
movl $0x50, %esi
movl $0x20, %edx
callq 0x16e0
jmp 0x1c325
movq -0x2ac0(%rbp), %rax
movq %rax, -0x2968(%rbp)
leaq -0x2a90(%rbp), %rdi
movq -0x10(%rbp), %rax
movq (%rax), %rdx
leaq 0x108f(%rip), %rsi      # 0x1d3d7
movb $0x0, %al
callq 0x1070
leaq -0x2a90(%rbp), %rdi
callq 0x10b0
cmpq $0x0, %rax
jbe 0x1c3e6
leaq -0x2a90(%rbp), %rdi
callq 0x10b0
subq $0x1, %rax
movsbl -0x2a90(%rbp,%rax), %eax
cmpl $0x5c, %eax
je 0x1c39f
leaq -0x2a90(%rbp), %rdi
callq 0x10b0
subq $0x1, %rax
movsbl -0x2a90(%rbp,%rax), %eax
cmpl $0x2f, %eax
jne 0x1c3c9
jmp 0x1c3e6
movq -0x2ac0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x2970(%rbp)
movl %eax, -0x2974(%rbp)
movl $0x2b80, %esi           # imm = 0x2B80
callq 0x1150
jmp 0x1c7e4
leaq -0x2a90(%rbp), %rdi
callq 0x10b0
subq $0x1, %rax
movb $0x0, -0x2a90(%rbp,%rax)
jmp 0x1c34f
leaq -0x2a90(%rbp), %rax
movq %rax, -0x2ac8(%rbp)
leaq -0x2a90(%rbp), %rdi
callq 0x10b0
movq -0x2ac8(%rbp), %rdi
addq %rax, %rdi
leaq 0xfc6(%rip), %rsi       # 0x1d3d7
leaq 0x101b(%rip), %rdx      # 0x1d433
movb $0x0, %al
callq 0x1070
movq -0x2968(%rbp), %rdi
leaq -0x2a90(%rbp), %rsi
callq 0x3120
testb $0x1, %al
jne 0x1c465
leaq -0x2a90(%rbp), %rsi
leaq 0x101f(%rip), %rdi      # 0x1d463
movb $0x0, %al
callq 0x1060
leaq 0x103d(%rip), %rdi      # 0x1d48f
movb $0x0, %al
callq 0x1060
movl $0x0, -0x4(%rbp)
jmp 0x1c7d8
movq -0x2968(%rbp), %rdi
callq 0x2be0
movq $0x0, -0x2a98(%rbp)
movl $0x0, -0x2a9c(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x1c4ae
leaq -0x2a98(%rbp), %rdi
leaq 0x1020(%rip), %rsi      # 0x1d4ba
leaq 0x102d(%rip), %rdx      # 0x1d4ce
callq 0x1c260
movl %eax, -0x2a9c(%rbp)
jmp 0x1c4cf
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rsi
leaq -0x2a98(%rbp), %rdi
leaq 0x100a(%rip), %rdx      # 0x1d4ce
callq 0x1c260
movl %eax, -0x2a9c(%rbp)
cmpl $0x0, -0x2a9c(%rbp)
jne 0x1c4e2
cmpq $0x0, -0x2a98(%rbp)
jne 0x1c545
leaq 0xfe8(%rip), %rdi       # 0x1d4d1
movb $0x0, %al
callq 0x1060
movq -0x2968(%rbp), %rdi
callq 0x2c60
cmpq $0x0, -0x2968(%rbp)
je 0x1c539
movq -0x2968(%rbp), %rax
movq %rax, -0x2ad0(%rbp)
cmpq $0x0, %rax
je 0x1c537
movq -0x2ad0(%rbp), %rdi
callq 0x2370
movq -0x2ad0(%rbp), %rdi
movl $0x2b80, %esi           # imm = 0x2B80
callq 0x1150
jmp 0x1c539
movl $0x0, -0x4(%rbp)
jmp 0x1c7d8
movq -0x2968(%rbp), %rax
movl $0x10, 0x2c4(%rax)
movq -0x2968(%rbp), %rax
movl $0xa, 0x2cc(%rax)
movq -0x2968(%rbp), %rax
movsd 0xaa2(%rip), %xmm0      # 0x1d018
movsd %xmm0, 0x12c8(%rax)
movq -0x2968(%rbp), %rax
movsd 0xe63(%rip), %xmm0      # 0x1d3f0
movsd %xmm0, 0x12c0(%rax)
movq -0x2968(%rbp), %rax
movl $0x0, 0x1420(%rax)
movq -0x2968(%rbp), %rax
movsd 0xe33(%rip), %xmm0      # 0x1d3e8
movsd %xmm0, 0x1430(%rax)
movq -0x2968(%rbp), %rax
movsd 0xe14(%rip), %xmm0      # 0x1d3e0
movsd %xmm0, 0x1438(%rax)
movq -0x2968(%rbp), %rdi
leaq 0xf06(%rip), %rsi       # 0x1d4e8
callq 0x12670
xorps %xmm0, %xmm0
movsd %xmm0, -0x2aa8(%rbp)
leaq -0x160(%rbp), %rdi
xorl %esi, %esi
movl $0x140, %edx            # imm = 0x140
callq 0x10c0
leaq -0x2960(%rbp), %rdi
xorl %esi, %esi
movl $0x2800, %edx           # imm = 0x2800
callq 0x10c0
movl $0x0, -0x2aac(%rbp)
movl -0x2aac(%rbp), %eax
movq -0x2968(%rbp), %rcx
cmpl 0x2c4(%rcx), %eax
jge 0x1c720
movq -0x2a98(%rbp), %rdi
movslq -0x2aac(%rbp), %rax
leaq -0x160(%rbp), %rdx
shlq $0x2, %rax
addq %rax, %rdx
leaq 0xe95(%rip), %rsi       # 0x1d4f3
movb $0x0, %al
callq 0x1210
cmpl $0x0, %eax
jne 0x1c66f
jmp 0x1c720
movl $0x0, -0x2ab0(%rbp)
movl -0x2ab0(%rbp), %eax
movslq -0x2aac(%rbp), %rcx
cmpl -0x160(%rbp,%rcx,4), %eax
jae 0x1c70a
xorps %xmm0, %xmm0
movsd %xmm0, -0x2ab8(%rbp)
movq -0x2a98(%rbp), %rdi
leaq 0xe4e(%rip), %rsi       # 0x1d4f6
leaq -0x2ab8(%rbp), %rdx
movb $0x0, %al
callq 0x1210
cmpl $0x0, %eax
jne 0x1c6bd
jmp 0x1c70a
movsd -0x2ab8(%rbp), %xmm0
movq -0x2968(%rbp), %rax
divsd 0x12c0(%rax), %xmm0
cvttsd2si %xmm0, %edx
movslq -0x2aac(%rbp), %rcx
leaq -0x2960(%rbp), %rax
shlq $0x7, %rcx
addq %rcx, %rax
movl -0x2ab0(%rbp), %ecx
movl %edx, (%rax,%rcx,4)
movl -0x2ab0(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2ab0(%rbp)
jmp 0x1c679
jmp 0x1c70c
movl -0x2aac(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2aac(%rbp)
jmp 0x1c622
movq -0x2a98(%rbp), %rdi
callq 0x11e0
cmpl $0x0, %eax
je 0x1c733
jmp 0x1c772
movsd 0x8dd(%rip), %xmm0      # 0x1d018
addsd -0x2aa8(%rbp), %xmm0
movsd %xmm0, -0x2aa8(%rbp)
movq -0x2968(%rbp), %rdi
movsd -0x2aa8(%rbp), %xmm0
leaq -0x160(%rbp), %rsi
leaq -0x2960(%rbp), %rdx
callq 0x14080
jmp 0x1c5f2
movq -0x2968(%rbp), %rdi
callq 0x2c60
cmpq $0x0, -0x2968(%rbp)
je 0x1c7bb
movq -0x2968(%rbp), %rax
movq %rax, -0x2ad8(%rbp)
cmpq $0x0, %rax
je 0x1c7b9
movq -0x2ad8(%rbp), %rdi
callq 0x2370
movq -0x2ad8(%rbp), %rdi
movl $0x2b80, %esi           # imm = 0x2B80
callq 0x1150
jmp 0x1c7bb
cmpq $0x0, -0x2a98(%rbp)
je 0x1c7d1
movq -0x2a98(%rbp), %rdi
callq 0x1130
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x2ae0, %rsp           # imm = 0x2AE0
popq %rbp
retq
movq -0x2970(%rbp), %rdi
callq 0x1200