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.

410 lines
9.9 KiB

  1. //This is an object interface for reading HIT data files
  2. //See HIT documentation for details and examples.
  3. /*
  4. .L hitreader.c
  5. Hitdata data;
  6. data.read(my_file.da2); //to load whole file at once – forget it! See below.
  7. data.read(my_file.da2,1000,100,10) //to read 100 frames starting from frame 1000 and incrementing by 10 (i.e. frame 1000, 1010, 1020, ... 1990 will be read)
  8. //Reading 10 000 frames is reasonable. Reading 100 000 frames made my VM beg for memory.
  9. data.nrFrames //to see how many frames you have
  10. data.frames[0].nrBoards //to see how many boards you had in the system
  11. data.frames[0].boards[0].nrChannels //to see how many channels you have in board 0
  12. data.frames[10].boards[0].data[100] //get signal value for frame 10, board 0, channel 100
  13. data.frames[10].boards[0].syncframe.local_ctr //get the local synchro counter for frame 10, board 0
  14. //same for .global_ctr, .sma_state, .dummy, .device_nr, .data_ok
  15. */
  16. //*********************** Helper *************************
  17. //#define debug(str) std::cout << "HIT DEBUG: " << str << endl;
  18. #define debug(str)
  19. //*********************** Syncframe *************************
  20. class Syncframe
  21. {
  22. public:
  23. Syncframe()
  24. {
  25. debug("Syncframe()");
  26. local_ctr = global_ctr = 0;
  27. sma_state = dummy = 0;
  28. device_nr = -1;
  29. data_ok = 0;
  30. };
  31. ~Syncframe()
  32. {
  33. debug("~Syncframe()");
  34. };
  35. int sizeInFile()
  36. {
  37. return 16;
  38. };
  39. int read(std::ifstream* file)
  40. {
  41. char buffer[16];
  42. file->read(buffer,16);
  43. if (file->fail())
  44. return 0;
  45. local_ctr = *(unsigned short*)(buffer+0);
  46. global_ctr = *(unsigned short*)(buffer+2);
  47. sma_state = *(unsigned short*)(buffer+4);
  48. dummy = *(unsigned short*)(buffer+6);
  49. device_nr = *(int*)(buffer+8);
  50. data_ok = *(int*)(buffer+12);
  51. std::cout << "Syncframe:" << local_ctr << " " << global_ctr << " " << sma_state << " " << dummy << " " << device_nr << " " << data_ok << std::endl;
  52. return 1;
  53. };
  54. unsigned short local_ctr;
  55. unsigned short global_ctr;
  56. unsigned short sma_state;
  57. unsigned short dummy;
  58. int device_nr;
  59. unsigned int data_ok;
  60. };
  61. //*********************** Sensorframe *************************
  62. class Boardframe
  63. {
  64. public:
  65. Boardframe(int nr_channels = 0)
  66. {
  67. debug("Boardframe()");
  68. data = NULL;
  69. resize (nr_channels);
  70. };
  71. Boardframe(const Boardframe& in)
  72. {
  73. debug("Boardframe(Boardframe&)");
  74. data = NULL;
  75. resize(in.nrChannels);
  76. for (int i = 0; i < nrChannels; i++)
  77. data[i] = in.data[i];
  78. syncframe = in.syncframe;
  79. };
  80. Boardframe& operator=(const Boardframe& in)
  81. {
  82. debug("Boardframe::operator==");
  83. resize(in.nrChannels);//creates an array called data of length nrChannels
  84. for (int i = 0; i < nrChannels; i++)
  85. data[i] = in.data[i];
  86. syncframe = in.syncframe;
  87. return *this;
  88. };
  89. ~Boardframe()
  90. {
  91. debug("~Boardframe()");
  92. if (data)
  93. delete[] data;
  94. };
  95. void resize(int nr_channels)
  96. {
  97. if (data)
  98. delete[] data;
  99. nrChannels = nr_channels;
  100. if (nrChannels)
  101. data = new unsigned short[nrChannels];
  102. else
  103. data = NULL;
  104. };
  105. int sizeInFile()
  106. {
  107. return syncframe.sizeInFile() + nrChannels*2;
  108. };
  109. int read(std::ifstream* file)
  110. {
  111. if (syncframe.read(file) == 0)//get the syncframe before the board data
  112. return 0;
  113. //I must be already resized at this point!
  114. file->read((char*)data,2*nrChannels);
  115. if (file->fail())
  116. return 0;
  117. std::cout<< "data[" << nrChannels << "]: ";
  118. for (int i = 0;i<nrChannels;i++) std::cout << data[i] << " ";
  119. std::cout << std::endl;
  120. return 1;
  121. };
  122. unsigned short& operator[] (int index)
  123. {
  124. return data[index];
  125. };
  126. Syncframe syncframe;
  127. int nrChannels;
  128. unsigned short* data;
  129. };
  130. //*********************** Fullframe *************************
  131. class Fullframe
  132. {
  133. public:
  134. Fullframe(int nr_boards = 0)
  135. {
  136. debug("Fullframe()");
  137. boards = NULL;
  138. resize(nr_boards);
  139. };
  140. Fullframe(const Fullframe& in)
  141. {
  142. debug("Fullframe(Fullframe&)");
  143. boards = NULL;
  144. resize(in.nrBoards);
  145. for (int i = 0; i < nrBoards; i++)
  146. boards[i] = in.boards[i];
  147. };
  148. Fullframe& operator=(const Fullframe& in)
  149. {
  150. debug("Fullframe::operator==");
  151. resize(in.nrBoards);
  152. for (int i = 0; i < nrBoards; i++)
  153. boards[i] = in.boards[i];
  154. return *this;
  155. };
  156. ~Fullframe()
  157. {
  158. debug("~Fullframe()");
  159. if (boards)
  160. delete[] boards;
  161. };
  162. void resize (int nr_boards)
  163. {
  164. if (boards)
  165. delete[] boards;
  166. nrBoards = nr_boards;
  167. if (nrBoards)
  168. boards = new Boardframe[nrBoards];
  169. else
  170. boards = NULL;
  171. }
  172. int sizeInFile()
  173. {
  174. if (boards)
  175. return 2 + nrBoards*2 + nrBoards * boards[0].sizeInFile();
  176. else
  177. return 0; //no boards, makes no sense...
  178. };
  179. int read(std::ifstream* file)
  180. {
  181. //Read number of boards
  182. unsigned short nr_boards;
  183. file->read((char*)&nr_boards,2);
  184. if (file->fail() || nr_boards>6){
  185. std::cerr << "Unrealistic number of board to be read:"<< nr_boards << std:endl;
  186. return 0;
  187. }
  188. std::cout << " nr_boards: " << nr_boards << std::endl;
  189. //Read channel counts
  190. unsigned short* channel_counts = new unsigned short[nr_boards];
  191. file->read((char*)channel_counts,nr_boards*2);
  192. if (file->fail())
  193. {
  194. delete[] channel_counts;
  195. return 0;
  196. }
  197. //Read board frames
  198. resize(nr_boards);
  199. for (int board_nr = 0; board_nr < nr_boards; board_nr++)
  200. {
  201. std::cout << " channel_counts[" << board_nr << "]: "<< channel_counts[board_nr] << std::endl;
  202. boards[board_nr].resize(channel_counts[board_nr]);
  203. if (boards[board_nr].read(file) == 0)//read the board
  204. {
  205. delete[] channel_counts;
  206. return 0;
  207. }
  208. }
  209. delete[] channel_counts;
  210. return 1;
  211. };
  212. int nrChannels()
  213. {
  214. int result = 0;
  215. for (int board_nr = 0; board_nr < nrBoards; board_nr++)
  216. result += boards[board_nr].nrChannels;
  217. return result;
  218. };
  219. unsigned short& operator[] (int index)
  220. {
  221. for (int board_nr = 0; board_nr < nrBoards; board_nr++)
  222. {
  223. if (index >= boards[board_nr].nrChannels)
  224. index -= boards[board_nr].nrChannels;
  225. else
  226. return boards[board_nr][index];
  227. }
  228. std::cerr << " ### Fullframe::operator[]: index out of range!" << std::endl;
  229. // return (*NULL); //this will cause crash (intended).
  230. return boards[nrBoards][index];
  231. };
  232. int nrBoards;
  233. Boardframe* boards;
  234. };
  235. //*********************** Hitdata *************************
  236. class Hitdata
  237. {
  238. public:
  239. Hitdata(int nr_frames = 0)
  240. {
  241. frames = NULL;
  242. resize(nr_frames);
  243. };
  244. Hitdata(const Hitdata& in)
  245. {
  246. frames = NULL;
  247. resize(in.nrFrames);
  248. for (int i = 0; i < nrFrames; i++)
  249. frames[i] = in.frames[i];
  250. };
  251. Hitdata& operator=(const Hitdata& in)
  252. {
  253. resize(in.nrFrames);
  254. for (int i = 0; i < nrFrames; i++)
  255. frames[i] = in.frames[i];
  256. return *this;
  257. };
  258. ~Hitdata()
  259. {
  260. if (nrFrames)
  261. delete[] frames;
  262. };
  263. void resize (int nr_frames)
  264. {
  265. if (nrFrames)
  266. delete[] frames;
  267. nrFrames = nr_frames;
  268. if (nrFrames)
  269. frames = new Fullframe[nrFrames];
  270. else
  271. frames = NULL;
  272. };
  273. //Read data from a given file.
  274. //first_frame is the number of first frame to be read
  275. //nr_frames is the maximum number of frames to be read
  276. //-1 to read all of them
  277. //increment allows you reading once every nth sample
  278. //Return number of frames read or 0 in case of failure
  279. int readFile(char* filename, int first_frame = 0, int nr_frames = -1, int increment = 1)
  280. {
  281. std::ifstream file;
  282. //Open the file
  283. file.open(filename, ios_base::in | ios_base::binary);
  284. if (!file.is_open())
  285. {
  286. std::cerr << " ### Hitdata: File could not be open!" << std::endl;
  287. return 0; //file could not be opened
  288. }
  289. //Read first record to find board configuration
  290. Fullframe sampleframe;
  291. if (sampleframe.read(&file) == 0)
  292. {
  293. std::cerr << " ### Hitdata: First frame could not be read!" << std::endl;
  294. file.close();
  295. return 0;
  296. }
  297. //Check file size
  298. file.seekg(0, std::ios::beg);
  299. std::streamsize fsize = file.tellg();
  300. file.seekg(0, std::ios::end);
  301. fsize = file.tellg() - fsize;
  302. //Determine real frames to read
  303. unsigned int max_frames = (fsize / sampleframe.sizeInFile() - first_frame) / increment;
  304. if ((max_frames == -1) || (max_frames < nr_frames))
  305. nr_frames = max_frames;
  306. std::cout << " Hitdata: Nr frames to be read: " << nr_frames << std::endl;
  307. //Read!
  308. resize(nr_frames); //make an array called frames of size nr_frames
  309. file.seekg(first_frame * sampleframe.sizeInFile(), std::ios::beg);
  310. for (int frame_nr = 0; frame_nr < nr_frames; frame_nr++)
  311. {
  312. // if ((frame_nr%100) == 0)
  313. std::cout << " Frame " << frame_nr << std::endl;
  314. file.seekg((first_frame + frame_nr*increment) * sampleframe.sizeInFile(), std::ios::beg);
  315. if (frames[frame_nr].read(&file) == 0) //read the next frame
  316. {
  317. std::cerr << " ### Hitdata: Frame " << frame_nr << " could not be read!" << std::endl;
  318. file.close(); //read error, finish!
  319. // frames = frame_nr; //Kinky! We decrease nr_frames, but the actual array size remains unchanged!
  320. ///???? I don't know what the above line does.
  321. return frame_nr;
  322. }
  323. std::cout << frames[frame_nr].nrBoards << std::endl;
  324. }
  325. //Finished
  326. file.close();
  327. return nr_frames;
  328. };
  329. Fullframe& operator[] (int index)
  330. {
  331. if (index < nrFrames)
  332. return frames[index];
  333. else
  334. {
  335. std::cerr << " ### Hitdata::operator[]: index out of range!" << std::endl;
  336. // return (*NULL); //this will cause crash (intended).
  337. return frames[index];
  338. }
  339. };
  340. int nrFrames;
  341. Fullframe* frames;
  342. };