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. THIS DOESN'T WORK!
  5. .L hitreader.c
  6. Hitdata data;
  7. data.read(my_file.da2); //to load whole file at once – forget it! See below.
  8. 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)
  9. //Reading 10 000 frames is reasonable. Reading 100 000 frames made my VM beg for memory.
  10. data.nrFrames //to see how many frames you have
  11. data.frames[0].nrBoards //to see how many boards you had in the system
  12. data.frames[0].boards[0].nrChannels //to see how many channels you have in board 0
  13. data.frames[10].boards[0].data[100] //get signal value for frame 10, board 0, channel 100
  14. data.frames[10].boards[0].syncframe.local_ctr //get the local synchro counter for frame 10, board 0
  15. //same for .global_ctr, .sma_state, .dummy, .device_nr, .data_ok
  16. */
  17. //*********************** Helper *************************
  18. #include <fstream>
  19. #include <iostream>
  20. using namespace std;
  21. //#define debug(str) std::cout << "HIT DEBUG: " << str << endl;
  22. #define debug(str)
  23. //*********************** Syncframe *************************
  24. class Syncframe
  25. {
  26. public:
  27. Syncframe()
  28. {
  29. debug("Syncframe()");
  30. local_ctr = global_ctr = 0;
  31. sma_state = dummy = 0;
  32. device_nr = -1;
  33. data_ok = 0;
  34. };
  35. ~Syncframe()
  36. {
  37. debug("~Syncframe()");
  38. };
  39. int sizeInFile()
  40. {
  41. return 16;
  42. };
  43. int read(std::ifstream* file)
  44. {
  45. char buffer[16];
  46. file->read(buffer,16);
  47. if (file->fail())
  48. return 0;
  49. local_ctr = *(unsigned short*)(buffer+0);
  50. global_ctr = *(unsigned short*)(buffer+2);
  51. sma_state = *(unsigned short*)(buffer+4);
  52. dummy = *(unsigned short*)(buffer+6);
  53. device_nr = *(int*)(buffer+8);
  54. data_ok = *(int*)(buffer+12);
  55. // std::cout << "Syncframe:" << local_ctr << " " << global_ctr << " " << sma_state << " " << dummy << " " << device_nr << " " << data_ok << std::endl;
  56. return 1;
  57. };
  58. unsigned short local_ctr;
  59. unsigned short global_ctr;
  60. unsigned short sma_state;
  61. unsigned short dummy;
  62. int device_nr;
  63. unsigned int data_ok;
  64. };
  65. //*********************** Sensorframe *************************
  66. class Boardframe
  67. {
  68. public:
  69. Boardframe(int nr_channels = 0)
  70. {
  71. debug("Boardframe()");
  72. data = NULL;
  73. resize (nr_channels);
  74. };
  75. Boardframe(const Boardframe& in)
  76. {
  77. debug("Boardframe(Boardframe&)");
  78. data = NULL;
  79. resize(in.nrChannels);
  80. for (int i = 0; i < nrChannels; i++)
  81. data[i] = in.data[i];
  82. syncframe = in.syncframe;
  83. };
  84. Boardframe& operator=(const Boardframe& in)
  85. {
  86. debug("Boardframe::operator==");
  87. resize(in.nrChannels);//creates an array called data of length nrChannels
  88. for (int i = 0; i < nrChannels; i++)
  89. data[i] = in.data[i];
  90. syncframe = in.syncframe;
  91. return *this;
  92. };
  93. ~Boardframe()
  94. {
  95. debug("~Boardframe()");
  96. if (data)
  97. delete[] data;
  98. };
  99. void resize(int nr_channels)
  100. {
  101. if (data)
  102. delete[] data;
  103. nrChannels = nr_channels;
  104. if (nrChannels)
  105. data = new unsigned short[nrChannels];
  106. else
  107. data = NULL;
  108. };
  109. int sizeInFile()
  110. {
  111. // std::cout << "boardframe.sizeInFile() = " << syncframe.sizeInFile() + nrChannels*2 << std::endl;
  112. return syncframe.sizeInFile() + nrChannels*2;
  113. };
  114. int read(std::ifstream* file)
  115. {
  116. if (syncframe.read(file) == 0)//get the syncframe before the board data
  117. return 0;
  118. //I must be already resized at this point!
  119. file->read((char*)data,2*nrChannels);
  120. if (file->fail())
  121. return 0;
  122. // std::cout<< "data[" << nrChannels << "]: ";
  123. // for (int i = 0;i<nrChannels;i++) std::cout << data[i] << " ";
  124. // std::cout << std::endl;
  125. return 1;
  126. };
  127. unsigned short& operator[] (int index)
  128. {
  129. return data[index];
  130. };
  131. Syncframe syncframe;
  132. int nrChannels;
  133. unsigned short* data;
  134. };
  135. //*********************** Fullframe *************************
  136. class Fullframe
  137. {
  138. public:
  139. Fullframe(int nr_boards = 0)
  140. {
  141. debug("Fullframe()");
  142. boards = NULL;
  143. resize(nr_boards);
  144. };
  145. Fullframe(const Fullframe& in)
  146. {
  147. debug("Fullframe(Fullframe&)");
  148. boards = NULL;
  149. resize(in.nrBoards);
  150. for (int i = 0; i < nrBoards; i++)
  151. boards[i] = in.boards[i];
  152. };
  153. Fullframe& operator=(const Fullframe& in)
  154. {
  155. debug("Fullframe::operator==");
  156. resize(in.nrBoards);
  157. for (int i = 0; i < nrBoards; i++)
  158. boards[i] = in.boards[i];
  159. return *this;
  160. };
  161. ~Fullframe()
  162. {
  163. debug("~Fullframe()");
  164. if (boards)
  165. delete[] boards;
  166. };
  167. void resize (int nr_boards)
  168. {
  169. if (boards)
  170. delete[] boards;
  171. nrBoards = nr_boards;
  172. if (nrBoards)
  173. boards = new Boardframe[nrBoards];
  174. else
  175. boards = NULL;
  176. }
  177. int sizeInFile()
  178. {
  179. if (boards){
  180. // std::cout << "Fullframe.sizeInFile() = " << 2 + nrBoards*2 + nrBoards * boards[0].sizeInFile() << std::endl;
  181. // return 2 + nrBoards*2 + nrBoards * boards[0].sizeInFile();
  182. //// boards[0].sizeInFile() returns 656 for every board...
  183. return 2 + 4*2 + (16 + 320 * 2) + (16 + 128*2)*3; //1482
  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. };