KFPVEfficiencies.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //----------------------------------------------------------------------------
  2. // Implementation of the KFParticle class
  3. // .
  4. // @author I.Kisel, I.Kulakov, M.Zyzak
  5. // @version 1.0
  6. // @since 20.08.13
  7. //
  8. //
  9. // -= Copyright &copy ALICE HLT and CBM L1 Groups =-
  10. //____________________________________________________________________________
  11. #ifndef KFPVEfficiencies_H
  12. #define KFPVEfficiencies_H
  13. #ifndef HLTCA_STANDALONE
  14. #include "TNamed.h"
  15. #endif
  16. #include <map>
  17. #include <iomanip>
  18. #include "KFMCCounter.h"
  19. /** @class KFPVEfficiencies
  20. ** @brief Class to calculate efficiency of KF Particle Finder.
  21. ** @author M.Zyzak, I.Kisel
  22. ** @date 05.02.2019
  23. ** @version 1.0
  24. **
  25. ** The class calculates reconstruction efficiency of the primary vertices.\n
  26. ** Definitions:\n
  27. ** background - physics background, when daughter particle come from the secondary vertex;\n
  28. ** ghost - combinatorial background, tracks do not form a real vertex;\n
  29. ** clone - a vertex is reconstructed several times, for example, half of tracks form one group and
  30. ** another half form second group.
  31. **/
  32. class KFPVEfficiencies: public TNamed
  33. {
  34. public:
  35. KFPVEfficiencies():
  36. names(),
  37. indices(),
  38. ratio_reco(),
  39. mc(),
  40. reco(),
  41. ratio_ghost(),
  42. ratio_bg(),
  43. ratio_clone(),
  44. ghost(),
  45. bg(),
  46. clone()
  47. {
  48. AddCounter(Form("%s","PV"), Form("%-*s",12,"PV"));
  49. AddCounter(Form("%s","PVtrigger"), Form("%-*s",12,"PV trigger"));
  50. AddCounter(Form("%s","PVpileup"), Form("%-*s",12,"PV pileup "));
  51. }
  52. virtual ~KFPVEfficiencies(){};
  53. virtual void AddCounter(TString shortname, TString name)
  54. {
  55. /** Adds a counter with the name defined by "name" to all counter
  56. ** objects. For easiness of operation with counters, a shortname is assigned
  57. ** to each of them and the corresponding entry in the map indices is done.
  58. ** \param[in] shortname - a short name of the counter for fast and easy access to its index
  59. ** \param[in] name - name of the counter which is added to each counter object.
  60. **/
  61. indices[shortname] = names.size();
  62. names.push_back(name);
  63. ratio_reco.AddCounter();
  64. mc.AddCounter();
  65. reco.AddCounter();
  66. ratio_ghost.AddCounter();
  67. ratio_bg.AddCounter();
  68. ratio_clone.AddCounter();
  69. ghost.AddCounter();
  70. bg.AddCounter();
  71. clone.AddCounter();
  72. };
  73. /** \brief Operator to add efficiency table from object "a" to the current object. Returns the current object after addition. */
  74. KFPVEfficiencies& operator+=(KFPVEfficiencies& a){
  75. mc += a.mc; reco += a.reco;
  76. ghost += a.ghost; bg += a.bg; clone += a.clone;
  77. return *this;
  78. };
  79. /** \brief Function to calculate efficiency after all counters are set. If the counters are modified the function should be called again. */
  80. void CalcEff(){
  81. ratio_reco = reco/mc;
  82. KFMCCounter<int> allReco = reco + ghost + bg;
  83. ratio_ghost = ghost/allReco;
  84. ratio_bg = bg/allReco;
  85. ratio_clone = clone/allReco;
  86. };
  87. void Inc(bool isReco, int nClones, TString name)
  88. {
  89. /** Increases counters by one, if the corresponding boolean variable is "true".
  90. ** MC counter is increased in any case.
  91. ** \param[in] isReco - "true" if vertex is reconstructed
  92. ** \param[in] nClones - number of double reconstructed vertices for the given MC vertex,
  93. ** will be added to the "clone" counters
  94. ** \param[in] name - "shortname" of the set of counters, which should be increased
  95. **/
  96. const int index = indices[name];
  97. mc.counters[index]++;
  98. if (isReco) reco.counters[index]++;
  99. if(nClones > 0)
  100. clone.counters[index] += nClones;
  101. };
  102. void IncReco(bool isGhost, bool isBg, TString name)
  103. {
  104. /** Increases counters by one, if the corresponding boolean variable is "true".
  105. ** \param[in] isGhost - "true" if ghost is added
  106. ** \param[in] isBg - "true" if physics background is added
  107. ** \param[in] name - "shortname" of the set of counters, which should be increased
  108. **/
  109. const int index = indices[name];
  110. if (isGhost) ghost.counters[index]++;
  111. if (isBg) bg.counters[index]++;
  112. };
  113. /** \brief Prints the efficiency table on the screen. */
  114. void PrintEff(){
  115. std::ios_base::fmtflags original_flags = std::cout.flags();
  116. std::cout.setf(std::ios::fixed);
  117. std::cout.setf(std::ios::showpoint);
  118. std::cout.precision(3);
  119. std::cout << " : "
  120. << " Eff "
  121. <<" / "<< " Ghost "
  122. <<" / "<< "BackGr "
  123. <<" / "<< "Clone "
  124. <<" / "<< "N Ghost"
  125. <<" / "<< "N BackGr"
  126. <<" / "<< "N Reco "
  127. <<" / "<< "N Clone "
  128. <<" | "<< " N MC " << std::endl;
  129. int NCounters = mc.NCounters;
  130. for (int iC = 0; iC < NCounters; iC++){
  131. std::cout << names[iC]
  132. << " : " << std::setw(6) << ratio_reco.counters[iC]
  133. << " / " << std::setw(6) << ratio_ghost.counters[iC] // particles w\o MCParticle
  134. << " / " << std::setw(6) << ratio_bg.counters[iC] // particles with incorrect MCParticle
  135. << " / " << std::setw(6) << ratio_clone.counters[iC] // particles with incorrect MCParticle
  136. << " / " << std::setw(6) << ghost.counters[iC]
  137. << " / " << std::setw(7) << bg.counters[iC]
  138. << " / " << std::setw(6) << reco.counters[iC]
  139. << " / " << std::setw(7) << clone.counters[iC]
  140. << " | " << std::setw(6) << mc.counters[iC] << std::endl;
  141. }
  142. std::cout.flags(original_flags);
  143. };
  144. /** \brief Operator to write efficiencies to file. */
  145. friend std::fstream & operator<<(std::fstream &strm, KFPVEfficiencies &a) {
  146. strm << a.ratio_reco;
  147. strm << a.mc;
  148. strm << a.reco;
  149. strm << a.ratio_ghost;
  150. strm << a.ratio_bg;
  151. strm << a.ratio_clone;
  152. strm << a.ghost;
  153. strm << a.bg;
  154. strm << a.clone;
  155. return strm;
  156. }
  157. /** \brief Operator to read efficiencies from file. */
  158. friend std::fstream & operator>>(std::fstream &strm, KFPVEfficiencies &a){
  159. strm >> a.ratio_reco;
  160. strm >> a.mc;
  161. strm >> a.reco;
  162. strm >> a.ratio_ghost;
  163. strm >> a.ratio_bg;
  164. strm >> a.ratio_clone;
  165. strm >> a.ghost;
  166. strm >> a.bg;
  167. strm >> a.clone;
  168. return strm;
  169. }
  170. /** \brief Adds efficiency from the file with the name defined by "fileName" to the current objects. */
  171. void AddFromFile(TString fileName)
  172. {
  173. std::fstream file(fileName.Data(),std::fstream::in);
  174. file >> *this;
  175. }
  176. private:
  177. std::vector<TString> names; ///< Names of the counters. The same for all counters objects.
  178. std::map<TString, int> indices; ///< Map between the counter index and its short name.
  179. KFMCCounter<double> ratio_reco; ///< Efficiency.
  180. KFMCCounter<int> mc; ///< Counters of the Monte Carlo vertices.
  181. KFMCCounter<int> reco; ///< Counters of the reconstructed vertices.
  182. KFMCCounter<double> ratio_ghost; ///< Ratio of the ghost candidates to the total number of candidates.
  183. KFMCCounter<double> ratio_bg; ///< Ratio of the physics background candidates to the total number of candidates.
  184. KFMCCounter<double> ratio_clone; ///< Ratio of double reconstructed vertices to the total number of signal candidates.
  185. KFMCCounter<int> ghost; ///< Counters of the ghost candidates.
  186. KFMCCounter<int> bg; ///< Counters of the physics background candidates.
  187. KFMCCounter<int> clone; ///< Counters of the double reconstructed vertices.
  188. };
  189. #endif