From bf44c3286f3175382783f85864b24ef313234f26 Mon Sep 17 00:00:00 2001
From: Patrick Robbe <robbe@lal.in2p3.fr>
Date: Fri, 24 Jul 2020 23:11:48 +0900
Subject: [PATCH] Add possibility to write in a text file

---
 .gitignore                                    |  1 +
 .../main_pcie40_dmahighrate.cpp               | 24 +++++++++++++++++--
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6f4771f..303b792 100644
--- a/.gitignore
+++ b/.gitignore
@@ -45,3 +45,4 @@ Pcie40Applications/pcie40_dmahighrate
 Pcie40Applications/regconfig
 Pcie40Applications/statlink
 Pcie40Applications/timediff
+data_file.txt
diff --git a/Pcie40Applications/main_pcie40_dmahighrate.cpp b/Pcie40Applications/main_pcie40_dmahighrate.cpp
index d483a92..0785165 100644
--- a/Pcie40Applications/main_pcie40_dmahighrate.cpp
+++ b/Pcie40Applications/main_pcie40_dmahighrate.cpp
@@ -22,6 +22,7 @@ int max_number_of_messages = 10 ;
 #include<sys/ipc.h>
 #include<sys/shm.h>
 #include<sys/types.h>
+#include <fstream>
 
 std::map< int , int > n_messages = { 
   { 0 , 0 } , // no data
@@ -218,6 +219,13 @@ int analyzeHeader( unsigned int * & data , unsigned int & size , double & dsize
   return 0 ; 
 }
 
+void writeToFile( std::ofstream & the_file , unsigned int * data , int size ) {
+  the_file << "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl ; // to separate events
+  for ( int i = 0 ; i < 8 ; ++i ) { // Write the data in 32bit values
+    the_file << std::hex <<  data[ i ] << std::endl ;
+  }
+}
+
 int analyzeEventData( unsigned int * data , int i , unsigned int size , unsigned int &exprun , unsigned int &runnumber , unsigned int &evtnum ,
 		      std::set< int > vlinks , struct shm_crc * shmp[ NUMBER_OF_PROCESSES ]  ) {
   int expected_number_of_links = vlinks.size() ;
@@ -373,19 +381,26 @@ void analyzeEventGenerator( unsigned int * data , int i , unsigned int size ) {
 
 int main (int argc ,char** argv) {
   bool isData = true ;
+  bool writeInFile = false ; 
   printf( "Program to read events by DMA\n" ) ;
   if ( argc != 2) {
-    printf( "Needs one argument: 0 for data, 1 for internal generator\n" ) ;
+    printf( "Needs one argument: 0 for data, 1 for internal generator, 2 for writing into a file\n" ) ;
     return 0 ;
   }
 		    
-  if ( 0 != atoi(argv[ 1 ]) ) { 
+  if ( 1 == atoi(argv[ 1 ]) ) { 
     isData = false ;
     printf( "Generator mode\n" ) ;
+  }  else if ( 2 == atoi(argv[ 1 ]) ) { 
+    writeInFile = true ;
+    printf( "Data and file mode\n" ) ;
   } else { 
     printf( "Data mode\n" ) ;
   }
 
+  std::ofstream the_file ;
+  if ( writeInFile ) the_file.open( "data_file.txt" ) ;
+
   int shmid[ NUMBER_OF_PROCESSES ] ;
   // shared memory
   for ( int p = 0 ; p < NUMBER_OF_PROCESSES ; ++p ) {
@@ -558,6 +573,8 @@ int main (int argc ,char** argv) {
       if ( ( k % 1000 ) == 0 ) { 
 	printf( "Event number %d\n" , k ) ;
       }
+      if ( ( k % 100 ) == 0 ) 
+	if ( writeInFile ) writeToFile( the_file , data , esize ) ;
       if ( ( i > 0 ) && ( ( i % S_PAGE_SLOT_NMB ) == 0 ) ) pcie40_freeSuperPage( 0 , 1 ) ;
     }
   }
@@ -586,5 +603,8 @@ int main (int argc ,char** argv) {
   ecs_close( 0 , 0 ) ;
   ecs_close( 0 , 2 ) ;
   dma_close( 0 ) ;
+
+  if ( writeInFile ) the_file.close() ;
+
   return 0 ;
 }
-- 
GitLab