All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
extractCAFMetadata.cc File Reference
#include <iostream>
#include <string>
#include <sys/stat.h>
#include "TError.h"
#include "TFile.h"
#include "TTree.h"

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

int main ( int  argc,
char **  argv 
)

Definition at line 10 of file extractCAFMetadata.cc.

11 {
12  gErrorIgnoreLevel = 100000;
13 
14  if(argc != 2){
15  std::cerr << "Usage: must supply one filename as an argument" << std::endl;
16  exit(1);
17  }
18 
19  const std::string filePath = argv[1];
20 
21  struct stat buf;
22  if(stat(filePath.c_str(), &buf) != 0){
23  std::cerr << "ERROR: File does not exist: " << filePath << std::endl;
24  exit(1);
25  }
26 
27  std::unique_ptr<TFile> f(TFile::Open(filePath.c_str(), "READ"));
28 
29  if(!f->IsOpen()){
30  std::cerr << "ERROR: Unable to open " << filePath
31  << " as a TFile, is this a proper ROOT file?" << std::endl;
32  exit(1);
33  }
34 
35  TDirectory* metadata = f->GetDirectory("metadata");
36  if(!metadata){
37  std::cerr << "ERROR: Unable to access metadata in " << filePath
38  << " is this a proper CAF with metadata?" << std::endl;
39  exit(1);
40  }
41 
42  TTree* tr = (TTree*)metadata->Get("metatree");
43  if(!tr){
44  std::cerr << "ERROR: Unable to access metadata tree in " << filePath
45  << " is this a proper CAF with metadata?" << std::endl;
46  exit(1);
47  }
48 
49  std::cout << "{\n";
50 
51  std::string key, value;
52  std::string* pkey = &key;
53  std::string* pvalue = &value;
54  tr->SetBranchAddress("key", &pkey);
55  tr->SetBranchAddress("value", &pvalue);
56 
57  bool first = true;
58  for(int i = 0; i < tr->GetEntries(); ++i){
59  if(!first) std::cout << "," << std::endl;
60  first = false;
61 
62  tr->GetEntry(i);
63 
64  std::cout << " \"" << key << "\": ";
65  // if(value.empty()) value = "\"none\"";
66  // The convention is that values are already suitably escaped inside the
67  // CAF file.
68  std::cout << value;
69  }
70  std::cout << "\n}" << std::endl;
71 
72  return 0;
73 }
BEGIN_PROLOG could also be cerr
temporary value
BEGIN_PROLOG could also be cout