KFMCCounter.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 KFMCCounter_H
  12. #define KFMCCounter_H
  13. #include <iostream>
  14. #include <fstream>
  15. #include <vector>
  16. /** @class KFMCCounter
  17. ** @brief A helper structure to store information on the number of reconstructed and Monte Carlo particles for efficiency calculation.
  18. ** @author M.Zyzak, I.Kisel
  19. ** @date 05.02.2019
  20. ** @version 1.0
  21. **
  22. ** The class is used to calculate reconstruction efficiency and ratios of a given set of particles.
  23. **/
  24. template <typename T>
  25. struct KFMCCounter
  26. {
  27. int NCounters; ///< Number of counters in the current object.
  28. std::vector<T> counters; ///< Counters of different set of particles.
  29. KFMCCounter():NCounters(0),counters(0) { }
  30. KFMCCounter(int nCounters):NCounters(nCounters), counters(nCounters,T(0)) { } ///< Constructs the object with the set of counters "nCounters".
  31. void AddCounter(){ NCounters++; counters.push_back(T(0)); } ///< Adds a counter to the existing list.
  32. void AddCounters(int nCounters){ NCounters += nCounters; counters.resize( NCounters, T(0)); } ///< Adds several counters to the existing list.
  33. /** Operator adds all counters from object "a" to the current object. Returns the current object. */
  34. KFMCCounter& operator+=(KFMCCounter& a){
  35. if (NCounters != a.NCounters){
  36. std::cout << " KFMCCounter: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << std::endl;
  37. }
  38. else{
  39. for (int iC = 0; iC < NCounters; iC++){
  40. counters[iC] += a.counters[iC];
  41. }
  42. }
  43. return *this;
  44. };
  45. /** Operator adds all counters from object "a" to the current object, result is stored to the temporary object. Returns the temporary object. */
  46. KFMCCounter operator+(KFMCCounter& a){
  47. KFMCCounter res = *this;
  48. res += a;
  49. return res;
  50. };
  51. /** Operator divides all counters from the current object to the counters from object "a", result is stored to the temporary object. Returns the temporary object. */
  52. template <typename T2>
  53. KFMCCounter<double> operator/(KFMCCounter<T2>& a){
  54. KFMCCounter<double> b(NCounters);
  55. if (NCounters != a.NCounters){
  56. std::cout << " KFMCCounter: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << std::endl;
  57. }
  58. else{
  59. for (int iC = 0; iC < NCounters; iC++){
  60. b.counters[iC] = Div(counters[iC],a.counters[iC]);
  61. }
  62. }
  63. return b;
  64. }
  65. /** Operator divides all counters from the current object to the value "a", result is stored to the temporary object. Returns the temporary object. */
  66. template <typename T2>
  67. KFMCCounter<T2> operator/(double a){
  68. KFMCCounter<T2> b(NCounters);
  69. for (int iC = 0; iC < NCounters; iC++){
  70. b.counters[iC] = (T2)Div(counters[iC],a);
  71. }
  72. return b;
  73. }
  74. /** Operator to write the object "a" to the file "strm".*/
  75. friend std::fstream & operator<<(std::fstream &strm, const KFMCCounter<T> &a ){
  76. strm << a.NCounters << " " << a.counters.size() << " ";
  77. for(unsigned int iV=0; iV<a.counters.size(); iV++)
  78. strm << a.counters[iV] << " ";
  79. strm << std::endl;
  80. return strm;
  81. }
  82. /** Operator to write the object "a" to the stream "strm".*/
  83. friend std::ostream & operator<<(std::ostream &strm, const KFMCCounter<T> &a ){
  84. strm << a.NCounters << " " << a.counters.size() << " ";
  85. for(unsigned int iV=0; iV<a.counters.size(); iV++)
  86. strm << a.counters[iV] << " ";
  87. strm << std::endl;
  88. return strm;
  89. }
  90. /** Operator to read the object "a" from the file "strm".*/
  91. friend std::fstream & operator>>(std::fstream &strm, KFMCCounter<T> &a ){
  92. int tmp;
  93. strm >> tmp;
  94. a.NCounters = tmp;
  95. strm >> tmp;
  96. a.counters.resize(tmp,T(0));
  97. for(int iV=0; iV<tmp; iV++)
  98. {
  99. T tmp1;
  100. strm >> tmp1;
  101. a.counters[iV] = tmp1;
  102. }
  103. return strm;
  104. }
  105. private:
  106. /** Divides value "a" on value "b" if b is not zero, otherwise returns "-1". */
  107. double Div(double a, double b){return (b > 0) ? a/b : -1.;}
  108. };
  109. #endif