musrfit 1.10.0
msr2data.cpp
Go to the documentation of this file.
1/***************************************************************************
2
3 msr2data.cpp
4
5 Author: Bastian M. Wojek / Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8***************************************************************************/
9
10/***************************************************************************
11 * Copyright (C) 2009-2026 by Bastian M. Wojek / Andreas Suter *
12 * andreas.suter@psi.ch *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
29
30// note: msr2data is on purpose implemented in a way that shows string handling can be done solely
31// using std::string, boost and related standard C++ features
32// This implies, however, occasionally strange constructs when interoperating with PMusr-classes
33// which mostly rely on ROOT's TString.
34
35#ifdef HAVE_CONFIG_H
36#include "config.h"
37#endif
38
39#ifdef HAVE_GIT_REV_H
40#include "git-revision.h"
41#endif
42
43#include "PMusr.h"
44#include "PMsr2Data.h"
45
46#include <algorithm>
47#include <sstream>
48#include <iostream>
49#include <fstream>
50#include <cstdlib>
51#include <limits>
52#include <string>
53#include <memory>
54
55#include <boost/algorithm/string.hpp>
56#include <boost/algorithm/string/case_conv.hpp> // for to_lower() in std::string
57
58#include <boost/lexical_cast.hpp> // for atoi-replacement
59
60//--------------------------------------------------------------------------
70bool isNumber(const std::string &s)
71{
72 unsigned int number(0);
73 try {
74 number = boost::lexical_cast<unsigned int>(s);
75 return true;
76 }
77 catch(boost::bad_lexical_cast &) {
78 return false;
79 }
80}
81
82//--------------------------------------------------------------------------
87{
88 std::cout << std::endl << "usage 0: msr2data [--version] | [--help]";
89 std::cout << std::endl << "usage 1: msr2data <run> <extension> options";
90 std::cout << std::endl << "usage 2: msr2data <run1> <run2> <extension> options";
91 std::cout << std::endl << "usage 3: msr2data \\[<runList>\\] <extension> options";
92 std::cout << std::endl << "usage 4: msr2data <runListFileName> <extension> options";
93 std::cout << std::endl;
94 std::cout << std::endl << " <runList> can be:";
95 std::cout << std::endl << " (i) <run0>, <run1>, <run2>, ... <runN> : run numbers, e.g. 123 124";
96 std::cout << std::endl << " (ii) <run0>-<runN> : a range, e.g. 123-125 -> 123 124 125";
97 std::cout << std::endl << " (iii) <run0>:<runN>:<step> : a sequence, e.g. 123:127:2 -> 123 125 127";
98 std::cout << std::endl << " <step> will give the step width and has to be a positive number!";
99 std::cout << std::endl << " a <runList> can also combine (i)-(iii), e.g. 123 128-130 133, etc.";
100 std::cout << std::endl << " <runListFileName> : an ASCII file containing a list of run numbers and optional";
101 std::cout << std::endl << " external parameters is passed to msr2data. For details see";
102 std::cout << std::endl << " the online documentation: http://lmu.web.psi.ch/musrfit/user/MUSR/Msr2Data.html";
103 std::cout << std::endl << " <extension> : msr-file extension, e.g. _tf_h13 for the file name 8472_tf_h13.msr";
104 std::cout << std::endl;
105 std::cout << std::endl << "options:";
106 std::cout << std::endl << " -o<outputfile> : specify the name of the DB or column-data output file; default: out.db/out.dat";
107 std::cout << std::endl << " if the option '-o none' is used, no output file will be written.";
108 std::cout << std::endl << " new : before writing a new output file, delete the contents of any existing file with the same name";
109 std::cout << std::endl << " data : instead of to a DB file the data are written to a simple column structure";
110 std::cout << std::endl << " header : force writing of the file header to the output file";
111 std::cout << std::endl << " noheader : no file header is written to the output file";
112 std::cout << std::endl << " If either none or both of the header options are given, the file header will be written";
113 std::cout << std::endl << " if a new file is created, but not if the output file exists already!";
114 std::cout << std::endl << " nosummary : no additional data from the run data file is written to the output file";
115 std::cout << std::endl << " paramList <param> : option used to select the parameters which shall be exported.";
116 std::cout << std::endl << " <param> is a list of parameter numbers to be exported. Allowed lists are:";
117 std::cout << std::endl << " 1-16 will export parameters 1 to 16. 1 3 5 will export parameters 1 3 5.";
118 std::cout << std::endl << " A combination of both is possible, e.g. 1-16 19 31 62, and so on.";
119 std::cout << std::endl << " fit : invoke musrfit to fit the specified runs";
120 std::cout << std::endl << " All msr input files are assumed to be present, none is newly generated!";
121 std::cout << std::endl << " fit-<template>! : generate msr files for the runs to be processed from the <template> run";
122 std::cout << std::endl << " and call musrfit for fitting these runs";
123 std::cout << std::endl << " fit-<template> : same as above, but the <template> run is only used for the first file creation---";
124 std::cout << std::endl << " the successive files are generated using the musrfit output from the preceding runs";
125 std::cout << std::endl << " msr-<template> : same as above without calling musrfit";
126 std::cout << std::endl << " In case any fitting option is present, this option is ignored!";
127 std::cout << std::endl << " -k, --keep-mn2-output : if fitting is used, pass the option --keep-mn2-output to musrfit";
128 std::cout << std::endl << " -t, --title-from-data-file : if fitting is used, pass the option --title-from-data-file to musrfit";
129 std::cout << std::endl << " -e, --estimateN0: estimate N0 for single histogram fits.";
130 std::cout << std::endl << " -y, --yaml: write fit results (MINUIT2.OUTPUT) into a yaml-file. Output <msr-file>.yaml";
131 std::cout << std::endl << " -p, --per-run-block-chisq: will per run block chisq to the msr-file.";
132 std::cout << std::endl;
133 std::cout << std::endl << " global : switch on the global-fit mode";
134 std::cout << std::endl << " Within that mode all specified runs will be united in a single msr file!";
135 std::cout << std::endl << " The fit parameters can be either run specific or common to all runs.";
136 std::cout << std::endl << " For a complete description of this feature please refer to the manual.";
137 std::cout << std::endl;
138 std::cout << std::endl << " global+[!] : operate in the global-fit mode, however, in case a global input file is created";
139 std::cout << std::endl << " all specified runs are pre-analyzed first one by one using the given template.";
140 std::cout << std::endl << " For the generation of the global input file, the run-specific parameter values are taken";
141 std::cout << std::endl << " from this pre-analysis for each run---they are not just copied from the template.";
142 std::cout << std::endl << " The specification of '!' determines which fit mode (see above) is used for this pre-analysis.";
143 std::cout << std::endl;
144 std::cout << std::endl << " Typical examples:";
145 std::cout << std::endl;
146 std::cout << std::endl << " msr2data 2047 2050 _tf_histo fit-2046";
147 std::cout << std::endl << " will use 2046_tf_histo.msr as templete, and subsequently generating 2047_tf_histo.msr until";
148 std::cout << std::endl << " 2050_tf_histo.msr and fit them.";
149 std::cout << std::endl;
150 std::cout << std::endl << " msr2data 2047 2050 _tf_histo msr-2046";
151 std::cout << std::endl << " will use 2046_tf_histo.msr as templete, and subsequently generating 2047_tf_histo.msr until";
152 std::cout << std::endl << " 2050_tf_histo.msr, but NO fitting will be done.";
153 std::cout << std::endl;
154 std::cout << std::endl << " msr2data 2046 2050 _tf_histo -o fitParam.db";
155 std::cout << std::endl << " will collect the fit parameters from runs 2046-2050 (msr-files 2046_tf_histo.msr etc.) and";
156 std::cout << std::endl << " write them to the file fitParam.db (DB-format).";
157 std::cout << std::endl;
158 std::cout << std::endl << " msr2data [2047:2053:2 2056] _tf_histo fit-2045";
159 std::cout << std::endl << " will use 2045_tf_histo.msr as templete, and subsequently generating msr-files from the run-list:";
160 std::cout << std::endl << " 2047 2049 2051 2053 2056 (2047_tf_histo.msr etc.) and fit them.";
161 std::cout << std::endl;
162 std::cout << std::endl << " msr2data 2046 2058 _tf_histo paramList 1-12 data -o fitParam.dat";
163 std::cout << std::endl << " will export the parameters number 1 trough 12 in a column like fashion of the runs 2046 to 2058,";
164 std::cout << std::endl << " collected form the msr-files 2046_tf_histo.msr and so on.";
165 std::cout << std::endl;
166 std::cout << std::endl << " For further information please refer to";
167 std::cout << std::endl << " http://lmu.web.psi.ch/musrfit/user/html/msr2data.html#msr2data";
168 std::cout << std::endl << std::endl;
169}
170
171//--------------------------------------------------------------------------
182std::string msr2data_validArguments(const std::vector<std::string> &arg)
183{
184 std::string word;
185
186 for (std::vector<std::string>::const_iterator iter(arg.begin()); iter != arg.end(); ++iter) {
187 if ( (!iter->compare("header")) || (!iter->compare("noheader")) || (!iter->compare("nosummary")) \
188 || (!iter->substr(0,3).compare("fit")) || (!iter->compare("-k")) || (!iter->compare("--keep-mn2-output")) \
189 || (!iter->compare("-t")) || (!iter->compare("--title-from-data-file")) \
190 || (!iter->compare("-e")) || (!iter->compare("--estimateN0")) \
191 || (!iter->compare("-y") || (!iter->compare("--yaml"))) \
192 || (!iter->compare("-p")) || (!iter->compare("--per-run-block-chisq")) \
193 || (!iter->compare("data")) || (!iter->substr(0,4).compare("msr-")) || (!iter->compare("global")) \
194 || (!iter->compare("global+")) || (!iter->compare("global+!")) || (!iter->compare("new")) \
195 || !iter->compare("paramList") )
196 word.clear();
197 else if (!iter->substr(0,2).compare("-o")) {
198 word.clear();
199 if (!iter->compare("-o")) {
200 if (++iter == arg.end())
201 break;
202 else
203 continue;
204 }
205 } else {
206 word = *iter;
207 break;
208 }
209 }
210
211 return word;
212
213}
214
215//--------------------------------------------------------------------------
226std::string msr2data_outputfile(std::vector<std::string> &arg, bool db = true)
227{
228 std::string outputFile;
229 if (db)
230 outputFile = "out.db";
231 else
232 outputFile = "out.dat";
233
234 std::vector<std::string>::iterator iterNext(arg.begin());
235 for (std::vector<std::string>::iterator iter(arg.begin()); iter != arg.end(); ++iter) {
236 iterNext = iter + 1;
237 if (!iter->substr(0,2).compare("-o")) {
238 if (!iter->compare("-o")) {
239 if ((iterNext != arg.end()) && (iterNext->compare("header")) && (iterNext->compare("noheader")) && (iterNext->compare("nosummary")) \
240 && (iterNext->substr(0,3).compare("fit")) && (iterNext->compare("-k")) && (iterNext->compare("-t")) \
241 && (iterNext->compare("-e")) && (iterNext->compare("-p")) \
242 && (iterNext->compare("data")) && (iterNext->substr(0,3).compare("msr")) && (iterNext->compare("global")) \
243 && (iterNext->compare("global+")) && (iterNext->compare("global+!")) && (iterNext->compare("new"))) {
244 outputFile = *iterNext;
245 arg.erase(iterNext);
246 arg.erase(iter);
247 break;
248 } else {
249 std::cout << std::endl;
250 std::cout << ">> msr2data: **WARNING** You did not specify an output file! The default one (" << outputFile << ") will be used." << std::endl;
251 arg.erase(iter);
252 break;
253 }
254 } else {
255 outputFile = iter->substr(2);
256 arg.erase(iter);
257 break;
258 }
259 }
260 }
261
262 return outputFile;
263}
264
265//--------------------------------------------------------------------------
276bool msr2data_useOption(std::vector<std::string> &arg, const std::string &s)
277{
278 bool option(true);
279
280 std::vector<std::string>::iterator iter;
281 iter = find(arg.begin(), arg.end(), s);
282
283 if (iter != arg.end()) {
284 option = false;
285 arg.erase(iter);
286 }
287 return option;
288}
289
290//--------------------------------------------------------------------------
305int msr2data_doFitting(std::vector<std::string> &arg, bool &chainfit)
306{
307 int temp(0);
308
309 std::string s;
310 std::vector<std::string>::iterator iter(arg.begin());
311 while (iter != arg.end()) {
312 if (!iter->compare("fit")) { // fit found
313 if (temp) { // temp already found previously
314 return -2; // fatal error - another fit-<temp> option is specified
315 }
316 temp = -1; // fit only, do not prepare input files
317 chainfit = false;
318 iter = arg.erase(iter);
319 }
320 else if (!iter->substr(0,4).compare("fit-")) { // 'fit-' found
321 if (temp) {
322 return -2; // fatal error - another fit option is specified
323 }
324 s = iter->substr(4);
325 std::string::size_type loc = s.rfind('!');
326 if (loc != std::string::npos) {
327 chainfit = false;
328 s.erase(loc);
329 }
330 else
331 chainfit = true;
332 try {
333 temp = boost::lexical_cast<int>(s);
334 arg.erase(iter);
335 }
336 catch(boost::bad_lexical_cast &) {
337 return -3; // the specified template is no number
338 }
339 } else {
340 ++iter;
341 }
342 }
343
344 return temp;
345}
346
347//--------------------------------------------------------------------------
358int msr2data_doInputCreation(std::vector<std::string> &arg, bool &inputOnly)
359{
360 int temp(0);
361
362 std::string s;
363 for (std::vector<std::string>::iterator iter(arg.begin()); iter != arg.end(); ++iter) {
364 if (!iter->substr(0,4).compare("msr-")) {
365 s = iter->substr(4);
366 try {
367 temp = boost::lexical_cast<int>(s);
368 arg.erase(iter);
369 }
370 catch(boost::bad_lexical_cast &) {
371 temp = -3; // the specified template is no number
372 }
373 inputOnly = true;
374 break;
375 }
376 }
377
378 return temp;
379}
380
381//--------------------------------------------------------------------------
386int msr2data_paramList(std::vector<std::string> &arg, std::vector<unsigned int> &paramList)
387{
388 paramList.clear(); // make sure paramList is empty
389
390 unsigned int idx=0;
391 // check if paramList tag is present
392 for (unsigned int i=0; i<arg.size(); i++) {
393 if (!arg[i].compare("paramList")) {
394 idx = i+1;
395 break;
396 }
397 }
398
399 if (idx == 0) { // paramList tag NOT present
400 return 0;
401 }
402
403 // make sure there are parameter list elements to follow
404 if (idx == arg.size()) {
405 std::cerr << std::endl << "**ERROR** found paramList without any arguments!" << std::endl;
407 return -1;
408 }
409
410 // paramList tag present and further elements present: collect them
411 std::vector<std::string> str;
412 unsigned int idx_end=0;
413 size_t pos=std::string::npos;
414 for (unsigned int i=idx; i<arg.size(); i++) {
415 pos = arg[i].find("-");
416 if (pos == 0) { // likely something like -o, -k, etc.
417 idx_end = i;
418 break;
419 } else if (pos != std::string::npos) { // looks like a parameter list like n0-n1
420 boost::split(str, arg[i], boost::is_any_of("-"));
421 if (str.size() != 2) { // something is wrong, since the structure n0-n1 is expected
422 std::cerr << std::endl << "**ERROR** found token " << arg[i] << " in paramList command which cannot be handled." << std::endl;
424 return -1;
425 }
426 if (!str[0].compare("fit") || !str[0].compare("msr")) {
427 idx_end = i;
428 break;
429 }
430 if (!isNumber(str[0]) || !isNumber(str[1])) {
431 std::cerr << std::endl << "**ERROR** found token " << arg[i] << " in paramList command which cannot be handled." << std::endl;
433 return -1;
434 }
435 unsigned int start=boost::lexical_cast<unsigned int>(str[0]);
436 unsigned int end=boost::lexical_cast<unsigned int>(str[1]);
437 for (unsigned int j=start; j<=end; j++)
438 paramList.push_back(j);
439 } else if (isNumber(arg[i])) { // a single number
440 paramList.push_back(boost::lexical_cast<unsigned int>(arg[i]));
441 } else { // likely the next argument not related to paramList
442 idx_end = i;
443 break;
444 }
445 }
446 if (idx_end == 0)
447 idx_end = arg.size();
448
449 // remove all the paramList arguments for arg
450 arg.erase(arg.begin()+idx-1, arg.begin()+idx_end);
451
452 // go through the parameter list and make sure the values are unique
453 for (unsigned int i=0; i<paramList.size(); i++) {
454 for (unsigned int j=i+1; j<paramList.size(); j++) {
455 if (paramList[i] == paramList[j]) {
456 std::cerr << std::endl << "**ERROR** the parameter list numbers have to be unique. Found " << paramList[i] << " at least 2 times." << std::endl;
458 return -1;
459 }
460 }
461 }
462
463 return paramList.size();
464}
465
466//--------------------------------------------------------------------------
478int main(int argc, char *argv[])
479{
480 // check if version dump is wanted
481 if (argc == 2) {
482 if (!strcmp(argv[1], "--help")) {
484 return 0;
485 } else if (!strcmp(argv[1], "--version")) {
486#ifdef HAVE_CONFIG_H
487#ifdef HAVE_GIT_REV_H
488 std::cout << std::endl << "msr2data version: " << PACKAGE_VERSION << ", git-branch: " << GIT_BRANCH << ", git-rev: " << GIT_CURRENT_SHA1 << " (" << BUILD_TYPE << "), ROOT version: " << ROOT_VERSION_USED << std::endl << std::endl;
489#else
490 std::cout << std::endl << "msr2data version: " << PACKAGE_VERSION << " (" << BUILD_TYPE << "), ROOT version: " << ROOT_VERSION_USED << std::endl << std::endl;
491#endif
492#else
493#ifdef HAVE_GIT_REV_H
494 std::cout << std::endl << "msr2data git-branch: " << GIT_BRANCH << ", git-rev: " << GIT_CURRENT_SHA1 << std::endl << std::endl;
495#else
496 std::cout << std::endl << "msr2data version: unknown." << std::endl << std::endl;
497#endif
498#endif
499 return 0;
500 } else {
502 return 0;
503 }
504 }
505
506 // check the number of arguments
507 if (argc < 2) {
509 return 0;
510 }
511
512 // use a string-vector for the arguments to get rid of char* as far as possible...
513 std::vector<std::string> arg;
514 for (int i(1); i<argc; ++i) {
515 arg.push_back(argv[i]);
516 }
517
518 unsigned int runTAG;
519 std::vector<unsigned int> run_vec;
520 std::string run_list;
521 std::string msrExtension;
522 std::vector<unsigned int> param_vec;
523
524 try {
525 if (arg[0].at(0) == '[') { // In case a list of runs is given by [...]
526 runTAG = 1;
527
528 unsigned int firstRunNumberInArg(0), lastRunNumberInArg(0);
529 unsigned int rightbracket(std::numeric_limits<unsigned int>::max());
530
531 if (!arg[0].compare("["))
532 firstRunNumberInArg = 1;
533 else
534 arg[0]=arg[0].substr(1);
535
536 for (unsigned int i(firstRunNumberInArg); i<arg.size(); ++i) {
537 std::string::size_type loc = arg[i].rfind(']');
538 if ( loc != std::string::npos ) {
539 rightbracket = i;
540 if (!arg[i].compare("]") && i > 0)
541 lastRunNumberInArg = i-1;
542 else {
543 arg[i]=arg[i].substr(0,loc);
544 lastRunNumberInArg = i;
545 }
546 break;
547 }
548 }
549 if (rightbracket == std::numeric_limits<unsigned int>::max()) {
550 std::cerr << std::endl;
551 std::cerr << ">> msr2data: **ERROR** You used the list specification without closing bracket (])! Quitting now." << std::endl;
552 return 0;
553 }
554
555 // generate run_list string
556 for (unsigned int i(firstRunNumberInArg); i<=lastRunNumberInArg; ++i) {
557 run_list += arg[i] + " ";
558 }
559 // parse run_list string
560 std::unique_ptr<PStringNumberList> nl = std::make_unique<PStringNumberList>(run_list);
561 std::string errorMsg("");
562 if (!nl->Parse(errorMsg)) {
563 std::cerr << std::endl;
564 std::cerr << ">> msr2data: " << errorMsg << " - Quitting now." << std::endl;
565 return 0;
566 }
567 // get run list vector
568 run_vec = nl->GetList();
569
570 msrExtension = arg[rightbracket + 1];
571
572 std::vector<std::string>::iterator iter(arg.begin());
573 for (unsigned int i(0); i<rightbracket + 2; ++i) {
574 arg.erase(iter);
575 iter = arg.begin();
576 }
577
578 } else {
579 bool argOneIsNumber(isNumber(arg[0])), argTwoIsNumber(isNumber(arg[1]));
580
581 if (argOneIsNumber && argTwoIsNumber) { // the first two arguments are numbers: an interval of numbers is given
582 runTAG = 2;
583
584 if (arg.size() < 3) {
585 std::cerr << std::endl;
586 std::cerr << ">> msr2data: **ERROR** No msr-file extension specified! Quitting now..." << std::endl;
587 run_vec.clear();
588 arg.clear();
589 return 0;
590 }
591
592 run_vec.push_back(boost::lexical_cast<unsigned int>(arg[0]));
593 run_vec.push_back(boost::lexical_cast<unsigned int>(arg[1]));
594
595 msrExtension = arg[2];
596
597 std::vector<std::string>::iterator iter(arg.begin());
598 for (unsigned int i(0); i < 3; i++) {
599 arg.erase(iter);
600 iter = arg.begin();
601 }
602 } else if (argOneIsNumber && !argTwoIsNumber) { // only one run number is given
603 runTAG = 3;
604 run_vec.push_back(boost::lexical_cast<unsigned int>(arg[0]));
605 msrExtension = arg[1];
606
607 std::vector<std::string>::iterator iter(arg.begin());
608 for (unsigned int i(0); i < 2; i++) {
609 arg.erase(iter);
610 iter = arg.begin();
611 }
612 } else { // assume a runlist-file with "independent variables" is given
613 runTAG = 4;
614 run_list = arg[0];
615 msrExtension = arg[1];
616
617 std::vector<std::string>::iterator iter(arg.begin());
618 for (unsigned int i(0); i < 2; i++) {
619 arg.erase(iter);
620 iter = arg.begin();
621 }
622 }
623 }
624 }
625 catch(boost::bad_lexical_cast &) {
626 std::cerr << std::endl;
627 std::cerr << ">> msr2data: **ERROR** At least one given run number is out of range! Quitting..." << std::endl;
628 run_vec.clear();
629 arg.clear();
630 return -1;
631 }
632
633 // check if parameter list is given
634 int noParamList(msr2data_paramList(arg, param_vec));
635 if (noParamList == -1) {
636 arg.clear();
637 return -1;
638 }
639
640 // check the validity of the command line given command line arguments
641 std::string wrongArgument(msr2data_validArguments(arg));
642 if (!wrongArgument.empty()) {
643 std::cerr << std::endl;
644 std::cerr << ">> msr2data: **ERROR** Unknown argument: " << wrongArgument << ". Quitting..." << std::endl;
645 run_vec.clear();
646 arg.clear();
647 return -1;
648 }
649
650 // check if the output format is DB or data
651 bool db(msr2data_useOption(arg, "data"));
652
653 // check the arguments for the "-o" option and set the output filename
654 std::string outputFile(msr2data_outputfile(arg, db));
655
656 // introduce check, if no output should be generated - in that case we do not need msrfile and rundata handlers later
657 bool realOutput(true);
658 if (!boost::algorithm::to_lower_copy(outputFile).compare("none"))
659 realOutput = false;
660
661 // create the msr2data-object and set the run numbers according to the runTAG above
662 std::unique_ptr<PMsr2Data> msr2dataHandler = std::make_unique<PMsr2Data>(msrExtension);
663
664 int status;
665
666 switch(runTAG) {
667 case 1:
668 status = msr2dataHandler->SetRunNumbers(run_vec);
669 break;
670 case 2:
671 status = msr2dataHandler->SetRunNumbers(run_vec[0], run_vec[1]);
672 break;
673 case 3:
674 status = msr2dataHandler->SetRunNumbers(run_vec[0]);
675 break;
676 case 4:
677 status = msr2dataHandler->SetRunNumbers(run_list);
678 break;
679 default:
680 std::cerr << std::endl;
681 std::cerr << ">> msr2data: **ERROR** None of the possible run list specifications has been detected! Quitting now..." << std::endl;
682 return 0;
683 }
684
685 if (status == 1) {
686 std::cerr << std::endl;
687 std::cerr << ">> msr2data: **ERROR** The run numbers are out of range! Quitting..." << std::endl;
688 return status;
689 } else if (status == -1) {
690 return status;
691 }
692
693 run_vec.clear();
694
695 // check if fitting should be done and in case, which template run number to use
696 int temp(0);
697 bool chainfit(true), onlyInputCreation(false);
698 std::string musrfitOptions;
699
700 temp = msr2data_doFitting(arg, chainfit);
701
702 if (temp == -2) {
703 std::cerr << std::endl;
704 std::cerr << ">> msr2data: **ERROR** More than one fitting options are specified! Quitting..." << std::endl;
705 return temp;
706 } else if (temp == -3) {
707 std::cerr << std::endl;
708 std::cerr << ">> msr2data: **ERROR** The given template has not a valid run number! Quitting..." << std::endl;
709 return temp;
710 }
711
712
713 // check if any options should be passed to musrfit
714 if (temp) {
715 if (!msr2data_useOption(arg, "-k") || !msr2data_useOption(arg, "--keep-mn2-output"))
716 musrfitOptions.append("-k ");
717 if (!msr2data_useOption(arg, "-t") || !msr2data_useOption(arg, "--title-from-data-file"))
718 musrfitOptions.append("-t ");
719 if (!msr2data_useOption(arg, "-e") || !msr2data_useOption(arg, "--estimateN0"))
720 musrfitOptions.append("-e ");
721 if (!msr2data_useOption(arg, "-y") || !msr2data_useOption(arg, "--yaml"))
722 musrfitOptions.append("-y ");
723 if (!msr2data_useOption(arg, "-p") || !msr2data_useOption(arg, "--per-run-block-chisq"))
724 musrfitOptions.append("-p ");
725 }
726
727 // if no fitting should be done, check if only the input files should be created
728 if (!temp) {
729 temp = msr2data_doInputCreation(arg, onlyInputCreation);
730 if (onlyInputCreation) {
731 // if only input files should be created, do not write data to an output file (no matter, what has been determined earlier)
732 realOutput = false;
733 outputFile = "none";
734 }
735 if (temp == -3) {
736 std::cerr << std::endl;
737 std::cerr << ">> msr2data: **ERROR** The given template has not a valid run number! Quitting..." << std::endl;
738 return temp;
739 }
740 }
741
742 // check if msr2data should work in the global fit regime
743 bool setNormalMode(msr2data_useOption(arg, "global"));
744 unsigned int globalMode(0);
745
746 if (!msr2data_useOption(arg, "global+!")) {
747 setNormalMode = false;
748 globalMode = 1;
749 } else if (!msr2data_useOption(arg, "global+")) {
750 setNormalMode = false;
751 globalMode = 2;
752 }
753
754 // At this point it should be clear if any template for input-file generation is given or not.
755 // Therefore, the number of digits in the run number format is determined only here.
756 if (temp > 0) {
757 status = msr2dataHandler->DetermineRunNumberDigits(temp, setNormalMode);
758 } else {
759 status = msr2dataHandler->DetermineRunNumberDigits(msr2dataHandler->GetPresentRun(), setNormalMode);
760 }
761
762 if (status) {
763 return status;
764 }
765
766 // Check if all given run numbers are covered by the formatting of the data file name
767 status = msr2dataHandler->CheckRunNumbersInRange();
768 if (status) {
769 std::cerr << std::endl;
770 std::cerr << ">> msr2data: **ERROR** At least one given run number is out of range! Quitting..." << std::endl;
771 return status;
772 }
773
774 bool writeSummary(false);
775 unsigned int writeHeader(2);
776 // writeHeader: 0 - no header
777 // 1 - write header explicitly (even if the file is present already)
778 // 2 - write header automatically if a new file is created and do not if the data is appended to an existing file
779
780 std::unique_ptr<std::fstream> fileOutput;
781 if (realOutput) {
782 // check the arguments for the "header" and "noheader" options
783 if (!msr2data_useOption(arg, "header")) {
784 if (!msr2data_useOption(arg, "noheader")) {
785 writeHeader = 2;
786 } else {
787 writeHeader = 1;
788 }
789 } else if (!msr2data_useOption(arg, "noheader")) {
790 writeHeader = 0;
791 }
792
793 // check the arguments for the "nosummary" option
794 writeSummary = msr2data_useOption(arg, "nosummary");
795
796 // delete old db/data file if the "new" option is given
797 if (!msr2data_useOption(arg, "new")) {
798 fileOutput = std::make_unique<std::fstream>();
799 fileOutput->open(outputFile.c_str(), std::ios::in);
800 if (fileOutput->is_open()) {
801 std::cout << std::endl << ">> msr2data: **INFO** Deleting output file " << outputFile << std::endl;
802 fileOutput->close();
803 fileOutput->open(outputFile.c_str(), std::ios::out | std::ios::trunc);
804 fileOutput->close();
805 } else {
806 std::cout << std::endl << ">> msr2data: **INFO** Ignoring the 'new' option since " << outputFile << " does not exist yet." << std::endl;
807 }
808 if (writeHeader == 2) {
809 writeHeader = 1;
810 }
811 }
812 }
813
814 // GLOBAL MODE
815 if (!setNormalMode) {
816 std::ostringstream strInfile;
817 strInfile << msr2dataHandler->GetPresentRun() << "+global" << msrExtension << ".msr";
818
819 // if fitting should be done, prepare a new input file
820 if (temp) {
821 if (temp > 0) { // if it is smaller no input file should be generated
822 bool success(msr2dataHandler->PrepareGlobalInputFile(temp, strInfile.str(), globalMode));
823
824 if (!success) {
825 std::cerr << std::endl << ">> msr2data: **ERROR** Input file generation has not been successful! Quitting..." << std::endl;
826 return -1;
827 }
828 }
829
830 // and do the fitting
831 if (!onlyInputCreation) {
832 // check if MUSRFITPATH is set, if not issue a warning
833 std::string path("");
834 bool pathSet(false);
835 char *pathPtr(getenv("MUSRFITPATH"));
836 if (pathPtr) {
837 path = boost::lexical_cast<std::string>(pathPtr);
838 if (!path.empty()) {
839 pathSet = true;
840 path.append("/");
841 }
842 }
843 if (!pathSet) {
844 std::cerr << std::endl << ">> msr2data: **WARNING** The MUSRFITPATH environment variable is not set!";
845 std::cerr << std::endl << ">> msr2data: **WARNING** Please set it or at least ensure that musrfit can be found on the PATH!" << std::endl;
846 }
847 std::ostringstream oss;
848 oss << path << "musrfit" << " " << strInfile.str() << " " << musrfitOptions;
849 std::cout << std::endl << ">> msr2data: **INFO** Calling " << oss.str() << std::endl;
850 if (system(oss.str().c_str()) == -1) {
851 std::cerr << "**ERROR** cmd: " << oss.str().c_str() << " failed." << std::endl;
852 }
853 }
854 }
855
856 if (realOutput) {
857 // read musrfit startup file
858 if (writeSummary) {
859 status = msr2dataHandler->ParseXmlStartupFile();
860 }
861
862 // Read msr file
863 status = msr2dataHandler->ReadMsrFile(strInfile.str());
864 if (status != PMUSR_SUCCESS) {
865 arg.clear();
866 return status;
867 }
868
869 // read data files
870 if (writeSummary)
871 status = msr2dataHandler->ReadRunDataFile();
872
873 unsigned int counter(0);
874
875 while (msr2dataHandler->GetPresentRun()) {
876 // write DB or dat file
877 status = msr2dataHandler->WriteOutput(outputFile, param_vec, db, writeHeader, !setNormalMode, counter);
878 if (status == -1) {
879 return status;
880 }
881 ++counter;
882 }
883 }
884 } else { // NORMAL MODE - one msr-file for each run
885
886 // read musrfit startup file
887 if (writeSummary) {
888 status = msr2dataHandler->ParseXmlStartupFile();
889 }
890
891 // Processing the run list, do the fitting and write the data to the DB or data output file
892 bool firstrun(true);
893 unsigned int oldtemp(0); // should be accessed only when updated before...
894 std::ostringstream strInfile;
895
896 while (msr2dataHandler->GetPresentRun()) {
897 strInfile.clear();
898 strInfile.str("");
899 strInfile << msr2dataHandler->GetPresentRun() << msrExtension << ".msr";
900
901 // if fitting should be done, prepare a new input file
902 if (temp) {
903 if (temp > 0) {
904 bool success(true);
905 if (firstrun || !chainfit)
906 success = msr2dataHandler->PrepareNewInputFile(temp, false);
907 else
908 success = msr2dataHandler->PrepareNewInputFile(oldtemp, false);
909 if (firstrun)
910 firstrun = false;
911 oldtemp = msr2dataHandler->GetPresentRun();
912
913 if (!success) {
914 std::cerr << std::endl << ">> msr2data: **ERROR** Input file generation has not been successful! Quitting..." << std::endl;
915 return -1;
916 }
917 }
918
919 // and do the fitting
920 if (!onlyInputCreation) {
921 // check if MUSRFITPATH is set, if not issue a warning
922 std::string path("");
923 bool pathSet(false);
924 char *pathPtr(getenv("MUSRFITPATH"));
925 if (pathPtr) {
926 path = boost::lexical_cast<std::string>(pathPtr);
927 if (!path.empty()) {
928 pathSet = true;
929 path.append("/");
930 }
931 }
932 if (!pathSet) {
933 std::cerr << std::endl << ">> msr2data: **WARNING** The MUSRFITPATH environment variable is not set!";
934 std::cerr << std::endl << ">> msr2data: **WARNING** Please set it or at least ensure that musrfit can be found on the PATH!" << std::endl;
935 }
936 std::ostringstream oss;
937 oss << path << "musrfit" << " " << strInfile.str() << " " << musrfitOptions;
938 std::string cmdStr = oss.str();
939 cmdStr.erase(std::remove(cmdStr.begin(), cmdStr.end(), '\r'), cmdStr.end());
940 cmdStr.erase(std::remove(cmdStr.begin(), cmdStr.end(), '\n'), cmdStr.end());
941 std::cout << std::endl << ">> msr2data: **INFO** Calling " << cmdStr << std::endl;
942 if (system(cmdStr.c_str()) == -1) {
943 std::cerr << "**ERROR** cmd: " << cmdStr << " failed." << std::endl;
944 }
945 }
946 }
947
948 // read msr-file
949 if (realOutput) {
950 status = msr2dataHandler->ReadMsrFile(strInfile.str());
951 if (status != PMUSR_SUCCESS) {
952 // if the msr-file cannot be read, write no output but proceed to the next run
953 status = msr2dataHandler->WriteOutput("none", param_vec, db, writeHeader);
954 if (status == -1) {
955 return status;
956 } else {
957 continue;
958 }
959 }
960 }
961
962 // read data files
963 if (writeSummary)
964 status = msr2dataHandler->ReadRunDataFile();
965
966 // write DB or dat file
967 status = msr2dataHandler->WriteOutput(outputFile, param_vec, db, writeHeader);
968 if (status == -1) {
969 return status;
970 }
971 }
972 }
973
974 // Make sure that the empty line at the end of the output file gets written
975 // This is needed to create a "valid db-file", however, we do it for all output files
976 // Unfortunately, this can be done in a coherent way only on that level
977 // Unfortunately, there are also problems with boost::filesystem::exists(outputFile)
978 // Therefore, first try to open the file for reading and if this works, write to it - not clean but it works
979 if (realOutput) {
980 fileOutput = std::make_unique<std::fstream>();
981 fileOutput->open(outputFile.c_str(), std::ios::in);
982 if (fileOutput->is_open()) {
983 fileOutput->close();
984 fileOutput->open(outputFile.c_str(), std::ios::out | std::ios::app);
985 if (fileOutput->is_open()) {
986 *fileOutput << std::endl << std::endl;
987 fileOutput->close();
988 } else {
989 std::cerr << std::endl << ">> msr2data: **ERROR** The output file " << outputFile << " cannot be opened! Please check!";
990 std::cerr << std::endl;
991 }
992 } else {
993 std::cerr << std::endl << ">> msr2data: **WARNING** No output has been written to the file " << outputFile << "!";
994 std::cerr << std::endl << ">> msr2data: **WARNING** Please check the range of runs and the specified options!" << std::endl;
995 }
996 }
997
998 if (!arg.empty()) {
999 std::cout << std::endl << ">> msr2data: **INFO** The following command line arguments have been specified but not been used: " << std::endl;
1000 std::cout << ">> msr2data: **INFO**";
1001 for (unsigned int i(0); i<arg.size(); ++i)
1002 std::cout << " " << arg[i];
1003 std::cout << std::endl;
1004 }
1005
1006 std::cout << std::endl << ">> msr2data: done ..." << std::endl;
1007
1008 return 1;
1009}
#define PMUSR_SUCCESS
Successful operation completion.
Definition PMusr.h:53
return status
int main(int argc, char *argv[])
Definition addRun.cpp:364
int msr2data_doInputCreation(std::vector< std::string > &arg, bool &inputOnly)
Definition msr2data.cpp:358
void msr2data_syntax()
Definition msr2data.cpp:86
std::string msr2data_outputfile(std::vector< std::string > &arg, bool db=true)
Definition msr2data.cpp:226
std::string msr2data_validArguments(const std::vector< std::string > &arg)
Definition msr2data.cpp:182
int msr2data_doFitting(std::vector< std::string > &arg, bool &chainfit)
Definition msr2data.cpp:305
bool isNumber(const std::string &s)
Definition msr2data.cpp:70
bool msr2data_useOption(std::vector< std::string > &arg, const std::string &s)
Definition msr2data.cpp:276
int msr2data_paramList(std::vector< std::string > &arg, std::vector< unsigned int > &paramList)
Definition msr2data.cpp:386