CMS 3D CMS Logo

TopProjector.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_PFCandProducer_TopProjector_
2 #define PhysicsTools_PFCandProducer_TopProjector_
3 
4 // system include files
5 #include <iostream>
6 #include <memory>
7 #include <string>
8 
9 // user include files
13 
16 
18 
22 
25 
27 
29 
37 #include <iostream>
38 
39 
41 template < class Top, class Bottom>
42  class TopProjectorFwdPtrOverlap : public std::unary_function<edm::FwdPtr<Top>, bool > {
43 
44  public:
47 
48 
50 
51  explicit TopProjectorFwdPtrOverlap(edm::ParameterSet const & iConfig ){ bottom_ = 0;}
52 
53  inline void setBottom(BottomFwdPtr const & bottom ) { bottom_ = &bottom; }
54 
55  bool operator() ( TopFwdPtr const & top ) const{
56  bool topFwdGood = top.ptr().isNonnull() && top.ptr().isAvailable();
57  bool topBckGood = top.backPtr().isNonnull() && top.backPtr().isAvailable();
58  bool bottomFwdGood = bottom_->ptr().isNonnull() && bottom_->ptr().isAvailable();
59  bool bottomBckGood = bottom_->backPtr().isNonnull() && bottom_->backPtr().isAvailable();
60 
61  bool matched =
62  (topFwdGood && bottomFwdGood && top.ptr().refCore() == bottom_->ptr().refCore() && top.ptr().key() == bottom_->ptr().key()) ||
63  (topFwdGood && bottomBckGood && top.ptr().refCore() == bottom_->backPtr().refCore() && top.ptr().key() == bottom_->backPtr().key()) ||
64  (topBckGood && bottomFwdGood && top.backPtr().refCore() == bottom_->ptr().refCore() && top.backPtr().key() == bottom_->ptr().key()) ||
65  (topBckGood && bottomBckGood && top.backPtr().refCore() == bottom_->backPtr().refCore() && top.backPtr().key() == bottom_->backPtr().key())
66  ;
67  if ( !matched ) {
68  for ( unsigned isource = 0; isource < top->numberOfSourceCandidatePtrs(); ++isource ) {
69  reco::CandidatePtr const & topSrcPtr = top->sourceCandidatePtr(isource);
70  bool topSrcGood = topSrcPtr.isNonnull() && topSrcPtr.isAvailable();
71  if ( (topSrcGood && bottomFwdGood && topSrcPtr.refCore() == bottom_->ptr().refCore() && topSrcPtr.key() == bottom_->ptr().key())||
72  (topSrcGood && bottomBckGood && topSrcPtr.refCore() == bottom_->backPtr().refCore() && topSrcPtr.key() == bottom_->backPtr().key())
73  ) {
74  matched = true;
75  break;
76  }
77  }
78  }
79  if ( !matched ) {
80  for ( unsigned isource = 0; isource < (*bottom_)->numberOfSourceCandidatePtrs(); ++isource ) {
81  reco::CandidatePtr const & bottomSrcPtr = (*bottom_)->sourceCandidatePtr(isource);
82  bool bottomSrcGood = bottomSrcPtr.isNonnull() && bottomSrcPtr.isAvailable();
83  if ( (topFwdGood && bottomSrcGood && bottomSrcPtr.refCore() == top.ptr().refCore() && bottomSrcPtr.key() == top.ptr().key() )||
84  (topBckGood && bottomSrcGood && bottomSrcPtr.refCore() == top.backPtr().refCore() && bottomSrcPtr.key() == top.backPtr().key() )
85  ) {
86  matched = true;
87  break;
88  }
89  }
90  }
91 
92  return matched;
93 
94  }
95 
96  protected :
97  BottomFwdPtr const * bottom_;
98 
99 };
100 
101 
103 template < class Top, class Bottom>
104  class TopProjectorDeltaROverlap : public std::unary_function<edm::FwdPtr<Top>, bool > {
105 
106  public:
109 
112  deltaR2_( config.getParameter<double>("deltaR") ),
113  bottom_(0),bottomCPtr_(0),botEta_(-999.f),botPhi_(0.f)
114  {deltaR2_*=deltaR2_;}
115 
116 
117  inline void setBottom(BottomFwdPtr const & bottom ) {
118  bottom_ = &bottom; bottomCPtr_=&**bottom_;
119  botEta_ = bottomCPtr_->eta();
120  botPhi_ = bottomCPtr_->phi();
121  }
122 
123  bool operator() ( TopFwdPtr const & top ) const{
124  const Top& oTop = *top; float topEta = oTop.eta(); float topPhi = oTop.phi();
125  bool matched = reco::deltaR2( topEta, topPhi, botEta_, botPhi_) < deltaR2_;
126  return matched;
127  }
128 
129  protected :
130  double deltaR2_;
131  BottomFwdPtr const * bottom_;
132  const Bottom* bottomCPtr_;
133  float botEta_,botPhi_;
134 };
135 
136 template< class Top, class Bottom, class Matcher = TopProjectorFwdPtrOverlap<Top,Bottom> >
138 
139  public:
140 
141  typedef std::vector<Top> TopCollection;
144  typedef std::vector<TopFwdPtr> TopFwdPtrCollection;
146 
147  typedef std::vector<Bottom> BottomCollection;
152  typedef std::vector<BottomFwdPtr> BottomFwdPtrCollection;
154 
156 
158 
159 
160  void produce(edm::Event&, const edm::EventSetup&);
161 
162 
163  private:
166 
168  bool enable_;
169 
172 
176 
180 };
181 
182 
183 
184 
185 template< class Top, class Bottom, class Matcher >
187  match_(iConfig),
188  enable_(iConfig.getParameter<bool>("enable")) {
189  name_ = iConfig.getUntrackedParameter<std::string>("name","No Name");
190  inputTagTop_ = iConfig.getParameter<edm::InputTag>("topCollection");
191  tokenTop_ = consumes<TopFwdPtrCollection>(inputTagTop_);
192  inputTagBottom_ = iConfig.getParameter<edm::InputTag>("bottomCollection");
193  tokenBottom_ = consumes<BottomFwdPtrCollection>(inputTagBottom_);
194 
195  // will produce a collection of the unmasked candidates in the
196  // bottom collection
197  produces< BottomFwdPtrCollection > ();
198 }
199 
200 
201 template< class Top, class Bottom, class Matcher >
203  const edm::EventSetup& iSetup) {
204  // get the various collections
205 
206  // Access the masking collection
207  TopFwdPtrHandle tops;
208  iEvent.getByToken( tokenTop_, tops );
209  std::list< TopFwdPtr > topsList;
210 
211  for ( typename TopFwdPtrCollection::const_iterator ibegin = tops->begin(),
212  iend = tops->end(), i = ibegin; i != iend; ++i ) {
213  topsList.push_back( *i );
214  }
215 
216 
217 /* if( !tops.isValid() ) { */
218 /* std::ostringstream err; */
219 /* err<<"The top collection must be supplied."<<std::endl */
220 /* <<"It is now set to : "<<inputTagTop_<<std::endl; */
221 /* edm::LogError("PFPAT")<<err.str(); */
222 /* throw cms::Exception( "MissingProduct", err.str()); */
223 /* } */
224 
225 
226 
227 
228  // Access the collection to
229  // be masked by the other ones
230  BottomFwdPtrHandle bottoms;
231  iEvent.getByToken( tokenBottom_, bottoms );
232 
233 /* if( !bottoms.isValid() ) { */
234 /* std::ostringstream err; */
235 /* err<<"The bottom collection must be supplied."<<std::endl */
236 /* <<"It is now set to : "<<inputTagBottom_<<std::endl; */
237 /* edm::LogError("PFPAT")<<err.str(); */
238 /* throw cms::Exception( "MissingProduct", err.str()); */
239 /* } */
240 
241 
242  /* if(verbose_) { */
243  /* const edm::Provenance& topProv = iEvent.getProvenance(tops.id()); */
244  /* const edm::Provenance& bottomProv = iEvent.getProvenance(bottoms.id()); */
245 
246  /* edm::LogDebug("TopProjection")<<"Top projector: event "<<iEvent.id().event()<<std::endl; */
247  /* edm::LogDebug("TopProjection")<<"Inputs --------------------"<<std::endl; */
248  /* edm::LogDebug("TopProjection")<<"Top : " */
249  /* <<tops.id()<<"\t"<<tops->size()<<std::endl */
250  /* <<topProv.branchDescription()<<std::endl; */
251 
252  /* for(unsigned i=0; i<tops->size(); i++) { */
253  /* TopFwdPtr top = (*tops)[i]; */
254  /* edm::LogDebug("TopProjection") << "< " << i << " " << top.key() << " : " << *top << std::endl; */
255  /* } */
256 
257 
258  /* edm::LogDebug("TopProjection")<<"Bottom : " */
259  /* <<bottoms.id()<<"\t"<<bottoms->size()<<std::endl */
260  /* <<bottomProv.branchDescription()<<std::endl; */
261 
262  /* for(unsigned i=0; i<bottoms->size(); i++) { */
263  /* BottomFwdPtr bottom = (*bottoms)[i]; */
264  /* edm::LogDebug("TopProjection") << "> " << i << " " << bottom.key() << " : " << *bottom << std::endl; */
265  /* } */
266 
267  /* } */
268 
269 
270  // output collection of FwdPtrs to objects,
271  // selected from the Bottom collection
272  std::unique_ptr< BottomFwdPtrCollection >
273  pBottomFwdPtrOutput( new BottomFwdPtrCollection );
274 
275  LogDebug("TopProjection")<<" Remaining candidates in the bottom collection ------ ";
276 
277  for(typename BottomFwdPtrCollection::const_iterator i=bottoms->begin(),
278  iend = bottoms->end(), ibegin = i; i != iend; ++i) {
279 
280  BottomFwdPtr const & bottom = *i;
281  match_.setBottom(bottom);
282  typename std::list< TopFwdPtr >::iterator found = topsList.end();
283  if ( enable_ ) {
284  found = std::find_if( topsList.begin(), topsList.end(), match_ );
285  }
286 
287  // If this is masked in the top projection, we remove it.
288  if( found != topsList.end() ) {
289  LogDebug("TopProjection")<<"X "<<i-ibegin << **i;
290  topsList.erase(found);
291  continue;
292  }
293  // otherwise, we keep it.
294  else {
295  LogDebug("TopProjection")<<"O "<<i-ibegin << **i;
296  pBottomFwdPtrOutput->push_back( bottom );
297  }
298  }
299 
300  iEvent.put(std::move(pBottomFwdPtrOutput));
301 }
302 
303 
304 #endif
#define LogDebug(id)
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void produce(edm::Event &, const edm::EventSetup &)
Definition: TopProjector.h:202
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
Ptr< value_type > const & ptr() const
Definition: FwdPtr.h:122
edm::Handle< BottomFwdPtrCollection > BottomFwdPtrHandle
Definition: TopProjector.h:153
key_type key() const
Definition: Ptr.h:186
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:460
edm::FwdPtr< Bottom > BottomFwdPtr
Definition: TopProjector.h:151
std::vector< TopFwdPtr > TopFwdPtrCollection
Definition: TopProjector.h:144
bool isAvailable() const
Definition: Ptr.h:259
const Bottom * bottomCPtr_
Definition: TopProjector.h:132
edm::Ptr< Bottom > BottomPtr
Definition: TopProjector.h:149
BottomFwdPtr const * bottom_
Definition: TopProjector.h:97
std::vector< Bottom > BottomCollection
Definition: TopProjector.h:147
Definition: config.py:1
void setBottom(BottomFwdPtr const &bottom)
Definition: TopProjector.h:53
edm::Handle< TopFwdPtrCollection > TopFwdPtrHandle
Definition: TopProjector.h:145
edm::EDGetTokenT< BottomFwdPtrCollection > tokenBottom_
Definition: TopProjector.h:179
edm::FwdPtr< Top > TopFwdPtr
Definition: TopProjector.h:107
Ptr< value_type > const & backPtr() const
Definition: FwdPtr.h:123
TopProjector(const edm::ParameterSet &)
Definition: TopProjector.h:186
static const std::string Top
std::vector< Top > TopCollection
Definition: TopProjector.h:141
edm::FwdPtr< Top > TopFwdPtr
Definition: TopProjector.h:143
edm::FwdPtr< Bottom > BottomFwdPtr
Definition: TopProjector.h:46
int iEvent
Definition: GenABIO.cc:230
edm::FwdPtr< Bottom > BottomFwdPtr
Definition: TopProjector.h:108
Matcher match_
Matching method.
Definition: TopProjector.h:165
void setBottom(BottomFwdPtr const &bottom)
Definition: TopProjector.h:117
RefCore const & refCore() const
Definition: Ptr.h:190
double f[11][100]
This checks a slew of possible overlaps for FwdPtr<Candidate> and derivatives.
Definition: TopProjector.h:42
bool operator()(TopFwdPtr const &top) const
Definition: TopProjector.h:55
bool isNonnull() const
Checks for non-null.
Definition: Ptr.h:169
bool enable_
enable? if not, all candidates in the bottom collection are copied to the output collection ...
Definition: TopProjector.h:168
TopProjectorFwdPtrOverlap(edm::ParameterSet const &iConfig)
Definition: TopProjector.h:51
This checks matching based on delta R.
Definition: TopProjector.h:104
edm::InputTag inputTagBottom_
input tag for the masked collection.
Definition: TopProjector.h:178
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
edm::Ref< BottomCollection > BottomRef
Definition: TopProjector.h:150
std::string name_
name of the top projection
Definition: TopProjector.h:171
edm::FwdPtr< Top > TopFwdPtr
Definition: TopProjector.h:45
edm::InputTag inputTagTop_
input tag for the top (masking) collection
Definition: TopProjector.h:174
BottomFwdPtr const * bottom_
Definition: TopProjector.h:131
edm::EDGetTokenT< TopFwdPtrCollection > tokenTop_
Definition: TopProjector.h:175
edm::Handle< std::vector< Bottom > > BottomHandle
Definition: TopProjector.h:148
edm::Handle< std::vector< Top > > TopHandle
Definition: TopProjector.h:142
std::vector< BottomFwdPtr > BottomFwdPtrCollection
Definition: TopProjector.h:152
def move(src, dest)
Definition: eostools.py:510
TopProjectorDeltaROverlap(edm::ParameterSet const &config)
Definition: TopProjector.h:111