CMS 3D CMS Logo

CandCombiner.h
Go to the documentation of this file.
1 #ifndef CandAlgos_CandCombiner_h
2 #define CandAlgos_CandCombiner_h
3 
30 #include <string>
31 #include <vector>
32 #include <algorithm>
33 
34 namespace edm {
35  class ParameterSet;
36 }
37 
38 namespace reco {
39  namespace modules {
40 
41  struct RoleNames {
42  explicit RoleNames(const edm::ParameterSet& cfg) {
43  if (cfg.exists("name"))
44  name_ = cfg.getParameter<std::string>("name");
45  else
46  name_ = "";
47  if (cfg.exists("roles"))
48  roles_ = cfg.getParameter<std::vector<std::string> >("roles");
49  else
50  roles_ = std::vector<std::string>();
51  }
52  const std::vector<std::string> roles() const { return roles_; }
53  void set(reco::CompositeCandidate& c) const {
54  c.setName(name_);
55  c.setRoles(roles_);
56  c.applyRoles();
57  }
58 
59  private:
62  // Name of the roles
63  std::vector<std::string> roles_;
64  };
65 
69  using namespace cand::parser;
70  using namespace std;
71  string decay(cfg.getParameter<string>("decay"));
73  for (vector<ConjInfo>::iterator label = labels_.begin(); label != labels_.end(); ++label)
74  if (label->mode_ == ConjInfo::kPlus)
75  dauCharge_.push_back(1);
76  else if (label->mode_ == ConjInfo::kMinus)
77  dauCharge_.push_back(-1);
78  else
79  dauCharge_.push_back(0);
80  else
81  throw edm::Exception(edm::errors::Configuration, "failed to parse \"" + decay + "\"");
82 
83  int lists = labels_.size();
84  if (lists != 2 && lists != 3)
85  throw edm::Exception(edm::errors::LogicError, "invalid number of collections");
86  bool found;
87  const string setLongLived("setLongLived");
88  vector<string> vBoolParams = cfg.getParameterNamesForType<bool>();
89  found = find(vBoolParams.begin(), vBoolParams.end(), setLongLived) != vBoolParams.end();
90  if (found)
91  setLongLived_ = cfg.getParameter<bool>("setLongLived");
92  const string setMassConstraint("setMassConstraint");
93  found = find(vBoolParams.begin(), vBoolParams.end(), setMassConstraint) != vBoolParams.end();
94  if (found)
95  setMassConstraint_ = cfg.getParameter<bool>("setMassConstraint");
96  const string setPdgId("setPdgId");
97  vector<string> vIntParams = cfg.getParameterNamesForType<int>();
98  found = find(vIntParams.begin(), vIntParams.end(), setPdgId) != vIntParams.end();
99  if (found) {
100  setPdgId_ = true;
101  pdgId_ = cfg.getParameter<int>("setPdgId");
102  }
103  tokens_ =
104  edm::vector_transform(labels_, [this](ConjInfo const& cI) { return consumes<CandidateView>(cI.tag_); });
105  }
106 
107  protected:
109  std::vector<cand::parser::ConjInfo> labels_;
110  std::vector<edm::EDGetTokenT<CandidateView> > tokens_;
112  std::vector<int> dauCharge_;
118  bool setPdgId_;
120  int pdgId_;
121  };
122 
123  template <typename Selector,
124  typename PairSelector = AnyPairSelector,
125  typename Cloner = ::combiner::helpers::NormalClone,
127  typename Setup = AddFourMomenta,
130  public:
134  combinerInit_(consumesCollector()),
135  combiner_(reco::modules::make<Selector>(cfg, consumesCollector()),
137  Setup(cfg),
138  cfg.existsAs<bool>("checkCharge") ? cfg.getParameter<bool>("checkCharge") : true,
139  cfg.existsAs<bool>("checkOverlap") ? cfg.getParameter<bool>("checkOverlap") : true,
140  dauCharge_),
141  names_(cfg) {
142  produces<OutputCollection>();
143  }
145  ~CandCombiner() override {}
146 
147  private:
149  void produce(edm::Event& evt, const edm::EventSetup& es) override {
150  combinerInit_.init(combiner_.setup(), evt, es);
151  int n = labels_.size();
152  std::vector<edm::Handle<CandidateView> > colls(n);
153  for (int i = 0; i < n; ++i)
154  evt.getByToken(tokens_[i], colls[i]);
155 
156  std::unique_ptr<OutputCollection> out = combiner_.combine(colls, names_.roles());
158  typename OutputCollection::iterator i = out->begin(), e = out->end();
159  for (; i != e; ++i) {
160  names_.set(*i);
161  if (setLongLived_)
162  i->setLongLived();
163  if (setMassConstraint_)
164  i->setMassConstraint();
165  if (setPdgId_)
166  i->setPdgId(pdgId_);
167  }
168  }
169  evt.put(std::move(out));
170  }
174 
176  };
177 
178  } // namespace modules
179 } // namespace reco
180 
181 #endif
~CandCombiner() override
destructor
Definition: CandCombiner.h:145
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
std::vector< CompositeCandidate > CompositeCandidateCollection
collection of Candidate objects
const double kPlus
Definition: ParticleMasses.h:8
S make(const edm::ParameterSet &cfg)
int pdgId_
which pdgId to set
Definition: CandCombiner.h:120
void produce(edm::Event &evt, const edm::EventSetup &es) override
process an event
Definition: CandCombiner.h:149
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
auto vector_transform(std::vector< InputType > const &input, Function predicate) -> std::vector< typename std::remove_cv< typename std::remove_reference< decltype(predicate(input.front()))>::type >::type >
Definition: transform.h:11
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
edm::InputTag tag_
Definition: decayParser.h:36
char const * label
std::vector< std::string > roles_
Definition: CandCombiner.h:63
std::vector< edm::EDGetTokenT< CandidateView > > tokens_
Definition: CandCombiner.h:110
RoleNames(const edm::ParameterSet &cfg)
Definition: CandCombiner.h:42
const std::vector< std::string > roles() const
Definition: CandCombiner.h:52
Functor that operates on <T>
Definition: Selector.h:22
CandCombiner(const edm::ParameterSet &cfg)
constructor from parameter settypedef
Definition: CandCombiner.h:132
std::string name_
Name of this candidate.
Definition: CandCombiner.h:61
bool decayParser(const std::string &iValue, std::vector< ConjInfo > &oStrings)
bool setPdgId_
set pdgId flag
Definition: CandCombiner.h:118
Init combinerInit_
combiner utility
Definition: CandCombiner.h:172
bool setLongLived_
set long lived flag
Definition: CandCombiner.h:114
fixed size matrix
HLT enums.
std::vector< cand::parser::ConjInfo > labels_
label vector
Definition: CandCombiner.h:109
::CandCombiner< Selector, PairSelector, Cloner, OutputCollection, Setup > combiner_
Definition: CandCombiner.h:173
std::vector< int > dauCharge_
daughter charges
Definition: CandCombiner.h:112
def move(src, dest)
Definition: eostools.py:511
CandCombinerBase(const edm::ParameterSet &cfg)
Definition: CandCombiner.h:67
bool setMassConstraint_
set mass constraint flag
Definition: CandCombiner.h:116