Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-08-05 08:16:05

0001 #include "CDBInterface.h"
0002 
0003 #include <sphenixnpc/SphenixClient.h>
0004 
0005 #include <ffaobjects/CdbUrlSave.h>
0006 #include <ffaobjects/CdbUrlSavev1.h>
0007 
0008 #include <fun4all/Fun4AllReturnCodes.h>
0009 #include <fun4all/Fun4AllServer.h>
0010 #include <fun4all/SubsysReco.h>  // for SubsysReco
0011 
0012 #include <phool/PHCompositeNode.h>
0013 #include <phool/PHIODataNode.h>    // for PHIODataNode
0014 #include <phool/PHNode.h>          // for PHNode
0015 #include <phool/PHNodeIterator.h>  // for PHNodeIterator
0016 #include <phool/PHObject.h>        // for PHObject
0017 #include <phool/getClass.h>
0018 #include <phool/phool.h>
0019 #include <phool/recoConsts.h>
0020 
0021 #include <TSystem.h>
0022 
0023 #include <cstdint>   // for uint64_t
0024 #include <iostream>  // for operator<<, basic_ostream, endl
0025 #include <utility>   // for pair
0026 #include <vector>    // for vector
0027 
0028 CDBInterface *CDBInterface::__instance{nullptr};
0029 
0030 CDBInterface *CDBInterface::instance()
0031 {
0032   if (__instance)
0033   {
0034     return __instance;
0035   }
0036   __instance = new CDBInterface();
0037   return __instance;
0038 }
0039 
0040 //____________________________________________________________________________..
0041 CDBInterface::CDBInterface(const std::string &name)
0042   : SubsysReco(name)
0043 {
0044   Fun4AllServer *se = Fun4AllServer::instance();
0045   se->addNewSubsystem(this);
0046 }
0047 
0048 //____________________________________________________________________________..
0049 
0050 CDBInterface::~CDBInterface()
0051 {
0052   delete cdbclient;
0053 }
0054 
0055 //____________________________________________________________________________..
0056 int CDBInterface::End(PHCompositeNode *topNode)
0057 {
0058   PHNodeIterator iter(topNode);
0059   PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
0060   CdbUrlSave *cdburls = findNode::getClass<CdbUrlSave>(runNode, "CdbUrl");
0061   if (!cdburls)
0062   {
0063     cdburls = new CdbUrlSavev1();
0064     PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(cdburls, "CdbUrl", "PHObject");
0065     runNode->addNode(newNode);
0066   }
0067   else
0068   {
0069     std::set<std::tuple<std::string, std::string, uint64_t>> tmp_set;
0070     for (const auto &cdburl : *cdburls)
0071     {
0072       tmp_set.insert(cdburl);
0073     }
0074     // remove duplicates in our set
0075     // not possible using for range loops, iterator gets invalidated
0076     for (auto itr = m_UrlVector.cbegin(); itr != m_UrlVector.cend();)
0077     {
0078       if (tmp_set.find(*itr) != tmp_set.end())
0079       {
0080         if (Verbosity())
0081         {
0082           std::cout << PHWHERE << " removing already saved: domain " << std::get<0>(*itr)
0083                     << ", url: " << std::get<1>(*itr)
0084                     << ", timestamp: " << std::get<2>(*itr) << std::endl;
0085         }
0086         itr = m_UrlVector.erase(itr);
0087       }
0088       else
0089       {
0090         ++itr;
0091       }
0092     }
0093   }
0094   for (const auto &tuple : m_UrlVector)
0095   {
0096     cdburls->AddUrl(tuple);
0097   }
0098   if (Verbosity() > 1)
0099   {
0100     cdburls->identify();
0101   }
0102   return Fun4AllReturnCodes::EVENT_OK;
0103 }
0104 
0105 //____________________________________________________________________________..
0106 void CDBInterface::Print(const std::string & /* what */) const
0107 {
0108   for (const auto &iter : m_UrlVector)
0109   {
0110     std::cout << "domain: " << std::get<0>(iter)
0111               << ", url: " << std::get<1>(iter)
0112               << ", timestamp: " << std::get<2>(iter) << std::endl;
0113   }
0114 }
0115 
0116 std::string CDBInterface::getUrl(const std::string &domain, const std::string &filename)
0117 {
0118   if (disable)
0119   {
0120     return "";
0121   }
0122   std::string domain_noconst = domain;
0123   recoConsts *rc = recoConsts::instance();
0124   if (!rc->FlagExist("CDB_GLOBALTAG"))
0125   {
0126     std::cout << PHWHERE << "CDB_GLOBALTAG flag needs to be set via" << std::endl;
0127     std::cout << "rc->set_StringFlag(\"CDB_GLOBALTAG\",<global tag>)" << std::endl;
0128     gSystem->Exit(1);
0129   }
0130   if (!rc->FlagExist("TIMESTAMP"))
0131   {
0132     std::cout << PHWHERE << "TIMESTAMP flag needs to be set via" << std::endl;
0133     std::cout << "rc->set_uint64Flag(\"TIMESTAMP\",<64 bit timestamp>)" << std::endl;
0134     gSystem->Exit(1);
0135   }
0136   if (cdbclient == nullptr)
0137   {
0138     cdbclient = new SphenixClient(rc->get_StringFlag("CDB_GLOBALTAG"));
0139   }
0140   uint64_t timestamp = rc->get_uint64Flag("TIMESTAMP");
0141   if (Verbosity() > 0)
0142   {
0143     std::cout << "Global Tag: " << rc->get_StringFlag("CDB_GLOBALTAG")
0144               << ", domain: " << domain_noconst
0145               << ", timestamp: " << timestamp;
0146   }
0147   std::string return_url = cdbclient->getCalibration(domain_noconst, timestamp);
0148   if (return_url.empty())
0149   {
0150     if (!disable_default)
0151     {
0152       std::string domain_copy = domain_noconst;
0153       domain_noconst = domain_noconst + "_default";
0154       return_url = cdbclient->getCalibration(domain_noconst, timestamp);
0155       if (return_url.empty())
0156       {
0157         if (Verbosity() > 0)
0158         {
0159           std::cout << "... reply: no file found for "
0160                     << domain_copy << " or " << domain_noconst << std::endl;
0161         }
0162         return_url = filename;
0163       }
0164     }
0165     else
0166     {
0167       if (Verbosity() > 0)
0168       {
0169         std::cout << "... reply: no file found for "
0170                   << domain_noconst << std::endl;
0171       }
0172     }
0173   }
0174   if (Verbosity() > 0)
0175   {
0176     if (!return_url.empty())
0177     {
0178       std::cout << "... reply: " << return_url << std::endl;
0179     }
0180   }
0181   auto pret = m_UrlVector.insert(make_tuple(domain_noconst, return_url, timestamp));
0182   if (!pret.second && Verbosity() > 1)
0183   {
0184     std::cout << PHWHERE << "not adding again " << domain_noconst << ", url: " << return_url
0185               << ", time stamp: " << timestamp << std::endl;
0186   }
0187   return return_url;
0188 }