Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:19:38

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 
0023 #ifndef KFPHISTOGRAM
0024 #define KFPHISTOGRAM
0025 
0026 #include "KFPHistogramSet.h"
0027 #include "KFPartEfficiencies.h"
0028 #include "KFParticleTopoReconstructor.h"
0029 #include <map>
0030 #include <iostream>
0031 #include <fstream>
0032 
0033 /** @class KFPHistogram
0034  ** @brief A common object containing histograms for all particle species.
0035  ** @author  M.Zyzak, I.Kisel
0036  ** @date 05.02.2019
0037  ** @version 1.0
0038  **
0039  ** The class is used to collect histograms in the environment,
0040  ** where ROOT is not available, for example at Intel Xeon Phi cards.
0041  ** Contains a set of histograms for each decay reconstructed by the
0042  ** KF Particle Finder package, allocates the memory for all histograms:
0043  ** This allows faster allocation, faster transfer of the memory,
0044  ** easier access from the Intel Xeon Phi, better performance.
0045  **/
0046 
0047 class KFPHistogram
0048 {  
0049  public:
0050   KFPHistogram(): fPdgToIndex(), fOutFileName("KFPHistograms.txt"), fMemory(0)
0051   {
0052     KFPartEfficiencies partEff;
0053     fPdgToIndex = partEff.GetPdgToIndexMap();
0054     
0055     int dataSize = 0;
0056     for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++)
0057     {
0058       fKFPHistogramSet[iParticle] = KFPHistogramSet(iParticle);
0059       dataSize += fKFPHistogramSet[iParticle].DataSize();
0060     }
0061     
0062     fMemory = new int[dataSize];
0063     std::memset(fMemory, 0, dataSize);
0064     
0065     int* pointer = fMemory;
0066     for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++)
0067     {
0068       fKFPHistogramSet[iParticle].SetHistogramMemory(pointer);
0069       pointer += fKFPHistogramSet[iParticle].DataSize();
0070     }
0071   } ///< Default Constructor. Creates histograms, allocates memory for them.
0072   ~KFPHistogram() { if(fMemory) delete [] fMemory; }
0073   
0074   void SetOutFileName(std::string name) { fOutFileName = name; } ///< Set the name of the output file.
0075   
0076   inline void Fill(const KFParticle& particle)
0077   {
0078     std::map<int, int>::iterator it;
0079     it=fPdgToIndex.find(particle.GetPDG());
0080     if(it != fPdgToIndex.end())
0081       fKFPHistogramSet[it->second].Fill(particle);
0082   } ///< Fills histograms using parameters of the given particle.
0083   
0084   inline void Fill(const KFParticleTopoReconstructor& topoReconstructor)
0085   {
0086     for(unsigned int iParticle=0; iParticle<topoReconstructor.GetParticles().size(); iParticle++)
0087       Fill(topoReconstructor.GetParticles()[iParticle]);
0088   } ///< Fills histograms for each particle reconstructed by the KFParticleFinder object from the given KFParticleTopoReconstructor.
0089     
0090   KFPHistogramSet GetHistogramSet(int iSet)   const { return fKFPHistogramSet[iSet]; } ///< Returns set of histograms for the decay with index iSet.
0091   /** \brief Returns "iHistogram" histogram from the set of histograms for the decay with index "iSet". */
0092   KFPHistogram1D  GetHistogram(int iSet, int iHistogram) const { return fKFPHistogramSet[iSet].GetHistogram1D(iHistogram); }
0093   
0094   friend std::fstream & operator<<(std::fstream &strm, KFPHistogram &histograms)
0095   {
0096     for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++ )
0097     {
0098       strm << iParticle << std::endl;
0099       const int& nHistograms = histograms.fKFPHistogramSet[iParticle].GetNHisto1D();
0100       for(int iHistogram = 0; iHistogram<nHistograms; iHistogram++)
0101       {
0102         const KFPHistogram1D& histogram = histograms.fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram);
0103         strm << histogram.Name() << " " << histogram.MinBin() << " " << histogram.MaxBin() << " " << histogram.NBins() << std::endl;
0104         for(int iBin=0; iBin<histogram.NBins()+2; iBin++)
0105           strm << histogram.GetHistogram()[iBin] << " ";
0106         strm << std::endl;
0107       }
0108     }
0109 
0110     return strm;
0111   } ///< Stores all histograms to the output file.
0112   
0113   void Save() 
0114   {
0115     std::fstream file(fOutFileName.data(),std::fstream::out);
0116     file << (*this);
0117     file.close();
0118   } ///< Stores all histograms to the file with the name defined in KFPHistogram::fOutFileName.
0119   
0120   bool FillFromFile( std::string prefix )
0121   {
0122     std::fstream ifile(prefix.data(),std::fstream::in);
0123     if ( !ifile.is_open() ) return 0;
0124 
0125     int iSet = 0;
0126     
0127     for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++ )
0128     {
0129       ifile >> iSet;
0130       const int& nHistograms = fKFPHistogramSet[iParticle].GetNHisto1D();
0131       for(int iHistogram = 0; iHistogram<nHistograms; iHistogram++)
0132       {
0133         std::string name;
0134         float minBin = 0.f, maxBin = 0.f;
0135         int nBins = 0;
0136         ifile >> name >> minBin >> maxBin >> nBins;
0137         if(nBins  != fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram).NBins()  || 
0138            minBin != fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram).MinBin() || 
0139            maxBin != fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram).MaxBin() ) 
0140         {
0141           std::cout << "Fatal error: size of the histograms is not in an agreement with the current version." << std::endl;
0142           exit(1);
0143         }
0144         
0145         int binContent = 0;
0146         for(int iBin=0; iBin<nBins+2; iBin++)
0147         {
0148           ifile >> binContent;
0149           fKFPHistogramSet[iParticle].SetHisto1DBinContent(iHistogram, iBin, binContent);
0150         }
0151       }
0152     }
0153     
0154     ifile.close();
0155     return 1;
0156   } ///< Reads object from the file with the name defined by "prefix".
0157   
0158   inline void operator += ( const KFPHistogram &h )
0159   {
0160     for(int i=0; i<KFPartEfficiencies::nParticles; i++)
0161       fKFPHistogramSet[i] += h.fKFPHistogramSet[i];
0162   }///< Adds all histograms from object "h" to the current object.
0163   
0164  private:
0165   std::map<int, int> fPdgToIndex; ///< A map between PDG code and index of the decay in the KF Particle Finder scheme. A copy of an object from KFPartEfficiencies.
0166   std::string fOutFileName; ///< The name of the output file, where histograms will be stored.
0167   KFPHistogramSet fKFPHistogramSet[KFPartEfficiencies::nParticles]; ///< A set of histograms for all decays reconstructed by KF Particle Finder.
0168   int* fMemory; ///< A pointer to the memory for all histograms.
0169   
0170   KFPHistogram(const KFPHistogram&); ///< Does not allow copying of the objects of this class.
0171   KFPHistogram& operator=(const KFPHistogram&); ///< Does not allow copying of the objects of this class.
0172 };
0173 
0174 #endif