9 #include "Pandora/AlgorithmHeaders.h" 20 CheatingSliceSelectionTool::CheatingSliceSelectionTool() :
32 unsigned int sliceCounter{0};
35 FloatVector targetWeights(inputSliceVector.size());
36 FloatVector otherWeights(inputSliceVector.size());
39 for (
const CaloHitList &sliceHits : inputSliceVector)
41 CaloHitList localHitList{};
43 for (
const CaloHit *
const pSliceCaloHit : sliceHits)
44 localHitList.push_back(static_cast<const CaloHit *>(pSliceCaloHit->GetParentAddress()));
46 for (
const CaloHit *
const pCaloHit : localHitList)
48 float thisTargetParticleWeight{0.f}, thisTotalWeight{0.f};
49 const MCParticleWeightMap &hitMCParticleWeightMap(pCaloHit->GetMCParticleWeightMap());
51 if (hitMCParticleWeightMap.empty())
54 MCParticleList mcParticleList;
55 for (
const auto &mapEntry : hitMCParticleWeightMap)
56 mcParticleList.push_back(mapEntry.first);
59 for (
const MCParticle *
const pMCParticle : mcParticleList)
61 const float weight{hitMCParticleWeightMap.at(pMCParticle)};
64 if (this->
IsTarget(pParentMCParticle))
65 thisTargetParticleWeight +=
weight;
71 if (thisTotalWeight > std::numeric_limits<float>::epsilon())
73 thisTargetParticleWeight *= 1.f / thisTotalWeight;
74 thisTotalWeight = 1.f;
78 thisTargetParticleWeight = 0.f;
79 thisTotalWeight = 0.f;
81 targetWeights[sliceCounter] += thisTargetParticleWeight;
82 otherWeights[sliceCounter] += (1. - thisTargetParticleWeight);
88 float totalTargetWeight{0.f};
89 for (
const float weight : targetWeights)
90 totalTargetWeight +=
weight;
93 for (
unsigned int i = 0; i < targetWeights.size(); ++i)
95 const float sliceWeight = targetWeights[i] + otherWeights[i];
96 const float completeness{totalTargetWeight > std::numeric_limits<float>::epsilon() ? targetWeights[i] / totalTargetWeight : 0.f};
97 const float purity{sliceWeight > std::numeric_limits<float>::epsilon() ? targetWeights[i] / sliceWeight : 0.f};
102 reducedSliceVarIndexMap.emplace(completeness, i);
107 reducedSliceVarIndexMap.emplace(purity, i);
111 std::vector<int> reducedSliceIndices{};
113 for (
const auto [cutVariable, index] : reducedSliceVarIndexMap)
116 reducedSliceIndices.push_back(index);
117 outputSliceVector.push_back(inputSliceVector[index]);
127 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"MaxSlices",
m_maxSlices));
128 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"Threshold",
m_threshold));
130 PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadValue(xmlHandle,
"CutVariable",
m_cutVariable));
134 std::cout <<
"CheatingSliceSelectionTool::ReadSettings: Unknown cut variable \'" <<
m_cutVariable <<
"\'" << std::endl;
135 return STATUS_CODE_INVALID_PARAMETER;
138 return STATUS_CODE_SUCCESS;
void SelectSlices(const pandora::Algorithm *const pAlgorithm, const SliceVector &inputSliceVector, SliceVector &outputSliceVector)
Select which slice(s) to use.
std::string m_cutVariable
The variable to cut on ("purity" or "completeness") - default "completeness".
int m_maxSlices
The maximum number of slices to retain (0 to retain all) - default 0.
std::map< float, int, std::greater< float > > MetricSliceIndexMap
static bool SortByMomentum(const pandora::MCParticle *const pLhs, const pandora::MCParticle *const pRhs)
Sort mc particles by their momentum.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
Header file for the lar monte carlo particle helper helper class.
float m_threshold
The minimum cut threshold to retain a slice (< 0 for no threshold) - default -1.
static const pandora::MCParticle * GetParentMCParticle(const pandora::MCParticle *const pMCParticle)
Get the parent mc particle.
virtual bool IsTarget(const pandora::MCParticle *const mcParticle) const =0
Template method to determine if an MC particle matches the target criteria for slice selection...
std::vector< pandora::CaloHitList > SliceVector