2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
5/** @file T_AthenaPoolCoolMultChanCnv.icc
6 * @brief This templated class provides the POOL converter to
7 * translate a DataVector<ELEM_T> to and from a CondAttrListCollection. In
8 * this case, the elements of T are written/read and their POOL
9 * tokens are stored in CondAttrListCollection.
10 * @author RD Schaffer <R.D.Schaffer@cern.ch>
13#include "AthenaPoolCnvSvc/IAthenaPoolCnvSvc.h"
14#include "AthenaPoolUtilities/AthenaAttributeList.h"
15#include "AthenaPoolUtilities/CondAttrListCollection.h"
16#include "AthenaPoolUtilities/CondAttrListCollAddress.h"
17#include "AthenaPoolUtilities/CondMultChanCollection.h"
19#include "GaudiKernel/ClassID.h"
20#include "GaudiKernel/StatusCode.h"
21#include "GaudiKernel/MsgStream.h"
23#include "AthenaKernel/CLASS_DEF.h"
24#include "AthenaKernel/StorableConversions.h"
26#include "CxxUtils/checker_macros.h"
32//__________________________________________________________________________
33template <class COLL_T, class ELEM_T, class ELEM_P>
34T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::T_AthenaPoolCoolMultChanCnv(ISvcLocator* svcloc)
36 T_AthenaPoolCustCnv<COLL_T, ELEM_T>(svcloc)
38 ATH_MSG_DEBUG("AthenaPool Converter Constructor for " << this->classID());
41//__________________________________________________________________________
42template <class COLL_T, class ELEM_T, class ELEM_P>
43StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::createObj(IOpaqueAddress* pAddr, DataObject*& pObj)
45 // This is the "read" method for a collection T (i.e. DataObject
46 // pObj) which contains objects of type T::value_type.
48 // There are two situations we must allow for:
50 // 1) When reading from COOL - the "standard production" scenario
51 // 2) When reading directly the collection from a pool file/db.
52 // This is used by "developers" who are testing their
55 // For 1) the IOpaqueAddress contains a ptr to a
56 // CondAttrListCollection, and this contains tokens to each of the
57 // objects in the collection to be read.
59 // For 2) the IOpaqueAddress DOES NOT contain a ptr to a
60 // CondAttrListCollection, rather we simply use the collection
61 // token and read in the CondMultChanCollImpl and build a
62 // CondAttrListCollection
65 ATH_MSG_DEBUG("Read in objects for collection");
67 m_persCtx = pAddr->ipar()[0];
69 CondAttrListCollAddress* addr = dynamic_cast<CondAttrListCollAddress*>(pAddr);
71 std::unique_ptr<COLL_T> obj;
73 // Successful cast - situation 1)
75 // Loop over tokens inside the CondAttrListCollection, read
76 // them in and add them to the data object.
77 CondAttrListCollection* coll = addr->attrListColl();
78 COLL_T* pobj = nullptr;
79 StatusCode sc = attrListCollToObject(coll, pobj);
80 obj = std::unique_ptr<COLL_T>(pobj);
81 if (sc != StatusCode::SUCCESS) {
82 ATH_MSG_ERROR("Unable to read in tokens from CondAttrListCollection");
83 return(StatusCode::FAILURE);
85 ATH_MSG_DEBUG("Created CondAttrListCollection from an CondAttrListCollAddress");
87 // Situation 2) - read in the CondMultChanCollImpl and build a
88 // CondAttrListCollection from the CondMultChanCollImpl
89 COLL_T* pobj = nullptr;
90 StatusCode sc = condMultChanCollImplToObject( *(pAddr->par()), pobj);
91 obj = std::unique_ptr<COLL_T>(pobj);
92 if (sc != StatusCode::SUCCESS) {
93 ATH_MSG_ERROR("Unable to read in CondMultChanCollImpl");
94 return(StatusCode::FAILURE);
96 ATH_MSG_DEBUG("Created CondAttrListCollection from an CondMultChanCollImpl");
99 // Initialize object after having been read in
100 StatusCode sc = obj->initialize();
101 if (sc != StatusCode::SUCCESS) {
102 ATH_MSG_ERROR("Unable to initialize object read in");
103 return(StatusCode::FAILURE);
106 // Convert to DataObject
107 pObj = SG::asStorable (std::move (obj));
109 ATH_MSG_ERROR("Cannot get DataObject from COLL_T");
110 return(StatusCode::FAILURE);
112 ATH_MSG_DEBUG("End createObj");
113 return(StatusCode::SUCCESS);
116//__________________________________________________________________________
117template <class COLL_T, class ELEM_T, class ELEM_P>
119T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::objectToAttrListColl ATLAS_NOT_THREAD_SAFE
120 (COLL_T* obj, IOpaqueAddress*& pAddr,
121 CondAttrListCollection*& attrListColl,
122 std::unique_ptr<Token>& implToken)
124 ATH_MSG_DEBUG("Creating a CondAttrListCollection from a collection");
127 attrListColl = new CondAttrListCollection(true); // set hasRunEventTime
129 // Make sure the channel vector is filled
130 if (obj->size() != obj->chan_size()) {
131 ATH_MSG_ERROR("Must fill in channel numbers! Number of objects: " << obj->size()
132 << " Number of channels: " << obj->chan_size());
133 return(StatusCode::FAILURE);
136 // Fill IOVs if they are there
137 bool hasIOVs = (obj->iov_size() == obj->size());
139 // Get the contained object in the COLL_T
140 CondMultChanCollImpl* impl = obj->implementation();
142 // Reset tokens in case this object was previously written out
145 // Loop over objects in COLL_T*
146 typename COLL_T::chan_const_iterator itChan = obj->chan_begin();
147 typename COLL_T::iov_const_iterator itIOV = obj->iov_begin();
148 std::string token_str;
149 std::lock_guard<AthenaPoolConverter::CallMutex> lock(this->m_conv_mut);
150 for (unsigned int chan = 0; chan < obj->size(); ++chan, ++itChan) {
151 ELEM_T* elem = (*obj)[chan];
152 // Allow for T/P separation, convert to persistent object
153 ELEM_P* elem_p = this->createPersistent(elem);
154 std::unique_ptr<Token> token;
155 StatusCode sc = this->objectToPool(elem_p, token, "", *pAddr->par());
156 if (sc != StatusCode::SUCCESS || !token) {
157 ATH_MSG_ERROR("Unable to write out object");
158 return(StatusCode::FAILURE);
160 token_str = token->toString();
162 // Create a new AthenaAttributeList
163 AthenaAttributeList attrList;
164 attrList.extend("PoolRef","string");
165 attrList["PoolRef"].setValue( token_str );
166 attrListColl->add((*itChan), attrList);
168 attrListColl->add((*itChan), (*itIOV));
171 ATH_MSG_DEBUG("Adding new attrList for token: " << token_str);
173 // Save as well in implementation
174 impl->add(token_str);
178 ATH_MSG_DEBUG("Print out tokens in implementaion:");
179 CondMultChanCollImpl::token_const_iterator itToken = impl->token_begin();
180 CondMultChanCollImpl::token_const_iterator itTokenEnd = impl->token_end();
181 CondMultChanCollImpl::chan_const_iterator itChan1 = impl->chan_begin();
182 for (; itToken != itTokenEnd; ++itToken, ++itChan1) {
183 ATH_MSG_DEBUG(*itToken);
184 ATH_MSG_DEBUG("Chan " << *itChan1);
187 StatusCode sc = this->objectToPool(impl, implToken, "", *pAddr->par());
188 if (sc != StatusCode::SUCCESS || !implToken) {
189 ATH_MSG_ERROR("Unable to write out CondMultChanCollImpl");
190 return(StatusCode::FAILURE);
192 ATH_MSG_DEBUG("Wrote out CondMultChanCollImpl - token: " << implToken->toString());
194 ATH_MSG_DEBUG("End objectToAttrListColl");
195 return(StatusCode::SUCCESS);
198//__________________________________________________________________________
199template <class COLL_T, class ELEM_T, class ELEM_P>
200StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::createRep(DataObject* pObj, IOpaqueAddress*& pAddr)
202 // This is the "write" method for a collection of COLL_T*
203 // (i.e. DataObject pObj). Each element of the collection is
204 // written out and the tokens are stored in a
205 // CondAttrListCollection.
207 ATH_MSG_DEBUG("Write out objects in collection");
210 SG::fromStorable(pObj, obj);
212 ATH_MSG_ERROR("Unable to convert DataObject");
213 return(StatusCode::FAILURE);
215 CondAttrListCollection* coll = 0;
216 std::unique_ptr<Token> token;
217 // Ok, because it's just creating a new list.
218 StatusCode sc ATLAS_THREAD_SAFE = objectToAttrListColl(obj, pAddr, coll, token);
219 if (sc != StatusCode::SUCCESS || !token) {
220 ATH_MSG_ERROR("Unable to get objects and fill CondAttrListCollection with tokens");
221 return(StatusCode::FAILURE);
223 CondAttrListCollAddress* addr = new CondAttrListCollAddress(POOL_StorageType,
226 addr->setAttrListColl(coll);
227 delete pAddr; pAddr = addr;
230 ATH_MSG_DEBUG("End createRep");
231 return(StatusCode::SUCCESS);
234//__________________________________________________________________________
235template <class COLL_T, class ELEM_T, class ELEM_P>
236StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::fillRepRefs(IOpaqueAddress* /*pAddr*/, DataObject* /*pObj*/)
238 ATH_MSG_DEBUG("End fillRepRefs");
239 return(StatusCode::SUCCESS);
242//__________________________________________________________________________
243template <class COLL_T, class ELEM_T, class ELEM_P>
245T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::attrListCollToObject(CondAttrListCollection* attrListColl,
249 ATH_MSG_DEBUG("Creating a collection from a CondAttrListCollection");
254 // Check the sizes of objs and IOVs
255 if (attrListColl->size() != attrListColl->iov_size()) {
256 ATH_MSG_ERROR("CondAttrListCollection incorrect size for IOVs! "
257 << " Number of objects: " << attrListColl->size()
258 << " Number of IOVs: " << attrListColl->iov_size());
259 return(StatusCode::FAILURE);
262 /// Add the pointer to the CondAttrListCollection to COLL_T
263 /// object. This will allow subsequent addition of IOVRange
264 /// objects added to COLL_T to be propagated to the
265 /// CondAttrListCollection. This is needed for IOV registration
266 /// which may happen AFTER a read back of the COLL_T.
267 obj->implementation()->setAttrListColl(attrListColl);
269 // Loop over objects in COLL_T*
271 CondAttrListCollection::const_iterator it = attrListColl->begin();
272 CondAttrListCollection::const_iterator last = attrListColl->end();
273 CondAttrListCollection::iov_const_iterator itIOV = attrListColl->iov_begin();
276 const std::string contextStr = std::format("[CTXT={:08X}]", m_persCtx);
277 std::strncpy(context, contextStr.c_str(), sizeof(context) - 1);
278 for (; it != last; ++it, ++itIOV) {
281 const coral::AttributeList& attrList = (*it).second;
282 token=attrList["PoolRef"].data<std::string>();
285 catch(const std::exception& x) {
286 ATH_MSG_ERROR("PoolRef not found in attribute list");
287 return(StatusCode::FAILURE);
289 // Allow T/P separation - set token and use createTransient
290 ELEM_T* elem {nullptr};
292 std::lock_guard<AthenaPoolConverter::CallMutex> lock(this->m_conv_mut);
293 poolToken.fromString(token + context);
294 elem = this->createTransient(&poolToken);
296 // Add elem, channel number and IOV
297 obj->push_back(elem);
298 obj->add((*it).first);
299 obj->add((*itIOV).second);
301 ATH_MSG_DEBUG("Read in new object for token: " << token);
305 ATH_MSG_DEBUG("End attrListCollToObject");
306 return(StatusCode::SUCCESS);
309/// Read in CondMultChanCollImpl and the objects for its tokens,
310/// saving them in the output collection
311/// @param collImpl [IN] CondMultChanCollImpl token
312/// @param obj [OUT] pointer to the collection of objects.
313template <class COLL_T, class ELEM_T, class ELEM_P>
315T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::condMultChanCollImplToObject(const std::string& collImplToken,
319 ATH_MSG_DEBUG("Creating a collection from a CondMultChanCollImpl for token");
325 // Read in the CondMultChanCollImpl
326 std::lock_guard<AthenaPoolConverter::CallMutex> lock(this->m_conv_mut);
327 CondMultChanCollImpl* impl = 0;
330 const std::string contextStr = std::format("[CTXT={:08X}]", m_persCtx);
331 std::strncpy(context, contextStr.c_str(), sizeof(context) - 1);
332 poolToken.fromString(collImplToken + context);
333 StatusCode sc = this->poolToObject(&poolToken, impl);
334 if (sc != StatusCode::SUCCESS) {
335 ATH_MSG_ERROR("Unable to read in CondMultChanCollImpl");
336 return(StatusCode::FAILURE);
339 // Make sure the channel vector is filled
340 if (impl->token_size() != impl->chan_size()) {
341 ATH_MSG_ERROR("Token and channel vectors are not the same size! Number of tokens: " << impl->token_size()
342 << " Number of channels: " << impl->chan_size());
343 return(StatusCode::FAILURE);
346 // Fill IOVs if they are there
347 bool hasIOVs = (impl->iov_size() == impl->token_size());
349 // Loop over tokens in Impl and retrieve objects
350 CondMultChanCollImpl::chan_const_iterator itChan = impl->chan_begin();
351 CondMultChanCollImpl::iov_const_iterator itIOV = impl->iov_begin();
352 CondMultChanCollImpl::token_const_iterator itToken = impl->token_begin();
353 for (unsigned int i = 0; i < impl->chan_size(); ++i, ++itChan, ++itIOV, ++itToken) {
355 // Allow T/P separation
356 poolToken.fromString(*itToken + context);
357 elem = this->createTransient(&poolToken);
359 // Add elem, channel number and IOV
360 obj->push_back(elem);
362 if (hasIOVs)obj->add(*itIOV);
364 ATH_MSG_DEBUG("Read in new object for token: " << *itToken);
370 ATH_MSG_DEBUG("End condMultChanCollImplToObject");
371 return(StatusCode::SUCCESS);
374/// Dummy methods not needed here
375//__________________________________________________________________________
376template <class COLL_T, class ELEM_T, class ELEM_P>
378StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::transToPers(COLL_T* /*obj*/, ELEM_T*& /*persObj*/)
380 return(StatusCode::SUCCESS);
383//__________________________________________________________________________
384template <class COLL_T, class ELEM_T, class ELEM_P>
386StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::persToTrans(COLL_T*& /*transObj*/, ELEM_T* /*obj*/)
388 return(StatusCode::SUCCESS);
391//__________________________________________________________________________
392// Read object of type P. This is an exception-throwing version of poolToObject()
393// plus reading of all extending objects
394template <class COLL_T, class ELEM_T, class ELEM_P>
397P* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::poolReadObject(const Token* token) {
399 if( this->poolToObject( token , persObj ).isFailure() ) {
400 throw std::runtime_error("POOL read failed");
405//__________________________________________________________________________
406// Read object of type P. This is an exception-throwing version of poolToObject()
407// plus reading of all extending objects
408template <class COLL_T, class ELEM_T, class ELEM_P>
410ELEM_T* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::poolReadObject(const Token* token) {
412 if( this->poolToObject( token, persObj ).isFailure() ) {
413 throw std::runtime_error("POOL read failed");
418//__________________________________________________________________________
419template <class COLL_T, class ELEM_T, class ELEM_P>
420ELEM_P* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::createPersistent(ELEM_T* tobj)
422 // Default implementation - ELEM_T == ELEM_P so just return input
423 ELEM_P* obj = dynamic_cast<ELEM_P*>(tobj);
427//__________________________________________________________________________
428template <class COLL_T, class ELEM_T, class ELEM_P>
429ELEM_T* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::createTransient(const Token* token)
431 // Default version - no T/P separation. We read in transient
434 ELEM_T* elem = this->poolReadObject(token);