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.

324 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","gaus(0)+[3]");
  55. // TF1 * gausfunc2 = new TF1("gausfunc2","[0]*exp(-((x-[1])/[2])^2) +[3]*exp(-((x-[1])/[4])^2) + [5]");
  56. TGraphErrors * gausgraph;
  57. int numoverthresh=0;
  58. double maxchannelamp = 0.;
  59. double threshold = 10.;
  60. int maxchannel = 0;
  61. int numtocalc = 0;
  62. int sidenumtocalc= 0;
  63. Long64_t nentries = fChain->GetEntries();
  64. Long64_t ientry = LoadTree(1);
  65. fChain->GetEntry(1);
  66. Double_t basetimeoffset = time;
  67. Double_t channelamp[128] = {0.};
  68. Double_t abschannelamp[128] = {0.};
  69. Double_t channelamp2[128] = {0.};
  70. Double_t abschannelamp2[128] = {0.};
  71. Bool_t beamonflag = false;
  72. Int_t waitcounter = 0;
  73. Int_t lastfit = 0;
  74. Int_t lastfit2 = 0;
  75. Double_t channellist_gsl[128];
  76. Double_t channellist_gsl2[128];
  77. Long64_t nbytes = 0, nb = 0;
  78. for (Long64_t jentry=0; jentry<nentries;jentry++) {
  79. Long64_t ientry = LoadTree(jentry);
  80. if (ientry < 0) break;
  81. nb = fChain->GetEntry(jentry); nbytes += nb;
  82. // if (Cut(ientry) < 0) continue;
  83. time_1 = time-basetimeoffset;
  84. ic1_1 = ic1;
  85. ic2_1 = ic2;
  86. mw1_focusx_1 = mw1_focusx;
  87. mw1_focusy_1 = mw1_focusy;
  88. mw2_focusx_1 = mw2_focusx;
  89. mw2_focusy_1 = mw2_focusy;
  90. mw1_posx_1 = mw1_posx;
  91. mw1_posy_1 = mw1_posy;
  92. mw2_posx_1 = mw2_posx;
  93. mw2_posy_1= mw2_posy;
  94. eventid_1 = jentry;
  95. if (jentry % 1000 ==0) cout << jentry << " events completed..." << endl;
  96. //calculate a rolling average of the signal
  97. arrayavg = 0;
  98. for (int i = 1; i<length;i++){
  99. array[i-1] = array[i];
  100. }
  101. if(ic1_1>-100) array[length-1] = ic1_1 ;
  102. for (int i = 0; i<length;i++){
  103. arrayavg += array[i]/double(length);
  104. }
  105. rollingavg = double(arrayavg);
  106. // cout << rollingavg << endl;
  107. //rerun the baseline fitter using the data in front of each spill
  108. if(rollingavg>0.5 && !beamonflag && jentry>5000) {
  109. beamonflag = true; beamon = 1;
  110. cout << "Beam On: " << jentry << endl;
  111. Baseline(true,1800,int(jentry-2000),true);
  112. // bkgfunc->SetParameters( bkgfitPar0[128], bkgfitPar1[128], bkgfitPar2[128], bkgfitPar3[128]);
  113. }
  114. if(rollingavg <0.25 && beamonflag && jentry>5000 )
  115. {
  116. beamonflag = false;
  117. beamon = 0;
  118. }
  119. if (ic1_1>0.01) {totaltime+=0.100; totalcurrent+=ic1_1;}
  120. for (int ch = 4; ch < 128; ch++){
  121. bkgfunc->SetParameters( bkgfitPar0[ch], bkgfitPar1[ch], bkgfitPar2[ch], bkgfitPar3[ch]); //set the parameters for the common mode subtraction
  122. channelamp[ch] = channels[ch]- baseline[ch] - bkgfunc->Eval(time_1); //subtract the baseline
  123. if (beamon==1&&ic1_1>0.01) th2d_beamSignal_channel->Fill(ch,channelamp[ch]);
  124. if (beamon==0&&ic1_1<=0.01) th2d_bkg_channel->Fill(ch,channelamp[ch]);
  125. }
  126. // calculate mean and integral
  127. beamSignal_1 = 0.;
  128. numoverthresh=0;
  129. maxchannelamp = 0.;
  130. threshold = 20.;
  131. maxchannel = 0;
  132. // cout << (time_1-basetimeoffset)<< endl;
  133. for (int ch = 4; ch < 64; ch++){
  134. // bkgfunc->SetParameters( bkgfitPar0[ch], bkgfitPar1[ch], bkgfitPar2[ch], bkgfitPar3[ch]); //set the parameters for the common mode subtraction
  135. // channelamp[ch] = channels[ch]- baseline[ch] - bkgfunc->Eval(time_1); //subtract the baseline
  136. //channelamp_smooth[ch] = channelamp[ch];//to be smoothed later
  137. if (channelamp[ch] < 0.) {abschannelamp[ch] = 0.;}
  138. else {abschannelamp[ch] = channelamp[ch];}
  139. if (ic1_1>0.01) th2d_beamSignal_channel->Fill(ch,channelamp[ch]);
  140. if (ic1_1<=0.01) th2d_bkg_channel->Fill(ch,channelamp[ch]);
  141. if ( channelamp[ch] >threshold) {
  142. numoverthresh++;
  143. if ( channelamp[ch] > maxchannelamp) {
  144. maxchannelamp = channelamp[ch] ;
  145. maxchannel = ch;
  146. }
  147. }
  148. channellist_gsl[ch] = 0;
  149. channelamp_smooth[ch] = 0.;
  150. }
  151. numoverthresh+=4;
  152. numtocalc = 0;
  153. sidenumtocalc = 0;
  154. for (int ch = maxchannel-numoverthresh/2 ; ch < maxchannel + numoverthresh/2; ch++){
  155. if (ch>=4 && ch<=63){
  156. beamSignal_1 += channelamp[ch]; //integrate the signal around the peak channel
  157. channelamp_smooth[numtocalc] =channelamp[ch] ;
  158. channellist_gsl[numtocalc] = ch;
  159. numtocalc++;
  160. }
  161. }
  162. beamSidebandNoise_1 = 0.;
  163. for (int ch = 4; ch < maxchannel-numoverthresh/2; ch++){
  164. if (ch>=4 && ch<=63){
  165. beamSidebandNoise_1 += channelamp[ch]; //integrate the noise outside the peak
  166. sidenumtocalc++;
  167. }
  168. }
  169. for (int ch = maxchannel+numoverthresh/2; ch < 64; ch++){
  170. if (ch>=4 && ch<=63){
  171. beamSidebandNoise_1 += channelamp[ch]; //integrate the noise outside the peak
  172. sidenumtocalc++;
  173. }
  174. }
  175. beamSidebandNoise_1 = beamSidebandNoise_1 /double(sidenumtocalc)*double(numtocalc);
  176. // if (ic1_1<=5 && ic2_1<=5 && jentry<1000) { cout << beamSignal_1 << " "<<(time_1)<<" " << bkgfunc->Eval((time_1)) << endl; }
  177. // beamSignal_1 -= bkgfunc->Eval(time_1);
  178. //statistical analysis of the beam position
  179. // FFTsmoothing(channelamp_smooth);//Fourier transform smoothing
  180. beamPosX_1 = gsl_stats_wmean(channelamp_smooth,1,channellist_gsl,1,numtocalc); //calculate the weighted mean
  181. beamFocusX_1 = gsl_stats_wsd_with_fixed_mean(channelamp_smooth,1,channellist_gsl,1,numtocalc,beamPosX_1); //SD
  182. beamSkewX_1 = gsl_stats_wskew_m_sd(channelamp_smooth,1,channellist_gsl,1,numtocalc,beamPosX_1,beamFocusX_1); //skewness (symmetry)
  183. beamKurtX_1 = gsl_stats_wkurtosis_m_sd(channelamp_smooth,1,channellist_gsl,1,numtocalc,beamPosX_1,beamFocusX_1); //excess kurtosis (well behaved tails)
  184. beamNumX_1 = numtocalc;
  185. beamFocusX_1 *=2.3548;//SD-->FWHM
  186. if (ic1_1>0.01 && beamSignal_1>5) th2d_mw1_beamPosX->Fill(mw1_posx_1,beamPosX_1);
  187. if (ic1_1>0.01 && beamSignal_1>5) th2d_ic1_beamSignal->Fill(ic1_1,beamSignal_1);
  188. if (ic1_1>0.01 && beamSignal_1>5) th1d_beamSignal_ic1ratio->Fill(beamSignal_1/ic1_1);
  189. //fit with a gaussian function;
  190. if (beamon==1&&ic1_1>0.05){
  191. if (numtocalc>10&&numtocalc<50){
  192. gausfunc->SetParameters(beamSignal_1/(sqrt(2)*beamFocusX_1/2.3548),beamPosX_1,beamFocusX_1/2.3548,0.);
  193. gausfunc2->SetParameters(beamSignal_1/(sqrt(2)*beamFocusX_1/2.3548),128-beamPosX_1,beamFocusX_1/2.3548,0.);
  194. }
  195. else{
  196. gausfunc->SetParameters(500.,32.,10.,0.);
  197. gausfunc2->SetParameters(500.,92.,10.,0.);
  198. }
  199. gausfunc->SetParLimits(0,0.,10000.);
  200. gausfunc->SetParLimits(1,10.,50.);
  201. gausfunc->SetParLimits(2,0.5,30.);
  202. gausfunc->SetParLimits(3,-150.,150.);
  203. gausfunc2->SetParLimits(0,0.,10000.);
  204. gausfunc2->SetParLimits(1,64,124);
  205. gausfunc2->SetParLimits(2,0.5,30.);
  206. gausfunc2->SetParLimits(3,-150.,150.);
  207. gausgraph = new TGraphErrors(128,channellist,channelamp,errorx,errory);
  208. lastfit = gausgraph->Fit(gausfunc,"QRN","",4,60); // single gaussian fit
  209. lastfit2 = gausgraph->Fit(gausfunc2,"QRN","",68,124);// second peak
  210. beamPosX_fit = gausfunc->GetParameter(1);
  211. beamFocusX_fit =2.3548* gausfunc->GetParameter(2);
  212. beamChi2_fit = gausfunc->GetChisquare()/gausfunc->GetNDF();
  213. beamPeakX_fit = gausfunc->GetParameter(0);
  214. beamPosX_fit2 = gausfunc2->GetParameter(1);
  215. beamFocusX_fit2 =2.3548* gausfunc2->GetParameter(2);
  216. beamChi2_fit2 = gausfunc2->GetChisquare()/gausfunc2->GetNDF();
  217. beamPeakX_fit2 = gausfunc2->GetParameter(0);
  218. for (int ch = 4; ch < 64; ch++){
  219. th2d_fitdiff_channel->Fill(ch, double(channelamp[ch]-gausfunc->Eval(ch)));
  220. }
  221. for (int ch = 64; ch < 128; ch++){
  222. th2d_fit2diff_channel->Fill(ch, double(channelamp[ch]-gausfunc2->Eval(ch)));
  223. }
  224. }
  225. newdata->Fill();
  226. }
  227. TVectorD v(10);
  228. v[0] = totalcurrent;
  229. v[1] = totaltime;
  230. v.Write("icinfo");
  231. th2d_mw1_beamPosX->Write();
  232. th2d_ic1_beamSignal->Write();
  233. th1d_beamSignal_ic1ratio->Write();
  234. th2d_beamSignal_channel->Write();
  235. th2d_bkg_channel->Write();
  236. th2d_fitdiff_channel->Write();
  237. th2d_fit2diff_channel->Write();
  238. newdata->Write();
  239. }
  240. int main(int argc, char **argv)
  241. {
  242. // Working directories
  243. const char *dirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_01-12-2016/with_timestamp/";
  244. const char *pin_dirname = "/work/leverington/beamprofilemonitor/hitdata/HIT_01-12-2016/with_timestamp/pin/";
  245. const char *ext = ".root";
  246. TSystemDirectory pin_dir(pin_dirname, pin_dirname);
  247. if (true)
  248. {
  249. TSystemFile* file;
  250. TString fname = argv[1];
  251. // fname = file->GetName();
  252. // execute single PiN_run***.root
  253. if ( fname.EndsWith(ext) && !fname.BeginsWith("SAVE") && fname.BeginsWith("PiN_run"))
  254. {
  255. analyse2 *mon = new analyse2();
  256. printf("File name: %s \n", fname.Data());
  257. // Main part
  258. // Initialize(DIRName, FileName, baselineEvents, prelimEvents, beamLevel, firstFibermat, readOutFrequency in Hz, integrationTime in us)
  259. mon->Initialize(dirname, fname.Data(), 3200, 10000, 1., true, 1000., 0.);
  260. mon->Baseline(true,1000,1,true);
  261. mon->Loop(); //analysis loop
  262. // cout << th2d_mw1_beamPosX->GetCorrelationFactor() << endl;
  263. mon->Save();//save output tree file
  264. // mon->Close();//close output tree file
  265. delete mon;
  266. }
  267. }
  268. return 0;
  269. }