data analysis scripts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.4 KiB

  1. //This file contains structures and functions used for processing the HIT2017 files
  2. #include <fstream>
  3. #include <iostream>
  4. using namespace std;
  5. #define DATA_SAMPLES_PER_SENSOR 64
  6. #define DATA_SENSORS_PER_BOARD 2
  7. //**** Structure definitions ****
  8. typedef struct SyncFrame
  9. {
  10. //unsigned short channel_id;
  11. unsigned short local_ctr;// local counter // 16 bits, possibly different for each board
  12. unsigned short global_ctr; //global counter same for all board up to 512
  13. unsigned short sma_state; //external signal input (external timestamper)
  14. //these files are additional compared to STM side
  15. unsigned short dummy = 0xFFFF; //spacer for nice structure packing
  16. int device_nr; //device number
  17. int data_ok; //0 = not ok, anything else is ok
  18. } SyncFrame;
  19. typedef struct BufferData
  20. {
  21. SyncFrame sync_frame;
  22. unsigned short sensor_data[DATA_SENSORS_PER_BOARD * DATA_SAMPLES_PER_SENSOR];
  23. } BufferData;
  24. //**** Interface functions ****
  25. //Read some data from a binary HIT2017 file
  26. // The data are grouped in block of BufferData type. Each block is a single frame
  27. // from one detector board. If there are more than 1 board, the data is ordered sequentially:
  28. // B1F1 B2F1 .. BmF1 B1F2 B2F2 .. BmF2 .. B1Fn B2Fn .. BmFn
  29. // where Bx stands for board x (1..m) and Fx stands for frame x (1..n)
  30. // User can read files in smaller blocks by specifying the first block and the number of blocks
  31. // to be read. The function will return the number of correctly read blocks.
  32. // The user must allocate memory for the data before running the function.
  33. // If smaller number of blocks was read than specified, the remaining data in the
  34. // allocated memory should be considered invalid.
  35. //Parameters:
  36. // filename: name of the file to read (with extension)
  37. // first: number of first data record (starting with 0)
  38. // nr: number of data records to be read (one record is one board!
  39. // dataptr: a pointer to an allocaced memory block with min size = sizeof(BufferData)*nr
  40. //Returned value: number of records read.
  41. int hit_read_binary_file(const char* file, int first, int nr, BufferData* dataptr);
  42. //**** Function bodies ****
  43. int hit_read_binary_file(const char* filename, int first, int nr, BufferData* dataptr)
  44. {
  45. ifstream file(filename, ifstream::in | ifstream::binary);
  46. if (!file.is_open()){
  47. return -1; //file could not be opened
  48. }
  49. //seek to right position
  50. file.seekg(first*sizeof(BufferData));
  51. //read data
  52. file.read((char*)dataptr, nr*sizeof(BufferData));
  53. int blocks_read = file.gcount() / sizeof(BufferData);
  54. // //close the file
  55. file.close();
  56. return blocks_read;
  57. }
  58. int getFileSize(const char* filename)
  59. {
  60. ifstream file(filename, ifstream::in | ifstream::binary);
  61. if(!file.is_open())
  62. {
  63. return -1;
  64. }
  65. file.seekg(0, ios::end);
  66. int fileSize = file.tellg();
  67. file.close();
  68. return fileSize;
  69. }
  70. int getBlock(const char* filename,int block)
  71. {
  72. ifstream file(filename, ifstream::in | ifstream::binary);
  73. if(!file.is_open())
  74. {
  75. return -1;
  76. }
  77. file.seekg (block*sizeof(BufferData));
  78. BufferData* dataptr = new BufferData();
  79. file.read ((char*)dataptr ,sizeof(BufferData));
  80. file.close();
  81. cout << dataptr->sync_frame.device_nr << endl;
  82. cout << dataptr->sensor_data[127] << endl;
  83. return 0;
  84. }