// File: /workspace/github_cpp_makefile_star2_O2/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 0x1536(%rip), %rdi      # 0x126ec
callq 0x21e0
leaq 0x154e(%rip), %rdi      # 0x12710
callq 0x21e0
leaq 0x1570(%rip), %rdi      # 0x1273e
callq 0x21e0
leaq 0x1596(%rip), %rdi      # 0x12770
callq 0x21e0
leal -0x5(%r14), %eax
cmpl $-0x4, %eax
ja 0x111fb
leaq 0x1299(%rip), %rdi      # 0x12488
xorl %eax, %eax
callq 0x2040
jmp 0x1183b
cmpl $0x3, %r14d
jb 0x1126f
movq 0x10(%rbx), %rdi
movb $0x1, %r15b
cmpb $0x2d, (%rdi)
jne 0x1121b
cmpb $0x66, 0x1(%rdi)
jne 0x1121b
cmpb $0x0, 0x2(%rdi)
setne %r15b
leaq 0x1281(%rip), %rsi      # 0x124a3
callq 0x21b0
testl %eax, %eax
sete %cl
movl %ecx, 0x10(%rsp)
cmpl $0x4, %r14d
jne 0x1127a
movq 0x18(%rbx), %rdi
cmpb $0x2d, (%rdi)
jne 0x1124f
cmpb $0x66, 0x1(%rdi)
jne 0x1124f
cmpb $0x0, 0x2(%rdi)
setne %cl
andb %cl, %r15b
testl %eax, %eax
sete %bpl
leaq 0x1247(%rip), %rsi      # 0x124a3
callq 0x21b0
testl %eax, %eax
sete %al
orb %bpl, %al
movl %eax, 0x10(%rsp)
jmp 0x1127a
movb $0x1, %r15b
movl $0x0, 0x10(%rsp)
movq 0x8(%rbx), %rsi
leaq 0x3c0(%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, %rbx
pushq $0x50
popq %rsi
movq %rax, %rdi
movl $0xc8, %edx
callq 0x25b8
leaq 0x3c0(%rsp), %rsi
movq %rbx, 0x8(%rsp)
movq %rbx, %rdi
callq 0x347c
testb %al, %al
je 0x1183b
leaq 0x60(%rsp), %rdi
leaq 0x3c0(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x11894
leaq 0x11b0(%rip), %rsi      # 0x124a7
leaq 0x40(%rsp), %rdi
leaq 0x17(%rsp), %rdx
callq 0x11894
leaq 0x60(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x2250
leaq 0x40(%rsp), %rdi
callq 0x2090
movq 0x60(%rsp), %rdi
leaq 0x1181(%rip), %rsi      # 0x124ac
callq 0x2170
movq %rax, %r14
movq 0x8(%rsp), %rbx
movq 0x20(%rbx), %rdx
leaq 0x116c(%rip), %rsi      # 0x124af
movq %rax, %rdi
xorl %eax, %eax
callq 0x21c0
movq (%rbx), %rdx
leaq 0x1168(%rip), %rsi      # 0x124bf
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movq 0x60(%rbx), %rdx
leaq 0x1167(%rip), %rsi      # 0x124d3
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x298(%rbx), %edx
leaq 0x115e(%rip), %rsi      # 0x124e1
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movq %rbx, %rdi
callq 0xf9e8
leaq 0x1156(%rip), %rsi      # 0x124f2
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x21c0
movq %rbx, %rdi
callq 0xf9f6
leaq 0x1154(%rip), %rsi      # 0x1250b
movq %r14, %rdi
movl %eax, %edx
xorl %eax, %eax
callq 0x21c0
movl 0x270(%rbx), %edx
leaq 0x1150(%rip), %rsi      # 0x12520
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x2a8(%rbx), %edx
leaq 0x1155(%rip), %rsi      # 0x1253c
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
cmpl $0x0, 0x1294(%rbx)
je 0x11405
leaq 0x115e(%rip), %rdi      # 0x1255f
pushq $0xc
jmp 0x1140e
leaq 0x1145(%rip), %rdi      # 0x12551
pushq $0xd
popq %rsi
pushq $0x1
popq %rdx
movq %r14, %rcx
callq 0x2240
movq 0x8(%rsp), %rbx
movq 0x268(%rbx), %rdx
leaq 0x113f(%rip), %rsi      # 0x1256c
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x290(%rbx), %edx
leaq 0x1141(%rip), %rsi      # 0x12585
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movl 0x288(%rbx), %edx
leaq 0x113c(%rip), %rsi      # 0x12597
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movsd 0x12b8(%rbx), %xmm0
leaq 0x1132(%rip), %rsi      # 0x125a6
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movl 0x288(%rbx), %eax
cmpl $0x10, %eax
je 0x1148e
cmpl $0x8, %eax
jne 0x114f0
movl 0x1414(%rbx), %edx
leaq 0x1124(%rip), %rsi      # 0x125bf
movq %r14, %rdi
xorl %eax, %eax
callq 0x21c0
movsd 0x1428(%rbx), %xmm0
leaq 0x1133(%rip), %rsi      # 0x125e7
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movsd 0x1430(%rbx), %xmm0
leaq 0x1136(%rip), %rsi      # 0x12603
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
movsd 0x1438(%rbx), %xmm0
leaq 0x1139(%rip), %rsi      # 0x1261f
movq %r14, %rdi
movb $0x1, %al
callq 0x21c0
leaq 0x220(%rbx), %rdi
callq 0x2060
leaq 0x1136(%rip), %rsi      # 0x12639
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movq %rbx, %rdi
addq $0x228, %rdi            # imm = 0x228
callq 0x2060
leaq 0x1121(%rip), %rsi      # 0x12647
movq %r14, %rdi
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x2190
xorpd %xmm0, %xmm0
movapd %xmm0, 0x20(%rsp)
movsd %xmm0, 0x18(%rsp)
movb %r15b, 0x7(%rsp)
movq %rbx, %rdi
callq 0xde8a
testb %al, %al
je 0x117b1
movq 0x8(%rsp), %rdi
callq 0xf9e0
movq %r14, %rdi
leaq 0x10de(%rip), %rsi      # 0x12655
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
movq 0x8(%rsp), %rbx
movq %rbx, %rdi
callq 0xf9fe
movapd 0x20(%rsp), %xmm2
movapd %xmm2, %xmm3
cmpeqsd 0xedf(%rip), %xmm3      # 0x12480
movapd %xmm3, %xmm4
movapd %xmm0, %xmm1
andpd %xmm0, %xmm4
andnpd %xmm2, %xmm3
orpd %xmm4, %xmm3
movapd %xmm3, 0x20(%rsp)
subsd %xmm3, %xmm1
cmpl $0x0, 0x2a8(%rbx)
movapd %xmm1, 0x30(%rsp)
je 0x1161d
movapd %xmm1, %xmm0
mulsd 0xe9e(%rip), %xmm0      # 0x12478
movq %r14, %rdi
leaq 0x1088(%rip), %rsi      # 0x1266c
movb $0x2, %al
callq 0x21c0
movapd 0x30(%rsp), %xmm1
cmpl $0x0, 0x2a8(%rbx)
je 0x1161d
subsd 0x18(%rsp), %xmm1
movapd %xmm1, %xmm0
mulsd 0xe6c(%rip), %xmm0      # 0x12478
movq %r14, %rdi
leaq 0x106e(%rip), %rsi      # 0x12684
movb $0x2, %al
callq 0x21c0
movq %rbx, %rdi
leaq 0x80(%rsp), %rsi
callq 0xf90a
movl 0x12e0(%rbx), %esi
testl %esi, %esi
jne 0x117fd
cmpl $0x5, 0x290(%rbx)
jne 0x11656
movq %rbx, %rdi
leaq 0xffc0(%rsp), %rsi
callq 0xf678
jmp 0x11666
movq %rbx, %rdi
leaq 0x5c0(%rsp), %rsi
callq 0xf1c0
movl 0x12e0(%rbx), %esi
testl %esi, %esi
jne 0x1180f
leaq 0xffc0(%rsp), %r13
leaq 0x5c0(%rsp), %rbx
xorl %ebp, %ebp
movq 0x8(%rsp), %r15
movq %r15, %rdi
callq 0xf9e8
movl %eax, %eax
cmpq %rax, %rbp
jae 0x11779
leaq 0x1(%rbp), %r12
movq %r14, %rdi
leaq 0xff0(%rip), %rsi       # 0x1269c
movl %r12d, %edx
xorl %eax, %eax
callq 0x21c0
movl 0x80(%rsp,%rbp,4), %edx
movq %r14, %rdi
leaq 0xfd9(%rip), %rsi       # 0x126a0
xorl %eax, %eax
callq 0x21c0
cmpl $0x5, 0x290(%r15)
jne 0x11704
xorl %r15d, %r15d
movl 0x80(%rsp,%rbp,4), %eax
cmpq %rax, %r15
jae 0x11758
movsd (%r13,%r15,8), %xmm0
movq %r14, %rdi
leaq 0xfad(%rip), %rsi       # 0x126a5
movb $0x1, %al
callq 0x21c0
incq %r15
jmp 0x116db
xorl %r15d, %r15d
movl 0x80(%rsp,%rbp,4), %eax
cmpq %rax, %r15
jae 0x11758
movl (%rbx,%r15,4), %edx
cmpb $0x0, 0x10(%rsp)
je 0x11742
cvtsi2sd %edx, %xmm0
movq 0x8(%rsp), %rax
mulsd 0x12b8(%rax), %xmm0
movq %r14, %rdi
leaq 0xf71(%rip), %rsi       # 0x126aa
movb $0x1, %al
callq 0x21c0
jmp 0x11753
movq %r14, %rdi
leaq 0xf54(%rip), %rsi       # 0x126a0
xorl %eax, %eax
callq 0x21c0
incq %r15
jmp 0x11707
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x2190
addq $0x320, %rbx            # imm = 0x320
addq $0x640, %r13            # imm = 0x640
movq %r12, %rbp
jmp 0x11686
movq %r15, %rdi
callq 0x33c6
movb 0x7(%rsp), %r15b
movq %r14, %rdi
leaq 0xf21(%rip), %rsi       # 0x126b1
movq %rax, %rdx
xorl %eax, %eax
callq 0x21c0
pushq $0xa
popq %rdi
movq %r14, %rsi
callq 0x2190
movapd 0x30(%rsp), %xmm0
movsd %xmm0, 0x18(%rsp)
movq 0x8(%rsp), %rbx
cmpl $0x0, 0x12e0(%rbx)
jne 0x117de
movq %rbx, %rdi
callq 0xf9e0
cmpq $0x65, %rax
setae %al
andb %r15b, %al
cmpb $0x1, %al
movq 0x8(%rsp), %rbx
jne 0x11553
movq %rbx, %rdi
callq 0x2da8
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbx, %rdi
callq 0x2140
movq %r14, %rdi
callq 0x2120
jmp 0x11831
leaq 0x1c0(%rsp), %rdx
movq %rbx, %rdi
callq 0xf9be
jmp 0x11821
leaq 0x1c0(%rsp), %rdx
movq 0x8(%rsp), %rdi
callq 0xf9be
leaq 0x1c0(%rsp), %rdi
movq %r14, %rsi
callq 0x2150
leaq 0x60(%rsp), %rdi
callq 0x2090
xorl %eax, %eax
addq $0x2f3c8, %rsp          # imm = 0x2F3C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x1187f
movq %rax, %r14
leaq 0x40(%rsp), %rdi
callq 0x2090
jmp 0x11882
jmp 0x1187f
movq %rax, %r14
jmp 0x1188c
jmp 0x1187f
movq %rax, %r14
movl $0x2b80, %esi           # imm = 0x2B80
movq %rbx, %rdi
callq 0x2140
jmp 0x1188c
jmp 0x1187f
jmp 0x1187f
movq %rax, %r14
leaq 0x60(%rsp), %rdi
callq 0x2090
movq %r14, %rdi
callq 0x2210