19#include "HistogramDef.hh"
20#include "HistogramDefSet.hh"
21#include "PerEntryHistogram.hh"
22#include "PerEntryHistogramSet.hh"
23#include "SpectraParticles.hh"
25#include "BDSOutputROOTEventSampler.hh"
26#include "RBDSException.hh"
37PerEntryHistogramSet::PerEntryHistogramSet(
const HistogramDefSet* definitionIn,
40 baseDefinition(definitionIn->baseDefinition),
43 branchName(definitionIn->branchName),
44 dynamicallyStoreParticles(definitionIn->dynamicallyStoreParticles),
45 dynamicallyStoreIons(definitionIn->dynamicallyStoreIons),
47 what(definitionIn->what),
48 topN(definitionIn->topN),
51 for (
const auto& pSpecDef : definitionIn->definitions)
53 auto pSpec = pSpecDef.first;
54 auto def = pSpecDef.second;
56 histograms[pSpec] = hist;
57 histogramsByPDGID[pSpec.first] = hist;
58 allPerEntryHistograms.push_back(hist);
59 if (pSpec.second == RBDS::SpectraParticles::all)
61 if (
IsIon(pSpec.first))
62 {ions.insert(pSpec.first);}
64 {nonIons.insert(pSpec.first);}
69void PerEntryHistogramSet::CreatePerEntryHistogram(
long long int pdgID)
71 allPDGIDs.insert(pdgID);
75 {nonIons.insert(pdgID);}
78 def->histName =
"Top" + std::to_string(topN) +
"_Spectra_" + def->histName +
"_" + std::to_string(pdgID);
79 def->selection = HistogramDefSet::AddPDGFilterToSelection(ParticleSpec(pdgID,RBDS::SpectraParticles::all),
85 histograms[ParticleSpec(pdgID, RBDS::SpectraParticles::all)] = hist;
86 histogramsByPDGID[pdgID] = hist;
87 allPerEntryHistograms.push_back(hist);
90PerEntryHistogramSet::~PerEntryHistogramSet()
92 delete baseDefinition;
93 for (
auto kv : allPerEntryHistograms)
101 if (dynamicallyStoreParticles || dynamicallyStoreIons)
105 std::set<long long int> pdgIDSet;
109 std::vector<long long int> missing;
110 std::set_difference(pdgIDSet.begin(), pdgIDSet.end(),
111 allPDGIDs.begin(), allPDGIDs.end(),
112 std::back_inserter(missing));
113 if (!missing.empty())
115 for (
auto pdgID : missing)
117 bool isIon = IsIon(pdgID);
118 if ((isIon && dynamicallyStoreIons) || (!isIon && dynamicallyStoreParticles))
119 {CreatePerEntryHistogram(pdgID);}
124 for (
auto hist : allPerEntryHistograms)
130 for (
auto hist : allPerEntryHistograms)
136 if (what == HistogramDefSet::writewhat::all)
138 for (
auto hist : allPerEntryHistograms)
144 std::vector<long long int> desiredPDGIDs;
147 case HistogramDefSet::writewhat::all:
148 {std::copy(allPDGIDs.begin(), allPDGIDs.end(), std::back_inserter(desiredPDGIDs));
break;}
149 case HistogramDefSet::writewhat::topN:
150 {desiredPDGIDs =
TopN(topN);
break;}
151 case HistogramDefSet::writewhat::ions:
152 {std::copy(ions.begin(), ions.end(), std::back_inserter(desiredPDGIDs));
break;}
153 case HistogramDefSet::writewhat::topNIons:
154 {desiredPDGIDs =
TopNIons(topN);
break;}
155 case HistogramDefSet::writewhat::particles:
156 {std::copy(nonIons.begin(), nonIons.end(), std::back_inserter(desiredPDGIDs));
break;}
157 case HistogramDefSet::writewhat::topNParticles:
161 for (
auto pdgID : desiredPDGIDs)
162 {histogramsByPDGID.at(pdgID)->Write(dir);}
170 std::map<long long int, double> integrals;
172 {integrals[id] = histogramsByPDGID.at(
id)->Integral();}
176 std::multimap<double, long long int> sorted = BDS::flip_map(integrals);
180 std::vector<long long int> topResult;
184 for (
auto it = sorted.rbegin(); it != sorted.rend() && i < nInt; it++, i++)
185 {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.