ATLAS Offline Software
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 
7 #include "IOVDbCoolFunctions.h"
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"
17 #include "AthenaKernel/IOVRange.h"
18 //
19 #include <unordered_map>
20 #include <typeinfo>
21 #include <typeindex>
22 #include <stdexcept>
23 
24 namespace{
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 
49 namespace IOVDbNamespace{
50 
51  const coral::AttributeListSpecification &
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
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 
86  IOVTime
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 
IOVRange
Validity Range object. Holds two IOVTimes (start and stop)
Definition: IOVRange.h:30
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
IOVRange.h
Validity Range object. Holds two IOVTime instances (start and stop)
CaloCondBlobAlgs_fillNoiseFromASCII.spec
spec
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:47
IOVDbConn.h
IOVDbConn::name
const std::string & name() const
Definition: IOVDbConn.h:70
python.subdetectors.tile.Blob
Blob
Definition: tile.py:17
IOVDbNamespace::iovTimeFromSeconds
unsigned long long iovTimeFromSeconds(const unsigned long long seconds)
Create a long long time in ns from s.
Definition: IOVDbCoolFunctions.cxx:98
python.PyKernel.AttributeList
AttributeList
Definition: PyKernel.py:36
IOVDbNamespace::makeEpochOrRunLumi
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.
Definition: IOVDbCoolFunctions.cxx:87
dq_defect_copy_defect_database.channels
def channels
Definition: dq_defect_copy_defect_database.py:56
dq_defect_copy_defect_database.since
def since
Definition: dq_defect_copy_defect_database.py:54
IOVDbConn::getCoolDb
cool::IDatabasePtr getCoolDb()
Definition: IOVDbConn.cxx:31
IOVDbNamespace::attrList2Spec
const coral::AttributeListSpecification & attrList2Spec(const coral::AttributeList &atrlist)
return the AttributeListSpecification of an AttributeList
Definition: IOVDbCoolFunctions.cxx:52
dq_defect_copy_defect_database.until
def until
Definition: dq_defect_copy_defect_database.py:55
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
IOVTime
Basic time unit for IOVSvc. Hold time as a combination of run and event numbers.
Definition: IOVTime.h:33
IOVTime::setRETime
void setRETime(uint64_t time) noexcept
Definition: IOVTime.cxx:84
run
Definition: run.py:1
IOVDbNamespace::channelList
std::pair< std::vector< cool::ChannelId >, std::vector< std::string > > channelList(IOVDbConn *pConnection, const std::string &folderName, const bool named)
Retrieve channel information.
Definition: IOVDbCoolFunctions.cxx:160
python.LArCalib_HVCorrConfig.seconds
seconds
Definition: LArCalib_HVCorrConfig.py:86
IOVTime::setTimestamp
void setTimestamp(uint64_t timestamp) noexcept
Definition: IOVTime.cxx:72
IOVDbNamespace::makeRange
IOVRange makeRange(const cool::ValidityKey since, const cool::ValidityKey until, const bool timeIsEpoch)
Make an IOVRange from two validity keys.
Definition: IOVDbCoolFunctions.cxx:113
IOVDbConn
Definition: IOVDbConn.h:18
CaloCellTimeCorrFiller.folderName
string folderName
Definition: CaloCellTimeCorrFiller.py:20
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:227
IOVDbCoolFunctions.h
IOVDbNamespace::typeSizeIsKnown
bool typeSizeIsKnown(const coral::Attribute &attribute)
Return a bool indicating whether the size of a given Attribute can be determined.
Definition: IOVDbCoolFunctions.cxx:67
IOVDbNamespace::runLumiFromIovTime
std::pair< unsigned long long, unsigned long long > runLumiFromIovTime(const unsigned long long iovTime)
Return a [run,lumi] pair from an IOV time.
Definition: IOVDbCoolFunctions.cxx:108
lumiFormat.lumi
lumi
Definition: lumiFormat.py:106
IOVDbNamespace::attributeSize
unsigned int attributeSize(const coral::Attribute &attribute)
return the size (in bytes) of an Attribute
Definition: IOVDbCoolFunctions.cxx:57
IOVDbNamespace::attributeListSize
unsigned int attributeListSize(const coral::AttributeList &atrlist)
return the size (in bytes) of an AttributeList
Definition: IOVDbCoolFunctions.cxx:72
IOVDbNamespace::folderMetadata
std::pair< bool, std::string > folderMetadata(IOVDbConn *pConnection, const std::string &folderName)
Retrieve (bool) multiversion flag and folder description
Definition: IOVDbCoolFunctions.cxx:136
WriteCalibToCool.strval
strval
Definition: WriteCalibToCool.py:583
IOVDbNamespace::iovTimeFromRunLumi
unsigned long long iovTimeFromRunLumi(const unsigned long long run, const unsigned long long lumi)
Create a long long representing the IOV from run, lumi.
Definition: IOVDbCoolFunctions.cxx:103
IOVDbNamespace::countSelectedChannels
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.
Definition: IOVDbCoolFunctions.cxx:81
IOVDbNamespace::makeChannel
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.
Definition: IOVDbCoolFunctions.cxx:128
IOVDbNamespace
Definition: Base64Codec.cxx:16
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37