LArSoft  v10_06_00
Liquid Argon Software toolkit - https://larsoft.org/
RandomClusteringAlgorithm.cc
Go to the documentation of this file.
1 
9 #include "Pandora/AlgorithmHeaders.h"
10 
12 
13 #include <algorithm>
14 #include <random>
15 #include <vector>
16 
17 using namespace pandora;
18 
19 namespace lar_content
20 {
21 
22 //------------------------------------------------------------------------------------------------------------------------------------------
23 
24 StatusCode RandomClusteringAlgorithm::Run()
25 {
26  const CaloHitList *pCaloHits{nullptr};
27  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::GetCurrentList(*this, pCaloHits));
28 
29  if (pCaloHits->size() < m_numNewClusters)
30  return STATUS_CODE_SUCCESS;
31 
32  std::vector<unsigned int> idxs, splitIdxs;
33  for (unsigned int i = 0; i < pCaloHits->size(); i++)
34  idxs.emplace_back(i);
35  std::shuffle(idxs.begin(), idxs.end(), std::default_random_engine(0));
36  for (unsigned int i = 0; i < m_numNewClusters - 1; i++)
37  splitIdxs.emplace_back(idxs.at(i));
38  std::sort(splitIdxs.begin(), splitIdxs.end());
39 
40  std::vector<unsigned int>::const_iterator splitItr{splitIdxs.cbegin()};
41  PandoraContentApi::Cluster::Parameters params;
42  unsigned int cntr{0};
43  for (const CaloHit *const pCaloHit : *pCaloHits)
44  {
45  if (splitItr != splitIdxs.cend() && cntr++ > *splitItr)
46  {
47  const Cluster *pCluster{nullptr};
48  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::Cluster::Create(*this, params, pCluster));
49  params.m_caloHitList.clear();
50  splitItr++;
51  }
52  params.m_caloHitList.emplace_back(pCaloHit);
53  }
54  const Cluster *pCluster{nullptr};
55  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, PandoraContentApi::Cluster::Create(*this, params, pCluster));
56 
57  return STATUS_CODE_SUCCESS;
58 }
59 
60 //------------------------------------------------------------------------------------------------------------------------------------------
61 
62 StatusCode RandomClusteringAlgorithm::ReadSettings(const TiXmlHandle xmlHandle)
63 {
64  PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, XmlHelper::ReadValue(xmlHandle, "NumNewClusters", m_numNewClusters));
65  if (m_numNewClusters == 0)
66  return STATUS_CODE_INVALID_PARAMETER;
67 
68  return STATUS_CODE_SUCCESS;
69 }
70 
71 } // namespace lar_content
intermediate_table::const_iterator const_iterator