00001 #include <algorithm>
00002 #include <vector>
00003
00004 #include "FWCore/Sources/interface/DaqProvenanceHelper.h"
00005
00006 #include "DataFormats/Provenance/interface/BranchChildren.h"
00007 #include "DataFormats/Provenance/interface/BranchIDList.h"
00008 #include "DataFormats/Provenance/interface/ProcessConfigurationRegistry.h"
00009 #include "DataFormats/Provenance/interface/ProcessHistory.h"
00010 #include "DataFormats/Provenance/interface/ProcessHistoryRegistry.h"
00011 #include "DataFormats/Provenance/interface/ProductRegistry.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013 #include "FWCore/Utilities/interface/GetPassID.h"
00014 #include "FWCore/Utilities/interface/TypeWithDict.h"
00015 #include "FWCore/Version/interface/GetReleaseVersion.h"
00016
00017 namespace edm {
00018 DaqProvenanceHelper::DaqProvenanceHelper(TypeID const& rawDataType)
00019 : constBranchDescription_(BranchDescription(InEvent
00020 , "rawDataCollector"
00021
00022 , "LHC"
00023
00024 , "FEDRawDataCollection"
00025 , "FEDRawDataCollection"
00026 , ""
00027 , "DaqSource"
00028 , ParameterSetID()
00029 , TypeWithDict(rawDataType.typeInfo())
00030 , false))
00031 , dummyProvenance_(constBranchDescription_.branchID())
00032 , processParameterSet_()
00033 , oldProcessName_()
00034 , oldBranchID_()
00035 , newBranchID_()
00036 , oldProcessHistoryID_(nullptr)
00037 , phidMap_() {
00038
00039
00040
00041 std::string const& moduleLabel = constBranchDescription_.moduleLabel();
00042 std::string const& processName = constBranchDescription_.processName();
00043 std::string const& moduleName = constBranchDescription_.moduleName();
00044 typedef std::vector<std::string> vstring;
00045 vstring empty;
00046
00047 vstring modlbl;
00048 modlbl.reserve(1);
00049 modlbl.push_back(moduleLabel);
00050 processParameterSet_.addParameter("@all_sources", modlbl);
00051
00052 ParameterSet triggerPaths;
00053 triggerPaths.addParameter<vstring>("@trigger_paths", empty);
00054 processParameterSet_.addParameter<ParameterSet>("@trigger_paths", triggerPaths);
00055
00056 ParameterSet pseudoInput;
00057 pseudoInput.addParameter<std::string>("@module_edm_type", "Source");
00058 pseudoInput.addParameter<std::string>("@module_label", moduleLabel);
00059 pseudoInput.addParameter<std::string>("@module_type", moduleName);
00060 processParameterSet_.addParameter<ParameterSet>(moduleLabel, pseudoInput);
00061
00062 processParameterSet_.addParameter<vstring>("@all_esmodules", empty);
00063 processParameterSet_.addParameter<vstring>("@all_esprefers", empty);
00064 processParameterSet_.addParameter<vstring>("@all_essources", empty);
00065 processParameterSet_.addParameter<vstring>("@all_loopers", empty);
00066 processParameterSet_.addParameter<vstring>("@all_modules", empty);
00067 processParameterSet_.addParameter<vstring>("@end_paths", empty);
00068 processParameterSet_.addParameter<vstring>("@paths", empty);
00069 processParameterSet_.addParameter<std::string>("@process_name", processName);
00070
00071 processParameterSet_.registerIt();
00072
00073
00074 }
00075
00076 ProcessHistoryID
00077 DaqProvenanceHelper::daqInit(ProductRegistry& productRegistry) const {
00078
00079
00080 productRegistry.copyProduct(constBranchDescription_.me());
00081
00082
00083 ProcessHistory ph;
00084 ph.emplace_back(constBranchDescription_.processName(), processParameterSet_.id(), getReleaseVersion(), getPassID());
00085 ProcessConfiguration const& pc = ph.data().back();
00086 ProcessHistoryRegistry::instance()->insertMapped(ph);
00087
00088
00089 ProcessConfigurationRegistry::instance()->insertMapped(pc);
00090
00091
00092 return ph.id();
00093 }
00094
00095 bool
00096 DaqProvenanceHelper::matchProcesses(ProcessConfiguration const& newPC, ProcessHistory const& ph) const {
00097 for(auto const& pc : ph) {
00098 if(pc.processName() == oldProcessName_) {
00099 return(pc.releaseVersion() == newPC.releaseVersion() && pc.passID() == newPC.passID());
00100 }
00101 }
00102 return false;
00103 }
00104
00105 void
00106 DaqProvenanceHelper::fixMetaData(std::vector<ProcessConfiguration>& pcv, std::vector<ProcessHistory>& phv) {
00107 std::vector<ProcessConfiguration> newPCs;
00108 for(auto const& pc : pcv) {
00109 if(pc.processName() == oldProcessName_) {
00110 newPCs.emplace_back(constBranchDescription_.processName(),
00111 processParameterSet_.id(),
00112 pc.releaseVersion(), pc.passID());
00113 }
00114 }
00115 assert(!newPCs.empty());
00116 pcv.reserve(pcv.size() + newPCs.size());
00117 pcv.insert(pcv.end(), newPCs.begin(), newPCs.end());
00118
00119 for(auto& ph : phv) {
00120 for(auto const& newPC : newPCs) {
00121 if(matchProcesses(newPC, ph)) {
00122 ProcessHistoryID oldPHID = ph.id();
00123 ph.push_front(newPC);
00124 ProcessHistoryID newPHID = ph.id();
00125 phidMap_.insert(std::make_pair(oldPHID, newPHID));
00126 break;
00127 }
00128 }
00129 }
00130
00131 phv.reserve(phv.size() + newPCs.size());
00132 for(auto const& newPC : newPCs) {
00133 phv.emplace_back();
00134 phv.back().push_front(newPC);
00135 }
00136 }
00137
00138 void
00139 DaqProvenanceHelper::fixMetaData(std::vector<BranchID>& branchID) const {
00140 std::replace(branchID.begin(), branchID.end(), oldBranchID_, newBranchID_);
00141 }
00142
00143 void
00144 DaqProvenanceHelper::fixMetaData(BranchIDLists const& branchIDLists) const {
00145 BranchID::value_type oldID = oldBranchID_.id();
00146 BranchID::value_type newID = newBranchID_.id();
00147
00148 BranchIDLists& lists = const_cast<BranchIDLists&>(branchIDLists);
00149 for(auto& list : lists) {
00150 std::replace(list.begin(), list.end(), oldID, newID);
00151 }
00152 }
00153
00154 void
00155 DaqProvenanceHelper::fixMetaData(BranchChildren& branchChildren) const {
00156 typedef std::map<BranchID, std::set<BranchID> > BCMap;
00157
00158 BCMap& childLookup = const_cast<BCMap&>(branchChildren.childLookup());
00159
00160 {
00161 BCMap::iterator i = childLookup.find(oldBranchID_);
00162 if(i != childLookup.end()) {
00163 childLookup.insert(std::make_pair(newBranchID_, i->second));
00164 childLookup.erase(i);
00165 }
00166 }
00167
00168 for(auto& child : childLookup) {
00169 if(child.second.erase(oldBranchID_) != 0) {
00170 child.second.insert(newBranchID_);
00171 }
00172 }
00173 }
00174
00175
00176 ProcessHistoryID const&
00177 DaqProvenanceHelper::mapProcessHistoryID(ProcessHistoryID const& phid) {
00178 ProcessHistoryIDMap::const_iterator it = phidMap_.find(phid);
00179 assert(it != phidMap_.end());
00180 oldProcessHistoryID_ = &it->first;
00181 return it->second;
00182 }
00183
00184
00185 ParentageID const&
00186 DaqProvenanceHelper::mapParentageID(ParentageID const& parentageID) const {
00187 ParentageIDMap::const_iterator it = parentageIDMap_.find(parentageID);
00188 if(it == parentageIDMap_.end()) {
00189 return parentageID;
00190 }
00191 return it->second;
00192 }
00193
00194
00195 BranchID const&
00196 DaqProvenanceHelper::mapBranchID(BranchID const& branchID) const {
00197 return(branchID == oldBranchID_ ? newBranchID_ : branchID);
00198 }
00199 }