star-travex
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PrgOptionProcessor.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <algorithm>
4 #include <boost/regex.hpp>
5 
6 #include "TFile.h"
7 #include "TSystem.h"
8 
9 #include "travex/utils.h"
11 
12 
14 
15 
16 PrgOptionProcessor::PrgOptionProcessor(int argc, char **argv, const std::string& stiTreeName) :
17  tvx::ProgramOptions(argc, argv),
18  fGeomFilePath(),
19  fVolumeListFile(),
20  fVolumePattern(),
21  fVolumeList(),
22  fStiTChain(new TChain(stiTreeName.c_str(), "READ"))
23 {
24  // Declare supported options
25  fOptions.add_options()
26  ("geom-file", po::value<std::string>(&fGeomFilePath)->default_value("y2014a.root"), "Full path to a ROOT file with TGeo geometry")
27  ("volume-pattern,p", po::value<std::string>(&fVolumePattern)->implicit_value("process_all_volumes"),
28  "A regex pattern to match Sti/TGeo volume names. If specified without a value all volumes will be matched")
29  ("volume-pattern-flist,l", po::value<std::string>(&fVolumeListFile), "Full path to a text file with regex patterns to match Sti/TGeo volume names")
30  ;
31 
32  // Set default values for Sti volume name patterns. These are used if the user does not specify any
33  fVolumeList.insert("^.*IDSM_1/PXMO_1/PXLA_[\\d]+/LADR_\\d/PXSI_[\\d]+/PLAC_1.*$");
34  fVolumeList.insert("^.*IDSM_1/IBMO_1/IBAM_[\\d]+/IBLM_\\d/IBSS_1.*$");
35  fVolumeList.insert("^.*IDSM_1/SFMO_1/SFLM_[\\d]+/SFSW_[\\d]+/SFSL_1/SFSD_1.*$");
36 }
37 
38 
40 {
41  delete fStiTChain; fStiTChain = nullptr;
42 }
43 
44 
51 {
52  po::store(po::parse_command_line(fArgc, fArgv, fOptions), fOptionsValues);
53  po::notify(fOptionsValues);
54 
55  VerifyOptions();
56 
57  // Create chains based on the user input
59 }
60 
61 
63 {
64  TVX_INFO("User provided options:");
65 
66  tvx::ProgramOptions::VerifyOptions();
67 
68  if (fOptionsValues.count("geom-file"))
69  {
70  std::string pathToGeomFile = boost::any_cast<std::string>(fOptionsValues["geom-file"].value());
71  std::cout << "File with TGeo geometry: " << pathToGeomFile << std::endl;
72 
73  if ( !std::ifstream(pathToGeomFile.c_str()).good() )
74  TVX_FATAL("File \"%s\" does not exist", pathToGeomFile.c_str());
75  } else
76  TVX_FATAL("Geometry file not set");
77 
78 
79  if (fOptionsValues.count("volume-pattern-flist"))
80  {
81  std::cout << "fVolumeListFile: " << fVolumeListFile << std::endl;
82  std::ifstream volListFile(fVolumeListFile.c_str());
83 
84  if (!volListFile.good()) {
85  TVX_FATAL("File \"%s\" does not exist", fVolumeListFile.c_str());
86  }
87 
88  fVolumeList.clear();
89  std::string pattern;
90 
91  while ( volListFile.good() )
92  {
93  volListFile >> pattern;
94 
95  try {
96  boost::regex re(pattern);
97  }
98  catch (boost::regex_error& e) {
99  TVX_FATAL("Provided regex \"%s\" is not valid", pattern.c_str());
100  }
101 
102  if (volListFile.eof()) break;
103 
104  fVolumeList.insert(pattern);
105  }
106 
107  TVX_INFO("User patterns (fVolumeList) are:");
108  std::copy(fVolumeList.begin(), fVolumeList.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
109  } else {
110  TVX_INFO("Default patterns (fVolumeList) are:");
111  std::copy(fVolumeList.begin(), fVolumeList.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
112  }
113 
114 
115  if (fOptionsValues.count("volume-pattern"))
116  {
117  // Process implicit value first
118  if (fVolumePattern.compare("process_all_volumes") == 0) {
119  fVolumeList.clear();
120  }
121  else {
122 
123  try {
124  boost::regex re(fVolumePattern);
125  }
126  catch (boost::regex_error& e) {
127  TVX_FATAL("Provided regex \"%s\" is not valid", fVolumePattern.c_str());
128  }
129 
130  fVolumeList.clear();
131  fVolumeList.insert(fVolumePattern);
132  }
133 
134  TVX_INFO("User patterns (fVolumeList) are:");
135  std::copy(fVolumeList.begin(), fVolumeList.end(), std::ostream_iterator<std::string>(std::cout, "\n"));
136  }
137 }
138 
139 
144 bool PrgOptionProcessor::MatchedVolName(const std::string & volName) const
145 {
146  if (volName.empty() || fVolumeList.empty())
147  return true;
148 
149  std::set<std::string>::const_iterator iPattern = fVolumeList.begin();
150 
151  for( ; iPattern != fVolumeList.end(); ++iPattern )
152  {
153  boost::regex r(*iPattern);
154 
155  if ( boost::regex_match(volName, r) )
156  return true;
157  }
158 
159  return false;
160 }
161 
162 
164 {
165  TFile file( fInFilePath.c_str() );
166 
167  if ( file.IsZombie() )
168  {
169  TVX_WARNING("Input file is not a root file: %s\nWill treat it as a file list", fInFilePath.c_str());
170 
171  std::ifstream treeListFile(fInFilePath.c_str());
172 
173  while ( treeListFile.good() )
174  {
175  std::string treeFileName;
176  treeListFile >> treeFileName;
177  if (treeListFile.eof()) break;
178 
179  AddToInputChains(treeFileName);
180  }
181  } else
182  {
183  TVX_INFO("Found root file: %s", fInFilePath.c_str());
184  AddToInputChains(fInFilePath);
185  }
186 }
187 
188 
195 void PrgOptionProcessor::AddToInputChains(std::string stiTreeRootFileName)
196 {
197  TFile file( stiTreeRootFileName.c_str() );
198 
199  if ( file.IsZombie() )
200  TVX_FATAL("Input file is not a valid root file: %s", stiTreeRootFileName.c_str());
201 
202  fStiTChain->AddFile( stiTreeRootFileName.c_str() );
203  TVX_INFO("Found valid ROOT file with Sti tree: %s", stiTreeRootFileName.c_str());
204 }
Processes and controls user options provided in the command line.
TChain * fStiTChain
A (chained) TTree from the input file.
virtual void AddToInputChains(std::string stiTreeRootFileName)
This private method takes a path to a valid ROOT file as input.
std::string fGeomFilePath
Full path to a ROOT file with TGeo geometry.
std::set< std::string > fVolumeList
A list of volume names to consider.
virtual void VerifyOptions()
bool MatchedVolName(const std::string &volName) const
Note that the function returns true when the internal list of regex'es formed by the user specified o...
void ProcessOptions()
Takes the standard command line arguments and parses them with the boost program_options utility...
std::string fVolumeListFile
Full path to a text file with Sti/TGeo volume names.
std::string fVolumePattern
Regex pattern provided by the user in the command line.