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.

327 lines
11 KiB

  1. #define analyse2_cxx
  2. #include "analyse2.h"
  3. #include <TH2.h>
  4. #include <TStyle.h>
  5. #include <TCanvas.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <iostream>
  9. #include <vector>
  10. #include <utility>
  11. #include <TFile.h>
  12. #include <TTree.h>
  13. #include <TSystemDirectory.h>
  14. #include <gsl/gsl_statistics.h>
  15. #include <math.h>
  16. #include <gsl/gsl_errno.h>
  17. #include <gsl/gsl_fft_complex.h>
  18. #include <TF1.h>
  19. #include <TGraphErrors.h>
  20. using namespace std;
  21. void analyse2::Loop()
  22. {
  23. // In a ROOT session, you can do:
  24. // Root > .L analyse2.C
  25. // Root > analyse2 t
  26. // Root > t.GetEntry(12); // Fill t data members with entry number 12
  27. // Root > t.Show(); // Show values of entry 12
  28. // Root > t.Show(16); // Read and show values of entry 16
  29. // Root > t.Loop(); // Loop on all entries
  30. //
  31. // This is the loop skeleton where:
  32. // jentry is the global entry number in the chain
  33. // ientry is the entry number in the current Tree
  34. // Note that the argument to GetEntry must be:
  35. // jentry for TChain::GetEntry
  36. // ientry for TTree::GetEntry and TBranch::GetEntry
  37. //
  38. // To read only selected branches, Insert statements like:
  39. // METHOD1:
  40. // fChain-> ve types of modulesSetBranchStatus("*",0); // disable all branches
  41. // fChain->SetBranchStatus("branchname",1); // activate branchname
  42. // METHOD2: replace line
  43. // fChain->GetEntry(jentry); //read all branches
  44. //by b_branchname->GetEntry(ientry); //read only this branch
  45. if (fChain == 0) return;
  46. Double_t totalcurrent = 0.;
  47. Double_t totaltime = 0.;
  48. const int length = 100;
  49. double array[length] = {0.};
  50. double arrayavg = 0.;
  51. TF1 * bkgfunc = new TF1("bkgfunc","[0]+[1]*TMath::Cos(x*[2]*(2*3.14159)+[3])");
  52. bkgfunc->SetParameters( bkgfitPar0[64], bkgfitPar1[64], bkgfitPar2[64], bkgfitPar3[64]);
  53. TF1 * gausfunc = new TF1("gausfunc","gaus(0)+[3]");
  54. TF1 * gausfunc2 = new TF1("gausfunc2","[0]*exp(-((x-[1])/[2])^2) +[3]*exp(-((x-[1])/[4])^2) + [5]");
  55. TGraphErrors * gausgraph;
  56. int numoverthresh=0;
  57. double maxchannelamp = 0.;
  58. double threshold = 10.;
  59. int maxchannel = 0;
  60. int numtocalc = 0;
  61. int sidenumtocalc= 0;
  62. Long64_t nentries = fChain->GetEntries();
  63. Long64_t ientry = LoadTree(1);
  64. fChain->GetEntry(1);
  65. Double_t basetimeoffset = time;
  66. Double_t channelamp[64] = {0.};
  67. Double_t abschannelamp[64] = {0.};
  68. Bool_t beamonflag = false;
  69. Int_t waitcounter = 0;
  70. Int_t lastfit = 0;
  71. Int_t lastfit2 = 0;
  72. Double_t channellist_gsl[64];
  73. Bool_t savegraph = true;
  74. Long64_t nbytes = 0, nb = 0;
  75. for (Long64_t jentry=0; jentry<nentries;jentry++) {
  76. Long64_t ientry = LoadTree(jentry);
  77. if (ientry < 0) break;
  78. nb = fChain->GetEntry(jentry); nbytes += nb;
  79. // if (Cut(ientry) < 0) continue;
  80. time_1 = time-basetimeoffset;
  81. ic1_1 = ic1;
  82. ic2_1 = ic2;
  83. mw1_focusx_1 = mw1_focusx;
  84. mw1_focusy_1 = mw1_focusy;
  85. mw2_focusx_1 = mw2_focusx;
  86. mw2_focusy_1 = mw2_focusy;
  87. mw1_posx_1 = mw1_posx;
  88. mw1_posy_1 = mw1_posy;
  89. mw2_posx_1 = mw2_posx;
  90. mw2_posy_1= mw2_posy;
  91. eventid_1 = jentry;
  92. if (jentry % 10000 ==0) cout << jentry << " events completed..." << endl;
  93. //calculate a rolling average of the signal
  94. arrayavg = 0;
  95. for (int i = 1; i<length;i++){
  96. array[i-1] = array[i];
  97. }
  98. if(ic1_1>-100) array[length-1] = ic1_1 ;
  99. for (int i = 0; i<length;i++){
  100. arrayavg += array[i]/double(length);
  101. }
  102. rollingavg = double(arrayavg);
  103. waitcounter++;
  104. // cout << rollingavg << " " << waitcounter << " " << ic1_1 << endl;
  105. //rerun the baseline fitter using the data in front of each spill
  106. if(rollingavg>1 && !beamonflag && jentry>5000) {
  107. beamonflag = true; beamon = 1;
  108. cout << "Beam On: " << jentry << endl;
  109. Baseline(true,3200,int(jentry-4000),true);
  110. bkgfunc->SetParameters( bkgfitPar0[64], bkgfitPar1[64], bkgfitPar2[64], bkgfitPar3[64]);
  111. waitcounter = 0;
  112. }
  113. if(rollingavg <0.5 && beamonflag && jentry>5000 )
  114. {
  115. beamonflag = false;
  116. beamon = 0;
  117. waitcounter = 0;
  118. }
  119. if (ic1_1>0.01) {totaltime+=0.312; totalcurrent+=ic1_1;}
  120. // calculate mean and integral
  121. beamSignal_1 = 0.;
  122. numoverthresh=0;
  123. maxchannelamp = 0.;
  124. threshold = 14;
  125. maxchannel = 0;
  126. // cout << (time_1-basetimeoffset)<< endl;
  127. for (int ch = 4; ch < 64; ch++){
  128. bkgfunc->SetParameters( bkgfitPar0[ch], bkgfitPar1[ch], bkgfitPar2[ch], bkgfitPar3[ch]); //set the parameters for the common mode subtraction
  129. channelamp[ch] = channels[ch]- baseline[ch] - bkgfunc->Eval(time_1); //subtract the baseline
  130. //channelamp_smooth[ch] = channelamp[ch];//to be smoothed later
  131. if (channelamp[ch] < 0.) {abschannelamp[ch] = 0.;}
  132. else {abschannelamp[ch] = channelamp[ch];}
  133. if (ic1_1>0.01) th2d_beamSignal_channel->Fill(ch,channelamp[ch]);
  134. if (ic1_1<=0.01) th2d_bkg_channel->Fill(ch,channelamp[ch]);
  135. if ( channelamp[ch] >threshold) {
  136. numoverthresh++;
  137. if ( channelamp[ch] > maxchannelamp) {
  138. maxchannelamp = channelamp[ch] ;
  139. maxchannel = ch;
  140. }
  141. }
  142. channellist_gsl[ch] = 0;
  143. channelamp_smooth[ch] = 0.;
  144. }
  145. numoverthresh+=4;
  146. numtocalc = 0;
  147. sidenumtocalc = 0;
  148. beamSidebandNoise_1 = 0.;
  149. // for (int ch = 4; ch < maxchannel-numoverthresh/2; ch++){
  150. // if (ch>=4 && ch<=63){
  151. // beamSidebandNoise_1 += channelamp[ch]; //integrate the noise outside the peak
  152. // sidenumtocalc++;
  153. // }
  154. // }
  155. for (int ch = maxchannel+numoverthresh/2; ch < 64; ch++){
  156. if (ch>=4 && ch<=63){
  157. beamSidebandNoise_1 += channelamp[ch]; //integrate the noise outside the peak
  158. sidenumtocalc++;
  159. }
  160. }
  161. beamSidebandNoise_1 = beamSidebandNoise_1 /double(sidenumtocalc); // channel baseline shift
  162. for (int ch = maxchannel-numoverthresh/2 ; ch < maxchannel + numoverthresh/2; ch++){
  163. if (ch>=4 && ch<=63){
  164. beamSignal_1 += channelamp[ch]; //integrate the signal around the peak channel
  165. channelamp_smooth[numtocalc] = channelamp[ch] - beamSidebandNoise_1 ;
  166. beamSignal_1 += channelamp[ch]- beamSidebandNoise_1 ; //integrate the signal around the peak channel
  167. channellist_gsl[numtocalc] = ch;
  168. numtocalc++;
  169. }
  170. }
  171. // if (ic1_1<=5 && ic2_1<=5 && jentry<1000) { cout << beamSignal_1 << " "<<(time_1)<<" " << bkgfunc->Eval((time_1)) << endl; }
  172. // beamSignal_1 -= bkgfunc->Eval(time_1);
  173. //statistical analysis of the beam position
  174. // FFTsmoothing(channelamp_smooth);//Fourier transform smoothing
  175. beamPosX_1 = gsl_stats_wmean(channelamp_smooth,1,channellist_gsl,1,numtocalc); //calculate the weighted mean
  176. beamFocusX_1 = gsl_stats_wsd_with_fixed_mean(channelamp_smooth,1,channellist_gsl,1,numtocalc,beamPosX_1); //SD
  177. beamSkewX_1 = gsl_stats_wskew_m_sd(channelamp_smooth,1,channellist_gsl,1,numtocalc,beamPosX_1,beamFocusX_1); //skewness (symmetry)
  178. beamKurtX_1 = gsl_stats_wkurtosis_m_sd(channelamp_smooth,1,channellist_gsl,1,numtocalc,beamPosX_1,beamFocusX_1); //excess kurtosis (well behaved tails)
  179. beamNumX_1 = numtocalc;
  180. beamFocusX_1 *=2.3548;//SD-->FWHM
  181. if (ic1_1>0.01 && beamSignal_1>5) th2d_mw1_beamPosX->Fill(mw1_posx_1,beamPosX_1);
  182. if (ic1_1>0.01 && beamSignal_1>5) th2d_ic1_beamSignal->Fill(ic1_1,beamSignal_1);
  183. if (ic1_1>0.01 && beamSignal_1>5) th1d_beamSignal_ic1ratio->Fill(beamSignal_1/ic1_1);
  184. //fit with a gaussian function;
  185. if (beamon==1){
  186. if (numtocalc>10&&numtocalc<50){
  187. gausfunc->SetParameters(beamSignal_1/(sqrt(2)*beamFocusX_1/2.3548),beamPosX_1,beamFocusX_1/2.3548,0.);
  188. gausfunc2->SetParameters(beamSignal_1/(sqrt(2)*beamFocusX_1/2.3548),beamPosX_1,beamFocusX_1/2.3548,beamSignal_1/(10*sqrt(2)*beamFocusX_1/2.3548),2*beamFocusX_1/2.3548,0.);
  189. }
  190. else{
  191. gausfunc->SetParameters(500.,32.,10.,0.);
  192. gausfunc2->SetParameters(500.,32,5.0,50.,10.0,0.);
  193. }
  194. gausfunc->SetParLimits(0,0.,10000.);
  195. gausfunc->SetParLimits(1,10.,50.);
  196. gausfunc->SetParLimits(2,0.5,30.);
  197. gausfunc->SetParLimits(3,-150.,150.);
  198. gausfunc2->SetParLimits(0,0.,10000.);
  199. gausfunc2->SetParLimits(1,0.,64.);
  200. gausfunc2->SetParLimits(2,0.5,50.);
  201. gausfunc2->SetParLimits(3,0.,500.);
  202. gausfunc2->SetParLimits(4,1.5,50.);
  203. gausfunc2->SetParLimits(5,-150.,150.);
  204. gausgraph = new TGraphErrors(64,channellist,channelamp,errorx,errory);
  205. lastfit = gausgraph->Fit(gausfunc,"QRN","",4,60); // single gaussian fit
  206. if (beamon && savegraph && waitcounter>5000) {
  207. gausgraph->SetName("example"); gausgraph->SetTitle("example");
  208. gausgraph->Write();
  209. gausfunc->Write();
  210. cout << "Example data saved" << endl;
  211. savegraph = false;
  212. }
  213. // lastfit2 = gausgraph->Fit(gausfunc2,"QR","",4,60);// two gaussian fit
  214. beamPosX_fit = gausfunc->GetParameter(1);
  215. beamFocusX_fit =2.3548* gausfunc->GetParameter(2);
  216. beamChi2_fit = gausfunc->GetChisquare()/gausfunc->GetNDF();
  217. beamPeakX_fit = gausfunc->GetParameter(0);
  218. beamPosX_fit2 = gausfunc2->GetParameter(1);
  219. beamFocusX_fit2 =2.3548* gausfunc2->GetParameter(2);
  220. beamFocus2X_fit2 =2.3548* gausfunc2->GetParameter(4);
  221. beamChi2_fit2 = gausfunc2->GetChisquare()/gausfunc->GetNDF();
  222. beamPeakX_fit2 = gausfunc2->GetParameter(0);
  223. beamPeak2X_fit2 = gausfunc2->GetParameter(3);
  224. for (int ch = 4; ch < 64; ch++){
  225. th2d_fitdiff_channel->Fill(ch, double(channelamp[ch]-gausfunc->Eval(ch)));
  226. th2d_fit2diff_channel->Fill(ch, double(channelamp[ch]-gausfunc2->Eval(ch)));
  227. }
  228. }
  229. newdata->Fill();
  230. }
  231. TVectorD v(10);
  232. v[0] = totalcurrent;
  233. v[1] = totaltime;
  234. v.Write("icinfo");
  235. th2d_mw1_beamPosX->Write();
  236. th2d_ic1_beamSignal->Write();
  237. th1d_beamSignal_ic1ratio->Write();
  238. th2d_beamSignal_channel->Write();
  239. th2d_bkg_channel->Write();
  240. th2d_fitdiff_channel->Write();
  241. th2d_PhaseFFT_channel->Write();
  242. th2d_AmpFFT_channel->Write();
  243. newdata->Write();
  244. }
  245. int main(int argc, char **argv)
  246. {
  247. // Working directories
  248. const char *dirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_26-11-2016/with_timestamp/";
  249. const char *pin_dirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_26-11-2016/with_timestamp/pin/";
  250. const char *ext = ".root";
  251. TSystemDirectory pin_dir(pin_dirname, pin_dirname);
  252. if (true)
  253. {
  254. TSystemFile* file;
  255. TString fname = argv[1];
  256. // fname = file->GetName();
  257. // execute single PiN_run***.root
  258. if ( fname.EndsWith(ext) && !fname.BeginsWith("SAVE") && fname.BeginsWith("PiN_run"))
  259. {
  260. analyse2 *mon = new analyse2();
  261. printf("File name: %s \n", fname.Data());
  262. // Main part
  263. // Initialize(DIRName, FileName, baselineEvents, prelimEvents, beamLevel, firstFibermat, readOutFrequency in Hz, integrationTime in us)
  264. mon->Initialize(dirname, fname.Data(), 3200, 10000, 1., true, 3000., 0.);
  265. mon->Baseline(true,1000,1,true);
  266. mon->Loop(); //analysis loop
  267. // cout << th2d_mw1_beamPosX->GetCorrelationFactor() << endl;
  268. mon->Save();//save output tree file
  269. // mon->Close();//close output tree file
  270. delete mon;
  271. }
  272. }
  273. return 0;
  274. }