ATLAS Offline Software
Loading...
Searching...
No Matches
TileMutableDataContainer.icc
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4/**
5 * @file TileEvent/TileMutableDataContainer.icc
6 * @author scott snyder <snyder@bnl.gov>
7 * @date Oct, 2018
8 * @brief Helper for holding non-const raw data prior to recording in SG.
9 */
10
11
12#include <stdexcept>
13
14
15/**
16 * @brief Constructor.
17 * @param createColl If true, create all collections now.
18 * @param type Hash type (from TileFragHash::TYPE).
19 * @param unit Measurement units for amplitude data.
20 * @param ownPolicy Ownership mode for collections.
21 *
22 * Call status() after this to check for errors.
23 */
24template <class BASE>
25TileMutableDataContainer<BASE>::TileMutableDataContainer
26 (bool createColl /*= false*/,
27 TYPE type /*= TileFragHash::Default*/,
28 UNIT unit /*= TileRawChannelUnit::ADCcounts*/,
29 SG::OwnershipPolicy ownPolicy /*= SG::OWN_ELEMENTS*/)
30 : BASE (false, type, unit, ownPolicy),
31 m_locked (false),
32 m_sc (StatusCode::SUCCESS),
33 m_defaultType (type),
34 m_defaultUnit (unit)
35{
36 if (createColl) {
37 // Create all collections.
38 const TileFragHash& hashFunc = this->hashFunc();
39 int ncoll = hashFunc.max();
40 for (int i=0; i<ncoll;++i) {
41 TileFragHash::ID frag = hashFunc.identifier(i);
42 auto coll = std::make_unique<Collection> (frag, ownPolicy) ;
43 if (addCollection (std::move (coll),
44 static_cast<IdentifierHash>(i)).isFailure())
45 {
46 m_sc = StatusCode::FAILURE;
47 }
48 }
49 }
50}
51
52
53/**
54 * @brief Copy constructor.
55 * @param other Container to copy.
56 *
57 * This is a deep copy; all contained collections and channels will be copied.
58 * Call status() after this to check for errors.
59 */
60template <class BASE>
61TileMutableDataContainer<BASE>::TileMutableDataContainer
62 (const BASE& other)
63 : BASE (false,
64 other.get_type(),
65 other.get_unit(),
66 SG::OWN_ELEMENTS),
67 m_locked (false),
68 m_sc (StatusCode::SUCCESS),
69 m_defaultType (other.get_type()),
70 m_defaultUnit (other.get_unit())
71{
72 this->set_bsflags (other.get_bsflags());
73 for (IdentifierHash hash : other.GetAllCurrentHashes()) {
74 const Collection* coll = other.indexFindPtr (hash);
75 auto newColl = std::make_unique<Collection> (*coll);
76 if (addCollection (std::move (newColl), hash).isFailure()) {
77 m_sc = StatusCode::FAILURE;
78 }
79 }
80}
81
82
83/**
84 * @brief Add a collection to the container.
85 * @param coll Collection ot add.
86 * @param hash Hash value for the collection.
87 *
88 * We maintain a non-const reference to the collection.
89 */
90template <class BASE>
91StatusCode
92TileMutableDataContainer<BASE>::addCollection (std::unique_ptr<Collection> coll,
93 IdentifierHash hash)
94{
95 if (!hash.is_valid()) {
96 throw std::out_of_range ("TileMutableDataContainer::addCollection");
97 }
98 if (hash >= m_mutableCollections.size()) {
99 m_mutableCollections.resize (hash+1);
100 }
101 m_mutableCollections.at(hash) = coll.get();
102 return BASE::addOrDelete (std::move (coll), hash);
103}
104
105
106/**
107 * @brief Add a new channel.
108 * @param rch Channel to add.
109 *
110 * This should be used for owning container (created with SG::OWN_ELEMENTS).
111 * A new collection will be created if needed.
112 * In that case, we maintain a non-const reference to it.
113 */
114template <class BASE>
115StatusCode
116TileMutableDataContainer<BASE>::push_back (std::unique_ptr<Element> rch)
117{
118 int frag = rch->frag_ID();
119 IdentifierHash hash = static_cast<IdentifierHash>(this->hashFunc()(frag));
120
121 // Find the collection; create a new one if needed.
122 Collection* coll = indexFindPtr (hash);
123 if (!coll) {
124 auto newColl = std::make_unique<Collection> (frag, SG::OWN_ELEMENTS);
125 coll = newColl.get();
126 if (addCollection (std::move (newColl), hash).isFailure()) {
127 return StatusCode::FAILURE;
128 }
129 }
130
131 coll->push_back (std::move (rch));
132 return StatusCode::SUCCESS;
133}
134
135
136/**
137 * @brief Add a new channel.
138 * @param rch Channel to add.
139 *
140 * This should be used for non-owning container (created with SG::VIEW_ELEMENTS).
141 * A new collection will be created if needed.
142 * In that case, we maintain a non-const reference to it.
143 */
144template <class BASE>
145StatusCode
146TileMutableDataContainer<BASE>::push_back (Element* rch)
147{
148 int frag = rch->frag_ID();
149 IdentifierHash hash = static_cast<IdentifierHash>(this->hashFunc()(frag));
150
151 // Find the collection; create a new one if needed.
152 Collection* coll = indexFindPtr (hash);
153 if (!coll) {
154 auto newColl = std::make_unique<Collection> (frag, SG::OWN_ELEMENTS);
155 coll = newColl.get();
156 if (addCollection (std::move (newColl), hash).isFailure()) {
157 return StatusCode::FAILURE;
158 }
159 }
160
161 coll->push_back (rch);
162 return StatusCode::SUCCESS;
163}
164
165
166/**
167 * @brief Look up a (non-const) collection via hash.
168 * @param hash Hash value to find.
169 */
170template <class BASE>
171//coverity[BAD_OVERRIDE]
172typename TileMutableDataContainer<BASE>::Collection*
173TileMutableDataContainer<BASE>::indexFindPtr (IdentifierHash hash)
174{
175 if (!m_locked && hash < m_mutableCollections.size()) {
176 return m_mutableCollections[hash];
177 }
178 return nullptr;
179}
180
181
182/**
183 * @brief Return the error status from the constructors.
184 */
185template <class BASE>
186StatusCode TileMutableDataContainer<BASE>::status() const
187{
188 return m_sc;
189}
190
191
192/**
193 * @brief Lock this object.
194 *
195 * Called when this object is locked in SG.
196 * Prohibit non-const acces to this container.
197 */
198template <class BASE>
199void TileMutableDataContainer<BASE>::lock()
200{
201 m_locked = true;
202}
203
204
205/**
206 * @brief Recycle this object for use in another event.
207 *
208 * This is called from AthenaKernel/RecyclableDataObject when this object
209 * is released by StoreGate. Unlock the object so that non-const access
210 * is again possible, and clear out the contents if the collections.
211 */
212template <class BASE>
213void TileMutableDataContainer<BASE>::recycle()
214{
215 // ??? Try to verify that this object is not in SG? Check refcount??
216 m_locked = false;
217 for (IdentifierHash hash : this->GetAllCurrentHashes()) {
218 Collection* coll = this->indexFindPtr (hash);
219 coll->clear();
220 }
221
222 this->set_type (m_defaultType);
223 this->set_unit (m_defaultUnit);
224 this->set_bsflags (0);
225}