CMS 3D CMS Logo

PATJetSelector.h
Go to the documentation of this file.
1 //
2 //
3 
4 #ifndef PhysicsTools_PatAlgos_PATJetSelector_h
5 #define PhysicsTools_PatAlgos_PATJetSelector_h
6 
8 
10 
15 
17 
18 
19 #include <vector>
20 
21 
22 namespace pat {
23 
25  public:
26 
27 
28  PATJetSelector( edm::ParameterSet const & params ) :
29  srcToken_(consumes<edm::View<pat::Jet> >( params.getParameter<edm::InputTag>("src") )),
30  cut_( params.getParameter<std::string>("cut") ),
31  cutLoose_( params.getParameter<std::string>("cutLoose") ),
32  filter_( params.exists("filter") ? params.getParameter<bool>("filter") : false ),
33  nLoose_( params.getParameter<unsigned>("nLoose") ),
34  selector_( cut_ ),
36  {
37  produces< std::vector<pat::Jet> >();
38  produces<reco::GenJetCollection> ("genJets");
39  produces<std::vector<CaloTower> > ("caloTowers");
40  produces<reco::PFCandidateCollection > ("pfCandidates");
41  produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
42  }
43 
44  ~PATJetSelector() override {}
45 
46  virtual void beginJob() {}
47  virtual void endJob() {}
48 
49  virtual bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) override {
50 
51  auto patJets = std::make_unique<std::vector<Jet>>();
52 
53  auto genJetsOut = std::make_unique<reco::GenJetCollection>();
54  auto caloTowersOut = std::make_unique<std::vector<CaloTower> >();
55  auto pfCandidatesOut = std::make_unique<reco::PFCandidateCollection>();
56  auto tagInfosOut = std::make_unique<edm::OwnVector<reco::BaseTagInfo>>();
57 
58 
60  edm::RefProd<std::vector<CaloTower> > h_caloTowersOut = iEvent.getRefBeforePut<std::vector<CaloTower> > ( "caloTowers" );
61  edm::RefProd<reco::PFCandidateCollection > h_pfCandidatesOut = iEvent.getRefBeforePut<reco::PFCandidateCollection > ( "pfCandidates" );
63 
65  iEvent.getByToken( srcToken_, h_jets );
66 
67  unsigned nl = 0; // number of loose jets
68  // First loop over the products and make the secondary output collections
69  for ( edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(),
70  iend = h_jets->end(), ijet = ibegin;
71  ijet != iend; ++ijet ) {
72 
73  bool selectedLoose = false;
74  if ( nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet) ) {
75  selectedLoose = true;
76  ++nl;
77  }
78 
79 
80  if ( selector_(*ijet) || selectedLoose ) {
81  // Copy over the calo towers
82  for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
83  itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
84  itower != itowerEnd; ++itower ) {
85  // Add to global calo tower list
86  caloTowersOut->push_back( **itower );
87  }
88 
89 
90  // Copy over the pf candidates
91  for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
92  icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
93  icand != icandEnd; ++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(), iinfo = iinfoBegin;
101  iinfo != iinfoEnd; ++iinfo ) {
102  // Add to global calo tower list
103  tagInfosOut->push_back( **iinfo );
104  }
105 
106  // Copy the gen jet
107  if ( ijet->genJet() != 0 ) {
108  genJetsOut->push_back( *(ijet->genJet()) );
109  }
110 
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" );
118  edm::OrphanHandle<reco::PFCandidateCollection> oh_pfCandidatesOut = iEvent.put(std::move(pfCandidatesOut), "pfCandidates" );
119  edm::OrphanHandle<edm::OwnVector<reco::BaseTagInfo> > oh_tagInfosOut = iEvent.put(std::move(tagInfosOut), "tagInfos" );
120 
121 
122 
123 
124 
125  unsigned int caloTowerIndex = 0;
126  unsigned int pfCandidateIndex = 0;
127  unsigned int tagInfoIndex = 0;
128  unsigned int genJetIndex = 0;
129  // Now set the Ptrs with the orphan handles.
130  nl = 0; // Reset number of loose jets
131  for ( edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(),
132  iend = h_jets->end(), ijet = ibegin;
133  ijet != iend; ++ijet ) {
134 
135  bool selectedLoose = false;
136  if ( nLoose_ > 0 && nl < nLoose_ && selectorLoose_(*ijet) ) {
137  selectedLoose = true;
138  ++nl;
139  }
140 
141  if ( selector_(*ijet) || selectedLoose ) {
142  // Add the jets that pass to the output collection
143  patJets->push_back( *ijet );
144 
145  // Copy over the calo towers
146  for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
147  itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
148  itower != itowerEnd; ++itower ) {
149  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
150 
151  // ptr to "this" tower in the global list
152  edm::Ptr<CaloTower> outPtr( oh_caloTowersOut, caloTowerIndex);
153  patJets->back().updateFwdCaloTowerFwdPtr( itower - itowerBegin,// index of "this" tower in the jet
154  outPtr
155  );
156  ++caloTowerIndex;
157  }
158 
159 
160  // Copy over the pf candidates
161  for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
162  icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
163  icand != icandEnd; ++icand ) {
164  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
165 
166  // ptr to "this" cand in the global list
167  edm::Ptr<reco::PFCandidate> outPtr( oh_pfCandidatesOut, pfCandidateIndex );
168  patJets->back().updateFwdPFCandidateFwdPtr( icand - icandBegin,// index of "this" tower in the jet
169  outPtr
170  );
171  ++pfCandidateIndex;
172  }
173 
174  // Copy the tag infos
175  for ( TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
176  iinfoEnd = ijet->tagInfosFwdPtr().end(), iinfo = iinfoBegin;
177  iinfo != iinfoEnd; ++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  );
185  ++tagInfoIndex;
186  }
187 
188  // Copy the gen jet
189  if ( ijet->genJet() != 0 ) {
190  patJets->back().updateFwdGenJetFwdRef( edm::Ref<reco::GenJetCollection>( oh_genJetsOut, genJetIndex) // ref to "this" genjet in the global list
191  );
192  ++genJetIndex;
193  }
194 
195  }
196  }
197 
198 
199  // put genEvt in Event
200  bool pass = patJets->size() > 0;
201  iEvent.put(std::move(patJets));
202 
203  if ( filter_ )
204  return pass;
205  else
206  return true;
207  }
208 
209 
210  static void fillDescriptions(edm::ConfigurationDescriptions & descriptions) {
212  iDesc.setComment("Energy Correlation Functions adder");
213  iDesc.add<edm::InputTag>("src", edm::InputTag("no default"))->setComment("input collection");
214  iDesc.add<std::string> ("cut", "")->setComment("Jet selection.");
215  iDesc.add<std::string> ("cutLoose", "")->setComment("Loose jet selection. Will keep nLoose loose jets.");
216  iDesc.add<bool> ("filter", false)->setComment("Filter selection?");
217  iDesc.add<unsigned>("nLoose", 0)->setComment("Keep nLoose loose jets that satisfy cutLoose");
218  descriptions.add("PATJetSelector", iDesc);
219  }
220 
221  protected:
224  const std::string cutLoose_; // Cut to define loose jets.
225  const bool filter_;
226  const unsigned nLoose_; // If desired, keep nLoose loose jets.
228  const StringCutObjectSelector<Jet> selectorLoose_; // Selector for loose jets.
229  };
230 
231 }
232 
233 
234 #endif
const StringCutObjectSelector< Jet > selector_
const StringCutObjectSelector< Jet > selectorLoose_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
virtual void endJob()
const std::string cutLoose_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
std::vector< GenJet > GenJetCollection
collection of GenJet objects
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Definition: HeavyIon.h:7
iterator begin()
Definition: OwnVector.h:244
void setComment(std::string const &value)
const std::string cut_
virtual void beginJob()
int iEvent
Definition: GenABIO.cc:230
~PATJetSelector() override
Definition: Jet.py:1
virtual bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup) override
const unsigned nLoose_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
RefProd< PROD > getRefBeforePut()
Definition: Event.h:147
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
iterator end()
Definition: OwnVector.h:249
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
const edm::EDGetTokenT< edm::View< pat::Jet > > srcToken_
def move(src, dest)
Definition: eostools.py:510
PATJetSelector(edm::ParameterSet const &params)