From 23ac67a443e272fd5788611b8e636ddaee61ff0a Mon Sep 17 00:00:00 2001 From: Andreas Suter Date: Sat, 11 Nov 2023 18:26:25 +0100 Subject: [PATCH] add an option to musrfit which allows to replace to musrfit_startup.xml by the default one. --- src/classes/PStartupHandler.cpp | 61 ++++++++++++++++++++------------- src/include/PStartupHandler.h | 4 +-- src/musrfit.cpp | 15 ++++++-- 3 files changed, 53 insertions(+), 27 deletions(-) diff --git a/src/classes/PStartupHandler.cpp b/src/classes/PStartupHandler.cpp index d3c79259..a134d199 100644 --- a/src/classes/PStartupHandler.cpp +++ b/src/classes/PStartupHandler.cpp @@ -91,7 +91,7 @@ int parseXmlFile(TSAXParser *saxParser, const char *startup_path_name) /** *

Constructor. Check if the musrfit_startup.xml file is found in some standard search paths */ -PStartupHandler::PStartupHandler() +PStartupHandler::PStartupHandler(bool reset_startup_file) { fStartupFileFound = false; fStartupFilePath = ""; @@ -145,6 +145,17 @@ PStartupHandler::PStartupHandler() } } + // musrfit_startup.xml found. Check if it should be rewritten + if (fStartupFileFound && reset_startup_file) { + std::cout << std::endl; + std::cout << ">> Will only reset the file: '" << fStartupFilePath.Data() << "'."<< std::endl; + std::cout << std::endl; + if (!WriteDefaultStartupFile(reset_startup_file)) { + std::cerr << std::endl << "**ERROR** couldn't re-write " << fStartupFilePath.Data() << "." << std::endl; + return; + } + } + // if musrfit_startup.xml is still not found, will create a default one if (!fStartupFileFound) { std::cout << std::endl << "**INFO** no musrfit_startup.xml file found, will write a default one." << std::endl; @@ -606,33 +617,37 @@ Bool_t PStartupHandler::StartupFileExists(Char_t *fln) //-------------------------------------------------------------------------- // WriteDefaultStartupFile //-------------------------------------------------------------------------- -Bool_t PStartupHandler::WriteDefaultStartupFile() +Bool_t PStartupHandler::WriteDefaultStartupFile(bool reset_startup_file) { - // get home Char_t startup_path_name[256]; - Char_t *home = nullptr; - home = getenv("HOME"); - if (home == nullptr) { - std::cerr << std::endl << "**ERROR** couldn't obtain $HOME." << std::endl; - return false; - } - - // first check that $HOME/.musrfit exists and if NOT create it - struct stat info; - - snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit", home); - if (!stat(startup_path_name, &info)) { - if (!(info.st_mode & S_IFDIR)) - return false; - } else { - if (mkdir(startup_path_name, 0777)) { - std::cerr << std::endl << "**ERROR** couldn't create '" << startup_path_name << "'" << std::endl; + if (reset_startup_file) { // reset the found + snprintf(startup_path_name, sizeof(startup_path_name), "%s", fStartupFilePath.Data()); + } else { // no musrfit_startup.xml found, hence write default under $HOME/.musrfit + // get home + Char_t *home = nullptr; + home = getenv("HOME"); + if (home == nullptr) { + std::cerr << std::endl << "**ERROR** couldn't obtain $HOME." << std::endl; return false; } - } - // set path-name for musrfit_startup.xml - snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit/musrfit_startup.xml", home); + // first check that $HOME/.musrfit exists and if NOT create it + struct stat info; + + snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit", home); + if (!stat(startup_path_name, &info)) { + if (!(info.st_mode & S_IFDIR)) + return false; + } else { + if (mkdir(startup_path_name, 0777)) { + std::cerr << std::endl << "**ERROR** couldn't create '" << startup_path_name << "'" << std::endl; + return false; + } + } + + // set path-name for musrfit_startup.xml + snprintf(startup_path_name, sizeof(startup_path_name), "%s/.musrfit/musrfit_startup.xml", home); + } std::ofstream fout(startup_path_name, std::ofstream::out); if (!fout.is_open()) { diff --git a/src/include/PStartupHandler.h b/src/include/PStartupHandler.h index 40973ce7..f112bbf1 100644 --- a/src/include/PStartupHandler.h +++ b/src/include/PStartupHandler.h @@ -54,7 +54,7 @@ int parseXmlFile(TSAXParser*, const char*); class PStartupHandler : public TObject, public TQObject { public: - PStartupHandler(); + PStartupHandler(bool reset_startup_file=false); virtual ~PStartupHandler(); virtual void OnStartDocument(); // SLOT @@ -97,7 +97,7 @@ class PStartupHandler : public TObject, public TQObject PIntVector fColorList; ///< color list Bool_t StartupFileExists(Char_t *fln); - Bool_t WriteDefaultStartupFile(); + Bool_t WriteDefaultStartupFile(bool reset_startup_file=false); ClassDef(PStartupHandler, 1) }; diff --git a/src/musrfit.cpp b/src/musrfit.cpp index 490f6ebd..68f61a00 100644 --- a/src/musrfit.cpp +++ b/src/musrfit.cpp @@ -121,6 +121,12 @@ void musrfit_syntax() std::cout << std::endl << " -u, --use-no-of-threads :"; std::cout << std::endl << " : number of threads to be used (OpenMP). Needs to be <= max. number of cores."; std::cout << std::endl << " If OpenMP is enable, the maximal number of cores is used, if it is not limited by this option."; + std::cout << std::endl << " -r, --reset: reset startup musrfit_startup.xml, i.e. rewrite a default, and quit."; + std::cout << std::endl << " The order of which musrfit_startup.xml is reset is:"; + std::cout << std::endl << " (i) if present in the current dir."; + std::cout << std::endl << " (ii) if present under $HOME/.musrfit/"; + std::cout << std::endl << " (iii) if present under $MUSRFITPATH/"; + std::cout << std::endl << " (iv) if present under $ROOTSYS/"; std::cout << std::endl << " --dump is writing a data file with the fit data and the theory"; std::cout << std::endl << " can be 'ascii', 'root'"; std::cout << std::endl << " --timeout : overwrites to predefined timeout of " << timeout << " (sec)."; @@ -442,6 +448,7 @@ int main(int argc, char *argv[]) bool chisq_only = false; bool title_from_data_file = false; bool timeout_enabled = true; + bool reset_startup_file = false; PStartupOptions startup_options; startup_options.writeExpectedChisq = false; startup_options.estimateN0 = false; @@ -568,6 +575,8 @@ int main(int argc, char *argv[]) show_syntax = true; break; } + } else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--reset")) { + reset_startup_file = true; } else if (!strcmp(argv[i], "--timeout")) { if (i> musrfit **ERROR** no msr-file present!" << std::endl; } @@ -618,7 +627,9 @@ int main(int argc, char *argv[]) // read startup file char startup_path_name[128]; std::unique_ptr saxParser = std::make_unique(); - std::unique_ptr startupHandler = std::make_unique(); + std::unique_ptr startupHandler = std::make_unique(reset_startup_file); + if (reset_startup_file) // only rewrite musrfit_startup.xml has been requested + return PMUSR_SUCCESS; if (!startupHandler->StartupFileFound()) { std::cerr << std::endl << ">> musrfit **WARNING** couldn't find " << startupHandler->GetStartupFilePath().Data(); std::cerr << std::endl;