12 #include "boost/version.hpp"
37 bfs::path tmppath(searchdir.Data());
38 if( bfs::exists(tmppath) && bfs::is_directory(tmppath)) {
39 std::cout << tmppath.string()
40 <<
" is a directory; adding it to the search path\n";
42 }
else if( bfs::exists(tmppath)) {
43 std::cout<<tmppath.string()<<
" exists but is not a directory.\n";
45 std::cout<<tmppath.string()<<
" doesn't exist.\n";
57 if (varvalue.IsDigit()){
58 value = varvalue.Atoi();
59 }
else if (varvalue.BeginsWith(
"0x") || varvalue.BeginsWith(
"0X")
60 || varvalue.BeginsWith(
"x") || varvalue.BeginsWith(
"X")
63 Int_t end = varvalue.Index(
" ");
64 std::istringstream stream1;
66 stream1.str(varvalue.Data());
73 tmp += varvalue(start, end-start);
75 end = varvalue.Index(
" ", start);
78 end = varvalue.Length();
79 tmp += varvalue(start, end-start);
80 stream1.str(tmp.Data());
82 stream1 >> std::hex >> value;
93 : fCommentChars(kDefaultCommentChars),
94 fWhitespaceChars(kDefaultWhitespaceChars),
95 fTokenSepChars(kDefaultTokenSepChars),
96 fSectionChars(kDefaultSectionChars),
97 fModuleChars(kDefaultModuleChars),
112 : fCommentChars(kDefaultCommentChars),
113 fWhitespaceChars(kDefaultWhitespaceChars),
114 fTokenSepChars(kDefaultTokenSepChars),
115 fSectionChars(kDefaultSectionChars),
116 fModuleChars(kDefaultModuleChars),
121 bfs::path file(name);
124 if (name.find(
"/") == 0) {
126 QwWarning <<
"Could not open absolute path " << name <<
". "
127 <<
"Parameter file will remain empty." <<
QwLog::endl;
140 #if BOOST_VERSION >= 104600
142 std::string file_stem = file.stem().string();
143 std::string file_ext = file.extension().string();
144 #elif BOOST_VERSION >= 103600
146 std::string file_stem = file.stem();
147 std::string file_ext = file.extension();
150 std::string file_stem = bfs::basename(file);
151 std::string file_ext = bfs::extension(file);
155 Int_t best_score = 0;
161 if (score > best_score) {
165 }
else if (score == best_score) {
167 QwWarning <<
"Equally likely parameter files encountered: " << best_path.string()
169 QwMessage <<
"Analysis will use parameter file: " << best_path.string()
175 if (best_score == 0) {
176 QwError <<
"Could not find any parameter file with name " << name <<
". "
177 <<
"Parameter file will remain empty." <<
QwLog::endl;
184 if (best_score > 0 &&
OpenFile(best_path) ==
false) {
185 QwError <<
"Could not open parameter file " << best_path.string() <<
". "
186 <<
"Parameter file will remain empty." <<
QwLog::endl;
207 Bool_t local_debug =
false;
209 Bool_t status =
false;
211 Bool_t check_whether_path_exists_and_is_a_regular_file =
false;
214 #if BOOST_VERSION >= 103600
215 check_whether_path_exists_and_is_a_regular_file = bfs::exists(file) && bfs::is_regular_file(file);
216 #elif BOOST_VERSION >= 103400
217 check_whether_path_exists_and_is_a_regular_file = bfs::exists(file) && bfs::is_regular(file);
219 check_whether_path_exists_and_is_a_regular_file = bfs::exists(file);
222 if (check_whether_path_exists_and_is_a_regular_file) {
228 fFile.open(file.string().c_str());
230 QwError <<
"QwParameterFile::OpenFile Unable to read parameter file "
236 std::cout <<
"------before close------------" << std::endl;
237 std::cout <<
fStream.str() << std::endl;
246 QwError <<
"QwParameterFile::OpenFile Unable to open parameter file "
251 std::cout <<
"-------after close ----------" << std::endl;
252 std::cout <<
fStream.str() << std::endl;
268 const bfs::path& directory,
269 const std::string& file_stem,
270 const std::string& file_ext,
271 bfs::path& best_path)
274 if (! bfs::exists(directory))
return false;
280 int open_ended_latest_start = 0;
281 int open_ended_range_score = 0;
285 bfs::directory_iterator end_iterator;
286 for (bfs::directory_iterator file_iterator(directory);
287 file_iterator != end_iterator;
292 #if BOOST_VERSION >= 104600
293 std::string file_name = file_iterator->path().filename().string();
294 #elif BOOST_VERSION >= 103600
295 std::string file_name = file_iterator->filename();
297 std::string file_name = file_iterator->leaf();
300 size_t pos_stem = file_name.find(file_stem);
301 if (pos_stem != 0)
continue;
303 size_t pos_ext = file_name.rfind(file_ext);
304 if (pos_ext != file_name.length() - file_ext.length())
continue;
307 const int score_no_run_label = 1;
311 const int score_open_ended_run_range_min = 1000;
312 const int score_open_ended_run_range_max = 9000;
316 const int score_closed_run_range_min = 10000;
317 const int score_closed_run_range_max = 90000;
319 const int score_single_run_label = INT_MAX;
322 size_t label_length = pos_ext - file_stem.length();
324 if (label_length == 0) {
325 score = score_no_run_label;
328 if (file_name.at(pos_stem + file_stem.length()) ==
'.') {
329 std::string label = file_name.substr(pos_stem + file_stem.length() + 1, label_length - 1);
332 if ((range.first <= run) && (run <= range.second)) {
335 if (range.first == range.second) {
336 score = score_single_run_label;
339 }
else if (range.second < INT_MAX) {
340 int number_of_runs = abs(range.second - range.first);
341 score = score_closed_run_range_max - number_of_runs;
342 if (score < score_closed_run_range_min) {
343 score = score_closed_run_range_min;
349 }
else if (range.second == INT_MAX) {
351 if (range.first > open_ended_latest_start) {
352 open_ended_latest_start = range.first;
353 open_ended_range_score++;
354 score = score_open_ended_run_range_min + open_ended_range_score;
355 if (score > score_open_ended_run_range_max) {
356 score = score_open_ended_run_range_max;
360 }
else score = score_open_ended_run_range_min;
371 if (score > best_score) {
372 best_path = *file_iterator;
389 const std::string& chars,
391 TString::EStripType head_tail)
401 mypos = token.find_first_not_of(chars);
402 if (head_tail & TString::kLeading) token.erase(0,mypos);
404 mypos = token.find_last_not_of(chars);
405 mypos = token.find_first_of(chars,mypos);
406 if (mypos != std::string::npos && (head_tail & TString::kTrailing)){
413 TString::EStripType head_tail)
420 std::string commentchars =
"";
421 commentchars += commentchar;
428 size_t mypos =
fLine.find_first_of(commentchars);
429 if (mypos != std::string::npos){
449 Bool_t status=kFALSE;
458 TRegexp regexp(vline);
460 if (vname.Contains(regexp)){
472 const std::string& separatorchars,
476 std::string tmpvar, tmpval;
479 varname = tmpvar.c_str();
480 varvalue = tmpval.c_str();
486 const std::string& separatorchars,
487 std::string &varname,
488 std::string &varvalue)
490 Bool_t status = kFALSE;
491 size_t equiv_pos1 =
fLine.find_first_of(separatorchars);
492 if (equiv_pos1 != std::string::npos){
493 size_t equiv_pos2 =
fLine.find_first_not_of(separatorchars,equiv_pos1);
494 if (equiv_pos2 != std::string::npos){
495 varname =
fLine.substr(0,equiv_pos1);
496 varvalue =
fLine.substr(equiv_pos2);
506 const std::string& separatorchars,
507 const TString& varname,
512 if (status) varvalue = tmpval.c_str();
517 const std::string& separatorchars,
518 const std::string& varname,
519 std::string& varvalue)
523 std::string this_varname;
525 if (this_varname == varname)
return kTRUE;
540 std::string secname_tmp = secname.Data();
542 secname = secname_tmp;
549 Bool_t status = kFALSE;
550 size_t equiv_pos1 =
fLine.find_first_of(
'[');
551 if (equiv_pos1 != std::string::npos) {
552 size_t equiv_pos2 =
fLine.find_first_of(
']',equiv_pos1);
553 if (equiv_pos2 != std::string::npos && equiv_pos2 - equiv_pos1 > 1) {
554 secname =
fLine.substr(equiv_pos1 + 1, equiv_pos2 - equiv_pos1 - 1);
570 std::string secname_tmp = secname.Data();
572 secname = secname_tmp;
579 Bool_t status = kFALSE;
581 if (equiv_pos1 != std::string::npos) {
583 if (equiv_pos2 != std::string::npos && equiv_pos2 - equiv_pos1 > 1) {
584 secname =
fLine.substr(equiv_pos1 + 1, equiv_pos2 - equiv_pos1 - 1);
597 std::string this_secname;
599 if (this_secname == secname)
return kTRUE;
609 std::string this_secname;
611 if (this_secname == secname)
return kTRUE;
621 std::string this_secname;
623 if (this_secname == secname)
return kTRUE;
633 std::string this_secname;
635 if (this_secname == secname)
return kTRUE;
647 std::string nextheader;
662 std::string nextheader;
677 Bool_t status = kTRUE;
680 if (tmpname == secname){
681 QwDebug <<
"QwParameterFile::SkipSection: "
682 <<
"Begin skipping section " << tmpname <<
QwLog::endl;
686 QwDebug <<
"QwParameterFile::SkipSection: "
687 <<
"Reached the end of the section."
719 if (
IsEOF())
return 0;
726 if (
IsEOF())
return 0;
750 if (
IsEOF())
return 0;
757 if (
IsEOF())
return 0;
764 std::string tmpstring =
"";
767 size_t pos2 =
fLine.find_first_of(separatorchars.c_str(), pos1);
768 if (pos2 == std::string::npos){
770 tmpstring =
fLine.substr(pos1);
773 tmpstring =
fLine.substr(pos1,pos2-pos1);
784 stream << file.
fStream.rdbuf();
804 std::pair<int,int> mypair;
805 size_t pos = range.find_first_of(separatorchars);
806 if (pos == std::string::npos) {
808 mypair.first = atoi(range.substr(0,range.length()).c_str());
809 mypair.second = mypair.first;
811 size_t end = range.length() - pos - 1;
815 mypair.second = atoi(range.substr(pos+1, end).c_str());
816 }
else if (pos == range.length() - 1) {
818 mypair.first = atoi(range.substr(0,pos).c_str());
819 mypair.second = INT_MAX;
821 mypair.first = atoi(range.substr(0,pos).c_str());
822 mypair.second = atoi(range.substr(pos+1, end).c_str());
827 if (mypair.first < 0){
829 return std::pair<int,int>(INT_MAX,INT_MAX);
830 }
else if (mypair.first > mypair.second){
831 QwError <<
"The first value must not be larger than the second value"
833 return std::pair<int,int>(INT_MAX,INT_MAX);
837 QwVerbose <<
"The range goes from " << mypair.first
847 Char_t delimiters[] =
"/";
855 TObjArray* all_strings = in.Tokenize(delim);
856 TObjString* last_string = (TObjString*) all_strings->Last();
857 return last_string->GetString();
865 TList *list = fParameterFile->GetListOfLines();
866 for(Int_t i=0; i < list->GetSize(); i++) {
867 ms += list->At(i)->GetName();
870 delete fParameterFile;
871 fParameterFile = NULL;
877 std::transform(keyname.begin(), keyname.end(),
878 keyname.begin(), ::tolower);
887 std::string tmpname, tmpvalue;
893 std::transform(tmpname.begin(), tmpname.end(),
894 tmpname.begin(), ::tolower);
static const double ms
Time units: base unit is ms.
#define QwMessage
Predefined log drain for regular messages.
TString fBestParamFileName
static void AppendToSearchPath(const TString &searchdir)
Add a directory to the search path.
std::stringstream fStream
Bool_t LineHasModuleHeader()
std::ostream & operator<<(std::ostream &out, const QwColor &color)
Output stream operator which uses the enum-to-escape-code mapping.
TString GetParameterFileContents()
static UInt_t GetUInt(const TString &varvalue)
static const std::string kDefaultWhitespaceChars
Bool_t FileHasSectionHeader(const std::string &secname)
static const std::string kDefaultSectionChars
std::string GetNextToken()
Bool_t HasVariablePair(const std::string &separatorchars, std::string &varname, std::string &varvalue)
#define QwVerbose
Predefined log drain for verbose messages.
static const std::string kDefaultTokenSepChars
std::map< std::string, std::string > fKeyValuePair
QwParameterFile * ReadNextSection(std::string &secname, const bool keep_header=false)
TString LastString(TString in, char *delim)
QwParameterFile * ReadNextModule(std::string &secname, bool keep_header=false)
Bool_t HasValue(TString &vname)
Bool_t FileHasModuleHeader(const std::string &secname)
#define QwDebug
Predefined log drain for debugging output.
A logfile class, based on an identical class in the Hermes analyzer.
size_t fCurrentPos
Internal line storage.
QwParameterFile * ReadUntilNextSection(const bool add_current_line=false)
Bool_t ReadNextLine_Single(std::string &varvalue)
std::string fSectionChars
static UInt_t fCurrentRunNumber
static std::pair< int, int > ParseIntRange(const std::string &separatorchars, const std::string &range)
Parse a range of integers as #:# where either can be missing.
void AddBreakpointKeyword(std::string keyname)
int FindFile(const bfs::path &dir_path, const std::string &file_stem, const std::string &file_ext, bfs::path &path_found)
Find the first file in a directory that conforms to the run label.
Bool_t FileHasVariablePair(const std::string &separatorchars, const std::string &varname, std::string &varvalue)
static const std::string kDefaultCommentChars
A color changing class for the output stream.
Bool_t LineHasSectionHeader()
QwParameterFile * ReadSectionPreamble()
Rewinds to the start and read until it finds next section header.
static std::ostream & endl(std::ostream &)
End of the line.
static std::vector< bfs::path > fSearchPaths
std::set< std::string > fBreakpointWords
bool OpenFile(const bfs::path &path_found)
Open a file.
void TrimWhitespace(TString::EStripType head_tail=TString::kBoth)
static const std::string kDefaultModuleChars
Bool_t ReadNextLine_Greedy(std::string &varvalue)
QwParameterFile * ReadModulePreamble()
Rewinds to the start and read until it finds next module header.
TString fBestParamFileNameAndPath
#define QwWarning
Predefined log drain for warnings.
std::string fWhitespaceChars
void AddLine(const std::string &line)
QwParameterFile * ReadUntilNextModule(const bool add_current_line=false)
void Trim(const std::string &chars, std::string &token, TString::EStripType head_tail=TString::kBoth)
#define QwError
Predefined log drain for errors.
Bool_t SkipSection(std::string secname)
Skips from the beginning of the section 'secname' until the first section that does not have that nam...