2 Copyright (C) 2002-2021 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"
31 //__________________________________________________________________________
32 template <class COLL_T, class ELEM_T, class ELEM_P>
33 T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::T_AthenaPoolCoolMultChanCnv(ISvcLocator* svcloc)
35 T_AthenaPoolCustCnv<COLL_T, ELEM_T>(svcloc)
37 ATH_MSG_DEBUG("AthenaPool Converter Constructor for " << this->classID());
40 //__________________________________________________________________________
41 template <class COLL_T, class ELEM_T, class ELEM_P>
42 StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::createObj(IOpaqueAddress* pAddr, DataObject*& pObj)
44 // This is the "read" method for a collection T (i.e. DataObject
45 // pObj) which contains objects of type T::value_type.
47 // There are two situations we must allow for:
49 // 1) When reading from COOL - the "standard production" scenario
50 // 2) When reading directly the collection from a pool file/db.
51 // This is used by "developers" who are testing their
54 // For 1) the IOpaqueAddress contains a ptr to a
55 // CondAttrListCollection, and this contains tokens to each of the
56 // objects in the collection to be read.
58 // For 2) the IOpaqueAddress DOES NOT contain a ptr to a
59 // CondAttrListCollection, rather we simply use the collection
60 // token and read in the CondMultChanCollImpl and build a
61 // CondAttrListCollection
64 ATH_MSG_DEBUG("Read in objects for collection");
66 m_persCtx = pAddr->ipar()[0];
68 CondAttrListCollAddress* addr = dynamic_cast<CondAttrListCollAddress*>(pAddr);
70 std::unique_ptr<COLL_T> obj;
72 // Successful cast - situation 1)
74 // Loop over tokens inside the CondAttrListCollection, read
75 // them in and add them to the data object.
76 CondAttrListCollection* coll = addr->attrListColl();
77 COLL_T* pobj = nullptr;
78 StatusCode sc = attrListCollToObject(coll, pobj);
79 obj = std::unique_ptr<COLL_T>(pobj);
80 if (sc != StatusCode::SUCCESS) {
81 ATH_MSG_ERROR("Unable to read in tokens from CondAttrListCollection");
82 return(StatusCode::FAILURE);
84 ATH_MSG_DEBUG("Created CondAttrListCollection from an CondAttrListCollAddress");
86 // Situation 2) - read in the CondMultChanCollImpl and build a
87 // CondAttrListCollection from the CondMultChanCollImpl
88 COLL_T* pobj = nullptr;
89 StatusCode sc = condMultChanCollImplToObject( *(pAddr->par()), pobj);
90 obj = std::unique_ptr<COLL_T>(pobj);
91 if (sc != StatusCode::SUCCESS) {
92 ATH_MSG_ERROR("Unable to read in CondMultChanCollImpl");
93 return(StatusCode::FAILURE);
95 ATH_MSG_DEBUG("Created CondAttrListCollection from an CondMultChanCollImpl");
98 // Initialize object after having been read in
99 StatusCode sc = obj->initialize();
100 if (sc != StatusCode::SUCCESS) {
101 ATH_MSG_ERROR("Unable to initialize object read in");
102 return(StatusCode::FAILURE);
105 // Convert to DataObject
106 pObj = SG::asStorable (std::move (obj));
108 ATH_MSG_ERROR("Cannot get DataObject from COLL_T");
109 return(StatusCode::FAILURE);
111 ATH_MSG_DEBUG("End createObj");
112 return(StatusCode::SUCCESS);
115 //__________________________________________________________________________
116 template <class COLL_T, class ELEM_T, class ELEM_P>
118 T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::objectToAttrListColl ATLAS_NOT_THREAD_SAFE
119 (COLL_T* obj, IOpaqueAddress*& pAddr,
120 CondAttrListCollection*& attrListColl,
123 ATH_MSG_DEBUG("Creating a CondAttrListCollection from a collection");
126 attrListColl = new CondAttrListCollection(true); // set hasRunEventTime
128 // Make sure the channel vector is filled
129 if (obj->size() != obj->chan_size()) {
130 ATH_MSG_ERROR("Must fill in channel numbers! Number of objects: " << obj->size()
131 << " Number of channels: " << obj->chan_size());
132 return(StatusCode::FAILURE);
135 // Fill IOVs if they are there
136 bool hasIOVs = (obj->iov_size() == obj->size());
138 // Get the contained object in the COLL_T
139 CondMultChanCollImpl* impl = obj->implementation();
141 // Reset tokens in case this object was previously written out
144 // Loop over objects in COLL_T*
145 typename COLL_T::chan_const_iterator itChan = obj->chan_begin();
146 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 StatusCode sc = this->objectToPool(elem_p, token, "", *pAddr->par());
155 if (sc != StatusCode::SUCCESS || !token) {
156 ATH_MSG_ERROR("Unable to write out object");
157 return(StatusCode::FAILURE);
159 token_str = token->toString();
161 // Create a new AthenaAttributeList
162 AthenaAttributeList attrList;
163 attrList.extend("PoolRef","string");
164 attrList["PoolRef"].setValue( token_str );
165 attrListColl->add((*itChan), attrList);
167 attrListColl->add((*itChan), (*itIOV));
170 ATH_MSG_DEBUG("Adding new attrList for token: " << token_str);
172 // Save as well in implementation
173 impl->add(token_str);
174 delete token; token = 0;
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 //__________________________________________________________________________
199 template <class COLL_T, class ELEM_T, class ELEM_P>
200 StatusCode 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;
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;
229 delete token; token = 0;
231 ATH_MSG_DEBUG("End createRep");
232 return(StatusCode::SUCCESS);
235 //__________________________________________________________________________
236 template <class COLL_T, class ELEM_T, class ELEM_P>
237 StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::fillRepRefs(IOpaqueAddress* /*pAddr*/, DataObject* /*pObj*/)
239 ATH_MSG_DEBUG("End fillRepRefs");
240 return(StatusCode::SUCCESS);
243 //__________________________________________________________________________
244 template <class COLL_T, class ELEM_T, class ELEM_P>
246 T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::attrListCollToObject(CondAttrListCollection* attrListColl,
250 ATH_MSG_DEBUG("Creating a collection from a CondAttrListCollection");
255 // Check the sizes of objs and IOVs
256 if (attrListColl->size() != attrListColl->iov_size()) {
257 ATH_MSG_ERROR("CondAttrListCollection incorrect size for IOVs! "
258 << " Number of objects: " << attrListColl->size()
259 << " Number of IOVs: " << attrListColl->iov_size());
260 return(StatusCode::FAILURE);
263 /// Add the pointer to the CondAttrListCollection to COLL_T
264 /// object. This will allow subsequent addition of IOVRange
265 /// objects added to COLL_T to be propagated to the
266 /// CondAttrListCollection. This is needed for IOV registration
267 /// which may happen AFTER a read back of the COLL_T.
268 obj->implementation()->setAttrListColl(attrListColl);
270 // Loop over objects in COLL_T*
272 CondAttrListCollection::const_iterator it = attrListColl->begin();
273 CondAttrListCollection::const_iterator last = attrListColl->end();
274 CondAttrListCollection::iov_const_iterator itIOV = attrListColl->iov_begin();
275 for (; it != last; ++it, ++itIOV) {
278 const coral::AttributeList& attrList = (*it).second;
279 token=attrList["PoolRef"].data<std::string>();
282 catch(const std::exception& x) {
283 ATH_MSG_ERROR("PoolRef not found in attribute list");
284 return(StatusCode::FAILURE);
286 // Allow T/P separation - set token and use createTransient
287 ELEM_T* elem {nullptr};
289 std::lock_guard<AthenaPoolConverter::CallMutex> lock(this->m_conv_mut);
290 this->setToken( token );
291 elem = this->createTransient();
293 // Add elem, channel number and IOV
294 obj->push_back(elem);
295 obj->add((*it).first);
296 obj->add((*itIOV).second);
298 ATH_MSG_DEBUG("Read in new object for token: " << token);
302 ATH_MSG_DEBUG("End attrListCollToObject");
303 return(StatusCode::SUCCESS);
306 /// Read in CondMultChanCollImpl and the objects for its tokens,
307 /// saving them in the output collection
308 /// @param collImpl [IN] CondMultChanCollImpl token
309 /// @param obj [OUT] pointer to the collection of objects.
310 template <class COLL_T, class ELEM_T, class ELEM_P>
312 T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::condMultChanCollImplToObject(const std::string& collImplToken,
316 ATH_MSG_DEBUG("Creating a collection from a CondMultChanCollImpl for token");
322 // Read in the CondMultChanCollImpl
323 std::lock_guard<AthenaPoolConverter::CallMutex> lock(this->m_conv_mut);
324 CondMultChanCollImpl* impl = 0;
325 this->setToken(collImplToken);
326 StatusCode sc = this->poolToObject(this->m_i_poolToken, impl);
327 if (sc != StatusCode::SUCCESS) {
328 ATH_MSG_ERROR("Unable to read in CondMultChanCollImpl");
329 return(StatusCode::FAILURE);
332 // Make sure the channel vector is filled
333 if (impl->token_size() != impl->chan_size()) {
334 ATH_MSG_ERROR("Token and channel vectors are not the same size! Number of tokens: " << impl->token_size()
335 << " Number of channels: " << impl->chan_size());
336 return(StatusCode::FAILURE);
339 // Fill IOVs if they are there
340 bool hasIOVs = (impl->iov_size() == impl->token_size());
342 // Loop over tokens in Impl and retrieve objects
343 CondMultChanCollImpl::chan_const_iterator itChan = impl->chan_begin();
344 CondMultChanCollImpl::iov_const_iterator itIOV = impl->iov_begin();
345 CondMultChanCollImpl::token_const_iterator itToken = impl->token_begin();
346 for (unsigned int i = 0; i < impl->chan_size(); ++i, ++itChan, ++itIOV, ++itToken) {
348 // Allow T/P separation - set token and use createTransient
349 this->setToken( *itToken );
350 elem = this->createTransient();
352 // Add elem, channel number and IOV
353 obj->push_back(elem);
355 if (hasIOVs)obj->add(*itIOV);
357 ATH_MSG_DEBUG("Read in new object for token: " << *itToken);
363 ATH_MSG_DEBUG("End condMultChanCollImplToObject");
364 return(StatusCode::SUCCESS);
367 /// Dummy methods not needed here
368 //__________________________________________________________________________
369 template <class COLL_T, class ELEM_T, class ELEM_P>
371 StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::transToPers(COLL_T* /*obj*/, ELEM_T*& /*persObj*/)
373 return(StatusCode::SUCCESS);
376 //__________________________________________________________________________
377 template <class COLL_T, class ELEM_T, class ELEM_P>
379 StatusCode T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::persToTrans(COLL_T*& /*transObj*/, ELEM_T* /*obj*/)
381 return(StatusCode::SUCCESS);
384 //__________________________________________________________________________
385 // Read object of type P. This is an exception-throwing version of poolToObject()
386 // plus reading of all extending objects
387 template <class COLL_T, class ELEM_T, class ELEM_P>
390 P* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::poolReadObject() {
392 if( this->poolToObject( this->m_i_poolToken , persObj ).isFailure() ) {
393 throw std::runtime_error("POOL read failed");
398 //__________________________________________________________________________
399 // Read object of type P. This is an exception-throwing version of poolToObject()
400 // plus reading of all extending objects
401 template <class COLL_T, class ELEM_T, class ELEM_P>
403 ELEM_T* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::poolReadObject() {
405 if( this->poolToObject( this->m_i_poolToken , persObj ).isFailure() ) {
406 throw std::runtime_error("POOL read failed");
411 //__________________________________________________________________________
412 template <class COLL_T, class ELEM_T, class ELEM_P>
413 ELEM_P* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::createPersistent(ELEM_T* tobj)
415 // Default implementation - ELEM_T == ELEM_P so just return input
416 ELEM_P* obj = dynamic_cast<ELEM_P*>(tobj);
420 //__________________________________________________________________________
421 template <class COLL_T, class ELEM_T, class ELEM_P>
422 ELEM_T* T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::createTransient()
424 // Default version - no T/P separation. We read in transient
427 ELEM_T* elem = this->poolReadObject();
431 //__________________________________________________________________________
432 /// specialized version that adds persistency contextID to tokens (for reading)
433 template <class COLL_T, class ELEM_T, class ELEM_P>
435 T_AthenaPoolCoolMultChanCnv<COLL_T, ELEM_T, ELEM_P>::
436 setToken (const std::string& token)
439 ::sprintf(context, "[CTXT=%08X]", m_persCtx);
440 T_AthenaPoolCustCnv<COLL_T, ELEM_T>::setToken( token + context );