Belle II Software  release-05-01-25
b2file-catalog-add.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2014 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Thomas Kuhr *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <framework/logging/Logger.h>
12 #include <framework/core/FileCatalog.h>
13 #include <framework/dataobjects/FileMetaData.h>
14 
15 #include <TFile.h>
16 #include <TTree.h>
17 #include <TError.h>
18 
19 #include <boost/program_options.hpp>
20 #include <string>
21 #include <iostream>
22 
23 using namespace std;
24 using namespace Belle2;
25 namespace prog = boost::program_options;
26 
27 int main(int argc, char* argv[])
28 {
29  // Define command line options
30  prog::options_description options("Options");
31  options.add_options()
32  ("help,h", "print all available options")
33  ("file,f", prog::value<string>(), "local file name")
34  ;
35 
36  prog::positional_options_description posOptDesc;
37  posOptDesc.add("file", -1);
38 
39  prog::variables_map varMap;
40  prog::store(prog::command_line_parser(argc, argv).
41  options(options).positional(posOptDesc).run(), varMap);
42  prog::notify(varMap);
43 
44  // Check for help option
45  if (varMap.count("help")) {
46  cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
47  cout << "Add the given file to the file catalog (e.g. so it can be found as a parent file).\n\n";
48  cout << options << endl;
49  return 0;
50  }
51 
52  // Check for file option
53  if (!varMap.count("file")) {
54  B2ERROR("No file name given");
55  return 1;
56  }
57  string fileName = varMap["file"].as<string>();
58 
59  // Open file and read metadata
60  gErrorIgnoreLevel = kError;
61  TFile* file = TFile::Open(fileName.c_str(), "READ");
62  if (!file || !file->IsOpen()) {
63  B2ERROR("Couldn't open file " << fileName);
64  return 1;
65  }
66  auto* tree = (TTree*) file->Get("persistent");
67  if (!tree) {
68  B2ERROR("No tree persistent found in " << fileName);
69  return 1;
70  }
71  TBranch* branch = tree->GetBranch("FileMetaData");
72  if (!branch) {
73  B2ERROR("No meta data found in " << fileName);
74  return 1;
75  }
76  FileMetaData* metaData = nullptr;
77  branch->SetAddress(&metaData);
78  tree->GetEntry(0);
79 
80  // Register file in metadata catalog
81  if (!FileCatalog::Instance().registerFile(fileName, *metaData)) {
82  B2ERROR("Registration of file " << fileName << " failed");
83  return 1;
84  }
85 
86  return 0;
87 }
88 
Belle2::FileMetaData
Metadata information about a file.
Definition: FileMetaData.h:39
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19