MultiPandoraApiImpl.cc
Go to the documentation of this file.
1 /**
2  * @file larpandoracontent/LArControlFlow/MultiPandoraApiImpl.cc
3  *
4  * @brief Implementation of the MultiPandoraApiImpl class.
5  *
6  * $Log: $
7  */
8 
9 #include "Pandora/Pandora.h"
10 #include "Pandora/StatusCodes.h"
11 
13 
15 {
17 }
18 
19 //------------------------------------------------------------------------------------------------------------------------------------------
20 
21 const pandora::Pandora *MultiPandoraApiImpl::GetPandoraInstance(const pandora::Pandora *const pPrimaryPandora, const unsigned int volumeId) const
22 {
23  PandoraInstanceList instanceList(this->GetDaughterPandoraInstanceList(pPrimaryPandora));
24  instanceList.push_back(pPrimaryPandora);
25 
26  for (const pandora::Pandora *const pPandora : instanceList)
27  {
28  try
29  {
30  if (volumeId == this->GetVolumeId(pPandora))
31  return pPandora;
32  }
33  catch (const pandora::StatusCodeException &)
34  {
35  }
36  }
37 
38  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
39 }
40 
41 //------------------------------------------------------------------------------------------------------------------------------------------
42 
43 const PandoraInstanceList &MultiPandoraApiImpl::GetDaughterPandoraInstanceList(const pandora::Pandora *const pPrimaryPandora) const
44 {
46 
47  if (m_primaryToDaughtersMap.end() == iter)
48  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
49 
50  return iter->second;
51 }
52 
53 //------------------------------------------------------------------------------------------------------------------------------------------
54 
55 const pandora::Pandora *MultiPandoraApiImpl::GetPrimaryPandoraInstance(const pandora::Pandora *const pDaughterPandora) const
56 {
57  PandoraRelationMap::const_iterator iter = m_daughterToPrimaryMap.find(pDaughterPandora);
58 
59  if (m_daughterToPrimaryMap.end() == iter)
60  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
61 
62  return iter->second;
63 }
64 
65 //------------------------------------------------------------------------------------------------------------------------------------------
66 
67 unsigned int MultiPandoraApiImpl::GetVolumeId(const pandora::Pandora *const pPandora) const
68 {
70 
71  if (m_pandoraToVolumeIdMap.end() == iter)
72  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
73 
74  return iter->second;
75 }
76 
77 //------------------------------------------------------------------------------------------------------------------------------------------
78 
79 void MultiPandoraApiImpl::SetVolumeId(const pandora::Pandora *const pPandora, const unsigned int volumeId)
80 {
81  if (m_pandoraToVolumeIdMap.count(pPandora))
82  throw pandora::StatusCodeException(pandora::STATUS_CODE_ALREADY_PRESENT);
83 
84  if (!m_pandoraToVolumeIdMap.insert(PandoraToVolumeIdMap::value_type(pPandora, volumeId)).second)
85  throw pandora::StatusCodeException(pandora::STATUS_CODE_FAILURE);
86 }
87 
88 //------------------------------------------------------------------------------------------------------------------------------------------
89 
91 {
92 }
93 
94 //------------------------------------------------------------------------------------------------------------------------------------------
95 
97 {
98  // ATTN This is a copy of the input map, which will be modified by calls to delete pandora instances
99  PandoraInstanceMap pandoraInstanceMap(m_primaryToDaughtersMap);
100 
101  for (const auto &mapElement : pandoraInstanceMap)
102  this->DeletePandoraInstances(mapElement.first);
103 }
104 
105 //------------------------------------------------------------------------------------------------------------------------------------------
106 
107 void MultiPandoraApiImpl::AddPrimaryPandoraInstance(const pandora::Pandora *const pPrimaryPandora)
108 {
109  if (!m_primaryToDaughtersMap.insert(PandoraInstanceMap::value_type(pPrimaryPandora, PandoraInstanceList())).second)
110  throw pandora::StatusCodeException(pandora::STATUS_CODE_ALREADY_PRESENT);
111 }
112 
113 //------------------------------------------------------------------------------------------------------------------------------------------
114 
115 void MultiPandoraApiImpl::AddDaughterPandoraInstance(const pandora::Pandora *const pPrimaryPandora, const pandora::Pandora *const pDaughterPandora)
116 {
117  PandoraInstanceMap::iterator iter = m_primaryToDaughtersMap.find(pPrimaryPandora);
118 
119  if (m_primaryToDaughtersMap.end() == iter)
120  throw pandora::StatusCodeException(pandora::STATUS_CODE_NOT_FOUND);
121 
122  iter->second.push_back(pDaughterPandora);
123 
124  if (!m_daughterToPrimaryMap.insert(PandoraRelationMap::value_type(pDaughterPandora, pPrimaryPandora)).second)
125  throw pandora::StatusCodeException(pandora::STATUS_CODE_FAILURE);
126 }
127 
128 //------------------------------------------------------------------------------------------------------------------------------------------
129 
130 void MultiPandoraApiImpl::DeletePandoraInstances(const pandora::Pandora *const pPrimaryPandora)
131 {
132  PandoraInstanceList pandoraInstanceList;
133 
134  try
135  {
136  pandoraInstanceList = this->GetDaughterPandoraInstanceList(pPrimaryPandora);
137  }
138  catch (const pandora::StatusCodeException &)
139  {
140  std::cout << "MultiPandoraApiImpl::DeletePandoraInstances - unable to find daughter instances associated with primary "
141  << pPrimaryPandora << std::endl;
142  }
143 
144  pandoraInstanceList.push_back(pPrimaryPandora);
145  m_primaryToDaughtersMap.erase(pPrimaryPandora);
146 
147  for (const pandora::Pandora *const pPandora : pandoraInstanceList)
148  {
149  m_pandoraToVolumeIdMap.erase(pPandora);
150  m_daughterToPrimaryMap.erase(pPandora);
151  delete pPandora;
152  }
153 }
intermediate_table::iterator iterator
PandoraToVolumeIdMap m_pandoraToVolumeIdMap
The map from pandora instance to volume id.
~MultiPandoraApiImpl()
Destructor;.
void AddDaughterPandoraInstance(const pandora::Pandora *const pPrimaryPandora, const pandora::Pandora *const pDaughterPandora)
Add a pandora daughter instance, associated to a primary pandora instance.
void DeletePandoraInstances(const pandora::Pandora *const pPrimaryPandora)
Delete all pandora instances associated with (and including) a specified primary pandora instance...
void AddPrimaryPandoraInstance(const pandora::Pandora *const pPrimaryPandora)
Declare a new primary pandora instance.
intermediate_table::const_iterator const_iterator
const pandora::Pandora * GetPrimaryPandoraInstance(const pandora::Pandora *const pDaughterPandora) const
Get the address of the primary pandora instance associated with a given daughter pandora instance...
std::vector< const pandora::Pandora * > PandoraInstanceList
Header file for the MultiPandoraApiImpl class.
std::unordered_map< const pandora::Pandora *, PandoraInstanceList > PandoraInstanceMap
void SetVolumeId(const pandora::Pandora *const pPandora, const unsigned int volumeId)
Set the volume id associated with a given pandora instance.
PandoraRelationMap m_daughterToPrimaryMap
The map from daughter pandora instance to primary pandora instance.
const PandoraInstanceMap & GetPandoraInstanceMap() const
Get the pandora instance map.
MultiPandoraApiImpl()
Default constructor;.
const PandoraInstanceList & GetDaughterPandoraInstanceList(const pandora::Pandora *const pPrimaryPandora) const
Get the list of daughter pandora instances associated with a given primary pandora instance...
PandoraInstanceMap m_primaryToDaughtersMap
The map from primary pandora instance to list of daughter pandora instances.
unsigned int GetVolumeId(const pandora::Pandora *const pPandora) const
Get the volume id associated with a given pandora instance.
const pandora::Pandora * GetPandoraInstance(const pandora::Pandora *const pPrimaryPandora, const unsigned int volumeId) const
Get the address of the pandora instance associated with a given primary pandora instance and volume i...
QTextStream & endl(QTextStream &s)