Back to home page

sPhenix code displayed by LXR

 
 

    


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

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 KFPVEfficiencies_H
0023 #define KFPVEfficiencies_H
0024 
0025 #ifndef HLTCA_STANDALONE
0026 #include "TNamed.h"
0027 #endif
0028 
0029 #include <map>
0030 #include <iomanip>
0031 #include "KFMCCounter.h"
0032 
0033 /** @class KFPVEfficiencies
0034  ** @brief Class to calculate efficiency of KF Particle Finder.
0035  ** @author  M.Zyzak, I.Kisel
0036  ** @date 05.02.2019
0037  ** @version 1.0
0038  **
0039  ** The class calculates reconstruction efficiency of the primary vertices.\n
0040  ** Definitions:\n
0041  ** background - physics background, when daughter particle come from the secondary vertex;\n
0042  ** ghost - combinatorial background, tracks do not form a real vertex;\n
0043  ** clone - a vertex is reconstructed several times, for example, half of tracks form one group and
0044  ** another half form second group.
0045  **/
0046 
0047 class KFPVEfficiencies: public TNamed
0048 {
0049  public:
0050 
0051   KFPVEfficiencies():
0052     names(),
0053     indices(),
0054     ratio_reco(),
0055     mc(),
0056     reco(),
0057     ratio_ghost(),
0058     ratio_bg(),
0059     ratio_clone(),
0060     ghost(),
0061     bg(),
0062     clone()
0063   {
0064     AddCounter(Form("%s","PV"), Form("%-*s",12,"PV"));
0065     AddCounter(Form("%s","PVtrigger"), Form("%-*s",12,"PV trigger"));
0066     AddCounter(Form("%s","PVpileup"), Form("%-*s",12,"PV pileup "));
0067   }
0068 
0069   virtual ~KFPVEfficiencies(){};
0070 
0071   virtual void AddCounter(TString shortname, TString name)
0072   {
0073     /** Adds a counter with the name defined by "name" to all counter
0074      ** objects. For easiness of operation with counters, a shortname is assigned
0075      ** to each of them and the corresponding entry in the map indices is done.
0076      ** \param[in] shortname - a short name of the counter for fast and easy access to its index
0077      ** \param[in] name - name of the counter which is added to each counter object.
0078      **/
0079     indices[shortname] = names.size();
0080     names.push_back(name);
0081 
0082     ratio_reco.AddCounter();
0083     mc.AddCounter();
0084     reco.AddCounter();
0085 
0086     ratio_ghost.AddCounter();
0087     ratio_bg.AddCounter();
0088     ratio_clone.AddCounter();
0089     ghost.AddCounter();
0090     bg.AddCounter();
0091     clone.AddCounter();
0092   };
0093 
0094   /** \brief Operator to add efficiency table from object "a" to the current object. Returns the current object after addition. */
0095   KFPVEfficiencies& operator+=(KFPVEfficiencies& a){
0096     mc += a.mc; reco += a.reco;
0097     ghost += a.ghost; bg += a.bg; clone += a.clone;
0098     return *this;
0099   };
0100   
0101   /** \brief Function to calculate efficiency after all counters are set. If the counters are modified the function should be called again. */
0102   void CalcEff(){
0103     ratio_reco = reco/mc;
0104 
0105     KFMCCounter<int> allReco = reco + ghost + bg;
0106     ratio_ghost = ghost/allReco;
0107     ratio_bg  = bg/allReco;
0108     ratio_clone  = clone/allReco;
0109   };
0110   
0111 
0112   void Inc(bool isReco, int nClones, TString name)
0113   {
0114     /** Increases counters by one, if the corresponding boolean variable is "true".
0115      ** MC counter is increased in any case.
0116      ** \param[in] isReco - "true" if vertex is reconstructed
0117      ** \param[in] nClones - number of double reconstructed vertices for the given MC vertex,
0118      ** will be added to the "clone" counters
0119      ** \param[in] name  - "shortname" of the set of counters, which should be increased
0120      **/
0121     const int index = indices[name];
0122     
0123     mc.counters[index]++;
0124     if (isReco) reco.counters[index]++;
0125     if(nClones > 0)
0126       clone.counters[index] += nClones;
0127   };
0128 
0129   void IncReco(bool isGhost, bool isBg, TString name)
0130   {
0131     /** Increases counters by one, if the corresponding boolean variable is "true".
0132      ** \param[in] isGhost - "true" if ghost is added
0133      ** \param[in] isBg - "true" if physics background is added
0134      ** \param[in] name  - "shortname" of the set of counters, which should be increased
0135      **/
0136     const int index = indices[name];
0137 
0138     if (isGhost) ghost.counters[index]++;
0139     if (isBg)    bg.counters[index]++;
0140   };
0141 
0142   /** \brief Prints the efficiency table on the screen. */
0143   void PrintEff(){
0144     std::ios_base::fmtflags original_flags = std::cout.flags();
0145     std::cout.setf(std::ios::fixed);
0146     std::cout.setf(std::ios::showpoint);
0147     std::cout.precision(3);
0148     std::cout << "              : "
0149          << "   Eff "
0150          <<" / "<< " Ghost "
0151          <<" / "<< "BackGr "
0152          <<" / "<< "Clone  "
0153          <<" / "<< "N Ghost"
0154          <<" / "<< "N BackGr"
0155          <<" / "<< "N Reco "
0156          <<" / "<< "N Clone "
0157          <<" | "<< "  N MC "  << std::endl;
0158     
0159     int NCounters = mc.NCounters;
0160     for (int iC = 0; iC < NCounters; iC++){
0161         std::cout << names[iC]
0162              << "  : " << std::setw(6) << ratio_reco.counters[iC]              
0163              << "  / " << std::setw(6) << ratio_ghost.counters[iC]  // particles w\o MCParticle
0164              << "  / " << std::setw(6) << ratio_bg.counters[iC]     // particles with incorrect MCParticle
0165              << "  / " << std::setw(6) << ratio_clone.counters[iC]     // particles with incorrect MCParticle
0166              << "  / " << std::setw(6) << ghost.counters[iC]
0167              << "  / " << std::setw(7) << bg.counters[iC]
0168              << "  / " << std::setw(6) << reco.counters[iC]
0169              << "  / " << std::setw(7) << clone.counters[iC]
0170              << "  | " << std::setw(6) << mc.counters[iC]  << std::endl;
0171     }
0172     std::cout.flags(original_flags); 
0173   };
0174   
0175   /** \brief Operator to write efficiencies to file. */
0176   friend std::fstream & operator<<(std::fstream &strm, KFPVEfficiencies &a) {
0177 
0178     strm << a.ratio_reco;
0179     strm << a.mc;
0180     strm << a.reco;
0181     strm << a.ratio_ghost;
0182     strm << a.ratio_bg;
0183     strm << a.ratio_clone;
0184     strm << a.ghost;
0185     strm << a.bg;
0186     strm << a.clone;
0187 
0188     return strm;
0189   }
0190   /** \brief Operator to read efficiencies from file. */
0191   friend std::fstream & operator>>(std::fstream &strm, KFPVEfficiencies &a){
0192 
0193     strm >> a.ratio_reco;
0194     strm >> a.mc;
0195     strm >> a.reco;
0196     strm >> a.ratio_ghost;
0197     strm >> a.ratio_bg;
0198     strm >> a.ratio_clone;
0199     strm >> a.ghost;
0200     strm >> a.bg;
0201     strm >> a.clone;
0202 
0203     return strm;
0204   }
0205   /** \brief Adds efficiency from the file with the name defined by "fileName" to the current objects. */
0206   void AddFromFile(TString fileName)
0207   {
0208     std::fstream file(fileName.Data(),std::fstream::in);
0209     file >> *this;
0210   }
0211 
0212  private:
0213   std::vector<TString> names;      ///< Names of the counters. The same for all counters objects.
0214   std::map<TString, int> indices;  ///< Map between the counter index and its short name.
0215 
0216   KFMCCounter<double> ratio_reco;  ///< Efficiency.
0217 
0218   KFMCCounter<int> mc;             ///< Counters of the Monte Carlo vertices.
0219   KFMCCounter<int> reco;           ///< Counters of the reconstructed vertices.
0220 
0221   KFMCCounter<double> ratio_ghost; ///< Ratio of the ghost candidates to the total number of candidates.
0222   KFMCCounter<double> ratio_bg;    ///< Ratio of the physics background candidates to the total number of candidates.
0223   KFMCCounter<double> ratio_clone; ///< Ratio of double reconstructed vertices to the total number of signal candidates.
0224 
0225   KFMCCounter<int> ghost;          ///< Counters of the ghost candidates.
0226   KFMCCounter<int> bg;             ///< Counters of the physics background candidates.
0227   KFMCCounter<int> clone;          ///< Counters of the double reconstructed vertices.
0228 };
0229 
0230 #endif