1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
#ifndef UtilAlgos_Merger_h
#define UtilAlgos_Merger_h
/** \class Merger
*
* Merges an arbitrary number of collections
* into a single collection.
*
* Template parameters:
* - C : collection type
* - P : policy class that specifies how objects
* in the collection are are cloned
*
* \author Luca Lista, INFN
*
* \version $Revision: 1.2 $
*
* $Id: Merger.h,v 1.2 2010/02/20 20:55:21 wmtan Exp $
*
*/
#include "FWCore/Framework/interface/global/EDProducer.h"
#include "FWCore/Framework/interface/Event.h"
#include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
#include "FWCore/ParameterSet/interface/ParameterSetDescription.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/Utilities/interface/transform.h"
#include "FWCore/Utilities/interface/InputTag.h"
#include "DataFormats/Common/interface/CloneTrait.h"
#include <vector>
template <typename InputCollection,
typename OutputCollection = InputCollection,
typename P = typename edm::clonehelper::CloneTrait<InputCollection>::type>
class Merger : public edm::global::EDProducer<> {
public:
/// constructor from parameter set
explicit Merger(const edm::ParameterSet&);
/// destructor
~Merger() override;
static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
private:
/// process an event
void produce(edm::StreamID, edm::Event&, const edm::EventSetup&) const override;
/// vector of strings
typedef std::vector<edm::EDGetTokenT<InputCollection>> vtoken;
/// labels of the collections to be merged
vtoken srcToken_;
};
template <typename InputCollection, typename OutputCollection, typename P>
Merger<InputCollection, OutputCollection, P>::Merger(const edm::ParameterSet& par)
: srcToken_(edm::vector_transform(par.template getParameter<std::vector<edm::InputTag>>("src"),
[this](edm::InputTag const& tag) { return consumes<InputCollection>(tag); })) {
produces<OutputCollection>();
}
template <typename InputCollection, typename OutputCollection, typename P>
Merger<InputCollection, OutputCollection, P>::~Merger() {}
template <typename InputCollection, typename OutputCollection, typename P>
void Merger<InputCollection, OutputCollection, P>::produce(edm::StreamID,
edm::Event& evt,
const edm::EventSetup&) const {
std::unique_ptr<OutputCollection> coll(new OutputCollection);
for (typename vtoken::const_iterator s = srcToken_.begin(); s != srcToken_.end(); ++s) {
edm::Handle<InputCollection> h;
evt.getByToken(*s, h);
for (typename InputCollection::const_iterator c = h->begin(); c != h->end(); ++c) {
coll->push_back(P::clone(*c));
}
}
evt.put(std::move(coll));
}
template <typename InputCollection, typename OutputCollection, typename P>
void Merger<InputCollection, OutputCollection, P>::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
edm::ParameterSetDescription desc;
desc.add<std::vector<edm::InputTag>>("src",
{
edm::InputTag("collection1"),
edm::InputTag("collection2"),
});
descriptions.addWithDefaultLabel(desc);
}
#endif
|