File: ReflFactory.cpp

package info (click to toggle)
vecgeom 1.2.8%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 24,016 kB
  • sloc: cpp: 88,803; ansic: 6,888; python: 1,035; sh: 582; sql: 538; makefile: 23
file content (219 lines) | stat: -rw-r--r-- 6,957 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//!    \file ReflFactory.cpp
//!    \brief Reflection factory for placed volumes
//!
//!    \authors Author:  Andrei Gheata <andrei.gheata@cern.ch>
//!

#include "VecGeom/management/ReflFactory.h"

#include "VecGeom/volumes/LogicalVolume.h"
#include "VecGeom/volumes/PlacedVolume.h"
#include "VecGeom/volumes/UnplacedScaledShape.h"

namespace vecgeom {
inline namespace cxx {

ReflFactory::ReflFactory()
{
  fNameExtension = "_refl";
}

bool ReflFactory::Place(Transformation3D const &pureTransform3D, Vector3 const &scale, std::string const &name,
                        vecgeom::LogicalVolume *LV, vecgeom::LogicalVolume *motherLV, int copyNo)
{
  std::string name_refl = name + "_refl";
  if (fVerboseLevel > 0) {
    std::cout << "Place " << name << " lv " << LV << " " << LV->GetName() << " inside " << motherLV->GetName()
              << std::endl;
  }

  //
  // reflection IS NOT present in transform3D
  //

  if (!IsReflection(scale)) {
    // Place with pureTransform3D in the mother
    auto pv1 = LV->Place(name.c_str(), &pureTransform3D);
    pv1->SetCopyNo(copyNo);
    motherLV->PlaceDaughter(pv1);

    if (LogicalVolume *reflMotherLV = GetReflectedLV(motherLV)) {
      // if mother was reflected
      // reflect this LV and place it in reflected mother
      auto newTransform3D = ConvertScaledToPureTransformation(pureTransform3D, scale);

      auto pv2 = ReflectLV(LV, scale)->Place(name_refl.c_str(), &newTransform3D);
      pv2->SetCopyNo(copyNo);
      reflMotherLV->PlaceDaughter(pv2);
    }

    return true;
  }

  //
  //  reflection IS present in transform3D. Only (1,1,-1) currently supported
  //
  // assert((scale - Vector3(1, 1, -1)).Mag() < 1.e-8);

  auto pv1 = ReflectLV(LV, scale)->Place(name_refl.c_str(), &pureTransform3D);
  pv1->SetCopyNo(copyNo);
  motherLV->PlaceDaughter(pv1);

  if (LogicalVolume *reflMotherLV = GetReflectedLV(motherLV)) {

    // if mother was reflected
    // place the refLV consituent in reflected mother
    auto newTransform3D = ConvertScaledToPureTransformation(pureTransform3D, scale);

    auto pv2 = LV->Place(name.c_str(), &newTransform3D);
    pv2->SetCopyNo(copyNo);
    reflMotherLV->PlaceDaughter(pv2);
  }
  return true;
}

vecgeom::LogicalVolume *ReflFactory::GetConstituentLV(vecgeom::LogicalVolume *reflLV) const
{
  // Returns the consituent volume of the given reflected volume,
  // nullptr if the given reflected volume was not found.

  LogicalVolumesMapIterator it = fReflectedLVMap.find(reflLV);
  if (it == fReflectedLVMap.end()) return nullptr;
  return (*it).second;
}

vecgeom::LogicalVolume *ReflFactory::GetReflectedLV(vecgeom::LogicalVolume *lv) const
{
  // Returns the reflected volume of the given consituent volume,
  // nullptr if the given volume was not reflected.

  LogicalVolumesMapIterator it = fConstituentLVMap.find(lv);
  if (it == fConstituentLVMap.end()) return nullptr;
  return (*it).second;
}

vecgeom::Transformation3D ReflFactory::ConvertScaledToPureTransformation(Transformation3D const &pureTransform3D,
                                                                         Vector3 const &scale)
{
  Transformation3D scale3D(0, 0, 0, 0, 0, 0, scale[0], scale[1], scale[2]);
  Transformation3D scale3Dinv;
  scale3D.Inverse(scale3Dinv);
  // long-cut for scale * pureTransfore * scale.Inverse()
  Transformation3D newTransform3D = scale3D;
  newTransform3D.MultiplyFromRight(pureTransform3D);
  newTransform3D.MultiplyFromRight(scale3Dinv);
  return newTransform3D;
}

bool ReflFactory::IsConstituent(vecgeom::LogicalVolume const *lv) const
{
  // Returns true if the given volume has been already reflected
  // (is in the map of constituent volumes).

  return (fConstituentLVMap.find((vecgeom::LogicalVolume *)(lv)) != fConstituentLVMap.end());
}

bool ReflFactory::IsReflected(vecgeom::LogicalVolume const *lv) const
{
  // Returns true if the given volume is a reflected volume
  // (is in the map reflected  volumes).

  return (fReflectedLVMap.find((vecgeom::LogicalVolume *)(lv)) != fReflectedLVMap.end());
}

vecgeom::LogicalVolume *ReflFactory::ReflectLV(vecgeom::LogicalVolume *LV, Vector3 const &scale)
{
  // Gets/creates the reflected solid and logical volume
  // and copies + transforms LV daughters.
  vecgeom::LogicalVolume *refLV = GetReflectedLV(LV);
  if (!refLV) {
    // create new (reflected) objects
    refLV = CreateReflectedLV(LV, scale);
    // process daughters
    ReflectDaughters(LV, refLV, scale);
  }
  return refLV;
}

vecgeom::LogicalVolume *ReflFactory::CreateReflectedLV(vecgeom::LogicalVolume *LV, Vector3 const &scale)
{
  // Creates the reflected solid and logical volume
  // and add the logical volumes pair in the maps.

  // consistency check
  //
  assert(fReflectedLVMap.find(LV) == fReflectedLVMap.end());
  vecgeom::UnplacedScaledShape *refSolid =
      new vecgeom::UnplacedScaledShape(LV->GetUnplacedVolume(), scale[0], scale[1], scale[2]);

  vecgeom::LogicalVolume *refLV = new LogicalVolume((std::string(LV->GetName()) + "_refl").c_str(), refSolid);
  fConstituentLVMap[LV]         = refLV;
  fReflectedLVMap[refLV]        = LV;

  return refLV;
}

void ReflFactory::ReflectDaughters(vecgeom::LogicalVolume *LV, vecgeom::LogicalVolume *refLV, Vector3 const &scale)
{
  // Reflects daughters recursively.
  for (auto *dPV : LV->GetDaughters()) {
    ReflectPlacedVolume(dPV, refLV, scale);
  }
}

void ReflFactory::ReflectPlacedVolume(vecgeom::VPlacedVolume const *dPV, vecgeom::LogicalVolume *refLV,
                                      Vector3 const &scale)
{
  // Copies and transforms daughter of PVPlacement type of
  // a constituent volume into a reflected volume.

  auto dLV = (vecgeom::LogicalVolume *)dPV->GetLogicalVolume();

  // update daughter transformation
  //
  auto dt = ConvertScaledToPureTransformation(*dPV->GetTransformation(), scale);

  vecgeom::LogicalVolume *refDLV;

  if (!IsReflected(dLV)) {

    // get reflected volume if already created, or create one
    refDLV = GetReflectedLV(dLV);

    if (refDLV == nullptr) {
      // create new daughter solid and logical volume
      //
      if (fVerboseLevel > 0) {
        std::cout << "Daughter: " << dPV << "  " << dLV->GetName() << "_" << dPV->GetCopyNo() << " will be reflected."
                  << std::endl;
      }
      refDLV = CreateReflectedLV(dLV, scale);

      // recursive call
      //
      ReflectDaughters(dLV, refDLV, scale);
    }

  } else {
    if (fVerboseLevel > 0) {
      std::cout << "Daughter: " << dPV << "  " << dLV->GetName() << "_" << dPV->GetCopyNo()
                << " will be reconstitued.\n";
    }

    refDLV = GetConstituentLV(dLV);
  }
  // create new daughter physical volume
  // with updated transformation
  auto pv = refDLV->Place(dPV->GetName(), &dt);
  pv->SetCopyNo(dPV->GetCopyNo());
  refLV->PlaceDaughter(pv);
}

void ReflFactory::Clean()
{
  fConstituentLVMap.clear();
  fReflectedLVMap.clear();
}

} // namespace cxx
} // namespace vecgeom