CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/IOPool/Input/src/ProvenanceAdaptor.cc

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------
00002 
00003 ProvenanceAdaptor.cc
00004 
00005 ----------------------------------------------------------------------*/
00006   //------------------------------------------------------------
00007   // Class ProvenanceAdaptor: adapts old provenance (fileFormatVersion_.value() < 11) to new provenance.
00008 
00009 #include "IOPool/Input/src/ProvenanceAdaptor.h"
00010 
00011 #include <cassert>
00012 #include <set>
00013 #include <utility>
00014 #include <string>
00015 #include <memory>
00016 #include "DataFormats/Provenance/interface/BranchID.h"
00017 #include "DataFormats/Provenance/interface/ProcessConfiguration.h"
00018 #include "DataFormats/Provenance/interface/ProcessHistory.h"
00019 #include "DataFormats/Provenance/interface/ProductRegistry.h"
00020 #include "FWCore/Utilities/interface/Algorithms.h"
00021 namespace edm {
00022   
00023   void
00024   ProvenanceAdaptor::fixProcessHistory(ProcessHistoryMap& pHistMap,
00025                                        ProcessHistoryVector& pHistVector) {
00026     assert (pHistMap.empty() != pHistVector.empty());
00027     for (ProcessHistoryVector::const_iterator i = pHistVector.begin(), e = pHistVector.end(); i != e; ++i) {
00028       pHistMap.insert(std::make_pair(i->id(), *i));
00029     }
00030     pHistVector.clear();
00031     for (ProcessHistoryMap::const_iterator i = pHistMap.begin(), e = pHistMap.end(); i != e; ++i) {
00032       ProcessHistory newHist;
00033       ProcessHistoryID const& oldphID = i->first;
00034       for (ProcessHistory::const_iterator it = i->second.begin(), et = i->second.end(); it != et; ++it) {
00035         ParameterSetID const& newPsetID = convertID(it->parameterSetID());
00036         newHist.emplace_back(it->processName(), newPsetID, it->releaseVersion(), it->passID());
00037       }
00038       assert(newHist.size() == i->second.size());
00039       ProcessHistoryID newphID = newHist.id();
00040       pHistVector.push_back(std::move(newHist));
00041       if (newphID != oldphID) {
00042         processHistoryIdConverter_.insert(std::make_pair(oldphID, newphID));
00043       }
00044     }
00045     assert(pHistVector.size() == pHistMap.size());
00046   }
00047 
00048   namespace {
00049     typedef StringVector OneHistory;
00050     typedef std::set<OneHistory> Histories;
00051     typedef std::pair<std::string, BranchID> Product;
00052     typedef std::vector<Product> OrderedProducts;
00053     struct Sorter {
00054       explicit Sorter(Histories const& histories) : histories_(histories) {}
00055       bool operator()(Product const& a, Product const& b) const;
00056       Histories const histories_;
00057     };
00058     bool Sorter::operator()(Product const& a, Product const& b) const {
00059       assert (a != b);
00060       if (a.first == b.first) return false;
00061       bool mayBeTrue = false;
00062       for (Histories::const_iterator it = histories_.begin(), itEnd = histories_.end(); it != itEnd; ++it) {
00063         OneHistory::const_iterator itA = find_in_all(*it, a.first);
00064         if (itA == it->end()) continue;
00065         OneHistory::const_iterator itB = find_in_all(*it, b.first);
00066         if (itB == it->end()) continue;
00067         assert (itA != itB);
00068         if (itB < itA) {
00069           // process b precedes process a;
00070           return false;
00071         }
00072         // process a precedes process b;
00073         mayBeTrue = true;
00074       }
00075       return mayBeTrue;
00076     }
00077 
00078     void
00079     fillProcessConfiguration(ProcessHistoryVector const& pHistVec, ProcessConfigurationVector& procConfigVector) {
00080       procConfigVector.clear();
00081       std::set<ProcessConfiguration> pcset;
00082       for (ProcessHistoryVector::const_iterator it = pHistVec.begin(), itEnd = pHistVec.end();
00083           it != itEnd; ++it) {
00084         for (ProcessConfigurationVector::const_iterator i = it->begin(), iEnd = it->end();
00085             i != iEnd; ++i) {
00086           if (pcset.insert(*i).second) {
00087             procConfigVector.push_back(*i);
00088           }
00089         }
00090       }
00091     }
00092 
00093     void
00094     fillListsAndIndexes(ProductRegistry const& productRegistry,
00095                         ProcessHistoryMap const& pHistMap,
00096                         boost::shared_ptr<BranchIDLists const>& branchIDLists,
00097                         std::vector<BranchListIndex>& branchListIndexes) {
00098       OrderedProducts orderedProducts;
00099       std::set<std::string> processNamesThatProduced;
00100       ProductRegistry::ProductList const& prodList = productRegistry.productList();
00101       for (ProductRegistry::ProductList::const_iterator it = prodList.begin(), itEnd = prodList.end();
00102           it != itEnd; ++it) {
00103         if (it->second.branchType() == InEvent) {
00104           it->second.init();
00105           processNamesThatProduced.insert(it->second.processName());
00106           orderedProducts.emplace_back(it->second.processName(), it->second.branchID());
00107         }
00108       }
00109       assert (!orderedProducts.empty());
00110       Histories processHistories;
00111       size_t max = 0;
00112       for(ProcessHistoryMap::const_iterator it = pHistMap.begin(), itEnd = pHistMap.end(); it != itEnd; ++it) {
00113         ProcessHistory const& pHist = it->second;
00114         OneHistory processHistory;
00115         for(ProcessHistory::const_iterator i = pHist.begin(), iEnd = pHist.end(); i != iEnd; ++i) {
00116           if (processNamesThatProduced.find(i->processName()) != processNamesThatProduced.end()) {
00117             processHistory.push_back(i->processName());
00118           }
00119         }
00120         max = (processHistory.size() > max ? processHistory.size() : max);
00121         assert(max <= processNamesThatProduced.size());
00122         if (processHistory.size() > 1) {
00123           processHistories.insert(processHistory);
00124         }
00125       }
00126       stable_sort_all(orderedProducts, Sorter(processHistories));
00127 
00128       std::unique_ptr<BranchIDLists> pv(new BranchIDLists);
00129       std::unique_ptr<BranchIDList> p(new BranchIDList);
00130       std::string processName;
00131       BranchListIndex blix = 0;
00132       for (OrderedProducts::const_iterator it = orderedProducts.begin(), itEnd = orderedProducts.end(); it != itEnd; ++it) {
00133         if (it->first != processName) {
00134           if (!processName.empty()) {
00135             branchListIndexes.push_back(blix);
00136             ++blix;
00137             pv->push_back(std::move(*p));
00138             p.reset(new BranchIDList);
00139           }
00140           processName = it->first;
00141         }
00142         p->push_back(it->second.id());
00143       }
00144       branchListIndexes.push_back(blix);
00145       pv->push_back(std::move(*p));
00146       branchIDLists.reset(pv.release());
00147     }
00148   }
00149 
00150   ProvenanceAdaptor::ProvenanceAdaptor(
00151              ProductRegistry& productRegistry,
00152              ProcessHistoryMap& pHistMap,
00153              ProcessHistoryVector& pHistVector,
00154              ProcessConfigurationVector& procConfigVector,
00155              ParameterSetIdConverter const& parameterSetIdConverter,
00156              bool fullConversion) :
00157                 parameterSetIdConverter_(parameterSetIdConverter),
00158                 processHistoryIdConverter_(),
00159                 branchIDLists_(),
00160                 branchListIndexes_() {
00161     fixProcessHistory(pHistMap, pHistVector);
00162     fillProcessConfiguration(pHistVector, procConfigVector);
00163     if (fullConversion) {
00164       fillListsAndIndexes(productRegistry, pHistMap, branchIDLists_, branchListIndexes_);
00165     }
00166   }
00167 
00168   ProvenanceAdaptor::~ProvenanceAdaptor() {}
00169 
00170   ParameterSetID const&
00171   ProvenanceAdaptor::convertID(ParameterSetID const& oldID) const {
00172     ParameterSetIdConverter::const_iterator it = parameterSetIdConverter_.find(oldID);
00173     if (it == parameterSetIdConverter_.end()) {
00174       return oldID;
00175     }
00176     return it->second; 
00177   }
00178 
00179   ProcessHistoryID const&
00180   ProvenanceAdaptor::convertID(ProcessHistoryID const& oldID) const {
00181     ProcessHistoryIdConverter::const_iterator it = processHistoryIdConverter_.find(oldID);
00182     if (it == processHistoryIdConverter_.end()) {
00183       return oldID;
00184     }
00185     return it->second; 
00186   }
00187 
00188   boost::shared_ptr<BranchIDLists const>
00189   ProvenanceAdaptor::branchIDLists() const {
00190     return branchIDLists_;
00191   }
00192 
00193   void
00194   ProvenanceAdaptor::branchListIndexes(BranchListIndexes & indexes)  const {
00195     indexes = branchListIndexes_;
00196   }
00197 }