Angular analysis of B+->K*+(K+pi0)mumu
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.

1324 lines
67 KiB

  1. //Renata Kopecna
  2. #include <momfit.hh>
  3. #include <fitter.hh>
  4. #include <folder.hh>
  5. #include <paths.hh>
  6. #include <funcs.hh>
  7. #include <design.hh>
  8. #include <event.hh>
  9. #include <constants.hh>
  10. #include <bu2kstarmumu_pdf.hh>
  11. #include <bu2kstarmumu_plotter.hh>
  12. #include <bu2kstarmumu_parameters.hh>
  13. #include <bu2kstarmumu_generator.hh>
  14. #include <spdlog.h>
  15. #include <TRandom3.h>
  16. #include <TTree.h>
  17. //background coefficients, obtained from fits to the sideband data of the rare channel (David thinks)
  18. //4 cause David had 4 PDFs
  19. const double Bctl1[4] = { 0.11700, -0.0568, 0.0367, 0.0160};
  20. const double Bctl2[4] = {-0.0697, -0.1243, -0.0788, -0.1197};
  21. const double Bctk1[4] = { 0.0720, 0.4544, 0.0761, 0.2105};
  22. const double Bctk2[4] = { 0.1652, 0.2065, 0.1177, 0.1951};
  23. //Obtained from MC //TODO //Taken from main fit, update when fixed
  24. const double f_m_B[4] = {5282.32, 5282.08, 5278.57, 5279.48};
  25. const double f_lambda1[4] = {-0.00386, -0.00610, -0.00362, -0.00578};
  26. const double f_lambda2[4] = {-0.03320, -0.00120, -0.04600, -0.00100};
  27. const double f_alpha_1[4] = { 1.7910, 1.8000, 1.6510, 1.5400};
  28. const double f_alpha_2[4] = { 2.3200, 2.4300, 2.0100, 2.2500};
  29. const double f_n_1[4] = { 1.4400, 1.4900, 1.6800, 1.6100};
  30. const double f_n_2[4] = { 2.8900, 3.7000, 3.8700, 3.7000};
  31. const double f_sigma_1[4] = {14.8300, 15.4800, 14.8400, 14.4};
  32. const double f_sigma_2[4] = {14.0700, 13.1000, 15.6200, 13.3};
  33. const double f_f_CB[4] = { 0.6360, 0.3600, 0.3040, 0.6200};
  34. const double f_m_scale[4] = { 1.1814, 1.0648, 1.1547, 1.1168};
  35. //Obtained from MC //TODO //Taken from main fit, update when fixed
  36. ///including the first q2bin: //TODO: get from my MC
  37. const std::vector<double> FL_scale = {0.4836193455050577, 1.2251014138607774, 1.274499551405837, 1.1368614009399862};
  38. //Obtained from MC //TODO //Taken from main fit, update when fixed
  39. const std::vector<double> FL_scale_toys = { //TODO Get from my MC!
  40. 0.5, //scaled from average of bin 1-4
  41. 1.0 + 0.07 / (61.*4) * (61.+ 62.+ 96.+ 125.),
  42. 1.0 + 0.12 / (62.*4) * (61.+ 62.+ 96.+ 125.),
  43. 1.0 - 0.02 / (96.*4) * (61.+ 62.+ 96.+ 125.),
  44. 1.0 - 0.17 / (125.*4) * (61.+ 62.+ 96.+ 125.),
  45. 1.0,//evaluated by itself
  46. 1.0 + 0.04 / (129. * 2) * (129. + 69.),
  47. 1.0 - 0.04 / (69. * 2) * (129. + 69.),
  48. };
  49. //TODO: not used at the moment, check for constants and everything before usage
  50. int momfit(fcnc::options opts,
  51. bool fitReference, bool fitSignalMC, bool fitData, bool blind,
  52. bool Fit1bin, bool Fit2bins, bool FitAllbins){
  53. if (fitReference) spdlog::info("[MOM]\t\tMoM from reference channel: J/psi K*+");
  54. // (jobs_to_do[7]) //reference channel only
  55. if (fitSignalMC) spdlog::info("[MOM]\t\tMoM from signal fitSignalMC");
  56. //if(jobs_to_do[8]) //signal fitSignalMC only
  57. if (fitData) spdlog::info("[MOM]\t\tMoM signal data");
  58. // if(jobs_to_do[9]) //signal data << FINAL FIT!!
  59. //const bool blind = jobs_to_do[9];
  60. //have extra option for the usage of binning:
  61. const bool SimultaneousMoM = fitReference;
  62. const bool SimultaneousFit = true;
  63. const bool UseAccPerRun = true;
  64. const bool UseAccPer2Years = true;
  65. const bool LargeBinning = !fitSignalMC && !fitReference && !fitData;
  66. const bool WeightedFit = true;
  67. const bool OnlyBackground = false;
  68. const bool OnlySignal = false;
  69. const bool OneFit = !fitSignalMC && !fitReference && !SimultaneousFit;
  70. const bool FitR = fitReference;
  71. const bool SystematicsWithToys = opts.systematic == 0;
  72. if (!fitSignalMC && SystematicsWithToys){
  73. spdlog::critical("Cannot run data AND generate toys for systematics!");
  74. return 2; //TODO: decide what error is for wrong parameters
  75. }
  76. const bool FitFinalToySamples = false;
  77. const bool FSfrom2DLargeBins = !fitSignalMC && !fitReference && !LargeBinning && !SystematicsWithToys && !FitFinalToySamples;
  78. const bool UseExternalFS = true;
  79. const bool FixFStoStart = false;
  80. const bool FitOnlyMagDown = opts.job_id == 3 && !SystematicsWithToys;
  81. const bool FitOnlyMagUp = opts.job_id == 4 && !SystematicsWithToys;
  82. const bool LargerMKpiRange = false;
  83. const bool NP = false;
  84. //TODO: big time, go through the couts and the constants here
  85. if(!fitReference && !fitSignalMC) opts.plots_m_bins = 50;
  86. assert(!OneFit || !SimultaneousMoM);
  87. if(fitReference){
  88. opts.TheQ2binsmin = {8.68};
  89. opts.TheQ2binsmax = {10.09};
  90. }
  91. else if(LargeBinning){
  92. opts.TheQ2binsmin = {1.0, 11.0, 15.0};
  93. opts.TheQ2binsmax = {8.0, 12.5, 19.0};
  94. }
  95. if(fitSignalMC && !fitReference){ //Fit also Jpsi region in fitSignalMC
  96. opts.TheQ2binsmin.push_back(8.68);
  97. opts.TheQ2binsmax.push_back(10.09);
  98. }
  99. if(LargerMKpiRange){
  100. opts.mkpi_min -= 50.;
  101. opts.mkpi_max += 50.;
  102. }
  103. opts.write_eps = !(FitOnlyMagDown || FitOnlyMagUp || SystematicsWithToys || FitFinalToySamples);
  104. opts.write_C = !(FitOnlyMagDown || FitOnlyMagUp || SystematicsWithToys || FitFinalToySamples);
  105. //configurate for 2D mass fit to obtain sWeights
  106. opts.only_mass2DFit = true;
  107. opts.only_angles = false;
  108. opts.only_Bmass = false;
  109. opts.squared_hesse = !fitData;
  110. opts.minos_errors = false;
  111. opts.weighted_fit = WeightedFit;
  112. opts.fit_mkpi = true;
  113. opts.use_mkpi = false;
  114. opts.generate_mkpi = true;
  115. opts.simple_mkpi = false;
  116. opts.isobar = false;
  117. opts.shift_lh = false;
  118. opts.swave = (opts.use_mkpi || opts.fit_mkpi) && !(fitSignalMC || OnlyBackground);
  119. opts.extended_ml = !fitSignalMC && !OnlyBackground && !OnlySignal; //extended maximum likelyhood
  120. //Needed for sWeights
  121. opts.fit_fl = !fitData && SystematicsWithToys;
  122. opts.fit_afb = opts.fit_fl;
  123. opts.full_angular = true;
  124. //how many individual pdfs are used?
  125. const UInt_t nPDFs = 2;
  126. std::vector< std::string > samplename;
  127. samplename.push_back("Run1 DD"); //TODO
  128. samplename.push_back("Run1 LL");
  129. samplename.push_back("Run2 DD");
  130. samplename.push_back("Run2 LL");
  131. //load events from data tuple:
  132. std::vector<fcnc::event>events[nPDFs];
  133. //TODO
  134. if(FitFinalToySamples){
  135. events[0] = fcnc::load_events(get_finalToySamplePath(0,opts.job_id,fitReference), "Events", -1); //Run1
  136. events[1] = fcnc::load_events(get_finalToySamplePath(1,opts.job_id,fitReference), "Events", -1); //Run2
  137. }
  138. else{
  139. events[0] = fcnc::load_events(get_theFCNCpath(fitSignalMC,1), "Events", -1); //Run1
  140. events[1] = fcnc::load_events(get_theFCNCpath(fitSignalMC,2), "Events", -1); //Run2
  141. }
  142. spdlog::info("Events:");
  143. for(UInt_t n = 0; n < nPDFs; n++){
  144. std::cout << samplename.at(n) << ":\t" << (blind ? "Larger 1 " : std::to_string(events[n].size())) << std::endl;
  145. assert(events[n].size() || SystematicsWithToys);
  146. }
  147. TRandom3 * rnd = new TRandom3(0);
  148. //determine number of bins:
  149. const UInt_t nBins = opts.TheQ2binsmin.size();
  150. std::vector<int>fit_results[nBins];
  151. std::vector<double>fitted_FS[nBins];
  152. std::vector<double>fitted_FSerr[nBins];
  153. std::vector<double>fitted_FSerrUp[nBins];
  154. std::vector<double>fitted_FSerrDown[nBins];
  155. //signal event number estimations:
  156. double N_sig_Rare[8] = {101., 61., 62., 96., 125., 124., 129., 69.};
  157. double N_bkg_Rare[8] = {131., 206., 279., 358., 358., 247., 158., 93.};
  158. //estimation of how many events lie within each sub-set: Run I DD, Run I LL, Run II DD, Run II LL. taken from jpsi!
  159. double ev_frac[4] = {0.14, 0.09, 0.46, 0.30};
  160. if(OneFit)
  161. ev_frac[0] = 1.0;
  162. //TODO whyyyy is this all here
  163. std::vector<double> s1s, s1c, s2s, s2c, s3, s4, s5, s6s, s6c, s7, s8, s9;
  164. if(nBins == 9){
  165. s1s = { 0.5010, 0.1660, 0.1270, 0.1760, 0.2570, 0.3910, 0.4670, 0.4930, 0.329};
  166. s1c = { 0.2450, 0.6996, 0.7993, 0.7384, 0.6414, 0.4201, 0.3513, 0.3344, 0.000};
  167. s2s = { 0.1448, 0.0749, 0.0514, 0.0662, 0.0900, 0.1449, 0.1620, 0.1662, 0.000};
  168. s2c = {-0.1953,-0.6639,-0.7774,-0.7254,-0.6334,-0.4171,-0.3497,-0.3332, 0.000};
  169. s3 = { 0.0110,-0.0030, 0.0010,-0.0100,-0.0330,-0.0520,-0.1460,-0.2240,-0.006};
  170. s4 = { 0.0050, 0.0100,-0.1140,-0.1970,-0.2530,-0.2620,-0.2910,-0.2980,-0.244};
  171. s5 = { 0.2330, 0.0730,-0.1630,-0.3100,-0.4130,-0.4430,-0.3410,-0.2570, 0.000};
  172. s6s = {-0.0650,-0.1950,-0.0860, 0.0950, 0.2930, 0.4920, 0.5410, 0.4650, 0.000};
  173. s6c = { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.000};
  174. s7 = { 0.0030, 0.0350, 0.0120, 0.0080,-0.0110, 0.0150,-0.0140, 0.0130, 0.000};
  175. s8 = {-0.0050, 0.0060,-0.0040,-0.0050,-0.0120, 0.0160, 0.0010, 0.0060,-0.060};
  176. s9 = {-0.0220, 0.0010,-0.0020,-0.0090, 0.0130, 0.0010, 0.0100, 0.0000,-0.084};
  177. }
  178. else if(nBins == 8){
  179. //results from signal channel generator level fitSignalMC fit
  180. s1s = { 0.5120, 0.1800, 0.1370, 0.1930, 0.2750, 0.4170, 0.4840, 0.5030};
  181. s1c = { 0.2450, 0.6996, 0.7993, 0.7384, 0.6414, 0.4201, 0.3513, 0.3344};
  182. s2s = { 0.1448, 0.0749, 0.0514, 0.0662, 0.0900, 0.1449, 0.1620, 0.1662};
  183. s2c = {-0.1953,-0.6639,-0.7774,-0.7254,-0.6334,-0.4171,-0.3497,-0.3332};
  184. s3 = { 0.0000, 0.0020,-0.0080,-0.0120,-0.0240,-0.0620,-0.1520,-0.2330};
  185. s4 = { 0.0870, 0.0040,-0.1080,-0.1950,-0.2510,-0.2770,-0.2900,-0.3040};
  186. s5 = { 0.2440, 0.0950,-0.1590,-0.3050,-0.4140,-0.4210,-0.3400,-0.2490};
  187. s6s = {-0.1270,-0.2070,-0.0860, 0.0950, 0.3020, 0.5140, 0.5570, 0.4700};
  188. s6c = { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000};
  189. s7 = {-0.0060,-0.0100,-0.0030, 0.0020, 0.0030,-0.0020,-0.0020, 0.0050};
  190. s8 = { 0.0010,-0.0030,-0.0040,-0.0030,-0.0010, 0.0030,-0.0010, 0.0030};
  191. s9 = {-0.0020, 0.0000, 0.0020, 0.0040,-0.0020,-0.0010,-0.0010, 0.0010};
  192. /* OLD MoM start values
  193. s1s = { 0.5010, 0.1660, 0.1270, 0.1760, 0.2570, 0.3910, 0.4670, 0.4930};
  194. s1c = { 0.2450, 0.6996, 0.7993, 0.7384, 0.6414, 0.4201, 0.3513, 0.3344};
  195. s2s = { 0.1448, 0.0749, 0.0514, 0.0662, 0.0900, 0.1449, 0.1620, 0.1662};
  196. s2c = {-0.1953,-0.6639,-0.7774,-0.7254,-0.6334,-0.4171,-0.3497,-0.3332};
  197. s3 = { 0.0110,-0.0030, 0.0010,-0.0100,-0.0330,-0.0520,-0.1460,-0.2240};
  198. s4 = { 0.0050, 0.0100,-0.1140,-0.1970,-0.2530,-0.2620,-0.2910,-0.2980};
  199. s5 = { 0.2330, 0.0730,-0.1630,-0.3100,-0.4130,-0.4430,-0.3410,-0.2570};
  200. s6s = {-0.0650,-0.1950,-0.0860, 0.0950, 0.2930, 0.4920, 0.5410, 0.4650};
  201. s6c = { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000};
  202. s7 = { 0.0030, 0.0350, 0.0120, 0.0080,-0.0110, 0.0150,-0.0140, 0.0130};
  203. s8 = {-0.0050, 0.0060,-0.0040,-0.0050,-0.0120, 0.0160, 0.0010, 0.0060};
  204. s9 = {-0.0220, 0.0010,-0.0020,-0.0090, 0.0130, 0.0010, 0.0100, 0.0000};
  205. */
  206. //TODO whyyyy is this all here
  207. if(NP){
  208. s1s = {0.5602306113691828,0.23270880313206915,0.1882591401081556,0.2360087282257996,0.3049337611889009,0.4271747118350757,0.49093645131734026,0.5057945417334112};
  209. s1c = { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000};
  210. s2s = { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000};
  211. s2c = { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000};
  212. s3 = {0.010889127654682522,0.002826464227113492,-0.009846207821458525,-0.0236075131233653,-0.03873716522303356,-0.08541706129465602,-0.17283544777091792,-0.2508649715871772};
  213. s4 = {0.06515621731101214,-0.03441408879881635,-0.14972587209244403,-0.22058159609322783,-0.2569091889106349,-0.28043043274306023,-0.2933132627545414,-0.3090558271133447};
  214. s5 = {0.28355403122517436,0.15060676750257493,-0.08697300891551848,-0.2453363631814247,-0.3317290913621316,-0.37328734325703,-0.28988943793825117,-0.20704345542879504};
  215. s6s = {-0.14203502700533388,-0.2517350722752868,-0.12322680301923153,0.05716905988908093,0.22412264548364097,0.4666205500210169,0.48430363375918584,0.38863572446689215};
  216. s6c = { 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000};
  217. s7 = {-0.018795316409125306,-0.023477089751902755,-0.019670089562814908,-0.015167436307089688,-0.010845329740737182,-0.002747600804479865,-0.0017200885766387283,-0.0006988704922596641};
  218. s8 = {-0.010521697907580636,-0.008845120251655493,-0.005715190092060276,-0.004200375084315877,-0.0032359717141892687,0.0016256878486157777,0.0005880437600457862,0.00023165273472761083};
  219. s9 = {-0.0006861229900257304,-0.0006888890894988537,-0.0006222598407671807,-0.0005993069103744563,-0.0006186949055500562,0.0006613554952330075,0.0004840128898600523,0.0003040455330802454};
  220. }
  221. }
  222. else if(nBins == 3){
  223. s1s = { 0.329, 0.329, 0.329};
  224. s1c = { 0.0, 0.0, 0.0};
  225. s2s = { 0.0, 0.0, 0.0};
  226. s2c = { 0.0, 0.0, 0.0};
  227. s3 = {-0.006, -0.006, -0.006};
  228. s4 = {-0.244, -0.244, -0.244};
  229. s5 = {-0.005, -0.005, -0.005};
  230. s6s = {-0.002, -0.002, -0.002};
  231. s6c = { 0.0, 0.0, 0.0};
  232. s7 = { 0.000, 0.000, 0.000};
  233. s8 = {-0.060, -0.060, -0.060};
  234. s9 = {-0.084, -0.084, -0.084};
  235. }
  236. else if(nBins == 2){
  237. s1s = { 0.1959, 0.4917};
  238. s1c = { 0.7441, 0.3447};
  239. s2s = { 0.0646, 0.1636};
  240. s2c = {-0.7217, -0.3432};
  241. s3 = {-0.0042, -0.1747};
  242. s4 = { 0.1033, 0.2929};
  243. s5 = {-0.1458, -0.3157};
  244. s6s = { 0.0652, -0.5338};
  245. s6c = { 0.0000, 0.0000};
  246. s7 = { 0.0340, -0.0000};
  247. s8 = {-0.0115, -0.0002};
  248. s9 = { 0.0003, 0.0003};
  249. }
  250. else if(nBins == 1){
  251. s1s = { 0.329};
  252. s1c = { 0.0};
  253. s2s = { 0.0};
  254. s2c = { 0.0};
  255. s3 = {-0.006};
  256. s4 = {-0.244};
  257. s5 = {-0.005};
  258. s6s = {-0.002};
  259. s6c = { 0.0};
  260. s7 = { 0.000};
  261. s8 = {-0.060};
  262. s9 = {-0.084};
  263. }
  264. else{
  265. spdlog::error("No init values found for S_i in binning scheme with number of q2 bins: {0:d}", nBins);
  266. assert(0);
  267. }
  268. //current fitter, plotter, parameteres and pdfs:
  269. fcnc::fitter f(&opts);
  270. fcnc::folder fldr(&opts);
  271. fcnc::options theOptions[nPDFs];
  272. fcnc::bu2kstarmumu_plotter * thePlotter[nPDFs];
  273. std::vector<fcnc::parameters*> theParams [nBins];
  274. std::vector<fcnc::parameters*> the2DParams [nBins];
  275. std::vector<fcnc::pdf*> theProbs [nBins];
  276. std::vector< std::vector<fcnc::event>* > selection[nBins];
  277. std::vector<UInt_t> pdf_idx;
  278. pdf_idx.push_back(0); //Run1 DD
  279. pdf_idx.push_back(1); //Run1 LL
  280. pdf_idx.push_back(2); //Run2 DD
  281. pdf_idx.push_back(3); //Run2 LL
  282. //set parameter FS common:
  283. std::vector<std::string> common_params;
  284. common_params.push_back("FS");
  285. if(FitR)
  286. common_params.push_back("R");
  287. if(!fitReference){
  288. common_params.push_back("gamkst");
  289. common_params.push_back("mkst");
  290. }
  291. f.set_common_parameters(common_params);
  292. for(UInt_t n = 0; n < nPDFs; n++){
  293. theOptions[n] = opts;
  294. theOptions[n].name = std::to_string(n+1);
  295. theOptions[n].run = UseAccPerRun ? (n > 1 ? 2 : 1) : 12;
  296. theOptions[n].DDLL = (n % 2 == 0) ? "DD" : "LL";
  297. theOptions[n].update_angle_ranges();
  298. thePlotter[n] = new fcnc::bu2kstarmumu_plotter(&theOptions[n]);
  299. }
  300. double sig_frac[nBins];
  301. unsigned int event_numbers[nBins][4];
  302. for(unsigned int b = 0; b < nBins; b++){
  303. for(unsigned int n = 0; n < 4; n++){
  304. //get event numbers and signal fraction from global function, given the total number of events
  305. EventNumbers(b, n, sig_frac[b], event_numbers[b][n], 2597, nBins, 4);
  306. if(OnlySignal)
  307. event_numbers[b][n] *= sig_frac[b];
  308. }
  309. }
  310. for(UInt_t b = 0; b < nBins; b++){
  311. selection[b].clear();
  312. for(UInt_t n = 0; n < nPDFs; n++){
  313. //create parameter sets:
  314. fcnc::bu2kstarmumu_parameters * leParameters = new fcnc::bu2kstarmumu_parameters(&theOptions[n]);
  315. theOptions[n].q2_min = opts.TheQ2binsmin.at(b);
  316. theOptions[n].q2_max = opts.TheQ2binsmax.at(b);
  317. //create PDFs
  318. fcnc::bu2kstarmumu_pdf * lePDF = new fcnc::bu2kstarmumu_pdf(&theOptions[n], leParameters);
  319. //initiate parameters for mass fit
  320. if(opts.extended_ml){
  321. if(fitSignalMC){
  322. spdlog::error("Never use extended_ml option for a pure signal sample, like in fitSignalMC!");
  323. assert(0);
  324. }
  325. if(OnlyBackground){
  326. spdlog::error("Never use extended_ml option for a pure background sample!");
  327. assert(0);
  328. }
  329. else{
  330. if((SystematicsWithToys || FitFinalToySamples) && !fitReference){
  331. leParameters->n_sig.init(event_numbers[b][n]*sig_frac[b], event_numbers[b][n]*sig_frac[b]*0.2, event_numbers[b][n]*sig_frac[b]*5., 0.1);
  332. leParameters->n_bkg.init(event_numbers[b][n]*(1.-sig_frac[b]), event_numbers[b][n]*(1.-sig_frac[b])*0.2, event_numbers[b][n]*(1.-sig_frac[b])*5., 0.1);
  333. leParameters->f_sig.init(sig_frac[b], 0.0, 1.0, 0.0);
  334. }
  335. else if(fitData && !LargeBinning){
  336. leParameters->n_sig.init(N_sig_Rare[b]*ev_frac[n], 0.0/*N_sig_Rare[b]*ev_frac[n]*0.2*/, N_sig_Rare[b]*ev_frac[n]*5., 0.1);
  337. leParameters->n_bkg.init(N_bkg_Rare[b]*ev_frac[n], N_bkg_Rare[b]*ev_frac[n]*0.2, N_bkg_Rare[b]*ev_frac[n]*5., 0.1);
  338. leParameters->f_sig.init(N_sig_Rare[b]*ev_frac[n]/(N_sig_Rare[b]*ev_frac[n]+N_bkg_Rare[b]*ev_frac[n]), 0.0, 1.0, 0.0);
  339. }
  340. else if(fitData && LargeBinning){
  341. leParameters->n_sig.init(100., 5., 5000., 0.1);
  342. leParameters->n_bkg.init(100., 5., 5000., 0.1);
  343. leParameters->f_sig.init(0.5, 0.0, 1.0, 0.0);
  344. }
  345. else{
  346. leParameters->n_sig.init(events[n].size() * 0.85, events[n].size() * 0.4, events[n].size() * 1.2, 0.1);
  347. leParameters->n_bkg.init(events[n].size() * 0.15, events[n].size() * 0.01, events[n].size() * 0.5, 0.1);
  348. leParameters->f_sig.init(0.5, 0.0, 1.0, 0.0);
  349. }
  350. }
  351. }
  352. else{
  353. if(fitSignalMC || OnlySignal)
  354. leParameters->f_sig.init(1.0, 0.0, 1.0, 0.0);
  355. else if(OnlyBackground){
  356. leParameters->f_sig.init(0.0, 0.0, 1.0, 0.0);
  357. }
  358. else
  359. leParameters->f_sig.init(0.35, 0.0, 1.0, 0.01);
  360. }
  361. leParameters->m_b.init(f_m_B[n], B_MASS_LOW, B_MASS_HIGH, OnlyBackground ? 0.0 : 0.01);
  362. if(opts.twotailedcrystalball)
  363. leParameters->m_res_1.init(1.0, 0.0, 1.0, 0.0);
  364. else
  365. leParameters->m_res_1.init(f_f_CB[n], 0.0, 1.0, fitSignalMC ? 0.01 : 0.0);
  366. if(leParameters->f_sig.get_value() != 1.0){
  367. if(fitData || true){ //only use single exponential for all cases
  368. leParameters->fm_tau.init(1.0, 0.0, 1.0, 0.0);
  369. leParameters->m_lambda.init(f_lambda1[n], -1.0e-2, -1.0e-6, 1.0e-6);
  370. leParameters->m_lambda_2.init(f_lambda2[n], -1.0e-2, -1.0e-6, 0.0);
  371. }
  372. else{
  373. leParameters->fm_tau.init((n % 2 == 1) ? 1.0 : 0.8, 0.0, 1.0, (n % 2 == 1) ? 0.0 : 0.01);
  374. leParameters->m_lambda.init(f_lambda1[n], -1.0e-1, -1.0e-6, 1.0e-6);
  375. leParameters->m_lambda_2.init(f_lambda2[n], -1.0e-1, -1.0e-6, (n % 2 == 1) ? 0.0 : 1.0e-6);
  376. }
  377. leParameters->m_tau.init( 1./2.835e-3, 100.0, 1.0e+4, 0.0);
  378. leParameters->m_tau_2.init(1./1.664e-3, 0.0, 1.0e+4, 0.0);
  379. }
  380. else{
  381. leParameters->fm_tau.init(0.8, 0.0, 1.0, 0.0);
  382. leParameters->m_lambda.init(-2.835e-3, -1.0e-1, -1.0e-6, 0.0);
  383. leParameters->m_lambda_2.init(-1.664e-4, -1.0e-1, -1.0e-6, 0.0);
  384. leParameters->m_tau.init( 1./2.835e-3, 100.0, 1.0e+4, 0.0);
  385. leParameters->m_tau_2.init(6.01e+2, 0.0, 1.0e+4, 0.0);
  386. }
  387. if(opts.twotailedcrystalball)
  388. leParameters->m_sigma_1.init(f_sigma_1[n], 5.0, 50.0, ((fitReference || fitSignalMC) && !OnlyBackground) ? 0.01 : 0.0);
  389. else{
  390. leParameters->m_sigma_1.init(f_sigma_1[n], 5.0, 50.0, ((fitReference || fitSignalMC) && !OnlyBackground) ? 0.01 : 0.0);
  391. leParameters->m_sigma_2.init(f_sigma_2[n], 5.0, 50.0, ((fitReference || fitSignalMC) && !OnlyBackground) ? 0.01 : 0.0);
  392. }
  393. leParameters->alpha_1.init(f_alpha_1[n], 0.5, 5.0, fitSignalMC ? 0.0001 : 0.0);
  394. leParameters->alpha_2.init(f_alpha_2[n], 0.5, 5.0, fitSignalMC ? 0.0001 : 0.0);
  395. leParameters->n_1.init(f_n_1[n], 0.1, 50.0, fitSignalMC ? 0.001 : 0.0);
  396. leParameters->n_2.init(f_n_2[n], 0.1, 50.0, fitSignalMC ? 0.001 : 0.0);
  397. if(!fitReference && !fitSignalMC && !SystematicsWithToys)
  398. leParameters->m_scale.init(f_m_scale[n], 0.0, 2.0, 0.0);
  399. else
  400. leParameters->m_scale.init(1.0, 0.0, 2.0, 0.0);
  401. leParameters->eff_q2.init(0.5*(opts.TheQ2binsmin.at(b)+opts.TheQ2binsmax.at(b)), opts.TheQ2binsmin.front(), opts.TheQ2binsmax.back(), 0.0);
  402. /*
  403. if(!fitSignalMC && !fitReference){
  404. if(LargeBinning)
  405. leParameters->load_only_Bmass_param_values("fitresult_SignalFit_fitSignalMC_SimultaneousFit_2Dfit_bin"+std::to_string(b)+"_pdf"+std::to_string(n)+".txt");
  406. else{
  407. if(leParameters->eff_q2() < 8.68)
  408. leParameters->load_only_Bmass_param_values("fitresult_SignalFit_fitSignalMC_SimultaneousFit_2Dfit_bin2_pdf"+std::to_string(n)+".txt");
  409. else if(leParameters->eff_q2() > 15.0)
  410. leParameters->load_only_Bmass_param_values("fitresult_SignalFit_fitSignalMC_SimultaneousFit_2Dfit_bin6_pdf"+std::to_string(n)+".txt");
  411. else
  412. leParameters->load_only_Bmass_param_values("fitresult_SignalFit_fitSignalMC_SimultaneousFit_2Dfit_bin5_pdf"+std::to_string(n)+".txt");
  413. }
  414. }
  415. */
  416. //initialize parameters to blind them!
  417. //leParameters->Fl.init(3.0/4.0*(s1c.at(b)-s2c.at(b)/3.0), 0.0, 1.0, 0.0);
  418. leParameters->Fl.init(1.0-4.0/3.0*s1s.at(b), 0.0, 1.0, 0.0);
  419. leParameters->S1s.init(s1s.at(b), -1.0, 1.0, 0.0);
  420. leParameters->S3.init(s3.at(b), -1.0, 1.0, 0.0);
  421. leParameters->S4.init(s4.at(b), -1.0, 1.0, 0.0);
  422. leParameters->S5.init(s5.at(b), -1.0, 1.0, 0.0);
  423. leParameters->Afb.init(3.0/4.0*s6s.at(b), -0.75, 0.75, 0.0);
  424. leParameters->S6s.init(s6s.at(b), -1.0, 1.0, 0.0);
  425. leParameters->S7.init(s7.at(b), -1.0, 1.0, 0.0);
  426. leParameters->S8.init(s8.at(b), -1.0, 1.0, 0.0);
  427. leParameters->S9.init(s9.at(b), -1.0, 1.0, 0.0);
  428. leParameters->SS1.init(0.0, -1.0, 1.0, 0.0);
  429. leParameters->SS2.init(0.0, -1.0, 1.0, 0.0);
  430. leParameters->SS3.init(0.0, -1.0, 1.0, 0.0);
  431. leParameters->SS4.init(0.0, -1.0, 1.0, 0.0);
  432. leParameters->SS5.init(0.0, -1.0, 1.0, 0.0);
  433. if(blind){
  434. double blind_scale = 1.0;
  435. leParameters->Fl.set_blinding(true, blind_scale, true);
  436. leParameters->S1s.set_blinding(true, blind_scale, true);
  437. leParameters->S3.set_blinding(true, blind_scale, true);
  438. leParameters->S4.set_blinding(true, blind_scale, true);
  439. leParameters->S5.set_blinding(true, blind_scale, true);
  440. leParameters->Afb.set_blinding(true, blind_scale, true);
  441. leParameters->S6s.set_blinding(true, blind_scale, true);
  442. leParameters->S7.set_blinding(true, blind_scale, true);
  443. leParameters->S8.set_blinding(true, blind_scale, true);
  444. leParameters->S9.set_blinding(true, blind_scale, true);
  445. }
  446. leParameters->FS .init(opts.swave ? 0.1*FL_scale[b] : 0.0, -1.0, 1.0, opts.swave ? 0.001 : 0.0);
  447. if(opts.fit_mkpi || opts.use_mkpi){
  448. //p-wave
  449. if(fitData)
  450. leParameters->gammakstar.init(0.0503, 0.01, 0.10, 0.001, 0.01, true);//PDG value
  451. else
  452. leParameters->gammakstar.init(0.0503, 0.01, 0.10, OnlyBackground ? 0.0 : 0.001);//PDG value
  453. leParameters->mkstar.init(PDGMASS_K_STAR_PLUS / 1000., 0.85, 1.0, OnlyBackground ? 0.0 : 0.001);//PDG value
  454. //s-wave
  455. leParameters->gammakstarplus.init(0.236, 0.1, 1.0, 0.0); //PDG value
  456. leParameters->mkstarplus.init(1.41, 1.2, 1.6, 0.0); //PDG value
  457. leParameters->asphase.init(TMath::Pi(), 0.0, 2.0*TMath::Pi(), 0.0);
  458. leParameters->a.init(1.95, 0.001, 20.0, 0.0);
  459. leParameters->r.init(1.78, 0.001, 10.0, 0.0);
  460. leParameters->R.init(1.6, 0.0, 10.0, FitR ? 0.001 : 0.0);
  461. //background parameter
  462. if(leParameters->f_sig() != 1.0){
  463. leParameters->cbkgmkpi0.init(1.0, -1.0, 1.0, 0.0);
  464. leParameters->cbkgmkpi1.init(0.1, -1.0, 1.0, 0.01);
  465. leParameters->cbkgmkpi2.init(0.0, -1.0, 1.0, 0.0);
  466. leParameters->cbkgmkpi3.init(0.0, -1.0, 1.0, 0.0);
  467. leParameters->cbkgmkpi4.init(0.0, -1.0, 1.0, 0.0);
  468. opts.mkpi_threshold = false;
  469. leParameters->nthreshold.init(1.5, 0.0, 15.0, 0.0);
  470. }
  471. }
  472. //background parameter //TODO loop //TODO nPDFs
  473. if(leParameters->f_sig() != 1.0){
  474. leParameters->cbkgctl0.init(1.0, -1.0, 1.0, 0.0);
  475. leParameters->cbkgctl1.init(Bctl1[n], -1.0, 1.0, 0.0);
  476. leParameters->cbkgctl2.init(Bctl2[n], -1.0, 1.0, 0.0);
  477. leParameters->cbkgctl3.init(0.0, -1.0, 1.0, 0.0);
  478. leParameters->cbkgctl4.init(0.0, -1.0, 1.0, 0.0);
  479. leParameters->cbkgctk0.init(1.0, -1.0, 1.0, 0.0);
  480. leParameters->cbkgctk1.init(Bctk1[n], -1.0, 1.0, 0.0);
  481. leParameters->cbkgctk2.init(Bctk2[n], -1.0, 1.0, 0.0);
  482. leParameters->cbkgctk3.init(0.0, -1.0, 1.0, 0.0);
  483. leParameters->cbkgctk4.init(0.0, -1.0, 1.0, 0.0);
  484. leParameters->cbkgphi0.init(1.0, -1.0, 1.0, 0.0);
  485. leParameters->cbkgphi1.init(0.0, -1.0, 1.0, 0.0);
  486. leParameters->cbkgphi2.init(0.0, -1.0, 1.0, 0.0);
  487. leParameters->cbkgphi3.init(0.0, -1.0, 1.0, 0.0);
  488. leParameters->cbkgphi4.init(0.0, -1.0, 1.0, 0.0);
  489. }
  490. if(SystematicsWithToys || FitFinalToySamples){
  491. fcnc::bu2kstarmumu_parameters * le2DParameters = new fcnc::bu2kstarmumu_parameters(&theOptions[n]);
  492. le2DParameters->eff_q2.init(0.5*(opts.TheQ2binsmin.at(b)+opts.TheQ2binsmax.at(b)), opts.TheQ2binsmin.front(), opts.TheQ2binsmax.back(), 0.0);
  493. //initiate parameters for B mass fit
  494. if(OnlySignal)
  495. le2DParameters->f_sig.init(1.0, 0.0, 1.0, 0.0);
  496. else
  497. le2DParameters->f_sig.init(N_sig_Rare[b]*ev_frac[n]/(N_sig_Rare[b]*ev_frac[n]+N_bkg_Rare[b]*ev_frac[n]), 0.0, 1.0, 0.001);
  498. le2DParameters->m_b.init(f_m_B[n], B_MASS_LOW, B_MASS_HIGH, 0.01);
  499. if(opts.twotailedcrystalball)
  500. le2DParameters->m_res_1.init(1.0, 0.0, 1.0, 0.0);
  501. else
  502. le2DParameters->m_res_1.init(f_f_CB[n], 0.0, 1.0, 0.0);
  503. le2DParameters->fm_tau.init(1.0, 0.0, 1.0, 0.0);
  504. le2DParameters->m_lambda.init(f_lambda1[n], -1.0e-1, -1.0e-6, 1.0e-6);
  505. le2DParameters->m_lambda_2.init(f_lambda2[n], -1.0e-1, -1.0e-6, 0.0);
  506. if(opts.twotailedcrystalball){
  507. le2DParameters->m_sigma_1.init(f_sigma_1[n], 5.0, 40.0, 0.0);
  508. }
  509. else{
  510. le2DParameters->m_sigma_1.init(f_sigma_1[n], 5.0, 40.0, 0.0);
  511. le2DParameters->m_sigma_2.init(f_sigma_2[n], 5.0, 40.0, 0.0);
  512. }
  513. le2DParameters->alpha_1.init(f_alpha_1[n], 0.5, 5.0, 0.0);
  514. le2DParameters->alpha_2.init(f_alpha_2[n], 0.5, 3.0, 0.0);
  515. le2DParameters->n_1.init(f_n_1[n], 0.1, 15.0, 0.0);
  516. le2DParameters->n_2.init(f_n_2[n], 0.1, 10.0, 0.0);
  517. le2DParameters->m_scale.init(1.0, 0.0, 2.0, 0.0);
  518. //mK*
  519. le2DParameters->gammakstar.init(0.0503, 0.01, 0.8, 0.0); //PDG value
  520. le2DParameters->mkstar.init(PDGMASS_K_STAR_PLUS / 1000., 0.7, 1.2, 0.01); //PDG value
  521. //s-wave
  522. if(opts.swave)
  523. le2DParameters->FS.init(0.063, -1.00, 1.00, 0.001);
  524. else
  525. le2DParameters->FS.init(0.0, -1.00, 1.00, 0.0);
  526. le2DParameters->gammakstarplus.init(0.236, 0.1, 1.0, 0.0); //PDG value
  527. le2DParameters->mkstarplus.init(1.41, 1.2, 1.6, 0.0); //PDG value
  528. le2DParameters->asphase.init(TMath::Pi(), 0.0, 2.0*TMath::Pi(), 0.0);
  529. le2DParameters->a.init(1.95, 0.0, 20.0, 0.0);
  530. le2DParameters->r.init(1.78, 0.0, 10.0, 0.0);
  531. le2DParameters->R.init(1.6, 0.0, 10.0, 0.0);
  532. //background parameter
  533. le2DParameters->cbkgmkpi0.init(1.0, -1.0, 1.0, 0.0);
  534. le2DParameters->cbkgmkpi1.init(0.0, -1.0, 1.0, 0.01);
  535. the2DParams[b].push_back(le2DParameters);
  536. }
  537. //define center of q2bin as effective q2:
  538. lePDF->load_coeffs_eff_phsp_4d();
  539. lePDF->update_cached_normalization(leParameters);
  540. //save parameters and pdfs in vectors:
  541. bool include = false;
  542. for(UInt_t m = 0; m < pdf_idx.size(); m++){
  543. if(n == pdf_idx.at(m)){
  544. include = true;
  545. break;
  546. }
  547. }
  548. if(include){
  549. theParams [b].push_back(leParameters);
  550. theProbs [b].push_back(lePDF);
  551. }
  552. spdlog::info("[PDF{0:d}]\tSaved PDF and parameters!", n);
  553. //create vector with events according to the requested fits/pulls
  554. std::vector<fcnc::event> * leEvents = new std::vector<fcnc::event>;
  555. //choose events from the event vector and sort corresponding to q2bin or non-resonant:
  556. UInt_t NNN = events[n].size();
  557. //This randomly pre-selected list of events is used, if not all events are wanted:
  558. std::vector<UInt_t>eee;
  559. for(UInt_t e = 0; e < NNN; e++){
  560. fcnc::event meas;
  561. meas = events[n].at(e);
  562. //if wanted, only choose events from within q2 bin
  563. if(meas.q2 < opts.TheQ2binsmin.at(b) || meas.q2 > opts.TheQ2binsmax.at(b))
  564. continue;
  565. if(meas.q2 > 0.98 && meas.q2 < 1.1)//exclude phi resonance
  566. continue;
  567. if(meas.m < opts.m_low || meas.m > opts.m_high)
  568. continue;
  569. if(OnlyBackground)
  570. if(meas.m > 5230. && meas.m < 5330.)//exclude B peak
  571. continue;
  572. //crosscheck between mag up and mag down:
  573. if(FitOnlyMagDown && meas.magnet > 0)
  574. continue;
  575. if(FitOnlyMagUp && meas.magnet < 0)
  576. continue;
  577. if(meas.costhetal < -1. || meas.costhetal > +1.){
  578. spdlog::warn("cos(theta_L) out of range: {0:f}", meas.costhetal);
  579. continue;
  580. }
  581. if(meas.costhetak < -1. || meas.costhetak > +1.){
  582. spdlog::warn("cos(theta_K) out of range: {0:f}", meas.costhetak);
  583. continue;
  584. }
  585. if(meas.phi < -TMath::Pi() || meas.phi > +TMath::Pi()){
  586. spdlog::warn("phi out of range: {0:f}", meas.phi);
  587. continue;
  588. }
  589. if(meas.mkpi < opts.mkpi_min || meas.mkpi > opts.mkpi_max){
  590. spdlog::warn("m(Kpi) out of range: {0:f}", meas.mkpi);
  591. continue;
  592. }
  593. leEvents->push_back(meas);
  594. }
  595. if(SystematicsWithToys){
  596. leEvents->clear();
  597. fcnc::bu2kstarmumu_generator gen(&theOptions[n]);
  598. UInt_t idx = pdf_idx.at(n);
  599. if(UseAccPer2Years && theOptions[n].run == 2){
  600. theOptions[n].run = 21;
  601. lePDF->load_coeffs_eff_phsp_4d();
  602. lePDF->update_cached_normalization(leParameters);
  603. std::vector<fcnc::event> eves1516 = gen.generate(0.5 + event_numbers[b][idx] * (idx%2==0 ? 0.275 : 0.292) , leParameters, lePDF);
  604. for(unsigned int k = 0; k < eves1516.size(); k++)
  605. eves1516.at(k).year = 2016;
  606. theOptions[n].run = 22;
  607. lePDF->load_coeffs_eff_phsp_4d();
  608. lePDF->update_cached_normalization(leParameters);
  609. std::vector<fcnc::event> eves1718 = gen.generate(0.5 + event_numbers[b][idx] * (idx%2==0 ? 0.725 : 0.708), leParameters, lePDF);
  610. for(unsigned int k = 0; k < eves1718.size(); k++)
  611. eves1718.at(k).year = 2018;
  612. theOptions[n].run = 2;
  613. leEvents->insert(leEvents->end(), eves1516.begin(), eves1516.end());
  614. leEvents->insert(leEvents->end(), eves1718.begin(), eves1718.end());
  615. }
  616. else{
  617. lePDF->load_coeffs_eff_phsp_4d();
  618. lePDF->update_cached_normalization(leParameters);
  619. std::vector<fcnc::event> genevents = gen.generate(0.5 + event_numbers[b][idx], leParameters, lePDF);
  620. for(unsigned int k = 0; k < genevents.size(); k++)
  621. genevents.at(k).year = (theOptions[idx].run == 2 ? 2016 : 2012);
  622. leEvents->insert(leEvents->end(), genevents.begin(), genevents.end());
  623. }
  624. }
  625. if(UseAccPer2Years && theOptions[n].run == 2){
  626. //split the Run2 data-set into two groups: [0]: 2015+16 [1]: 2017+18
  627. std::vector<fcnc::event> * Events_2015_2016 = new std::vector<fcnc::event>;
  628. std::vector<fcnc::event> * Events_2017_2018 = new std::vector<fcnc::event>;
  629. UInt_t nBefore = leEvents->size();
  630. for(UInt_t e = 0; e < leEvents->size(); e++){
  631. fcnc::event meas = leEvents->at(e);
  632. if(meas.year == 2015 || meas.year == 2016)
  633. Events_2015_2016->push_back(meas);
  634. else if(meas.year == 2017 || meas.year == 2018)
  635. Events_2017_2018->push_back(meas);
  636. else{
  637. spdlog::critical("Invalid year found in data-set of Run2: {0:d}", meas.year);
  638. assert(0);
  639. }
  640. }
  641. //update cached eff for 2015+16 and 2017+18 individually
  642. theOptions[n].run = 21;
  643. lePDF->load_coeffs_eff_phsp_4d();
  644. lePDF->update_cached_normalization(leParameters);
  645. lePDF->update_cached_efficiencies(leParameters, Events_2015_2016);
  646. theOptions[n].run = 22;
  647. lePDF->load_coeffs_eff_phsp_4d();
  648. lePDF->update_cached_normalization(leParameters);
  649. lePDF->update_cached_efficiencies(leParameters, Events_2017_2018);
  650. //recombine both data-sets:
  651. theOptions[n].run = 2;
  652. leEvents->clear();
  653. leEvents->insert(leEvents->end(), Events_2015_2016->begin(), Events_2015_2016->end());
  654. leEvents->insert(leEvents->end(), Events_2017_2018->begin(), Events_2017_2018->end());
  655. assert(nBefore == leEvents->size());
  656. }
  657. else{
  658. lePDF->load_coeffs_eff_phsp_4d();
  659. lePDF->update_cached_normalization(leParameters);
  660. lePDF->update_cached_efficiencies(leParameters, leEvents);
  661. }
  662. if(include)
  663. selection[b].push_back(leEvents);
  664. spdlog::info("[PDF{0:d}]\tFinished selecting {1:f} events!", n, leEvents->size());
  665. } // end of PDF
  666. } // end of q2 bins
  667. double FSfromLargeBins[3], dFSfromLargeBins[3];
  668. if((SystematicsWithToys || FitFinalToySamples) && !OnlySignal && !fitReference){
  669. //combine events to larger bins and fit before fitting all 8 bins:
  670. int fit_results[3];
  671. std::vector< std::vector<fcnc::event>* > combinedEvents[3];
  672. bool ex_ml_buffer = theOptions[0].extended_ml;
  673. for(unsigned int n = 0; n < nPDFs; n++){
  674. combinedEvents[0].push_back(new std::vector<fcnc::event>);
  675. combinedEvents[0].back()->insert(combinedEvents[0].back()->end(), selection[1].at(n)->begin(), selection[1].at(n)->end());
  676. combinedEvents[0].back()->insert(combinedEvents[0].back()->end(), selection[2].at(n)->begin(), selection[2].at(n)->end());
  677. combinedEvents[0].back()->insert(combinedEvents[0].back()->end(), selection[3].at(n)->begin(), selection[3].at(n)->end());
  678. combinedEvents[0].back()->insert(combinedEvents[0].back()->end(), selection[4].at(n)->begin(), selection[4].at(n)->end());
  679. combinedEvents[1].push_back(new std::vector<fcnc::event>);
  680. combinedEvents[1].back()->insert(combinedEvents[1].back()->end(), selection[5].at(n)->begin(), selection[5].at(n)->end());
  681. combinedEvents[2].push_back(new std::vector<fcnc::event>);
  682. combinedEvents[2].back()->insert(combinedEvents[2].back()->end(), selection[6].at(n)->begin(), selection[6].at(n)->end());
  683. combinedEvents[2].back()->insert(combinedEvents[2].back()->end(), selection[7].at(n)->begin(), selection[7].at(n)->end());
  684. theOptions[n].extended_ml = false;
  685. }
  686. //extract FS from this fit and constrain FS to this value:
  687. for(int b = 0; b < 3; b++){
  688. fit_results[b] = f.fit(theProbs[b], the2DParams[b], combinedEvents[b]);
  689. for(unsigned int n = 0; n < nPDFs; n++){
  690. fcnc::bu2kstarmumu_parameters * dieParameter = (fcnc::bu2kstarmumu_parameters *) the2DParams[b].at(n);
  691. FSfromLargeBins[b] = dieParameter->FS.get_value();
  692. dFSfromLargeBins[b] = dieParameter->FS.get_error();
  693. spdlog::info("[LARGEBIN{0:d}][PDF{1:d}]\tFS = {2:f} +/- {3:f}",b, n, FSfromLargeBins[b], dFSfromLargeBins[b]);
  694. }
  695. }
  696. if(false){
  697. TFile* fout = new TFile(("finalresult_OnlyToys"+std::to_string(opts.job_id)+"_2DfitLargeBins"+(LargerMKpiRange ? "_MediumMKpiRange" : "")+".root").c_str(), "RECREATE");
  698. fout->cd();
  699. int param_index = 0;
  700. fcnc::bu2kstarmumu_parameters * dieParameter = (fcnc::bu2kstarmumu_parameters *) the2DParams[0].at(0);
  701. for(UInt_t p = 0; p < dieParameter->nparameters(); p++){
  702. fcnc::parameter* param = dieParameter->get_parameter(p);
  703. if(param->get_step_size() != 0.0){//skip constant parameters
  704. std::string parname(param->get_name());
  705. std::string pardesc(param->get_description());
  706. spdlog::info("Creating new TTree for parameter: " +parname);
  707. TTree* t = new TTree(parname.c_str(), pardesc.c_str());
  708. double q2a, q2b, value, error, error_up, error_down, start_value, nominal_value, nominal_error=0.0;
  709. int pdf, bin, migrad, status_cov;
  710. const unsigned int corr_max = param->correlations.size();
  711. double tmp_corr[corr_max];
  712. t->Branch("pdf",&pdf,"pdf/I");
  713. t->Branch("bin",&bin,"bin/I");
  714. t->Branch("value",&value,"value/D");
  715. t->Branch("q2min",&q2a,"q2min/D");
  716. t->Branch("q2max",&q2b,"q2max/D");
  717. t->Branch("error",&error,"error/D");
  718. t->Branch("error_up",&error_up,"error_up/D");
  719. t->Branch("error_down",&error_down,"error_down/D");
  720. t->Branch("start_value",&start_value,"start_value/D");
  721. t->Branch("migrad",&migrad,"migrad/I");
  722. t->Branch("status_cov",&status_cov,"status_cov/I");
  723. t->Branch("nominal_value",&nominal_value,"nominal_value/D");
  724. t->Branch("nominal_error",&nominal_error,"nominal_error/D");
  725. t->Branch("index",&param_index, "index/I");
  726. std::stringstream corr_stream;
  727. corr_stream << "correlations[" << corr_max << "]/D";
  728. t->Branch("correlations",&tmp_corr,corr_stream.str().c_str());
  729. pdf = 1;
  730. for(UInt_t b = 0; b < 3; b++){
  731. fcnc::parameter* par = the2DParams[b].at(0)->get_parameter(p);
  732. UInt_t paridx = 0;
  733. while(par->get_name() != parname.c_str()){
  734. par = the2DParams[b].at(0)->get_parameter(paridx);
  735. paridx++;
  736. if(paridx == the2DParams[b].at(0)->nparameters()){
  737. spdlog::warn("Parameter '"+parname+"' not found in PDF 0. Continue with next PDF.");
  738. break;
  739. }
  740. }
  741. if(paridx == the2DParams[b].at(0)->nparameters())
  742. break;
  743. bin = b;
  744. q2a = opts.TheQ2binsmin[b];
  745. q2b = opts.TheQ2binsmax[b];
  746. value = par->get_value();
  747. error = par->get_error();
  748. error_up = par->get_error_up();
  749. error_down = par->get_error_down();
  750. start_value = par->get_start_value();
  751. nominal_value = par->get_start_value();
  752. migrad = fit_results[b] % 100;
  753. status_cov = fit_results[b] / 100;
  754. for (unsigned int l = 0; l < corr_max; l++)
  755. tmp_corr[l] = 0.0;
  756. for (unsigned int l = 0; l < par->correlations.size(); l++)
  757. tmp_corr[l] = par->correlations.at(l);
  758. t->Fill();
  759. }
  760. t->Write();
  761. delete t;
  762. param_index++;
  763. }
  764. }
  765. fout->Close();
  766. }
  767. if(false){
  768. for(UInt_t b = 0; b < 3; b++){
  769. for(unsigned int n = 0; n < nPDFs; n++){
  770. std::cout << "|| BIN " << b << "\tPDF " << n << std::endl;
  771. the2DParams[b].at(n)->print_parameters(false);
  772. }
  773. }
  774. }
  775. if(false){
  776. for(UInt_t b = 0; b < 3; b++){
  777. for(unsigned int n = 0; n < nPDFs; n++){
  778. fcnc::bu2kstarmumu_parameters * dieParameter = (fcnc::bu2kstarmumu_parameters *) the2DParams[b].at(n);
  779. fcnc::bu2kstarmumu_pdf * leProb = (fcnc::bu2kstarmumu_pdf *) theProbs[b].at(n);
  780. thePlotter[n]->plot_data(leProb, dieParameter, combinedEvents[b].at(n), get_MoMPlot_path(), "ToyMoM_LargeBins_bin"+std::to_string(b)+"_pdf"+std::to_string(n), true);
  781. }
  782. }
  783. }
  784. for(unsigned int n = 0; n < nPDFs; n++){
  785. theOptions[n].extended_ml = ex_ml_buffer;
  786. }
  787. }
  788. bool do_fit = !OnlySignal;
  789. int fitresult = 0;
  790. if(do_fit){
  791. for(UInt_t b = 0; b < nBins; b++){
  792. std::vector<fcnc::event> * AllEvents = new std::vector<fcnc::event>;
  793. for(UInt_t n = 0; n < nPDFs; n++){
  794. if(SimultaneousFit){
  795. if(n == 0){
  796. fitresult = f.fit(theProbs[b], theParams[b], selection[b]);
  797. }
  798. }
  799. else if(OneFit){
  800. if(n > 0)
  801. continue;
  802. AllEvents->clear();
  803. for(UInt_t nn = 0; nn < nPDFs; nn++)
  804. AllEvents->insert(AllEvents->end(), selection[b].at(nn)->begin(), selection[b].at(nn)->end());
  805. fitresult = f.fit(theProbs[b].at(n), theParams[b].at(n), AllEvents);
  806. }
  807. else{
  808. fitresult = f.fit(theProbs[b].at(n), theParams[b].at(n), selection[b].at(n));
  809. }
  810. fit_results[b].push_back(fitresult);
  811. fcnc::bu2kstarmumu_parameters * leParameters = (fcnc::bu2kstarmumu_parameters *) theParams[b].at(n);
  812. fcnc::bu2kstarmumu_pdf * leProb = (fcnc::bu2kstarmumu_pdf *) theProbs[b].at(n);
  813. if(!SimultaneousFit || n == 0){
  814. double fitted_fs = leParameters->FS.get_value();
  815. double fitted_dfs_up = leParameters->FS.get_error_up();
  816. double fitted_dfs_down = leParameters->FS.get_error_down();
  817. double fitted_dfs = leParameters->FS.get_error();
  818. if(opts.minos_errors)
  819. fitted_dfs = std::max(fabs(fitted_dfs_up), fabs(fitted_dfs_down));
  820. if(fitSignalMC || !opts.swave){
  821. fitted_fs = 0.0;
  822. fitted_dfs = 0.0;
  823. }
  824. else if(FSfrom2DLargeBins){
  825. if(leParameters->eff_q2() < 9.0){
  826. fitted_fs = 0.252622 * FL_scale[b];
  827. fitted_dfs = 0.103659 * FL_scale[b];
  828. }
  829. else if(leParameters->eff_q2() < 15.0){
  830. fitted_fs = 0.013818 * FL_scale[b];
  831. fitted_dfs = 0.146519 * FL_scale[b];
  832. }
  833. else{
  834. fitted_fs = 0.0164393 * FL_scale[b];
  835. fitted_dfs = 0.0895912 * FL_scale[b];
  836. }
  837. }
  838. else if((SystematicsWithToys || FitFinalToySamples) && !fitReference){//use FS obtained from the fit in the larger q2bins:
  839. if(leParameters->eff_q2() < 9.0){
  840. fitted_fs = FSfromLargeBins[0] * FL_scale[b];
  841. fitted_dfs = dFSfromLargeBins[0] * FL_scale[b];
  842. }
  843. else if(leParameters->eff_q2() < 15.0){
  844. fitted_fs = FSfromLargeBins[1] * FL_scale[b];
  845. fitted_dfs = dFSfromLargeBins[1] * FL_scale[b];
  846. }
  847. else{
  848. fitted_fs = FSfromLargeBins[2] * FL_scale[b];
  849. fitted_dfs = dFSfromLargeBins[2] * FL_scale[b];
  850. }
  851. }
  852. std::cout << std::setprecision(5) << "FS: " << fitted_fs << "+/-" << fitted_dfs << std::endl;
  853. fitted_FS[b].push_back(fitted_fs);
  854. fitted_FSerr[b].push_back(fitted_dfs);
  855. fitted_FSerrUp[b].push_back(fitted_dfs_up);
  856. fitted_FSerrDown[b].push_back(fitted_dfs_down);
  857. }
  858. //plot fit projections of 2Dfit
  859. std::ostringstream eps_label;
  860. spdlog::info("[PLOT]\tStart configurations for plotting!");
  861. if(fitReference){
  862. eps_label << "JpsiFit";
  863. }
  864. else if(fitSignalMC){
  865. eps_label << "FCNC_SignalfitSignalMC_fit_bin" << std::to_string(b);
  866. }
  867. else{
  868. eps_label << "FCNCfit_bin" << std::to_string(b);
  869. }
  870. if (LargeBinning) eps_label << "_LargeBins";
  871. else if(Fit2bins) eps_label << "_2BINS";
  872. else if(Fit1bin) eps_label << "_1BIN";
  873. else if(FitAllbins) eps_label << "_9BINS";
  874. if(OneFit) eps_label << "_FullDataSet";
  875. else eps_label << (n % 2 == 0 ? "_DD_Run" : "_LL_Run") << std::to_string( (int) (1+n/2));
  876. eps_label << "_MoM_2Dfit";
  877. theOptions[n].plot_label = "LHCb data";
  878. theOptions[n].q2_label = q2_label(theOptions[n].TheQ2binsmin.at(b),theOptions[n].TheQ2binsmax.at(b));
  879. if(opts.write_eps){
  880. std::cout << "[PLOT]\t" << eps_label.str() << std::endl;
  881. if(OneFit)
  882. thePlotter[n]->plot_data(leProb, leParameters, AllEvents, get_MoMPlot_path(), eps_label.str(), false);
  883. else
  884. thePlotter[n]->plot_data(leProb, leParameters, selection[b].at(n), get_MoMPlot_path(), eps_label.str(), false);
  885. }
  886. }//end loop PDFs
  887. if(SimultaneousFit){
  888. std::ostringstream eps_label;
  889. spdlog::info("[PLOT]\tStart configurations for combined plotting!");
  890. if(fitReference){
  891. eps_label << "JpsiFit";
  892. }
  893. else if(fitSignalMC){
  894. eps_label << "FCNC_SignalfitSignalMC_fit_bin" << std::to_string(b);
  895. }
  896. else{
  897. eps_label << "FCNCfit_bin" << std::to_string(b);
  898. }
  899. if (LargeBinning) eps_label << "_LargeBins";
  900. else if(Fit2bins) eps_label << "_2BINS";
  901. else if(Fit1bin) eps_label << "_1BIN";
  902. else if(FitAllbins) eps_label << "_9BINS";
  903. eps_label << "_AllPDFs";
  904. eps_label << "_MoM_2Dfit";
  905. std::vector<fcnc::bu2kstarmumu_pdf*> * prober = (std::vector<fcnc::bu2kstarmumu_pdf*> *) & theProbs[b];
  906. std::vector<fcnc::bu2kstarmumu_parameters*> * paramser = (std::vector<fcnc::bu2kstarmumu_parameters*> *) & theParams[b];
  907. thePlotter[0]->SetPulls(true);
  908. thePlotter[0]->plot_added_pdfs(prober, paramser, & selection[b],
  909. get_MoMPlot_path(), eps_label.str(), true);
  910. thePlotter[0]->SetPulls(false);
  911. eps_label << "_noPulls";
  912. thePlotter[0]->plot_added_pdfs(prober, paramser, & selection[b],
  913. get_MoMPlot_path(), eps_label.str(), true);
  914. }
  915. }//end loop q2 bins
  916. }//end do_fit
  917. else{//fill vectors with nominal values, if fit was not done:
  918. for(UInt_t b = 0; b < nBins; b++){
  919. for(UInt_t n = 0; n < nPDFs; n++){
  920. if(FixFStoStart)
  921. fitted_FS[b].push_back( opts.swave ? 0.1*FL_scale[b] : 0.0);
  922. else
  923. fitted_FS[b].push_back(0.);
  924. fitted_FSerr[b].push_back(0.);
  925. fitted_FSerrUp[b].push_back(0.);
  926. fitted_FSerrDown[b].push_back(0.);
  927. }
  928. fit_results[b].push_back(300);
  929. }
  930. }
  931. bool do_mom = !LargeBinning;
  932. if(do_mom){
  933. for(UInt_t b = 0; b < nBins; b++){
  934. std::vector<fcnc::event> allEvents;
  935. allEvents.clear();
  936. //assign sWeights:
  937. if(!(fitSignalMC || OnlySignal) && do_fit){//pure signal and fitSignalMC don't need sWeights
  938. if(OneFit){
  939. //for the combined fit, first add all events together and then apply sWeights according to first parameter idx
  940. for(UInt_t n = 0; n < nPDFs; n++){
  941. spdlog::info("[MOM]\tCombine events into one set for bin={0:d} pdf={1:d}",b, n);
  942. allEvents.insert(allEvents.end(), selection[b].at(n)->begin(), selection[b].at(n)->end());
  943. }
  944. spdlog::info("[MOM]\tGet sWeights from fit for the full dataset of bin={0:d}",b);
  945. fcnc::bu2kstarmumu_parameters * leParameters = (fcnc::bu2kstarmumu_parameters *) theParams[b].at(0);
  946. fcnc::bu2kstarmumu_pdf * leProb = (fcnc::bu2kstarmumu_pdf *) theProbs[b].at(0);
  947. spdlog::info("[MOM]\tGet combined sWeights from fit for bin={0:d}",b);
  948. leProb->calculate_sweights(leParameters, &allEvents);
  949. spdlog::info("[MOM]\tGet combined sWeights from fit for bin={0:d} ... DONE!",b);
  950. }
  951. else{
  952. //for individual mom, each sub-sets gets sWeights and no sub-sets are added together
  953. for(UInt_t n = 0; n < nPDFs; n++){
  954. fcnc::bu2kstarmumu_parameters * leParameters = (fcnc::bu2kstarmumu_parameters *) theParams[b].at(n);
  955. fcnc::bu2kstarmumu_pdf * leProb = (fcnc::bu2kstarmumu_pdf *) theProbs[b].at(n);
  956. spdlog::info("[MOM]\tGet sWeights from fit for bin={0:d} pdf={1:d}",b, n);
  957. leProb->calculate_sweights(leParameters, selection[b].at(n));
  958. spdlog::info("[MOM]\tGet sWeights from fit for bin={0:d} pdf={1:d} ...DONE.",b, n);
  959. //for the simultaneous mom, first apply sWeights according to parameters[n], then add all events together
  960. if(SimultaneousMoM){
  961. spdlog::info("[MOM]\tCombine events (after sWeights have been applied) into one set for bin={0:d} pdf={1:d}",b, n);
  962. allEvents.insert(allEvents.end(), selection[b].at(n)->begin(), selection[b].at(n)->end());
  963. }
  964. }
  965. }
  966. }//end of sWeight assignments
  967. //extract and save angular moments:
  968. if(SimultaneousMoM || SimultaneousFit || OneFit){//do MoM for the full dataset of this q2bin
  969. std::vector<double> mom(18, 0.0), obs(18, 0.0);
  970. std::vector<double> momcov(18*18, 0.0), obscov(18*18, 0.0);
  971. spdlog::info("[MoM]\tGet moments from the combined dataset! bin={0:d}",b);
  972. if(!allEvents.size()){
  973. for(UInt_t n = 0; n < nPDFs; n++){
  974. spdlog::info("[MOM]\tCombine events for MoM into one set for bin={0:d} pdf={1:d}",b, n);
  975. allEvents.insert(allEvents.end(), selection[b].at(n)->begin(), selection[b].at(n)->end());
  976. }
  977. }
  978. fcnc::bu2kstarmumu_parameters * leParameters = (fcnc::bu2kstarmumu_parameters *) theParams[b].at(0);
  979. fcnc::bu2kstarmumu_pdf * leProb = (fcnc::bu2kstarmumu_pdf *) theProbs[b].at(0);
  980. leProb->get_moments(allEvents, mom, momcov);
  981. spdlog::info("[MoM]\tConvert moments to angular observables for the combined dataset! bin={0:d}",b);
  982. fcnc::moments_to_observables(rnd, mom, momcov, fitted_FS[b].at(0), fitted_FSerr[b].at(0), obs, obscov, FixFStoStart || UseExternalFS);
  983. spdlog::info("[MoM]\tSave angular observables to parameters for the combined dataset! bin={0:d}",b);
  984. leProb->save_moments_to_obs(leParameters, obs, obscov);
  985. spdlog::info("[MoM]\tDone for the combined dataset of bin={0:d}",b);
  986. }
  987. else{//do MoM individually for each sub-set
  988. for(UInt_t n = 0; n < nPDFs; n++){
  989. std::vector<double> mom(18, 0.0), obs(18, 0.0);;
  990. std::vector<double> momcov(18*18, 0.0), obscov(18*18, 0.0);
  991. spdlog::info("[MoM]\tGet moments from the combined dataset! bin={0:d} pdf={1:d}",b, n);
  992. fcnc::bu2kstarmumu_parameters * leParameters = (fcnc::bu2kstarmumu_parameters *) theParams[b].at(n);
  993. fcnc::bu2kstarmumu_pdf * leProb = (fcnc::bu2kstarmumu_pdf *) theProbs[b].at(n);
  994. leProb->get_moments(*selection[b].at(n), mom, momcov);
  995. spdlog::info("[MoM]\tConvert moments to angular observables for bin={0:d} pdf={1:d}",b, n);
  996. fcnc::moments_to_observables(rnd, mom, momcov, fitted_FS[b].at(n), fitted_FSerr[b].at(n), obs, obscov, FixFStoStart || UseExternalFS);
  997. spdlog::info("[MoM]\tSave angular observables to parameters for bin={0:d} pdf={1:d}",b, n);
  998. leProb->save_moments_to_obs(leParameters, obs, obscov);
  999. spdlog::info("[MoM]\tDone for bin={0:d} pdf={1:d}",b, n);
  1000. }
  1001. }
  1002. //plot all four PDFs:
  1003. for(UInt_t n = 0; n < nPDFs; n++){
  1004. if((SimultaneousMoM || OneFit) && n > 0)
  1005. continue;
  1006. std::ostringstream qqbin;
  1007. std::ostringstream eps_label;
  1008. std::cout << "[PLOT]\tStart configurations for plotting!" << std::endl;
  1009. if(fitReference) eps_label << "JpsiFit";
  1010. else if(fitSignalMC) eps_label << "FCNC_SignalfitSignalMC_fit_bin" << std::to_string(b);
  1011. else eps_label << "FCNCfit_bin" << std::to_string(b);
  1012. if (LargeBinning) eps_label << "_LargeBins";
  1013. else if(Fit2bins) eps_label << "_2BINS";
  1014. else if(Fit1bin) eps_label << "_1BIN";
  1015. else if(FitAllbins) eps_label << "_9BINS";
  1016. if(SimultaneousMoM || OneFit) eps_label << "_FullDataSet";
  1017. else eps_label << (n % 2 == 0 ? "_DD_Run" : "_LL_Run") << std::to_string( (int) (1+n/2));
  1018. eps_label << "_MoM";
  1019. theOptions[n].plot_label = "LHCb data";
  1020. theOptions[n].q2_label = q2_label(theOptions[n].TheQ2binsmin.at(b),theOptions[n].TheQ2binsmax.at(b));
  1021. if(opts.write_eps){
  1022. bool wf = theOptions[n].weighted_fit;
  1023. bool mf = theOptions[n].only_mass2DFit;
  1024. bool oa = theOptions[n].only_angles;
  1025. bool el = theOptions[n].extended_ml;
  1026. theOptions[n].weighted_fit = true; //only for the plotter
  1027. theOptions[n].only_mass2DFit = false; //only for the plotter
  1028. theOptions[n].only_angles = true; //only for the plotter
  1029. theOptions[n].extended_ml = false;//only for the plotter
  1030. std::cout << "[PLOT]\t" << eps_label.str() << std::endl;
  1031. if(SimultaneousMoM || OneFit)
  1032. thePlotter[n]->plot_data((fcnc::bu2kstarmumu_pdf*)theProbs[b].at(n), (fcnc::bu2kstarmumu_parameters*)theParams[b].at(n), &allEvents, get_MoMPlot_path(), eps_label.str(), true);
  1033. else
  1034. thePlotter[n]->plot_data((fcnc::bu2kstarmumu_pdf*)theProbs[b].at(n), (fcnc::bu2kstarmumu_parameters*)theParams[b].at(n), selection[b].at(n), get_MoMPlot_path(), eps_label.str(), true);
  1035. theOptions[n].weighted_fit = wf;
  1036. theOptions[n].only_mass2DFit = mf;
  1037. theOptions[n].only_angles = oa;
  1038. theOptions[n].extended_ml = el;
  1039. }
  1040. } //end loop PDFs
  1041. } //end loop over bins
  1042. } //end do_mom
  1043. bool print_parameters = false;
  1044. //print and save-to-.txt of all parameters:
  1045. std::cout << std::endl;
  1046. std::cout << "==========================================" << std::endl;
  1047. std::cout << "|| PARAMETERS ||" << std::endl;
  1048. std::cout << "==========================================" << std::endl;
  1049. for(UInt_t b = 0; b < nBins; b++){
  1050. for(UInt_t i = 0; i < (SimultaneousMoM || OneFit ? 1 : pdf_idx.size()); i++){
  1051. if(print_parameters){
  1052. std::cout << "|| BIN " << b << "\tPDF " << pdf_idx.at(i) << std::endl;
  1053. theParams[b].at(i)->print_parameters(false);
  1054. }
  1055. std::ostringstream oss;
  1056. oss << "momresult";
  1057. if(fitReference) oss << "_JpsiFit";
  1058. else oss << "_SignalFit";
  1059. if(fitSignalMC) oss << "_fitSignalMC";
  1060. if(SimultaneousFit) oss << "_SimultaneousFit";
  1061. else oss << "_IndividualFit";
  1062. oss << "_2Dfit";
  1063. if(!opts.full_angular) oss << "_folding" << opts.folding;
  1064. if(nBins > 1) oss << "_bin" << b;
  1065. if (LargeBinning) oss << "_LargeBins";
  1066. else if(Fit2bins) oss << "_2BINS";
  1067. else if(Fit1bin) oss << "_1BIN";
  1068. else if(FitAllbins) oss << "_9BINS";
  1069. if(FitOnlyMagDown) oss << "_OnlyMagDown";
  1070. if(FitOnlyMagUp) oss << "_OnlyMagUp";
  1071. if(!(SimultaneousMoM || OneFit)) oss << "_pdf" << i;
  1072. oss << ".txt";
  1073. std::vector<fcnc::bu2kstarmumu_parameters*> * paramser = (std::vector<fcnc::bu2kstarmumu_parameters*> *) & theParams[b];
  1074. if(!(SystematicsWithToys || FitFinalToySamples))
  1075. paramser->at(i)->save_param_values(oss.str());
  1076. }
  1077. }
  1078. //print all fitresults:
  1079. std::cout << std::endl;
  1080. std::cout << "==========================================" << std::endl;
  1081. std::cout << "|| FIT RESULTS ||" << std::endl;
  1082. std::cout << "==========================================" << std::endl;
  1083. if(SimultaneousFit)
  1084. std::cout << "|| BIN\t\tfit result\tF_S ||" << std::endl;
  1085. else
  1086. std::cout << "|| BIN\t\tPDF\t\tfit result\tF_S ||" << std::endl;
  1087. for(UInt_t b = 0; b < nBins; b++){
  1088. if(SimultaneousFit || OneFit){
  1089. if(opts.minos_errors)
  1090. printf("|| %d\t\t%d\t\t%f (+)%f\t (-)%f\t||\n", b, fit_results[b].at(0), fitted_FS[b].at(0), fitted_FSerrUp[b].at(0), fitted_FSerrDown[b].at(0));
  1091. else
  1092. printf("|| %d\t\t%d\t\t%f +/- %f\t||\n", b, fit_results[b].at(0), fitted_FS[b].at(0), fitted_FSerr[b].at(0));
  1093. }
  1094. else{
  1095. for(UInt_t i = 0; i < pdf_idx.size(); i++){
  1096. if(opts.minos_errors)
  1097. printf("|| %d\t\t%d\t\t%d\t\t%f (+)%f\t (-)%f\t||\n", b, pdf_idx.at(i), fit_results[b].at(i), fitted_FS[b].at(i), fitted_FSerr[b].at(i), fitted_FSerrDown[b].at(i));
  1098. else
  1099. printf("|| %d\t\t%d\t\t%d\t\t%f +/- %f\t||\n", b, pdf_idx.at(i), fit_results[b].at(i), fitted_FS[b].at(i), fitted_FSerr[b].at(i));
  1100. }
  1101. }
  1102. }
  1103. std::cout << std::endl;
  1104. //save all values to TTree:
  1105. std::ostringstream sout;
  1106. sout << "finalresults_";
  1107. if(fitSignalMC) sout << "fitSignalMC_";
  1108. else if(fitReference) sout << "Jpsi_";
  1109. if(SystematicsWithToys) sout << "OnlyToys" << opts.job_id << "_";
  1110. if(FitFinalToySamples) sout << "FinalToys" << opts.job_id << "_";
  1111. if(FitOnlyMagDown) sout << "OnlyMagDown_";
  1112. if(FitOnlyMagUp) sout << "OnlyMagUp_";
  1113. if(LargeBinning) sout << "LargeBins_";
  1114. if(NP) sout << "NP_";
  1115. sout << "MoM.root";
  1116. TFile* fout = new TFile(sout.str().c_str(), "RECREATE");
  1117. fout->cd();
  1118. int param_index = 0;
  1119. //loop over parameters:
  1120. UInt_t pdf_null_idx = (SimultaneousMoM || OneFit || SimultaneousFit ? 0 : nPDFs-1);
  1121. for(UInt_t p = 0; p < theParams[0].at(pdf_null_idx)->nparameters(); p++){
  1122. fcnc::parameter* param = theParams[0].at(pdf_null_idx)->get_parameter(p);
  1123. if(param->get_step_size() != 0.0){//skip constant parameters
  1124. std::string parname(param->get_name());
  1125. std::string pardesc(param->get_description());
  1126. spdlog::info("Creating new TTree for parameter:"+ parname);
  1127. TTree* t = new TTree(parname.c_str(), pardesc.c_str());
  1128. double q2a, q2b, value, error, error_up, error_down, start_value, nominal_value, nominal_error=0.0;
  1129. int pdf, bin, migrad, status_cov;
  1130. const unsigned int corr_max = param->correlations.size();
  1131. double tmp_corr[corr_max];
  1132. t->Branch("pdf",&pdf,"pdf/I");
  1133. t->Branch("bin",&bin,"bin/I");
  1134. t->Branch("value",&value,"value/D");
  1135. t->Branch("q2min",&q2a,"q2min/D");
  1136. t->Branch("q2max",&q2b,"q2max/D");
  1137. t->Branch("error",&error,"error/D");
  1138. t->Branch("error_up",&error_up,"error_up/D");
  1139. t->Branch("error_down",&error_down,"error_down/D");
  1140. t->Branch("start_value",&start_value,"start_value/D");
  1141. t->Branch("migrad",&migrad,"migrad/I");
  1142. t->Branch("status_cov",&status_cov,"status_cov/I");
  1143. t->Branch("nominal_value",&nominal_value,"nominal_value/D");
  1144. t->Branch("nominal_error",&nominal_error,"nominal_error/D");
  1145. t->Branch("index",&param_index, "index/I");
  1146. std::stringstream corr_stream;
  1147. corr_stream << "correlations[" << corr_max << "]/D";
  1148. t->Branch("correlations",&tmp_corr,corr_stream.str().c_str());
  1149. UInt_t startPDF = 0;
  1150. UInt_t stopPDF = pdf_idx.size();
  1151. if(SimultaneousMoM)
  1152. startPDF = stopPDF - 1;
  1153. else if(OneFit)
  1154. stopPDF = 1;
  1155. for(UInt_t i = startPDF; i < stopPDF; i++){
  1156. pdf = i + 1;
  1157. for(UInt_t b = 0; b < nBins; b++){
  1158. fcnc::parameter* par = theParams[b].at(i)->get_parameter(p);
  1159. UInt_t paridx = 0;
  1160. while(par->get_name() != parname.c_str()){
  1161. par = theParams[b].at(i)->get_parameter(paridx);
  1162. paridx++;
  1163. if(paridx == theParams[b].at(i)->nparameters()){
  1164. spdlog::warn("Parameter '"+parname+"' not found in PDF{0:d}. Continue with next PDF.",i);
  1165. break;
  1166. }
  1167. }
  1168. if(paridx == theParams[b].at(i)->nparameters())
  1169. break;
  1170. bin = b;
  1171. q2a = opts.TheQ2binsmin[b];
  1172. q2b = opts.TheQ2binsmax[b];
  1173. value = par->get_value();
  1174. error = par->get_error();
  1175. error_up = par->get_error_up();
  1176. error_down = par->get_error_down();
  1177. start_value = par->get_start_value();
  1178. nominal_value = par->get_start_value();
  1179. if(SimultaneousFit){
  1180. migrad = fit_results[b].at(0) % 100;
  1181. status_cov = fit_results[b].at(0) / 100;
  1182. }
  1183. else{
  1184. migrad = fit_results[b].at(i) % 100;
  1185. status_cov = fit_results[b].at(i) / 100;
  1186. }
  1187. if (opts.minos_errors){
  1188. if (error_up==0.0 && value < par->get_max())
  1189. error_up = par->get_max() - value;
  1190. if (error_down==0.0 && value > par->get_min())
  1191. error_down = par->get_min() - value;//needs to be negative
  1192. }
  1193. for (unsigned int l = 0; l < corr_max; l++)
  1194. tmp_corr[l] = 0.0;
  1195. for (unsigned int l = 0; l < par->correlations.size(); l++)
  1196. tmp_corr[l] = par->correlations.at(l);
  1197. t->Fill();
  1198. }
  1199. }
  1200. t->Write();
  1201. delete t;
  1202. param_index++;
  1203. }
  1204. }
  1205. fout->Close();
  1206. return 0;
  1207. }