Typedefs | Functions
fcldump.cxx File Reference
#include <string>
#include <iostream>
#include <vector>
#include <sstream>
#include <memory>
#include "cetlib/filepath_maker.h"
#include "fhiclcpp/ParameterSet.h"
#include "fhiclcpp/make_ParameterSet.h"

Go to the source code of this file.

Typedefs

typedef std::shared_ptr< ParameterSetPSPtr
 
typedef std::vector< PSPtrPSPtrVector
 
typedef std::vector< int > IntVector
 
typedef std::vector< double > DoubleVector
 
typedef std::vector< DoubleVectorDVVector
 
typedef std::vector< stringStringVector
 
typedef std::vector< StringVectorSVVector
 
typedef std::vector< DVVectorDVVVector
 
typedef std::vector< ParameterSetPSVector
 
typedef std::vector< PSPtrPSPVector
 

Functions

template<class T >
string delim ()
 
template<>
string delim< string > ()
 
template<class T >
ostream & operator<< (ostream &out, const std::vector< T > &vec)
 
void print_block (string prefix, PSPtr pcfgs[], unsigned int nlevrem)
 
int main (int argc, char **argv)
 

Typedef Documentation

typedef std::vector<double> DoubleVector

Definition at line 27 of file fcldump.cxx.

Definition at line 28 of file fcldump.cxx.

Definition at line 31 of file fcldump.cxx.

typedef std::vector<int> IntVector

Definition at line 26 of file fcldump.cxx.

typedef std::shared_ptr<ParameterSet> PSPtr

Definition at line 24 of file fcldump.cxx.

Definition at line 25 of file fcldump.cxx.

Definition at line 33 of file fcldump.cxx.

Definition at line 32 of file fcldump.cxx.

Definition at line 29 of file fcldump.cxx.

Definition at line 30 of file fcldump.cxx.

Function Documentation

template<class T >
string delim ( )

Definition at line 40 of file fcldump.cxx.

40 { return ""; }
template<>
string delim< string > ( )

Definition at line 43 of file fcldump.cxx.

43 { return "\""; }
int main ( int  argc,
char **  argv 
)

Definition at line 173 of file fcldump.cxx.

173  {
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 }
void print_block(string prefix, PSPtr pcfgs[], unsigned int nlevrem)
Definition: fcldump.cxx:66
std::string getenv(std::string const &name)
Definition: getenv.cc:15
std::vector< PSPtr > PSPtrVector
Definition: fcldump.cxx:25
string filepath
Definition: train.py:371
unique_ptr< InputSource > make(ParameterSet const &conf, InputSourceDescription &desc)
QTextStream & endl(QTextStream &s)
template<class T >
ostream& operator<< ( ostream &  out,
const std::vector< T > &  vec 
)

Definition at line 50 of file fcldump.cxx.

50  {
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 }
void print_block ( string  prefix,
PSPtr  pcfgs[],
unsigned int  nlevrem 
)

Definition at line 66 of file fcldump.cxx.

66  {
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 }
bool dbg
std::vector< ParameterSet > PSVector
Definition: fcldump.cxx:32
void print_block(string prefix, PSPtr pcfgs[], unsigned int nlevrem)
Definition: fcldump.cxx:66
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
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
std::vector< int > IntVector
Definition: fcldump.cxx:26
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
std::vector< DoubleVector > DVVector
Definition: fcldump.cxx:28
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
QTextStream & endl(QTextStream &s)