Back to home page

sPhenix code displayed by LXR

 
 

    


File indexing completed on 2025-12-17 09:19:36

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   int iret = UpdateRunNode(topNode);PHNodeIterator iter(topNode);
0059   return iret;
0060 }
0061 
0062 //____________________________________________________________________________..
0063 int CDBInterface::UpdateRunNode(PHCompositeNode *topNode)
0064 {
0065   PHNodeIterator iter(topNode);
0066   PHCompositeNode *runNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "RUN"));
0067   CdbUrlSave *cdburls = findNode::getClass<CdbUrlSave>(runNode, "CdbUrl");
0068   if (!cdburls)
0069   {
0070     cdburls = new CdbUrlSavev1();
0071     PHIODataNode<PHObject> *newNode = new PHIODataNode<PHObject>(cdburls, "CdbUrl", "PHObject");
0072     runNode->addNode(newNode);
0073   }
0074   else
0075   {
0076     std::set<std::tuple<std::string, std::string, uint64_t>> tmp_set;
0077     for (const auto &cdburl : *cdburls)
0078     {
0079       tmp_set.insert(cdburl);
0080     }
0081     // remove duplicates in our set
0082     // not possible using for range loops, iterator gets invalidated
0083     for (auto itr = m_UrlVector.cbegin(); itr != m_UrlVector.cend();)
0084     {
0085       if (tmp_set.contains(*itr))
0086       {
0087         if (Verbosity() > 2)
0088         {
0089           std::cout << PHWHERE << " cleaning duplicately saved: domain " << std::get<0>(*itr)
0090                     << ", url: " << std::get<1>(*itr)
0091                     << ", timestamp: " << std::get<2>(*itr) << std::endl;
0092         }
0093         itr = m_UrlVector.erase(itr);
0094       }
0095       else
0096       {
0097         ++itr;
0098       }
0099     }
0100   }
0101   for (const auto &tuple : m_UrlVector)
0102   {
0103     cdburls->AddUrl(tuple);
0104   }
0105   if (Verbosity() > 1)
0106   {
0107     cdburls->identify();
0108   }
0109   return Fun4AllReturnCodes::EVENT_OK;
0110 }
0111 
0112 //____________________________________________________________________________..
0113 void CDBInterface::Print(const std::string & /* what */) const
0114 {
0115   for (const auto &iter : m_UrlVector)
0116   {
0117     std::cout << "domain: " << std::get<0>(iter)
0118               << ", url: " << std::get<1>(iter)
0119               << ", timestamp: " << std::get<2>(iter) << std::endl;
0120   }
0121 }
0122 
0123 std::string CDBInterface::getUrl(const std::string &domain, const std::string &filename)
0124 {
0125   if (disable)
0126   {
0127     return "";
0128   }
0129   std::string domain_noconst = domain;
0130   recoConsts *rc = recoConsts::instance();
0131   if (!rc->FlagExist("CDB_GLOBALTAG"))
0132   {
0133     std::cout << PHWHERE << "CDB_GLOBALTAG flag needs to be set via" << std::endl;
0134     std::cout << "rc->set_StringFlag(\"CDB_GLOBALTAG\",<global tag>)" << std::endl;
0135     gSystem->Exit(1);
0136   }
0137   if (!rc->FlagExist("TIMESTAMP"))
0138   {
0139     std::cout << PHWHERE << "TIMESTAMP flag needs to be set via" << std::endl;
0140     std::cout << "rc->set_uint64Flag(\"TIMESTAMP\",<64 bit timestamp>)" << std::endl;
0141     gSystem->Exit(1);
0142   }
0143   if (cdbclient == nullptr)
0144   {
0145     cdbclient = new SphenixClient(rc->get_StringFlag("CDB_GLOBALTAG"));
0146   }
0147   uint64_t timestamp = rc->get_uint64Flag("TIMESTAMP");
0148   if (Verbosity() > 0)
0149   {
0150     std::cout << "Global Tag: " << rc->get_StringFlag("CDB_GLOBALTAG")
0151               << ", domain: " << domain_noconst
0152               << ", timestamp: " << timestamp;
0153   }
0154   std::string return_url = cdbclient->getCalibration(domain_noconst, timestamp);
0155   if (return_url.empty())
0156   {
0157     if (!disable_default)
0158     {
0159       std::string domain_copy = domain_noconst;
0160       domain_noconst = domain_noconst + "_default";
0161       return_url = cdbclient->getCalibration(domain_noconst, timestamp);
0162       if (return_url.empty())
0163       {
0164         if (Verbosity() > 0)
0165         {
0166           std::cout << "... reply: no file found for "
0167                     << domain_copy << " or " << domain_noconst << std::endl;
0168         }
0169         return_url = filename;
0170       }
0171     }
0172     else
0173     {
0174       if (Verbosity() > 0)
0175       {
0176         std::cout << "... reply: no file found for "
0177                   << domain_noconst << std::endl;
0178       }
0179     }
0180   }
0181   if (Verbosity() > 0)
0182   {
0183     if (!return_url.empty())
0184     {
0185       std::cout << "... reply: " << return_url << std::endl;
0186     }
0187   }
0188   auto pret = m_UrlVector.insert(make_tuple(domain_noconst, return_url, timestamp));
0189   if (!pret.second && Verbosity() > 1)
0190   {
0191     std::cout << PHWHERE << "not adding again " << domain_noconst << ", url: " << return_url
0192               << ", time stamp: " << timestamp << std::endl;
0193   }
0194   return return_url;
0195 }