11 #include <framework/logging/Logger.h>
12 #include <framework/core/FileCatalog.h>
13 #include <framework/dataobjects/FileMetaData.h>
19 #include <boost/program_options.hpp>
26 namespace prog = boost::program_options;
28 int main(
int argc,
char* argv[])
32 std::vector<std::string> dataDescriptions;
34 prog::options_description options(
"Options");
36 (
"help,h",
"print all available options")
37 (
"file", prog::value<string>(&fileName),
"file name")
38 (
"lfn,l", prog::value<string>(&lfn),
"logical file name")
39 (
"description,d", prog::value<std::vector<std::string>>(&dataDescriptions),
40 "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")
43 prog::positional_options_description posOptDesc;
44 posOptDesc.add(
"file", -1);
46 prog::variables_map varMap;
47 prog::store(prog::command_line_parser(argc, argv).
48 options(options).positional(posOptDesc).run(), varMap);
52 if (varMap.count(
"help")) {
53 cout <<
"Usage: " << argv[0] <<
" [OPTIONS] [FILE]\n";
54 cout <<
"Add/edit LFN in given file or modify the data description stored in the file, also update file catalog.\n\n";
55 cout << options << endl;
60 if (!varMap.count(
"file")) {
61 B2ERROR(
"The filename is missing.");
64 if (!varMap.count(
"lfn") && !varMap.count(
"description")) {
65 B2ERROR(
"Neither lfn nor data descriptions to be modified, nothing to do");
70 gErrorIgnoreLevel = kError;
71 TFile* file = TFile::Open(fileName.c_str(),
"UPDATE");
72 if (!file || !file->IsOpen()) {
73 B2ERROR(
"Failed to open the file " << fileName);
79 auto* tree =
dynamic_cast<TTree*
>(file->Get(
"persistent"));
80 TTree* newTree =
nullptr;
82 fileMetaData =
dynamic_cast<FileMetaData*
>(file->Get(
"FileMetaData"));
84 B2WARNING(
"Failed to get persistent tree in the file " << fileName);
85 tree =
new TTree(
"persistent",
"persistent");
87 tree->Branch(
"FileMetaData", &fileMetaData);
91 tree->SetBranchAddress(
"FileMetaData", &fileMetaData);
92 newTree = tree->CloneTree(0);
97 const std::string oldLFN = fileMetaData->
getLfn();
100 if (varMap.count(
"lfn")) fileMetaData->
setLfn(lfn);
101 if (!dataDescriptions.empty()) {
102 for (
const auto& keyvalue : dataDescriptions) {
103 size_t pos = keyvalue.find(
'=');
104 if (pos == std::string::npos) {
108 const std::string key = keyvalue.substr(0, pos);
109 const std::string value = keyvalue.substr(pos + 1);
116 newTree->Write(newTree->GetName(), TObject::kWriteDelete);
118 fileMetaData->Write(
"FileMetaData");
125 std::string oldPFN = oldLFN;
127 if (FileCatalog::Instance().getMetaData(oldPFN, localMetaData)) {
128 localMetaData = *fileMetaData;
129 FileCatalog::Instance().registerFile(fileName, localMetaData, oldLFN);