Analysis Software
Documentation for sPHENIX simulation 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/lexical_cast.hpp>
4 #include <boost/tokenizer.hpp>
5 
6 #include <algorithm> // for max
7 #include <iostream>
8 #include <vector>
9 
10 // relying on our standard filenames ...-<runnumber>-<segment>.<ext>
11 // extract run number and segment number from filename
12 std::pair<int, int>
14 {
15  int runnumber = 0;
16  int segment = -9999;
17  boost::char_separator<char> sep("-.");
18  boost::tokenizer<boost::char_separator<char> > tok(filename, sep);
19  // tokenizer does not have reverse iterator, so fill it in vector
20  // and reverse iterate on vector
21  std::vector<std::string> tokens;
22  for (auto& t : tok)
23  {
24  tokens.push_back(t);
25  }
26  tokens.pop_back(); // remove the file extension
27  // try to extract segment number
28  try
29  {
30  segment = boost::lexical_cast<int>((*(tokens.rbegin())));
31  }
32  catch (boost::bad_lexical_cast const&)
33  {
34  std::cout << "Cannot extract segment number from filename "
35  << filename << std::endl;
36  std::cout << "Segment string after parsing: input string "
37  << *(tokens.rbegin())
38  << " is not valid segment number" << std::endl;
39  std::cout << "filename " << filename << " not standard -runnumber-segment.ext"
40  << std::endl;
41  std::cout << "using " << segment << " as segment number" << std::endl;
42  }
43  tokens.pop_back(); // remove the segment number
44  // try to extract run number
45  try
46  {
47  runnumber = boost::lexical_cast<int>((*(tokens.rbegin())));
48  }
49  catch (boost::bad_lexical_cast const&)
50  {
51  std::cout << "Cannot extract run number from filename "
52  << filename << std::endl;
53  std::cout << "Segment string after parsing: input string "
54  << *(tokens.rbegin())
55  << " is not valid run number" << std::endl;
56  std::cout << "filename " << filename << " not standard -runnumber-segment.ext"
57  << std::endl;
58  std::cout << "returning " << runnumber << " as run number" << std::endl;
59  }
60  return std::make_pair(runnumber, segment);
61 }