merge in Bastian M. Wojek's cpp-version of msr2data. This should help to step towards a real platform independent musrfit package.

This commit is contained in:
nemu 2009-08-31 11:25:18 +00:00
parent 9ac709451c
commit 060192488c
3 changed files with 1315 additions and 0 deletions

799
src/classes/PMsr2Data.cpp Normal file
View File

@ -0,0 +1,799 @@
/***************************************************************************
PMsr2Data.cpp
Author: Bastian M. Wojek / Andreas Suter
e-mail: andreas.suter@psi.ch
$Id$
***************************************************************************/
/***************************************************************************
* Copyright (C) 2009 by Bastian M. Wojek / Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <cctype>
#include <sstream>
#include <fstream>
#include <iomanip>
using namespace std;
#include <boost/algorithm/string/trim.hpp> // for stripping leading whitespace in std::string
#include <boost/algorithm/string/case_conv.hpp> // for to_lower() in std::string
#include <boost/algorithm/string/split.hpp> // split strings at certain characters
using namespace boost::algorithm;
#include "PMsr2Data.h"
//-------------------------------------------------------------
/**
* <p> Write formatted output to file
*/
void writeValues(ofstream &outFile, const double &value, const unsigned int &width)
{
if (fabs(value) >= 1.0e6 || (fabs(value) < 1.0e-4 && fabs(value) > 0.0))
outFile << scientific << setprecision(width - 8);
else
outFile.setf(ios::floatfield);
outFile << setw(width) << left << value;
return;
}
//-------------------------------------------------------------
/**
* <p>
*/
PMsr2Data::PMsr2Data(const string &ext) : fFileExtension(ext), fRunListFile(false)
{
fRunVector.clear();
fRunVectorIter = fRunVector.end();
fRunListFileStream = 0;
fSaxParser = 0;
fStartupHandler = 0;
fDataHandler = 0;
fMsrHandler = 0;
}
//-------------------------------------------------------------
/**
* <p>
*/
PMsr2Data::~PMsr2Data()
{
fRunVector.clear();
fRunVectorIter = fRunVector.end();
fIndVar.clear();
if (fRunListFileStream) {
fRunListFileStream->close();
delete fRunListFileStream;
fRunListFileStream = 0;
}
if (fSaxParser) {
delete fSaxParser;
fSaxParser = 0;
}
if (fStartupHandler) {
delete fStartupHandler;
fStartupHandler = 0;
}
if (fDataHandler) {
delete fDataHandler;
fDataHandler = 0;
}
if (fMsrHandler) {
delete fMsrHandler;
fMsrHandler = 0;
}
}
//-------------------------------------------------------------
/**
* <p>
*/
unsigned int PMsr2Data::GetPresentRun() const
{
if (fRunVectorIter != fRunVector.end())
return *fRunVectorIter;
else
return 0;
}
//-------------------------------------------------------------
/**
* <p>
*/
int PMsr2Data::SetRunNumbers(unsigned int runNo)
{
if (runNo > 9999 || runNo < 1)
return 1;
fRunVector.clear();
fRunVector.push_back(runNo);
fRunVectorIter = fRunVector.begin();
return 0;
}
//-------------------------------------------------------------
/**
* <p>
*/
int PMsr2Data::SetRunNumbers(unsigned int runNoStart, unsigned int runNoEnd)
{
if ((runNoStart > 9999) || (runNoEnd > 9999) || (runNoStart < 1) || (runNoEnd < 1))
return 1;
fRunVector.clear();
if (runNoStart <= runNoEnd) {
for (unsigned int i(runNoStart); i<=runNoEnd; ++i)
fRunVector.push_back(i);
} else {
for (unsigned int i(runNoStart); i>=runNoEnd; --i)
fRunVector.push_back(i);
}
fRunVectorIter = fRunVector.begin();
return 0;
}
//-------------------------------------------------------------
/**
* <p>
*/
int PMsr2Data::SetRunNumbers(const vector<unsigned int> &runListVector)
{
if (runListVector.empty())
return -1;
for (vector<unsigned int>::const_iterator iter(runListVector.begin()); iter!=runListVector.end(); iter++) {
if (*iter > 9999 || *iter < 1)
return 1;
}
fRunVector = runListVector;
fRunVectorIter = fRunVector.begin();
return 0;
}
//-------------------------------------------------------------
/**
* <p>
*/
int PMsr2Data::SetRunNumbers(const string &runListFile)
{
fRunVector.clear();
ifstream in(runListFile.c_str());
if (!in) {
cout << endl;
cout << ">> msr2data: **ERROR** The runlist file " << runListFile << " cannot be opened! Please check!" << endl;
return -1;
}
string line, indvar;
istringstream strLine;
vector<string> splitVec;
unsigned int cntr(0);
unsigned int runNo;
while (getline(in, line)) {
trim(line);
if (line.empty())
continue;
else if (line.at(0) == '#')
continue;
else
cntr++;
split( splitVec, line, is_any_of("#") ); // split the string if any comments appear on the line
if (cntr == 1) { // Read in the names of the independent variables in the runlist file
strLine.clear();
strLine.str(splitVec[0]);
strLine >> indvar; // "RUN"
if (indvar.compare("RUN")) {
cout << endl;
cout << ">> msr2data: **ERROR** The format of the runlist file " << runListFile << " is not correct! Please check!" << endl;
}
while (strLine >> indvar)
fIndVar.push_back(indvar);
}
if (cntr > 1) {
strLine.clear();
strLine.str(splitVec[0]);
strLine >> runNo;
if (runNo > 9999 || runNo < 1)
return 1;
fRunVector.push_back(runNo);
}
splitVec.clear();
}
in.close();
fRunVectorIter = fRunVector.begin();
fRunListFile = true;
fRunListFileStream = new ifstream(runListFile.c_str());
return 0;
}
//-------------------------------------------------------------
/**
* <p>
*/
int PMsr2Data::ParseXmlStartupFile()
{
int status;
fSaxParser = new TSAXParser();
fStartupHandler = new PStartupHandler();
string startup_path_name(fStartupHandler->GetStartupFilePath().Data());
fSaxParser->ConnectToHandler("PStartupHandler", fStartupHandler);
status = fSaxParser->ParseFile(startup_path_name.c_str());
// check for parse errors
if (status) { // error
cout << endl << ">> msr2data: **WARNING** reading/parsing musrfit_startup.xml." << endl;
// clean up
if (fSaxParser) {
delete fSaxParser;
fSaxParser = 0;
}
if (fStartupHandler) {
delete fStartupHandler;
fStartupHandler = 0;
}
}
return status;
}
//-------------------------------------------------------------
/**
* <p>
*/
int PMsr2Data::ReadMsrFile(const string &infile)
{
int status;
fMsrHandler = new PMsrHandler(infile.c_str());
status = fMsrHandler->ReadMsrFile();
if (status != PMUSR_SUCCESS) {
switch (status) {
case PMUSR_MSR_FILE_NOT_FOUND:
cout << endl << ">> msr2data: **ERROR** Could not find " << infile << endl;
break;
case PMUSR_MSR_SYNTAX_ERROR:
cout << endl << ">> msr2data: **SYNTAX ERROR** in file " << infile << ", full stop here." << endl;
break;
default:
cout << endl << ">> msr2data: **UNKOWN ERROR** when trying to read the msr-file" << endl;
break;
}
}
return status;
}
//-------------------------------------------------------------
/**
* <p>
*/
bool PMsr2Data::ReadRunDataFile()
{
if (fStartupHandler)
fDataHandler = new PRunDataHandler(fMsrHandler, fStartupHandler->GetDataPathList());
else
fDataHandler = new PRunDataHandler(fMsrHandler);
bool success = fDataHandler->IsAllDataAvailable();
if (!success) {
cout << endl << ">> msr2data: **WARNING** Could not read all data files, will continue without the data file information..." << endl;
delete fDataHandler;
fDataHandler = 0;
}
return success;
}
//-------------------------------------------------------------
/**
* <p>
*/
bool PMsr2Data::PrepareNewInputFile(unsigned int tempRun) const
{
if (fRunVectorIter == fRunVector.end())
return false;
if (*fRunVectorIter == tempRun)
return true;
const int N(4); // number of digits for the runnumber
ostringstream strInfile;
strInfile << tempRun << fFileExtension << ".msr";
ifstream in(strInfile.str().c_str());
if (!in) {
cout << endl;
cout << ">> msr2data: **ERROR** The template msr-file " << strInfile.str() << " cannot be opened! Please check!" << endl;
return false;
}
ostringstream strOutfile;
strOutfile << *fRunVectorIter << fFileExtension << ".msr";
ofstream out(strOutfile.str().c_str());
if (!out) {
cout << endl;
cout << ">> msr2data: **ERROR** The new msr file " << strOutfile.str() << " cannot be opened! Please check!" << endl;
return false;
}
ostringstream tempRunNumber;
tempRunNumber.fill('0');
tempRunNumber.setf(ios::internal, ios::adjustfield);
tempRunNumber.width(N);
tempRunNumber << tempRun;
ostringstream newRunNumber;
newRunNumber.fill('0');
newRunNumber.setf(ios::internal, ios::adjustfield);
newRunNumber.width(N);
newRunNumber << *fRunVectorIter;
cout << endl << ">> msr2data: **INFO** Generating new input msr file " << strOutfile.str() << endl;
string line, firstOnLine;
istringstream strLine;
// write the run number in the TITLE line of the msr-file (is overwritten later by musrfit if called with the -t option)
getline(in, line);
out << *fRunVectorIter << endl;
while (getline(in, line)) {
if (line.empty()) {
out << endl;
continue;
}
if (!line.compare("SET BATCH") || !line.compare("END RETURN"))
continue;
strLine.clear();
strLine.str(line);
strLine >> firstOnLine;
if (!firstOnLine.compare("RUN")) {
string::size_type loc = line.rfind(tempRunNumber.str());
if ( loc != string::npos ) {
line.replace(loc, N, newRunNumber.str());
} else {
cout << endl;
cout << ">> msr2data: **WARNING** The template run file number does not match the \"file index\"" << endl;
cout << ">> msr2data: **WARNING** Unexpected things may happen... (for sure)" << endl;
}
}
out << line << endl;
}
in.close();
out.close();
return true;
}
//-------------------------------------------------------------
/**
* <p>
*/
void PMsr2Data::WriteOutput(const string &outfile, bool db, bool withHeader) const
{
if (!to_lower_copy(outfile).compare("none")) {
fRunVectorIter++;
return;
}
string msrTitle(fMsrHandler->GetMsrTitle()->Data());
string msrFileName(fMsrHandler->GetFileName().Data());
unsigned int msrNoOfParams(fMsrHandler->GetNoOfParams());
PMsrParamList *msrParamList(fMsrHandler->GetMsrParamList());
vector<string> dataParamNames;
vector<string> dataParamLabels;
vector<double> dataParam, dataParamErr;
if (fDataHandler) {
PMsrRunList *msrRunList(fMsrHandler->GetMsrRunList());
PRawRunData *rawRunData(fDataHandler->GetRunData((*msrRunList)[0].fRunName[0]));
switch (rawRunData->fTemp.size()) {
case 1:
dataParamNames.push_back("dataT");
dataParamLabels.push_back("T (K)");
dataParam.push_back(rawRunData->fTemp[0].first);
dataParamErr.push_back(rawRunData->fTemp[0].second);
break;
default:
ostringstream oss;
for (unsigned int i(0); i<rawRunData->fTemp.size(); i++) {
oss << "dataT" << i;
dataParamNames.push_back(oss.str());
oss.str("");
oss << "T" << i << " (K)";
dataParamLabels.push_back(oss.str());
oss.str("");
dataParam.push_back(rawRunData->fTemp[i].first);
dataParamErr.push_back(rawRunData->fTemp[i].second);
}
break;
}
double value;
value = rawRunData->fField;
if (value != -9.9e99) {
dataParamNames.push_back("dataB");
dataParamLabels.push_back("mu0 H (G)");
dataParam.push_back(value);
}
value = rawRunData->fEnergy;
if (value != -999.0) {
dataParamNames.push_back("dataE");
dataParamLabels.push_back("Implantation Energy (keV)");
dataParam.push_back(value);
}
value = rawRunData->fTransport;
if (value != -999.0) {
dataParamNames.push_back("dataTr");
dataParamLabels.push_back("Transport (kV)");
dataParam.push_back(value);
}
vector<double> ra(rawRunData->fRingAnode);
if (ra.size() > 1) {
dataParamNames.push_back("dataRALRAR");
dataParamLabels.push_back("RAL-RAR (kV)");
dataParam.push_back(ra[0]-ra[1]);
}
if (ra.size() == 4) {
dataParamNames.push_back("dataRATRAB");
dataParamLabels.push_back("RAT-RAB (kV)");
dataParam.push_back(ra[2]-ra[3]);
}
}
// get the independent variable values from the runlist file if needed
vector<double> indVarValues;
if (fRunListFile) {
string line;
vector<string> splitVec;
unsigned int runNo;
double val;
istringstream strLine;
while (getline(*fRunListFileStream, line)) {
trim(line);
if (line.empty())
continue;
else if (line.at(0) == '#' || !line.substr(0,3).compare("RUN"))
continue;
else {
split( splitVec, line, is_any_of("#") ); // split the string if any comments appear on the line
strLine.clear();
strLine.str(splitVec[0]);
strLine >> runNo;
if (runNo != *fRunVectorIter) {
cout << endl;
cout << ">> msr2data: **ERROR** The run number in the runlist file does not match the one which should be processed..." << endl;
cout << ">> msr2data: **ERROR** Something is very strange... Please report this bug!" << endl;
fRunVectorIter = fRunVector.end();
return;
}
while (strLine >> val) {
indVarValues.push_back(val);
}
if (indVarValues.size() != fIndVar.size()) {
cout << endl;
cout << ">> msr2data: **ERROR** The number of data entries in the runlist file for the run number " << runNo << endl;
cout << ">> msr2data: **ERROR** does not match the number of labels given in the RUN-line! Please check the file!" << endl;
fRunVectorIter = fRunVector.end();
return;
}
break;
}
}
}
// The RUNLIST file stream and the run vector iterator might get out of synchronization, if the following check is placed before the above block...
PMsrStatisticStructure *msrStatistic(fMsrHandler->GetMsrStatistic());
if (!msrStatistic->fValid) {
cout << endl;
cout << ">> msr2data: **WARNING** The fit of run " << *fRunVectorIter << " has not converged!" << endl;
cout << ">> msr2data: **WARNING** Its parameter data have not been appended to the output file " << outfile << endl;
fRunVectorIter++;
delete fMsrHandler;
fMsrHandler = 0;
if (fDataHandler) {
delete fDataHandler;
fDataHandler = 0;
}
// clean up some vectors
dataParamNames.clear();
dataParamLabels.clear();
dataParam.clear();
dataParamErr.clear();
indVarValues.clear();
return;
}
// open the DB or dat file and write the data
ofstream outFile(outfile.c_str(), ios::app);
if (!outFile) {
cout << endl;
cout << ">> msr2data: The output file " << outfile << " cannot be opened! Please check!" << endl;
fRunVectorIter = fRunVector.end();
return;
}
if (db) {
if (withHeader && (fRunVectorIter == fRunVector.begin())) {
outFile << "TITLE" << endl;
outFile << ">>>Put your title here<<<" << endl << endl;
outFile << "Abstract" << endl;
outFile << ">>>Put your abstract here<<<" << endl << endl;
outFile << "LABELS" << endl;
if (fDataHandler) {
for (unsigned int i(0); i < dataParamLabels.size(); ++i) {
outFile << dataParamLabels[i] << endl;
}
}
if (fRunListFile) {
for (unsigned int i(0); i < fIndVar.size(); ++i) {
outFile << fIndVar[i] << endl;
}
}
for (unsigned int i(0); i < msrNoOfParams; ++i) {
outFile << (*msrParamList)[i].fName.Data() << endl;
}
if (msrStatistic->fChisq)
outFile << "CHISQ" << endl;
else
outFile << "maxLH" << endl;
outFile << "NDF" << endl;
if (msrStatistic->fChisq)
outFile << "CHISQred" << endl;
else
outFile << "maxLHred" << endl;
outFile << "RUN" << endl;
outFile << endl << "Data";
if (fDataHandler) {
for (unsigned int i(0); i < dataParamNames.size(); ++i) {
outFile << " " << dataParamNames[i];
}
}
if (fRunListFile) {
for (unsigned int i(0); i < fIndVar.size(); ++i) {
outFile << " " << fIndVar[i];
}
}
for (unsigned int i(0); i < msrNoOfParams; ++i) {
outFile << " " << (*msrParamList)[i].fName.Data();
}
if (msrStatistic->fChisq)
outFile << " " << "CHISQ";
else
outFile << " " << "maxLH";
outFile << " " << "NDF";
if (msrStatistic->fChisq)
outFile << " " << "CHISQred";
else
outFile << " " << "maxLHred";
outFile << " " << "RUN" << endl;
outFile << "\\-e" << endl;
}
if (fDataHandler) {
for (unsigned int i(0); i < dataParam.size(); ++i) {
if (i < dataParamErr.size())
outFile << dataParamNames[i] << " = " << dataParam[i] << ", " \
<< dataParamErr[i] << ", " << dataParamErr[i] << ",\\" << endl;
else
outFile << dataParamNames[i] << " = " << dataParam[i] << ", 0, 0,\\" << endl;
}
}
if (fRunListFile) {
for (unsigned int i(0); i < indVarValues.size(); ++i) {
outFile << fIndVar[i] << " = " << indVarValues[i] << ", 0, 0,\\" << endl;
}
}
for (unsigned int i(0); i < msrNoOfParams; ++i) {
outFile << (*msrParamList)[i].fName.Data() << " = " << (*msrParamList)[i].fValue << ", ";
if ((*msrParamList)[i].fPosErrorPresent)
outFile << (*msrParamList)[i].fPosError << ", ";
else
outFile << fabs((*msrParamList)[i].fStep) << ", ";
outFile << fabs((*msrParamList)[i].fStep) << ",\\" << endl;
}
if (msrStatistic->fChisq)
outFile << "CHISQ = " << msrStatistic->fMin << ", 0, 0,\\" << endl;
else
outFile << "maxLH = " << msrStatistic->fMin << ", 0, 0,\\" << endl;
outFile << "NDF = " << msrStatistic->fNdf << ", 0, 0,\\" << endl;
if (msrStatistic->fChisq)
outFile << "CHISQred = " << msrStatistic->fMin/msrStatistic->fNdf << ", 0, 0,\\" << endl;
else
outFile << "maxLHred = " << msrStatistic->fMin/msrStatistic->fNdf << ", 0, 0,\\" << endl;
outFile << *fRunVectorIter << ",,, " << msrTitle << endl;
} else { // no DB file but ASCII file with data columns
unsigned int length(0), maxlength(12);
for (unsigned int i(0); i < dataParamNames.size(); ++i) {
length = dataParamNames[i].length();
if (length > maxlength)
maxlength = length;
}
for (unsigned int i(0); i < fIndVar.size(); ++i) {
length = fIndVar[i].length();
if (length > maxlength)
maxlength = length;
}
string s;
for (unsigned int i(0); i < msrNoOfParams; ++i) {
s = (*msrParamList)[i].fName.Data();
length = s.length();
if (length > maxlength)
maxlength = length;
}
if (maxlength < 13)
maxlength = 13; // will use a minimum field width of 13 which corresponds to: -1.23456e-07 + ' '
else
maxlength += 1; // maximum length of parameter names + ' '
if (withHeader && (fRunVectorIter == fRunVector.begin())) {
if (fDataHandler) {
for (unsigned int i(0); i < dataParamNames.size(); ++i) {
s = dataParamNames[i];
if (i < dataParamErr.size()) {
outFile << setw(maxlength) << left << s << setw(maxlength + 3) << left << s + "Err";
} else
outFile << setw(maxlength) << left << s;
}
}
if (fRunListFile) {
for (unsigned int i(0); i < fIndVar.size(); ++i) {
outFile << setw(maxlength) << left << fIndVar[i];
}
}
for (unsigned int i(0); i < msrNoOfParams; ++i) {
s = (*msrParamList)[i].fName.Data();
outFile << setw(maxlength) << left << s \
<< setw(maxlength + 6) << left << s + "PosErr" \
<< setw(maxlength + 6) << left << s + "NegErr";
}
s.clear();
if (msrStatistic->fChisq)
outFile << setw(maxlength) << left << "CHISQ";
else
outFile << setw(maxlength) << left << "maxLH";
outFile << setw(maxlength) << left << "NDF";
if (msrStatistic->fChisq)
outFile << setw(maxlength) << left << "CHISQred";
else
outFile << setw(maxlength) << left << "maxLHred";
outFile << setw(maxlength) << left << "RUN" << endl;
}
if (fDataHandler) {
for (unsigned int i(0); i < dataParam.size(); ++i) {
if (i < dataParamErr.size()) {
writeValues(outFile, dataParam[i], maxlength);
writeValues(outFile, dataParamErr[i], maxlength + 3);
} else {
writeValues(outFile, dataParam[i], maxlength);
}
}
}
if (fRunListFile) {
for (unsigned int i(0); i < indVarValues.size(); ++i) {
writeValues(outFile, indVarValues[i], maxlength);
}
}
for (unsigned int i(0); i < msrNoOfParams; ++i) {
writeValues(outFile, (*msrParamList)[i].fValue, maxlength);
if ((*msrParamList)[i].fPosErrorPresent)
writeValues(outFile, (*msrParamList)[i].fPosError, maxlength + 6);
else
writeValues(outFile, fabs((*msrParamList)[i].fStep), maxlength + 6);
writeValues(outFile, fabs((*msrParamList)[i].fStep), maxlength + 6);
}
writeValues(outFile, msrStatistic->fMin, maxlength);
writeValues(outFile, msrStatistic->fNdf, maxlength);
writeValues(outFile, msrStatistic->fMin/msrStatistic->fNdf, maxlength);
writeValues(outFile, *fRunVectorIter, maxlength);
outFile << endl;
}
cout << endl << ">> msr2data: **INFO** Parameter data of file " << msrFileName << " have been appended to " << outfile << endl;
fRunVectorIter++;
if (fRunVectorIter == fRunVector.end())
outFile << endl << endl;
outFile.close();
delete fMsrHandler;
fMsrHandler = 0;
if (fDataHandler) {
delete fDataHandler;
fDataHandler = 0;
}
// clean up some vectors
dataParamNames.clear();
dataParamLabels.clear();
dataParam.clear();
dataParamErr.clear();
indVarValues.clear();
return;
}

81
src/include/PMsr2Data.h Normal file
View File

@ -0,0 +1,81 @@
/***************************************************************************
PMsr2Data.h
Author: Bastian M. Wojek / Andreas Suter
e-mail: andreas.suter@psi.ch
$Id$
***************************************************************************/
/***************************************************************************
* Copyright (C) 2009 by Bastian M. Wojek / Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _PMSR2DATA_H_
#define _PMSR2DATA_H_
#include <string>
using namespace std;
#include "PRunDataHandler.h"
#include "PStartupHandler.h"
#include <TSAXParser.h>
//-------------------------------------------------------------
/**
* <p>
*/
class PMsr2Data
{
public:
PMsr2Data(const string&); // File extension
~PMsr2Data();
int SetRunNumbers(unsigned int); // single run given
int SetRunNumbers(unsigned int, unsigned int); // run list specified through first and last run number
int SetRunNumbers(const string&); // run list file given
int SetRunNumbers(const vector<unsigned int>&); // explicit run list specified using [ ]
unsigned int GetPresentRun() const;
int ParseXmlStartupFile();
int ReadMsrFile(const string&);
bool ReadRunDataFile();
bool PrepareNewInputFile(unsigned int) const; // template
void WriteOutput(const string&, bool, bool) const;
private:
string fFileExtension;
vector<unsigned int> fRunVector;
mutable vector<unsigned int>::const_iterator fRunVectorIter;
bool fRunListFile;
vector<string> fIndVar;
ifstream *fRunListFileStream;
TSAXParser *fSaxParser;
PStartupHandler *fStartupHandler;
mutable PRunDataHandler *fDataHandler;
mutable PMsrHandler *fMsrHandler;
};
#endif // _PMSR2DATA_H_

435
src/msr2data.cpp Normal file
View File

@ -0,0 +1,435 @@
/***************************************************************************
msr2data.cpp
Author: Bastian M. Wojek / Andreas Suter
e-mail: andreas.suter@psi.ch
$Id$
***************************************************************************/
/***************************************************************************
* Copyright (C) 2009 by Bastian M. Wojek / Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "PMsr2Data.h"
#include <algorithm>
#include <sstream>
#include <iostream>
using namespace std;
#include <boost/algorithm/string/case_conv.hpp> // for to_lower() in std::string
using namespace boost::algorithm;
//--------------------------------------------------------------------------
/**
* <p>
* std::string
*/
bool isNumber(const string &s)
{
unsigned int number;
istringstream iss;
ostringstream oss;
iss.str(s);
iss >> number;
oss << number;
if (iss.str().compare(oss.str()))
return false;
return true;
}
//--------------------------------------------------------------------------
/**
* <p>
*/
void msr2data_syntax()
{
cout << endl << "usage 1: msr2data <run> <extension> [-o<outputfile>] [data] [noheader] [nosummary] [fit-<template>[!]] [-k] [-t]";
cout << endl << "usage 2: msr2data <run1> <run2> <extension> [-o<outputfile>] [data] [noheader] [nosummary] [fit-<template>[!]] [-k] [-t]";
cout << endl << "usage 3: msr2data \\[<run1> <run2> ... <runN>\\] <extension> [-o<outputfile> ] [data] [noheader] [nosummary] [fit-<template>[!]] [-k] [-t]";
cout << endl << "usage 4: msr2data <runlist> <extension> [-o<outputfile>] [data] [noheader] [nosummary] [fit-<template>[!]] [-k] [-t]";
cout << endl;
cout << endl << " <run>, <run1>, <run2>, ... <runN> : run numbers";
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 << " data : instead of to a DB file the data are written to a simple column structure";
cout << endl << " noheader : no file header is written to the output file";
cout << endl << " nosummary : no additional data from the run data file is written to the output file";
cout << endl << " fit-<template>! : generate msr-files for the runs to be processed from the <template>-run";
cout << endl << " and call musrfit for fitting these runs";
cout << endl << " fit-<template> : same as above, but the <template>-run is only used for the first file creation - ";
cout << endl << " the succeding files are generated using the musrfit-output from the last runs";
cout << endl << " -k : if fitting is used, pass the option --keep-mn2-output to musrfit";
cout << endl << " -t : if fitting is used, pass the option --title-from-data-file to musrfit";
cout << endl;
cout << endl << " For further information please refer to";
cout << endl << " https://wiki.intranet.psi.ch/MUSR/Msr2Data";
cout << endl << endl;
}
//--------------------------------------------------------------------------
/**
* <p>
* std::vector of std::string
* bool (DB file ore not)
*/
string msr2data_outputfile(vector<string> &arg, bool db = true)
{
string outputFile;
if (db)
outputFile = "out.db";
else
outputFile = "out.dat";
vector<string>::iterator iterNext(arg.begin());
for (vector<string>::iterator iter(arg.begin()); iter != arg.end(); iter++) {
iterNext = iter + 1;
if (!iter->substr(0,2).compare("-o")) {
if (!iter->compare("-o")) {
if ((iterNext != arg.end()) && (iterNext->compare("noheader")) && (iterNext->compare("nosummary")) \
&& (iterNext->substr(0,3).compare("fit")) && (iterNext->compare("-k")) && (iterNext->compare("-t")) \
&& (iterNext->compare("data")) ) {
outputFile = *iterNext;
arg.erase(iterNext);
arg.erase(iter);
break;
} else {
cout << endl;
cout << ">> msr2data: **WARNING** You did not specify an output file! The default one (" << outputFile << ") will be used." << endl;
arg.erase(iter);
break;
}
} else {
outputFile = iter->substr(2);
arg.erase(iter);
break;
}
}
}
return outputFile;
}
//--------------------------------------------------------------------------
/**
* <p>
* std::vector of std::string
* string
*/
bool msr2data_useOption(vector<string> &arg, const string &s)
{
bool option(true);
vector<string>::iterator iter;
iter = find(arg.begin(), arg.end(), s);
if (iter != arg.end()) {
option = false;
arg.erase(iter);
}
return option;
}
//--------------------------------------------------------------------------
/**
* <p>
* std::vector of std::string
* bool
*/
unsigned int msr2data_doFitting(vector<string> &arg, bool &chainfit)
{
unsigned int temp(0);
string s;
istringstream iss;
for (vector<string>::iterator iter(arg.begin()); iter != arg.end(); iter++) {
if (!iter->substr(0,4).compare("fit-")) {
s = iter->substr(4);
string::size_type loc = s.rfind('!');
if (loc != string::npos)
chainfit = false;
else
chainfit = true;
iss.str(s);
iss >> temp;
arg.erase(iter);
break;
}
}
return temp;
}
//--------------------------------------------------------------------------
/**
* <p>
*
* \param argc
* \param argv
*/
int main(int argc, char *argv[])
{
// check the number of arguments
if (argc < 3) {
msr2data_syntax();
return 0;
}
// use a string-vector for the arguments to get rid of god damn char* as far as possible...
vector<string> arg;
for (int i(1); i<argc; i++) {
arg.push_back(argv[i]);
}
unsigned int runTAG;
vector<unsigned int> run_vec;
string run_list;
string msrExtension;
if (arg[0].at(0) == '[') { // In case a list of runs is given by [...]
runTAG = 1;
unsigned int firstRunNumberInArg(0), lastRunNumberInArg(0);
unsigned int rightbracket(999);
if (!arg[0].compare("["))
firstRunNumberInArg = 1;
else
arg[0]=arg[0].substr(1);
for (unsigned int i(firstRunNumberInArg); i<arg.size(); i++) {
string::size_type loc = arg[i].rfind(']');
if ( loc != string::npos ) {
rightbracket = i;
if (!arg[i].compare("]") && i > 0)
lastRunNumberInArg = i-1;
else {
arg[i]=arg[i].substr(0,loc);
lastRunNumberInArg = i;
}
break;
}
}
if (rightbracket == 999) {
cout << endl;
cout << ">> msr2data: **ERROR** You used the list specification without closing bracket (])! Quitting now." << endl;
return 0;
}
for (unsigned int i(firstRunNumberInArg); i<=lastRunNumberInArg; ++i)
run_vec.push_back(atoi(arg[i].c_str()));
msrExtension = arg[rightbracket + 1];
vector<string>::iterator iter(arg.begin());
for (unsigned int i(0); i<rightbracket + 2; i++) {
arg.erase(iter);
iter = arg.begin();
}
} else {
bool argOneIsNumber(isNumber(arg[0])), argTwoIsNumber(isNumber(arg[1]));
if (argOneIsNumber && argTwoIsNumber) { // the first two arguments are numbers: an interval of numbers is given
runTAG = 2;
if (arg.size() < 3) {
cout << endl;
cout << ">> msr2data: **ERROR** No msr-file extension specified! Quitting now..." << endl;
run_vec.clear();
arg.clear();
return 0;
}
run_vec.push_back(atoi(arg[0].c_str()));
run_vec.push_back(atoi(arg[1].c_str()));
msrExtension = arg[2];
vector<string>::iterator iter(arg.begin());
for (unsigned int i(0); i < 3; i++) {
arg.erase(iter);
iter = arg.begin();
}
} else if (argOneIsNumber && !argTwoIsNumber) { // only one run number is given
runTAG = 3;
run_vec.push_back(atoi(arg[0].c_str()));
msrExtension = arg[1];
vector<string>::iterator iter(arg.begin());
for (unsigned int i(0); i < 2; i++) {
arg.erase(iter);
iter = arg.begin();
}
} else { // assume a runlist-file with "independent variables" is given
runTAG = 4;
run_list = arg[0];
msrExtension = arg[1];
vector<string>::iterator iter(arg.begin());
for (unsigned int i(0); i < 2; i++) {
arg.erase(iter);
iter = arg.begin();
}
}
}
// check if the output format is DB or data
bool db(msr2data_useOption(arg, "data"));
// check the arguments for the "-o" option and set the output filename
string outputFile(msr2data_outputfile(arg, db));
// introduce check, if no output should be generated - in that case we do not need msrfile and rundata handlers later
bool realOutput(true);
if (!to_lower_copy(outputFile).compare("none"))
realOutput = false;
// create the msr2data-object and set the run numbers according to the runTAG above
PMsr2Data msr2dataHandler(msrExtension);
int status;
switch(runTAG) {
case 1:
status = msr2dataHandler.SetRunNumbers(run_vec);
break;
case 2:
status = msr2dataHandler.SetRunNumbers(run_vec[0], run_vec[1]);
break;
case 3:
status = msr2dataHandler.SetRunNumbers(run_vec[0]);
break;
case 4:
status = msr2dataHandler.SetRunNumbers(run_list);
break;
default:
cout << endl;
cout << ">> msr2data: **ERROR** None of the possible run list specifications has been detected! Quitting now..." << endl;
run_vec.clear();
arg.clear();
return 0;
}
if (status == 1) {
cout << endl;
cout << ">> msr2data: **ERROR** The run numbers are out of range! Quitting..." << endl;
run_vec.clear();
arg.clear();
return status;
}
run_vec.clear();
// check if fitting should be done and in case, which template run number to use
unsigned int temp(0);
bool chainfit(true);
string musrfitOptions;
temp = msr2data_doFitting(arg, chainfit);
// check if any options should be passed to musrfit
if (temp) {
if (!msr2data_useOption(arg, "-k"))
musrfitOptions.append("-k ");
if (!msr2data_useOption(arg, "-t"))
musrfitOptions.append("-t ");
}
bool writeHeader(false), writeSummary(false);
if (realOutput) {
// check the arguments for the "noheader" option
writeHeader = msr2data_useOption(arg, "noheader");
// check the arguments for the "nosummary" option
writeSummary = msr2data_useOption(arg, "nosummary");
}
// read musrfit startup file
if (writeSummary) {
status = msr2dataHandler.ParseXmlStartupFile();
}
// Processing the run list, do the fitting and write the data to the DB or data output file
bool firstrun(true);
unsigned int oldtemp(temp);
while (msr2dataHandler.GetPresentRun()) {
ostringstream strInfile;
strInfile << msr2dataHandler.GetPresentRun() << msrExtension << ".msr";
// if fitting should be done, prepare a new input file
if (temp) {
bool success(true);
if (firstrun || !chainfit)
success = msr2dataHandler.PrepareNewInputFile(temp);
else
success = msr2dataHandler.PrepareNewInputFile(oldtemp);
if (firstrun)
firstrun = false;
oldtemp = msr2dataHandler.GetPresentRun();
if (!success) {
cout << endl << ">> msr2data: **ERROR** Input file generation has not been successful! Quitting..." << endl;
arg.clear();
return -1;
}
// and do the fitting
ostringstream oss;
oss << "musrfit" << " " << strInfile.str() << " " << musrfitOptions;
cout << endl << ">> msr2data: **INFO** Calling " << oss.str() << endl;
system(oss.str().c_str());
}
// read msr-file
if (realOutput) {
status = msr2dataHandler.ReadMsrFile(strInfile.str());
if (status != PMUSR_SUCCESS) {
arg.clear();
return status;
}
}
// read data files
if (writeSummary)
writeSummary = msr2dataHandler.ReadRunDataFile();
// write DB or dat file
msr2dataHandler.WriteOutput(outputFile, db, writeHeader);
}
if (!arg.empty()) {
cout << endl << ">> msr2data: **INFO** The following command line arguments have been specified but not been used: " << endl;
cout << ">> msr2data: **INFO**";
for (unsigned int i(0); i<arg.size(); i++)
cout << " " << arg[i];
cout << endl;
arg.clear();
}
cout << endl << ">> msr2data: done ..." << endl;
return 1;
}