CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/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 
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 
00140   
00141 
00142   // Access the collection to
00143   // be masked by the other ones
00144   BottomHandle bottoms;
00145   iEvent.getByLabel(  inputTagBottom_, bottoms );
00146 
00147 /*   if( !bottoms.isValid() ) { */
00148 /*     std::ostringstream  err; */
00149 /*     err<<"The bottom collection must be supplied."<<std::endl */
00150 /*        <<"It is now set to : "<<inputTagBottom_<<std::endl; */
00151 /*     edm::LogError("PFPAT")<<err.str(); */
00152 /*     throw cms::Exception( "MissingProduct", err.str()); */
00153 /*   } */
00154 
00155  
00156   if(verbose_) {
00157     const edm::Provenance& topProv = iEvent.getProvenance(tops.id());
00158     const edm::Provenance& bottomProv = iEvent.getProvenance(bottoms.id());
00159 
00160     std::cout<<"Top projector: event "<<iEvent.id().event()<<std::endl;
00161     std::cout<<"Inputs --------------------"<<std::endl;
00162     std::cout<<"Top      :  "
00163         <<tops.id()<<"\t"<<tops->size()<<std::endl
00164         <<topProv.branchDescription()<<std::endl
00165         <<"Bottom   :  "
00166         <<bottoms.id()<<"\t"<<bottoms->size()<<std::endl
00167         <<bottomProv.branchDescription()<<std::endl;
00168   }
00169 
00170 
00171   // output collection of objects,
00172   // selected from the Bottom collection
00173 
00174   std::auto_ptr< BottomCollection >
00175     pBottomOutput( new BottomCollection );
00176   
00177   // mask for each bottom object.
00178   // at the beginning, all bottom objects are unmasked.
00179   std::vector<bool> masked( bottoms->size(), false);
00180     
00181   if( enable_ )
00182     processCollection( tops, bottoms, masked, name_.c_str(), iEvent );
00183 
00184   const BottomCollection& inCands = *bottoms;
00185 
00186   if(verbose_)
00187     std::cout<<" Remaining candidates in the bottom collection ------ "<<std::endl;
00188   
00189   for(unsigned i=0; i<inCands.size(); i++) {
00190     
00191     if(masked[i]) {
00192       if(verbose_)
00193         std::cout<<"X "<<i<<" "<<inCands[i]<<std::endl;
00194       continue;
00195     }
00196     else {
00197       if(verbose_)
00198         std::cout<<"O "<<i<<" "<<inCands[i]<<std::endl;
00199 
00200       pBottomOutput->push_back( inCands[i] );
00201       BottomPtr motherPtr( bottoms, i );
00202       pBottomOutput->back().setSourceCandidatePtr(motherPtr); 
00203     }
00204   }
00205 
00206   iEvent.put( pBottomOutput );
00207 }
00208 
00209 
00210 
00211 template< class Top, class Bottom > 
00212 void TopProjector< Top, Bottom >::processCollection( const edm::Handle< std::vector<Top> >& tops,
00213                                                      const edm::Handle< std::vector<Bottom> >& bottoms ,
00214                                                      std::vector<bool>& masked,
00215                                                      const char* objectName,
00216                                                      const edm::Event& iEvent) const {
00217 
00218   if( tops.isValid() && bottoms.isValid() ) {
00219     const std::vector<Top>& topCollection = *tops;
00220     
00221     if(verbose_) 
00222       std::cout<<"******* TopProjector "<<objectName
00223                <<" size = "<<topCollection.size()<<" ******** "<<std::endl;
00224     
00225     for(unsigned i=0; i<topCollection.size(); i++) {
00226       
00227       
00228       edm::Ptr<Top>   ptr( tops, i);
00229       reco::CandidatePtr basePtr( ptr );
00230  
00231       
00232       reco::CandidatePtrVector ancestors;
00233       ptrToAncestor( basePtr,
00234                      ancestors,
00235                      bottoms.id(), 
00236                      iEvent );
00237       
00238       if(verbose_) {
00239 /*      std::cout<<"\t"<<objectName<<" "<<i */
00240 /*               <<" pt,eta,phi = " */
00241 /*               <<basePtr->pt()<<"," */
00242 /*               <<basePtr->eta()<<"," */
00243 /*               <<basePtr->phi()<<std::endl; */
00244         
00245         std::cout<<"\t"<<topCollection[i]<<std::endl;
00246         printAncestors( ancestors, bottoms );
00247       }
00248   
00249       maskAncestors( ancestors, masked );
00250     }
00251   }
00252 
00253 }
00254 
00255 
00256 template< class Top, class Bottom >
00257 void  TopProjector<Top,Bottom>::printAncestors( const reco::CandidatePtrVector& ancestors,
00258                                       const edm::Handle< std::vector<Bottom> >& allPFCandidates ) const {
00259   
00260 
00261   std::vector<Bottom> pfs = *allPFCandidates;
00262 
00263   for(unsigned i=0; i<ancestors.size(); i++) {
00264 
00265     edm::ProductID id = ancestors[i].id();
00266     assert( id == allPFCandidates.id() );
00267  
00268     unsigned index = ancestors[i].key();
00269     assert( index < pfs.size() );
00270     
00271     std::cout<<"\t\t"<<pfs[index]<<std::endl;
00272   }
00273 }
00274 
00275 
00276 
00277 template< class Top, class Bottom >
00278 void
00279 TopProjector<Top,Bottom>::ptrToAncestor( reco::CandidatePtr candPtr,
00280                                          reco::CandidatePtrVector& ancestors,
00281                                          const edm::ProductID& ancestorsID,
00282                                          const edm::Event& iEvent) const {
00283 
00284   
00285   unsigned nSources = candPtr->numberOfSourceCandidatePtrs();
00286 
00287   if(verbose_) {
00288     const edm::Provenance& hereProv = iEvent.getProvenance(candPtr.id());
00289 
00290     std::cout<<"going down from "<<candPtr.id()
00291         <<"/"<<candPtr.key()<<" #mothers "<<nSources
00292         <<" ancestor id "<<ancestorsID<<std::endl
00293         <<hereProv.branchDescription()<<std::endl;
00294   }  
00295 
00296   for(unsigned i=0; i<nSources; i++) {
00297     
00298     reco::CandidatePtr mother = candPtr->sourceCandidatePtr(i);
00299     if( verbose_ ) {
00300 /*       const Provenance& motherProv = iEvent.getProvenance(mother.id()); */
00301       std::cout<<"  mother id "<<mother.id()<<std::endl;
00302     }
00303     if(  mother.id() != ancestorsID ) {
00304       // the mother is not yet at lowest level
00305       ptrToAncestor( mother, ancestors, ancestorsID, iEvent );
00306     }
00307     else {
00308       // adding mother to the list of ancestors
00309       ancestors.push_back( mother ); 
00310     }
00311   }
00312 }
00313 
00314 
00315 
00316 
00317 template< class Top, class Bottom >
00318 void TopProjector<Top,Bottom>::maskAncestors( const reco::CandidatePtrVector& ancestors,
00319                                          std::vector<bool>& masked ) const {
00320   
00321   for(unsigned i=0; i<ancestors.size(); i++) {
00322     unsigned index = ancestors[i].key();
00323     assert( index<masked.size() );
00324     
00325     //     if(verbose_) {
00326     //       ProductID id = ancestors[i].id();
00327     //       std::cout<<"\tmasking "<<index<<", ancestor "<<id<<"/"<<index<<std::endl;
00328     //     }
00329     masked[index] = true;
00330   }
00331 }
00332 
00333 
00334 #endif