Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
KFPHistogram.h
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file KFPHistogram.h
1 /*
2  * This file is part of KFParticle package
3  * Copyright (C) 2007-2019 FIAS Frankfurt Institute for Advanced Studies
4  * 2007-2019 Goethe University of Frankfurt
5  * 2007-2019 Ivan Kisel <I.Kisel@compeng.uni-frankfurt.de>
6  * 2007-2019 Maksym Zyzak
7  *
8  * KFParticle is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * KFParticle is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 
23 #ifndef KFPHISTOGRAM
24 #define KFPHISTOGRAM
25 
26 #include "KFPHistogramSet.h"
27 #include "KFPartEfficiencies.h"
29 #include <map>
30 #include <iostream>
31 #include <fstream>
32 
48 {
49  public:
50  KFPHistogram(): fPdgToIndex(), fOutFileName("KFPHistograms.txt"), fMemory(0)
51  {
52  KFPartEfficiencies partEff;
53  fPdgToIndex = partEff.GetPdgToIndexMap();
54 
55  int dataSize = 0;
56  for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++)
57  {
58  fKFPHistogramSet[iParticle] = KFPHistogramSet(iParticle);
59  dataSize += fKFPHistogramSet[iParticle].DataSize();
60  }
61 
62  fMemory = new int[dataSize];
63  std::memset(fMemory, 0, dataSize);
64 
65  int* pointer = fMemory;
66  for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++)
67  {
68  fKFPHistogramSet[iParticle].SetHistogramMemory(pointer);
69  pointer += fKFPHistogramSet[iParticle].DataSize();
70  }
71  }
72  ~KFPHistogram() { if(fMemory) delete [] fMemory; }
73 
75 
76  inline void Fill(const KFParticle& particle)
77  {
78  std::map<int, int>::iterator it;
79  it=fPdgToIndex.find(particle.GetPDG());
80  if(it != fPdgToIndex.end())
81  fKFPHistogramSet[it->second].Fill(particle);
82  }
83 
84  inline void Fill(const KFParticleTopoReconstructor& topoReconstructor)
85  {
86  for(unsigned int iParticle=0; iParticle<topoReconstructor.GetParticles().size(); iParticle++)
87  Fill(topoReconstructor.GetParticles()[iParticle]);
88  }
89 
90  KFPHistogramSet GetHistogramSet(int iSet) const { return fKFPHistogramSet[iSet]; }
91 
92  KFPHistogram1D GetHistogram(int iSet, int iHistogram) const { return fKFPHistogramSet[iSet].GetHistogram1D(iHistogram); }
93 
94  friend std::fstream & operator<<(std::fstream &strm, KFPHistogram &histograms)
95  {
96  for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++ )
97  {
98  strm << iParticle << std::endl;
99  const int& nHistograms = histograms.fKFPHistogramSet[iParticle].GetNHisto1D();
100  for(int iHistogram = 0; iHistogram<nHistograms; iHistogram++)
101  {
102  const KFPHistogram1D& histogram = histograms.fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram);
103  strm << histogram.Name() << " " << histogram.MinBin() << " " << histogram.MaxBin() << " " << histogram.NBins() << std::endl;
104  for(int iBin=0; iBin<histogram.NBins()+2; iBin++)
105  strm << histogram.GetHistogram()[iBin] << " ";
106  strm << std::endl;
107  }
108  }
109 
110  return strm;
111  }
112 
113  void Save()
114  {
115  std::fstream file(fOutFileName.data(),std::fstream::out);
116  file << (*this);
117  file.close();
118  }
119 
121  {
122  std::fstream ifile(prefix.data(),std::fstream::in);
123  if ( !ifile.is_open() ) return 0;
124 
125  int iSet = 0;
126 
127  for(int iParticle=0; iParticle<KFPartEfficiencies::nParticles; iParticle++ )
128  {
129  ifile >> iSet;
130  const int& nHistograms = fKFPHistogramSet[iParticle].GetNHisto1D();
131  for(int iHistogram = 0; iHistogram<nHistograms; iHistogram++)
132  {
134  float minBin = 0.f, maxBin = 0.f;
135  int nBins = 0;
136  ifile >> name >> minBin >> maxBin >> nBins;
137  if(nBins != fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram).NBins() ||
138  minBin != fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram).MinBin() ||
139  maxBin != fKFPHistogramSet[iParticle].GetHistogram1D(iHistogram).MaxBin() )
140  {
141  std::cout << "Fatal error: size of the histograms is not in an agreement with the current version." << std::endl;
142  exit(1);
143  }
144 
145  int binContent = 0;
146  for(int iBin=0; iBin<nBins+2; iBin++)
147  {
148  ifile >> binContent;
149  fKFPHistogramSet[iParticle].SetHisto1DBinContent(iHistogram, iBin, binContent);
150  }
151  }
152  }
153 
154  ifile.close();
155  return 1;
156  }
157 
158  inline void operator += ( const KFPHistogram &h )
159  {
160  for(int i=0; i<KFPartEfficiencies::nParticles; i++)
162  }
163 
164  private:
165  std::map<int, int> fPdgToIndex;
168  int* fMemory;
169 
170  KFPHistogram(const KFPHistogram&);
172 };
173 
174 #endif