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 <csignal>
20#include <iostream>
21#include <string>
22
23// Boost headers
24#include <boost/program_options.hpp>
25
26using namespace Belle2;
27namespace prog = boost::program_options;
28
29int main(int argc, char* argv[])
30{
31 //remove SIGPIPE handler set by ROOT which sometimes caused infinite loops
32 //See https://savannah.cern.ch/bugs/?97991
33 //default action is to abort
34 if (std::signal(SIGPIPE, SIG_DFL) == SIG_ERR)
35 B2FATAL("Cannot remove SIGPIPE signal handler");
36
37 // Define command line options
38 prog::options_description options("Options");
39 options.add_options()
40 ("help,h", "print all available options")
41 ("file,f", prog::value<std::string>(), "local file name")
42 ("lfn,l", prog::value<std::string>(), "logical file name")
43 ("all,a", "print all information")
44 ("json", "print machine-readable information in JSON format. Implies --all and --steering.")
45 ("steering,s", "print steering file contents")
46 ;
47
48 prog::positional_options_description posOptDesc;
49 posOptDesc.add("file", -1);
50
51 prog::variables_map varMap;
52 try {
53 prog::store(prog::command_line_parser(argc, argv).
54 options(options).positional(posOptDesc).run(), varMap);
55 prog::notify(varMap);
56 } catch (std::exception& e) {
57 std::cout << "Problem parsing command line: " << e.what() << std::endl;
58 std::cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
59 std::cout << options << std::endl;
60 return 1;
61 }
62
63 //Check for help option
64 if (varMap.count("help") or argc == 1) {
65 std::cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
66 std::cout << options << std::endl;
67 return 0;
68 }
69
70 FileMetaData metaData{};
71
72 //Check for file and lfn options
73 if (varMap.count("file")) {
74 gErrorIgnoreLevel = kError;
75 std::string fileName = varMap["file"].as<std::string>();
76 try {
77 RootIOUtilities::RootFileInfo fileInfo{fileName};
78 metaData = fileInfo.getFileMetaData();
79 } catch (const std::invalid_argument&) {
80 B2FATAL("The input file can not be opened"
81 << LogVar("File name", fileName));
82 } catch (const std::runtime_error& e) {
83 B2FATAL("Something went wrong with the input file"
84 << LogVar("File name", fileName)
85 << LogVar("Issue", e.what()));
86 }
87 } else if (varMap.count("lfn")) {
88 std::string lfn = varMap["lfn"].as<std::string>();
89 if (!FileCatalog::Instance().getMetaData(lfn, metaData))
90 B2FATAL("No FileMetaData found in FileCatalog"
91 << LogVar("LFN", varMap["lfn"].as<int>()));
92 } else
93 B2FATAL("Please specify either a file name or a LFN.");
94
95 const char* option = "";
96 if (varMap.count("json"))
97 option = "json";
98 else if (varMap.count("all"))
99 option = "all";
100 metaData.Print(option);
101 if (std::string(option) != "json" and varMap.count("steering"))
102 metaData.Print("steering");
103
104 return 0;
105}
static FileCatalog & Instance()
Static method to get a reference to the FileCatalog instance.
Definition: FileCatalog.cc:23
virtual bool getMetaData(std::string &fileName, FileMetaData &metaData)
Get the metadata of a file with given (logical) file name.
Definition: FileCatalog.cc:142
Metadata information about a file.
Definition: FileMetaData.h:29
Helper class to factorize some necessary tasks when working with Belle2 output files.
Definition: RootFileInfo.h:27
Class to store variables with their name which were sent to the logging service.
Abstract base class for different kinds of events.