CMS 3D CMS Logo

PATObjectSelector.cc
Go to the documentation of this file.
19 
20 #include <vector>
21 
22 namespace pat {
23 
25  public:
27  : srcToken_(consumes<edm::View<pat::Jet>>(params.getParameter<edm::InputTag>("src"))),
28  cut_(params.getParameter<std::string>("cut")),
29  cutLoose_(params.getParameter<std::string>("cutLoose")),
30  filter_(params.exists("filter") ? params.getParameter<bool>("filter") : false),
31  nLoose_(params.getParameter<unsigned>("nLoose")),
32  selector_(cut_),
34  produces<std::vector<pat::Jet>>();
35  produces<reco::GenJetCollection>("genJets");
36  produces<std::vector<CaloTower>>("caloTowers");
37  produces<reco::PFCandidateCollection>("pfCandidates");
38  produces<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
39  }
40 
41  ~PATJetSelector() override {}
42 
43  virtual void beginJob() {}
44  virtual void endJob() {}
45 
46  bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
47  auto patJets = std::make_unique<std::vector<Jet>>();
48 
49  auto genJetsOut = std::make_unique<reco::GenJetCollection>();
50  auto caloTowersOut = std::make_unique<std::vector<CaloTower>>();
51  auto pfCandidatesOut = std::make_unique<reco::PFCandidateCollection>();
52  auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
53 
54  edm::RefProd<reco::GenJetCollection> h_genJetsOut = iEvent.getRefBeforePut<reco::GenJetCollection>("genJets");
55  edm::RefProd<std::vector<CaloTower>> h_caloTowersOut =
56  iEvent.getRefBeforePut<std::vector<CaloTower>>("caloTowers");
58  iEvent.getRefBeforePut<reco::PFCandidateCollection>("pfCandidates");
60  iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
61 
63  iEvent.getByToken(srcToken_, h_jets);
64 
65  unsigned nl = 0; // number of loose jets
66  // First loop over the products and make the secondary output collections
67  for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
68  ijet != iend;
69  ++ijet) {
70  bool selectedLoose = false;
71  if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
72  selectedLoose = true;
73  ++nl;
74  }
75 
76  if (selector_(*ijet) || selectedLoose) {
77  // Copy over the calo towers
78  for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
79  itowerEnd = ijet->caloTowersFwdPtr().end(),
80  itower = itowerBegin;
81  itower != itowerEnd;
82  ++itower) {
83  // Add to global calo tower list
84  caloTowersOut->push_back(**itower);
85  }
86 
87  // Copy over the pf candidates
88  for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
89  icandEnd = ijet->pfCandidatesFwdPtr().end(),
90  icand = icandBegin;
91  icand != icandEnd;
92  ++icand) {
93  // Add to global pf candidate list
94  pfCandidatesOut->push_back(**icand);
95  }
96 
97  // Copy the tag infos
98  for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
99  iinfoEnd = ijet->tagInfosFwdPtr().end(),
100  iinfo = iinfoBegin;
101  iinfo != iinfoEnd;
102  ++iinfo) {
103  // Add to global calo tower list
104  tagInfosOut->push_back(**iinfo);
105  }
106 
107  // Copy the gen jet
108  if (ijet->genJet() != nullptr) {
109  genJetsOut->push_back(*(ijet->genJet()));
110  }
111  }
112  }
113 
114  // Output the secondary collections.
115  edm::OrphanHandle<reco::GenJetCollection> oh_genJetsOut = iEvent.put(std::move(genJetsOut), "genJets");
116  edm::OrphanHandle<std::vector<CaloTower>> oh_caloTowersOut = iEvent.put(std::move(caloTowersOut), "caloTowers");
118  iEvent.put(std::move(pfCandidatesOut), "pfCandidates");
120  iEvent.put(std::move(tagInfosOut), "tagInfos");
121 
122  unsigned int caloTowerIndex = 0;
123  unsigned int pfCandidateIndex = 0;
124  unsigned int tagInfoIndex = 0;
125  unsigned int genJetIndex = 0;
126  // Now set the Ptrs with the orphan handles.
127  nl = 0; // Reset number of loose jets
128  for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
129  ijet != iend;
130  ++ijet) {
131  bool selectedLoose = false;
132  if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
133  selectedLoose = true;
134  ++nl;
135  }
136 
137  if (selector_(*ijet) || selectedLoose) {
138  // Add the jets that pass to the output collection
139  patJets->push_back(*ijet);
140 
141  // Copy over the calo towers
142  for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
143  itowerEnd = ijet->caloTowersFwdPtr().end(),
144  itower = itowerBegin;
145  itower != itowerEnd;
146  ++itower) {
147  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
148 
149  // ptr to "this" tower in the global list
150  edm::Ptr<CaloTower> outPtr(oh_caloTowersOut, caloTowerIndex);
151  patJets->back().updateFwdCaloTowerFwdPtr(itower - itowerBegin, // index of "this" tower in the jet
152  outPtr);
153  ++caloTowerIndex;
154  }
155 
156  // Copy over the pf candidates
157  for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
158  icandEnd = ijet->pfCandidatesFwdPtr().end(),
159  icand = icandBegin;
160  icand != icandEnd;
161  ++icand) {
162  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
163 
164  // ptr to "this" cand in the global list
165  edm::Ptr<reco::PFCandidate> outPtr(oh_pfCandidatesOut, pfCandidateIndex);
166  patJets->back().updateFwdPFCandidateFwdPtr(icand - icandBegin, // index of "this" tower in the jet
167  outPtr);
168  ++pfCandidateIndex;
169  }
170 
171  // Copy the tag infos
172  for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
173  iinfoEnd = ijet->tagInfosFwdPtr().end(),
174  iinfo = iinfoBegin;
175  iinfo != iinfoEnd;
176  ++iinfo) {
177  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
178 
179  // ptr to "this" info in the global list
180  edm::Ptr<reco::BaseTagInfo> outPtr(oh_tagInfosOut, tagInfoIndex);
181  patJets->back().updateFwdTagInfoFwdPtr(iinfo - iinfoBegin, // index of "this" tower in the jet
182  outPtr);
183  ++tagInfoIndex;
184  }
185 
186  // Copy the gen jet
187  if (ijet->genJet() != nullptr) {
188  patJets->back().updateFwdGenJetFwdRef(
189  edm::Ref<reco::GenJetCollection>(oh_genJetsOut, genJetIndex) // ref to "this" genjet in the global list
190  );
191  ++genJetIndex;
192  }
193  }
194  }
195 
196  // put genEvt in Event
197  bool pass = !patJets->empty();
198  iEvent.put(std::move(patJets));
199 
200  if (filter_)
201  return pass;
202  else
203  return true;
204  }
205 
208  iDesc.setComment("Energy Correlation Functions adder");
209  iDesc.add<edm::InputTag>("src", edm::InputTag("no default"))->setComment("input collection");
210  iDesc.add<std::string>("cut", "")->setComment("Jet selection.");
211  iDesc.add<std::string>("cutLoose", "")->setComment("Loose jet selection. Will keep nLoose loose jets.");
212  iDesc.add<bool>("filter", false)->setComment("Filter selection?");
213  iDesc.add<unsigned>("nLoose", 0)->setComment("Keep nLoose loose jets that satisfy cutLoose");
214  descriptions.add("PATJetSelector", iDesc);
215  }
216 
217  protected:
220  const std::string cutLoose_; // Cut to define loose jets.
221  const bool filter_;
222  const unsigned nLoose_; // If desired, keep nLoose loose jets.
224  const StringCutObjectSelector<Jet> selectorLoose_; // Selector for loose jets.
225  };
226 
227 } // namespace pat
228 
229 namespace pat {
230 
235  /* typedef SingleObjectSelector< */
236  /* std::vector<Jet>, */
237  /* StringCutObjectSelector<Jet> */
238  /* > PATJetSelector; */
241  typedef SingleObjectSelector<
242  std::vector<CompositeCandidate>,
243  StringCutObjectSelector<CompositeCandidate, true> // true => lazy parsing => get all methods of daughters
244  >
250 
273  typedef SingleObjectSelector<
274  std::vector<CompositeCandidate>,
275  StringCutObjectSelector<CompositeCandidate, true>, // true => lazy parsing => get all methods of daughters
278 
281 
283 
284 } // namespace pat
285 
286 using namespace pat;
287 
299 
309 
const StringCutObjectSelector< Jet > selector_
SingleObjectSelector< std::vector< Tau >, StringCutObjectSelector< Tau > > PATTauSelector
const StringCutObjectSelector< Jet > selectorLoose_
const std::string cutLoose_
SingleObjectSelector< std::vector< TriggerObjectStandAlone >, StringCutObjectSelector< TriggerObjectStandAlone > > PATTriggerObjectStandAloneSelector
std::vector< GenJet > GenJetCollection
collection of GenJet objects
SingleObjectSelector< std::vector< Photon >, StringCutObjectSelector< Photon >, edm::RefVector< std::vector< Photon > > > PATPhotonRefSelector
SingleObjectSelector< std::vector< Electron >, StringCutObjectSelector< Electron > > PATElectronSelector
SingleObjectSelector< std::vector< PFParticle >, StringCutObjectSelector< PFParticle > > PATPFParticleSelector
SingleObjectSelector< pat::IsolatedTrackCollection, StringCutObjectSelector< pat::IsolatedTrack > > IsoTrackSelector
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
iterator begin()
Definition: OwnVector.h:280
void setComment(std::string const &value)
const std::string cut_
virtual void beginJob()
int iEvent
Definition: GenABIO.cc:224
SingleObjectSelector< std::vector< CompositeCandidate >, StringCutObjectSelector< CompositeCandidate, true > > PATCompositeCandidateSelector
Definition: Jet.py:1
SingleObjectSelector< std::vector< Electron >, StringCutObjectSelector< Electron >, edm::RefVector< std::vector< Electron > > > PATElectronRefSelector
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
SingleObjectSelector< std::vector< GenericParticle >, StringCutObjectSelector< GenericParticle >, edm::RefVector< std::vector< GenericParticle > > > PATGenericParticleRefSelector
SingleObjectSelector< std::vector< CompositeCandidate >, StringCutObjectSelector< CompositeCandidate, true >, edm::RefVector< std::vector< CompositeCandidate > > > PATCompositeCandidateRefSelector
SingleObjectSelector< std::vector< GenericParticle >, StringCutObjectSelector< GenericParticle > > PATGenericParticleSelector
ParameterDescriptionBase * add(U const &iLabel, T const &value)
SingleObjectSelector< std::vector< Jet >, StringCutObjectSelector< Jet >, edm::RefVector< std::vector< Jet > > > PATJetRefSelector
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
iterator end()
Definition: OwnVector.h:285
ObjectCountFilter< pat::MuonCollection, StringCutObjectSelector< pat::Muon > >::type MuonRefPatCount
SingleObjectSelector< std::vector< Tau >, StringCutObjectSelector< Tau >, edm::RefVector< std::vector< Tau > > > PATTauRefSelector
void add(std::string const &label, ParameterSetDescription const &psetDescription)
SingleObjectSelector< std::vector< MET >, StringCutObjectSelector< MET >, edm::RefVector< std::vector< MET > > > PATMETRefSelector
SingleObjectSelector< std::vector< MET >, StringCutObjectSelector< MET > > PATMETSelector
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
SingleObjectSelector< std::vector< Photon >, StringCutObjectSelector< Photon > > PATPhotonSelector
SingleObjectSelector< std::vector< Muon >, StringCutObjectSelector< Muon > > PATMuonSelector
const edm::EDGetTokenT< edm::View< pat::Jet > > srcToken_
SingleObjectSelector< std::vector< PFParticle >, StringCutObjectSelector< PFParticle >, edm::RefVector< std::vector< PFParticle > > > PATPFParticleRefSelector
def move(src, dest)
Definition: eostools.py:511
SingleObjectSelector< std::vector< Muon >, StringCutObjectSelector< Muon >, edm::RefVector< std::vector< Muon > > > PATMuonRefSelector
PATJetSelector(edm::ParameterSet const &params)