9#include <framework/logging/Logger.h>
10#include <framework/core/FileCatalog.h>
11#include <framework/dataobjects/FileMetaData.h>
17#include <boost/program_options.hpp>
24namespace prog = boost::program_options;
26int main(
int argc,
char* argv[])
30 std::vector<std::string> dataDescriptions;
32 prog::options_description options(
"Options");
34 (
"help,h",
"print all available options")
35 (
"file", prog::value<string>(&fileName),
"file name")
36 (
"lfn,l", prog::value<string>(&lfn),
"logical file name")
37 (
"globalTag,g", prog::value<string>(>),
"global tag")
38 (
"description,d", prog::value<std::vector<std::string>>(&dataDescriptions),
39 "data description to set of the form key=value. If the argument does not contain an equal sign it's interpeted as a key to delete from the dataDescriptions")
42 prog::positional_options_description posOptDesc;
43 posOptDesc.add(
"file", -1);
45 prog::variables_map varMap;
46 prog::store(prog::command_line_parser(argc, argv).
47 options(options).positional(posOptDesc).run(), varMap);
51 if (varMap.count(
"help")) {
52 cout <<
"Usage: " << argv[0] <<
" [OPTIONS] [FILE]\n";
53 cout <<
"Add/edit LFN in given file or modify the data description stored in the file, also update file catalog.\n\n";
54 cout << options << endl;
59 if (!varMap.count(
"file")) {
60 B2ERROR(
"The filename is missing.");
63 if (!varMap.count(
"lfn") && !varMap.count(
"globalTag") && !varMap.count(
"description")) {
64 B2ERROR(
"No metadata modificaton was requested, nothing to do");
69 gErrorIgnoreLevel = kError;
70 TFile* file = TFile::Open(fileName.c_str(),
"UPDATE");
71 if (!file || !file->IsOpen()) {
72 B2ERROR(
"Failed to open the file " << fileName);
78 auto* tree =
dynamic_cast<TTree*
>(file->Get(
"persistent"));
79 TTree* newTree =
nullptr;
81 fileMetaData =
dynamic_cast<FileMetaData*
>(file->Get(
"FileMetaData"));
83 B2WARNING(
"Failed to get persistent tree in the file " << fileName);
84 tree =
new TTree(
"persistent",
"persistent");
86 tree->Branch(
"FileMetaData", &fileMetaData);
90 tree->SetBranchAddress(
"FileMetaData", &fileMetaData);
91 newTree = tree->CloneTree(0);
96 B2ERROR(
"Failed to load FileMetaData from file " << fileName);
101 const std::string oldLFN = fileMetaData->
getLfn();
104 if (varMap.count(
"lfn")) fileMetaData->
setLfn(lfn);
106 if (!dataDescriptions.empty()) {
107 for (
const auto& keyvalue : dataDescriptions) {
108 size_t pos = keyvalue.find(
'=');
109 if (pos == std::string::npos) {
113 const std::string key = keyvalue.substr(0, pos);
114 const std::string value = keyvalue.substr(pos + 1);
123 newTree->Write(newTree->GetName(), TObject::kWriteDelete);
125 fileMetaData->Write(
"FileMetaData");
132 std::string oldPFN = oldLFN;
135 localMetaData = *fileMetaData;
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
virtual bool registerFile(const std::string &fileName, FileMetaData &metaData, const std::string &oldLFN="")
Register a file in the (local) file catalog.
Abstract base class for different kinds of events.