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.

375 lines
14 KiB

  1. #include <string.h>
  2. #include <stdio.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <utility>
  6. #include <TFile.h>
  7. #include <TTree.h>
  8. #include <TSystemDirectory.h>
  9. #include <string.h>
  10. #include <TFile.h>
  11. #include <TTree.h>
  12. #include <TH1.h>
  13. #include <TH2.h>
  14. #include <TNtuple.h>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <TRandom.h>
  18. #include <TROOT.h>
  19. #include <algorithm>
  20. #include <assert.h>
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include <time.h>
  24. #include <sys/time.h>
  25. #include <TCanvas.h>
  26. #include <TF1.h>
  27. #include <TProfile.h>
  28. #include <TVectorD.h>
  29. #include <cstdlib>
  30. #include <cmath>
  31. #include <vector>
  32. #include <TApplication.h>
  33. using namespace std;
  34. int main(int argc, char **argv)
  35. {
  36. // Working directories
  37. const char *dirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_26-11-2016/with_timestamp/";
  38. const char *listfile = NULL;
  39. int ion = atoi(argv[1]);
  40. if (ion==1) {listfile = "energylist_p";}
  41. else if (ion==2) {listfile = "energylist_he";}
  42. else if (ion==3) {listfile = "energylist_c";}
  43. else if (ion==4) {listfile = "energylist_o";}
  44. else {cerr<< argv[1] << " not allowed" << endl;
  45. cerr << "specify (1, 2, 3, or 4) for p, He4, C12, or O16" << endl;
  46. return 1;
  47. }
  48. ifstream in;
  49. ofstream ic1energyfile,ic2energyfile, beamenergyfile,beamenergyfile_iccorr, noisefile, focusfile,focusHITfile, skewfile, kurtfile,focusFitfile,chi2Fitfile;
  50. ofstream beamenergyfile_beta, peakFitfile;
  51. TFile *f;
  52. TTree *tree;
  53. Int_t nfiles = 0;
  54. Int_t EList = 0;
  55. Double_t Energy = 0.;
  56. Double_t F1 = 0.;
  57. Double_t Intensity = 0.;
  58. Double_t SP = 0.;
  59. char Filename[50];
  60. Int_t id = 0;
  61. Double_t signalmean = 0.;
  62. Double_t signalmeanerror = 0.;
  63. Double_t signalmean_corr = 0.;
  64. Double_t signalmeanerror_corr = 0.;
  65. Double_t signalmean_offsetcorr = 0.;
  66. Double_t signalmeanerror_offsetcorr = 0.;
  67. Double_t ic1mean = 0.;
  68. Double_t ic1meanerror = 0.;
  69. Double_t ic2mean = 0.;
  70. Double_t ic2meanerror = 0.;
  71. Double_t sp_air = 0.;
  72. Double_t sp_ps = 0.;
  73. Double_t sp2_air = 0.;
  74. Double_t sp2_ps = 0.;
  75. Double_t ic_beam_ratio = 0.;
  76. Double_t ic_beam_ratioerror = 0.;
  77. Double_t totalcurrent = 0.;
  78. Double_t totaltime = 0.;
  79. Double_t intcorr = 0.;
  80. Double_t icbpmcorrelation = 0.;
  81. Double_t noisemean = 0.;
  82. Double_t noiseRMS = 0.;
  83. Double_t focusmean = 0.;
  84. Double_t focusRMS = 0.;
  85. Double_t skewmean = 0.;
  86. Double_t skewRMS = 0.;
  87. Double_t kurtmean = 0.;
  88. Double_t kurtRMS = 0.;
  89. Double_t focusFitmean = 0.;
  90. Double_t focusFitRMS = 0.;
  91. Double_t chi2Fitmean = 0.;
  92. Double_t chi2FitRMS = 0.;
  93. Double_t peakFitmean = 0.;
  94. Double_t peakFitRMS = 0.;
  95. Double_t beta = 0.;
  96. Double_t gamma = 0.;
  97. Double_t zsqr = 0.;
  98. cout << "Trying to open"<< Form(": %s%s",dirname, listfile)<<"... " << endl;
  99. in.open(Form("%s%s.prn",dirname, listfile));
  100. ic1energyfile.open(Form("%s_ic1.txt",listfile ));
  101. ic2energyfile.open(Form("%s_ic1ratio.txt",listfile ));
  102. beamenergyfile.open(Form("%s_bpm1.txt",listfile ));
  103. beamenergyfile_beta.open(Form("%s_bpmbeta1.txt",listfile ));
  104. beamenergyfile_iccorr.open(Form("%s_bpm1ratio.txt",listfile ));
  105. noisefile.open(Form("%s_noise.txt",listfile ));
  106. focusfile.open(Form("%s_focus1.txt",listfile ));
  107. focusHITfile.open(Form("%s_focusHIT.txt",listfile ));
  108. skewfile.open(Form("%s_skew1.txt",listfile ));
  109. kurtfile.open(Form("%s_kurt1.txt",listfile ));
  110. focusFitfile.open(Form("%s_focusfit.txt",listfile ));
  111. chi2Fitfile.open(Form("%s_chi2fit.txt",listfile ));
  112. peakFitfile.open(Form("%s_peakfit.txt",listfile ));
  113. TF1 * f_sp_air = new TF1("f_sp_air","[0]*pow(x,[1])+[2]", 50, 250); //stopping power of protons in Air [MeV cm2 /g]
  114. f_sp_air->SetParameters(303.746, -0.873697,1.01335); //protons between 50 and 300 MeV
  115. TF1 * f_sp_ps = new TF1("f_sp_ps","[0]*pow(x,[1])+[2]", 50, 250); //stopping power of protons in polystyrene [MeV cm2 /g]
  116. f_sp_ps->SetParameters(361.936, -0.892255, 1.19133); //protons between 50 and 220 MeV
  117. TF1 * f_h2sp_air = new TF1("f_h2sp_air","[0]*pow(x,[1])+[2]", 50, 250); //stopping power of helium in air [MeV cm2 /g]
  118. f_h2sp_air->SetParameters(1236.51, -0.880225, 4.17041); //helium between 50 and 300 MeV
  119. TF1 * f_h2sp_ps = new TF1("f_h2sp_ps","[0]*pow(x,[1])+[2]", 50, 250); //stopping power of helium in polystyrene [MeV cm2 /g]
  120. f_h2sp_ps->SetParameters(1387.08, -0.882686,4.60833); //heelium between 50 and 300 MeV
  121. TF1 * f_c12sp_air = new TF1("f_c12sp_air","[0]*pow(x,[1])+[2]", 80, 480); //stopping power of carbon in air [MeV cm2 /g]
  122. f_c12sp_air->SetParameters(13291.2, -0.925464,45.3876); //carbon between 80 and 480 MeV
  123. TF1 * f_c12sp_ps = new TF1("f_c12sp_ps","[0]*pow(x,[1])+[2]", 80, 480); //stopping power of carbon in polystyrene [MeV cm2 /g]
  124. f_c12sp_ps->SetParameters(14538.9, -0.922423,49.2859); //carbon between 80 and 480 MeV
  125. TF1 * f_o16sp_air = new TF1("f_o16sp_air","[0]*pow(x,[1])+[2]", 80, 480); //stopping power of oxygen in air [MeV cm2 /g]
  126. f_o16sp_air->SetParameters(24624.9, -0.925425,80.6828); //oxygen between 80 and 480 MeV
  127. TF1 * f_o16sp_ps = new TF1("f_o16sp_ps","[0]*pow(x,[1])+[2]", 80,480); //stopping power of oxygen in polystyrene [MeV cm2 /g]
  128. f_o16sp_ps->SetParameters(26465.6, -0.928309,88.6728); //oxygen between 80 and 480 MeV
  129. ///// ic1/SP intensity correction factor = 19.8E6+/-0.1E6 particles/s per nA/(Mevcm2/g)
  130. if (!in) {
  131. cout << Form("%s%s",dirname, listfile) << " not found." << endl;
  132. return 0;}
  133. cout << Form("%s%s",dirname, listfile) << " opened." << endl;
  134. in >> EList >> Energy >> F1 >> Intensity >> SP >> Filename >> id;//prime the while loop
  135. while (!in.eof()) {
  136. TFile * file = TFile::Open(Form("%s%s",dirname,Filename));
  137. TFile * outfile = TFile::Open(Form("plots/%s_plots",Filename),"RECREATE");
  138. cout << Filename << endl;
  139. // TH1D* signalDist = 0;
  140. // file->GetObject("signalDist",signalDist);
  141. // signalmean = signalDist->GetMean();
  142. // signalmeanerror = signalDist->GetMeanError();
  143. // cout << signalmean << " " << signalmeanerror << endl;
  144. // delete signalDist;
  145. // TCanvas *c1 = new TCanvas("c1");
  146. TTree *data;
  147. file->GetObject("newdata", data);
  148. TH2D *h_beamSignal_channel = (TH2D*)file->Get("th2d_beamSignal_channel");
  149. TH2D *h_bkg_channel = (TH2D*)file->Get("th2d_bkg_channel");
  150. noisemean = h_bkg_channel->GetMean(2);
  151. noiseRMS = h_bkg_channel->GetRMS(2);
  152. TVectorD *v = (TVectorD*)file->Get("icinfo");
  153. // v->Print();
  154. totalcurrent = (*v)[0];
  155. totaltime = (*v)[1];
  156. // data->Print();
  157. TH1D* h_beamSignal_1 = new TH1D("h_beamSignal_1","h_beamSignal_1",510,-500,50000);
  158. TH1D* h_ic1_1 = new TH1D("h_ic1_1","h_ic1_1",510,-50,500);
  159. TH1D* h_ic2_1 = new TH1D("h_ic2_1","h_ic2_1",510,-50,500);
  160. TH1D* h_ic_beam_ratio = new TH1D("h_ic_beam_ratio","h_ic_beam_ratio",500,0,500.);
  161. TH2D* h_beamSignal_v_ic = new TH2D("h_beamSignal_v_ic","h_beamSignal_v_ic",510,-50,500,510,-500,50000);
  162. TH1D* h_beamFocusX_1 = new TH1D("h_beamFocusX_1","h_beamFocusX_1",200,0,50.);
  163. TH1D* h_beamSkewX_1 = new TH1D("h_beamSkewX_1","h_beamSkewX_1",200,-10,10.);
  164. TH1D* h_beamKurtX_1 = new TH1D("h_beamKurtX_1","h_beamKurtX_1",200,-10,10.);
  165. TH1D* h_beamFocusX_fit = new TH1D("h_beamFocusX_fit","h_beamFocusX_fit",200,0,50.);
  166. TH1D* h_beamChi2_fit = new TH1D("h_beamChi2_fit","h_beamChi2_fit",200,0,50.);
  167. TH1D* h_beamPeakX_fit = new TH1D("h_beamPeakX_fit","h_beamPeakX_fit",1000,0,8000.);
  168. if (id>=0&&id<=26){
  169. //protons
  170. sp_air = f_sp_air->Eval(Energy)*(Intensity/1.0E6);
  171. sp_ps = f_sp_ps->Eval(Energy)*(Intensity/1.0E6);
  172. sp2_air = f_sp_air->Eval(Energy);
  173. sp2_ps = f_sp_ps->Eval(Energy);
  174. gamma = Energy/938.272 +1.; // gamma = E_u/M_u +1
  175. beta = sqrt(1. - 1./(gamma*gamma));
  176. zsqr = 1.;
  177. }
  178. else if (id>=27&&id<=52){
  179. //helium
  180. sp_air = f_h2sp_air->Eval(Energy)*(Intensity/1.0E6);
  181. sp_ps = f_h2sp_ps->Eval(Energy)*(Intensity/1.0E6);
  182. sp2_air = f_h2sp_air->Eval(Energy);
  183. sp2_ps = f_h2sp_ps->Eval(Energy);
  184. gamma = Energy/932.1055 +1.;// gamma = E_u/M_u +1
  185. beta = sqrt(1. - 1./(gamma*gamma));
  186. zsqr = 4.;
  187. }
  188. else if (id>=53&&id<=78){
  189. //carbon
  190. sp_air = f_c12sp_air->Eval(Energy)*(Intensity/1.0E6);
  191. sp_ps = f_c12sp_ps->Eval(Energy)*(Intensity/1.0E6);
  192. sp2_air = f_c12sp_air->Eval(Energy);
  193. sp2_ps = f_c12sp_ps->Eval(Energy);
  194. gamma = Energy/932.3539 +1.;// gamma = E_u/M_u +1
  195. beta = sqrt(1. - 1./(gamma*gamma));
  196. zsqr = 36.;
  197. }
  198. else if (id>=79&&id<=99){
  199. //oxygen
  200. sp_air = f_o16sp_air->Eval(Energy)*(Intensity/1.0E6);
  201. sp_ps = f_o16sp_ps->Eval(Energy)*(Intensity/1.0E6);
  202. sp2_air = f_o16sp_air->Eval(Energy);
  203. sp2_ps = f_o16sp_ps->Eval(Energy);
  204. gamma = Energy/931.4418 +1.;// gamma = E_u/M_u +1
  205. beta = sqrt(1. - 1./(gamma*gamma));
  206. zsqr = 64.;
  207. }
  208. else {
  209. sp_air = -1.;
  210. sp_ps = -1.;
  211. sp2_air = -1.;
  212. sp2_ps = -1.;
  213. gamma = 0.;
  214. beta = 0.;
  215. zsqr = -1.;
  216. }
  217. // cout << Form("beamPeakX_fit*sqrt(2)*%F/2.3548",F1) << endl;
  218. // data->Project("h_beamSignal_1","beamSignal_1","beamon==1&&beamSignal_1>50");
  219. // data->Project("h_beamSignal_1", Form("beamPeakX_fit*sqrt(2)*%F/2.3548",F1) ,"beamon==1&&ic1_1>0.005&&beamChi2_fit<200&&beamPeakX_fit>20");
  220. data->Project("h_beamSignal_1", "beamPeakX_fit*sqrt(2)*beamFocusX_fit/2.3548" ,"beamon==1&&ic1_1>0.001&&beamChi2_fit<200&&beamPeakX_fit>20");
  221. data->Project("h_beamFocusX_1","beamFocusX_1","beamon==1&&ic1_1>0.005&&beamSignal_1>100");
  222. data->Project("h_beamSkewX_1","beamSkewX_1","beamon==1&&ic1_1>0.005&&beamSignal_1>100");
  223. data->Project("h_beamKurtX_1","beamKurtX_1","beamon==1&&ic1_1>0.005&&beamSignal_1>100");
  224. data->Project("h_beamFocusX_fit","beamFocusX_fit","beamon==1&&ic1_1>0.005&&beamChi2_fit<200&&beamPeakX_fit>20");
  225. data->Project("h_beamChi2_fit","beamChi2_fit","beamon==1&&ic1_1>0.005&&beamChi2_fit<200&&beamPeakX_fit>20");
  226. data->Project("h_beamPeakX_fit","beamPeakX_fit","beamon==1&&ic1_1>0.005&&beamChi2_fit<200&&beamPeakX_fit>20");
  227. focusmean = h_beamFocusX_1->GetMean();
  228. focusRMS = h_beamFocusX_1->GetRMS();
  229. skewmean = h_beamSkewX_1->GetMean();
  230. skewRMS = h_beamSkewX_1->GetMeanError();
  231. kurtmean = h_beamKurtX_1->GetMean();
  232. kurtRMS = h_beamKurtX_1->GetMeanError();
  233. focusFitmean = h_beamFocusX_fit->GetMean();
  234. focusFitRMS = h_beamFocusX_fit->GetRMS();
  235. chi2Fitmean = h_beamChi2_fit->GetMean();
  236. chi2FitRMS = h_beamChi2_fit->GetRMS();
  237. peakFitmean = h_beamPeakX_fit->GetMean();
  238. peakFitRMS = h_beamPeakX_fit->GetRMS();
  239. data->Project("h_ic1_1","ic1_1","ic1_1>0.01");
  240. data->Project("h_ic2_1","ic2_1","ic2_1>0.01");
  241. data->Project("h_beamSignal_v_ic","(beamPeakX_fit*sqrt(2)*beamFocusX_fit/2.3548):ic1_1","beamon==1&&ic1_1>0.005&&beamChi2_fit<200&&beamPeakX_fit>20");
  242. icbpmcorrelation = h_beamSignal_v_ic->GetCorrelationFactor();
  243. data->Project("h_ic_beam_ratio","(beamPeakX_fit*sqrt(2)*beamFocusX_fit/2.3548)/ic1_1","beamon==1&&ic1_1>0.005&&beamChi2_fit<200&&beamPeakX_fit>20");
  244. signalmean = h_beamSignal_1->GetMean();
  245. signalmeanerror = h_beamSignal_1->GetMeanError();
  246. ic1mean = h_ic1_1->GetMean();
  247. ic1meanerror = h_ic1_1->GetMeanError();
  248. ic2mean = h_ic2_1->GetMean();
  249. ic2meanerror = h_ic2_1->GetMeanError();
  250. ic_beam_ratio=h_ic_beam_ratio->GetMean();
  251. ic_beam_ratioerror=h_ic_beam_ratio->GetRMS();
  252. // intcorr = (Intensity /19.8E6) / (totalcurrent/sp2_air/totaltime);
  253. intcorr = (Intensity / 19.8E6) / (ic1mean/sp2_air)/ 3.11;
  254. cout << id << " " <<sp2_air << " " << ic1mean/sp2_air << " " << intcorr << endl;
  255. // cout << id <<" " << sp2_ps << " " <<signalmean/(Intensity/1.0E6)/(ic1mean/sp_air) << " " << signalmeanerror/(Intensity/1.0E6)/(ic1mean/sp_air) << endl;
  256. ic1energyfile << sp2_air << " " << ic1mean *intcorr/(Intensity/1.E6) << " " << (ic1meanerror/ic1mean + 0.01)*ic1mean*intcorr/(Intensity/1.E6) << " " << Filename << endl;
  257. // ic2energyfile << sp2_air << " " << ic2mean / (Intensity/1.0E6) / (0.659869 + 1.50842 * sp2_air) << " " << ic2meanerror/sp_air << " " << Filename << endl;
  258. beamenergyfile << sp2_ps << " " << signalmean*intcorr/(Intensity/1.E6) << " " << (signalmeanerror/signalmean + 0.01)*signalmean*intcorr/(Intensity/1.E6)<< endl;
  259. beamenergyfile_beta << beta << " " << signalmean*intcorr/(Intensity/1.E6)/sp2_ps << " " << (signalmeanerror/signalmean+0.01) * signalmean*intcorr/(Intensity/1.E6)/sp2_ps << endl;
  260. beamenergyfile_iccorr << sp2_ps << " " << (ic_beam_ratio*sp2_air/sp2_ps)/84.5 << " " << (ic_beam_ratioerror*sp2_air/sp2_ps)/84.5 << endl;
  261. ic2energyfile << sp2_air << " " << icbpmcorrelation << endl;
  262. noisefile << sp2_ps << " " << noisemean << " " << noiseRMS << endl;
  263. focusfile << sp2_ps << " " << focusmean*0.8 << " " << focusRMS*0.8 << endl;
  264. focusHITfile << sp2_ps << " " << F1 << endl;
  265. skewfile << sp2_ps << " " << skewmean << " " << skewRMS << endl;
  266. kurtfile << sp2_ps << " " << kurtmean << " " << kurtRMS << endl;
  267. focusFitfile << sp2_ps << " " << focusFitmean*0.8 << " " << focusFitRMS*0.8 << endl;
  268. chi2Fitfile << sp2_ps << " " << chi2Fitmean << " " << chi2FitRMS << endl;
  269. peakFitfile << sp2_ps << " " << peakFitmean << " " << peakFitRMS*0.8 << endl;
  270. // h_beamSignal_1->Draw("");
  271. // c1->SaveAs(Form("figs/h_beamsignal_%i.pdf",nfiles));
  272. // c1->Destructor();
  273. // f->Close();
  274. outfile->Write();
  275. outfile->Close();
  276. file->Close();
  277. in >> EList >> Energy >> F1 >> Intensity >> SP >> Filename >> id;
  278. // cout << EList << Energy << F1 << Intensity << SP << Filename << id << endl;
  279. nfiles++;
  280. }
  281. ic1energyfile.close();
  282. ic2energyfile.close();
  283. beamenergyfile.close();
  284. beamenergyfile_iccorr.close();
  285. beamenergyfile_beta.close();
  286. noisefile.close();
  287. focusfile.close();
  288. focusHITfile.close();
  289. skewfile.close();
  290. kurtfile.close();
  291. in.close();
  292. focusFitfile.close();
  293. chi2Fitfile.close();
  294. return 0;
  295. }