CMS 3D CMS Logo

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PATObjectSelector.cc
Go to the documentation of this file.
20 
21 #include <vector>
22 
23 namespace pat {
24 
26  public:
28  : srcToken_(consumes<edm::View<pat::Jet>>(params.getParameter<edm::InputTag>("src"))),
29  cut_(params.getParameter<std::string>("cut")),
30  cutLoose_(params.getParameter<std::string>("cutLoose")),
31  filter_(params.exists("filter") ? params.getParameter<bool>("filter") : false),
32  nLoose_(params.getParameter<unsigned>("nLoose")),
33  selector_(cut_),
35  produces<std::vector<pat::Jet>>();
36  produces<reco::GenJetCollection>("genJets");
37  produces<std::vector<CaloTower>>("caloTowers");
38  produces<reco::PFCandidateCollection>("pfCandidates");
39  produces<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
40  }
41 
42  ~PATJetSelector() override {}
43 
44  virtual void beginJob() {}
45  virtual void endJob() {}
46 
47  bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
48  auto patJets = std::make_unique<std::vector<Jet>>();
49 
50  auto genJetsOut = std::make_unique<reco::GenJetCollection>();
51  auto caloTowersOut = std::make_unique<std::vector<CaloTower>>();
52  auto pfCandidatesOut = std::make_unique<reco::PFCandidateCollection>();
53  auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
54 
55  edm::RefProd<reco::GenJetCollection> h_genJetsOut = iEvent.getRefBeforePut<reco::GenJetCollection>("genJets");
56  edm::RefProd<std::vector<CaloTower>> h_caloTowersOut =
57  iEvent.getRefBeforePut<std::vector<CaloTower>>("caloTowers");
59  iEvent.getRefBeforePut<reco::PFCandidateCollection>("pfCandidates");
61  iEvent.getRefBeforePut<edm::OwnVector<reco::BaseTagInfo>>("tagInfos");
62 
64  iEvent.getByToken(srcToken_, h_jets);
65 
66  unsigned nl = 0; // number of loose jets
67  // First loop over the products and make the secondary output collections
68  for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
69  ijet != iend;
70  ++ijet) {
71  bool selectedLoose = false;
72  if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
73  selectedLoose = true;
74  ++nl;
75  }
76 
77  if (selector_(*ijet) || selectedLoose) {
78  // Copy over the calo towers
79  for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
80  itowerEnd = ijet->caloTowersFwdPtr().end(),
81  itower = itowerBegin;
82  itower != itowerEnd;
83  ++itower) {
84  // Add to global calo tower list
85  caloTowersOut->push_back(**itower);
86  }
87 
88  // Copy over the pf candidates
89  for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
90  icandEnd = ijet->pfCandidatesFwdPtr().end(),
91  icand = icandBegin;
92  icand != icandEnd;
93  ++icand) {
94  // Add to global pf candidate list
95  pfCandidatesOut->push_back(**icand);
96  }
97 
98  // Copy the tag infos
99  for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
100  iinfoEnd = ijet->tagInfosFwdPtr().end(),
101  iinfo = iinfoBegin;
102  iinfo != iinfoEnd;
103  ++iinfo) {
104  // Add to global calo tower list
105  tagInfosOut->push_back(**iinfo);
106  }
107 
108  // Copy the gen jet
109  if (ijet->genJet() != nullptr) {
110  genJetsOut->push_back(*(ijet->genJet()));
111  }
112  }
113  }
114 
115  // Output the secondary collections.
116  edm::OrphanHandle<reco::GenJetCollection> oh_genJetsOut = iEvent.put(std::move(genJetsOut), "genJets");
117  edm::OrphanHandle<std::vector<CaloTower>> oh_caloTowersOut = iEvent.put(std::move(caloTowersOut), "caloTowers");
119  iEvent.put(std::move(pfCandidatesOut), "pfCandidates");
121  iEvent.put(std::move(tagInfosOut), "tagInfos");
122 
123  unsigned int caloTowerIndex = 0;
124  unsigned int pfCandidateIndex = 0;
125  unsigned int tagInfoIndex = 0;
126  unsigned int genJetIndex = 0;
127  // Now set the Ptrs with the orphan handles.
128  nl = 0; // Reset number of loose jets
129  for (edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(), iend = h_jets->end(), ijet = ibegin;
130  ijet != iend;
131  ++ijet) {
132  bool selectedLoose = false;
133  if (nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet)) {
134  selectedLoose = true;
135  ++nl;
136  }
137 
138  if (selector_(*ijet) || selectedLoose) {
139  // Add the jets that pass to the output collection
140  patJets->push_back(*ijet);
141 
142  // Copy over the calo towers
143  for (CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
144  itowerEnd = ijet->caloTowersFwdPtr().end(),
145  itower = itowerBegin;
146  itower != itowerEnd;
147  ++itower) {
148  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
149 
150  // ptr to "this" tower in the global list
151  edm::Ptr<CaloTower> outPtr(oh_caloTowersOut, caloTowerIndex);
152  patJets->back().updateFwdCaloTowerFwdPtr(itower - itowerBegin, // index of "this" tower in the jet
153  outPtr);
154  ++caloTowerIndex;
155  }
156 
157  // Copy over the pf candidates
158  for (reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
159  icandEnd = ijet->pfCandidatesFwdPtr().end(),
160  icand = icandBegin;
161  icand != icandEnd;
162  ++icand) {
163  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
164 
165  // ptr to "this" cand in the global list
166  edm::Ptr<reco::PFCandidate> outPtr(oh_pfCandidatesOut, pfCandidateIndex);
167  patJets->back().updateFwdPFCandidateFwdPtr(icand - icandBegin, // index of "this" tower in the jet
168  outPtr);
169  ++pfCandidateIndex;
170  }
171 
172  // Copy the tag infos
173  for (TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
174  iinfoEnd = ijet->tagInfosFwdPtr().end(),
175  iinfo = iinfoBegin;
176  iinfo != iinfoEnd;
177  ++iinfo) {
178  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
179 
180  // ptr to "this" info in the global list
181  edm::Ptr<reco::BaseTagInfo> outPtr(oh_tagInfosOut, tagInfoIndex);
182  patJets->back().updateFwdTagInfoFwdPtr(iinfo - iinfoBegin, // index of "this" tower in the jet
183  outPtr);
184  ++tagInfoIndex;
185  }
186 
187  // Copy the gen jet
188  if (ijet->genJet() != nullptr) {
189  patJets->back().updateFwdGenJetFwdRef(
190  edm::Ref<reco::GenJetCollection>(oh_genJetsOut, genJetIndex) // ref to "this" genjet in the global list
191  );
192  ++genJetIndex;
193  }
194  }
195  }
196 
197  // put genEvt in Event
198  bool pass = !patJets->empty();
199  iEvent.put(std::move(patJets));
200 
201  if (filter_)
202  return pass;
203  else
204  return true;
205  }
206 
209  iDesc.setComment("Energy Correlation Functions adder");
210  iDesc.add<edm::InputTag>("src", edm::InputTag("no default"))->setComment("input collection");
211  iDesc.add<std::string>("cut", "")->setComment("Jet selection.");
212  iDesc.add<std::string>("cutLoose", "")->setComment("Loose jet selection. Will keep nLoose loose jets.");
213  iDesc.add<bool>("filter", false)->setComment("Filter selection?");
214  iDesc.add<unsigned>("nLoose", 0)->setComment("Keep nLoose loose jets that satisfy cutLoose");
215  descriptions.add("PATJetSelector", iDesc);
216  }
217 
218  protected:
221  const std::string cutLoose_; // Cut to define loose jets.
222  const bool filter_;
223  const unsigned nLoose_; // If desired, keep nLoose loose jets.
225  const StringCutObjectSelector<Jet> selectorLoose_; // Selector for loose jets.
226  };
227 
228 } // namespace pat
229 
230 namespace pat {
231 
236  /* typedef SingleObjectSelector< */
237  /* std::vector<Jet>, */
238  /* StringCutObjectSelector<Jet> */
239  /* > PATJetSelector; */
242  typedef SingleObjectSelector<
243  std::vector<CompositeCandidate>,
244  StringCutObjectSelector<CompositeCandidate, true> // true => lazy parsing => get all methods of daughters
245  >
251 
252  typedef SingleObjectSelector<
253  std::vector<PackedCandidate>,
254  StringCutObjectSelector<PackedCandidate, true> // true => lazy parsing => get all methods of daughters
255  >
257 
280  typedef SingleObjectSelector<
281  std::vector<CompositeCandidate>,
282  StringCutObjectSelector<CompositeCandidate, true>, // true => lazy parsing => get all methods of daughters
285 
288 
290 
291 } // namespace pat
292 
293 using namespace pat;
294 
307 
317 
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< PackedCandidate >, StringCutObjectSelector< PackedCandidate, true > > PATPackedCandidateSelector
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:88
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)