Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-03 08:20:16

0001 /*
0002  * This file is part of KFParticle package
0003  * Copyright (C) 2007-2019 FIAS Frankfurt Institute for Advanced Studies
0004  *               2007-2019 Goethe University of Frankfurt
0005  *               2007-2019 Ivan Kisel <I.Kisel@compeng.uni-frankfurt.de>
0006  *               2007-2019 Maksym Zyzak
0007  *
0008  * KFParticle is free software: you can redistribute it and/or modify
0009  * it under the terms of the GNU General Public License as published by
0010  * the Free Software Foundation, either version 3 of the License, or
0011  * (at your option) any later version.
0012  *
0013  * KFParticle is distributed in the hope that it will be useful,
0014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016  * GNU General Public License for more details.
0017  *
0018  * You should have received a copy of the GNU General Public License
0019  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
0020  */
0021 
0022 #ifndef KFMCCounter_H
0023 #define KFMCCounter_H
0024 
0025 #include <iostream>
0026 #include <fstream>
0027 #include <vector>
0028 
0029 /** @class KFMCCounter
0030  ** @brief A helper structure to store information on the number of reconstructed and Monte Carlo particles for efficiency calculation.
0031  ** @author  M.Zyzak, I.Kisel
0032  ** @date 05.02.2019
0033  ** @version 1.0
0034  **
0035  ** The class is used to calculate reconstruction efficiency and ratios of a given set of particles.
0036  **/
0037 
0038 template <typename T>
0039 struct KFMCCounter
0040 {
0041   int NCounters; ///< Number of counters in the current object.
0042   
0043   std::vector<T> counters; ///< Counters of different set of particles.
0044 
0045   KFMCCounter():NCounters(0),counters(0) { }
0046   KFMCCounter(int nCounters):NCounters(nCounters), counters(nCounters,T(0)) { } ///< Constructs the object with the set of counters "nCounters".
0047 
0048   void AddCounter(){ NCounters++; counters.push_back(T(0)); } ///< Adds a counter to the existing list.
0049   void AddCounters(int nCounters){ NCounters += nCounters; counters.resize( NCounters, T(0)); } ///< Adds several counters to the existing list.
0050   
0051   /** Operator adds all counters from object "a" to the current object. Returns the current object. */
0052   KFMCCounter& operator+=(KFMCCounter& a){
0053     if (NCounters != a.NCounters){
0054       std::cout << " KFMCCounter: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << std::endl;
0055     }
0056     else{
0057       for (int iC = 0; iC < NCounters; iC++){
0058         counters[iC] += a.counters[iC];
0059       }
0060     }
0061     return *this;
0062   };
0063   /** Operator adds all counters from object "a" to the current object, result is stored to the temporary object. Returns the temporary object. */
0064   KFMCCounter operator+(KFMCCounter& a){
0065     KFMCCounter res = *this;
0066     res += a;
0067     return res;
0068   };
0069   /** 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. */
0070   template <typename T2>
0071   KFMCCounter<double> operator/(KFMCCounter<T2>& a){
0072     KFMCCounter<double> b(NCounters);
0073     if (NCounters != a.NCounters){
0074       std::cout << " KFMCCounter: Error. Addition of counters of different sizes: " << NCounters << " " << a.NCounters << std::endl;
0075     }
0076     else{
0077       for (int iC = 0; iC < NCounters; iC++){
0078         b.counters[iC] = Div(counters[iC],a.counters[iC]);
0079       }
0080     }
0081     return b;
0082   }
0083   /** Operator divides all counters from the current object to the value "a", result is stored to the temporary object. Returns the temporary object. */
0084   template <typename T2>
0085   KFMCCounter<T2> operator/(double a){
0086     KFMCCounter<T2> b(NCounters);
0087     for (int iC = 0; iC < NCounters; iC++){
0088       b.counters[iC] = (T2)Div(counters[iC],a);
0089     }
0090     return b;
0091   }
0092   /** Operator to write the object "a" to the file "strm".*/
0093   friend std::fstream & operator<<(std::fstream &strm, const KFMCCounter<T> &a ){
0094     strm << a.NCounters << " " << a.counters.size() << " ";
0095     for(unsigned int iV=0; iV<a.counters.size(); iV++)
0096       strm << a.counters[iV] << " ";
0097     strm << std::endl;
0098     return strm;
0099   }
0100   /** Operator to write the object "a" to the stream "strm".*/
0101   friend std::ostream & operator<<(std::ostream &strm, const KFMCCounter<T> &a ){
0102     strm << a.NCounters << " " << a.counters.size() << " ";
0103     for(unsigned int iV=0; iV<a.counters.size(); iV++)
0104       strm << a.counters[iV] << " ";
0105     strm << std::endl;
0106     return strm;
0107   }
0108   /** Operator to read the object "a" from the file "strm".*/
0109   friend std::fstream & operator>>(std::fstream &strm, KFMCCounter<T> &a ){
0110     int tmp;
0111     strm >> tmp;
0112     a.NCounters = tmp;
0113     strm >> tmp;
0114     a.counters.resize(tmp,T(0));
0115     for(int iV=0; iV<tmp; iV++)
0116     {
0117       T tmp1;
0118       strm >> tmp1;
0119       a.counters[iV] = tmp1;
0120     }
0121     return strm;
0122   }
0123 
0124   private:
0125     /** Divides value "a" on value "b" if b is not zero, otherwise returns "-1". */
0126     double Div(double a, double b){return (b > 0) ? a/b : -1.;}
0127 };
0128 
0129 #endif