CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
PATJetSelector.h
Go to the documentation of this file.
1 //
2 // $Id: PATJetSelector.h,v 1.6 2010/08/10 01:54:55 srappocc Exp $
3 //
4 
5 #ifndef PhysicsTools_PatAlgos_PATJetSelector_h
6 #define PhysicsTools_PatAlgos_PATJetSelector_h
7 
9 
11 
16 
18 
19 
20 #include <vector>
21 
22 
23 namespace pat {
24 
25  class PATJetSelector : public edm::EDFilter {
26  public:
27 
28 
29  PATJetSelector( edm::ParameterSet const & params ) :
30  edm::EDFilter( ),
31  src_( params.getParameter<edm::InputTag>("src") ),
32  cut_( params.getParameter<std::string>("cut") ),
33  filter_(false),
34  selector_( cut_ )
35  {
36  produces< std::vector<pat::Jet> >();
37  produces<reco::GenJetCollection> ("genJets");
38  produces<std::vector<CaloTower> > ("caloTowers");
39  produces<reco::PFCandidateCollection > ("pfCandidates");
40  produces<edm::OwnVector<reco::BaseTagInfo> > ("tagInfos");
41 
42  if ( params.exists("filter") ) {
43  filter_ = params.getParameter<bool>("filter");
44  }
45  }
46 
47  virtual ~PATJetSelector() {}
48 
49  virtual void beginJob() {}
50  virtual void endJob() {}
51 
52  virtual bool filter(edm::Event& iEvent, const edm::EventSetup& iSetup) {
53 
54  std::auto_ptr< std::vector<Jet> > patJets ( new std::vector<Jet>() );
55 
56  std::auto_ptr<reco::GenJetCollection > genJetsOut ( new reco::GenJetCollection() );
57  std::auto_ptr<std::vector<CaloTower> > caloTowersOut( new std::vector<CaloTower> () );
58  std::auto_ptr<reco::PFCandidateCollection > pfCandidatesOut( new reco::PFCandidateCollection() );
59  std::auto_ptr<edm::OwnVector<reco::BaseTagInfo> > tagInfosOut ( new edm::OwnVector<reco::BaseTagInfo>() );
60 
61 
63  edm::RefProd<std::vector<CaloTower> > h_caloTowersOut = iEvent.getRefBeforePut<std::vector<CaloTower> > ( "caloTowers" );
64  edm::RefProd<reco::PFCandidateCollection > h_pfCandidatesOut = iEvent.getRefBeforePut<reco::PFCandidateCollection > ( "pfCandidates" );
66 
68  iEvent.getByLabel( src_, h_jets );
69 
70  // First loop over the products and make the secondary output collections
71  for ( edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(),
72  iend = h_jets->end(), ijet = ibegin;
73  ijet != iend; ++ijet ) {
74 
75  // Check the selection
76  if ( selector_(*ijet) ) {
77  // Copy over the calo towers
78  for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
79  itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
80  itower != itowerEnd; ++itower ) {
81  // Add to global calo tower list
82  caloTowersOut->push_back( **itower );
83  }
84 
85 
86  // Copy over the pf candidates
87  for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
88  icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
89  icand != icandEnd; ++icand ) {
90  // Add to global pf candidate list
91  pfCandidatesOut->push_back( **icand );
92  }
93 
94  // Copy the tag infos
95  for ( TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
96  iinfoEnd = ijet->tagInfosFwdPtr().end(), iinfo = iinfoBegin;
97  iinfo != iinfoEnd; ++iinfo ) {
98  // Add to global calo tower list
99  tagInfosOut->push_back( **iinfo );
100  }
101 
102  // Copy the gen jet
103  if ( ijet->genJet() != 0 ) {
104  genJetsOut->push_back( *(ijet->genJet()) );
105  }
106 
107  }
108  }
109 
110 
111  // Output the secondary collections.
112  edm::OrphanHandle<reco::GenJetCollection> oh_genJetsOut = iEvent.put( genJetsOut, "genJets" );
113  edm::OrphanHandle<std::vector<CaloTower> > oh_caloTowersOut = iEvent.put( caloTowersOut, "caloTowers" );
114  edm::OrphanHandle<reco::PFCandidateCollection> oh_pfCandidatesOut = iEvent.put( pfCandidatesOut, "pfCandidates" );
115  edm::OrphanHandle<edm::OwnVector<reco::BaseTagInfo> > oh_tagInfosOut = iEvent.put( tagInfosOut, "tagInfos" );
116 
117 
118 
119 
120 
121  unsigned int caloTowerIndex = 0;
122  unsigned int pfCandidateIndex = 0;
123  unsigned int tagInfoIndex = 0;
124  unsigned int genJetIndex = 0;
125  // Now set the Ptrs with the orphan handles.
126  for ( edm::View<pat::Jet>::const_iterator ibegin = h_jets->begin(),
127  iend = h_jets->end(), ijet = ibegin;
128  ijet != iend; ++ijet ) {
129 
130  // Check the selection
131  if ( selector_(*ijet) ) {
132  // Add the jets that pass to the output collection
133  patJets->push_back( *ijet );
134 
135  // Copy over the calo towers
136  for ( CaloTowerFwdPtrVector::const_iterator itowerBegin = ijet->caloTowersFwdPtr().begin(),
137  itowerEnd = ijet->caloTowersFwdPtr().end(), itower = itowerBegin;
138  itower != itowerEnd; ++itower ) {
139  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
140 
141  // ptr to "this" tower in the global list
142  edm::Ptr<CaloTower> outPtr( oh_caloTowersOut, caloTowerIndex);
143  patJets->back().updateFwdCaloTowerFwdPtr( itower - itowerBegin,// index of "this" tower in the jet
144  outPtr
145  );
146  ++caloTowerIndex;
147  }
148 
149 
150  // Copy over the pf candidates
151  for ( reco::PFCandidateFwdPtrVector::const_iterator icandBegin = ijet->pfCandidatesFwdPtr().begin(),
152  icandEnd = ijet->pfCandidatesFwdPtr().end(), icand = icandBegin;
153  icand != icandEnd; ++icand ) {
154  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
155 
156  // ptr to "this" cand in the global list
157  edm::Ptr<reco::PFCandidate> outPtr( oh_pfCandidatesOut, pfCandidateIndex );
158  patJets->back().updateFwdPFCandidateFwdPtr( icand - icandBegin,// index of "this" tower in the jet
159  outPtr
160  );
161  ++pfCandidateIndex;
162  }
163 
164  // Copy the tag infos
165  for ( TagInfoFwdPtrCollection::const_iterator iinfoBegin = ijet->tagInfosFwdPtr().begin(),
166  iinfoEnd = ijet->tagInfosFwdPtr().end(), iinfo = iinfoBegin;
167  iinfo != iinfoEnd; ++iinfo ) {
168  // Update the "forward" bit of the FwdPtr to point at the new tower collection.
169 
170  // ptr to "this" info in the global list
171  edm::Ptr<reco::BaseTagInfo > outPtr( oh_tagInfosOut, tagInfoIndex );
172  patJets->back().updateFwdTagInfoFwdPtr( iinfo - iinfoBegin,// index of "this" tower in the jet
173  outPtr
174  );
175  ++tagInfoIndex;
176  }
177 
178  // Copy the gen jet
179  if ( ijet->genJet() != 0 ) {
180  patJets->back().updateFwdGenJetFwdRef( edm::Ref<reco::GenJetCollection>( oh_genJetsOut, genJetIndex) // ref to "this" genjet in the global list
181  );
182  ++genJetIndex;
183  }
184 
185  }
186  }
187 
188 
189  // put genEvt in Event
190  bool pass = patJets->size() > 0;
191  iEvent.put(patJets);
192 
193  if ( filter_ )
194  return pass;
195  else
196  return true;
197  }
198 
199  protected:
201  std::string cut_;
202  bool filter_;
204  };
205 
206 }
207 
208 
209 #endif
T getParameter(std::string const &) const
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:81
virtual void endJob()
std::vector< GenJet > GenJetCollection
collection of GenJet objects
bool exists(std::string const &parameterName) const
checks if a parameter exists
iterator begin()
Definition: OwnVector.h:227
virtual void beginJob()
int iEvent
Definition: GenABIO.cc:243
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
edm::InputTag src_
StringCutObjectSelector< Jet > selector_
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
RefProd< PROD > getRefBeforePut()
Definition: Event.h:97
virtual ~PATJetSelector()
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
iterator end()
Definition: OwnVector.h:232
virtual bool filter(edm::Event &iEvent, const edm::EventSetup &iSetup)
PATJetSelector(edm::ParameterSet const &params)