Belle II Software development
b2file-metadata-show.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// Basf2 headers
10#include <framework/core/FileCatalog.h>
11#include <framework/dataobjects/FileMetaData.h>
12#include <framework/io/RootFileInfo.h>
13#include <framework/logging/Logger.h>
14
15// ROOT headers
16#include <TError.h>
17
18// C++ headers
19#include <iostream>
20#include <string>
21
22// Boost headers
23#include <boost/program_options.hpp>
24
25using namespace Belle2;
26namespace prog = boost::program_options;
27
28int main(int argc, char* argv[])
29{
30 // Define command line options
31 prog::options_description options("Options");
32 options.add_options()
33 ("help,h", "print all available options")
34 ("file,f", prog::value<std::string>(), "local file name")
35 ("lfn,l", prog::value<std::string>(), "logical file name")
36 ("all,a", "print all information")
37 ("json", "print machine-readable information in JSON format. Implies --all and --steering.")
38 ("steering,s", "print steering file contents")
39 ;
40
41 prog::positional_options_description posOptDesc;
42 posOptDesc.add("file", -1);
43
44 prog::variables_map varMap;
45 try {
46 prog::store(prog::command_line_parser(argc, argv).
47 options(options).positional(posOptDesc).run(), varMap);
48 prog::notify(varMap);
49 } catch (std::exception& e) {
50 std::cout << "Problem parsing command line: " << e.what() << std::endl;
51 std::cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
52 std::cout << options << std::endl;
53 return 1;
54 }
55
56 //Check for help option
57 if (varMap.count("help") or argc == 1) {
58 std::cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
59 std::cout << options << std::endl;
60 return 0;
61 }
62
63 FileMetaData metaData{};
64
65 //Check for file and lfn options
66 if (varMap.count("file")) {
67 gErrorIgnoreLevel = kError;
68 std::string fileName = varMap["file"].as<std::string>();
69 try {
70 RootIOUtilities::RootFileInfo fileInfo{fileName};
71 metaData = fileInfo.getFileMetaData();
72 } catch (const std::invalid_argument&) {
73 B2FATAL("The input file can not be opened"
74 << LogVar("File name", fileName));
75 } catch (const std::runtime_error& e) {
76 B2FATAL("Something went wrong with the input file"
77 << LogVar("File name", fileName)
78 << LogVar("Issue", e.what()));
79 }
80 } else if (varMap.count("lfn")) {
81 std::string lfn = varMap["lfn"].as<std::string>();
82 if (!FileCatalog::Instance().getMetaData(lfn, metaData))
83 B2FATAL("No FileMetaData found in FileCatalog"
84 << LogVar("LFN", varMap["lfn"].as<int>()));
85 } else
86 B2FATAL("Please specify either a file name or a LFN.");
87
88 const char* option = "";
89 if (varMap.count("json"))
90 option = "json";
91 else if (varMap.count("all"))
92 option = "all";
93 metaData.Print(option);
94 if (std::string(option) != "json" and varMap.count("steering"))
95 metaData.Print("steering");
96
97 return 0;
98}
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
Metadata information about a file.
Helper class to factorize some necessary tasks when working with Belle2 output files.
const FileMetaData & getFileMetaData()
Return the event metadata from the file.
Class to store variables with their name which were sent to the logging service.
Abstract base class for different kinds of events.