diff --git a/Scripts_20210119/hit_analyse_v2 b/Scripts_20210119/hit_analyse_v2 index 5710b0b..9c0d4e2 100755 Binary files a/Scripts_20210119/hit_analyse_v2 and b/Scripts_20210119/hit_analyse_v2 differ diff --git a/Scripts_20210119/hit_analyse_v2.c b/Scripts_20210119/hit_analyse_v2.c index f49f33d..c132e8d 100644 --- a/Scripts_20210119/hit_analyse_v2.c +++ b/Scripts_20210119/hit_analyse_v2.c @@ -1,5 +1,7 @@ #define hit_analyse_v2_cxx #include "hit_analyse_v2.h" +#include + int main(int argc, char **argv){ @@ -22,11 +24,49 @@ int opendatafiles(int argc, char ** argv){ return 0; //file could not be opened } else {std::cout << filename << " opened successfully." << std::endl;} + + + ethercat_ts_filename = Form("%s/ethercat/SVB/timestamp/%s_channel18_timestamp.txt",argv[1],argv[2]); + ethercat_ts_file.open(ethercat_ts_filename, ifstream::in); + if (!ethercat_ts_file.is_open()) + { + std::cerr << " ### Hitdata: File could not be opened!" << ethercat_ts_filename << std::endl; + return 0; //file could not be opened + } + else {std::cout << ethercat_ts_filename << " opened successfully." << std::endl;} + + ethercat_ic1_filename = Form("%s/ethercat/SVB/%s_channel1.txt",argv[1],argv[2]); + ethercat_ic1_file.open(ethercat_ic1_filename, ifstream::in); + if (!ethercat_ic1_file.is_open()) + { + std::cerr << " ### Hitdata: File could not be opened!" << ethercat_ic1_filename << std::endl; + return 0; //file could not be opened + } + else {std::cout << ethercat_ic1_filename << " opened successfully." << std::endl;} + + ethercat_ic2_filename = Form("%s/ethercat/SVB/%s_channel2.txt",argv[1],argv[2]); + ethercat_ic2_file.open(ethercat_ic2_filename, ifstream::in); + if (!ethercat_ic2_file.is_open()) + { + std::cerr << " ### Hitdata: File could not be opened!" << ethercat_ic2_filename << std::endl; + return 0; //file could not be opened + } + else {std::cout << ethercat_ic2_filename << " opened successfully." << std::endl;} + + + bpm_ts_filename = Form("%s/root/timestamp/%s_timestamp.txt",argv[1],argv[2]); + bpm_ts_file.open(bpm_ts_filename, ifstream::in); + if (!bpm_ts_file.is_open()) + { + std::cerr << " ### Hitdata: File could not be opened!" << bpm_ts_filename << std::endl; + return 0; //file could not be opened + } + else {std::cout << bpm_ts_filename << " opened successfully." << std::endl;} + + string visualize_check = argv[5]; //plot data + if (visualize_check == "vis_true") {visualize = true;} + else{ visualize= false;} } - string visualize_check = argv[5]; //plot data - if (visualize_check == "vis_true") {visualize = true;} - else{ visualize= false;} - return 1; } @@ -35,6 +75,10 @@ int closedatafiles(){ if (file.is_open()) file.close(); // if (timestampfile.is_open()) timestampfile.close(); //if (offsetfile.is_open()) offsetfile.close(); + ethercat_ts_file.close(); + ethercat_ic1_file.close(); + ethercat_ic1_file.close(); + bpm_ts_file.close(); // rootFile->Write(); rootFile->Close(); @@ -42,11 +86,88 @@ int closedatafiles(){ } +double * timealign() +{ + + static double offsetpar[4]; + + std::vector ts_eth; + std::vector frame_eth; + + std::vector ts_bpm; + std::vector frame_bpm; + + double dummy1, dummy2; + + while (ethercat_ts_file >> dummy1 >> dummy2){ + ts_eth.push_back(dummy1); + frame_eth.push_back(dummy2); + } + + cout << "size of ts_eth: " << ts_eth.size() << endl; + + while (bpm_ts_file >> dummy1 >> dummy2){ + ts_bpm.push_back(dummy1); + frame_bpm.push_back(dummy2); + } + cout << "size of ts_bpm: " << ts_bpm.size() << endl;; + + auto [c0_eth, c1_eth] = boost::math::statistics::simple_ordinary_least_squares(frame_eth, ts_eth); + auto [c0_bpm, c1_bpm] = boost::math::statistics::simple_ordinary_least_squares(frame_bpm, ts_bpm); + + + + std::cout << "timestamp = " << c0_eth << " + " << c1_eth << "*x" << "\n"; + std::cout << "timestamp = " << c0_bpm << " + " << c1_bpm << "*y" << "\n"; + // eth_frame(x) = floor(( offsetpar[2] + offsetpar[3]*bpm_frame(y) - offsetpar[0])/offsetpar[1]); + // bpm_frame(y) = (offsetpar[0]+ offsetpar[1]*eth_frame(x) - offsetpar[2]) /offsetpar[3]; + //offset = (c0_eth - c0_bpm)*10; + offsetpar[0] = c0_eth; + offsetpar[1] = c1_eth; + offsetpar[2] = c0_bpm; + offsetpar[3] = c1_bpm; + + ethercat_ts_file.close(); + bpm_ts_file.close(); + + return offsetpar; +} + + +void readethercat() +{ + double time[2]; + double tempic1 = 0.; + double tempic2 = 0.; + while(ethercat_ic1_file >> time[0] >> tempic1){ + vec_ic1.push_back(tempic1); + } + + while(ethercat_ic2_file >> time[1] >> tempic2){ + vec_ic2.push_back(tempic2); + } + + // cout << time[0][0] << " " << ethercat.ic1 << " " << ethercat.ic2 << endl; + // cout << time[0][0] << " " << time[0][1] << " " << time[0][0]-time[0][1] << endl; + // cout << time[1][0] << " " << time[1][1] << " " << time[1][0]-time[1][1] << endl; + +} + + + int analyse(int argc, char **argv) { int first_frame = 0; // 1440000 int nr_frames = -1; int increment = 1; + double frameoffset = atof(argv[3]); + + double dummy; + + double * offset = timealign(); + int ethercatoffset = floor((offset[0]+ offset[1]*0.0 - offset[2]) /offset[3] - frameoffset); //start at the 0th ethercat frame + first_frame = ethercatoffset; // start looking in the BPM data only once the ethercat data has started + cout << "first frame: " << first_frame << endl; ///timestamp finding variables @@ -70,9 +191,9 @@ int analyse(int argc, char **argv) outfilename+="root/timestamp/"; outfilename+=argv[2]; outfilename+="_timestamp.txt"; - outfile.open(outfilename); - if (outfile.good()) {cout << outfilename << " opened successfully." << endl;} - else {cout << outfilename << " opening failed." << endl;} + // outfile.open(outfilename); + // if (outfile.good()) {cout << outfilename << " opened successfully." << endl;} + // else {cout << outfilename << " opening failed." << endl;} cout << "samples_per_50ms: " << samples_per_50ms << endl; cout << "samples_per_one_transmission :" << samples_per_one_transmission << endl; @@ -146,14 +267,31 @@ int analyse(int argc, char **argv) BPMbeamrecon_Zeroed.n_channels = 0; //read board + readethercat(); //get the relevant ethercat data;. fills two vectors for ic1 and ic2 + int ethercat_frame = 0; //Read! std::cout << "Reading data starting from frame: " << first_frame << std::endl; file.seekg(first_frame * sampleframe.sizeInFile(), std::ios::beg); for (int frame_nr = first_frame; frame_nr < nr_frames; frame_nr++) { eventID=frame_nr; - if ((frame_nr%100000) == 0) - std::cout << " Frame " << frame_nr << std::endl; + // offset = floor((c0_bpm + c1_bpm*currentframe - c0_eth)/c1_eth); + ethercatoffset = floor(( offset[2] + offset[3]*(frame_nr+frameoffset) - offset[0])/offset[1]); + + //cout << eventID << " " << ethercatoffset << " " << ( offset[2] + offset[3]*float(frame_nr+frameoffset) - offset[0] ) / offset[1] << endl; + if (ethercatoffset>=0){ + ethercat.ic1 = vec_ic1[ethercatoffset]; //not a simple 2:1 frame alignmenmt + ethercat.ic2 = vec_ic2[ethercatoffset]; + ethercat.ic1 += vec_ic1[ethercatoffset+1]; + ethercat.ic2 += vec_ic2[ethercatoffset+1]; + + } + else{ + continue; + } + + if ((frame_nr%100000) == 0) + std::cout << " Frame " << frame_nr << std::endl; file.seekg((frame_nr*increment) * sampleframe.sizeInFile() , std::ios::beg); if (sampleframe.read(&file) == 0) //read the next frame and catch if returns error @@ -236,7 +374,7 @@ int analyse(int argc, char **argv) //fill with the middle values where the bits should be byte_data.clear(); bit_pattern_to_test.clear(); - cout << transmission_start_in_samples << ": bit pattern[40] "; + // cout << transmission_start_in_samples << ": bit pattern[40] "; int count=0; for (int byte_nr =0; byte_nr<4; byte_nr++){ for (int bit_nr =0; bit_nr<10; bit_nr++){ @@ -254,7 +392,7 @@ int analyse(int argc, char **argv) bit_pattern_to_test[20]==0 && bit_pattern_to_test[29]==1 && bit_pattern_to_test[30]==0 && bit_pattern_to_test[39]==1){ //then - cout << "Timestamp found at entry " << transmission_start_in_samples << endl; + // cout << "Timestamp found at entry " << transmission_start_in_samples << endl; //fill byte for (int jbyte_nr =0; jbyte_nr<4; jbyte_nr++){ @@ -276,10 +414,14 @@ int analyse(int argc, char **argv) block_value+=byte_data[jbyte]*byte_multipliers[jbyte]; } block_data.push_back(block_value); - if ( block_nr>1) {printf("Block %i at %1.1i: %f ms Delta=%f ms\n", block_nr, block_positions[block_nr-1], block_data[block_nr-1],block_data[block_nr-1]- block_data[block_nr-2]);} - else { printf("Block %i at %1.1i: %f ms\n", block_nr, block_positions[block_nr-1], block_data[block_nr-1]); } + if ( block_nr>1) { + // printf("Block %i at %1.1i: %f ms Delta=%f ms\n", block_nr, block_positions[block_nr-1], block_data[block_nr-1],block_data[block_nr-1]- block_data[block_nr-2]); + } + else { + // printf("Block %i at %1.1i: %f ms\n", block_nr, block_positions[block_nr-1], block_data[block_nr-1]); + } - outfile << std::setprecision(11) << block_data[block_nr-1] << " " << block_positions[block_nr-1] << endl; + // outfile << std::setprecision(11) << block_data[block_nr-1] << " " << block_positions[block_nr-1] << endl; } else{ cout << "Bad Block at entry " << transmission_start_in_samples << endl; @@ -325,6 +467,7 @@ void histograms(int fargc, char ** argv){ //rootTree ->Branch("board_2", &board_b[2], "channel_amp[320]/D:channel_position[320]:avg_position:avg_width:integratedsignalamp:max_channel_amp:maxchannel/I:nrChannels:board_number:sma_state"); //rootTree ->Branch("board_3", &board_b[3], "channel_amp[320]/D:channel_position[320]:avg_position:avg_width:integratedsignalamp:max_channel_amp:maxchannel/I:nrChannels:board_number:sma_state"); + rootTree ->Branch("ethercat",ðercat,"ic1/D:ic2"); rootTree ->Branch("eventID",&eventID,"eventID/I"); TH2D_b0_signal_vs_channel = new TH2D("TH2D_b0_signal_vs_channel","TH2D_b0_signal_vs_channel",320,0,320,1200,-2000,20000); diff --git a/Scripts_20210119/hit_analyse_v2.h b/Scripts_20210119/hit_analyse_v2.h index d1e4425..dcc5e2e 100644 --- a/Scripts_20210119/hit_analyse_v2.h +++ b/Scripts_20210119/hit_analyse_v2.h @@ -37,9 +37,10 @@ int analyse(int argc, char **argv); int opendatafiles(int argc, char ** argv); void histograms(int argc, char ** argv); +double * timealign(); int closedatafiles(); int set_background_v2(int start_frame, int frames); - +void readethercat(); //////global variables @@ -53,7 +54,12 @@ char * rootfilename; char * offsetfilename; ifstream offsetfile; ofstream outfile; +char * ethercat_ts_filename; +char * bpm_ts_filename; +char * ethercat_ic1_filename; +char * ethercat_ic2_filename; +ifstream ethercat_ts_file, bpm_ts_file, ethercat_ic2_file, ethercat_ic1_file;; struct bpm_frame_v2 { @@ -90,6 +96,17 @@ struct beamRecon { }; +struct ethercatdata{ + double ic1; + double ic2; +}; + +ethercatdata ethercat; +vector vec_ic1; +vector vec_ic2; + + + beamRecon beamreconstruction(bpm_frame_v2 frametoanalyse, double threshold); beamRecon BPMbeamrecon_0,BPMbeamrecon_1,BPMbeamrecon_2,BPMbeamrecon_3;