Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-09-07 04:35:22

0001 #ifndef InputTagDistributor_H
0002 #define InputTagDistributor_H
0003 
0004 #include "FWCore/Framework/interface/ConsumesCollector.h"
0005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0006 #include "FWCore/Utilities/interface/InputTag.h"
0007 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0008 #include "FWCore/ServiceRegistry/interface/ActivityRegistry.h"
0009 #include "DataFormats/Provenance/interface/ModuleDescription.h"
0010 
0011 #include <map>
0012 #include <iostream>
0013 
0014 class InputTagDistributor {
0015 public:
0016   InputTagDistributor(const edm::ParameterSet& pset, edm::ConsumesCollector& iC) {
0017     std::vector<std::string> inpuTags = pset.getParameterNamesForType<edm::InputTag>();
0018     for (std::vector<std::string>::iterator i = inpuTags.begin(); i != inpuTags.end(); ++i)
0019       inputTags_[*i] = pset.getParameter<edm::InputTag>(*i);
0020   }
0021   const edm::InputTag& inputTag(std::string s) {
0022     std::map<std::string, edm::InputTag>::iterator findMe = inputTags_.find(s);
0023     if (findMe != inputTags_.end())
0024       return findMe->second;
0025     else {
0026       std::stringstream known;
0027       for (findMe = inputTags_.begin(); findMe != inputTags_.end(); ++findMe)
0028         known << findMe->first << " ---> " << findMe->second << "\n";
0029       edm::LogError("InputTagDistributor") << " cannot distribute InputTag: " << s << "\n knonw mapping is:\n"
0030                                            << known.str();
0031     }
0032     return inputTags_[s];
0033   }
0034 
0035 private:
0036   std::map<std::string, edm::InputTag> inputTags_;
0037 };
0038 
0039 class InputTagDistributorService {
0040 private:
0041   InputTagDistributor* SetInputTagDistributorUniqueInstance_;
0042   std::map<std::string, InputTagDistributor*> multipleInstance_;
0043 
0044 public:
0045   InputTagDistributorService(const edm::ParameterSet& iConfig, edm::ActivityRegistry& r) {
0046     r.watchPreModuleConstruction(this, &InputTagDistributorService::preModule);
0047   };
0048   ~InputTagDistributorService() {}
0049 
0050   InputTagDistributor& init(std::string user, const edm::ParameterSet& iConfig, edm::ConsumesCollector&& iC) {
0051     if (multipleInstance_.find(user) != multipleInstance_.end()) {
0052       std::cerr << user << " InputTagDistributor user already defined." << std::endl;
0053       throw;
0054     } else
0055       SetInputTagDistributorUniqueInstance_ = new InputTagDistributor(iConfig, iC);
0056     multipleInstance_[user] = SetInputTagDistributorUniqueInstance_;
0057     return (*SetInputTagDistributorUniqueInstance_);
0058   }
0059   void preModule(const edm::ModuleDescription& desc) {
0060     //does a set with the module name, except that it does not throw on non-configured modules
0061     std::map<std::string, InputTagDistributor*>::iterator f = multipleInstance_.find(desc.moduleLabel());
0062     if (f != multipleInstance_.end())
0063       SetInputTagDistributorUniqueInstance_ = f->second;
0064     else {
0065       //do not say anything but set it to zero to get a safe crash in get() if ever called
0066       SetInputTagDistributorUniqueInstance_ = nullptr;
0067     }
0068   }
0069   /*  InputTagDistributor & set(std::string & user){
0070     std::map<std::string, InputTagDistributor*>::iterator f=multipleInstance_.find(user);
0071     if (f == multipleInstance_.end()){
0072       std::cerr<<user<<" InputTagDistributor  user not defined. but it does not matter."<<std::endl;
0073       //      throw;}
0074     }
0075     else {
0076       SetInputTagDistributorUniqueInstance_ = f->second;
0077       return (*SetInputTagDistributorUniqueInstance_);
0078     }
0079     }*/
0080   InputTagDistributor& get() {
0081     if (!SetInputTagDistributorUniqueInstance_) {
0082       std::cerr << " SetInputTagDistributorUniqueInstance_ is not valid." << std::endl;
0083       throw;
0084     } else {
0085       return (*SetInputTagDistributorUniqueInstance_);
0086     }
0087   }
0088 
0089   edm::InputTag retrieve(std::string src, const edm::ParameterSet& pset) {
0090     //if used without setting any InputTag mapping
0091     if (multipleInstance_.empty())
0092       return pset.getParameter<edm::InputTag>(src);
0093 
0094     // some mapping was setup
0095     InputTagDistributor& which = get();
0096     std::map<std::string, InputTagDistributor*>::iterator inverseMap = multipleInstance_.begin();
0097     std::map<std::string, InputTagDistributor*>::iterator inverseMap_end = multipleInstance_.end();
0098     for (; inverseMap != inverseMap_end; ++inverseMap)
0099       if (inverseMap->second == &which)
0100         break;
0101     LogDebug("InputTagDistributor") << "typeCode: " << pset.retrieve(src).typeCode() << "\n"
0102                                     << pset.dump() << "\n"
0103                                     << "looking for: " << src << " by user: " << inverseMap->first << std::endl;
0104     std::string typeCode;
0105     typeCode += pset.retrieve(src).typeCode();
0106     std::string iTC;
0107     iTC += 'S';
0108     if (typeCode == iTC)
0109       return which.inputTag(pset.getParameter<std::string>(src));
0110     else
0111       return pset.getParameter<edm::InputTag>(src);
0112   }
0113 };
0114 
0115 #endif