LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
CheatingClusterCreationAlgorithm.cc
Go to the documentation of this file.
1 
9 #include "Pandora/AlgorithmHeaders.h"
10 
12 
13 using namespace pandora;
14 
15 namespace lar_content
16 {
17 
18 CheatingClusterCreationAlgorithm::CheatingClusterCreationAlgorithm() :
19  m_collapseToPrimaryMCParticles(false)
20 {
21 }
22 
23 //------------------------------------------------------------------------------------------------------------------------------------------
24 
26 {
27  MCParticleToHitListMap mcParticleToHitListMap;
28  this->GetMCParticleToHitListMap(mcParticleToHitListMap);
29  this->CreateClusters(mcParticleToHitListMap);
30 
31  return STATUS_CODE_SUCCESS;
32 }
33 
34 //------------------------------------------------------------------------------------------------------------------------------------------
35 
37 {
39 
41  {
42  const MCParticleList *pMCParticleList(nullptr);
43  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetList(*this, m_mcParticleListName, pMCParticleList));
44 
45  LArMCParticleHelper::GetMCPrimaryMap(pMCParticleList, mcPrimaryMap);
46  }
47 
48  const CaloHitList *pCaloHitList(nullptr);
49  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetCurrentList(*this, pCaloHitList));
50 
51  for (const CaloHit *const pCaloHit : *pCaloHitList)
52  {
53  try
54  {
55  if (!PandoraContentApi::IsAvailable(*this, pCaloHit))
56  continue;
57 
58  this->SimpleMCParticleCollection(pCaloHit, mcPrimaryMap, mcParticleToHitListMap);
59  }
60  catch (const StatusCodeException &)
61  {
62  }
63  }
64 }
65 
66 //------------------------------------------------------------------------------------------------------------------------------------------
67 
69  const LArMCParticleHelper::MCRelationMap &mcPrimaryMap, MCParticleToHitListMap &mcParticleToHitListMap) const
70 {
71  const MCParticle *pMCParticle(MCParticleHelper::GetMainMCParticle(pCaloHit));
72 
73  if (!this->SelectMCParticlesForClustering(pMCParticle))
74  return;
75 
77  {
78  LArMCParticleHelper::MCRelationMap::const_iterator primaryIter = mcPrimaryMap.find(pMCParticle);
79 
80  if (mcPrimaryMap.end() == primaryIter)
81  throw StatusCodeException(STATUS_CODE_NOT_FOUND);
82 
83  pMCParticle = primaryIter->second;
84  }
85 
86  mcParticleToHitListMap[pMCParticle].push_back(pCaloHit);
87 }
88 
89 //------------------------------------------------------------------------------------------------------------------------------------------
90 
91 bool CheatingClusterCreationAlgorithm::SelectMCParticlesForClustering(const MCParticle *const pMCParticle) const
92 {
93  if (m_particleIdList.empty())
94  return true;
95 
96  for (const int particleId : m_particleIdList)
97  {
98  if (pMCParticle->GetParticleId() == particleId)
99  return true;
100  }
101 
102  return false;
103 }
104 
105 //------------------------------------------------------------------------------------------------------------------------------------------
106 
108 {
109  MCParticleVector mcParticleVector;
110  for (const auto &mapEntry : mcParticleToHitListMap)
111  mcParticleVector.push_back(mapEntry.first);
112  std::sort(mcParticleVector.begin(), mcParticleVector.end(), LArMCParticleHelper::SortByMomentum);
113 
114  for (const MCParticle *const pMCParticle : mcParticleVector)
115  {
116  const CaloHitList &caloHitList(mcParticleToHitListMap.at(pMCParticle));
117 
118  if (caloHitList.empty())
119  continue;
120 
121  const Cluster *pCluster(nullptr);
122  PandoraContentApi::Cluster::Parameters parameters;
123  parameters.m_caloHitList = caloHitList;
124  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::Cluster::Create(*this, parameters, pCluster));
125 
126  PandoraContentApi::Cluster::Metadata metadata;
127 
128  switch (pMCParticle->GetParticleId())
129  {
130  case PHOTON:
131  case E_PLUS:
132  case E_MINUS:
133  case MU_PLUS:
134  case MU_MINUS:
135  metadata.m_particleId = pMCParticle->GetParticleId();
136  break;
137  default:
138  break;
139  }
140 
141  if (metadata.m_particleId.IsInitialized())
142  PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::Cluster::AlterMetadata(*this, pCluster, metadata));
143  }
144 }
145 
146 //------------------------------------------------------------------------------------------------------------------------------------------
147 
148 StatusCode CheatingClusterCreationAlgorithm::ReadSettings(const TiXmlHandle xmlHandle)
149 {
150  PANDORA_RETURN_RESULT_IF_AND_IF(STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=,
151  XmlHelper::ReadValue(xmlHandle, "CollapseToPrimaryMCParticles", m_collapseToPrimaryMCParticles));
152 
154  {
155  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "MCParticleListName", m_mcParticleListName));
156  }
157 
158  PANDORA_RETURN_RESULT_IF_AND_IF(
159  STATUS_CODE_SUCCESS, STATUS_CODE_NOT_FOUND, !=, XmlHelper::ReadVectorOfValues(xmlHandle, "ParticleIdList", m_particleIdList));
160 
161  return STATUS_CODE_SUCCESS;
162 }
163 
164 } // namespace lar_content
pandora::IntVector m_particleIdList
list of particle ids of MCPFOs to be selected
void GetMCParticleToHitListMap(MCParticleToHitListMap &mcParticleToHitListMap) const
Create map between each (primary) MC particle and associated calo hits.
pandora::StatusCode ReadSettings(const pandora::TiXmlHandle xmlHandle)
std::string m_mcParticleListName
The mc particle list name, required if want to collapse mc particle hierarchy.
intermediate_table::const_iterator const_iterator
void SimpleMCParticleCollection(const pandora::CaloHit *const pCaloHit, const LArMCParticleHelper::MCRelationMap &mcPrimaryMap, MCParticleToHitListMap &mcParticleToHitListMap) const
Simple mc particle collection, using main mc particle associated with each calo hit.
static void GetMCPrimaryMap(const pandora::MCParticleList *const pMCParticleList, MCRelationMap &mcPrimaryMap)
Get mapping from individual mc particles (in a provided list) and their primary parent mc particles...
bool SelectMCParticlesForClustering(const pandora::MCParticle *const pMCParticle) const
Check whether mc particle is of a type specified for inclusion in cheated clustering.
static bool SortByMomentum(const pandora::MCParticle *const pLhs, const pandora::MCParticle *const pRhs)
Sort mc particles by their momentum.
Header file for the cheating cluster creation algorithm class.
bool m_collapseToPrimaryMCParticles
Whether to collapse mc particle hierarchies to primary particles.
std::vector< art::Ptr< simb::MCParticle > > MCParticleVector
std::unordered_map< const pandora::MCParticle *, pandora::CaloHitList > MCParticleToHitListMap
void CreateClusters(const MCParticleToHitListMap &mcParticleToHitListMap) const
Create clusters based on information in the mc particle to hit list map.
std::unordered_map< const pandora::MCParticle *, const pandora::MCParticle * > MCRelationMap