/home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/gen/include/RAT/PosGen_RegexFill.hh Source File

Ratpac-two: /home/docs/checkouts/readthedocs.org/user_builds/ratpac/checkouts/latest/src/gen/include/RAT/PosGen_RegexFill.hh Source File
Ratpac-two
PosGen_RegexFill.hh
1 // Position generator to fill all volumes with names that match a regular
2 // expression. Excludes daughters unless they also match. Positions uniformly
3 // distributed by volume among matched volumes.
4 
5 #ifndef __RAT_PosGen_RegexFill__
6 #define __RAT_PosGen_RegexFill__
7 
8 #include <G4AffineTransform.hh>
9 #include <G4LogicalVolume.hh>
10 #include <G4ThreeVector.hh>
11 #include <G4VPhysicalVolume.hh>
12 #include <G4VSolid.hh>
13 #include <RAT/GLG4PosGen.hh>
14 #include <map>
15 #include <utility>
16 #include <vector>
17 
18 // C POSIX Regex library, switch to <regex> in c++11
19 #include <regex.h>
20 
21 class G4VPhysicalVolume;
22 class G4VSolid;
23 
24 namespace RAT {
25 
26 typedef struct {
27  // Logical volume representing found volume
28  G4LogicalVolume *log;
29 
30  // Physical volume that was found
31  G4VPhysicalVolume *phys;
32 
33  // Transformation from volume coordinates to global coordinates
34  G4AffineTransform transform;
35 
36  // Volume of solid and bounding box
37  double solidVolume, boundVolume;
38 
39  // Bounding box in volume coodinates
40  double x0, x1, y0, y1, z0, z1;
41 
42  // Pairs of Solids (in daughter coords) and transformations from volume to
43  // daughter coords
44  std::vector<std::pair<G4VSolid *, G4AffineTransform>> daughters;
45 } FillVolume;
46 
47 class PosGen_RegexFill : public GLG4PosGen {
48  public:
49  // Position generator to be called `regexfill`
50  PosGen_RegexFill(const char *dbname = "regexfill") : GLG4PosGen(dbname) {}
51  virtual ~PosGen_RegexFill() {}
52 
53  // Generates a random position in a volume that matched a regex
54  // Positions are uniformly distributed (by volume) across all matches
55  // Positions exclude daughters of mathched volumes unless the daughter also
56  // matches
57  virtual void GeneratePosition(G4ThreeVector &pos);
58 
59  // Adds all volumes that match the regex to the fill list
60  virtual void SetState(G4String regex);
61 
62  // Returns a space separated list of regexes
63  virtual G4String GetState() const;
64 
65  protected:
66  // Recursively traverses daughters of mother to find any volume names
67  // mathching the regex Adds found volumes to the `found` parameter and
68  // calculates all necessary fields of FillVolume
69  static void FindVolumes(G4LogicalVolume *mother, regex_t *re, std::vector<FillVolume> &found);
70 
71  // Arbitrary volume calculations are terribly slow, so cache solids that
72  // have been calculated already. G4VSolid lifetime is the same as the
73  // simulation so presumably this is safe.
74  static double GetVolume(G4VSolid *solid);
75 
76  // Accumulates set regexes
77  std::string fState;
78 
79  // Volumes that matched the regexes
80  std::vector<FillVolume> fVolumes;
81 
82  // Stores the sum of the volume up to the requested index
83  std::vector<double> fVolumeCumu;
84 
85  // Calculated volume cache
86  static std::map<G4VSolid *, double> fSolidVolumes;
87 };
88 
89 } // namespace RAT
90 
91 #endif
Definition: GLG4PosGen.hh:18
Definition: PosGen_RegexFill.hh:47
Definition: CCCrossSecMessenger.hh:29
Definition: PosGen_RegexFill.hh:26