18 #include "libxml/xmlmemory.h"
19 #include "libxml/parser.h"
37 using std::ostringstream;
39 using namespace genie;
45 config_pool.
Print(stream);
55 LOG(
"AlgConfigPool",
pERROR) <<
"Could not load XML config file";
70 map<string, Registry *>::iterator citer;
72 string key = citer->first;
101 <<
"AlgConfigPool late initialization: Loading all XML config. files";
107 <<
"Global parameter lists not available "
108 "This can be normal if you are running "
109 "Comparisons/Reweight in some cases";
116 <<
"Master config file not available";
121 SLOG(
"AlgConfigPool",
pWARN ) <<
"Tune generator List not available" ;
126 map<string, string>::const_iterator conf_file_iter;
129 conf_file_iter !=
fConfigFiles.end(); ++conf_file_iter) {
131 string alg_name = conf_file_iter->first;
132 string file_name = conf_file_iter->second;
135 << setfill(
'.') << setw(40) << alg_name <<
" -> " << file_name;
139 <<
"*** GENIE XML config file " << full_path;
143 <<
"Error in loading config sets for algorithm = " << alg_name;
158 bool is_accessible = ! (gSystem->AccessPathName(
fMasterConfig.c_str() ));
159 if (!is_accessible) {
161 <<
"The XML doc doesn't exist! (filename : " <<
fMasterConfig <<
")";
168 <<
"The XML doc can't be parsed! (filename : " <<
fMasterConfig <<
")";
172 xmlNodePtr xml_root = xmlDocGetRootElement(xml_doc);
175 <<
"The XML doc is empty! (filename : " <<
fMasterConfig <<
")";
180 if( xmlStrcmp(xml_root->name, (
const xmlChar *)
"genie_config") ) {
182 <<
"The XML doc has invalid root element! "
190 xmlNodePtr xml_ac = xml_root->xmlChildrenNode;
191 while (xml_ac != NULL) {
192 if( (!xmlStrcmp(xml_ac->name, (
const xmlChar *)
"config")) ) {
197 xmlNodeListGetString(xml_doc, xml_ac->xmlChildrenNode, 1));
199 pair<string, string> alg_conf(alg_name, config_file);
202 xml_ac = xml_ac->next;
211 if (std::getenv(
"GENIE_REWEIGHT")) {
213 return main && rew_main;
223 SLOG(
"AlgConfigPool",
pINFO) <<
"Loading global parameter lists";
229 string key_prefix =
"GlobalParameterList";
232 return this->
LoadRegistries(key_prefix, glob_params,
"global_param_list");
239 SLOG(
"AlgConfigPool",
pINFO) <<
"Loading Common " << file_id <<
" lists";
242 std::string xml_name =
"Common" + file_id +
".xml" ;
246 string key_prefix =
"Common" + file_id +
"List";
249 if ( ! this->
LoadRegistries(key_prefix, full_path,
"common_"+file_id+
"_list") ) {
251 SLOG(
"AlgConfigPool",
pERROR) <<
"Failed to load Common " << file_id ;
262 SLOG(
"AlgConfigPool",
pINFO) <<
"Loading Tune Gerator List";
268 string key_prefix =
"TuneGeneratorList";
271 return this->
LoadRegistries(key_prefix, generator_list_file,
"tune_generator_list");
281 string key_prefix = alg_name;
288 string key_prefix,
string file_name,
string root)
292 SLOG(
"AlgConfigPool",
pDEBUG) <<
"[-] Loading registries:";
294 bool is_accessible = ! (gSystem->AccessPathName(file_name.c_str()));
295 if (!is_accessible) {
297 <<
"The XML doc doesn't exist! (filename : " << file_name <<
")";
301 xmlDocPtr xml_doc = xmlParseFile( file_name.c_str() );
304 <<
"The XML document can't be parsed! (filename : " << file_name <<
")";
308 xmlNodePtr xml_cur = xmlDocGetRootElement( xml_doc );
311 <<
"The XML document is empty! (filename : " << file_name <<
")";
315 if( xmlStrcmp(xml_cur->name, (
const xmlChar *) root.c_str()) ) {
317 <<
"The XML document has invalid root element! "
318 <<
"(filename : " << file_name <<
")";
319 xmlFreeNode(xml_cur);
325 xml_cur = xml_cur->xmlChildrenNode;
326 while (xml_cur != NULL) {
328 if( (!xmlStrcmp(xml_cur->name, (
const xmlChar *)
"param_set")) ) {
335 key << key_prefix <<
"/" << param_set;
343 xmlNodePtr xml_param = xml_cur->xmlChildrenNode;
344 while (xml_param != NULL) {
345 if( (!xmlStrcmp(xml_param->name, (
const xmlChar *)
"param")) ) {
355 xmlNodeListGetString(
356 xml_doc, xml_param->xmlChildrenNode, 1));
359 if ( param_type.find(
"vec-" ) == 0 ) {
361 param_type = param_type.substr( 4 ) ;
367 else if (param_type.find(
"mat-" ) == 0) {
368 param_type = param_type.substr( 4 ) ;
369 LOG(
"AlgConfigPool",
pNOTICE) <<
"Liang Liu" << param_type ;
371 if(!importfile.compare(
"false")){
374 this ->
AddParameterMatrix(config, param_type, param_name, param_value, rowdelim, coldelim);
376 else if(!importfile.compare(
"true")){
381 param_type, param_name,
384 xml_param = xml_param->next;
387 xmlFreeNode(xml_param);
391 pair<string, Registry *> single_reg(key.str(), config);
394 SLOG(
"AlgConfigPool",
pDEBUG) <<
" |---o " << key.str();
396 xml_cur = xml_cur->next;
399 xmlFreeNode(xml_cur);
407 const string & delim ) {
416 <<
"Adding Parameter Vector [" << pt <<
"]: Key = "
417 << pn <<
" -> Value = " << pv;
423 std::stringstream n_value ;
424 n_value << bits.size() ;
428 for (
unsigned int i = 0 ; i < bits.size() ; ++i ) {
442 const string & rowdelim,
const string & coldelim ) {
453 if(!rowdelim.compare(coldelim) || rowdelim.empty() || coldelim.empty()) {
454 LOG(
"AlgConfigPool",
pFATAL) <<
"row and column have wrong delims: " << rowdelim <<
" " << coldelim ;
458 <<
"Adding Parameter Matrix [" << pt <<
"]: Key = "
459 << pn <<
" -> Value = " << pv;
466 unsigned int n_row = 0, n_col = 0;
467 std::stringstream r_value ;
468 r_value << mat_row.size() ;
469 n_row = mat_row.size() ;
473 for (
unsigned int i = 0 ; i < mat_row.size() ; ++i ) {
477 std::stringstream c_value ;
478 c_value << bits.size();
483 if(n_col != bits.size()){
484 LOG(
"AlgConfigPool",
pFATAL) <<
"wrong size of matrix in row: " << i;
489 for (
unsigned int j = 0 ; j < bits.size() ; ++j ) {
497 return n_row * n_col;
512 char * GENIE_PATH = std::getenv(
"GENIE");
513 string filepath = std::string(GENIE_PATH) +
"/" + pv;
514 std::ifstream file(filepath);
515 if (!file.is_open()) {
516 throw std::runtime_error(
"Could not open file");
520 int n_row = 0, n_col = 0;
522 while (getline(file, line)) {
523 size_t start = line.find_first_not_of(
" \t");
524 std::string trimmedLine = (start == std::string::npos) ?
"" : line.substr(start);
525 if(trimmedLine.empty())
continue;
526 if(!trimmedLine.empty() && trimmedLine[0] ==
'%')
continue;
528 std::istringstream iss(line);
531 while (iss >> value) {
540 LOG(
"AlgConfigPool",
pFATAL) <<
"wrong size of matrix in row: " << i_row;
547 std::stringstream r_value ;
551 std::stringstream c_value ;
562 string ptype,
string pname,
string pvalue)
568 <<
"Adding Parameter [" << ptype <<
"]: Key = "
569 << pname <<
" -> Value = " << pvalue;
571 bool isRootObjParam = (strcmp(ptype.c_str(),
"h1f") == 0) ||
572 (strcmp(ptype.c_str(),
"Th2f") == 0) ||
573 (strcmp(ptype.c_str(),
"tree") == 0);
574 bool isBasicParam = (strcmp(ptype.c_str(),
"int") == 0) ||
575 (strcmp(ptype.c_str(),
"bool") == 0) ||
576 (strcmp(ptype.c_str(),
"double") == 0) ||
577 (strcmp(ptype.c_str(),
"string") == 0) ||
578 (strcmp(ptype.c_str(),
"alg") == 0);
585 <<
"Parameter [" << ptype <<
"]: Key = " << pname
586 <<
" -> Value = " << pvalue <<
" could not be added";
592 Registry * r,
string ptype,
string pname,
string pvalue)
596 if (ptype==
"double") {
597 RgDbl item = (double) atof(pvalue.c_str());
600 else if (ptype==
"int") {
601 RgInt item = (int) atoi(pvalue.c_str());
604 else if (ptype==
"bool") {
605 if (pvalue==
"true" ) r->
Set(key,
true );
606 else if (pvalue==
"TRUE" ) r->
Set(key,
true );
607 else if (pvalue==
"1" ) r->
Set(key,
true );
608 else if (pvalue==
"false") r->
Set(key,
false);
609 else if (pvalue==
"FALSE") r->
Set(key,
false);
610 else if (pvalue==
"0" ) r->
Set(key,
false);
613 <<
"Could not set bool param: " << key;
616 else if (ptype==
"string") {
620 else if (ptype==
"alg") {
623 if (algv.size()==2) {
627 else if (algv.size()==1) {
632 <<
"Unrecognized algorithm id: " << pvalue;
635 RgAlg item(name,config);
640 <<
"Config. parameter: " << key
641 <<
"has unrecognized type: " << ptype;
646 Registry * r,
string ptype,
string pname,
string pvalue)
652 if(rootobjv.size() != 2) {
654 <<
"ROOT objects are added in XML config files as: "
655 <<
"object-name@file-name. Wrong syntax in: [" << pvalue <<
"]";
657 <<
"Parameter [" << ptype <<
"]: Key = " << pname
658 <<
" -> Value = " << pvalue <<
" could not be added";
661 string rootobj = rootobjv[0];
662 string rootfile = rootobjv[1];
664 TFile f(rootfile.c_str(),
"read");
667 TH1F * h = (TH1F*) f.Get(rootobj.c_str());
669 TH1F * ch =
new TH1F(*h);
674 <<
"No TH1F named = " << rootobj <<
" in ROOT file = " << rootfile;
676 }
else if (ptype==
"h2f") {
677 TH2F * h2 = (TH2F*) f.Get(rootobj.c_str());
679 TH2F * ch2 =
new TH2F(*h2);
680 ch2->SetDirectory(0);
684 <<
"No TH2F named = " << rootobj <<
" in ROOT file = " << rootfile;
686 }
else if (ptype==
"tree") {
687 TTree * t = (TTree*) f.Get(rootobj.c_str());
690 TTree * ct = t->CopyTree(
"1");
694 <<
"No TTree named = " << rootobj <<
" in ROOT file = " << rootfile;
702 string key = algorithm->
Id().
Key();
708 string key = algid.
Key();
714 AlgId id(alg_name,param_set);
715 string key =
id.Key();
721 LOG(
"AlgConfigPool",
pDEBUG) <<
"Searching for registry with key " << key;
724 map<string, Registry *>::const_iterator config_entry =
726 return config_entry->second;
728 LOG(
"AlgConfigPool",
pDEBUG) <<
"No config registry for key " << key;
736 string glob_param_set = (gSystem->Getenv(
"GUSERPHYSOPT")) ?
737 string(gSystem->Getenv(
"GUSERPHYSOPT")) :
"Default";
739 key <<
"GlobalParameterList/" << glob_param_set;
748 key <<
"Common" << file_id <<
"List/" << set_name;
761 key <<
"TuneGeneratorList/Default";
773 string frame(100,
'~');
775 typedef map<string, Registry *>::const_iterator sregIter;
776 typedef map<string, Registry *>::size_type sregSize;
781 << endl <<
"Algorithm Configuration Pool ("
782 << size <<
" configuration sets found)"
783 << endl << frame << endl;
787 const Registry & reg = *(iter->second);
788 stream << iter->first;
790 stream << frame << endl;
static string BuildParamMatKey(const std::string &comm_name, unsigned int i, unsigned int j)
vector< string > fConfigKeyList
list of all available configuration keys
void DummyMethodAndSilentCompiler()
bool LoadGlobalParamLists(void)
void Print(ostream &stream) const
static string BuildParamMatColSizeKey(const std::string &comm_name)
string TrimSpaces(xmlChar *xmls)
A singleton class holding all configuration registries built while parsing all loaded XML configurati...
Algorithm abstract base class.
void AddConfigParameter(Registry *r, string pt, string pn, string pv)
bool LoadSingleAlgConfig(string alg_name, string file_name)
Registry * CommonList(const string &file_id, const string &set_name) const
int main(int argc, char **argv)
int AddParameterMatrix(Registry *r, string pt, string pn, string pv, const string &rowdelim, const string &coldelim)
void AddRootObjParameter(Registry *r, string pt, string pn, string pv)
static AlgConfigPool * fInstance
#define LOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a string (using the FILE...
static string BuildParamMatRowSizeKey(const std::string &comm_name)
Registry * TuneGeneratorList(void) const
string GetXMLFilePath(string basename)
void AddBasicParameter(Registry *r, string pt, string pn, string pv)
bool LoadTuneGeneratorList(void)
void Lock(void)
locks the registry
void SetName(string name)
set the registry name
const vector< string > & ConfigKeyList(void) const
Registry * GlobalParameterList(void) const
string TrimSpaces(string input)
Algorithm ID (algorithm name + configuration set name)
int AddParameterVector(Registry *r, string pt, string pn, string pv, const string &delim=";")
bool LoadMasterConfigs(void)
bool LoadRegistries(string key_base, string file_name, string root)
virtual const AlgId & Id(void) const
Get algorithm ID.
map< string, string > fConfigFiles
algorithm -> XML config file
vector< string > Split(string input, string delim)
static string BuildParamVectSizeKey(const std::string &comm_name)
ostream & operator<<(ostream &stream, const AlgConfigPool &config_pool)
A registry. Provides the container for algorithm configuration parameters.
map< string, Registry * > fRegistryPool
algorithm/param_set -> Registry
bool LoadMasterConfig(std::string configname)
Registry * FindRegistry(string key) const
#define SLOG(stream, priority)
A macro that returns the requested log4cpp::Category appending a short string (using the FUNCTION and...
string GetAttribute(xmlNodePtr xml_cur, string attr_name)
void Set(RgIMapPair entry)
static string BuildParamVectKey(const std::string &comm_name, unsigned int i)
bool LoadCommonLists(const string &file_id)
static AlgConfigPool * Instance()
string fMasterConfig
lists config files for all algorithms