LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
util.cc
Go to the documentation of this file.
1 #include "artg4tk/util/util.hh"
2 
3 #include "Geant4/G4LogicalVolume.hh"
4 #include "Geant4/G4VisAttributes.hh"
5 
6 #include <iomanip>
7 #include <sstream>
8 
9 #include "cetlib_except/exception.h"
10 #include <cstdlib>
11 
12 // Set visual attributes
13 void
14 artg4tk::setVisAtts(G4LogicalVolume* lv,
15  bool display,
16  const std::vector<double>& rgba,
17  std::function<void(G4VisAttributes*)> func)
18 {
19 
20  G4VisAttributes* att;
21 
22  // Do we want to display?
23  if (display) {
24 
25  // We do! Set the color
26  att = new G4VisAttributes(G4Colour(rgba[0], rgba[1], rgba[2], rgba[3]));
27 
28  // Run any function that was passed in
29  func(att);
30 
31  }
32 
33  else {
34  // We do not want to display. Too bad.
35  att = new G4VisAttributes(0);
36  }
37 
38  // Set the attributes in the logical volume
39  lv->SetVisAttributes(att);
40 }
41 
42 // Set visual attributes without a helper function
43 void
44 artg4tk::setVisAtts(G4LogicalVolume* lv, bool display, const std::vector<double>& rgba)
45 {
46  setVisAtts(lv, display, rgba, [](G4VisAttributes*) {});
47 }
48 
49 // Add number to name
50 std::string
51 artg4tk::addNumberToName(const std::string& name, int number)
52 {
53 
54  std::ostringstream newName;
55  newName << name << '[' << std::setfill('0') << std::setw(2) << number << ']';
56 
57  return newName.str();
58 }
59 
60 // find base path
61 std::string
62 artg4tk::basePath(const std::string& envVar, const std::string& pkgName)
63 {
64  std::string path;
65  char* pathC = std::getenv(envVar.c_str());
66  if (pathC) {
67  path = std::string(pathC);
68  } else {
69  pathC = std::getenv("MRB_BUILDDIR");
70  if (pathC) {
71  path = std::string(pathC) + "/" + pkgName;
72  } else {
73  throw cet::exception("NOBASEPATH")
74  << "Need enviornment variable " << envVar << " or $MRB_BUILDDIR set to find path\n";
75  }
76  }
77 
78  return path;
79 }
std::string addNumberToName(const std::string &name, int number)
Definition: util.cc:51
std::string basePath(const std::string &envVar, const std::string &pkgName)
Definition: util.cc:62
void setVisAtts(G4LogicalVolume *lv, bool display, const std::vector< double > &rgba)
Definition: util.cc:44
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33