LazyROOTObjLoader.cxx
Go to the documentation of this file.
1 /*
2  * LazyROOTObjLoader.cxx
3  * Wrapper around ROOT object loading that only loads objects when needed.
4  *
5  * Created on: Apr 14, 2016
6  * Author: J. Wolcott <jwolcott@fnal.gov>
7  */
8 
9 #include <exception>
10 #include <memory>
11 #include <string>
12 #include <wordexp.h>
13 
14 #include "TFile.h"
15 
16 #ifdef USE_CETLIB
17 #include "cetlib/search_path.h"
18 #endif
19 
20 #include "NOvARwgt/util/LazyROOTObjLoader.h"
21 
22 namespace novarwgt
23 {
24 
25  std::unique_ptr<TFile> FindAndOpenFile(const std::string& filename)
26  {
27  std::string fn;
28 
29  bool badFilename = false;
30  std::unique_ptr<TFile> f;
31 
32  // expand any environment variables, etc.
33  wordexp_t p;
34  // the 0 is the set of flags passed. see: https://www.gnu.org/software/libc/manual/html_node/Calling-Wordexp.html
35  if ( wordexp( filename.c_str(), &p, 0 ) != 0 || p.we_wordc != 1)
36  throw std::runtime_error( Form("novarwgt::FindAndOpenFile(): bad filename '%s'", filename.c_str()) );
37  fn = *(p.we_wordv);
38  for (size_t i=0; i<p.we_wordc; i++)
39  wordfree(&p);
40 
41  // only check the search path if the path is not absolute
42  if (!fn.empty() && fn.compare(0, 1, "/")==0)
43  {
44  f = std::make_unique<TFile>(fn.c_str(), "read");
45  if (f->IsZombie())
46  badFilename = true;
47  }
48  else
49  {
50 #ifdef USE_CETLIB
51  cet::search_path sp("FW_SEARCH_PATH");
52  if( !sp.find_file(filename, fn) )
53  badFilename = true;
54 #endif
55  f = std::make_unique<TFile>(fn.c_str(), "read");
56  if (f->IsZombie())
57  badFilename = true;
58  }
59 
60  if (badFilename)
61  throw std::runtime_error( Form("novarwgt::LookupFile(): purported file '%s' can't be located", filename.c_str()) );
62 
63  return f;
64  } // FindAndOpenFile()
65 } // namespace novarwgt
const char * p
Definition: xmltok.h:285
string filename
Definition: shutoffs.py:106
std::string find_file(std::string const &filename) const
std::unique_ptr< TFile > FindAndOpenFile(const std::string &filename)
enum BeamMode string