Skip to content
This repository was archived by the owner on May 6, 2024. It is now read-only.

Commit 1ec4833

Browse files
EinarElentomeichlersmith
authored andcommitted
Use range-based loops
1 parent b3d0e00 commit 1ec4833

2 files changed

Lines changed: 91 additions & 110 deletions

File tree

include/SimCore/Geo/AuxInfoReader.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,28 +63,30 @@ class AuxInfoReader {
6363
* @param name The name of the magnetic field.
6464
* @param auxInfoList The aux info defining the magnetic field.
6565
*/
66-
void createMagneticField(G4String name, const G4GDMLAuxListType *auxInfoList);
66+
void createMagneticField(const G4String &name,
67+
const G4GDMLAuxListType *auxInfoList);
6768

6869
/**
6970
* Create a detector region from GDML data.
7071
* @param name The name of the detector region.
7172
* @param auxInfoList The aux info defining the detector region.
7273
*/
73-
void createRegion(G4String name, const G4GDMLAuxListType *auxInfoList);
74+
void createRegion(const G4String &name, const G4GDMLAuxListType *auxInfoList);
7475

7576
/**
7677
* Create visualization attributes from GDML data.
7778
* @param name The name of the visualization attributes.
7879
* @param auxInfoList The aux info defining the visualization attributes.
7980
*/
80-
void createVisAttributes(G4String name, const G4GDMLAuxListType *auxInfoList);
81+
void createVisAttributes(const G4String &name,
82+
const G4GDMLAuxListType *auxInfoList);
8183

8284
/**
8385
* Create the detector header from the global auxinfo.
8486
* @param detectorVersion The aux value with the detector version.
8587
* @param auxInfoList The aux info with the detector header information.
8688
*/
87-
void createDetectorHeader(G4String detectorVersion,
89+
void createDetectorHeader(const G4String &detectorVersion,
8890
const G4GDMLAuxListType *auxInfoList);
8991

9092
private:

src/SimCore/Geo/AuxInfoReader.cxx

Lines changed: 85 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,23 @@ AuxInfoReader::~AuxInfoReader() {
3636

3737
void AuxInfoReader::readGlobalAuxInfo() {
3838
const G4GDMLAuxListType* auxInfoList = parser_->GetAuxList();
39-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
40-
auxInfoList->begin();
41-
iaux != auxInfoList->end(); iaux++) {
42-
G4String auxType = iaux->type;
43-
G4String auxVal = iaux->value;
44-
G4String auxUnit = iaux->unit;
39+
for (const auto& auxInfo : *auxInfoList) {
40+
G4String auxType = auxInfo.type;
41+
G4String auxVal = auxInfo.value;
4542

4643
if (auxType == "SensDet") {
4744
std::cerr
4845
<< "[ WARN ] : Not defining SensDet in GDML since v1.0 of SimCore. "
4946
"See https://github.com/LDMX-Software/SimCore/issues/39"
5047
<< std::endl;
5148
} else if (auxType == "MagneticField") {
52-
createMagneticField(auxVal, iaux->auxList);
49+
createMagneticField(auxVal, auxInfo.auxList);
5350
} else if (auxType == "Region") {
54-
createRegion(auxVal, iaux->auxList);
51+
createRegion(auxVal, auxInfo.auxList);
5552
} else if (auxType == "VisAttributes") {
56-
createVisAttributes(auxVal, iaux->auxList);
53+
createVisAttributes(auxVal, auxInfo.auxList);
5754
} else if (auxType == "DetectorVersion") {
58-
createDetectorHeader(auxVal, iaux->auxList);
55+
createDetectorHeader(auxVal, auxInfo.auxList);
5956
}
6057
}
6158
return;
@@ -65,74 +62,69 @@ void AuxInfoReader::assignAuxInfoToVolumes() {
6562
const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
6663
std::vector<G4LogicalVolume*>::const_iterator lvciter;
6764
for (lvciter = lvs->begin(); lvciter != lvs->end(); lvciter++) {
68-
G4GDMLAuxListType auxInfo =
65+
G4GDMLAuxListType auxInfoList =
6966
parser_->GetVolumeAuxiliaryInformation(*lvciter);
70-
if (auxInfo.size() > 0) {
71-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
72-
auxInfo.begin();
73-
iaux != auxInfo.end(); iaux++) {
74-
G4String auxType = iaux->type;
75-
G4String auxVal = iaux->value;
76-
G4String auxUnit = iaux->unit;
77-
78-
G4LogicalVolume* lv = (*lvciter);
79-
80-
if (auxType == "MagneticField") {
81-
G4String magFieldName = auxVal;
82-
G4MagneticField* magField =
83-
MagneticFieldStore::getInstance()->getMagneticField(magFieldName);
84-
if (magField != nullptr) {
85-
G4FieldManager* mgr = new G4FieldManager(magField);
86-
lv->SetFieldManager(mgr, true /* FIXME: hard-coded to force field manager to daughters */);
87-
// G4cout << "Assigned magnetic field " << magFieldName << " to
88-
// volume " << lv->GetName() << G4endl;
89-
} else {
90-
EXCEPTION_RAISE(
91-
"MissingInfo",
92-
"Unknown MagneticField ref in volume's auxiliary info: " +
93-
std::string(magFieldName.data()));
94-
}
95-
} else if (auxType == "Region") {
96-
G4String regionName = auxVal;
97-
G4Region* region =
98-
G4RegionStore::GetInstance()->GetRegion(regionName);
99-
if (region != nullptr) {
100-
region->AddRootLogicalVolume(lv);
101-
// G4cout << "Added volume " << lv->GetName() << " to region " <<
102-
// regionName << G4endl;
103-
} else {
104-
EXCEPTION_RAISE("MissingInfo", "Reference region '" +
105-
std::string(regionName.data()) +
106-
"' was not found!");
107-
}
108-
} else if (auxType == "VisAttributes") {
109-
G4String visName = auxVal;
110-
G4VisAttributes* visAttributes =
111-
VisAttributesStore::getInstance()->getVisAttributes(visName);
112-
if (visAttributes != nullptr) {
113-
lv->SetVisAttributes(visAttributes);
114-
// G4cout << "Assigned VisAttributes " << visName << " to volume "
115-
// << lv->GetName() << G4endl;
116-
} else {
117-
EXCEPTION_RAISE("MissingInfo", "Referenced VisAttributes '" +
118-
std::string(visName.data()) +
119-
"' was not found!");
120-
}
67+
68+
for (const auto& auxInfo : auxInfoList) {
69+
G4String auxType = auxInfo.type;
70+
G4String auxVal = auxInfo.value;
71+
72+
G4LogicalVolume* lv = (*lvciter);
73+
74+
if (auxType == "MagneticField") {
75+
const G4String& magFieldName = auxVal;
76+
G4MagneticField* magField =
77+
MagneticFieldStore::getInstance()->getMagneticField(magFieldName);
78+
if (magField != nullptr) {
79+
auto mgr = new G4FieldManager(magField);
80+
lv->SetFieldManager(
81+
mgr,
82+
true /* FIXME: hard-coded to force field manager to daughters */);
83+
// G4cout << "Assigned magnetic field " << magFieldName << " to
84+
// volume " << lv->GetName() << G4endl;
85+
} else {
86+
EXCEPTION_RAISE(
87+
"MissingInfo",
88+
"Unknown MagneticField ref in volume's auxiliary info: " +
89+
std::string(magFieldName.data()));
90+
}
91+
} else if (auxType == "Region") {
92+
const G4String& regionName = auxVal;
93+
G4Region* region = G4RegionStore::GetInstance()->GetRegion(regionName);
94+
if (region != nullptr) {
95+
region->AddRootLogicalVolume(lv);
96+
// G4cout << "Added volume " << lv->GetName() << " to region " <<
97+
// regionName << G4endl;
98+
} else {
99+
EXCEPTION_RAISE("MissingInfo", "Reference region '" +
100+
std::string(regionName.data()) +
101+
"' was not found!");
102+
}
103+
} else if (auxType == "VisAttributes") {
104+
const G4String& visName = auxVal;
105+
G4VisAttributes* visAttributes =
106+
VisAttributesStore::getInstance()->getVisAttributes(visName);
107+
if (visAttributes != nullptr) {
108+
lv->SetVisAttributes(visAttributes);
109+
// G4cout << "Assigned VisAttributes " << visName << " to volume "
110+
// << lv->GetName() << G4endl;
111+
} else {
112+
EXCEPTION_RAISE("MissingInfo", "Referenced VisAttributes '" +
113+
std::string(visName.data()) +
114+
"' was not found!");
121115
}
122116
}
123117
}
124118
}
125119
}
126120

127-
void AuxInfoReader::createMagneticField(G4String magFieldName,
121+
void AuxInfoReader::createMagneticField(const G4String& magFieldName,
128122
const G4GDMLAuxListType* auxInfoList) {
129123
// Find type of the mag field.
130124
G4String magFieldType("");
131-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
132-
auxInfoList->begin();
133-
iaux != auxInfoList->end(); iaux++) {
134-
G4String auxType = iaux->type;
135-
G4String auxVal = iaux->value;
125+
for (const auto& auxInfo : *auxInfoList) {
126+
G4String auxType = auxInfo.type;
127+
G4String auxVal = auxInfo.value;
136128

137129
if (auxType == "MagneticFieldType") {
138130
magFieldType = auxVal;
@@ -151,12 +143,10 @@ void AuxInfoReader::createMagneticField(G4String magFieldName,
151143
if (magFieldType == "G4UniformMagField") {
152144
double bx, by, bz;
153145
bx = by = bz = 0.;
154-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
155-
auxInfoList->begin();
156-
iaux != auxInfoList->end(); iaux++) {
157-
G4String auxType = iaux->type;
158-
G4String auxVal = iaux->value;
159-
G4String auxUnit = iaux->unit;
146+
for (const auto& auxInfo : *auxInfoList) {
147+
G4String auxType = auxInfo.type;
148+
G4String auxVal = auxInfo.value;
149+
G4String auxUnit = auxInfo.unit;
160150

161151
G4String expr = auxVal + "*" + auxUnit;
162152
if (auxType == "bx") {
@@ -176,14 +166,14 @@ void AuxInfoReader::createMagneticField(G4String magFieldName,
176166
// Create a global 3D field map by reading from a data file.
177167
} else if (magFieldType == "MagneticFieldMap3D") {
178168
string fileName;
179-
double offsetX, offsetY, offsetZ;
169+
double offsetX{};
170+
double offsetY{};
171+
double offsetZ{};
180172

181-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
182-
auxInfoList->begin();
183-
iaux != auxInfoList->end(); iaux++) {
184-
G4String auxType = iaux->type;
185-
G4String auxVal = iaux->value;
186-
G4String auxUnit = iaux->unit;
173+
for (const auto& auxInfo : *auxInfoList) {
174+
G4String auxType = auxInfo.type;
175+
G4String auxVal = auxInfo.value;
176+
G4String auxUnit = auxInfo.unit;
187177

188178
G4String expr = auxVal + "*" + auxUnit;
189179

@@ -225,15 +215,12 @@ void AuxInfoReader::createMagneticField(G4String magFieldName,
225215
MagneticFieldStore::getInstance()->addMagneticField(magFieldName, magField);
226216
}
227217

228-
void AuxInfoReader::createRegion(G4String name,
218+
void AuxInfoReader::createRegion(const G4String& name,
229219
const G4GDMLAuxListType* auxInfoList) {
230220
bool storeTrajectories = true;
231-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
232-
auxInfoList->begin();
233-
iaux != auxInfoList->end(); iaux++) {
234-
G4String auxType = iaux->type;
235-
G4String auxVal = iaux->value;
236-
G4String auxUnit = iaux->unit;
221+
for (const auto& auxInfo : *auxInfoList) {
222+
G4String auxType = auxInfo.type;
223+
G4String auxVal = auxInfo.value;
237224

238225
if (auxType == "StoreTrajectories") {
239226
if (auxVal == "false") {
@@ -243,7 +230,6 @@ void AuxInfoReader::createRegion(G4String name,
243230
}
244231
}
245232
}
246-
247233
G4VUserRegionInformation* regionInfo =
248234
new UserRegionInformation(storeTrajectories);
249235
// This looks like a memory leak, but isn't. I (Einar) have checked. Geant4
@@ -259,22 +245,19 @@ void AuxInfoReader::createRegion(G4String name,
259245
}
260246
// NOLINTEND
261247

262-
void AuxInfoReader::createVisAttributes(G4String name,
248+
void AuxInfoReader::createVisAttributes(const G4String& name,
263249
const G4GDMLAuxListType* auxInfoList) {
264-
G4double rgba[4] = {1., 1., 1., 1.};
250+
std::array<G4double, 4> rgba = {1., 1., 1., 1.};
265251
G4bool visible = true;
266252
G4bool dauInvisible = false;
267253
G4bool forceWireframe = false;
268254
G4bool forceSolid = false;
269255
G4double lineWidth = 1.0;
270256
G4VisAttributes::LineStyle lineStyle = G4VisAttributes::unbroken;
271257

272-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
273-
auxInfoList->begin();
274-
iaux != auxInfoList->end(); iaux++) {
275-
G4String auxType = iaux->type;
276-
G4String auxVal = iaux->value;
277-
G4String auxUnit = iaux->unit;
258+
for (const auto& auxInfo : *auxInfoList) {
259+
G4String auxType = auxInfo.type;
260+
G4String auxVal = auxInfo.value;
278261

279262
if (auxType == "R") {
280263
rgba[0] = atof(auxVal.c_str());
@@ -315,7 +298,7 @@ void AuxInfoReader::createVisAttributes(G4String name,
315298
}
316299
}
317300

318-
G4VisAttributes* visAttributes = new G4VisAttributes();
301+
auto visAttributes = new G4VisAttributes();
319302
visAttributes->SetColor(rgba[0], rgba[1], rgba[2], rgba[3]);
320303
visAttributes->SetVisibility(visible);
321304
visAttributes->SetDaughtersInvisible(dauInvisible);
@@ -329,20 +312,17 @@ void AuxInfoReader::createVisAttributes(G4String name,
329312
// G4endl << G4endl;
330313
}
331314

332-
void AuxInfoReader::createDetectorHeader(G4String auxValue,
315+
void AuxInfoReader::createDetectorHeader(const G4String& auxValue,
333316
const G4GDMLAuxListType* auxInfoList) {
334317
int detectorVersion = atoi(auxValue.c_str());
335318

336319
std::string detectorName("");
337320
std::string author("");
338321
std::string description("");
339322

340-
for (std::vector<G4GDMLAuxStructType>::const_iterator iaux =
341-
auxInfoList->begin();
342-
iaux != auxInfoList->end(); iaux++) {
343-
G4String auxType = iaux->type;
344-
G4String auxVal = iaux->value;
345-
G4String auxUnit = iaux->unit;
323+
for (const auto& auxInfo : *auxInfoList) {
324+
G4String auxType = auxInfo.type;
325+
G4String auxVal = auxInfo.value;
346326

347327
if (auxType == "DetectorName") {
348328
detectorName = auxVal;
@@ -364,5 +344,4 @@ void AuxInfoReader::createDetectorHeader(G4String auxValue,
364344
G4cout << " Description: " << detectorHeader_->getDescription() << G4endl;
365345
G4cout << G4endl;*/
366346
}
367-
368347
} // namespace simcore::geo

0 commit comments

Comments
 (0)