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.

212 lines
7.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. const char *foldername = "/work/leverington/beamprofilemonitor/hitdata/HIT_01-12-2016";
  16. const char *dbasename = Form("%s/database.csv",foldername);
  17. const char *dirname = Form("%s/with_timestamp/",foldername);
  18. const char *dirCsv = Form("%s/hit_csv/with_timestamp/",foldername);
  19. const char *ext = ".csv";
  20. //read database
  21. string key, value;
  22. ifstream infile(dbasename, ios::in);
  23. map<string, string> database;
  24. while (infile >> key >> value)
  25. {
  26. database[key] = value;
  27. cout << value << endl;
  28. }
  29. TSystemDirectory dir(dirname, dirname);
  30. TList *files = dir.GetListOfFiles();
  31. if (files)
  32. {
  33. TSystemFile *file;
  34. TString fname;
  35. TIter next(files);
  36. while ((file = (TSystemFile *)next()))
  37. {
  38. fname = file->GetName();
  39. if (fname == "database.csv")
  40. continue;
  41. if (!file->IsDirectory() && fname.EndsWith(ext))
  42. {
  43. TTree *tree = new TTree("t", "t");
  44. cout << fname << endl;
  45. 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');
  46. // 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", '\t');
  47. TString outname = TString(dirname) + fname;
  48. outname.Replace(outname.Length() - 3, 3, TString("root"));
  49. TFile *outfile = new TFile(outname.Data(), "recreate");
  50. outfile->cd();
  51. //outfile = tree->GetCurrentFile();
  52. TTree *tree2 = new TTree("t2", "t2");
  53. fname.Replace(fname.Length() - 3, 3, TString("root"));
  54. string key = string(fname.Data());
  55. key.erase(key.end() - 5, key.end());
  56. if(database[key] == "nothing"){
  57. outfile->Close();
  58. continue;
  59. }
  60. printf("%s\n", database[key].c_str());
  61. string value = string(dirCsv) + database[key] + ".csv";
  62. printf("name1: %s , name2: %s\n", key.data(), value.data());
  63. 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');
  64. int k = 0;
  65. int count = 0;
  66. double ic1, ic2, mw1_focusx, mw1_focusy, mw2_focusx, mw2_focusy, mw1_posx, mw1_posy, mw2_posx, mw2_posy;
  67. 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;
  68. double time;
  69. double time2;
  70. tree->SetBranchAddress("time", &time);
  71. tree2->SetBranchAddress("TIME2", &time2);
  72. tree2->SetBranchAddress("IC1", &ic1);
  73. tree2->SetBranchAddress("IC2", &ic2);
  74. tree2->SetBranchAddress("MW1_FOCUSX", &mw1_focusx);
  75. tree2->SetBranchAddress("MW1_FOCUSY", &mw1_focusy);
  76. tree2->SetBranchAddress("MW2_FOCUSX", &mw2_focusx);
  77. tree2->SetBranchAddress("MW2_FOCUSY", &mw2_focusy);
  78. tree2->SetBranchAddress("MW1_POSX", &mw1_posx);
  79. tree2->SetBranchAddress("MW1_POSY", &mw1_posy);
  80. tree2->SetBranchAddress("MW2_POSX", &mw2_posx);
  81. tree2->SetBranchAddress("MW2_POSY", &mw2_posy);
  82. TBranch *bic1 = tree->Branch("ic1", &ic1_avg, "ic1/D");
  83. TBranch *bic2 = tree->Branch("ic2", &ic2_avg, "ic2/D");
  84. TBranch *bmw1_focusx = tree->Branch("mw1_focusx", &mw1_focusx_avg, "mw1_focusx/D");
  85. TBranch *bmw1_focusy = tree->Branch("mw1_focusy", &mw1_focusy_avg, "mw1_focusy/D");
  86. TBranch *bmw2_focusx = tree->Branch("mw2_focusx", &mw2_focusx_avg, "mw2_focusx/D");
  87. TBranch *bmw2_focusy = tree->Branch("mw2_focusy", &mw2_focusy_avg, "mw2_focusy/D");
  88. TBranch *bmw1_posx = tree->Branch("mw1_posx", &mw1_posx_avg, "mw1_posx/D");
  89. TBranch *bmw1_posy = tree->Branch("mw1_posy", &mw1_posy_avg, "mw1_posy/D");
  90. TBranch *bmw2_posx = tree->Branch("mw2_posx", &mw2_posx_avg, "mw2_posx/D");
  91. TBranch *bmw2_posy = tree->Branch("mw2_posy", &mw2_posy_avg, "mw2_posy/D");
  92. int currentEntryTree2 = 1;
  93. int nevents = tree->GetEntries();
  94. int nevents2 = tree2->GetEntries();
  95. ic1_avg = 0;
  96. ic2_avg = 0;
  97. mw1_focusx_avg = 0;
  98. mw1_focusy_avg = 0;
  99. mw2_focusx_avg = 0;
  100. mw2_focusy_avg = 0;
  101. mw1_posx_avg = 0;
  102. mw1_posy_avg = 0;
  103. mw2_posx_avg = 0;
  104. mw2_posy_avg = 0;
  105. time2 = 0;
  106. tree2->GetEntry(currentEntryTree2);
  107. for (int i = 0; i < nevents; i++)
  108. {
  109. tree->GetEntry(i);
  110. if (i % 10000 == 0)
  111. {
  112. printf("merging event %d ,", i);
  113. printf("Time %f \n", time);
  114. printf("Entry hit %d ,", currentEntryTree2);
  115. printf("Time hit %f \n", time2);
  116. }
  117. int count = 0;
  118. int count2 = 0;
  119. while (time2 <= (time + 0.2) && currentEntryTree2 < nevents2)
  120. {
  121. if ((time2 - time) <= 0.1 && (time2 - time) >= 0)
  122. {
  123. tree2->GetEntry(currentEntryTree2);
  124. ic1_avg += ic1;
  125. ic2_avg += ic2;
  126. count++;
  127. }
  128. tree2->GetEntry(currentEntryTree2);
  129. if ((time2 - time) <= 0.1 && (time2 - time) >= 0)
  130. {
  131. tree2->GetEntry(currentEntryTree2 - 4);
  132. mw1_focusx_avg += mw1_focusx;
  133. mw1_focusy_avg += mw1_focusy;
  134. mw2_focusx_avg += mw2_focusx;
  135. mw2_focusy_avg += mw2_focusy;
  136. mw1_posx_avg += mw1_posx;
  137. mw1_posy_avg += mw1_posy;
  138. mw2_posx_avg += mw2_posx;
  139. mw2_posy_avg += mw2_posy;
  140. count2++;
  141. }
  142. tree2->GetEntry(currentEntryTree2);
  143. currentEntryTree2++;
  144. }
  145. ic1_avg /= count;
  146. ic2_avg /= count;
  147. mw1_focusx_avg /= count2;
  148. mw1_focusy_avg /= count2;
  149. mw2_focusx_avg /= count2;
  150. mw2_focusy_avg /= count2;
  151. mw1_posx_avg /= count2;
  152. mw1_posy_avg /= count2;
  153. mw2_posx_avg /= count2;
  154. mw2_posy_avg /= count2;
  155. bic1->Fill();
  156. bic2->Fill();
  157. bmw1_focusx->Fill();
  158. bmw1_focusy->Fill();
  159. bmw2_focusx->Fill();
  160. bmw2_focusy->Fill();
  161. bmw1_posx->Fill();
  162. bmw1_posy->Fill();
  163. bmw2_posx->Fill();
  164. bmw2_posy->Fill();
  165. ic1_avg = 0;
  166. ic2_avg = 0;
  167. mw1_focusx_avg = 0;
  168. mw1_focusy_avg = 0;
  169. mw2_focusx_avg = 0;
  170. mw2_focusy_avg = 0;
  171. mw1_posx_avg = 0;
  172. mw1_posy_avg = 0;
  173. mw2_posx_avg = 0;
  174. mw2_posy_avg = 0;
  175. }
  176. tree->Write();
  177. outfile->Close();
  178. }
  179. }
  180. }
  181. return 0;
  182. }