LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
lar_content::CheatingBeamParticleSliceSelectionTool Class Reference

CheatingBeamParticleSliceSelectionTool class. More...

#include "CheatingBeamParticleSliceSelectionTool.h"

Inheritance diagram for lar_content::CheatingBeamParticleSliceSelectionTool:
lar_content::CheatingSliceSelectionTool lar_content::SliceSelectionBaseTool

Public Types

typedef std::map< float, int, std::greater< float > > MetricSliceIndexMap
 

Public Member Functions

void SelectSlices (const pandora::Algorithm *const pAlgorithm, const SliceVector &inputSliceVector, SliceVector &outputSliceVector)
 Select which slice(s) to use. More...
 

Protected Member Functions

bool IsTarget (const pandora::MCParticle *const mcParticle) const
 Template method to determine if an MC particle matches the target criteria for slice selection. Return true if match. More...
 

Protected Attributes

int m_maxSlices
 The maximum number of slices to retain (0 to retain all) - default 0. More...
 
float m_threshold
 The minimum cut threshold to retain a slice (< 0 for no threshold) - default -1. More...
 
std::string m_cutVariable
 The variable to cut on ("purity" or "completeness") - default "completeness". More...
 

Private Member Functions

pandora::StatusCode ReadSettings (const pandora::TiXmlHandle xmlHandle)
 

Detailed Description

Member Typedef Documentation

typedef std::map<float, int, std::greater<float> > lar_content::CheatingSliceSelectionTool::MetricSliceIndexMap
inherited

Definition at line 37 of file CheatingSliceSelectionTool.h.

Member Function Documentation

bool lar_content::CheatingBeamParticleSliceSelectionTool::IsTarget ( const pandora::MCParticle *const  mcParticle) const
protectedvirtual

Template method to determine if an MC particle matches the target criteria for slice selection. Return true if match.

Parameters
mcParticlethe MC particle to check

Implements lar_content::CheatingSliceSelectionTool.

Definition at line 20 of file CheatingBeamParticleSliceSelectionTool.cc.

21 {
22  return LArMCParticleHelper::IsBeamParticle(mcParticle);
23 }
static bool IsBeamParticle(const pandora::MCParticle *const pMCParticle)
Returns true if passed a primary beam MCParticle.
StatusCode lar_content::CheatingBeamParticleSliceSelectionTool::ReadSettings ( const pandora::TiXmlHandle  xmlHandle)
private

Definition at line 27 of file CheatingBeamParticleSliceSelectionTool.cc.

28 {
30 }
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
void lar_content::CheatingSliceSelectionTool::SelectSlices ( const pandora::Algorithm *const  pAlgorithm,
const SliceVector inputSliceVector,
SliceVector outputSliceVector 
)
virtualinherited

Select which slice(s) to use.

Parameters
pAlgorithmthe address of the master instance, used to access MCParticles when in training mode
inputSliceVectorthe initial slice vector
outputSliceVectorthe output slice vector

Implements lar_content::SliceSelectionBaseTool.

Definition at line 29 of file CheatingSliceSelectionTool.cc.

References lar_content::LArMCParticleHelper::GetParentMCParticle(), lar_content::CheatingSliceSelectionTool::IsTarget(), lar_content::CheatingSliceSelectionTool::m_cutVariable, lar_content::CheatingSliceSelectionTool::m_maxSlices, lar_content::CheatingSliceSelectionTool::m_threshold, lar_content::LArMCParticleHelper::SortByMomentum(), and weight.

30 {
31  // ATTN Ensure this only runs if slicing enabled
32  unsigned int sliceCounter{0};
33  MetricSliceIndexMap reducedSliceVarIndexMap;
34 
35  FloatVector targetWeights(inputSliceVector.size());
36  FloatVector otherWeights(inputSliceVector.size());
37 
38  // Calculate target and total weight for each slice
39  for (const CaloHitList &sliceHits : inputSliceVector)
40  {
41  CaloHitList localHitList{};
42  // ATTN Must ensure we copy the hit actually owned by master instance; access differs with/without slicing enabled
43  for (const CaloHit *const pSliceCaloHit : sliceHits)
44  localHitList.push_back(static_cast<const CaloHit *>(pSliceCaloHit->GetParentAddress()));
45 
46  for (const CaloHit *const pCaloHit : localHitList)
47  {
48  float thisTargetParticleWeight{0.f}, thisTotalWeight{0.f};
49  const MCParticleWeightMap &hitMCParticleWeightMap(pCaloHit->GetMCParticleWeightMap());
50 
51  if (hitMCParticleWeightMap.empty())
52  continue;
53 
54  MCParticleList mcParticleList;
55  for (const auto &mapEntry : hitMCParticleWeightMap)
56  mcParticleList.push_back(mapEntry.first);
57  mcParticleList.sort(LArMCParticleHelper::SortByMomentum);
58 
59  for (const MCParticle *const pMCParticle : mcParticleList)
60  {
61  const float weight{hitMCParticleWeightMap.at(pMCParticle)};
62  const MCParticle *const pParentMCParticle{LArMCParticleHelper::GetParentMCParticle(pMCParticle)};
63 
64  if (this->IsTarget(pParentMCParticle))
65  thisTargetParticleWeight += weight;
66 
67  thisTotalWeight += weight;
68  }
69 
70  // ATTN normalise arbitrary input weights at this point
71  if (thisTotalWeight > std::numeric_limits<float>::epsilon())
72  {
73  thisTargetParticleWeight *= 1.f / thisTotalWeight;
74  thisTotalWeight = 1.f;
75  }
76  else
77  {
78  thisTargetParticleWeight = 0.f;
79  thisTotalWeight = 0.f;
80  }
81  targetWeights[sliceCounter] += thisTargetParticleWeight;
82  otherWeights[sliceCounter] += (1. - thisTargetParticleWeight);
83  }
84 
85  ++sliceCounter;
86  }
87 
88  float totalTargetWeight{0.f};
89  for (const float weight : targetWeights)
90  totalTargetWeight += weight;
91 
92  // Add slices passing cut threshold to map
93  for (unsigned int i = 0; i < targetWeights.size(); ++i)
94  {
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};
98  // Already checked correctness of variable in ReadSettings
99  if (m_cutVariable == "completeness")
100  {
101  if (completeness >= m_threshold)
102  reducedSliceVarIndexMap.emplace(completeness, i);
103  }
104  else if (m_cutVariable == "purity")
105  {
106  if (purity >= m_threshold)
107  reducedSliceVarIndexMap.emplace(purity, i);
108  }
109  }
110  // Select the best m_maxSlices slices - prefix increment ensures all slices retained if m_maxSlices == 0
111  std::vector<int> reducedSliceIndices{};
112  int i = 0;
113  for (const auto [cutVariable, index] : reducedSliceVarIndexMap)
114  { // ATTN: Map is sorted on cut variable from max to min
115  (void)cutVariable; // GCC 7 support, versions 8+ do not need this
116  reducedSliceIndices.push_back(index);
117  outputSliceVector.push_back(inputSliceVector[index]);
118  if (++i == m_maxSlices)
119  break;
120  }
121 }
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.
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.
double weight
Definition: plottest35.C:25
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...

Member Data Documentation

std::string lar_content::CheatingSliceSelectionTool::m_cutVariable
protectedinherited
int lar_content::CheatingSliceSelectionTool::m_maxSlices
protectedinherited

The maximum number of slices to retain (0 to retain all) - default 0.

Definition at line 50 of file CheatingSliceSelectionTool.h.

Referenced by lar_content::CheatingSliceSelectionTool::ReadSettings(), and lar_content::CheatingSliceSelectionTool::SelectSlices().

float lar_content::CheatingSliceSelectionTool::m_threshold
protectedinherited

The documentation for this class was generated from the following files: