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.

440 lines
15 KiB

  1. #define hit_analyse_v2_cxx
  2. #include "hit_analyse_v2.h"
  3. int main(int argc, char **argv){
  4. opendatafiles(argc, argv);
  5. histograms(argc, argv);
  6. analyse(argc, argv);
  7. closedatafiles();
  8. return 0;
  9. }
  10. int opendatafiles(int argc, char ** argv){
  11. if (argc>2){
  12. //open bpm data file
  13. filename = Form("%s%s.da2",argv[1],argv[2]);
  14. file.open(filename, ifstream::in | ifstream::binary);
  15. if (!file.is_open())
  16. {
  17. std::cerr << " ### Hitdata: File could not be opened!" << filename << std::endl;
  18. return 0; //file could not be opened
  19. }
  20. else {std::cout << filename << " opened successfully." << std::endl;}
  21. }
  22. string visualize_check = argv[5]; //plot data
  23. if (visualize_check == "vis_true") {visualize = true;}
  24. else{ visualize= false;}
  25. return 1;
  26. }
  27. int closedatafiles(){
  28. if (file.is_open()) file.close();
  29. // if (timestampfile.is_open()) timestampfile.close();
  30. //if (offsetfile.is_open()) offsetfile.close();
  31. rootFile->Write();
  32. rootFile->Close();
  33. }
  34. int analyse(int argc, char **argv)
  35. {
  36. int first_frame = 0; // 1440000
  37. int nr_frames = -1;
  38. int increment = 1;
  39. //Read first record to find board configuration
  40. Fullframe sampleframe;
  41. if (sampleframe.read(&file) == 0)
  42. {
  43. std::cerr << " ### Hitdata: First frame could not be read!" << std::endl;
  44. file.close();
  45. return 0;
  46. }
  47. else {
  48. std::cout << "Sample frame size (bytes): " << sampleframe.sizeInFile() << std::endl;
  49. }
  50. //Check file size
  51. file.seekg(0, std::ios::beg);
  52. std::streamsize fsize = file.tellg();
  53. file.seekg(0, std::ios::end);
  54. fsize = file.tellg() - fsize;
  55. //Determine real frames to read
  56. unsigned int max_frames = fsize / sampleframe.sizeInFile();
  57. if ((max_frames == -1) || (max_frames < nr_frames))
  58. nr_frames = max_frames;
  59. std::cout << " Hitdata: Nr frames to be read: " << nr_frames << std::endl;
  60. ///set the background levels from first N events
  61. int bkg_frames = 1000;
  62. if (set_background_v2(0, bkg_frames)==0) return 0;
  63. BPMbeamrecon_Zeroed.Position = -128.;
  64. BPMbeamrecon_Zeroed.Focus = -1.;
  65. BPMbeamrecon_Zeroed.Peak = -1.;
  66. BPMbeamrecon_Zeroed.Position = -128.;
  67. BPMbeamrecon_Zeroed.Rsqr = -1.;
  68. BPMbeamrecon_Zeroed.Skew = -128.;
  69. BPMbeamrecon_Zeroed.Position = -128.;
  70. BPMbeamrecon_Zeroed.Sum = 0.;
  71. BPMbeamrecon_Zeroed.n_channels = 0;
  72. //read board
  73. //Read!
  74. std::cout << "Reading data starting from frame: " << first_frame << std::endl;
  75. file.seekg(first_frame * sampleframe.sizeInFile(), std::ios::beg);
  76. for (int frame_nr = first_frame; frame_nr < nr_frames; frame_nr++)
  77. {
  78. eventID=frame_nr;
  79. if ((frame_nr%100000) == 0)
  80. std::cout << " Frame " << frame_nr << std::endl;
  81. file.seekg((frame_nr*increment) * sampleframe.sizeInFile() , std::ios::beg);
  82. if (sampleframe.read(&file) == 0) //read the next frame and catch if returns error
  83. {
  84. std::cerr << " ### Hitdata: Frame " << frame_nr << " could not be read! Stopping." << std::endl;
  85. file.close(); //read error, finish!
  86. return 0;
  87. }
  88. for (int boardnumber = 0; boardnumber<4; boardnumber++){
  89. board_b[boardnumber] = readboard(sampleframe,boardnumber);//a bit redundant but does some analysis
  90. // std::cout << board_b[0].integratedsignalamp << std::endl;
  91. if (boardnumber==0&&board_b[0].integratedsignalamp>1000 && board_b[0].maxchannel_amp>100.){
  92. BPMbeamrecon_0 = beamreconstruction(board_b[0], 80.); // do the linear regression fit of the beam;
  93. // std::cout << "doing regression" << std::endl;
  94. }
  95. else if (boardnumber==0) {BPMbeamrecon_0=BPMbeamrecon_Zeroed;}
  96. if (boardnumber==1&&board_b[1].integratedsignalamp>1000 && board_b[1].maxchannel_amp>100.){
  97. BPMbeamrecon_1 = beamreconstruction(board_b[1], 80.); // do the linear regression fit of the beam;
  98. // std::cout << "doing regression" << std::endl;
  99. }
  100. else if (boardnumber==1) {BPMbeamrecon_1=BPMbeamrecon_Zeroed;}
  101. if (boardnumber==2&&board_b[2].integratedsignalamp>1000 && board_b[2].maxchannel_amp>100.){
  102. BPMbeamrecon_2 = beamreconstruction(board_b[2], 80.); // do the linear regression fit of the beam;
  103. // std::cout << "doing regression" << std::endl;
  104. }
  105. else if (boardnumber==2) {BPMbeamrecon_2=BPMbeamrecon_Zeroed;}
  106. if (boardnumber==3&&board_b[3].integratedsignalamp>1000 && board_b[3].maxchannel_amp>100.){
  107. BPMbeamrecon_3 = beamreconstruction(board_b[3], 80.); // do the linear regression fit of the beam;
  108. // std::cout << "doing regression" << std::endl;
  109. }
  110. else if (boardnumber==3) {BPMbeamrecon_3=BPMbeamrecon_Zeroed;}
  111. }
  112. for (int j = 0;j<320;j++){
  113. if (board_b[0].maxchannel_amp>100.) TH2D_b0_signal_vs_channel->Fill(j, board_b[0].channel_amp[j]);
  114. if (board_b[1].maxchannel_amp>100.) TH2D_b1_signal_vs_channel->Fill(j, board_b[1].channel_amp[j]);
  115. if (board_b[2].maxchannel_amp>100.) TH2D_b2_signal_vs_channel->Fill(j, board_b[2].channel_amp[j]);
  116. if (board_b[3].maxchannel_amp>100.) TH2D_b3_signal_vs_channel->Fill(j, board_b[3].channel_amp[j]);
  117. }
  118. rootTree->Fill();
  119. }
  120. return 1;
  121. }
  122. void histograms(int fargc, char ** argv){
  123. //open output root file
  124. rootfilename = Form("%s/root/%s.root",argv[1],argv[2]);
  125. rootFile = new TFile(rootfilename,"RECREATE");
  126. if ( rootFile->IsOpen() ) {printf("ROOT file opened successfully\n");
  127. }
  128. else { printf("ROOT file failed to open. \n");}
  129. rootTree = new TTree("t","HIT Data Root Tree");
  130. rootTree ->Branch("BPMbeamrecon_0", &BPMbeamrecon_0, "Position/D:Focus:Peak:Rsqr:Skew:Kurtosis:Sum:n_channels/I");
  131. rootTree ->Branch("BPMbeamrecon_1", &BPMbeamrecon_1, "Position/D:Focus:Peak:Rsqr:Skew:Kurtosis:Sum:n_channels/I");
  132. rootTree ->Branch("BPMbeamrecon_2", &BPMbeamrecon_2, "Position/D:Focus:Peak:Rsqr:Skew:Kurtosis:Sum:n_channels/I");
  133. rootTree ->Branch("BPMbeamrecon_3", &BPMbeamrecon_3, "Position/D:Focus:Peak:Rsqr:Skew:Kurtosis:Sum:n_channels/I");
  134. rootTree ->Branch("eventID",&eventID,"eventID/I");
  135. TH2D_b0_signal_vs_channel = new TH2D("TH2D_b0_signal_vs_channel","TH2D_b0_signal_vs_channel",320,0,320,1200,-2000,20000);
  136. TH2D_b1_signal_vs_channel = new TH2D("TH2D_b1_signal_vs_channel","TH2D_b1_signal_vs_channel",320,0,320,1200,-2000,20000);
  137. TH2D_b2_signal_vs_channel = new TH2D("TH2D_b2_signal_vs_channel","TH2D_b2_signal_vs_channel",320,0,320,1200,-2000,20000);
  138. TH2D_b3_signal_vs_channel = new TH2D("TH2D_b3_signal_vs_channel","TH2D_b3_signal_vs_channel",320,0,320,1200,-2000,20000);
  139. }
  140. //Function for average
  141. double avg ( vector<Channel> v )
  142. {
  143. double return_value = 0.0;
  144. int n = v.size();
  145. for ( int i=0; i < n; i++)
  146. {
  147. return_value += v[i].chnumber;
  148. }
  149. return ( return_value / double(n));
  150. }
  151. //****************End of average funtion****************
  152. //Function for variance
  153. double variance ( vector<Channel> v , double mean )
  154. {
  155. double sum = 0.0;
  156. double temp =0.0;
  157. double var =0.0;
  158. for ( int j =0; j < v.size(); j++)
  159. {
  160. temp = pow((v[j].chnumber - mean) , 2);
  161. sum += temp;
  162. }
  163. return var = sum/double(v.size() -2);
  164. }
  165. //****************End of variance funtion****************
  166. int set_background_v2(int start_frame, int max_frames){
  167. std::cout << "Setting background levels." << std::endl;
  168. for (int j = 0; j<320; j++){
  169. for (int k = 0; k<4; k++){
  170. board_b_bkg[k].channel_amp[j] = 0.;
  171. }
  172. }
  173. //Read first record to find board configuration
  174. Fullframe sampleframe;
  175. //Read
  176. // file.seekg(sampleframe.sizeInFile(), std::ios::beg);
  177. for (int frame_nr = start_frame; frame_nr < max_frames; frame_nr++)
  178. {
  179. file.seekg(frame_nr * sampleframe.sizeInFile() , std::ios::beg);
  180. if (sampleframe.read(&file) == 0) //read the next frame and catch if returns error
  181. {
  182. std::cerr << " ### Hitdata: Frame " << frame_nr << " could not be read!" << std::endl;
  183. file.close(); //read error, finish!
  184. return 0;
  185. }
  186. for (int boardnumber = 0; boardnumber<4; boardnumber++){
  187. for (int j = 0; j<sampleframe.boards[boardnumber].nrChannels;j++){
  188. board_b_bkg[boardnumber].channel_amp[j] += sampleframe.boards[boardnumber].data[j] / double(max_frames);
  189. // std::cout << j << " " << board.channel_amp[j] << " " << dataptr->sensor_data[j] << std::endl;
  190. }
  191. }
  192. }
  193. std::cout << "Background set." << std::endl;
  194. return 1;
  195. }
  196. bpm_frame_v2 readboard(Fullframe frame, int boardnumber){
  197. bpm_frame_v2 board;
  198. board.integratedsignalamp = 0.;
  199. board.maxchannel_amp = 0.;
  200. // file.seekg(boardnumber*sizeof(BufferData)+4*frame*sizeof(BufferData));
  201. //file.read ((char*)dataptr ,sizeof(BufferData));
  202. if (frame.boards[boardnumber].syncframe.device_nr==boardnumber){
  203. for (int j = 0; j<frame.boards[boardnumber].nrChannels;j++){
  204. //subtract the background from the data
  205. if (boardnumber==0){
  206. // board_0 has the even and odd channels swapped in the hardware.
  207. if(j%2==0){
  208. board.channel_amp[j] = frame.boards[boardnumber].data[j+1] - board_b_bkg[boardnumber].channel_amp[j+1];
  209. }
  210. else{
  211. board.channel_amp[j] = frame.boards[boardnumber].data[j-1] - board_b_bkg[boardnumber].channel_amp[j-1];
  212. }
  213. }
  214. else{
  215. board.channel_amp[j] = frame.boards[boardnumber].data[j] - board_b_bkg[boardnumber].channel_amp[j];
  216. // std::cout << j << " " << board.channel_amp[j] << " " << frame.boards[boardnumber].data[j] << std::endl;
  217. }
  218. //sum the signal across channels
  219. board.integratedsignalamp += board.channel_amp[j];
  220. //find the peak channel
  221. if (board.channel_amp[j]> board.maxchannel_amp) {
  222. board.maxchannel = j;
  223. board.maxchannel_amp = board.channel_amp[j];
  224. // cout << maxchannel_b0 << " " <<maxchannelamp_b0 << endl;
  225. }
  226. //set the channel positions in mm
  227. board.channel_position[j] = 0.8*j + 0.2*(floor(j/64));
  228. //cout << board.channel_position[j] << " " << j << " " << (floor(j/64)) << endl;
  229. }
  230. }
  231. else std::cerr << "Error reading board data." << std::endl;
  232. return board;
  233. }
  234. beamRecon beamreconstruction(bpm_frame_v2 frametoanalyse, double threshold = 30.){
  235. ///////////////// linear regression using Integration by parts of gaussian function.
  236. beamRecon beam;
  237. double SumT, SumS, SumS2, SumST, SumT2, SumY, SumYS, SumYT, sigmaABC, muABC,p,c, b, b_den, b_num, SumYYP, SumYYM, MeanY;
  238. TMatrixD M1(3,3);
  239. TMatrixD M1inv(3,3);
  240. TVectorD ABC(3);
  241. TVectorD M2(3);
  242. vector<double> signal_list;
  243. vector<double> channel_list;
  244. SumY = 0.;
  245. SumS = 0.;
  246. SumT = 0.;
  247. SumS2 = 0.;
  248. SumST = 0.;
  249. SumT2 = 0.;
  250. SumYS = 0.;
  251. SumYT = 0.;
  252. b_den = 0.;
  253. b_num = 0.;
  254. b = 0.;
  255. p = 0.;
  256. c = 0.;
  257. SumYYM = 0.;
  258. SumYYP = 0.;
  259. MeanY = 0.;
  260. // const int array_length = sizeof(frametoanalyse.channel_amp)/sizeof(double);
  261. const int array_length = 320;
  262. vector<Channel> channel_reducedlist; //for anomaly detection
  263. vector<Channel> channel_reducedlistcopy; //for anomaly detection
  264. Channel tmp;
  265. int temp_lastneighbour= -128;
  266. for (int i = 0; i< array_length; i++){
  267. if (frametoanalyse.channel_amp[i]>=threshold) {
  268. // cout << "ch: " << i << endl;
  269. // signal_list.push_back(frametoanalyse.channel_amp[i]);
  270. // channel_list.push_back(frametoanalyse.channel_position[i]);
  271. tmp.amplitude = frametoanalyse.channel_amp[i];
  272. tmp.position = frametoanalyse.channel_position[i];
  273. tmp.chnumber = i;
  274. tmp.last_neighbour = temp_lastneighbour;
  275. temp_lastneighbour = i ;
  276. channel_reducedlist.push_back(tmp);
  277. if (channel_reducedlist.size()>1){
  278. channel_reducedlist[channel_reducedlist.size() - 2].next_neighbour = i;
  279. }
  280. }
  281. }
  282. //anomaly detection
  283. //remove channels without neighbours.
  284. for (int i = 0; i<channel_reducedlist.size() ;i++){
  285. if (abs(channel_reducedlist[i].chnumber - channel_reducedlist[i].last_neighbour)<=1 || abs(channel_reducedlist[i].chnumber-channel_reducedlist[i].next_neighbour)<=1 ){
  286. channel_reducedlistcopy.push_back(channel_reducedlist[i]);
  287. // cout << channel_reducedlist[i].chnumber << " " << channel_reducedlist[i].last_neighbour << " " << channel_reducedlist[i].next_neighbour << endl;
  288. }
  289. }
  290. // channel_reducedlist.clear();//empty list to reuse it.
  291. double cluster_average;
  292. double cluster_variance;
  293. if(channel_reducedlistcopy.size()>2){
  294. cluster_average = avg(channel_reducedlistcopy);
  295. cluster_variance = variance(channel_reducedlistcopy, cluster_average);
  296. // cout << cluster_average << " " << cluster_variance << endl;
  297. }
  298. //include all channels +/- 2*variance of the main cluster
  299. for (int i = 0; i< array_length; i++){
  300. if (abs(i-cluster_average)<2*cluster_variance){
  301. signal_list.push_back(frametoanalyse.channel_amp[i]);
  302. channel_list.push_back(frametoanalyse.channel_position[i]);
  303. }
  304. }
  305. // sort(channel_reducedlist.begin(),channel_reducedlist.end(),CompareChannels);
  306. const int vector_length = channel_list.size();
  307. beam.n_channels = vector_length;
  308. beam.Sum = std::accumulate(signal_list.begin(), signal_list.end(),0);
  309. if (vector_length<=3) return beam;
  310. double S[vector_length];
  311. double T[vector_length];
  312. for(int k=0; k<vector_length;k++){
  313. if (k==0){
  314. S[k]=0.; T[k]=0.;
  315. }
  316. else{
  317. S[k] = S[k-1]+0.5*( signal_list[k] + signal_list[k-1] ) * ( channel_list[k] - channel_list[k-1] );
  318. T[k] = T[k-1]+0.5*( channel_list[k] * signal_list[k] + channel_list[k-1] * signal_list[k-1] ) * ( channel_list[k] - channel_list[k-1] );
  319. }
  320. // cout << S[k] << " " << T[k] << endl;
  321. SumS += S[k]; SumT += T[k];
  322. SumY += signal_list[k];
  323. SumS2 += S[k]*S[k]; SumST += S[k]*T[k]; SumT2 += T[k]*T[k];
  324. SumYS += signal_list[k]*S[k];
  325. SumYT += signal_list[k]*T[k];
  326. MeanY+=signal_list[k];
  327. }
  328. MeanY/=vector_length;
  329. M1(0,0) = SumT2; M1(0,1) = SumST; M1(0,2) = SumT; M1(1,0) = SumST; M1(1,1) = SumS2;
  330. M1(1,2) = SumS; M1(2,0) = SumT; M1(2,1) = SumS;
  331. M1(2,2) = vector_length;
  332. M2(0) = SumYT; M2(1) = SumYS; M2(2) = SumY;
  333. M1inv = M1.Invert(); ABC = M1inv * M2;
  334. //calculate b,p,c ---> y = b*exp(-p*(x-c)*(x-c))
  335. p = -ABC(0)/2.; c = -ABC(1)/ABC(0);
  336. for(int k=0; k<vector_length;k++){
  337. b_num += exp(-p*(channel_list[k]-c)*(channel_list[k]-c)) * signal_list[k];
  338. b_den += exp(-2*p*(channel_list[k]-c)*(channel_list[k]-c));
  339. }
  340. b = b_num/b_den;
  341. beam.Position = -ABC(1)/ ABC(0);
  342. beam.Focus = 2.3548/sqrt(2*p);
  343. beam.Peak = b;
  344. beam.Rsqr = SumYYP/SumYYM;
  345. beam.Skew = gsl_stats_wskew_m_sd(&signal_list[0],1,&channel_list[0],1,vector_length,beam.Position,beam.Focus/2.3548); //skewness (symmetry)
  346. beam.Kurtosis = gsl_stats_wkurtosis_m_sd(&signal_list[0],1,&channel_list[0],1,vector_length,beam.Position,beam.Focus/2.3548); //excess kurtosis (well behaved tails)
  347. return beam;
  348. }