ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Fun4AllUtils.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Fun4AllUtils.cc
1 #include "Fun4AllUtils.h"
2 
3 #include <boost/tokenizer.hpp>
4 // this is an ugly hack, the gcc optimizer has a bug which
5 // triggers the uninitialized variable warning which
6 // stops compilation because of our -Werror
7 #include <boost/version.hpp> // to get BOOST_VERSION
8 #if (__GNUC__ == 4 && __GNUC_MINOR__ == 4 && BOOST_VERSION == 105700)
9 #pragma GCC diagnostic ignored "-Wuninitialized"
10 #pragma message "ignoring bogus gcc warning in boost header lexical_cast.hpp"
11 #include <boost/lexical_cast.hpp>
12 #pragma GCC diagnostic warning "-Wuninitialized"
13 #else
14 #include <boost/lexical_cast.hpp>
15 #endif
16 
17 #include <algorithm> // for max
18 #include <iostream>
19 #include <vector>
20 
21 using namespace std;
22 
23 // relying on our standard filenames ...-<runnumber>-<segment>.<ext>
24 // extract run number and segment number from filename
25 std::pair<int, int>
27 {
28  int runnumber = 0;
29  int segment = -9999;
30  boost::char_separator<char> sep("-.");
31  boost::tokenizer<boost::char_separator<char> > tok(filename, sep);
32  // tokenizer does not have reverse iterator, so fill it in vector
33  // and reverse iterate on vector
34  vector<string> tokens;
35  for (auto& t : tok)
36  {
37  tokens.push_back(t);
38  }
39  tokens.pop_back(); // remove the file extension
40  // try to extract segment number
41  try
42  {
43  segment = boost::lexical_cast<int>((*(tokens.rbegin())));
44  }
45  catch (boost::bad_lexical_cast const&)
46  {
47  cout << "Cannot extract segment number from filename "
48  << filename << endl;
49  cout << "Segment string after parsing: input string "
50  << *(tokens.rbegin())
51  << " is not valid segment number" << endl;
52  cout << "filename " << filename << " not standard -runnumber-segment.ext"
53  << endl;
54  cout << "using " << segment << " as segment number" << endl;
55  }
56  tokens.pop_back(); // remove the segment number
57  // try to extract run number
58  try
59  {
60  runnumber = boost::lexical_cast<int>((*(tokens.rbegin())));
61  }
62  catch (boost::bad_lexical_cast const&)
63  {
64  cout << "Cannot extract run number from filename "
65  << filename << endl;
66  cout << "Segment string after parsing: input string "
67  << *(tokens.rbegin())
68  << " is not valid run number" << endl;
69  cout << "filename " << filename << " not standard -runnumber-segment.ext"
70  << endl;
71  cout << "returning " << runnumber << " as run number" << endl;
72  }
73  return make_pair(runnumber, segment);
74 }