Go to the documentation of this file.00001 #ifndef PhysicsTools_PFCandProducer_TopProjector_
00002 #define PhysicsTools_PFCandProducer_TopProjector_
00003
00004
00005 #include <iostream>
00006 #include <memory>
00007 #include <string>
00008
00009
00010 #include "FWCore/Framework/interface/Frameworkfwd.h"
00011 #include "FWCore/Framework/interface/EDProducer.h"
00012 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00013
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/MakerMacros.h"
00016
00017 #include "DataFormats/Provenance/interface/ProductID.h"
00018
00019 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
00020 #include "DataFormats/TauReco/interface/PFTau.h"
00021 #include "DataFormats/JetReco/interface/PFJet.h"
00022
00023 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00024 #include "DataFormats/Candidate/interface/OverlapChecker.h"
00025
00026 #include "DataFormats/Math/interface/deltaR.h"
00027
00028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00029
00037 #include <iostream>
00038
00039
00041 template < class Top, class Bottom>
00042 class TopProjectorFwdPtrOverlap : public std::unary_function<edm::FwdPtr<Top>, bool > {
00043
00044 public:
00045 typedef edm::FwdPtr<Top> TopFwdPtr;
00046 typedef edm::FwdPtr<Bottom> BottomFwdPtr;
00047
00048
00049 explicit TopProjectorFwdPtrOverlap( ){bottom_ = 0;}
00050
00051 explicit TopProjectorFwdPtrOverlap(edm::ParameterSet const & iConfig ){ bottom_ = 0;}
00052
00053 inline void setBottom(BottomFwdPtr const & bottom ) { bottom_ = ⊥ }
00054
00055 bool operator() ( TopFwdPtr const & top ) const{
00056 bool topFwdGood = top.ptr().isNonnull() && top.ptr().isAvailable();
00057 bool topBckGood = top.backPtr().isNonnull() && top.backPtr().isAvailable();
00058 bool bottomFwdGood = bottom_->ptr().isNonnull() && bottom_->ptr().isAvailable();
00059 bool bottomBckGood = bottom_->backPtr().isNonnull() && bottom_->backPtr().isAvailable();
00060
00061 bool matched =
00062 (topFwdGood && bottomFwdGood && top.ptr().refCore() == bottom_->ptr().refCore() && top.ptr().key() == bottom_->ptr().key()) ||
00063 (topFwdGood && bottomBckGood && top.ptr().refCore() == bottom_->backPtr().refCore() && top.ptr().key() == bottom_->backPtr().key()) ||
00064 (topBckGood && bottomFwdGood && top.backPtr().refCore() == bottom_->ptr().refCore() && top.backPtr().key() == bottom_->ptr().key()) ||
00065 (topBckGood && bottomBckGood && top.backPtr().refCore() == bottom_->backPtr().refCore() && top.backPtr().key() == bottom_->backPtr().key())
00066 ;
00067 if ( !matched ) {
00068 for ( unsigned isource = 0; isource < top->numberOfSourceCandidatePtrs(); ++isource ) {
00069 reco::CandidatePtr const & topSrcPtr = top->sourceCandidatePtr(isource);
00070 bool topSrcGood = topSrcPtr.isNonnull() && topSrcPtr.isAvailable();
00071 if ( (topSrcGood && bottomFwdGood && topSrcPtr.refCore() == bottom_->ptr().refCore() && topSrcPtr.key() == bottom_->ptr().key())||
00072 (topSrcGood && bottomBckGood && topSrcPtr.refCore() == bottom_->backPtr().refCore() && topSrcPtr.key() == bottom_->backPtr().key())
00073 ) {
00074 matched = true;
00075 break;
00076 }
00077 }
00078 }
00079 if ( !matched ) {
00080 for ( unsigned isource = 0; isource < (*bottom_)->numberOfSourceCandidatePtrs(); ++isource ) {
00081 reco::CandidatePtr const & bottomSrcPtr = (*bottom_)->sourceCandidatePtr(isource);
00082 bool bottomSrcGood = bottomSrcPtr.isNonnull() && bottomSrcPtr.isAvailable();
00083 if ( (topFwdGood && bottomSrcGood && bottomSrcPtr.refCore() == top.ptr().refCore() && bottomSrcPtr.key() == top.ptr().key() )||
00084 (topBckGood && bottomSrcGood && bottomSrcPtr.refCore() == top.backPtr().refCore() && bottomSrcPtr.key() == top.backPtr().key() )
00085 ) {
00086 matched = true;
00087 break;
00088 }
00089 }
00090 }
00091
00092 return matched;
00093
00094 }
00095
00096 protected :
00097 BottomFwdPtr const * bottom_;
00098
00099 };
00100
00101
00103 template < class Top, class Bottom>
00104 class TopProjectorDeltaROverlap : public std::unary_function<edm::FwdPtr<Top>, bool > {
00105
00106 public:
00107 typedef edm::FwdPtr<Top> TopFwdPtr;
00108 typedef edm::FwdPtr<Bottom> BottomFwdPtr;
00109
00110 explicit TopProjectorDeltaROverlap() {bottom_ = 0;}
00111 explicit TopProjectorDeltaROverlap(edm::ParameterSet const & config ) :
00112 deltaR2_( config.getParameter<double>("deltaR") ),
00113 bottom_(0),bottomCPtr_(0),botEta_(-999.f),botPhi_(0.f)
00114 {deltaR2_*=deltaR2_;}
00115
00116
00117 inline void setBottom(BottomFwdPtr const & bottom ) {
00118 bottom_ = ⊥ bottomCPtr_=&**bottom_;
00119 botEta_ = bottomCPtr_->eta();
00120 botPhi_ = bottomCPtr_->phi();
00121 }
00122
00123 bool operator() ( TopFwdPtr const & top ) const{
00124 const Top& oTop = *top; float topEta = oTop.eta(); float topPhi = oTop.phi();
00125 bool matched = reco::deltaR2( topEta, topPhi, botEta_, botPhi_) < deltaR2_;
00126 return matched;
00127 }
00128
00129 protected :
00130 double deltaR2_;
00131 BottomFwdPtr const * bottom_;
00132 const Bottom* bottomCPtr_;
00133 float botEta_,botPhi_;
00134 };
00135
00136 template< class Top, class Bottom, class Matcher = TopProjectorFwdPtrOverlap<Top,Bottom> >
00137 class TopProjector : public edm::EDProducer {
00138
00139 public:
00140
00141 typedef std::vector<Top> TopCollection;
00142 typedef edm::Handle< std::vector<Top> > TopHandle;
00143 typedef edm::FwdPtr<Top> TopFwdPtr;
00144 typedef std::vector<TopFwdPtr> TopFwdPtrCollection;
00145 typedef edm::Handle< TopFwdPtrCollection > TopFwdPtrHandle;
00146
00147 typedef std::vector<Bottom> BottomCollection;
00148 typedef edm::Handle< std::vector<Bottom> > BottomHandle;
00149 typedef edm::Ptr<Bottom> BottomPtr;
00150 typedef edm::Ref<BottomCollection> BottomRef;
00151 typedef edm::FwdPtr<Bottom> BottomFwdPtr;
00152 typedef std::vector<BottomFwdPtr> BottomFwdPtrCollection;
00153 typedef edm::Handle< BottomFwdPtrCollection > BottomFwdPtrHandle;
00154
00155 TopProjector(const edm::ParameterSet&);
00156
00157 ~TopProjector() {};
00158
00159
00160 void produce(edm::Event&, const edm::EventSetup&);
00161
00162
00163 private:
00165 Matcher match_;
00166
00168 bool enable_;
00169
00171 std::string name_;
00172
00174 edm::InputTag inputTagTop_;
00175
00177 edm::InputTag inputTagBottom_;
00178 };
00179
00180
00181
00182
00183 template< class Top, class Bottom, class Matcher >
00184 TopProjector< Top, Bottom, Matcher>::TopProjector(const edm::ParameterSet& iConfig) :
00185 match_(iConfig),
00186 enable_(iConfig.getParameter<bool>("enable")) {
00187 name_ = iConfig.getUntrackedParameter<std::string>("name","No Name");
00188 inputTagTop_ = iConfig.getParameter<edm::InputTag>("topCollection");
00189 inputTagBottom_ = iConfig.getParameter<edm::InputTag>("bottomCollection");
00190
00191
00192
00193 produces< BottomFwdPtrCollection > ();
00194 }
00195
00196
00197 template< class Top, class Bottom, class Matcher >
00198 void TopProjector< Top, Bottom, Matcher >::produce(edm::Event& iEvent,
00199 const edm::EventSetup& iSetup) {
00200
00201
00202
00203 TopFwdPtrHandle tops;
00204 iEvent.getByLabel( inputTagTop_, tops );
00205 std::list< TopFwdPtr > topsList;
00206
00207 for ( typename TopFwdPtrCollection::const_iterator ibegin = tops->begin(),
00208 iend = tops->end(), i = ibegin; i != iend; ++i ) {
00209 topsList.push_back( *i );
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 BottomFwdPtrHandle bottoms;
00227 iEvent.getByLabel( inputTagBottom_, bottoms );
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268 std::auto_ptr< BottomFwdPtrCollection >
00269 pBottomFwdPtrOutput( new BottomFwdPtrCollection );
00270
00271 LogDebug("TopProjection")<<" Remaining candidates in the bottom collection ------ ";
00272
00273 for(typename BottomFwdPtrCollection::const_iterator i=bottoms->begin(),
00274 iend = bottoms->end(), ibegin = i; i != iend; ++i) {
00275
00276 BottomFwdPtr const & bottom = *i;
00277 match_.setBottom(bottom);
00278 typename std::list< TopFwdPtr >::iterator found = topsList.end();
00279 if ( enable_ ) {
00280 found = std::find_if( topsList.begin(), topsList.end(), match_ );
00281 }
00282
00283
00284 if( found != topsList.end() ) {
00285 LogDebug("TopProjection")<<"X "<<i-ibegin << **i;
00286 topsList.erase(found);
00287 continue;
00288 }
00289
00290 else {
00291 LogDebug("TopProjection")<<"O "<<i-ibegin << **i;
00292 pBottomFwdPtrOutput->push_back( bottom );
00293 }
00294 }
00295
00296 iEvent.put( pBottomFwdPtrOutput );
00297 }
00298
00299
00300 #endif