CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/PhysicsTools/PFCandProducer/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 
00025 
00033 #include <iostream>
00034 
00035 
00036 template< class Top, class Bottom>
00037 class TopProjector : public edm::EDProducer {
00038 
00039  public:
00040 
00041   typedef std::vector<Top> TopCollection;
00042   typedef edm::Handle< std::vector<Top> > TopHandle;
00043   typedef std::vector<Bottom> BottomCollection;
00044   typedef edm::Handle< std::vector<Bottom> > BottomHandle;
00045   typedef edm::Ptr<Bottom> BottomPtr; 
00046 
00047   TopProjector(const edm::ParameterSet&);
00048 
00049   ~TopProjector() {};
00050 
00051   
00052   void produce(edm::Event&, const edm::EventSetup&);
00053 
00054 
00055  private:
00056  
00060   void
00061     ptrToAncestor( reco::CandidatePtr candRef,
00062                    reco::CandidatePtrVector& ancestors,
00063                    const edm::ProductID& ancestorsID,
00064                    const edm::Event& iEvent ) const;
00065 
00069   void maskAncestors( const reco::CandidatePtrVector& ancestors,
00070                       std::vector<bool>& masked ) const;
00071     
00072   
00073   void processCollection( const edm::Handle< std::vector<Top> >& handle,
00074                           const edm::Handle< std::vector<Bottom> >& allPFCandidates ,
00075                           std::vector<bool>& masked,
00076                           const char* objectName,
00077                           const edm::Event& iEvent ) const; 
00078 
00079   void  printAncestors( const reco::CandidatePtrVector& ancestors,
00080                         const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const;
00081 
00082 
00084   bool            enable_;
00085 
00087   bool            verbose_;
00088 
00090   std::string     name_;
00091  
00093   edm::InputTag   inputTagTop_;
00094 
00096   edm::InputTag   inputTagBottom_;
00097 };
00098 
00099 
00100 
00101 
00102 template< class Top, class Bottom>
00103 TopProjector< Top, Bottom >::TopProjector(const edm::ParameterSet& iConfig) : 
00104   enable_(iConfig.getParameter<bool>("enable")) {
00105 
00106   verbose_ = iConfig.getUntrackedParameter<bool>("verbose",false);
00107   name_ = iConfig.getUntrackedParameter<std::string>("name","No Name");
00108   inputTagTop_ = iConfig.getParameter<edm::InputTag>("topCollection");
00109   inputTagBottom_ = iConfig.getParameter<edm::InputTag>("bottomCollection");
00110 
00111   // will produce a collection of the unmasked candidates in the 
00112   // bottom collection 
00113   produces< std::vector<Bottom> >();
00114 }
00115 
00116 
00117 template< class Top, class Bottom >
00118 void TopProjector< Top, Bottom >::produce(edm::Event& iEvent,
00119                                           const edm::EventSetup& iSetup) {
00120   
00121   if( verbose_)
00122     std::cout<<"Event -------------------- "<<iEvent.id().event()<<std::endl;
00123   
00124   // get the various collections
00125 
00126   // Access the masking collection
00127   TopHandle tops;
00128   iEvent.getByLabel(  inputTagTop_, tops );
00129 
00130 
00131 /*   if( !tops.isValid() ) { */
00132 /*     std::ostringstream  err; */
00133 /*     err<<"The top collection must be supplied."<<std::endl */
00134 /*        <<"It is now set to : "<<inputTagTop_<<std::endl; */
00135 /*     edm::LogError("PFPAT")<<err.str(); */
00136 /*     throw cms::Exception( "MissingProduct", err.str()); */
00137 /*   } */
00138 
00139   edm::ProductID topsID = tops.id();
00140 
00141   
00142 
00143   // Access the collection to
00144   // be masked by the other ones
00145   BottomHandle bottoms;
00146   iEvent.getByLabel(  inputTagBottom_, bottoms );
00147 
00148 /*   if( !bottoms.isValid() ) { */
00149 /*     std::ostringstream  err; */
00150 /*     err<<"The bottom collection must be supplied."<<std::endl */
00151 /*        <<"It is now set to : "<<inputTagBottom_<<std::endl; */
00152 /*     edm::LogError("PFPAT")<<err.str(); */
00153 /*     throw cms::Exception( "MissingProduct", err.str()); */
00154 /*   } */
00155 
00156   edm::ProductID bottomsID = bottoms.id();
00157 
00158  
00159   if(verbose_) {
00160     const edm::Provenance& topProv = iEvent.getProvenance(tops.id());
00161     const edm::Provenance& bottomProv = iEvent.getProvenance(bottoms.id());
00162 
00163     std::cout<<"Top projector: event "<<iEvent.id().event()<<std::endl;
00164     std::cout<<"Inputs --------------------"<<std::endl;
00165     std::cout<<"Top      :  "
00166         <<tops.id()<<"\t"<<tops->size()<<std::endl
00167         <<topProv.branchDescription()<<std::endl
00168         <<"Bottom   :  "
00169         <<bottoms.id()<<"\t"<<bottoms->size()<<std::endl
00170         <<bottomProv.branchDescription()<<std::endl;
00171   }
00172 
00173 
00174   // output collection of objects,
00175   // selected from the Bottom collection
00176 
00177   std::auto_ptr< BottomCollection >
00178     pBottomOutput( new BottomCollection );
00179   
00180   // mask for each bottom object.
00181   // at the beginning, all bottom objects are unmasked.
00182   std::vector<bool> masked( bottoms->size(), false);
00183     
00184   if( enable_ )
00185     processCollection( tops, bottoms, masked, name_.c_str(), iEvent );
00186 
00187   const BottomCollection& inCands = *bottoms;
00188 
00189   if(verbose_)
00190     std::cout<<" Remaining candidates in the bottom collection ------ "<<std::endl;
00191   
00192   for(unsigned i=0; i<inCands.size(); i++) {
00193     
00194     if(masked[i]) {
00195       if(verbose_)
00196         std::cout<<"X "<<i<<" "<<inCands[i]<<std::endl;
00197       continue;
00198     }
00199     else {
00200       if(verbose_)
00201         std::cout<<"O "<<i<<" "<<inCands[i]<<std::endl;
00202 
00203       pBottomOutput->push_back( inCands[i] );
00204       BottomPtr motherPtr( bottoms, i );
00205       pBottomOutput->back().setSourceCandidatePtr(motherPtr); 
00206     }
00207   }
00208 
00209   iEvent.put( pBottomOutput );
00210 }
00211 
00212 
00213 
00214 template< class Top, class Bottom > 
00215 void TopProjector< Top, Bottom >::processCollection( const edm::Handle< std::vector<Top> >& tops,
00216                                                      const edm::Handle< std::vector<Bottom> >& bottoms ,
00217                                                      std::vector<bool>& masked,
00218                                                      const char* objectName,
00219                                                      const edm::Event& iEvent) const {
00220 
00221   if( tops.isValid() && bottoms.isValid() ) {
00222     const std::vector<Top>& topCollection = *tops;
00223     
00224     if(verbose_) 
00225       std::cout<<"******* TopProjector "<<objectName
00226                <<" size = "<<topCollection.size()<<" ******** "<<std::endl;
00227     
00228     for(unsigned i=0; i<topCollection.size(); i++) {
00229       
00230       
00231       edm::Ptr<Top>   ptr( tops, i);
00232       reco::CandidatePtr basePtr( ptr );
00233  
00234       
00235       reco::CandidatePtrVector ancestors;
00236       ptrToAncestor( basePtr,
00237                      ancestors,
00238                      bottoms.id(), 
00239                      iEvent );
00240       
00241       if(verbose_) {
00242 /*      std::cout<<"\t"<<objectName<<" "<<i */
00243 /*               <<" pt,eta,phi = " */
00244 /*               <<basePtr->pt()<<"," */
00245 /*               <<basePtr->eta()<<"," */
00246 /*               <<basePtr->phi()<<std::endl; */
00247         
00248         std::cout<<"\t"<<topCollection[i]<<std::endl;
00249         printAncestors( ancestors, bottoms );
00250       }
00251   
00252       maskAncestors( ancestors, masked );
00253     }
00254   }
00255 
00256 }
00257 
00258 
00259 template< class Top, class Bottom >
00260 void  TopProjector<Top,Bottom>::printAncestors( const reco::CandidatePtrVector& ancestors,
00261                                       const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const {
00262   
00263 
00264   std::vector<Bottom> pfs = *allPFCandidates;
00265 
00266   for(unsigned i=0; i<ancestors.size(); i++) {
00267 
00268     edm::ProductID id = ancestors[i].id();
00269     assert( id == allPFCandidates.id() );
00270  
00271     unsigned index = ancestors[i].key();
00272     assert( index < pfs.size() );
00273     
00274     std::cout<<"\t\t"<<pfs[index]<<std::endl;
00275   }
00276 }
00277 
00278 
00279 
00280 template< class Top, class Bottom >
00281 void
00282 TopProjector<Top,Bottom>::ptrToAncestor( reco::CandidatePtr candPtr,
00283                                          reco::CandidatePtrVector& ancestors,
00284                                          const edm::ProductID& ancestorsID,
00285                                          const edm::Event& iEvent) const {
00286 
00287   
00288   unsigned nSources = candPtr->numberOfSourceCandidatePtrs();
00289 
00290   if(verbose_) {
00291     const edm::Provenance& hereProv = iEvent.getProvenance(candPtr.id());
00292 
00293     std::cout<<"going down from "<<candPtr.id()
00294         <<"/"<<candPtr.key()<<" #mothers "<<nSources
00295         <<" ancestor id "<<ancestorsID<<std::endl
00296         <<hereProv.branchDescription()<<std::endl;
00297   }  
00298 
00299   for(unsigned i=0; i<nSources; i++) {
00300     
00301     reco::CandidatePtr mother = candPtr->sourceCandidatePtr(i);
00302     if( verbose_ ) {
00303 /*       const Provenance& motherProv = iEvent.getProvenance(mother.id()); */
00304       std::cout<<"  mother id "<<mother.id()<<std::endl;
00305     }
00306     if(  mother.id() != ancestorsID ) {
00307       // the mother is not yet at lowest level
00308       ptrToAncestor( mother, ancestors, ancestorsID, iEvent );
00309     }
00310     else {
00311       // adding mother to the list of ancestors
00312       ancestors.push_back( mother ); 
00313     }
00314   }
00315 }
00316 
00317 
00318 
00319 
00320 template< class Top, class Bottom >
00321 void TopProjector<Top,Bottom>::maskAncestors( const reco::CandidatePtrVector& ancestors,
00322                                          std::vector<bool>& masked ) const {
00323   
00324   for(unsigned i=0; i<ancestors.size(); i++) {
00325     unsigned index = ancestors[i].key();
00326     assert( index<masked.size() );
00327     
00328     //     if(verbose_) {
00329     //       ProductID id = ancestors[i].id();
00330     //       std::cout<<"\tmasking "<<index<<", ancestor "<<id<<"/"<<index<<std::endl;
00331     //     }
00332     masked[index] = true;
00333   }
00334 }
00335 
00336 
00337 #endif