// File: /workspace/github_cpp_makefile_star2_O3/NichtJens[P]lmf2txt/lmf2txt.cpp
// Function: main
int main(int argc, char* argv[])
{
	#ifdef _DEBUG
		printf("\n***********************\n    SLOW DEBUG VERSION USED\n***********************\n");
	#endif

	printf("syntax: lmf2txt filename [-f] [-ns]\n");
	printf("        if -f is omitted then only the header\n");
	printf("        and the first 100 events will be written.\n");
	printf("        -ns converts the values to nanoseconds.\n");

	char LMF_Filename[500];

	if (argc < 2 || argc > 4)	{
		printf("wrong number of arguments.");
		return 0;
	}

	bool only_100_events_flag = true;
	bool time_conversion_flag = false;

	if (argc >= 3)	{
		if (strcmp((char*)argv[2],"-f") == 0) only_100_events_flag = false;
		if (strcmp((char*)argv[2],"-ns") == 0) time_conversion_flag = true;
		
	}
	if (argc == 4)	{
		if (strcmp((char*)argv[3],"-f") == 0) only_100_events_flag = false;
		if (strcmp((char*)argv[3],"-ns") == 0) time_conversion_flag = true;
		
	}

	strcpy(LMF_Filename,(char*)argv[1]);
	unsigned int	i,j;
	unsigned int	number_of_hits[NUM_CHANNELS];
	memset(number_of_hits,0,NUM_CHANNELS*4);
	int				iTDC[NUM_CHANNELS][NUM_IONS];
	double			dTDC[NUM_CHANNELS][NUM_IONS];

	LMF_IO * LMF = new LMF_IO(NUM_CHANNELS,NUM_IONS);

	if (!LMF->OpenInputLMF(LMF_Filename)) {
		return false;
	}

	char error_text[512];
	std::string ascii_output_filename = LMF_Filename;
	ascii_output_filename += std::string(".txt");
	FILE * ascii_outputfile_handle = fopen(ascii_output_filename.c_str(),"wt");


//	bool flag = false;

// Print parts of the header information:
// ---------------------------------------
	FPRINT"File name = %s\n",LMF->FilePathName.c_str());
	FPRINT"Versionstring = %s\n",LMF->Versionstring.c_str());
	FPRINT"Comment = %s\n",LMF->Comment.c_str());

	FPRINT"Headersize = %i\n",LMF->Headersize);

	FPRINT"Number of channels = %i\n",LMF->GetNumberOfChannels());
	FPRINT"Number of hits = %i\n",LMF->GetMaxNumberOfHits());
	FPRINT"Number of Coordinates = %i\n",LMF->Numberofcoordinates);
	
	
	FPRINT"Timestamp info = %i\n",LMF->timestamp_format);
	if (LMF->common_mode == 0) FPRINT"Common start\n"); else FPRINT"Common stop\n");
	FPRINT"Number of events = %lli\n",LMF->uint64_Numberofevents);
	FPRINT"Data format = %i\n",LMF->data_format_in_userheader);
	FPRINT"DAQ_ID = 0x%x\n",LMF->DAQ_ID);

	FPRINT"TDC resolution = %lf ns\n",LMF->tdcresolution);
	if (LMF->DAQ_ID == 0x000008 || LMF->DAQ_ID == 0x000010) {
		//FPRINT"TDC8HP Header Version %i\n",LMF->TDC8HP.UserHeaderVersion);
		FPRINT"Trigger channel = %i (counting from 1)\n",LMF->TDC8HP.TriggerChannel_p64);
		FPRINT"Trigger dead time = %lf ns\n",LMF->TDC8HP.TriggerDeadTime_p68);
		FPRINT"Group range start = %lf ns\n",LMF->TDC8HP.GroupRangeStart_p69);
		FPRINT"Group range end = %lf ns\n",LMF->TDC8HP.GroupRangeEnd_p70);
	}
	
	FPRINT"Starttime: %s",ctime(&LMF->Starttime));
	FPRINT"Stoptime:  %s",ctime(&LMF->Stoptime));
	FPRINT"\n");


//	LMF->OpenOutputLMF("test.lmf");

	double first_timestamp = 0.;
	double last_timestamp = 0.;

// Start reading event data:
// ---------------------------------------
	while(true) {
		if (LMF->ReadNextEvent())  {
			FPRINT"------- #%llu -------\n",LMF->GetEventNumber());
			double new_timestamp = LMF->GetDoubleTimeStamp();
			if (first_timestamp == 0.) first_timestamp = new_timestamp;
			new_timestamp -= first_timestamp;
			if (LMF->timestamp_format != 0) FPRINT"T  = %.3lf ns = \t%lf s\n",new_timestamp*1.e9,new_timestamp);
			if (LMF->timestamp_format != 0) FPRINT"dT = %.3lf ns = \t%lf s\n",(new_timestamp-last_timestamp)*1.e9,new_timestamp-last_timestamp);

			last_timestamp = new_timestamp;

			LMF->GetNumberOfHitsArray(number_of_hits);
			if (LMF->errorflag) {
				LMF->GetErrorText(LMF->errorflag,error_text);
				FPRINT"%s",error_text);
				return false;
			}
			if (LMF->data_format_in_userheader==5) {
				LMF->GetTDCDataArray(&dTDC[0][0]);
			} else LMF->GetTDCDataArray(&iTDC[0][0]);;

			if (LMF->errorflag) {
				LMF->GetErrorText(LMF->errorflag,error_text);
				FPRINT"%s",error_text);
				return false;
			}

			for (i=0;i<LMF->GetNumberOfChannels();i++) {
				FPRINT"chan %5i",i+1);
				FPRINT" %5i",number_of_hits[i]);
				if (LMF->data_format_in_userheader==5) {
					for (j=0;j<number_of_hits[i];j++) FPRINT" %lf",dTDC[i][j]);
				} else
					for (j=0;j<number_of_hits[i];j++) {
						if (!time_conversion_flag) FPRINT" %5i",iTDC[i][j]);
						if ( time_conversion_flag) FPRINT" %.3lf",iTDC[i][j]*LMF->tdcresolution);
					}
				FPRINT"\n");
			}
			FPRINT"Levelinfo: %llx\n",LMF->GetLastLevelInfo());
			FPRINT"\n");
		}

//		if (LMF->GetEventNumber()%50000 == 0) {
//			__int8 c;
//			while (_kbhit()) c = _getch();
//			if (c == 'q') break;
//		}

		if (LMF->errorflag) break;
		if (LMF->GetEventNumber() > 100	&& only_100_events_flag) break;
	}

	//LMF->CloseOutputLMF();


	if (LMF) delete LMF;

	fclose(ascii_outputfile_handle);

	return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2f3c8, %rsp          # imm = 0x2F3C8
movq %rsi, %rbx
movl %edi, %r14d
leaq 0xfce(%rip), %rdi       # 0x1471c
callq 0x21e0
leaq 0xfe6(%rip), %rdi       # 0x14740
callq 0x21e0
leaq 0x1008(%rip), %rdi      # 0x1476e
callq 0x21e0
leaq 0x102e(%rip), %rdi      # 0x147a0
callq 0x21e0
leal -0x5(%r14), %eax
cmpl $-0x4, %eax
ja 0x13793
leaq 0xd31(%rip), %rdi       # 0x144b8
xorl %eax, %eax
callq 0x2040
jmp 0x13e40
cmpl $0x3, %r14d
jb 0x13807
movq 0x10(%rbx), %rdi
movb $0x1, %r15b
cmpb $0x2d, (%rdi)
jne 0x137b3
cmpb $0x66, 0x1(%rdi)
jne 0x137b3
cmpb $0x0, 0x2(%rdi)
setne %r15b
leaq 0xd19(%rip), %rsi       # 0x144d3
callq 0x21b0
testl %eax, %eax
sete %cl
movl %ecx, 0x4(%rsp)
cmpl $0x4, %r14d
jne 0x13812
movq 0x18(%rbx), %rdi
cmpb $0x2d, (%rdi)
jne 0x137e7
cmpb $0x66, 0x1(%rdi)
jne 0x137e7
cmpb $0x0, 0x2(%rdi)
setne %cl
andb %cl, %r15b
testl %eax, %eax
sete %bpl
leaq 0xcdf(%rip), %rsi       # 0x144d3
callq 0x21b0
testl %eax, %eax
sete %al
orb %bpl, %al
movl %eax, 0x4(%rsp)
jmp 0x13812
movb $0x1, %r15b
movl $0x0, 0x4(%rsp)
movq 0x8(%rbx), %rsi
leaq 0x1c0(%rsp), %rdi
callq 0x2110
leaq 0x80(%rsp), %rdi
movl $0x140, %edx            # imm = 0x140
xorl %esi, %esi
callq 0x20b0
movl $0x2b80, %edi           # imm = 0x2B80
callq 0x2130
movq %rax, %r13
movq %rax, %rdi
movl $0x50, %esi
movl $0xc8, %edx
callq 0x2648
leaq 0x1c0(%rsp), %rsi
movq %r13, %rdi
callq 0x39d4
testb %al, %al
je 0x13e40
leaq 0x20(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x1c0(%rsp), %r14
movq %r14, %rdi
callq 0x2090
leaq (%rsp,%rax), %rdx
addq $0x1c0, %rdx            # imm = 0x1C0
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x13eb4
leaq 0x50(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0xc28(%rip), %rsi       # 0x144d7
leaq 0xc25(%rip), %rdx       # 0x144db
leaq 0x40(%rsp), %rdi
callq 0x13eb4
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x2060
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x138eb
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x10(%rsp), %rdi
leaq 0xbe5(%rip), %rsi       # 0x144dc
callq 0x2170
movq %rax, %r14
movq 0x20(%r13), %rdx
leaq 0xbd5(%rip), %rsi       # 0x144df
movq %rax, %rdi
xorl %eax, %eax
callq 0x21c0
movq (%r13), %rdx
leaq 0xbd0(%rip), %rsi       # 0x144ef
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movq 0x60(%r13), %rdx
leaq 0xbcf(%rip), %rsi       # 0x14503
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x298(%r13), %edx
leaq 0xbc5(%rip), %rsi       # 0x14511
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
callq 0x11f8e
leaq 0xbbd(%rip), %rsi       # 0x14522
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
callq 0x11f9c
leaq 0xbbb(%rip), %rsi       # 0x1453b
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x21c0
movl 0x270(%r13), %edx
leaq 0xbb6(%rip), %rsi       # 0x14550
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x2a8(%r13), %edx
leaq 0xbba(%rip), %rsi       # 0x1456c
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
cmpl $0x0, 0x1294(%r13)
je 0x139d4
leaq 0xbc2(%rip), %rdi       # 0x1458f
movl $0xc, %esi
jmp 0x139e0
leaq 0xba6(%rip), %rdi       # 0x14581
movl $0xd, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2240
movq 0x268(%r13), %rdx
leaq 0xba1(%rip), %rsi       # 0x1459c
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x290(%r13), %edx
leaq 0xba2(%rip), %rsi       # 0x145b5
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x288(%r13), %edx
leaq 0xb9c(%rip), %rsi       # 0x145c7
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movsd 0x12b8(%r13), %xmm0
leaq 0xb91(%rip), %rsi       # 0x145d6
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movl 0x288(%r13), %eax
cmpl $0x10, %eax
je 0x13a60
cmpl $0x8, %eax
jne 0x13ac6
movl 0x1414(%r13), %edx
leaq 0xb81(%rip), %rsi       # 0x145ef
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movsd 0x1428(%r13), %xmm0
leaq 0xb8f(%rip), %rsi       # 0x14617
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movsd 0x1430(%r13), %xmm0
leaq 0xb91(%rip), %rsi       # 0x14633
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movsd 0x1438(%r13), %xmm0
leaq 0xb93(%rip), %rsi       # 0x1464f
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
leaq 0x220(%r13), %rdi
callq 0x2070
leaq 0xb90(%rip), %rsi       # 0x14669
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0x2070
leaq 0xb7b(%rip), %rsi       # 0x14677
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movl $0xa, %edi
movq %r14, %rsi
callq 0x2190
xorpd %xmm0, %xmm0
movapd %xmm0, 0x60(%rsp)
movsd %xmm0, 0x8(%rsp)
movb %r15b, 0x3(%rsp)
movq %r13, %rdi
callq 0x100fa
testb %al, %al
je 0x13daf
movq %r13, %rdi
callq 0x11f86
movq %r14, %rdi
leaq 0xb38(%rip), %rsi       # 0x14685
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
callq 0x11fa4
movapd 0x60(%rsp), %xmm2
movapd %xmm2, %xmm3
cmpeqsd 0x93e(%rip), %xmm3      # 0x144b0
movapd %xmm3, %xmm4
movapd %xmm0, %xmm1
andpd %xmm0, %xmm4
andnpd %xmm2, %xmm3
orpd %xmm4, %xmm3
movapd %xmm3, 0x30(%rsp)
subsd %xmm3, %xmm1
cmpl $0x0, 0x2a8(%r13)
movapd %xmm1, 0x70(%rsp)
je 0x13bf0
movapd %xmm1, %xmm0
mulsd 0x8fc(%rip), %xmm0      # 0x144a8
movq %r14, %rdi
leaq 0xae6(%rip), %rsi       # 0x1469c
movb $0x2, %al
callq 0x21c0
movapd 0x70(%rsp), %xmm1
cmpl $0x0, 0x2a8(%r13)
je 0x13bf0
subsd 0x8(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x8c9(%rip), %xmm0      # 0x144a8
movq %r14, %rdi
leaq 0xacb(%rip), %rsi       # 0x146b4
movb $0x2, %al
callq 0x21c0
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
callq 0x11e94
movl 0x12e0(%r13), %esi
testl %esi, %esi
jne 0x13df2
cmpl $0x5, 0x290(%r13)
jne 0x13c2b
movq %r13, %rdi
leaq 0xffc0(%rsp), %rsi
callq 0x11c48
jmp 0x13c3b
movq %r13, %rdi
leaq 0x5c0(%rsp), %rsi
callq 0x11810
movl 0x12e0(%r13), %esi
testl %esi, %esi
jne 0x13e04
movapd 0x30(%rsp), %xmm0
movapd %xmm0, 0x60(%rsp)
leaq 0xffc0(%rsp), %r12
leaq 0x5c0(%rsp), %rbx
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x11f8e
movl %eax, %eax
cmpq %rax, %r15
jae 0x13d75
leaq 0x1(%r15), %rdx
movq %r14, %rdi
leaq 0xa42(%rip), %rsi       # 0x146cc
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0x21c0
movl 0x80(%rsp,%r15,4), %edx
movq %r14, %rdi
leaq 0xa28(%rip), %rsi       # 0x146d0
xorl %eax, %eax
callq 0x21c0
movq %r13, %rbp
cmpl $0x5, 0x290(%r13)
movl 0x80(%rsp,%r15,4), %eax
jne 0x13cf8
testl %eax, %eax
je 0x13d4d
xorl %r13d, %r13d
movsd (%r12,%r13,8), %xmm0
movq %r14, %rdi
leaq 0x9f6(%rip), %rsi       # 0x146d5
movb $0x1, %al
callq 0x21c0
incq %r13
movl 0x80(%rsp,%r15,4), %eax
cmpq %rax, %r13
jb 0x13ccf
jmp 0x13d4d
testl %eax, %eax
je 0x13d4d
xorl %r13d, %r13d
movl (%rbx,%r13,4), %edx
cmpb $0x0, 0x4(%rsp)
je 0x13d2c
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
mulsd 0x12b8(%rbp), %xmm0
movq %r14, %rdi
leaq 0x9b7(%rip), %rsi       # 0x146da
movb $0x1, %al
callq 0x21c0
jmp 0x13d3d
movq %r14, %rdi
leaq 0x99a(%rip), %rsi       # 0x146d0
xorl %eax, %eax
callq 0x21c0
incq %r13
movl 0x80(%rsp,%r15,4), %eax
cmpq %rax, %r13
jb 0x13cff
movl $0xa, %edi
movq %r14, %rsi
callq 0x2190
addq $0x320, %rbx            # imm = 0x320
addq $0x640, %r12            # imm = 0x640
movq %rbp, %r13
movq 0x30(%rsp), %r15
jmp 0x13c69
movq %r13, %rdi
callq 0x38f4
movb 0x3(%rsp), %r15b
movq %r14, %rdi
leaq 0x955(%rip), %rsi       # 0x146e1
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movl $0xa, %edi
movq %r14, %rsi
callq 0x2190
movapd 0x70(%rsp), %xmm0
movsd %xmm0, 0x8(%rsp)
cmpl $0x0, 0x12e0(%r13)
jne 0x13dd3
movq %r13, %rdi
callq 0x11f86
cmpq $0x65, %rax
setae %al
andb %r15b, %al
cmpb $0x1, %al
jne 0x13b2b
movq %r13, %rdi
callq 0x2f90
movl $0x2b80, %esi           # imm = 0x2B80
movq %r13, %rdi
callq 0x2140
movq %r14, %rdi
callq 0x2120
jmp 0x13e24
leaq 0x3c0(%rsp), %rdx
movq %r13, %rdi
callq 0x11f58
jmp 0x13e14
leaq 0x3c0(%rsp), %rdx
movq %r13, %rdi
callq 0x11f58
leaq 0x3c0(%rsp), %rdi
movq %r14, %rsi
callq 0x2150
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x13e40
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2140
xorl %eax, %eax
addq $0x2f3c8, %rsp          # imm = 0x2F3C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x13e8c
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x13e8f
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
jmp 0x13e8f
jmp 0x13e8c
movq %rax, %r14
jmp 0x13eab
jmp 0x13e8c
movq %rax, %r14
movl $0x2b80, %esi           # imm = 0x2B80
movq %r13, %rdi
jmp 0x13ea6
jmp 0x13e8c
jmp 0x13e8c
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x13eab
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2140
movq %r14, %rdi
callq 0x2220
nop