Belle II Software development
b2file-catalog-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#include <string>
19#include <iostream>
20
21using namespace std;
22using namespace Belle2;
23namespace prog = boost::program_options;
24
25int main(int argc, char* argv[])
26{
27 // Define command line options
28 prog::options_description options("Options");
29 options.add_options()
30 ("help,h", "print all available options")
31 ("file,f", prog::value<string>(), "local file name")
32 ;
33
34 prog::positional_options_description posOptDesc;
35 posOptDesc.add("file", -1);
36
37 prog::variables_map varMap;
38 prog::store(prog::command_line_parser(argc, argv).
39 options(options).positional(posOptDesc).run(), varMap);
40 prog::notify(varMap);
41
42 // Check for help option
43 if (varMap.count("help")) {
44 cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
45 cout << "Add the given file to the file catalog (e.g. so it can be found as a parent file).\n\n";
46 cout << options << endl;
47 return 0;
48 }
49
50 // Check for file option
51 if (!varMap.count("file")) {
52 B2ERROR("No file name given");
53 return 1;
54 }
55 string fileName = varMap["file"].as<string>();
56
57 // Open file and read metadata
58 gErrorIgnoreLevel = kError;
59 TFile* file = TFile::Open(fileName.c_str(), "READ");
60 if (!file || !file->IsOpen()) {
61 B2ERROR("Couldn't open file " << fileName);
62 return 1;
63 }
64 auto* tree = (TTree*) file->Get("persistent");
65 if (!tree) {
66 B2ERROR("No tree persistent found in " << fileName);
67 return 1;
68 }
69 TBranch* branch = tree->GetBranch("FileMetaData");
70 if (!branch) {
71 B2ERROR("No meta data found in " << fileName);
72 return 1;
73 }
74 FileMetaData* metaData = nullptr;
75 branch->SetAddress(&metaData);
76 tree->GetEntry(0);
77
78 // Register file in metadata catalog
79 if (!FileCatalog::Instance().registerFile(fileName, *metaData)) {
80 B2ERROR("Registration of file " << fileName << " failed");
81 return 1;
82 }
83
84 return 0;
85}
86
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
Definition: FileCatalog.cc:23
Metadata information about a file.
Definition: FileMetaData.h:29
Abstract base class for different kinds of events.
STL namespace.