CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC2/src/CommonTools/ParticleFlow/plugins/TopProjector.h

Go to the documentation of this file.
00001 #ifndef PhysicsTools_PFCandProducer_TopProjector_
00002 #define PhysicsTools_PFCandProducer_TopProjector_
00003 
00004 // system include files
00005 #include <iostream>
00006 #include <memory>
00007 #include <string>
00008 
00009 // user include files
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_ = &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_ = &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   // will produce a collection of the unmasked candidates in the 
00192   // bottom collection 
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   // get the various collections
00201 
00202   // Access the masking collection
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 /*   if( !tops.isValid() ) { */
00214 /*     std::ostringstream  err; */
00215 /*     err<<"The top collection must be supplied."<<std::endl */
00216 /*        <<"It is now set to : "<<inputTagTop_<<std::endl; */
00217 /*     edm::LogError("PFPAT")<<err.str(); */
00218 /*     throw cms::Exception( "MissingProduct", err.str()); */
00219 /*   } */
00220 
00221 
00222   
00223 
00224   // Access the collection to
00225   // be masked by the other ones
00226   BottomFwdPtrHandle bottoms;
00227   iEvent.getByLabel(  inputTagBottom_, bottoms );
00228 
00229 /*   if( !bottoms.isValid() ) { */
00230 /*     std::ostringstream  err; */
00231 /*     err<<"The bottom collection must be supplied."<<std::endl */
00232 /*        <<"It is now set to : "<<inputTagBottom_<<std::endl; */
00233 /*     edm::LogError("PFPAT")<<err.str(); */
00234 /*     throw cms::Exception( "MissingProduct", err.str()); */
00235 /*   } */
00236 
00237  
00238   /* if(verbose_) { */
00239   /*   const edm::Provenance& topProv = iEvent.getProvenance(tops.id()); */
00240   /*   const edm::Provenance& bottomProv = iEvent.getProvenance(bottoms.id()); */
00241 
00242   /*   edm::LogDebug("TopProjection")<<"Top projector: event "<<iEvent.id().event()<<std::endl; */
00243   /*   edm::LogDebug("TopProjection")<<"Inputs --------------------"<<std::endl; */
00244   /*   edm::LogDebug("TopProjection")<<"Top      :  " */
00245   /*         <<tops.id()<<"\t"<<tops->size()<<std::endl */
00246   /*         <<topProv.branchDescription()<<std::endl; */
00247 
00248   /*   for(unsigned i=0; i<tops->size(); i++) { */
00249   /*     TopFwdPtr top = (*tops)[i]; */
00250   /*     edm::LogDebug("TopProjection") << "< " << i << " " << top.key() << " : " << *top << std::endl; */
00251   /*   } */
00252 
00253 
00254   /*   edm::LogDebug("TopProjection")<<"Bottom   :  " */
00255   /*         <<bottoms.id()<<"\t"<<bottoms->size()<<std::endl */
00256   /*         <<bottomProv.branchDescription()<<std::endl; */
00257 
00258   /*   for(unsigned i=0; i<bottoms->size(); i++) { */
00259   /*     BottomFwdPtr bottom = (*bottoms)[i]; */
00260   /*     edm::LogDebug("TopProjection") << "> " << i << " " << bottom.key() << " : " << *bottom << std::endl; */
00261   /*   } */
00262 
00263   /* } */
00264 
00265 
00266   // output collection of FwdPtrs to objects,
00267   // selected from the Bottom collection
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     // If this is masked in the top projection, we remove it. 
00284     if( found != topsList.end() ) {
00285       LogDebug("TopProjection")<<"X "<<i-ibegin << **i;
00286       topsList.erase(found);
00287       continue;
00288     }
00289     // otherwise, we keep it. 
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