added the class PStringNumberList which allow to parse a generic encoded list of numbers. Used this class to improve PMsrHandler, and improved the runList feature of msr2data. For details see the documentation of msr2data.

This commit is contained in:
suter_a 2015-08-16 11:43:57 +02:00
parent bccc97ff22
commit b113a94ee9
9 changed files with 265 additions and 195 deletions

View File

@ -3165,15 +3165,19 @@ Bool_t PMsrHandler::HandleRunEntry(PMsrLines &lines)
if (tokens->GetEntries() < 2) {
error = true;
} else {
PIntVector group;
PUIntVector group;
str = iter->fLine;
if (ParseDetectorGrouping(str, group)) {
PStringNumberList *rl = new PStringNumberList(str.Data());
string errorMsg("");
if (rl->Parse(errorMsg, true)) {
group = rl->GetList();
for (UInt_t i=0; i<group.size(); i++) {
param.SetForwardHistoNo(group[i]);
}
} else {
error = true;
}
delete rl;
group.clear();
}
}
@ -3186,15 +3190,19 @@ Bool_t PMsrHandler::HandleRunEntry(PMsrLines &lines)
if (tokens->GetEntries() < 2) {
error = true;
} else {
PIntVector group;
PUIntVector group;
str = iter->fLine;
if (ParseDetectorGrouping(str, group)) {
PStringNumberList *rl = new PStringNumberList(str.Data());
string errorMsg("");
if (rl->Parse(errorMsg, true)) {
group = rl->GetList();
for (UInt_t i=0; i<group.size(); i++) {
param.SetBackwardHistoNo(group[i]);
}
} else {
error = true;
}
delete rl;
group.clear();
}
}
@ -3892,6 +3900,9 @@ Bool_t PMsrHandler::HandlePlotEntry(PMsrLines &lines)
param.fLifeTimeCorrection = true;
} else if (iter1->fLine.Contains("runs", TString::kIgnoreCase)) { // handle plot runs
TComplex run;
PStringNumberList *rl;
string errorMsg;
PUIntVector runList;
switch (param.fPlotType) {
case -1:
error = true;
@ -3900,31 +3911,21 @@ Bool_t PMsrHandler::HandlePlotEntry(PMsrLines &lines)
case MSR_PLOT_ASYM:
case MSR_PLOT_NON_MUSR:
case MSR_PLOT_MU_MINUS:
tokens = iter1->fLine.Tokenize(" \t");
if (!tokens) {
rl = new PStringNumberList(iter1->fLine.Data());
if (!rl->Parse(errorMsg, true)) {
cerr << endl << ">> PMsrHandler::HandlePlotEntry: **SEVERE ERROR** Couldn't tokenize PLOT in line " << iter1->fLineNo;
cerr << endl << ">> Error Message: " << errorMsg;
cerr << endl << endl;
return false;
}
if (tokens->GetEntries() < 2) { // runs missing
error = true;
} else {
for (Int_t i=1; i<tokens->GetEntries(); i++) {
ostr = dynamic_cast<TObjString*>(tokens->At(i));
str = ostr->GetString();
if (str.IsDigit()) {
run = TComplex(str.Atoi(),-1.0);
param.fRuns.push_back(run);
} else {
error = true;
}
}
runList = rl->GetList();
for (UInt_t i=0; i<runList.size(); i++) {
run = TComplex(runList[i], -1.0);
param.fRuns.push_back(run);
}
// clean up
if (tokens) {
delete tokens;
tokens = 0;
}
runList.clear();
delete rl;
break;
default:
error = true;
@ -5644,86 +5645,6 @@ UInt_t PMsrHandler::LastSignificant(Double_t dval, UInt_t precLimit)
return lastSignificant;
}
//--------------------------------------------------------------------------
// ParseDetectorGrouping (private)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param str forward/backward string to be decoded
* \param group decoded detector grouping vector
*
* <b>return:</b> true if parsing was successful, false otherwise.
*/
Bool_t PMsrHandler::ParseDetectorGrouping(TString str, PIntVector &group)
{
TObjArray *tok=0, *tok1=0;
TObjString *ostr=0;
Int_t first=0, last=0;
// change cn - cm to cn-cm. Will *NOT* handle cn - cm etc
str = str.ReplaceAll(" - ", "-");
group.clear();
tok = str.Tokenize(" \t");
// check that there are indeed enough tokens
if (tok->GetEntries() < 2) {
delete tok;
return false;
}
for (Int_t i=1; i<tok->GetEntries(); i++) {
ostr = dynamic_cast<TObjString*>(tok->At(i));
if (ostr->GetString().Contains("-")) { // hopefully a cn-cm token
tok1 = ostr->GetString().Tokenize("-");
if (tok1->GetEntries() == 2) {
ostr = dynamic_cast<TObjString*>(tok1->At(0));
if (ostr->GetString().IsDigit()) {
first = ostr->GetString().Atoi();
} else {
if (tok) delete tok;
if (tok1) delete tok1;
return false;
}
ostr = dynamic_cast<TObjString*>(tok1->At(1));
if (ostr->GetString().IsDigit()) {
last = ostr->GetString().Atoi();
} else {
if (tok) delete tok;
if (tok1) delete tok1;
return false;
}
if (last < first) {
if (tok) delete tok;
return false;
}
for (Int_t i=first; i<=last; i++)
group.push_back(i);
} else {
if (tok) delete tok;
if (tok1) delete tok1;
return false;
}
} else { // hopefully a number
if (ostr->GetString().IsDigit()) {
group.push_back(ostr->GetString().Atoi());
} else {
if (tok) delete tok;
return false;
}
}
}
// clean up
if (tok) delete tok;
if (tok1) delete tok1;
return true;
}
//--------------------------------------------------------------------------
// MakeDetectorGroupingString (private)
//--------------------------------------------------------------------------

View File

@ -28,10 +28,12 @@
***************************************************************************/
#include <cassert>
#include <iostream>
using namespace std;
#include <boost/algorithm/string.hpp>
using namespace boost;
#include "PMusr.h"
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@ -1741,3 +1743,167 @@ void PMsrRunBlock::SetMapGlobal(UInt_t idx, Int_t ival)
// else do nothing at the moment
return;
}
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// implementation PStringNumberList
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//--------------------------------------------------------------------------
// Parse (public)
//--------------------------------------------------------------------------
/**
* <p>Helper class which parses list of numbers of the following 3 forms and its combination.
* (i) list of integers separted by spaces, e.g. 1 3 7 14
* (ii) a range of integers of the form nS-nE, e.g. 13-27 which will generate 13, 14, 15, .., 26, 27
* (iii) a sequence of integers of the form nS:nE:nStep, e.g. 10:20:2 which will generate 10, 12, 14, .., 18, 20
*
* \param errorMsg error message
* \param ignoreFirstToken if true, the first parse token will be ignored
*
* @return true if parse has been successful, otherwise false
*/
bool PStringNumberList::Parse(string &errorMsg, bool ignoreFirstToken)
{
bool result=true;
vector<string> splitVec;
int ival;
// before checking tokens, remove 'forbidden' " - " and " : "
StripSpaces();
// split string into space separated tokens
split(splitVec, fString, is_any_of(" "), token_compress_on);
unsigned int start=0;
if (ignoreFirstToken)
start=1;
for (unsigned int i=start; i<splitVec.size(); i++) {
if (splitVec[i].length() != 0) { // ignore empty tokens
if (splitVec[i].find("-") != string::npos) { // check for potential range
vector<string> subSplitVec;
// split potential nS-nE token
split(subSplitVec, splitVec[i], is_any_of("-"), token_compress_on);
int start=-1, end=-1;
unsigned int count=0;
for (unsigned int j=0; j<subSplitVec.size(); j++) {
if (subSplitVec[j].length() != 0) { // ignore empty tokens
if (!IsNumber(subSplitVec[j])) {
result = false;
} else {
count++;
if (count == 1)
start = atoi(subSplitVec[j].c_str());
else if (count == 2)
end = atoi(subSplitVec[j].c_str());
else
result = false;
}
}
}
if ((start < 0) || (end < 0)) { // check that there is a vaild start and end
errorMsg = "**ERROR** start or end of a range is not valid";
result = false;
}
if (result) { // no error, hence check start and end
if (start > end) {
int swap = end;
cerr << "**WARNING** start=" << start << " > end=" << end << ", hence I will swap them" << endl;
end = start;
start = swap;
}
for (int j=start; j<=end; j++)
fList.push_back(j);
}
} else if (splitVec[i].find(":") != string::npos) { // check for potential sequence
vector<string> subSplitVec;
// split potential rStart:rEnd:rStep token
split(subSplitVec, splitVec[i], is_any_of(":"), token_compress_on);
int start=-1, end=-1, step=-1;
unsigned int count=0;
for (unsigned int j=0; j<subSplitVec.size(); j++) {
if (subSplitVec[j].length() != 0) { // ignore empty tokens
if (!IsNumber(subSplitVec[j])) {
result = false;
} else {
count++;
if (count == 1)
start = atoi(subSplitVec[j].c_str());
else if (count == 2)
end = atoi(subSplitVec[j].c_str());
else if (count == 3)
step = atoi(subSplitVec[j].c_str());
else
result = false;
}
}
}
if ((start < 0) || (end < 0) || (step < 0)) { // check that there is a vaild start and end
errorMsg = "**ERROR** start, end, or step of a sequence is not valid";
result = false;
}
if (result) { // no error, hence check start and end
if (start > end) {
int swap = end;
cerr << "**WARNING** start=" << start << " > end=" << end << ", hence I will swap them" << endl;
end = start;
start = swap;
}
for (int j=start; j<=end; j+=step)
fList.push_back(j);
}
} else if (IsNumber(splitVec[i])) {
ival = atoi(splitVec[i].c_str());
fList.push_back(ival);
} else {
errorMsg = "**ERROR** invalid token: " + splitVec[i];
result = false;
}
}
}
return result;
}
//--------------------------------------------------------------------------
// StripSpaces (private)
//--------------------------------------------------------------------------
/**
* <p>This routine removes arbitray number of spaces between '-' and ':',
* e.g. 123 - 125 will be converted to 123-125, etc.
*/
void PStringNumberList::StripSpaces()
{
string str=fString;
int pos=-1;
// backward scan
for (int i=str.size(); i>=0; --i) { // check if first space is found
if ((str[i] == ' ') && (pos == -1)) {
pos = i;
} else if ((str[i] == '-') || (str[i] == ':')) { // check for '-' or ':'
if (pos != -1) {
str.erase(i+1, pos-i);
}
} else if (str[i] != ' ') { // anything but different than a space leads to a reset of the pos counter
pos = -1;
}
}
// forward scan
for (unsigned int i=0; i<str.size(); i++) { // check if first space is found
if ((str[i] == ' ') && (pos == -1)) {
pos = i;
} else if ((str[i] == '-') || (str[i] == ':')) { // check for '-' or ':'
if (pos != -1) {
str.erase(pos, i-pos);
i = pos;
}
} else if (str[i] != ' ') { // anything but different than a space leads to a reset of the pos counter
pos = -1;
}
}
fString = str;
}

View File

@ -152,7 +152,6 @@ class PMsrHandler
virtual UInt_t NeededPrecision(Double_t dval, UInt_t precLimit=13);
virtual UInt_t LastSignificant(Double_t dval, UInt_t precLimit=6);
virtual Bool_t ParseDetectorGrouping(TString str, PIntVector &group);
virtual void MakeDetectorGroupingString(TString str, PIntVector &group, TString &result, Bool_t includeDetector = true);
virtual void CheckLegacyLifetimecorrection();

View File

@ -801,4 +801,29 @@ typedef struct {
Double_t alphaEstimateN0; ///< relates the Bkg to N0, i.e. Bkg = alpha*N0
} PStartupOptions;
//-------------------------------------------------------------
/**
* <p>Helper class which parses list of numbers of the following 3 forms and its combination.
* (i) list of integers separted by spaces, e.g. 1 3 7 14
* (ii) a range of integers of the form nS-nE, e.g. 13-27 which will generate 13, 14, 15, .., 26, 27
* (iii) a sequence of integers of the form nS:nE:nStep, e.g. 10:20:2 which will generate 10, 12, 14, .., 18, 20
*/
class PStringNumberList {
public:
PStringNumberList(char *str) { fString = str; }
PStringNumberList(string str) { fString = str; }
virtual ~PStringNumberList() { fList.clear(); }
virtual bool Parse(string &errorMsg, bool ignoreFirstToken=false);
virtual PUIntVector GetList() { return fList; }
private:
string fString;
bool fIsValid;
PUIntVector fList;
virtual bool IsNumber(string &str) { return (str.find_first_not_of("0123456789") == string::npos); }
virtual void StripSpaces();
};
#endif // _PMUSR_H_

View File

@ -37,6 +37,7 @@
#endif
#include "git-revision.h"
#include "PMusr.h"
#include "PMsr2Data.h"
#include <algorithm>
@ -87,12 +88,17 @@ void msr2data_syntax()
cout << endl << " [fit [-k] [-t] | fit-<template>[!] [-k] [-t] | msr-<template>]";
cout << endl << "usage 2: msr2data <run1> <run2> <extension> [-o<outputfile>] [new] [data] [[no]header] [nosummary] [global[+[!]]]";
cout << endl << " [fit [-k] [-t] | fit-<template>[!] [-k] [-t] | msr-<template>]";
cout << endl << "usage 3: msr2data \\[<run1> <run2> ... <runN>\\] <extension> [-o<outputfile> ] [new] [data] [[no]header] [nosummary] [global[+[!]]]";
cout << endl << "usage 3: msr2data \\[<runList>\\] <extension> [-o<outputfile> ] [new] [data] [[no]header] [nosummary] [global[+[!]]]";
cout << endl << " [fit [-k] [-t] | fit-<template>[!] [-k] [-t] | msr-<template>]";
cout << endl << "usage 4: msr2data <runlist> <extension> [-o<outputfile>] [new] [data] [[no]header] [nosummary] [global[+[!]]]";
cout << endl << " [fit [-k] [-t] | fit-<template>[!] [-k] [-t] | msr-<template>]";
cout << endl;
cout << endl << " <run>, <run1>, <run2>, ... <runN> : run numbers";
cout << endl << " <runList> can be:";
cout << endl << " (i) <run0>, <run1>, <run2>, ... <runN> : run numbers, e.g. 123 124";
cout << endl << " (ii) <run0>-<runN> : a range, e.g. 123-125 -> 123 124 125";
cout << endl << " (iii) <run0>:<runN>:<step> : a sequence, e.g. 123:127:2 -> 123 125 127";
cout << endl << " <step> will give the step width and has to be a positive number!";
cout << endl << " a <runList> can also combine (i)-(iii), e.g. 123 128-130 133, etc.";
cout << endl << " <extension> : msr-file extension, e.g. _tf_h13 for the file name 8472_tf_h13.msr";
cout << endl << " -o<outputfile> : specify the name of the DB or column-data output file; default: out.db/out.dat";
cout << endl << " if the option '-o none' is used, no output file will be written.";
@ -125,9 +131,27 @@ void msr2data_syntax()
cout << endl << " from this pre-analysis for each run---they are not just copied from the template.";
cout << endl << " The specification of '!' determines which fit mode (see above) is used for this pre-analysis.";
cout << endl;
cout << endl << " Typical examples:";
cout << endl;
cout << endl << " msr2data 2047 2050 _tf_histo fit-2046";
cout << endl << " will use 2046_tf_histo.msr as templete, and subsequently generating 2047_tf_histo.msr until";
cout << endl << " 2050_tf_histo.msr and fit them.";
cout << endl;
cout << endl << " msr2data 2047 2050 _tf_histo msr-2046";
cout << endl << " will use 2046_tf_histo.msr as templete, and subsequently generating 2047_tf_histo.msr until";
cout << endl << " 2050_tf_histo.msr, but NO fitting will be done.";
cout << endl;
cout << endl << " msr2data 2046 2050 _tf_histo -o fitParam.db";
cout << endl << " will collect the fit parameters from runs 2046-2050 (msr-files 2046_tf_histo.msr etc.) and";
cout << endl << " write them to the file fitParam.db (DB-format).";
cout << endl;
cout << endl << " msr2data [2047:2053:2 2056] _tf_histo fit-2045";
cout << endl << " will use 2045_tf_histo.msr as templete, and subsequently generating msr-files from the run-list:";
cout << endl << " 2047 2049 2051 2053 2056 (2047_tf_histo.msr etc.) and fit them.";
cout << endl;
cout << endl << " For further information please refer to";
cout << endl << " https://intranet.psi.ch/MUSR/Msr2Data";
cout << endl << " http://lmu.web.psi.ch/facilities/software/musrfit/user/intranet.psi.ch/MUSR/Msr2Data.html";
cout << endl << " http://lmu.web.psi.ch/musrfit/user/MUSR/Msr2Data.html";
cout << endl << " https://intranet.psi.ch/MUSR/Msr2Data";
cout << endl << endl;
}
@ -449,8 +473,26 @@ int main(int argc, char *argv[])
return 0;
}
for (unsigned int i(firstRunNumberInArg); i<=lastRunNumberInArg; ++i)
run_vec.push_back(boost::lexical_cast<unsigned int>(arg[i]));
// generate run_list string
for (unsigned int i(firstRunNumberInArg); i<=lastRunNumberInArg; ++i) {
run_list += arg[i] + " ";
}
// parse run_list string
PStringNumberList *nl = new PStringNumberList(run_list);
if (nl == 0) { // couldn't invoke object
cerr << endl;
cerr << ">> msr2data: **ERROR** Couldn't invoke run_list parser object! Quitting now." << endl;
return 0;
}
string errorMsg("");
if (!nl->Parse(errorMsg)) {
cerr << endl;
cerr << ">> msr2data: " << errorMsg << " - Quitting now." << endl;
return 0;
}
// get run list vector
run_vec = nl->GetList();
delete nl;
msrExtension = arg[rightbracket + 1];

View File

@ -28,6 +28,7 @@
***************************************************************************/
#include <iostream>
#include <string>
using namespace std;
#include <QTextEdit>
@ -679,42 +680,6 @@ void PTextEdit::doConnections( PSubTextEdit *e )
connect( e, SIGNAL( cursorPositionChanged() ), this, SLOT( currentCursorPosition() ));
}
//----------------------------------------------------------------------------------------------------
/**
* <p>Validates a given runlist.
*
* \param runList run list string which should be a space separated list of run numbers.
*/
bool PTextEdit::validRunList(const QString runList)
{
bool success = true;
int i = 0;
QString subStr;
bool done = false;
int val = 0;
bool ok;
while (!done) {
subStr = runList.section(' ', i, i, QString::SectionSkipEmpty);
if (subStr.isEmpty()) {
done = true;
continue;
}
i++;
val = subStr.toInt(&ok);
if (!ok) {
done = true;
success = false;
}
}
if (i == 0) { // no token found
success = false;
}
return success;
}
//----------------------------------------------------------------------------------------------------
/**
* <p>Start the dialog to enter a msr-file title. See also https://intranet.psi.ch/MUSR/MusrFit#4_1_The_Title
@ -1892,12 +1857,6 @@ void PTextEdit::musrMsr2Data()
break;
case 1: // run list
runList = fMsr2DataParam->runList;
if (!validRunList(runList)) {
QMessageBox::critical(this, "**ERROR**",
"Invalid Run List!\nThe run list needs to be a space separated list of run numbers.",
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
break;
case 2: // run list file name
runListFileName = fMsr2DataParam->runListFileName;

View File

@ -77,7 +77,6 @@ private:
void load( const QString &f, const int index=-1 );
PSubTextEdit *currentEditor() const;
void doConnections( PSubTextEdit *e );
bool validRunList(const QString runList);
void fileSystemWatcherActivation();
private slots:

View File

@ -493,40 +493,6 @@ void PTextEdit::doConnections( PSubTextEdit *e )
connect( e, SIGNAL( cursorPositionChanged(int, int) ), this, SLOT( currentCursorPosition(int, int) ));
}
//----------------------------------------------------------------------------------------------------
/**
* <p>
*/
bool PTextEdit::validRunList(const QString runList)
{
bool success = true;
int i = 0;
QString subStr;
bool done = false;
int val;
bool ok;
while (!done) {
subStr = runList.section(' ', i, i, QString::SectionSkipEmpty);
if (subStr.isEmpty()) {
done = true;
continue;
}
i++;
val = subStr.toInt(&ok);
if (!ok) {
done = true;
success = false;
}
}
if (i == 0) { // no token found
success = false;
}
return success;
}
//----------------------------------------------------------------------------------------------------
/**
* <p>
@ -1500,12 +1466,6 @@ void PTextEdit::musrMsr2Data()
break;
case 1: // run list
runList = fMsr2DataParam->runList;
if (!validRunList(runList)) {
QMessageBox::critical(this, "**ERROR**",
"Invalid Run List!\nThe run list needs to be a space separated list of run numbers.",
QMessageBox::Ok, QMessageBox::NoButton);
return;
}
break;
case 2: // run list file name
runListFileName = fMsr2DataParam->runListFileName;

View File

@ -64,7 +64,6 @@ private:
void load( const QString &f, const int index=-1 );
PSubTextEdit *currentEditor() const;
void doConnections( PSubTextEdit *e );
bool validRunList(const QString runList);
private slots:
void fileNew();