ATLAS Offline Software
AthenaBarCodeImpl.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <sstream>
6 
7 #include "GaudiKernel/ServiceHandle.h"
8 
10 
12 
13 #include "GaudiKernel/IMessageSvc.h"
14 #include "GaudiKernel/MsgStream.h"
16 
17 #include <iostream>
18 #include <cstdlib>
19 
20 #include "uuid/uuid.h"
21 
22 std::atomic<AthenaBarCode_t> AthenaBarCodeImpl::m_barcodeCounter = 0;
23 
25 
26  //This function will be call the first time any "access" function is called
27  //using const type because those "access" functions might be const
28  //Will only change mutable variables
29 
30  // std::cout<<"Calling AthenaBarCodeImpl::AthenaBarCodeImpl() "<<std::endl;
31 
32  //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
33  //Set counter part
34  //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
35 
37 
39 
40  try {
42  barcode);
43  }
44  catch (const GaudiException& Exception) {
45  throw std::runtime_error(
46  "AthenaBarCodeImpl::Can not Set Counter Bit, Counter Overflow");
47  }
48 
49  //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
50  //Set reserve part
51  //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
52  try {
54  }
55  catch (const GaudiException& Exception) {
56  throw std::runtime_error(
57  "AthenaBarCodeImpl::Can not initialize Reserve Bit");
58  }
59 
60  //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
61  //Set Version part
62  //* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
63  try {
65  }
66  catch (const GaudiException& Exception) {
67  throw std::runtime_error(
68  "AthenaBarCodeImpl::Can not initialize Version Bit");
69  }
70 
71 
72  try {
74  }
75  catch (const GaudiException& Exception) {
76  throw std::runtime_error("AthenaBarCodeImpl::Can not set UUID Hash Bits");
77  }
78 
80 
81  /* std::cout << "[AthenaBarCodeImpl::m_barcode,m_defaultHash,m_barcodeCounter]= "
82  << std::hex
83  << (m_barcode)<< "\t"
84  << m_defaultHash<< "\t"
85  << (m_barcodeCounter)<<"\t"
86  << std::dec
87  << std::endl;
88  */
89 }
90 
92  : m_barcode (IAthenaBarCode::UNDEFINEDBARCODE)
93 {
94 }
95 
97  : m_barcode (static_cast<AthenaBarCode_t>(other.m_barcode))
98 {
99 }
100 
102 {
103  if (this != &other) {
104  m_barcode = static_cast<AthenaBarCode_t>(other.m_barcode);
105  }
106  return *this;
107 }
108 
109 bool
112  initABC();
113 
114  if (obj.getAthenaBarCode() == m_barcode)
115  return true;
116  return false;
117 }
118 
119 bool
122  initABC();
123 
124  if ((obj.getAthenaBarCode()) >> (ReserveBits + VersionBits) == m_barcode
125  >> (ReserveBits + VersionBits))
126  return true;
127  return false;
128 }
129 
133  initABC();
135 }
136 
137 void
139 
141  initABC();
142 
143  AthenaBarCodeVersion_t currversion = getVersion();
144 
145  try {
147  setBits(SVersionBits, VersionBits, currversion + 1, bc);
148  m_barcode = bc;
149  }
150  catch (const GaudiException& Exception) {
151  throw std::runtime_error(
152  "AthenaBarCodeImpl::newVersion()::Version Overflow");
153  }
154 }
155 
156 void
159  initABC();
160 
161  try {
164  m_barcode = bc;
165  }
166  catch (const GaudiException& Exception) {
167  throw std::runtime_error("AthenaBarCodeImpl::newVersion()::Version Too big");
168  }
169 }
170 
171 std::ostream&
172 AthenaBarCodeImpl::dump(std::ostream& out) const {
174  initABC();
175 
176  out << "\n[AthenaBarCode]= " << std::hex << (m_barcode) << std::dec << std::endl;
177  return out;
178 }
179 
180 bool
183  initABC();
184 
185  //FIXME: it is possible that m_defaultHash is not yet initialized.
186  return getUUIDHash() == getDefaultHash();
187 }
188 
191  const int maxdigs = 16;
192  AthenaBarCode_t tmp1 = static_cast<AthenaBarCode_t> (2166136261UL);
193 
194  for (int i = 0; i < maxdigs; i++) {
195  tmp1 ^= (AthenaBarCode_t)(guid[i]);
196  tmp1 *= 16777619UL;
197  }
198 
199  return (tmp1 << (TotalBits - UUIDBits)) >> (TotalBits - UUIDBits);
200 }
201 
205  initABC();
206 
207  return m_barcode;
208 }
209 
210 void
212  m_barcode = id;
213 }
214 
218  initABC();
220 }
221 
222 void
225  initABC();
226 
227  try {
229  setBits(SReserveBits, ReserveBits, id, bc);
230  m_barcode = bc;
231  }
232  catch (const GaudiException& Exception) {
233  throw std::runtime_error(
234  "AthenaBarCodeImpl::newVersion()::Version Overflow");
235  }
236 }
237 
240  return getUUIDHash();
241 }
242 
245  return getBits(SUUIDBits, UUIDBits);
246 }
247 
249 AthenaBarCodeImpl::getBits(unsigned short startbit, unsigned short nbits) const {
250 
252  //now m_barcode=aaaaaxxaa;
253  tmp = m_barcode << (startbit); //tmp=xxaa00000;
254  tmp = tmp >> (TotalBits - nbits); //tmp=0000000xx
255 
256  return tmp;
257 }
258 
259 void
260 AthenaBarCodeImpl::setBits(unsigned short startbit, unsigned short nbits,
261  AthenaBarCode_t id,
262  AthenaBarCode_t& bc) const
263 {
266  tmp2 = 0;
267 
268  if ((id) >= ((~tmp2) >> (TotalBits - nbits))) {
269 
270  IMessageSvc *msgsvc = Athena::getMessageSvc();
271  if (msgsvc) {
272  MsgStream msg(msgsvc, "AthenaBarCodeImpl");
273  msg << MSG::WARNING << "setBits::bit to be set:" << id
274  << " is larger than limit:" << ((~tmp2) >> (TotalBits - nbits))
275  << endmsg;
276  }
277  else {
278  std::cout << "setBits::bit to be set:" << id << " is larger than limit:"
279  << ((~tmp2) >> (TotalBits - nbits)) << std::endl;
280  }
281  throw std::runtime_error("AthenaBarCodeImpl::setBit Failed");
282  }
283 
285  tmp = (id << (TotalBits - nbits - startbit)); //tmp=00000xx00
286  tmp2 = ((~tmp2) >> (TotalBits - nbits)); //tmp2=000000011
287 
288  tmp2 = (tmp2 << (TotalBits - nbits - startbit)); //tmp2=000001100
289  tmp2 = (~tmp2); //tmp2=111110011
290 
291  //now m_barcode=aaaaayyaa
292  bc &= tmp2; //m_barcode=aaaaa00aa
293  bc |= tmp; //m_barcode=aaaaaxxaa
294 
295 }
296 
297 void
299  getDefaultHash (jobid);
300 }
301 
302 
304 AthenaBarCodeImpl::getDefaultHash (const char* jobid /*= nullptr*/)
305 {
306  static const AthenaBarCode_t defaultHash = makeDefaultHash (jobid);
307  return defaultHash;
308 }
309 
310 
313 {
314  if (jobid) {
315  return AthenaBarCodeImpl::hashUUID(jobid);
316  }
317 
318  // First look for a uuid as an environment variable.
319  // This can be set in cases where we don't have the full
320  // Gaudi/Athena environment available (eg, ARA), and we
321  // don't want to try to create JobIDSvc. This has to be
322  // communicated in some way external to this library,
323  // as we may end up here while initializing the dictionary
324  // for this library.
325  const char* env_uuid = getenv ("_ATHENABARCODEIMPL_JOBUUID");
326  if (env_uuid) {
327  return AthenaBarCodeImpl::hashUUID(env_uuid);
328  }
329 
330  // std::cout<<"no UUID stored, generating."<<std::endl;
331 
332  ServiceHandle<IJobIDSvc> p_jobidsvc("JobIDSvc", "JobIDSvc");
333  StatusCode sc = p_jobidsvc.retrieve();
334  if (!sc.isSuccess() || 0 == p_jobidsvc) {
335  //FIXME
336  //use uuid instead if service not available
337  JobID_t JobID;
338  uuid_generate(JobID);
339  return AthenaBarCodeImpl::hashUUID((const char *) JobID);
340  /* std::cout << "Could not find JobIDSvc, using uuid directly"
341  <<"jobid "<<o.str()
342  << std::endl;*/
343  }
344 
345  PJobID_t pjobid = p_jobidsvc->getJobID();
346  return AthenaBarCodeImpl::hashUUID((const char *) pjobid);
347 }
AthenaBarCodeImpl::setReserveBits
void setReserveBits(AthenaBarCode_t id)
Definition: AthenaBarCodeImpl.cxx:223
AthenaBarCodeImpl.h
AthenaBarCodeImpl::m_barcodeCounter
static std::atomic< AthenaBarCode_t > m_barcodeCounter
Definition: AthenaBarCodeImpl.h:109
AthenaBarCodeImpl::AthenaBarCodeImpl
AthenaBarCodeImpl()
Definition: AthenaBarCodeImpl.cxx:91
AthenaBarCodeImpl::m_barcode
std::atomic< AthenaBarCode_t > m_barcode
Definition: AthenaBarCodeImpl.h:110
IAthenaBarCode::UNDEFINEDBARCODE
static const AthenaBarCode_t UNDEFINEDBARCODE
Definition: AthenaKernel/AthenaKernel/IAthenaBarCode.h:52
PJobID_t
const unsigned char * PJobID_t
Definition: IJobIDSvc.h:21
DeMoUpdate.tmp2
string tmp2
Definition: DeMoUpdate.py:1168
AthenaBarCodeImpl::SUUIDBits
static const unsigned short SUUIDBits
Definition: AthenaBarCodeImpl.h:48
AthenaBarCodeImpl::hashUUID
static AthenaBarCode_t hashUUID(const char *)
Definition: AthenaBarCodeImpl.cxx:190
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
AthenaBarCodeImpl::UUIDBits
static const unsigned short UUIDBits
Definition: AthenaBarCodeImpl.h:43
AthenaBarCodeImpl
Definition: AthenaBarCodeImpl.h:37
AthenaBarCodeImpl::CounterBits
static const unsigned short CounterBits
Definition: AthenaBarCodeImpl.h:44
AthenaBarCodeImpl::SVersionBits
static const unsigned short SVersionBits
Definition: AthenaBarCodeImpl.h:50
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
JobID_t
uuid_t JobID_t
Definition: IJobIDSvc.h:20
AthenaBarCodeImpl::setBits
void setBits(unsigned short startbit, unsigned short nbits, AthenaBarCode_t id, AthenaBarCode_t &bc) const
Definition: AthenaBarCodeImpl.cxx:260
AthenaBarCodeImpl::getUUIDHash
AthenaBarCode_t getUUIDHash() const
Definition: AthenaBarCodeImpl.cxx:244
AthenaBarCodeImpl::setAthenaBarCode
void setAthenaBarCode(AthenaBarCode_t id)
Definition: AthenaBarCodeImpl.cxx:211
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
AthenaBarCodeImpl::hasUUIDHash
AthenaBarCode_t hasUUIDHash() const
Definition: AthenaBarCodeImpl.cxx:239
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Pythia8_A14_NNPDF23LO_Var1Down_Common.ver
ver
Definition: Pythia8_A14_NNPDF23LO_Var1Down_Common.py:26
AthenaBarCode_t
uint64_t AthenaBarCode_t
barcode for all INav4Mom classes
Definition: AthenaKernel/AthenaKernel/IAthenaBarCode.h:44
AthenaBarCodeImpl::TotalBits
static const unsigned short TotalBits
Definition: AthenaBarCodeImpl.h:41
AthenaBarCodeImpl::newVersion
void newVersion()
Definition: AthenaBarCodeImpl.cxx:138
lumiFormat.i
int i
Definition: lumiFormat.py:85
AthenaBarCodeImpl::getDefaultHash
static AthenaBarCode_t getDefaultHash(const char *jobid=nullptr)
Definition: AthenaBarCodeImpl.cxx:304
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
AthenaBarCodeImpl::getReserveBits
AthenaBarCode_t getReserveBits() const
Definition: AthenaBarCodeImpl.cxx:216
AthenaBarCodeImpl::hasSameAthenaBarCodeExceptVersion
bool hasSameAthenaBarCodeExceptVersion(const IAthenaBarCode &obj) const
Definition: AthenaBarCodeImpl.cxx:120
AthenaBarCodeImpl::SReserveBits
static const unsigned short SReserveBits
Definition: AthenaBarCodeImpl.h:51
AthenaBarCodeImpl::getBits
AthenaBarCode_t getBits(unsigned short startbit, unsigned short nbits) const
Definition: AthenaBarCodeImpl.cxx:249
AthenaBarCodeImpl::initABC
void initABC() const
Definition: AthenaBarCodeImpl.cxx:24
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
LArConditionsTestConfig.msgsvc
msgsvc
Definition: LArConditionsTestConfig.py:74
IAthenaBarCode
Definition: AthenaKernel/AthenaKernel/IAthenaBarCode.h:48
AthenaBarCodeImpl::SCounterBits
static const unsigned short SCounterBits
Definition: AthenaBarCodeImpl.h:49
pool_uuid.guid
guid
Definition: pool_uuid.py:112
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:220
AthenaBarCodeImpl::getAthenaBarCode
AthenaBarCode_t getAthenaBarCode() const
Definition: AthenaBarCodeImpl.cxx:203
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
AthenaBarCodeImpl::hasSameAthenaBarCode
bool hasSameAthenaBarCode(const IAthenaBarCode &obj) const
Definition: AthenaBarCodeImpl.cxx:110
AthenaBarCodeImpl::makeDefaultHash
static AthenaBarCode_t makeDefaultHash(const char *jobid)
Definition: AthenaBarCodeImpl.cxx:312
IJobIDSvc.h
AthenaBarCodeImpl::setDefaultHash
static void setDefaultHash(const char *jobid)
Definition: AthenaBarCodeImpl.cxx:298
AthenaBarCodeVersion_t
AthenaBarCode_t AthenaBarCodeVersion_t
Definition: AthenaKernel/AthenaKernel/IAthenaBarCode.h:46
AthenaBarCodeImpl::createdInCurrentJob
bool createdInCurrentJob() const
Definition: AthenaBarCodeImpl.cxx:181
AthenaBarCodeImpl::dump
std::ostream & dump(std::ostream &out) const
Definition: AthenaBarCodeImpl.cxx:172
test_pyathena.counter
counter
Definition: test_pyathena.py:15
python.PyAthena.obj
obj
Definition: PyAthena.py:132
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
AthenaBarCodeImpl::VersionBits
static const unsigned short VersionBits
Definition: AthenaBarCodeImpl.h:45
AthenaBarCodeImpl::setVersion
void setVersion(AthenaBarCodeVersion_t newversion)
Definition: AthenaBarCodeImpl.cxx:157
AthenaBarCodeImpl::operator=
AthenaBarCodeImpl & operator=(const AthenaBarCodeImpl &)
Definition: AthenaBarCodeImpl.cxx:101
ServiceHandle
Definition: ClusterMakerTool.h:37
AthenaBarCodeImpl::getVersion
AthenaBarCodeVersion_t getVersion() const
Definition: AthenaBarCodeImpl.cxx:131
AthenaBarCodeImpl::ReserveBits
static const unsigned short ReserveBits
Definition: AthenaBarCodeImpl.h:46