GeoModel - GeoModelCore
GeometryMap.cxx
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include "GeoModelKernel/GeometryMap.h"
6 #include "GeoModelKernel/GeoVolumeCursor.h"
7 #include <map>
8 #include <sys/types.h>
9 #include <regex.h>
10 // '^to\w*re$'
12 
13 public:
14 
15  Clockwork():lV(0){}
16 
17  std::vector<std::string> geometryRegex;
18  std::vector<regex_t *> geometryPreg;
19  std::vector<std::vector<std::string> > pathList;
20  std::vector<PVConstLink> physicalVolumes;
21  unsigned int lV;
22 
23  void finalize(unsigned int i, PVConstLink v);
24 };
25 
26 
27 GeometryMap::GeometryMap():
28  m_c(new Clockwork()) {
29 }
30 
31 
32 GeometryMap::~GeometryMap() {
33  delete m_c;
34 }
35 
36 void GeometryMap::add(const std::string & Path) {
37  std::vector <std::string> pathList;
38 
39  std::string path=Path;
40  // First look for the character "/":
41  size_t pos = 0;
42  while (1) {
43  size_t end = path.find('/',pos);
44  if (end==path.npos) break;
45 
46  std::string sub0(path,pos,end-pos);
47  pathList.push_back(sub0);
48  std::string sub1(path,end+1,path.npos-end-1);
49  path=sub1;
50  pos=0;
51  }
52  // Then look for wild cards:
53  m_c->pathList.push_back(pathList);
54 
55  std::string regex="^";
56  //size_t pos=0;
57  while (1) {
58  size_t end = path.find('*',pos);
59  std::string sub(path,pos,end-pos);
60  regex+=sub;
61  if (end==path.npos) break;
62  regex+=".*";
63  pos=end+1;
64  if (pos==path.npos) break;
65  }
66  regex+="$";
67  m_c->geometryRegex.push_back(regex);
68  regex_t *preg = new regex_t;
69  regcomp(preg,regex.c_str(),REG_EXTENDED);
70  m_c->geometryPreg.push_back(preg);
71 
72 }
73 
74 void GeometryMap::finalize( PVConstLink v) {
75 
76  for (size_t i=0;i<m_c->pathList.size();i++) {
77  m_c->lV=0;
78  m_c->finalize(i,v);
79  }
80 }
81 
82 
83 void GeometryMap::Clockwork::finalize(unsigned int i, PVConstLink v) {
84 
85  static const size_t NMATCH=11;
86  regmatch_t pmatch[NMATCH];
87 
88  // Something has gone wrong...
89  if (lV>pathList[i].size()) {
90  return;
91  }
92  if (lV==pathList[i].size()) {
93  std::string name = v->getLogVol()->getName();
94  if (regexec((geometryPreg[i]),name.c_str(), NMATCH, pmatch, 0)==0) {
95  physicalVolumes.push_back(v);
96  }
97  }
98  else {
99  GeoVolumeCursor x(v,nullptr);
100  while (!x.atEnd()) {
101  lV++;
102  finalize(i, x.getVolume());
103  lV--;
104  x.next();
105  }
106  }
107 }
108 
109 
110 
111 
112 
113 
114 GeometryMap::Iterator GeometryMap::begin() const {
115  return m_c->physicalVolumes.begin();
116 }
117 
118 GeometryMap::Iterator GeometryMap::end() const {
119  return m_c->physicalVolumes.end();
120 }
void next()
Advance.
PVConstLink getVolume() const
Returns the selected physical volume or nullptr if not found.
Represents a path from one point in the general tree, to the present point.
Definition: Path.h:18
bool atEnd() const
Check if we are at the last volume.
const GeoLogVol * getLogVol() const
Returns the logical volume.
Definition: GeoVPhysVol.cxx:39