Belle II Software  release-06-02-00
b2file-metadata-add.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/logging/Logger.h>
10 #include <framework/core/FileCatalog.h>
11 #include <framework/dataobjects/FileMetaData.h>
12 
13 #include <TFile.h>
14 #include <TTree.h>
15 #include <TError.h>
16 
17 #include <boost/program_options.hpp>
18 
19 #include <string>
20 #include <iostream>
21 
22 using namespace std;
23 using namespace Belle2;
24 namespace prog = boost::program_options;
25 
26 int main(int argc, char* argv[])
27 {
28  std::string fileName;
29  std::string lfn;
30  std::vector<std::string> dataDescriptions;
31  // define command line options
32  prog::options_description options("Options");
33  options.add_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  ("description,d", prog::value<std::vector<std::string>>(&dataDescriptions),
38  "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")
39  ;
40 
41  prog::positional_options_description posOptDesc;
42  posOptDesc.add("file", -1);
43 
44  prog::variables_map varMap;
45  prog::store(prog::command_line_parser(argc, argv).
46  options(options).positional(posOptDesc).run(), varMap);
47  prog::notify(varMap);
48 
49  // check for help option
50  if (varMap.count("help")) {
51  cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
52  cout << "Add/edit LFN in given file or modify the data description stored in the file, also update file catalog.\n\n";
53  cout << options << endl;
54  return 0;
55  }
56 
57  // check parameters
58  if (!varMap.count("file")) {
59  B2ERROR("The filename is missing.");
60  return 1;
61  }
62  if (!varMap.count("lfn") && !varMap.count("description")) {
63  B2ERROR("Neither lfn nor data descriptions to be modified, nothing to do");
64  return 1;
65  }
66 
67  // open the root file
68  gErrorIgnoreLevel = kError;
69  TFile* file = TFile::Open(fileName.c_str(), "UPDATE");
70  if (!file || !file->IsOpen()) {
71  B2ERROR("Failed to open the file " << fileName);
72  return 1;
73  }
74 
75  // read the FileMetaData object or create a new one if it doesn't exist
76  FileMetaData* fileMetaData = nullptr;
77  auto* tree = dynamic_cast<TTree*>(file->Get("persistent"));
78  TTree* newTree = nullptr;
79  if (!tree) {
80  fileMetaData = dynamic_cast<FileMetaData*>(file->Get("FileMetaData"));
81  if (!fileMetaData) {
82  B2WARNING("Failed to get persistent tree in the file " << fileName);
83  tree = new TTree("persistent", "persistent");
84  fileMetaData = new FileMetaData;
85  tree->Branch("FileMetaData", &fileMetaData);
86  newTree = tree;
87  }
88  } else {
89  tree->SetBranchAddress("FileMetaData", &fileMetaData);
90  newTree = tree->CloneTree(0);
91  tree->GetEntry(0);
92  }
93 
94  if (!fileMetaData) {
95  B2ERROR("Failed to load FileMetaData from file " << fileName);
96  return 1;
97  }
98 
99  // remember old lfn in case this file was registered in file catalog
100  const std::string oldLFN = fileMetaData->getLfn();
101 
102  // update the IDs and write the updated FileMetaData to the file
103  if (varMap.count("lfn")) fileMetaData->setLfn(lfn);
104  if (!dataDescriptions.empty()) {
105  for (const auto& keyvalue : dataDescriptions) {
106  size_t pos = keyvalue.find('=');
107  if (pos == std::string::npos) {
108  // no '=' in -d argument, assume deletion
109  fileMetaData->removeDataDescription(keyvalue);
110  } else {
111  const std::string key = keyvalue.substr(0, pos);
112  const std::string value = keyvalue.substr(pos + 1);
113  fileMetaData->setDataDescription(key, value);
114  }
115  }
116  }
117  if (newTree) {
118  newTree->Fill();
119  newTree->Write(newTree->GetName(), TObject::kWriteDelete);
120  } else {
121  fileMetaData->Write("FileMetaData");
122  }
123 
124  // properly close file
125  file->Close();
126 
127  // update the local file catalog but only *if* the file was already registered
128  std::string oldPFN = oldLFN;
129  FileMetaData localMetaData;
130  if (FileCatalog::Instance().getMetaData(oldPFN, localMetaData)) {
131  localMetaData = *fileMetaData;
132  FileCatalog::Instance().registerFile(fileName, localMetaData, oldLFN);
133  }
134  return 0;
135 }
Metadata information about a file.
Definition: FileMetaData.h:29
void removeDataDescription(const std::string &key)
remove an existing data description
Definition: FileMetaData.h:207
const std::string & getLfn() const
Logical file name getter.
Definition: FileMetaData.h:37
void setLfn(const std::string &lfn)
Setter for LFN.
Definition: FileMetaData.h:135
void setDataDescription(const std::string &key, const std::string &value)
describe the data, if the key exists contents will be overwritten.
Definition: FileMetaData.h:204
Abstract base class for different kinds of events.
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:75