ATLAS Offline Software
Loading...
Searching...
No Matches
Blob2ToolConstants.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3*/
4
6
8#include "CoralBase/Attribute.h"
9#include "CoralBase/AttributeListSpecification.h"
10#include "CoralBase/AttributeListException.h"
11#include "CoralBase/Blob.h"
12
13#include "TBufferFile.h"
14#include "TClass.h"
15
17#include "CxxUtils/crc64.h"
18
20 const std::string& name,
21 const IInterface* parent) :
22 AthAlgTool(type,name,parent)
23{
24 declareInterface<Blob2ToolConstants>(this);
25}
26
27
29 msg(MSG::DEBUG) << "Initializing..." << endmsg;
30 return StatusCode::SUCCESS;
31}
32
33
35
36 coral::AttributeListSpecification* spec = new coral::AttributeListSpecification();
37
38 spec->extend("clsname","string"); //Data member of CaloRec::ToolConstants
39 spec->extend("version","unsigned int"); //Data member of CaloRec::ToolConstants
40 spec->extend("blobVersion","unsigned int"); //Should allow schema evolution if needed
41 spec->extend("Constants","blob"); //Holds the map<string,Arrayrep>
42
43
44 auto attrList = std::make_unique<coral::AttributeList>(*spec);
45
46 spec->release();
47 // cppcheck-suppress memleak
48 spec = nullptr;
49
50 (*attrList)["clsname"].data<std::string>()=tc->clsname();
51 (*attrList)["version"].data<unsigned int>()=(unsigned int)tc->version();
52 (*attrList)["blobVersion"].data<unsigned int>()=(unsigned int)0;
53
54 coral::Blob& blob=(*attrList)["Constants"].data<coral::Blob>();
55
56 //typedef std::map<std::string, CaloRec::Arrayrep> T;
57 TClass* klass = TClass::GetClass ("std::map<std::string, CaloRec::Arrayrep>");
58 if (klass==nullptr) {
59 msg( MSG::ERROR) << "Can't find TClass std::map<std::string, CaloRec::Arrayrep>" << endmsg;
60 return nullptr;
61 }
62 else
63 msg(MSG::DEBUG) << "Got TClass std::map<std::string, CaloRec::Arrayrep>" << endmsg;
64
65 TBufferFile buf (TBuffer::kWrite);
66
67 if (buf.WriteObjectAny (&tc->map(), klass) != 1) {
68 msg(MSG::ERROR) << "Failed to stream CaloRec::ToolConstants::Maptype " << endmsg;
69 return nullptr;
70 }
71
72 blob.resize(buf.Length());
73 void* adr = blob.startingAddress();
74 memcpy(adr,buf.Buffer(),buf.Length());
75 return attrList.release();
76 }
77
78StatusCode Blob2ToolConstants::AttrListToToolConstants(const coral::AttributeList& attrList, CaloRec::ToolConstants& tc) const {
79
81 std::unique_ptr<T> map;
82
83 try {
84 tc.clsname(attrList["clsname"].data<std::string>());
85 tc.version(attrList["version"].data<unsigned int>());
86 const unsigned blobVersion=attrList["blobVersion"].data<unsigned int>();
87 const coral::Blob& blob = attrList["Constants"].data<coral::Blob>();
88
89 if (blobVersion!=0) {
90 msg(MSG::ERROR) << "Can't interpret BLOB version " << blobVersion << endmsg;
91 return StatusCode::FAILURE;
92 }
93
94 TClass* klass = TClass::GetClass ("std::map<std::string, CaloRec::Arrayrep>");
95 if (klass==nullptr) {
96 msg(MSG::ERROR) << "Can't find TClass std::map<std::string, CaloRec::Arrayrep>" << endmsg;
97 return StatusCode::FAILURE;
98 }
99 else
100 msg(MSG::DEBUG) << "Got TClass std::map<std::string, CaloRec::Arrayrep>" << endmsg;
101
102 // TBufferFile needs a void* even in read-only mode:
103 void* blob_start ATLAS_THREAD_SAFE = const_cast<void*>(blob.startingAddress());
104 TBufferFile buf (TBuffer::kRead, blob.size(), blob_start, false);
105 map.reset( (T*)buf.ReadObjectAny (klass) );
106 }catch (coral::AttributeListException &e) {
107 msg(MSG::ERROR) << e.what() << endmsg;
108 return StatusCode::FAILURE;
109 }
110
111 T::const_iterator it=map->begin();
112 T::const_iterator it_e=map->end();
113 for (;it!=it_e;++it) {
114 tc.setrep(it->first,it->second);
115 }
116 return StatusCode::SUCCESS;
117}
118
119uint32_t Blob2ToolConstants::nameToChannelNumber(const std::string& name) {
120 const uint64_t hash64=CxxUtils::crc64(name);
121 const uint32_t hash32=(uint32_t)(hash64 & 0xFFFFFFFF);
122 //std::cout << "HASH: 64bit:" << std::hex << hash64 << " 32bit:" << hash32 << std::endl;
123 return hash32;
124 }
125
126
127static const InterfaceID IID_Blob2ToolConstants ("Blob2ToolConstants", 1 , 0);
128
129
132}
133
#define endmsg
static const InterfaceID IID_Blob2ToolConstants("Blob2ToolConstants", 1, 0)
An AlgTool to convert btw ToolConstants and coral::AttributeList.
char data[hepevt_bytes_allocation_ATLAS]
Definition HepEvt.cxx:11
static Double_t tc
Container for the tool constants managed by ToolWithConstants.
Define macros for attributes used to control the static checker.
#define ATLAS_THREAD_SAFE
AthAlgTool(const std::string &type, const std::string &name, const IInterface *parent)
Constructor with parameters:
MsgStream & msg() const
virtual StatusCode initialize()
StatusCode AttrListToToolConstants(const coral::AttributeList &attrList, CaloRec::ToolConstants &tc) const
Method convert AttributeList->ToolConstants (reading)
static const InterfaceID & interfaceID()
static unsigned nameToChannelNumber(const std::string &name)
Method to create a channel number (hash)
coral::AttributeList * ToolConstantsToAttrList(const CaloRec::ToolConstants *tc) const
Method convert ToolConstants -> AttributeList (writing)
Blob2ToolConstants(const std::string &type, const std::string &name, const IInterface *parent)
Container for the tool constants managed by ToolWithConstants.
std::map< std::string, CxxUtils::Arrayrep > Maptype
Type of the map from keys to values.
STL class.
A crc-64 implementation, using pclmul where possible.
uint64_t crc64(const CRCTable &table, const char *data, size_t data_len)
Find the CRC-64 of a string,.
Definition crc64.cxx:696