CMS 3D CMS Logo

InputFileCatalog.cc
Go to the documentation of this file.
1 //
4 
6 
9 
13 
14 #include <boost/algorithm/string.hpp>
15 
16 #include <iostream>
17 
18 namespace edm {
19 
20  InputFileCatalog::InputFileCatalog(std::vector<std::string> const& fileNames,
21  std::string const& override,
22  bool useLFNasPFNifLFNnotFound,
23  edm::CatalogType catType)
24  : logicalFileNames_(fileNames), fileNames_(fileNames), fileCatalogItems_(), overrideFileLocator_() {
25  init(override, useLFNasPFNifLFNnotFound, catType);
26  }
27 
29 
30  std::vector<std::string> InputFileCatalog::fileNames(unsigned iCatalog) const {
31  std::vector<std::string> tmp;
32  tmp.reserve(fileCatalogItems_.size());
33  for (auto const& item : fileCatalogItems_) {
34  tmp.push_back(item.fileName(iCatalog));
35  }
36  return tmp;
37  }
38 
39  void InputFileCatalog::init(std::string const& inputOverride,
40  bool useLFNasPFNifLFNnotFound,
41  edm::CatalogType catType) {
42  typedef std::vector<std::string>::iterator iter;
43 
44  if (!overrideFileLocator_ && !inputOverride.empty()) {
45  if (catType == edm::CatalogType::TrivialCatalog) {
47  std::make_unique<FileLocator>(inputOverride); // propagate_const<T> has no reset() function
48  } else if (catType == edm::CatalogType::RucioCatalog) {
49  //now make a struct from input string
50  std::vector<std::string> tmps;
51  boost::algorithm::split(tmps, inputOverride, boost::is_any_of(std::string(",")));
52  if (tmps.size() != 5) {
53  cms::Exception ex("FileCatalog");
54  ex << "Trying to override input file catalog but invalid input override string " << inputOverride
55  << " (Should be site,subSite,storageSite,volume,protocol)";
56  ex.addContext("Calling edm::InputFileCatalog::init()");
57  throw ex;
58  }
59 
60  edm::CatalogAttributes inputOverride_struct(tmps[0], //current-site
61  tmps[1], //current-subSite
62  tmps[2], //desired-data-access-site
63  tmps[3], //desired-data-access-volume
64  tmps[4]); //desired-data-access-protocol
65 
67  std::make_unique<FileLocator>(inputOverride_struct); // propagate_const<T> has no reset() function
68  }
69  }
70 
71  Service<SiteLocalConfig> localconfservice;
72  if (!localconfservice.isAvailable()) {
73  cms::Exception ex("FileCatalog");
74  ex << "edm::SiteLocalConfigService is not available";
75  ex.addContext("Calling edm::InputFileCatalog::init()");
76  throw ex;
77  }
78 
79  if (catType == edm::CatalogType::TrivialCatalog) {
80  std::vector<std::string> const& tmp_dataCatalogs = localconfservice->trivialDataCatalogs();
81  if (!fileLocators_trivalCatalog_.empty())
83  //Construct all file locators from data catalogs. If a data catalog is invalid (wrong protocol for example), it is skipped and no file locator is constructed (an exception is thrown out from FileLocator::init).
84  for (const auto& catalog : tmp_dataCatalogs) {
85  try {
86  fileLocators_trivalCatalog_.push_back(std::make_unique<FileLocator>(catalog));
87  } catch (cms::Exception const& e) {
88  edm::LogWarning("InputFileCatalog")
89  << "Caught an exception while constructing a file locator in InputFileCatalog::init: " << e.what()
90  << "Skip this catalog";
91  }
92  }
93  if (fileLocators_trivalCatalog_.empty()) {
94  cms::Exception ex("FileCatalog");
95  ex << "Unable to construct any file locator in InputFileCatalog::init";
96  ex.addContext("Calling edm::InputFileCatalog::init()");
97  throw ex;
98  }
99  } else if (catType == edm::CatalogType::RucioCatalog) {
100  std::vector<edm::CatalogAttributes> const& tmp_dataCatalogs = localconfservice->dataCatalogs();
101  if (!fileLocators_.empty())
102  fileLocators_.clear();
103  //Construct all file locators from data catalogs. If a data catalog is invalid (wrong protocol for example), it is skipped and no file locator is constructed (an exception is thrown out from FileLocator::init).
104  for (const auto& catalog : tmp_dataCatalogs) {
105  try {
106  fileLocators_.push_back(std::make_unique<FileLocator>(catalog));
107  } catch (cms::Exception const& e) {
108  edm::LogWarning("InputFileCatalog")
109  << "Caught an exception while constructing a file locator in InputFileCatalog::init: " << e.what()
110  << "Skip this catalog";
111  }
112  }
113  if (fileLocators_.empty()) {
114  cms::Exception ex("FileCatalog");
115  ex << "Unable to construct any file locator in InputFileCatalog::init";
116  ex.addContext("Calling edm::InputFileCatalog::init()");
117  throw ex;
118  }
119  } else {
120  cms::Exception ex("FileCatalog");
121  ex << "Undefined catalog type";
122  ex.addContext("Calling edm::InputFileCatalog::init()");
123  throw ex;
124  }
125 
126  for (iter it = fileNames_.begin(), lt = logicalFileNames_.begin(), itEnd = fileNames_.end(); it != itEnd;
127  ++it, ++lt) {
128  boost::trim(*it);
129  std::vector<std::string> pfns;
130  if (it->empty()) {
131  cms::Exception ex("FileCatalog");
132  ex << "An empty string specified in the fileNames parameter for input source";
133  ex.addContext("Calling edm::InputFileCatalog::init()");
134  throw ex;
135  }
136  if (isPhysical(*it)) {
137  if (it->back() == ':') {
138  cms::Exception ex("FileCatalog");
139  ex << "An empty physical file name specified in the fileNames parameter for input source";
140  ex.addContext("Calling edm::InputFileCatalog::init()");
141  throw ex;
142  }
143  pfns.push_back(*it);
144  // Clear the LFN.
145  lt->clear();
146  } else {
147  boost::trim(*lt);
148  findFile(*lt, pfns, useLFNasPFNifLFNnotFound, catType);
149  }
150 
151  fileCatalogItems_.push_back(FileCatalogItem(pfns, *lt));
152  }
153  }
154 
156  std::vector<std::string>& pfns,
157  bool useLFNasPFNifLFNnotFound,
158  edm::CatalogType catType) {
159  if (overrideFileLocator_) {
160  pfns.push_back(overrideFileLocator_->pfn(lfn, catType));
161  } else {
162  if (catType == edm::CatalogType::TrivialCatalog) {
163  for (auto const& locator : fileLocators_trivalCatalog_) {
164  std::string pfn = locator->pfn(lfn, edm::CatalogType::TrivialCatalog);
165  if (pfn.empty() && useLFNasPFNifLFNnotFound)
166  pfns.push_back(lfn);
167  else
168  pfns.push_back(pfn);
169  }
170  } else if (catType == edm::CatalogType::RucioCatalog) {
171  for (auto const& locator : fileLocators_) {
172  std::string pfn = locator->pfn(lfn, edm::CatalogType::RucioCatalog);
173  if (pfn.empty() && useLFNasPFNifLFNnotFound)
174  pfns.push_back(lfn);
175  else
176  pfns.push_back(pfn);
177  }
178  } else {
179  cms::Exception ex("FileCatalog");
180  ex << "Undefined catalog type";
181  ex.addContext("Calling edm::InputFileCatalog::findFile()");
182  throw ex;
183  }
184  }
185 
186  // Empty PFN will be found by caller.
187  }
188 
189 } // namespace edm
void findFile(std::string const &lfn, std::vector< std::string > &pfns, bool useLFNasPFNifLFNnotFound, edm::CatalogType catType)
std::vector< std::string > fileNames(unsigned iCatalog) const
std::vector< FileCatalogItem > fileCatalogItems_
static void trim(std::string &s)
std::vector< std::string > fileNames_
std::vector< edm::propagate_const< std::unique_ptr< FileLocator > > > fileLocators_
static bool isPhysical(std::string const &name)
void init(std::string const &override, bool useLFNasPFNifLFNnotFound, edm::CatalogType catType)
InputFileCatalog(std::vector< std::string > const &fileNames, std::string const &override, bool useLFNasPFNifLFNnotFound=false, edm::CatalogType catType=edm::CatalogType::RucioCatalog)
std::vector< edm::propagate_const< std::unique_ptr< FileLocator > > > fileLocators_trivalCatalog_
void addContext(std::string const &context)
Definition: Exception.cc:169
HLT enums.
edm::propagate_const< std::unique_ptr< FileLocator > > overrideFileLocator_
bool isAvailable() const
Definition: Service.h:40
Log< level::Warning, false > LogWarning
tmp
align.sh
Definition: createJobs.py:716
std::vector< std::string > logicalFileNames_