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.

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