36 fs::path tmppath(searchdir.Data());
37 if( fs::exists(tmppath) && fs::is_directory(tmppath)) {
38 std::cout << tmppath.string()
39 <<
" is a directory; adding it to the search path\n";
41 }
else if( fs::exists(tmppath)) {
42 std::cout<<tmppath.string()<<
" exists but is not a directory.\n";
44 std::cout<<tmppath.string()<<
" doesn't exist.\n";
56 if (varvalue.IsDigit()){
57 value = varvalue.Atoi();
58 }
else if (varvalue.BeginsWith(
"0x") || varvalue.BeginsWith(
"0X")
59 || varvalue.BeginsWith(
"x") || varvalue.BeginsWith(
"X")
62 Int_t end = varvalue.Index(
" ");
63 std::istringstream stream1;
65 stream1.str(varvalue.Data());
72 tmp += varvalue(start, end-start);
74 end = varvalue.Index(
" ", start);
77 end = varvalue.Length();
78 tmp += varvalue(start, end-start);
79 stream1.str(tmp.Data());
81 stream1 >> std::hex >> value;
123 if (name.find(
"/") == 0) {
125 QwWarning <<
"Could not open absolute path " << name <<
". "
126 <<
"Parameter file will remain empty." <<
QwLog::endl;
140 std::string file_stem = file.stem().string();
141 std::string file_ext = file.extension().string();
144 Int_t best_score = 0;
150 if (score > best_score) {
154 }
else if (score == best_score) {
156 QwWarning <<
"Equally likely parameter files encountered: " << best_path.string()
158 QwMessage <<
"Analysis will use parameter file: " << best_path.string()
164 if (best_score == 0) {
165 QwError <<
"Could not find any parameter file with name " << name <<
". "
166 <<
"Parameter file will remain empty." <<
QwLog::endl;
173 if (best_score > 0 &&
OpenFile(best_path) ==
false) {
174 QwError <<
"Could not open parameter file " << best_path.string() <<
". "
175 <<
"Parameter file will remain empty." <<
QwLog::endl;
196 Bool_t local_debug =
false;
198 Bool_t status =
false;
200 Bool_t check_whether_path_exists_and_is_a_regular_file =
false;
203 check_whether_path_exists_and_is_a_regular_file = fs::exists(file) && fs::is_regular_file(file);
205 if (check_whether_path_exists_and_is_a_regular_file) {
211 fFile.open(file.string().c_str());
213 QwError <<
"QwParameterFile::OpenFile Unable to read parameter file "
219 std::cout <<
"------before close------------" << std::endl;
220 std::cout <<
fStream.str() << std::endl;
229 QwError <<
"QwParameterFile::OpenFile Unable to open parameter file "
234 std::cout <<
"-------after close ----------" << std::endl;
235 std::cout <<
fStream.str() << std::endl;
251 const fs::path& directory,
252 const std::string& file_stem,
253 const std::string& file_ext,
257 if (! fs::exists(directory))
return false;
263 int open_ended_latest_start = 0;
264 int open_ended_range_score = 0;
268 fs::directory_iterator end_iterator;
269 for (fs::directory_iterator file_iterator(directory);
270 file_iterator != end_iterator;
275 std::string file_name = file_iterator->path().filename().string();
277 size_t pos_stem = file_name.find(file_stem);
278 if (pos_stem != 0)
continue;
280 size_t pos_ext = file_name.rfind(file_ext);
281 if (pos_ext != file_name.length() - file_ext.length())
continue;
284 const int score_no_run_label = 1;
288 const int score_open_ended_run_range_min = 1000;
289 const int score_open_ended_run_range_max = 9000;
293 const int score_closed_run_range_min = 10000;
294 const int score_closed_run_range_max = 90000;
296 const int score_single_run_label = INT_MAX;
299 size_t label_length = pos_ext - file_stem.length();
301 if (label_length == 0) {
302 score = score_no_run_label;
305 if (file_name.at(pos_stem + file_stem.length()) ==
'.') {
306 std::string label = file_name.substr(pos_stem + file_stem.length() + 1, label_length - 1);
309 if ((range.first <= run) && (run <= range.second)) {
312 if (range.first == range.second) {
313 score = score_single_run_label;
316 }
else if (range.second < INT_MAX) {
317 int number_of_runs = abs(range.second - range.first);
318 score = score_closed_run_range_max - number_of_runs;
319 if (score < score_closed_run_range_min) {
320 score = score_closed_run_range_min;
326 }
else if (range.second == INT_MAX) {
328 if (range.first > open_ended_latest_start) {
329 open_ended_latest_start = range.first;
330 open_ended_range_score++;
331 score = score_open_ended_run_range_min + open_ended_range_score;
332 if (score > score_open_ended_run_range_max) {
333 score = score_open_ended_run_range_max;
337 }
else score = score_open_ended_run_range_min;
348 if (score > best_score) {
349 best_path = *file_iterator;
366 const std::string& chars,
368 TString::EStripType head_tail)
378 mypos = token.find_first_not_of(chars);
379 if (head_tail & TString::kLeading) token.erase(0,mypos);
381 mypos = token.find_last_not_of(chars);
382 mypos = token.find_first_of(chars,mypos);
383 if (mypos != std::string::npos && (head_tail & TString::kTrailing)){
390 TString::EStripType head_tail)
397 std::string commentchars =
"";
398 commentchars += commentchar;
405 size_t mypos =
fLine.find_first_of(commentchars);
406 if (mypos != std::string::npos){
426 Bool_t status=kFALSE;
435 TRegexp regexp(vline);
437 if (vname.Contains(regexp)){
449 const std::string& separatorchars,
453 std::string tmpvar, tmpval;
456 varname = tmpvar.c_str();
457 varvalue = tmpval.c_str();
463 const std::string& separatorchars,
464 std::string &varname,
465 std::string &varvalue)
467 Bool_t status = kFALSE;
468 size_t equiv_pos1 =
fLine.find_first_of(separatorchars);
469 if (equiv_pos1 != std::string::npos){
470 size_t equiv_pos2 =
fLine.find_first_not_of(separatorchars,equiv_pos1);
471 if (equiv_pos2 != std::string::npos){
472 varname =
fLine.substr(0,equiv_pos1);
473 varvalue =
fLine.substr(equiv_pos2);
483 const std::string& separatorchars,
484 const TString& varname,
489 if (status) varvalue = tmpval.c_str();
494 const std::string& separatorchars,
495 const std::string& varname,
496 std::string& varvalue)
500 std::string this_varname;
502 if (this_varname == varname)
return kTRUE;
517 std::string secname_tmp = secname.Data();
519 secname = secname_tmp;
526 Bool_t status = kFALSE;
528 if (equiv_pos1 != std::string::npos) {
530 if (equiv_pos2 != std::string::npos && equiv_pos2 - equiv_pos1 > 1) {
531 secname =
fLine.substr(equiv_pos1 + 1, equiv_pos2 - equiv_pos1 - 1);
547 std::string secname_tmp = secname.Data();
549 secname = secname_tmp;
556 Bool_t status = kFALSE;
558 if (equiv_pos1 != std::string::npos) {
560 if (equiv_pos2 != std::string::npos && equiv_pos2 - equiv_pos1 > 1) {
561 secname =
fLine.substr(equiv_pos1 + 1, equiv_pos2 - equiv_pos1 - 1);
574 std::string this_secname;
576 if (this_secname == secname)
return kTRUE;
586 std::string this_secname;
588 if (this_secname == secname)
return kTRUE;
598 std::string this_secname;
600 if (this_secname == secname)
return kTRUE;
610 std::string this_secname;
612 if (this_secname == secname)
return kTRUE;
624 std::string nextheader;
626 if (add_current_line) section->AddLine(
GetLine());
639 std::string nextheader;
641 if (add_current_line) section->AddLine(
GetLine());
654 Bool_t status = kTRUE;
657 if (tmpname == secname){
658 QwDebug <<
"QwParameterFile::SkipSection: "
659 <<
"Begin skipping section " << tmpname <<
QwLog::endl;
663 QwDebug <<
"QwParameterFile::SkipSection: "
664 <<
"Reached the end of the section."
696 if (
IsEOF())
return nullptr;
703 if (
IsEOF())
return nullptr;
727 if (
IsEOF())
return nullptr;
734 if (
IsEOF())
return nullptr;
741 std::string tmpstring =
"";
744 size_t pos2 =
fLine.find_first_of(separatorchars.c_str(), pos1);
745 if (pos2 == std::string::npos){
747 tmpstring =
fLine.substr(pos1);
750 tmpstring =
fLine.substr(pos1,pos2-pos1);
761 stream << file.
fStream.rdbuf();
776 const std::string scalechars(
"kM");
777 size_t pos = value.find_first_of(scalechars);
778 if (pos == std::string::npos) {
780 retval = atoi(value.c_str());
781 }
else if (pos == value.length()-1) {
782 retval = atoi(value.substr(0,pos).c_str());
783 switch (value[pos]) {
784 case 'k': retval *= 1e3;
break;
785 case 'M': retval *= 1e6;
break;
817 std::pair<int,int> mypair;
818 size_t pos = range.find_first_of(separatorchars);
819 if (pos == std::string::npos) {
821 mypair.first =
ParseInt(range.substr(0,range.length()).c_str());
822 mypair.second = mypair.first;
824 size_t end = range.length() - pos - 1;
828 mypair.second =
ParseInt(range.substr(pos+1, end).c_str());
829 }
else if (pos == range.length() - 1) {
831 mypair.first =
ParseInt(range.substr(0,pos).c_str());
832 mypair.second = INT_MAX;
834 mypair.first =
ParseInt(range.substr(0,pos).c_str());
835 mypair.second =
ParseInt(range.substr(pos+1, end).c_str());
840 if (mypair.first < 0){
842 return std::pair<int,int>(INT_MAX,INT_MAX);
843 }
else if (mypair.first > mypair.second){
844 QwError <<
"The first value ("<< mypair.first<<
") must not be larger than the second value (" << mypair.second <<
")"
846 return std::pair<int,int>(INT_MAX,INT_MAX);
850 QwVerbose <<
"The range goes from " << mypair.first
860 Char_t delimiters[] =
"/";
868 TObjArray* all_strings = in.Tokenize(delim);
869 TObjString* last_string = (TObjString*) all_strings->Last();
870 TString return_string = last_string->GetString();
872 return return_string;
880 TList *list = fParameterFile->GetListOfLines();
881 for(Int_t i=0; i < list->GetSize(); i++) {
882 ms += list->At(i)->GetName();
885 delete fParameterFile;
886 fParameterFile = NULL;
892 std::transform(keyname.begin(), keyname.end(),
893 keyname.begin(), ::tolower);
902 std::string tmpname, tmpvalue;
908 std::transform(tmpname.begin(), tmpname.end(),
909 tmpname.begin(), ::tolower);
910 QwDebug <<
"QwParameterFile::ReadNextLine_Greedy: varname="
911 << tmpname <<
"; varvalue=" << tmpvalue <<
QwLog::endl;
A logfile class, based on an identical class in the Hermes analyzer.
#define QwVerbose
Predefined log drain for verbose messages.
#define QwError
Predefined log drain for errors.
#define QwWarning
Predefined log drain for warnings.
#define QwMessage
Predefined log drain for regular messages.
#define QwDebug
Predefined log drain for debugging output.
Parameter file parsing and management.
std::ostream & operator<<(std::ostream &stream, const QwParameterFile &file)
A color changing class for the output stream.
static std::ostream & endl(std::ostream &)
End of the line.
size_t fCurrentPos
Internal line storage.
Bool_t LineHasSectionHeader()
static const std::string kDefaultWhitespaceChars
Bool_t ReadNextLine_Single(std::string &varvalue)
std::string GetNextToken()
std::map< std::string, std::string > fKeyValuePair
static int ParseInt(const std::string &value)
Parse an integer as #[kM] with optional metric scale factor.
std::stringstream fStream
static const std::string kDefaultTokenSepChars
Bool_t LineHasModuleHeader()
static void AppendToSearchPath(const TString &searchdir)
Add a directory to the search path.
TString GetParameterFileContents()
bool OpenFile(const fs::path &path_found)
Open a file.
TString fBestParamFileName
Bool_t HasValue(TString &vname)
std::unique_ptr< QwParameterFile > ReadUntilNextModule(const bool add_current_line=false)
static const std::string kDefaultSectionChars
Bool_t ReadNextLine_Greedy(std::string &varvalue)
std::unique_ptr< QwParameterFile > ReadNextModule(std::string &secname, const bool keep_header=false)
Bool_t FileHasSectionHeader(const std::string &secname)
void TrimWhitespace(TString::EStripType head_tail=TString::kBoth)
Bool_t HasVariablePair(const std::string &separatorchars, std::string &varname, std::string &varvalue)
std::set< std::string > fBreakpointWords
int FindFile(const fs::path &dir_path, const std::string &file_stem, const std::string &file_ext, fs::path &path_found)
Find the first file in a directory that conforms to the run label.
TString LastString(TString in, char *delim)
Bool_t SkipSection(std::string secname)
Skips from the beginning of the section 'secname' until the first section that does not have that nam...
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.
std::string fSectionChars
std::string fWhitespaceChars
void AddBreakpointKeyword(std::string keyname)
QwParameterFile(const std::string &filename)
static UInt_t fCurrentRunNumber
static const std::string kDefaultCommentChars
TString fBestParamFileNameAndPath
static UInt_t GetUInt(const TString &varvalue)
Bool_t FileHasModuleHeader(const std::string &secname)
std::string fTokenSepChars
const std::string fFilename
std::unique_ptr< QwParameterFile > ReadModulePreamble()
Rewinds to the start and read until it finds next module header.
void Trim(const std::string &chars, std::string &token, TString::EStripType head_tail=TString::kBoth)
std::string fCommentChars
std::unique_ptr< QwParameterFile > ReadUntilNextSection(const bool add_current_line=false)
static const std::string kDefaultModuleChars
std::unique_ptr< QwParameterFile > ReadSectionPreamble()
Rewinds to the start and read until it finds next section header.
Bool_t FileHasVariablePair(const std::string &separatorchars, const std::string &varname, std::string &varvalue)
static std::vector< fs::path > fSearchPaths
std::unique_ptr< QwParameterFile > ReadNextSection(std::string &secname, const bool keep_header=false)