gSplineAdd.cxx
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \program gspladd
5 
6 \brief Merges XML files containing GENIE cross section splines
7 
8  Syntax :
9  gspladd -f file_list -d directory_list -o output.xml
10  [--message-thresholds xml_file]
11 
12  Options :
13  -f
14  A list of input xml cross-section files. If more than one then
15  separate using commas.
16  -d
17  A list of input directories where to look for xml cross section
18  files. If more than one then separate using commas.
19  -o
20  output xml file
21  --message-thresholds
22  Allows users to customize the message stream thresholds.
23  The thresholds are specified using an XML file.
24  See $GENIE/config/Messenger.xml for the XML schema.
25 
26  Notes :
27  There must be at least 2 files for the merges to work
28 
29  Examples :
30 
31  1) shell% gspladd -f xsec_Fe56.xml,xsec_O16.xml -o xsec.xml
32 
33  will merge xsec_Fe56.xml and xsec_O16.xml into a single file
34  named xsec_all.xml
35 
36  2) shell% gspladd -f xsec_Fe56.xml -d /path,/other_path -o xsec.xml
37 
38  will merge xsec_Fe56.xml with all the xml cross-section files that
39  can be found in the /path and /other_path directories and write-out
40  a single file named xsec_all.xml
41 
42 \author Costas Andreopoulos <constantinos.andreopoulos \at cern.ch>
43  Rutherford Appleton Laboratory
44 
45 \created July 05, 2007
46 
47 \cpright Copyright (c) 2003-2020, The GENIE Collaboration
48  For the full text of the license visit http://copyright.genie-mc.org
49 
50 */
51 //____________________________________________________________________________
52 
53 #include <cassert>
54 #include <sstream>
55 #include <string>
56 #include <vector>
57 
58 #include <TSystem.h>
59 
63 #include "Framework/Utils/RunOpt.h"
69 
70 using std::string;
71 using std::vector;
72 using std::ostringstream;
73 
74 using namespace genie;
75 
76 vector<string> GetAllInputFiles (void);
77 void GetCommandLineArgs (int argc, char ** argv);
78 void PrintSyntax (void);
79 
80 //User-specified options:
81 string gOutFile; ///< output XML file
82 vector<string> gInpFiles; ///< list of input XML files
83 vector<string> gInpDirs; ///< list of input dirs (to look for XML files)
84 vector<string> gAllFiles; ///< list of all input files
85 
86 //____________________________________________________________________________
87 int main(int argc, char ** argv)
88 {
89  GetCommandLineArgs(argc,argv);
90 
91  utils::app_init::MesgThresholds(RunOpt::Instance()->MesgThresholdFiles());
92 
94 
95  vector<string>::const_iterator file_iter = gAllFiles.begin();
96  for( ; file_iter != gAllFiles.end(); ++file_iter) {
97  string filename = *file_iter;
98  LOG("gspladd", pNOTICE) << " ---- >> Loading file : " << filename;
99  XmlParserStatus_t ist = xspl->LoadFromXml(filename, true);
100  assert(ist==kXmlOK);
101  }
102 
103  LOG("gspladd",pDEBUG) << *xspl ;
104 
105  LOG("gspladd", pNOTICE)
106  << " ****** Saving all loaded splines into : " << gOutFile;
107  xspl->SaveAsXml(gOutFile);
108 
109  return 0;
110 }
111 //____________________________________________________________________________
112 vector<string> GetAllInputFiles(void)
113 {
114  vector<string> files;
115 
118 
119  // add all files that were input explictly
120  file_iter = gInpFiles.begin();
121  for( ; file_iter != gInpFiles.end(); ++file_iter) {
122  string filename = *file_iter;
123  files.push_back(filename);
124  } // file_iter
125 
126  // loop over input directories
127  dir_iter = gInpDirs.begin();
128  for( ; dir_iter != gInpDirs.end(); ++dir_iter) {
129  string path = *dir_iter;
130  // get all XML files in this dir
131  vector<string> path_files = utils::system::GetAllFilesInPath(path,"xml");
132  // add these files too
133  file_iter = path_files.begin();
134  for( ; file_iter != path_files.end(); ++file_iter) {
135  string filename = *file_iter;
136  files.push_back(filename);
137  }//file_iter
138  }//dir_iter
139 
140  return files;
141 }
142 //____________________________________________________________________________
143 void GetCommandLineArgs(int argc, char ** argv)
144 {
145  LOG("gspladd", pNOTICE) << "Parsing command line arguments";
146 
147  // Common run options.
149 
150  // Parse run options for this app
151 
152  CmdLnArgParser parser(argc,argv);
153 
154  if( parser.OptionExists('f') ) {
155  LOG("gspladd", pINFO) << "Reading input files";
156  string inpfiles = parser.ArgAsString('f');
157  if(inpfiles.find(",") != string::npos) {
158  // split the comma separated list
159  gInpFiles = utils::str::Split(inpfiles, ",");
160  } else {
161  // there is just one file
162  gInpFiles.push_back(inpfiles);
163  }
164  }
165 
166  if( parser.OptionExists('d') ) {
167  LOG("gspladd", pINFO) << "Reading input directories";
168  string inpdirs = parser.ArgAsString('d');
169  if(inpdirs.find(",") != string::npos) {
170  // split the comma separated list
171  gInpDirs = utils::str::Split(inpdirs, ",");
172  } else {
173  // there is just one directory
174  gInpDirs.push_back(inpdirs);
175  }
176  }
177 
178  if( parser.OptionExists('o') ) {
179  LOG("gspladd", pINFO) << "Reading output file name";
180  gOutFile = parser.ArgAsString('o');
181  } else {
182  LOG("gspladd", pFATAL) << "You must specify an output file name";
183  PrintSyntax();
184  exit(1);
185  }
186 
187  gAllFiles = GetAllInputFiles();
188  if(gAllFiles.size() <= 1) {
189  LOG("gspladd", pFATAL) << "There must be at least 2 input files";
190  PrintSyntax();
191  exit(1);
192  }
193 }
194 //____________________________________________________________________________
195 void PrintSyntax(void)
196 {
197  LOG("gspladd", pNOTICE)
198  << "\n\n" << "Syntax:" << "\n"
199  << " gspladd -f file_list -d directory_list -o output.xml\n"
200  << " [--message-thresholds xml_file]\n";
201 
202 }
203 //____________________________________________________________________________
vector< string > gInpFiles
list of input XML files
Definition: gSplineAdd.cxx:82
vector< string > GetAllInputFiles(void)
Definition: gSplineAdd.cxx:112
vector< string > gAllFiles
list of all input files
Definition: gSplineAdd.cxx:84
string ArgAsString(char opt)
THE MAIN GENIE PROJECT NAMESPACE
Definition: AlgCmp.h:25
std::string string
Definition: nybbler.cc:12
void ReadFromCommandLine(int argc, char **argv)
Definition: RunOpt.cxx:99
#define pFATAL
Definition: Messenger.h:56
vector< string > GetAllFilesInPath(string path, string extension="")
Definition: SystemUtils.cxx:29
struct vector vector
intermediate_table::const_iterator const_iterator
static XSecSplineList * Instance()
string filename
Definition: train.py:213
void PrintSyntax(void)
Definition: gSplineAdd.cxx:195
list files
Definition: languages.py:9
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
Definition: Messenger.h:96
#define pINFO
Definition: Messenger.h:62
vector< string > gInpDirs
list of input dirs (to look for XML files)
Definition: gSplineAdd.cxx:83
int main(int argc, char **argv)
Definition: gSplineAdd.cxx:87
void GetCommandLineArgs(int argc, char **argv)
Definition: gSplineAdd.cxx:143
void SaveAsXml(const string &filename, bool save_init=true) const
static RunOpt * Instance(void)
Definition: RunOpt.cxx:54
vector< string > Split(string input, string delim)
Definition: StringUtils.cxx:36
void MesgThresholds(string inpfile)
Definition: AppInit.cxx:99
Command line argument parser.
#define pNOTICE
Definition: Messenger.h:61
enum genie::EXmlParseStatus XmlParserStatus_t
List of cross section vs energy splines.
XmlParserStatus_t LoadFromXml(const string &filename, bool keep=false)
bool OptionExists(char opt)
was option set?
string gOutFile
output XML file
Definition: gSplineAdd.cxx:81
#define pDEBUG
Definition: Messenger.h:63