better db-file handling

This commit is contained in:
nemu 2009-02-02 09:56:27 +00:00
parent 93abe979ba
commit 2c82b1691a
2 changed files with 98 additions and 27 deletions

View File

@ -1243,7 +1243,7 @@ bool PRunDataHandler::ReadDBFile()
runData.fDataNonMusr.fYIndex = -1; runData.fDataNonMusr.fYIndex = -1;
int lineNo = 0; int lineNo = 0;
int dataNo = 0; int idx;
int dbTag = -1; int dbTag = -1;
char instr[512]; char instr[512];
TString line, workStr; TString line, workStr;
@ -1260,6 +1260,7 @@ bool PRunDataHandler::ReadDBFile()
// get next line from file // get next line from file
f.getline(instr, sizeof(instr)); f.getline(instr, sizeof(instr));
line = TString(instr); line = TString(instr);
// cout << endl << instr;
lineNo++; lineNo++;
// check if comment line // check if comment line
@ -1362,10 +1363,17 @@ bool PRunDataHandler::ReadDBFile()
} }
if (labelledFormat) { // handle labelled formated data if (labelledFormat) { // handle labelled formated data
// check if run line // check if run line
const char *str = workStr.Data(); const char *str = workStr.Data();
if (isdigit(str[0])) { // run line if (isdigit(str[0])) { // run line
TString run("run");
idx = GetDataTagIndex(run, runData.fDataNonMusr.fDataTags);
if (idx == -1) {
cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo << ":";
cout << endl << ">> " << workStr.Data();
cout << endl << ">> found potential run data line without run data tag.";
return false;
}
// split string in tokens // split string in tokens
tokens = workStr.Tokenize(","); // line has structure: runNo,,, runTitle tokens = workStr.Tokenize(","); // line has structure: runNo,,, runTitle
ostr = dynamic_cast<TObjString*>(tokens->At(0)); ostr = dynamic_cast<TObjString*>(tokens->At(0));
@ -1379,12 +1387,11 @@ bool PRunDataHandler::ReadDBFile()
return false; return false;
} }
val = tstr.Atof(); val = tstr.Atof();
runData.fDataNonMusr.fData[dataNo].push_back(val); runData.fDataNonMusr.fData[idx].push_back(val);
runData.fDataNonMusr.fErrData[dataNo].push_back(0.0); runData.fDataNonMusr.fErrData[idx].push_back(1.0);
dataNo = 0; // next run block starts } else { // tag = data line
} else { // tag = data line // remove all possible spaces
// remove all possible spaces workStr.ReplaceAll(" ", "");
workStr.ReplaceAll(" ", "");
// split string in tokens // split string in tokens
tokens = workStr.Tokenize("=,"); // line has structure: tag = val,err1,err2, tokens = workStr.Tokenize("=,"); // line has structure: tag = val,err1,err2,
if (tokens->GetEntries() < 3) { if (tokens->GetEntries() < 3) {
@ -1394,11 +1401,23 @@ bool PRunDataHandler::ReadDBFile()
delete tokens; delete tokens;
return false; return false;
} }
ostr = dynamic_cast<TObjString*>(tokens->At(0));
tstr = ostr->GetString();
idx = GetDataTagIndex(tstr, runData.fDataNonMusr.fDataTags);
// cout << endl << "->> " << tstr.Data() << ", " << idx;
if (idx == -1) {
cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo << ":";
cout << endl << ">> " << workStr.Data();
cout << endl << ">> data tag error: " << tstr.Data() << " seems not present in the data tag list";
delete tokens;
return false;
}
switch (tokens->GetEntries()) { switch (tokens->GetEntries()) {
case 3: // tag = val,,, case 3: // tag = val,,,
ostr = dynamic_cast<TObjString*>(tokens->At(1)); ostr = dynamic_cast<TObjString*>(tokens->At(1));
tstr = ostr->GetString(); tstr = ostr->GetString();
// cout << endl << ">>> " << tstr.Data();
if (!tstr.IsFloat()) { if (!tstr.IsFloat()) {
cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo << ":"; cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo << ":";
cout << endl << ">> " << workStr.Data(); cout << endl << ">> " << workStr.Data();
@ -1408,8 +1427,11 @@ bool PRunDataHandler::ReadDBFile()
return false; return false;
} }
val = tstr.Atof(); val = tstr.Atof();
runData.fDataNonMusr.fData[dataNo].push_back(val); // cout << endl << ">>> " << val << ", " << idx << endl;
runData.fDataNonMusr.fErrData[dataNo].push_back(0.0); runData.fDataNonMusr.fData[idx].push_back(val);
// cout << endl << ">>> " << endl;
runData.fDataNonMusr.fErrData[idx].push_back(1.0);
// cout << endl << ">>> done <<<" << endl;
break; break;
case 4: // tag = val,err,, case 4: // tag = val,err,,
case 5: // tag = val,err1,err2, case 5: // tag = val,err1,err2,
@ -1425,7 +1447,7 @@ bool PRunDataHandler::ReadDBFile()
return false; return false;
} }
val = tstr.Atof(); val = tstr.Atof();
runData.fDataNonMusr.fData[dataNo].push_back(val); runData.fDataNonMusr.fData[idx].push_back(val);
// handle err1 (err2 will be ignored for the time being) // handle err1 (err2 will be ignored for the time being)
ostr = dynamic_cast<TObjString*>(tokens->At(2)); ostr = dynamic_cast<TObjString*>(tokens->At(2));
tstr = ostr->GetString(); tstr = ostr->GetString();
@ -1438,7 +1460,7 @@ bool PRunDataHandler::ReadDBFile()
return false; return false;
} }
val = tstr.Atof(); val = tstr.Atof();
runData.fDataNonMusr.fErrData[dataNo].push_back(val); runData.fDataNonMusr.fErrData[idx].push_back(val);
break; break;
default: default:
cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo << ":"; cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo << ":";
@ -1448,7 +1470,6 @@ bool PRunDataHandler::ReadDBFile()
return false; return false;
break; break;
} }
dataNo++;
} }
} else { // handle row formated data } else { // handle row formated data
@ -1483,7 +1504,7 @@ bool PRunDataHandler::ReadDBFile()
ostr = dynamic_cast<TObjString*>(tokens->At(i+1)); ostr = dynamic_cast<TObjString*>(tokens->At(i+1));
tstr = ostr->GetString(); tstr = ostr->GetString();
if (tstr.IsWhitespace()) { if (tstr.IsWhitespace()) {
runData.fDataNonMusr.fErrData[j].push_back(0.0); runData.fDataNonMusr.fErrData[j].push_back(1.0);
} else if (tstr.IsFloat()) { } else if (tstr.IsFloat()) {
runData.fDataNonMusr.fErrData[j].push_back(tstr.Atof()); runData.fDataNonMusr.fErrData[j].push_back(tstr.Atof());
} else { } else {
@ -1507,15 +1528,39 @@ bool PRunDataHandler::ReadDBFile()
// cout << endl << ">> run title: '" << runData.fRunTitle.Data() << "'"; // cout << endl << ">> run title: '" << runData.fRunTitle.Data() << "'";
// cout << endl; // cout << endl;
//
// cout << endl << ">> first data vector:"; // check that the number of labels == the number of data tags
// cout << endl << ">> label:" << runData.fDataNonMusr.fLabels[28].Data(); if (runData.fDataNonMusr.fLabels.size() != runData.fDataNonMusr.fDataTags.size()) {
// for (unsigned int i=0; i<runData.fDataNonMusr.fData[28].size(); i++) { cout << endl << "PRunDataHandler::ReadDBFile **ERROR**";
// cout << endl << ">> " << runData.fDataNonMusr.fData[28][i] << " +- " << runData.fDataNonMusr.fErrData[28][i]; cout << endl << ">> number of LABELS found = " << runData.fDataNonMusr.fLabels.size();
// } cout << endl << ">> number of Data tags found = " << runData.fDataNonMusr.fDataTags.size();
// cout << endl; cout << endl << ">> They have to be equal!!";
// if (tokens) {
// assert(0); delete tokens;
tokens = 0;
}
return false;
}
// check if all vectors have the same size
for (unsigned int i=1; i<runData.fDataNonMusr.fData.size(); i++) {
if (runData.fDataNonMusr.fData[i].size() != runData.fDataNonMusr.fData[i-1].size()) {
cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo;
cout << endl << ">> label: " << runData.fDataNonMusr.fDataTags[i-1].Data() << ", number data elements = " << runData.fDataNonMusr.fData[i-1].size();
cout << endl << ">> label: " << runData.fDataNonMusr.fDataTags[i].Data() << ", number data elements = " << runData.fDataNonMusr.fData[i].size();
cout << endl << ">> They have to be equal!!";
success = false;
break;
}
if (runData.fDataNonMusr.fErrData[i].size() != runData.fDataNonMusr.fErrData[i-1].size()) {
cout << endl << "PRunDataHandler::ReadDBFile **ERROR** in line no " << lineNo;
cout << endl << ">> label: " << runData.fDataNonMusr.fDataTags[i-1].Data() << ", number data elements = " << runData.fDataNonMusr.fData[i-1].size();
cout << endl << ">> label: " << runData.fDataNonMusr.fDataTags[i].Data() << ", number error data elements = " << runData.fDataNonMusr.fErrData[i].size();
cout << endl << ">> They have to be equal!!";
success = false;
break;
}
}
// clean up tokens // clean up tokens
if (tokens) { if (tokens) {
@ -1532,7 +1577,7 @@ bool PRunDataHandler::ReadDBFile()
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// StripWhitespace // StripWhitespace (private)
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* <p> * <p>
@ -1599,7 +1644,7 @@ bool PRunDataHandler::StripWhitespace(TString &str)
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// IsWhitespace // IsWhitespace (private)
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* <p> * <p>
@ -1620,7 +1665,7 @@ bool PRunDataHandler::IsWhitespace(const char *str)
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// ToDouble // ToDouble (private)
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* <p> * <p>
@ -1666,7 +1711,7 @@ double PRunDataHandler::ToDouble(TString &str, bool &ok)
} }
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
// ToInt // ToInt (private)
//-------------------------------------------------------------------------- //--------------------------------------------------------------------------
/** /**
* <p> * <p>
@ -1710,3 +1755,28 @@ int PRunDataHandler::ToInt(TString &str, bool &ok)
return value; return value;
} }
//--------------------------------------------------------------------------
// GetDataTagIndex (private)
//--------------------------------------------------------------------------
/**
* <p>
*
* \param str
* \param dataTags
*/
int PRunDataHandler::GetDataTagIndex(TString &str, PStringVector &dataTags)
{
int result = -1;
// check all the other possible data tags
for (unsigned int i=0; i<dataTags.size(); i++) {
if (!dataTags[i].CompareTo(str, TString::kIgnoreCase)) {
result = i;
break;
}
}
return result;
}

View File

@ -74,6 +74,7 @@ class PRunDataHandler
virtual bool IsWhitespace(const char *str); virtual bool IsWhitespace(const char *str);
virtual double ToDouble(TString &str, bool &ok); virtual double ToDouble(TString &str, bool &ok);
virtual int ToInt(TString &str, bool &ok); virtual int ToInt(TString &str, bool &ok);
virtual int GetDataTagIndex(TString &str, PStringVector &fLabels);
}; };
#endif // _PRUNDATAHANDLER_H_ #endif // _PRUNDATAHANDLER_H_