20#include "HistogramDef.hh"
21#include "HistogramDefSet.hh"
22#include "PerEntryHistogram.hh"
23#include "PerEntryHistogramSet.hh"
24#include "SpectraParticles.hh"
26#include "BDSOutputROOTEventSampler.hh"
27#include "RBDSException.hh"
38PerEntryHistogramSet::PerEntryHistogramSet(
const HistogramDefSet* definitionIn,
41 baseDefinition(definitionIn->baseDefinition),
44 branchName(definitionIn->branchName),
45 dynamicallyStoreParticles(definitionIn->dynamicallyStoreParticles),
46 dynamicallyStoreIons(definitionIn->dynamicallyStoreIons),
48 what(definitionIn->what),
49 topN(definitionIn->topN),
52 for (
const auto& pSpecDef : definitionIn->definitions)
54 auto pSpec = pSpecDef.first;
55 auto def = pSpecDef.second;
57 histograms[pSpec] = hist;
58 histogramsByPDGID[pSpec.first] = hist;
59 allPerEntryHistograms.push_back(hist);
60 if (pSpec.second == RBDS::SpectraParticles::all)
62 if (
IsIon(pSpec.first))
63 {ions.insert(pSpec.first);}
65 {nonIons.insert(pSpec.first);}
70void PerEntryHistogramSet::CreatePerEntryHistogram(
long long int pdgID)
72 allPDGIDs.insert(pdgID);
76 {nonIons.insert(pdgID);}
79 def->histName =
"Top" + std::to_string(topN) +
"_Spectra_" + def->histName +
"_" + std::to_string(pdgID);
80 def->selection = HistogramDefSet::AddPDGFilterToSelection(ParticleSpec(pdgID,RBDS::SpectraParticles::all),
86 histograms[ParticleSpec(pdgID, RBDS::SpectraParticles::all)] = hist;
87 histogramsByPDGID[pdgID] = hist;
88 allPerEntryHistograms.push_back(hist);
91PerEntryHistogramSet::~PerEntryHistogramSet()
93 delete baseDefinition;
94 for (
auto kv : allPerEntryHistograms)
102 if (dynamicallyStoreParticles || dynamicallyStoreIons)
106 std::set<long long int> pdgIDSet;
110 std::vector<long long int> missing;
111 std::set_difference(pdgIDSet.begin(), pdgIDSet.end(),
112 allPDGIDs.begin(), allPDGIDs.end(),
113 std::back_inserter(missing));
114 if (!missing.empty())
116 for (
auto pdgID : missing)
118 bool isIon = IsIon(pdgID);
119 if ((isIon && dynamicallyStoreIons) || (!isIon && dynamicallyStoreParticles))
120 {CreatePerEntryHistogram(pdgID);}
125 for (
auto hist : allPerEntryHistograms)
131 for (
auto hist : allPerEntryHistograms)
137 if (what == HistogramDefSet::writewhat::all)
139 for (
auto hist : allPerEntryHistograms)
145 std::vector<long long int> desiredPDGIDs;
148 case HistogramDefSet::writewhat::all:
149 {std::copy(allPDGIDs.begin(), allPDGIDs.end(), std::back_inserter(desiredPDGIDs));
break;}
150 case HistogramDefSet::writewhat::topN:
151 {desiredPDGIDs =
TopN(topN);
break;}
152 case HistogramDefSet::writewhat::ions:
153 {std::copy(ions.begin(), ions.end(), std::back_inserter(desiredPDGIDs));
break;}
154 case HistogramDefSet::writewhat::topNIons:
155 {desiredPDGIDs =
TopNIons(topN);
break;}
156 case HistogramDefSet::writewhat::particles:
157 {std::copy(nonIons.begin(), nonIons.end(), std::back_inserter(desiredPDGIDs));
break;}
158 case HistogramDefSet::writewhat::topNParticles:
162 for (
auto pdgID : desiredPDGIDs)
163 {histogramsByPDGID.at(pdgID)->Write(dir);}
171 std::map<long long int, double> integrals;
173 {integrals[id] = histogramsByPDGID.at(
id)->Integral();}
177 std::multimap<double, long long int> sorted = BDS::flip_map(integrals);
181 std::vector<long long int> topResult;
185 for (
auto it = sorted.rbegin(); it != sorted.rend() && i < nInt; it++, i++)
186 {topResult.push_back(it->second);}
Specification for a set of histograms.
Common specification for a histogram.
virtual HistogramDef * Clone() const =0
Copy this instance. Virtual to be overridden in derived classes.
virtual void Write(TDirectory *dir=nullptr)
virtual void AccumulateCurrentEntry(long int entryNumber)
std::vector< long long int > TopN(int n) const
Get top part of set. Sorted in descending order of integral.
std::vector< long long int > TopNIons(int n) const
Get top part of set. Sorted in descending order of integral.
virtual void GetPDGIDSetFromSampler(std::set< long long int > &setIn) const =0
virtual void CheckSampler()=0
virtual void Terminate()
Terminate the accumulator and save the result to the result member variable.
std::vector< long long int > TopNNonIons(int n) const
Get top part of set. Sorted in descending order of integral.
std::vector< long long int > TopUtility(const std::set< long long int > &s, size_t n) const
Utility function to find top N in set s. Sorted in descending order of integral.
Holder for information to calculate per entry histograms.
virtual void Terminate()
Terminate the accumulator and save the result to the result member variable.
PerEntryHistogram()
Public constructor only for compatibility with ROOT - not intended for use.
virtual void Write(TDirectory *dir=nullptr)
virtual void AccumulateCurrentEntry(long int entryNumber)
void AddNEmptyEntries(unsigned long i)
G4bool IsIon(const G4ParticleDefinition *particle)
Whether a particle is an ion. A proton is counted NOT as an ion.