fcldump.cxx
Go to the documentation of this file.
1 // fcldump.cxx
2 //
3 // David Adams
4 // September 2015
5 
6 #include <string>
7 #include <iostream>
8 #include <vector>
9 #include <sstream>
10 #include <memory>
11 #include "cetlib/filepath_maker.h"
12 #include "fhiclcpp/ParameterSet.h"
14 
15 using std::string;
16 using std::cout;
17 using std::endl;
18 using std::ostream;
19 using std::istringstream;
20 using std::make_shared;
23 
24 typedef std::shared_ptr<ParameterSet> PSPtr;
25 typedef std::vector<PSPtr> PSPtrVector;
26 typedef std::vector<int> IntVector;
27 typedef std::vector<double> DoubleVector;
28 typedef std::vector<DoubleVector> DVVector;
29 typedef std::vector<string> StringVector;
30 typedef std::vector<StringVector> SVVector;
31 typedef std::vector<DVVector> DVVVector;
32 typedef std::vector<ParameterSet> PSVector;
33 typedef std::vector<PSPtr> PSPVector;
34 
35 //**********************************************************************
36 
37 // Delimiter.
38 
39 template<class T>
40 string delim() { return ""; }
41 
42 template<>
43 string delim<string>() { return "\""; }
44 
45 //**********************************************************************
46 
47 // Print a sequence.
48 
49 template<class T>
50 ostream& operator<<(ostream& out, const std::vector<T>& vec) {
51  out << "[";
52  bool first = true;
53  for ( auto val : vec ) {
54  if ( ! first ) cout << ", ";
55  out << delim<T>() << val << delim<T>();
56  first = false;
57  }
58  out << "]";
59  return out;
60 }
61 
62 //**********************************************************************
63 
64 // Print a block.
65 
66 void print_block(string prefix, PSPtr pcfgs[], unsigned int nlevrem) {
67  const string myname = "fcldump:print_block: ";
68  int dbg = 0;
69  ParameterSet* pcfg = pcfgs[0].get();
70  // First print the values.
71  int nblk = 0;
72  for ( string key : pcfg->get_names() ) {
73  ++nblk;
74  if ( pcfg->is_key_to_table(key) ) continue;
75  cout << prefix << key << ": ";
76  if ( pcfg->is_key_to_sequence(key) ) {
77  if ( dbg ) cout << myname << prefix << "Key to sequence: " << key << endl;
78  try {
79  cout << pcfg->get<IntVector>(key);
80  } catch (...) {
81  try {
82  cout << pcfg->get<DoubleVector>(key);
83  } catch (...) {
84  try {
85  cout << pcfg->get<DVVector>(key);
86  } catch (...) {
87  try {
88  cout << pcfg->get<DVVVector>(key);
89  } catch (...) {
90  try {
91  cout << pcfg->get<StringVector>(key);
92  } catch (...) {
93  try {
94  cout << pcfg->get<SVVector>(key);
95  } catch (...) {
96  try {
97  // Trick case: sequence of parameter sets.
98  PSVector psets = pcfg->get<PSVector>(key);
99  cout << "[";
100  int submaxlev = nlevrem;
101  if ( submaxlev > 0 ) {
102  for ( const ParameterSet& pset : psets ) {
103  cout << endl;
104  PSPtrVector subcfgs(submaxlev, std::make_shared<ParameterSet>(pset));
105  subcfgs[0] = std::make_shared<ParameterSet>(pset);
106  cout << prefix << "{" << endl;
107  print_block(prefix + " ", &subcfgs[0], submaxlev-1);
108  cout << prefix << "}";
109  }
110  cout << endl << prefix << "]";
111  } else {
112  for ( unsigned int ips=0; ips<psets.size(); ++ips ) cout << ".";
113  cout << "]";
114  }
115  } catch (fhicl::exception& exc) {
116  cout << "ERROR: Unknown data type for sequence key " << key << endl;
117  throw exc;
118  }
119  }
120  }
121  }
122  }
123  }
124  }
125  } else {
126  if ( dbg ) cout << myname << prefix << "Key to value: " << key << endl;
127  try {
128  int val = pcfg->get<int>(key);
129  cout << val;
130  } catch (...) {
131  try {
132  double val = pcfg->get<double>(key);
133  cout << val;
134  } catch (...) {
135  try {
136  cout << "\"" << pcfg->get<string>(key) << "\"";
137  } catch (fhicl::exception& exc) {
138  cout << "ERROR: Unknown data type for non-sequence key " << key << endl;
139  throw exc;
140  }
141  }
142  }
143  }
144  cout << endl;
145  }
146  // Next the blocks.
147  for ( string key : pcfg->get_pset_names() ) {
148  ++nblk;
149  if ( dbg ) cout << myname << prefix << "Key to parameter set: " << key << endl;
150  cout << prefix << key << ": {";
151  PSPtr pcfgnext(new ParameterSet);
152  if ( ! pcfg->get_if_present<ParameterSet>(key, *pcfgnext) ) {
153  cout << " ERROR!!!!!!!!!!!!!!!!" << endl;
154  break;
155  }
156  int nKeysNext = pcfgnext->get_names().size();
157  if ( ! pcfgnext->get_names().size() ) {
158  cout << " }";
159  } else if ( nlevrem > 0 ) {
160  std::swap(pcfgs[1], pcfgnext);
161  cout << endl;
162  print_block(prefix + " ", &pcfgs[1], nlevrem-1);
163  cout << prefix << "}";
164  } else {
165  for ( int ikey=0; ikey<nKeysNext; ++ikey ) cout << ".";
166  cout << "}";
167  }
168  cout << endl;
169  }
170  if ( nblk == 0 ) cout << myname << "WARNING: Block has no entries." << endl;
171 }
172 
173 int main(int argc, char** argv) {
174  const string myname = "fcldump: ";
175  bool help = argc == 1;
176  unsigned int maxlev = 0;
177  string fname;
178  if ( argc > 1 ) {
179  string arg = argv[1];
180  if ( arg == "-h" ) help = true;
181  else fname = argv[1];
182  }
183  if ( argc > 2 ) {
184  istringstream ssarg(argv[2]);
185  ssarg >> maxlev;
186  }
187  if ( help ) {
188  cout << "Usage: " << argv[0] << " FCLFILE [DEPTH]" << endl;
189  cout << " DEPTH > 0 dumps fcl contents to that depth." << endl;
190  cout << " Full path to the fcl file is displayed if DEPTH is omitted" << endl;
191  return 0;
192  }
193 
194  // Find the file.
195  string path = getenv("FHICL_FILE_PATH");
196  if ( path.size() == 0 ) path = ".";
197  filepath_lookup fpm(path);
198  string filepath;
199  try { filepath = fpm(fname); }
200  catch(...) {
201  cout << myname << "ERROR: Unable to find file " << fname << endl;
202  cout << myname << "Search path:" << endl;
203  string::size_type ipos = 0;
204  while ( ipos != string::npos ) {
205  string::size_type jpos = path.find(":", ipos+1);
206  cout << " " << path.substr(ipos, jpos-ipos) << endl;
207  if ( jpos == string::npos ) break;
208  ipos = jpos + 1;
209  }
210  return 8;
211  }
212 
213  cout << filepath << endl;
214  if ( maxlev <= 0 ) return 0;
215  cout << endl;
216 
217  // Fetch top-level configuration and print.
218  PSPtrVector cfgs(maxlev, std::make_shared<ParameterSet>());
219  *cfgs.front() = ParameterSet::make(fname, fpm);
220  string prefix;
221  print_block(prefix, &cfgs[0], maxlev-1);
222  return 0;
223 }
int main(int argc, char **argv)
Definition: fcldump.cxx:173
string delim()
Definition: fcldump.cxx:40
bool dbg
std::string string
Definition: nybbler.cc:12
std::vector< ParameterSet > PSVector
Definition: fcldump.cxx:32
void print_block(string prefix, PSPtr pcfgs[], unsigned int nlevrem)
Definition: fcldump.cxx:66
std::vector< PSPtr > PSPVector
Definition: fcldump.cxx:33
bool is_key_to_sequence(std::string const &key) const
Definition: ParameterSet.h:171
std::vector< std::string > get_pset_names() const
bool is_key_to_table(std::string const &key) const
Definition: ParameterSet.h:165
void swap(Handle< T > &a, Handle< T > &b)
def key(type, name=None)
Definition: graph.py:13
std::string getenv(std::string const &name)
Definition: getenv.cc:15
T get(std::string const &key) const
Definition: ParameterSet.h:271
std::shared_ptr< ParameterSet > PSPtr
Definition: fcldump.cxx:24
std::vector< double > DoubleVector
Definition: fcldump.cxx:27
std::vector< PSPtr > PSPtrVector
Definition: fcldump.cxx:25
string filepath
Definition: train.py:371
std::vector< int > IntVector
Definition: fcldump.cxx:26
unique_ptr< InputSource > make(ParameterSet const &conf, InputSourceDescription &desc)
std::vector< DVVector > DVVVector
Definition: fcldump.cxx:31
std::vector< StringVector > SVVector
Definition: fcldump.cxx:30
std::vector< string > StringVector
Definition: fcldump.cxx:29
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
std::vector< std::string > get_names() const
string delim< string >()
Definition: fcldump.cxx:43
std::vector< DoubleVector > DVVector
Definition: fcldump.cxx:28
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)