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.

276 lines
9.9 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.50; //offset between ic and bpm readouts
  30. mwoffset = 0; // offset for timestamped event. MW chamber position correlation seems to be better in other windows
  31. timewindow = -0.099; //should be a negative number. window size in time to average over.
  32. timeoffset2 = -0.50; //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_01-12-2016/database.csv";
  44. const char *dirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_01-12-2016/with_timestamp/csv/";
  45. const char *outputdirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_01-12-2016/with_timestamp/pin/";
  46. const char *dirCsv = "/work/leverington/beamprofilemonitor/hitdata/HIT_01-12-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) + 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: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 energy;
  91. double intensity;
  92. double ionsort;
  93. double time2;
  94. tree->SetBranchAddress("time", &time);
  95. tree2->SetBranchAddress("TIME2", &time2);
  96. tree2->SetBranchAddress("IC1", &ic1);
  97. tree2->SetBranchAddress("IC2", &ic2);
  98. tree2->SetBranchAddress("MW1_FOCUSX", &mw1_focusx);
  99. tree2->SetBranchAddress("MW1_FOCUSY", &mw1_focusy);
  100. tree2->SetBranchAddress("MW2_FOCUSX", &mw2_focusx);
  101. tree2->SetBranchAddress("MW2_FOCUSY", &mw2_focusy);
  102. tree2->SetBranchAddress("MW1_POSX", &mw1_posx);
  103. tree2->SetBranchAddress("MW1_POSY", &mw1_posy);
  104. tree2->SetBranchAddress("MW2_POSX", &mw2_posx);
  105. tree2->SetBranchAddress("MW2_POSY", &mw2_posy);
  106. tree2->SetBranchAddress("ENERGY", &energy);
  107. tree2->SetBranchAddress("INTENSITY", &intensity);
  108. tree2->SetBranchAddress("ION-SORT", &ionsort);
  109. TBranch *bic1 = tree->Branch("ic1", &ic1_avg, "ic1/D");
  110. TBranch *bic2 = tree->Branch("ic2", &ic2_avg, "ic2/D");
  111. TBranch *bmw1_focusx = tree->Branch("mw1_focusx", &mw1_focusx_avg, "mw1_focusx/D");
  112. TBranch *bmw1_focusy = tree->Branch("mw1_focusy", &mw1_focusy_avg, "mw1_focusy/D");
  113. TBranch *bmw2_focusx = tree->Branch("mw2_focusx", &mw2_focusx_avg, "mw2_focusx/D");
  114. TBranch *bmw2_focusy = tree->Branch("mw2_focusy", &mw2_focusy_avg, "mw2_focusy/D");
  115. TBranch *bmw1_posx = tree->Branch("mw1_posx", &mw1_posx_avg, "mw1_posx/D");
  116. TBranch *bmw1_posy = tree->Branch("mw1_posy", &mw1_posy_avg, "mw1_posy/D");
  117. TBranch *bmw2_posx = tree->Branch("mw2_posx", &mw2_posx_avg, "mw2_posx/D");
  118. TBranch *bmw2_posy = tree->Branch("mw2_posy", &mw2_posy_avg, "mw2_posy/D");
  119. TBranch *benergy = tree->Branch("energy", &energy, "energy/D");
  120. TBranch *bintensity = tree->Branch("intensity", &intensity, "intensity/D");
  121. TBranch *bionsort = tree->Branch("ionsort", &ionsort, "ionsort/D");
  122. int currentEntryTree2 = 1;
  123. int nevents = tree->GetEntries();
  124. int nevents2 = tree2->GetEntries();
  125. ic1_avg = 0;
  126. ic2_avg = 0;
  127. mw1_focusx_avg = 0;
  128. mw1_focusy_avg = 0;
  129. mw2_focusx_avg = 0;
  130. mw2_focusy_avg = 0;
  131. mw1_posx_avg = 0;
  132. mw1_posy_avg = 0;
  133. mw2_posx_avg = 0;
  134. mw2_posy_avg = 0;
  135. time2 = 0;
  136. tree2->GetEntry(currentEntryTree2);
  137. for (int i = 0; i < nevents; i++)
  138. {
  139. tree->GetEntry(i);
  140. if (i % 10000 == 0)
  141. {
  142. printf("merging event %d ,", i);
  143. printf("Time %f \n", time);
  144. printf("Entry hit %d ,", currentEntryTree2);
  145. printf("Time hit %f \n", time2);
  146. }
  147. int count = 0;
  148. int count2 = 0;
  149. while ( time2 < time + timeoffset && currentEntryTree2 < nevents2)
  150. {
  151. if (time2 - time - timeoffset > timewindow)
  152. {
  153. tree2->GetEntry(currentEntryTree2);
  154. if (ic1>0) ic1_avg += ic1;
  155. if (ic2>0) ic2_avg += ic2;
  156. if (ic1>0) count++;
  157. // 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);
  158. energy = energy;
  159. intensity = intensity;
  160. ionsort = ionsort;
  161. }
  162. tree2->GetEntry(currentEntryTree2);
  163. if ( time2 - time - timeoffset2 > timewindow2)
  164. {
  165. tree2->GetEntry(currentEntryTree2 + mwoffset); //why currentEtryTree2-4?
  166. mw1_focusx_avg += mw1_focusx;
  167. mw1_focusy_avg += mw1_focusy;
  168. mw2_focusx_avg += mw2_focusx;
  169. mw2_focusy_avg += mw2_focusy;
  170. mw1_posx_avg += mw1_posx;
  171. mw1_posy_avg += mw1_posy;
  172. mw2_posx_avg += mw2_posx;
  173. mw2_posy_avg += mw2_posy;
  174. if (i % 2000 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f %2.3f %2.3f %i \n", count2, mw1_posx, mw2_posx, time2, time, timeoffset, time2 - time - timeoffset, mwoffset);
  175. count2++;
  176. // if (i % 2000 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  177. //if (i % 2001 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  178. // if (i % 2002 == 0) printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  179. // printf("%i %2.3f %2.3f %2.3f %2.3f\n", count2, time, time2, ic1, mw1_posx);
  180. }
  181. // currentEntryTree2++;
  182. tree2->GetEntry(currentEntryTree2);
  183. currentEntryTree2++;
  184. }
  185. if (count2>0){
  186. mw1_focusx_avg /= double(count2);
  187. mw1_focusy_avg /= double(count2);
  188. mw2_focusx_avg /= double(count2);
  189. mw2_focusy_avg /= double(count2);
  190. mw1_posx_avg /= double(count2); //the positions are weighted by the charge
  191. mw1_posy_avg /= double(count2);
  192. mw2_posx_avg /= double(count2);
  193. mw2_posy_avg /= double(count2);
  194. }
  195. if(count>0){
  196. ic1_avg /= double(count);
  197. ic2_avg /= double(count);
  198. }
  199. // if (i % 2000 == 0) cout << ic1_avg << " " << ic2_avg << endl;
  200. if (i % 2000 == 0) cout << mw1_posx_avg << " " << mw2_posx_avg << endl;
  201. bic1->Fill();
  202. bic2->Fill();
  203. bmw1_focusx->Fill();
  204. bmw1_focusy->Fill();
  205. bmw2_focusx->Fill();
  206. bmw2_focusy->Fill();
  207. bmw1_posx->Fill();
  208. bmw1_posy->Fill();
  209. bmw2_posx->Fill();
  210. bmw2_posy->Fill();
  211. bintensity->Fill();
  212. benergy->Fill();
  213. bionsort->Fill();
  214. ic1_avg = 0.;
  215. ic2_avg = 0.;
  216. mw1_focusx_avg = 0.;
  217. mw1_focusy_avg = 0.;
  218. mw2_focusx_avg = 0.;
  219. mw2_focusy_avg = 0.;
  220. mw1_posx_avg = 0.;
  221. mw1_posy_avg = 0.;
  222. mw2_posx_avg = 0.;
  223. mw2_posy_avg = 0.;
  224. }
  225. tree->Write();
  226. outfile->Close();
  227. }
  228. }
  229. return 0;
  230. }