Belle II Software  release-05-01-25
b2file-metadata-show.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - 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 <csignal>
20 
21 #include <boost/program_options.hpp>
22 
23 #include <string>
24 #include <iostream>
25 
26 using namespace std;
27 using namespace Belle2;
28 namespace prog = boost::program_options;
29 
30 int main(int argc, char* argv[])
31 {
32  //remove SIGPIPE handler set by ROOT which sometimes caused infinite loops
33  //See https://savannah.cern.ch/bugs/?97991
34  //default action is to abort
35  if (signal(SIGPIPE, SIG_DFL) == SIG_ERR) {
36  B2FATAL("Cannot remove SIGPIPE signal handler");
37  }
38 
39  // Define command line options
40  prog::options_description options("Options");
41  options.add_options()
42  ("help,h", "print all available options")
43  ("file,f", prog::value<string>(), "local file name")
44  ("lfn,l", prog::value<string>(), "logical file name")
45  ("all,a", "print all information")
46  ("json", "print machine-readable information in JSON format. Implies --all and --steering.")
47  ("steering,s", "print steering file contents")
48  ;
49 
50  prog::positional_options_description posOptDesc;
51  posOptDesc.add("file", -1);
52 
53  prog::variables_map varMap;
54  try {
55  prog::store(prog::command_line_parser(argc, argv).
56  options(options).positional(posOptDesc).run(), varMap);
57  prog::notify(varMap);
58  } catch (std::exception& e) {
59  cout << "Problem parsing command line: " << e.what() << endl;
60  cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
61  cout << options << endl;
62  return 1;
63  }
64 
65  //Check for help option
66  if (varMap.count("help") or argc == 1) {
67  cout << "Usage: " << argv[0] << " [OPTIONS] [FILE]\n";
68  cout << options << endl;
69  return 0;
70  }
71 
72  FileMetaData metaData;
73  FileMetaData* metaDataPtr = &metaData;
74 
75  //Check for file option
76  if (varMap.count("file")) {
77  gErrorIgnoreLevel = kError;
78  string fileName = varMap["file"].as<string>();
79  TFile* file = TFile::Open(fileName.c_str(), "READ");
80  if (!file || !file->IsOpen()) {
81  B2ERROR("Couldn't open file " << fileName);
82  return 1;
83  }
84  auto* tree = (TTree*) file->Get("persistent");
85  if (!tree) {
86  B2ERROR("No tree persistent found in " << fileName);
87  return 1;
88  }
89  TBranch* branch = tree->GetBranch("FileMetaData");
90  if (!branch) {
91  B2ERROR("No meta data found in " << fileName);
92  return 1;
93  }
94  metaDataPtr = nullptr;
95  branch->SetAddress(&metaDataPtr);
96  tree->GetEntry(0);
97 
98  } else if (varMap.count("lfn")) {
99  std::string lfn = varMap["lfn"].as<string>();
100  if (!FileCatalog::Instance().getMetaData(lfn, metaData)) {
101  B2ERROR("No meta data found in file catalog for LFN " << varMap["lfn"].as<int>());
102  return 1;
103  }
104 
105  } else {
106  B2ERROR("Please specify either a file name, a unique ID, or a LFN.");
107  return 1;
108  }
109 
110  const char* option = "";
111  if (varMap.count("json")) option = "json";
112  else if (varMap.count("all")) option = "all";
113  metaDataPtr->Print(option);
114  if (string(option) != "json" and varMap.count("steering")) metaDataPtr->Print("steering");
115 
116  return 0;
117 }
118 
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
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
Belle2::FileMetaData::Print
virtual void Print(Option_t *option="") const override
Print the content of the meta data object.
Definition: FileMetaData.cc:78