ATLAS Offline Software
Loading...
Searching...
No Matches
FileCatalogUtils.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7#include <algorithm>
8#include <exception>
9
10void FileCatalogUtils::getFirstPFN( const Gaudi::IFileCatalog& fc,
11 const std::string& fid,
12 std::string& pfn,
13 std::string& tech ) {
14 Gaudi::IFileCatalog::Files pfns_techs;
15 fc.getPFN( fid, pfns_techs );
16 if( pfns_techs.empty() ) {
17 pfn.clear();
18 tech.clear();
19 } else {
20 pfn = pfns_techs[0].first;
21 tech = pfns_techs[0].second;
22 }
23}
24
25void FileCatalogUtils::lookupFileByPFN( const Gaudi::IFileCatalog& fc,
26 const std::string& pfn,
27 std::string& fid,
28 std::string& tech ) {
29 fid = fc.lookupPFN( pfn );
30 tech.clear();
31 if( !fid.empty() ) {
32 Gaudi::IFileCatalog::Files pfns_techs;
33 fc.getPFN( fid, pfns_techs );
34 for( const auto& attr_pair: pfns_techs ) {
35 if( attr_pair.first == pfn )
36 tech = attr_pair.second;
37 }
38 }
39}
40
41void FileCatalogUtils::registerPFN( Gaudi::IFileCatalog& fc,
42 const std::string& pfn,
43 const std::string& ftype,
44 std::string& fid ) {
45 if( fc.existsPFN(pfn) ) {
46 throw std::runtime_error("PFN '" + pfn + "' already registered (APR: \" registerPFN \" from \" FileCatalog \")");
47 }
48 if( fid.empty() ) fid = fc.createFID();
49 fc.registerPFN(fid, pfn, ftype);
50}
51
52std::string FileCatalogUtils::addCatalog( Gaudi::IFileCatalogMgr& mgr,
53 const std::string& connect,
54 bool forWriting ) {
55 std::string url;
56 if( connect.empty() ) {
57 url = "file:PoolFileCatalog.xml";
58 } else {
59 auto pos = connect.find("file:");
60 url = (pos==std::string::npos)? "file:" + connect : connect.substr(pos);
61 }
62 Gaudi::IFileCatalogMgr::Catalogs& cats = mgr.catalogs();
63 auto i = std::find_if( cats.begin(), cats.end(),
64 [&]( const Gaudi::IFileCatalog* f )
65 { return url == f->connectInfo(); } );
66 std::string addedConnectStr;
67 if( i==cats.end() ) {
68 // add a new catalog
69 addedConnectStr = "xmlcatalog_" + url;
70 mgr.addCatalog( addedConnectStr );
71 }
72 if( forWriting ) {
73 mgr.setWriteCatalog( mgr.findCatalog( url, true ) );
74 }
75 return addedConnectStr;
76}
void registerPFN(Gaudi::IFileCatalog &fc, const std::string &pfn, const std::string &ftype, std::string &fid)
Register PFN, assign new FID if not given. Throws if the PFN is already registered.
std::string addCatalog(Gaudi::IFileCatalogMgr &mgr, const std::string &connect, bool forWriting=false)
Add new catalog, identified by name, to the existing ones, optionally as the write catalog.
void lookupFileByPFN(const Gaudi::IFileCatalog &fc, const std::string &pfn, std::string &fid, std::string &tech)
Get FID and filetype for a given PFN.
void getFirstPFN(const Gaudi::IFileCatalog &fc, const std::string &fid, std::string &pfn, std::string &tech)
Get the first PFN + filetype for the given FID.