ATLAS Offline Software
Loading...
Searching...
No Matches
TrigSerializeConverter.h
Go to the documentation of this file.
1// Dear emacs, this is -*- c++ -*-
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// $Id$
8#ifndef TRIGSERIALIZECONVERTER_H
9#define TRIGSERIALIZECONVERTER_H
10
11#include "GaudiKernel/Converter.h"
12#include "GaudiKernel/ToolHandle.h"
13#include "GaudiKernel/ServiceHandle.h"
14
18
22
26
27#include <memory>
28
29
30//fwd
31template< class Cnv >
32class CnvFactory;
33
34/***
35 * @brief TrigSerializeConverter is a templated converter which implements
36 * conversions from transient to persistent representation (and back)
37 * used by TrigSerializeCnvSvc to serialize and deserialize Trigger objects
38 *
39 * @author Jiri Masik Jiri.Masik@cern.ch
40 * @author Lukas Heinrich lukas.heinrich@cern.ch
41 */
42
43namespace SG{class AuxElement;}
44namespace SG{class IAuxStore;}
45
46
47template<typename T>
48 struct isxAOD {
49 template<typename C>
50 constexpr static typename std::enable_if<std::is_base_of<SG::AuxElement,typename std::pointer_traits<typename C::value_type>::element_type>::value,bool>::type isxAODVector(C*){return true;}
51 template<typename C>
52 constexpr static bool isxAODVector(...){return false;}
53 template<typename C>
54 constexpr static typename std::enable_if<std::is_base_of<SG::IAuxStore,C>::value,bool>::type isAuxVector(C*){return true;}
55 template<typename C>
56 constexpr static bool isAuxVector(...){return false;}
57 static const bool value = isxAODVector<T>(0) || isAuxVector<T>(0);
58};
59
60
62
63
71template <class DATA>
72DATA* prepareForWrite (DATA* d, std::unique_ptr<DATA>& /*holder*/)
73{
74 return d;
75}
76
77
88template <class DV>
90 std::unique_ptr<ViewVector<DV> >& holder)
91{
92 holder = std::make_unique<ViewVector<DV> > (*d);
93 holder->setClearOnPersistent();
94 return holder.get();
95}
96
97
104template <class DATA>
105DATA* finishRead (DATA* d)
106{
107 return d;
108}
109
110
118template <class DV>
120{
121 // In the case of schema evolution, we'll actually get a
122 // @c ViewVector for a different _v type.
123 if (typeid(*d) != typeid(ViewVector<DV>)) {
124 auto d2 = std::make_unique<ViewVector<DV> > (*d);
125 delete d;
126 d = d2.release();
127 // root read rule doesn't do anything in this case.
128 d->toTransient();
129 }
130 d->clearPersistent();
131 return d;
132}
133
134
135} // namespace TrigSerialization
136
137
138template< typename DATA >
140
141public:
142 static const bool typeIsxAOD = isxAOD<DATA>::value;
143
144 // per->tran
145 StatusCode createObj( IOpaqueAddress* iAddr, DataObject *& pO ) {
146
147 const std::string clname = ClassID_traits< DATA >::typeName();
148
149 std::string normalized = SG::normalizedTypeinfoName( typeid(DATA) ); // normalized string representation (matching the names used in the root dictionary)
150
151
152 void *ptr( 0 );
153 if( m_log->level() <= MSG::DEBUG ) {
154 *m_log << MSG::DEBUG << "In createObj for : " << clname << " normalized to " << normalized << endmsg;
155 }
156
157 StatusCode sc = m_convHelper->createObj( normalized, iAddr, ptr, typeIsxAOD);
158 if( sc.isFailure() ){
159 *m_log << MSG::WARNING << "m_convHelper->createObj failed for "
160 << clname << endmsg;
161 }
162
163 DATA *nObj = ( DATA* ) ptr;
164 nObj = TrigSerialization::finishRead (nObj);
165 if (!nObj){
166 *m_log << MSG::ERROR << "TrigSerializeConverter::createObj: nObj is null."<< endmsg;
167 return StatusCode::FAILURE;
168 }
169 pO = SG::asStorable( nObj );
170 if( m_log->level() <= MSG::DEBUG ) {
171 *m_log << MSG::DEBUG << "IOpaq: " << iAddr
172 << " created nObj: " << nObj << endmsg;
173 }
174
175
176
177
178 if( m_sgsvc){
179 TrigStreamAddress *addr = dynamic_cast< TrigStreamAddress* >( iAddr );
180 if( addr ) {
181 if( m_sgsvc->contains< DATA >( addr->sgkey() ) ) {
182 *m_log << MSG::WARNING << "TrigSerializeConverter::createObj object "
183 << clname << " / " << addr->sgkey()
184 << " is already in the store; not overwriting"
185 << endmsg;
186 } else {
187 sc = m_sgsvc->record( nObj, addr->sgkey() );
188 }
189 if( sc.isFailure() ) {
190 *m_log << MSG::ERROR << "SG::record failed for " << addr->sgkey()
191 << endmsg;
192 } else if( m_log->level() <= MSG::DEBUG ) {
193 *m_log << MSG::DEBUG << "SG::record key: "
194 << addr->sgkey() << " class: " << clname << endmsg;
195 }
196 } else {
197 *m_log << MSG::WARNING << "createObj cast failed" << endmsg;
198 }
199 } else if( m_log->level() <= MSG::DEBUG ) {
200 *m_log << MSG::DEBUG << "did not put an object into SG" << endmsg;
201 }
202
203 return sc;
204 }
205
206 // tran->per
207 StatusCode createRep( DataObject* pObj, IOpaqueAddress*& pAddr ) {
208
209 // const std::string clname = ClassID_traits<DATA>::typeName(); // old style, does not contain the _vX of the actual class type
210
211 // const std::type_info& typeId = ClassID_traits<DATA>::typeId(); // damn, this line just does not compile
212
213 std::string classname = SG::normalizedTypeinfoName( typeid(DATA) ); // normalized string representation (matching the names used in the root dictionary)
214
215 DATA *d( 0 );
216 SG::fromStorable( pObj, d );
217 if( m_log->level() <= MSG::DEBUG ) {
218 *m_log << MSG::DEBUG << "My createRep for " << classID() << " "
219 << classname << endmsg;
220 }
221
222 std::unique_ptr<DATA> holder;
223 void *serptr = TrigSerialization::prepareForWrite (d, holder);
224 //
225 std::vector< uint32_t > ser;
226
227 StatusCode sc = m_convHelper->createRep( classname, serptr, ser, typeIsxAOD);
228 if( m_log->level() <= MSG::DEBUG ) {
229 *m_log << MSG::DEBUG << "convHelper " << ser.size() << endmsg;
230 }
231
232 TrigStreamAddress *addr = new TrigStreamAddress( classID(), classname, "" );
233 addr->add( ser );
234
235 pAddr = addr;
236
237 return sc;
238 }
239
240 StatusCode initialize(){
241
242 if( m_log ) {
243 m_log->setLevel( msgSvc()->outputLevel( "TrigSerializeConverter" ) );
244 } else {
245 return StatusCode::FAILURE;
246 }
247
248 StatusCode sc = m_convHelper.retrieve();
249 if( ! sc.isSuccess() ) {
250 if( m_log->level() <= MSG::DEBUG ) {
251 *m_log << MSG::DEBUG << m_convHelper << "not retrieved" << endmsg;
252 }
253 } else {
254 if( m_log->level() <= MSG::DEBUG ) {
255 *m_log << MSG::DEBUG << m_convHelper << "m_serializer retrieved"
256 << endmsg;
257 }
258 if( ! m_convHelper->initialize().isSuccess() ) {
259 if( m_log->level() <= MSG::DEBUG ) {
260 *m_log << MSG::DEBUG << m_convHelper << " not initialized"
261 << endmsg;
262 }
263 }
264 }
265
266 StatusCode scsg = m_sgHandle.retrieve();
267 if (scsg.isFailure())
268 *m_log << MSG::ERROR << "cannot access SG" << endmsg;
269 else {
270 m_sgsvc = m_sgHandle.operator->();
271 }
272 return sc;
273 }
274
275 static CLID classID() { return ClassID_traits<DATA>::ID(); }
276
277 virtual long int repSvcType() const;
278 static long int storageType();
279
280 TrigSerializeConverter( ISvcLocator* svcloc )
281 : Converter( storageType(), classID(), svcloc ),
282 m_convHelper( "TrigSerializeConvHelper/TrigSerializeConvHelper" ),
283 m_sgHandle( "StoreGateSvc", "TrigSerializeConverter" ),
284 m_log( 0 ), m_sgsvc( 0 ) {
285
286 m_log = new MsgStream( msgSvc(), "TrigSerializeConverter" );
287 }
288
290 delete m_log;
291 }
292
293private:
294 ToolHandle< ITrigSerializeConvHelper > m_convHelper;
296 MsgStream *m_log;
298};
299
300
301template <typename DATA>
305template <typename DATA>
307 return storageType();
308}
309
310
311#endif // TRIGSERIALIZECONVERTER_H
#define endmsg
a traits class that associates a CLID to a type T It also detects whether T inherits from Gaudi DataO...
uint32_t CLID
The Class ID type.
static Double_t sc
convert to and from a SG storable
Identify view containers to be made persistent.
Abstract factory to create the converter.
Base class for elements of a container that can have aux data.
Definition AuxElement.h:484
Interface for non-const operations on an auxiliary store.
Definition IAuxStore.h:48
void setClearOnPersistent()
Set a flag to declare that the vector should be cleared on the next call to toPersistent().
The Athena Transient Store API.
static long int storageType()
ServiceHandle< StoreGateSvc > m_sgHandle
TrigSerializeConverter(ISvcLocator *svcloc)
virtual long int repSvcType() const
StatusCode createRep(DataObject *pObj, IOpaqueAddress *&pAddr)
ToolHandle< ITrigSerializeConvHelper > m_convHelper
StatusCode createObj(IOpaqueAddress *iAddr, DataObject *&pO)
IOpaqueAddress for TrigSerializenCnvSvc.
void add(const std::vector< uint32_t > &a)
const std::string & sgkey() const
Identify view containers to be made persistent.
Definition ViewVector.h:67
struct color C
Forward declaration.
std::string normalizedTypeinfoName(const std::type_info &info)
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
bool fromStorable(DataObject *pDObj, T *&pTrans, bool quiet=false, IRegisterTransient *irt=0, bool isConst=true)
DataObject * asStorable(SG::DataObjectSharedPtr< T > pObject)
DATA * finishRead(DATA *d)
Called after an object has been read from BS.
DATA * prepareForWrite(DATA *d, std::unique_ptr< DATA > &)
Called before serializing an object to BS.
Helper to disable undefined behavior sanitizer for a function.
#define NO_SANITIZE_UNDEFINED
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
static const bool value
static constexpr bool isAuxVector(...)
static constexpr bool isxAODVector(...)
static constexpr std::enable_if< std::is_base_of< SG::IAuxStore, C >::value, bool >::type isAuxVector(C *)
static constexpr std::enable_if< std::is_base_of< SG::AuxElement, typenamestd::pointer_traits< typenameC::value_type >::element_type >::value, bool >::type isxAODVector(C *)