Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:19:06

0001 /*----------------------------------------------------------------------
0002 
0003 ProvenanceAdaptor.cc
0004 
0005 ----------------------------------------------------------------------*/
0006 //------------------------------------------------------------
0007 // Class ProvenanceAdaptor: adapts old provenance (fileFormatVersion_.value() < 11) to new provenance.
0008 
0009 #include "IOPool/Input/src/ProvenanceAdaptor.h"
0010 
0011 #include <cassert>
0012 #include <memory>
0013 
0014 #include <set>
0015 #include <utility>
0016 #include <string>
0017 #include <memory>
0018 #include "DataFormats/Provenance/interface/BranchID.h"
0019 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
0020 #include "DataFormats/Provenance/interface/ProcessHistory.h"
0021 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0022 #include "FWCore/Utilities/interface/Algorithms.h"
0023 namespace edm {
0024 
0025   void ProvenanceAdaptor::fixProcessHistory(ProcessHistoryMap& pHistMap, ProcessHistoryVector& pHistVector) {
0026     assert(pHistMap.empty() != pHistVector.empty());
0027     for (ProcessHistoryVector::const_iterator i = pHistVector.begin(), e = pHistVector.end(); i != e; ++i) {
0028       pHistMap.insert(std::make_pair(i->id(), *i));
0029     }
0030     pHistVector.clear();
0031     for (ProcessHistoryMap::const_iterator i = pHistMap.begin(), e = pHistMap.end(); i != e; ++i) {
0032       ProcessHistory newHist;
0033       ProcessHistoryID const& oldphID = i->first;
0034       for (ProcessHistory::const_iterator it = i->second.begin(), et = i->second.end(); it != et; ++it) {
0035         ParameterSetID const& newPsetID = convertID(it->parameterSetID());
0036         newHist.emplace_back(it->processName(), newPsetID, it->releaseVersion(), it->passID());
0037       }
0038       assert(newHist.size() == i->second.size());
0039       ProcessHistoryID newphID = newHist.id();
0040       pHistVector.push_back(std::move(newHist));
0041       if (newphID != oldphID) {
0042         processHistoryIdConverter_.insert(std::make_pair(oldphID, newphID));
0043       }
0044     }
0045     assert(pHistVector.size() == pHistMap.size());
0046   }
0047 
0048   namespace {
0049     typedef StringVector OneHistory;
0050     typedef std::set<OneHistory> Histories;
0051     typedef std::pair<std::string, BranchID> Product;
0052     typedef std::vector<Product> OrderedProducts;
0053     struct Sorter {
0054       explicit Sorter(Histories const& histories) : histories_(histories) {}
0055       bool operator()(Product const& a, Product const& b) const;
0056       Histories const histories_;
0057     };
0058     bool Sorter::operator()(Product const& a, Product const& b) const {
0059       assert(a != b);
0060       if (a.first == b.first)
0061         return false;
0062       bool mayBeTrue = false;
0063       for (Histories::const_iterator it = histories_.begin(), itEnd = histories_.end(); it != itEnd; ++it) {
0064         OneHistory::const_iterator itA = find_in_all(*it, a.first);
0065         if (itA == it->end())
0066           continue;
0067         OneHistory::const_iterator itB = find_in_all(*it, b.first);
0068         if (itB == it->end())
0069           continue;
0070         assert(itA != itB);
0071         if (itB < itA) {
0072           // process b precedes process a;
0073           return false;
0074         }
0075         // process a precedes process b;
0076         mayBeTrue = true;
0077       }
0078       return mayBeTrue;
0079     }
0080 
0081     void fillProcessConfiguration(ProcessHistoryVector const& pHistVec, ProcessConfigurationVector& procConfigVector) {
0082       procConfigVector.clear();
0083       std::set<ProcessConfiguration> pcset;
0084       for (auto const& history : pHistVec) {
0085         for (auto const& process : history) {
0086           if (pcset.insert(process).second) {
0087             procConfigVector.push_back(process);
0088           }
0089         }
0090       }
0091     }
0092 
0093     void fillListsAndIndexes(ProductRegistry& productRegistry,
0094                              ProcessHistoryMap const& pHistMap,
0095                              std::shared_ptr<BranchIDLists const>& branchIDLists,
0096                              std::vector<BranchListIndex>& branchListIndexes) {
0097       OrderedProducts orderedProducts;
0098       std::set<std::string> processNamesThatProduced;
0099       ProductRegistry::ProductList& prodList = productRegistry.productListUpdator();
0100       for (auto& item : prodList) {
0101         BranchDescription& prod = item.second;
0102         if (prod.branchType() == InEvent) {
0103           prod.init();
0104           processNamesThatProduced.insert(prod.processName());
0105           orderedProducts.emplace_back(prod.processName(), prod.branchID());
0106         }
0107       }
0108       assert(!orderedProducts.empty());
0109       Histories processHistories;
0110       size_t max = 0;
0111       for (ProcessHistoryMap::const_iterator it = pHistMap.begin(), itEnd = pHistMap.end(); it != itEnd; ++it) {
0112         ProcessHistory const& pHist = it->second;
0113         OneHistory processHistory;
0114         for (ProcessHistory::const_iterator i = pHist.begin(), iEnd = pHist.end(); i != iEnd; ++i) {
0115           if (processNamesThatProduced.find(i->processName()) != processNamesThatProduced.end()) {
0116             processHistory.push_back(i->processName());
0117           }
0118         }
0119         max = (processHistory.size() > max ? processHistory.size() : max);
0120         assert(max <= processNamesThatProduced.size());
0121         if (processHistory.size() > 1) {
0122           processHistories.insert(processHistory);
0123         }
0124       }
0125       stable_sort_all(orderedProducts, Sorter(processHistories));
0126 
0127       auto pv = std::make_unique<BranchIDLists>();
0128       auto p = std::make_unique<BranchIDList>();
0129       std::string processName;
0130       BranchListIndex blix = 0;
0131       for (OrderedProducts::const_iterator it = orderedProducts.begin(), itEnd = orderedProducts.end(); it != itEnd;
0132            ++it) {
0133         if (it->first != processName) {
0134           if (!processName.empty()) {
0135             branchListIndexes.push_back(blix);
0136             ++blix;
0137             pv->push_back(std::move(*p));
0138             p = std::make_unique<BranchIDList>();
0139           }
0140           processName = it->first;
0141         }
0142         p->push_back(it->second.id());
0143       }
0144       branchListIndexes.push_back(blix);
0145       pv->push_back(std::move(*p));
0146       branchIDLists.reset(pv.release());
0147     }
0148   }  // namespace
0149 
0150   ProvenanceAdaptor::ProvenanceAdaptor(ProductRegistry& productRegistry,
0151                                        ProcessHistoryMap& pHistMap,
0152                                        ProcessHistoryVector& pHistVector,
0153                                        ProcessConfigurationVector& procConfigVector,
0154                                        ParameterSetIdConverter const& parameterSetIdConverter,
0155                                        bool fullConversion)
0156       : parameterSetIdConverter_(parameterSetIdConverter),
0157         processHistoryIdConverter_(),
0158         branchIDLists_(),
0159         branchListIndexes_() {
0160     fixProcessHistory(pHistMap, pHistVector);
0161     fillProcessConfiguration(pHistVector, procConfigVector);
0162     if (fullConversion) {
0163       fillListsAndIndexes(productRegistry, pHistMap, branchIDLists_, branchListIndexes_);
0164     }
0165   }
0166 
0167   ProvenanceAdaptor::~ProvenanceAdaptor() {}
0168 
0169   ParameterSetID const& ProvenanceAdaptor::convertID(ParameterSetID const& oldID) const {
0170     ParameterSetIdConverter::const_iterator it = parameterSetIdConverter_.find(oldID);
0171     if (it == parameterSetIdConverter_.end()) {
0172       return oldID;
0173     }
0174     return it->second;
0175   }
0176 
0177   ProcessHistoryID const& ProvenanceAdaptor::convertID(ProcessHistoryID const& oldID) const {
0178     ProcessHistoryIdConverter::const_iterator it = processHistoryIdConverter_.find(oldID);
0179     if (it == processHistoryIdConverter_.end()) {
0180       return oldID;
0181     }
0182     return it->second;
0183   }
0184 
0185   std::shared_ptr<BranchIDLists const> ProvenanceAdaptor::branchIDLists() const { return branchIDLists_; }
0186 
0187   void ProvenanceAdaptor::branchListIndexes(BranchListIndexes& indexes) const { indexes = branchListIndexes_; }
0188 }  // namespace edm