ATLAS Offline Software
Loading...
Searching...
No Matches
IOVDbCoolFunctions.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3*/
4//@file IOVDbCoolFunctions.cxx
5//@author Shaun Roe
6
8#include "IOVDbConn.h"
9#include "CoralBase/Blob.h"
10#include "CoralBase/Attribute.h"
11#include "CoralBase/AttributeList.h"
12#include "CoralBase/AttributeListSpecification.h"
13//
14#include "CoolKernel/ChannelSelection.h"
15#include "CoolKernel/IDatabase.h"
16#include "CoolKernel/IFolder.h"
18//
19#include <unordered_map>
20#include <typeinfo>
21#include <typeindex>
22#include <stdexcept>
23
24namespace{
25 const std::unordered_map<std::type_index, std::function<int(const coral::Attribute &)>>
26 sizeFunctions{
27 {std::type_index(typeid(bool)), [](const coral::Attribute & /*attr*/)->int { return 1; } },
28 {std::type_index(typeid(unsigned char)), [](const coral::Attribute & /*attr*/)->int { return 1; } },
29 {std::type_index(typeid(char)), [](const coral::Attribute & /*attr*/)->int { return 1; } },
30 //
31 {std::type_index(typeid(short)), [](const coral::Attribute & /*attr*/)->int { return 2; } },
32 {std::type_index(typeid(unsigned short)), [](const coral::Attribute & /*attr*/)->int { return 2; } },
33 {std::type_index(typeid(char)), [](const coral::Attribute & /*attr*/)->int { return 2; } },
34 //
35 {std::type_index(typeid(int)), [](const coral::Attribute & /*attr*/)->int { return 4; } },
36 {std::type_index(typeid(unsigned int)), [](const coral::Attribute & /*attr*/)->int { return 4; } },
37 {std::type_index(typeid(float)), [](const coral::Attribute & /*attr*/)->int { return 4; } },
38 //
39 {std::type_index(typeid(long long)), [](const coral::Attribute & /*attr*/)->int { return 8; } },
40 {std::type_index(typeid(unsigned long long)), [](const coral::Attribute & /*attr*/)->int { return 8; } },
41 {std::type_index(typeid(double)), [](const coral::Attribute & /*attr*/)->int { return 8; } },
42 //
43 {std::type_index(typeid(std::string)), [](const coral::Attribute & attr)->int { return attr.data<std::string>().size(); } },
44 {std::type_index(typeid(coral::Blob)), [](const coral::Attribute & attr)->int { return attr.data<coral::Blob>().size(); } }
45 };
46}
47
48
49namespace IOVDbNamespace{
50
51 const coral::AttributeListSpecification &
52 attrList2Spec(const coral::AttributeList& atrlist){
53 return atrlist.specification();
54 }
55
56 unsigned int
57 attributeSize(const coral::Attribute & attribute){
58 const auto & spec{attribute.specification()};
59 try{
60 return sizeFunctions.at(std::type_index(spec.type()))(attribute);
61 }catch (const std::out_of_range& oor) {
62 return 0;
63 }
64 }
65
66 bool
67 typeSizeIsKnown(const coral::Attribute & attribute){
68 return (sizeFunctions.find(std::type_index(attribute.specification().type())) != sizeFunctions.end());
69 }
70
71 unsigned int
72 attributeListSize(const coral::AttributeList& atrlist){
73 unsigned int total{};
74 for (const auto & attribute:atrlist){
75 total+=IOVDbNamespace::attributeSize(attribute);
76 }
77 return total;
78 }
79
80 int
81 countSelectedChannels(const std::vector<cool::ChannelId> & channels, const cool::ChannelSelection & selected){
82 auto isSelected = [& selected](cool::ChannelId id){return selected.inSelection(id);};
83 return std::count_if(channels.begin(), channels.end(),isSelected);//return type of count_if is signed
84 }
85
87 makeEpochOrRunLumi(const cool::ValidityKey key, const bool timeIsEpoch){
88 IOVTime time;
89 if(timeIsEpoch){
90 time.setTimestamp(key);
91 } else {
92 time.setRETime(key);
93 }
94 return time;
95 }
96
97 unsigned long long
98 iovTimeFromSeconds(const unsigned long long seconds){
99 return seconds*1000000000LL;
100 }
101
102 unsigned long long
103 iovTimeFromRunLumi(const unsigned long long run, const unsigned long long lumi){
104 return (run<<32) + lumi;
105 }
106
107 std::pair<unsigned long long, unsigned long long>
108 runLumiFromIovTime(const unsigned long long iovTime){
109 return std::pair<unsigned long long, unsigned long long>{iovTime>>32, iovTime&0xFFFFFFFF};
110 }
111
112 IOVRange
113 makeRange(const cool::ValidityKey since,const cool::ValidityKey until,const bool timeIsEpoch) {
114 // make an IOVRange object corresponding to given interval
115 // dealing with timestamp/RunLB differences
116 IOVTime itsince,ituntil;
117 if (timeIsEpoch) {
118 itsince.setTimestamp(since);
119 ituntil.setTimestamp(until);
120 } else {
121 itsince.setRETime(since);
122 ituntil.setRETime(until);
123 }
124 return IOVRange(itsince,ituntil);
125 }
126
127 cool::ChannelId
128 makeChannel(const std::string& strval, const cool::ChannelId defchan) {
129 // construct a cool channelId from the string value (numeric)
130 // if empty, use the default value
131 if (not strval.empty()) return std::stol(strval);
132 return defchan;
133 }
134
135 std::pair<bool, std::string>
136 folderMetadata(IOVDbConn * pConnection, const std::string & folderName){
137 bool multiversion{};
138 std::string folderdesc{};
139 // folder being read from COOL
140 // get COOL database - will wake up connection on first access
141 cool::IDatabasePtr dbPtr=pConnection->getCoolDb();
142 if (not dbPtr.get()) {
143 throw std::runtime_error( "Conditions database connection " + pConnection->name() + " cannot be opened - STOP" );
144 }
145 // get folder and read information
146 if (!dbPtr->existsFolder(folderName)) {
147 throw std::runtime_error( "Folder " + folderName + " does not exist" );
148 }
149 cool::IFolderPtr fldPtr=dbPtr->getFolder(folderName);
150 // get versiontype of folder
151 multiversion=(fldPtr->versioningMode()==cool::FolderVersioning::MULTI_VERSION);
152 // read and process description string
153 folderdesc=fldPtr->description();
154 return std::make_pair(multiversion, std::move(folderdesc));
155 }
156
157
158
159 std::pair<std::vector<cool::ChannelId>, std::vector<std::string>>
160 channelList(IOVDbConn * pConnection, const std::string & folderName, const bool named){
161 std::vector<cool::ChannelId> channelNumbers;
162 std::vector<std::string> channelNames;
163 //
164 cool::IDatabasePtr dbPtr=pConnection->getCoolDb();
165 // get folder and read information
166 if (!dbPtr->existsFolder(folderName)) {
167 throw std::runtime_error( "Folder " + folderName + " does not exist" );
168 }
169 cool::IFolderPtr fldPtr=dbPtr->getFolder(folderName);
170 if (named) {
171 typedef std::map<cool::ChannelId,std::string> ChanMap_t;
172 const ChanMap_t & chanmap=fldPtr->listChannelsWithNames();
173 channelNumbers.reserve(chanmap.size());
174 channelNames.reserve(chanmap.size());
175 auto unzipElement = [&](const ChanMap_t::value_type & element){
176 channelNumbers.push_back(element.first);
177 channelNames.push_back(element.second);
178 };
179 std::for_each(chanmap.begin(), chanmap.end(),unzipElement);
180 } else {
181 channelNumbers=fldPtr->listChannels();
182 }
183 return std::make_pair(std::move(channelNumbers), std::move(channelNames));
184 }
185
186}
187
Validity Range object.
const std::string & name() const
Definition IOVDbConn.h:70
cool::IDatabasePtr getCoolDb()
Definition IOVDbConn.cxx:31
Validity Range object.
Definition IOVRange.h:30
Basic time unit for IOVSvc.
Definition IOVTime.h:33
void setRETime(uint64_t time) noexcept
Definition IOVTime.cxx:84
void setTimestamp(uint64_t timestamp) noexcept
Definition IOVTime.cxx:72
int countSelectedChannels(const std::vector< cool::ChannelId > &channels, const cool::ChannelSelection &selected)
Count the number of selected channels in a vector of channels according to cool::Channel selection.
unsigned int attributeListSize(const coral::AttributeList &atrlist)
return the size (in bytes) of an AttributeList
unsigned int attributeSize(const coral::Attribute &attribute)
return the size (in bytes) of an Attribute
std::pair< unsigned long long, unsigned long long > runLumiFromIovTime(const unsigned long long iovTime)
Return a [run,lumi] pair from an IOV time.
bool typeSizeIsKnown(const coral::Attribute &attribute)
Return a bool indicating whether the size of a given Attribute can be determined.
std::pair< bool, std::string > folderMetadata(IOVDbConn *pConnection, const std::string &folderName)
Retrieve (bool) multiversion flag and folder description.
const coral::AttributeListSpecification & attrList2Spec(const coral::AttributeList &atrlist)
return the AttributeListSpecification of an AttributeList
IOVTime makeEpochOrRunLumi(const cool::ValidityKey key, const bool timeIsEpoch)
Create an IOVTime in ns of epoch or run-lumi (determined by the bool) from a ValidityKey.
std::pair< std::vector< cool::ChannelId >, std::vector< std::string > > channelList(IOVDbConn *pConnection, const std::string &folderName, const bool named)
Retrieve channel information.
unsigned long long iovTimeFromSeconds(const unsigned long long seconds)
Create a long long time in ns from s.
cool::ChannelId makeChannel(const std::string &strval, const cool::ChannelId defchan)
Create a ChannelId from a string; if string is empty, return the default channel number given.
unsigned long long iovTimeFromRunLumi(const unsigned long long run, const unsigned long long lumi)
Create a long long representing the IOV from run, lumi.
IOVRange makeRange(const cool::ValidityKey since, const cool::ValidityKey until, const bool timeIsEpoch)
Make an IOVRange from two validity keys.
Definition run.py:1