// File: /workspace/github_cpp_makefile_star2_O1/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 0x1955(%rip), %rdi      # 0x156c7
callq 0x21e0
leaq 0x196d(%rip), %rdi      # 0x156eb
callq 0x21e0
leaq 0x198f(%rip), %rdi      # 0x15719
callq 0x21e0
leaq 0x19b5(%rip), %rdi      # 0x1574b
callq 0x21e0
leal -0x5(%r14), %eax
cmpl $-0x4, %eax
ja 0x13db7
leaq 0x16b5(%rip), %rdi      # 0x15460
xorl %eax, %eax
callq 0x2040
jmp 0x1446e
cmpl $0x3, %r14d
jl 0x13df2
movq 0x10(%rbx), %r15
leaq 0x16b3(%rip), %rsi      # 0x1547b
movq %r15, %rdi
callq 0x21b0
testl %eax, %eax
setne %al
movl %eax, (%rsp)
leaq 0x169f(%rip), %rsi      # 0x1547e
movq %r15, %rdi
callq 0x21b0
testl %eax, %eax
sete %al
movl %eax, 0x4(%rsp)
jmp 0x13dff
movb $0x1, %al
movl %eax, (%rsp)
movl $0x0, 0x4(%rsp)
cmpl $0x4, %r14d
jne 0x13e48
movq 0x18(%rbx), %r14
leaq 0x166b(%rip), %rsi      # 0x1547b
movq %r14, %rdi
callq 0x21b0
testl %eax, %eax
movl (%rsp), %ecx
movzbl %cl, %ecx
cmovel %eax, %ecx
movl %ecx, (%rsp)
leaq 0x1651(%rip), %rsi      # 0x1547e
movq %r14, %rdi
callq 0x21b0
testl %eax, %eax
movzbl 0x4(%rsp), %eax
movl $0x1, %ecx
cmovnel %eax, %ecx
movl %ecx, 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 0x263e
leaq 0x1c0(%rsp), %rsi
movq %r13, %rdi
callq 0x3936
testb %al, %al
je 0x1446e
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 0x144e2
leaq 0x50(%rsp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x159d(%rip), %rsi      # 0x15482
leaq 0x159a(%rip), %rdx      # 0x15486
leaq 0x40(%rsp), %rdi
callq 0x144e2
movq 0x40(%rsp), %rsi
movq 0x48(%rsp), %rdx
leaq 0x10(%rsp), %rdi
callq 0x2060
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x13f21
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
movq 0x10(%rsp), %rdi
leaq 0x155a(%rip), %rsi      # 0x15487
callq 0x2170
movq %rax, %r14
movq 0x20(%r13), %rdx
leaq 0x154a(%rip), %rsi      # 0x1548a
movq %rax, %rdi
xorl %eax, %eax
callq 0x21c0
movq (%r13), %rdx
leaq 0x1545(%rip), %rsi      # 0x1549a
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movq 0x60(%r13), %rdx
leaq 0x1544(%rip), %rsi      # 0x154ae
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x298(%r13), %edx
leaq 0x153a(%rip), %rsi      # 0x154bc
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
callq 0x1213e
leaq 0x1532(%rip), %rsi      # 0x154cd
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
callq 0x1214c
leaq 0x1530(%rip), %rsi      # 0x154e6
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x21c0
movl 0x270(%r13), %edx
leaq 0x152b(%rip), %rsi      # 0x154fb
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x2a8(%r13), %edx
leaq 0x152f(%rip), %rsi      # 0x15517
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
cmpl $0x0, 0x1294(%r13)
je 0x1400a
leaq 0x1537(%rip), %rdi      # 0x1553a
movl $0xc, %esi
jmp 0x14016
leaq 0x151b(%rip), %rdi      # 0x1552c
movl $0xd, %esi
movl $0x1, %edx
movq %r14, %rcx
callq 0x2240
movq 0x268(%r13), %rdx
leaq 0x1516(%rip), %rsi      # 0x15547
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x290(%r13), %edx
leaq 0x1517(%rip), %rsi      # 0x15560
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x288(%r13), %edx
leaq 0x1511(%rip), %rsi      # 0x15572
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movsd 0x12b8(%r13), %xmm0
leaq 0x1506(%rip), %rsi      # 0x15581
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movl 0x288(%r13), %eax
cmpl $0x10, %eax
je 0x14096
cmpl $0x8, %eax
jne 0x140fc
movl 0x1414(%r13), %edx
leaq 0x14f6(%rip), %rsi      # 0x1559a
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movsd 0x1428(%r13), %xmm0
leaq 0x1504(%rip), %rsi      # 0x155c2
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movsd 0x1430(%r13), %xmm0
leaq 0x1506(%rip), %rsi      # 0x155de
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movsd 0x1438(%r13), %xmm0
leaq 0x1508(%rip), %rsi      # 0x155fa
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
leaq 0x220(%r13), %rdi
callq 0x2070
leaq 0x1505(%rip), %rsi      # 0x15614
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0x2070
leaq 0x14f0(%rip), %rsi      # 0x15622
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)
movq %r13, %rdi
callq 0x1029e
testb %al, %al
je 0x143db
movq %r13, %rdi
callq 0x12136
movq %r14, %rdi
leaq 0x14b2(%rip), %rsi      # 0x15630
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movq %r13, %rdi
callq 0x12154
movapd 0x60(%rsp), %xmm2
movapd %xmm2, %xmm3
cmpeqsd 0x12b5(%rip), %xmm3     # 0x15458
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 0x141ee
movapd %xmm1, %xmm0
mulsd 0x1273(%rip), %xmm0     # 0x15450
movq %r14, %rdi
leaq 0x1460(%rip), %rsi      # 0x15647
movb $0x2, %al
callq 0x21c0
cmpl $0x0, 0x2a8(%r13)
je 0x14221
movapd 0x70(%rsp), %xmm1
subsd 0x8(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0x1240(%rip), %xmm0     # 0x15450
movq %r14, %rdi
leaq 0x1445(%rip), %rsi      # 0x1565f
movb $0x2, %al
callq 0x21c0
movq %r13, %rdi
leaq 0x80(%rsp), %rsi
callq 0x12044
movl 0x12e0(%r13), %esi
testl %esi, %esi
jne 0x14420
cmpl $0x5, 0x290(%r13)
jne 0x1425c
movq %r13, %rdi
leaq 0xffc0(%rsp), %rsi
callq 0x11df6
jmp 0x1426c
movq %r13, %rdi
leaq 0x5c0(%rsp), %rsi
callq 0x119ba
movl 0x12e0(%r13), %esi
testl %esi, %esi
jne 0x14432
movapd 0x30(%rsp), %xmm0
movapd %xmm0, 0x60(%rsp)
leaq 0xffc0(%rsp), %r12
leaq 0x5c0(%rsp), %rbx
xorl %r15d, %r15d
movq %r13, %rdi
callq 0x1213e
movl %eax, %eax
cmpq %rax, %r15
jae 0x143a6
leaq 0x1(%r15), %rdx
movq %r14, %rdi
leaq 0x13bc(%rip), %rsi      # 0x15677
movq %rdx, 0x30(%rsp)
xorl %eax, %eax
callq 0x21c0
movl 0x80(%rsp,%r15,4), %edx
movq %r14, %rdi
leaq 0x13a2(%rip), %rsi      # 0x1567b
xorl %eax, %eax
callq 0x21c0
movq %r13, %rbp
cmpl $0x5, 0x290(%r13)
movl 0x80(%rsp,%r15,4), %eax
jne 0x14329
testl %eax, %eax
je 0x1437e
xorl %r13d, %r13d
movsd (%r12,%r13,8), %xmm0
movq %r14, %rdi
leaq 0x1370(%rip), %rsi      # 0x15680
movb $0x1, %al
callq 0x21c0
incq %r13
movl 0x80(%rsp,%r15,4), %eax
cmpq %rax, %r13
jb 0x14300
jmp 0x1437e
testl %eax, %eax
je 0x1437e
xorl %r13d, %r13d
movl (%rbx,%r13,4), %edx
cmpb $0x0, 0x4(%rsp)
je 0x1435d
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
mulsd 0x12b8(%rbp), %xmm0
movq %r14, %rdi
leaq 0x1331(%rip), %rsi      # 0x15685
movb $0x1, %al
callq 0x21c0
jmp 0x1436e
movq %r14, %rdi
leaq 0x1314(%rip), %rsi      # 0x1567b
xorl %eax, %eax
callq 0x21c0
incq %r13
movl 0x80(%rsp,%r15,4), %eax
cmpq %rax, %r13
jb 0x14330
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 0x1429a
movq %r13, %rdi
callq 0x3856
movq %r14, %rdi
leaq 0x12d4(%rip), %rsi      # 0x1568c
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 0x14401
movq %r13, %rdi
callq 0x12136
cmpq $0x65, %rax
jb 0x1415c
testb $0x1, (%rsp)
je 0x1415c
movq %r13, %rdi
callq 0x2f88
movl $0x2b80, %esi           # imm = 0x2B80
movq %r13, %rdi
callq 0x2140
movq %r14, %rdi
callq 0x2120
jmp 0x14452
leaq 0x3c0(%rsp), %rdx
movq %r13, %rdi
callq 0x12108
jmp 0x14442
leaq 0x3c0(%rsp), %rdx
movq %r13, %rdi
callq 0x12108
leaq 0x3c0(%rsp), %rdi
movq %r14, %rsi
callq 0x2150
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x1446e
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 0x144ba
movq %rax, %r14
movq 0x40(%rsp), %rdi
cmpq %rbx, %rdi
je 0x144bd
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x2140
jmp 0x144bd
jmp 0x144ba
movq %rax, %r14
jmp 0x144d9
jmp 0x144ba
movq %rax, %r14
movl $0x2b80, %esi           # imm = 0x2B80
movq %r13, %rdi
jmp 0x144d4
jmp 0x144ba
jmp 0x144ba
movq %rax, %r14
movq 0x10(%rsp), %rdi
leaq 0x20(%rsp), %rax
cmpq %rax, %rdi
je 0x144d9
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x2140
movq %r14, %rdi
callq 0x2220
nop