CMS 3D CMS Logo

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