Skip to content

Commit 515d2ae

Browse files
committed
Better initialization of sensitive volumes
Avoiding double loop finding; Expects external file of format id1:name id2:name listing sensitive volumes.
1 parent 31b85f4 commit 515d2ae

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

src/MCStepLoggerImpl.cxx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,17 +224,14 @@ class StepLogger
224224
if (std::getenv("MCSTEPLOG_TTREE")) {
225225
mTTreeIO = true;
226226
}
227-
228-
229-
// init the sensitive volume stuff
230-
StepInfo::lookupstructures.initSensitiveVolLookup(getSensitiveVolFile());
231227
}
232228

233229
void addStep(TVirtualMC* mc)
234230
{
235231
if (!mVolMapInitialized) {
236232
// try to load the volumename -> modulename mapping
237233
initVolumeMap();
234+
StepInfo::lookupstructures.initSensitiveVolLookup(getSensitiveVolFile());
238235
mVolMapInitialized = true;
239236
}
240237

src/StepInfo.cxx

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <cassert>
2828
#include <iostream>
2929
#include <fstream>
30+
#include <sstream>
31+
#include <cstdlib>
3032

3133
ClassImp(o2::StepInfo);
3234
ClassImp(o2::MagCallInfo);
@@ -163,38 +165,29 @@ bool StepLookups::initSensitiveVolLookup(const std::string& filename)
163165
auto vlist = gGeoManager->GetListOfVolumes();
164166
volidtoissensitive.resize(vlist->GetEntries(), false);
165167

166-
auto setSensitive = [this](int volID, bool sensitive) {
167-
if (volID >= volidtoissensitive.size()) {
168-
// should not happen
169-
assert(false);
170-
}
171-
volidtoissensitive[volID] = sensitive;
172-
};
173-
174-
// lambda returning all ids with that name
175-
// assume the name to be unique
176-
// unfortunately this is very slow: we should think about a more clever way
177-
auto findSensVolumeAndRegister = [&vlist, setSensitive](const std::string& name) {
178-
for (int i = 0; i < vlist->GetEntries(); ++i) {
179-
auto v = static_cast<TGeoVolume*>(vlist->At(i));
180-
if (strlen(v->GetName()) == strlen(name.c_str())) {
181-
if (strcmp(v->GetName(), name.c_str()) == 0) {
182-
setSensitive(v->GetNumber(), true);
183-
std::cout << "Registering " << v->GetNumber() << " as id for sensitive volume " << name << "\n";
184-
}
185-
}
186-
}
187-
};
188-
189168
// open for reading or fail
190169
std::ifstream ifs;
191170
ifs.open(filename);
192171
if (ifs.is_open()) {
193172
std::string line;
194173
std::vector<int> ids;
195174
while (std::getline(ifs, line)) {
196-
// a line is supposed to be a volume name
197-
findSensVolumeAndRegister(line);
175+
std::istringstream ss(line);
176+
std::string token;
177+
// split the line into key + value
178+
int counter = 0;
179+
std::string keyvalue[2] = { "NULL", "NULL" };
180+
while (counter < 2 && std::getline(ss, token, ':')) {
181+
if (!token.empty()) {
182+
keyvalue[counter] = token;
183+
counter++;
184+
}
185+
}
186+
// mark this as sensitive
187+
int index = atoi(keyvalue[0].c_str());
188+
assert(index >= 0);
189+
assert(index < volidtoissensitive.size());
190+
volidtoissensitive[index] = true;
198191
}
199192
return true;
200193
}

0 commit comments

Comments
 (0)