CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
AnythingToValueMap.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_TagAndProbe_interface_AnythingToValueMap_h
2 #define PhysicsTools_TagAndProbe_interface_AnythingToValueMap_h
3 
8 
12 
13 namespace pat { namespace helper {
14 
15  template<class Adaptor, class Collection = typename Adaptor::Collection, typename value_type = typename Adaptor::value_type>
17  public:
18  typedef typename edm::ValueMap<value_type> Map;
19  typedef typename Map::Filler MapFiller;
20  explicit AnythingToValueMap(const edm::ParameterSet & iConfig) :
21  failSilently_(iConfig.getUntrackedParameter<bool>("failSilently", false)),
22  src_(consumes<Collection>(iConfig.getParameter<edm::InputTag>("src"))),
23  adaptor_(iConfig,consumesCollector()) {
24  produces< Map >(adaptor_.label());
25  }
27 
28  virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup) ;
29 
30  private:
33  Adaptor adaptor_;
34  };
35 
36 template<class Adaptor, class Collection, typename value_type>
39  iEvent.getByToken(src_, handle);
40  if (handle.failedToGet() && failSilently_) return;
41 
42  bool adaptorOk = adaptor_.init(iEvent);
43  if ((!adaptorOk) && failSilently_) return;
44 
45  std::vector<value_type> ret;
46  ret.reserve(handle->size());
47 
48  adaptor_.run(*handle, ret);
49 
50  std::auto_ptr<Map> map(new Map());
51  MapFiller filler(*map);
52  filler.insert(handle, ret.begin(), ret.end());
53  filler.fill();
54  iEvent.put(map, adaptor_.label());
55 }
56 
57  template<class Adaptor, class Collection = typename Adaptor::Collection, typename value_type = typename Adaptor::value_type>
59  public:
60  typedef typename edm::ValueMap<value_type> Map;
61  typedef typename Map::Filler MapFiller;
62  explicit ManyThingsToValueMaps(const edm::ParameterSet & iConfig) :
63  failSilently_(iConfig.getUntrackedParameter<bool>("failSilently", false)),
64  src_(consumes<Collection>(iConfig.getParameter<edm::InputTag>("collection"))),
65  inputs_(iConfig.getParameter<std::vector<edm::InputTag> >("associations"))
66  {
67  for (std::vector<edm::InputTag>::const_iterator it = inputs_.begin(), ed = inputs_.end(); it != ed; ++it) {
68  adaptors_.push_back(Adaptor(*it, iConfig, consumesCollector()));
69  produces< Map >(adaptors_.back().label());
70  }
71  }
73 
74  virtual void produce(edm::Event & iEvent, const edm::EventSetup & iSetup) ;
75 
76  private:
79  std::vector<edm::InputTag> inputs_;
80  std::vector<Adaptor> adaptors_;
81  };
82 
83 template<class Adaptor, class Collection, typename value_type>
86  iEvent.getByToken(src_, handle);
87  if (handle.failedToGet() && failSilently_) return;
88 
89  std::vector<value_type> ret;
90  ret.reserve(handle->size());
91 
92  for (typename std::vector<Adaptor>::iterator it = adaptors_.begin(), ed = adaptors_.end(); it != ed; ++it) {
93  ret.clear();
94  if (it->run(iEvent, *handle, ret)) {
95  std::auto_ptr<Map> map(new Map());
96  MapFiller filler(*map);
97  filler.insert(handle, ret.begin(), ret.end());
98  filler.fill();
99  iEvent.put(map, it->label());
100  } else {
101  if (!failSilently_) throw cms::Exception("ManyThingsToValueMaps") << "Error in adapter " << it->label() << "\n";
102  }
103  }
104 }
105 
106 } } // namespace;
107 
108 
109 #endif
edm::ValueMap< value_type > Map
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
std::vector< edm::InputTag > inputs_
int iEvent
Definition: GenABIO.cc:230
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:115
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
tuple handle
Definition: patZpeak.py:22
bool failedToGet() const
Definition: HandleBase.h:79
AnythingToValueMap(const edm::ParameterSet &iConfig)
edm::EDGetTokenT< Collection > src_
edm::ValueMap< value_type > Map
edm::EDGetTokenT< Collection > src_
ManyThingsToValueMaps(const edm::ParameterSet &iConfig)
volatile std::atomic< bool > shutdown_flag false
virtual void produce(edm::Event &iEvent, const edm::EventSetup &iSetup)