extractCAFMetadataInC.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <memory>
3 #include <string>
4 
5 #include <sys/stat.h>
6 
7 #include "TError.h"
8 #include "TFile.h"
9 #include "TObject.h"
10 
11 const std::string xRootDBase = "root://fndca1.fnal.gov:1094/pnfs/fnal.gov/usr/";
12 
14 {
15  filePath.replace(filePath.find("/pnfs/"), 6, xRootDBase);
16  return filePath;
17 }
18 
20 {
21  size_t pos = 0;
22  while(true){
23  pos = str.find("\"", pos);
24  if(pos == std::string::npos) return str;
25  str.replace(pos, 1, "\\\"");
26  pos += 2; // Where to start looking next time
27  }
28 }
29 
30 int main(int argc, char** argv)
31 {
32  gErrorIgnoreLevel = 100000;
33 
34  if(argc < 2){
35  std::cerr << "Usage: must supply at least one filename as an argument"
36  << std::endl;
37  exit(1);
38  }
39 
40  std::cout << "{" << std::endl;
41 
42  for(int i = 1; i < argc; ++i){
43  std::string filePath = argv[i];
44 
45  struct stat buf;
46  if(stat(filePath.c_str(), &buf) != 0){
47  std::cerr << "ERROR: File does not exist: " << filePath << std::endl;
48  continue;
49  }
50 
51  const char* base = basename(filePath.c_str());
52 
53  if(filePath.find("/pnfs/") != std::string::npos)
54  filePath = pnfsToXRootD(filePath);
55 
56  std::unique_ptr<TFile> f(TFile::Open(filePath.c_str(), "READ"));
57 
58  if(!f->IsOpen()){
59  std::cerr << "ERROR: Unable to open " << filePath
60  << " as a TFile, is this a proper ROOT file?" << std::endl;
61  continue;
62  }
63 
64  std::cout << " \"" << base << "\": {" << std::endl;
65 
66  TDirectory* metadata = (TDirectory*)f->Get("metadata");
67  if(!metadata || metadata->Class() != TDirectory::Class()){
68  std::cerr << "ERROR: Unable to access metadata in " << filePath
69  << " is this a proper CAF with metadata?" << std::endl;
70  continue;
71  }
72 
73  TList* keys = metadata->GetListOfKeys();
74 
75  TIter next(keys);
76  TObject* key;
77  bool first = true;
78  while((key = next())){
79  if(first){
80  first = false;
81  }
82  else{
83  std::cout << "," << std::endl;
84  }
85 
86  std::cout << " \"" << key->GetName() << "\": ";
87  std::string val = metadata->Get(key->GetName())->GetName();
88  if(val.empty()) val = "none";
89  std::cout << "\"" << escapeQuotes(val) << "\"";
90  }
92 
93  std::cout << " }";
94  if(i < argc-1) std::cout << ",";
96  } // end for i
97 
98  std::cout << "}" << std::endl;
99 
100  return 0;
101 }
int main(int argc, char **argv)
keys
Reco plots.
Definition: caf_analysis.py:46
const std::string xRootDBase
OStream cerr
Definition: OStream.cxx:7
const XML_Char int const XML_Char int const XML_Char * base
Definition: expat.h:331
std::string GetName(int i)
std::string escapeQuotes(std::string str)
OStream cout
Definition: OStream.cxx:6
exit(0)
void next()
Definition: show_event.C:84
std::string pnfsToXRootD(std::string filePath)
enum BeamMode string