A FLUKA simulation that records the energy deposited in a single layer of polysterene from an ion beam. HTCondor submission scripts are included.
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.

355 lines
11 KiB

3 years ago
  1. /// \ingroup tutorial_fit
  2. /// \notebook
  3. /// Convoluted Landau and Gaussian Fitting Function
  4. /// (using ROOT's Landau and Gauss functions)
  5. ///
  6. /// Based on a Fortran code by R.Fruehwirth (fruhwirth@hephy.oeaw.ac.at)
  7. ///
  8. /// to execute this example, do:
  9. ///
  10. /// ~~~{.cpp}
  11. /// root > .x langaus.C
  12. /// ~~~
  13. ///
  14. /// or
  15. ///
  16. /// ~~~{.cpp}
  17. /// root > .x langaus.C++
  18. /// ~~~
  19. ///
  20. /// \macro_image
  21. /// \macro_output
  22. /// \macro_code
  23. ///
  24. /// \authors H.Pernegger, Markus Friedl
  25. #include "TH1.h"
  26. #include "TF1.h"
  27. #include "TROOT.h"
  28. #include "TStyle.h"
  29. #include "TMath.h"
  30. #include <TH2.h>
  31. #include <TStyle.h>
  32. #include <TCanvas.h>
  33. #include <iostream>
  34. #include <fstream>
  35. #include <iomanip>
  36. #include <cstdlib>
  37. #include <cmath>
  38. #include <string.h>
  39. #include <TLorentzVector.h>
  40. #include <vector>
  41. #include <TApplication.h>
  42. #include <TAxis.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <TFile.h>
  46. #include <TTree.h>
  47. Double_t langaufun(Double_t *x, Double_t *par) {
  48. //Fit parameters:
  49. //par[0]=Width (scale) parameter of Landau density
  50. //par[1]=Most Probable (MP, location) parameter of Landau density
  51. //par[2]=Total area (integral -inf to inf, normalization constant)
  52. //par[3]=Width (sigma) of convoluted Gaussian function
  53. //
  54. //In the Landau distribution (represented by the CERNLIB approximation),
  55. //the maximum is located at x=-0.22278298 with the location parameter=0.
  56. //This shift is corrected within this function, so that the actual
  57. //maximum is identical to the MP parameter.
  58. // Numeric constants
  59. Double_t invsq2pi = 0.3989422804014; // (2 pi)^(-1/2)
  60. Double_t mpshift = -0.22278298; // Landau maximum location
  61. // Control constants
  62. Double_t np = 200.0; // number of convolution steps
  63. Double_t sc = 4.0; // convolution extends to +-sc Gaussian sigmas
  64. // Variables
  65. Double_t xx;
  66. Double_t mpc;
  67. Double_t fland;
  68. Double_t sum = 0.0;
  69. Double_t xlow,xupp;
  70. Double_t step;
  71. Double_t i;
  72. // MP shift correction
  73. mpc = par[1] - mpshift * par[0];
  74. // Range of convolution integral
  75. xlow = x[0] - sc * par[3];
  76. xupp = x[0] + 2*sc * par[3];
  77. step = (xupp-xlow) / np;
  78. // Convolution integral of Landau and Gaussian by sum
  79. for(i=1.0; i<=np/2; i++) {
  80. xx = xlow + (i-.5) * step;
  81. fland = TMath::Landau(xx,mpc,par[0]) / par[0];
  82. sum += fland * TMath::Gaus(x[0],xx,par[3]);
  83. xx = xupp - (i-.5) * step;
  84. fland = TMath::Landau(xx,mpc,par[0]) / par[0];
  85. sum += fland * TMath::Gaus(x[0],xx,par[3]);
  86. }
  87. return (par[2] * step * sum * invsq2pi / par[3]);
  88. }
  89. TF1 *langaufit(TH1D *his, Double_t *fitrange, Double_t *startvalues, Double_t *parlimitslo, Double_t *parlimitshi, Double_t *fitparams, Double_t *fiterrors, Double_t *ChiSqr, Int_t *NDF)
  90. {
  91. // Once again, here are the Landau * Gaussian parameters:
  92. // par[0]=Width (scale) parameter of Landau density
  93. // par[1]=Most Probable (MP, location) parameter of Landau density
  94. // par[2]=Total area (integral -inf to inf, normalization constant)
  95. // par[3]=Width (sigma) of convoluted Gaussian function
  96. //
  97. // Variables for langaufit call:
  98. // his histogram to fit
  99. // fitrange[2] lo and hi boundaries of fit range
  100. // startvalues[4] reasonable start values for the fit
  101. // parlimitslo[4] lower parameter limits
  102. // parlimitshi[4] upper parameter limits
  103. // fitparams[4] returns the final fit parameters
  104. // fiterrors[4] returns the final fit errors
  105. // ChiSqr returns the chi square
  106. // NDF returns ndf
  107. Int_t i;
  108. Char_t FunName[100];
  109. sprintf(FunName,"Fitfcn_%s",his->GetName());
  110. TF1 *ffitold = (TF1*)gROOT->GetListOfFunctions()->FindObject(FunName);
  111. if (ffitold) delete ffitold;
  112. TF1 *ffit = new TF1(FunName,langaufun,fitrange[0],fitrange[1],4);
  113. ffit->SetParameters(startvalues);
  114. ffit->SetParNames("Width","MP","Area","GSigma");
  115. for (i=0; i<4; i++) {
  116. ffit->SetParLimits(i, parlimitslo[i], parlimitshi[i]);
  117. }
  118. his->Fit(FunName,"RB0"); // fit within specified range, use ParLimits, do not plot
  119. ffit->GetParameters(fitparams); // obtain fit parameters
  120. for (i=0; i<4; i++) {
  121. fiterrors[i] = ffit->GetParError(i); // obtain fit parameter errors
  122. }
  123. ChiSqr[0] = ffit->GetChisquare(); // obtain chi^2
  124. NDF[0] = ffit->GetNDF(); // obtain ndf
  125. return (ffit); // return fit function
  126. }
  127. Int_t langaupro(Double_t *params, Double_t &maxx, Double_t &FWHM) {
  128. // Seaches for the location (x value) at the maximum of the
  129. // Landau-Gaussian convolute and its full width at half-maximum.
  130. //
  131. // The search is probably not very efficient, but it's a first try.
  132. Double_t p,x,fy,fxr,fxl;
  133. Double_t step;
  134. Double_t l,lold;
  135. Int_t i = 0;
  136. Int_t MAXCALLS = 10000;
  137. // Search for maximum
  138. p = params[1] - 0.1 * params[0];
  139. step = 0.05 * params[0];
  140. lold = -2.0;
  141. l = -1.0;
  142. while ( (l != lold) && (i < MAXCALLS) ) {
  143. i++;
  144. lold = l;
  145. x = p + step;
  146. l = langaufun(&x,params);
  147. if (l < lold)
  148. step = -step/10;
  149. p += step;
  150. }
  151. if (i == MAXCALLS)
  152. return (-1);
  153. maxx = x;
  154. fy = l/2;
  155. // Search for right x location of fy
  156. p = maxx + params[0];
  157. step = params[0];
  158. lold = -2.0;
  159. l = -1e300;
  160. i = 0;
  161. while ( (l != lold) && (i < MAXCALLS) ) {
  162. i++;
  163. lold = l;
  164. x = p + step;
  165. l = TMath::Abs(langaufun(&x,params) - fy);
  166. if (l > lold)
  167. step = -step/10;
  168. p += step;
  169. }
  170. if (i == MAXCALLS)
  171. return (-2);
  172. fxr = x;
  173. // Search for left x location of fy
  174. p = maxx - 0.5 * params[0];
  175. step = -params[0];
  176. lold = -2.0;
  177. l = -1e300;
  178. i = 0;
  179. while ( (l != lold) && (i < MAXCALLS) ) {
  180. i++;
  181. lold = l;
  182. x = p + step;
  183. l = TMath::Abs(langaufun(&x,params) - fy);
  184. if (l > lold)
  185. step = -step/10;
  186. p += step;
  187. }
  188. if (i == MAXCALLS)
  189. return (-3);
  190. fxl = x;
  191. FWHM = fxr - fxl;
  192. return (0);
  193. }
  194. void langaus() {
  195. // Fill Histogram
  196. /* Int_t data[100] = {10,20,50,3,10,5,2,6,11,18,18,55,90,141,255,323,454,563,681,
  197. 737,821,796,832,720,637,558,519,460,357,291,279,241,212,
  198. 153,164,139,106,95,91,76,80,80,59,58,51,30,49,23,35,28,23,
  199. 22,27,27,24,20,16,17,14,20,12,12,13,10,17,7,6,12,6,12,4,
  200. 9,9,10,3,4,5,2,4,1,5,5,1,7,1,6,3,3,3,4,5,4,4,2,2,7,2,4};
  201. TH1F *hSNR = new TH1F("snr","Signal-to-noise",400,0,400);
  202. */
  203. // for (Int_t i=0; i<100; i++) hSNR->Fill(i,data[i]);
  204. TH1D * hSNR;
  205. Double_t graph_x[30], graph_y[30], graph_xerr[30], graph_yerr[30];
  206. Double_t beta_proton[30] = {0.308525262, 0.340993523, 0.366173485, 0.386743776, 0.404197708, 0.4194131, 0.43294541, 0.445179695, 0.456345494, 0.466616582, 0.476154213, 0.48502264, 0.493348242, 0.501186212, 0.50863865, 0.515744144, 0.522562549, 0.52911069, 0.535379917, 0.541397728, 0.549575745, 0.557428612, 0.564849395, 0.571867977, 0.578541117, 0.584900169};
  207. Double_t beta_helium[30] = {0.316661966, 0.34727202, 0.371222186, 0.391037227, 0.408018455, 0.422922098, 0.436235455, 0.44827542, 0.459299095, 0.469441244, 0.478845524, 0.487649369, 0.495886825, 0.503656244, 0.510990953, 0.517959457, 0.52457247, 0.530888884, 0.536925849, 0.54269899, 0.550671248, 0.55845178, 0.565814653, 0.572798702, 0.579448698, 0.585785313};
  208. Double_t beta_carbon[30] = {0.407931067, 0.448122448, 0.479020432, 0.504000457, 0.524971648, 0.543076553, 0.559041917, 0.573329576, 0.586254563, 0.598061846, 0.608917559, 0.618952311, 0.62829287, 0.637039726, 0.645286945, 0.65311609, 0.660570813, 0.667689852, 0.67446932, 0.680943928, 0.689677353, 0.698000799, 0.70580765, 0.71315081, 0.720086739, 0.726650602};
  209. Double_t beta_oxygen[30] = {0.43638582, 0.479000679, 0.51134888, 0.537325331, 0.559061572, 0.57764689, 0.594123989, 0.608730698, 0.621997639, 0.63402408, 0.645050809, 0.655226738, 0.664724227, 0.673475951, 0.681810969, 0.689681788, 0.697243281, 0.704219104, 0.710968918, 0.717442538, 0.726111033, 0.734356548, 0.742073831, 0.749383281, 0.756156282, 0.762562424};
  210. // hSNR = h_beamSignal_b0[13];
  211. // Fitting SNR histo
  212. printf("Fitting...\n");
  213. TCanvas * c1 = new TCanvas("c1","c1", 800, 600);
  214. // Setting fit range and start values
  215. Double_t fr[2];
  216. Double_t sv[4], pllo[4], plhi[4], fp[4], fpe[4];
  217. Double_t chisqr;
  218. Int_t ndf;
  219. TF1 *fitsnr;
  220. Double_t SNRPeak, SNRFWHM;
  221. char rootfilename[50] = "";
  222. char saveplotname[50] = "";
  223. char fout_mpv_name[50] = "";
  224. Double_t norm;
  225. ofstream myfile, fout_mpv;
  226. int j = 1;
  227. myfile.open ("MPVcorrection_helium0mm05.txt");
  228. sprintf(fout_mpv_name, "jobs%i/plots/mpv.txt",j);
  229. fout_mpv.open (fout_mpv_name);
  230. for (int i = 0; i<26;i++){
  231. sprintf(rootfilename, "jobs%i/runjob%i001_eventdata_out.root",j,i);
  232. TFile *rootFile = new TFile(rootfilename,"OPEN");
  233. TH1D * hSNR = (TH1D*)rootFile->Get("h_spratio");
  234. norm = hSNR->GetEntries();
  235. hSNR->Scale(1/norm);
  236. fr[0]=hSNR->GetMean() - 2*hSNR->GetRMS();
  237. fr[1]=hSNR->GetMean() + 3*hSNR->GetRMS();
  238. pllo[0]=0.0006; pllo[1]=0.50; pllo[2]=0.001; pllo[3]=0.01;
  239. plhi[0]=0.01; plhi[1]=1.5; plhi[2]=0.055; plhi[3]=0.05;
  240. sv[0]= hSNR->GetRMS()/10.;;
  241. //sv[1]=2.1;
  242. sv[2]=0.01;
  243. // sv[3]=0.03;
  244. sv[3] = hSNR->GetRMS()/2.;
  245. sv[1] = hSNR->GetMean();
  246. fitsnr = langaufit(hSNR,fr,sv,pllo,plhi,fp,fpe,&chisqr,&ndf);
  247. langaupro(fp,SNRPeak,SNRFWHM);
  248. graph_x[i] = beta_helium[i];
  249. graph_y[i] = fp[1];
  250. graph_yerr[i] = sqrt(fitsnr->GetParError(1)*fitsnr->GetParError(1)+0.012*0.012*graph_y[i]*graph_y[i]);
  251. fout_mpv << graph_x[i] << " " << graph_y[i] << " " << graph_yerr[i] << endl;
  252. printf("Fitting done\nPlotting results...\n");
  253. myfile << i << " " << graph_y[i] << endl;
  254. // Global style settings
  255. gStyle->SetOptStat(1111);
  256. gStyle->SetOptFit(111);
  257. //gStyle->SetLabelSize(0.03,"x");
  258. //gStyle->SetLabelSize(0.03,"y");
  259. // hSNR->GetXaxis()->SetRange(0,70);
  260. hSNR->Draw();
  261. fitsnr->Draw("lsame");
  262. c1->Update();
  263. sprintf(saveplotname, "jobs%i/plots/runjob%i001_h_spratio.pdf",j,i);
  264. c1->SaveAs(saveplotname);
  265. // c1->WaitPrimitive();
  266. hSNR->Delete();
  267. rootFile->Close();
  268. }
  269. TGraphErrors * graph_1 = new TGraphErrors(23,graph_x, graph_y, 0, graph_yerr);
  270. TCanvas * c2 = new TCanvas("c2","c2", 800, 600);
  271. c2->cd();
  272. graph_1->Draw("A*");
  273. c2->SaveAs("MPVcorrection_helium0mm05.pdf");
  274. myfile.close();
  275. fout_mpv.close();
  276. }