File indexing completed on 2025-08-05 08:19:38
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
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
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
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 }
0072 ~KFPHistogram() { if(fMemory) delete [] fMemory; }
0073
0074 void SetOutFileName(std::string name) { fOutFileName = name; }
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 }
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 }
0089
0090 KFPHistogramSet GetHistogramSet(int iSet) const { return fKFPHistogramSet[iSet]; }
0091
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 }
0112
0113 void Save()
0114 {
0115 std::fstream file(fOutFileName.data(),std::fstream::out);
0116 file << (*this);
0117 file.close();
0118 }
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 }
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 }
0163
0164 private:
0165 std::map<int, int> fPdgToIndex;
0166 std::string fOutFileName;
0167 KFPHistogramSet fKFPHistogramSet[KFPartEfficiencies::nParticles];
0168 int* fMemory;
0169
0170 KFPHistogram(const KFPHistogram&);
0171 KFPHistogram& operator=(const KFPHistogram&);
0172 };
0173
0174 #endif