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.

135 lines
4.9 KiB

  1. /**
  2. * @file fitter.hh
  3. * @author Christoph Langenbruch, Renata Kopecna
  4. * @date 2010-10-13
  5. *
  6. */
  7. #ifndef FITTER_H
  8. #define FITTER_H
  9. #include <string>
  10. #include <vector>
  11. #include <mutex>
  12. #include <TMinuit.h>
  13. namespace fcnc {
  14. class event;
  15. class parameter;
  16. class parameters;
  17. class pdf;
  18. class plotter;
  19. class options;
  20. class fitter;
  21. ///gives wether a i (absolutely) larger than b
  22. template<class T> struct GreaterThan{
  23. bool operator()(const T a, const T b) const
  24. { return std::abs(a) > std::abs(b); }
  25. };
  26. ///adds up all probability values in an efficient and numerically optimal manner
  27. template<typename iterator> typename iterator::value_type add_results(iterator begin, iterator end){
  28. typedef typename iterator::value_type T;
  29. if (end == begin) return 0.;
  30. std::make_heap(begin, end, GreaterThan<T>());
  31. for (; begin + 1 < end; ) {
  32. T x = *begin;
  33. std::pop_heap(begin, end--, GreaterThan<T>());
  34. do {
  35. x += *begin;
  36. std::pop_heap(begin, end--, GreaterThan<T>());
  37. } while (std::abs(*begin) >= std::abs(x) && begin < end);
  38. *end++ = x;
  39. std::push_heap(begin, end, GreaterThan<T>());
  40. }
  41. return *begin;
  42. }
  43. ///The fitter class implements an unbinned multithreaded maximum likelihood fit
  44. class fitter {
  45. public:
  46. ///global mutex for use of threads
  47. //static boost::mutex fitter_mutex;
  48. static std::mutex fitter_mutex;
  49. ///global fitter object for minuit
  50. static fitter* minuit_one_fitter;
  51. ///empirical constant which is subtracted to improve the numerical stability of the fit
  52. double empirical_constant;
  53. ///the minuit 1 minimizer object
  54. TMinuit* minuit_one;
  55. ///pointer to events to fit
  56. std::vector< std::vector<event>* > events;
  57. ///pointer to parameter sets to fit
  58. std::vector< parameters* > params;
  59. ///common parameters in different parameter sets
  60. std::vector< std::string > common_params;
  61. //plotters
  62. std::vector< plotter* > plots;
  63. ///pointer to the pdf(s)
  64. std::vector<pdf*> pdfs;
  65. ///pointer to the options object
  66. options* opts;
  67. ///if true, this fitter has already been initialized
  68. bool initialized;
  69. ///every thread adds the result of their likelihood calculation to this
  70. long double lh_tot;
  71. ///use square of weights for error correction in weighted fits
  72. bool square_weights;
  73. public:
  74. ///minuit one FCN
  75. static void minuit_one_fcn(Int_t &npar, Double_t *grad, Double_t &lh, Double_t *params, Int_t iflag);
  76. ///constructor
  77. fitter(options* o);
  78. ///destructor
  79. ~fitter();
  80. /** fit the parameter set to the events using the pdf
  81. * @param probability pointer to pdf to use in the likelihood fit
  82. * @param parameters pointer to parameterset to use in the likelihood fit
  83. * @param events pointer to a vector of events for which to maximise the likelihood
  84. * @param index optional parameter to give the current run of a toystudy **/
  85. int fit(std::vector<pdf*> probs, std::vector<parameters*> parms, std::vector< std::vector<event> * > ev, std::string index="");
  86. /** fit the parameter set to the events using the pdf
  87. * @param vector of probability pointers to pdfs to use in the likelihood fit
  88. * @param vector of pointers to parametersets to use in the likelihood fit
  89. * @param vector of pointers to vector of events for which to maximise the likelihood
  90. * @param index optional parameter to give the current run of a toystudy **/
  91. int fit(pdf* probs, parameters* parms, std::vector<event>* ev, std::string index="");
  92. //set pdfs, parameters and events to extract likelihoods manually:
  93. void init(std::vector<pdf*> probs, std::vector<parameters*> parms, std::vector< std::vector<event> * > ev, std::string index="");
  94. void init(pdf* probs, parameters* parms, std::vector<event>* ev, std::string index="");
  95. ///add a parameter to minuit
  96. void define_parameter(int i, const parameter & p);
  97. ///reset all parameters to the start values
  98. void reset_param_values();
  99. ///calculates log (prod(p_i)) = sum lh(p_i), calls likelihood_thread for every thread
  100. double likelihood();
  101. ///calculates the log pdf for a single thread
  102. void likelihood_thread(int thread_id);
  103. //set plotter
  104. void set_plotters(plotter* plot);
  105. ///set plotters
  106. void set_plotters(std::vector<plotter*> plotters);
  107. ///define common parameters for all pdfs
  108. void set_common_parameters(std::vector<std::string> common_pars);
  109. ///is the parameter with name one of the common parameters
  110. bool is_common(std::string name);
  111. ///is this the first occurence of the parameter?
  112. bool is_first(std::string name, unsigned int idx_i, unsigned int idx_j);
  113. ///returns the nth minuit index for parameter with name
  114. int nth_minuit_index(std::string name, unsigned int n);
  115. ///returns the first minuit index for parameter with name
  116. int first_minuit_index(std::string name);
  117. ///returns wether this is an analysis with blinded parameters
  118. bool is_blind();
  119. };
  120. }//end namespace
  121. //Print migrad status with explanation
  122. void printMigradStatus(int status);
  123. #endif