Skip to content

Commit 5a1c039

Browse files
committed
Remove const_cast from DaqProvenanceHelper
A simple refactoring of the code avoids the need for the cast.
1 parent 9fcde00 commit 5a1c039

File tree

6 files changed

+19
-14
lines changed

6 files changed

+19
-14
lines changed

DataFormats/Provenance/interface/BranchChildren.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ namespace edm {
4343
// const accessor for the data
4444
map_t const& childLookup() const { return childLookup_; }
4545

46+
map_t& mutableChildLookup() { return childLookup_; }
47+
4648
private:
4749
map_t childLookup_;
4850

FWCore/Sources/interface/DaqProvenanceHelper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace edm {
4242
bool matchProcesses(ProcessConfiguration const& pc, ProcessHistory const& ph) const;
4343
void fixMetaData(ProcessConfigurationVector& pcv, std::vector<ProcessHistory>& phv);
4444
void fixMetaData(std::vector<BranchID>& branchIDs) const;
45-
void fixMetaData(BranchIDLists const&) const;
45+
void fixMetaData(BranchIDLists&) const;
4646
void fixMetaData(ProductDependencies& productDependencies) const;
4747
ProcessHistoryID const& mapProcessHistoryID(ProcessHistoryID const& phid);
4848
ParentageID const& mapParentageID(ParentageID const& phid) const;

FWCore/Sources/src/DaqProvenanceHelper.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,20 +161,17 @@ namespace edm {
161161
std::replace(branchID.begin(), branchID.end(), oldBranchID_, newBranchID_);
162162
}
163163

164-
void DaqProvenanceHelper::fixMetaData(BranchIDLists const& branchIDLists) const {
164+
void DaqProvenanceHelper::fixMetaData(BranchIDLists& branchIDLists) const {
165165
BranchID::value_type oldID = oldBranchID_.id();
166166
BranchID::value_type newID = newBranchID_.id();
167-
// The const_cast is ugly, but it beats the alternatives.
168-
BranchIDLists& lists = const_cast<BranchIDLists&>(branchIDLists);
169-
for (auto& list : lists) {
167+
for (auto& list : branchIDLists) {
170168
std::replace(list.begin(), list.end(), oldID, newID);
171169
}
172170
}
173171

174172
void DaqProvenanceHelper::fixMetaData(ProductDependencies& productDependencies) const {
175173
typedef std::map<BranchID, std::set<BranchID> > BCMap;
176-
// The const_cast is ugly, but it beats the alternatives.
177-
BCMap& childLookup = const_cast<BCMap&>(productDependencies.childLookup());
174+
BCMap& childLookup = productDependencies.mutableChildLookup();
178175
// First fix any old branchID's in the key.
179176
{
180177
BCMap::iterator i = childLookup.find(oldBranchID_);

IOPool/Input/src/ProvenanceAdaptor.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ namespace edm {
9292

9393
void fillListsAndIndexes(ProductRegistry& productRegistry,
9494
ProcessHistoryMap const& pHistMap,
95-
std::shared_ptr<BranchIDLists const>& branchIDLists,
95+
std::shared_ptr<BranchIDLists>& branchIDLists,
9696
std::vector<BranchListIndex>& branchListIndexes) {
9797
OrderedProducts orderedProducts;
9898
std::set<std::string> processNamesThatProduced;
@@ -182,7 +182,11 @@ namespace edm {
182182
return it->second;
183183
}
184184

185-
std::shared_ptr<BranchIDLists const> ProvenanceAdaptor::branchIDLists() const { return branchIDLists_; }
185+
std::shared_ptr<BranchIDLists> ProvenanceAdaptor::releaseBranchIDLists() {
186+
auto ptr = branchIDLists_;
187+
branchIDLists_.reset();
188+
return ptr;
189+
}
186190

187191
void ProvenanceAdaptor::branchListIndexes(BranchListIndexes& indexes) const { indexes = branchListIndexes_; }
188192
} // namespace edm

IOPool/Input/src/ProvenanceAdaptor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ namespace edm {
3838
ProvenanceAdaptor(ProvenanceAdaptor const&) = delete; // Disallow copying and moving
3939
ProvenanceAdaptor& operator=(ProvenanceAdaptor const&) = delete; // Disallow copying and moving
4040

41-
std::shared_ptr<BranchIDLists const> branchIDLists() const;
41+
std::shared_ptr<BranchIDLists> releaseBranchIDLists();
4242

4343
void branchListIndexes(BranchListIndexes& indexes) const;
4444

@@ -51,7 +51,7 @@ namespace edm {
5151

5252
ParameterSetIdConverter parameterSetIdConverter_;
5353
ProcessHistoryIdConverter processHistoryIdConverter_;
54-
std::shared_ptr<BranchIDLists const> branchIDLists_;
54+
std::shared_ptr<BranchIDLists> branchIDLists_;
5555
std::vector<BranchListIndex> branchListIndexes_;
5656
}; // class ProvenanceAdaptor
5757
} // namespace edm

IOPool/Input/src/RootFile.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,14 @@ namespace edm {
400400
}
401401
}
402402
}
403+
std::shared_ptr<BranchIDLists> mutableBranchIDLists;
403404
if (!fileFormatVersion().splitProductIDs()) {
404405
// Old provenance format input file. Create a provenance adaptor.
405406
// propagate_const<T> has no reset() function
406407
provenanceAdaptor_ = std::make_unique<ProvenanceAdaptor>(
407408
inputProdDescReg, pHistMap, pHistVector, processConfigurations, psetIdConverter, true);
408409
// Fill in the branchIDLists branch from the provenance adaptor
409-
branchIDLists_ = provenanceAdaptor_->branchIDLists();
410+
mutableBranchIDLists = provenanceAdaptor_->releaseBranchIDLists();
410411
} else {
411412
if (!fileFormatVersion().triggerPathsTracked()) {
412413
// New provenance format, but change in ParameterSet Format. Create a provenance adaptor.
@@ -418,7 +419,7 @@ namespace edm {
418419
if (metaDataTree->FindBranch(poolNames::branchIDListBranchName().c_str()) == nullptr) {
419420
throw Exception(errors::EventCorruption) << "Failed to find branchIDLists branch in metaData tree.\n";
420421
}
421-
branchIDLists_.reset(branchIDListsAPtr.release());
422+
mutableBranchIDLists.reset(branchIDListsAPtr.release());
422423
}
423424

424425
if (fileFormatVersion().hasThinnedAssociations()) {
@@ -460,9 +461,10 @@ namespace edm {
460461
inputProdDescReg.copyProduct(newBD);
461462
// Fix up other per file metadata.
462463
daqProvenanceHelper_->fixMetaData(processConfigurations, pHistVector);
463-
daqProvenanceHelper_->fixMetaData(*branchIDLists_);
464+
daqProvenanceHelper_->fixMetaData(*mutableBranchIDLists);
464465
daqProvenanceHelper_->fixMetaData(*productDependencies_);
465466
}
467+
branchIDLists_ = std::move(mutableBranchIDLists);
466468
}
467469

468470
for (auto const& history : pHistVector) {

0 commit comments

Comments
 (0)