Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CDBInterface.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file CDBInterface.cc
1 #include "CDBInterface.h"
2 
4 
7 
10 #include <fun4all/SubsysReco.h> // for SubsysReco
11 
12 #include <phool/PHCompositeNode.h>
13 #include <phool/PHIODataNode.h> // for PHIODataNode
14 #include <phool/PHNode.h> // for PHNode
15 #include <phool/PHNodeIterator.h> // for PHNodeIterator
16 #include <phool/PHObject.h> // for PHObject
17 #include <phool/getClass.h>
18 #include <phool/phool.h>
19 #include <phool/recoConsts.h>
20 
21 #include <TSystem.h>
22 
23 #include <cstdint> // for uint64_t
24 #include <iostream> // for operator<<, basic_ostream, endl
25 #include <utility> // for pair
26 #include <vector> // for vector
27 
29 
31 {
32  if (__instance)
33  {
34  return __instance;
35  }
36  __instance = new CDBInterface();
37  return __instance;
38 }
39 
40 //____________________________________________________________________________..
42  : SubsysReco(name)
43 {
45  se->addNewSubsystem(this);
46 }
47 
48 //____________________________________________________________________________..
49 
51 {
52  delete cdbclient;
53 }
54 
55 //____________________________________________________________________________..
57 {
58  PHNodeIterator iter(topNode);
59  PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
60  CdbUrlSave *cdburls = findNode::getClass<CdbUrlSave>(runNode, "CdbUrl");
61  if (!cdburls)
62  {
63  cdburls = new CdbUrlSavev1();
64  PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(cdburls, "CdbUrl", "PHObject");
65  runNode->addNode(newNode);
66  }
67  else
68  {
69  std::set<std::tuple<std::string, std::string, uint64_t>> tmp_set;
70  for (const auto &cdburl : *cdburls)
71  {
72  tmp_set.insert(cdburl);
73  }
74  // remove duplicates in our set
75  // not possible using for range loops, iterator gets invalidated
76  for (auto itr = m_UrlVector.cbegin(); itr != m_UrlVector.cend();)
77  {
78  if (tmp_set.find(*itr) != tmp_set.end())
79  {
80  if (Verbosity())
81  {
82  std::cout << PHWHERE << " removing already saved: domain " << std::get<0>(*itr)
83  << ", url: " << std::get<1>(*itr)
84  << ", timestamp: " << std::get<2>(*itr) << std::endl;
85  }
86  itr = m_UrlVector.erase(itr);
87  }
88  else
89  {
90  ++itr;
91  }
92  }
93  }
94  for (auto &tuple : m_UrlVector)
95  {
96  cdburls->AddUrl(tuple);
97  }
98  if (Verbosity() > 1)
99  {
100  cdburls->identify();
101  }
103 }
104 
105 //____________________________________________________________________________..
106 void CDBInterface::Print(const std::string & /* what */) const
107 {
108  for (auto &iter : m_UrlVector)
109  {
110  std::cout << "domain: " << std::get<0>(iter)
111  << ", url: " << std::get<1>(iter)
112  << ", timestamp: " << std::get<2>(iter) << std::endl;
113  }
114 }
115 
117 {
119  if (!rc->FlagExist("CDB_GLOBALTAG"))
120  {
121  std::cout << PHWHERE << "CDB_GLOBALTAG flag needs to be set via" << std::endl;
122  std::cout << "rc->set_StringFlag(\"CDB_GLOBALTAG\",<global tag>)" << std::endl;
123  gSystem->Exit(1);
124  }
125  if (!rc->FlagExist("TIMESTAMP"))
126  {
127  std::cout << PHWHERE << "TIMESTAMP flag needs to be set via" << std::endl;
128  std::cout << "rc->set_uint64Flag(\"TIMESTAMP\",<64 bit timestamp>)" << std::endl;
129  gSystem->Exit(1);
130  }
131  if (cdbclient == nullptr)
132  {
133  cdbclient = new SphenixClient(rc->get_StringFlag("CDB_GLOBALTAG"));
134  }
135  uint64_t timestamp = rc->get_uint64Flag("TIMESTAMP");
136  if (Verbosity() > 0)
137  {
138  std::cout << "Global Tag: " << rc->get_StringFlag("CDB_GLOBALTAG")
139  << ", domain: " << domain
140  << ", timestamp: " << timestamp;
141  }
142  std::string return_url = cdbclient->getCalibration(domain, timestamp);
143  if (Verbosity() > 0)
144  {
145  if (return_url.empty())
146  {
147  std::cout << "... reply: no file found" << std::endl;
148  }
149  else
150  {
151  std::cout << "... reply: " << return_url << std::endl;
152  }
153  }
154  if (return_url.empty())
155  {
156  return_url = filename;
157  }
158  auto pret = m_UrlVector.insert(make_tuple(domain, return_url, timestamp));
159  if (!pret.second && Verbosity() > 1)
160  {
161  std::cout << PHWHERE << "not adding again " << domain << ", url: " << return_url
162  << ", time stamp: " << timestamp << std::endl;
163  }
164  return return_url;
165 }