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.

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