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 sampler =
event->GetSampler(branchName +
".");
104 {
throw RBDSException(
"Cannot find sampler \"" + branchName +
"\" or Model tree was not stored");}
112 if (dynamicallyStoreParticles || dynamicallyStoreIons)
116 std::set<long long int> pdgIDSet(sampler->partID.begin(), sampler->partID.end());
119 std::vector<long long int> missing;
120 std::set_difference(pdgIDSet.begin(), pdgIDSet.end(),
121 allPDGIDs.begin(), allPDGIDs.end(),
122 std::back_inserter(missing));
123 if (!missing.empty())
125 for (
auto pdgID : missing)
127 bool isIon = IsIon(pdgID);
128 if ((isIon && dynamicallyStoreIons) || (!isIon && dynamicallyStoreParticles))
129 {CreatePerEntryHistogram(pdgID);}
134 for (
auto hist : allPerEntryHistograms)
140 for (
auto hist : allPerEntryHistograms)
146 if (what == HistogramDefSet::writewhat::all)
148 for (
auto hist : allPerEntryHistograms)
154 std::vector<long long int> desiredPDGIDs;
157 case HistogramDefSet::writewhat::all:
158 {std::copy(allPDGIDs.begin(), allPDGIDs.end(), std::back_inserter(desiredPDGIDs));
break;}
159 case HistogramDefSet::writewhat::topN:
160 {desiredPDGIDs =
TopN(topN);
break;}
161 case HistogramDefSet::writewhat::ions:
162 {std::copy(ions.begin(), ions.end(), std::back_inserter(desiredPDGIDs));
break;}
163 case HistogramDefSet::writewhat::topNIons:
164 {desiredPDGIDs =
TopNIons(topN);
break;}
165 case HistogramDefSet::writewhat::particles:
166 {std::copy(nonIons.begin(), nonIons.end(), std::back_inserter(desiredPDGIDs));
break;}
167 case HistogramDefSet::writewhat::topNParticles:
171 for (
auto pdgID : desiredPDGIDs)
172 {histogramsByPDGID.at(pdgID)->Write(dir);}
180 std::map<long long int, double> integrals;
182 {integrals[id] = histogramsByPDGID.at(
id)->Integral();}
186 std::multimap<double, long long int> sorted = BDS::flip_map(integrals);
190 std::vector<long long int> topResult;
194 for (
auto it = sorted.rbegin(); it != sorted.rend() && i < nInt; it++, i++)
195 {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 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)
General exception with possible name of object and message.
G4bool IsIon(const G4ParticleDefinition *particle)
Whether a particle is an ion. A proton is counted NOT as an ion.