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.

248 lines
9.1 KiB

  1. #include <string.h>
  2. #include <string>
  3. #include <TFile.h>
  4. #include <TTree.h>
  5. #include <map>
  6. #include <fstream>
  7. #include <TSystemDirectory.h>
  8. #include <iostream>
  9. using namespace std;
  10. void csvToRoot(TString filename)
  11. {
  12. }
  13. Int_t main(Int_t argc, const char *argv[])
  14. {
  15. double timeoffset;
  16. int mwoffset;
  17. double timewindow;
  18. double timeoffset2;
  19. double timewindow2;
  20. if (argc==7){
  21. timeoffset = atof(argv[2]);
  22. mwoffset = atoi(argv[3]);
  23. timewindow = atof(argv[4]);
  24. timeoffset2 = atof(argv[5]);
  25. timewindow2 = atof(argv[6]);
  26. printf("Time offsets: %f %f\n", timeoffset, timeoffset2);
  27. }
  28. else if (argc==2){
  29. timeoffset = -0.24; //offset between ic and bpm readouts
  30. mwoffset = 2; // offset for timestamped event. MW chamber position correlation seems to be better in other windows
  31. timewindow = -0.312; //should be a negative number. window size in time to average over.
  32. timeoffset2 = -0.21; //offset between ic and bpm readouts
  33. timewindow2 = -0.05; //should be a negative number. window size in time to average over.
  34. printf("Default timeoffsets:%f\n", timeoffset);
  35. printf("Default mwoffset:%i\n", mwoffset);
  36. }
  37. else {
  38. printf("./convert_batch PiN_run32.csv \n");
  39. printf("or\n");
  40. printf("./convert_batch PiN_run32.csv -0.25 2 -0.312 -0.21 0.05\n");
  41. return 1;
  42. }
  43. const char *dbasename = "/work/leverington/beamprofilemonitor/hitdata/HIT_26-11-2016/database.csv";
  44. const char *dirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_26-11-2016/with_timestamp/csv/";
  45. const char *outputdirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_26-11-2016/with_timestamp/pin/";
  46. const char *dirCsv = "/work/leverington/beamprofilemonitor/hitdata/HIT_26-11-2016/hit_csv/with_timestamp/";
  47. const char *ext = ".csv";
  48. //read database
  49. string key, value;
  50. ifstream infile(dbasename, ios::in);
  51. map<string, string> database;
  52. while (infile >> key >> value)
  53. {
  54. database[key] = value;
  55. // cout << value << endl;
  56. }
  57. TSystemDirectory dir(dirname, dirname);
  58. if (true)
  59. {
  60. TSystemFile *file;
  61. TString fname = argv[1];
  62. if (fname.EndsWith(ext))
  63. {
  64. TTree *tree = new TTree("t", "t");
  65. cout << fname << endl;
  66. //tree->ReadFile(TString(TString(dirname) + file->GetName()), "time/D:event_id:sync_out:sync_in:data_ok:led_voltage:current:ch00:ch01:ch02:ch03:ch04:ch05:ch06:ch07:ch08:ch09:ch10:ch11:ch12:ch13:ch14:ch15:ch16:ch17:ch18:ch19:ch20:ch21:ch22:ch23:ch24:ch25:ch26:ch27:ch28:ch29:ch30:ch31:ch32:ch33:ch34:ch35:ch36:ch37:ch38:ch39:ch40:ch41:ch42:ch43:ch44:ch45:ch46:ch47:ch48:ch49:ch50:ch51:ch52:ch53:ch54:ch55:ch56:ch57:ch58:ch59:ch60:ch61:ch62:ch63:ch64:ch65:ch66:ch67:ch68:ch69:ch70:ch71:ch72:ch73:ch74:ch75:ch76:ch77:ch78:ch79:ch80:ch81:ch82:ch83:ch84:ch85:ch86:ch87:ch88:ch89:ch90:ch91:ch92:ch93:ch94:ch95:ch96:ch97:ch98:ch99:ch100:ch101:ch102:ch103:ch104:ch105:ch106:ch107:ch108:ch109:ch110:ch111:ch112:ch113:ch114:ch115:ch116:ch117:ch118:ch119:ch120:ch121:ch122:ch123:ch124:ch125:ch126:ch127", '\t');
  67. tree->ReadFile(TString(TString(dirname) + fname.Data()), "time/D:event_id:sync_out:sync_in:data_ok:led_voltage:current:ch00:ch01:ch02:ch03:ch04:ch05:ch06:ch07:ch08:ch09:ch10:ch11:ch12:ch13:ch14:ch15:ch16:ch17:ch18:ch19:ch20:ch21:ch22:ch23:ch24:ch25:ch26:ch27:ch28:ch29:ch30:ch31:ch32:ch33:ch34:ch35:ch36:ch37:ch38:ch39:ch40:ch41:ch42:ch43:ch44:ch45:ch46:ch47:ch48:ch49:ch50:ch51:ch52:ch53:ch54:ch55:ch56:ch57:ch58:ch59:ch60:ch61:ch62:ch63", '\t');
  68. TString outname = TString(outputdirname) + fname ;
  69. outname.Replace(outname.Length() - 4, 4, TString(TString(argv[2])+".root"));
  70. TFile *outfile = new TFile(outname.Data(), "recreate");
  71. outfile->cd();
  72. //outfile = tree->GetCurrentFile();
  73. TTree *tree2 = new TTree("t2", "t2");
  74. fname.Replace(fname.Length() - 3, 3, TString("root"));
  75. string key = string(fname.Data());
  76. key.erase(key.end() - 5, key.end());
  77. if(database[key] == "nothing"){
  78. outfile->Close();
  79. return 1;
  80. }
  81. printf("%s\n", database[key].c_str());
  82. string value = string(dirCsv) + database[key] + ".csv";
  83. printf("name1: %s , name2: %s\n", key.data(), value.data());
  84. tree2->ReadFile(value.data(), "TIME2/D:IC1:IC2:MW1_FOCUSX:MW1_FOCUSY:MW2_FOCUSX:MW2_FOCUSY:MW1_POSX:MW1_POSY:MW2_POSX:MW2_POSY:TCS-SPAM:TCS-KO:BNEXTPOINT:ANALOG_IN1:ANALOG_IN2:ENERGY:INTENSITY:ION-SORT", '\t');
  85. int k = 0;
  86. int count = 0;
  87. double ic1, ic2, mw1_focusx, mw1_focusy, mw2_focusx, mw2_focusy, mw1_posx, mw1_posy, mw2_posx, mw2_posy;
  88. double ic1_avg, ic2_avg, mw1_focusx_avg, mw1_focusy_avg, mw2_focusx_avg, mw2_focusy_avg, mw1_posx_avg, mw1_posy_avg, mw2_posx_avg, mw2_posy_avg;
  89. double time;
  90. double time2;
  91. tree->SetBranchAddress("time", &time);
  92. tree2->SetBranchAddress("TIME2", &time2);
  93. tree2->SetBranchAddress("IC1", &ic1);
  94. tree2->SetBranchAddress("IC2", &ic2);
  95. tree2->SetBranchAddress("MW1_FOCUSX", &mw1_focusx);
  96. tree2->SetBranchAddress("MW1_FOCUSY", &mw1_focusy);
  97. tree2->SetBranchAddress("MW2_FOCUSX", &mw2_focusx);
  98. tree2->SetBranchAddress("MW2_FOCUSY", &mw2_focusy);
  99. tree2->SetBranchAddress("MW1_POSX", &mw1_posx);
  100. tree2->SetBranchAddress("MW1_POSY", &mw1_posy);
  101. tree2->SetBranchAddress("MW2_POSX", &mw2_posx);
  102. tree2->SetBranchAddress("MW2_POSY", &mw2_posy);
  103. TBranch *bic1 = tree->Branch("ic1", &ic1_avg, "ic1/D");
  104. TBranch *bic2 = tree->Branch("ic2", &ic2_avg, "ic2/D");
  105. TBranch *bmw1_focusx = tree->Branch("mw1_focusx", &mw1_focusx_avg, "mw1_focusx/D");
  106. TBranch *bmw1_focusy = tree->Branch("mw1_focusy", &mw1_focusy_avg, "mw1_focusy/D");
  107. TBranch *bmw2_focusx = tree->Branch("mw2_focusx", &mw2_focusx_avg, "mw2_focusx/D");
  108. TBranch *bmw2_focusy = tree->Branch("mw2_focusy", &mw2_focusy_avg, "mw2_focusy/D");
  109. TBranch *bmw1_posx = tree->Branch("mw1_posx", &mw1_posx_avg, "mw1_posx/D");
  110. TBranch *bmw1_posy = tree->Branch("mw1_posy", &mw1_posy_avg, "mw1_posy/D");
  111. TBranch *bmw2_posx = tree->Branch("mw2_posx", &mw2_posx_avg, "mw2_posx/D");
  112. TBranch *bmw2_posy = tree->Branch("mw2_posy", &mw2_posy_avg, "mw2_posy/D");
  113. int currentEntryTree2 = 1;
  114. int nevents = tree->GetEntries();
  115. int nevents2 = tree2->GetEntries();
  116. ic1_avg = 0;
  117. ic2_avg = 0;
  118. mw1_focusx_avg = 0;
  119. mw1_focusy_avg = 0;
  120. mw2_focusx_avg = 0;
  121. mw2_focusy_avg = 0;
  122. mw1_posx_avg = 0;
  123. mw1_posy_avg = 0;
  124. mw2_posx_avg = 0;
  125. mw2_posy_avg = 0;
  126. time2 = 0;
  127. tree2->GetEntry(currentEntryTree2);
  128. for (int i = 0; i < nevents; i++)
  129. {
  130. tree->GetEntry(i);
  131. if (i % 10000 == 0)
  132. {
  133. printf("merging event %d ,", i);
  134. printf("Time %f \n", time);
  135. printf("Entry hit %d ,", currentEntryTree2);
  136. printf("Time hit %f \n", time2);
  137. }
  138. int count = 0;
  139. int count2 = 0;
  140. while ( time2 < time + timeoffset && currentEntryTree2 < nevents2)
  141. {
  142. if (time2 - time - timeoffset > timewindow)
  143. {
  144. tree2->GetEntry(currentEntryTree2);
  145. if (ic1>0) ic1_avg += ic1;
  146. if (ic2>0) ic2_avg += ic2;
  147. if (ic1>0) count++;
  148. if (i % 2000 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f %2.3f %i \n", count, ic1, time2, time, timeoffset, time2 - time - timeoffset, mwoffset);
  149. }
  150. tree2->GetEntry(currentEntryTree2);
  151. if ( time2 - time - timeoffset2 > timewindow2)
  152. {
  153. tree2->GetEntry(currentEntryTree2 + mwoffset); //why currentEtryTree2-4?
  154. mw1_focusx_avg += mw1_focusx;
  155. mw1_focusy_avg += mw1_focusy;
  156. mw2_focusx_avg += mw2_focusx;
  157. mw2_focusy_avg += mw2_focusy;
  158. mw1_posx_avg += mw1_posx;
  159. mw1_posy_avg += mw1_posy;
  160. mw2_posx_avg += mw2_posx;
  161. mw2_posy_avg += mw2_posy;
  162. count2++;
  163. // if (i % 2000 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  164. //if (i % 2001 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  165. // if (i % 2002 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  166. // printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  167. }
  168. // currentEntryTree2++;
  169. tree2->GetEntry(currentEntryTree2);
  170. currentEntryTree2++;
  171. }
  172. if (count2>0){
  173. mw1_focusx_avg /= double(count2);
  174. mw1_focusy_avg /= double(count2);
  175. mw2_focusx_avg /= double(count2);
  176. mw2_focusy_avg /= double(count2);
  177. mw1_posx_avg /= double(count2); //the positions are weighted by the charge
  178. mw1_posy_avg /= double(count2);
  179. mw2_posx_avg /= double(count2);
  180. mw2_posy_avg /= double(count2);
  181. }
  182. if(count>0){
  183. ic1_avg /= double(count);
  184. ic2_avg /= double(count);
  185. }
  186. if (i % 2000 == 0) cout << ic1_avg << " " << ic2_avg << endl;
  187. bic1->Fill();
  188. bic2->Fill();
  189. bmw1_focusx->Fill();
  190. bmw1_focusy->Fill();
  191. bmw2_focusx->Fill();
  192. bmw2_focusy->Fill();
  193. bmw1_posx->Fill();
  194. bmw1_posy->Fill();
  195. bmw2_posx->Fill();
  196. bmw2_posy->Fill();
  197. ic1_avg = 0.;
  198. ic2_avg = 0.;
  199. mw1_focusx_avg = 0.;
  200. mw1_focusy_avg = 0.;
  201. mw2_focusx_avg = 0.;
  202. mw2_focusy_avg = 0.;
  203. mw1_posx_avg = 0.;
  204. mw1_posy_avg = 0.;
  205. mw2_posx_avg = 0.;
  206. mw2_posy_avg = 0.;
  207. }
  208. tree->Write();
  209. outfile->Close();
  210. }
  211. }
  212. return 0;
  213. }